Automattic\WooCommerce\DataBase\Migrations\CustomOrderTable

CLIRunner::verify_meta_data()privateWC 1.0

Verify meta data as part of verifying the order object.

{} Это метод класса: CLIRunner{}

Хуков нет.

Возвращает

Массив. Failed IDs with meta details.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->verify_meta_data( $order_ids, $failed_ids ) : array;
$order_ids(массив) (обязательный)
Order IDs.
$failed_ids(массив) (обязательный)
Array for storing failed IDs.

Код CLIRunner::verify_meta_data() WC 6.7.0

private function verify_meta_data( array $order_ids, array $failed_ids ) : array {
	global $wpdb;
	if ( ! count( $order_ids ) ) {
		return array();
	}
	$excluded_columns             = $this->post_to_cot_migrator->get_migrated_meta_keys();
	$excluded_columns_placeholder = implode( ', ', array_fill( 0, count( $excluded_columns ), '%s' ) );
	$order_ids_placeholder        = implode( ', ', array_fill( 0, count( $order_ids ), '%d' ) );
	$meta_table                   = OrdersTableDataStore::get_meta_table_name();

	// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared -- table names are hardcoded, orders_ids and excluded_columns are prepared.
	$query           = $wpdb->prepare(
		"
SELECT {$wpdb->postmeta}.post_id as entity_id, {$wpdb->postmeta}.meta_key, {$wpdb->postmeta}.meta_value
FROM $wpdb->postmeta
WHERE
      {$wpdb->postmeta}.post_id in ( $order_ids_placeholder ) AND
      {$wpdb->postmeta}.meta_key not in ( $excluded_columns_placeholder )
ORDER BY {$wpdb->postmeta}.post_id ASC, {$wpdb->postmeta}.meta_key ASC;
",
		array_merge(
			$order_ids,
			$excluded_columns
		)
	);
	$source_data = $wpdb->get_results( $query, ARRAY_A );
	// phpcs:enable

	$normalized_source_data = $this->normalize_raw_meta_data( $source_data );

	// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared -- table names are hardcoded, orders_ids and excluded_columns are prepared.
	$migrated_query = $wpdb->prepare(
		"
SELECT $meta_table.order_id as entity_id, $meta_table.meta_key, $meta_table.meta_value
FROM $meta_table
WHERE
$meta_table.order_id in ( $order_ids_placeholder )
ORDER BY $meta_table.order_id ASC, $meta_table.meta_key ASC;
",
		$order_ids
	);
	$migrated_data  = $wpdb->get_results( $migrated_query, ARRAY_A );
	// phpcs:enable

	$normalized_migrated_meta_data = $this->normalize_raw_meta_data( $migrated_data );

	foreach ( $normalized_source_data as $order_id => $meta ) {
		foreach ( $meta as $meta_key => $values ) {
			$migrated_meta_values = isset( $normalized_migrated_meta_data[ $order_id ][ $meta_key ] ) ? $normalized_migrated_meta_data[ $order_id ][ $meta_key ] : array();
			$diff = array_diff( $values, $migrated_meta_values );

			if ( count( $diff ) ) {
				if ( ! isset( $failed_ids[ $order_id ] ) ) {
					$failed_ids[ $order_id ] = array();
				}
				$failed_ids[ $order_id ][] = array(
					'order_id'   => $order_id,
					'meta_key'   => $meta_key,
					'orig_meta_values' => $values,
					'new_meta_values' => $migrated_meta_values,
				);
			}
		}
	}

	return $failed_ids;
}