Action
Since 5.7
uncanny-automator
automator_recipe_max_completions_allowed_updated
Fires when a recipe max completions allowed is updated. Fires when a recipe's maximum completions allowed setting is updated, passing the recipe ID and new value.
add_action( 'automator_recipe_max_completions_allowed_updated', $callback, 10, 2 );
Description
Fires after a recipe's maximum completions allowed setting has been successfully updated, typically via the REST API. Developers can hook into this action to perform custom logic, such as logging the change, invalidating caches, or triggering additional automations based on the updated completion limit for the specified recipe ID.
Usage
add_action( 'automator_recipe_max_completions_allowed_updated', 'your_function_name', 10, 2 );
Parameters
-
$post_id(mixed) - - **$recipe_completions_allowed** `mixed`
-
$return(mixed)
Examples
add_action( 'automator_recipe_max_completions_allowed_updated', 'my_custom_recipe_completion_logging', 10, 3 );
/**
* Logs when a recipe's max completions allowed is updated.
*
* This function is triggered after the max completions for a WordPress Automator recipe
* has been successfully updated via the REST API. It demonstrates how to access
* the recipe ID, the new completion limit, and the response data.
*
* @param int|WP_Error $post_id The ID of the recipe post, or a WP_Error object on failure.
* @param int $recipe_completions_allowed The new maximum number of completions allowed for the recipe.
* @param array $return The data returned by the REST API request.
*/
function my_custom_recipe_completion_logging( $post_id, $recipe_completions_allowed, $return ) {
// Ensure the $post_id is valid before proceeding.
if ( is_wp_error( $post_id ) || ! $post_id ) {
// Log the error if the recipe ID is invalid.
error_log( 'Failed to log recipe completion update due to invalid post ID: ' . ( is_wp_error( $post_id ) ? $post_id->get_error_message() : 'no post ID' ) );
return;
}
// Get the recipe object for more detailed information.
$recipe = Automator()->get_recipe_object( $post_id );
// Check if we successfully retrieved the recipe object.
if ( ! $recipe || is_wp_error( $recipe ) ) {
error_log( 'Failed to retrieve recipe object for post ID ' . $post_id . ' during completion update logging.' );
return;
}
// You can now use the $post_id, $recipe_completions_allowed, and $return data.
// For example, we can log this information to the WordPress debug log.
$log_message = sprintf(
'Recipe "%s" (ID: %d) max completions updated to %d. API response: %s',
$recipe->get_title(),
$post_id,
$recipe_completions_allowed,
print_r( $return, true ) // Use print_r to log array data.
);
// Use WordPress's built-in error logging function.
error_log( $log_message );
// You could also:
// - Update a custom database table with this information.
// - Send a notification to an administrator.
// - Trigger another action based on this update.
}
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:1251
public function recipe_max_completions_allowed( WP_REST_Request $request ) {
// Make sure we have a post ID and a post status
if ( $request->has_param( 'post_ID' ) && absint( $request->get_param( 'post_ID' ) ) && $request->has_param( 'recipe_completions_allowed' ) ) {
$recipe_completions_allowed = sanitize_text_field( $request->get_param( 'recipe_completions_allowed' ) );
$post_id = absint( $request->get_param( 'post_ID' ) );
if ( '-1' === $recipe_completions_allowed ) {
$recipe_completions_allowed = - 1;
} elseif ( is_numeric( $recipe_completions_allowed ) ) {
$recipe_completions_allowed = absint( $recipe_completions_allowed );
} else {
$recipe_completions_allowed = 1;
}
update_post_meta( $post_id, 'recipe_max_completions_allowed', $recipe_completions_allowed );
Automator()->cache->clear_automator_recipe_part_cache( $post_id );
$return['message'] = 'Updated!';
$return['success'] = true;
$return['action'] = 'updated_recipe_max_completions_allowed';
$return['recipes_object'] = Automator()->get_recipes_data( true, $post_id );
$return['_recipe'] = Automator()->get_recipe_object( $post_id );
/**
* Fires when a recipe max completions allowed is updated.
*
* @since 5.7
*/
do_action( 'automator_recipe_max_completions_allowed_updated', $post_id, $recipe_completions_allowed, $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 );
}