Action Since 5.7 uncanny-automator

automator_recipe_completions_allowed_updated

Fires when a recipe completions allowed is updated. Fires when a recipe's completion limit is updated, providing the post ID and new limit.

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

Description

Fires after the "completions allowed" setting for a recipe is successfully updated via the REST API. Developers can use this hook to react to changes in recipe limits, perhaps by logging the update, notifying administrators, or triggering other related automations. The $post_id, new $recipe_completions_allowed value, and the full $return payload are provided.


Usage

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

Parameters

$post_id (mixed)
- **$recipe_completions_allowed** `mixed`
$return (mixed)

Examples

<?php
/**
 * Example function to hook into 'automator_recipe_completions_allowed_updated'.
 * This function logs the recipe ID and the new completions allowed value.
 * It also demonstrates accessing the $return array to potentially modify data before it's returned by the REST API.
 *
 * @param int    $post_id                      The ID of the recipe post.
 * @param int    $recipe_completions_allowed   The new value for recipe completions allowed.
 * @param array  $return                     The array containing data to be returned by the REST API.
 */
function my_automator_log_recipe_completions_update( $post_id, $recipe_completions_allowed, $return ) {
    // Log the update for debugging or auditing purposes.
    // In a real-world scenario, you might use WP_Error, a custom logging service, or the WordPress error log.
    error_log( sprintf(
        'Automator: Recipe completions updated for Recipe ID %d. New completions allowed: %d',
        $post_id,
        $recipe_completions_allowed
    ) );

    // Example of accessing and potentially modifying the $return array.
    // Let's say we want to add a custom status message to the response.
    if ( isset( $return['_recipe'] ) && is_object( $return['_recipe'] ) ) {
        $return['custom_message'] = sprintf(
            'Recipe "%s" has had its completion limit updated to %d.',
            $return['_recipe']->get_title(),
            $recipe_completions_allowed
        );
    }

    // This is an action hook, so no return value is expected or used by the original function.
    // However, if you were to accidentally return a value, it would be ignored by do_action().
}

// Add the action hook. We are expecting 3 arguments.
// The priority is set to 10, which is the default and generally fine unless you need to run before or after other hooks.
add_action( 'automator_recipe_completions_allowed_updated', 'my_automator_log_recipe_completions_update', 10, 3 );
?>

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

public function recipe_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_completions_allowed', $recipe_completions_allowed );
			Automator()->cache->clear_automator_recipe_part_cache( $post_id );

			$return['message']        = 'Updated!';
			$return['success']        = true;
			$return['action']         = 'updated_recipe_completions_allowed';
			$return['recipes_object'] = Automator()->get_recipes_data( true, $post_id );
			$return['_recipe']        = Automator()->get_recipe_object( $post_id );

			/**
			 * Fires when a recipe completions allowed is updated.
			 *
			 * @since 5.7
			 */
			do_action( 'automator_recipe_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 );
	}

Scroll to Top