CLIRunner.php
4 months ago
PostMetaToOrderMetaMigrator.php
2 years ago
PostToOrderAddressTableMigrator.php
2 years ago
PostToOrderOpTableMigrator.php
2 years ago
PostToOrderTableMigrator.php
3 years ago
PostsToOrdersMigrationController.php
1 year ago
PostToOrderAddressTableMigrator.php
169 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class for WPPost to wc_order_address table migrator. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Database\Migrations\CustomOrderTable; |
| 7 | |
| 8 | use Automattic\WooCommerce\Database\Migrations\MetaToCustomTableMigrator; |
| 9 | use Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore; |
| 10 | |
| 11 | /** |
| 12 | * Helper class to migrate records from the WordPress post table |
| 13 | * to the custom order addresses table. |
| 14 | * |
| 15 | * @package Automattic\WooCommerce\Database\Migrations\CustomOrderTable |
| 16 | */ |
| 17 | class PostToOrderAddressTableMigrator extends MetaToCustomTableMigrator { |
| 18 | /** |
| 19 | * Type of addresses being migrated; 'billing' or 'shipping'. |
| 20 | * |
| 21 | * @var $type |
| 22 | */ |
| 23 | protected $type; |
| 24 | |
| 25 | /** |
| 26 | * PostToOrderAddressTableMigrator constructor. |
| 27 | * |
| 28 | * @param string $type Type of address being migrated; 'billing' or 'shipping'. |
| 29 | */ |
| 30 | public function __construct( $type ) { |
| 31 | $this->type = $type; |
| 32 | parent::__construct(); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Get schema config for wp_posts and wc_order_address table. |
| 37 | * |
| 38 | * @return array Config. |
| 39 | */ |
| 40 | protected function get_schema_config(): array { |
| 41 | global $wpdb; |
| 42 | |
| 43 | return array( |
| 44 | 'source' => array( |
| 45 | 'entity' => array( |
| 46 | 'table_name' => $wpdb->posts, |
| 47 | 'meta_rel_column' => 'ID', |
| 48 | 'destination_rel_column' => 'ID', |
| 49 | 'primary_key' => 'ID', |
| 50 | ), |
| 51 | 'meta' => array( |
| 52 | 'table_name' => $wpdb->postmeta, |
| 53 | 'meta_id_column' => 'meta_id', |
| 54 | 'meta_key_column' => 'meta_key', |
| 55 | 'meta_value_column' => 'meta_value', |
| 56 | 'entity_id_column' => 'post_id', |
| 57 | ), |
| 58 | ), |
| 59 | 'destination' => array( |
| 60 | 'table_name' => OrdersTableDataStore::get_addresses_table_name(), |
| 61 | 'source_rel_column' => 'order_id', |
| 62 | 'primary_key' => 'id', |
| 63 | 'primary_key_type' => 'int', |
| 64 | ), |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Get columns config. |
| 70 | * |
| 71 | * @return \string[][] Config. |
| 72 | */ |
| 73 | protected function get_core_column_mapping(): array { |
| 74 | $type = $this->type; |
| 75 | |
| 76 | return array( |
| 77 | 'ID' => array( |
| 78 | 'type' => 'int', |
| 79 | 'destination' => 'order_id', |
| 80 | ), |
| 81 | 'type' => array( |
| 82 | 'type' => 'string', |
| 83 | 'destination' => 'address_type', |
| 84 | 'select_clause' => "'$type'", |
| 85 | ), |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Get meta data config. |
| 91 | * |
| 92 | * @return \string[][] Config. |
| 93 | */ |
| 94 | public function get_meta_column_config(): array { |
| 95 | $type = $this->type; |
| 96 | |
| 97 | return array( |
| 98 | "_{$type}_first_name" => array( |
| 99 | 'type' => 'string', |
| 100 | 'destination' => 'first_name', |
| 101 | ), |
| 102 | "_{$type}_last_name" => array( |
| 103 | 'type' => 'string', |
| 104 | 'destination' => 'last_name', |
| 105 | ), |
| 106 | "_{$type}_company" => array( |
| 107 | 'type' => 'string', |
| 108 | 'destination' => 'company', |
| 109 | ), |
| 110 | "_{$type}_address_1" => array( |
| 111 | 'type' => 'string', |
| 112 | 'destination' => 'address_1', |
| 113 | ), |
| 114 | "_{$type}_address_2" => array( |
| 115 | 'type' => 'string', |
| 116 | 'destination' => 'address_2', |
| 117 | ), |
| 118 | "_{$type}_city" => array( |
| 119 | 'type' => 'string', |
| 120 | 'destination' => 'city', |
| 121 | ), |
| 122 | "_{$type}_state" => array( |
| 123 | 'type' => 'string', |
| 124 | 'destination' => 'state', |
| 125 | ), |
| 126 | "_{$type}_postcode" => array( |
| 127 | 'type' => 'string', |
| 128 | 'destination' => 'postcode', |
| 129 | ), |
| 130 | "_{$type}_country" => array( |
| 131 | 'type' => 'string', |
| 132 | 'destination' => 'country', |
| 133 | ), |
| 134 | "_{$type}_email" => array( |
| 135 | 'type' => 'string', |
| 136 | 'destination' => 'email', |
| 137 | ), |
| 138 | "_{$type}_phone" => array( |
| 139 | 'type' => 'string', |
| 140 | 'destination' => 'phone', |
| 141 | ), |
| 142 | ); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Additional WHERE clause to only fetch the addresses of the current type. |
| 147 | * |
| 148 | * @param array $entity_ids The ids of the entities being inserted or updated. |
| 149 | * @return string The additional string for the WHERE clause. |
| 150 | */ |
| 151 | protected function get_additional_where_clause_for_get_data_to_insert_or_update( array $entity_ids ): string { |
| 152 | return "AND destination.`address_type` = '{$this->type}'"; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Helper function to generate where clause for fetching data for verification. |
| 157 | * |
| 158 | * @param array $source_ids Array of IDs from source table. |
| 159 | * |
| 160 | * @return string WHERE clause. |
| 161 | */ |
| 162 | protected function get_where_clause_for_verification( $source_ids ) { |
| 163 | global $wpdb; |
| 164 | $query = parent::get_where_clause_for_verification( $source_ids ); |
| 165 | // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $query should already be prepared, $schema_config is hardcoded. |
| 166 | return $wpdb->prepare( "$query AND {$this->schema_config['destination']['table_name']}.address_type = %s", $this->type ); |
| 167 | } |
| 168 | } |
| 169 |