Completed
Push — issues/1796 ( fe0f55...71eafd )
by Ravinder
17:33
created

upgrade-functions.php ➔ give_v20_logs_upgrades()   F

Complexity

Conditions 15
Paths 400

Size

Total Lines 94
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 60
nc 400
nop 0
dl 0
loc 94
rs 3.7313
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

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

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

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

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

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