Completed
Push — issues/1796 ( 616c54 )
by Ravinder
20:12
created

upgrade-functions.php ➔ give_do_automatic_upgrades()   D

Complexity

Conditions 10
Paths 32

Size

Total Lines 46
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 29
nc 32
nop 0
dl 0
loc 46
rs 4.983
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 26 and the first side effect is on line 16.

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

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

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

Loading history...
2
/**
3
 * Upgrade Functions
4
 *
5
 * @package     Give
6
 * @subpackage  Admin/Upgrades
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.0
10
 *
11
 * NOTICE: When adding new upgrade notices, please be sure to put the action into the upgrades array during install: /includes/install.php @ Appox Line 156
12
 */
13
14
// Exit if accessed directly.
15
if ( ! defined( 'ABSPATH' ) ) {
16
	exit;
17
}
18
19
20
/**
21
 * Perform automatic database upgrades when necessary.
22
 *
23
 * @since 1.6
24
 * @return void
25
 */
26
function give_do_automatic_upgrades() {
27
	$did_upgrade  = false;
28
	$give_version = preg_replace( '/[^0-9.].*/', '', get_option( 'give_version' ) );
29
30
	if ( ! $give_version ) {
31
		// 1.0 is the first version to use this option so we must add it.
32
		$give_version = '1.0';
33
	}
34
35
	switch ( true ) {
36
37
		case version_compare( $give_version, '1.6', '<' ) :
38
			give_v16_upgrades();
39
			$did_upgrade = true;
40
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
		case version_compare( $give_version, '1.8.9', '<' ) :
58
			give_v189_upgrades();
59
			$did_upgrade = true;
60
61
		case version_compare( $give_version, '2.0', '<' ) :
62
			give_v20_upgrades();
63
			$did_upgrade = true;
64
65
66
	}
67
68
	if ( $did_upgrade ) {
69
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
70
	}
71
}
72
73
add_action( 'admin_init', 'give_do_automatic_upgrades' );
74
add_action( 'give_upgrades', 'give_do_automatic_upgrades' );
75
76
/**
77
 * Display Upgrade Notices
78
 *
79
 * @since 1.0
80
 * @return void
81
 */
82
function give_show_upgrade_notices() {
83
	// Don't show notices on the upgrades page.
84
	if ( isset( $_GET['page'] ) && $_GET['page'] == 'give-upgrades' ) {
85
		return;
86
	}
87
88
	$give_version = get_option( 'give_version' );
89
90
	if ( ! $give_version ) {
91
		// 1.0 is the first version to use this option so we must add it.
92
		$give_version = '1.0';
93
	}
94
95
	$give_version = preg_replace( '/[^0-9.].*/', '', $give_version );
96
97
	/**
98
	 *  NOTICE:
99
	 *
100
	 *  When adding new upgrade notices, please be sure to put the action into the upgrades array during install:
101
	 *  /includes/install.php @ Approx Line 156
102
	 */
103
104
	// Resume updates.
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( give_maybe_resume_upgrade(), admin_url( 'index.php' ) );
109
110
		Give()->notices->register_notice( array(
111
			'id'          => 'give-resume-updates',
112
			'type'        => 'warning',
113
			'description' => sprintf(
114
				__( 'Give needs to complete a database upgrade that was previously started, click <a href="%s">here</a> to resume the upgrade.', 'give' ),
115
				esc_url( $resume_url )
116
			),
117
		) );
118
119
		return;
120
	}
121
122
	// v1.3.2 Upgrades
123
	Give()->notices->register_notice( array(
124
		'id'          => 'give-version-1-3-2-updates',
125
		'type'        => 'warning',
126
		'description' => sprintf(
127
			'<p>' . __( 'Give 1.3.2 needs to upgrade the donor database, click <a href="%s">here</a> to start the upgrade.', 'give' ) . '</p>',
128
			esc_url( admin_url( 'index.php?page=give-upgrades&give-upgrade=upgrade_give_payment_customer_id' ) )
129
		),
130
		'show'        => ( version_compare( $give_version, '1.3.2', '<' ) || ! give_has_upgrade_completed( 'upgrade_give_payment_customer_id' ) ),
131
	) );
132
133
	// v1.3.4 Upgrades //ensure the user has gone through 1.3.4.
134
	Give()->notices->register_notice( array(
135
		'id'          => 'give-version-1-3-4-updates',
136
		'type'        => 'warning',
137
		'description' => sprintf(
138
			'<p>' . __( 'Give 1.3.4 needs to upgrade the donations database, click <a href="%s">here</a> to start the upgrade.', 'give' ) . '</p>',
139
			esc_url( admin_url( 'index.php?page=give-upgrades&give-upgrade=upgrade_give_offline_status' ) )
140
		),
141
		'show'        => ( version_compare( $give_version, '1.3.4', '<' ) || ( ! give_has_upgrade_completed( 'upgrade_give_offline_status' ) && give_has_upgrade_completed( 'upgrade_give_payment_customer_id' ) ) ),
142
	) );
143
144
	// v1.8 form metadata upgrades.
145
	Give()->notices->register_notice( array(
146
		'id'          => 'give-version-1-8-updates',
147
		'type'        => 'warning',
148
		'description' => sprintf(
149
			__( 'Give 1.8 needs to upgrade the form database, click <a class="give-upgrade-link" href="%s">here</a> to start the upgrade.', 'give' ),
150
			esc_url( admin_url( 'index.php?page=give-upgrades&give-upgrade=give_v18_upgrades_form_metadata' ) )
151
		),
152
		'show'        => ( version_compare( $give_version, '1.8', '<' ) || ! give_has_upgrade_completed( 'v18_upgrades_form_metadata' ) )
153
	) );
154
155
	// v1.8.9 Upgrades
156
	Give()->notices->register_notice( array(
157
		'id'          => 'give-version-1-8-9-updates',
158
		'type'        => 'warning',
159
		'description' => sprintf(
160
			__( 'Give 1.8.9 needs to upgrade the donation forms meta-fields in database, click <a href="%s">here</a> to start the upgrade.', 'give' ),
161
			esc_url( admin_url( 'index.php?page=give-upgrades&give-upgrade=v189_upgrades_levels_post_meta' ) )
162
		),
163
		'show'        => ( version_compare( $give_version, '1.8.9', '<' ) || ( ! give_has_upgrade_completed( 'v189_upgrades_levels_post_meta' ) ) ),
164
	) );
165
166
	// v2.0 form metadata upgrades.
167
	Give()->notices->register_notice( array(
168
		'id'          => 'give-version-2-0-0-updates',
169
		'type'        => 'warning',
170
		'description' => sprintf(
171
			__( 'Give 2.0 needs to upgrade the form database, click %1$shere%2$s to start the upgrade.', 'give' ),
172
			'<a class="give-upgrade-link" href="' . esc_url( admin_url( 'index.php?page=give-upgrades&give-upgrade=give_v20_upgrades_form_metadata' ) ) . '">',
173
			'</a>'
174
		),
175
		'show'        => ( version_compare( $give_version, '2.0', '<' ) || ( ! give_has_upgrade_completed( 'v20_upgrades_form_metadata' ) ) ),
176
	) );
177
178
	// End 'Stepped' upgrade process notices.
179
	?>
180
	<script>
181
		jQuery(document).ready(function ($) {
182
			var $upgrade_links = $('.give-upgrade-link');
183
			if ($upgrade_links.length) {
184
				$upgrade_links.on('click', function (e) {
185
					e.preventDefault();
186
187
					if (!window.confirm('<?php _e( 'Please make sure to create a database backup before initiating the upgrade.', 'give' ); ?>')) {
188
						return;
189
					}
190
191
					// Redirect to upgrdae link.
192
					window.location.assign($(this).attr('href'));
193
				});
194
			}
195
		});
196
	</script>
197
	<?php
198
}
199
200
add_action( 'admin_notices', 'give_show_upgrade_notices' );
201
202
/**
203
 * Triggers all upgrade functions
204
 *
205
 * This function is usually triggered via AJAX
206
 *
207
 * @since 1.0
208
 * @return void
209
 */
210
function give_trigger_upgrades() {
211
212
	if ( ! current_user_can( 'manage_give_settings' ) ) {
213
		wp_die( esc_html__( 'You do not have permission to do Give upgrades.', 'give' ), esc_html__( 'Error', 'give' ), array(
214
			'response' => 403,
215
		) );
216
	}
217
218
	$give_version = get_option( 'give_version' );
219
220
	if ( ! $give_version ) {
221
		// 1.0 is the first version to use this option so we must add it.
222
		$give_version = '1.0';
223
		add_option( 'give_version', $give_version );
224
	}
225
226
	update_option( 'give_version', GIVE_VERSION );
227
	delete_option( 'give_doing_upgrade' );
228
229
	if ( DOING_AJAX ) {
230
		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...
231
	} // 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...
232
}
233
234
add_action( 'wp_ajax_give_trigger_upgrades', 'give_trigger_upgrades' );
235
236
/**
237
 * Check if the upgrade routine has been run for a specific action
238
 *
239
 * @since  1.0
240
 *
241
 * @param  string $upgrade_action The upgrade action to check completion for
242
 *
243
 * @return bool                   If the action has been added to the completed actions array
244
 */
245
function give_has_upgrade_completed( $upgrade_action = '' ) {
246
247
	if ( empty( $upgrade_action ) ) {
248
		return false;
249
	}
250
251
	$completed_upgrades = give_get_completed_upgrades();
252
253
	return in_array( $upgrade_action, $completed_upgrades );
254
255
}
256
257
/**
258
 * For use when doing 'stepped' upgrade routines, to see if we need to start somewhere in the middle
259
 *
260
 * @since 1.8
261
 *
262
 * @return mixed   When nothing to resume returns false, otherwise starts the upgrade where it left off
263
 */
264
function give_maybe_resume_upgrade() {
265
	$doing_upgrade = get_option( 'give_doing_upgrade', false );
266
	if ( empty( $doing_upgrade ) ) {
267
		return false;
268
	}
269
270
	return $doing_upgrade;
271
}
272
273
/**
274
 * Adds an upgrade action to the completed upgrades array
275
 *
276
 * @since  1.0
277
 *
278
 * @param  string $upgrade_action The action to add to the completed upgrades array
279
 *
280
 * @return bool                   If the function was successfully added
281
 */
282
function give_set_upgrade_complete( $upgrade_action = '' ) {
283
284
	if ( empty( $upgrade_action ) ) {
285
		return false;
286
	}
287
288
	$completed_upgrades   = give_get_completed_upgrades();
289
	$completed_upgrades[] = $upgrade_action;
290
291
	// Remove any blanks, and only show uniques.
292
	$completed_upgrades = array_unique( array_values( $completed_upgrades ) );
293
294
	return update_option( 'give_completed_upgrades', $completed_upgrades );
295
}
296
297
/**
298
 * Get's the array of completed upgrade actions
299
 *
300
 * @since  1.0
301
 * @return array The array of completed upgrades
302
 */
303
function give_get_completed_upgrades() {
304
305
	$completed_upgrades = get_option( 'give_completed_upgrades' );
306
307
	if ( false === $completed_upgrades ) {
308
		$completed_upgrades = array();
309
	}
310
311
	return $completed_upgrades;
312
313
}
314
315
/**
316
 * Upgrades the
317
 *
318
 * Standardizes the discrepancies between two metakeys `_give_payment_customer_id` and `_give_payment_donor_id`
319
 *
320
 * @since      1.3.2
321
 */
322
function give_v132_upgrade_give_payment_customer_id() {
323
	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...
324
	if ( ! current_user_can( 'manage_give_settings' ) ) {
325
		wp_die( esc_html__( 'You do not have permission to do Give upgrades.', 'give' ), esc_html__( 'Error', 'give' ), array(
326
			'response' => 403,
327
		) );
328
	}
329
330
	ignore_user_abort( true );
331
332
	if ( ! give_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {
333
		@set_time_limit( 0 );
334
	}
335
336
	// UPDATE DB METAKEYS.
337
	$sql   = "UPDATE $wpdb->postmeta SET meta_key = '_give_payment_customer_id' WHERE meta_key = '_give_payment_donor_id'";
338
	$query = $wpdb->query( $sql );
339
340
	update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
341
	give_set_upgrade_complete( 'upgrade_give_payment_customer_id' );
342
	delete_option( 'give_doing_upgrade' );
343
	wp_redirect( admin_url() );
344
	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...
345
346
}
347
348
add_action( 'give_upgrade_give_payment_customer_id', 'give_v132_upgrade_give_payment_customer_id' );
349
350
/**
351
 * Upgrades the Offline Status
352
 *
353
 * Reverses the issue where offline donations in "pending" status where inappropriately marked as abandoned
354
 *
355
 * @since      1.3.4
356
 */
357
function give_v134_upgrade_give_offline_status() {
358
359
	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...
360
361
	if ( ! current_user_can( 'manage_give_settings' ) ) {
362
		wp_die( esc_html__( 'You do not have permission to do Give upgrades.', 'give' ), esc_html__( 'Error', 'give' ), array(
363
			'response' => 403,
364
		) );
365
	}
366
367
	ignore_user_abort( true );
368
369
	if ( ! give_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {
370
		@set_time_limit( 0 );
371
	}
372
373
	// Get abandoned offline payments.
374
	$select = "SELECT ID FROM $wpdb->posts p ";
375
	$join   = "LEFT JOIN $wpdb->postmeta m ON p.ID = m.post_id ";
376
	$where  = "WHERE p.post_type = 'give_payment' ";
377
	$where  .= "AND ( p.post_status = 'abandoned' )";
378
	$where  .= "AND ( m.meta_key = '_give_payment_gateway' AND m.meta_value = 'offline' )";
379
380
	$sql            = $select . $join . $where;
381
	$found_payments = $wpdb->get_col( $sql );
382
383
	foreach ( $found_payments as $payment ) {
384
385
		// Only change ones marked abandoned since our release last week because the admin may have marked some abandoned themselves.
386
		$modified_time = get_post_modified_time( 'U', false, $payment );
387
388
		// 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...
389
		if ( $modified_time >= 1450124863 ) {
390
391
			give_update_payment_status( $payment, 'pending' );
392
393
		}
394
	}
395
396
	update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
397
	give_set_upgrade_complete( 'upgrade_give_offline_status' );
398
	delete_option( 'give_doing_upgrade' );
399
	wp_redirect( admin_url() );
400
	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...
401
402
}
403
404
add_action( 'give_upgrade_give_offline_status', 'give_v134_upgrade_give_offline_status' );
405
406
/**
407
 * Cleanup User Roles
408
 *
409
 * This upgrade routine removes unused roles and roles with typos
410
 *
411
 * @since      1.5.2
412
 */
413
function give_v152_cleanup_users() {
414
415
	$give_version = get_option( 'give_version' );
416
417
	if ( ! $give_version ) {
418
		// 1.0 is the first version to use this option so we must add it.
419
		$give_version = '1.0';
420
	}
421
422
	$give_version = preg_replace( '/[^0-9.].*/', '', $give_version );
423
424
	// v1.5.2 Upgrades
425
	if ( version_compare( $give_version, '1.5.2', '<' ) || ! give_has_upgrade_completed( 'upgrade_give_user_caps_cleanup' ) ) {
426
427
		// Delete all caps with "ss".
428
		// Also delete all unused "campaign" roles.
429
		$delete_caps = array(
430
			'delete_give_formss',
431
			'delete_others_give_formss',
432
			'delete_private_give_formss',
433
			'delete_published_give_formss',
434
			'read_private_forms',
435
			'edit_give_formss',
436
			'edit_others_give_formss',
437
			'edit_private_give_formss',
438
			'edit_published_give_formss',
439
			'publish_give_formss',
440
			'read_private_give_formss',
441
			'assign_give_campaigns_terms',
442
			'delete_give_campaigns',
443
			'delete_give_campaigns_terms',
444
			'delete_give_campaignss',
445
			'delete_others_give_campaignss',
446
			'delete_private_give_campaignss',
447
			'delete_published_give_campaignss',
448
			'edit_give_campaigns',
449
			'edit_give_campaigns_terms',
450
			'edit_give_campaignss',
451
			'edit_others_give_campaignss',
452
			'edit_private_give_campaignss',
453
			'edit_published_give_campaignss',
454
			'manage_give_campaigns_terms',
455
			'publish_give_campaignss',
456
			'read_give_campaigns',
457
			'read_private_give_campaignss',
458
			'view_give_campaigns_stats',
459
			'delete_give_paymentss',
460
			'delete_others_give_paymentss',
461
			'delete_private_give_paymentss',
462
			'delete_published_give_paymentss',
463
			'edit_give_paymentss',
464
			'edit_others_give_paymentss',
465
			'edit_private_give_paymentss',
466
			'edit_published_give_paymentss',
467
			'publish_give_paymentss',
468
			'read_private_give_paymentss',
469
		);
470
471
		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...
472
		foreach ( $delete_caps as $cap ) {
473
			foreach ( array_keys( $wp_roles->roles ) as $role ) {
474
				$wp_roles->remove_cap( $role, $cap );
475
			}
476
		}
477
478
		// Create Give plugin roles.
479
		$roles = new Give_Roles();
480
		$roles->add_roles();
481
		$roles->add_caps();
482
483
		// The Update Ran.
484
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
485
		give_set_upgrade_complete( 'upgrade_give_user_caps_cleanup' );
486
		delete_option( 'give_doing_upgrade' );
487
488
	}// 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...
489
490
}
491
492
add_action( 'admin_init', 'give_v152_cleanup_users' );
493
494
/**
495
 * 1.6 Upgrade routine to create the customer meta table.
496
 *
497
 * @since  1.6
498
 * @return void
499
 */
500
function give_v16_upgrades() {
501
	// Create the donor databases.
502
	$donors_db = new Give_DB_Donors();
503
	$donors_db->create_table();
504
	$donor_meta = new Give_DB_Donor_Meta();
505
	$donor_meta->create_table();
506
}
507
508
/**
509
 * 1.7 Upgrades.
510
 *
511
 * a. Update license api data for plugin addons.
512
 * b. Cleanup user roles.
513
 *
514
 * @since  1.7
515
 * @return void
516
 */
517
function give_v17_upgrades() {
518
	// Upgrade license data.
519
	give_v17_upgrade_addon_license_data();
520
	give_v17_cleanup_roles();
521
}
522
523
/**
524
 * Upgrade license data
525
 *
526
 * @since 1.7
527
 */
528
function give_v17_upgrade_addon_license_data() {
529
	$give_options = give_get_settings();
530
531
	$api_url = 'https://givewp.com/give-sl-api/';
532
533
	// Get addons license key.
534
	$addons = array();
535
	foreach ( $give_options as $key => $value ) {
536
		if ( false !== strpos( $key, '_license_key' ) ) {
537
			$addons[ $key ] = $value;
538
		}
539
	}
540
541
	// Bailout: We do not have any addon license data to upgrade.
542
	if ( empty( $addons ) ) {
543
		return false;
544
	}
545
546
	foreach ( $addons as $key => $addon_license ) {
547
548
		// Get addon shortname.
549
		$shortname = str_replace( '_license_key', '', $key );
550
551
		// Addon license option name.
552
		$addon_license_option_name = $shortname . '_license_active';
553
554
		// bailout if license is empty.
555
		if ( empty( $addon_license ) ) {
556
			delete_option( $addon_license_option_name );
557
			continue;
558
		}
559
560
		// Get addon name.
561
		$addon_name       = array();
562
		$addon_name_parts = explode( '_', str_replace( 'give_', '', $shortname ) );
563
		foreach ( $addon_name_parts as $name_part ) {
564
565
			// Fix addon name
566
			switch ( $name_part ) {
567
				case 'authorizenet' :
568
					$name_part = 'authorize.net';
569
					break;
570
			}
571
572
			$addon_name[] = ucfirst( $name_part );
573
		}
574
575
		$addon_name = implode( ' ', $addon_name );
576
577
		// Data to send to the API
578
		$api_params = array(
579
			'edd_action' => 'activate_license', // never change from "edd_" to "give_"!
580
			'license'    => $addon_license,
581
			'item_name'  => urlencode( $addon_name ),
582
			'url'        => home_url(),
583
		);
584
585
		// Call the API.
586
		$response = wp_remote_post(
587
			$api_url,
588
			array(
589
				'timeout'   => 15,
590
				'sslverify' => false,
591
				'body'      => $api_params,
592
			)
593
		);
594
595
		// Make sure there are no errors.
596
		if ( is_wp_error( $response ) ) {
597
			delete_option( $addon_license_option_name );
598
			continue;
599
		}
600
601
		// Tell WordPress to look for updates.
602
		set_site_transient( 'update_plugins', null );
603
604
		// Decode license data.
605
		$license_data = json_decode( wp_remote_retrieve_body( $response ) );
606
		update_option( $addon_license_option_name, $license_data );
607
	}// 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...
608
}
609
610
611
/**
612
 * Cleanup User Roles.
613
 *
614
 * This upgrade routine removes unused roles and roles with typos.
615
 *
616
 * @since      1.7
617
 */
618
function give_v17_cleanup_roles() {
619
620
	// Delete all caps with "_give_forms_" and "_give_payments_"
621
	// These roles have no usage; the proper is singular.
622
	$delete_caps = array(
623
		'view_give_forms_stats',
624
		'delete_give_forms_terms',
625
		'assign_give_forms_terms',
626
		'edit_give_forms_terms',
627
		'manage_give_forms_terms',
628
		'view_give_payments_stats',
629
		'manage_give_payments_terms',
630
		'edit_give_payments_terms',
631
		'assign_give_payments_terms',
632
		'delete_give_payments_terms',
633
	);
634
635
	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...
636
	foreach ( $delete_caps as $cap ) {
637
		foreach ( array_keys( $wp_roles->roles ) as $role ) {
638
			$wp_roles->remove_cap( $role, $cap );
639
		}
640
	}
641
642
	// Set roles again.
643
	$roles = new Give_Roles();
644
	$roles->add_roles();
645
	$roles->add_caps();
646
647
}
648
649
/**
650
 * 1.8 Upgrades.
651
 *
652
 * a. Upgrade checkbox settings to radio button settings.
653
 * a. Update form meta for new metabox settings.
654
 *
655
 * @since  1.8
656
 * @return void
657
 */
658
function give_v18_upgrades() {
659
	// Upgrade checkbox settings to radio button settings.
660
	give_v18_upgrades_core_setting();
661
}
662
663
/**
664
 * Upgrade core settings.
665
 *
666
 * @since  1.8
667
 * @return void
668
 */
669
function give_v18_upgrades_core_setting() {
670
	// Core settings which changes from checkbox to radio.
671
	$core_setting_names = array_merge(
672
		array_keys( give_v18_renamed_core_settings() ),
673
		array(
674
			'uninstall_on_delete',
675
			'scripts_footer',
676
			'test_mode',
677
			'email_access',
678
			'terms',
679
			'give_offline_donation_enable_billing_fields',
680
		)
681
	);
682
683
	// Bailout: If not any setting define.
684
	if ( $give_settings = get_option( 'give_settings' ) ) {
685
686
		$setting_changed = false;
687
688
		// Loop: check each setting field.
689
		foreach ( $core_setting_names as $setting_name ) {
690
			// New setting name.
691
			$new_setting_name = preg_replace( '/^(enable_|disable_)/', '', $setting_name );
692
693
			// Continue: If setting already set.
694
			if (
695
				array_key_exists( $new_setting_name, $give_settings )
696
				&& in_array( $give_settings[ $new_setting_name ], array( 'enabled', 'disabled' ) )
697
			) {
698
				continue;
699
			}
700
701
			// Set checkbox value to radio value.
702
			$give_settings[ $setting_name ] = ( ! empty( $give_settings[ $setting_name ] ) && 'on' === $give_settings[ $setting_name ] ? 'enabled' : 'disabled' );
703
704
			// @see https://github.com/WordImpress/Give/issues/1063
705
			if ( false !== strpos( $setting_name, 'disable_' ) ) {
706
707
				$give_settings[ $new_setting_name ] = ( give_is_setting_enabled( $give_settings[ $setting_name ] ) ? 'disabled' : 'enabled' );
708
			} elseif ( false !== strpos( $setting_name, 'enable_' ) ) {
709
710
				$give_settings[ $new_setting_name ] = ( give_is_setting_enabled( $give_settings[ $setting_name ] ) ? 'enabled' : 'disabled' );
711
			}
712
713
			// Tell bot to update core setting to db.
714
			if ( ! $setting_changed ) {
715
				$setting_changed = true;
716
			}
717
		}
718
719
		// Update setting only if they changed.
720
		if ( $setting_changed ) {
721
			update_option( 'give_settings', $give_settings );
722
		}
723
	}// 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...
724
725
	give_set_upgrade_complete( 'v18_upgrades_core_setting' );
726
}
727
728
/**
729
 * Upgrade form metadata for new metabox settings.
730
 *
731
 * @since  1.8
732
 * @return void
733
 */
734
function give_v18_upgrades_form_metadata() {
735
	if ( ! current_user_can( 'manage_give_settings' ) ) {
736
		wp_die( esc_html__( 'You do not have permission to do Give upgrades.', 'give' ), esc_html__( 'Error', 'give' ), array(
737
			'response' => 403,
738
		) );
739
	}
740
741
	ignore_user_abort( true );
742
743
	if ( ! give_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {
744
		@set_time_limit( 0 );
745
	}
746
747
	$step = isset( $_GET['step'] ) ? absint( $_GET['step'] ) : 1;
748
749
	// form query
750
	$forms = new WP_Query( array(
751
			'paged'          => $step,
752
			'status'         => 'any',
753
			'order'          => 'ASC',
754
			'post_type'      => 'give_forms',
755
			'posts_per_page' => 20,
756
		)
757
	);
758
759
	if ( $forms->have_posts() ) {
760
		while ( $forms->have_posts() ) {
761
			$forms->the_post();
762
763
			// Form content.
764
			// Note in version 1.8 display content setting split into display content and content placement setting.
765
			// You can delete _give_content_option in future
766
			$show_content = give_get_meta( get_the_ID(), '_give_content_option', true );
767
			if ( $show_content && ! give_get_meta( get_the_ID(), '_give_display_content', true ) ) {
768
				$field_value = ( 'none' !== $show_content ? 'enabled' : 'disabled' );
769
				give_update_meta( get_the_ID(), '_give_display_content', $field_value );
770
771
				$field_value = ( 'none' !== $show_content ? $show_content : 'give_pre_form' );
772
				give_update_meta( get_the_ID(), '_give_content_placement', $field_value );
773
			}
774
775
			// "Disable" Guest Donation. Checkbox
776
			// See: https://github.com/WordImpress/Give/issues/1470
777
			$guest_donation        = give_get_meta( get_the_ID(), '_give_logged_in_only', true );
778
			$guest_donation_newval = ( in_array( $guest_donation, array( 'yes', 'on' ) ) ? 'disabled' : 'enabled' );
779
			give_update_meta( get_the_ID(), '_give_logged_in_only', $guest_donation_newval );
780
781
			// Offline Donations
782
			// See: https://github.com/WordImpress/Give/issues/1579
783
			$offline_donation = give_get_meta( get_the_ID(), '_give_customize_offline_donations', true );
784
			if ( 'no' === $offline_donation ) {
785
				$offline_donation_newval = 'global';
786
			} elseif ( 'yes' === $offline_donation ) {
787
				$offline_donation_newval = 'enabled';
788
			} else {
789
				$offline_donation_newval = 'disabled';
790
			}
791
			give_update_meta( get_the_ID(), '_give_customize_offline_donations', $offline_donation_newval );
792
793
			// Convert yes/no setting field to enabled/disabled.
794
			$form_radio_settings = array(
795
				// Custom Amount.
796
				'_give_custom_amount',
797
798
				// Donation Gaol.
799
				'_give_goal_option',
800
801
				// Close Form.
802
				'_give_close_form_when_goal_achieved',
803
804
				// Term & conditions.
805
				'_give_terms_option',
806
807
				// Billing fields.
808
				'_give_offline_donation_enable_billing_fields_single',
809
			);
810
811
			foreach ( $form_radio_settings as $meta_key ) {
812
				// Get value.
813
				$field_value = give_get_meta( get_the_ID(), $meta_key, true );
814
815
				// Convert meta value only if it is in yes/no/none.
816
				if ( in_array( $field_value, array( 'yes', 'on', 'no', 'none' ) ) ) {
817
818
					$field_value = ( in_array( $field_value, array( 'yes', 'on' ) ) ? 'enabled' : 'disabled' );
819
					give_update_meta( get_the_ID(), $meta_key, $field_value );
820
				}
821
			}
822
		}// 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...
823
824
		wp_reset_postdata();
825
826
		// Forms found so upgrade them
827
		$step ++;
828
		$redirect = add_query_arg( array(
829
			'page'         => 'give-upgrades',
830
			'give-upgrade' => 'give_v18_upgrades_form_metadata',
831
			'step'         => $step,
832
		), admin_url( 'index.php' ) );
833
		wp_redirect( $redirect );
834
		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...
835
836
	} else {
837
		// No more forms found, finish up.
838
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
839
		delete_option( 'give_doing_upgrade' );
840
		give_set_upgrade_complete( 'v18_upgrades_form_metadata' );
841
842
		wp_redirect( admin_url() );
843
		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...
844
	}
845
}
846
847
add_action( 'give_give_v18_upgrades_form_metadata', 'give_v18_upgrades_form_metadata' );
848
849
/**
850
 * Get list of core setting renamed in version 1.8.
851
 *
852
 * @since  1.8
853
 * @return array
854
 */
855
function give_v18_renamed_core_settings() {
856
	return array(
857
		'disable_paypal_verification' => 'paypal_verification',
858
		'disable_css'                 => 'css',
859
		'disable_welcome'             => 'welcome',
860
		'disable_forms_singular'      => 'forms_singular',
861
		'disable_forms_archives'      => 'forms_archives',
862
		'disable_forms_excerpt'       => 'forms_excerpt',
863
		'disable_form_featured_img'   => 'form_featured_img',
864
		'disable_form_sidebar'        => 'form_sidebar',
865
		'disable_admin_notices'       => 'admin_notices',
866
		'disable_the_content_filter'  => 'the_content_filter',
867
		'enable_floatlabels'          => 'floatlabels',
868
		'enable_categories'           => 'categories',
869
		'enable_tags'                 => 'tags',
870
	);
871
}
872
873
874
/**
875
 * Upgrade core settings.
876
 *
877
 * @since  1.8.7
878
 * @return void
879
 */
880
function give_v187_upgrades() {
881
	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...
882
883
	/**
884
	 * Upgrade 1: Remove stat and cache transients.
885
	 */
886
	$cached_options = $wpdb->get_col(
887
		$wpdb->prepare(
888
			"SELECT * FROM {$wpdb->options} where (option_name LIKE '%%%s%%' OR option_name LIKE '%%%s%%')",
889
			array(
890
				'_transient_give_stats_',
891
				'give_cache',
892
				'_transient_give_add_ons_feed',
893
				'_transient__give_ajax_works',
894
				'_transient_give_total_api_keys',
895
				'_transient_give_i18n_give_promo_hide',
896
				'_transient_give_contributors',
897
				'_transient_give_estimated_monthly_stats',
898
				'_transient_give_earnings_total',
899
				'_transient_give_i18n_give_',
900
				'_transient__give_installed',
901
				'_transient__give_activation_redirect',
902
				'_transient__give_hide_license_notices_shortly_',
903
				'give_income_total',
904
			)
905
		),
906
		1
907
	);
908
909
	// User related transients.
910
	$user_apikey_options = $wpdb->get_results(
911
		$wpdb->prepare(
912
			"SELECT user_id, meta_key
913
			FROM $wpdb->usermeta
914
			WHERE meta_value=%s",
915
			'give_user_public_key'
916
		),
917
		ARRAY_A
918
	);
919
920
	if ( ! empty( $user_apikey_options ) ) {
921
		foreach ( $user_apikey_options as $user ) {
922
			$cached_options[] = '_transient_' . md5( 'give_api_user_' . $user['meta_key'] );
923
			$cached_options[] = '_transient_' . md5( 'give_api_user_public_key' . $user['user_id'] );
924
			$cached_options[] = '_transient_' . md5( 'give_api_user_secret_key' . $user['user_id'] );
925
		}
926
	}
927
928
	if ( ! empty( $cached_options ) ) {
929
		foreach ( $cached_options as $option ) {
930
			switch ( true ) {
931
				case ( false !== strpos( $option, 'transient' ) ):
932
					$option = str_replace( '_transient_', '', $option );
933
					delete_transient( $option );
934
					break;
935
936
				default:
937
					delete_option( $option );
938
			}
939
		}
940
	}
941
}
942
943
/**
944
 * Update Capabilities for Give_Worker User Role.
945
 *
946
 * This upgrade routine will update access rights for Give_Worker User Role.
947
 *
948
 * @since      1.8.8
949
 */
950
function give_v188_upgrades() {
951
952
	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...
953
954
	// Get the role object.
955
	$give_worker = get_role( 'give_worker' );
956
957
	// A list of capabilities to add for give workers.
958
	$caps_to_add = array(
959
		'edit_posts',
960
		'edit_pages',
961
	);
962
963
	foreach ( $caps_to_add as $cap ) {
964
		// Add the capability.
965
		$give_worker->add_cap( $cap );
966
	}
967
968
}
969
970
/**
971
 * Update Post meta for minimum and maximum amount for multi level donation forms
972
 *
973
 * This upgrade routine adds post meta for give_forms CPT for multi level donation form.
974
 *
975
 * @since      1.8.9
976
 */
977
function give_v189_upgrades_levels_post_meta_callback() {
978
979
	if ( ! current_user_can( 'manage_give_settings' ) ) {
980
		wp_die( esc_html__( 'You do not have permission to do Give upgrades.', 'give' ), esc_html__( 'Error', 'give' ), array(
981
			'response' => 403,
982
		) );
983
	}
984
985
	ignore_user_abort( true );
986
987
	if ( ! give_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {
988
		@set_time_limit( 0 );
989
	}
990
991
	$step = isset( $_GET['step'] ) ? absint( $_GET['step'] ) : 1;
992
993
	// form query
994
	$donation_forms = new WP_Query( array(
995
			'paged'          => $step,
996
			'status'         => 'any',
997
			'order'          => 'ASC',
998
			'post_type'      => 'give_forms',
999
			'posts_per_page' => 20,
1000
		)
1001
	);
1002
1003
	if ( $donation_forms->have_posts() ) {
1004
		while ( $donation_forms->have_posts() ) {
1005
			$donation_forms->the_post();
1006
			$form_id = get_the_ID();
1007
1008
			// Remove formatting from _give_set_price
1009
			update_post_meta(
1010
				$form_id,
1011
				'_give_set_price',
1012
				give_sanitize_amount( get_post_meta( $form_id, '_give_set_price', true ) )
1013
			);
1014
1015
			// Remove formatting from _give_custom_amount_minimum
1016
			update_post_meta(
1017
				$form_id,
1018
				'_give_custom_amount_minimum',
1019
				give_sanitize_amount( get_post_meta( $form_id, '_give_custom_amount_minimum', true ) )
1020
			);
1021
1022
			// Bailout.
1023
			if ( 'set' === get_post_meta( $form_id, '_give_price_option', true ) ) {
1024
				continue;
1025
			}
1026
1027
			$donation_levels = get_post_meta( $form_id, '_give_donation_levels', true );
1028
1029
			if ( ! empty( $donation_levels ) ) {
1030
1031
				foreach ( $donation_levels as $index => $donation_level ) {
1032
					if ( isset( $donation_level['_give_amount'] ) ) {
1033
						$donation_levels[ $index ]['_give_amount'] = give_sanitize_amount( $donation_level['_give_amount'] );
1034
					}
1035
				}
1036
1037
				update_post_meta( $form_id, '_give_donation_levels', $donation_levels );
1038
1039
				$donation_levels_amounts = wp_list_pluck( $donation_levels, '_give_amount' );
1040
1041
				$min_amount = min( $donation_levels_amounts );
1042
				$max_amount = max( $donation_levels_amounts );
1043
1044
				// Set Minimum and Maximum amount for Multi Level Donation Forms
1045
				give_update_meta( $form_id, '_give_levels_minimum_amount', $min_amount ? give_sanitize_amount( $min_amount ) : 0 );
1046
				give_update_meta( $form_id, '_give_levels_maximum_amount', $max_amount ? give_sanitize_amount( $max_amount ) : 0 );
1047
			}
1048
1049
		}
1050
1051
		/* Restore original Post Data */
1052
		wp_reset_postdata();
1053
1054
		// Forms found so upgrade them
1055
		$step ++;
1056
		$redirect = add_query_arg( array(
1057
			'page'         => 'give-upgrades',
1058
			'give-upgrade' => 'v189_upgrades_levels_post_meta',
1059
			'step'         => $step,
1060
		), admin_url( 'index.php' ) );
1061
		wp_redirect( $redirect );
1062
		exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_v189_upgrades_levels_post_meta_callback() 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...
1063
	} else {
1064
		// The Update Ran.
1065
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
1066
		give_set_upgrade_complete( 'v189_upgrades_levels_post_meta' );
1067
		delete_option( 'give_doing_upgrade' );
1068
1069
		wp_redirect( admin_url() );
1070
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_v189_upgrades_levels_post_meta_callback() 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...
1071
	}
1072
1073
}
1074
1075
add_action( 'give_v189_upgrades_levels_post_meta', 'give_v189_upgrades_levels_post_meta_callback' );
1076
1077
1078
/**
1079
 * Give version 1.8.9 upgrades
1080
 *
1081
 * @since      1.8.9
1082
 */
1083
function give_v189_upgrades() {
1084
	/**
1085
	 * 1. Remove user license related notice show blocked ( Give_Notice will handle )
1086
	 */
1087
	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...
1088
1089
	// Delete permanent notice blocker.
1090
	$wpdb->query(
1091
		$wpdb->prepare(
1092
			"
1093
					DELETE FROM $wpdb->usermeta
1094
					WHERE meta_key
1095
					LIKE '%%%s%%'
1096
					",
1097
			'_give_hide_license_notices_permanently'
1098
		)
1099
	);
1100
1101
	// Delete short notice blocker.
1102
	$wpdb->query(
1103
		$wpdb->prepare(
1104
			"
1105
					DELETE FROM $wpdb->options
1106
					WHERE option_name
1107
					LIKE '%%%s%%'
1108
					",
1109
			'__give_hide_license_notices_shortly_'
1110
		)
1111
	);
1112
}
1113
1114
/**
1115
 * 2.0 Upgrades.
1116
 *
1117
 * @since  2.0
1118
 * @return void
1119
 */
1120
function give_v20_upgrades() {
1121
	// Upgrade email settings.
1122
	give_v20_upgrades_email_setting();
1123
}
1124
1125
/**
1126
 * Move old email api settings to new email setting api for following emails:
1127
 *    1. new offline donation         [This was hard coded]
1128
 *    2. offline donation instruction
1129
 *    3. new donation
1130
 *    4. donation receipt
1131
 *
1132
 * @since 2.0
1133
 */
1134
function give_v20_upgrades_email_setting() {
1135
	$all_setting = give_get_settings();
1136
1137
	// Bailout on fresh install.
1138
	if ( empty( $all_setting ) ) {
1139
		return;
1140
	}
1141
1142
	$settings = array(
1143
		'offline_donation_subject'      => 'offline-donation-instruction_email_subject',
1144
		'global_offline_donation_email' => 'offline-donation-instruction_email_message',
1145
		'donation_subject'              => 'donation-receipt_email_subject',
1146
		'donation_receipt'              => 'donation-receipt_email_message',
1147
		'donation_notification_subject' => 'new-donation_email_subject',
1148
		'donation_notification'         => 'new-donation_email_message',
1149
		'admin_notice_emails'           => array(
1150
			'new-donation_recipient',
1151
			'new-offline-donation_recipient',
1152
			'new-donor-register_recipient',
1153
		),
1154
		'admin_notices'                 => 'new-donation_notification',
1155
	);
1156
1157
	foreach ( $settings as $old_setting => $new_setting ) {
1158
		// Do not update already modified
1159
		if ( ! is_array( $new_setting ) ) {
1160
			if ( array_key_exists( $new_setting, $all_setting ) || ! array_key_exists( $old_setting, $all_setting ) ) {
1161
				continue;
1162
			}
1163
		}
1164
1165
		switch ( $old_setting ) {
1166
			case 'admin_notices':
1167
				$notification_status = give_get_option( $old_setting, 'disabled' );
1168
1169
				give_update_option( $new_setting, $notification_status );
1170
				give_delete_option( $old_setting );
1171
				break;
1172
1173
			// @todo: Delete this option later ( version > 2.0 ) because we need this for backward compatibility give_get_admin_notice_emails.
1174
			case 'admin_notice_emails':
1175
				$recipients = give_get_admin_notice_emails();
0 ignored issues
show
Deprecated Code introduced by
The function give_get_admin_notice_emails() has been deprecated with message: 2.0

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
1176
1177
				foreach ( $new_setting as $setting ) {
0 ignored issues
show
Bug introduced by
The expression $new_setting of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
1178
					// bailout if setting already exist.
1179
					if ( array_key_exists( $setting, $all_setting ) ) {
1180
						continue;
1181
					}
1182
1183
					give_update_option( $setting, $recipients );
1184
				}
1185
				break;
1186
1187
			default:
1188
				give_update_option( $new_setting, give_get_option( $old_setting ) );
1189
				give_delete_option( $old_setting );
1190
		}
1191
	}
1192
}
1193
1194
1195
/**
1196
 * Upgrade form metadata for new metabox settings.
1197
 *
1198
 * @since  2.0
1199
 * @return void
1200
 */
1201
function give_v20_upgrades_form_metadata() {
1202
	if ( ! current_user_can( 'manage_give_settings' ) ) {
1203
		wp_die( esc_html__( 'You do not have permission to do Give upgrades.', 'give' ), esc_html__( 'Error', 'give' ), array(
1204
			'response' => 403,
1205
		) );
1206
	}
1207
1208
	ignore_user_abort( true );
1209
1210
	if ( ! give_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {
1211
		@set_time_limit( 0 );
1212
	}
1213
1214
	$step = isset( $_GET['step'] ) ? absint( $_GET['step'] ) : 1;
1215
1216
	// form query
1217
	$forms = new WP_Query( array(
1218
			'paged'          => $step,
1219
			'status'         => 'any',
1220
			'order'          => 'ASC',
1221
			'post_type'      => 'give_forms',
1222
			'posts_per_page' => 20,
1223
		)
1224
	);
1225
1226
	if ( $forms->have_posts() ) {
1227
		while ( $forms->have_posts() ) {
1228
			$forms->the_post();
1229
1230
			// Update offline instruction email notification status.
1231
			$offline_instruction_notification_status = get_post_meta( get_the_ID(), '_give_customize_offline_donations', true );
1232
			$offline_instruction_notification_status = give_is_setting_enabled( $offline_instruction_notification_status, array( 'enabled', 'global' ) )
1233
				? $offline_instruction_notification_status
1234
				: 'global';
1235
			update_post_meta( get_the_ID(), '_give_offline-donation-instruction_notification', $offline_instruction_notification_status );
1236
1237
			// Update offline instruction email message.
1238
			update_post_meta(
1239
				get_the_ID(),
1240
				'_give_offline-donation-instruction_email_message',
1241
				get_post_meta(
1242
					get_the_ID(),
1243
					// @todo: Delete this option later ( version > 2.0 ).
1244
					'_give_offline_donation_email',
1245
					true
1246
				)
1247
			);
1248
1249
			// Update offline instruction email subject.
1250
			update_post_meta(
1251
				get_the_ID(),
1252
				'_give_offline-donation-instruction_email_subject',
1253
				get_post_meta(
1254
					get_the_ID(),
1255
					// @todo: Delete this option later ( version > 2.0 ).
1256
					'_give_offline_donation_subject',
1257
					true
1258
				)
1259
			);
1260
1261
1262
		}// 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...
1263
1264
		wp_reset_postdata();
1265
1266
		// Forms found so upgrade them
1267
		$step ++;
1268
		$redirect = add_query_arg( array(
1269
			'page'         => 'give-upgrades',
1270
			'give-upgrade' => 'give_v20_upgrades_form_metadata',
1271
			'step'         => $step,
1272
		), admin_url( 'index.php' ) );
1273
		wp_redirect( $redirect );
1274
		exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_v20_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...
1275
1276
	} else {
1277
		// No more forms found, finish up.
1278
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
1279
		delete_option( 'give_doing_upgrade' );
1280
		give_set_upgrade_complete( 'v20_upgrades_form_metadata' );
1281
1282
		wp_redirect( admin_url() );
1283
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_v20_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...
1284
	}
1285
}
1286
1287
add_action( 'give_give_v20_upgrades_form_metadata', 'give_v20_upgrades_form_metadata' );
1288