Completed
Pull Request — master (#1412)
by Ravinder
17:25
created

upgrade-functions.php ➔ give_show_upgrade_notices()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 43
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

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