Action Since 5.8 uncanny-automator

automator_recipe_notes_updated

Fires when recipe notes are updated. Fires after recipe notes are updated, providing the post ID and new notes to any connected functions.

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

Description

Fires after recipe notes are successfully updated via the REST API. Developers can leverage this hook to perform custom actions, such as logging changes, triggering related automations, or updating other parts of the system based on the new notes. It passes the updated recipe's post ID, the new notes content, and a return object.


Usage

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

Parameters

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

Examples

add_action( 'automator_recipe_notes_updated', 'my_automator_recipe_notes_updated_handler', 10, 3 );

/**
 * Example handler for the 'automator_recipe_notes_updated' action.
 * This function logs the updated notes for a specific recipe.
 *
 * @param int    $post_id The ID of the recipe post.
 * @param string $notes   The updated notes for the recipe.
 * @param mixed  $return  The return value from the original action.
 */
function my_automator_recipe_notes_updated_handler( $post_id, $notes, $return ) {
    // Ensure we have a valid post ID and notes.
    if ( ! $post_id || ! is_numeric( $post_id ) || empty( $notes ) ) {
        error_log( 'my_automator_recipe_notes_updated_handler: Invalid or missing data provided.' );
        return;
    }

    // Log the update for debugging or auditing purposes.
    // In a real-world scenario, you might store this in a custom log table,
    // send a notification, or trigger other related actions.
    $log_message = sprintf(
        'Recipe Notes Updated for Post ID: %d. New notes: "%s". Original return: %s',
        $post_id,
        sanitize_textarea_field( $notes ), // Sanitize the notes before logging.
        print_r( $return, true ) // Convert the return value to a string for logging.
    );
    error_log( $log_message );

    // Example: If the return value indicates a successful update,
    // you might want to perform further actions.
    // For instance, if $return is an array and contains 'success' => true.
    if ( is_array( $return ) && isset( $return['success'] ) && true === $return['success'] ) {
        // Example: Trigger another internal process or update related meta data.
        // update_post_meta( $post_id, '_automator_notes_last_updated', current_time( 'mysql' ) );
    }
}

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

public function change_recipe_notes( WP_REST_Request $request ) {

		// Validate we have a post ID and notes is set.
		$post_id = $request->has_param( 'post_ID' ) ? absint( $request->get_param( 'post_ID' ) ) : 0;
		if ( empty( $post_id ) || ! $request->has_param( 'notes' ) ) {
			return new WP_REST_Response(
				array(
					'message' => 'Failed to update',
					'success' => false,
					'action'  => 'show_error',
				),
				200
			);
		}

		// Sanitize the notes.
		$notes = sanitize_textarea_field( trim( $request->get_param( 'notes' ) ) );

		// If the notes are empty, delete the post meta.
		if ( empty( $notes ) ) {
			delete_post_meta( $post_id, 'uap_recipe_notes' );
		} else {
			// Update the post meta.
			update_post_meta( $post_id, 'uap_recipe_notes', $notes );
		}

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

		/**
		 * Fires when recipe notes are updated.
		 *
		 * @since 5.8
		*/
		do_action( 'automator_recipe_notes_updated', $post_id, $notes, $return );

		return new WP_REST_Response( $return, 200 );
	}

Scroll to Top