Completed
Push — issues/1849 ( 0646af...ebd8ee )
by Ravinder
17:48
created

upgrade-functions.php ➔ give_v1812_update_amount_values_callback()   D

Complexity

Conditions 15
Paths 133

Size

Total Lines 91
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 44
nc 133
nop 0
dl 0
loc 91
rs 4.597
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 26 and the first side effect is on line 16.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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;
40
41
		case version_compare( $give_version, '1.7', '<' ) :
42
			give_v17_upgrades();
43
			$did_upgrade = true;
44
45
		case version_compare( $give_version, '1.8', '<' ) :
46
			give_v18_upgrades();
47
			$did_upgrade = true;
48
49
		case version_compare( $give_version, '1.8.7', '<' ) :
50
			give_v187_upgrades();
51
			$did_upgrade = true;
52
53
		case version_compare( $give_version, '1.8.8', '<' ) :
54
			give_v188_upgrades();
55
			$did_upgrade = true;
56
57
		case version_compare( $give_version, '1.8.9', '<' ) :
58
			give_v189_upgrades();
59
			$did_upgrade = true;
60
61
	}
62
63
	if ( $did_upgrade ) {
64
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
65
	}
66
}
67
68
add_action( 'admin_init', 'give_do_automatic_upgrades' );
69
add_action( 'give_upgrades', 'give_do_automatic_upgrades' );
70
71
/**
72
 * Display Upgrade Notices
73
 *
74
 * @since 1.0
75
 * @since 1.8.12 Update new udate process code.
76
 *
77
 * @param Give_Updates $give_updates
78
 *
79
 * @return void
80
 */
81
function give_show_upgrade_notices( $give_updates ) {
82
	// v1.3.2 Upgrades
83
	$give_updates->register(
84
		array(
85
			'id'       => 'upgrade_give_payment_customer_id',
86
			'version'  => '1.3.2',
87
			'callback' => 'give_v132_upgrade_give_payment_customer_id',
88
		)
89
	);
90
91
	// v1.3.4 Upgrades ensure the user has gone through 1.3.4.
92
	$give_updates->register(
93
		array(
94
			'id'       => 'upgrade_give_offline_status',
95
			'depend'   => 'upgrade_give_payment_customer_id',
96
			'version'  => '1.3.4',
97
			'callback' => 'give_v134_upgrade_give_offline_status',
98
		)
99
	);
100
101
	// v1.8 form metadata upgrades.
102
	$give_updates->register(
103
		array(
104
			'id'       => 'v18_upgrades_form_metadata',
105
			'version'  => '1.8',
106
			'callback' => 'give_v18_upgrades_form_metadata',
107
		)
108
	);
109
110
	// v1.8.9 Upgrades
111
	$give_updates->register(
112
		array(
113
			'id'       => 'v189_upgrades_levels_post_meta',
114
			'version'  => '1.8.9',
115
			'callback' => 'give_v189_upgrades_levels_post_meta_callback',
116
		)
117
	);
118
119
	// v1.8.12 Upgrades
120
	$give_updates->register(
121
		array(
122
			'id'       => 'v1812_update_amount_values',
123
			'version'  => '1.8.12',
124
			'callback' => 'give_v1812_update_amount_values_callback',
125
		)
126
	);
127
}
128
129
add_action( 'give_register_updates', 'give_show_upgrade_notices' );
130
131
/**
132
 * Triggers all upgrade functions
133
 *
134
 * This function is usually triggered via AJAX
135
 *
136
 * @since 1.0
137
 * @return void
138
 */
139
function give_trigger_upgrades() {
140
141
	if ( ! current_user_can( 'manage_give_settings' ) ) {
142
		wp_die( esc_html__( 'You do not have permission to do Give upgrades.', 'give' ), esc_html__( 'Error', 'give' ), array(
143
			'response' => 403,
144
		) );
145
	}
146
147
	$give_version = get_option( 'give_version' );
148
149
	if ( ! $give_version ) {
150
		// 1.0 is the first version to use this option so we must add it.
151
		$give_version = '1.0';
152
		add_option( 'give_version', $give_version );
153
	}
154
155
	update_option( 'give_version', GIVE_VERSION );
156
	delete_option( 'give_doing_upgrade' );
157
158
	if ( DOING_AJAX ) {
159
		die( 'complete' );
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_trigger_upgrades() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
160
	} // End if().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
161
}
162
163
add_action( 'wp_ajax_give_trigger_upgrades', 'give_trigger_upgrades' );
164
165
/**
166
 * Check if the upgrade routine has been run for a specific action
167
 *
168
 * @since  1.0
169
 *
170
 * @param  string $upgrade_action The upgrade action to check completion for
171
 *
172
 * @return bool                   If the action has been added to the completed actions array
173
 */
174
function give_has_upgrade_completed( $upgrade_action = '' ) {
175
176
	if ( empty( $upgrade_action ) ) {
177
		return false;
178
	}
179
180
	$completed_upgrades = give_get_completed_upgrades();
181
182
	return in_array( $upgrade_action, $completed_upgrades );
183
184
}
185
186
/**
187
 * For use when doing 'stepped' upgrade routines, to see if we need to start somewhere in the middle
188
 *
189
 * @since 1.8
190
 *
191
 * @return mixed   When nothing to resume returns false, otherwise starts the upgrade where it left off
192
 */
193
function give_maybe_resume_upgrade() {
194
	$doing_upgrade = get_option( 'give_doing_upgrade', false );
195
	if ( empty( $doing_upgrade ) ) {
196
		return false;
197
	}
198
199
	return $doing_upgrade;
200
}
201
202
/**
203
 * Adds an upgrade action to the completed upgrades array
204
 *
205
 * @since  1.0
206
 *
207
 * @param  string $upgrade_action The action to add to the completed upgrades array
208
 *
209
 * @return bool                   If the function was successfully added
210
 */
211
function give_set_upgrade_complete( $upgrade_action = '' ) {
212
213
	if ( empty( $upgrade_action ) ) {
214
		return false;
215
	}
216
217
	$completed_upgrades   = give_get_completed_upgrades();
218
	$completed_upgrades[] = $upgrade_action;
219
220
	// Remove any blanks, and only show uniques.
221
	$completed_upgrades = array_unique( array_values( $completed_upgrades ) );
222
223
	/**
224
	 * Fire the action when any upgrade set to complete.
225
	 *
226
	 * @since 1.8.12
227
	 */
228
	do_action( 'give_set_upgrade_completed', $upgrade_action, $completed_upgrades );
229
230
	return update_option( 'give_completed_upgrades', $completed_upgrades );
231
}
232
233
/**
234
 * Get's the array of completed upgrade actions
235
 *
236
 * @since  1.0
237
 * @return array The array of completed upgrades
238
 */
239
function give_get_completed_upgrades() {
240
241
	return (array) get_option( 'give_completed_upgrades' );
242
243
}
244
245
/**
246
 * Upgrades the
247
 *
248
 * Standardizes the discrepancies between two metakeys `_give_payment_customer_id` and `_give_payment_donor_id`
249
 *
250
 * @since      1.3.2
251
 */
252
function give_v132_upgrade_give_payment_customer_id() {
253
	global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
254
255
	/* @var Give_Updates $give_updates */
256
	$give_updates = Give_Updates::get_instance();
257
258
	if ( ! current_user_can( 'manage_give_settings' ) ) {
259
		wp_die( esc_html__( 'You do not have permission to do Give upgrades.', 'give' ), esc_html__( 'Error', 'give' ), array(
260
			'response' => 403,
261
		) );
262
	}
263
264
	ignore_user_abort( true );
265
266
	if ( ! give_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {
267
		@set_time_limit( 0 );
268
	}
269
270
	// UPDATE DB METAKEYS.
271
	$sql   = "UPDATE $wpdb->postmeta SET meta_key = '_give_payment_customer_id' WHERE meta_key = '_give_payment_donor_id'";
272
	$query = $wpdb->query( $sql );
273
274
	$give_updates::$percentage = 100;
275
	give_set_upgrade_complete( 'upgrade_give_payment_customer_id' );
276
}
277
278
279
/**
280
 * Upgrades the Offline Status
281
 *
282
 * Reverses the issue where offline donations in "pending" status where inappropriately marked as abandoned
283
 *
284
 * @since      1.3.4
285
 */
286
function give_v134_upgrade_give_offline_status() {
287
	global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
288
289
	/* @var Give_Updates $give_updates */
290
	$give_updates = Give_Updates::get_instance();
291
292
	// Get abandoned offline payments.
293
	$select = "SELECT ID FROM $wpdb->posts p ";
294
	$join   = "LEFT JOIN $wpdb->postmeta m ON p.ID = m.post_id ";
295
	$where  = "WHERE p.post_type = 'give_payment' ";
296
	$where  .= "AND ( p.post_status = 'abandoned' )";
297
	$where  .= "AND ( m.meta_key = '_give_payment_gateway' AND m.meta_value = 'offline' )";
298
299
	$sql            = $select . $join . $where;
300
	$found_payments = $wpdb->get_col( $sql );
301
302
	foreach ( $found_payments as $payment ) {
303
304
		// Only change ones marked abandoned since our release last week because the admin may have marked some abandoned themselves.
305
		$modified_time = get_post_modified_time( 'U', false, $payment );
306
307
		// 1450124863 =  12/10/2015 20:42:25.
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
308
		if ( $modified_time >= 1450124863 ) {
309
310
			give_update_payment_status( $payment, 'pending' );
311
312
		}
313
	}
314
315
	$give_updates::$percentage = 100;
316
	give_set_upgrade_complete( 'upgrade_give_offline_status' );
317
}
318
319
320
/**
321
 * Cleanup User Roles
322
 *
323
 * This upgrade routine removes unused roles and roles with typos
324
 *
325
 * @since      1.5.2
326
 */
327
function give_v152_cleanup_users() {
328
329
	$give_version = get_option( 'give_version' );
330
331
	if ( ! $give_version ) {
332
		// 1.0 is the first version to use this option so we must add it.
333
		$give_version = '1.0';
334
	}
335
336
	$give_version = preg_replace( '/[^0-9.].*/', '', $give_version );
337
338
	// v1.5.2 Upgrades
339
	if ( version_compare( $give_version, '1.5.2', '<' ) || ! give_has_upgrade_completed( 'upgrade_give_user_caps_cleanup' ) ) {
340
341
		// Delete all caps with "ss".
342
		// Also delete all unused "campaign" roles.
343
		$delete_caps = array(
344
			'delete_give_formss',
345
			'delete_others_give_formss',
346
			'delete_private_give_formss',
347
			'delete_published_give_formss',
348
			'read_private_forms',
349
			'edit_give_formss',
350
			'edit_others_give_formss',
351
			'edit_private_give_formss',
352
			'edit_published_give_formss',
353
			'publish_give_formss',
354
			'read_private_give_formss',
355
			'assign_give_campaigns_terms',
356
			'delete_give_campaigns',
357
			'delete_give_campaigns_terms',
358
			'delete_give_campaignss',
359
			'delete_others_give_campaignss',
360
			'delete_private_give_campaignss',
361
			'delete_published_give_campaignss',
362
			'edit_give_campaigns',
363
			'edit_give_campaigns_terms',
364
			'edit_give_campaignss',
365
			'edit_others_give_campaignss',
366
			'edit_private_give_campaignss',
367
			'edit_published_give_campaignss',
368
			'manage_give_campaigns_terms',
369
			'publish_give_campaignss',
370
			'read_give_campaigns',
371
			'read_private_give_campaignss',
372
			'view_give_campaigns_stats',
373
			'delete_give_paymentss',
374
			'delete_others_give_paymentss',
375
			'delete_private_give_paymentss',
376
			'delete_published_give_paymentss',
377
			'edit_give_paymentss',
378
			'edit_others_give_paymentss',
379
			'edit_private_give_paymentss',
380
			'edit_published_give_paymentss',
381
			'publish_give_paymentss',
382
			'read_private_give_paymentss',
383
		);
384
385
		global $wp_roles;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
386
		foreach ( $delete_caps as $cap ) {
387
			foreach ( array_keys( $wp_roles->roles ) as $role ) {
388
				$wp_roles->remove_cap( $role, $cap );
389
			}
390
		}
391
392
		// Create Give plugin roles.
393
		$roles = new Give_Roles();
394
		$roles->add_roles();
395
		$roles->add_caps();
396
397
		// The Update Ran.
398
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
399
		give_set_upgrade_complete( 'upgrade_give_user_caps_cleanup' );
400
		delete_option( 'give_doing_upgrade' );
401
402
	}// End if().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
403
404
}
405
406
add_action( 'admin_init', 'give_v152_cleanup_users' );
407
408
/**
409
 * 1.6 Upgrade routine to create the customer meta table.
410
 *
411
 * @since  1.6
412
 * @return void
413
 */
414
function give_v16_upgrades() {
415
	// Create the donor databases.
416
	$donors_db = new Give_DB_Donors();
417
	$donors_db->create_table();
418
	$donor_meta = new Give_DB_Donor_Meta();
419
	$donor_meta->create_table();
420
}
421
422
/**
423
 * 1.7 Upgrades.
424
 *
425
 * a. Update license api data for plugin addons.
426
 * b. Cleanup user roles.
427
 *
428
 * @since  1.7
429
 * @return void
430
 */
431
function give_v17_upgrades() {
432
	// Upgrade license data.
433
	give_v17_upgrade_addon_license_data();
434
	give_v17_cleanup_roles();
435
}
436
437
/**
438
 * Upgrade license data
439
 *
440
 * @since 1.7
441
 */
442
function give_v17_upgrade_addon_license_data() {
443
	$give_options = give_get_settings();
444
445
	$api_url = 'https://givewp.com/give-sl-api/';
446
447
	// Get addons license key.
448
	$addons = array();
449
	foreach ( $give_options as $key => $value ) {
450
		if ( false !== strpos( $key, '_license_key' ) ) {
451
			$addons[ $key ] = $value;
452
		}
453
	}
454
455
	// Bailout: We do not have any addon license data to upgrade.
456
	if ( empty( $addons ) ) {
457
		return false;
458
	}
459
460
	foreach ( $addons as $key => $addon_license ) {
461
462
		// Get addon shortname.
463
		$shortname = str_replace( '_license_key', '', $key );
464
465
		// Addon license option name.
466
		$addon_license_option_name = $shortname . '_license_active';
467
468
		// bailout if license is empty.
469
		if ( empty( $addon_license ) ) {
470
			delete_option( $addon_license_option_name );
471
			continue;
472
		}
473
474
		// Get addon name.
475
		$addon_name       = array();
476
		$addon_name_parts = explode( '_', str_replace( 'give_', '', $shortname ) );
477
		foreach ( $addon_name_parts as $name_part ) {
478
479
			// Fix addon name
480
			switch ( $name_part ) {
481
				case 'authorizenet' :
482
					$name_part = 'authorize.net';
483
					break;
484
			}
485
486
			$addon_name[] = ucfirst( $name_part );
487
		}
488
489
		$addon_name = implode( ' ', $addon_name );
490
491
		// Data to send to the API
492
		$api_params = array(
493
			'edd_action' => 'activate_license', // never change from "edd_" to "give_"!
494
			'license'    => $addon_license,
495
			'item_name'  => urlencode( $addon_name ),
496
			'url'        => home_url(),
497
		);
498
499
		// Call the API.
500
		$response = wp_remote_post(
501
			$api_url,
502
			array(
503
				'timeout'   => 15,
504
				'sslverify' => false,
505
				'body'      => $api_params,
506
			)
507
		);
508
509
		// Make sure there are no errors.
510
		if ( is_wp_error( $response ) ) {
511
			delete_option( $addon_license_option_name );
512
			continue;
513
		}
514
515
		// Tell WordPress to look for updates.
516
		set_site_transient( 'update_plugins', null );
517
518
		// Decode license data.
519
		$license_data = json_decode( wp_remote_retrieve_body( $response ) );
520
		update_option( $addon_license_option_name, $license_data );
521
	}// End foreach().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
522
}
523
524
525
/**
526
 * Cleanup User Roles.
527
 *
528
 * This upgrade routine removes unused roles and roles with typos.
529
 *
530
 * @since      1.7
531
 */
532
function give_v17_cleanup_roles() {
533
534
	// Delete all caps with "_give_forms_" and "_give_payments_"
535
	// These roles have no usage; the proper is singular.
536
	$delete_caps = array(
537
		'view_give_forms_stats',
538
		'delete_give_forms_terms',
539
		'assign_give_forms_terms',
540
		'edit_give_forms_terms',
541
		'manage_give_forms_terms',
542
		'view_give_payments_stats',
543
		'manage_give_payments_terms',
544
		'edit_give_payments_terms',
545
		'assign_give_payments_terms',
546
		'delete_give_payments_terms',
547
	);
548
549
	global $wp_roles;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
550
	foreach ( $delete_caps as $cap ) {
551
		foreach ( array_keys( $wp_roles->roles ) as $role ) {
552
			$wp_roles->remove_cap( $role, $cap );
553
		}
554
	}
555
556
	// Set roles again.
557
	$roles = new Give_Roles();
558
	$roles->add_roles();
559
	$roles->add_caps();
560
561
}
562
563
/**
564
 * 1.8 Upgrades.
565
 *
566
 * a. Upgrade checkbox settings to radio button settings.
567
 * a. Update form meta for new metabox settings.
568
 *
569
 * @since  1.8
570
 * @return void
571
 */
572
function give_v18_upgrades() {
573
	// Upgrade checkbox settings to radio button settings.
574
	give_v18_upgrades_core_setting();
575
}
576
577
/**
578
 * Upgrade core settings.
579
 *
580
 * @since  1.8
581
 * @return void
582
 */
583
function give_v18_upgrades_core_setting() {
584
	// Core settings which changes from checkbox to radio.
585
	$core_setting_names = array_merge(
586
		array_keys( give_v18_renamed_core_settings() ),
587
		array(
588
			'uninstall_on_delete',
589
			'scripts_footer',
590
			'test_mode',
591
			'email_access',
592
			'terms',
593
			'give_offline_donation_enable_billing_fields',
594
		)
595
	);
596
597
	// Bailout: If not any setting define.
598
	if ( $give_settings = get_option( 'give_settings' ) ) {
599
600
		$setting_changed = false;
601
602
		// Loop: check each setting field.
603
		foreach ( $core_setting_names as $setting_name ) {
604
			// New setting name.
605
			$new_setting_name = preg_replace( '/^(enable_|disable_)/', '', $setting_name );
606
607
			// Continue: If setting already set.
608
			if (
609
				array_key_exists( $new_setting_name, $give_settings )
610
				&& in_array( $give_settings[ $new_setting_name ], array( 'enabled', 'disabled' ) )
611
			) {
612
				continue;
613
			}
614
615
			// Set checkbox value to radio value.
616
			$give_settings[ $setting_name ] = ( ! empty( $give_settings[ $setting_name ] ) && 'on' === $give_settings[ $setting_name ] ? 'enabled' : 'disabled' );
617
618
			// @see https://github.com/WordImpress/Give/issues/1063
619
			if ( false !== strpos( $setting_name, 'disable_' ) ) {
620
621
				$give_settings[ $new_setting_name ] = ( give_is_setting_enabled( $give_settings[ $setting_name ] ) ? 'disabled' : 'enabled' );
622
			} elseif ( false !== strpos( $setting_name, 'enable_' ) ) {
623
624
				$give_settings[ $new_setting_name ] = ( give_is_setting_enabled( $give_settings[ $setting_name ] ) ? 'enabled' : 'disabled' );
625
			}
626
627
			// Tell bot to update core setting to db.
628
			if ( ! $setting_changed ) {
629
				$setting_changed = true;
630
			}
631
		}
632
633
		// Update setting only if they changed.
634
		if ( $setting_changed ) {
635
			update_option( 'give_settings', $give_settings );
636
		}
637
	}// End if().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
638
639
	give_set_upgrade_complete( 'v18_upgrades_core_setting' );
640
}
641
642
/**
643
 * Upgrade form metadata for new metabox settings.
644
 *
645
 * @since  1.8
646
 * @return void
647
 */
648
function give_v18_upgrades_form_metadata() {
649
	/* @var Give_Updates $give_updates */
650
	$give_updates = Give_Updates::get_instance();
651
652
	// form query
653
	$forms = new WP_Query( array(
654
			'paged'          => $give_updates::$step,
655
			'status'         => 'any',
656
			'order'          => 'ASC',
657
			'post_type'      => 'give_forms',
658
			'posts_per_page' => 20,
659
		)
660
	);
661
662
	if ( $forms->have_posts() ) {
663
		$give_updates::$percentage = ( ( $give_updates::$step * 20 ) / $forms->found_posts  ) * 100;
664
665
		error_log( print_r( "{$give_updates::$step}-{$forms->found_posts}-{$give_updates::$percentage}", true ) . "\n", 3, WP_CONTENT_DIR . '/debug_new.log' );
666
667
		while ( $forms->have_posts() ) {
668
			$forms->the_post();
669
670
			// Form content.
671
			// Note in version 1.8 display content setting split into display content and content placement setting.
672
			// You can delete _give_content_option in future
673
			$show_content = give_get_meta( get_the_ID(), '_give_content_option', true );
674
			if ( $show_content && ! give_get_meta( get_the_ID(), '_give_display_content', true ) ) {
675
				$field_value = ( 'none' !== $show_content ? 'enabled' : 'disabled' );
676
				give_update_meta( get_the_ID(), '_give_display_content', $field_value );
677
678
				$field_value = ( 'none' !== $show_content ? $show_content : 'give_pre_form' );
679
				give_update_meta( get_the_ID(), '_give_content_placement', $field_value );
680
			}
681
682
			// "Disable" Guest Donation. Checkbox
683
			// See: https://github.com/WordImpress/Give/issues/1470
684
			$guest_donation        = give_get_meta( get_the_ID(), '_give_logged_in_only', true );
685
			$guest_donation_newval = ( in_array( $guest_donation, array( 'yes', 'on' ) ) ? 'disabled' : 'enabled' );
686
			give_update_meta( get_the_ID(), '_give_logged_in_only', $guest_donation_newval );
687
688
			// Offline Donations
689
			// See: https://github.com/WordImpress/Give/issues/1579
690
			$offline_donation = give_get_meta( get_the_ID(), '_give_customize_offline_donations', true );
691
			if ( 'no' === $offline_donation ) {
692
				$offline_donation_newval = 'global';
693
			} elseif ( 'yes' === $offline_donation ) {
694
				$offline_donation_newval = 'enabled';
695
			} else {
696
				$offline_donation_newval = 'disabled';
697
			}
698
			give_update_meta( get_the_ID(), '_give_customize_offline_donations', $offline_donation_newval );
699
700
			// Convert yes/no setting field to enabled/disabled.
701
			$form_radio_settings = array(
702
				// Custom Amount.
703
				'_give_custom_amount',
704
705
				// Donation Gaol.
706
				'_give_goal_option',
707
708
				// Close Form.
709
				'_give_close_form_when_goal_achieved',
710
711
				// Term & conditions.
712
				'_give_terms_option',
713
714
				// Billing fields.
715
				'_give_offline_donation_enable_billing_fields_single',
716
			);
717
718
			foreach ( $form_radio_settings as $meta_key ) {
719
				// Get value.
720
				$field_value = give_get_meta( get_the_ID(), $meta_key, true );
721
722
				// Convert meta value only if it is in yes/no/none.
723
				if ( in_array( $field_value, array( 'yes', 'on', 'no', 'none' ) ) ) {
724
725
					$field_value = ( in_array( $field_value, array( 'yes', 'on' ) ) ? 'enabled' : 'disabled' );
726
					give_update_meta( get_the_ID(), $meta_key, $field_value );
727
				}
728
			}
729
		}// End while().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
730
731
		wp_reset_postdata();
732
733
	} else {
734
		// No more forms found, finish up.
735
		give_set_upgrade_complete( 'v18_upgrades_form_metadata' );
736
	}
737
}
738
739
740
/**
741
 * Get list of core setting renamed in version 1.8.
742
 *
743
 * @since  1.8
744
 * @return array
745
 */
746
function give_v18_renamed_core_settings() {
747
	return array(
748
		'disable_paypal_verification' => 'paypal_verification',
749
		'disable_css'                 => 'css',
750
		'disable_welcome'             => 'welcome',
751
		'disable_forms_singular'      => 'forms_singular',
752
		'disable_forms_archives'      => 'forms_archives',
753
		'disable_forms_excerpt'       => 'forms_excerpt',
754
		'disable_form_featured_img'   => 'form_featured_img',
755
		'disable_form_sidebar'        => 'form_sidebar',
756
		'disable_admin_notices'       => 'admin_notices',
757
		'disable_the_content_filter'  => 'the_content_filter',
758
		'enable_floatlabels'          => 'floatlabels',
759
		'enable_categories'           => 'categories',
760
		'enable_tags'                 => 'tags',
761
	);
762
}
763
764
765
/**
766
 * Upgrade core settings.
767
 *
768
 * @since  1.8.7
769
 * @return void
770
 */
771
function give_v187_upgrades() {
772
	global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
773
774
	/**
775
	 * Upgrade 1: Remove stat and cache transients.
776
	 */
777
	$cached_options = $wpdb->get_col(
778
		$wpdb->prepare(
779
			"SELECT * FROM {$wpdb->options} where (option_name LIKE '%%%s%%' OR option_name LIKE '%%%s%%')",
780
			array(
781
				'_transient_give_stats_',
782
				'give_cache',
783
				'_transient_give_add_ons_feed',
784
				'_transient__give_ajax_works' .
785
				'_transient_give_total_api_keys',
786
				'_transient_give_i18n_give_promo_hide',
787
				'_transient_give_contributors',
788
				'_transient_give_estimated_monthly_stats',
789
				'_transient_give_earnings_total',
790
				'_transient_give_i18n_give_',
791
				'_transient__give_installed',
792
				'_transient__give_activation_redirect',
793
				'_transient__give_hide_license_notices_shortly_',
794
				'give_income_total',
795
			)
796
		),
797
		1
798
	);
799
800
	// User related transients.
801
	$user_apikey_options = $wpdb->get_results(
802
		$wpdb->prepare(
803
			"SELECT user_id, meta_key
804
			FROM $wpdb->usermeta
805
			WHERE meta_value=%s",
806
			'give_user_public_key'
807
		),
808
		ARRAY_A
809
	);
810
811
	if ( ! empty( $user_apikey_options ) ) {
812
		foreach ( $user_apikey_options as $user ) {
813
			$cached_options[] = '_transient_' . md5( 'give_api_user_' . $user['meta_key'] );
814
			$cached_options[] = '_transient_' . md5( 'give_api_user_public_key' . $user['user_id'] );
815
			$cached_options[] = '_transient_' . md5( 'give_api_user_secret_key' . $user['user_id'] );
816
		}
817
	}
818
819
	if ( ! empty( $cached_options ) ) {
820
		foreach ( $cached_options as $option ) {
821
			switch ( true ) {
822
				case ( false !== strpos( $option, 'transient' ) ):
823
					$option = str_replace( '_transient_', '', $option );
824
					delete_transient( $option );
825
					break;
826
827
				default:
828
					delete_option( $option );
829
			}
830
		}
831
	}
832
}
833
834
/**
835
 * Update Capabilities for Give_Worker User Role.
836
 *
837
 * This upgrade routine will update access rights for Give_Worker User Role.
838
 *
839
 * @since      1.8.8
840
 */
841
function give_v188_upgrades() {
842
843
	global $wp_roles;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
844
845
	// Get the role object.
846
	$give_worker = get_role( 'give_worker' );
847
848
	// A list of capabilities to add for give workers.
849
	$caps_to_add = array(
850
		'edit_posts',
851
		'edit_pages',
852
	);
853
854
	foreach ( $caps_to_add as $cap ) {
855
		// Add the capability.
856
		$give_worker->add_cap( $cap );
857
	}
858
859
}
860
861
/**
862
 * Update Post meta for minimum and maximum amount for multi level donation forms
863
 *
864
 * This upgrade routine adds post meta for give_forms CPT for multi level donation form.
865
 *
866
 * @since      1.8.9
867
 */
868
function give_v189_upgrades_levels_post_meta_callback() {
869
	/* @var Give_Updates $give_updates */
870
	$give_updates = Give_Updates::get_instance();
871
872
	// form query
873
	$donation_forms = new WP_Query( array(
874
			'paged'          => $give_updates::$step,
875
			'status'         => 'any',
876
			'order'          => 'ASC',
877
			'post_type'      => 'give_forms',
878
			'posts_per_page' => 20,
879
		)
880
	);
881
882
	if ( $donation_forms->have_posts() ) {
883
		$give_updates::$percentage = ( ( $give_updates::$step * 20 ) / $donation_forms->found_posts  ) * 100;
884
885
		while ( $donation_forms->have_posts() ) {
886
			$donation_forms->the_post();
887
			$form_id = get_the_ID();
888
889
			// Remove formatting from _give_set_price
890
			update_post_meta(
891
				$form_id,
892
				'_give_set_price',
893
				give_sanitize_amount( get_post_meta( $form_id, '_give_set_price', true ) )
894
			);
895
896
			// Remove formatting from _give_custom_amount_minimum
897
			update_post_meta(
898
				$form_id,
899
				'_give_custom_amount_minimum',
900
				give_sanitize_amount( get_post_meta( $form_id, '_give_custom_amount_minimum', true ) )
901
			);
902
903
			// Bailout.
904
			if ( 'set' === get_post_meta( $form_id, '_give_price_option', true ) ) {
905
				continue;
906
			}
907
908
			$donation_levels = get_post_meta( $form_id, '_give_donation_levels', true );
909
910
			if ( ! empty( $donation_levels ) ) {
911
912
				foreach ( $donation_levels as $index => $donation_level ) {
913
					if ( isset( $donation_level['_give_amount'] ) ) {
914
						$donation_levels[ $index ]['_give_amount'] = give_sanitize_amount( $donation_level['_give_amount'] );
915
					}
916
				}
917
918
				update_post_meta( $form_id, '_give_donation_levels', $donation_levels );
919
920
				$donation_levels_amounts = wp_list_pluck( $donation_levels, '_give_amount' );
921
922
				$min_amount = min( $donation_levels_amounts );
923
				$max_amount = max( $donation_levels_amounts );
924
925
				// Set Minimum and Maximum amount for Multi Level Donation Forms
926
				give_update_meta( $form_id, '_give_levels_minimum_amount', $min_amount ? give_sanitize_amount( $min_amount ) : 0 );
927
				give_update_meta( $form_id, '_give_levels_maximum_amount', $max_amount ? give_sanitize_amount( $max_amount ) : 0 );
928
			}
929
930
		}
931
932
		/* Restore original Post Data */
933
		wp_reset_postdata();
934
	} else {
935
		// The Update Ran.
936
		give_set_upgrade_complete( 'v189_upgrades_levels_post_meta' );
937
	}
938
939
}
940
941
942
/**
943
 * Give version 1.8.9 upgrades
944
 *
945
 * @since      1.8.9
946
 */
947
function give_v189_upgrades() {
948
	/**
949
	 * 1. Remove user license related notice show blocked ( Give_Notice will handle )
950
	 */
951
	global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
952
953
	// Delete permanent notice blocker.
954
	$wpdb->query(
955
		$wpdb->prepare(
956
			"
957
					DELETE FROM $wpdb->usermeta
958
					WHERE meta_key
959
					LIKE '%%%s%%'
960
					",
961
			'_give_hide_license_notices_permanently'
962
		)
963
	);
964
965
	// Delete short notice blocker.
966
	$wpdb->query(
967
		$wpdb->prepare(
968
			"
969
					DELETE FROM $wpdb->options
970
					WHERE option_name
971
					LIKE '%%%s%%'
972
					",
973
			'__give_hide_license_notices_shortly_'
974
		)
975
	);
976
977
}
978
979
980
/**
981
 * Give version 1.8.12 update
982
 *
983
 * Standardized amount values to six decimal
984
 *
985
 * @see        https://github.com/WordImpress/Give/issues/1849#issuecomment-315128602
986
 *
987
 * @since      1.8.12
988
 */
989
function give_v1812_update_amount_values_callback() {
990
	/* @var Give_Updates $give_updates */
991
	$give_updates = Give_Updates::get_instance();
992
993
	// form query
994
	$donation_forms = new WP_Query( array(
995
			'paged'          => $give_updates::$step,
996
			'status'         => 'any',
997
			'order'          => 'ASC',
998
			'post_type'      => array( 'give_forms', 'give_payment' ),
999
			'posts_per_page' => 20,
1000
		)
1001
	);
1002
1003
	if ( $donation_forms->have_posts() ) {
1004
		$give_updates->set_percentage( $donation_forms->found_posts );
1005
1006
		while ( $donation_forms->have_posts() ) {
1007
			$donation_forms->the_post();
1008
			global $post;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
1009
1010
			$meta = get_post_meta( $post->ID );
1011
1012
			switch ( $post->post_type ) {
1013
				case 'give_forms':
1014
					// _give_set_price
1015
					if( ! empty( $meta['_give_set_price'][0] ) ) {
1016
						update_post_meta( $post->ID, '_give_set_price', give_sanitize_amount_for_db( $meta['_give_set_price'][0] ) );
1017
					}
1018
1019
					// _give_custom_amount_minimum
1020
					if( ! empty( $meta['_give_custom_amount_minimum'][0] ) ) {
1021
						update_post_meta( $post->ID, '_give_custom_amount_minimum', give_sanitize_amount_for_db( $meta['_give_custom_amount_minimum'][0] ) );
1022
					}
1023
1024
					// _give_levels_minimum_amount
1025
					if( ! empty( $meta['_give_levels_minimum_amount'][0] ) ) {
1026
						update_post_meta( $post->ID, '_give_levels_minimum_amount', give_sanitize_amount_for_db( $meta['_give_levels_minimum_amount'][0] ) );
1027
					}
1028
1029
					// _give_levels_maximum_amount
1030
					if( ! empty( $meta['_give_levels_maximum_amount'][0] ) ) {
1031
						update_post_meta( $post->ID, '_give_levels_maximum_amount', give_sanitize_amount_for_db( $meta['_give_levels_maximum_amount'][0] ) );
1032
					}
1033
1034
					// _give_set_goal
1035
					if( ! empty( $meta['_give_set_goal'][0] ) ) {
1036
						update_post_meta( $post->ID, '_give_set_goal', give_sanitize_amount_for_db( $meta['_give_set_goal'][0] ) );
1037
					}
1038
1039
					// _give_form_earnings
1040
					if( ! empty( $meta['_give_form_earnings'][0] ) ) {
1041
						update_post_meta( $post->ID, '_give_form_earnings', give_sanitize_amount_for_db( $meta['_give_form_earnings'][0] ) );
1042
					}
1043
1044
					// _give_custom_amount_minimum
1045
					if( ! empty( $meta['_give_donation_levels'][0] ) ) {
1046
						$donation_levels = unserialize( $meta['_give_donation_levels'][0] );
1047
1048
						foreach( $donation_levels as $index => $level ) {
1049
							if( empty( $level['_give_amount'] ) ) {
1050
								continue;
1051
							}
1052
1053
							$donation_levels[$index]['_give_amount'] = give_sanitize_amount_for_db( $level['_give_amount'] );
1054
						}
1055
1056
						$meta['_give_donation_levels'] = $donation_levels;
1057
						update_post_meta( $post->ID, '_give_donation_levels', $meta['_give_donation_levels'] );
1058
					}
1059
1060
1061
					break;
1062
1063
				case 'give_payment':
1064
					// _give_payment_total
1065
					if( ! empty( $meta['_give_payment_total'][0] ) ) {
1066
						update_post_meta( $post->ID, '_give_payment_total', give_sanitize_amount_for_db( $meta['_give_payment_total'][0] ) );
1067
					}
1068
1069
					break;
1070
			}
1071
		}
1072
1073
		/* Restore original Post Data */
1074
		wp_reset_postdata();
1075
	} else {
1076
		// The Update Ran.
1077
		give_set_upgrade_complete( 'v1812_update_amount_values' );
1078
	}
1079
}
1080