automator_action_tokens_field_token_value
Filters the value of a token for an Automator action, allowing modification before it's used in an automation.
add_filter( 'automator_action_tokens_field_token_value', $callback, 10, 3 );
Description
Filters the value of a token retrieved from action meta. Developers can use this hook to modify or dynamically generate token values before they are used in automations. This hook fires after the token value is fetched but before it's processed further. The provided parameters allow access to the action's meta data, the action ID, and the overall process arguments.
Usage
add_filter( 'automator_action_tokens_field_token_value', 'your_function_name', 10, 3 );
Parameters
-
$db_action_meta(mixed) - This parameter contains the metadata associated with the action from the database.
-
$action_id(mixed) - This parameter contains the meta data associated with the action in the database.
-
$process_args(mixed) - This parameter represents the unique identifier of the specific action within the automation recipe.
Return Value
The filtered value.
Examples
/**
* Example: Format a phone number token if it exists in the action meta.
*
* This callback function intercepts the 'automator_action_tokens_field_token_value'
* filter to modify the value of a specific token. In this example, we're checking
* if the token represents a phone number and, if so, formatting it according to
* a specific standard before it's used.
*
* @param array $db_action_meta The serialized action meta data.
* @param int $action_id The ID of the action.
* @param array $process_args Arguments passed to the process.
*
* @return array|mixed The modified action meta, or the original if no changes are made.
*/
add_filter( 'automator_action_tokens_field_token_value', function( $db_action_meta, $action_id, $process_args ) {
// Example: Assume we have a specific token key we want to modify, e.g., 'phone_number'.
// In a real scenario, you'd likely have a more robust way to identify the token.
// For demonstration, we'll simulate checking for a key within the $db_action_meta array.
$target_meta_key = 'user_phone_number_field'; // Replace with your actual token's meta key.
// Check if the target meta key exists in the action meta.
if ( isset( $db_action_meta[ $target_meta_key ] ) ) {
$original_phone_number = $db_action_meta[ $target_meta_key ];
// Basic validation and formatting for a US phone number.
// This is a simplified example; real-world phone number formatting can be complex.
$formatted_phone_number = preg_replace( '/D/', '', $original_phone_number ); // Remove non-numeric characters.
if ( strlen( $formatted_phone_number ) === 10 ) {
// Format as (XXX) XXX-XXXX
$formatted_phone_number = '(' . substr( $formatted_phone_number, 0, 3 ) . ') ' . substr( $formatted_phone_number, 3, 3 ) . '-' . substr( $formatted_phone_number, 6, 4 );
} elseif ( strlen( $formatted_phone_number ) === 11 && substr( $formatted_phone_number, 0, 1 ) === '1' ) {
// Format as +1 (XXX) XXX-XXXX for US numbers starting with 1
$formatted_phone_number = '+1 (' . substr( $formatted_phone_number, 1, 3 ) . ') ' . substr( $formatted_phone_number, 4, 3 ) . '-' . substr( $formatted_phone_number, 7, 4 );
}
// Update the value in the action meta.
$db_action_meta[ $target_meta_key ] = $formatted_phone_number;
}
// Always return the (potentially modified) meta data.
return $db_action_meta;
}, 10, 3 );
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/core/services/recipe/action/token/parser.php:298
private function get_field_value( $action_log_id = 0, $action_meta_key = '', $args = array(), $process_args = array(), $action_id = null ) {
$value = '';
$db_action_meta = Automator()->db->action->get_meta( $action_log_id, 'metas' );
$action_meta = apply_filters( 'automator_action_tokens_field_token_value', (array) maybe_unserialize( $db_action_meta ), $action_id, $process_args );
// Decide whether to split the meta key with parts or go with meta key. Supports repeater field.
$is_4th_part_correctly_separated = $this->is_correctly_separated( explode( '|', $action_meta_key ) );
// Assume, it's a repeater field.
if ( $is_4th_part_correctly_separated ) {
list( $option_code, $index, $field_code ) = explode( '|', $action_meta_key );
// Since $action_meta_key is now something like `GF_FIELDS|1GF_COLUMN_NAME`.
// We should revert it back to actual $option_code.
$action_meta_key = $option_code;
}
$value = $this->find_token_value( $action_meta_key, $action_meta );
if ( false !== $value ) {
$value = $this->handle_custom_value( $value, $action_meta );
}
// If its a repeater field and the value is JSON, get the requested field code from index.
$meta_value = isset( $value->meta_value ) ? $value->meta_value : null;
$repeater_fields = is_string( $meta_value ) ? json_decode( $meta_value, true ) : $meta_value;
if ( $is_4th_part_correctly_separated && ! empty( $repeater_fields ) ) {
return isset( $repeater_fields[ $index ][ $field_code ] ) ? $repeater_fields[ $index ][ $field_code ] : '';
}
// Handle non-repeater JSON values from field.
if ( Automator()->utilities->is_json_string( $meta_value ) ) {
$decoded = json_decode( $meta_value, true );
// Repeater field is classified as group 1.
if ( 'g1' === Array_Group_Classifier::classify_array( $decoded ) ) {
return wp_json_encode( $decoded );
}
return join( ', ', $decoded );
}
return ! empty( $meta_value ) ? $meta_value : '';
}
Internal Usage
Found in uncanny-automator-pro/src/core/loops/process-hooks-callbacks.php:62:
add_filter( 'automator_action_tokens_field_token_value', array( $this, 'parse_action_tokens_fields' ), 10, 3 );