Filter uncanny-automator

automator_recipe_part_meta_value

Filters the metadata value for a recipe part, allowing modification before saving to the database.

add_filter( 'automator_recipe_part_meta_value', $callback, 10, 4 );

Description

Filters the meta value of a recipe part when copying or migrating a recipe. Developers can modify the meta value, for instance, to adjust URLs or update internal references, before it's saved to the new recipe part. This hook provides control over how individual meta fields are handled during these processes.


Usage

add_filter( 'automator_recipe_part_meta_value', 'your_function_name', 10, 4 );

Parameters

$val (mixed)
The `$val` parameter contains the current meta value being processed, which can be filtered by the hook.
$post_id (mixed)
This parameter represents the meta value that is being filtered.
$new_post_id (mixed)
This parameter holds the ID of the original post or recipe part being copied.
$key (mixed)
This parameter holds the ID of the newly created post when copying a recipe, which is used to correctly associate the copied meta values with the new post.

Return Value

The filtered value.


Examples

/**
 * Modify the value of a recipe part meta.
 *
 * This filter allows developers to alter the meta value before it's saved
 * when a recipe is being copied or imported. For example, you might want to
 * ensure that specific types of data, like external URLs, are updated to
 * reflect the new recipe's context.
 *
 * @param mixed  $val          The current meta value.
 * @param int    $post_id      The ID of the original recipe part.
 * @param int    $new_post_id  The ID of the new (copied/imported) recipe part.
 * @param string $key          The meta key of the recipe part.
 *
 * @return mixed The modified meta value.
 */
add_filter( 'automator_recipe_part_meta_value', function( $val, $post_id, $new_post_id, $key ) {
    // Example: If this meta key relates to a specific webhook URL and we are copying
    // the recipe, we might want to clear out the old webhook URL so the user
    // is prompted to enter a new one for the duplicated recipe.
    if ( 'webhook_url' === $key && ! empty( $val ) ) {
        // For demonstration, let's assume we want to clear it.
        // In a real scenario, you might have logic to generate a new placeholder or prompt.
        $val = '';
    }

    // Example: If the meta value contains an array of user IDs and we are copying,
    // we might want to ensure the user IDs are still valid in the new context,
    // or perhaps remove them if the action should not be re-assigned.
    if ( is_array( $val ) && in_array( 'user_ids', $val ) ) { // Hypothetical check for a key indicating user IDs array
        // For demonstration, let's assume we want to remove the user_ids entry if present.
        // In reality, you'd iterate and validate/modify specific IDs.
        $val = array_diff_key( $val, array_flip( array_filter( $val, 'is_numeric' ) ) );
    }

    // Always return the value, whether modified or not.
    return $val;
}, 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/admin/class-copy-recipe-parts.php:384

public function copy_recipe_metas( $post_id, $new_post_id, $post_parent = 0, $post_meta = array() ) {

		if ( empty( $post_meta ) ) {
			$post_meta = get_post_meta( $post_id );
		}

		foreach ( $post_meta as $key => $value ) {
			if ( 'automator_duplicated_from' === $key ) {
				continue;
			}

			// Update Automator plugin version
			if ( 'uap_recipe_version' === $key || 'uap_trigger_version' === $key || 'uap_action_version' === $key ) {
				update_post_meta( $new_post_id, $key, AUTOMATOR_PLUGIN_VERSION );
				continue;
			}

			// Updating Magic link and Magic button IDs
			if ( 'ANONWPMAGICLINK' === $key || 'WPMAGICLINK' === $key || 'WPMAGICBUTTON' === $key || 'ANONWPMAGICBUTTON' === $key ) {
				update_post_meta( $new_post_id, $key, $new_post_id );
				continue;
			}

			$val = isset( $value[0] ) ? maybe_unserialize( $value[0] ) : '';

			// Stash action conditions until end of process.
			if ( self::ACTION_CONDITIONS_META_KEY === $key ) {
				$this->action_conditions[ $new_post_id ] = $val;
				continue;
			}

			// Check if we should be modifying this meta key.
			$skip_meta_key = in_array( $key, $this->do_not_modify_meta_keys, true );
			if ( ! $skip_meta_key ) {
				// Replace IDs in tokens
				$val = $this->modify_tokens( $val, $new_post_id );
			}

			// Pass it thru a filter
			$val = apply_filters( 'automator_recipe_part_meta_value', $val, $post_id, $new_post_id, $key );

			// any remaining meta
			update_post_meta( $new_post_id, $key, $val );

			// Action to hook into meta
			do_action( 'automator_recipe_part_copy_meta_value', $key, $value, $post_id, $new_post_id );
		}
	}


Internal Usage

Found in src/core/migrations/class-migrate-nested-tokens.php:19:

add_filter( 'automator_recipe_part_meta_value', array( $this, 'replace_strings_in_imports' ), 10, 4 );

Found in src/core/migrations/class-migrate-nested-tokens-pro.php:19:

add_filter( 'automator_recipe_part_meta_value', array( $this, 'replace_strings_in_imports' ), 10, 4 );

Found in src/core/admin/class-import-recipe.php:286:

add_filter( 'automator_recipe_part_meta_value', array( $this, 'maybe_adjust_hardcoded_meta_urls' ), 10, 4 );
Scroll to Top