-
Notifications
You must be signed in to change notification settings - Fork 1
/
Install.php
383 lines (353 loc) · 10.2 KB
/
Install.php
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?php
/**
* Mollie install.
*
* @author Pronamic <[email protected]>
* @copyright 2005-2023 Pronamic
* @license GPL-3.0-or-later
* @package Pronamic\WordPress\Pay
*/
namespace Pronamic\WordPress\Pay\Gateways\Mollie;
use Pronamic\WordPress\Pay\Upgrades\Upgrade;
/**
* Install class
*/
class Install extends Upgrade {
/**
* Execute.
*
* @link https://github.com/woocommerce/woocommerce/blob/4.0.0/includes/class-wc-install.php#L272-L306
* @return void
*/
public function execute() {
$this->create_tables();
$this->add_foreign_keys();
$this->convert_user_meta();
}
/**
* Create tables.
*
* @link https://github.com/woocommerce/woocommerce/blob/4.0.0/includes/class-wc-install.php#L630-L720
* @return void
*/
private function create_tables() {
global $wpdb;
/**
* Requirements.
*/
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
/**
* Table options.
*
* In MySQL 5.6, InnoDB is the default MySQL storage engine. Unless you
* have configured a different default storage engine, issuing a
* CREATE TABLE statement without an ENGINE= clause creates an InnoDB
* table.
*
* @link https://dev.mysql.com/doc/refman/5.6/en/innodb-introduction.html
*
* If a storage engine is specified that is not available, MySQL uses
* the default engine instead. Normally, this is MyISAM. For example,
* if a table definition includes the ENGINE=INNODB option but the MySQL
* server does not support INNODB tables, the table is created as a
* MyISAM table.
*
* @link https://dev.mysql.com/doc/refman/5.6/en/create-table.html
*/
$table_options = 'ENGINE=InnoDB ' . $wpdb->get_charset_collate();
/**
* Queries.
*
* @link https://github.com/WordPress/WordPress/blob/5.3/wp-admin/includes/schema.php
*/
$queries = "
CREATE TABLE $wpdb->pronamic_pay_mollie_organizations (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
mollie_id varchar(16) NOT NULL,
name varchar(128) DEFAULT NULL,
email varchar(100) DEFAULT NULL,
PRIMARY KEY ( id ),
UNIQUE KEY mollie_id ( mollie_id )
) $table_options;
CREATE TABLE $wpdb->pronamic_pay_mollie_profiles (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
mollie_id varchar(16) NOT NULL,
organization_id bigint(20) unsigned DEFAULT NULL,
name varchar(128) DEFAULT NULL,
email varchar(100) DEFAULT NULL,
api_key_test varchar(35) DEFAULT NULL,
api_key_live varchar(35) DEFAULT NULL,
PRIMARY KEY ( id ),
UNIQUE KEY mollie_id ( mollie_id ),
KEY organization_id ( organization_id )
) $table_options;
CREATE TABLE $wpdb->pronamic_pay_mollie_customers (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
mollie_id varchar(16) NOT NULL,
organization_id bigint(20) unsigned DEFAULT NULL,
profile_id bigint(20) unsigned DEFAULT NULL,
test_mode tinyint(1) NOT NULL,
email varchar(100) DEFAULT NULL,
name varchar(255) DEFAULT NULL,
PRIMARY KEY ( id ),
UNIQUE KEY mollie_id ( mollie_id ),
KEY organization_id ( organization_id ),
KEY profile_id ( profile_id ),
KEY test_mode ( test_mode ),
KEY email ( email )
) $table_options;
CREATE TABLE $wpdb->pronamic_pay_mollie_customer_users (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
customer_id bigint(20) unsigned NOT NULL,
user_id bigint(20) unsigned NOT NULL,
PRIMARY KEY ( id ),
UNIQUE KEY customer_user ( customer_id, user_id )
) $table_options;
";
/**
* Execute.
*
* @link https://developer.wordpress.org/reference/functions/dbdelta/
* @link https://github.com/WordPress/WordPress/blob/5.3/wp-admin/includes/upgrade.php#L2538-L2915
*/
\dbDelta( $queries );
}
/**
* Add foreign keys.
*
* @return void
*/
private function add_foreign_keys() {
global $wpdb;
/**
* Foreign keys.
*
* @link https://core.trac.wordpress.org/ticket/19207
* @link https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html
*/
$data = [
(object) [
'table' => $wpdb->pronamic_pay_mollie_profiles,
'name' => 'fk_profile_organization_id',
'query' => "
ALTER TABLE $wpdb->pronamic_pay_mollie_profiles
ADD CONSTRAINT fk_profile_organization_id
FOREIGN KEY ( organization_id )
REFERENCES $wpdb->pronamic_pay_mollie_organizations ( id )
ON DELETE RESTRICT
ON UPDATE RESTRICT
;
",
],
(object) [
'table' => $wpdb->pronamic_pay_mollie_customers,
'name' => 'fk_customer_organization_id',
'query' => "
ALTER TABLE $wpdb->pronamic_pay_mollie_customers
ADD CONSTRAINT fk_customer_organization_id
FOREIGN KEY ( organization_id )
REFERENCES $wpdb->pronamic_pay_mollie_organizations ( id )
ON DELETE RESTRICT
ON UPDATE RESTRICT
;
",
],
(object) [
'table' => $wpdb->pronamic_pay_mollie_customers,
'name' => 'fk_customer_profile_id',
'query' => "
ALTER TABLE $wpdb->pronamic_pay_mollie_customers
ADD CONSTRAINT fk_customer_profile_id
FOREIGN KEY ( profile_id )
REFERENCES $wpdb->pronamic_pay_mollie_profiles ( id )
ON DELETE RESTRICT
ON UPDATE RESTRICT
;
",
],
(object) [
'table' => $wpdb->pronamic_pay_mollie_customer_users,
'name' => 'fk_customer_id',
'query' => "
ALTER TABLE $wpdb->pronamic_pay_mollie_customer_users
ADD CONSTRAINT fk_customer_id
FOREIGN KEY customer_id ( customer_id )
REFERENCES $wpdb->pronamic_pay_mollie_customers ( id )
ON DELETE RESTRICT
ON UPDATE RESTRICT
;
",
],
(object) [
'table' => $wpdb->pronamic_pay_mollie_customer_users,
'name' => 'fk_customer_user_id',
'query' => "
ALTER TABLE $wpdb->pronamic_pay_mollie_customer_users
ADD CONSTRAINT fk_customer_user_id
FOREIGN KEY user_id ( user_id )
REFERENCES $wpdb->users ( id )
ON DELETE CASCADE
ON UPDATE CASCADE
;
",
],
];
foreach ( $data as $item ) {
try {
$this->add_foreign_key( $item );
} catch ( \Exception $e ) {
// Foreign keys are not strictly required.
continue;
}
}
}
/**
* Add specified foreign key.
*
* @param object $item Foreign key data.
* @return void
* @throws \Exception Throws exception when adding foreign key fails.
* @throws \InvalidArgumentException Throws invalid argument exception if item misses required `table`, `name` or `query` property.
*/
private function add_foreign_key( $item ) {
global $wpdb;
if ( ! \property_exists( $item, 'table' ) ) {
throw new \InvalidArgumentException( 'Foreign key item must contain `table` property.' );
}
if ( ! \property_exists( $item, 'name' ) ) {
throw new \InvalidArgumentException( 'Foreign key item must contain `name` property.' );
}
if ( ! \property_exists( $item, 'query' ) ) {
throw new \InvalidArgumentException( 'Foreign key item must contain `query` property.' );
}
/**
* Suppress errors.
*
* We suppress errors because adding foreign keys to for example
* a `$wpdb->users` MyISAM table will trigger the following error:
*
* "Error in query (1005): Can't create table '●●●●●●●●. # Sql-●●●●●●●●●●' (errno: 150)"
*
* @link https://github.com/WordPress/WordPress/blob/5.3/wp-includes/wp-db.php#L1544-L1559
*/
$suppress_errors = $wpdb->suppress_errors( true );
/**
* Check if foreign key exists
*
* @link https://github.com/woocommerce/woocommerce/blob/3.9.0/includes/class-wc-install.php#L663-L681
*/
$result = $wpdb->get_var(
$wpdb->prepare(
"
SELECT COUNT(*)
FROM information_schema.TABLE_CONSTRAINTS
WHERE CONSTRAINT_SCHEMA = %s
AND CONSTRAINT_NAME = %s
AND CONSTRAINT_TYPE = 'FOREIGN KEY'
AND TABLE_NAME = %s
",
$wpdb->dbname,
$item->name,
$item->table
)
);
if ( null === $result ) {
throw new \Exception(
\sprintf(
'Could not count foreign keys: %s, database error: %s.',
\esc_html( $item->name ),
\esc_html( $wpdb->last_error )
)
);
}
$number_constraints = \intval( $result );
if ( 0 === $number_constraints ) {
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Query is prepared.
$result = $wpdb->query( $item->query );
$wpdb->suppress_errors( $suppress_errors );
if ( false === $result ) {
throw new \Exception(
\sprintf(
'Could not add foreign key: %s, database error: %s.',
\esc_html( $item->name ),
\esc_html( $wpdb->last_error )
)
);
}
}
$wpdb->suppress_errors( $suppress_errors );
}
/**
* Convert user meta.
*
* @return void
* @throws \Exception Throws exception when database update query fails.
*/
private function convert_user_meta() {
global $wpdb;
$query = "
INSERT IGNORE INTO $wpdb->pronamic_pay_mollie_customers (
mollie_id,
test_mode
)
SELECT
meta_value AS mollie_id,
'_pronamic_pay_mollie_customer_id_test' = meta_key AS test_mode
FROM
$wpdb->usermeta
WHERE
meta_key IN (
'_pronamic_pay_mollie_customer_id',
'_pronamic_pay_mollie_customer_id_test'
)
AND
meta_value != ''
;
";
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Query is prepared.
$result = $wpdb->query( $query );
if ( false === $result ) {
throw new \Exception(
\sprintf(
'Could not convert user meta, database error: %s.',
\esc_html( $wpdb->last_error )
)
);
}
$query = "
INSERT IGNORE INTO $wpdb->pronamic_pay_mollie_customer_users (
customer_id,
user_id
)
SELECT
mollie_customer.id AS mollie_customer_id,
wp_user.ID AS wp_user_id
FROM
$wpdb->pronamic_pay_mollie_customers AS mollie_customer
INNER JOIN
$wpdb->usermeta AS wp_user_meta
ON BINARY wp_user_meta.meta_value = mollie_customer.mollie_id
INNER JOIN
$wpdb->users AS wp_user
ON wp_user_meta.user_id = wp_user.ID
WHERE
wp_user_meta.meta_key IN (
'_pronamic_pay_mollie_customer_id',
'_pronamic_pay_mollie_customer_id_test'
)
AND
wp_user_meta.meta_value != ''
;
";
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Query is prepared.
$result = $wpdb->query( $query );
if ( false === $result ) {
throw new \Exception(
\sprintf(
'Could not convert user meta, database error: %s.',
\esc_html( $wpdb->last_error )
)
);
}
}
}