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