Filter uncanny-automator

automator_calculation_token_output

Filters the output of a calculation token before it's used within a recipe.

add_filter( 'automator_calculation_token_output', $callback, 10, 6 );

Description

Filters the output of a calculation token. Developers can modify the final calculated result before it's used in a recipe, or provide an error message if an exception occurs. This hook is called after the calculation is performed and before the result is returned.


Usage

add_filter( 'automator_calculation_token_output', 'your_function_name', 10, 6 );

Parameters

$return (mixed)
This parameter holds the value that will be returned after the calculation token has been processed.
$pieces (mixed)
This parameter contains the calculated value before it's returned by the function, allowing for modifications.
$recipe_id (mixed)
This parameter holds an array of arguments that were provided to the calculation token, potentially including data from the trigger and other sources.
$trigger_data (mixed)
This parameter contains the ID of the Automator recipe currently being processed.
$user_id (mixed)
This parameter contains the ID of the user who triggered the automation recipe.
$replace_args (mixed)
This parameter stores the ID of the user associated with the current automation recipe.

Return Value

The filtered value.


Examples

add_filter( 'automator_calculation_token_output', 'my_custom_calculation_token_output', 10, 6 );

/**
 * Custom function to modify the output of the automator_calculation_token_output filter.
 *
 * This example demonstrates how to append additional information to the calculation result
 * based on the recipe and trigger data.
 *
 * @param mixed $return The original calculated result.
 * @param mixed $pieces The pieces of the recipe.
 * @param int   $recipe_id The ID of the current recipe.
 * @param array $trigger_data Data associated with the trigger.
 * @param int   $user_id The ID of the user performing the action.
 * @param array $replace_args Arguments for replacement.
 * @return mixed The modified calculated result.
 */
function my_custom_calculation_token_output( $return, $pieces, $recipe_id, $trigger_data, $user_id, $replace_args ) {

    // Check if the recipe ID is one we want to modify
    if ( $recipe_id === 123 ) {

        // Check if the trigger data contains specific information
        if ( isset( $trigger_data['post_id'] ) && ! empty( $trigger_data['post_id'] ) ) {
            $post_title = get_the_title( $trigger_data['post_id'] );
            $return .= ' (Processed for post: ' . esc_html( $post_title ) . ')';
        }

        // Add a static suffix if it's a specific user
        if ( $user_id === 456 ) {
            $return .= ' - Admin Override';
        }
    }

    // Always return the modified or original value
    return $return;
}

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/integrations/advanced/tokens/calculation-token.php:164
src/core/classes/class-calculation-token.php:100

public function parse_integration_token( $return, $pieces, $recipe_id, $trigger_data, $user_id, $replace_args ) {
		$this->return       = $return;
		$this->pieces       = $pieces;
		$this->recipe_id    = $recipe_id;
		$this->trigger_data = $trigger_data;
		$this->user_id      = $user_id;
		$this->replace_args = $replace_args;

		$this->formula = $this->get_formula( $replace_args );

		$this->parsed_formula = $this->formula;
		$this->string_calc    = new ChrisKonnertzStringCalcStringCalc();

		try {

			$this->calculate();

			$return = $this->get_result();

		} catch ( Exception $e ) {
			$return = $e->getMessage();
		}

		return apply_filters( 'automator_calculation_token_output', $return, $pieces, $recipe_id, $trigger_data, $user_id, $replace_args );
	}

Scroll to Top