Issues (7)

includes/updates/update-2.0.0.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * Update 2.0.0
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2021 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay
9
 */
10
11
/**
12
 * Execute changes made in Pronamic Pay 2.0.0
13
 *
14
 * @link https://github.com/WordPress/WordPress/blob/3.5.1/wp-admin/includes/upgrade.php#L413
15
 * @since 2.0.0
16
 */
17
18
// Check if there is not already an upgrade running
19
if ( get_transient( 'pronamic_pay_upgrade_200' ) ) {
20
	return;
21
}
22
23
set_transient( 'pronamic_pay_upgrade_200', true, 3600 ); // 60 minutes
24
25
// Upgrade
26
global $wpdb;
27
28
require_once ABSPATH . '/wp-admin/includes/upgrade.php';
29
30
$charset_collate = '';
31
if ( ! empty( $wpdb->charset ) ) {
32
	$charset_collate = 'DEFAULT CHARACTER SET ' . $wpdb->charset;
33
}
34
if ( ! empty( $wpdb->collate ) ) {
35
	$charset_collate .= ' COLLATE ' . $wpdb->collate;
36
}
37
38
/*
39
40
-- You can undo the database upgrade by executing the following queries
41
42
UPDATE wp_pronamic_ideal_configurations SET post_id = null;
43
DELETE FROM wp_posts WHERE post_type = 'pronamic_gateway';
44
45
UPDATE wp_pronamic_ideal_payments SET post_id = null;
46
DELETE FROM wp_posts WHERE post_type = 'pronamic_payment';
47
48
UPDATE wp_rg_ideal_feeds SET post_id = null;
49
DELETE FROM wp_posts WHERE post_type = 'pronamic_pay_gf';
50
51
UPDATE wp_options SET option_value = 0 WHERE option_name = 'pronamic_pay_db_version';
52
53
DELETE FROM wp_postmeta WHERE post_id NOT IN ( SELECT ID FROM wp_posts );
54
55
*/
56
57
/**
58
 * Configs
59
 */
60
61
global $pronamic_ideal;
62
63
$config_table = $wpdb->prefix . 'pronamic_ideal_configurations';
64
65
$sql = "CREATE TABLE $config_table (
66
	id MEDIUMINT(8) UNSIGNED NOT NULL AUTO_INCREMENT,
67
	post_id BIGINT(20) UNSIGNED NULL,
68
	variant_id VARCHAR(64) NULL,
69
	merchant_id VARCHAR(64) NULL,
70
	sub_id VARCHAR(64) NULL,
71
	mode VARCHAR(64) NULL,
72
	hash_key VARCHAR(64) NULL,
73
	private_key TEXT NULL,
74
	private_key_password VARCHAR(64) NULL,
75
	private_certificate TEXT NULL,
76
	meta LONGTEXT,
77
	PRIMARY KEY  (id)
78
) $charset_collate;";
79
80
dbDelta( $sql );
81
82
// Query
83
$query = "
84
	SELECT
85
		*
86
	FROM
87
		$config_table
88
	WHERE
89
		post_id IS NULL
90
	LIMIT
91
		1
92
	;
93
";
94
95
$have_configs = true;
96
97
while ( $have_configs ) {
98
	$configs = $wpdb->get_results( $query ); // WPCS: unprepared SQL ok.
99
100
	$have_configs = ! empty( $configs );
101
102
	foreach ( $configs as $config ) {
103
		/* translators: %d: configuration ID */
104
		$title = sprintf( __( 'Configuration %d', 'pronamic_ideal' ), $config->id );
105
106
		// Post
107
		$post = array(
108
			'post_title'  => $title,
109
			'post_type'   => 'pronamic_gateway',
110
			'post_status' => 'publish',
111
		);
112
113
		$post_id = wp_insert_post( $post );
114
115
		if ( $post_id ) {
116
			$wpdb->update(
117
				$config_table,
118
				array(
119
					'post_id' => $post_id,
120
				),
121
				array(
122
					'id' => $config->id,
123
				),
124
				'%d',
125
				'%d'
126
			);
127
128
			// Meta
129
			// We ignore (@) all notice of not existing properties
130
			$config_meta = json_decode( $config->meta );
131
132
			$meta = array();
133
134
			$meta['legacy_id'] = $config->id;
135
			$meta['id']        = $config->variant_id;
136
			$meta['mode']      = $config->mode;
137
138
			// iDEAL
139
			$meta['ideal_merchant_id'] = $config->merchant_id;
140
			$meta['ideal_sub_id']      = $config->sub_id;
141
142
			// iDEAL Basic
143
			$meta['ideal_hash_key'] = $config->hash_key;
144
145
			// iDEAL Advanced
146
			$meta['ideal_private_key']          = $config->private_key;
147
			$meta['ideal_private_key_password'] = $config->private_key_password;
148
			$meta['ideal_private_certificate']  = $config->private_certificate;
149
150
			// OmniKassa
151
			if ( 'rabobank-omnikassa' === $config->variant_id ) {
152
				$meta['omnikassa_merchant_id'] = $config->merchant_id;
153
				$meta['omnikassa_secret_key']  = $config->hash_key;
154
155
				$key_version = @$config_meta->keyVersion;
156
				// In Pronamic iDEAL v1.0 we stored the key version in the iDEAL sub ID
157
				$key_version = empty( $key_version ) ? $config->sub_id : $key_version;
158
159
				$meta['omnikassa_key_version'] = $key_version;
160
161
				unset( $meta['ideal_merchant_id'] );
162
				unset( $meta['ideal_hash_key'] );
163
			}
164
165
			// Buckaroo
166
			$meta['buckaroo_website_key'] = @$config_meta->buckarooWebsiteKey;
167
			$meta['buckaroo_secret_key']  = @$config_meta->buckarooSecretKey;
168
169
			// Icepay
170
			$meta['icepay_merchant_id'] = @$config_meta->icepayMerchantId;
171
			$meta['icepay_secret_code'] = @$config_meta->icepaySecretCode;
172
173
			// Mollie
174
			$meta['mollie_partner_id']  = @$config_meta->molliePartnerId;
175
			$meta['mollie_profile_key'] = @$config_meta->mollieProfileKey;
176
177
			// Sisow
178
			$meta['sisow_merchant_id']  = @$config_meta->sisowMerchantId;
179
			$meta['sisow_merchant_key'] = @$config_meta->sisowMerchantKey;
180
181
			// TargetPay
182
			$meta['targetpay_layout_code'] = @$config_meta->targetPayLayoutCode;
183
184
			// Qantani
185
			$meta['qantani_merchant_id']     = @$config_meta->qantani_merchant_id;
186
			$meta['qantani_merchant_key']    = @$config_meta->qantani_merchant_key;
187
			$meta['qantani_merchant_secret'] = @$config_meta->qantani_merchant_secret;
188
189
			// Ogone
190
			$meta['ogone_psp_id']              = @$config_meta->pspId;
191
			$meta['ogone_sha_in_pass_phrase']  = @$config_meta->shaInPassPhrase;
192
			$meta['ogone_sha_out_pass_phrase'] = @$config_meta->shaOutPassPhrase;
193
			$meta['ogone_user_id']             = @$config_meta->ogone_user_id;
194
			$meta['ogone_password']            = @$config_meta->ogone_password;
195
196
			// Other
197
			$meta['country']           = @$config_meta->country;
198
			$meta['state_or_province'] = @$config_meta->stateOrProvince;
199
			$meta['locality']          = @$config_meta->locality;
200
			$meta['organization']      = @$config_meta->organization;
201
			$meta['organization_unit'] = @$config_meta->organizationUnit;
202
			$meta['common_name']       = @$config_meta->commonName;
203
			$meta['email']             = @$config_meta->eMailAddress;
204
205
			foreach ( $meta as $key => $value ) {
206
				if ( ! empty( $value ) ) {
207
					$meta_key = '_pronamic_gateway_' . $key;
208
209
					update_post_meta( $post_id, $meta_key, $value );
0 ignored issues
show
It seems like $post_id can also be of type WP_Error; however, parameter $post_id of update_post_meta() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

209
					update_post_meta( /** @scrutinizer ignore-type */ $post_id, $meta_key, $value );
Loading history...
210
				}
211
			}
212
		}
213
	}
214
}
215
216
/**
217
 * Config IDs map
218
 */
219
$query = "
220
	SELECT
221
		id,
222
		post_id
223
	FROM
224
		$config_table
225
	;
226
";
227
228
$config_ids_map = array();
229
230
$config_ids = $wpdb->get_results( $query ); // WPCS: unprepared SQL ok.
231
232
foreach ( $config_ids as $config_id ) {
233
	$config_ids_map[ $config_id->id ] = $config_id->post_id;
234
}
235
236
/**
237
 * Gravity Forms payment feeds
238
 */
239
$feeds_table = $wpdb->prefix . 'rg_ideal_feeds';
240
241
$sql = "CREATE TABLE $feeds_table (
242
	id MEDIUMINT(8) UNSIGNED NOT NULL AUTO_INCREMENT,
243
	post_id BIGINT(20) UNSIGNED NULL,
244
	form_id MEDIUMINT(8) UNSIGNED NOT NULL,
245
	configuration_id MEDIUMINT(8) UNSIGNED NOT NULL,
246
	is_active TINYINT(1) NOT NULL DEFAULT 1,
247
	meta LONGTEXT,
248
	PRIMARY KEY  (id),
249
	KEY form_id (form_id),
250
	KEY configuration_id (configuration_id)
251
) $charset_collate;";
252
253
dbDelta( $sql );
254
255
// Query
256
$query = "
257
	SELECT
258
		*
259
	FROM
260
		$feeds_table
261
	WHERE
262
		post_id IS NULL
263
	LIMIT
264
		1
265
	;
266
";
267
268
$have_feeds = true;
269
270
while ( $have_feeds ) {
271
	$feeds = $wpdb->get_results( $query ); // WPCS: unprepared SQL ok.
272
273
	$have_feeds = ! empty( $feeds );
274
275
	foreach ( $feeds as $feed ) {
276
		// Post
277
		$post = array(
278
			'post_title'  => sprintf(
279
				/* translators: %d: payment feed ID */
280
				__( 'Payment Form %d', 'pronamic_ideal' ),
281
				$feed->id
282
			),
283
			'post_type'   => 'pronamic_pay_gf',
284
			'post_status' => 'publish',
285
		);
286
287
		$post_id = wp_insert_post( $post );
288
289
		if ( $post_id ) {
290
			$wpdb->update(
291
				$feeds_table,
292
				array(
293
					'post_id' => $post_id,
294
				),
295
				array(
296
					'id' => $feed->id,
297
				),
298
				'%d',
299
				'%d'
300
			);
301
302
			// Meta
303
			// We ignore (@) all notice of not existing properties
304
			$meta = array();
305
306
			$feed_meta = json_decode( $feed->meta, true );
307
308
			$meta['form_id']                  = $feed->form_id;
309
			$meta['config_id']                = @$config_ids_map[ $feed->configuration_id ];
310
			$meta['is_active']                = $feed->is_active;
311
			$meta['transaction_description']  = @$feed_meta['transactionDescription'];
312
			$meta['delay_notification_ids']   = @$feed_meta['delayNotificationIds'];
313
			$meta['delay_admin_notification'] = @$feed_meta['delayAdminNotification'];
314
			$meta['delay_user_notification']  = @$feed_meta['delayUserNotification'];
315
			$meta['delay_post_creation']      = @$feed_meta['delayPostCreation'];
316
			$meta['condition_enabled']        = @$feed_meta['conditionEnabled'];
317
			$meta['condition_field_id']       = @$feed_meta['conditionFieldId'];
318
			$meta['condition_operator']       = @$feed_meta['conditionOperator'];
319
			$meta['condition_value']          = @$feed_meta['conditionValue'];
320
			$meta['user_role_field_id']       = @$feed_meta['userRoleFieldId'];
321
			$meta['fields']                   = @$feed_meta['fields'];
322
			$meta['links']                    = @$feed_meta['links'];
323
324
			if ( is_array( $meta['links'] ) ) {
325
				foreach ( $meta['links'] as &$link ) {
326
					if ( isset( $link['pageId'] ) ) {
327
						$link['page_id'] = $link['pageId'];
328
					}
329
				}
330
			}
331
332
			foreach ( $meta as $key => $value ) {
333
				if ( ! empty( $value ) ) {
334
					$meta_key = '_pronamic_pay_gf_' . $key;
335
336
					update_post_meta( $post_id, $meta_key, $value );
337
				}
338
			}
339
		}
340
	}
341
}
342
343
/**
344
 * Payments.
345
 */
346
$payments_table = $wpdb->prefix . 'pronamic_ideal_payments';
347
348
$sql = "CREATE TABLE $payments_table (
349
	id MEDIUMINT(8) UNSIGNED NOT NULL AUTO_INCREMENT,
350
	post_id BIGINT(20) UNSIGNED NULL,
351
	configuration_id MEDIUMINT(8) UNSIGNED NOT NULL,
352
	purchase_id VARCHAR(16) NULL,
353
	transaction_id VARCHAR(32) NULL,
354
	date_gmt DATETIME NOT NULL,
355
	amount DECIMAL(10, 2) NOT NULL,
356
	currency VARCHAR(8) NOT NULL,
357
	expiration_period VARCHAR(8) NOT NULL,
358
	language VARCHAR(8) NOT NULL,
359
	entrance_code VARCHAR(40) NULL,
360
	description TEXT NOT NULL,
361
	consumer_name VARCHAR(35) NULL,
362
	consumer_account_number VARCHAR(10) NULL,
363
	consumer_iban VARCHAR(34) NULL,
364
	consumer_bic VARCHAR(11) NULL,
365
	consumer_city VARCHAR(24) NULL,
366
	status VARCHAR(32) NULL DEFAULT NULL,
367
	status_requests MEDIUMINT(8) DEFAULT 0,
368
	source VARCHAR(32) NULL DEFAULT NULL,
369
	source_id VARCHAR(32) NULL DEFAULT NULL,
370
	email VARCHAR(128) NULL DEFAULT NULL,
371
	PRIMARY KEY  (id)
372
) $charset_collate;";
373
374
dbDelta( $sql );
375
376
// We convert the payments in groups of 100 so not everything will load in memory at once.
377
$query = "
378
	SELECT
379
		*
380
	FROM
381
		$payments_table
382
	WHERE
383
		post_id IS NULL
384
	LIMIT
385
		1
386
	;
387
";
388
389
$have_payments = true;
390
391
while ( $have_payments ) {
392
	$payments = $wpdb->get_results( $query ); // WPCS: unprepared SQL ok.
393
394
	$have_payments = ! empty( $payments );
395
396
	foreach ( $payments as $payment ) {
397
		// Post
398
		$post = array(
399
			'post_title'    => sprintf(
400
				/* translators: %d: payment ID */
401
				__( 'Payment %d', 'pronamic_ideal' ),
402
				$payment->id
403
			),
404
			'post_date'     => get_date_from_gmt( $payment->date_gmt ),
405
			'post_date_gmt' => $payment->date_gmt,
406
			'post_type'     => 'pronamic_payment',
407
			'post_status'   => 'publish',
408
		);
409
410
		$post_id = wp_insert_post( $post );
411
412
		if ( $post_id ) {
413
			$wpdb->update(
414
				$payments_table,
415
				array(
416
					'post_id' => $post_id,
417
				),
418
				array(
419
					'id' => $payment->id,
420
				),
421
				'%d',
422
				'%d'
423
			);
424
425
			// Meta
426
			$meta = array(
427
				'config_id'               => @$config_ids_map[ $payment->configuration_id ],
428
				'purchase_id'             => $payment->purchase_id,
429
				'transaction_id'          => $payment->transaction_id,
430
				'currency'                => $payment->currency,
431
				'amount'                  => $payment->amount,
432
				'expiration_period'       => $payment->expiration_period,
433
				'language'                => $payment->language,
434
				'entrance_code'           => $payment->entrance_code,
435
				'description'             => $payment->description,
436
				'consumer_name'           => $payment->consumer_name,
437
				'consumer_account_number' => $payment->consumer_account_number,
438
				'consumer_iban'           => $payment->consumer_iban,
439
				'consumer_bic'            => $payment->consumer_bic,
440
				'consumer_city'           => $payment->consumer_city,
441
				'status'                  => $payment->status,
442
				'source'                  => $payment->source,
443
				'source_id'               => $payment->source_id,
444
				'email'                   => $payment->email,
445
			);
446
447
			foreach ( $meta as $key => $value ) {
448
				if ( ! empty( $value ) ) {
449
					$meta_key = '_pronamic_payment_' . $key;
450
451
					update_post_meta( $post_id, $meta_key, $value );
452
				}
453
			}
454
		}
455
	}
456
}
457
458
//////////////////////////////////////////////////
459
// Options config IDs
460
//////////////////////////////////////////////////
461
462
$options = array(
463
	// EventEspresso
464
	// @link https://github.com/pronamic/wp-pronamic-ideal/blob/1.3.4/classes/Pronamic/EventEspresso/IDeal/AddOn.php#L72
465
	'pronamic_ideal_event_espresso_configuration_id' => 'pronamic_pay_ideal_event_espreso_config_id',
466
	// Jigoshop
467
	// @link https://github.com/pronamic/wp-pronamic-ideal/blob/1.3.4/classes/Pronamic/Jigoshop/IDeal/IDealGateway.php#L62
468
	'jigoshop_pronamic_ideal_enabled'                => 'pronamic_pay_ideal_jigoshop_enabled',
469
	'jigoshop_pronamic_ideal_title'                  => 'pronamic_pay_ideal_jigoshop_title',
470
	'jigoshop_pronamic_ideal_description'            => 'pronamic_pay_ideal_jigoshop_description',
471
	'jigoshop_pronamic_ideal_configuration_id'       => 'pronamic_pay_ideal_jigoshop_config_id',
472
	// Membership
473
	'pronamic_ideal_membership_chosen_configuration' => 'pronamic_pay_ideal_membership_config_id',
474
	// s2Member
475
	// @link https://github.com/pronamic/wp-pronamic-ideal/blob/1.3.4/classes/Pronamic/S2Member/Bridge/Settings.php#L52
476
	'pronamic_ideal_s2member_chosen_configuration'   => 'pronamic_pay_ideal_s2member_config_id',
477
	// WP e-Commerce
478
	// @link https://github.com/pronamic/wp-pronamic-ideal/blob/1.3.4/classes/Pronamic/WPeCommerce/IDeal/IDealMerchant.php#L35
479
	'pronamic_ideal_wpsc_configuration_id'           => 'pronamic_pay_ideal_wpsc_config_id',
480
);
481
482
foreach ( $options as $key_old => $key_new ) {
483
	$value = get_option( $key_old );
484
485
	if ( ! empty( $value ) ) {
486
		$value_new = @$config_ids_map[ $value ];
487
488
		update_option( $key_new, $value_new );
489
	}
490
}
491
492
//////////////////////////////////////////////////
493
// Complex options config IDs
494
//////////////////////////////////////////////////
495
496
// Shopp
497
// @link https://github.com/pronamic/wp-pronamic-ideal/blob/1.3.4/classes/Pronamic/Shopp/IDeal/GatewayModule.php#L72
498
$shopp_meta_table = $wpdb->prefix . 'shopp_meta';
499
500
// @link http://cube3x.com/2013/04/how-to-check-if-table-exists-in-wordpress-database/
501
if ( $shopp_meta_table === $wpdb->get_var( "SHOW TABLES LIKE '$shopp_meta_table';" ) ) { // WPCS: unprepared SQL ok.
502
	$query = "SELECT id, value FROM $shopp_meta_table WHERE type = 'setting' AND name = 'Pronamic_Shopp_IDeal_GatewayModule';";
503
504
	$row = $wpdb->get_row( $query ); // WPCS: unprepared SQL ok.
505
506
	if ( $row ) {
507
		$settings = maybe_unserialize( $row->value );
508
509
		if ( is_array( $settings ) && isset( $settings['pronamic_shopp_ideal_configuration'] ) ) {
510
			$value = $settings['pronamic_shopp_ideal_configuration'];
511
512
			$settings['config_id'] = @$config_ids_map[ $value ];
513
514
			$wpdb->update(
515
				$shopp_meta_table,
516
				array(
517
					'value' => serialize( $settings ),
518
				),
519
				array(
520
					'id' => $row->id,
521
				)
522
			);
523
		}
524
	}
525
}
526
527
// WooCommerce
528
// @link https://github.com/pronamic/wp-pronamic-ideal/blob/1.3.4/classes/Pronamic/WooCommerce/IDeal/IDealGateway.php#L42
529
$settings = get_option( 'woocommerce_pronamic_ideal_settings' );
530
531
if ( is_array( $settings ) && isset( $settings['configuration_id'] ) ) {
532
	$value = $settings['configuration_id'];
533
534
	$settings['config_id'] = @$config_ids_map[ $value ];
535
536
	unset( $settings['configuration_id'] );
537
538
	update_option( 'woocommerce_pronamic_pay_ideal_settings', $settings );
539
}
540
541
delete_transient( 'pronamic_pay_upgrade_200' );
542