Filter uncanny-automator

automator_parse_inner_token

Filters a parsed token during the automator inner token processing stage.

add_filter( 'automator_parse_inner_token', $callback, 10, 4 );

Description

Filters the parsed output of an inner token before it's fully processed. This hook allows developers to modify how specific tokens within a larger string are interpreted, enabling custom token parsing or manipulation of their values. It's primarily used internally for legacy token parsing.


Usage

add_filter( 'automator_parse_inner_token', 'your_function_name', 10, 4 );

Parameters

$parsed (mixed)
This parameter represents the processed token value after it has been parsed.
$token (mixed)
This parameter contains the result of previously parsed parts of the token.
$pieces (mixed)
The `$token` parameter contains the token string itself that is being parsed.
$args (mixed)
This parameter contains any additional arguments passed to the token.

Return Value

The filtered value.


Examples

/**
 * Example of using the automator_parse_inner_token filter to modify a token's parsed value.
 * This example will append "-processed" to any token that represents a post ID.
 *
 * @param mixed $parsed  The already parsed token value.
 * @param mixed $token   The raw token string (e.g., '[[post_id]]').
 * @param mixed $pieces  An array containing the token parts.
 * @param mixed $args    Additional arguments passed to the parsing function.
 *
 * @return mixed The potentially modified parsed token value.
 */
function my_automator_process_post_id_tokens( $parsed, $token, $pieces, $args ) {
	// Check if the token is likely a post ID and if the parsed value is numeric (a typical post ID).
	// This is a simplified check; a more robust solution might involve checking $token directly for common post ID patterns.
	if ( is_numeric( $parsed ) && str_contains( $token, 'post_id' ) ) {
		// Append "-processed" to the post ID. This is a demonstration; real-world logic might
		// fetch post data and modify based on that, or add specific metadata.
		$parsed = $parsed . '-processed';
	}

	// Always return the (potentially modified) parsed value.
	return $parsed;
}

// Add the filter to WordPress.
// The second argument '4' indicates that this callback function accepts 4 arguments.
add_filter( 'automator_parse_inner_token', 'my_automator_process_post_id_tokens', 10, 4 );

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/deprecated/legacy-token-parser.php:970
src/core/deprecated/legacy-token-parser.php:1006
src/core/lib/utilities/class-automator-input-parser.php:961
src/core/lib/utilities/class-automator-input-parser.php:997

public function parse_inner_token_post_id_part( $pieces, $args ) {
		if ( ! array_key_exists( 1, $pieces ) ) {
			return $pieces;
		}
		if ( ! preg_match( '/[[(.+)]]/', $pieces[1], $arr ) ) {
			return $pieces;
		}
		$recipe_id    = $args['recipe_id'];
		$user_id      = $args['user_id'];
		$trigger_args = $args;
		unset( $trigger_args['pieces'] );
		$token     = str_replace(
			array( '[', ']', ';' ),
			array(
				'{',
				'}',
				':',
			),
			$arr[0]
		);
		$parsed    = $this->text( $token, $recipe_id, $user_id, $trigger_args );
		$pieces[1] = apply_filters( 'automator_parse_inner_token', $parsed, $token, $pieces, $args );

		return $pieces;
	}


Scroll to Top