Copy_Recipe_Parts::modify_conditions( $content,  $post_id,  $new_post_id )


Parameters Parameters

$content

(Required)

$post_id

(Required)

$new_post_id

(Required)


Top ↑

Return Return

(false|mixed|string)


Source Source

File: src/core/admin/class-copy-recipe-parts.php

	public function modify_conditions( $content, $post_id = 0, $new_post_id = 0 ) {
		if ( empty( $content ) ) {
			return $content;
		}
		// decode into array/object
		$content = json_decode( $content );
		global $wpdb;
		foreach ( $content as $k => $condition ) {
			if ( ! isset( $condition->actions ) ) {
				continue;
			}
			foreach ( $condition->actions as $kk => $action_id ) {
				// Use `automator_duplicated_from` meta to figure out the new action ID
				// Since the action conditions are stored at the Recipe level by the Action ID
				// We have to do a lookup based on the old Action ID and the new recipe post parent ID
				$qry = $wpdb->prepare(
					"SELECT pm.post_id
FROM $wpdb->postmeta pm
JOIN $wpdb->posts p
ON p.ID = pm.post_id AND p.post_parent = %d
WHERE pm.meta_value = %d
AND pm.meta_key = %s;",
					$new_post_id,
					$action_id,
					'automator_duplicated_from'
				);
				$new_action_id = $wpdb->get_var( $qry ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
				if ( is_numeric( $new_action_id ) ) {
					unset( $condition->actions[ $kk ] ); // Remove old action ID
					$condition->actions[ $kk ] = $new_action_id; // Add new action ID
				}
			}
		}
		// return encoded string
		return wp_json_encode( $content );
	}