Automattic\WooCommerce\Database\Migrations
MetaToCustomTableMigrator::fill_source_metadata()
Fill source metadata for given IDs for verification. This will return filled data in following format: [
{ $source_table_$source_column: $value, ..., $destination_table_$destination_column: $value, ... meta_source_{$destination_column_name1}: $meta_value, ... },
... ]
Метод класса: MetaToCustomTableMigrator{}
Хуков нет.
Возвращает
Массив
. Filled $results param with source metadata.
Использование
// private - только в коде основоного (родительского) класса $result = $this->fill_source_metadata( $results, $source_ids );
- $results(массив) (обязательный)
- Entity data from both source and destination table.
- $source_ids(массив) (обязательный)
- List of source IDs.
Код MetaToCustomTableMigrator::fill_source_metadata() MetaToCustomTableMigrator::fill source metadata WC 9.2.3
private function fill_source_metadata( $results, $source_ids ) { global $wpdb; $meta_table = $this->schema_config['source']['meta']['table_name']; $meta_entity_id_column = $this->schema_config['source']['meta']['entity_id_column']; $meta_key_column = $this->schema_config['source']['meta']['meta_key_column']; $meta_value_column = $this->schema_config['source']['meta']['meta_value_column']; $meta_id_column = $this->schema_config['source']['meta']['meta_id_column']; $meta_columns = array_keys( $this->meta_column_mapping ); $meta_columns_placeholder = implode( ', ', array_fill( 0, count( $meta_columns ), '%s' ) ); $source_ids_placeholder = implode( ', ', array_fill( 0, count( $source_ids ), '%d' ) ); $query = $wpdb->prepare( // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare "SELECT $meta_entity_id_column as entity_id, $meta_key_column as meta_key, $meta_value_column as meta_value FROM $meta_table WHERE $meta_entity_id_column IN ($source_ids_placeholder) AND $meta_key_column IN ($meta_columns_placeholder) ORDER BY $meta_id_column ASC", array_merge( $source_ids, $meta_columns ) ); //phpcs:enable // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared $meta_data = $wpdb->get_results( $query, ARRAY_A ); $source_metadata_rows = array(); foreach ( $meta_data as $meta_datum ) { if ( ! isset( $source_metadata_rows[ $meta_datum['entity_id'] ] ) ) { $source_metadata_rows[ $meta_datum['entity_id'] ] = array(); } $destination_column = $this->meta_column_mapping[ $meta_datum['meta_key'] ]['destination']; $alias = "meta_source_{$destination_column}"; if ( isset( $source_metadata_rows[ $meta_datum['entity_id'] ][ $alias ] ) ) { // Only process first value, duplicate values mapping to flat columns are ignored to be consistent with WP core. continue; } $source_metadata_rows[ $meta_datum['entity_id'] ][ $alias ] = $meta_datum['meta_value']; } foreach ( $results as $index => $result_row ) { $source_id = $result_row[ $this->schema_config['source']['entity']['table_name'] . '_' . $this->schema_config['source']['entity']['primary_key'] ]; $results[ $index ] = array_merge( $result_row, ( $source_metadata_rows[ $source_id ] ?? array() ) ); } return $results; }