automator_before_process_action
Fires before an Automator action is processed, allowing modification of its execution.
add_action( 'automator_before_process_action', $callback, 10, 4 );
Description
Fires before an action is processed within Uncanny Automator. Developers can hook into this action to perform custom logic or modify action data before it's executed. It provides access to the user ID, action data, recipe ID, and arguments, allowing for powerful conditional processing and data manipulation.
Usage
add_action( 'automator_before_process_action', 'your_function_name', 10, 4 );
Parameters
-
$this(mixed) - The `$user_id` parameter represents the ID of the user for whom the action is being processed.
-
$this(mixed) - This parameter likely contains the user ID for whom the action is being processed.
-
$this(mixed) - This parameter contains the data associated with the specific action being processed.
-
$this(mixed) - The third parameter passed to this hook is the `$recipe_id`, which represents the unique identifier of the recipe currently being processed.
Examples
<?php
/**
* Example function to log details before an Uncanny Automator action is processed.
* This function demonstrates how to hook into 'automator_before_process_action'
* to inspect and potentially modify data before the action execution.
*
* @param int $user_id The ID of the user performing the action.
* @param array $action_data The data associated with the action.
* @param int $recipe_id The ID of the recipe the action belongs to.
* @param array $args Additional arguments passed to the action.
*/
function my_automator_log_before_action( $user_id, $action_data, $recipe_id, $args ) {
// Check if Uncanny Automator Pro is active to avoid errors if it's not.
if ( ! defined( 'UNCA NY_AUTOMATOR_PRO_VERSION' ) ) {
return;
}
// Log some basic information about the action being processed.
error_log( sprintf(
'Uncanny Automator: Before processing action. User ID: %d, Recipe ID: %d, Action Type: %s',
$user_id,
$recipe_id,
isset( $action_data['type'] ) ? $action_data['type'] : 'Unknown'
) );
// Example: If you wanted to check for a specific action type and add a note.
if ( isset( $action_data['type'] ) && 'your_specific_action_type' === $action_data['type'] ) {
error_log( 'Uncanny Automator: Detected specific action type. Performing pre-checks...' );
// You could add custom pre-checks here. For example:
// if ( ! my_custom_pre_check( $user_id, $action_data ) ) {
// // Handle error or modify action_data if possible.
// }
}
// Example: Accessing and potentially modifying action data.
// Be cautious when modifying data as it can affect the action's outcome.
// If you modify $action_data, ensure the action can handle it.
if ( isset( $action_data['settings']['custom_field'] ) ) {
$original_value = $action_data['settings']['custom_field'];
$action_data['settings']['custom_field'] = 'modified_by_hook_' . $original_value;
error_log( sprintf(
'Uncanny Automator: Modified custom_field for User ID %d. Original: "%s", New: "%s"',
$user_id,
$original_value,
$action_data['settings']['custom_field']
) );
}
// You can also inspect other arguments like $args.
// error_log( print_r( $args, true ) );
}
// Add the action hook. The '10' is the priority, and '4' is the number of arguments accepted.
add_action( 'automator_before_process_action', 'my_automator_log_before_action', 10, 4 );
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/actions/abstract-action.php:344
src/core/lib/recipe-parts/trait-actions.php:59
public function do_action( $user_id, $action_data, $recipe_id, $args ) {
// Clear errors in case there are some left from a previous action.
$this->errors = array();
$this->user_id = $user_id;
$this->action_data = $action_data;
$this->recipe_id = $recipe_id;
$this->args = $args;
do_action( 'automator_before_process_action', $this->user_id, $this->action_data, $this->recipe_id, $this->args );
$this->maybe_parsed = $this->maybe_parse_tokens( $this->user_id, $this->action_data, $this->recipe_id, $this->args );
$this->action_data['maybe_parsed'] = $this->maybe_parsed;
$error = null;
try {
$result = $this->process_action( $this->user_id, $this->action_data, $this->recipe_id, $this->args, $this->maybe_parsed );
} catch ( Error $e ) {
$result = false;
$this->add_log_error( $e->getMessage() );
} catch ( Exception $e ) {
$result = false;
$this->add_log_error( $e->getMessage() );
}
if ( false === $result ) {
$this->action_data['complete_with_errors'] = true;
$error = $this->get_log_errors();
}
if ( null === $result && ! $this->is_complete_with_notice() ) {
$action_data['do_nothing'] = true;
$this->action_data['complete_with_errors'] = true;
$error = $this->get_log_errors();
}
if ( null === $result && $this->is_complete_with_notice() ) {
$this->action_data['complete_with_notice'] = true;
$error = $this->get_log_errors();
}
Automator()->complete->action( $this->user_id, $this->action_data, $this->recipe_id, $error );
do_action( 'automator_after_process_action', $this->user_id, $this->action_data, $this->recipe_id, $this->args, $this->maybe_parsed );
}
Internal Usage
Found in uncanny-automator-pro/src/integrations/run-code/actions/run-code-run-js.php:47:
add_action( 'automator_before_process_action', array( $this, 'prepare_filter' ), 10, 4 );