Filter uncanny-automator

automator_get_recipe_data_by_recipe_id

Filters the data retrieved for a specific recipe by its ID, allowing modification before it's returned.

add_filter( 'automator_get_recipe_data_by_recipe_id', $callback, 10, 2 );

Description

Fires after recipe data, including extra options for actions, is retrieved by ID. Developers can modify this data, particularly the 'extra_options' array, for actions and conditions before it's used. This is crucial for ensuring action options are correctly loaded, especially in admin or REST contexts.


Usage

add_filter( 'automator_get_recipe_data_by_recipe_id', 'your_function_name', 10, 2 );

Parameters

$recipe (mixed)
This parameter holds the recipe data fetched from the database or cache, which can be filtered by the `automator_get_recipe_data_by_recipe_id` filter.
$key (mixed)
This parameter represents the cached recipe data retrieved for a given recipe ID.

Return Value

The filtered value.


Examples

add_filter( 'automator_get_recipe_data_by_recipe_id', 'my_custom_automator_recipe_data', 10, 2 );

/**
 * Example filter to conditionally modify recipe data based on a specific recipe key.
 *
 * This function demonstrates how to hook into the 'automator_get_recipe_data_by_recipe_id'
 * filter to add or modify data associated with a recipe before it's cached and returned.
 * In this example, we'll add a custom flag to recipes with a specific key.
 *
 * @param array $recipe The recipe data array.
 * @param string $key The unique identifier (key) of the recipe.
 *
 * @return array The modified recipe data array.
 */
function my_custom_automator_recipe_data( $recipe, $key ) {

    // Define a specific recipe key we want to target.
    $target_recipe_key = 'my-special-automation-key';

    // Check if the current recipe key matches our target.
    if ( $key === $target_recipe_key ) {

        // Add a custom flag to the recipe data.
        // This could be used later for conditional logic within the automator.
        if ( isset( $recipe[ $key ] ) && is_array( $recipe[ $key ] ) ) {
            $recipe[ $key ]['custom_processing_flag'] = true;
            // You could also modify existing data, e.g.,
            // $recipe[ $key ]['title'] = 'Modified: ' . $recipe[ $key ]['title'];
        }
    }

    // It's crucial to return the modified (or unmodified) $recipe array.
    return $recipe;
}

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/class-automator-functions.php:1017

public function get_recipe_data_by_recipe_id( $recipe_id = null, $force_new = false ) {

		$recipes_loops = Automator()->loop_db()->fetch_all_recipes_loops();

		if ( null === $recipe_id ) {
			return array();
		}

		$key    = 'automator_recipe_data_of_' . $recipe_id;
		$recipe = Automator()->cache->get( $key );

		if ( ! empty( $recipe ) && false === $force_new ) {
			return $recipe;
		}

		$recipe  = array();
		$recipes = get_post( $recipe_id );
		if ( ! $recipes ) {
			return array();
		}

		$cached = Automator()->cache->get( 'get_recipe_type' );

		$is_recipe_completed           = $this->is_recipe_completed( $recipe_id );
		$key                           = $recipe_id;
		$recipe[ $key ]['ID']          = $recipe_id;
		$recipe[ $key ]['post_status'] = $recipes->post_status;
		$recipe[ $key ]['recipe_type'] = isset( $cached[ $recipe_id ] ) ? $cached[ $recipe_id ] : $this->utilities->get_recipe_type( $recipe_id );

		$triggers_array             = array();
		$triggers                   = $this->get_recipe_data( AUTOMATOR_POST_TYPE_TRIGGER, $recipe_id, $triggers_array );
		$recipe[ $key ]['triggers'] = $triggers;

		$action_array = array();
		$actions      = $this->get_recipe_data( AUTOMATOR_POST_TYPE_ACTION, $recipe_id, $action_array );

		/**
		 * Add loops inside the actions. This is a temporary solution and must be removed in the future.
		 *
		 * Adding loop actions as top level actions.
		 *
		 * @since 5.0
		 */
		$loop_actions = Automator()->loop_db()->find_recipe_loops_actions( $recipe_id, true, true, $recipes_loops );

		if ( ! empty( $loop_actions ) ) {
			$actions = array_merge( $actions, $loop_actions );
		}

		$recipe[ $key ]['actions'] = $actions;

		$closure_array              = array();
		$closures                   = $this->get_recipe_data( AUTOMATOR_POST_TYPE_CLOSURE, $recipe_id, $closure_array );
		$recipe[ $key ]['closures'] = $closures;

		$recipe[ $key ]['completed_by_current_user'] = $is_recipe_completed;

		// extra_options is consumed only by the recipe editor JS to populate static
		// dropdowns. At execution time, the Fields Resolver reads the stored copy from
		// uap_options (written during the last editor load). Skip the expensive callbacks
		// on non-admin requests to avoid unnecessary API calls and DB queries.
		// Note: is_admin() is false for REST API requests, so we also check for Automator
		// REST requests to ensure newly added actions get their options cached.
		// On the frontend execution path the Fields_Resolver lazy-builds any missing
		// per-item schema cache directly — see Fields_Resolver::get_cached_extra_options().
		$recipe[ $key ]['extra_options'] = ( is_admin() || is_automator_rest_request() || is_mcp_rest_request() )
			? $this->load_extra_options( $recipe[ $key ] )
			: array();

		$recipe = apply_filters( 'automator_get_recipe_data_by_recipe_id', $recipe, $key );

		Automator()->cache->set( $key, $recipe );

		return $recipe;
	}

Internal Usage

Found in uncanny-automator-pro/src/core/classes/actions-conditions.php:82:

add_filter( 'automator_get_recipe_data_by_recipe_id', array( $this, 'add_to_recipes_object' ), 10, 2 );
Scroll to Top