Action
uncanny-automator
automator_cache_recipe_post_status_changed
Fires when the post status of a recipe in Automator changes.
add_action( 'automator_cache_recipe_post_status_changed', $callback, 10, 1 );
Description
Fires when a recipe's post status changes, triggering cache invalidation for related data. Developers can hook into this to clear additional caches or perform custom actions after a recipe's status update. The `$args` parameter can contain information about the changed recipe, if provided.
Usage
add_action( 'automator_cache_recipe_post_status_changed', 'your_function_name', 10, 1 );
Parameters
-
$args(mixed) - This parameter contains the new status of the post that was changed.
Examples
<?php
/**
* Handle the clearing of recipe-related caches when a recipe's post status changes.
*
* This is a realistic example of how you might hook into the
* 'automator_cache_recipe_post_status_changed' action to perform
* specific actions when a recipe's status is updated. In this case,
* it demonstrates clearing various caches that are relevant to recipes.
*
* @param array $args An array containing information about the changed recipe,
* typically including 'post_id' and 'old_status', 'new_status'.
*/
function my_automator_clear_recipe_caches_on_status_change( $args ) {
// Ensure we have the necessary arguments to proceed.
if ( ! is_array( $args ) || ! isset( $args['post_id'] ) ) {
error_log( 'automator_cache_recipe_post_status_changed hook received invalid arguments.' );
return;
}
$recipe_id = absint( $args['post_id'] );
$old_status = isset( $args['old_status'] ) ? sanitize_key( $args['old_status'] ) : '';
$new_status = isset( $args['new_status'] ) ? sanitize_key( $args['new_status'] ) : '';
// Example: Log the status change for debugging purposes.
error_log( sprintf( 'Recipe ID %d status changed from "%s" to "%s". Clearing relevant caches.', $recipe_id, $old_status, $new_status ) );
// In a real-world scenario, you'd likely have a class or helper functions
// to manage cache clearing. For demonstration, we'll use placeholder logic.
// Placeholder for clearing specific recipe data caches.
// Replace 'my_automator_cache_handler' with your actual cache handler class/instance.
if ( class_exists( 'My_Automator_Cache_Handler' ) ) {
$cache_handler = new My_Automator_Cache_Handler();
$cache_handler->clear_recipe_data_cache( $recipe_id );
$cache_handler->clear_recipe_specific_transients( $recipe_id );
} else {
// Fallback or alternative cache clearing if the handler isn't available.
// Example: WordPress transients.
delete_transient( 'my_automator_recipe_details_' . $recipe_id );
delete_transient( 'my_automator_recipe_settings_' . $recipe_id );
}
// If the recipe was published or unpublished, you might need to refresh
// global caches related to active recipes.
if ( $old_status !== $new_status ) {
if ( 'publish' === $new_status || 'publish' === $old_status ) {
// Example: Clear a transient that lists all active recipes.
delete_transient( 'my_automator_all_active_recipes' );
}
}
}
// Add the action hook with a priority of 10 and accepting 1 argument ($args).
add_action( 'automator_cache_recipe_post_status_changed', 'my_automator_clear_recipe_caches_on_status_change', 10, 1 );
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/lib/helpers/class-automator-cache-handler.php:346
public function recipe_post_status_changed( ...$args ) {
if ( 1 === count( $args ) ) {
return;
}
$new_status = $args[0];
$old_status = $args[1];
$post = $args[2];
if ( ! $post instanceof WP_Post ) {
return;
}
// prepare transient key.
$transient_key = apply_filters( 'automator_transient_name', 'automator_transient', array() );
// suffix post type is needed.
$transient_key .= md5( wp_json_encode( $post->post_type ) );
$this->remove( $transient_key );
// Clear recipes data cache
$this->remove( $this->recipes_data );
$this->remove( 'automator_actionified_triggers' );
$this->remove( 'automator_integration_directories_loaded' );
$this->remove( 'automator_get_all_integrations' );
$this->remove( 'get_recipe_type' );
do_action( 'automator_cache_recipe_post_status_changed', $args );
}
Internal Usage
Found in src/core/lib/helpers/class-automator-cache-handler.php:68:
add_action( 'automator_cache_recipe_post_status_changed', array( $this, 'handle_condition_status_update_legacy' ), 10, 1 );
Found in uncanny-automator-pro/src/core/includes/automator-pro-cache-handler.php:25:
add_action( 'automator_cache_recipe_post_status_changed', array( $this, 'recipe_post_status_changed' ) );