Filter uncanny-automator

automator_recipe_copy_modify_tokens

Filters the content of a duplicated Automator recipe, allowing modification of tokens before saving the new recipe.

add_filter( 'automator_recipe_copy_modify_tokens', $callback, 10, 2 );

Description

This filter allows modification of recipe content during the copy process. Developers can alter tokens before they are saved in the new recipe. It fires after potential loop token replacements but before the content is encoded or decoded, offering fine-grained control over token values when duplicating recipes.


Usage

add_filter( 'automator_recipe_copy_modify_tokens', 'your_function_name', 10, 2 );

Parameters

$content (mixed)
This parameter contains the content or tokens that need to be potentially modified.
$new_post_id (mixed)
This parameter contains the content of the recipe that needs to be modified.

Return Value

The filtered value.


Examples

/**
 * Example function to modify recipe tokens when copying a recipe part.
 * This example demonstrates how to replace a specific placeholder token
 * with a dynamically generated value based on the new post ID.
 *
 * @param mixed $content The content being processed, potentially containing tokens.
 * @param int   $new_post_id The ID of the new post (recipe part) being created.
 * @return mixed The modified content.
 */
add_filter( 'automator_recipe_copy_modify_tokens', 'my_custom_recipe_token_modifier', 10, 2 );

function my_custom_recipe_token_modifier( $content, $new_post_id ) {
	// Example: Replace a placeholder token like The `automator_recipe_copy_modify_tokens` hook allows you to **modify or add custom tokens** that are available during the duplication or copying of an Automator recipe. with the actual new post ID.
	// This could be useful if you want to pre-populate certain fields with the new recipe's ID
	// during the copying process.
	$placeholder_token = 'The `automator_recipe_copy_modify_tokens` hook allows you to **modify or add custom tokens** that are available during the duplication or copying of an Automator recipe.';

	if ( is_string( $content ) && strpos( $content, $placeholder_token ) !== false ) {
		$content = str_replace( $placeholder_token, $new_post_id, $content );
	}

	// You could add more complex logic here, for example:
	// - Inspecting the $content to identify specific token types.
	// - Fetching additional data based on $new_post_id.
	// - Performing conditional replacements.

	return $content;
}

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/admin/class-copy-recipe-parts.php:473

public function modify_tokens( $content, $new_post_id = 0 ) {

		if ( empty( $content ) ) {
			return $content;
		}

		//Check if it's a webhook URL
		if ( 0 !== $new_post_id && ! is_array( $content ) && ! is_object( $content ) && preg_match( '//wp-json/uap/v2/uap-/', $content ) ) {
			// Only modify webhook URL of the trigger. We are leaving webhook URL of action alone.
			if ( AUTOMATOR_POST_TYPE_TRIGGER === get_post_type( $new_post_id ) ) {
				$new     = sprintf( 'uap/v2/uap-%d-%d', wp_get_post_parent_id( $new_post_id ), $new_post_id );
				$content = preg_replace( '/uap/v2/uap-.+/', $new, $content );
			}
		}

		// Check if it's an object or array : Google sheets etc.
		$encoded      = is_array( $content ) || is_object( $content );
		$decode_param = is_array( $content ); // Decode as array if it was an array
		$content      = $encoded
			? wp_json_encode( $content, JSON_UNESCAPED_UNICODE )
			: $content;

		// Check if any replaceable token exists
		if ( false === $this->token_exists_in_content( $content ) ) {
			// Check if we need to decode.
			return $encoded ? json_decode( $content, $decode_param ) : $content;
		}

		// Replace if trigger token exists
		if ( ! empty( $this->trigger_tokens ) ) {
			$content = $this->replace_trigger_token_ids( $content );
		}

		// Replace if action token exists
		if ( ! empty( $this->action_tokens ) ) {
			$content = $this->replace_action_token_ids( $content );
		}

		// Replace if loop token exists
		if ( ! empty( $this->loop_tokens ) ) {
			$content = $this->replace_loop_token_ids( $content );
		}

		// Add filter for special tokens.
		$content = apply_filters( 'automator_recipe_copy_modify_tokens', $content, $new_post_id );

		// Check if we need to decode.
		return $encoded ? json_decode( $content, $decode_param ) : $content;
	}

Internal Usage

Found in src/core/admin/class-copy-recipe-parts.php:59:

add_filter( 'automator_recipe_copy_modify_tokens', array( $this, 'handle_data_loop_tokens' ), 10, 2 );
Scroll to Top