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