Action uncanny-automator

automator_before_action_completed

Fires before an Automator action is marked as completed, allowing for modifications to the action's final state.

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

Description

Fires before an action is marked as completed within a recipe. Developers can use this hook to perform custom actions, modify completion status, or log additional information before the action is officially finished. It provides access to action and recipe details.


Usage

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

Parameters

$do_action_args (mixed)
This parameter contains an array of arguments passed to the `automator_before_action_completed` hook, including details about the action, recipe, any errors, and the recipe log.

Examples

/**
 * Example of how to hook into the 'automator_before_action_completed' action.
 * This function will be executed just before an action is marked as completed
 * within the Automator plugin. It receives an array of arguments related to the
 * action and recipe processing.
 *
 * We can use this hook to perform custom logging, update custom meta,
 * or potentially modify the action completion process based on certain conditions.
 *
 * @param array $do_action_args An array containing details about the action and recipe.
 *                              Expected keys include: 'action_id', 'recipe_id',
 *                              'error_message', 'recipe_log_id', 'args'.
 */
add_action( 'automator_before_action_completed', function( $do_action_args ) {

	// Check if the action completed with an error.
	if ( ! empty( $do_action_args['error_message'] ) ) {
		// Log a specific message for failed actions for debugging.
		error_log( sprintf(
			'Automator Action Failed: Recipe ID %d, Action ID %d. Error: %s',
			$do_action_args['recipe_id'],
			$do_action_args['action_id'],
			$do_action_args['error_message']
		) );

		// Potentially notify an administrator about the failed action.
		// This is a simplified example; a real implementation might involve
		// more robust notification logic.
		if ( get_option( 'automator_notify_on_action_failure' ) === 'yes' ) {
			// Example: send_admin_notification( 'Automator Action Failed', 'Details...' );
		}
	} else {
		// Log a success message for completed actions.
		// This can be useful for tracking successful recipe executions.
		error_log( sprintf(
			'Automator Action Completed Successfully: Recipe ID %d, Action ID %d.',
			$do_action_args['recipe_id'],
			$do_action_args['action_id']
		) );

		// Example: Update a custom meta field on the user if this action is user-specific.
		// This assumes 'args' contains a 'user_id' key.
		if ( isset( $do_action_args['args']['user_id'] ) && absint( $do_action_args['args']['user_id'] ) > 0 ) {
			$user_id = absint( $do_action_args['args']['user_id'] );
			update_user_meta( $user_id, 'automator_last_action_' . $do_action_args['action_id'], current_time( 'mysql' ) );
		}
	}

	// You can also access and potentially modify the arguments before they are used further.
	// For instance, to add extra debugging information to the log.
	$do_action_args['custom_debug_info'] = 'Processed at ' . current_time( 'datetime' );

	// If you needed to modify the arguments that are passed to subsequent hooks or processes,
	// you would typically do that with a filter. However, this is an action hook, so
	// modifications here affect how *this* callback function behaves or what it does
	// with the data. If you needed to pass modified $do_action_args to a *later* action hook,
	// you'd need a hook after this one.

}, 10, 1 ); // Priority 10, accepts 1 argument.

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

'action_id'     => $action_id,
			'recipe_id'     => $recipe_id,
			'error_message' => $error_message,
			'recipe_log_id' => $recipe_log_id,
			'args'          => $args,
		);

		do_action( 'automator_before_action_completed', $do_action_args );

		$error_message = $this->get_action_error_message( $user_id, $action_data, $recipe_id, $error_message, $recipe_log_id, $args );

		$process_further = apply_filters( 'automator_before_action_created', true, $user_id, $action_data, $recipe_id, $error_message, $recipe_log_id, $args );

		do_action( 'automator_before_action_completed_after_message_and_process_further', $do_action_args, $error_message, $process_further );


Scroll to Top