Completed
Pull Request — master (#1907)
by
unknown
19:56
created

upgrade-functions.php ➔ give_show_upgrade_notices()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.8007

Importance

Changes 0
Metric Value
cc 1
eloc 22
nc 1
nop 1
dl 0
loc 38
ccs 1
cts 14
cp 0.0714
crap 1.8007
rs 8.8571
c 0
b 0
f 0
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 1
	);
118
}
119
120
add_action( 'give_register_updates', 'give_show_upgrade_notices' );
121 1
122
/**
123 1
 * Triggers all upgrade functions
124
 *
125
 * This function is usually triggered via AJAX
126
 *
127
 * @since 1.0
128
 * @return void
129
 */
130
function give_trigger_upgrades() {
131
132
	if ( ! current_user_can( 'manage_give_settings' ) ) {
133
		wp_die( esc_html__( 'You do not have permission to do Give upgrades.', 'give' ), esc_html__( 'Error', 'give' ), array(
134
			'response' => 403,
135
		) );
136
	}
137
138 1
	$give_version = get_option( 'give_version' );
139
140
	if ( ! $give_version ) {
141
		// 1.0 is the first version to use this option so we must add it.
142 1
		$give_version = '1.0';
143 1
		add_option( 'give_version', $give_version );
144
	}
145
146 1
	update_option( 'give_version', GIVE_VERSION );
147
	delete_option( 'give_doing_upgrade' );
148 1
149
	if ( DOING_AJAX ) {
150
		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...
151
	} // 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...
152
}
153
154
add_action( 'wp_ajax_give_trigger_upgrades', 'give_trigger_upgrades' );
155
156
/**
157
 * Check if the upgrade routine has been run for a specific action
158
 *
159 1
 * @since  1.0
160
 *
161 1
 * @param  string $upgrade_action The upgrade action to check completion for
162
 *
163
 * @return bool                   If the action has been added to the completed actions array
164
 */
165 1
function give_has_upgrade_completed( $upgrade_action = '' ) {
166
167
	if ( empty( $upgrade_action ) ) {
168
		return false;
169
	}
170
171
	$completed_upgrades = give_get_completed_upgrades();
172
173
	return in_array( $upgrade_action, $completed_upgrades );
174
175
}
176
177
/**
178
 * For use when doing 'stepped' upgrade routines, to see if we need to start somewhere in the middle
179
 *
180
 * @since 1.8
181
 *
182
 * @return mixed   When nothing to resume returns false, otherwise starts the upgrade where it left off
183
 */
184
function give_maybe_resume_upgrade() {
185
	$doing_upgrade = get_option( 'give_doing_upgrade', false );
186
	if ( empty( $doing_upgrade ) ) {
187
		return false;
188
	}
189
190
	return $doing_upgrade;
191
}
192
193
/**
194
 * Adds an upgrade action to the completed upgrades array
195
 *
196
 * @since  1.0
197
 *
198
 * @param  string $upgrade_action The action to add to the completed upgrades array
199
 *
200
 * @return bool                   If the function was successfully added
201
 */
202
function give_set_upgrade_complete( $upgrade_action = '' ) {
203
204
	if ( empty( $upgrade_action ) ) {
205
		return false;
206
	}
207
208
	$completed_upgrades   = give_get_completed_upgrades();
209
	$completed_upgrades[] = $upgrade_action;
210
211
	// Remove any blanks, and only show uniques.
212
	$completed_upgrades = array_unique( array_values( $completed_upgrades ) );
213
214
	/**
215
	 * Fire the action when any upgrade set to complete.
216
	 *
217
	 * @since 1.8.12
218
	 */
219
	do_action( 'give_set_upgrade_completed', $upgrade_action, $completed_upgrades );
220
221
	return update_option( 'give_completed_upgrades', $completed_upgrades );
222
}
223
224
/**
225
 * Get's the array of completed upgrade actions
226
 *
227
 * @since  1.0
228
 * @return array The array of completed upgrades
229
 */
230
function give_get_completed_upgrades() {
231
232
	return (array) get_option( 'give_completed_upgrades' );
233
234
}
235
236
/**
237
 * Upgrades the
238
 *
239
 * Standardizes the discrepancies between two metakeys `_give_payment_customer_id` and `_give_payment_donor_id`
240
 *
241
 * @since      1.3.2
242
 */
243
function give_v132_upgrade_give_payment_customer_id() {
244
	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...
245
246
	/* @var Give_Updates $give_updates */
247
	$give_updates = Give_Updates::get_instance();
248
249
	if ( ! current_user_can( 'manage_give_settings' ) ) {
250
		wp_die( esc_html__( 'You do not have permission to do Give upgrades.', 'give' ), esc_html__( 'Error', 'give' ), array(
251
			'response' => 403,
252
		) );
253
	}
254
255
	ignore_user_abort( true );
256
257
	if ( ! give_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {
258
		@set_time_limit( 0 );
259
	}
260
261
	// UPDATE DB METAKEYS.
262
	$sql   = "UPDATE $wpdb->postmeta SET meta_key = '_give_payment_customer_id' WHERE meta_key = '_give_payment_donor_id'";
263
	$query = $wpdb->query( $sql );
264
265
	$give_updates::$percentage = 100;
266
	give_set_upgrade_complete( 'upgrade_give_payment_customer_id' );
267
}
268
269
270
/**
271
 * Upgrades the Offline Status
272
 *
273
 * Reverses the issue where offline donations in "pending" status where inappropriately marked as abandoned
274
 *
275
 * @since      1.3.4
276
 */
277
function give_v134_upgrade_give_offline_status() {
278
	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...
279
280
	/* @var Give_Updates $give_updates */
281
	$give_updates = Give_Updates::get_instance();
282
283
	// Get abandoned offline payments.
284
	$select = "SELECT ID FROM $wpdb->posts p ";
285
	$join   = "LEFT JOIN $wpdb->postmeta m ON p.ID = m.post_id ";
286
	$where  = "WHERE p.post_type = 'give_payment' ";
287
	$where  .= "AND ( p.post_status = 'abandoned' )";
288
	$where  .= "AND ( m.meta_key = '_give_payment_gateway' AND m.meta_value = 'offline' )";
289
290
	$sql            = $select . $join . $where;
291
	$found_payments = $wpdb->get_col( $sql );
292
293
	foreach ( $found_payments as $payment ) {
294
295
		// Only change ones marked abandoned since our release last week because the admin may have marked some abandoned themselves.
296
		$modified_time = get_post_modified_time( 'U', false, $payment );
297
298
		// 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...
299
		if ( $modified_time >= 1450124863 ) {
300
301
			give_update_payment_status( $payment, 'pending' );
302
303
		}
304
	}
305
306
	$give_updates::$percentage = 100;
307
	give_set_upgrade_complete( 'upgrade_give_offline_status' );
308
}
309
310
311
/**
312
 * Cleanup User Roles
313
 *
314
 * This upgrade routine removes unused roles and roles with typos
315
 *
316
 * @since      1.5.2
317
 */
318
function give_v152_cleanup_users() {
319
320
	$give_version = get_option( 'give_version' );
321
322
	if ( ! $give_version ) {
323
		// 1.0 is the first version to use this option so we must add it.
324
		$give_version = '1.0';
325
	}
326
327
	$give_version = preg_replace( '/[^0-9.].*/', '', $give_version );
328
329
	// v1.5.2 Upgrades
330
	if ( version_compare( $give_version, '1.5.2', '<' ) || ! give_has_upgrade_completed( 'upgrade_give_user_caps_cleanup' ) ) {
331
332
		// Delete all caps with "ss".
333
		// Also delete all unused "campaign" roles.
334
		$delete_caps = array(
335
			'delete_give_formss',
336
			'delete_others_give_formss',
337
			'delete_private_give_formss',
338
			'delete_published_give_formss',
339
			'read_private_forms',
340
			'edit_give_formss',
341
			'edit_others_give_formss',
342
			'edit_private_give_formss',
343
			'edit_published_give_formss',
344
			'publish_give_formss',
345
			'read_private_give_formss',
346
			'assign_give_campaigns_terms',
347
			'delete_give_campaigns',
348
			'delete_give_campaigns_terms',
349
			'delete_give_campaignss',
350
			'delete_others_give_campaignss',
351
			'delete_private_give_campaignss',
352
			'delete_published_give_campaignss',
353
			'edit_give_campaigns',
354
			'edit_give_campaigns_terms',
355
			'edit_give_campaignss',
356
			'edit_others_give_campaignss',
357
			'edit_private_give_campaignss',
358
			'edit_published_give_campaignss',
359
			'manage_give_campaigns_terms',
360
			'publish_give_campaignss',
361
			'read_give_campaigns',
362
			'read_private_give_campaignss',
363
			'view_give_campaigns_stats',
364
			'delete_give_paymentss',
365
			'delete_others_give_paymentss',
366
			'delete_private_give_paymentss',
367
			'delete_published_give_paymentss',
368
			'edit_give_paymentss',
369
			'edit_others_give_paymentss',
370
			'edit_private_give_paymentss',
371
			'edit_published_give_paymentss',
372
			'publish_give_paymentss',
373
			'read_private_give_paymentss',
374
		);
375
376
		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...
377
		foreach ( $delete_caps as $cap ) {
378
			foreach ( array_keys( $wp_roles->roles ) as $role ) {
379
				$wp_roles->remove_cap( $role, $cap );
380
			}
381
		}
382
383
		// Create Give plugin roles.
384
		$roles = new Give_Roles();
385
		$roles->add_roles();
386
		$roles->add_caps();
387
388
		// The Update Ran.
389
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
390
		give_set_upgrade_complete( 'upgrade_give_user_caps_cleanup' );
391
		delete_option( 'give_doing_upgrade' );
392
393
	}// 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...
394
395
}
396
397
add_action( 'admin_init', 'give_v152_cleanup_users' );
398
399
/**
400
 * 1.6 Upgrade routine to create the customer meta table.
401
 *
402
 * @since  1.6
403
 * @return void
404
 */
405
function give_v16_upgrades() {
406
	// Create the donor databases.
407
	$donors_db = new Give_DB_Donors();
408
	$donors_db->create_table();
409
	$donor_meta = new Give_DB_Donor_Meta();
410
	$donor_meta->create_table();
411
}
412
413
/**
414
 * 1.7 Upgrades.
415
 *
416
 * a. Update license api data for plugin addons.
417
 * b. Cleanup user roles.
418
 *
419
 * @since  1.7
420
 * @return void
421
 */
422
function give_v17_upgrades() {
423
	// Upgrade license data.
424
	give_v17_upgrade_addon_license_data();
425
	give_v17_cleanup_roles();
426
}
427
428
/**
429
 * Upgrade license data
430
 *
431
 * @since 1.7
432
 */
433
function give_v17_upgrade_addon_license_data() {
434
	$give_options = give_get_settings();
435
436
	$api_url = 'https://givewp.com/give-sl-api/';
437
438
	// Get addons license key.
439
	$addons = array();
440
	foreach ( $give_options as $key => $value ) {
441
		if ( false !== strpos( $key, '_license_key' ) ) {
442
			$addons[ $key ] = $value;
443
		}
444
	}
445
446
	// Bailout: We do not have any addon license data to upgrade.
447
	if ( empty( $addons ) ) {
448
		return false;
449
	}
450
451
	foreach ( $addons as $key => $addon_license ) {
452
453
		// Get addon shortname.
454
		$shortname = str_replace( '_license_key', '', $key );
455
456
		// Addon license option name.
457
		$addon_license_option_name = $shortname . '_license_active';
458
459
		// bailout if license is empty.
460
		if ( empty( $addon_license ) ) {
461
			delete_option( $addon_license_option_name );
462
			continue;
463
		}
464
465
		// Get addon name.
466
		$addon_name       = array();
467
		$addon_name_parts = explode( '_', str_replace( 'give_', '', $shortname ) );
468
		foreach ( $addon_name_parts as $name_part ) {
469
470
			// Fix addon name
471
			switch ( $name_part ) {
472
				case 'authorizenet' :
473
					$name_part = 'authorize.net';
474
					break;
475
			}
476
477
			$addon_name[] = ucfirst( $name_part );
478
		}
479
480
		$addon_name = implode( ' ', $addon_name );
481
482
		// Data to send to the API
483
		$api_params = array(
484
			'edd_action' => 'activate_license', // never change from "edd_" to "give_"!
485
			'license'    => $addon_license,
486
			'item_name'  => urlencode( $addon_name ),
487
			'url'        => home_url(),
488
		);
489
490
		// Call the API.
491
		$response = wp_remote_post(
492
			$api_url,
493
			array(
494
				'timeout'   => 15,
495
				'sslverify' => false,
496
				'body'      => $api_params,
497
			)
498
		);
499
500
		// Make sure there are no errors.
501
		if ( is_wp_error( $response ) ) {
502
			delete_option( $addon_license_option_name );
503
			continue;
504
		}
505
506
		// Tell WordPress to look for updates.
507
		set_site_transient( 'update_plugins', null );
508
509
		// Decode license data.
510
		$license_data = json_decode( wp_remote_retrieve_body( $response ) );
511
		update_option( $addon_license_option_name, $license_data );
512
	}// 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...
513
}
514
515
516
/**
517
 * Cleanup User Roles.
518
 *
519
 * This upgrade routine removes unused roles and roles with typos.
520
 *
521
 * @since      1.7
522
 */
523
function give_v17_cleanup_roles() {
524
525
	// Delete all caps with "_give_forms_" and "_give_payments_"
526
	// These roles have no usage; the proper is singular.
527
	$delete_caps = array(
528
		'view_give_forms_stats',
529
		'delete_give_forms_terms',
530
		'assign_give_forms_terms',
531
		'edit_give_forms_terms',
532
		'manage_give_forms_terms',
533
		'view_give_payments_stats',
534
		'manage_give_payments_terms',
535
		'edit_give_payments_terms',
536
		'assign_give_payments_terms',
537
		'delete_give_payments_terms',
538
	);
539
540
	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...
541
	foreach ( $delete_caps as $cap ) {
542
		foreach ( array_keys( $wp_roles->roles ) as $role ) {
543
			$wp_roles->remove_cap( $role, $cap );
544
		}
545
	}
546
547
	// Set roles again.
548
	$roles = new Give_Roles();
549
	$roles->add_roles();
550
	$roles->add_caps();
551
552
}
553
554
/**
555
 * 1.8 Upgrades.
556
 *
557
 * a. Upgrade checkbox settings to radio button settings.
558
 * a. Update form meta for new metabox settings.
559
 *
560
 * @since  1.8
561
 * @return void
562
 */
563
function give_v18_upgrades() {
564
	// Upgrade checkbox settings to radio button settings.
565
	give_v18_upgrades_core_setting();
566
}
567
568
/**
569
 * Upgrade core settings.
570
 *
571
 * @since  1.8
572
 * @return void
573
 */
574
function give_v18_upgrades_core_setting() {
575
	// Core settings which changes from checkbox to radio.
576
	$core_setting_names = array_merge(
577
		array_keys( give_v18_renamed_core_settings() ),
578
		array(
579
			'uninstall_on_delete',
580
			'scripts_footer',
581
			'test_mode',
582
			'email_access',
583
			'terms',
584
			'give_offline_donation_enable_billing_fields',
585
		)
586
	);
587
588
	// Bailout: If not any setting define.
589
	if ( $give_settings = get_option( 'give_settings' ) ) {
590
591
		$setting_changed = false;
592
593
		// Loop: check each setting field.
594
		foreach ( $core_setting_names as $setting_name ) {
595
			// New setting name.
596
			$new_setting_name = preg_replace( '/^(enable_|disable_)/', '', $setting_name );
597
598
			// Continue: If setting already set.
599
			if (
600
				array_key_exists( $new_setting_name, $give_settings )
601
				&& in_array( $give_settings[ $new_setting_name ], array( 'enabled', 'disabled' ) )
602
			) {
603
				continue;
604
			}
605
606
			// Set checkbox value to radio value.
607
			$give_settings[ $setting_name ] = ( ! empty( $give_settings[ $setting_name ] ) && 'on' === $give_settings[ $setting_name ] ? 'enabled' : 'disabled' );
608
609
			// @see https://github.com/WordImpress/Give/issues/1063
610
			if ( false !== strpos( $setting_name, 'disable_' ) ) {
611
612
				$give_settings[ $new_setting_name ] = ( give_is_setting_enabled( $give_settings[ $setting_name ] ) ? 'disabled' : 'enabled' );
613
			} elseif ( false !== strpos( $setting_name, 'enable_' ) ) {
614
615
				$give_settings[ $new_setting_name ] = ( give_is_setting_enabled( $give_settings[ $setting_name ] ) ? 'enabled' : 'disabled' );
616
			}
617
618
			// Tell bot to update core setting to db.
619
			if ( ! $setting_changed ) {
620
				$setting_changed = true;
621
			}
622
		}
623
624
		// Update setting only if they changed.
625
		if ( $setting_changed ) {
626
			update_option( 'give_settings', $give_settings );
627
		}
628
	}// 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...
629
630
	give_set_upgrade_complete( 'v18_upgrades_core_setting' );
631
}
632
633
/**
634
 * Upgrade form metadata for new metabox settings.
635
 *
636
 * @since  1.8
637
 * @return void
638
 */
639
function give_v18_upgrades_form_metadata() {
640
	/* @var Give_Updates $give_updates */
641
	$give_updates = Give_Updates::get_instance();
642
643
	// form query
644
	$forms = new WP_Query( array(
645
			'paged'          => $give_updates::$step,
646
			'status'         => 'any',
647
			'order'          => 'ASC',
648
			'post_type'      => 'give_forms',
649
			'posts_per_page' => 20,
650
		)
651
	);
652
653
	if ( $forms->have_posts() ) {
654
		$give_updates::$percentage = ( ( $give_updates::$step * 20 ) / $forms->found_posts  ) * 100;
655
656
		error_log( print_r( "{$give_updates::$step}-{$forms->found_posts}-{$give_updates::$percentage}", true ) . "\n", 3, WP_CONTENT_DIR . '/debug_new.log' );
657
658
		while ( $forms->have_posts() ) {
659
			$forms->the_post();
660
661
			// Form content.
662
			// Note in version 1.8 display content setting split into display content and content placement setting.
663
			// You can delete _give_content_option in future
664
			$show_content = give_get_meta( get_the_ID(), '_give_content_option', true );
665
			if ( $show_content && ! give_get_meta( get_the_ID(), '_give_display_content', true ) ) {
666
				$field_value = ( 'none' !== $show_content ? 'enabled' : 'disabled' );
667
				give_update_meta( get_the_ID(), '_give_display_content', $field_value );
668
669
				$field_value = ( 'none' !== $show_content ? $show_content : 'give_pre_form' );
670
				give_update_meta( get_the_ID(), '_give_content_placement', $field_value );
671
			}
672
673
			// "Disable" Guest Donation. Checkbox
674
			// See: https://github.com/WordImpress/Give/issues/1470
675
			$guest_donation        = give_get_meta( get_the_ID(), '_give_logged_in_only', true );
676
			$guest_donation_newval = ( in_array( $guest_donation, array( 'yes', 'on' ) ) ? 'disabled' : 'enabled' );
677
			give_update_meta( get_the_ID(), '_give_logged_in_only', $guest_donation_newval );
678
679
			// Offline Donations
680
			// See: https://github.com/WordImpress/Give/issues/1579
681
			$offline_donation = give_get_meta( get_the_ID(), '_give_customize_offline_donations', true );
682
			if ( 'no' === $offline_donation ) {
683
				$offline_donation_newval = 'global';
684
			} elseif ( 'yes' === $offline_donation ) {
685
				$offline_donation_newval = 'enabled';
686
			} else {
687
				$offline_donation_newval = 'disabled';
688
			}
689
			give_update_meta( get_the_ID(), '_give_customize_offline_donations', $offline_donation_newval );
690
691
			// Convert yes/no setting field to enabled/disabled.
692
			$form_radio_settings = array(
693
				// Custom Amount.
694
				'_give_custom_amount',
695
696
				// Donation Gaol.
697
				'_give_goal_option',
698
699
				// Close Form.
700
				'_give_close_form_when_goal_achieved',
701
702
				// Term & conditions.
703
				'_give_terms_option',
704
705
				// Billing fields.
706
				'_give_offline_donation_enable_billing_fields_single',
707
			);
708
709
			foreach ( $form_radio_settings as $meta_key ) {
710
				// Get value.
711
				$field_value = give_get_meta( get_the_ID(), $meta_key, true );
712
713
				// Convert meta value only if it is in yes/no/none.
714
				if ( in_array( $field_value, array( 'yes', 'on', 'no', 'none' ) ) ) {
715
716
					$field_value = ( in_array( $field_value, array( 'yes', 'on' ) ) ? 'enabled' : 'disabled' );
717
					give_update_meta( get_the_ID(), $meta_key, $field_value );
718
				}
719
			}
720
		}// 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...
721
722
		wp_reset_postdata();
723
724
	} else {
725
		// No more forms found, finish up.
726
		give_set_upgrade_complete( 'v18_upgrades_form_metadata' );
727
	}
728
}
729
730
731
/**
732
 * Get list of core setting renamed in version 1.8.
733
 *
734
 * @since  1.8
735
 * @return array
736
 */
737
function give_v18_renamed_core_settings() {
738
	return array(
739
		'disable_paypal_verification' => 'paypal_verification',
740
		'disable_css'                 => 'css',
741
		'disable_welcome'             => 'welcome',
742
		'disable_forms_singular'      => 'forms_singular',
743
		'disable_forms_archives'      => 'forms_archives',
744
		'disable_forms_excerpt'       => 'forms_excerpt',
745
		'disable_form_featured_img'   => 'form_featured_img',
746
		'disable_form_sidebar'        => 'form_sidebar',
747
		'disable_admin_notices'       => 'admin_notices',
748
		'disable_the_content_filter'  => 'the_content_filter',
749
		'enable_floatlabels'          => 'floatlabels',
750
		'enable_categories'           => 'categories',
751
		'enable_tags'                 => 'tags',
752
	);
753
}
754
755
756
/**
757
 * Upgrade core settings.
758
 *
759
 * @since  1.8.7
760
 * @return void
761
 */
762
function give_v187_upgrades() {
763
	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...
764
765
	/**
766
	 * Upgrade 1: Remove stat and cache transients.
767
	 */
768
	$cached_options = $wpdb->get_col(
769
		$wpdb->prepare(
770
			"SELECT * FROM {$wpdb->options} where (option_name LIKE '%%%s%%' OR option_name LIKE '%%%s%%')",
771
			array(
772
				'_transient_give_stats_',
773
				'give_cache',
774
				'_transient_give_add_ons_feed',
775
				'_transient__give_ajax_works' .
776
				'_transient_give_total_api_keys',
777
				'_transient_give_i18n_give_promo_hide',
778
				'_transient_give_contributors',
779
				'_transient_give_estimated_monthly_stats',
780
				'_transient_give_earnings_total',
781
				'_transient_give_i18n_give_',
782
				'_transient__give_installed',
783
				'_transient__give_activation_redirect',
784
				'_transient__give_hide_license_notices_shortly_',
785
				'give_income_total',
786
			)
787
		),
788
		1
789
	);
790
791
	// User related transients.
792
	$user_apikey_options = $wpdb->get_results(
793
		$wpdb->prepare(
794
			"SELECT user_id, meta_key
795
			FROM $wpdb->usermeta
796
			WHERE meta_value=%s",
797
			'give_user_public_key'
798
		),
799
		ARRAY_A
800
	);
801
802
	if ( ! empty( $user_apikey_options ) ) {
803
		foreach ( $user_apikey_options as $user ) {
804
			$cached_options[] = '_transient_' . md5( 'give_api_user_' . $user['meta_key'] );
805
			$cached_options[] = '_transient_' . md5( 'give_api_user_public_key' . $user['user_id'] );
806
			$cached_options[] = '_transient_' . md5( 'give_api_user_secret_key' . $user['user_id'] );
807
		}
808
	}
809
810
	if ( ! empty( $cached_options ) ) {
811
		foreach ( $cached_options as $option ) {
812
			switch ( true ) {
813
				case ( false !== strpos( $option, 'transient' ) ):
814
					$option = str_replace( '_transient_', '', $option );
815
					delete_transient( $option );
816
					break;
817
818
				default:
819
					delete_option( $option );
820
			}
821
		}
822
	}
823
}
824
825
/**
826
 * Update Capabilities for Give_Worker User Role.
827
 *
828
 * This upgrade routine will update access rights for Give_Worker User Role.
829
 *
830
 * @since      1.8.8
831
 */
832
function give_v188_upgrades() {
833
834
	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...
835
836
	// Get the role object.
837
	$give_worker = get_role( 'give_worker' );
838
839
	// A list of capabilities to add for give workers.
840
	$caps_to_add = array(
841
		'edit_posts',
842
		'edit_pages',
843
	);
844
845
	foreach ( $caps_to_add as $cap ) {
846
		// Add the capability.
847
		$give_worker->add_cap( $cap );
848
	}
849
850
}
851
852
/**
853
 * Update Post meta for minimum and maximum amount for multi level donation forms
854
 *
855
 * This upgrade routine adds post meta for give_forms CPT for multi level donation form.
856
 *
857
 * @since      1.8.9
858
 */
859
function give_v189_upgrades_levels_post_meta_callback() {
860
	/* @var Give_Updates $give_updates */
861
	$give_updates = Give_Updates::get_instance();
862
863
	// form query
864
	$donation_forms = new WP_Query( array(
865
			'paged'          => $give_updates::$step,
866
			'status'         => 'any',
867
			'order'          => 'ASC',
868
			'post_type'      => 'give_forms',
869
			'posts_per_page' => 20,
870
		)
871
	);
872
873
	if ( $donation_forms->have_posts() ) {
874
		$give_updates::$percentage = ( ( $give_updates::$step * 20 ) / $donation_forms->found_posts  ) * 100;
875
876
		while ( $donation_forms->have_posts() ) {
877
			$donation_forms->the_post();
878
			$form_id = get_the_ID();
879
880
			// Remove formatting from _give_set_price
881
			update_post_meta(
882
				$form_id,
883
				'_give_set_price',
884
				give_sanitize_amount( get_post_meta( $form_id, '_give_set_price', true ) )
885
			);
886
887
			// Remove formatting from _give_custom_amount_minimum
888
			update_post_meta(
889
				$form_id,
890
				'_give_custom_amount_minimum',
891
				give_sanitize_amount( get_post_meta( $form_id, '_give_custom_amount_minimum', true ) )
892
			);
893
894
			// Bailout.
895
			if ( 'set' === get_post_meta( $form_id, '_give_price_option', true ) ) {
896
				continue;
897
			}
898
899
			$donation_levels = get_post_meta( $form_id, '_give_donation_levels', true );
900
901
			if ( ! empty( $donation_levels ) ) {
902
903
				foreach ( $donation_levels as $index => $donation_level ) {
904
					if ( isset( $donation_level['_give_amount'] ) ) {
905
						$donation_levels[ $index ]['_give_amount'] = give_sanitize_amount( $donation_level['_give_amount'] );
906
					}
907
				}
908
909
				update_post_meta( $form_id, '_give_donation_levels', $donation_levels );
910
911
				$donation_levels_amounts = wp_list_pluck( $donation_levels, '_give_amount' );
912
913
				$min_amount = min( $donation_levels_amounts );
914
				$max_amount = max( $donation_levels_amounts );
915
916
				// Set Minimum and Maximum amount for Multi Level Donation Forms
917
				give_update_meta( $form_id, '_give_levels_minimum_amount', $min_amount ? give_sanitize_amount( $min_amount ) : 0 );
918
				give_update_meta( $form_id, '_give_levels_maximum_amount', $max_amount ? give_sanitize_amount( $max_amount ) : 0 );
919
			}
920
921
		}
922
923
		/* Restore original Post Data */
924
		wp_reset_postdata();
925
	} else {
926
		// The Update Ran.
927
		give_set_upgrade_complete( 'v189_upgrades_levels_post_meta' );
928
	}
929
930
}
931
932
933
/**
934
 * Give version 1.8.9 upgrades
935
 *
936
 * @since      1.8.9
937
 */
938
function give_v189_upgrades() {
939
	/**
940
	 * 1. Remove user license related notice show blocked ( Give_Notice will handle )
941
	 */
942
	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...
943
944
	// Delete permanent notice blocker.
945
	$wpdb->query(
946
		$wpdb->prepare(
947
			"
948
					DELETE FROM $wpdb->usermeta
949
					WHERE meta_key
950
					LIKE '%%%s%%'
951
					",
952
			'_give_hide_license_notices_permanently'
953
		)
954
	);
955
956
	// Delete short notice blocker.
957
	$wpdb->query(
958
		$wpdb->prepare(
959
			"
960
					DELETE FROM $wpdb->options
961
					WHERE option_name
962
					LIKE '%%%s%%'
963
					",
964
			'__give_hide_license_notices_shortly_'
965
		)
966
	);
967
968
}
969