Test Failed
Pull Request — master (#2551)
by Devin
04:51
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 %

Code Coverage

Tests 0
CRAP Score 2

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