Completed
Push — master ( 0af146...0e0d02 )
by Devin
13s
created

upgrade-functions.php ➔ give_v18_upgrades_form_metadata()   F

Complexity

Conditions 15
Paths 336

Size

Total Lines 105
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 240

Importance

Changes 0
Metric Value
cc 15
eloc 53
nc 336
nop 0
dl 0
loc 105
ccs 0
cts 0
cp 0
crap 240
rs 3.7313
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
50
	if ( $did_upgrade ) {
51
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
52
	}
53
}
54
55
add_action( 'admin_init', 'give_do_automatic_upgrades' );
56
add_action( 'give_upgrades', 'give_do_automatic_upgrades' );
57
58
/**
59
 * Display Upgrade Notices
60
 *
61
 * @since 1.0
62
 * @return void
63
 */
64
function give_show_upgrade_notices() {
65
	// Don't show notices on the upgrades page.
66
	if ( isset( $_GET['page'] ) && $_GET['page'] == 'give-upgrades' ) {
67
		return;
68
	}
69
70
	$give_version = get_option( 'give_version' );
71
72
	if ( ! $give_version ) {
73
		// 1.0 is the first version to use this option so we must add it.
74
		$give_version = '1.0';
75
	}
76
77
	$give_version = preg_replace( '/[^0-9.].*/', '', $give_version );
78
79
	/*
80
	 *  NOTICE:
81
	 *
82
	 *  When adding new upgrade notices, please be sure to put the action into the upgrades array during install:
83
	 *  /includes/install.php @ Appox Line 156
84
	 *
85
	 */
86
87
	// v1.3.2 Upgrades
88
	if ( version_compare( $give_version, '1.3.2', '<' ) || ! give_has_upgrade_completed( 'upgrade_give_payment_customer_id' ) ) {
89
		printf(
90
			/* translators: %s: upgrade URL */
91
			'<div class="updated"><p>' . __( 'Give needs to upgrade the donor database, click <a href="%s">here</a> to start the upgrade.', 'give' ) . '</p></div>',
92
			esc_url( admin_url( 'index.php?page=give-upgrades&give-upgrade=upgrade_give_payment_customer_id' ) )
93
		);
94
	}
95
96
	// v1.3.4 Upgrades //ensure the user has gone through 1.3.4.
97
	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' ) ) ) {
98
		printf(
99
			/* translators: %s: upgrade URL */
100
			'<div class="updated"><p>' . __( 'Give needs to upgrade the donations database, click <a href="%s">here</a> to start the upgrade.', 'give' ) . '</p></div>',
101
			esc_url( admin_url( 'index.php?page=give-upgrades&give-upgrade=upgrade_give_offline_status' ) )
102
		);
103
	}
104
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( $resume_upgrade, admin_url( 'index.php' ) );
109
		echo Give_Notices::notice_html(
110
			sprintf(
111
				__( 'Give needs to complete a database upgrade that was previously started, click <a href="%s">here</a> to resume the upgrade.', 'give' ),
112
				esc_url( $resume_url )
113
			)
114
		);
115
116
		return;
117 1
	}
118
119
	// v1.8 form metadata upgrades.
120
	if ( version_compare( $give_version, '1.8', '<' ) || ! give_has_upgrade_completed( 'v18_upgrades_form_metadata' ) ) {
121 1
		echo Give_Notices::notice_html(
122
			sprintf(
123 1
				esc_html__( 'Give needs to upgrade the form database, click %1$shere%2$s to start the upgrade.', 'give' ),
124
				'<a class="give-upgrade-link" href="' . esc_url( admin_url( 'index.php?page=give-upgrades&give-upgrade=give_v18_upgrades_form_metadata' ) ) . '">',
125
				'</a>'
126
			)
127
		);
128
	}
129
130
	// End 'Stepped' upgrade process notices.
131
	?>
132
	<script>
133
		jQuery(document).ready(function($){
134
			var $upgrdae_links = $('.give-upgrade-link');
135
			if( $upgrdae_links.length ) {
136
				$upgrdae_links.on( 'click', function(e){
137
					e.preventDefault();
138 1
139
					if( ! window.confirm( give_vars.upgrade_confirmation ) ) {
140
						return;
141
					}
142 1
143 1
					// Redirect to upgrdae link.
144
					window.location.assign( $(this).attr('href') );
145
				});
146 1
			}
147
		});
148 1
	</script>
149
	<?php
150
}
151
152
add_action( 'admin_notices', 'give_show_upgrade_notices' );
153
154
/**
155
 * Triggers all upgrade functions
156
 *
157
 * This function is usually triggered via AJAX
158
 *
159 1
 * @since 1.0
160
 * @return void
161 1
 */
162
function give_trigger_upgrades() {
163
164
	if ( ! current_user_can( 'manage_give_settings' ) ) {
165 1
		wp_die( esc_html__( 'You do not have permission to do Give upgrades.', 'give' ), esc_html__( 'Error', 'give' ), array(
166
			'response' => 403,
167
		) );
168
	}
169
170
	$give_version = get_option( 'give_version' );
171
172
	if ( ! $give_version ) {
173
		// 1.0 is the first version to use this option so we must add it.
174
		$give_version = '1.0';
175
		add_option( 'give_version', $give_version );
176
	}
177
178
	update_option( 'give_version', GIVE_VERSION );
179
	delete_option( 'give_doing_upgrade' );
180
181
	if ( DOING_AJAX ) {
182
		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...
183
	} // 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...
184
}
185
186
add_action( 'wp_ajax_give_trigger_upgrades', 'give_trigger_upgrades' );
187
188
/**
189
 * Check if the upgrade routine has been run for a specific action
190
 *
191
 * @since  1.0
192
 *
193
 * @param  string $upgrade_action The upgrade action to check completion for
194
 *
195
 * @return bool                   If the action has been added to the completed actions array
196
 */
197
function give_has_upgrade_completed( $upgrade_action = '' ) {
198
199
	if ( empty( $upgrade_action ) ) {
200
		return false;
201
	}
202
203
	$completed_upgrades = give_get_completed_upgrades();
204
205
	return in_array( $upgrade_action, $completed_upgrades );
206
207
}
208
209
/**
210
 * For use when doing 'stepped' upgrade routines, to see if we need to start somewhere in the middle
211
 *
212
 * @since 1.8
213
 *
214
 * @return mixed   When nothing to resume returns false, otherwise starts the upgrade where it left off
215
 */
216
function give_maybe_resume_upgrade() {
217
	$doing_upgrade = get_option( 'give_doing_upgrade', false );
218
	if ( empty( $doing_upgrade ) ) {
219
		return false;
220
	}
221
222
	return $doing_upgrade;
223
}
224
225
/**
226
 * Adds an upgrade action to the completed upgrades array
227
 *
228
 * @since  1.0
229
 *
230
 * @param  string $upgrade_action The action to add to the completed upgrades array
231
 *
232
 * @return bool                   If the function was successfully added
233
 */
234
function give_set_upgrade_complete( $upgrade_action = '' ) {
235
236
	if ( empty( $upgrade_action ) ) {
237
		return false;
238
	}
239
240
	$completed_upgrades   = give_get_completed_upgrades();
241
	$completed_upgrades[] = $upgrade_action;
242
243
	// Remove any blanks, and only show uniques.
244
	$completed_upgrades = array_unique( array_values( $completed_upgrades ) );
245
246
	return update_option( 'give_completed_upgrades', $completed_upgrades );
247
}
248
249
/**
250
 * Get's the array of completed upgrade actions
251
 *
252
 * @since  1.0
253
 * @return array The array of completed upgrades
254
 */
255
function give_get_completed_upgrades() {
256
257
	$completed_upgrades = get_option( 'give_completed_upgrades' );
258
259
	if ( false === $completed_upgrades ) {
260
		$completed_upgrades = array();
261
	}
262
263
	return $completed_upgrades;
264
265
}
266
267
/**
268
 * Upgrades the
269
 *
270
 * Standardizes the discrepancies between two metakeys `_give_payment_customer_id` and `_give_payment_donor_id`
271
 *
272
 * @since      1.3.2
273
 */
274
function give_v132_upgrade_give_payment_customer_id() {
275
	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...
276
	if ( ! current_user_can( 'manage_give_settings' ) ) {
277
		wp_die( esc_html__( 'You do not have permission to do Give upgrades.', 'give' ), esc_html__( 'Error', 'give' ), array(
278
			'response' => 403,
279
		) );
280
	}
281
282
	ignore_user_abort( true );
283
284
	if ( ! give_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {
285
		@set_time_limit( 0 );
286
	}
287
288
	// UPDATE DB METAKEYS.
289
	$sql   = "UPDATE $wpdb->postmeta SET meta_key = '_give_payment_customer_id' WHERE meta_key = '_give_payment_donor_id'";
290
	$query = $wpdb->query( $sql );
291
292
	update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
293
	give_set_upgrade_complete( 'upgrade_give_payment_customer_id' );
294
	delete_option( 'give_doing_upgrade' );
295
	wp_redirect( admin_url() );
296
	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...
297
298
}
299
300
add_action( 'give_upgrade_give_payment_customer_id', 'give_v132_upgrade_give_payment_customer_id' );
301
302
/**
303
 * Upgrades the Offline Status
304
 *
305
 * Reverses the issue where offline donations in "pending" status where inappropriately marked as abandoned
306
 *
307
 * @since      1.3.4
308
 */
309
function give_v134_upgrade_give_offline_status() {
310
311
	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...
312
313
	if ( ! current_user_can( 'manage_give_settings' ) ) {
314
		wp_die( esc_html__( 'You do not have permission to do Give upgrades.', 'give' ), esc_html__( 'Error', 'give' ), array(
315
			'response' => 403,
316
		) );
317
	}
318
319
	ignore_user_abort( true );
320
321
	if ( ! give_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {
322
		@set_time_limit( 0 );
323
	}
324
325
	// Get abandoned offline payments.
326
	$select = "SELECT ID FROM $wpdb->posts p ";
327
	$join   = "LEFT JOIN $wpdb->postmeta m ON p.ID = m.post_id ";
328
	$where  = "WHERE p.post_type = 'give_payment' ";
329
	$where .= "AND ( p.post_status = 'abandoned' )";
330
	$where .= "AND ( m.meta_key = '_give_payment_gateway' AND m.meta_value = 'offline' )";
331
332
	$sql            = $select . $join . $where;
333
	$found_payments = $wpdb->get_col( $sql );
334
335
	foreach ( $found_payments as $payment ) {
336
337
		// Only change ones marked abandoned since our release last week because the admin may have marked some abandoned themselves.
338
		$modified_time = get_post_modified_time( 'U', false, $payment );
339
340
		// 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...
341
		if ( $modified_time >= 1450124863 ) {
342
343
			give_update_payment_status( $payment, 'pending' );
344
345
		}
346
	}
347
348
	update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
349
	give_set_upgrade_complete( 'upgrade_give_offline_status' );
350
	delete_option( 'give_doing_upgrade' );
351
	wp_redirect( admin_url() );
352
	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...
353
354
}
355
356
add_action( 'give_upgrade_give_offline_status', 'give_v134_upgrade_give_offline_status' );
357
358
/**
359
 * Cleanup User Roles
360
 *
361
 * This upgrade routine removes unused roles and roles with typos
362
 *
363
 * @since      1.5.2
364
 */
365
function give_v152_cleanup_users() {
366
367
	$give_version = get_option( 'give_version' );
368
369
	if ( ! $give_version ) {
370
		// 1.0 is the first version to use this option so we must add it.
371
		$give_version = '1.0';
372
	}
373
374
	$give_version = preg_replace( '/[^0-9.].*/', '', $give_version );
375
376
	// v1.5.2 Upgrades
377
	if ( version_compare( $give_version, '1.5.2', '<' ) || ! give_has_upgrade_completed( 'upgrade_give_user_caps_cleanup' ) ) {
378
379
		// Delete all caps with "ss".
380
		// Also delete all unused "campaign" roles.
381
		$delete_caps = array(
382
			'delete_give_formss',
383
			'delete_others_give_formss',
384
			'delete_private_give_formss',
385
			'delete_published_give_formss',
386
			'read_private_forms',
387
			'edit_give_formss',
388
			'edit_others_give_formss',
389
			'edit_private_give_formss',
390
			'edit_published_give_formss',
391
			'publish_give_formss',
392
			'read_private_give_formss',
393
			'assign_give_campaigns_terms',
394
			'delete_give_campaigns',
395
			'delete_give_campaigns_terms',
396
			'delete_give_campaignss',
397
			'delete_others_give_campaignss',
398
			'delete_private_give_campaignss',
399
			'delete_published_give_campaignss',
400
			'edit_give_campaigns',
401
			'edit_give_campaigns_terms',
402
			'edit_give_campaignss',
403
			'edit_others_give_campaignss',
404
			'edit_private_give_campaignss',
405
			'edit_published_give_campaignss',
406
			'manage_give_campaigns_terms',
407
			'publish_give_campaignss',
408
			'read_give_campaigns',
409
			'read_private_give_campaignss',
410
			'view_give_campaigns_stats',
411
			'delete_give_paymentss',
412
			'delete_others_give_paymentss',
413
			'delete_private_give_paymentss',
414
			'delete_published_give_paymentss',
415
			'edit_give_paymentss',
416
			'edit_others_give_paymentss',
417
			'edit_private_give_paymentss',
418
			'edit_published_give_paymentss',
419
			'publish_give_paymentss',
420
			'read_private_give_paymentss',
421
		);
422
423
		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...
424
		foreach ( $delete_caps as $cap ) {
425
			foreach ( array_keys( $wp_roles->roles ) as $role ) {
426
				$wp_roles->remove_cap( $role, $cap );
427
			}
428
		}
429
430
		// Create Give plugin roles.
431
		$roles = new Give_Roles();
432
		$roles->add_roles();
433
		$roles->add_caps();
434
435
		// The Update Ran.
436
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
437
		give_set_upgrade_complete( 'upgrade_give_user_caps_cleanup' );
438
		delete_option( 'give_doing_upgrade' );
439
440
	}// 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...
441
442
}
443
444
add_action( 'admin_init', 'give_v152_cleanup_users' );
445
446
/**
447
 * 1.6 Upgrade routine to create the customer meta table.
448
 *
449
 * @since  1.6
450
 * @return void
451
 */
452
function give_v16_upgrades() {
453
	@Give()->customers->create_table();
454
	@Give()->customer_meta->create_table();
455
}
456
457
/**
458
 * 1.7 Upgrades.
459
 *
460
 * a. Update license api data for plugin addons.
461
 * b. Cleanup user roles.
462
 *
463
 * @since  1.7
464
 * @return void
465
 */
466
function give_v17_upgrades() {
467
	// Upgrade license data.
468
	give_v17_upgrade_addon_license_data();
469
	give_v17_cleanup_roles();
470
}
471
472
/**
473
 * Upgrade license data
474
 *
475
 * @since 1.7
476
 */
477
function give_v17_upgrade_addon_license_data() {
478
	$give_options = give_get_settings();
479
480
	$api_url = 'https://givewp.com/give-sl-api/';
481
482
	// Get addons license key.
483
	$addons = array();
484
	foreach ( $give_options as $key => $value ) {
485
		if ( false !== strpos( $key, '_license_key' ) ) {
486
			$addons[ $key ] = $value;
487
		}
488
	}
489
490
	// Bailout: We do not have any addon license data to upgrade.
491
	if ( empty( $addons ) ) {
492
		return false;
493
	}
494
495
	foreach ( $addons as $key => $addon_license ) {
496
497
		// Get addon shortname.
498
		$shortname = str_replace( '_license_key', '', $key );
499
500
		// Addon license option name.
501
		$addon_license_option_name = $shortname . '_license_active';
502
503
		// bailout if license is empty.
504
		if ( empty( $addon_license ) ) {
505
			delete_option( $addon_license_option_name );
506
			continue;
507
		}
508
509
		// Get addon name.
510
		$addon_name       = array();
511
		$addon_name_parts = explode( '_', str_replace( 'give_', '', $shortname ) );
512
		foreach ( $addon_name_parts as $name_part ) {
513
514
			// Fix addon name
515
			switch ( $name_part ) {
516
				case 'authorizenet' :
517
					$name_part = 'authorize.net';
518
					break;
519
			}
520
521
			$addon_name[] = ucfirst( $name_part );
522
		}
523
524
		$addon_name = implode( ' ', $addon_name );
525
526
		// Data to send to the API
527
		$api_params = array(
528
			'edd_action' => 'activate_license', // never change from "edd_" to "give_"!
529
			'license'    => $addon_license,
530
			'item_name'  => urlencode( $addon_name ),
531
			'url'        => home_url(),
532
		);
533
534
		// Call the API.
535
		$response = wp_remote_post(
536
			$api_url,
537
			array(
538
				'timeout'   => 15,
539
				'sslverify' => false,
540
				'body'      => $api_params,
541
			)
542
		);
543
544
		// Make sure there are no errors.
545
		if ( is_wp_error( $response ) ) {
546
			delete_option( $addon_license_option_name );
547
			continue;
548
		}
549
550
		// Tell WordPress to look for updates.
551
		set_site_transient( 'update_plugins', null );
552
553
		// Decode license data.
554
		$license_data = json_decode( wp_remote_retrieve_body( $response ) );
555
		update_option( $addon_license_option_name, $license_data );
556
	}// 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...
557
}
558
559
560
/**
561
 * Cleanup User Roles.
562
 *
563
 * This upgrade routine removes unused roles and roles with typos.
564
 *
565
 * @since      1.7
566
 */
567
function give_v17_cleanup_roles() {
568
569
	// Delete all caps with "_give_forms_" and "_give_payments_"
570
	// These roles have no usage; the proper is singular.
571
	$delete_caps = array(
572
		'view_give_forms_stats',
573
		'delete_give_forms_terms',
574
		'assign_give_forms_terms',
575
		'edit_give_forms_terms',
576
		'manage_give_forms_terms',
577
		'view_give_payments_stats',
578
		'manage_give_payments_terms',
579
		'edit_give_payments_terms',
580
		'assign_give_payments_terms',
581
		'delete_give_payments_terms',
582
	);
583
584
	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...
585
	foreach ( $delete_caps as $cap ) {
586
		foreach ( array_keys( $wp_roles->roles ) as $role ) {
587
			$wp_roles->remove_cap( $role, $cap );
588
		}
589
	}
590
591
	// Set roles again.
592
	$roles = new Give_Roles();
593
	$roles->add_roles();
594
	$roles->add_caps();
595
596
}
597
598
/**
599
 * 1.8 Upgrades.
600
 *
601
 * a. Upgrade checkbox settings to radio button settings.
602
 * a. Update form meta for new metabox settings.
603
 *
604
 * @since  1.8
605
 * @return void
606
 */
607
function give_v18_upgrades() {
608
	// Upgrade checkbox settings to radio button settings.
609
	give_v18_upgrades_core_setting();
610
}
611
612
/**
613
 * Upgrade core settings.
614
 *
615
 * @since  1.8
616
 * @return void
617
 */
618
function give_v18_upgrades_core_setting() {
619
	// Core settings which changes from checkbox to radio.
620
	$core_setting_names = array_merge(
621
		array_keys( give_v18_renamed_core_settings() ),
622
		array(
623
			'uninstall_on_delete',
624
			'scripts_footer',
625
			'test_mode',
626
			'email_access',
627
			'terms',
628
			'give_offline_donation_enable_billing_fields',
629
		)
630
	);
631
632
	// Bailout: If not any setting define.
633
	if ( $give_settings = get_option( 'give_settings' ) ) {
634
635
		$setting_changed = false;
636
637
		// Loop: check each setting field.
638
		foreach ( $core_setting_names as $setting_name ) {
639
			// New setting name.
640
			$new_setting_name = preg_replace( '/^(enable_|disable_)/', '', $setting_name );
641
642
			// Continue: If setting already set.
643
			if (
644
				array_key_exists( $new_setting_name, $give_settings )
645
				&& in_array( $give_settings[ $new_setting_name ], array( 'enabled', 'disabled' ) )
646
			) {
647
				continue;
648
			}
649
650
			// Set checkbox value to radio value.
651
			$give_settings[ $setting_name ] = ( ! empty( $give_settings[ $setting_name ] ) && 'on' === $give_settings[ $setting_name ] ? 'enabled' : 'disabled' );
652
653
			// @see https://github.com/WordImpress/Give/issues/1063
654
			if ( false !== strpos( $setting_name, 'disable_' ) ) {
655
656
				$give_settings[ $new_setting_name ] = ( give_is_setting_enabled( $give_settings[ $setting_name ] ) ? 'disabled' : 'enabled' );
657
			} elseif ( false !== strpos( $setting_name, 'enable_' ) ) {
658
659
				$give_settings[ $new_setting_name ] = ( give_is_setting_enabled( $give_settings[ $setting_name ] ) ? 'enabled' : 'disabled' );
660
			}
661
662
			// Tell bot to update core setting to db.
663
			if ( ! $setting_changed ) {
664
				$setting_changed = true;
665
			}
666
		}
667
668
		// Update setting only if they changed.
669
		if ( $setting_changed ) {
670
			update_option( 'give_settings', $give_settings );
671
		}
672
	}// 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...
673
674
	give_set_upgrade_complete( 'v18_upgrades_core_setting' );
675
}
676
677
/**
678
 * Upgrade form metadata for new metabox settings.
679
 *
680
 * @since  1.8
681
 * @return void
682
 */
683
function give_v18_upgrades_form_metadata() {
684
	if ( ! current_user_can( 'manage_give_settings' ) ) {
685
		wp_die( esc_html__( 'You do not have permission to do Give upgrades.', 'give' ), esc_html__( 'Error', 'give' ), array(
686
			'response' => 403,
687
		) );
688
	}
689
690
	ignore_user_abort( true );
691
692
	if ( ! give_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {
693
		@set_time_limit( 0 );
694
	}
695
696
	$step = isset( $_GET['step'] ) ? absint( $_GET['step'] ) : 1;
697
698
	// form query
699
	$forms = new WP_Query( array(
700
			'paged'          => $step,
701
			'status'         => 'any',
702
			'order'          => 'ASC',
703
			'post_type'      => 'give_forms',
704
			'posts_per_page' => 20,
705
		)
706
	);
707
708
709
	if ( $forms->have_posts() ) {
710
		while ( $forms->have_posts() ) {
711
			$forms->the_post();
712
713
			// Form content.
714
			// Note in version 1.8 display content setting split into display content and content placement setting.
715
			// You can delete _give_content_option in future
716
			$show_content = get_post_meta( get_the_ID(), '_give_content_option', true );
717
			if ( $show_content && ! get_post_meta( get_the_ID(), '_give_display_content', true ) ) {
718
				$field_value = ( 'none' !== $show_content ? 'enabled' : 'disabled' );
719
				update_post_meta( get_the_ID(), '_give_display_content', $field_value );
720
721
				$field_value = ( 'none' !== $show_content ? $show_content : 'give_pre_form' );
722
				update_post_meta( get_the_ID(), '_give_content_placement', $field_value );
723
			}
724
            
725
			// "Disable" Guest Donation. Checkbox
726
			// See: https://github.com/WordImpress/Give/issues/1470
727
			$guest_donation = get_post_meta( get_the_ID(), '_give_logged_in_only', true );
728
			$guest_donation_newval = ( in_array( $guest_donation, array( 'yes', 'on' ) ) ? 'disabled' : 'enabled' );
729
			update_post_meta( get_the_ID(), '_give_logged_in_only', $guest_donation_newval );
730
731
			// Convert yes/no setting field to enabled/disabled.
732
			$form_radio_settings = array(
733
				// Custom Amount.
734
				'_give_custom_amount',
735
736
				// Donation Gaol.
737
				'_give_goal_option',
738
739
				// Close Form.
740
				'_give_close_form_when_goal_achieved',
741
742
				// Term & conditions.
743
				'_give_terms_option',
744
745
				// Offline donation.
746
				'_give_customize_offline_donations',
747
748
				// Billing fields.
749
				'_give_offline_donation_enable_billing_fields_single',
750
			);
751
752
			foreach ( $form_radio_settings as $meta_key ) {
753
				// Get value.
754
				$field_value = get_post_meta( get_the_ID(), $meta_key, true );
755
756
				// Convert meta value only if it is in yes/no/none.
757
				if ( in_array( $field_value, array( 'yes', 'on', 'no', 'none' ) ) ) {
758
759
					$field_value = ( in_array( $field_value, array( 'yes', 'on' ) ) ? 'enabled' : 'disabled' );
760
					update_post_meta( get_the_ID(), $meta_key, $field_value );
761
				}
762
			}
763
		}// 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...
764
765
		wp_reset_postdata();
766
767
		// Forms found so upgrade them
768
		$step ++;
769
		$redirect = add_query_arg( array(
770
			'page'         => 'give-upgrades',
771
			'give-upgrade' => 'give_v18_upgrades_form_metadata',
772
			'step'         => $step,
773
		), admin_url( 'index.php' ) );
774
		wp_redirect( $redirect );
775
		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...
776
777
	} else {
778
		// No more forms found, finish up.
779
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
780
		delete_option( 'give_doing_upgrade' );
781
		give_set_upgrade_complete( 'v18_upgrades_form_metadata' );
782
783
784
		wp_redirect( admin_url() );
785
		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...
786
	}
787
}
788
789
add_action( 'give_give_v18_upgrades_form_metadata', 'give_v18_upgrades_form_metadata' );
790
791
/**
792
 * Get list of core setting renamed in version 1.8.
793
 *
794
 * @since  1.8
795
 * @return array
796
 */
797
function give_v18_renamed_core_settings() {
798
	return array(
799
		'disable_paypal_verification' => 'paypal_verification',
800
		'disable_css'                 => 'css',
801
		'disable_welcome'             => 'welcome',
802
		'disable_forms_singular'      => 'forms_singular',
803
		'disable_forms_archives'      => 'forms_archives',
804
		'disable_forms_excerpt'       => 'forms_excerpt',
805
		'disable_form_featured_img'   => 'form_featured_img',
806
		'disable_form_sidebar'        => 'form_sidebar',
807
		'disable_admin_notices'       => 'admin_notices',
808
		'disable_the_content_filter'  => 'the_content_filter',
809
		'enable_floatlabels'          => 'floatlabels',
810
		'enable_categories'           => 'categories',
811
		'enable_tags'                 => 'tags',
812
	);
813
}
814