Action Since 3.0 uncanny-automator

automator_recipe_action_created

Fires when a new Automator recipe action is created, providing its ID, code, and request data.

add_action( 'automator_recipe_action_created', $callback, 10, 3 );

Description

Fires after a new action is successfully created within a recipe. Developers can leverage this hook to perform custom logic, such as updating related metadata, sending notifications, or triggering secondary processes based on the newly created action's ID, item code, and the REST request that initiated its creation.


Usage

add_action( 'automator_recipe_action_created', 'your_function_name', 10, 3 );

Parameters

$post_id (int)
Action ID
$item_code (string)
Action item code
$request (WP_REST_Request)

Examples

add_action( 'automator_recipe_action_created', 'my_uncanny_automator_action_logger', 10, 3 );

/**
 * Logs the creation of a new Uncanny Automator recipe action.
 *
 * This function is triggered when a new action is created within an Uncanny Automator recipe.
 * It demonstrates how to access and utilize the provided parameters to perform custom logic,
 * such as logging the event or triggering further automation.
 *
 * @param int             $post_id     The ID of the newly created action post.
 * @param string          $item_code   The unique code identifying the type of action created.
 * @param WP_REST_Request $request     The WordPress REST API request object that triggered the action creation.
 */
function my_uncanny_automator_action_logger( $post_id, $item_code, $request ) {

    // Basic check to ensure we have valid data before proceeding.
    if ( ! absint( $post_id ) || empty( $item_code ) || ! $request instanceof WP_REST_Request ) {
        // Log an error or do nothing if the parameters are not as expected.
        error_log( 'Uncanny Automator: Invalid parameters received for automator_recipe_action_created hook.' );
        return;
    }

    // Get the recipe ID from the request. This assumes the recipe ID is available in the request parameters.
    // You might need to inspect the $request object more closely to find the correct parameter.
    // A common pattern is to have it in the request body or URL parameters.
    $recipe_id = $request->get_param( 'recipe_id' ); // Example: Adjust 'recipe_id' based on actual request structure.

    // Get user information if available from the request.
    // This is highly dependent on how the action creation is being initiated.
    $user_id = 0;
    if ( $request->has_param( 'user_id' ) ) {
        $user_id = absint( $request->get_param( 'user_id' ) );
    } elseif ( is_user_logged_in() ) {
        $user_id = get_current_user_id();
    }

    // Log the event to the WordPress debug log.
    // In a real-world scenario, you might store this in a custom database table,
    // send an email notification, or trigger another process.
    $log_message = sprintf(
        'Uncanny Automator: New action created. Action ID: %d, Item Code: %s, Recipe ID: %s, User ID: %d',
        $post_id,
        $item_code,
        $recipe_id ? absint( $recipe_id ) : 'N/A',
        $user_id
    );

    // Use error_log for development/debugging. For production, consider a more robust logging solution.
    error_log( $log_message );

    // Example: If the action item code is 'send-email', we might want to do something specific.
    if ( 'send-email' === $item_code ) {
        // You could potentially fetch details about this specific 'send-email' action
        // using $post_id and get_post_meta() to see what email is being configured.
        $email_subject = get_post_meta( $post_id, 'email_subject', true ); // Example meta key
        error_log( sprintf(
            'Uncanny Automator: "Send Email" action created. Subject: %s',
            $email_subject ? $email_subject : 'Not specified'
        ) );
    }

    // Example: If this is a critical action, you might want to flag it for review.
    // This could involve adding a custom meta field to the action post.
    // For demonstration, we'll just log a message.
    if ( str_contains( $item_code, 'highly_critical' ) ) {
        error_log( sprintf(
            'Uncanny Automator: HIGHLY CRITICAL action created with ID %d and item code %s.',
            $post_id,
            $item_code
        ) );
        // You might update the post meta here to mark it for review.
        // update_post_meta( $post_id, '_needs_review', true );
    }
}

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/automator-post-types/uo-recipe/class-recipe-post-rest-api.php:551

* @param int $post_id Action ID
			 * @param string $item_code Action item code
			 * @param WP_REST_Request $request
			 *
			 * @since 3.0
			 * @package Uncanny_Automator
			 */
			do_action( 'automator_recipe_action_created', $post_id, $item_code, $request );
		}

		if ( 'create_closure' === $action ) {
			Automator()->set_recipe_part_meta( $post_id, $item_code, $integration_code, $post_type, $default_meta );

			/**
			 * @param int $post_id Closure ID


Internal Usage

Found in uncanny-automator-pro/src/core/includes/automator-pro-cache-handler.php:27:

add_action( 'automator_recipe_action_created', array( $this, 'recipe_post_status_changed' ), 99999 );
Scroll to Top