Filter uncanny-automator

automator_transient_name

Filters the transient name used for storing temporary data within the automator.

add_filter( 'automator_transient_name', $callback, 10, 1 );

Description

Allows developers to modify the transient name used for caching automator data. You can alter the default 'automator_transient' string or add custom logic to generate dynamic transient keys based on arguments. This is useful for ensuring unique cache keys or implementing custom caching strategies.


Usage

add_filter( 'automator_transient_name', 'your_function_name', 10, 1 );

Return Value

The filtered value.


Examples

/**
 * Example of how to use the 'automator_transient_name' filter hook.
 * This filter allows you to modify the base transient name used by the Automator plugin.
 *
 * In this example, we'll prepend a prefix based on the current user's role
 * to make the transient unique for different user roles, potentially preventing
 * cache conflicts if different roles view content differently.
 */
add_filter(
	'automator_transient_name',
	function( $transient_name, $args ) {
		// Get the current user.
		$current_user = wp_get_current_user();

		// If no user is logged in, use the default transient name.
		if ( ! $current_user || ! $current_user->exists() || empty( $current_user->roles ) ) {
			return $transient_name;
		}

		// Get the first role of the user.
		$user_role = reset( $current_user->roles );

		// Prepend the user role to the transient name.
		// This makes the transient name unique for each user role.
		$modified_transient_name = sanitize_key( $user_role ) . '_' . $transient_name;

		// You could also incorporate other arguments if they were passed and relevant.
		// For instance, if the second argument was a post ID, you might include it.
		// if ( isset( $args[0] ) && is_numeric( $args[0] ) ) {
		//     $modified_transient_name .= '_' . $args[0];
		// }

		return $modified_transient_name;
	},
	10, // Priority: A standard priority.
	2   // Accepted arguments: The filter provides two arguments.
);

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:239
src/core/lib/helpers/class-automator-cache-handler.php:333
src/core/lib/helpers/class-automator-cache-handler.php:367

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