Test Failed
Push — develop ( 5f6097...889b97 )
by Remco
04:40
created

src/Upgrade300.php (1 issue)

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