Action uncanny-automator

automator_trigger_completed

Fires when an automation process has successfully completed, allowing for additional actions.

add_action( 'automator_trigger_completed', $callback, 10, 1 );

Description

Fired when a recipe trigger has successfully completed its execution. Developers can hook into this action to perform custom actions after a trigger is confirmed complete, such as logging specific details or initiating secondary processes. The `$process_further` parameter contains data related to the trigger's completion status.


Usage

add_action( 'automator_trigger_completed', 'your_function_name', 10, 1 );

Parameters

$process_further (mixed)
This parameter determines whether the recipe process should continue after the trigger has completed.

Examples

<?php
/**
 * Example function to handle the automator_trigger_completed hook.
 * This function demonstrates how to access the $process_further data
 * and conditionally log information about the completed trigger.
 *
 * @param mixed $process_further Data related to the completed trigger process.
 */
function my_automator_handle_trigger_completion( $process_further ) {
	// Ensure the expected data structure is present.
	// The $process_further variable is typically an array after the extract() function.
	if ( ! is_array( $process_further ) ) {
		error_log( 'automator_trigger_completed hook received invalid data.' );
		return;
	}

	// Example: Log trigger details if a specific flag is set within $process_further.
	// This is a hypothetical scenario based on potential internal usage.
	if ( isset( $process_further['log_trigger_details'] ) && true === $process_further['log_trigger_details'] ) {
		$recipe_id = isset( $process_further['recipe_id'] ) ? $process_further['recipe_id'] : 'N/A';
		$trigger_id = isset( $process_further['trigger_id'] ) ? $process_further['trigger_id'] : 'N/A';
		$user_id = isset( $process_further['user_id'] ) ? $process_further['user_id'] : 'N/A';

		$log_message = sprintf(
			'Automator: Trigger completed for Recipe ID %1$s, Trigger ID %2$s, User ID %3$s.',
			esc_html( $recipe_id ),
			esc_html( $trigger_id ),
			esc_html( $user_id )
		);

		// In a real-world scenario, you might use WordPress's WP_Debug_Log or a custom logging function.
		// For this example, we'll simulate logging.
		error_log( $log_message );
	}

	// Example: Conditionally perform another action based on the trigger completion.
	// This could be for triggering subsequent actions, sending notifications, etc.
	if ( isset( $process_further['should_send_notification'] ) && true === $process_further['should_send_notification'] ) {
		$recipient_email = isset( $process_further['recipient_email'] ) ? $process_further['recipient_email'] : get_option( 'admin_email' );
		$subject = 'Automator Trigger Completed Notification';
		$body = sprintf(
			'The trigger for recipe "%1$s" has been completed for user ID "%2$s".',
			isset( $process_further['recipe_title'] ) ? esc_html( $process_further['recipe_title'] ) : 'Unknown Recipe',
			esc_html( isset( $process_further['user_id'] ) ? $process_further['user_id'] : 'N/A' )
		);

		// Use wp_mail() for sending emails in WordPress.
		wp_mail( $recipient_email, $subject, $body );
	}
}

// Hook into the 'automator_trigger_completed' action.
// The '10' is the default priority. The '1' indicates that the callback function accepts one argument.
add_action( 'automator_trigger_completed', 'my_automator_handle_trigger_completion', 10, 1 );

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

$process_further = apply_filters( 'automator_maybe_continue_recipe_process', $process_further );

		extract( $process_further, EXTR_OVERWRITE ); // phpcs:ignore WordPress.PHP.DontExtract.extract_extract

		// The trigger is now completed
		do_action_deprecated( 'uap_trigger_completed', array( $process_further ), '3.0', 'automator_trigger_completed' );

		do_action( 'automator_trigger_completed', $process_further );

		// If all triggers for the recipe are completed.
		if ( $maybe_continue_recipe_process && $this->triggers_completed( $recipe_id, $user_id, $recipe_log_id, $args ) ) {

			// Store the historical count of a recipe
			$this->add_recipe_count( $recipe_id );


Scroll to Top