Test Failed
Push — develop ( abf4e1...29070a )
by Reüel
03:42
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.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( $wpdb->prepare( "
148
				SELECT COUNT(*)
149
				FROM information_schema.TABLE_CONSTRAINTS
150
				WHERE CONSTRAINT_SCHEMA = %s
151
				AND CONSTRAINT_NAME = %s
152
				AND CONSTRAINT_TYPE = 'FOREIGN KEY'
153
				AND TABLE_NAME = %s
154
				",
155
				$wpdb->dbname,
156
				$item->name,
157
				$item->table
158
			) );
159
160
			if ( null === $result ) {
161
				throw new \Exception(
162
					\sprintf(
163
						'Could not count foreign keys: %s, database error: %s.',
164
						$$item->name,
165
						$wpdb->last_error
166
					)
167
				);
168
			}
169
170
			$number_constraints = \intval( $result );
171
172
			if ( 0 === $number_constraints ) {
173
				$result = $wpdb->query( $item->query );
174
175
				if ( false === $result ) {
176
					throw new \Exception(
177
						\sprintf(
178
							'Could not add foreign key: %s, database error: %s.',
179
							$item->name,
180
							$wpdb->last_error
181
						)
182
					);
183
				}
184
			}
185
		}
186
187
		/**
188
		 * Convert user meta.
189
		 */
190
		$this->convert_user_meta();
191
	}
192
193
	/**
194
	 * Convert user meta.
195
	 *
196
	 * @throws \Exception Throws exception when database update query fails.
197
	 */
198
	private function convert_user_meta() {
199
		global $wpdb;
200
201
		$query = "
202
			INSERT IGNORE INTO $wpdb->pronamic_pay_mollie_customers (
203
				mollie_id,
204
				test_mode
205
			)
206
			SELECT
207
				meta_value AS mollie_id,
208
				'_pronamic_pay_mollie_customer_id_test' = meta_key AS test_mode
209
			FROM
210
				$wpdb->usermeta
211
			WHERE
212
				meta_key IN (
213
					'_pronamic_pay_mollie_customer_id',
214
					'_pronamic_pay_mollie_customer_id_test'
215
				)
216
					AND
217
				meta_value != ''
218
			;
219
		";
220
221
		$result = $wpdb->query( $query );
222
223
		if ( false === $result ) {
224
			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...
225
				sprintf(
226
					'Could not convert user meta, database error: %s.',
227
					$wpdb->last_error
228
				)
229
			);
230
		}
231
232
		$query = "
233
			INSERT IGNORE INTO $wpdb->pronamic_pay_mollie_customer_users (
234
				customer_id,
235
				user_id
236
			)
237
			SELECT
238
				mollie_customer.id AS mollie_customer_id,
239
				wp_user.ID AS wp_user_id
240
			FROM
241
				$wpdb->pronamic_pay_mollie_customers AS mollie_customer
242
					INNER JOIN
243
				$wpdb->usermeta AS wp_user_meta
244
						ON wp_user_meta.meta_value = mollie_customer.mollie_id
245
					INNER JOIN
246
				$wpdb->users AS wp_user
247
						ON wp_user_meta.user_id = wp_user.ID
248
			WHERE
249
				wp_user_meta.meta_key IN (
250
					'_pronamic_pay_mollie_customer_id',
251
					'_pronamic_pay_mollie_customer_id_test'
252
				)
253
					AND
254
				wp_user_meta.meta_value != ''
255
			;
256
		";
257
258
		$result = $wpdb->query( $query );
259
260
		if ( false === $result ) {
261
			throw new Exception(
262
				sprintf(
263
					'Could not convert user meta, database error: %s.',
264
					$wpdb->last_error
265
				)
266
			);
267
		}
268
	}
269
}
270