Action uncanny-automator

automator_before_trigger_is_completed

Fires before a trigger is marked as completed, allowing modifications or logging before the final status update.

add_action( 'automator_before_trigger_is_completed', $callback, 10, 5 );

Description

Fires just before a trigger is marked as completed within an automation recipe. Developers can use this hook to perform actions, modify data, or log information related to the trigger's completion. It provides access to the user ID, trigger ID, recipe ID, trigger log ID, and any associated arguments for comprehensive manipulation.


Usage

add_action( 'automator_before_trigger_is_completed', 'your_function_name', 10, 5 );

Parameters

$user_id (mixed)
The user ID of the user associated with the trigger.
$trigger_id (mixed)
The ID of the user whose trigger is about to be completed.
$recipe_id (mixed)
This parameter contains the ID of the trigger that is about to be completed.
$trigger_log_id (mixed)
This parameter contains the ID of the recipe that the trigger belongs to.
$args (mixed)
This parameter contains any additional arguments passed to the trigger.

Examples

add_action( 'automator_before_trigger_is_completed', function ( $user_id, $trigger_id, $recipe_id, $trigger_log_id, $args ) {

	// Example: Log details about the trigger completion.
	// This could be useful for debugging or auditing purposes.
	if ( ! $user_id || ! $trigger_id || ! $recipe_id || ! $trigger_log_id ) {
		// Log an error if essential parameters are missing.
		error_log( 'automator_before_trigger_is_completed hook received missing parameters.' );
		return;
	}

	$user = get_user_by( 'id', $user_id );
	$trigger_post = get_post( $trigger_id );
	$recipe_post = get_post( $recipe_id );
	$log_entry = get_post_meta( $trigger_log_id, 'automator_trigger_log', true );

	$log_message = sprintf(
		'Before completing trigger. User ID: %s, Trigger ID: %s, Recipe ID: %s, Trigger Log ID: %s. Trigger: "%s", Recipe: "%s". Args: %s.',
		$user_id,
		$trigger_id,
		$recipe_id,
		$trigger_log_id,
		$trigger_post ? $trigger_post->post_title : 'N/A',
		$recipe_post ? $recipe_post->post_title : 'N/A',
		print_r( $args, true )
	);

	// In a real scenario, you might save this to a custom log file,
	// a custom database table, or send it to an external logging service.
	// For this example, we'll use WordPress's error_log.
	error_log( $log_message );

	// Example: Check if a specific argument exists and take action.
	// Let's assume 'email_address' might be passed in $args for certain triggers.
	if ( isset( $args['email_address'] ) && ! empty( $args['email_address'] ) ) {
		$email_to_check = sanitize_email( $args['email_address'] );

		// Check if this email is on a specific blocklist.
		$blocklist = array( '[email protected]', '[email protected]' ); // Example blocklist
		if ( in_array( $email_to_check, $blocklist, true ) ) {
			// If the email is on the blocklist, you might want to prevent
			// the trigger from completing or add a note to the log.
			error_log( sprintf( 'Attempted to complete trigger with blocked email address: %s for User ID: %s, Trigger ID: %s', $email_to_check, $user_id, $trigger_id ) );
			// You could potentially update the trigger log to indicate a blocked action,
			// or even stop the process if this hook allowed for that (though it's 'before_complete', so limited.
			// A filter hook might be better for truly stopping processes.
		}
	}

	// Example: Conditional logic based on user role.
	if ( $user && in_array( 'administrator', $user->roles, true ) ) {
		// Administrator specific actions can be performed here.
		// For example, sending an internal notification.
		// You would replace this with actual notification logic.
		// MentionAdminAboutTriggerCompletion( $user_id, $trigger_id, $recipe_id );
	}

}, 10, 5 );

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

// The trigger is about to be completed
		do_action_deprecated(
			'uap_before_trigger_completed',
			array( $user_id, $trigger_id, $recipe_id, $trigger_log_id, $args ),
			'3.0',
			'automator_before_trigger_is_completed'
		);
		do_action( 'automator_before_trigger_is_completed', $user_id, $trigger_id, $recipe_id, $trigger_log_id, $args );

		$trigger_code        = get_post_meta( $trigger_id, 'code', true );
		$trigger_integration = Automator()->get->trigger_integration_from_trigger_code( $trigger_code );
		if ( 0 === Automator()->plugin_status->get( $trigger_integration ) ) {
			// The plugin for this action is NOT active
			Automator()->wp_error->add_error( 'complete_trigger', 'ERROR: You are trying to complete ' . $trigger_code . ' and the plugin ' . $trigger_integration . ' is not active.', $this );


Scroll to Top