ntity_id ] = array(); } $to_insert[ $entity_id ][ $meta_key ] = $meta_values; } else { if ( 1 === count( $meta_values ) && 1 === count( $already_migrated[ $entity_id ][ $meta_key ] ) ) { if ( $meta_values[0] === $already_migrated[ $entity_id ][ $meta_key ][0]['meta_value'] ) { continue; } if ( ! isset( $to_update[ $entity_id ] ) ) { $to_update[ $entity_id ] = array(); } $to_update[ $entity_id ][ $meta_key ] = array( 'id' => $already_migrated[ $entity_id ][ $meta_key ][0]['id'], // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value 'meta_value' => $meta_values[0], ); continue; } // There are multiple meta entries, let's find the unique entries and insert. $unique_meta_values = array_diff( $meta_values, array_column( $already_migrated[ $entity_id ][ $meta_key ], 'meta_value' ) ); if ( 0 === count( $unique_meta_values ) ) { continue; } if ( ! isset( $to_insert[ $entity_id ] ) ) { $to_insert[ $entity_id ] = array(); } $to_insert[ $entity_id ][ $meta_key ] = $unique_meta_values; } } } return array( $to_insert, $to_update ); } /** * Helper method to build query used to fetch data from source meta table. * * @param array $entity_ids List of entity IDs to build meta query for. * * @return string Query that can be used to fetch data. */ private function build_meta_table_query( array $entity_ids ): string { global $wpdb; $source_meta_table = $this->schema_config['source']['meta']['table_name']; $source_meta_key_column = $this->schema_config['source']['meta']['meta_key_column']; $source_meta_value_column = $this->schema_config['source']['meta']['meta_value_column']; $source_entity_id_column = $this->schema_config['source']['meta']['entity_id_column']; $order_by = "source.$source_entity_id_column ASC"; $where_clause = "source.`$source_entity_id_column` IN (" . implode( ', ', array_fill( 0, count( $entity_ids ), '%d' ) ) . ')'; $entity_table = $this->schema_config['source']['entity']['table_name']; $entity_id_column = $this->schema_config['source']['entity']['id_column']; $entity_meta_id_mapping_column = $this->schema_config['source']['entity']['source_id_column']; if ( isset( $this->schema_config['source']['excluded_keys'] ) && is_array( $this->schema_config['source']['excluded_keys'] ) ) { $key_placeholder = implode( ',', array_fill( 0, count( $this->schema_config['source']['excluded_keys'] ), '%s' ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- $source_meta_key_column is escaped for backticks, $key_placeholder is hardcoded. $exclude_clause = $wpdb->prepare( "source.$source_meta_key_column NOT IN ( $key_placeholder )", $this->schema_config['source']['excluded_keys'] ); $where_clause = "$where_clause AND $exclude_clause"; } // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare return $wpdb->prepare( " SELECT source.`$source_entity_id_column` as source_entity_id, entity.`$entity_id_column` as entity_id, source.`$source_meta_key_column` as meta_key, source.`$source_meta_value_column` as meta_value FROM `$source_meta_table` source JOIN `$entity_table` entity ON entity.`$entity_meta_id_mapping_column` = source.`$source_entity_id_column` WHERE $where_clause ORDER BY $order_by ", $entity_ids ); // phpcs:enable } }