Action
uncanny-automator
automator_cache_clear_automator_recipe_part_cache
Fires after an Automator recipe part's cache is cleared, allowing for custom actions based on the cleared recipe part ID.
add_action( 'automator_cache_clear_automator_recipe_part_cache', $callback, 10, 1 );
Description
Fired after individual parts of an Automator recipe's cache are cleared for a specific recipe ID. Developers can use this hook to perform additional cache invalidation or trigger other related actions when a recipe's cached data is refreshed. It ensures consistency across your Automator setup.
Usage
add_action( 'automator_cache_clear_automator_recipe_part_cache', 'your_function_name', 10, 1 );
Parameters
-
$post_id(mixed) - The ID of the post (likely a recipe) for which to clear the cached data.
Examples
<?php
/**
* Clear specific parts of the Automator recipe cache when a certain action occurs.
*
* This function is hooked into 'automator_cache_clear_automator_recipe_part_cache'
* and is responsible for clearing cached data related to a specific recipe part
* based on the provided post ID.
*
* @param int $recipe_id The ID of the recipe for which to clear cache parts.
*/
function my_automator_clear_recipe_part_cache( $recipe_id ) {
// Ensure the post ID is a valid integer.
if ( ! is_numeric( $recipe_id ) || intval( $recipe_id ) <= 0 ) {
return;
}
$recipe_id = absint( $recipe_id );
// Example: Clear cached data for triggers associated with this recipe.
// In a real scenario, you might have a dedicated cache handler or helper class.
$trigger_cache_key = 'automator_recipe_triggers_for_' . $recipe_id;
// Assuming you have a function like 'my_automator_delete_cache' to handle cache deletion.
// This could be a wrapper around WP_Object_Cache or a custom cache system.
my_automator_delete_cache( $trigger_cache_key );
// Example: Clear cached data for actions associated with this recipe.
$action_cache_key = 'automator_recipe_actions_for_' . $recipe_id;
my_automator_delete_cache( $action_cache_key );
// Example: Clear cached data for conditions associated with this recipe.
$condition_cache_key = 'automator_recipe_conditions_for_' . $recipe_id;
my_automator_delete_cache( $condition_cache_key );
// Log the cache clearing action for debugging purposes.
error_log( "Automator recipe part cache cleared for recipe ID: " . $recipe_id );
}
// Add the action hook with the correct number of arguments (1 for $recipe_id).
add_action( 'automator_cache_clear_automator_recipe_part_cache', 'my_automator_clear_recipe_part_cache', 10, 1 );
/**
* Placeholder for a custom cache deletion function.
* In a real plugin, this would interact with WordPress object cache or a custom solution.
*
* @param string $cache_key The cache key to delete.
*/
function my_automator_delete_cache( $cache_key ) {
// Example using WordPress Transients API (can be slower for frequent operations)
// delete_transient( $cache_key );
// Example using WordPress Object Cache API (preferred for performance)
if ( function_exists( 'wp_cache_delete' ) ) {
wp_cache_delete( $cache_key, 'automator_cache_group' ); // Using a custom cache group is recommended.
} else {
// Fallback for environments without object cache enabled.
// This might involve database operations or simply doing nothing if not critical.
}
}
?>
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:316
public function clear_automator_recipe_part_cache( $post_id ) {
$key = $this->recipes_data;
$this->remove( $key );
// Reset recipe ID cache
$key = 'automator_recipe_data_of_' . $post_id;
$this->remove( $key );
$this->remove( 'get_recipe_type' );
do_action( 'automator_cache_clear_automator_recipe_part_cache', $post_id );
}