Completed
Push — issues/1132 ( fe09c7...79489e )
by Ravinder
19:54
created

upgrade-functions.php ➔ give_show_upgrade_notices()   C

Complexity

Conditions 11
Paths 5

Size

Total Lines 117
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 57
nc 5
nop 0
dl 0
loc 117
rs 5.2653
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

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

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

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

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

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