Action uncanny-automator

automator_cache_maybe_clear_cache_for_recipes

Fires after a post is updated or saved, potentially clearing the cache for relevant recipes.

add_action( 'automator_cache_maybe_clear_cache_for_recipes', $callback, 10, 4 );

Description

Fires after specific post types are saved or updated, potentially clearing the recipes and actionified triggers cache. Developers can hook into this action to perform custom cache clearing operations for recipes or related data, ensuring fresh information is available after changes.


Usage

add_action( 'automator_cache_maybe_clear_cache_for_recipes', 'your_function_name', 10, 4 );

Parameters

$post_id (mixed)
The ID of the post that triggered the cache clearing.
$post (mixed)
This parameter contains the ID of the post that is being created or updated.
$update (mixed)
This parameter contains the post object itself, which is used to check its post type and determine if it's an Automator recipe.
$post_before (mixed)
This parameter indicates whether the post is being updated.

Examples

<?php

/**
 * Example function to hook into 'automator_cache_maybe_clear_cache_for_recipes' action.
 * This function would typically be used to clear specific caches related to recipes
 * when a post of a relevant type is updated or saved.
 *
 * @param int|WP_Post $post_id   The ID of the post being updated, or the WP_Post object itself.
 * @param WP_Post     $post      The current post object.
 * @param bool        $update    Whether this is an update operation.
 * @param WP_Post     $post_before The post object before the update (if $update is true).
 */
function my_automator_clear_recipe_caches( $post_id, $post, $update, $post_before ) {
	// Ensure we have a valid post object
	if ( ! $post instanceof WP_Post ) {
		// Attempt to get the post object if only ID is provided
		$post = get_post( $post_id );
		if ( ! $post instanceof WP_Post ) {
			return; // Cannot proceed without a valid post object
		}
	}

	// Example logic: Only clear cache if it's a specific post type relevant to Automator recipes.
	// Replace 'automator_recipe_type' with the actual post type registered by Automator.
	$relevant_post_types = array( 'automator_recipe', 'automator_trigger', 'automator_action' ); // Example post types

	if ( ! in_array( $post->post_type, $relevant_post_types, true ) ) {
		return; // Not a relevant post type, so no need to clear caches.
	}

	// In a real scenario, you would interact with the Automator cache handler.
	// For demonstration, let's assume a global or accessible $automator_cache_handler object.
	// Replace this with the actual way to access the cache handler in your context.
	if ( class_exists( 'Automator_Cache_Handler' ) ) {
		$cache_handler = Automator_Cache_Handler::get_instance(); // Assuming a static method to get instance

		// Clear specific caches related to recipes.
		// These are examples, actual cache keys might differ.
		$cache_handler->remove( 'automator_recipes_list' );
		$cache_handler->remove( 'automator_recipe_details_' . $post->ID );

		// If this is an update, you might also want to clear caches related to the previous state.
		if ( $update && $post_before instanceof WP_Post ) {
			$cache_handler->remove( 'automator_recipe_details_' . $post_before->ID );
		}

		// You might also trigger a broader cache invalidation if necessary.
		// $cache_handler->clear_all_related_to_recipes();
	}

	// You could also perform other actions here, like logging or clearing transient data.
	error_log( sprintf( 'Automator cache cleared for recipe post ID: %d (Update: %s)', $post->ID, var_export( $update, true ) ) );
}
add_action( 'automator_cache_maybe_clear_cache_for_recipes', 'my_automator_clear_recipe_caches', 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/lib/helpers/class-automator-cache-handler.php:267

public function maybe_clear_cache_for_recipes( $post_id, $post, $update, $post_before ) {

		$automator_types = automator_get_recipe_post_types();

		// If it's not an Automator post type, return.
		if ( ! in_array( $post->post_type, $automator_types, true ) ) {
			return;
		}

		// Clear recipes data cache
		$this->remove( $this->recipes_data );
		$this->remove( 'automator_actionified_triggers' );
		do_action( 'automator_cache_maybe_clear_cache_for_recipes', $post_id, $post, $update, $post_before );
	}


Scroll to Top