Completed
Push — hotfix/give_currency_filter ( 2ab3ef )
by Ravinder
49:07 queued 29:09
created

upgrade-functions.php ➔ give_v188_upgrades()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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