Filter uncanny-automator

automator_recipe_throttler_can_execute

Filters whether a recipe can execute, allowing modification of the execution logic based on various conditions.

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

Description

Fires before a recipe's throttler check. Developers can filter this hook to bypass or modify the default throttler execution logic, enabling custom rate limiting or execution conditions for recipes. The `$throttler` parameter contains the throttler object, and `$filter_args` provides context like `user_id` and `recipe_id`.


Usage

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

Parameters

$throttler (mixed)
This parameter contains an instance of the `Throttler` class, which is used to manage and determine execution limits for recipes.
$filter_args (mixed)
This parameter contains an instance of the `Throttler` class, which is used to manage and check recipe execution throttling.

Return Value

The filtered value.


Examples

/**
 * Prevent a specific recipe from executing if the user has a custom meta key set.
 *
 * This example shows how to intercept the throttler execution for a specific recipe
 * and prevent it based on custom user meta.
 *
 * @param bool  $can_execute The original result of $throttler->can_execute().
 * @param array $filter_args An array containing 'throttler', 'data', 'user_id', and 'recipe_id'.
 *
 * @return bool Whether the recipe should be allowed to execute (true) or not (false).
 */
function my_custom_recipe_throttler_override( $can_execute, $filter_args ) {
    // Only apply this logic to a specific recipe ID. Replace 'YOUR_RECIPE_ID_HERE' with the actual ID.
    $target_recipe_id = 'YOUR_RECIPE_ID_HERE';

    if ( $filter_args['recipe_id'] === $target_recipe_id ) {
        $user_id = $filter_args['user_id'];

        // Check if the user has a specific meta key indicating they should not run this recipe.
        // Replace 'my_custom_prevent_recipe_meta' with your actual meta key.
        $prevent_execution = get_user_meta( $user_id, 'my_custom_prevent_recipe_meta', true );

        // If the meta key exists and its value is 'yes' (or any truthy value you define),
        // then we want to prevent execution, so we return false.
        if ( ! empty( $prevent_execution ) && $prevent_execution === 'yes' ) {
            return false; // Prevent execution
        }
    }

    // If it's not the target recipe, or the meta condition isn't met,
    // return the original $can_execute value.
    return $can_execute;
}
add_filter( 'automator_recipe_throttler_can_execute', 'my_custom_recipe_throttler_override', 10, 2 );

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:2705

public function is_recipe_throttled( int $recipe_id, int $user_id ) {

		$data = (array) get_post_meta( $recipe_id, 'field_recipe_throttle', true );

		try {
			$throttler = new ServicesRecipeProcessThrottler( $recipe_id, $data );

			$filter_args = array(
				'throttler' => $throttler,
				'data'      => $data,
				'user_id'   => $user_id,
				'recipe_id' => $recipe_id,
			);

			// Allow people to override the throttler execution and introduce their own logic on runtime.
			$can_execute = apply_filters(
				'automator_recipe_throttler_can_execute',
				$throttler->can_execute( $user_id ),
				$filter_args
			);

			// If the recipe can execute, return false because the recipe is not throttled.
			if ( $can_execute ) {
				return false;
			}
		} catch ( Exception $e ) {
			// Log the error.
			automator_log( 'Error creating throttler: ' . $e->getMessage(), 'error' );
			// Return false because the recipe can't be throttled due to an error.
			return false;
		}

		// Otherwise, return true.
		return true;
	}

Scroll to Top