Action
Since 5.7
uncanny-automator
automator_recipe_action_duplicated
Fires when a recipe action is duplicated. Fires when a recipe action is duplicated, providing the recipe and return values to the hook.
add_action( 'automator_recipe_action_duplicated', $callback, 10, 1 );
Description
Fires after a recipe action has been successfully duplicated. Developers can use this hook to perform custom actions, such as logging the duplication, updating related data, or triggering further automation based on the duplicated action. The `$recipe_id` and `$return` parameters provide access to the recipe and its associated data.
Usage
add_action( 'automator_recipe_action_duplicated', 'your_function_name', 10, 1 );
Parameters
-
$recipe_id(mixed) - - **$return** `mixed`
Examples
add_action( 'automator_recipe_action_duplicated', 'my_automator_handle_recipe_action_duplicate', 10, 2 );
/**
* Logs the ID of a duplicated recipe action and potentially sends a notification.
*
* This function is hooked into the 'automator_recipe_action_duplicated' action.
* It receives the ID of the duplicated recipe and an array containing data
* about the duplicated recipe. In this example, it logs the recipe ID and
* could be extended to send an email notification to an admin.
*
* @param int $recipe_id The ID of the duplicated recipe.
* @param array $return An array containing data related to the duplicated recipe.
*/
function my_automator_handle_recipe_action_duplicate( $recipe_id, $return ) {
// Log the recipe ID that was duplicated for debugging or auditing purposes.
error_log( "Automator: Recipe action duplicated. Recipe ID: " . $recipe_id );
// You could also check if the duplicated recipe has specific properties
// and perform further actions. For example, if you wanted to track
// how many times a specific type of recipe is duplicated.
if ( isset( $return['_recipe'] ) && $return['_recipe'] instanceof Uncanny_AutomatorRecipe ) {
$duplicated_recipe_object = $return['_recipe'];
$recipe_type = $duplicated_recipe_object->get_type(); // Assuming a get_type() method exists
error_log( "Automator: Duplicated recipe type: " . $recipe_type );
// Example: Send an email notification if a specific recipe type is duplicated frequently.
// This is just a placeholder and would require actual email sending logic.
// if ( $recipe_type === 'some_specific_trigger_type' ) {
// wp_mail( '[email protected]', 'Specific Recipe Duplicated', 'A recipe of type ' . $recipe_type . ' with ID ' . $recipe_id . ' was duplicated.' );
// }
}
}
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:1696
public function duplicate_action( WP_REST_Request $request ) {
// Make sure we have a recipe ID and the newOrder
if ( ! $request->has_param( 'recipe_id' ) || ! $request->has_param( 'action_id' ) ) {
$return['message'] = 'Recipe or Action ID is empty';
$return['success'] = false;
$return['action'] = 'show_error';
return new WP_REST_Response( $return, 400 );
}
$recipe_id = absint( $request->get_param( 'recipe_id' ) );
$action_id = $request->get_param( 'action_id' );
$action_post = get_post( $action_id );
if ( ! $action_post instanceof WP_Post ) {
$return['message'] = 'Action does not exist';
$return['success'] = false;
$return['action'] = 'show_error';
return new WP_REST_Response( $return, 400 );
}
if ( AUTOMATOR_POST_TYPE_ACTION !== $action_post->post_type ) {
$return['message'] = 'Action is not of the correct post type';
$return['success'] = false;
$return['action'] = 'show_error';
return new WP_REST_Response( $return, 400 );
}
if ( ! automator_duplicate_recipe_part( $action_id, $recipe_id, 'draft' ) ) {
$return['message'] = 'Something went wrong';
$return['success'] = false;
$return['action'] = 'show_error';
return new WP_REST_Response( $return, 400 );
}
Automator()->cache->clear_automator_recipe_part_cache( $recipe_id );
$return = array();
$return['success'] = true;
$return['post_ID'] = $recipe_id;
$return['action'] = 'add-new-action';
$return['recipes_object'] = Automator()->get_recipes_data( true, $recipe_id );
$return['_recipe'] = Automator()->get_recipe_object( $recipe_id );
/**
* Fires when a recipe action is duplicated.
*
* @since 5.7
*/
do_action( 'automator_recipe_action_duplicated', $recipe_id, $return );
return new WP_REST_Response( $return, 200 );
}