Action Since 5.7 uncanny-automator

automator_recipe_actions_order_updated

Fires when a recipe actions order is updated. Fires when a recipe's actions order is updated, providing the recipe ID and new actions order.

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

Description

Fires after a recipe's action order has been successfully updated via the REST API. Developers can use this hook to perform custom actions based on the new order, such as re-evaluating recipe logic or triggering secondary processes. The hook provides the recipe ID, the new action order array, and a response array.


Usage

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

Parameters

$recipe_id (mixed)
- **$actions_order** `mixed`
$return (mixed)

Examples

add_action( 'automator_recipe_actions_order_updated', 'my_custom_recipe_order_update_handler', 10, 3 );

/**
 * Example handler for the automator_recipe_actions_order_updated hook.
 *
 * This function logs the updated actions order for a recipe and
 * potentially performs additional actions based on the recipe ID or order.
 *
 * @param int   $recipe_id       The ID of the recipe whose actions order was updated.
 * @param array $actions_order   An array representing the new order of actions for the recipe.
 * @param array $return          An array containing data returned by the original function.
 */
function my_custom_recipe_order_update_handler( $recipe_id, $actions_order, $return ) {

	// Log the update for debugging or auditing purposes.
	error_log( sprintf( 'Recipe actions order updated for Recipe ID: %d. New order: %s', $recipe_id, print_r( $actions_order, true ) ) );

	// Example: If a specific recipe is updated, trigger a notification.
	if ( 123 === $recipe_id ) {
		$recipe_object = isset( $return['_recipe'] ) ? $return['_recipe'] : Automator()->get_recipe_object( $recipe_id );
		if ( $recipe_object && $recipe_object->exists() ) {
			// In a real scenario, you'd send an email, trigger another automation, etc.
			// For this example, we'll just log a message.
			error_log( sprintf( 'Special notification needed for Recipe ID: %d (slug: %s) as its actions order was updated.', $recipe_id, $recipe_object->get_slug() ) );

			// You might also want to manipulate the $return array if you needed to
			// pass additional data back to the original REST API response.
			// $return['custom_message'] = 'Special handling applied for this recipe.';
		}
	}

	// Example: If the actions order is empty, perhaps flag the recipe for review.
	if ( empty( $actions_order ) ) {
		error_log( sprintf( 'Warning: Recipe ID: %d has an empty actions order after update.', $recipe_id ) );
		// You could potentially update a custom meta field on the recipe post.
		// update_post_meta( $recipe_id, '_recipe_actions_order_empty', true );
	}

	// This is an action hook, so no return value is expected to alter the original function's output.
	// If this were a filter, you would return the modified $recipe_id, $actions_order, or $return.
}

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:1588

public function update_actions_order( WP_REST_Request $request ) {

		// Make sure we have a recipe ID and the newOrder
		if ( $request->has_param( 'recipe_id' ) && $request->has_param( 'actions_order' ) ) {

			$recipe_id         = absint( $request->get_param( 'recipe_id' ) );
			$actions_order     = $request->has_param( 'actions_order' ) ? $request->get_param( 'actions_order' ) : array();
			$return['message'] = 'The action order array is empty.';
			$return['success'] = false;
			$return['action']  = 'update_actions_order';
			if ( ! empty( $actions_order ) ) {
				// Update the actions menu_order here
				foreach ( $actions_order as $index => $action_id ) {
					Automator()->db->action->update_menu_order( $action_id, ( $index + 1 ) * 10 );
				}
				$return['message'] = 'Updated!';
				$return['success'] = true;
			}

			Automator()->cache->clear_automator_recipe_part_cache( $recipe_id );

			$return['recipes_object'] = Automator()->get_recipes_data( true, $recipe_id );
			$return['_recipe']        = Automator()->get_recipe_object( $recipe_id );

			/**
			 * Fires when a recipe actions order is updated.
			 *
			 * @since 5.7
			 */
			do_action( 'automator_recipe_actions_order_updated', $recipe_id, $actions_order, $return );

			return new WP_REST_Response( $return, 200 );
		}

		$return['message'] = 'Failed to update';
		$return['success'] = false;
		$return['action']  = 'show_error';

		return new WP_REST_Response( $return, 200 );
	}

Scroll to Top