Action Since 7.0 uncanny-automator

automator_recipe_{$type}_add_before

Fires before a specific item type is added. Fires before an API integration item of a specific type is added to the Automator recipe.

add_action( 'automator_recipe_{$type}_add_before', $callback, 10, 1 );

Description

Fires before an item of a specific type is added via the API. Developers can use this hook to perform pre-addition validations, modify the request data, or trigger custom logic before the item is persisted. The `$request` object provides access to all incoming API data.


Usage

add_action( 'automator_recipe_{$type}_add_before', 'your_function_name', 10, 1 );

Parameters

$request (WP_REST_Request)
The REST request object.

Examples

// Hook to run before a new action is added to a recipe.
// This example checks if the user has permission to add a specific type of action.
add_action( 'automator_recipe_action_add_before', function( WP_REST_Request $request ) {

    // Get the action data from the request
    $action_data = $request->get_params();

    // Define allowed action types for the current user role
    $allowed_action_types = array( 'send_email', 'create_post' ); // Example allowed types

    // Check if the user is logged in and has the capability to add actions
    if ( is_user_logged_in() && current_user_can( 'manage_automator_recipes' ) ) {

        // Check if the 'type' parameter is present in the action data
        if ( isset( $action_data['type'] ) ) {
            $action_type = sanitize_key( $action_data['type'] );

            // If the action type is not allowed, throw a REST error
            if ( ! in_array( $action_type, $allowed_action_types, true ) ) {
                return new WP_Error(
                    'automator_invalid_action_type',
                    __( 'You do not have permission to add this type of action.', 'your-text-domain' ),
                    array( 'status' => 403 )
                );
            }
        }
    } else {
        // If the user is not logged in or lacks capability, throw a REST error
        return new WP_Error(
            'automator_permission_denied',
            __( 'You do not have permission to add actions.', 'your-text-domain' ),
            array( 'status' => 401 )
        );
    }

    // If all checks pass, do nothing and allow the action to be added
    return true; // For filters, you'd typically return the modified value or true/false

}, 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/api/transports/restful/recipe/items/traits/hooks/trait-hook-add-operations.php:44

protected function dispatch_add_before_hooks(): void {
		$type = $this->get_item_type();

		/**
		 * Fires before any recipe item is added.
		 *
		 * @since 7.0
		 *
		 * @param WP_REST_Request $request The REST request object.
		 * @param string          $type    The item type.
		 */
		do_action(
			'automator_recipe_item_add_before',
			$this->get_request(),
			$type
		);

		/**
		 * Fires before a specific item type is added.
		 *
		 * @since 7.0
		 *
		 * @param WP_REST_Request $request The REST request object.
		 */
		do_action(
			"automator_recipe_{$type}_add_before",
			$this->get_request()
		);
	}


Scroll to Top