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_profiles ( |
||
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 | name VARCHAR( 128 ) DEFAULT 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 | ) $charset_collate; |
||
74 | CREATE TABLE $wpdb->pronamic_pay_mollie_customers ( |
||
75 | id BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT, |
||
76 | mollie_id VARCHAR( 16 ) NOT NULL, |
||
77 | organization_id BIGINT( 20 ) UNSIGNED DEFAULT NULL, |
||
78 | profile_id BIGINT( 20 ) UNSIGNED DEFAULT NULL, |
||
79 | test_mode BOOL NOT NULL, |
||
80 | email VARCHAR( 100 ) DEFAULT NULL, |
||
81 | PRIMARY KEY ( id ), |
||
82 | UNIQUE KEY mollie_id ( mollie_id ), |
||
83 | KEY organization_id ( organization_id ), |
||
84 | KEY profile_id ( profile_id ), |
||
85 | KEY test_mode ( test_mode ), |
||
86 | KEY email ( email ) |
||
87 | ) $charset_collate; |
||
88 | CREATE TABLE $wpdb->pronamic_pay_mollie_customer_users ( |
||
89 | id BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT, |
||
90 | customer_id BIGINT( 20 ) UNSIGNED NOT NULL, |
||
91 | user_id BIGINT( 20 ) UNSIGNED NOT NULL, |
||
92 | PRIMARY KEY ( id ), |
||
93 | UNIQUE KEY customer_user ( customer_id, user_id ) |
||
94 | ) $charset_collate; |
||
95 | "; |
||
96 | |||
97 | /** |
||
98 | * Execute. |
||
99 | * |
||
100 | * @link https://developer.wordpress.org/reference/functions/dbdelta/ |
||
101 | * @link https://github.com/WordPress/WordPress/blob/5.3/wp-admin/includes/upgrade.php#L2538-L2915 |
||
102 | */ |
||
103 | \dbDelta( $queries ); |
||
104 | |||
105 | /** |
||
106 | * Foreign keys. |
||
107 | * |
||
108 | * @link https://core.trac.wordpress.org/ticket/19207 |
||
109 | * @link https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html |
||
110 | */ |
||
111 | $data = array( |
||
112 | (object) array( |
||
113 | 'table' => $wpdb->pronamic_pay_mollie_profiles, |
||
114 | 'name' => 'fk_profile_organization_id', |
||
115 | 'query' => " |
||
116 | ALTER TABLE $wpdb->pronamic_pay_mollie_profiles |
||
117 | ADD CONSTRAINT fk_profile_organization_id |
||
118 | FOREIGN KEY ( organization_id ) |
||
119 | REFERENCES $wpdb->pronamic_pay_mollie_organizations ( id ) |
||
120 | ON DELETE RESTRICT |
||
121 | ON UPDATE RESTRICT |
||
122 | ; |
||
123 | ", |
||
124 | ), |
||
125 | (object) array( |
||
126 | 'table' => $wpdb->pronamic_pay_mollie_customers, |
||
127 | 'name' => 'fk_customer_organization_id', |
||
128 | 'query' => " |
||
129 | ALTER TABLE $wpdb->pronamic_pay_mollie_customers |
||
130 | ADD CONSTRAINT fk_customer_organization_id |
||
131 | FOREIGN KEY ( organization_id ) |
||
132 | REFERENCES $wpdb->pronamic_pay_mollie_organizations ( id ) |
||
133 | ON DELETE RESTRICT |
||
134 | ON UPDATE RESTRICT |
||
135 | ; |
||
136 | ", |
||
137 | ), |
||
138 | (object) array( |
||
139 | 'table' => $wpdb->pronamic_pay_mollie_customers, |
||
140 | 'name' => 'fk_customer_profile_id', |
||
141 | 'query' => " |
||
142 | ALTER TABLE $wpdb->pronamic_pay_mollie_customers |
||
143 | ADD CONSTRAINT fk_customer_profile_id |
||
144 | FOREIGN KEY ( profile_id ) |
||
145 | REFERENCES $wpdb->pronamic_pay_mollie_profiles ( id ) |
||
146 | ON DELETE RESTRICT |
||
147 | ON UPDATE RESTRICT |
||
148 | ; |
||
149 | ", |
||
150 | ), |
||
151 | (object) array( |
||
152 | 'table' => $wpdb->pronamic_pay_mollie_customer_users, |
||
153 | 'name' => 'fk_customer_id', |
||
154 | 'query' => " |
||
155 | ALTER TABLE $wpdb->pronamic_pay_mollie_customer_users |
||
156 | ADD CONSTRAINT fk_customer_id |
||
157 | FOREIGN KEY customer_id ( customer_id ) |
||
158 | REFERENCES $wpdb->pronamic_pay_mollie_customers ( id ) |
||
159 | ON DELETE RESTRICT |
||
160 | ON UPDATE RESTRICT |
||
161 | ; |
||
162 | ", |
||
163 | ), |
||
164 | (object) array( |
||
165 | 'table' => $wpdb->pronamic_pay_mollie_customer_users, |
||
166 | 'name' => 'fk_customer_user_id', |
||
167 | 'query' => " |
||
168 | ALTER TABLE $wpdb->pronamic_pay_mollie_customer_users |
||
169 | ADD CONSTRAINT fk_customer_user_id |
||
170 | FOREIGN KEY user_id ( user_id ) |
||
171 | REFERENCES $wpdb->users ( id ) |
||
172 | ON DELETE CASCADE |
||
173 | ON UPDATE CASCADE |
||
174 | ; |
||
175 | ", |
||
176 | ), |
||
177 | ); |
||
178 | |||
179 | foreach ( $data as $item ) { |
||
180 | /** |
||
181 | * Check if foreign key exists |
||
182 | * |
||
183 | * @link https://github.com/woocommerce/woocommerce/blob/3.9.0/includes/class-wc-install.php#L663-L681 |
||
184 | */ |
||
185 | $result = $wpdb->get_var( $wpdb->prepare( " |
||
186 | SELECT COUNT(*) |
||
187 | FROM information_schema.TABLE_CONSTRAINTS |
||
188 | WHERE CONSTRAINT_SCHEMA = %s |
||
189 | AND CONSTRAINT_NAME = %s |
||
190 | AND CONSTRAINT_TYPE = 'FOREIGN KEY' |
||
191 | AND TABLE_NAME = %s |
||
192 | ", |
||
193 | $wpdb->dbname, |
||
194 | $item->name, |
||
195 | $item->table |
||
196 | ) ); |
||
197 | |||
198 | if ( null === $result ) { |
||
199 | throw new \Exception( |
||
200 | \sprintf( |
||
201 | 'Could not count foreign keys: %s, database error: %s.', |
||
202 | $$item->name, |
||
203 | $wpdb->last_error |
||
204 | ) |
||
205 | ); |
||
206 | } |
||
207 | |||
208 | $number_constraints = \intval( $result ); |
||
209 | |||
210 | if ( 0 === $number_constraints ) { |
||
211 | $result = $wpdb->query( $item->query ); |
||
212 | |||
213 | if ( false === $result ) { |
||
214 | throw new \Exception( |
||
215 | \sprintf( |
||
216 | 'Could not add foreign key: %s, database error: %s.', |
||
217 | $item->name, |
||
218 | $wpdb->last_error |
||
219 | ) |
||
220 | ); |
||
221 | } |
||
222 | } |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Convert user meta. |
||
227 | */ |
||
228 | $this->convert_user_meta(); |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * Convert user meta. |
||
233 | * |
||
234 | * @throws \Exception Throws exception when database update query fails. |
||
235 | */ |
||
236 | private function convert_user_meta() { |
||
237 | global $wpdb; |
||
238 | |||
239 | $query = " |
||
240 | INSERT IGNORE INTO $wpdb->pronamic_pay_mollie_customers ( |
||
241 | mollie_id, |
||
242 | test_mode |
||
243 | ) |
||
244 | SELECT |
||
245 | meta_value AS mollie_id, |
||
246 | '_pronamic_pay_mollie_customer_id_test' = meta_key AS test_mode |
||
247 | FROM |
||
248 | $wpdb->usermeta |
||
249 | WHERE |
||
250 | meta_key IN ( |
||
251 | '_pronamic_pay_mollie_customer_id', |
||
252 | '_pronamic_pay_mollie_customer_id_test' |
||
253 | ) |
||
254 | AND |
||
255 | meta_value != '' |
||
256 | ; |
||
257 | "; |
||
258 | |||
259 | $result = $wpdb->query( $query ); |
||
260 | |||
261 | if ( false === $result ) { |
||
262 | throw new Exception( |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
263 | sprintf( |
||
264 | 'Could not convert user meta, database error: %s.', |
||
265 | $wpdb->last_error |
||
266 | ) |
||
267 | ); |
||
268 | } |
||
269 | |||
270 | $query = " |
||
271 | INSERT IGNORE INTO $wpdb->pronamic_pay_mollie_customer_users ( |
||
272 | customer_id, |
||
273 | user_id |
||
274 | ) |
||
275 | SELECT |
||
276 | mollie_customer.id AS mollie_customer_id, |
||
277 | wp_user.ID AS wp_user_id |
||
278 | FROM |
||
279 | $wpdb->pronamic_pay_mollie_customers AS mollie_customer |
||
280 | INNER JOIN |
||
281 | $wpdb->usermeta AS wp_user_meta |
||
282 | ON wp_user_meta.meta_value = mollie_customer.mollie_id |
||
283 | INNER JOIN |
||
284 | $wpdb->users AS wp_user |
||
285 | ON wp_user_meta.user_id = wp_user.ID |
||
286 | WHERE |
||
287 | wp_user_meta.meta_key IN ( |
||
288 | '_pronamic_pay_mollie_customer_id', |
||
289 | '_pronamic_pay_mollie_customer_id_test' |
||
290 | ) |
||
291 | AND |
||
292 | wp_user_meta.meta_value != '' |
||
293 | ; |
||
294 | "; |
||
295 | |||
296 | $result = $wpdb->query( $query ); |
||
297 | |||
298 | if ( false === $result ) { |
||
299 | throw new Exception( |
||
300 | sprintf( |
||
301 | 'Could not convert user meta, database error: %s.', |
||
302 | $wpdb->last_error |
||
303 | ) |
||
304 | ); |
||
305 | } |
||
306 | } |
||
307 | } |
||
308 |