Test Failed
Push — release/1.8.17 ( baf467...e6f68c )
by Ravinder
05:06
created

upgrade-functions.php ➔ give_show_upgrade_notices()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 79
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 45
nc 1
nop 1
dl 0
loc 79
rs 8.8701
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
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;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
40
41
		case version_compare( $give_version, '1.7', '<' ) :
42
			give_v17_upgrades();
43
			$did_upgrade = true;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
44
45
		case version_compare( $give_version, '1.8', '<' ) :
46
			give_v18_upgrades();
47
			$did_upgrade = true;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
48
49
		case version_compare( $give_version, '1.8.7', '<' ) :
50
			give_v187_upgrades();
51
			$did_upgrade = true;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
52
53
		case version_compare( $give_version, '1.8.8', '<' ) :
54
			give_v188_upgrades();
55
			$did_upgrade = true;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
56
57
		case version_compare( $give_version, '1.8.9', '<' ) :
58
			give_v189_upgrades();
59
			$did_upgrade = true;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
60
61
		case version_compare( $give_version, '1.8.12', '<' ) :
62
			give_v1812_upgrades();
63
			$did_upgrade = true;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
64
65
		case version_compare( $give_version, '1.8.13', '<' ) :
66
			give_v1813_upgrades();
67
			$did_upgrade = true;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
68
69
		case version_compare( $give_version, '1.8.17', '<' ) :
70
			give_v1817_upgrades();
71
			$did_upgrade = true;
72
	}
73
74
	if ( $did_upgrade ) {
75
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
76
	}
77
}
78
79
add_action( 'admin_init', 'give_do_automatic_upgrades' );
80
add_action( 'give_upgrades', 'give_do_automatic_upgrades' );
81
82
/**
83
 * Display Upgrade Notices
84
 *
85
 * @since 1.0
86
 * @since 1.8.12 Update new update process code.
87
 *
88
 * @param Give_Updates $give_updates
89
 *
90
 * @return void
91
 */
92
function give_show_upgrade_notices( $give_updates ) {
93
	// v1.3.2 Upgrades
94
	$give_updates->register(
95
		array(
96
			'id'       => 'upgrade_give_payment_customer_id',
97
			'version'  => '1.3.2',
98
			'callback' => 'give_v132_upgrade_give_payment_customer_id',
99
		)
100
	);
101
102
	// v1.3.4 Upgrades ensure the user has gone through 1.3.4.
103
	$give_updates->register(
104
		array(
105
			'id'       => 'upgrade_give_offline_status',
106
			'depend'   => 'upgrade_give_payment_customer_id',
107
			'version'  => '1.3.4',
108
			'callback' => 'give_v134_upgrade_give_offline_status',
109
		)
110
	);
111
112
	// v1.8 form metadata upgrades.
113
	$give_updates->register(
114
		array(
115
			'id'       => 'v18_upgrades_form_metadata',
116
			'version'  => '1.8',
117
			'callback' => 'give_v18_upgrades_form_metadata',
118
		)
119
	);
120
121
	// v1.8.9 Upgrades
122
	$give_updates->register(
123
		array(
124
			'id'       => 'v189_upgrades_levels_post_meta',
125
			'version'  => '1.8.9',
126
			'callback' => 'give_v189_upgrades_levels_post_meta_callback',
127
		)
128
	);
129
130
	// v1.8.12 Upgrades
131
	$give_updates->register(
132
		array(
133
			'id'       => 'v1812_update_amount_values',
134
			'version'  => '1.8.12',
135
			'callback' => 'give_v1812_update_amount_values_callback',
136
		)
137
	);
138
139
	// v1.8.12 Upgrades
140
	$give_updates->register(
141
		array(
142
			'id'       => 'v1812_update_donor_purchase_values',
143
			'version'  => '1.8.12',
144
			'callback' => 'give_v1812_update_donor_purchase_value_callback',
145
		)
146
	);
147
148
	// v1.8.13 Upgrades for donor
149
	$give_updates->register(
150
		array(
151
			'id'       => 'v1813_update_donor_user_roles',
152
			'version'  => '1.8.13',
153
			'callback' => 'give_v1813_update_donor_user_roles_callback',
154
		)
155
	);
156
157
	// v1.8.17 Upgrades for donations.
158
	$give_updates->register( array(
159
		'id'       => 'v1817_update_donation_iranian_currency_code',
160
		'version'  => '1.8.17',
161
		'callback' => 'give_v1817_update_donation_iranian_currency_code',
162
	) );
163
164
	// v1.8.17 Upgrades for cleanup of user roles.
165
	$give_updates->register( array(
166
		'id'       => 'v1817_cleanup_user_roles',
167
		'version'  => '1.8.17',
168
		'callback' => 'give_v1817_cleanup_user_roles',
169
	) );
170
}
171
172
add_action( 'give_register_updates', 'give_show_upgrade_notices' );
173
174
/**
175
 * Triggers all upgrade functions
176
 *
177
 * This function is usually triggered via AJAX
178
 *
179
 * @since 1.0
180
 * @return void
181
 */
182
function give_trigger_upgrades() {
183
184 View Code Duplication
	if ( ! current_user_can( 'manage_give_settings' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
185
		wp_die( esc_html__( 'You do not have permission to do Give upgrades.', 'give' ), esc_html__( 'Error', 'give' ), array(
186
			'response' => 403,
187
		) );
188
	}
189
190
	$give_version = get_option( 'give_version' );
191
192
	if ( ! $give_version ) {
193
		// 1.0 is the first version to use this option so we must add it.
194
		$give_version = '1.0';
195
		add_option( 'give_version', $give_version );
196
	}
197
198
	update_option( 'give_version', GIVE_VERSION );
199
	delete_option( 'give_doing_upgrade' );
200
201
	if ( DOING_AJAX ) {
202
		die( 'complete' );
203
	} // End if().
204
}
205
206
add_action( 'wp_ajax_give_trigger_upgrades', 'give_trigger_upgrades' );
207
208
209
/**
210
 * Upgrades the
211
 *
212
 * Standardizes the discrepancies between two metakeys `_give_payment_customer_id` and `_give_payment_donor_id`
213
 *
214
 * @since      1.3.2
215
 */
216
function give_v132_upgrade_give_payment_customer_id() {
217
	global $wpdb;
218
219
	/* @var Give_Updates $give_updates */
220
	$give_updates = Give_Updates::get_instance();
221
222 View Code Duplication
	if ( ! current_user_can( 'manage_give_settings' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
223
		wp_die( esc_html__( 'You do not have permission to do Give upgrades.', 'give' ), esc_html__( 'Error', 'give' ), array(
224
			'response' => 403,
225
		) );
226
	}
227
228
	ignore_user_abort( true );
229
230
	if ( ! give_is_func_disabled( 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {
231
		@set_time_limit( 0 );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Coding Style introduced by
Silencing errors is discouraged
Loading history...
232
	}
233
234
	// UPDATE DB METAKEYS.
235
	$sql   = "UPDATE $wpdb->postmeta SET meta_key = '_give_payment_customer_id' WHERE meta_key = '_give_payment_donor_id'";
236
	$query = $wpdb->query( $sql );
0 ignored issues
show
Unused Code introduced by
$query is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
237
238
	$give_updates->percentage = 100;
0 ignored issues
show
Documentation Bug introduced by
It seems like 100 of type integer is incompatible with the declared type array of property $percentage.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
239
	give_set_upgrade_complete( 'upgrade_give_payment_customer_id' );
240
}
241
242
243
/**
244
 * Upgrades the Offline Status
245
 *
246
 * Reverses the issue where offline donations in "pending" status where inappropriately marked as abandoned
247
 *
248
 * @since      1.3.4
249
 */
250
function give_v134_upgrade_give_offline_status() {
251
	global $wpdb;
252
253
	/* @var Give_Updates $give_updates */
254
	$give_updates = Give_Updates::get_instance();
255
256
	// Get abandoned offline payments.
257
	$select = "SELECT ID FROM $wpdb->posts p ";
258
	$join   = "LEFT JOIN $wpdb->postmeta m ON p.ID = m.post_id ";
259
	$where  = "WHERE p.post_type = 'give_payment' ";
260
	$where  .= "AND ( p.post_status = 'abandoned' )";
261
	$where  .= "AND ( m.meta_key = '_give_payment_gateway' AND m.meta_value = 'offline' )";
262
263
	$sql            = $select . $join . $where;
264
	$found_payments = $wpdb->get_col( $sql );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
265
266
	foreach ( $found_payments as $payment ) {
267
268
		// Only change ones marked abandoned since our release last week because the admin may have marked some abandoned themselves.
269
		$modified_time = get_post_modified_time( 'U', false, $payment );
270
271
		// 1450124863 =  12/10/2015 20:42:25.
272
		if ( $modified_time >= 1450124863 ) {
273
274
			give_update_payment_status( $payment, 'pending' );
275
276
		}
277
	}
278
279
	$give_updates->percentage = 100;
0 ignored issues
show
Documentation Bug introduced by
It seems like 100 of type integer is incompatible with the declared type array of property $percentage.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
280
	give_set_upgrade_complete( 'upgrade_give_offline_status' );
281
}
282
283
284
/**
285
 * Cleanup User Roles
286
 *
287
 * This upgrade routine removes unused roles and roles with typos
288
 *
289
 * @since      1.5.2
290
 */
291
function give_v152_cleanup_users() {
292
293
	$give_version = get_option( 'give_version' );
294
295
	if ( ! $give_version ) {
296
		// 1.0 is the first version to use this option so we must add it.
297
		$give_version = '1.0';
298
	}
299
300
	$give_version = preg_replace( '/[^0-9.].*/', '', $give_version );
301
302
	// v1.5.2 Upgrades
303
	if ( version_compare( $give_version, '1.5.2', '<' ) || ! give_has_upgrade_completed( 'upgrade_give_user_caps_cleanup' ) ) {
304
305
		// Delete all caps with "ss".
306
		// Also delete all unused "campaign" roles.
307
		$delete_caps = array(
308
			'delete_give_formss',
309
			'delete_others_give_formss',
310
			'delete_private_give_formss',
311
			'delete_published_give_formss',
312
			'read_private_forms',
313
			'edit_give_formss',
314
			'edit_others_give_formss',
315
			'edit_private_give_formss',
316
			'edit_published_give_formss',
317
			'publish_give_formss',
318
			'read_private_give_formss',
319
			'assign_give_campaigns_terms',
320
			'delete_give_campaigns',
321
			'delete_give_campaigns_terms',
322
			'delete_give_campaignss',
323
			'delete_others_give_campaignss',
324
			'delete_private_give_campaignss',
325
			'delete_published_give_campaignss',
326
			'edit_give_campaigns',
327
			'edit_give_campaigns_terms',
328
			'edit_give_campaignss',
329
			'edit_others_give_campaignss',
330
			'edit_private_give_campaignss',
331
			'edit_published_give_campaignss',
332
			'manage_give_campaigns_terms',
333
			'publish_give_campaignss',
334
			'read_give_campaigns',
335
			'read_private_give_campaignss',
336
			'view_give_campaigns_stats',
337
			'delete_give_paymentss',
338
			'delete_others_give_paymentss',
339
			'delete_private_give_paymentss',
340
			'delete_published_give_paymentss',
341
			'edit_give_paymentss',
342
			'edit_others_give_paymentss',
343
			'edit_private_give_paymentss',
344
			'edit_published_give_paymentss',
345
			'publish_give_paymentss',
346
			'read_private_give_paymentss',
347
		);
348
349
		global $wp_roles;
350
		foreach ( $delete_caps as $cap ) {
351
			foreach ( array_keys( $wp_roles->roles ) as $role ) {
352
				$wp_roles->remove_cap( $role, $cap );
353
			}
354
		}
355
356
		// Create Give plugin roles.
357
		$roles = new Give_Roles();
358
		$roles->add_roles();
359
		$roles->add_caps();
360
361
		// The Update Ran.
362
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
363
		give_set_upgrade_complete( 'upgrade_give_user_caps_cleanup' );
364
		delete_option( 'give_doing_upgrade' );
365
366
	}// End if().
367
368
}
369
370
add_action( 'admin_init', 'give_v152_cleanup_users' );
371
372
/**
373
 * 1.6 Upgrade routine to create the customer meta table.
374
 *
375
 * @since  1.6
376
 * @return void
377
 */
378
function give_v16_upgrades() {
379
	// Create the donor databases.
380
	$donors_db = new Give_DB_Donors();
381
	$donors_db->create_table();
382
	$donor_meta = new Give_DB_Donor_Meta();
383
	$donor_meta->create_table();
384
}
385
386
/**
387
 * 1.7 Upgrades.
388
 *
389
 * a. Update license api data for plugin addons.
390
 * b. Cleanup user roles.
391
 *
392
 * @since  1.7
393
 * @return void
394
 */
395
function give_v17_upgrades() {
396
	// Upgrade license data.
397
	give_v17_upgrade_addon_license_data();
398
	give_v17_cleanup_roles();
399
}
400
401
/**
402
 * Upgrade license data
403
 *
404
 * @since 1.7
405
 */
406
function give_v17_upgrade_addon_license_data() {
407
	$give_options = give_get_settings();
408
409
	$api_url = 'https://givewp.com/give-sl-api/';
410
411
	// Get addons license key.
412
	$addons = array();
413
	foreach ( $give_options as $key => $value ) {
414
		if ( false !== strpos( $key, '_license_key' ) ) {
415
			$addons[ $key ] = $value;
416
		}
417
	}
418
419
	// Bailout: We do not have any addon license data to upgrade.
420
	if ( empty( $addons ) ) {
421
		return false;
422
	}
423
424
	foreach ( $addons as $key => $addon_license ) {
425
426
		// Get addon shortname.
427
		$shortname = str_replace( '_license_key', '', $key );
428
429
		// Addon license option name.
430
		$addon_license_option_name = $shortname . '_license_active';
431
432
		// bailout if license is empty.
433
		if ( empty( $addon_license ) ) {
434
			delete_option( $addon_license_option_name );
435
			continue;
436
		}
437
438
		// Get addon name.
439
		$addon_name       = array();
440
		$addon_name_parts = explode( '_', str_replace( 'give_', '', $shortname ) );
441
		foreach ( $addon_name_parts as $name_part ) {
442
443
			// Fix addon name
444
			switch ( $name_part ) {
445
				case 'authorizenet' :
446
					$name_part = 'authorize.net';
447
					break;
448
			}
449
450
			$addon_name[] = ucfirst( $name_part );
451
		}
452
453
		$addon_name = implode( ' ', $addon_name );
454
455
		// Data to send to the API.
456
		$api_params = array(
457
			'edd_action' => 'activate_license', // never change from "edd_" to "give_"!
458
			'license'    => $addon_license,
459
			'item_name'  => urlencode( $addon_name ),
460
			'url'        => home_url(),
461
		);
462
463
		// Call the API.
464
		$response = wp_remote_post(
465
			$api_url,
466
			array(
467
				'timeout'   => 15,
468
				'sslverify' => false,
469
				'body'      => $api_params,
470
			)
471
		);
472
473
		// Make sure there are no errors.
474
		if ( is_wp_error( $response ) ) {
475
			delete_option( $addon_license_option_name );
476
			continue;
477
		}
478
479
		// Tell WordPress to look for updates.
480
		set_site_transient( 'update_plugins', null );
481
482
		// Decode license data.
483
		$license_data = json_decode( wp_remote_retrieve_body( $response ) );
484
		update_option( $addon_license_option_name, $license_data );
485
	}// End foreach().
486
}
487
488
489
/**
490
 * Cleanup User Roles.
491
 *
492
 * This upgrade routine removes unused roles and roles with typos.
493
 *
494
 * @since      1.7
495
 */
496
function give_v17_cleanup_roles() {
497
498
	// Delete all caps with "_give_forms_" and "_give_payments_".
499
	// These roles have no usage; the proper is singular.
500
	$delete_caps = array(
501
		'view_give_forms_stats',
502
		'delete_give_forms_terms',
503
		'assign_give_forms_terms',
504
		'edit_give_forms_terms',
505
		'manage_give_forms_terms',
506
		'view_give_payments_stats',
507
		'manage_give_payments_terms',
508
		'edit_give_payments_terms',
509
		'assign_give_payments_terms',
510
		'delete_give_payments_terms',
511
	);
512
513
	global $wp_roles;
514
	foreach ( $delete_caps as $cap ) {
515
		foreach ( array_keys( $wp_roles->roles ) as $role ) {
516
			$wp_roles->remove_cap( $role, $cap );
517
		}
518
	}
519
520
	// Set roles again.
521
	$roles = new Give_Roles();
522
	$roles->add_roles();
523
	$roles->add_caps();
524
525
}
526
527
/**
528
 * 1.8 Upgrades.
529
 *
530
 * a. Upgrade checkbox settings to radio button settings.
531
 * a. Update form meta for new metabox settings.
532
 *
533
 * @since  1.8
534
 * @return void
535
 */
536
function give_v18_upgrades() {
537
	// Upgrade checkbox settings to radio button settings.
538
	give_v18_upgrades_core_setting();
539
}
540
541
/**
542
 * Upgrade core settings.
543
 *
544
 * @since  1.8
545
 * @return void
546
 */
547
function give_v18_upgrades_core_setting() {
548
	// Core settings which changes from checkbox to radio.
549
	$core_setting_names = array_merge(
550
		array_keys( give_v18_renamed_core_settings() ),
551
		array(
552
			'uninstall_on_delete',
553
			'scripts_footer',
554
			'test_mode',
555
			'email_access',
556
			'terms',
557
			'give_offline_donation_enable_billing_fields',
558
		)
559
	);
560
561
	// Bailout: If not any setting define.
562
	if ( $give_settings = get_option( 'give_settings' ) ) {
563
564
		$setting_changed = false;
565
566
		// Loop: check each setting field.
567
		foreach ( $core_setting_names as $setting_name ) {
568
			// New setting name.
569
			$new_setting_name = preg_replace( '/^(enable_|disable_)/', '', $setting_name );
570
571
			// Continue: If setting already set.
572
			if (
573
				array_key_exists( $new_setting_name, $give_settings )
574
				&& in_array( $give_settings[ $new_setting_name ], array( 'enabled', 'disabled' ) )
575
			) {
576
				continue;
577
			}
578
579
			// Set checkbox value to radio value.
580
			$give_settings[ $setting_name ] = ( ! empty( $give_settings[ $setting_name ] ) && 'on' === $give_settings[ $setting_name ] ? 'enabled' : 'disabled' );
581
582
			// @see https://github.com/WordImpress/Give/issues/1063.
583
			if ( false !== strpos( $setting_name, 'disable_' ) ) {
584
585
				$give_settings[ $new_setting_name ] = ( give_is_setting_enabled( $give_settings[ $setting_name ] ) ? 'disabled' : 'enabled' );
586
			} elseif ( false !== strpos( $setting_name, 'enable_' ) ) {
587
588
				$give_settings[ $new_setting_name ] = ( give_is_setting_enabled( $give_settings[ $setting_name ] ) ? 'enabled' : 'disabled' );
589
			}
590
591
			// Tell bot to update core setting to db.
592
			if ( ! $setting_changed ) {
593
				$setting_changed = true;
594
			}
595
		}
596
597
		// Update setting only if they changed.
598
		if ( $setting_changed ) {
599
			update_option( 'give_settings', $give_settings );
600
		}
601
	}// End if().
602
603
	give_set_upgrade_complete( 'v18_upgrades_core_setting' );
604
}
605
606
/**
607
 * Upgrade form metadata for new metabox settings.
608
 *
609
 * @since  1.8
610
 * @return void
611
 */
612
function give_v18_upgrades_form_metadata() {
613
	/* @var Give_Updates $give_updates */
614
	$give_updates = Give_Updates::get_instance();
615
616
	// form query
617
	$forms = new WP_Query( array(
618
			'paged'          => $give_updates->step,
619
			'status'         => 'any',
620
			'order'          => 'ASC',
621
			'post_type'      => 'give_forms',
622
			'posts_per_page' => 20,
623
		)
624
	);
625
626
	if ( $forms->have_posts() ) {
627
		$give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 20 ) );
628
629
		while ( $forms->have_posts() ) {
630
			$forms->the_post();
631
632
			// Form content.
633
			// Note in version 1.8 display content setting split into display content and content placement setting.
634
			// You can delete _give_content_option in future.
635
			$show_content = give_get_meta( get_the_ID(), '_give_content_option', true );
636
			if ( $show_content && ! give_get_meta( get_the_ID(), '_give_display_content', true ) ) {
637
				$field_value = ( 'none' !== $show_content ? 'enabled' : 'disabled' );
638
				give_update_meta( get_the_ID(), '_give_display_content', $field_value );
639
640
				$field_value = ( 'none' !== $show_content ? $show_content : 'give_pre_form' );
641
				give_update_meta( get_the_ID(), '_give_content_placement', $field_value );
642
			}
643
644
			// "Disable" Guest Donation. Checkbox.
645
			// See: https://github.com/WordImpress/Give/issues/1470.
646
			$guest_donation        = give_get_meta( get_the_ID(), '_give_logged_in_only', true );
647
			$guest_donation_newval = ( in_array( $guest_donation, array( 'yes', 'on' ) ) ? 'disabled' : 'enabled' );
648
			give_update_meta( get_the_ID(), '_give_logged_in_only', $guest_donation_newval );
649
650
			// Offline Donations.
651
			// See: https://github.com/WordImpress/Give/issues/1579.
652
			$offline_donation = give_get_meta( get_the_ID(), '_give_customize_offline_donations', true );
653
			if ( 'no' === $offline_donation ) {
654
				$offline_donation_newval = 'global';
655
			} elseif ( 'yes' === $offline_donation ) {
656
				$offline_donation_newval = 'enabled';
657
			} else {
658
				$offline_donation_newval = 'disabled';
659
			}
660
			give_update_meta( get_the_ID(), '_give_customize_offline_donations', $offline_donation_newval );
661
662
			// Convert yes/no setting field to enabled/disabled.
663
			$form_radio_settings = array(
664
				// Custom Amount.
665
				'_give_custom_amount',
666
667
				// Donation Gaol.
668
				'_give_goal_option',
669
670
				// Close Form.
671
				'_give_close_form_when_goal_achieved',
672
673
				// Term & conditions.
674
				'_give_terms_option',
675
676
				// Billing fields.
677
				'_give_offline_donation_enable_billing_fields_single',
678
			);
679
680
			foreach ( $form_radio_settings as $meta_key ) {
681
				// Get value.
682
				$field_value = give_get_meta( get_the_ID(), $meta_key, true );
683
684
				// Convert meta value only if it is in yes/no/none.
685
				if ( in_array( $field_value, array( 'yes', 'on', 'no', 'none' ) ) ) {
686
687
					$field_value = ( in_array( $field_value, array( 'yes', 'on' ) ) ? 'enabled' : 'disabled' );
688
					give_update_meta( get_the_ID(), $meta_key, $field_value );
689
				}
690
			}
691
		}// End while().
692
693
		wp_reset_postdata();
694
695
	} else {
696
		// No more forms found, finish up.
697
		give_set_upgrade_complete( 'v18_upgrades_form_metadata' );
698
	}
699
}
700
701
702
/**
703
 * Get list of core setting renamed in version 1.8.
704
 *
705
 * @since  1.8
706
 * @return array
707
 */
708
function give_v18_renamed_core_settings() {
709
	return array(
710
		'disable_paypal_verification' => 'paypal_verification',
711
		'disable_css'                 => 'css',
712
		'disable_welcome'             => 'welcome',
713
		'disable_forms_singular'      => 'forms_singular',
714
		'disable_forms_archives'      => 'forms_archives',
715
		'disable_forms_excerpt'       => 'forms_excerpt',
716
		'disable_form_featured_img'   => 'form_featured_img',
717
		'disable_form_sidebar'        => 'form_sidebar',
718
		'disable_admin_notices'       => 'admin_notices',
719
		'disable_the_content_filter'  => 'the_content_filter',
720
		'enable_floatlabels'          => 'floatlabels',
721
		'enable_categories'           => 'categories',
722
		'enable_tags'                 => 'tags',
723
	);
724
}
725
726
727
/**
728
 * Upgrade core settings.
729
 *
730
 * @since  1.8.7
731
 * @return void
732
 */
733
function give_v187_upgrades() {
734
	global $wpdb;
735
736
	/**
737
	 * Upgrade 1: Remove stat and cache transients.
738
	 */
739
	$cached_options = $wpdb->get_col(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
740
		$wpdb->prepare(
741
			"
742
					SELECT *
743
					FROM {$wpdb->options}
744
					WHERE (
745
					option_name LIKE %s
746
					OR option_name LIKE %s
747
					OR option_name LIKE %s
748
					OR option_name LIKE %s
749
					OR option_name LIKE %s
750
					OR option_name LIKE %s
751
					OR option_name LIKE %s
752
					OR option_name LIKE %s
753
					OR option_name LIKE %s
754
					OR option_name LIKE %s
755
					OR option_name LIKE %s
756
					OR option_name LIKE %s
757
					OR option_name LIKE %s
758
					)
759
					",
760
			array(
761
				'%_transient_give_stats_%',
762
				'give_cache%',
763
				'%_transient_give_add_ons_feed%',
764
				'%_transient__give_ajax_works' .
765
				'%_transient_give_total_api_keys%',
766
				'%_transient_give_i18n_give_promo_hide%',
767
				'%_transient_give_contributors%',
768
				'%_transient_give_estimated_monthly_stats%',
769
				'%_transient_give_earnings_total%',
770
				'%_transient_give_i18n_give_%',
771
				'%_transient__give_installed%',
772
				'%_transient__give_activation_redirect%',
773
				'%_transient__give_hide_license_notices_shortly_%',
774
				'%give_income_total%',
775
			)
776
		),
777
		1
778
	);
779
780
	// User related transients.
781
	$user_apikey_options = $wpdb->get_results(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
782
		$wpdb->prepare(
783
			"SELECT user_id, meta_key
784
			FROM $wpdb->usermeta
0 ignored issues
show
introduced by
Usage of users/usermeta tables is highly discouraged in VIP context, For storing user additional user metadata, you should look at User Attributes.
Loading history...
785
			WHERE meta_value=%s",
786
			'give_user_public_key'
787
		),
788
		ARRAY_A
789
	);
790
791
	if ( ! empty( $user_apikey_options ) ) {
792
		foreach ( $user_apikey_options as $user ) {
793
			$cached_options[] = '_transient_' . md5( 'give_api_user_' . $user['meta_key'] );
794
			$cached_options[] = '_transient_' . md5( 'give_api_user_public_key' . $user['user_id'] );
795
			$cached_options[] = '_transient_' . md5( 'give_api_user_secret_key' . $user['user_id'] );
796
		}
797
	}
798
799
	if ( ! empty( $cached_options ) ) {
800
		foreach ( $cached_options as $option ) {
801 View Code Duplication
			switch ( true ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
802
				case ( false !== strpos( $option, 'transient' ) ):
803
					$option = str_replace( '_transient_', '', $option );
804
					delete_transient( $option );
805
					break;
806
807
				default:
808
					delete_option( $option );
809
			}
810
		}
811
	}
812
}
813
814
/**
815
 * Update Capabilities for Give_Worker User Role.
816
 *
817
 * This upgrade routine will update access rights for Give_Worker User Role.
818
 *
819
 * @since      1.8.8
820
 */
821
function give_v188_upgrades() {
822
823
	global $wp_roles;
824
825
	// Get the role object.
826
	$give_worker = get_role( 'give_worker' );
827
828
	// A list of capabilities to add for give workers.
829
	$caps_to_add = array(
830
		'edit_posts',
831
		'edit_pages',
832
	);
833
834
	foreach ( $caps_to_add as $cap ) {
835
		// Add the capability.
836
		$give_worker->add_cap( $cap );
837
	}
838
839
}
840
841
/**
842
 * Update Post meta for minimum and maximum amount for multi level donation forms
843
 *
844
 * This upgrade routine adds post meta for give_forms CPT for multi level donation form.
845
 *
846
 * @since      1.8.9
847
 */
848
function give_v189_upgrades_levels_post_meta_callback() {
849
	/* @var Give_Updates $give_updates */
850
	$give_updates = Give_Updates::get_instance();
851
852
	// form query.
853
	$donation_forms = new WP_Query( array(
854
			'paged'          => $give_updates->step,
855
			'status'         => 'any',
856
			'order'          => 'ASC',
857
			'post_type'      => 'give_forms',
858
			'posts_per_page' => 20,
859
		)
860
	);
861
862
	if ( $donation_forms->have_posts() ) {
863
		$give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
864
865
		while ( $donation_forms->have_posts() ) {
866
			$donation_forms->the_post();
867
			$form_id = get_the_ID();
868
869
			// Remove formatting from _give_set_price.
870
			update_post_meta(
871
				$form_id,
872
				'_give_set_price',
873
				give_sanitize_amount( get_post_meta( $form_id, '_give_set_price', true ) )
874
			);
875
876
			// Remove formatting from _give_custom_amount_minimum.
877
			update_post_meta(
878
				$form_id,
879
				'_give_custom_amount_minimum',
880
				give_sanitize_amount( get_post_meta( $form_id, '_give_custom_amount_minimum', true ) )
881
			);
882
883
			// Bailout.
884
			if ( 'set' === get_post_meta( $form_id, '_give_price_option', true ) ) {
885
				continue;
886
			}
887
888
			$donation_levels = get_post_meta( $form_id, '_give_donation_levels', true );
889
890
			if ( ! empty( $donation_levels ) ) {
891
892
				foreach ( $donation_levels as $index => $donation_level ) {
893
					if ( isset( $donation_level['_give_amount'] ) ) {
894
						$donation_levels[ $index ]['_give_amount'] = give_sanitize_amount( $donation_level['_give_amount'] );
895
					}
896
				}
897
898
				update_post_meta( $form_id, '_give_donation_levels', $donation_levels );
899
900
				$donation_levels_amounts = wp_list_pluck( $donation_levels, '_give_amount' );
901
902
				$min_amount = min( $donation_levels_amounts );
903
				$max_amount = max( $donation_levels_amounts );
904
905
				// Set Minimum and Maximum amount for Multi Level Donation Forms
906
				give_update_meta( $form_id, '_give_levels_minimum_amount', $min_amount ? give_sanitize_amount( $min_amount ) : 0 );
907
				give_update_meta( $form_id, '_give_levels_maximum_amount', $max_amount ? give_sanitize_amount( $max_amount ) : 0 );
908
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
909
910
		}
911
912
		/* Restore original Post Data */
913
		wp_reset_postdata();
914
	} else {
915
		// The Update Ran.
916
		give_set_upgrade_complete( 'v189_upgrades_levels_post_meta' );
917
	}
918
919
}
920
921
922
/**
923
 * Give version 1.8.9 upgrades
924
 *
925
 * @since      1.8.9
926
 */
927
function give_v189_upgrades() {
928
	/**
929
	 * 1. Remove user license related notice show blocked ( Give_Notice will handle )
930
	 */
931
	global $wpdb;
932
933
	// Delete permanent notice blocker.
934
	$wpdb->query(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
935
		$wpdb->prepare(
936
			"
937
					DELETE FROM $wpdb->usermeta
0 ignored issues
show
introduced by
Usage of users/usermeta tables is highly discouraged in VIP context, For storing user additional user metadata, you should look at User Attributes.
Loading history...
938
					WHERE meta_key
939
					LIKE '%%%s%%'
940
					",
941
			'_give_hide_license_notices_permanently'
942
		)
943
	);
944
945
	// Delete short notice blocker.
946
	$wpdb->query(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
947
		$wpdb->prepare(
948
			"
949
					DELETE FROM $wpdb->options
950
					WHERE option_name
951
					LIKE '%%%s%%'
952
					",
953
			'__give_hide_license_notices_shortly_'
954
		)
955
	);
956
957
}
958
959
960
/**
961
 * Give version 1.8.9 upgrades
962
 *
963
 * @since 1.8.9
964
 */
965
function give_v1812_upgrades() {
966
	/**
967
	 * Validate number format settings.
968
	 */
969
	$give_settings        = give_get_settings();
970
	$give_setting_updated = false;
971
972
	if ( $give_settings['thousands_separator'] === $give_settings['decimal_separator'] ) {
973
		$give_settings['number_decimals']   = 0;
974
		$give_settings['decimal_separator'] = '';
975
		$give_setting_updated               = true;
976
977
	} elseif ( empty( $give_settings['decimal_separator'] ) ) {
978
		$give_settings['number_decimals'] = 0;
979
		$give_setting_updated             = true;
980
981
	} elseif ( 6 < absint( $give_settings['number_decimals'] ) ) {
982
		$give_settings['number_decimals'] = 5;
983
		$give_setting_updated             = true;
984
	}
985
986
	if ( $give_setting_updated ) {
987
		update_option( 'give_settings', $give_settings );
988
	}
989
}
990
991
992
/**
993
 * Give version 1.8.12 update
994
 *
995
 * Standardized amount values to six decimal
996
 *
997
 * @see        https://github.com/WordImpress/Give/issues/1849#issuecomment-315128602
998
 *
999
 * @since      1.8.12
1000
 */
1001
function give_v1812_update_amount_values_callback() {
1002
	/* @var Give_Updates $give_updates */
1003
	$give_updates = Give_Updates::get_instance();
1004
1005
	// form query.
1006
	$donation_forms = new WP_Query( array(
1007
			'paged'          => $give_updates->step,
1008
			'status'         => 'any',
1009
			'order'          => 'ASC',
1010
			'post_type'      => array( 'give_forms', 'give_payment' ),
1011
			'posts_per_page' => 20,
1012
		)
1013
	);
1014
1015
	if ( $donation_forms->have_posts() ) {
1016
		$give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
1017
1018
		while ( $donation_forms->have_posts() ) {
1019
			$donation_forms->the_post();
1020
			global $post;
1021
1022
			$meta = get_post_meta( $post->ID );
1023
1024
			switch ( $post->post_type ) {
1025
				case 'give_forms':
1026
					// _give_set_price.
1027
					if ( ! empty( $meta['_give_set_price'][0] ) ) {
1028
						update_post_meta( $post->ID, '_give_set_price', give_sanitize_amount_for_db( $meta['_give_set_price'][0] ) );
1029
					}
1030
1031
					// _give_custom_amount_minimum.
1032
					if ( ! empty( $meta['_give_custom_amount_minimum'][0] ) ) {
1033
						update_post_meta( $post->ID, '_give_custom_amount_minimum', give_sanitize_amount_for_db( $meta['_give_custom_amount_minimum'][0] ) );
1034
					}
1035
1036
					// _give_levels_minimum_amount.
1037
					if ( ! empty( $meta['_give_levels_minimum_amount'][0] ) ) {
1038
						update_post_meta( $post->ID, '_give_levels_minimum_amount', give_sanitize_amount_for_db( $meta['_give_levels_minimum_amount'][0] ) );
1039
					}
1040
1041
					// _give_levels_maximum_amount.
1042
					if ( ! empty( $meta['_give_levels_maximum_amount'][0] ) ) {
1043
						update_post_meta( $post->ID, '_give_levels_maximum_amount', give_sanitize_amount_for_db( $meta['_give_levels_maximum_amount'][0] ) );
1044
					}
1045
1046
					// _give_set_goal.
1047
					if ( ! empty( $meta['_give_set_goal'][0] ) ) {
1048
						update_post_meta( $post->ID, '_give_set_goal', give_sanitize_amount_for_db( $meta['_give_set_goal'][0] ) );
1049
					}
1050
1051
					// _give_form_earnings.
1052
					if ( ! empty( $meta['_give_form_earnings'][0] ) ) {
1053
						update_post_meta( $post->ID, '_give_form_earnings', give_sanitize_amount_for_db( $meta['_give_form_earnings'][0] ) );
1054
					}
1055
1056
					// _give_custom_amount_minimum.
1057
					if ( ! empty( $meta['_give_donation_levels'][0] ) ) {
1058
						$donation_levels = unserialize( $meta['_give_donation_levels'][0] );
1059
1060
						foreach ( $donation_levels as $index => $level ) {
1061
							if ( empty( $level['_give_amount'] ) ) {
1062
								continue;
1063
							}
1064
1065
							$donation_levels[ $index ]['_give_amount'] = give_sanitize_amount_for_db( $level['_give_amount'] );
1066
						}
1067
1068
						$meta['_give_donation_levels'] = $donation_levels;
1069
						update_post_meta( $post->ID, '_give_donation_levels', $meta['_give_donation_levels'] );
1070
					}
1071
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
1072
1073
					break;
1074
1075
				case 'give_payment':
1076
					// _give_payment_total.
1077
					if ( ! empty( $meta['_give_payment_total'][0] ) ) {
1078
						update_post_meta( $post->ID, '_give_payment_total', give_sanitize_amount_for_db( $meta['_give_payment_total'][0] ) );
1079
					}
1080
1081
					break;
1082
			}
1083
		}
1084
1085
		/* Restore original Post Data */
1086
		wp_reset_postdata();
1087
	} else {
1088
		// The Update Ran.
1089
		give_set_upgrade_complete( 'v1812_update_amount_values' );
1090
	}
1091
}
1092
1093
1094
/**
1095
 * Give version 1.8.12 update
1096
 *
1097
 * Standardized amount values to six decimal for donor
1098
 *
1099
 * @see        https://github.com/WordImpress/Give/issues/1849#issuecomment-315128602
1100
 *
1101
 * @since      1.8.12
1102
 */
1103
function give_v1812_update_donor_purchase_value_callback() {
1104
	/* @var Give_Updates $give_updates */
1105
	$give_updates = Give_Updates::get_instance();
1106
	$offset       = 1 === $give_updates->step ? 0 : $give_updates->step * 20;
1107
1108
	// form query.
1109
	$donors = Give()->donors->get_donors( array(
1110
			'number' => 20,
1111
			'offset' => $offset,
1112
		)
1113
	);
1114
1115
	if ( ! empty( $donors ) ) {
1116
		$give_updates->set_percentage( Give()->donors->count(), ( $give_updates->step * 20 ) );
1117
1118
		/* @var Object $donor */
1119
		foreach ( $donors as $donor ) {
1120
			Give()->donors->update( $donor->id, array( 'purchase_value' => give_sanitize_amount_for_db( $donor->purchase_value ) ) );
1121
		}
1122
	} else {
1123
		// The Update Ran.
1124
		give_set_upgrade_complete( 'v1812_update_donor_purchase_values' );
1125
	}
1126
}
1127
1128
/**
1129
 * Upgrade routine for updating user roles for existing donors.
1130
 *
1131
 * @since 1.8.13
1132
 */
1133
function give_v1813_update_donor_user_roles_callback() {
1134
	/* @var Give_Updates $give_updates */
1135
	$give_updates = Give_Updates::get_instance();
1136
	$offset       = 1 === $give_updates->step ? 0 : $give_updates->step * 20;
1137
1138
	// Fetch all the existing donors.
1139
	$donors = Give()->donors->get_donors( array(
1140
			'number' => 20,
1141
			'offset' => $offset,
1142
		)
1143
	);
1144
1145
	if ( ! empty( $donors ) ) {
1146
		$give_updates->set_percentage( Give()->donors->count(), ( $give_updates->step * 20 ) );
1147
1148
		/* @var Object $donor */
1149
		foreach ( $donors as $donor ) {
1150
			$user_id = $donor->user_id;
1151
1152
			// Proceed, if donor is attached with user.
1153
			if ( $user_id ) {
1154
				$user = get_userdata( $user_id );
1155
1156
				// Update user role, if user has subscriber role.
1157
				if ( is_array( $user->roles ) && in_array( 'subscriber', $user->roles ) ) {
1158
					wp_update_user(
1159
						array(
1160
							'ID'   => $user_id,
1161
							'role' => 'give_donor',
1162
						)
1163
					);
1164
				}
1165
			}
1166
		}
1167
	} else {
1168
		// The Update Ran.
1169
		give_set_upgrade_complete( 'v1813_update_donor_user_roles' );
1170
	}
1171
}
1172
1173
/**
1174
 * Correct currency code for "Iranian Currency" for all of the payments.
1175
 *
1176
 * @since 1.8.17
1177
 */
1178
function give_v1817_update_donation_iranian_currency_code() {
1179
	/* @var Give_Updates $give_updates */
1180
	$give_updates = Give_Updates::get_instance();
1181
1182
	// form query.
1183
	$payments = new WP_Query( array(
1184
			'paged'          => $give_updates->step,
1185
			'status'         => 'any',
1186
			'order'          => 'ASC',
1187
			'post_type'      => array( 'give_payment' ),
1188
			'posts_per_page' => 20,
1189
		)
1190
	);
1191
1192
	if ( $payments->have_posts() ) {
1193
		$give_updates->set_percentage( $payments->found_posts, ( $give_updates->step * 20 ) );
1194
1195
		while( $payments->have_posts() ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
1196
			$payments->the_post();
1197
1198
			$payment_meta = give_get_payment_meta( get_the_ID() );
1199
1200
			if ( 'RIAL' === $payment_meta['currency'] ) {
1201
				$payment_meta['currency'] = 'IRR';
1202
				give_update_meta( get_the_ID(), '_give_payment_meta', $payment_meta );
1203
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1204
1205
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1206
1207
	}else{
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
1208
		// The Update Ran.
1209
		give_set_upgrade_complete( 'v1817_update_donation_iranian_currency_code' );
1210
	}
1211
}
1212
1213
/**
1214
 * Version 1.8.13 automatic updates
1215
 *
1216
 * @since 1.8.13
1217
 */
1218
function give_v1813_upgrades() {
1219
	// Update admin setting.
1220
	give_update_option( 'donor_default_user_role', 'give_donor' );
1221
1222
	// Update Give roles.
1223
	$roles = new Give_Roles();
1224
	$roles->add_roles();
1225
	$roles->add_caps();
1226
}
1227
1228
/**
1229
 * Correct currency code for "Iranian Currency" in Give setting.
1230
 * Version 1.8.17 automatic updates
1231
 *
1232
 * @since 1.8.17
1233
 */
1234
function give_v1817_upgrades() {
1235
	$give_settings = give_get_settings();
1236
1237
	if ( 'RIAL' === $give_settings['currency'] ) {
1238
		$give_settings['currency'] = 'IRR';
1239
		update_option( 'give_settings', $give_settings );
1240
	}
1241
}
1242
1243
/**
1244
 * Process Clean up of User Roles for more flexibility.
1245
 *
1246
 * @since 1.8.17
1247
 */
1248
function give_v1817_process_cleanup_user_roles() {
1249
1250
	global $wp_roles;
1251
1252
	if( ! ( $wp_roles instanceof  WP_Role ) ) {
0 ignored issues
show
Bug introduced by
The class WP_Role does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
1253
		return;
1254
	}
1255
1256
	// Add Capabilities to user roles as required.
1257
	$add_caps = array(
1258
		'administrator' => array(
1259
			'view_give_payments',
1260
		),
1261
	);
1262
1263
	// Remove Capabilities to user roles as required.
1264
	$remove_caps = array(
1265
		'give_manager' => array(
1266
			'edit_others_pages',
1267
			'edit_others_posts',
1268
			'delete_others_pages',
1269
			'delete_others_posts',
1270
			'manage_categories',
1271
			'import',
1272
			'export',
1273
		),
1274
	);
1275
1276
	foreach ( $add_caps as $role => $caps ) {
1277
		foreach( $caps as $cap ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
1278
			$wp_roles->add_cap( $role, $cap );
1279
		}
1280
	}
1281
1282
	foreach ( $remove_caps as $role => $caps ) {
1283
		foreach( $caps as $cap ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
1284
			$wp_roles->remove_cap( $role, $cap );
1285
		}
1286
	}
1287
1288
}
1289
1290
/**
1291
 * Upgrade Routine - Clean up of User Roles for more flexibility.
1292
 *
1293
 * @since 1.8.17
1294
 */
1295
function give_v1817_cleanup_user_roles() {
1296
1297
	/* @var Give_Updates $give_updates */
1298
	$give_updates = Give_Updates::get_instance();
1299
1300
	give_v1817_process_cleanup_user_roles();
1301
1302
	$give_updates->percentage = 100;
0 ignored issues
show
Documentation Bug introduced by
It seems like 100 of type integer is incompatible with the declared type array of property $percentage.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
1303
1304
	// Create Give plugin roles.
1305
	$roles = new Give_Roles();
1306
	$roles->add_roles();
1307
	$roles->add_caps();
1308
1309
	give_set_upgrade_complete( 'v1817_cleanup_user_roles' );
1310
1311
}