Test Failed
Push — issues/2397 ( f0e197...4736c2 )
by Ravinder
09:25 queued 04:28
created

upgrade-functions.php ➔ give_v1818_upgrades()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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