Completed
Push — issues/1617 ( 85b236...52489e )
by Ravinder
16:05
created

upgrade-functions.php ➔ give_v187_upgrades()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 18
nc 4
nop 0
dl 0
loc 31
rs 8.5806
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 26 and the first side effect is on line 16.

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

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

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

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