Action uncanny-automator

automator_before_recipe_completed

Fires before a recipe is marked as completed, providing access to recipe, user, and log details.

add_action( 'automator_before_recipe_completed', $callback, 10, 4 );

Description

Fires before a recipe's execution is marked as completed. This hook allows developers to intervene and modify data or perform custom actions just before the recipe's final status is set. It provides the recipe ID, user ID, recipe log ID, and any associated arguments, enabling fine-grained control over the completion process.


Usage

add_action( 'automator_before_recipe_completed', 'your_function_name', 10, 4 );

Parameters

$recipe_id (mixed)
This parameter contains the unique identifier for the recipe that is about to be completed.
$user_id (mixed)
This parameter contains the ID of the recipe that is about to be marked as completed.
$recipe_log_id (mixed)
The `$user_id` parameter holds the ID of the user for whom the recipe is being completed.
$args (mixed)
This parameter holds the unique identifier for the log entry created when a recipe is completed.

Examples

/**
 * Example of how to hook into the 'automator_before_recipe_completed' action.
 * This function will log information about the recipe completion and potentially
 * modify or halt the completion process based on specific conditions.
 */
add_action(
	'automator_before_recipe_completed',
	function ( $recipe_id, $user_id, $recipe_log_id, $args ) {
		// Check if the recipe is for a specific user and if that user is an administrator.
		if ( $user_id && get_userdata( $user_id )->roles[0] === 'administrator' ) {
			// Log a message indicating that an admin's recipe completion is being processed.
			Automator()->utilities->debug_log(
				sprintf(
					'Admin user (ID: %d) is about to complete recipe (ID: %d). Log ID: %d',
					$user_id,
					$recipe_id,
					$recipe_log_id
				)
			);

			// Example: Prevent a specific recipe (e.g., recipe ID 123) from completing for admins.
			if ( $recipe_id === 123 ) {
				Automator()->utilities->debug_log( 'Preventing completion of recipe ID 123 for admin.' );
				// To halt the process, you might trigger an error or directly return false/null
				// if the Automator plugin is designed to handle that.
				// For this example, we'll just log and allow it to proceed, but in a real
				// scenario, you might throw an exception or use another mechanism.
				return; // Exit the hook early if we don't want to do anything further for this case.
			}
		}

		// Example: Check if the recipe arguments contain a specific key and value.
		if ( isset( $args['special_condition'] ) && $args['special_condition'] === 'perform_extra_action' ) {
			Automator()->utilities->debug_log(
				sprintf(
					'Special condition met for recipe ID %d. Performing extra action.',
					$recipe_id
				)
			);
			// Here you could perform an additional action before the recipe truly completes.
			// For instance, sending a notification or updating a post.
			// update_post_meta( $post_id, 'automator_extra_done', true );
		}

		// You can also simply log the standard details if no specific conditions are met.
		Automator()->utilities->debug_log(
			sprintf(
				'Before recipe completion: Recipe ID: %d, User ID: %d, Recipe Log ID: %d',
				$recipe_id,
				$user_id,
				$recipe_log_id
			)
		);
	},
	10, // Priority
	4  // 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/process/class-automator-recipe-process-complete.php:934

$recipe_log_id,
				$args,
			),
			'3.0',
			'automator_before_recipe_completed'
		);

		do_action( 'automator_before_recipe_completed', $recipe_id, $user_id, $recipe_log_id, $args );

		if ( null === $recipe_log_id ) {

			if ( null === $recipe_id || ! is_numeric( $recipe_id ) ) {
				Automator()->wp_error->add_error( 'complete_recipe', 'ERROR: You are trying to completed a recipe without providing a recipe_id', $this );

				return null;


Scroll to Top