Action uncanny-automator

automator_cache_maybe_clear_cache_for_posts

Fires when a post is saved or updated to potentially clear its cache.

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

Description

Fires when a post is saved or updated, potentially clearing related automator cache entries. Developers can use this to hook into post save events and perform custom cache invalidation logic for automator functionality. The hook passes the post ID, post object, update status, and post data before the update.


Usage

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

Parameters

$post_id (mixed)
The ID of the post that triggered the cache clearing action.
$post (mixed)
The ID of the post that triggered the cache clearing action.
$update (mixed)
This parameter contains the post object being processed.
$post_before (mixed)
This parameter indicates whether the post is being updated.

Examples

<?php

/**
 * Clear specific Automator cache entries when a post is updated or created.
 * This function is hooked into the 'automator_cache_maybe_clear_cache_for_posts' action.
 *
 * @param int $post_id The ID of the post that was updated or created.
 * @param WP_Post $post The WP_Post object for the current post.
 * @param bool $update Whether this is an update operation.
 * @param WP_Post|null $post_before The WP_Post object before the update, if any.
 */
function my_automator_clear_post_cache_on_save( $post_id, $post, $update, $post_before ) {

    // If this is an autosave, do nothing.
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }

    // If it's not a post that Automator is likely to cache, bail early.
    // This is a hypothetical check; you'd adjust this based on what Automator actually caches.
    $supported_post_types = array( 'post', 'page', 'custom_post_type_for_automator' );
    if ( ! in_array( $post->post_type, $supported_post_types ) ) {
        return;
    }

    // Example: If the post title has changed, or if it's a new post,
    // we might want to clear a specific cache related to that post's content.
    $should_clear_specific_cache = false;

    if ( ! $update ) {
        // It's a new post, always clear related cache.
        $should_clear_specific_cache = true;
    } elseif ( $update && $post_before instanceof WP_Post ) {
        // It's an update, check if the title changed.
        if ( $post->post_title !== $post_before->post_title ) {
            $should_clear_specific_cache = true;
        }

        // You could add more conditions here, e.g., if a custom field used by Automator changed.
        // For instance, if Automator uses a custom field like 'automator_trigger_data':
        // $old_trigger_data = get_post_meta( $post_id, 'automator_trigger_data', true );
        // $new_trigger_data = get_post_meta( $post_id, 'automator_trigger_data', true ); // Note: meta might not be available yet if saved later in the process
        // if ( $old_trigger_data !== $new_trigger_data ) {
        //     $should_clear_specific_cache = true;
        // }
    }

    if ( $should_clear_specific_cache ) {
        // Construct a cache key specific to this post.
        // This should ideally mirror how Automator generates its keys to ensure removal.
        // The exact key generation logic is crucial and depends on the Automator plugin's implementation.
        // For demonstration, let's assume a pattern like 'automator_post_cache_' . $post_id . '_' . md5($post->post_title)
        // In a real scenario, you would inspect Automator's `remove()` method or other cache clearing functions.

        // A more robust approach might involve filtering the key if Automator provides such a filter.
        $transient_key_base = apply_filters( 'my_automator_specific_post_transient_name', 'automator_post_cache', $post_id, $post );

        // Example: Use the post ID and a hash of its content or title for a more specific key.
        $specific_transient_key = $transient_key_base . '_' . $post_id . '_' . md5( $post->post_title . $post->post_modified );

        // Remove the specific cache entry. You'll need access to Automator's cache removal mechanism.
        // Assuming Automator has a static method or a global function for this, or you can access its class instance.
        // For this example, we'll use a placeholder call. In reality, you'd call the appropriate method.
        // Example: AutomatorCacheHandler::get_instance()->remove( $specific_transient_key );
        // Or if it's a function:
        // automator_cache_remove( $specific_transient_key );

        // If you don't have direct access to Automator's internal cache removal functions,
        // you might need to rely on WordPress's transient API if Automator uses it directly.
        delete_transient( $specific_transient_key );

        error_log( "Automator cache cleared for post ID: {$post_id} with key: {$specific_transient_key}" );
    }
}
add_action( 'automator_cache_maybe_clear_cache_for_posts', 'my_automator_clear_post_cache_on_save', 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:246

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

		// If it's post update, return
		if ( $update ) {
			return;
		}

		// If it's Automator post type, return
		if ( in_array( $post->post_type, automator_get_recipe_post_types(), true ) ) {
			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 );

		do_action( 'automator_cache_maybe_clear_cache_for_posts', $post_id, $post, $update, $post_before );
	}


Scroll to Top