Completed
Push — issues/1796 ( b53179...52862a )
by Ravinder
41:58 queued 22:00
created

upgrade-functions.php ➔ give_maybe_resume_upgrade()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
1301
		}// 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...
1302
1303
		wp_reset_postdata();
1304
1305
		// Forms found so upgrade them
1306
		$step ++;
1307
		$redirect = add_query_arg( array(
1308
			'page'         => 'give-upgrades',
1309
			'give-upgrade' => 'give_v20_logs_upgrades',
1310
			'step'         => $step,
1311
		), admin_url( 'index.php' ) );
1312
		wp_redirect( $redirect );
1313
		exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_v20_logs_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...
1314
1315
	} else {
1316
		// Delete terms and taxonomy.
1317
		$terms = get_terms( 'give_log_type', array( 'fields' => 'ids', 'hide_empty' => false ) );
1318
		if( ! empty( $terms ) ) {
1319
			foreach ( $terms as $term ) {
1320
				wp_delete_term( $term, 'give_log_type' );
1321
			}
1322
		}
1323
1324
		// Delete logs
1325
		$logIDs = get_posts( array(
1326
				'order'          => 'DESC',
1327
				'post_type'      => 'give_log',
1328
				'post_status'    => 'any',
1329
				'posts_per_page' => - 1,
1330
				'fields'         => 'ids',
1331
			)
1332
		);
1333
1334
		if ( ! empty( $logIDs ) ) {
1335
			foreach ( $logIDs as $log ) {
1336
				// Delete term relationship and posts.
1337
				wp_delete_object_term_relationships( $log, 'give_log_type' );
1338
				wp_delete_post( $log, true );
1339
			}
1340
		}
1341
1342
		unregister_taxonomy( 'give_log_type' );
1343
1344
		// Delete log cache.
1345
		Give()->logs->delete_cache();
1346
1347
		// No more forms found, finish up.
1348
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
1349
		delete_option( 'give_doing_upgrade' );
1350
		give_set_upgrade_complete( 'give_v20_logs_upgrades' );
1351
1352
		wp_redirect( admin_url() );
1353
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_v20_logs_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...
1354
	}
1355
}
1356
1357
add_action( 'give_give_v20_logs_upgrades', 'give_v20_logs_upgrades' );
1358