Completed
Push — backup/611 ( 661115 )
by Ravinder
1411:51 queued 1394:16
created

upgrade-functions.php ➔ give_v19_upgrades_email_setting()   B

Complexity

Conditions 9
Paths 13

Size

Total Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 13
nop 0
dl 0
loc 59
rs 7.3389
c 0
b 0
f 0

How to fix   Long Method   

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