Completed
Push — release/2.0 ( 4d845f...d9fa8a )
by Ravinder
18:24
created

upgrade-functions.php ➔ give_do_automatic_upgrades()   D

Complexity

Conditions 9
Paths 28

Size

Total Lines 41
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

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

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
1124
1125
				foreach ( $new_setting as $setting ) {
0 ignored issues
show
Bug introduced by
The expression $new_setting of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
1126
					// bailout if setting already exist.
1127
					if ( array_key_exists( $setting, $all_setting ) ) {
1128
						continue;
1129
					}
1130
1131
					give_update_option( $setting, $recipients );
1132
				}
1133
				break;
1134
1135
			default:
1136
				give_update_option( $new_setting, give_get_option( $old_setting ) );
1137
				give_delete_option( $old_setting );
1138
		}
1139
	}
1140
}
1141
1142
add_action( 'give_v189_upgrades_levels_post_meta', 'give_v189_upgrades_levels_post_meta_callback' );
1143
1144
1145
/**
1146
 * Upgrade form metadata for new metabox settings.
1147
 *
1148
 * @since  1.8
1149
 * @return void
1150
 */
1151
function give_v20_upgrades_form_metadata() {
1152
	if ( ! current_user_can( 'manage_give_settings' ) ) {
1153
		wp_die( esc_html__( 'You do not have permission to do Give upgrades.', 'give' ), esc_html__( 'Error', 'give' ), array(
1154
			'response' => 403,
1155
		) );
1156
	}
1157
1158
	ignore_user_abort( true );
1159
1160
	if ( ! give_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {
1161
		@set_time_limit( 0 );
1162
	}
1163
1164
	$step = isset( $_GET['step'] ) ? absint( $_GET['step'] ) : 1;
1165
1166
	// form query
1167
	$forms = new WP_Query( array(
1168
			'paged'          => $step,
1169
			'status'         => 'any',
1170
			'order'          => 'ASC',
1171
			'post_type'      => 'give_forms',
1172
			'posts_per_page' => 20,
1173
		)
1174
	);
1175
1176
	if ( $forms->have_posts() ) {
1177
		while ( $forms->have_posts() ) {
1178
			$forms->the_post();
1179
1180
			// Update offline instruction email notification status.
1181
			$offline_instruction_notification_status = get_post_meta( get_the_ID(), '_give_customize_offline_donations', true );
1182
			$offline_instruction_notification_status = give_is_setting_enabled( $offline_instruction_notification_status, array( 'enabled', 'global' ) )
1183
				? $offline_instruction_notification_status
1184
				: 'global';
1185
			update_post_meta( get_the_ID(), '_give_offline-donation-instruction_notification', $offline_instruction_notification_status );
1186
1187
			// Update offline instruction email message.
1188
			update_post_meta(
1189
				get_the_ID(),
1190
				'_give_offline-donation-instruction_email_message',
1191
				get_post_meta(
1192
					get_the_ID(),
1193
					// @todo: Delete this option later ( version > 2.0 ).
1194
					'_give_offline_donation_email',
1195
					true
1196
				)
1197
			);
1198
1199
			// Update offline instruction email subject.
1200
			update_post_meta(
1201
				get_the_ID(),
1202
				'_give_offline-donation-instruction_email_subject',
1203
				get_post_meta(
1204
					get_the_ID(),
1205
					// @todo: Delete this option later ( version > 2.0 ).
1206
					'_give_offline_donation_subject',
1207
					true
1208
				)
1209
			);
1210
1211
1212
		}// 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...
1213
1214
		wp_reset_postdata();
1215
1216
		// Forms found so upgrade them
1217
		$step ++;
1218
		$redirect = add_query_arg( array(
1219
			'page'         => 'give-upgrades',
1220
			'give-upgrade' => 'give_v20_upgrades_form_metadata',
1221
			'step'         => $step,
1222
		), admin_url( 'index.php' ) );
1223
		wp_redirect( $redirect );
1224
		exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_v20_upgrades_form_metadata() 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...
1225
1226
	} else {
1227
		// No more forms found, finish up.
1228
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
1229
		delete_option( 'give_doing_upgrade' );
1230
		give_set_upgrade_complete( 'v20_upgrades_form_metadata' );
1231
1232
		wp_redirect( admin_url() );
1233
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_v20_upgrades_form_metadata() 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...
1234
	}
1235
}
1236
1237
add_action( 'give_give_v20_upgrades_form_metadata', 'give_v20_upgrades_form_metadata' );
1238