Test Failed
Push — release/2.0.1 ( af25db...2d343c )
by Ravinder
16:06
created

upgrade-functions.php ➔ give_v201_add_missing_donors_callback()   D

Complexity

Conditions 17
Paths 4

Size

Total Lines 105
Code Lines 55

Duplication

Lines 19
Ratio 18.1 %

Importance

Changes 0
Metric Value
cc 17
eloc 55
nc 4
nop 0
dl 19
loc 105
rs 4.8361
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Upgrade Functions
4
 *
5
 * @package     Give
6
 * @subpackage  Admin/Upgrades
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.0
10
 *
11
 * NOTICE: When adding new upgrade notices, please be sure to put the action into the upgrades array during install:
12
 * /includes/install.php @ Appox Line 156
13
 */
14
15
// Exit if accessed directly.
16
if ( ! defined( 'ABSPATH' ) ) {
17
	exit;
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;
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...
76
77
		case version_compare( $give_version, '2.0', '<' ) :
78
			give_v20_upgrades();
79
			$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...
80
81
		case version_compare( $give_version, '2.0.1', '<' ) :
82
			// Do nothing on fresh install.
83
			if( ! doing_action( 'give_upgrades' ) ) {
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...
84
				give_v201_create_tables();
85
				Give_Updates::get_instance()->__health_background_update( Give_Updates::get_instance() );
86
			}
87
88
			$did_upgrade = true;
89
	}
90
91
	if ( $did_upgrade ) {
92
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
93
	}
94
}
95
96
add_action( 'admin_init', 'give_do_automatic_upgrades' );
97
add_action( 'give_upgrades', 'give_do_automatic_upgrades' );
98
99
/**
100
 * Display Upgrade Notices.
101
 *
102
 * IMPORTANT: ALSO UPDATE INSTALL.PHP WITH THE ID OF THE UPGRADE ROUTINE SO IT DOES NOT AFFECT NEW INSTALLS.
103
 *
104
 * @since 1.0
105
 * @since 1.8.12 Update new update process code.
106
 *
107
 * @param Give_Updates $give_updates
108
 *
109
 * @return void
110
 */
111
function give_show_upgrade_notices( $give_updates ) {
112
	// v1.3.2 Upgrades
113
	$give_updates->register(
114
		array(
115
			'id'       => 'upgrade_give_payment_customer_id',
116
			'version'  => '1.3.2',
117
			'callback' => 'give_v132_upgrade_give_payment_customer_id',
118
		)
119
	);
120
121
	// v1.3.4 Upgrades ensure the user has gone through 1.3.4.
122
	$give_updates->register(
123
		array(
124
			'id'       => 'upgrade_give_offline_status',
125
			'depend'   => 'upgrade_give_payment_customer_id',
126
			'version'  => '1.3.4',
127
			'callback' => 'give_v134_upgrade_give_offline_status',
128
		)
129
	);
130
131
	// v1.8 form metadata upgrades.
132
	$give_updates->register(
133
		array(
134
			'id'       => 'v18_upgrades_form_metadata',
135
			'version'  => '1.8',
136
			'callback' => 'give_v18_upgrades_form_metadata',
137
		)
138
	);
139
140
	// v1.8.9 Upgrades
141
	$give_updates->register(
142
		array(
143
			'id'       => 'v189_upgrades_levels_post_meta',
144
			'version'  => '1.8.9',
145
			'callback' => 'give_v189_upgrades_levels_post_meta_callback',
146
		)
147
	);
148
149
	// v1.8.12 Upgrades
150
	$give_updates->register(
151
		array(
152
			'id'       => 'v1812_update_amount_values',
153
			'version'  => '1.8.12',
154
			'callback' => 'give_v1812_update_amount_values_callback',
155
		)
156
	);
157
158
	// v1.8.12 Upgrades
159
	$give_updates->register(
160
		array(
161
			'id'       => 'v1812_update_donor_purchase_values',
162
			'version'  => '1.8.12',
163
			'callback' => 'give_v1812_update_donor_purchase_value_callback',
164
		)
165
	);
166
167
	// v1.8.13 Upgrades for donor
168
	$give_updates->register(
169
		array(
170
			'id'       => 'v1813_update_donor_user_roles',
171
			'version'  => '1.8.13',
172
			'callback' => 'give_v1813_update_donor_user_roles_callback',
173
		)
174
	);
175
176
	// v1.8.17 Upgrades for donations.
177
	$give_updates->register( array(
178
		'id'       => 'v1817_update_donation_iranian_currency_code',
179
		'version'  => '1.8.17',
180
		'callback' => 'give_v1817_update_donation_iranian_currency_code',
181
	) );
182
183
	// v1.8.17 Upgrades for cleanup of user roles.
184
	$give_updates->register( array(
185
		'id'       => 'v1817_cleanup_user_roles',
186
		'version'  => '1.8.17',
187
		'callback' => 'give_v1817_cleanup_user_roles',
188
	) );
189
190
	// v1.8.18 Upgrades for assigning custom amount to existing set donations.
191
	$give_updates->register( array(
192
		'id'       => 'v1818_assign_custom_amount_set_donation',
193
		'version'  => '1.8.18',
194
		'callback' => 'give_v1818_assign_custom_amount_set_donation',
195
	) );
196
197
	// v1.8.18 Cleanup the Give Worker Role Caps.
198
	$give_updates->register( array(
199
		'id'       => 'v1818_give_worker_role_cleanup',
200
		'version'  => '1.8.18',
201
		'callback' => 'give_v1818_give_worker_role_cleanup',
202
	) );
203
204
	// v2.0.0 Upgrades
205
	$give_updates->register(
206
		array(
207
			'id'       => 'v20_upgrades_form_metadata',
208
			'version'  => '2.0.0',
209
			'callback' => 'give_v20_upgrades_form_metadata_callback',
210
		)
211
	);
212
213
	// v2.0.0 User Address Upgrades
214
	$give_updates->register(
215
		array(
216
			'id'       => 'v20_upgrades_user_address',
217
			'version'  => '2.0.0',
218
			'callback' => 'give_v20_upgrades_user_address',
219
		)
220
	);
221
222
	// v2.0.0 Upgrades
223
	$give_updates->register(
224
		array(
225
			'id'       => 'v20_upgrades_payment_metadata',
226
			'version'  => '2.0.0',
227
			'callback' => 'give_v20_upgrades_payment_metadata_callback',
228
		)
229
	);
230
231
	// v2.0.0 Upgrades
232
	$give_updates->register(
233
		array(
234
			'id'       => 'v20_logs_upgrades',
235
			'version'  => '2.0.0',
236
			'callback' => 'give_v20_logs_upgrades_callback',
237
238
		)
239
	);
240
241
	// v2.0.0 Donor Name Upgrades
242
	$give_updates->register(
243
		array(
244
			'id'       => 'v20_upgrades_donor_name',
245
			'version'  => '2.0.0',
246
			'callback' => 'give_v20_upgrades_donor_name',
247
		)
248
	);
249
250
	// v2.0.0 Upgrades
251
	$give_updates->register(
252
		array(
253
			'id'       => 'v20_move_metadata_into_new_table',
254
			'version'  => '2.0.0',
255
			'callback' => 'give_v20_move_metadata_into_new_table_callback',
256
			'depend'   => array( 'v20_upgrades_payment_metadata', 'v20_upgrades_form_metadata' ),
257
		)
258
	);
259
260
	// v2.0.0 Upgrades
261
	$give_updates->register(
262
		array(
263
			'id'       => 'v20_rename_donor_tables',
264
			'version'  => '2.0.0',
265
			'callback' => 'give_v20_rename_donor_tables_callback',
266
			'depend'   => array(
267
				'v20_move_metadata_into_new_table',
268
				'v20_logs_upgrades',
269
				'v20_upgrades_form_metadata',
270
				'v20_upgrades_payment_metadata',
271
				'v20_upgrades_user_address',
272
				'v20_upgrades_donor_name'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
273
			),
274
		)
275
	);
276
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
277
278
	// v2.0.1 Upgrades
279
	$give_updates->register(
280
		array(
281
			'id'       => 'v201_upgrades_payment_metadata',
282
			'version'  => '2.0.1',
283
			'callback' => 'give_v201_upgrades_payment_metadata_callback',
284
		)
285
	);
286
287
	// v2.0.1 Upgrades
288
	$give_updates->register(
289
		array(
290
			'id'       => 'v201_add_missing_donors',
291
			'version'  => '2.0.1',
292
			'callback' => 'give_v201_add_missing_donors_callback',
293
		)
294
	);
295
296
	// Run v2.0.0 Upgrades again in 2.0.1
297
	$give_updates->register(
298
		array(
299
			'id'       => 'v201_move_metadata_into_new_table',
300
			'version'  => '2.0.1',
301
			'callback' => 'give_v201_move_metadata_into_new_table_callback',
302
			'depend'   => array( 'v201_upgrades_payment_metadata', 'v201_add_missing_donors' ),
303
		)
304
	);
305
306
	// Run v2.0.0 Upgrades again in 2.0.1
307
	$give_updates->register(
308
		array(
309
			'id'       => 'v201_logs_upgrades',
310
			'version'  => '2.0.1',
311
			'callback' => 'give_v201_logs_upgrades_callback',
312
313
		)
314
	);
315
}
316
317
add_action( 'give_register_updates', 'give_show_upgrade_notices' );
318
319
/**
320
 * Triggers all upgrade functions
321
 *
322
 * This function is usually triggered via AJAX
323
 *
324
 * @since 1.0
325
 * @return void
326
 */
327
function give_trigger_upgrades() {
328
329 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...
330
		wp_die( esc_html__( 'You do not have permission to do Give upgrades.', 'give' ), esc_html__( 'Error', 'give' ), array(
331
			'response' => 403,
332
		) );
333
	}
334
335
	$give_version = get_option( 'give_version' );
336
337
	if ( ! $give_version ) {
338
		// 1.0 is the first version to use this option so we must add it.
339
		$give_version = '1.0';
340
		add_option( 'give_version', $give_version );
341
	}
342
343
	update_option( 'give_version', GIVE_VERSION );
344
	delete_option( 'give_doing_upgrade' );
345
346
	if ( DOING_AJAX ) {
347
		die( 'complete' );
348
	} // End if().
349
}
350
351
add_action( 'wp_ajax_give_trigger_upgrades', 'give_trigger_upgrades' );
352
353
354
/**
355
 * Upgrades the
356
 *
357
 * Standardizes the discrepancies between two metakeys `_give_payment_customer_id` and `_give_payment_donor_id`
358
 *
359
 * @since      1.3.2
360
 */
361
function give_v132_upgrade_give_payment_customer_id() {
362
	global $wpdb;
363
364
	/* @var Give_Updates $give_updates */
365
	$give_updates = Give_Updates::get_instance();
366
367 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...
368
		wp_die( esc_html__( 'You do not have permission to do Give upgrades.', 'give' ), esc_html__( 'Error', 'give' ), array(
369
			'response' => 403,
370
		) );
371
	}
372
373
	give_ignore_user_abort();
374
375
	// UPDATE DB METAKEYS.
376
	$sql   = "UPDATE $wpdb->postmeta SET meta_key = '_give_payment_customer_id' WHERE meta_key = '_give_payment_donor_id'";
377
	$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...
378
379
	$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...
380
	give_set_upgrade_complete( 'upgrade_give_payment_customer_id' );
381
}
382
383
384
/**
385
 * Upgrades the Offline Status
386
 *
387
 * Reverses the issue where offline donations in "pending" status where inappropriately marked as abandoned
388
 *
389
 * @since      1.3.4
390
 */
391
function give_v134_upgrade_give_offline_status() {
392
	global $wpdb;
393
394
	/* @var Give_Updates $give_updates */
395
	$give_updates = Give_Updates::get_instance();
396
397
	// Get abandoned offline payments.
398
	$select = "SELECT ID FROM $wpdb->posts p ";
399
	$join   = "LEFT JOIN $wpdb->postmeta m ON p.ID = m.post_id ";
400
	$where  = "WHERE p.post_type = 'give_payment' ";
401
	$where  .= "AND ( p.post_status = 'abandoned' )";
402
	$where  .= "AND ( m.meta_key = '_give_payment_gateway' AND m.meta_value = 'offline' )";
403
404
	$sql            = $select . $join . $where;
405
	$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...
406
407
	foreach ( $found_payments as $payment ) {
408
409
		// Only change ones marked abandoned since our release last week because the admin may have marked some abandoned themselves.
410
		$modified_time = get_post_modified_time( 'U', false, $payment );
411
412
		// 1450124863 =  12/10/2015 20:42:25.
413
		if ( $modified_time >= 1450124863 ) {
414
415
			give_update_payment_status( $payment, 'pending' );
416
417
		}
418
	}
419
420
	$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...
421
	give_set_upgrade_complete( 'upgrade_give_offline_status' );
422
}
423
424
425
/**
426
 * Cleanup User Roles
427
 *
428
 * This upgrade routine removes unused roles and roles with typos
429
 *
430
 * @since      1.5.2
431
 */
432
function give_v152_cleanup_users() {
433
434
	$give_version = get_option( 'give_version' );
435
436
	if ( ! $give_version ) {
437
		// 1.0 is the first version to use this option so we must add it.
438
		$give_version = '1.0';
439
	}
440
441
	$give_version = preg_replace( '/[^0-9.].*/', '', $give_version );
442
443
	// v1.5.2 Upgrades
444
	if ( version_compare( $give_version, '1.5.2', '<' ) || ! give_has_upgrade_completed( 'upgrade_give_user_caps_cleanup' ) ) {
445
446
		// Delete all caps with "ss".
447
		// Also delete all unused "campaign" roles.
448
		$delete_caps = array(
449
			'delete_give_formss',
450
			'delete_others_give_formss',
451
			'delete_private_give_formss',
452
			'delete_published_give_formss',
453
			'read_private_forms',
454
			'edit_give_formss',
455
			'edit_others_give_formss',
456
			'edit_private_give_formss',
457
			'edit_published_give_formss',
458
			'publish_give_formss',
459
			'read_private_give_formss',
460
			'assign_give_campaigns_terms',
461
			'delete_give_campaigns',
462
			'delete_give_campaigns_terms',
463
			'delete_give_campaignss',
464
			'delete_others_give_campaignss',
465
			'delete_private_give_campaignss',
466
			'delete_published_give_campaignss',
467
			'edit_give_campaigns',
468
			'edit_give_campaigns_terms',
469
			'edit_give_campaignss',
470
			'edit_others_give_campaignss',
471
			'edit_private_give_campaignss',
472
			'edit_published_give_campaignss',
473
			'manage_give_campaigns_terms',
474
			'publish_give_campaignss',
475
			'read_give_campaigns',
476
			'read_private_give_campaignss',
477
			'view_give_campaigns_stats',
478
			'delete_give_paymentss',
479
			'delete_others_give_paymentss',
480
			'delete_private_give_paymentss',
481
			'delete_published_give_paymentss',
482
			'edit_give_paymentss',
483
			'edit_others_give_paymentss',
484
			'edit_private_give_paymentss',
485
			'edit_published_give_paymentss',
486
			'publish_give_paymentss',
487
			'read_private_give_paymentss',
488
		);
489
490
		global $wp_roles;
491
		foreach ( $delete_caps as $cap ) {
492
			foreach ( array_keys( $wp_roles->roles ) as $role ) {
493
				$wp_roles->remove_cap( $role, $cap );
494
			}
495
		}
496
497
		// Create Give plugin roles.
498
		$roles = new Give_Roles();
499
		$roles->add_roles();
500
		$roles->add_caps();
501
502
		// The Update Ran.
503
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
504
		give_set_upgrade_complete( 'upgrade_give_user_caps_cleanup' );
505
		delete_option( 'give_doing_upgrade' );
506
507
	}// End if().
508
509
}
510
511
add_action( 'admin_init', 'give_v152_cleanup_users' );
512
513
/**
514
 * 1.6 Upgrade routine to create the customer meta table.
515
 *
516
 * @since  1.6
517
 * @return void
518
 */
519
function give_v16_upgrades() {
520
	// Create the donor databases.
521
	$donors_db = new Give_DB_Donors();
522
	$donors_db->create_table();
523
	$donor_meta = new Give_DB_Donor_Meta();
524
	$donor_meta->create_table();
525
}
526
527
/**
528
 * 1.7 Upgrades.
529
 *
530
 * a. Update license api data for plugin addons.
531
 * b. Cleanup user roles.
532
 *
533
 * @since  1.7
534
 * @return void
535
 */
536
function give_v17_upgrades() {
537
	// Upgrade license data.
538
	give_v17_upgrade_addon_license_data();
539
	give_v17_cleanup_roles();
540
}
541
542
/**
543
 * Upgrade license data
544
 *
545
 * @since 1.7
546
 */
547
function give_v17_upgrade_addon_license_data() {
548
	$give_options = give_get_settings();
549
550
	$api_url = 'https://givewp.com/give-sl-api/';
551
552
	// Get addons license key.
553
	$addons = array();
554
	foreach ( $give_options as $key => $value ) {
555
		if ( false !== strpos( $key, '_license_key' ) ) {
556
			$addons[ $key ] = $value;
557
		}
558
	}
559
560
	// Bailout: We do not have any addon license data to upgrade.
561
	if ( empty( $addons ) ) {
562
		return false;
563
	}
564
565
	foreach ( $addons as $key => $addon_license ) {
566
567
		// Get addon shortname.
568
		$shortname = str_replace( '_license_key', '', $key );
569
570
		// Addon license option name.
571
		$addon_license_option_name = $shortname . '_license_active';
572
573
		// bailout if license is empty.
574
		if ( empty( $addon_license ) ) {
575
			delete_option( $addon_license_option_name );
576
			continue;
577
		}
578
579
		// Get addon name.
580
		$addon_name       = array();
581
		$addon_name_parts = explode( '_', str_replace( 'give_', '', $shortname ) );
582
		foreach ( $addon_name_parts as $name_part ) {
583
584
			// Fix addon name
585
			switch ( $name_part ) {
586
				case 'authorizenet' :
587
					$name_part = 'authorize.net';
588
					break;
589
			}
590
591
			$addon_name[] = ucfirst( $name_part );
592
		}
593
594
		$addon_name = implode( ' ', $addon_name );
595
596
		// Data to send to the API.
597
		$api_params = array(
598
			'edd_action' => 'activate_license', // never change from "edd_" to "give_"!
599
			'license'    => $addon_license,
600
			'item_name'  => urlencode( $addon_name ),
601
			'url'        => home_url(),
602
		);
603
604
		// Call the API.
605
		$response = wp_remote_post(
606
			$api_url,
607
			array(
608
				'timeout'   => 15,
609
				'sslverify' => false,
610
				'body'      => $api_params,
611
			)
612
		);
613
614
		// Make sure there are no errors.
615
		if ( is_wp_error( $response ) ) {
616
			delete_option( $addon_license_option_name );
617
			continue;
618
		}
619
620
		// Tell WordPress to look for updates.
621
		set_site_transient( 'update_plugins', null );
622
623
		// Decode license data.
624
		$license_data = json_decode( wp_remote_retrieve_body( $response ) );
625
		update_option( $addon_license_option_name, $license_data );
626
	}// End foreach().
627
}
628
629
630
/**
631
 * Cleanup User Roles.
632
 *
633
 * This upgrade routine removes unused roles and roles with typos.
634
 *
635
 * @since      1.7
636
 */
637
function give_v17_cleanup_roles() {
638
639
	// Delete all caps with "_give_forms_" and "_give_payments_".
640
	// These roles have no usage; the proper is singular.
641
	$delete_caps = array(
642
		'view_give_forms_stats',
643
		'delete_give_forms_terms',
644
		'assign_give_forms_terms',
645
		'edit_give_forms_terms',
646
		'manage_give_forms_terms',
647
		'view_give_payments_stats',
648
		'manage_give_payments_terms',
649
		'edit_give_payments_terms',
650
		'assign_give_payments_terms',
651
		'delete_give_payments_terms',
652
	);
653
654
	global $wp_roles;
655
	foreach ( $delete_caps as $cap ) {
656
		foreach ( array_keys( $wp_roles->roles ) as $role ) {
657
			$wp_roles->remove_cap( $role, $cap );
658
		}
659
	}
660
661
	// Set roles again.
662
	$roles = new Give_Roles();
663
	$roles->add_roles();
664
	$roles->add_caps();
665
666
}
667
668
/**
669
 * 1.8 Upgrades.
670
 *
671
 * a. Upgrade checkbox settings to radio button settings.
672
 * a. Update form meta for new metabox settings.
673
 *
674
 * @since  1.8
675
 * @return void
676
 */
677
function give_v18_upgrades() {
678
	// Upgrade checkbox settings to radio button settings.
679
	give_v18_upgrades_core_setting();
680
}
681
682
/**
683
 * Upgrade core settings.
684
 *
685
 * @since  1.8
686
 * @return void
687
 */
688
function give_v18_upgrades_core_setting() {
689
	// Core settings which changes from checkbox to radio.
690
	$core_setting_names = array_merge(
691
		array_keys( give_v18_renamed_core_settings() ),
692
		array(
693
			'uninstall_on_delete',
694
			'scripts_footer',
695
			'test_mode',
696
			'email_access',
697
			'terms',
698
			'give_offline_donation_enable_billing_fields',
699
		)
700
	);
701
702
	// Bailout: If not any setting define.
703
	if ( $give_settings = get_option( 'give_settings' ) ) {
704
705
		$setting_changed = false;
706
707
		// Loop: check each setting field.
708
		foreach ( $core_setting_names as $setting_name ) {
709
			// New setting name.
710
			$new_setting_name = preg_replace( '/^(enable_|disable_)/', '', $setting_name );
711
712
			// Continue: If setting already set.
713
			if (
714
				array_key_exists( $new_setting_name, $give_settings )
715
				&& in_array( $give_settings[ $new_setting_name ], array( 'enabled', 'disabled' ) )
716
			) {
717
				continue;
718
			}
719
720
			// Set checkbox value to radio value.
721
			$give_settings[ $setting_name ] = ( ! empty( $give_settings[ $setting_name ] ) && 'on' === $give_settings[ $setting_name ] ? 'enabled' : 'disabled' );
722
723
			// @see https://github.com/WordImpress/Give/issues/1063.
724
			if ( false !== strpos( $setting_name, 'disable_' ) ) {
725
726
				$give_settings[ $new_setting_name ] = ( give_is_setting_enabled( $give_settings[ $setting_name ] ) ? 'disabled' : 'enabled' );
727
			} elseif ( false !== strpos( $setting_name, 'enable_' ) ) {
728
729
				$give_settings[ $new_setting_name ] = ( give_is_setting_enabled( $give_settings[ $setting_name ] ) ? 'enabled' : 'disabled' );
730
			}
731
732
			// Tell bot to update core setting to db.
733
			if ( ! $setting_changed ) {
734
				$setting_changed = true;
735
			}
736
		}
737
738
		// Update setting only if they changed.
739
		if ( $setting_changed ) {
740
			update_option( 'give_settings', $give_settings );
741
		}
742
	}// End if().
743
744
	give_set_upgrade_complete( 'v18_upgrades_core_setting' );
745
}
746
747
/**
748
 * Upgrade form metadata for new metabox settings.
749
 *
750
 * @since  1.8
751
 * @return void
752
 */
753
function give_v18_upgrades_form_metadata() {
754
	/* @var Give_Updates $give_updates */
755
	$give_updates = Give_Updates::get_instance();
756
757
	// form query
758
	$forms = new WP_Query( array(
759
			'paged'          => $give_updates->step,
760
			'status'         => 'any',
761
			'order'          => 'ASC',
762
			'post_type'      => 'give_forms',
763
			'posts_per_page' => 20,
764
		)
765
	);
766
767
	if ( $forms->have_posts() ) {
768
		$give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 20 ) );
769
770
		while ( $forms->have_posts() ) {
771
			$forms->the_post();
772
773
			// Form content.
774
			// Note in version 1.8 display content setting split into display content and content placement setting.
775
			// You can delete _give_content_option in future.
776
			$show_content = give_get_meta( get_the_ID(), '_give_content_option', true );
777
			if ( $show_content && ! give_get_meta( get_the_ID(), '_give_display_content', true ) ) {
778
				$field_value = ( 'none' !== $show_content ? 'enabled' : 'disabled' );
779
				give_update_meta( get_the_ID(), '_give_display_content', $field_value );
780
781
				$field_value = ( 'none' !== $show_content ? $show_content : 'give_pre_form' );
782
				give_update_meta( get_the_ID(), '_give_content_placement', $field_value );
783
			}
784
785
			// "Disable" Guest Donation. Checkbox.
786
			// See: https://github.com/WordImpress/Give/issues/1470.
787
			$guest_donation        = give_get_meta( get_the_ID(), '_give_logged_in_only', true );
788
			$guest_donation_newval = ( in_array( $guest_donation, array( 'yes', 'on' ) ) ? 'disabled' : 'enabled' );
789
			give_update_meta( get_the_ID(), '_give_logged_in_only', $guest_donation_newval );
790
791
			// Offline Donations.
792
			// See: https://github.com/WordImpress/Give/issues/1579.
793
			$offline_donation = give_get_meta( get_the_ID(), '_give_customize_offline_donations', true );
794
			if ( 'no' === $offline_donation ) {
795
				$offline_donation_newval = 'global';
796
			} elseif ( 'yes' === $offline_donation ) {
797
				$offline_donation_newval = 'enabled';
798
			} else {
799
				$offline_donation_newval = 'disabled';
800
			}
801
			give_update_meta( get_the_ID(), '_give_customize_offline_donations', $offline_donation_newval );
802
803
			// Convert yes/no setting field to enabled/disabled.
804
			$form_radio_settings = array(
805
				// Custom Amount.
806
				'_give_custom_amount',
807
808
				// Donation Gaol.
809
				'_give_goal_option',
810
811
				// Close Form.
812
				'_give_close_form_when_goal_achieved',
813
814
				// Term & conditions.
815
				'_give_terms_option',
816
817
				// Billing fields.
818
				'_give_offline_donation_enable_billing_fields_single',
819
			);
820
821
			foreach ( $form_radio_settings as $meta_key ) {
822
				// Get value.
823
				$field_value = give_get_meta( get_the_ID(), $meta_key, true );
824
825
				// Convert meta value only if it is in yes/no/none.
826
				if ( in_array( $field_value, array( 'yes', 'on', 'no', 'none' ) ) ) {
827
828
					$field_value = ( in_array( $field_value, array( 'yes', 'on' ) ) ? 'enabled' : 'disabled' );
829
					give_update_meta( get_the_ID(), $meta_key, $field_value );
830
				}
831
			}
832
		}// End while().
833
834
		wp_reset_postdata();
835
836
	} else {
837
		// No more forms found, finish up.
838
		give_set_upgrade_complete( 'v18_upgrades_form_metadata' );
839
	}
840
}
841
842
843
/**
844
 * Get list of core setting renamed in version 1.8.
845
 *
846
 * @since  1.8
847
 * @return array
848
 */
849
function give_v18_renamed_core_settings() {
850
	return array(
851
		'disable_paypal_verification' => 'paypal_verification',
852
		'disable_css'                 => 'css',
853
		'disable_welcome'             => 'welcome',
854
		'disable_forms_singular'      => 'forms_singular',
855
		'disable_forms_archives'      => 'forms_archives',
856
		'disable_forms_excerpt'       => 'forms_excerpt',
857
		'disable_form_featured_img'   => 'form_featured_img',
858
		'disable_form_sidebar'        => 'form_sidebar',
859
		'disable_admin_notices'       => 'admin_notices',
860
		'disable_the_content_filter'  => 'the_content_filter',
861
		'enable_floatlabels'          => 'floatlabels',
862
		'enable_categories'           => 'categories',
863
		'enable_tags'                 => 'tags',
864
	);
865
}
866
867
868
/**
869
 * Upgrade core settings.
870
 *
871
 * @since  1.8.7
872
 * @return void
873
 */
874
function give_v187_upgrades() {
875
	global $wpdb;
876
877
	/**
878
	 * Upgrade 1: Remove stat and cache transients.
879
	 */
880
	$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...
881
		$wpdb->prepare(
882
			"
883
					SELECT *
884
					FROM {$wpdb->options}
885
					WHERE (
886
					option_name LIKE %s
887
					OR option_name LIKE %s
888
					OR option_name LIKE %s
889
					OR option_name LIKE %s
890
					OR option_name LIKE %s
891
					OR option_name LIKE %s
892
					OR option_name LIKE %s
893
					OR option_name LIKE %s
894
					OR option_name LIKE %s
895
					OR option_name LIKE %s
896
					OR option_name LIKE %s
897
					OR option_name LIKE %s
898
					OR option_name LIKE %s
899
					)
900
					",
901
			array(
902
				'%_transient_give_stats_%',
903
				'give_cache%',
904
				'%_transient_give_add_ons_feed%',
905
				'%_transient__give_ajax_works' .
906
				'%_transient_give_total_api_keys%',
907
				'%_transient_give_i18n_give_promo_hide%',
908
				'%_transient_give_contributors%',
909
				'%_transient_give_estimated_monthly_stats%',
910
				'%_transient_give_earnings_total%',
911
				'%_transient_give_i18n_give_%',
912
				'%_transient__give_installed%',
913
				'%_transient__give_activation_redirect%',
914
				'%_transient__give_hide_license_notices_shortly_%',
915
				'%give_income_total%',
916
			)
917
		),
918
		1
919
	);
920
921
	// User related transients.
922
	$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...
923
		$wpdb->prepare(
924
			"SELECT user_id, meta_key
925
			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...
926
			WHERE meta_value=%s",
927
			'give_user_public_key'
928
		),
929
		ARRAY_A
930
	);
931
932
	if ( ! empty( $user_apikey_options ) ) {
933
		foreach ( $user_apikey_options as $user ) {
934
			$cached_options[] = '_transient_' . md5( 'give_api_user_' . $user['meta_key'] );
935
			$cached_options[] = '_transient_' . md5( 'give_api_user_public_key' . $user['user_id'] );
936
			$cached_options[] = '_transient_' . md5( 'give_api_user_secret_key' . $user['user_id'] );
937
		}
938
	}
939
940
	if ( ! empty( $cached_options ) ) {
941
		foreach ( $cached_options as $option ) {
942 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...
943
				case ( false !== strpos( $option, 'transient' ) ):
944
					$option = str_replace( '_transient_', '', $option );
945
					delete_transient( $option );
946
					break;
947
948
				default:
949
					delete_option( $option );
950
			}
951
		}
952
	}
953
}
954
955
/**
956
 * Update Capabilities for Give_Worker User Role.
957
 *
958
 * This upgrade routine will update access rights for Give_Worker User Role.
959
 *
960
 * @since      1.8.8
961
 */
962
function give_v188_upgrades() {
963
964
	global $wp_roles;
965
966
	// Get the role object.
967
	$give_worker = get_role( 'give_worker' );
968
969
	// A list of capabilities to add for give workers.
970
	$caps_to_add = array(
971
		'edit_posts',
972
		'edit_pages',
973
	);
974
975
	foreach ( $caps_to_add as $cap ) {
976
		// Add the capability.
977
		$give_worker->add_cap( $cap );
978
	}
979
980
}
981
982
/**
983
 * Update Post meta for minimum and maximum amount for multi level donation forms
984
 *
985
 * This upgrade routine adds post meta for give_forms CPT for multi level donation form.
986
 *
987
 * @since      1.8.9
988
 */
989
function give_v189_upgrades_levels_post_meta_callback() {
990
	/* @var Give_Updates $give_updates */
991
	$give_updates = Give_Updates::get_instance();
992
993
	// form query.
994
	$donation_forms = new WP_Query( array(
995
			'paged'          => $give_updates->step,
996
			'status'         => 'any',
997
			'order'          => 'ASC',
998
			'post_type'      => 'give_forms',
999
			'posts_per_page' => 20,
1000
		)
1001
	);
1002
1003
	if ( $donation_forms->have_posts() ) {
1004
		$give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
1005
1006
		while ( $donation_forms->have_posts() ) {
1007
			$donation_forms->the_post();
1008
			$form_id = get_the_ID();
1009
1010
			// Remove formatting from _give_set_price.
1011
			update_post_meta(
1012
				$form_id,
1013
				'_give_set_price',
1014
				give_sanitize_amount( get_post_meta( $form_id, '_give_set_price', true ) )
1015
			);
1016
1017
			// Remove formatting from _give_custom_amount_minimum.
1018
			update_post_meta(
1019
				$form_id,
1020
				'_give_custom_amount_minimum',
1021
				give_sanitize_amount( get_post_meta( $form_id, '_give_custom_amount_minimum', true ) )
1022
			);
1023
1024
			// Bailout.
1025
			if ( 'set' === get_post_meta( $form_id, '_give_price_option', true ) ) {
1026
				continue;
1027
			}
1028
1029
			$donation_levels = get_post_meta( $form_id, '_give_donation_levels', true );
1030
1031
			if ( ! empty( $donation_levels ) ) {
1032
1033
				foreach ( $donation_levels as $index => $donation_level ) {
1034
					if ( isset( $donation_level['_give_amount'] ) ) {
1035
						$donation_levels[ $index ]['_give_amount'] = give_sanitize_amount( $donation_level['_give_amount'] );
1036
					}
1037
				}
1038
1039
				update_post_meta( $form_id, '_give_donation_levels', $donation_levels );
1040
1041
				$donation_levels_amounts = wp_list_pluck( $donation_levels, '_give_amount' );
1042
1043
				$min_amount = min( $donation_levels_amounts );
1044
				$max_amount = max( $donation_levels_amounts );
1045
1046
				// Set Minimum and Maximum amount for Multi Level Donation Forms
1047
				give_update_meta( $form_id, '_give_levels_minimum_amount', $min_amount ? give_sanitize_amount( $min_amount ) : 0 );
1048
				give_update_meta( $form_id, '_give_levels_maximum_amount', $max_amount ? give_sanitize_amount( $max_amount ) : 0 );
1049
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1050
1051
		}
1052
1053
		/* Restore original Post Data */
1054
		wp_reset_postdata();
1055
	} else {
1056
		// The Update Ran.
1057
		give_set_upgrade_complete( 'v189_upgrades_levels_post_meta' );
1058
	}
1059
1060
}
1061
1062
1063
/**
1064
 * Give version 1.8.9 upgrades
1065
 *
1066
 * @since      1.8.9
1067
 */
1068
function give_v189_upgrades() {
1069
	/**
1070
	 * 1. Remove user license related notice show blocked ( Give_Notice will handle )
1071
	 */
1072
	global $wpdb;
1073
1074
	// Delete permanent notice blocker.
1075
	$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...
1076
		$wpdb->prepare(
1077
			"
1078
					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...
1079
					WHERE meta_key
1080
					LIKE '%%%s%%'
1081
					",
1082
			'_give_hide_license_notices_permanently'
1083
		)
1084
	);
1085
1086
	// Delete short notice blocker.
1087
	$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...
1088
		$wpdb->prepare(
1089
			"
1090
					DELETE FROM $wpdb->options
1091
					WHERE option_name
1092
					LIKE '%%%s%%'
1093
					",
1094
			'__give_hide_license_notices_shortly_'
1095
		)
1096
	);
1097
}
1098
1099
/**
1100
 * 2.0 Upgrades.
1101
 *
1102
 * @since  2.0
1103
 * @return void
1104
 */
1105
function give_v20_upgrades() {
1106
	// Update cache setting.
1107
	give_update_option( 'cache', 'enabled' );
1108
1109
	// Upgrade email settings.
1110
	give_v20_upgrades_email_setting();
1111
}
1112
1113
/**
1114
 * Move old email api settings to new email setting api for following emails:
1115
 *    1. new offline donation         [This was hard coded]
1116
 *    2. offline donation instruction
1117
 *    3. new donation
1118
 *    4. donation receipt
1119
 *
1120
 * @since 2.0
1121
 */
1122
function give_v20_upgrades_email_setting() {
1123
	$all_setting = give_get_settings();
1124
1125
	// Bailout on fresh install.
1126
	if ( empty( $all_setting ) ) {
1127
		return;
1128
	}
1129
1130
	$settings = array(
1131
		'offline_donation_subject'      => 'offline-donation-instruction_email_subject',
1132
		'global_offline_donation_email' => 'offline-donation-instruction_email_message',
1133
		'donation_subject'              => 'donation-receipt_email_subject',
1134
		'donation_receipt'              => 'donation-receipt_email_message',
1135
		'donation_notification_subject' => 'new-donation_email_subject',
1136
		'donation_notification'         => 'new-donation_email_message',
1137
		'admin_notice_emails'           => array(
1138
			'new-donation_recipient',
1139
			'new-offline-donation_recipient',
1140
			'new-donor-register_recipient',
1141
		),
1142
		'admin_notices'                 => 'new-donation_notification',
1143
	);
1144
1145
	foreach ( $settings as $old_setting => $new_setting ) {
1146
		// Do not update already modified
1147
		if ( ! is_array( $new_setting ) ) {
1148
			if ( array_key_exists( $new_setting, $all_setting ) || ! array_key_exists( $old_setting, $all_setting ) ) {
1149
				continue;
1150
			}
1151
		}
1152
1153
		switch ( $old_setting ) {
1154
			case 'admin_notices':
1155
				$notification_status = give_get_option( $old_setting, 'enabled' );
1156
1157
				give_update_option( $new_setting, $notification_status );
0 ignored issues
show
Bug introduced by
It seems like $new_setting defined by $new_setting on line 1145 can also be of type array; however, give_update_option() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
1158
1159
				// @todo: Delete this option later ( version > 2.0 ), We need this for per form email addon.
1160
				// give_delete_option( $old_setting );
1161
1162
				break;
1163
1164
			// @todo: Delete this option later ( version > 2.0 ) because we need this for backward compatibility give_get_admin_notice_emails.
1165
			case 'admin_notice_emails':
1166
				$recipients = give_get_admin_notice_emails();
1167
1168
				foreach ( $new_setting as $setting ) {
0 ignored issues
show
Bug introduced by
The expression $new_setting of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
1169
					// bailout if setting already exist.
1170
					if ( array_key_exists( $setting, $all_setting ) ) {
1171
						continue;
1172
					}
1173
1174
					give_update_option( $setting, $recipients );
1175
				}
1176
				break;
1177
1178
			default:
1179
				give_update_option( $new_setting, give_get_option( $old_setting ) );
0 ignored issues
show
Bug introduced by
It seems like $new_setting defined by $new_setting on line 1145 can also be of type array; however, give_update_option() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
1180
				give_delete_option( $old_setting );
1181
		}
1182
	}
1183
}
1184
1185
/**
1186
 * Give version 1.8.9 upgrades
1187
 *
1188
 * @since 1.8.9
1189
 */
1190
function give_v1812_upgrades() {
1191
	/**
1192
	 * Validate number format settings.
1193
	 */
1194
	$give_settings        = give_get_settings();
1195
	$give_setting_updated = false;
1196
1197
	if ( $give_settings['thousands_separator'] === $give_settings['decimal_separator'] ) {
1198
		$give_settings['number_decimals']   = 0;
1199
		$give_settings['decimal_separator'] = '';
1200
		$give_setting_updated               = true;
1201
1202
	} elseif ( empty( $give_settings['decimal_separator'] ) ) {
1203
		$give_settings['number_decimals'] = 0;
1204
		$give_setting_updated             = true;
1205
1206
	} elseif ( 6 < absint( $give_settings['number_decimals'] ) ) {
1207
		$give_settings['number_decimals'] = 5;
1208
		$give_setting_updated             = true;
1209
	}
1210
1211
	if ( $give_setting_updated ) {
1212
		update_option( 'give_settings', $give_settings );
1213
	}
1214
}
1215
1216
1217
/**
1218
 * Give version 1.8.12 update
1219
 *
1220
 * Standardized amount values to six decimal
1221
 *
1222
 * @see        https://github.com/WordImpress/Give/issues/1849#issuecomment-315128602
1223
 *
1224
 * @since      1.8.12
1225
 */
1226
function give_v1812_update_amount_values_callback() {
1227
	/* @var Give_Updates $give_updates */
1228
	$give_updates = Give_Updates::get_instance();
1229
1230
	// form query.
1231
	$donation_forms = new WP_Query( array(
1232
			'paged'          => $give_updates->step,
1233
			'status'         => 'any',
1234
			'order'          => 'ASC',
1235
			'post_type'      => array( 'give_forms', 'give_payment' ),
1236
			'posts_per_page' => 20,
1237
		)
1238
	);
1239
	if ( $donation_forms->have_posts() ) {
1240
		$give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
1241
1242
		while ( $donation_forms->have_posts() ) {
1243
			$donation_forms->the_post();
1244
			global $post;
1245
1246
			$meta = get_post_meta( $post->ID );
1247
1248
			switch ( $post->post_type ) {
1249
				case 'give_forms':
1250
					// _give_set_price.
1251
					if ( ! empty( $meta['_give_set_price'][0] ) ) {
1252
						update_post_meta( $post->ID, '_give_set_price', give_sanitize_amount_for_db( $meta['_give_set_price'][0] ) );
1253
					}
1254
1255
					// _give_custom_amount_minimum.
1256
					if ( ! empty( $meta['_give_custom_amount_minimum'][0] ) ) {
1257
						update_post_meta( $post->ID, '_give_custom_amount_minimum', give_sanitize_amount_for_db( $meta['_give_custom_amount_minimum'][0] ) );
1258
					}
1259
1260
					// _give_levels_minimum_amount.
1261
					if ( ! empty( $meta['_give_levels_minimum_amount'][0] ) ) {
1262
						update_post_meta( $post->ID, '_give_levels_minimum_amount', give_sanitize_amount_for_db( $meta['_give_levels_minimum_amount'][0] ) );
1263
					}
1264
1265
					// _give_levels_maximum_amount.
1266
					if ( ! empty( $meta['_give_levels_maximum_amount'][0] ) ) {
1267
						update_post_meta( $post->ID, '_give_levels_maximum_amount', give_sanitize_amount_for_db( $meta['_give_levels_maximum_amount'][0] ) );
1268
					}
1269
1270
					// _give_set_goal.
1271
					if ( ! empty( $meta['_give_set_goal'][0] ) ) {
1272
						update_post_meta( $post->ID, '_give_set_goal', give_sanitize_amount_for_db( $meta['_give_set_goal'][0] ) );
1273
					}
1274
1275
					// _give_form_earnings.
1276
					if ( ! empty( $meta['_give_form_earnings'][0] ) ) {
1277
						update_post_meta( $post->ID, '_give_form_earnings', give_sanitize_amount_for_db( $meta['_give_form_earnings'][0] ) );
1278
					}
1279
1280
					// _give_custom_amount_minimum.
1281
					if ( ! empty( $meta['_give_donation_levels'][0] ) ) {
1282
						$donation_levels = unserialize( $meta['_give_donation_levels'][0] );
1283
1284
						foreach ( $donation_levels as $index => $level ) {
1285
							if ( empty( $level['_give_amount'] ) ) {
1286
								continue;
1287
							}
1288
1289
							$donation_levels[ $index ]['_give_amount'] = give_sanitize_amount_for_db( $level['_give_amount'] );
1290
						}
1291
1292
						$meta['_give_donation_levels'] = $donation_levels;
1293
						update_post_meta( $post->ID, '_give_donation_levels', $meta['_give_donation_levels'] );
1294
					}
1295
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
1296
1297
					break;
1298
1299
				case 'give_payment':
1300
					// _give_payment_total.
1301
					if ( ! empty( $meta['_give_payment_total'][0] ) ) {
1302
						update_post_meta( $post->ID, '_give_payment_total', give_sanitize_amount_for_db( $meta['_give_payment_total'][0] ) );
1303
					}
1304
1305
					break;
1306
			}
1307
		}
1308
1309
		/* Restore original Post Data */
1310
		wp_reset_postdata();
1311
	} else {
1312
		// The Update Ran.
1313
		give_set_upgrade_complete( 'v1812_update_amount_values' );
1314
	}
1315
}
1316
1317
1318
/**
1319
 * Give version 1.8.12 update
1320
 *
1321
 * Standardized amount values to six decimal for donor
1322
 *
1323
 * @see        https://github.com/WordImpress/Give/issues/1849#issuecomment-315128602
1324
 *
1325
 * @since      1.8.12
1326
 */
1327
function give_v1812_update_donor_purchase_value_callback() {
1328
	/* @var Give_Updates $give_updates */
1329
	$give_updates = Give_Updates::get_instance();
1330
	$offset       = 1 === $give_updates->step ? 0 : $give_updates->step * 20;
1331
1332
	// form query.
1333
	$donors = Give()->donors->get_donors( array(
1334
			'number' => 20,
1335
			'offset' => $offset,
1336
		)
1337
	);
1338
1339
	if ( ! empty( $donors ) ) {
1340
		$give_updates->set_percentage( Give()->donors->count(), $offset );
1341
1342
		/* @var Object $donor */
1343
		foreach ( $donors as $donor ) {
1344
			Give()->donors->update( $donor->id, array( 'purchase_value' => give_sanitize_amount_for_db( $donor->purchase_value ) ) );
1345
		}
1346
	} else {
1347
		// The Update Ran.
1348
		give_set_upgrade_complete( 'v1812_update_donor_purchase_values' );
1349
	}
1350
}
1351
1352
/**
1353
 * Upgrade routine for updating user roles for existing donors.
1354
 *
1355
 * @since 1.8.13
1356
 */
1357
function give_v1813_update_donor_user_roles_callback() {
1358
	/* @var Give_Updates $give_updates */
1359
	$give_updates = Give_Updates::get_instance();
1360
	$offset       = 1 === $give_updates->step ? 0 : $give_updates->step * 20;
1361
1362
	// Fetch all the existing donors.
1363
	$donors = Give()->donors->get_donors( array(
1364
			'number' => 20,
1365
			'offset' => $offset,
1366
		)
1367
	);
1368
1369
	if ( ! empty( $donors ) ) {
1370
		$give_updates->set_percentage( Give()->donors->count(), ( $give_updates->step * 20 ) );
1371
1372
		/* @var Object $donor */
1373
		foreach ( $donors as $donor ) {
1374
			$user_id = $donor->user_id;
1375
1376
			// Proceed, if donor is attached with user.
1377
			if ( $user_id ) {
1378
				$user = get_userdata( $user_id );
1379
1380
				// Update user role, if user has subscriber role.
1381
				if ( is_array( $user->roles ) && in_array( 'subscriber', $user->roles ) ) {
1382
					wp_update_user(
1383
						array(
1384
							'ID'   => $user_id,
1385
							'role' => 'give_donor',
1386
						)
1387
					);
1388
				}
1389
			}
1390
		}
1391
	} else {
1392
		// The Update Ran.
1393
		give_set_upgrade_complete( 'v1813_update_donor_user_roles' );
1394
	}
1395
}
1396
1397
1398
/**
1399
 * Version 1.8.13 automatic updates
1400
 *
1401
 * @since 1.8.13
1402
 */
1403
function give_v1813_upgrades() {
1404
	// Update admin setting.
1405
	give_update_option( 'donor_default_user_role', 'give_donor' );
1406
1407
	// Update Give roles.
1408
	$roles = new Give_Roles();
1409
	$roles->add_roles();
1410
	$roles->add_caps();
1411
}
1412
1413
/**
1414
 * Correct currency code for "Iranian Currency" for all of the payments.
1415
 *
1416
 * @since 1.8.17
1417
 */
1418
function give_v1817_update_donation_iranian_currency_code() {
1419
	/* @var Give_Updates $give_updates */
1420
	$give_updates = Give_Updates::get_instance();
1421
1422
	// form query.
1423
	$payments = new WP_Query( array(
1424
			'paged'          => $give_updates->step,
1425
			'status'         => 'any',
1426
			'order'          => 'ASC',
1427
			'post_type'      => array( 'give_payment' ),
1428
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1429
		)
1430
	);
1431
1432
	if ( $payments->have_posts() ) {
1433
		$give_updates->set_percentage( $payments->found_posts, ( $give_updates->step * 100 ) );
1434
1435
		while ( $payments->have_posts() ) {
1436
			$payments->the_post();
1437
1438
			$payment_meta = give_get_payment_meta( get_the_ID() );
1439
1440
			if ( 'RIAL' === $payment_meta['currency'] ) {
1441
				$payment_meta['currency'] = 'IRR';
1442
				give_update_meta( get_the_ID(), '_give_payment_meta', $payment_meta );
1443
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1444
1445
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1446
1447
	} else {
1448
		// The Update Ran.
1449
		give_set_upgrade_complete( 'v1817_update_donation_iranian_currency_code' );
1450
	}
1451
}
1452
1453
/**
1454
 * Correct currency code for "Iranian Currency" in Give setting.
1455
 * Version 1.8.17 automatic updates
1456
 *
1457
 * @since 1.8.17
1458
 */
1459
function give_v1817_upgrades() {
1460
	$give_settings = give_get_settings();
1461
1462
	if ( 'RIAL' === $give_settings['currency'] ) {
1463
		$give_settings['currency'] = 'IRR';
1464
		update_option( 'give_settings', $give_settings );
1465
	}
1466
}
1467
1468
/**
1469
 * Process Clean up of User Roles for more flexibility.
1470
 *
1471
 * @since 1.8.17
1472
 */
1473
function give_v1817_process_cleanup_user_roles() {
1474
1475
	global $wp_roles;
1476
1477
	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...
1478
		return;
1479
	}
1480
1481
	// Add Capabilities to user roles as required.
1482
	$add_caps = array(
1483
		'administrator' => array(
1484
			'view_give_payments',
1485
		),
1486
	);
1487
1488
	// Remove Capabilities to user roles as required.
1489
	$remove_caps = array(
1490
		'give_manager' => array(
1491
			'edit_others_pages',
1492
			'edit_others_posts',
1493
			'delete_others_pages',
1494
			'delete_others_posts',
1495
			'manage_categories',
1496
			'import',
1497
			'export',
1498
		),
1499
	);
1500
1501
	foreach ( $add_caps as $role => $caps ) {
1502
		foreach ( $caps as $cap ) {
1503
			$wp_roles->add_cap( $role, $cap );
1504
		}
1505
	}
1506
1507
	foreach ( $remove_caps as $role => $caps ) {
1508
		foreach ( $caps as $cap ) {
1509
			$wp_roles->remove_cap( $role, $cap );
1510
		}
1511
	}
1512
1513
}
1514
1515
/**
1516
 * Upgrade Routine - Clean up of User Roles for more flexibility.
1517
 *
1518
 * @since 1.8.17
1519
 */
1520
function give_v1817_cleanup_user_roles() {
1521
	/* @var Give_Updates $give_updates */
1522
	$give_updates = Give_Updates::get_instance();
1523
1524
	give_v1817_process_cleanup_user_roles();
1525
1526
	$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...
1527
1528
	// Create Give plugin roles.
1529
	$roles = new Give_Roles();
1530
	$roles->add_roles();
1531
	$roles->add_caps();
1532
1533
	give_set_upgrade_complete( 'v1817_cleanup_user_roles' );
1534
}
1535
1536
/**
1537
 * Automatic Upgrade for release 1.8.18.
1538
 *
1539
 * @since 1.8.18
1540
 */
1541
function give_v1818_upgrades() {
1542
1543
	// Remove email_access_installed from give_settings.
1544
	give_delete_option( 'email_access_installed' );
1545
}
1546
1547
/**
1548
 * Upgrade Routine - Assigns Custom Amount to existing donation of type set donation.
1549
 *
1550
 * @since 1.8.18
1551
 */
1552
function give_v1818_assign_custom_amount_set_donation() {
1553
1554
	/* @var Give_Updates $give_updates */
1555
	$give_updates   = Give_Updates::get_instance();
1556
1557
	$donations = new WP_Query( array(
1558
			'paged'          => $give_updates->step,
1559
			'status'         => 'any',
1560
			'order'          => 'ASC',
1561
			'post_type'      => array( 'give_payment' ),
1562
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1563
		)
1564
	);
1565
1566
	if ( $donations->have_posts() ) {
1567
		$give_updates->set_percentage( $donations->found_posts, $give_updates->step * 100 );
1568
1569
		while ( $donations->have_posts() ) {
1570
			$donations->the_post();
1571
1572
			$form          = new Give_Donate_Form( give_get_meta( get_the_ID(), '_give_payment_form_id', true ) );
1573
			$donation_meta = give_get_payment_meta( get_the_ID() );
1574
1575
			// Update Donation meta with price_id set as custom, only if it is:
1576
			// 1. Donation Type = Set Donation.
1577
			// 2. Donation Price Id is not set to custom.
1578
			// 3. Form has not enabled custom price and donation amount assures that it is custom amount.
1579
			if (
1580
				$form->ID &&
1581
				$form->is_set_type_donation_form() &&
1582
				( 'custom' !== $donation_meta['price_id'] ) &&
1583
				$form->is_custom_price( give_get_meta( get_the_ID(), '_give_payment_total', true ) )
1584
			) {
1585
				$donation_meta['price_id'] = 'custom';
1586
				give_update_meta( get_the_ID(), '_give_payment_meta', $donation_meta );
1587
				give_update_meta( get_the_ID(), '_give_payment_price_id', 'custom' );
1588
			}
1589
		}
1590
1591
		wp_reset_postdata();
1592
	} else {
1593
		// Update Ran Successfully.
1594
		give_set_upgrade_complete( 'v1818_assign_custom_amount_set_donation' );
1595
	}
1596
}
1597
1598
/**
1599
 * Upgrade Routine - Removed Give Worker caps.
1600
 *
1601
 * See: https://github.com/WordImpress/Give/issues/2476
1602
 *
1603
 * @since 1.8.18
1604
 */
1605
function give_v1818_give_worker_role_cleanup(){
1606
1607
	/* @var Give_Updates $give_updates */
1608
	$give_updates = Give_Updates::get_instance();
1609
1610
	global $wp_roles;
1611
1612
	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...
1613
		return;
1614
	}
1615
1616
	// Remove Capabilities to user roles as required.
1617
	$remove_caps = array(
1618
		'give_worker' => array(
1619
			'delete_give_payments',
1620
			'delete_others_give_payments',
1621
			'delete_private_give_payments',
1622
			'delete_published_give_payments',
1623
			'edit_others_give_payments',
1624
			'edit_private_give_payments',
1625
			'edit_published_give_payments',
1626
			'read_private_give_payments',
1627
		),
1628
	);
1629
1630
	foreach ( $remove_caps as $role => $caps ) {
1631
		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...
1632
			$wp_roles->remove_cap( $role, $cap );
1633
		}
1634
	}
1635
1636
	$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...
1637
1638
	// Create Give plugin roles.
1639
	$roles = new Give_Roles();
1640
	$roles->add_roles();
1641
	$roles->add_caps();
1642
1643
	give_set_upgrade_complete( 'v1818_give_worker_role_cleanup' );
1644
}
1645
1646
/**
1647
 *
1648
 * Upgrade form metadata for new metabox settings.
1649
 *
1650
 * @since  2.0
1651
 * @return void
1652
 */
1653
function give_v20_upgrades_form_metadata_callback() {
1654
	$give_updates = Give_Updates::get_instance();
1655
1656
	// form query
1657
	$forms = new WP_Query( array(
1658
			'paged'          => $give_updates->step,
1659
			'status'         => 'any',
1660
			'order'          => 'ASC',
1661
			'post_type'      => 'give_forms',
1662
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1663
		)
1664
	);
1665
1666
	if ( $forms->have_posts() ) {
1667
		$give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 100 ) );
1668
1669
		while ( $forms->have_posts() ) {
1670
			$forms->the_post();
1671
			global $post;
1672
1673
			// Update offline instruction email notification status.
1674
			$offline_instruction_notification_status = get_post_meta( get_the_ID(), '_give_customize_offline_donations', true );
1675
			$offline_instruction_notification_status = give_is_setting_enabled( $offline_instruction_notification_status, array(
0 ignored issues
show
Documentation introduced by
array('enabled', 'global') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1676
				'enabled',
1677
				'global',
1678
			) )
1679
				? $offline_instruction_notification_status
1680
				: 'global';
1681
			update_post_meta( get_the_ID(), '_give_offline-donation-instruction_notification', $offline_instruction_notification_status );
1682
1683
			// Update offline instruction email message.
1684
			update_post_meta(
1685
				get_the_ID(),
1686
				'_give_offline-donation-instruction_email_message',
1687
				get_post_meta(
1688
					get_the_ID(),
1689
					// @todo: Delete this option later ( version > 2.0 ).
1690
					'_give_offline_donation_email',
1691
					true
1692
				)
1693
			);
1694
1695
			// Update offline instruction email subject.
1696
			update_post_meta(
1697
				get_the_ID(),
1698
				'_give_offline-donation-instruction_email_subject',
1699
				get_post_meta(
1700
					get_the_ID(),
1701
					// @todo: Delete this option later ( version > 2.0 ).
1702
					'_give_offline_donation_subject',
1703
					true
1704
				)
1705
			);
1706
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
1707
1708
		}// End while().
1709
1710
		wp_reset_postdata();
1711
	} else {
1712
		// No more forms found, finish up.
1713
		give_set_upgrade_complete( 'v20_upgrades_form_metadata' );
1714
	}
1715
}
1716
1717
1718
/**
1719
 * Upgrade payment metadata for new metabox settings.
1720
 *
1721
 * @since  2.0
1722
 * @global wpdb $wpdb
1723
 * @return void
1724
 */
1725
function give_v20_upgrades_payment_metadata_callback() {
1726
	global $wpdb;
1727
	$give_updates = Give_Updates::get_instance();
1728
1729
	// form query
1730
	$forms = new WP_Query( array(
1731
			'paged'          => $give_updates->step,
1732
			'status'         => 'any',
1733
			'order'          => 'ASC',
1734
			'post_type'      => 'give_payment',
1735
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1736
		)
1737
	);
1738
1739
	if ( $forms->have_posts() ) {
1740
		$give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 100 ) );
1741
1742
		while ( $forms->have_posts() ) {
1743
			$forms->the_post();
1744
			global $post;
1745
1746
			// Split _give_payment_meta meta.
1747
			// @todo Remove _give_payment_meta after releases 2.0
1748
			$payment_meta = give_get_meta( $post->ID, '_give_payment_meta', true );
1749
1750
			if ( ! empty( $payment_meta ) ) {
1751
				_give_20_bc_split_and_save_give_payment_meta( $post->ID, $payment_meta );
1752
			}
1753
1754
			$deprecated_meta_keys = array(
1755
				'_give_payment_customer_id' => '_give_payment_donor_id',
1756
				'_give_payment_user_email'  => '_give_payment_donor_email',
1757
				'_give_payment_user_ip'     => '_give_payment_donor_ip',
1758
			);
1759
1760 View Code Duplication
			foreach ( $deprecated_meta_keys as $old_meta_key => $new_meta_key ) {
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...
1761
				// Do not add new meta key if already exist.
1762
				if ( $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id=%d AND meta_key=%s", $post->ID, $new_meta_key ) ) ) {
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...
1763
					continue;
1764
				}
1765
1766
				$wpdb->insert(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
1767
					$wpdb->postmeta,
1768
					array(
1769
						'post_id' => $post->ID,
1770
						'meta_key' => $new_meta_key,
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
1771
						'meta_value' => give_get_meta( $post->ID, $old_meta_key, true )
0 ignored issues
show
introduced by
Detected usage of meta_value, possible slow query.
Loading history...
1772
					)
1773
				);
1774
			}
1775
1776
			// Bailout
1777 View Code Duplication
			if ( $donor_id = give_get_meta( $post->ID, '_give_payment_donor_id', 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...
1778
				/* @var Give_Donor $donor */
1779
				$donor = new Give_Donor( $donor_id );
1780
1781
				$address['line1']   = give_get_meta( $post->ID, '_give_donor_billing_address1', true, '' );
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1782
				$address['line2']   = give_get_meta( $post->ID, '_give_donor_billing_address2', true, '' );
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1783
				$address['city']    = give_get_meta( $post->ID, '_give_donor_billing_city', true, '' );
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1784
				$address['state']   = give_get_meta( $post->ID, '_give_donor_billing_state', true, '' );
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1785
				$address['zip']     = give_get_meta( $post->ID, '_give_donor_billing_zip', true, '' );
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1786
				$address['country'] = give_get_meta( $post->ID, '_give_donor_billing_country', true, '' );
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1787
1788
				// Save address.
1789
				$donor->add_address( 'billing[]', $address );
1790
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1791
1792
		}// End while().
1793
1794
		wp_reset_postdata();
1795
	} else {
1796
		// @todo Delete user id meta after releases 2.0
1797
		// $wpdb->get_var( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key=%s", '_give_payment_user_id' ) );
1798
1799
		// No more forms found, finish up.
1800
		give_set_upgrade_complete( 'v20_upgrades_payment_metadata' );
1801
	}
1802
}
1803
1804
1805
/**
1806
 * Upgrade logs data.
1807
 *
1808
 * @since  2.0
1809
 * @return void
1810
 */
1811 View Code Duplication
function give_v20_logs_upgrades_callback() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
1812
	global $wpdb;
1813
	$give_updates = Give_Updates::get_instance();
1814
1815
	// form query
1816
	$forms = new WP_Query( array(
1817
			'paged'          => $give_updates->step,
1818
			'order'          => 'DESC',
1819
			'post_type'      => 'give_log',
1820
			'post_status'    => 'any',
1821
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1822
		)
1823
	);
1824
1825
	if ( $forms->have_posts() ) {
1826
		$give_updates->set_percentage( $forms->found_posts, $give_updates->step * 100 );
1827
1828
		while ( $forms->have_posts() ) {
1829
			$forms->the_post();
1830
			global $post;
1831
1832
			if( $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}give_logs WHERE ID=%d", $post->ID ) ) ) {
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...
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...
1833
				continue;
1834
			}
1835
1836
			$term      = get_the_terms( $post->ID, 'give_log_type' );
1837
			$term      = ! is_wp_error( $term ) && ! empty( $term ) ? $term[0] : array();
1838
			$term_name = ! empty( $term ) ? $term->slug : '';
1839
1840
			$log_data = array(
1841
				'ID'           => $post->ID,
1842
				'log_title'    => $post->post_title,
1843
				'log_content'  => $post->post_content,
1844
				'log_parent'   => 0,
1845
				'log_type'     => $term_name,
1846
				'log_date'     => $post->post_date,
1847
				'log_date_gmt' => $post->post_date_gmt,
1848
			);
1849
			$log_meta = array();
1850
1851
			if ( $old_log_meta = get_post_meta( $post->ID ) ) {
1852
				foreach ( $old_log_meta as $meta_key => $meta_value ) {
1853
					switch ( $meta_key ) {
1854
						case '_give_log_payment_id':
1855
							$log_data['log_parent']        = current( $meta_value );
1856
							$log_meta['_give_log_form_id'] = $post->post_parent;
1857
							break;
1858
1859
						default:
1860
							$log_meta[ $meta_key ] = current( $meta_value );
1861
					}
1862
				}
1863
			}
1864
1865
			if ( 'api_request' === $term_name ) {
1866
				$log_meta['_give_log_api_query'] = $post->post_excerpt;
1867
			}
1868
1869
			$wpdb->insert( "{$wpdb->prefix}give_logs", $log_data );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
1870
1871
			if ( ! empty( $log_meta ) ) {
1872
				foreach ( $log_meta as $meta_key => $meta_value ) {
1873
					Give()->logs->logmeta_db->update_meta( $post->ID, $meta_key, $meta_value );
1874
				}
1875
			}
1876
1877
			$logIDs[] = $post->ID;
1878
		}// End while().
1879
1880
		wp_reset_postdata();
1881
	} else {
1882
		// @todo: Delete terms and taxonomy after releases 2.0.
1883
		/*$terms = get_terms( 'give_log_type', array( 'fields' => 'ids', 'hide_empty' => false ) );
1884
		if ( ! empty( $terms ) ) {
1885
			foreach ( $terms as $term ) {
1886
				wp_delete_term( $term, 'give_log_type' );
1887
			}
1888
		}*/
1889
1890
		// @todo: Delete logs after releases 2.0.
1891
		/*$logIDs = get_posts( array(
1892
				'order'          => 'DESC',
1893
				'post_type'      => 'give_log',
1894
				'post_status'    => 'any',
1895
				'posts_per_page' => - 1,
1896
				'fields'         => 'ids',
1897
			)
1898
		);*/
1899
1900
		/*if ( ! empty( $logIDs ) ) {
1901
			foreach ( $logIDs as $log ) {
1902
				// Delete term relationship and posts.
1903
				wp_delete_object_term_relationships( $log, 'give_log_type' );
1904
				wp_delete_post( $log, true );
1905
			}
1906
		}*/
1907
1908
		// @todo: Unregister taxonomy after releases 2.0.
1909
		/*unregister_taxonomy( 'give_log_type' );*/
1910
1911
		// Delete log cache.
1912
		Give()->logs->delete_cache();
1913
1914
		// No more forms found, finish up.
1915
		give_set_upgrade_complete( 'v20_logs_upgrades' );
1916
	}
1917
}
1918
1919
1920
/**
1921
 * Move payment and form metadata to new table
1922
 *
1923
 * @since  2.0
1924
 * @return void
1925
 */
1926 View Code Duplication
function give_v20_move_metadata_into_new_table_callback() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
1927
	global $wpdb;
1928
	$give_updates = Give_Updates::get_instance();
1929
1930
	// form query
1931
	$payments = new WP_Query( array(
1932
			'paged'          => $give_updates->step,
1933
			'status'         => 'any',
1934
			'order'          => 'ASC',
1935
			'post_type'      => array( 'give_forms', 'give_payment' ),
1936
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1937
		)
1938
	);
1939
1940
	if ( $payments->have_posts() ) {
1941
		$give_updates->set_percentage( $payments->found_posts, $give_updates->step * 100 );
1942
1943
		while ( $payments->have_posts() ) {
1944
			$payments->the_post();
1945
			global $post;
1946
1947
			$meta_data = $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...
1948
				$wpdb->prepare(
1949
					"SELECT * FROM $wpdb->postmeta where post_id=%d",
1950
					get_the_ID()
1951
				),
1952
				ARRAY_A
1953
			);
1954
1955
			if ( ! empty( $meta_data ) ) {
1956
				foreach ( $meta_data as $index => $data ) {
1957
					// Check for duplicate meta values.
1958
					if( $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM " . ( 'give_forms' === $post->post_type ? $wpdb->formmeta : $wpdb->paymentmeta ) .  " WHERE meta_id=%d", $data['meta_id'] ), ARRAY_A ) ) {
0 ignored issues
show
Unused Code introduced by
$result 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
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
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...
Coding Style Comprehensibility introduced by
The string literal SELECT * FROM does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal WHERE meta_id=%d does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
1959
						continue;
1960
					}
1961
					
1962
					switch ( $post->post_type ) {
1963
						case 'give_forms':
1964
							$data['form_id'] = $data['post_id'];
1965
							unset( $data['post_id'] );
1966
1967
							Give()->form_meta->insert( $data );
1968
							// @todo: delete form meta from post meta table after releases 2.0.
1969
							/*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
1970
1971
							break;
1972
1973
						case 'give_payment':
1974
							$data['payment_id'] = $data['post_id'];
1975
							unset( $data['post_id'] );
1976
1977
							Give()->payment_meta->insert( $data );
1978
1979
							// @todo: delete donation meta from post meta table after releases 2.0.
1980
							/*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
1981
1982
							break;
1983
					}
1984
				}
1985
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1986
1987
		}// End while().
1988
1989
		wp_reset_postdata();
1990
	} else {
1991
		// No more forms found, finish up.
1992
		give_set_upgrade_complete( 'v20_move_metadata_into_new_table' );
1993
	}
1994
1995
}
1996
1997
/**
1998
 * Upgrade routine for splitting donor name into first name and last name.
1999
 *
2000
 * @since 2.0
2001
 *
2002
 * @return void
2003
 */
2004
function give_v20_upgrades_donor_name() {
2005
	/* @var Give_Updates $give_updates */
2006
	$give_updates = Give_Updates::get_instance();
2007
2008
	$donors = Give()->donors->get_donors( array(
2009
		'paged'  => $give_updates->step,
2010
		'number' => 100,
2011
	) );
2012
2013
	if ( $donors ) {
2014
		$give_updates->set_percentage( count( $donors ), $give_updates->step * 100 );
2015
		// Loop through Donors
2016
		foreach ( $donors as $donor ) {
2017
2018
			$donor_name       = explode( ' ', $donor->name, 2 );
2019
			$donor_first_name = Give()->donor_meta->get_meta( $donor->id, '_give_donor_first_name' );
2020
			$donor_last_name  = Give()->donor_meta->get_meta( $donor->id, '_give_donor_last_name' );
2021
2022
			// If first name meta of donor is not created, then create it.
2023 View Code Duplication
			if ( ! $donor_first_name && isset( $donor_name[0] ) ) {
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...
2024
				Give()->donor_meta->add_meta( $donor->id, '_give_donor_first_name', $donor_name[0] );
2025
			}
2026
2027
			// If last name meta of donor is not created, then create it.
2028 View Code Duplication
			if ( ! $donor_last_name && isset( $donor_name[1] ) ) {
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...
2029
				Give()->donor_meta->add_meta( $donor->id, '_give_donor_last_name', $donor_name[1] );
2030
			}
2031
2032
			// If Donor is connected with WP User then update user meta.
2033 View Code Duplication
			if ( $donor->user_id ) {
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...
2034
				if ( isset( $donor_name[0] ) ) {
2035
					update_user_meta( $donor->user_id, 'first_name', $donor_name[0] );
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
2036
				}
2037
				if ( isset( $donor_name[1] ) ) {
2038
					update_user_meta( $donor->user_id, 'last_name', $donor_name[1] );
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
2039
				}
2040
			}
2041
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2042
2043
	} else {
2044
		// The Update Ran.
2045
		give_set_upgrade_complete( 'v20_upgrades_donor_name' );
2046
	}
2047
2048
}
2049
2050
/**
2051
 * Upgrade routine for user addresses.
2052
 *
2053
 * @since 2.0
2054
 * @global wpdb $wpdb
2055
 *
2056
 * @return void
2057
 */
2058
function give_v20_upgrades_user_address() {
2059
	global $wpdb;
2060
2061
	/* @var Give_Updates $give_updates */
2062
	$give_updates = Give_Updates::get_instance();
2063
2064
	/* @var WP_User_Query $user_query */
2065
	$user_query = new WP_User_Query(
2066
		array(
2067
			'number' => 100,
2068
			'paged'  => $give_updates->step,
2069
		)
2070
	);
2071
2072
	$users = $user_query->get_results();
2073
2074
	if ( $users ) {
2075
		$give_updates->set_percentage( $user_query->get_total(), $give_updates->step * 100 );
2076
2077
		// Loop through Donors
2078
		foreach ( $users as $user ) {
2079
			/* @var Give_Donor $donor */
2080
			$donor = new Give_Donor( $user->ID, true );
2081
2082
			if ( ! $donor->id ) {
2083
				continue;
2084
			}
2085
2086
			$address = $wpdb->get_var(
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...
2087
				$wpdb->prepare(
2088
					"
2089
					SELECT meta_value 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...
2090
					WHERE user_id=%s
2091
					AND meta_key=%s
2092
					",
2093
					$user->ID,
2094
					'_give_user_address'
2095
				)
2096
			);
2097
2098 View Code Duplication
			if ( ! empty( $address ) ) {
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...
2099
				$address = maybe_unserialize( $address );
2100
				$donor->add_address( 'personal', $address );
2101
				$donor->add_address( 'billing[]', $address );
2102
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
2103
2104
				// @todo: delete _give_user_address from user meta after releases 2.0.
2105
				/*delete_user_meta( $user->ID, '_give_user_address' );*/
2106
			}
2107
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2108
2109
	} else {
2110
		// The Update Ran.
2111
		give_set_upgrade_complete( 'v20_upgrades_user_address' );
2112
	}
2113
2114
}
2115
2116
/**
2117
 * Upgrade logs data.
2118
 *
2119
 * @since  2.0
2120
 * @global wpdb $wpdb
2121
 * @return void
2122
 */
2123
function give_v20_rename_donor_tables_callback() {
2124
	global $wpdb;
2125
2126
	/* @var Give_Updates $give_updates */
2127
	$give_updates = Give_Updates::get_instance();
2128
2129
	$tables = array(
2130
		"{$wpdb->prefix}give_customers"    => "{$wpdb->prefix}give_donors",
2131
		"{$wpdb->prefix}give_customermeta" => "{$wpdb->prefix}give_donormeta",
2132
	);
2133
2134
	// Alter customer table
2135
	foreach ( $tables as $old_table => $new_table ) {
2136
		if (
2137
			$wpdb->query( $wpdb->prepare( "SHOW TABLES LIKE %s", $old_table ) ) &&
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...
Coding Style Comprehensibility introduced by
The string literal SHOW TABLES LIKE %s does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
2138
			! $wpdb->query( $wpdb->prepare( "SHOW TABLES LIKE %s", $new_table ) )
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...
Coding Style Comprehensibility introduced by
The string literal SHOW TABLES LIKE %s does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
2139
		) {
2140
			$wpdb->query( "ALTER TABLE {$old_table} RENAME TO {$new_table}" );
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...
introduced by
Attempting a database schema change is highly discouraged.
Loading history...
2141
2142
			if ( "{$wpdb->prefix}give_donormeta" === $new_table ) {
2143
				$wpdb->query( "ALTER TABLE {$new_table} CHANGE COLUMN customer_id donor_id bigint(20)" );
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...
introduced by
Attempting a database schema change is highly discouraged.
Loading history...
2144
			}
2145
		}
2146
	}
2147
2148
	$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...
2149
2150
	// No more forms found, finish up.
2151
	give_set_upgrade_complete( 'v20_rename_donor_tables' );
2152
2153
	// Re initiate donor classes.
2154
	Give()->donors     = new Give_DB_Donors();
2155
	Give()->donor_meta = new Give_DB_Donor_Meta();
2156
}
2157
2158
2159
/**
2160
 * Create missing meta tables.
2161
 *
2162
 * @since  2.0.1
2163
 * @global wpdb $wpdb
2164
 * @return void
2165
 */
2166
function give_v201_create_tables(){
2167
	global $wpdb;
2168
2169
	if ( ! $wpdb->query( $wpdb->prepare( "SHOW TABLES LIKE %s", "{$wpdb->prefix}give_paymentmeta" ) ) ) {
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...
Coding Style Comprehensibility introduced by
The string literal SHOW TABLES LIKE %s does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
2170
		Give()->payment_meta->create_table();
2171
	}
2172
2173
	if ( ! $wpdb->query( $wpdb->prepare( "SHOW TABLES LIKE %s", "{$wpdb->prefix}give_formmeta" ) ) ) {
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...
Coding Style Comprehensibility introduced by
The string literal SHOW TABLES LIKE %s does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
2174
		Give()->form_meta->create_table();
2175
	}
2176
2177
	if ( ! $wpdb->query( $wpdb->prepare( "SHOW TABLES LIKE %s", "{$wpdb->prefix}give_logs" ) ) ) {
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...
Coding Style Comprehensibility introduced by
The string literal SHOW TABLES LIKE %s does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
2178
		Give()->logs->log_db->create_table();
2179
	}
2180
2181
	if ( ! $wpdb->query( $wpdb->prepare( "SHOW TABLES LIKE %s", "{$wpdb->prefix}give_logmeta" ) ) ) {
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...
Coding Style Comprehensibility introduced by
The string literal SHOW TABLES LIKE %s does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
2182
		Give()->logs->logmeta_db->create_table();
2183
	}
2184
}
2185
2186
/**
2187
 * Upgrade payment metadata for new metabox settings.
2188
 *
2189
 * @since  2.0.1
2190
 * @global wpdb $wpdb
2191
 * @return void
2192
 */
2193
function give_v201_upgrades_payment_metadata_callback() {
2194
	global $wpdb;
2195
	$give_updates = Give_Updates::get_instance();
2196
2197
	// form query
2198
	$forms = new WP_Query( array(
2199
			'paged'          => $give_updates->step,
2200
			'status'         => 'any',
2201
			'order'          => 'ASC',
2202
			'post_type'      => 'give_payment',
2203
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
2204
			'date_query' => array(
2205
				'before'    => array(
2206
					'year'  => 2018,
2207
					'month' => 1,
2208
					'day'   => 8,
2209
				),
2210
				'inclusive' => true,
2211
			)
2212
		)
2213
	);
2214
2215
	if ( $forms->have_posts() ) {
2216
		$give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 100 ) );
2217
2218
		while ( $forms->have_posts() ) {
2219
			$forms->the_post();
2220
			global $post;
2221
2222
			// Do not add new meta keys if already refactored.
2223
			if ( $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id=%d AND meta_key=%s", $post->ID, '_give_payment_donor_id' ) ) ) {
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...
2224
				continue;
2225
			}
2226
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
2227
2228
			// Split _give_payment_meta meta.
2229
			// @todo Remove _give_payment_meta after releases 2.0
2230
			$payment_meta = give_get_meta( $post->ID, '_give_payment_meta', true );
2231
2232
			if ( ! empty( $payment_meta ) ) {
2233
				_give_20_bc_split_and_save_give_payment_meta( $post->ID, $payment_meta );
2234
			}
2235
2236
			$deprecated_meta_keys = array(
2237
				'_give_payment_customer_id' => '_give_payment_donor_id',
2238
				'_give_payment_user_email'  => '_give_payment_donor_email',
2239
				'_give_payment_user_ip'     => '_give_payment_donor_ip',
2240
			);
2241
2242 View Code Duplication
			foreach ( $deprecated_meta_keys as $old_meta_key => $new_meta_key ) {
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...
2243
				// Do not add new meta key if already exist.
2244
				if ( $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id=%d AND meta_key=%s", $post->ID, $new_meta_key ) ) ) {
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...
2245
					continue;
2246
				}
2247
2248
				$wpdb->insert(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
2249
					$wpdb->postmeta,
2250
					array(
2251
						'post_id' => $post->ID,
2252
						'meta_key' => $new_meta_key,
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
2253
						'meta_value' => give_get_meta( $post->ID, $old_meta_key, true )
0 ignored issues
show
introduced by
Detected usage of meta_value, possible slow query.
Loading history...
2254
					)
2255
				);
2256
			}
2257
2258
			// Bailout
2259 View Code Duplication
			if ( $donor_id = give_get_meta( $post->ID, '_give_payment_donor_id', 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...
2260
				/* @var Give_Donor $donor */
2261
				$donor = new Give_Donor( $donor_id );
2262
2263
				$address['line1']   = give_get_meta( $post->ID, '_give_donor_billing_address1', true, '' );
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
2264
				$address['line2']   = give_get_meta( $post->ID, '_give_donor_billing_address2', true, '' );
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
2265
				$address['city']    = give_get_meta( $post->ID, '_give_donor_billing_city', true, '' );
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
2266
				$address['state']   = give_get_meta( $post->ID, '_give_donor_billing_state', true, '' );
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
2267
				$address['zip']     = give_get_meta( $post->ID, '_give_donor_billing_zip', true, '' );
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
2268
				$address['country'] = give_get_meta( $post->ID, '_give_donor_billing_country', true, '' );
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
2269
2270
				// Save address.
2271
				$donor->add_address( 'billing[]', $address );
2272
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2273
2274
		}// End while().
2275
2276
		wp_reset_postdata();
2277
	} else {
2278
		// @todo Delete user id meta after releases 2.0
2279
		// $wpdb->get_var( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key=%s", '_give_payment_user_id' ) );
2280
2281
		// No more forms found, finish up.
2282
		give_set_upgrade_complete( 'v201_upgrades_payment_metadata' );
2283
	}
2284
}
2285
2286
/**
2287
 * Move payment and form metadata to new table
2288
 *
2289
 * @since  2.0.1
2290
 * @return void
2291
 */
2292 View Code Duplication
function give_v201_move_metadata_into_new_table_callback() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
2293
	global $wpdb;
2294
	$give_updates = Give_Updates::get_instance();
2295
2296
	// form query
2297
	$payments = new WP_Query( array(
2298
			'paged'          => $give_updates->step,
2299
			'status'         => 'any',
2300
			'order'          => 'ASC',
2301
			'post_type'      => array( 'give_forms', 'give_payment' ),
2302
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
2303
		)
2304
	);
2305
2306
	if ( $payments->have_posts() ) {
2307
		$give_updates->set_percentage( $payments->found_posts, $give_updates->step * 100 );
2308
2309
		while ( $payments->have_posts() ) {
2310
			$payments->the_post();
2311
			global $post;
2312
2313
			$meta_data = $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...
2314
				$wpdb->prepare(
2315
					"SELECT * FROM $wpdb->postmeta where post_id=%d",
2316
					get_the_ID()
2317
				),
2318
				ARRAY_A
2319
			);
2320
2321
			if ( ! empty( $meta_data ) ) {
2322
				foreach ( $meta_data as $index => $data ) {
2323
					// Check for duplicate meta values.
2324
					if( $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM " . ( 'give_forms' === $post->post_type ? $wpdb->formmeta : $wpdb->paymentmeta ) .  " WHERE meta_id=%d", $data['meta_id'] ), ARRAY_A ) ) {
0 ignored issues
show
Unused Code introduced by
$result 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
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
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...
Coding Style Comprehensibility introduced by
The string literal SELECT * FROM does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal WHERE meta_id=%d does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
2325
						continue;
2326
					}
2327
2328
					switch ( $post->post_type ) {
2329
						case 'give_forms':
2330
							$data['form_id'] = $data['post_id'];
2331
							unset( $data['post_id'] );
2332
2333
							Give()->form_meta->insert( $data );
2334
							// @todo: delete form meta from post meta table after releases 2.0.
2335
							/*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2336
2337
							break;
2338
2339
						case 'give_payment':
2340
							$data['payment_id'] = $data['post_id'];
2341
							unset( $data['post_id'] );
2342
2343
							Give()->payment_meta->insert( $data );
2344
2345
							// @todo: delete donation meta from post meta table after releases 2.0.
2346
							/*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2347
2348
							break;
2349
					}
2350
				}
2351
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2352
2353
		}// End while().
2354
2355
		wp_reset_postdata();
2356
	} else {
2357
		// No more forms found, finish up.
2358
		give_set_upgrade_complete( 'v201_move_metadata_into_new_table' );
2359
	}
2360
2361
}
2362
2363
/**
2364
 * Move data to new log table.
2365
 *
2366
 * @since  2.0.1
2367
 * @return void
2368
 */
2369 View Code Duplication
function give_v201_logs_upgrades_callback() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
2370
	global $wpdb;
2371
	$give_updates = Give_Updates::get_instance();
2372
2373
	// form query
2374
	$forms = new WP_Query( array(
2375
			'paged'          => $give_updates->step,
2376
			'order'          => 'DESC',
2377
			'post_type'      => 'give_log',
2378
			'post_status'    => 'any',
2379
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
2380
		)
2381
	);
2382
2383
	if ( $forms->have_posts() ) {
2384
		$give_updates->set_percentage( $forms->found_posts, $give_updates->step * 100 );
2385
2386
		while ( $forms->have_posts() ) {
2387
			$forms->the_post();
2388
			global $post;
2389
2390
			if( $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}give_logs WHERE ID=%d", $post->ID ) ) ) {
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...
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...
2391
				continue;
2392
			}
2393
2394
			$term      = get_the_terms( $post->ID, 'give_log_type' );
2395
			$term      = ! is_wp_error( $term ) && ! empty( $term ) ? $term[0] : array();
2396
			$term_name = ! empty( $term ) ? $term->slug : '';
2397
2398
			$log_data = array(
2399
				'ID'           => $post->ID,
2400
				'log_title'    => $post->post_title,
2401
				'log_content'  => $post->post_content,
2402
				'log_parent'   => 0,
2403
				'log_type'     => $term_name,
2404
				'log_date'     => $post->post_date,
2405
				'log_date_gmt' => $post->post_date_gmt,
2406
			);
2407
			$log_meta = array();
2408
2409
			if ( $old_log_meta = get_post_meta( $post->ID ) ) {
2410
				foreach ( $old_log_meta as $meta_key => $meta_value ) {
2411
					switch ( $meta_key ) {
2412
						case '_give_log_payment_id':
2413
							$log_data['log_parent']        = current( $meta_value );
2414
							$log_meta['_give_log_form_id'] = $post->post_parent;
2415
							break;
2416
2417
						default:
2418
							$log_meta[ $meta_key ] = current( $meta_value );
2419
					}
2420
				}
2421
			}
2422
2423
			if ( 'api_request' === $term_name ) {
2424
				$log_meta['_give_log_api_query'] = $post->post_excerpt;
2425
			}
2426
2427
			$wpdb->insert( "{$wpdb->prefix}give_logs", $log_data );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
2428
2429
			if ( ! empty( $log_meta ) ) {
2430
				foreach ( $log_meta as $meta_key => $meta_value ) {
2431
					Give()->logs->logmeta_db->update_meta( $post->ID, $meta_key, $meta_value );
2432
				}
2433
			}
2434
2435
			$logIDs[] = $post->ID;
2436
		}// End while().
2437
2438
		wp_reset_postdata();
2439
	} else {
2440
		// Delete log cache.
2441
		Give()->logs->delete_cache();
2442
2443
		// No more forms found, finish up.
2444
		give_set_upgrade_complete( 'v201_logs_upgrades' );
2445
	}
2446
}
2447
2448
function give_v201_add_missing_donors_callback(){
2449
	global $wpdb;
2450
2451
	$customers  = wp_list_pluck( $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}give_customers" ), 'id' );
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...
2452
	$donors     = wp_list_pluck( $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}give_donors" ), 'id' );
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...
2453
	$donor_data = array();
2454
2455
	if ( $missing_donors = array_diff( $customers, $donors ) ) {
2456
		foreach ( $missing_donors as $donor_id ) {
2457
			$donor_data[] = array(
2458
				'info' => $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}give_customers WHERE id=%d", $donor_id ) ),
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...
2459
				'meta' => $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}give_customermeta WHERE customer_id=%d", $donor_id ) ),
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...
2460
2461
			);
2462
		}
2463
	}
2464
2465
	if( ! empty( $donor_data ) ) {
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...
2466
		$donor_table_name = Give()->donors->table_name;
2467
		$donor_meta_table_name = Give()->donor_meta->table_name;
2468
2469
		Give()->donors->table_name = "{$wpdb->prefix}give_donors";
2470
		Give()->donor_meta->table_name = "{$wpdb->prefix}give_donormeta";
2471
2472
		foreach ( $donor_data as $donor ) {
2473
			$donor['info'][0] = (array) $donor['info'][0];
2474
2475
			// Prevent duplicate meta id issue.
2476
			if( $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}give_donors WHERE id=%d", $donor['info'][0]['id'] ) ) ){
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...
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...
2477
				continue;
2478
			}
2479
2480
			$donor_id = Give()->donors->add( $donor['info'][0] );
2481
2482
			if( ! empty( $donor['meta'] ) ) {
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...
2483
				foreach ( $donor['meta'] as $donor_meta ) {
2484
					$donor_meta = (array) $donor_meta;
2485
2486
					// Prevent duplicate meta id issue.
2487
					if( $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}give_donormeta WHERE meta_id=%d", $donor_meta['meta_id'] ) ) ){
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...
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...
2488
						unset( $donor_meta['meta_id'] );
2489
					}
2490
2491
					$donor_meta['donor_id'] = $donor_meta['customer_id'];
2492
					unset( $donor_meta['customer_id'] );
2493
2494
					Give()->donor_meta->insert( $donor_meta );
2495
				}
2496
			}
2497
2498
			/**
2499
			 * Fix donor name and address
2500
			 */
2501
			$donor = new Give_Donor( $donor_id );
2502
2503
			$address = $wpdb->get_var(
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...
2504
				$wpdb->prepare(
2505
					"
2506
					SELECT meta_value 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...
2507
					WHERE user_id=%s
2508
					AND meta_key=%s
2509
					",
2510
					$donor['info'][0]['user_id'],
2511
					'_give_user_address'
2512
				)
2513
			);
2514
2515 View Code Duplication
			if ( ! empty( $address ) ) {
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...
2516
				$address = maybe_unserialize( $address );
2517
				$donor->add_address( 'personal', $address );
2518
				$donor->add_address( 'billing[]', $address );
2519
			}
2520
2521
			$donor_name       = explode( ' ', $donor->name, 2 );
2522
			$donor_first_name = Give()->donor_meta->get_meta( $donor->id, '_give_donor_first_name' );
2523
			$donor_last_name  = Give()->donor_meta->get_meta( $donor->id, '_give_donor_last_name' );
2524
2525
			// If first name meta of donor is not created, then create it.
2526 View Code Duplication
			if ( ! $donor_first_name && isset( $donor_name[0] ) ) {
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...
2527
				Give()->donor_meta->add_meta( $donor->id, '_give_donor_first_name', $donor_name[0] );
2528
			}
2529
2530
			// If last name meta of donor is not created, then create it.
2531 View Code Duplication
			if ( ! $donor_last_name && isset( $donor_name[1] ) ) {
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...
2532
				Give()->donor_meta->add_meta( $donor->id, '_give_donor_last_name', $donor_name[1] );
2533
			}
2534
2535
			// If Donor is connected with WP User then update user meta.
2536 View Code Duplication
			if ( $donor->user_id ) {
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...
2537
				if ( isset( $donor_name[0] ) ) {
2538
					update_user_meta( $donor->user_id, 'first_name', $donor_name[0] );
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
2539
				}
2540
				if ( isset( $donor_name[1] ) ) {
2541
					update_user_meta( $donor->user_id, 'last_name', $donor_name[1] );
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
2542
				}
2543
			}
2544
		}
2545
2546
		Give()->donors->table_name = $donor_table_name;
2547
		Give()->donor_meta->table_name = $donor_meta_table_name;
2548
	}
2549
2550
	Give_Updates::get_instance()->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...
2551
	give_set_upgrade_complete('v201_add_missing_donors' );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
2552
}