Filter uncanny-automator

automator_process_complete_actions

Filters the actions executed after a recipe completes, allowing modifications before they are processed for a specific recipe.

add_filter( 'automator_process_complete_actions', $callback, 10, 5 );

Description

Fires after a recipe's triggers have completed and before its actions are processed. Developers can use this hook to modify the list of actions to be executed, or to perform custom logic based on the recipe's context, user, and log details. The `$actions` parameter contains the actions to be processed.


Usage

add_filter( 'automator_process_complete_actions', 'your_function_name', 10, 5 );

Parameters

$actions (mixed)
This parameter contains the actions associated with the recipe that have been processed or are about to be processed.
$recipe_id (mixed)
This parameter contains an array of all actions associated with the recipe.
$user_id (mixed)
This parameter contains the ID of the recipe that has just been completed.
$recipe_log_id (mixed)
This parameter contains the ID of the user for whom the recipe is being processed.
$args (mixed)
This parameter holds any additional arguments passed to the function that are relevant to the recipe completion process.

Return Value

The filtered value.


Examples

/**
 * Example filter for 'automator_process_complete_actions'.
 *
 * This filter allows modification of the actions to be processed after a recipe completes.
 * In this example, we'll conditionally add a custom action if a specific recipe ID is detected.
 *
 * @param array $actions       The array of actions to be processed.
 * @param int   $recipe_id     The ID of the completed recipe.
 * @param int   $user_id       The ID of the user associated with the recipe.
 * @param int   $recipe_log_id The ID of the recipe log entry.
 * @param array $args          Additional arguments passed to the process.
 *
 * @return array The modified array of actions.
 */
add_filter(
	'automator_process_complete_actions',
	function ( $actions, $recipe_id, $user_id, $recipe_log_id, $args ) {

		// Example: If the recipe ID is 123, add a custom action.
		if ( $recipe_id === 123 ) {
			$custom_action = array(
				'id'   => 'custom_log_entry',
				'name' => __( 'Log Custom Message', 'your-text-domain' ),
				'type' => 'custom',
				'settings' => array(
					'message' => sprintf(
						__( 'Recipe %d for user %d completed successfully. Log ID: %d', 'your-text-domain' ),
						$recipe_id,
						$user_id,
						$recipe_log_id
					),
				),
			);
			// Append the custom action to the existing list of actions.
			$actions[] = $custom_action;
		}

		// You could also conditionally remove actions, modify their settings, etc.
		// For example, to remove an action with a specific ID:
		// $actions = array_filter( $actions, function( $action ) {
		//     return $action['id'] !== 'some_action_to_remove';
		// } );

		// Always return the (potentially modified) $actions array.
		return $actions;
	},
	10, // Priority
	5  // Number of 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:325

public function complete_actions( $recipe_id = null, $user_id = null, $recipe_log_id = null, $args = array() ) {

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

		// No recipe actions found.
		if ( empty( $actions ) ) {

			// Check for closures.
			$closure_data = (array) Automator()->get_recipe_data( AUTOMATOR_POST_TYPE_CLOSURE, $recipe_id, array(), true );

			// No actions and no closures - complete the recipe with errors.
			if ( empty( $closure_data ) ) {
				Automator()->db->recipe->mark_complete( $recipe_log_id, Automator_Status::COMPLETED_WITH_ERRORS );

				return false;
			}

			// Have closures, complete the recipe.
			Automator()->db->recipe->mark_complete( $recipe_log_id, Automator_Status::COMPLETED );
			// This action hook is fired just before the closures are run.
			do_action( 'automator_recipe_process_complete_complete_actions_before_closures', $recipe_id, $user_id, $recipe_log_id, $args );

			$this->closures( $recipe_id, $user_id, $recipe_log_id, $args, $closure_data );

			return true;
		}

		// Recipe actions found - complete them.
		$recipe_actions_data = apply_filters(
			'automator_process_complete_actions',
			$actions,
			$recipe_id,
			$user_id,
			$recipe_log_id,
			$args
		);

		do_action( 'automator_before_process_complete_actions', $recipe_id, $user_id, $recipe_log_id, $actions, $args );

		// Run individual action on behalf of the user.
		foreach ( $recipe_actions_data as $action_data ) {

			$completed = $this->complete_action( $action_data, $recipe_id, $user_id, $recipe_log_id, $args );

			if ( false === $completed ) {
				Utilities::log(
					Automator()->wp_error->get_messages( 'complete_action' ),
					'Method complete_action has returned false',
					AUTOMATOR_DEBUG_MODE,
					'complete_actions'
				);
				continue;
			}
		}

		// This action hook is fired just before the closures are run.
		do_action( 'automator_recipe_process_complete_complete_actions_before_closures', $recipe_id, $user_id, $recipe_log_id, $args );

		// Update the count of the recipe
		Automator()->db->recipe->update_count( $recipe_id );

		$this->closures( $recipe_id, $user_id, $recipe_log_id, $args );

		return true;
	}


Scroll to Top