Action Since 5.7 uncanny-automator

automator_recipe_status_updated

Fires when a recipe status is changed. Added for Scheduled and Delayed actions. Fires when a recipe status is updated, particularly for scheduled and delayed actions.

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

Description

Fires after a recipe's status is updated, particularly for scheduled and delayed actions. Developers can use this hook to perform actions based on the new status, such as clearing import warnings or managing automation caches. It passes the recipe's post ID, recipe ID, new status, and additional return data.


Usage

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

Parameters

$post_id (mixed)
- **$recipe_id** `mixed`
$post_status (mixed)
- **$return** `mixed`

Examples

// Example: Log recipe status changes to a custom log file for debugging.
function my_automator_log_recipe_status_change( $post_id, $recipe_id, $post_status, $return_data ) {

    // Ensure we have the necessary data to log.
    if ( ! empty( $post_id ) && ! empty( $recipe_id ) && isset( $post_status ) ) {

        $log_message = sprintf(
            'Recipe Status Updated: Post ID: %s, Recipe ID: %s, New Status: %s, Return Data Keys: %s',
            $post_id,
            $recipe_id,
            $post_status,
            implode( ', ', array_keys( (array) $return_data ) ) // Log keys from return data as an example.
        );

        // You would typically use a more robust logging solution in production.
        // For this example, we'll simulate writing to a file.
        $log_file_path = WP_CONTENT_DIR . '/automator-recipe-status.log';
        $current_time = current_time( 'mysql' );

        // Append the log message to the file.
        file_put_contents( $log_file_path, "{$current_time} - {$log_message}n", FILE_APPEND );
    }
}
add_action( 'automator_recipe_status_updated', 'my_automator_log_recipe_status_change', 10, 4 );

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

public function change_post_status( WP_REST_Request $request ) {

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

			$status_types = array( 'draft', 'publish' );

			$post_status = sanitize_text_field( $request->get_param( 'post_status' ) );
			$post_id     = absint( $request->get_param( 'post_ID' ) );

			if ( in_array( $post_status, $status_types, true ) && $post_id ) {

				/*
				 * Save human readable sentence that will be stored as trigger and action meta.
				 * Once a trigger is completed, the human readable post meta value will be saved as trigger or action log
				 * meta fr the user to have more detail about it in the logs.
				 */
				if ( $request->has_param( 'sentence_human_readable' ) ) {
					$human_readable = sanitize_text_field( $request->get_param( 'sentence_human_readable' ) );
					update_post_meta( $post_id, 'sentence_human_readable', $human_readable );
				}

				$post = array(
					'ID'          => $post_id,
					'post_status' => $post_status,
				);

				$updated = wp_update_post( $post );

				// Fallback code to add add_action meta in < 3.0 triggers.
				$this->process_post_migratable( $post_id );

				if ( $updated ) {
					$return['message'] = 'Updated!';
					$return['success'] = true;
					$return['action']  = 'updated_post';
					Automator()->cache->clear_automator_recipe_part_cache( $post_id );

					if ( ! $request->has_param( 'recipe_id' ) ) {
						$recipe_id = $post_id;
						if ( AUTOMATOR_POST_TYPE_RECIPE !== get_post_type( $post_id ) ) {
							$ancestors = get_post_ancestors( $post_id );
							$recipe_id = array_pop( $ancestors );
						}
					} else {
						$recipe_id = absint( $request->get_param( 'recipe_id' ) );
					}

					$return['recipes_object'] = Automator()->get_recipes_data( true, $recipe_id );

					$recipe_object = Automator()->get_recipe_object( $recipe_id );

					$return['_recipe'] = $recipe_object;

					/**
					 * Fires when a recipe status is changed. Added for Scheduled and Delayed actions.
					 *
					 * @since 5.7
					 */
					do_action( 'automator_recipe_status_updated', $post_id, $recipe_id, $post_status, $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 );
	}


Internal Usage

Found in src/core/lib/helpers/class-automator-cache-handler.php:67:

add_action( 'automator_recipe_status_updated', array( $this, 'handle_condition_status_update' ), 10, 4 );

Found in src/core/lib/recipe-parts/class-recipe-manifest.php:107:

add_action( 'automator_recipe_status_updated', array( $this, 'on_recipe_option_updated' ) );

Found in src/core/admin/class-import-recipe.php:90:

add_action( 'automator_recipe_status_updated', array( $this, 'clear_imported_recipe_warning' ), 10, 4 );

Found in uncanny-automator-pro/src/core/webhook/class-webhook-index.php:65:

add_action( 'automator_recipe_status_updated', array( $this, 'rebuild' ) );
Scroll to Top