Action Since 7.0 uncanny-automator

automator_recipe_item_update_before

Fires before a recipe item is updated. Fires before an Automator recipe item is updated, providing access to the request and item type.

add_action( 'automator_recipe_item_update_before', $callback, 10, 2 );

Description

Fires right before a recipe item is updated via the API. This action provides access to the REST request object and the item's type, allowing developers to intercept or modify the update process, validate data, or perform custom logging before the item is saved.


Usage

add_action( 'automator_recipe_item_update_before', 'your_function_name', 10, 2 );

Parameters

$request (WP_REST_Request)
The REST request object.
$type (string)
The item type.

Examples

add_action( 'automator_recipe_item_update_before', 'my_automator_recipe_item_update_before_handler', 10, 2 );

/**
 * Example handler for the 'automator_recipe_item_update_before' action hook.
 *
 * This function demonstrates how to intercept a recipe item update before it occurs.
 * In this example, we're logging the item type and checking if a specific
 * parameter is present in the request, potentially for validation or modification.
 *
 * @param WP_REST_Request $request The REST request object containing the update data.
 * @param string          $type    The type of the recipe item being updated (e.g., 'action', 'trigger', 'condition').
 */
function my_automator_recipe_item_update_before_handler( WP_REST_Request $request, string $type ) {
	// Log the type of item being updated.
	error_log( "Attempting to update recipe item of type: " . esc_html( $type ) );

	// Get the parameters from the request.
	$params = $request->get_params();

	// Check if a specific parameter is present, for example, to ensure an 'order' is provided.
	if ( isset( $params['order'] ) ) {
		$order_value = intval( $params['order'] );
		if ( $order_value < 1 ) {
			// In a real-world scenario, you might want to throw an error or
			// adjust the data before it's saved. For this example, we'll log it.
			error_log( "Warning: Recipe item update for type '" . esc_html( $type ) . "' has an invalid order value: " . $order_value );
			// You could also potentially modify $params here if needed.
		}
	} else {
		error_log( "Info: 'order' parameter not found in the request for recipe item type: " . esc_html( $type ) );
	}

	// You could also access other parts of the request, like:
	// $item_id = $request['id'];
	// $new_data = $request->get_json_params();

	// No return value is needed for an action hook unless you intend to manipulate
	// the arguments passed to subsequent hooked functions (which is generally discouraged
	// for before hooks unless specifically designed for that purpose).
}

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-update-operations.php:31

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

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

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


Scroll to Top