Test Failed
Pull Request — master (#1942)
by Devin
05:03
created

upgrade-functions.php ➔ give_show_upgrade_notices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 56
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1.4705

Importance

Changes 0
Metric Value
cc 1
eloc 32
nc 1
nop 1
dl 0
loc 56
ccs 4
cts 18
cp 0.2222
crap 1.4705
rs 9.7251
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Upgrade Functions
4
 *
5
 * @package     Give
6
 * @subpackage  Admin/Upgrades
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.0
10
 *
11
 * NOTICE: When adding new upgrade notices, please be sure to put the action into the upgrades array during install: /includes/install.php @ Appox Line 156
12
 */
13
14
// Exit if accessed directly.
15
if ( ! defined( 'ABSPATH' ) ) {
16
	exit;
17
}
18
19
20
/**
21
 * Perform automatic database upgrades when necessary.
22
 *
23
 * @since 1.6
24
 * @return void
25
 */
26
function give_do_automatic_upgrades() {
27
	$did_upgrade  = false;
28
	$give_version = preg_replace( '/[^0-9.].*/', '', get_option( 'give_version' ) );
29
30
	if ( ! $give_version ) {
31
		// 1.0 is the first version to use this option so we must add it.
32
		$give_version = '1.0';
33
	}
34
35
	switch ( true ) {
36
37
		case version_compare( $give_version, '1.6', '<' ) :
38
			give_v16_upgrades();
39
			$did_upgrade = true;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
40
41
		case version_compare( $give_version, '1.7', '<' ) :
42
			give_v17_upgrades();
43
			$did_upgrade = true;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
44
45
		case version_compare( $give_version, '1.8', '<' ) :
46
			give_v18_upgrades();
47
			$did_upgrade = true;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
48
49
		case version_compare( $give_version, '1.8.7', '<' ) :
50
			give_v187_upgrades();
51
			$did_upgrade = true;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
52
53
		case version_compare( $give_version, '1.8.8', '<' ) :
54
			give_v188_upgrades();
55
			$did_upgrade = true;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
56
57
		case version_compare( $give_version, '1.8.9', '<' ) :
58
			give_v189_upgrades();
59
			$did_upgrade = true;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
60
61
		case version_compare( $give_version, '1.8.12', '<' ) :
62
			give_v1812_upgrades();
63
			$did_upgrade = true;
64
65
	}
66
67
	if ( $did_upgrade ) {
68
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
69
	}
70
}
71
72
add_action( 'admin_init', 'give_do_automatic_upgrades' );
73
add_action( 'give_upgrades', 'give_do_automatic_upgrades' );
74
75
/**
76
 * Display Upgrade Notices
77
 *
78
 * @since 1.0
79
 * @since 1.8.12 Update new update process code.
80
 *
81
 * @param Give_Updates $give_updates
82
 *
83
 * @return void
84
 */
85
function give_show_upgrade_notices( $give_updates ) {
86
	// v1.3.2 Upgrades
87
	$give_updates->register(
88
		array(
89
			'id'       => 'upgrade_give_payment_customer_id',
90
			'version'  => '1.3.2',
91
			'callback' => 'give_v132_upgrade_give_payment_customer_id',
92
		)
93
	);
94
95
	// v1.3.4 Upgrades ensure the user has gone through 1.3.4.
96
	$give_updates->register(
97
		array(
98
			'id'       => 'upgrade_give_offline_status',
99
			'depend'   => 'upgrade_give_payment_customer_id',
100
			'version'  => '1.3.4',
101
			'callback' => 'give_v134_upgrade_give_offline_status',
102
		)
103
	);
104
105
	// v1.8 form metadata upgrades.
106
	$give_updates->register(
107
		array(
108
			'id'       => 'v18_upgrades_form_metadata',
109
			'version'  => '1.8',
110
			'callback' => 'give_v18_upgrades_form_metadata',
111
		)
112
	);
113
114
	// v1.8.9 Upgrades
115
	$give_updates->register(
116
		array(
117 1
			'id'       => 'v189_upgrades_levels_post_meta',
118
			'version'  => '1.8.9',
119
			'callback' => 'give_v189_upgrades_levels_post_meta_callback',
120
		)
121 1
	);
122
123 1
	// v1.8.12 Upgrades
124
	$give_updates->register(
125
		array(
126
			'id'       => 'v1812_update_amount_values',
127
			'version'  => '1.8.12',
128
			'callback' => 'give_v1812_update_amount_values_callback',
129
		)
130
	);
131
132
	// v1.8.12 Upgrades
133
	$give_updates->register(
134
		array(
135
			'id'       => 'v1812_update_donor_purchase_values',
136
			'version'  => '1.8.12',
137
			'callback' => 'give_v1812_update_donor_purchase_value_callback',
138 1
		)
139
	);
140
}
141
142 1
add_action( 'give_register_updates', 'give_show_upgrade_notices' );
143 1
144
/**
145
 * Triggers all upgrade functions
146 1
 *
147
 * This function is usually triggered via AJAX
148 1
 *
149
 * @since 1.0
150
 * @return void
151
 */
152
function give_trigger_upgrades() {
153
154 View Code Duplication
	if ( ! current_user_can( 'manage_give_settings' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
		wp_die( esc_html__( 'You do not have permission to do Give upgrades.', 'give' ), esc_html__( 'Error', 'give' ), array(
156
			'response' => 403,
157
		) );
158
	}
159 1
160
	$give_version = get_option( 'give_version' );
161 1
162
	if ( ! $give_version ) {
163
		// 1.0 is the first version to use this option so we must add it.
164
		$give_version = '1.0';
165 1
		add_option( 'give_version', $give_version );
166
	}
167
168
	update_option( 'give_version', GIVE_VERSION );
169
	delete_option( 'give_doing_upgrade' );
170
171
	if ( DOING_AJAX ) {
172
		die( 'complete' );
173
	} // End if().
174
}
175
176
add_action( 'wp_ajax_give_trigger_upgrades', 'give_trigger_upgrades' );
177
178
179
/**
180
 * Upgrades the
181
 *
182
 * Standardizes the discrepancies between two metakeys `_give_payment_customer_id` and `_give_payment_donor_id`
183
 *
184
 * @since      1.3.2
185
 */
186
function give_v132_upgrade_give_payment_customer_id() {
187
	global $wpdb;
188
189
	/* @var Give_Updates $give_updates */
190
	$give_updates = Give_Updates::get_instance();
191
192 View Code Duplication
	if ( ! current_user_can( 'manage_give_settings' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
193
		wp_die( esc_html__( 'You do not have permission to do Give upgrades.', 'give' ), esc_html__( 'Error', 'give' ), array(
194
			'response' => 403,
195
		) );
196
	}
197
198
	ignore_user_abort( true );
199
200
	if ( ! give_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {
201
		@set_time_limit( 0 );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Coding Style introduced by
Silencing errors is discouraged
Loading history...
202
	}
203
204
	// UPDATE DB METAKEYS.
205
	$sql   = "UPDATE $wpdb->postmeta SET meta_key = '_give_payment_customer_id' WHERE meta_key = '_give_payment_donor_id'";
206
	$query = $wpdb->query( $sql );
0 ignored issues
show
Unused Code introduced by
$query is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
207
208
	$give_updates->percentage = 100;
0 ignored issues
show
Documentation Bug introduced by
It seems like 100 of type integer is incompatible with the declared type array of property $percentage.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
209
	give_set_upgrade_complete( 'upgrade_give_payment_customer_id' );
210
}
211
212
213
/**
214
 * Upgrades the Offline Status
215
 *
216
 * Reverses the issue where offline donations in "pending" status where inappropriately marked as abandoned
217
 *
218
 * @since      1.3.4
219
 */
220
function give_v134_upgrade_give_offline_status() {
221
	global $wpdb;
222
223
	/* @var Give_Updates $give_updates */
224
	$give_updates = Give_Updates::get_instance();
225
226
	// Get abandoned offline payments.
227
	$select = "SELECT ID FROM $wpdb->posts p ";
228
	$join   = "LEFT JOIN $wpdb->postmeta m ON p.ID = m.post_id ";
229
	$where  = "WHERE p.post_type = 'give_payment' ";
230
	$where  .= "AND ( p.post_status = 'abandoned' )";
231
	$where  .= "AND ( m.meta_key = '_give_payment_gateway' AND m.meta_value = 'offline' )";
232
233
	$sql            = $select . $join . $where;
234
	$found_payments = $wpdb->get_col( $sql );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
235
236
	foreach ( $found_payments as $payment ) {
237
238
		// Only change ones marked abandoned since our release last week because the admin may have marked some abandoned themselves.
239
		$modified_time = get_post_modified_time( 'U', false, $payment );
240
241
		// 1450124863 =  12/10/2015 20:42:25.
242
		if ( $modified_time >= 1450124863 ) {
243
244
			give_update_payment_status( $payment, 'pending' );
245
246
		}
247
	}
248
249
	$give_updates->percentage = 100;
0 ignored issues
show
Documentation Bug introduced by
It seems like 100 of type integer is incompatible with the declared type array of property $percentage.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
250
	give_set_upgrade_complete( 'upgrade_give_offline_status' );
251
}
252
253
254
/**
255
 * Cleanup User Roles
256
 *
257
 * This upgrade routine removes unused roles and roles with typos
258
 *
259
 * @since      1.5.2
260
 */
261
function give_v152_cleanup_users() {
262
263
	$give_version = get_option( 'give_version' );
264
265
	if ( ! $give_version ) {
266
		// 1.0 is the first version to use this option so we must add it.
267
		$give_version = '1.0';
268
	}
269
270
	$give_version = preg_replace( '/[^0-9.].*/', '', $give_version );
271
272
	// v1.5.2 Upgrades
273
	if ( version_compare( $give_version, '1.5.2', '<' ) || ! give_has_upgrade_completed( 'upgrade_give_user_caps_cleanup' ) ) {
274
275
		// Delete all caps with "ss".
276
		// Also delete all unused "campaign" roles.
277
		$delete_caps = array(
278
			'delete_give_formss',
279
			'delete_others_give_formss',
280
			'delete_private_give_formss',
281
			'delete_published_give_formss',
282
			'read_private_forms',
283
			'edit_give_formss',
284
			'edit_others_give_formss',
285
			'edit_private_give_formss',
286
			'edit_published_give_formss',
287
			'publish_give_formss',
288
			'read_private_give_formss',
289
			'assign_give_campaigns_terms',
290
			'delete_give_campaigns',
291
			'delete_give_campaigns_terms',
292
			'delete_give_campaignss',
293
			'delete_others_give_campaignss',
294
			'delete_private_give_campaignss',
295
			'delete_published_give_campaignss',
296
			'edit_give_campaigns',
297
			'edit_give_campaigns_terms',
298
			'edit_give_campaignss',
299
			'edit_others_give_campaignss',
300
			'edit_private_give_campaignss',
301
			'edit_published_give_campaignss',
302
			'manage_give_campaigns_terms',
303
			'publish_give_campaignss',
304
			'read_give_campaigns',
305
			'read_private_give_campaignss',
306
			'view_give_campaigns_stats',
307
			'delete_give_paymentss',
308
			'delete_others_give_paymentss',
309
			'delete_private_give_paymentss',
310
			'delete_published_give_paymentss',
311
			'edit_give_paymentss',
312
			'edit_others_give_paymentss',
313
			'edit_private_give_paymentss',
314
			'edit_published_give_paymentss',
315
			'publish_give_paymentss',
316
			'read_private_give_paymentss',
317
		);
318
319
		global $wp_roles;
320
		foreach ( $delete_caps as $cap ) {
321
			foreach ( array_keys( $wp_roles->roles ) as $role ) {
322
				$wp_roles->remove_cap( $role, $cap );
323
			}
324
		}
325
326
		// Create Give plugin roles.
327
		$roles = new Give_Roles();
328
		$roles->add_roles();
329
		$roles->add_caps();
330
331
		// The Update Ran.
332
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
333
		give_set_upgrade_complete( 'upgrade_give_user_caps_cleanup' );
334
		delete_option( 'give_doing_upgrade' );
335
336
	}// End if().
337
338
}
339
340
add_action( 'admin_init', 'give_v152_cleanup_users' );
341
342
/**
343
 * 1.6 Upgrade routine to create the customer meta table.
344
 *
345
 * @since  1.6
346
 * @return void
347
 */
348
function give_v16_upgrades() {
349
	// Create the donor databases.
350
	$donors_db = new Give_DB_Donors();
351
	$donors_db->create_table();
352
	$donor_meta = new Give_DB_Donor_Meta();
353
	$donor_meta->create_table();
354
}
355
356
/**
357
 * 1.7 Upgrades.
358
 *
359
 * a. Update license api data for plugin addons.
360
 * b. Cleanup user roles.
361
 *
362
 * @since  1.7
363
 * @return void
364
 */
365
function give_v17_upgrades() {
366
	// Upgrade license data.
367
	give_v17_upgrade_addon_license_data();
368
	give_v17_cleanup_roles();
369
}
370
371
/**
372
 * Upgrade license data
373
 *
374
 * @since 1.7
375
 */
376
function give_v17_upgrade_addon_license_data() {
377
	$give_options = give_get_settings();
378
379
	$api_url = 'https://givewp.com/give-sl-api/';
380
381
	// Get addons license key.
382
	$addons = array();
383
	foreach ( $give_options as $key => $value ) {
384
		if ( false !== strpos( $key, '_license_key' ) ) {
385
			$addons[ $key ] = $value;
386
		}
387
	}
388
389
	// Bailout: We do not have any addon license data to upgrade.
390
	if ( empty( $addons ) ) {
391
		return false;
392
	}
393
394
	foreach ( $addons as $key => $addon_license ) {
395
396
		// Get addon shortname.
397
		$shortname = str_replace( '_license_key', '', $key );
398
399
		// Addon license option name.
400
		$addon_license_option_name = $shortname . '_license_active';
401
402
		// bailout if license is empty.
403
		if ( empty( $addon_license ) ) {
404
			delete_option( $addon_license_option_name );
405
			continue;
406
		}
407
408
		// Get addon name.
409
		$addon_name       = array();
410
		$addon_name_parts = explode( '_', str_replace( 'give_', '', $shortname ) );
411
		foreach ( $addon_name_parts as $name_part ) {
412
413
			// Fix addon name
414
			switch ( $name_part ) {
415
				case 'authorizenet' :
416
					$name_part = 'authorize.net';
417
					break;
418
			}
419
420
			$addon_name[] = ucfirst( $name_part );
421
		}
422
423
		$addon_name = implode( ' ', $addon_name );
424
425
		// Data to send to the API
426
		$api_params = array(
427
			'edd_action' => 'activate_license', // never change from "edd_" to "give_"!
428
			'license'    => $addon_license,
429
			'item_name'  => urlencode( $addon_name ),
430
			'url'        => home_url(),
431
		);
432
433
		// Call the API.
434
		$response = wp_remote_post(
435
			$api_url,
436
			array(
437
				'timeout'   => 15,
438
				'sslverify' => false,
439
				'body'      => $api_params,
440
			)
441
		);
442
443
		// Make sure there are no errors.
444
		if ( is_wp_error( $response ) ) {
445
			delete_option( $addon_license_option_name );
446
			continue;
447
		}
448
449
		// Tell WordPress to look for updates.
450
		set_site_transient( 'update_plugins', null );
451
452
		// Decode license data.
453
		$license_data = json_decode( wp_remote_retrieve_body( $response ) );
454
		update_option( $addon_license_option_name, $license_data );
455
	}// End foreach().
456
}
457
458
459
/**
460
 * Cleanup User Roles.
461
 *
462
 * This upgrade routine removes unused roles and roles with typos.
463
 *
464
 * @since      1.7
465
 */
466
function give_v17_cleanup_roles() {
467
468
	// Delete all caps with "_give_forms_" and "_give_payments_"
469
	// These roles have no usage; the proper is singular.
470
	$delete_caps = array(
471
		'view_give_forms_stats',
472
		'delete_give_forms_terms',
473
		'assign_give_forms_terms',
474
		'edit_give_forms_terms',
475
		'manage_give_forms_terms',
476
		'view_give_payments_stats',
477
		'manage_give_payments_terms',
478
		'edit_give_payments_terms',
479
		'assign_give_payments_terms',
480
		'delete_give_payments_terms',
481
	);
482
483
	global $wp_roles;
484
	foreach ( $delete_caps as $cap ) {
485
		foreach ( array_keys( $wp_roles->roles ) as $role ) {
486
			$wp_roles->remove_cap( $role, $cap );
487
		}
488
	}
489
490
	// Set roles again.
491
	$roles = new Give_Roles();
492
	$roles->add_roles();
493
	$roles->add_caps();
494
495
}
496
497
/**
498
 * 1.8 Upgrades.
499
 *
500
 * a. Upgrade checkbox settings to radio button settings.
501
 * a. Update form meta for new metabox settings.
502
 *
503
 * @since  1.8
504
 * @return void
505
 */
506
function give_v18_upgrades() {
507
	// Upgrade checkbox settings to radio button settings.
508
	give_v18_upgrades_core_setting();
509
}
510
511
/**
512
 * Upgrade core settings.
513
 *
514
 * @since  1.8
515
 * @return void
516
 */
517
function give_v18_upgrades_core_setting() {
518
	// Core settings which changes from checkbox to radio.
519
	$core_setting_names = array_merge(
520
		array_keys( give_v18_renamed_core_settings() ),
521
		array(
522
			'uninstall_on_delete',
523
			'scripts_footer',
524
			'test_mode',
525
			'email_access',
526
			'terms',
527
			'give_offline_donation_enable_billing_fields',
528
		)
529
	);
530
531
	// Bailout: If not any setting define.
532
	if ( $give_settings = get_option( 'give_settings' ) ) {
533
534
		$setting_changed = false;
535
536
		// Loop: check each setting field.
537
		foreach ( $core_setting_names as $setting_name ) {
538
			// New setting name.
539
			$new_setting_name = preg_replace( '/^(enable_|disable_)/', '', $setting_name );
540
541
			// Continue: If setting already set.
542
			if (
543
				array_key_exists( $new_setting_name, $give_settings )
544
				&& in_array( $give_settings[ $new_setting_name ], array( 'enabled', 'disabled' ) )
545
			) {
546
				continue;
547
			}
548
549
			// Set checkbox value to radio value.
550
			$give_settings[ $setting_name ] = ( ! empty( $give_settings[ $setting_name ] ) && 'on' === $give_settings[ $setting_name ] ? 'enabled' : 'disabled' );
551
552
			// @see https://github.com/WordImpress/Give/issues/1063
553
			if ( false !== strpos( $setting_name, 'disable_' ) ) {
554
555
				$give_settings[ $new_setting_name ] = ( give_is_setting_enabled( $give_settings[ $setting_name ] ) ? 'disabled' : 'enabled' );
556
			} elseif ( false !== strpos( $setting_name, 'enable_' ) ) {
557
558
				$give_settings[ $new_setting_name ] = ( give_is_setting_enabled( $give_settings[ $setting_name ] ) ? 'enabled' : 'disabled' );
559
			}
560
561
			// Tell bot to update core setting to db.
562
			if ( ! $setting_changed ) {
563
				$setting_changed = true;
564
			}
565
		}
566
567
		// Update setting only if they changed.
568
		if ( $setting_changed ) {
569
			update_option( 'give_settings', $give_settings );
570
		}
571
	}// End if().
572
573
	give_set_upgrade_complete( 'v18_upgrades_core_setting' );
574
}
575
576
/**
577
 * Upgrade form metadata for new metabox settings.
578
 *
579
 * @since  1.8
580
 * @return void
581
 */
582
function give_v18_upgrades_form_metadata() {
583
	/* @var Give_Updates $give_updates */
584
	$give_updates = Give_Updates::get_instance();
585
586
	// form query
587
	$forms = new WP_Query( array(
588
			'paged'          => $give_updates->step,
589
			'status'         => 'any',
590
			'order'          => 'ASC',
591
			'post_type'      => 'give_forms',
592
			'posts_per_page' => 20,
593
		)
594
	);
595
596
	if ( $forms->have_posts() ) {
597
		$give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 20 ) );
598
599
		while ( $forms->have_posts() ) {
600
			$forms->the_post();
601
602
			// Form content.
603
			// Note in version 1.8 display content setting split into display content and content placement setting.
604
			// You can delete _give_content_option in future
605
			$show_content = give_get_meta( get_the_ID(), '_give_content_option', true );
606
			if ( $show_content && ! give_get_meta( get_the_ID(), '_give_display_content', true ) ) {
607
				$field_value = ( 'none' !== $show_content ? 'enabled' : 'disabled' );
608
				give_update_meta( get_the_ID(), '_give_display_content', $field_value );
609
610
				$field_value = ( 'none' !== $show_content ? $show_content : 'give_pre_form' );
611
				give_update_meta( get_the_ID(), '_give_content_placement', $field_value );
612
			}
613
614
			// "Disable" Guest Donation. Checkbox
615
			// See: https://github.com/WordImpress/Give/issues/1470
616
			$guest_donation        = give_get_meta( get_the_ID(), '_give_logged_in_only', true );
617
			$guest_donation_newval = ( in_array( $guest_donation, array( 'yes', 'on' ) ) ? 'disabled' : 'enabled' );
618
			give_update_meta( get_the_ID(), '_give_logged_in_only', $guest_donation_newval );
619
620
			// Offline Donations
621
			// See: https://github.com/WordImpress/Give/issues/1579
622
			$offline_donation = give_get_meta( get_the_ID(), '_give_customize_offline_donations', true );
623
			if ( 'no' === $offline_donation ) {
624
				$offline_donation_newval = 'global';
625
			} elseif ( 'yes' === $offline_donation ) {
626
				$offline_donation_newval = 'enabled';
627
			} else {
628
				$offline_donation_newval = 'disabled';
629
			}
630
			give_update_meta( get_the_ID(), '_give_customize_offline_donations', $offline_donation_newval );
631
632
			// Convert yes/no setting field to enabled/disabled.
633
			$form_radio_settings = array(
634
				// Custom Amount.
635
				'_give_custom_amount',
636
637
				// Donation Gaol.
638
				'_give_goal_option',
639
640
				// Close Form.
641
				'_give_close_form_when_goal_achieved',
642
643
				// Term & conditions.
644
				'_give_terms_option',
645
646
				// Billing fields.
647
				'_give_offline_donation_enable_billing_fields_single',
648
			);
649
650
			foreach ( $form_radio_settings as $meta_key ) {
651
				// Get value.
652
				$field_value = give_get_meta( get_the_ID(), $meta_key, true );
653
654
				// Convert meta value only if it is in yes/no/none.
655
				if ( in_array( $field_value, array( 'yes', 'on', 'no', 'none' ) ) ) {
656
657
					$field_value = ( in_array( $field_value, array( 'yes', 'on' ) ) ? 'enabled' : 'disabled' );
658
					give_update_meta( get_the_ID(), $meta_key, $field_value );
659
				}
660
			}
661
		}// End while().
662
663
		wp_reset_postdata();
664
665
	} else {
666
		// No more forms found, finish up.
667
		give_set_upgrade_complete( 'v18_upgrades_form_metadata' );
668
	}
669
}
670
671
672
/**
673
 * Get list of core setting renamed in version 1.8.
674
 *
675
 * @since  1.8
676
 * @return array
677
 */
678
function give_v18_renamed_core_settings() {
679
	return array(
680
		'disable_paypal_verification' => 'paypal_verification',
681
		'disable_css'                 => 'css',
682
		'disable_welcome'             => 'welcome',
683
		'disable_forms_singular'      => 'forms_singular',
684
		'disable_forms_archives'      => 'forms_archives',
685
		'disable_forms_excerpt'       => 'forms_excerpt',
686
		'disable_form_featured_img'   => 'form_featured_img',
687
		'disable_form_sidebar'        => 'form_sidebar',
688
		'disable_admin_notices'       => 'admin_notices',
689
		'disable_the_content_filter'  => 'the_content_filter',
690
		'enable_floatlabels'          => 'floatlabels',
691
		'enable_categories'           => 'categories',
692
		'enable_tags'                 => 'tags',
693
	);
694
}
695
696
697
/**
698
 * Upgrade core settings.
699
 *
700
 * @since  1.8.7
701
 * @return void
702
 */
703
function give_v187_upgrades() {
704
	global $wpdb;
705
706
	/**
707
	 * Upgrade 1: Remove stat and cache transients.
708
	 */
709
	$cached_options = $wpdb->get_col(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
710
		$wpdb->prepare(
711
			"SELECT * FROM {$wpdb->options} where (option_name LIKE '%%%s%%' OR option_name LIKE '%%%s%%')",
712
			array(
713
				'_transient_give_stats_',
714
				'give_cache',
715
				'_transient_give_add_ons_feed',
716
				'_transient__give_ajax_works' .
717
				'_transient_give_total_api_keys',
718
				'_transient_give_i18n_give_promo_hide',
719
				'_transient_give_contributors',
720
				'_transient_give_estimated_monthly_stats',
721
				'_transient_give_earnings_total',
722
				'_transient_give_i18n_give_',
723
				'_transient__give_installed',
724
				'_transient__give_activation_redirect',
725
				'_transient__give_hide_license_notices_shortly_',
726
				'give_income_total',
727
			)
728
		),
729
		1
730
	);
731
732
	// User related transients.
733
	$user_apikey_options = $wpdb->get_results(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
734
		$wpdb->prepare(
735
			"SELECT user_id, meta_key
736
			FROM $wpdb->usermeta
0 ignored issues
show
introduced by
Usage of users/usermeta tables is highly discouraged in VIP context, For storing user additional user metadata, you should look at User Attributes.
Loading history...
737
			WHERE meta_value=%s",
738
			'give_user_public_key'
739
		),
740
		ARRAY_A
741
	);
742
743
	if ( ! empty( $user_apikey_options ) ) {
744
		foreach ( $user_apikey_options as $user ) {
745
			$cached_options[] = '_transient_' . md5( 'give_api_user_' . $user['meta_key'] );
746
			$cached_options[] = '_transient_' . md5( 'give_api_user_public_key' . $user['user_id'] );
747
			$cached_options[] = '_transient_' . md5( 'give_api_user_secret_key' . $user['user_id'] );
748
		}
749
	}
750
751
	if ( ! empty( $cached_options ) ) {
752
		foreach ( $cached_options as $option ) {
753 View Code Duplication
			switch ( true ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
754
				case ( false !== strpos( $option, 'transient' ) ):
755
					$option = str_replace( '_transient_', '', $option );
756
					delete_transient( $option );
757
					break;
758
759
				default:
760
					delete_option( $option );
761
			}
762
		}
763
	}
764
}
765
766
/**
767
 * Update Capabilities for Give_Worker User Role.
768
 *
769
 * This upgrade routine will update access rights for Give_Worker User Role.
770
 *
771
 * @since      1.8.8
772
 */
773
function give_v188_upgrades() {
774
775
	global $wp_roles;
776
777
	// Get the role object.
778
	$give_worker = get_role( 'give_worker' );
779
780
	// A list of capabilities to add for give workers.
781
	$caps_to_add = array(
782
		'edit_posts',
783
		'edit_pages',
784
	);
785
786
	foreach ( $caps_to_add as $cap ) {
787
		// Add the capability.
788
		$give_worker->add_cap( $cap );
789
	}
790
791
}
792
793
/**
794
 * Update Post meta for minimum and maximum amount for multi level donation forms
795
 *
796
 * This upgrade routine adds post meta for give_forms CPT for multi level donation form.
797
 *
798
 * @since      1.8.9
799
 */
800
function give_v189_upgrades_levels_post_meta_callback() {
801
	/* @var Give_Updates $give_updates */
802
	$give_updates = Give_Updates::get_instance();
803
804
	// form query
805
	$donation_forms = new WP_Query( array(
806
			'paged'          => $give_updates->step,
807
			'status'         => 'any',
808
			'order'          => 'ASC',
809
			'post_type'      => 'give_forms',
810
			'posts_per_page' => 20,
811
		)
812
	);
813
814
	if ( $donation_forms->have_posts() ) {
815
		$give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
816
817
		while ( $donation_forms->have_posts() ) {
818
			$donation_forms->the_post();
819
			$form_id = get_the_ID();
820
821
			// Remove formatting from _give_set_price
822
			update_post_meta(
823
				$form_id,
824
				'_give_set_price',
825
				give_sanitize_amount( get_post_meta( $form_id, '_give_set_price', true ) )
826
			);
827
828
			// Remove formatting from _give_custom_amount_minimum
829
			update_post_meta(
830
				$form_id,
831
				'_give_custom_amount_minimum',
832
				give_sanitize_amount( get_post_meta( $form_id, '_give_custom_amount_minimum', true ) )
833
			);
834
835
			// Bailout.
836
			if ( 'set' === get_post_meta( $form_id, '_give_price_option', true ) ) {
837
				continue;
838
			}
839
840
			$donation_levels = get_post_meta( $form_id, '_give_donation_levels', true );
841
842
			if ( ! empty( $donation_levels ) ) {
843
844
				foreach ( $donation_levels as $index => $donation_level ) {
845
					if ( isset( $donation_level['_give_amount'] ) ) {
846
						$donation_levels[ $index ]['_give_amount'] = give_sanitize_amount( $donation_level['_give_amount'] );
847
					}
848
				}
849
850
				update_post_meta( $form_id, '_give_donation_levels', $donation_levels );
851
852
				$donation_levels_amounts = wp_list_pluck( $donation_levels, '_give_amount' );
853
854
				$min_amount = min( $donation_levels_amounts );
855
				$max_amount = max( $donation_levels_amounts );
856
857
				// Set Minimum and Maximum amount for Multi Level Donation Forms
858
				give_update_meta( $form_id, '_give_levels_minimum_amount', $min_amount ? give_sanitize_amount( $min_amount ) : 0 );
859
				give_update_meta( $form_id, '_give_levels_maximum_amount', $max_amount ? give_sanitize_amount( $max_amount ) : 0 );
860
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
861
862
		}
863
864
		/* Restore original Post Data */
865
		wp_reset_postdata();
866
	} else {
867
		// The Update Ran.
868
		give_set_upgrade_complete( 'v189_upgrades_levels_post_meta' );
869
	}
870
871
}
872
873
874
/**
875
 * Give version 1.8.9 upgrades
876
 *
877
 * @since      1.8.9
878
 */
879
function give_v189_upgrades() {
880
	/**
881
	 * 1. Remove user license related notice show blocked ( Give_Notice will handle )
882
	 */
883
	global $wpdb;
884
885
	// Delete permanent notice blocker.
886
	$wpdb->query(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
887
		$wpdb->prepare(
888
			"
889
					DELETE FROM $wpdb->usermeta
0 ignored issues
show
introduced by
Usage of users/usermeta tables is highly discouraged in VIP context, For storing user additional user metadata, you should look at User Attributes.
Loading history...
890
					WHERE meta_key
891
					LIKE '%%%s%%'
892
					",
893
			'_give_hide_license_notices_permanently'
894
		)
895
	);
896
897
	// Delete short notice blocker.
898
	$wpdb->query(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
899
		$wpdb->prepare(
900
			"
901
					DELETE FROM $wpdb->options
902
					WHERE option_name
903
					LIKE '%%%s%%'
904
					",
905
			'__give_hide_license_notices_shortly_'
906
		)
907
	);
908
909
}
910
911
912
/**
913
 * Give version 1.8.9 upgrades
914
 *
915
 * @since      1.8.9
916
 */
917
function give_v1812_upgrades() {
918
	/**
919
	 * Validate number format settings.
920
	 */
921
	$give_settings        = give_get_settings();
922
	$give_setting_updated = false;
923
924
	if ( $give_settings['thousands_separator'] === $give_settings['decimal_separator'] ) {
925
		$give_settings['number_decimals']   = 0;
926
		$give_settings['decimal_separator'] = '';
927
		$give_setting_updated               = true;
928
929
	} elseif ( empty( $give_settings['decimal_separator'] ) ) {
930
		$give_settings['number_decimals'] = 0;
931
		$give_setting_updated             = true;
932
933
	} elseif ( 6 < absint( $give_settings['number_decimals'] ) ) {
934
		$give_settings['number_decimals'] = 5;
935
		$give_setting_updated             = true;
936
	}
937
938
	if ( $give_setting_updated ) {
939
		update_option( 'give_settings', $give_settings );
940
	}
941
}
942
943
944
/**
945
 * Give version 1.8.12 update
946
 *
947
 * Standardized amount values to six decimal
948
 *
949
 * @see        https://github.com/WordImpress/Give/issues/1849#issuecomment-315128602
950
 *
951
 * @since      1.8.12
952
 */
953
function give_v1812_update_amount_values_callback() {
954
	/* @var Give_Updates $give_updates */
955
	$give_updates = Give_Updates::get_instance();
956
957
	// form query
958
	$donation_forms = new WP_Query( array(
959
			'paged'          => $give_updates->step,
960
			'status'         => 'any',
961
			'order'          => 'ASC',
962
			'post_type'      => array( 'give_forms', 'give_payment' ),
963
			'posts_per_page' => 20,
964
		)
965
	);
966
967
	if ( $donation_forms->have_posts() ) {
968
		$give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
969
970
		while ( $donation_forms->have_posts() ) {
971
			$donation_forms->the_post();
972
			global $post;
973
974
			$meta = get_post_meta( $post->ID );
975
976
			switch ( $post->post_type ) {
977
				case 'give_forms':
978
					// _give_set_price
979
					if( ! empty( $meta['_give_set_price'][0] ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
980
						update_post_meta( $post->ID, '_give_set_price', give_sanitize_amount_for_db( $meta['_give_set_price'][0] ) );
981
					}
982
983
					// _give_custom_amount_minimum
984
					if( ! empty( $meta['_give_custom_amount_minimum'][0] ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
985
						update_post_meta( $post->ID, '_give_custom_amount_minimum', give_sanitize_amount_for_db( $meta['_give_custom_amount_minimum'][0] ) );
986
					}
987
988
					// _give_levels_minimum_amount
989
					if( ! empty( $meta['_give_levels_minimum_amount'][0] ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
990
						update_post_meta( $post->ID, '_give_levels_minimum_amount', give_sanitize_amount_for_db( $meta['_give_levels_minimum_amount'][0] ) );
991
					}
992
993
					// _give_levels_maximum_amount
994
					if( ! empty( $meta['_give_levels_maximum_amount'][0] ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
995
						update_post_meta( $post->ID, '_give_levels_maximum_amount', give_sanitize_amount_for_db( $meta['_give_levels_maximum_amount'][0] ) );
996
					}
997
998
					// _give_set_goal
999
					if( ! empty( $meta['_give_set_goal'][0] ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
1000
						update_post_meta( $post->ID, '_give_set_goal', give_sanitize_amount_for_db( $meta['_give_set_goal'][0] ) );
1001
					}
1002
1003
					// _give_form_earnings
1004
					if( ! empty( $meta['_give_form_earnings'][0] ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
1005
						update_post_meta( $post->ID, '_give_form_earnings', give_sanitize_amount_for_db( $meta['_give_form_earnings'][0] ) );
1006
					}
1007
1008
					// _give_custom_amount_minimum
1009
					if( ! empty( $meta['_give_donation_levels'][0] ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
1010
						$donation_levels = unserialize( $meta['_give_donation_levels'][0] );
1011
1012
						foreach( $donation_levels as $index => $level ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
1013
							if( empty( $level['_give_amount'] ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
1014
								continue;
1015
							}
1016
1017
							$donation_levels[$index]['_give_amount'] = give_sanitize_amount_for_db( $level['_give_amount'] );
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
1018
						}
1019
1020
						$meta['_give_donation_levels'] = $donation_levels;
1021
						update_post_meta( $post->ID, '_give_donation_levels', $meta['_give_donation_levels'] );
1022
					}
1023
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
1024
1025
					break;
1026
1027
				case 'give_payment':
1028
					// _give_payment_total
1029
					if( ! empty( $meta['_give_payment_total'][0] ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
1030
						update_post_meta( $post->ID, '_give_payment_total', give_sanitize_amount_for_db( $meta['_give_payment_total'][0] ) );
1031
					}
1032
1033
					break;
1034
			}
1035
		}
1036
1037
		/* Restore original Post Data */
1038
		wp_reset_postdata();
1039
	} else {
1040
		// The Update Ran.
1041
		give_set_upgrade_complete( 'v1812_update_amount_values' );
1042
	}
1043
}
1044
1045
1046
/**
1047
 * Give version 1.8.12 update
1048
 *
1049
 * Standardized amount values to six decimal for donor
1050
 *
1051
 * @see        https://github.com/WordImpress/Give/issues/1849#issuecomment-315128602
1052
 *
1053
 * @since      1.8.12
1054
 */
1055
function give_v1812_update_donor_purchase_value_callback() {
1056
	/* @var Give_Updates $give_updates */
1057
	$give_updates = Give_Updates::get_instance();
1058
	$offset       = 1 === $give_updates->step ? 0 : $give_updates->step * 20;
1059
1060
	// form query
1061
	$donors = Give()->donors->get_donors( array(
1062
			'number' => 20,
1063
			'offset' => $offset,
1064
		)
1065
	);
1066
1067
	if ( ! empty( $donors ) ) {
1068
		$give_updates->set_percentage( Give()->donors->count(), ( $give_updates->step * 20 ) );
1069
1070
		/* @var Object $donor */
1071
		foreach ( $donors as $donor ) {
1072
			Give()->donors->update( $donor->id, array( 'purchase_value' => give_sanitize_amount_for_db( $donor->purchase_value ) ) );
1073
		}
1074
	} else {
1075
		// The Update Ran.
1076
		give_set_upgrade_complete( 'v1812_update_donor_purchase_values' );
1077
	}
1078
}
1079