Completed
Push — issues/1038 ( 82778e...ccd3e1 )
by Ravinder
19:16
created

upgrade-functions.php ➔ give_backward_compatibility_setting_api_1_8()   F

Complexity

Conditions 17
Paths 3841

Size

Total Lines 40
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 25
nc 3841
nop 1
dl 0
loc 40
rs 2.7204
c 0
b 0
f 0

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