Action Since 5.7 uncanny-automator

automator_recipe_requires_user_updated

Fires when a recipe requires user is updated. Fires when a recipe's user requirement status is updated, passing the recipe ID and new status.

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

Description

Fires after a recipe's 'Requires User' setting is successfully updated. Developers can use this hook to perform custom actions, such as logging changes, triggering notifications, or modifying related user or recipe data based on the new 'Requires User' status. The hook passes the recipe ID, the new requirement status, and the response data.


Usage

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

Parameters

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

Examples

add_action( 'automator_recipe_requires_user_updated', 'my_automator_handle_recipe_user_update', 10, 3 );

/**
 * Example function to handle updates to a recipe's user requirement.
 *
 * This function demonstrates how to react when the 'requires user' setting
 * of an Automator recipe is updated via the REST API. It might log the event,
 * clear a cache, or perform other actions based on the updated recipe.
 *
 * @param int|string $recipe_id      The ID of the recipe that was updated.
 * @param bool       $requires_user  The new value of the 'requires_user' setting (true or false).
 * @param array      $return_data    The data being returned in the REST API response, containing recipe details.
 */
function my_automator_handle_recipe_user_update( $recipe_id, $requires_user, $return_data ) {

	// Check if the recipe ID is valid.
	if ( ! $recipe_id || ! is_numeric( $recipe_id ) ) {
		// Log an error or handle invalid data gracefully.
		error_log( 'automator_recipe_requires_user_updated received an invalid recipe ID: ' . print_r( $recipe_id, true ) );
		return;
	}

	// Get the recipe object from the return data if available.
	$recipe_object = isset( $return_data['_recipe'] ) ? $return_data['_recipe'] : null;

	// Log the update for auditing or debugging purposes.
	$log_message = sprintf(
		'Automator recipe "%s" (ID: %d) has had its "requires user" setting updated to: %s.',
		( $recipe_object && isset( $recipe_object->name ) ) ? $recipe_object->name : 'Unknown Recipe',
		$recipe_id,
		$requires_user ? 'true' : 'false'
	);

	// In a real-world scenario, you might use a dedicated logging function or a plugin.
	// For this example, we'll use WordPress's error_log.
	error_log( $log_message );

	// Example: Clear a specific cache related to this recipe if it requires user interaction.
	if ( $requires_user ) {
		// This is a placeholder. In a real plugin, you'd have a specific caching mechanism.
		// e.g., Automator()->get_cache_manager()->clear_recipe_cache( $recipe_id );
		// For demonstration, let's just simulate clearing a cache.
		// echo "Simulating clearing cache for recipe ID: " . $recipe_id . "<br>";
	}

	// Example: Send a notification to an admin if a critical recipe's user requirement changes.
	// This would require more context about what constitutes a "critical" recipe.
	// if ( is_critical_automator_recipe( $recipe_id ) ) {
	//     wp_mail( '[email protected]', 'Automator Recipe User Requirement Changed', $log_message );
	// }
}

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

public function set_recipe_requires_user( WP_REST_Request $request ) {

		// Make sure we have a post ID and a post status
		if ( $request->has_param( 'recipePostID' ) && $request->has_param( 'requiresUser' ) ) {

			$recipe_id     = absint( $request->get_param( 'recipePostID' ) );
			$requires_user = $request->get_param( 'requiresUser' );
			// Adding user selector
			update_post_meta( $recipe_id, 'recipe_requires_user', $requires_user );
			// User selector is removed
			if ( ! $requires_user ) {
				delete_post_meta( $recipe_id, 'source' );
				delete_post_meta( $recipe_id, 'fields' );
			}

			$return['message'] = 'Updated!';
			$return['success'] = true;
			$return['action']  = 'updated_recipe';
			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 requires user is updated.
			 *
			 * @since 5.7
			 */
			do_action( 'automator_recipe_requires_user_updated', $recipe_id, $requires_user, $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