Completed
Push — issue/2360 ( c1b5b4 )
by Ravinder
457:34 queued 450:36
created

upgrade-functions.php ➔ give_v1818_assign_custom_amount_set_donation()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

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