Filter
uncanny-automator
automator_pre_token_parsed
Filters the token parsing process before it occurs, allowing modification of parsed data, meta key, or arguments.
add_filter( 'automator_pre_token_parsed', $callback, 10, 3 );
Description
Fires before a token is parsed and its value is determined. Developers can use this filter to modify the parsed token value, the meta key associated with it, or arguments passed to the token parser. This offers a powerful way to influence how tokens are processed and what data they ultimately represent within the system.
Usage
add_filter( 'automator_pre_token_parsed', 'your_function_name', 10, 3 );
Parameters
-
$parsed(mixed) - This parameter contains the partially processed token data that is being prepared for substitution.
-
$meta_key(mixed) - This parameter contains the already parsed token data, which can be modified by the filter.
-
$token_args(mixed) - This parameter holds the meta key associated with the token being parsed.
Return Value
The filtered value.
Examples
add_filter(
'automator_pre_token_parsed',
function( $parsed, $meta_key, $token_args ) {
// Example: If the meta_key indicates a specific field related to user email,
// and the parsed content is empty, try to fetch the user's email directly
// from the token_args if available. This could be useful if a token
// placeholder was expected but not populated by default.
if ( 'user_email_field' === $meta_key && empty( $parsed ) ) {
if ( isset( $token_args['user_id'] ) && $token_args['user_id'] > 0 ) {
$user_info = get_userdata( $token_args['user_id'] );
if ( $user_info && ! empty( $user_info->user_email ) ) {
$parsed = $user_info->user_email;
}
}
}
// Always return the (potentially modified) parsed value.
return $parsed;
},
10, // Priority
3 // Accepted arguments
);
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/lib/recipe-parts/actions/trait-action-parser.php:183
public function maybe_parse_tokens( $user_id, $action_data, $recipe_id, $args ) {
// Allows the parser to know that this action is under a loop.
if ( isset( $action_data['loop'] ) ) {
$args['loop'] = $action_data['loop'];
}
if ( ! array_key_exists( 'meta', $action_data ) ) {
return $this->get_parsed();
}
$metas = $action_data['meta'];
if ( empty( $metas ) ) {
return $this->get_parsed();
}
$this->pre_parse();
// Pass the 'should_apply_extra_formatting_property' to the $args['action_meta'] key.
$args['action_meta']['should_apply_extra_formatting'] = $this->get_should_apply_extra_formatting();
foreach ( $metas as $meta_key => $meta_value ) {
$is_json_string = Automator()->utilities->is_json_string( $meta_value );
// Parse the json string.
if ( true === $is_json_string ) {
// @added 5.9
$parser_args = array(
'recipe_id' => $recipe_id,
'user_id' => $user_id,
'args' => $args,
);
$json_parsed_string = $this->parse_json_string( $meta_value, $parser_args );
// Only skip if self::parse_json_string is successful.
if ( ! is_wp_error( $json_parsed_string ) ) {
$this->set_parsed( $meta_key, $json_parsed_string );
continue;
}
}
// Prevents autop when text only contains a single line.
if ( ! Automator()->utilities->has_multiple_lines( $meta_value ) ) {
$this->set_parsed( $meta_key, Automator()->parse->text( $meta_value, $recipe_id, $user_id, $args ) );
continue;
}
if ( ! $this->is_valid_token( $meta_key, $meta_value ) ) {
$parsed = Automator()->parse->text( $meta_value, $recipe_id, $user_id, $args );
$this->set_parsed( $meta_key, $this->should_wpautop( $parsed, $meta_key ) );
continue;
}
$parsed = Automator()->parse->text( $meta_value, $recipe_id, $user_id, $args );
$token_args = array(
'user_id' => $user_id,
'action_data' => $action_data,
'recipe_id' => $recipe_id,
'args' => $args,
);
$parsed = apply_filters( 'automator_pre_token_parsed', $parsed, $meta_key, $token_args );
$should_process_shortcode = apply_filters( 'automator_trait_action_parser_maybe_parse_tokens_should_process_shortcode', true, $token_args );
if ( true === $should_process_shortcode && $this->is_do_shortcode() ) {
$parsed = do_shortcode( $parsed );
}
$parsed = apply_filters( 'automator_post_token_parsed', $this->should_wpautop( $parsed, $meta_key ), $meta_key, $token_args );
$this->set_parsed( $meta_key, $parsed );
}
return $this->get_parsed();
}