Completed
Push — issues/1408 ( 577b26 )
by Ravinder
16:56
created

upgrade-functions.php ➔ give_show_upgrade_notices()   C

Complexity

Conditions 12
Paths 25

Size

Total Lines 68
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

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