Automattic\WooCommerce\Database\Migrations\CustomOrderTable
CLIRunner::verify_meta_data
Verify meta data as part of verifying the order object.
Method of the class: CLIRunner{}
No Hooks.
Returns
Array. Failed IDs with meta details.
Usage
// private - for code of main (parent) class only $result = $this->verify_meta_data( $order_ids, $failed_ids ): array;
- $order_ids(array) (required)
- Order IDs.
- $failed_ids(array) (required)
- Array for storing failed IDs.
CLIRunner::verify_meta_data() CLIRunner::verify meta data code WC 10.5.0
private function verify_meta_data( array $order_ids, array $failed_ids ): array {
$meta_keys_to_ignore = $this->synchronizer->get_ignored_order_props();
global $wpdb;
if ( ! count( $order_ids ) ) {
return array();
}
$excluded_columns = array_merge(
$this->post_to_cot_migrator->get_migrated_meta_keys(),
$meta_keys_to_ignore
);
$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, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- 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, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- 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, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Not a meta query.
'orig_meta_values' => $values,
'new_meta_values' => $migrated_meta_values,
);
}
}
}
return $failed_ids;
}