Action Since 6.7.0 uncanny-automator

automator_action_marked_{$status_name}

Fires after an action has been marked with a specific status. Dynamic hook name based on the completion status. This allows developers to hook into specific status changes for more targeted functionality. Fires when an action's status changes, providing flexibility for status-specific automation.

add_action( 'automator_action_marked_{$status_name}', $callback, 10, 5 );

Description

Fires after an Automator action's status is updated. This dynamic hook, like `automator_action_marked_completed`, allows granular control over specific status changes. Developers can leverage it to trigger custom logic, log details, or perform conditional actions based on an action's success, failure, or completion with errors.


Usage

add_action( 'automator_action_marked_{$status_name}', 'your_function_name', 10, 5 );

Parameters

$action_id (int)
The action ID that was marked with the status.
$recipe_log_id (int)
The recipe log ID associated with the action.
$recipe_details (array)
Details of the recipe log associated with the action.
$completed (int)
The completion status constant.
$error_message (string)
Error message if applicable, empty string otherwise.

Examples

<?php
/**
 * Example callback function to log when an Automator action completes with errors.
 *
 * This function demonstrates how to hook into the 'automator_action_marked_completed_with_errors'
 * action to perform custom logic when an action within an Automator recipe finishes but encounters errors.
 *
 * @param int   $action_id       The ID of the Automator action that was marked.
 * @param int   $recipe_log_id   The ID of the recipe log entry associated with this action.
 * @param array $recipe_details  An array containing details of the recipe log.
 * @param int   $completed       The completion status constant (should be automator_log_completed_with_errors in this case).
 * @param string $error_message  The error message generated during the action's execution.
 */
function my_automator_action_log_errors( int $action_id, int $recipe_log_id, array $recipe_details, int $completed, string $error_message ): void {
	// In a real-world scenario, you might want to:
	// 1. Log this to a custom database table for error tracking.
	// 2. Send an email notification to an administrator.
	// 3. Trigger another Automator recipe for error remediation.

	// For demonstration, we'll just log to the debug log.
	error_log( sprintf(
		'Automator Action Error: Action ID %1$d, Recipe Log ID %2$d. Status: %3$d. Error: %4$s',
		$action_id,
		$recipe_log_id,
		$completed,
		$error_message
	) );

	// You can also inspect recipe_details to get more context.
	// For example, if the action was sending an email, you might find recipient details here.
	// error_log( print_r( $recipe_details, true ) );
}

// Hook into the specific Automator action status.
// The '10' is the priority (default), and '5' indicates that the callback function accepts 5 arguments.
add_action( 'automator_action_marked_completed_with_errors', 'my_automator_action_log_errors', 10, 5 );


/**
 * Example callback function to log when an Automator action fails.
 *
 * This function demonstrates hooking into the 'automator_action_marked_failed' action
 * to handle actions that did not complete successfully.
 *
 * @param int   $action_id       The ID of the Automator action that was marked.
 * @param int   $recipe_log_id   The ID of the recipe log entry associated with this action.
 * @param array $recipe_details  An array containing details of the recipe log.
 * @param int   $completed       The completion status constant (should be automator_log_failed in this case).
 * @param string $error_message  The error message generated during the action's execution.
 */
function my_automator_action_handle_failure( int $action_id, int $recipe_log_id, array $recipe_details, int $completed, string $error_message ): void {
	// Similar to the error logging, you might want to perform specific actions
	// for actions that completely failed, not just completed with errors.
	error_log( sprintf(
		'Automator Action Failed: Action ID %1$d, Recipe Log ID %2$d. Status: %3$d. Error: %4$s',
		$action_id,
		$recipe_log_id,
		$completed,
		$error_message
	) );
}

// Hook into the specific Automator action status for failures.
add_action( 'automator_action_marked_failed', 'my_automator_action_handle_failure', 10, 5 );


/**
 * Example callback function to log when an Automator action completes successfully.
 *
 * This function demonstrates hooking into the 'automator_action_marked_completed' action
 * to acknowledge successful action executions.
 *
 * @param int   $action_id       The ID of the Automator action that was marked.
 * @param int   $recipe_log_id   The ID of the recipe log entry associated with this action.
 * @param array $recipe_details  An array containing details of the recipe log.
 * @param int   $completed       The completion status constant (should be automator_log_completed in this case).
 * @param string $error_message  An empty string as there are no errors.
 */
function my_automator_action_log_success( int $action_id, int $recipe_log_id, array $recipe_details, int $completed, string $error_message ): void {
	// You might use this for analytics, tracking successful automation steps,
	// or updating user meta if the action was user-specific.
	error_log( sprintf(
		'Automator Action Completed Successfully: Action ID %1$d, Recipe Log ID %2$d. Status: %3$d.',
		$action_id,
		$recipe_log_id,
		$completed
	) );
}

// Hook into the specific Automator action status for successful completions.
add_action( 'automator_action_marked_completed', 'my_automator_action_log_success', 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/utilities/db/class-automator-db-handler-actions.php:406

public function mark_complete( $action_id, $recipe_log_id, $completed = 1, $error_message = '' ) {
		$data = array(
			'completed'     => $completed,
			'date_time'     => current_time( 'mysql' ),
			'error_message' => $error_message,
		);

		$where = array(
			'automator_action_id'     => $action_id,
			'automator_recipe_log_id' => $recipe_log_id,
		);

		$updated = $this->update( $data, $where );

		$recipe_details = Automator()->db->recipe->get_by( 'ID', $recipe_log_id );

		if ( $updated ) {
			/**
			 * Fires after an action has been successfully marked as complete in the database.
			 *
			 * This hook allows developers to perform additional operations when an action
			 * is marked complete, such as logging, notifications, or triggering other processes.
			 *
			 * @since 6.7.0
			 *
			 * @param int    $action_id      The action ID that was marked complete.
			 * @param int    $recipe_log_id  The recipe log ID associated with the action.
			 * @param array  $recipe_details The recipe details associated with the action.
			 * @param int    $completed      The completion status (see Automator_Status constants).
			 * @param string $error_message  Error message if the action failed, empty string if successful.
			 *
			 * @example
			 * // Hook into the action completion event
			 * add_action( 'automator_action_completion_status_changed', 'my_custom_action_complete_handler', 10, 5 );
			 *
			 * function my_custom_action_complete_handler( $action_id, $recipe_log_id, $recipe_details, $completed, $error_message ) {
			 *     if ( Automator_Status::COMPLETED === $completed ) {
			 *         // Action completed successfully
			 *         error_log( "Action {$action_id} completed successfully for recipe log {$recipe_log_id}" );
			 *     } elseif ( Automator_Status::COMPLETED_WITH_ERRORS === $completed ) {
			 *         // Action completed with errors
			 *         error_log( "Action {$action_id} completed with errors: {$error_message}" );
			 *     }
			 * }
			 */
			do_action( 'automator_action_completion_status_changed', $action_id, $recipe_log_id, $recipe_details, $completed, $error_message );

			// Fire specific hooks based on completion status
			$all_statuses = Automator_Status::get_all_statuses();

			if ( array_key_exists( $completed, $all_statuses ) ) {
				// Convert status constant to hook-friendly name
				$status_name = Automator_Status::get_class_name( $completed );
				$status_name = str_replace( '-', '_', $status_name );

				/**
				 * Fires after an action has been marked with a specific status.
				 *
				 * Dynamic hook name based on the completion status. This allows developers
				 * to hook into specific status changes for more targeted functionality.
				 *
				 * @since 6.7.0
				 *
				 * @param int    $action_id      The action ID that was marked with the status.
				 * @param int    $recipe_log_id  The recipe log ID associated with the action.
				 * @param array  $recipe_details Details of the recipe log associated with the action.
				 * @param int    $completed      The completion status constant.
				 * @param string $error_message  Error message if applicable, empty string otherwise.
				 *
				 * @example
				 * // Hook into specific status events
				 * add_action( 'automator_action_marked_completed_with_errors', 'handle_action_errors', 10, 5 );
				 * add_action( 'automator_action_marked_failed', 'handle_action_failure', 10, 5 );
				 * add_action( 'automator_action_marked_completed', 'handle_action_success', 10, 5 );
				 */
				do_action( "automator_action_marked_{$status_name}", $action_id, $recipe_log_id, $recipe_details, $completed, $error_message );
			}
		}
	}


Scroll to Top