Action uncanny-automator

automator_after_maybe_trigger_complete

Fires after Automator potentially triggers a recipe, allowing for custom actions after completion.

add_action( 'automator_after_maybe_trigger_complete', $callback, 10, 2 );

Description

Fires after a trigger is successfully processed and marked as complete, but before any subsequent recipe actions are executed. Developers can use this hook to perform custom actions, log trigger completion details, or modify data before the recipe runner proceeds to the next step. It's a crucial point for post-trigger logic and data manipulation.


Usage

add_action( 'automator_after_maybe_trigger_complete', 'your_function_name', 10, 2 );

Parameters

$do_action (mixed)
This parameter indicates whether the action should be executed.
$this (mixed)
This parameter indicates whether the action should be executed based on the recipe's conditions and triggers.

Examples

add_action( 'automator_after_maybe_trigger_complete', 'my_automator_trigger_completion_handler', 10, 2 );

/**
 * Handles actions to be performed after a trigger has been potentially completed.
 *
 * This function can be used to perform custom logging, send notifications,
 * or trigger other workflows based on the completion status of an Automator recipe trigger.
 *
 * @param array $do_action The array containing information about the action being executed.
 *                         This typically includes details about the trigger and its arguments.
 * @param object $trigger_instance The instance of the trigger object that was processed.
 */
function my_automator_trigger_completion_handler( $do_action, $trigger_instance ) {
	// Check if the trigger was actually marked as complete.
	// The 'is_completed' key might not always be present if the trigger logic decided not to proceed.
	if ( isset( $do_action['is_completed'] ) && $do_action['is_completed'] ) {
		// Log that a trigger has been successfully completed.
		// In a real-world scenario, you might log this to a custom log file or use a more robust logging system.
		error_log( 'Automator: Trigger completed for recipe item ID: ' . $do_action['recipe_item_id'] );

		// Example: Send an internal notification or trigger another action.
		// For instance, if this is a specific type of trigger, we might want to perform
		// additional setup for the user or an administrator.
		if ( 'your_specific_trigger_type' === $trigger_instance->get_trigger_type() ) {
			$user_id = get_current_user_id(); // Or get user ID from $do_action['entry_args'] if available.
			// Send an admin notification if a specific trigger type is completed.
			// wp_mail( '[email protected]', 'Automator Trigger Completed', 'A specific trigger has just been completed.' );
			error_log( "Automator: Specific trigger type completed for user ID: {$user_id}." );
		}
	} else {
		// Log that the trigger was processed but not marked as complete.
		error_log( 'Automator: Trigger processed but not completed for recipe item ID: ' . $do_action['recipe_item_id'] );
	}
}

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/recipe-parts/triggers/abstract-trigger.php:555
src/core/lib/recipe-parts/triggers/trait-triggers.php:273

protected function process( $recipe_id, $trigger, $hook_args ) {

		if ( Automator()->is_recipe_throttled( absint( $recipe_id ), absint( $this->user_id ) ) ) {
			return;
		}

		$this->recipe_log_id = $this->maybe_create_recipe_log_entry( $this->recipe_id );

		$this->trigger_log_entry = $this->maybe_create_trigger_log_entry( $this->recipe_id, $this->recipe_log_id, $this->trigger );

		$this->trigger_records = array(
			'code'           => $this->get_code(),
			'user_id'        => $this->user_id,
			'trigger_id'     => (int) $this->trigger['ID'],
			'recipe_id'      => $this->recipe_id,
			'trigger_log_id' => $this->trigger_log_entry,
			'recipe_log_id'  => $this->recipe_log_id,
			'run_number'     => (int) Automator()->get->next_run_number( $this->recipe_id, $this->user_id, true ),
			'meta'           => $this->get_trigger_meta(),
			'get_trigger_id' => $this->trigger_log_entry,
		);

		$this->token_values = $this->hydrate_tokens( $this->trigger, $this->hook_args );
		$this->save_tokens( $this->get_code(), $this->token_values );

		$do_action = array(
			'trigger_entry' => $this->trigger,
			'entry_args'    => $this->trigger_records,
			'trigger_args'  => $this->hook_args,
		);

		do_action( 'automator_before_trigger_completed', $do_action, $this );

		$process_further = apply_filters( 'automator_trigger_should_complete', true, $do_action, $this );

		if ( $process_further ) {
			// Register the hook.
			$this->register_loopable_trigger_tokens_hooks( $do_action );
			// Fire the hook.
			do_action( 'automator_loopable_token_hydrate', $do_action['entry_args'], $do_action['trigger_args'] );
			// Complete the trigger.
			Automator()->complete->trigger( $this->trigger_records );
		}

		do_action( 'automator_after_maybe_trigger_complete', $do_action, $this );
	}


Scroll to Top