Action uncanny-automator

automator_before_trigger_run

Attempt to add trigger entry and autocomplete it if autocomplete is set to true. Fires before a trigger runs, allowing for entry creation and potential autocomplete.

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

Description

Fires before a trigger is processed and its entry is created. Developers can use this hook to modify trigger arguments or perform actions before the trigger's logic executes, potentially influencing the recipe's flow. The `$args` and `$pass_args` parameters provide access to trigger and recipe data.


Usage

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

Parameters

$args (mixed)
- **$pass_args** `mixed`

Examples

add_action( 'automator_before_trigger_run', 'my_custom_automator_trigger_logic', 10, 2 );

/**
 * Custom logic to run before a trigger is executed in the WordPress Automator plugin.
 * This example logs the trigger arguments and potentially modifies them before the trigger runs.
 *
 * @param mixed $args      The primary arguments passed to the trigger.
 * @param mixed $pass_args Additional arguments passed to the trigger.
 */
function my_custom_automator_trigger_logic( $args, $pass_args ) {
    // Get the current user ID if available
    $user_id = get_current_user_id();

    // Log the arguments for debugging purposes
    error_log( "Automator: Before trigger run. User ID: {$user_id}, Args: " . print_r( $args, true ) . ", Pass Args: " . print_r( $pass_args, true ) );

    // Example: Check if a specific argument is present and modify it if needed
    // This is a hypothetical example. Replace 'specific_arg_key' with an actual key you might expect.
    if ( isset( $args['specific_arg_key'] ) ) {
        // Perform some modification or validation on $args['specific_arg_key']
        // For instance, ensuring it's a valid URL or a specific format.
        $args['specific_arg_key'] = sanitize_text_field( $args['specific_arg_key'] );
        error_log( "Automator: Modified 'specific_arg_key'." );
    }

    // Example: Check if a specific condition in $pass_args is met and perhaps stop the trigger.
    // This is a hypothetical example. Replace 'stop_trigger_condition' with an actual condition.
    if ( isset( $pass_args['stop_trigger_condition'] ) && true === $pass_args['stop_trigger_condition'] ) {
        // In a real scenario, you might have a way to signal back to the Automator
        // core to prevent the trigger from running. This is highly dependent on the
        // Automator plugin's architecture. For this example, we'll just log it.
        error_log( "Automator: Trigger should be stopped based on a condition in pass_args." );
        // Note: To actually stop the trigger, you'd likely need to hook into a different
        // part of the Automator flow or use a mechanism provided by the plugin.
    }

    // IMPORTANT: This hook is an 'action', not a 'filter'. Therefore, you do not
    // need to return anything. If this were a filter hook, you would return
    // the potentially modified 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/recipe-parts/triggers/trait-triggers.php:221

protected function process_trigger( $args ) {

		// Enqueuing do_actions
		$this->enqueue_token_action_and_filter();
		/**
		 * Allow developers to manipulate $pass_args with custom arguments. For example, ignore_post_id.
		 */
		$pass_args = $this->prepare_entry_args( $args );

		/**
		 * Should the trigger be autocompleted and continue running trigger?
		 */
		$complete_trigger = apply_filters( 'automator_auto_complete_trigger', $this->do_trigger_autocomplete(), $pass_args, $args );

		/**
		 * Attempt to add trigger entry and autocomplete it if autocomplete is set to true.
		 */
		do_action( 'automator_before_trigger_run', $args, $pass_args );

		if ( true === $this->do_trigger_autocomplete() ) {
			Automator()->process->user->maybe_add_trigger_entry( $pass_args, $complete_trigger );

			do_action( 'automator_after_trigger_run', $args, $pass_args );

			return;
		}
		$entry_args = Automator()->process->user->maybe_add_trigger_entry( $pass_args, $complete_trigger, $args );
		/**
		 * If trigger is not autocompleted, an array is returned which should be handled manually.
		 */
		if ( empty( $entry_args ) ) {
			return;
		}

		foreach ( $entry_args as $result ) {
			if ( true === $result['result'] ) {
				$result_args = $result['args'];
				/**
				 * @var array $args | All the arguments passed to this function
				 * @var array $pass_args | All the arguments passed to run mark a trigger complete
				 * @var array $result_args | All the arguments returned after marking trigger complete
				 */
				$do_action = array(
					'trigger_entry' => $result_args,
					'entry_args'    => $pass_args,
					'trigger_args'  => $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 ) {

					// @since 5.10 - Added a way for a trigger loopable tokens to be processed at this point.
					do_action( 'automator_loopable_token_hydrate', $result_args, func_get_args() );

					Automator()->process->user->maybe_trigger_complete( $result_args );
				}

				do_action_deprecated(
					'automator_after_trigger_completed',
					array(
						$do_action,
						$this,
					),
					'4.1',
					'automator_after_maybe_trigger_complete'
				);
				do_action( 'automator_after_maybe_trigger_complete', $do_action, $this );
			}
		}

		do_action( 'automator_after_trigger_run', $args, $pass_args );
	}


Scroll to Top