Action uncanny-automator

automator_recipe_part_copy_meta_value

Fires after a recipe part's meta value is copied, passing the key, value, original post ID, and new post ID.

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

Description

Fires after a recipe part's meta value has been copied from an old post to a new one. Developers can use this action to perform custom actions or modify the copied meta value before it's saved to the new post, providing fine-grained control over recipe duplication.


Usage

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

Parameters

$key (mixed)
The `$key` parameter contains the meta key of the post metadata being copied.
$value (mixed)
This parameter represents the meta key for the metadata being copied.
$post_id (mixed)
This parameter contains the value of the meta field being copied.
$new_post_id (mixed)
This parameter contains the ID of the original post from which the recipe part is being copied.

Examples

add_action( 'automator_recipe_part_copy_meta_value', 'my_automator_handle_copied_meta', 10, 4 );

/**
 * Logs or processes meta values when a recipe part is copied.
 *
 * This function demonstrates how to hook into the `automator_recipe_part_copy_meta_value`
 * action to perform custom actions when a meta value is copied from an old post
 * to a new post during recipe part duplication.
 *
 * @param mixed $key The meta key of the copied value.
 * @param mixed $value The original value of the meta key.
 * @param int   $post_id The ID of the original post.
 * @param int   $new_post_id The ID of the new post where the meta is copied.
 */
function my_automator_handle_copied_meta( $key, $value, $post_id, $new_post_id ) {
    // Example: Log the meta key and new post ID for debugging purposes.
    // In a real-world scenario, you might want to:
    // - Perform specific actions based on the meta key.
    // - Update other related meta on the new post.
    // - Trigger further automations.

    if ( 'my_custom_trigger_setting' === $key ) {
        // If it's a specific meta key related to a custom trigger,
        // maybe we need to re-initialize or process it for the new post.
        error_log( "Automator: Meta key '{$key}' copied from post {$post_id} to new post {$new_post_id}. Original value: " . print_r( $value, true ) );

        // Example: If the copied meta requires some processing for the new post.
        // This is hypothetical, as actual processing would depend on the meta's purpose.
        $processed_value = maybe_unserialize( $value );
        // Perform some operations on $processed_value if needed.
        // update_post_meta( $new_post_id, $key, $processed_value ); // If reprocessing is necessary
    } else {
        // Log other meta keys being copied for general monitoring.
        error_log( "Automator: Copied meta key '{$key}' from post {$post_id} to new post {$new_post_id}." );
    }
}

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:390

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 );
		}
	}


Scroll to Top