1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Upgrade 3.0.0 |
4
|
|
|
* |
5
|
|
|
* @author Pronamic <[email protected]> |
6
|
|
|
* @copyright 2005-2020 Pronamic |
7
|
|
|
* @license GPL-3.0-or-later |
8
|
|
|
* @package Pronamic\WordPress\Pay\Gateways\Mollie |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Pronamic\WordPress\Pay\Gateways\Mollie; |
12
|
|
|
|
13
|
|
|
use Pronamic\WordPress\Pay\Upgrades\Upgrade; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Upgrade 3.0.0 |
17
|
|
|
* |
18
|
|
|
* @author Remco Tolsma |
19
|
|
|
* @version 3.0.0 |
20
|
|
|
* @since 3.0.0 |
21
|
|
|
*/ |
22
|
|
|
class Upgrade300 extends Upgrade { |
23
|
|
|
/** |
24
|
|
|
* Construct 3.0.0 upgrade. |
25
|
|
|
*/ |
26
|
|
|
public function __construct() { |
27
|
|
|
parent::__construct( '3.0.0' ); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Execute. |
32
|
|
|
* |
33
|
|
|
* @link https://github.com/WordPress/WordPress/blob/5.3/wp-includes/wp-db.php#L992-L1072 |
34
|
|
|
* @link https://github.com/WordPress/WordPress/blob/5.3/wp-admin/includes/schema.php#L25-L344 |
35
|
|
|
* @link https://developer.wordpress.org/reference/functions/dbdelta/ |
36
|
|
|
* @link https://github.com/wp-premium/gravityforms/blob/2.4.16/includes/class-gf-upgrade.php#L518-L531 |
37
|
|
|
* @throws \Exception Throws exception when database update query fails. |
38
|
|
|
*/ |
39
|
|
|
public function execute() { |
40
|
|
|
global $wpdb; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Requirements. |
44
|
|
|
*/ |
45
|
|
|
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Other. |
49
|
|
|
*/ |
50
|
|
|
$charset_collate = $wpdb->get_charset_collate(); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Queries. |
54
|
|
|
*/ |
55
|
|
|
$queries = " |
56
|
|
|
CREATE TABLE $wpdb->pronamic_pay_mollie_organizations ( |
57
|
|
|
id BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT, |
58
|
|
|
mollie_id VARCHAR( 16 ) NOT NULL, |
59
|
|
|
name VARCHAR( 128 ) DEFAULT NULL, |
60
|
|
|
email VARCHAR( 100 ) DEFAULT NULL, |
61
|
|
|
PRIMARY KEY ( id ), |
62
|
|
|
UNIQUE KEY mollie_id ( mollie_id ) |
63
|
|
|
) $charset_collate; |
64
|
|
|
CREATE TABLE $wpdb->pronamic_pay_mollie_customers ( |
65
|
|
|
id BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT, |
66
|
|
|
mollie_id VARCHAR( 16 ) NOT NULL, |
67
|
|
|
organization_id BIGINT( 20 ) UNSIGNED DEFAULT NULL, |
68
|
|
|
test_mode BOOL NOT NULL, |
69
|
|
|
email VARCHAR( 100 ) DEFAULT NULL, |
70
|
|
|
PRIMARY KEY ( id ), |
71
|
|
|
UNIQUE KEY mollie_id ( mollie_id ), |
72
|
|
|
KEY organization_id ( organization_id ), |
73
|
|
|
KEY test_mode ( test_mode ), |
74
|
|
|
KEY email ( email ) |
75
|
|
|
) $charset_collate; |
76
|
|
|
CREATE TABLE $wpdb->pronamic_pay_mollie_customer_users ( |
77
|
|
|
id BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT, |
78
|
|
|
customer_id BIGINT( 20 ) UNSIGNED NOT NULL, |
79
|
|
|
user_id BIGINT( 20 ) UNSIGNED NOT NULL, |
80
|
|
|
PRIMARY KEY ( id ), |
81
|
|
|
UNIQUE KEY customer_user ( customer_id, user_id ) |
82
|
|
|
) $charset_collate; |
83
|
|
|
"; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Execute. |
87
|
|
|
* |
88
|
|
|
* @link https://developer.wordpress.org/reference/functions/dbdelta/ |
89
|
|
|
* @link https://github.com/WordPress/WordPress/blob/5.3/wp-admin/includes/upgrade.php#L2538-L2915 |
90
|
|
|
*/ |
91
|
|
|
\dbDelta( $queries ); |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Foreign keys. |
95
|
|
|
* |
96
|
|
|
* @link https://core.trac.wordpress.org/ticket/19207 |
97
|
|
|
* @link https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html |
98
|
|
|
*/ |
99
|
|
|
$data = array( |
100
|
|
|
(object) array( |
101
|
|
|
'table' => $wpdb->pronamic_pay_mollie_customers, |
102
|
|
|
'name' => 'fk_customer_organization_id', |
103
|
|
|
'query' => " |
104
|
|
|
ALTER TABLE $wpdb->pronamic_pay_mollie_customers |
105
|
|
|
ADD CONSTRAINT fk_customer_organization_id |
106
|
|
|
FOREIGN KEY ( organization_id ) |
107
|
|
|
REFERENCES $wpdb->pronamic_pay_mollie_organizations ( id ) |
108
|
|
|
ON DELETE RESTRICT |
109
|
|
|
ON UPDATE RESTRICT |
110
|
|
|
; |
111
|
|
|
", |
112
|
|
|
), |
113
|
|
|
(object) array( |
114
|
|
|
'table' => $wpdb->pronamic_pay_mollie_customer_users, |
115
|
|
|
'name' => 'fk_customer_id', |
116
|
|
|
'query' => " |
117
|
|
|
ALTER TABLE $wpdb->pronamic_pay_mollie_customer_users |
118
|
|
|
ADD CONSTRAINT fk_customer_id |
119
|
|
|
FOREIGN KEY customer_id ( customer_id ) |
120
|
|
|
REFERENCES $wpdb->pronamic_pay_mollie_customers ( id ) |
121
|
|
|
ON DELETE RESTRICT |
122
|
|
|
ON UPDATE RESTRICT |
123
|
|
|
; |
124
|
|
|
", |
125
|
|
|
), |
126
|
|
|
(object) array( |
127
|
|
|
'table' => $wpdb->pronamic_pay_mollie_customer_users, |
128
|
|
|
'name' => 'fk_customer_user_id', |
129
|
|
|
'query' => " |
130
|
|
|
ALTER TABLE $wpdb->pronamic_pay_mollie_customer_users |
131
|
|
|
ADD CONSTRAINT fk_customer_user_id |
132
|
|
|
FOREIGN KEY user_id ( user_id ) |
133
|
|
|
REFERENCES $wpdb->users ( id ) |
134
|
|
|
ON DELETE CASCADE |
135
|
|
|
ON UPDATE CASCADE |
136
|
|
|
; |
137
|
|
|
", |
138
|
|
|
), |
139
|
|
|
); |
140
|
|
|
|
141
|
|
|
foreach ( $data as $item ) { |
142
|
|
|
/** |
143
|
|
|
* Check if foreign key exists |
144
|
|
|
* |
145
|
|
|
* @link https://github.com/woocommerce/woocommerce/blob/3.9.0/includes/class-wc-install.php#L663-L681 |
146
|
|
|
*/ |
147
|
|
|
$result = $wpdb->get_var( |
148
|
|
|
$wpdb->prepare( |
149
|
|
|
" |
150
|
|
|
SELECT COUNT(*) |
151
|
|
|
FROM information_schema.TABLE_CONSTRAINTS |
152
|
|
|
WHERE CONSTRAINT_SCHEMA = %s |
153
|
|
|
AND CONSTRAINT_NAME = %s |
154
|
|
|
AND CONSTRAINT_TYPE = 'FOREIGN KEY' |
155
|
|
|
AND TABLE_NAME = %s |
156
|
|
|
", |
157
|
|
|
$wpdb->dbname, |
158
|
|
|
$item->name, |
159
|
|
|
$item->table |
160
|
|
|
) |
161
|
|
|
); |
162
|
|
|
|
163
|
|
|
if ( null === $result ) { |
164
|
|
|
throw new \Exception( |
165
|
|
|
\sprintf( |
166
|
|
|
'Could not count foreign keys: %s, database error: %s.', |
167
|
|
|
$$item->name, |
168
|
|
|
$wpdb->last_error |
169
|
|
|
) |
170
|
|
|
); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$number_constraints = \intval( $result ); |
174
|
|
|
|
175
|
|
|
if ( 0 === $number_constraints ) { |
176
|
|
|
$result = $wpdb->query( $item->query ); |
177
|
|
|
|
178
|
|
|
if ( false === $result ) { |
179
|
|
|
throw new \Exception( |
180
|
|
|
\sprintf( |
181
|
|
|
'Could not add foreign key: %s, database error: %s.', |
182
|
|
|
$item->name, |
183
|
|
|
$wpdb->last_error |
184
|
|
|
) |
185
|
|
|
); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Convert user meta. |
192
|
|
|
*/ |
193
|
|
|
$this->convert_user_meta(); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Convert user meta. |
198
|
|
|
* |
199
|
|
|
* @throws \Exception Throws exception when database update query fails. |
200
|
|
|
*/ |
201
|
|
|
private function convert_user_meta() { |
202
|
|
|
global $wpdb; |
203
|
|
|
|
204
|
|
|
$query = " |
205
|
|
|
INSERT IGNORE INTO $wpdb->pronamic_pay_mollie_customers ( |
206
|
|
|
mollie_id, |
207
|
|
|
test_mode |
208
|
|
|
) |
209
|
|
|
SELECT |
210
|
|
|
meta_value AS mollie_id, |
211
|
|
|
'_pronamic_pay_mollie_customer_id_test' = meta_key AS test_mode |
212
|
|
|
FROM |
213
|
|
|
$wpdb->usermeta |
214
|
|
|
WHERE |
215
|
|
|
meta_key IN ( |
216
|
|
|
'_pronamic_pay_mollie_customer_id', |
217
|
|
|
'_pronamic_pay_mollie_customer_id_test' |
218
|
|
|
) |
219
|
|
|
AND |
220
|
|
|
meta_value != '' |
221
|
|
|
; |
222
|
|
|
"; |
223
|
|
|
|
224
|
|
|
$result = $wpdb->query( $query ); |
225
|
|
|
|
226
|
|
|
if ( false === $result ) { |
227
|
|
|
throw new Exception( |
|
|
|
|
228
|
|
|
sprintf( |
229
|
|
|
'Could not convert user meta, database error: %s.', |
230
|
|
|
$wpdb->last_error |
231
|
|
|
) |
232
|
|
|
); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
$query = " |
236
|
|
|
INSERT IGNORE INTO $wpdb->pronamic_pay_mollie_customer_users ( |
237
|
|
|
customer_id, |
238
|
|
|
user_id |
239
|
|
|
) |
240
|
|
|
SELECT |
241
|
|
|
mollie_customer.id AS mollie_customer_id, |
242
|
|
|
wp_user.ID AS wp_user_id |
243
|
|
|
FROM |
244
|
|
|
$wpdb->pronamic_pay_mollie_customers AS mollie_customer |
245
|
|
|
INNER JOIN |
246
|
|
|
$wpdb->usermeta AS wp_user_meta |
247
|
|
|
ON wp_user_meta.meta_value = mollie_customer.mollie_id |
248
|
|
|
INNER JOIN |
249
|
|
|
$wpdb->users AS wp_user |
250
|
|
|
ON wp_user_meta.user_id = wp_user.ID |
251
|
|
|
WHERE |
252
|
|
|
wp_user_meta.meta_key IN ( |
253
|
|
|
'_pronamic_pay_mollie_customer_id', |
254
|
|
|
'_pronamic_pay_mollie_customer_id_test' |
255
|
|
|
) |
256
|
|
|
AND |
257
|
|
|
wp_user_meta.meta_value != '' |
258
|
|
|
; |
259
|
|
|
"; |
260
|
|
|
|
261
|
|
|
$result = $wpdb->query( $query ); |
262
|
|
|
|
263
|
|
|
if ( false === $result ) { |
264
|
|
|
throw new Exception( |
265
|
|
|
sprintf( |
266
|
|
|
'Could not convert user meta, database error: %s.', |
267
|
|
|
$wpdb->last_error |
268
|
|
|
) |
269
|
|
|
); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|