Test Failed
Push — issue/3627 ( fa91e5...bf57b1 )
by Ravinder
10:14
created

upgrade-functions.php ➔ give_v230_move_donation_note_callback()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 4
nop 0
dl 0
loc 63
rs 8.4961
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Upgrade Functions
4
 *
5
 * @package     Give
6
 * @subpackage  Admin/Upgrades
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.0
10
 *
11
 * NOTICE: When adding new upgrade notices, please be sure to put the action into the upgrades array during install:
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' ) ) {
84
				give_v201_create_tables();
85
				Give_Updates::get_instance()->__health_background_update( Give_Updates::get_instance() );
86
				Give_Updates::$background_updater->dispatch();
87
			}
88
89
			$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...
90
91
		case version_compare( $give_version, '2.0.2', '<' ) :
92
			// Remove 2.0.1 update to rerun on 2.0.2
93
			$completed_upgrades = give_get_completed_upgrades();
94
			$v201_updates       = array(
95
				'v201_upgrades_payment_metadata',
96
				'v201_add_missing_donors',
97
				'v201_move_metadata_into_new_table',
98
				'v201_logs_upgrades',
99
			);
100
101
			foreach ( $v201_updates as $v201_update ) {
102
				if ( in_array( $v201_update, $completed_upgrades ) ) {
103
					unset( $completed_upgrades[ array_search( $v201_update, $completed_upgrades ) ] );
104
				}
105
			}
106
107
			update_option( 'give_completed_upgrades', $completed_upgrades, false );
108
109
			// Do nothing on fresh install.
110
			if ( ! doing_action( 'give_upgrades' ) ) {
111
				give_v201_create_tables();
112
				Give_Updates::get_instance()->__health_background_update( Give_Updates::get_instance() );
113
				Give_Updates::$background_updater->dispatch();
114
			}
115
116
			$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...
117
118
		case version_compare( $give_version, '2.0.3', '<' ) :
119
			give_v203_upgrades();
120
			$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...
121
122
		case version_compare( $give_version, '2.2.0', '<' ) :
123
			give_v220_upgrades();
124
			$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...
125
126
		case version_compare( $give_version, '2.2.1', '<' ) :
127
			give_v221_upgrades();
128
			$did_upgrade = true;
129
	}
130
131
	if ( $did_upgrade ) {
132
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ), false );
133
	}
134
}
135
136
add_action( 'admin_init', 'give_do_automatic_upgrades' );
137
add_action( 'give_upgrades', 'give_do_automatic_upgrades' );
138
139
/**
140
 * Display Upgrade Notices.
141
 *
142
 * IMPORTANT: ALSO UPDATE INSTALL.PHP WITH THE ID OF THE UPGRADE ROUTINE SO IT DOES NOT AFFECT NEW INSTALLS.
143
 *
144
 * @since 1.0
145
 * @since 1.8.12 Update new update process code.
146
 *
147
 * @param Give_Updates $give_updates
148
 *
149
 * @return void
150
 */
151
function give_show_upgrade_notices( $give_updates ) {
152
	// v1.3.2 Upgrades
153
	$give_updates->register(
154
		array(
155
			'id'       => 'upgrade_give_payment_customer_id',
156
			'version'  => '1.3.2',
157
			'callback' => 'give_v132_upgrade_give_payment_customer_id',
158
		)
159
	);
160
161
	// v1.3.4 Upgrades ensure the user has gone through 1.3.4.
162
	$give_updates->register(
163
		array(
164
			'id'       => 'upgrade_give_offline_status',
165
			'depend'   => 'upgrade_give_payment_customer_id',
166
			'version'  => '1.3.4',
167
			'callback' => 'give_v134_upgrade_give_offline_status',
168
		)
169
	);
170
171
	// v1.8 form metadata upgrades.
172
	$give_updates->register(
173
		array(
174
			'id'       => 'v18_upgrades_form_metadata',
175
			'version'  => '1.8',
176
			'callback' => 'give_v18_upgrades_form_metadata',
177
		)
178
	);
179
180
	// v1.8.9 Upgrades
181
	$give_updates->register(
182
		array(
183
			'id'       => 'v189_upgrades_levels_post_meta',
184
			'version'  => '1.8.9',
185
			'callback' => 'give_v189_upgrades_levels_post_meta_callback',
186
		)
187
	);
188
189
	// v1.8.12 Upgrades
190
	$give_updates->register(
191
		array(
192
			'id'       => 'v1812_update_amount_values',
193
			'version'  => '1.8.12',
194
			'callback' => 'give_v1812_update_amount_values_callback',
195
		)
196
	);
197
198
	// v1.8.12 Upgrades
199
	$give_updates->register(
200
		array(
201
			'id'       => 'v1812_update_donor_purchase_values',
202
			'version'  => '1.8.12',
203
			'callback' => 'give_v1812_update_donor_purchase_value_callback',
204
		)
205
	);
206
207
	// v1.8.13 Upgrades for donor
208
	$give_updates->register(
209
		array(
210
			'id'       => 'v1813_update_donor_user_roles',
211
			'version'  => '1.8.13',
212
			'callback' => 'give_v1813_update_donor_user_roles_callback',
213
		)
214
	);
215
216
	// v1.8.17 Upgrades for donations.
217
	$give_updates->register( array(
218
		'id'       => 'v1817_update_donation_iranian_currency_code',
219
		'version'  => '1.8.17',
220
		'callback' => 'give_v1817_update_donation_iranian_currency_code',
221
	) );
222
223
	// v1.8.17 Upgrades for cleanup of user roles.
224
	$give_updates->register( array(
225
		'id'       => 'v1817_cleanup_user_roles',
226
		'version'  => '1.8.17',
227
		'callback' => 'give_v1817_cleanup_user_roles',
228
	) );
229
230
	// v1.8.18 Upgrades for assigning custom amount to existing set donations.
231
	$give_updates->register( array(
232
		'id'       => 'v1818_assign_custom_amount_set_donation',
233
		'version'  => '1.8.18',
234
		'callback' => 'give_v1818_assign_custom_amount_set_donation',
235
	) );
236
237
	// v1.8.18 Cleanup the Give Worker Role Caps.
238
	$give_updates->register( array(
239
		'id'       => 'v1818_give_worker_role_cleanup',
240
		'version'  => '1.8.18',
241
		'callback' => 'give_v1818_give_worker_role_cleanup',
242
	) );
243
244
	// v2.0.0 Upgrades
245
	$give_updates->register(
246
		array(
247
			'id'       => 'v20_upgrades_form_metadata',
248
			'version'  => '2.0.0',
249
			'callback' => 'give_v20_upgrades_form_metadata_callback',
250
		)
251
	);
252
253
	// v2.0.0 User Address Upgrades
254
	$give_updates->register(
255
		array(
256
			'id'       => 'v20_upgrades_user_address',
257
			'version'  => '2.0.0',
258
			'callback' => 'give_v20_upgrades_user_address',
259
		)
260
	);
261
262
	// v2.0.0 Upgrades
263
	$give_updates->register(
264
		array(
265
			'id'       => 'v20_upgrades_payment_metadata',
266
			'version'  => '2.0.0',
267
			'callback' => 'give_v20_upgrades_payment_metadata_callback',
268
		)
269
	);
270
271
	// v2.0.0 Upgrades
272
	$give_updates->register(
273
		array(
274
			'id'       => 'v20_logs_upgrades',
275
			'version'  => '2.0.0',
276
			'callback' => 'give_v20_logs_upgrades_callback',
277
278
		)
279
	);
280
281
	// v2.0.0 Donor Name Upgrades
282
	$give_updates->register(
283
		array(
284
			'id'       => 'v20_upgrades_donor_name',
285
			'version'  => '2.0.0',
286
			'callback' => 'give_v20_upgrades_donor_name',
287
		)
288
	);
289
290
	// v2.0.0 Upgrades
291
	$give_updates->register(
292
		array(
293
			'id'       => 'v20_move_metadata_into_new_table',
294
			'version'  => '2.0.0',
295
			'callback' => 'give_v20_move_metadata_into_new_table_callback',
296
			'depend'   => array( 'v20_upgrades_payment_metadata', 'v20_upgrades_form_metadata' ),
297
		)
298
	);
299
300
	// v2.0.0 Upgrades
301
	$give_updates->register(
302
		array(
303
			'id'       => 'v20_rename_donor_tables',
304
			'version'  => '2.0.0',
305
			'callback' => 'give_v20_rename_donor_tables_callback',
306
			'depend'   => array(
307
				'v20_move_metadata_into_new_table',
308
				'v20_logs_upgrades',
309
				'v20_upgrades_form_metadata',
310
				'v20_upgrades_payment_metadata',
311
				'v20_upgrades_user_address',
312
				'v20_upgrades_donor_name',
313
			),
314
		)
315
	);
316
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
317
318
	// v2.0.1 Upgrades
319
	$give_updates->register(
320
		array(
321
			'id'       => 'v201_upgrades_payment_metadata',
322
			'version'  => '2.0.1',
323
			'callback' => 'give_v201_upgrades_payment_metadata_callback',
324
		)
325
	);
326
327
	// v2.0.1 Upgrades
328
	$give_updates->register(
329
		array(
330
			'id'       => 'v201_add_missing_donors',
331
			'version'  => '2.0.1',
332
			'callback' => 'give_v201_add_missing_donors_callback',
333
		)
334
	);
335
336
	// Run v2.0.0 Upgrades again in 2.0.1
337
	$give_updates->register(
338
		array(
339
			'id'       => 'v201_move_metadata_into_new_table',
340
			'version'  => '2.0.1',
341
			'callback' => 'give_v201_move_metadata_into_new_table_callback',
342
			'depend'   => array( 'v201_upgrades_payment_metadata', 'v201_add_missing_donors' ),
343
		)
344
	);
345
346
	// Run v2.0.0 Upgrades again in 2.0.1
347
	$give_updates->register(
348
		array(
349
			'id'       => 'v201_logs_upgrades',
350
			'version'  => '2.0.1',
351
			'callback' => 'give_v201_logs_upgrades_callback',
352
		)
353
	);
354
355
	// v2.1 Verify Form Status Upgrade.
356
	$give_updates->register(
357
		array(
358
			'id'       => 'v210_verify_form_status_upgrades',
359
			'version'  => '2.1.0',
360
			'callback' => 'give_v210_verify_form_status_upgrades_callback',
361
		)
362
	);
363
364
	// v2.1.3 Delete non attached donation meta.
365
	$give_updates->register(
366
		array(
367
			'id'       => 'v213_delete_donation_meta',
368
			'version'  => '2.1.3',
369
			'callback' => 'give_v213_delete_donation_meta_callback',
370
			'depends'  => array( 'v201_move_metadata_into_new_table' )
371
		)
372
	);
373
374
	// v2.1.5 Add additional capability to the give_manager role.
375
	$give_updates->register(
376
		array(
377
			'id'       => 'v215_update_donor_user_roles',
378
			'version'  => '2.1.5',
379
			'callback' => 'give_v215_update_donor_user_roles_callback',
380
		)
381
	);
382
383
	// v2.2.4 set each donor to anonymous by default.
384
	$give_updates->register(
385
		array(
386
			'id'       => 'v224_update_donor_meta',
387
			'version'  => '2.2.4',
388
			'callback' => 'give_v224_update_donor_meta_callback',
389
		)
390
	);
391
392
	// v2.3.0 Move donor notes to custom comment table.
393
	$give_updates->register(
394
		array(
395
			'id'       => 'v230_move_donor_note',
396
			'version'  => '2.3.0',
397
			'callback' => 'give_v230_move_donor_note_callback',
398
		)
399
	);
400
401
	// v2.3.0 Move donation notes to custom comment table.
402
	$give_updates->register(
403
		array(
404
			'id'       => 'v230_move_donation_note_3',
405
			'version'  => '2.3.0',
406
			'callback' => 'give_v230_move_donation_note_callback',
407
		)
408
	);
409
}
410
411
add_action( 'give_register_updates', 'give_show_upgrade_notices' );
412
413
/**
414
 * Triggers all upgrade functions
415
 *
416
 * This function is usually triggered via AJAX
417
 *
418
 * @since 1.0
419
 * @return void
420
 */
421
function give_trigger_upgrades() {
422
423 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...
424
		wp_die( esc_html__( 'You do not have permission to do Give upgrades.', 'give' ), esc_html__( 'Error', 'give' ), array(
425
			'response' => 403,
426
		) );
427
	}
428
429
	$give_version = get_option( 'give_version' );
430
431
	if ( ! $give_version ) {
432
		// 1.0 is the first version to use this option so we must add it.
433
		$give_version = '1.0';
434
		add_option( 'give_version', $give_version, '', false );
435
	}
436
437
	update_option( 'give_version', GIVE_VERSION, false );
438
	delete_option( 'give_doing_upgrade' );
439
440
	if ( DOING_AJAX ) {
441
		die( 'complete' );
442
	} // End if().
443
}
444
445
add_action( 'wp_ajax_give_trigger_upgrades', 'give_trigger_upgrades' );
446
447
448
/**
449
 * Upgrades the
450
 *
451
 * Standardizes the discrepancies between two metakeys `_give_payment_customer_id` and `_give_payment_donor_id`
452
 *
453
 * @since      1.3.2
454
 */
455
function give_v132_upgrade_give_payment_customer_id() {
456
	global $wpdb;
457
458
	/* @var Give_Updates $give_updates */
459
	$give_updates = Give_Updates::get_instance();
460
461
	// UPDATE DB METAKEYS.
462
	$sql   = "UPDATE $wpdb->postmeta SET meta_key = '_give_payment_customer_id' WHERE meta_key = '_give_payment_donor_id'";
463
	$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...
464
465
	$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...
466
	give_set_upgrade_complete( 'upgrade_give_payment_customer_id' );
467
}
468
469
470
/**
471
 * Upgrades the Offline Status
472
 *
473
 * Reverses the issue where offline donations in "pending" status where inappropriately marked as abandoned
474
 *
475
 * @since      1.3.4
476
 */
477
function give_v134_upgrade_give_offline_status() {
478
	global $wpdb;
479
480
	/* @var Give_Updates $give_updates */
481
	$give_updates = Give_Updates::get_instance();
482
483
	// Get abandoned offline payments.
484
	$select = "SELECT ID FROM $wpdb->posts p ";
485
	$join   = "LEFT JOIN $wpdb->postmeta m ON p.ID = m.post_id ";
486
	$where  = "WHERE p.post_type = 'give_payment' ";
487
	$where  .= "AND ( p.post_status = 'abandoned' )";
488
	$where  .= "AND ( m.meta_key = '_give_payment_gateway' AND m.meta_value = 'offline' )";
489
490
	$sql            = $select . $join . $where;
491
	$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...
492
493
	foreach ( $found_payments as $payment ) {
494
495
		// Only change ones marked abandoned since our release last week because the admin may have marked some abandoned themselves.
496
		$modified_time = get_post_modified_time( 'U', false, $payment );
497
498
		// 1450124863 =  12/10/2015 20:42:25.
499
		if ( $modified_time >= 1450124863 ) {
500
501
			give_update_payment_status( $payment, 'pending' );
502
503
		}
504
	}
505
506
	$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...
507
	give_set_upgrade_complete( 'upgrade_give_offline_status' );
508
}
509
510
511
/**
512
 * Cleanup User Roles
513
 *
514
 * This upgrade routine removes unused roles and roles with typos
515
 *
516
 * @since      1.5.2
517
 */
518
function give_v152_cleanup_users() {
519
520
	$give_version = get_option( 'give_version' );
521
522
	if ( ! $give_version ) {
523
		// 1.0 is the first version to use this option so we must add it.
524
		$give_version = '1.0';
525
	}
526
527
	$give_version = preg_replace( '/[^0-9.].*/', '', $give_version );
528
529
	// v1.5.2 Upgrades
530
	if ( version_compare( $give_version, '1.5.2', '<' ) || ! give_has_upgrade_completed( 'upgrade_give_user_caps_cleanup' ) ) {
531
532
		// Delete all caps with "ss".
533
		// Also delete all unused "campaign" roles.
534
		$delete_caps = array(
535
			'delete_give_formss',
536
			'delete_others_give_formss',
537
			'delete_private_give_formss',
538
			'delete_published_give_formss',
539
			'read_private_forms',
540
			'edit_give_formss',
541
			'edit_others_give_formss',
542
			'edit_private_give_formss',
543
			'edit_published_give_formss',
544
			'publish_give_formss',
545
			'read_private_give_formss',
546
			'assign_give_campaigns_terms',
547
			'delete_give_campaigns',
548
			'delete_give_campaigns_terms',
549
			'delete_give_campaignss',
550
			'delete_others_give_campaignss',
551
			'delete_private_give_campaignss',
552
			'delete_published_give_campaignss',
553
			'edit_give_campaigns',
554
			'edit_give_campaigns_terms',
555
			'edit_give_campaignss',
556
			'edit_others_give_campaignss',
557
			'edit_private_give_campaignss',
558
			'edit_published_give_campaignss',
559
			'manage_give_campaigns_terms',
560
			'publish_give_campaignss',
561
			'read_give_campaigns',
562
			'read_private_give_campaignss',
563
			'view_give_campaigns_stats',
564
			'delete_give_paymentss',
565
			'delete_others_give_paymentss',
566
			'delete_private_give_paymentss',
567
			'delete_published_give_paymentss',
568
			'edit_give_paymentss',
569
			'edit_others_give_paymentss',
570
			'edit_private_give_paymentss',
571
			'edit_published_give_paymentss',
572
			'publish_give_paymentss',
573
			'read_private_give_paymentss',
574
		);
575
576
		global $wp_roles;
577
		foreach ( $delete_caps as $cap ) {
578
			foreach ( array_keys( $wp_roles->roles ) as $role ) {
579
				$wp_roles->remove_cap( $role, $cap );
580
			}
581
		}
582
583
		// Create Give plugin roles.
584
		$roles = new Give_Roles();
585
		$roles->add_roles();
586
		$roles->add_caps();
587
588
		// The Update Ran.
589
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ), false );
590
		give_set_upgrade_complete( 'upgrade_give_user_caps_cleanup' );
591
		delete_option( 'give_doing_upgrade' );
592
593
	}// End if().
594
595
}
596
597
add_action( 'admin_init', 'give_v152_cleanup_users' );
598
599
/**
600
 * 1.6 Upgrade routine to create the customer meta table.
601
 *
602
 * @since  1.6
603
 * @return void
604
 */
605
function give_v16_upgrades() {
606
	// Create the donor databases.
607
	$donors_db = new Give_DB_Donors();
608
	$donors_db->create_table();
609
	$donor_meta = new Give_DB_Donor_Meta();
610
	$donor_meta->create_table();
611
}
612
613
/**
614
 * 1.7 Upgrades.
615
 *
616
 * a. Update license api data for plugin addons.
617
 * b. Cleanup user roles.
618
 *
619
 * @since  1.7
620
 * @return void
621
 */
622
function give_v17_upgrades() {
623
	// Upgrade license data.
624
	give_v17_upgrade_addon_license_data();
625
	give_v17_cleanup_roles();
626
}
627
628
/**
629
 * Upgrade license data
630
 *
631
 * @since 1.7
632
 */
633
function give_v17_upgrade_addon_license_data() {
634
	$give_options = give_get_settings();
635
636
	$api_url = 'https://givewp.com/give-sl-api/';
637
638
	// Get addons license key.
639
	$addons = array();
640
	foreach ( $give_options as $key => $value ) {
641
		if ( false !== strpos( $key, '_license_key' ) ) {
642
			$addons[ $key ] = $value;
643
		}
644
	}
645
646
	// Bailout: We do not have any addon license data to upgrade.
647
	if ( empty( $addons ) ) {
648
		return false;
649
	}
650
651
	foreach ( $addons as $key => $addon_license ) {
652
653
		// Get addon shortname.
654
		$shortname = str_replace( '_license_key', '', $key );
655
656
		// Addon license option name.
657
		$addon_license_option_name = $shortname . '_license_active';
658
659
		// bailout if license is empty.
660
		if ( empty( $addon_license ) ) {
661
			delete_option( $addon_license_option_name );
662
			continue;
663
		}
664
665
		// Get addon name.
666
		$addon_name       = array();
667
		$addon_name_parts = explode( '_', str_replace( 'give_', '', $shortname ) );
668
		foreach ( $addon_name_parts as $name_part ) {
669
670
			// Fix addon name
671
			switch ( $name_part ) {
672
				case 'authorizenet' :
673
					$name_part = 'authorize.net';
674
					break;
675
			}
676
677
			$addon_name[] = ucfirst( $name_part );
678
		}
679
680
		$addon_name = implode( ' ', $addon_name );
681
682
		// Data to send to the API.
683
		$api_params = array(
684
			'edd_action' => 'activate_license', // never change from "edd_" to "give_"!
685
			'license'    => $addon_license,
686
			'item_name'  => urlencode( $addon_name ),
687
			'url'        => home_url(),
688
		);
689
690
		// Call the API.
691
		$response = wp_remote_post(
692
			$api_url,
693
			array(
694
				'timeout'   => 15,
695
				'sslverify' => false,
696
				'body'      => $api_params,
697
			)
698
		);
699
700
		// Make sure there are no errors.
701
		if ( is_wp_error( $response ) ) {
702
			delete_option( $addon_license_option_name );
703
			continue;
704
		}
705
706
		// Tell WordPress to look for updates.
707
		set_site_transient( 'update_plugins', null );
708
709
		// Decode license data.
710
		$license_data = json_decode( wp_remote_retrieve_body( $response ) );
711
		update_option( $addon_license_option_name, $license_data, false );
712
	}// End foreach().
713
}
714
715
716
/**
717
 * Cleanup User Roles.
718
 *
719
 * This upgrade routine removes unused roles and roles with typos.
720
 *
721
 * @since      1.7
722
 */
723
function give_v17_cleanup_roles() {
724
725
	// Delete all caps with "_give_forms_" and "_give_payments_".
726
	// These roles have no usage; the proper is singular.
727
	$delete_caps = array(
728
		'view_give_forms_stats',
729
		'delete_give_forms_terms',
730
		'assign_give_forms_terms',
731
		'edit_give_forms_terms',
732
		'manage_give_forms_terms',
733
		'view_give_payments_stats',
734
		'manage_give_payments_terms',
735
		'edit_give_payments_terms',
736
		'assign_give_payments_terms',
737
		'delete_give_payments_terms',
738
	);
739
740
	global $wp_roles;
741
	foreach ( $delete_caps as $cap ) {
742
		foreach ( array_keys( $wp_roles->roles ) as $role ) {
743
			$wp_roles->remove_cap( $role, $cap );
744
		}
745
	}
746
747
	// Set roles again.
748
	$roles = new Give_Roles();
749
	$roles->add_roles();
750
	$roles->add_caps();
751
752
}
753
754
/**
755
 * 1.8 Upgrades.
756
 *
757
 * a. Upgrade checkbox settings to radio button settings.
758
 * a. Update form meta for new metabox settings.
759
 *
760
 * @since  1.8
761
 * @return void
762
 */
763
function give_v18_upgrades() {
764
	// Upgrade checkbox settings to radio button settings.
765
	give_v18_upgrades_core_setting();
766
}
767
768
/**
769
 * Upgrade core settings.
770
 *
771
 * @since  1.8
772
 * @return void
773
 */
774
function give_v18_upgrades_core_setting() {
775
	// Core settings which changes from checkbox to radio.
776
	$core_setting_names = array_merge(
777
		array_keys( give_v18_renamed_core_settings() ),
778
		array(
779
			'uninstall_on_delete',
780
			'scripts_footer',
781
			'test_mode',
782
			'email_access',
783
			'terms',
784
			'give_offline_donation_enable_billing_fields',
785
		)
786
	);
787
788
	// Bailout: If not any setting define.
789
	if ( $give_settings = get_option( 'give_settings' ) ) {
790
791
		$setting_changed = false;
792
793
		// Loop: check each setting field.
794
		foreach ( $core_setting_names as $setting_name ) {
795
			// New setting name.
796
			$new_setting_name = preg_replace( '/^(enable_|disable_)/', '', $setting_name );
797
798
			// Continue: If setting already set.
799
			if (
800
				array_key_exists( $new_setting_name, $give_settings )
801
				&& in_array( $give_settings[ $new_setting_name ], array( 'enabled', 'disabled' ) )
802
			) {
803
				continue;
804
			}
805
806
			// Set checkbox value to radio value.
807
			$give_settings[ $setting_name ] = ( ! empty( $give_settings[ $setting_name ] ) && 'on' === $give_settings[ $setting_name ] ? 'enabled' : 'disabled' );
808
809
			// @see https://github.com/WordImpress/Give/issues/1063.
810
			if ( false !== strpos( $setting_name, 'disable_' ) ) {
811
812
				$give_settings[ $new_setting_name ] = ( give_is_setting_enabled( $give_settings[ $setting_name ] ) ? 'disabled' : 'enabled' );
813
			} elseif ( false !== strpos( $setting_name, 'enable_' ) ) {
814
815
				$give_settings[ $new_setting_name ] = ( give_is_setting_enabled( $give_settings[ $setting_name ] ) ? 'enabled' : 'disabled' );
816
			}
817
818
			// Tell bot to update core setting to db.
819
			if ( ! $setting_changed ) {
820
				$setting_changed = true;
821
			}
822
		}
823
824
		// Update setting only if they changed.
825
		if ( $setting_changed ) {
826
			update_option( 'give_settings', $give_settings, false );
827
		}
828
	}// End if().
829
830
	give_set_upgrade_complete( 'v18_upgrades_core_setting' );
831
}
832
833
/**
834
 * Upgrade form metadata for new metabox settings.
835
 *
836
 * @since  1.8
837
 * @return void
838
 */
839
function give_v18_upgrades_form_metadata() {
840
	/* @var Give_Updates $give_updates */
841
	$give_updates = Give_Updates::get_instance();
842
843
	// form query
844
	$forms = new WP_Query( array(
845
			'paged'          => $give_updates->step,
846
			'status'         => 'any',
847
			'order'          => 'ASC',
848
			'post_type'      => 'give_forms',
849
			'posts_per_page' => 20,
850
		)
851
	);
852
853
	if ( $forms->have_posts() ) {
854
		$give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 20 ) );
855
856
		while ( $forms->have_posts() ) {
857
			$forms->the_post();
858
859
			// Form content.
860
			// Note in version 1.8 display content setting split into display content and content placement setting.
861
			// You can delete _give_content_option in future.
862
			$show_content = give_get_meta( get_the_ID(), '_give_content_option', true );
863
			if ( $show_content && ! give_get_meta( get_the_ID(), '_give_display_content', true ) ) {
864
				$field_value = ( 'none' !== $show_content ? 'enabled' : 'disabled' );
865
				give_update_meta( get_the_ID(), '_give_display_content', $field_value );
866
867
				$field_value = ( 'none' !== $show_content ? $show_content : 'give_pre_form' );
868
				give_update_meta( get_the_ID(), '_give_content_placement', $field_value );
869
			}
870
871
			// "Disable" Guest Donation. Checkbox.
872
			// See: https://github.com/WordImpress/Give/issues/1470.
873
			$guest_donation        = give_get_meta( get_the_ID(), '_give_logged_in_only', true );
874
			$guest_donation_newval = ( in_array( $guest_donation, array( 'yes', 'on' ) ) ? 'disabled' : 'enabled' );
875
			give_update_meta( get_the_ID(), '_give_logged_in_only', $guest_donation_newval );
876
877
			// Offline Donations.
878
			// See: https://github.com/WordImpress/Give/issues/1579.
879
			$offline_donation = give_get_meta( get_the_ID(), '_give_customize_offline_donations', true );
880
			if ( 'no' === $offline_donation ) {
881
				$offline_donation_newval = 'global';
882
			} elseif ( 'yes' === $offline_donation ) {
883
				$offline_donation_newval = 'enabled';
884
			} else {
885
				$offline_donation_newval = 'disabled';
886
			}
887
			give_update_meta( get_the_ID(), '_give_customize_offline_donations', $offline_donation_newval );
888
889
			// Convert yes/no setting field to enabled/disabled.
890
			$form_radio_settings = array(
891
				// Custom Amount.
892
				'_give_custom_amount',
893
894
				// Donation Gaol.
895
				'_give_goal_option',
896
897
				// Close Form.
898
				'_give_close_form_when_goal_achieved',
899
900
				// Term & conditions.
901
				'_give_terms_option',
902
903
				// Billing fields.
904
				'_give_offline_donation_enable_billing_fields_single',
905
			);
906
907
			foreach ( $form_radio_settings as $meta_key ) {
908
				// Get value.
909
				$field_value = give_get_meta( get_the_ID(), $meta_key, true );
910
911
				// Convert meta value only if it is in yes/no/none.
912
				if ( in_array( $field_value, array( 'yes', 'on', 'no', 'none' ) ) ) {
913
914
					$field_value = ( in_array( $field_value, array( 'yes', 'on' ) ) ? 'enabled' : 'disabled' );
915
					give_update_meta( get_the_ID(), $meta_key, $field_value );
916
				}
917
			}
918
		}// End while().
919
920
		wp_reset_postdata();
921
922
	} else {
923
		// No more forms found, finish up.
924
		give_set_upgrade_complete( 'v18_upgrades_form_metadata' );
925
	}
926
}
927
928
929
/**
930
 * Get list of core setting renamed in version 1.8.
931
 *
932
 * @since  1.8
933
 * @return array
934
 */
935
function give_v18_renamed_core_settings() {
936
	return array(
937
		'disable_paypal_verification' => 'paypal_verification',
938
		'disable_css'                 => 'css',
939
		'disable_welcome'             => 'welcome',
940
		'disable_forms_singular'      => 'forms_singular',
941
		'disable_forms_archives'      => 'forms_archives',
942
		'disable_forms_excerpt'       => 'forms_excerpt',
943
		'disable_form_featured_img'   => 'form_featured_img',
944
		'disable_form_sidebar'        => 'form_sidebar',
945
		'disable_admin_notices'       => 'admin_notices',
946
		'disable_the_content_filter'  => 'the_content_filter',
947
		'enable_floatlabels'          => 'floatlabels',
948
		'enable_categories'           => 'categories',
949
		'enable_tags'                 => 'tags',
950
	);
951
}
952
953
954
/**
955
 * Upgrade core settings.
956
 *
957
 * @since  1.8.7
958
 * @return void
959
 */
960
function give_v187_upgrades() {
961
	global $wpdb;
962
963
	/**
964
	 * Upgrade 1: Remove stat and cache transients.
965
	 */
966
	$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...
967
		$wpdb->prepare(
968
			"
969
					SELECT *
970
					FROM {$wpdb->options}
971
					WHERE (
972
					option_name LIKE %s
973
					OR option_name LIKE %s
974
					OR option_name LIKE %s
975
					OR option_name LIKE %s
976
					OR option_name LIKE %s
977
					OR option_name LIKE %s
978
					OR option_name LIKE %s
979
					OR option_name LIKE %s
980
					OR option_name LIKE %s
981
					OR option_name LIKE %s
982
					OR option_name LIKE %s
983
					OR option_name LIKE %s
984
					OR option_name LIKE %s
985
					)
986
					",
987
			array(
988
				'%_transient_give_stats_%',
989
				'give_cache%',
990
				'%_transient_give_add_ons_feed%',
991
				'%_transient__give_ajax_works' .
992
				'%_transient_give_total_api_keys%',
993
				'%_transient_give_i18n_give_promo_hide%',
994
				'%_transient_give_contributors%',
995
				'%_transient_give_estimated_monthly_stats%',
996
				'%_transient_give_earnings_total%',
997
				'%_transient_give_i18n_give_%',
998
				'%_transient__give_installed%',
999
				'%_transient__give_activation_redirect%',
1000
				'%_transient__give_hide_license_notices_shortly_%',
1001
				'%give_income_total%',
1002
			)
1003
		),
1004
		1
1005
	);
1006
1007
	// User related transients.
1008
	$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...
1009
		$wpdb->prepare(
1010
			"SELECT user_id, meta_key
1011
			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...
1012
			WHERE meta_value=%s",
1013
			'give_user_public_key'
1014
		),
1015
		ARRAY_A
1016
	);
1017
1018
	if ( ! empty( $user_apikey_options ) ) {
1019
		foreach ( $user_apikey_options as $user ) {
1020
			$cached_options[] = '_transient_' . md5( 'give_api_user_' . $user['meta_key'] );
1021
			$cached_options[] = '_transient_' . md5( 'give_api_user_public_key' . $user['user_id'] );
1022
			$cached_options[] = '_transient_' . md5( 'give_api_user_secret_key' . $user['user_id'] );
1023
		}
1024
	}
1025
1026
	if ( ! empty( $cached_options ) ) {
1027
		foreach ( $cached_options as $option ) {
1028 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...
1029
				case ( false !== strpos( $option, 'transient' ) ):
1030
					$option = str_replace( '_transient_', '', $option );
1031
					delete_transient( $option );
1032
					break;
1033
1034
				default:
1035
					delete_option( $option );
1036
			}
1037
		}
1038
	}
1039
}
1040
1041
/**
1042
 * Update Capabilities for Give_Worker User Role.
1043
 *
1044
 * This upgrade routine will update access rights for Give_Worker User Role.
1045
 *
1046
 * @since      1.8.8
1047
 */
1048
function give_v188_upgrades() {
1049
1050
	global $wp_roles;
1051
1052
	// Get the role object.
1053
	$give_worker = get_role( 'give_worker' );
1054
1055
	// A list of capabilities to add for give workers.
1056
	$caps_to_add = array(
1057
		'edit_posts',
1058
		'edit_pages',
1059
	);
1060
1061
	foreach ( $caps_to_add as $cap ) {
1062
		// Add the capability.
1063
		$give_worker->add_cap( $cap );
1064
	}
1065
1066
}
1067
1068
/**
1069
 * Update Post meta for minimum and maximum amount for multi level donation forms
1070
 *
1071
 * This upgrade routine adds post meta for give_forms CPT for multi level donation form.
1072
 *
1073
 * @since      1.8.9
1074
 */
1075
function give_v189_upgrades_levels_post_meta_callback() {
1076
	/* @var Give_Updates $give_updates */
1077
	$give_updates = Give_Updates::get_instance();
1078
1079
	// form query.
1080
	$donation_forms = new WP_Query( array(
1081
			'paged'          => $give_updates->step,
1082
			'status'         => 'any',
1083
			'order'          => 'ASC',
1084
			'post_type'      => 'give_forms',
1085
			'posts_per_page' => 20,
1086
		)
1087
	);
1088
1089
	if ( $donation_forms->have_posts() ) {
1090
		$give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
1091
1092
		while ( $donation_forms->have_posts() ) {
1093
			$donation_forms->the_post();
1094
			$form_id = get_the_ID();
1095
1096
			// Remove formatting from _give_set_price.
1097
			update_post_meta(
1098
				$form_id,
1099
				'_give_set_price',
1100
				give_sanitize_amount( get_post_meta( $form_id, '_give_set_price', true ) )
1101
			);
1102
1103
			// Remove formatting from _give_custom_amount_minimum.
1104
			update_post_meta(
1105
				$form_id,
1106
				'_give_custom_amount_minimum',
1107
				give_sanitize_amount( get_post_meta( $form_id, '_give_custom_amount_minimum', true ) )
1108
			);
1109
1110
			// Bailout.
1111
			if ( 'set' === get_post_meta( $form_id, '_give_price_option', true ) ) {
1112
				continue;
1113
			}
1114
1115
			$donation_levels = get_post_meta( $form_id, '_give_donation_levels', true );
1116
1117
			if ( ! empty( $donation_levels ) ) {
1118
1119
				foreach ( $donation_levels as $index => $donation_level ) {
1120
					if ( isset( $donation_level['_give_amount'] ) ) {
1121
						$donation_levels[ $index ]['_give_amount'] = give_sanitize_amount( $donation_level['_give_amount'] );
1122
					}
1123
				}
1124
1125
				update_post_meta( $form_id, '_give_donation_levels', $donation_levels );
1126
1127
				$donation_levels_amounts = wp_list_pluck( $donation_levels, '_give_amount' );
1128
1129
				$min_amount = min( $donation_levels_amounts );
1130
				$max_amount = max( $donation_levels_amounts );
1131
1132
				// Set Minimum and Maximum amount for Multi Level Donation Forms
1133
				give_update_meta( $form_id, '_give_levels_minimum_amount', $min_amount ? give_sanitize_amount( $min_amount ) : 0 );
1134
				give_update_meta( $form_id, '_give_levels_maximum_amount', $max_amount ? give_sanitize_amount( $max_amount ) : 0 );
1135
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1136
1137
		}
1138
1139
		/* Restore original Post Data */
1140
		wp_reset_postdata();
1141
	} else {
1142
		// The Update Ran.
1143
		give_set_upgrade_complete( 'v189_upgrades_levels_post_meta' );
1144
	}
1145
1146
}
1147
1148
1149
/**
1150
 * Give version 1.8.9 upgrades
1151
 *
1152
 * @since      1.8.9
1153
 */
1154
function give_v189_upgrades() {
1155
	/**
1156
	 * 1. Remove user license related notice show blocked ( Give_Notice will handle )
1157
	 */
1158
	global $wpdb;
1159
1160
	// Delete permanent notice blocker.
1161
	$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...
1162
		$wpdb->prepare(
1163
			"
1164
					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...
1165
					WHERE meta_key
1166
					LIKE '%%%s%%'
1167
					",
1168
			'_give_hide_license_notices_permanently'
1169
		)
1170
	);
1171
1172
	// Delete short notice blocker.
1173
	$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...
1174
		$wpdb->prepare(
1175
			"
1176
					DELETE FROM $wpdb->options
1177
					WHERE option_name
1178
					LIKE '%%%s%%'
1179
					",
1180
			'__give_hide_license_notices_shortly_'
1181
		)
1182
	);
1183
}
1184
1185
/**
1186
 * 2.0 Upgrades.
1187
 *
1188
 * @since  2.0
1189
 * @return void
1190
 */
1191
function give_v20_upgrades() {
1192
	// Update cache setting.
1193
	give_update_option( 'cache', 'enabled' );
1194
1195
	// Upgrade email settings.
1196
	give_v20_upgrades_email_setting();
1197
}
1198
1199
/**
1200
 * Move old email api settings to new email setting api for following emails:
1201
 *    1. new offline donation         [This was hard coded]
1202
 *    2. offline donation instruction
1203
 *    3. new donation
1204
 *    4. donation receipt
1205
 *
1206
 * @since 2.0
1207
 */
1208
function give_v20_upgrades_email_setting() {
1209
	$all_setting = give_get_settings();
1210
1211
	// Bailout on fresh install.
1212
	if ( empty( $all_setting ) ) {
1213
		return;
1214
	}
1215
1216
	$settings = array(
1217
		'offline_donation_subject'      => 'offline-donation-instruction_email_subject',
1218
		'global_offline_donation_email' => 'offline-donation-instruction_email_message',
1219
		'donation_subject'              => 'donation-receipt_email_subject',
1220
		'donation_receipt'              => 'donation-receipt_email_message',
1221
		'donation_notification_subject' => 'new-donation_email_subject',
1222
		'donation_notification'         => 'new-donation_email_message',
1223
		'admin_notice_emails'           => array(
1224
			'new-donation_recipient',
1225
			'new-offline-donation_recipient',
1226
			'new-donor-register_recipient',
1227
		),
1228
		'admin_notices'                 => 'new-donation_notification',
1229
	);
1230
1231
	foreach ( $settings as $old_setting => $new_setting ) {
1232
		// Do not update already modified
1233
		if ( ! is_array( $new_setting ) ) {
1234
			if ( array_key_exists( $new_setting, $all_setting ) || ! array_key_exists( $old_setting, $all_setting ) ) {
1235
				continue;
1236
			}
1237
		}
1238
1239
		switch ( $old_setting ) {
1240
			case 'admin_notices':
1241
				$notification_status = give_get_option( $old_setting, 'enabled' );
1242
1243
				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 1231 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...
1244
1245
				// @todo: Delete this option later ( version > 2.0 ), We need this for per form email addon.
1246
				// give_delete_option( $old_setting );
1247
1248
				break;
1249
1250
			// @todo: Delete this option later ( version > 2.0 ) because we need this for backward compatibility give_get_admin_notice_emails.
1251
			case 'admin_notice_emails':
1252
				$recipients = give_get_admin_notice_emails();
1253
1254
				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...
1255
					// bailout if setting already exist.
1256
					if ( array_key_exists( $setting, $all_setting ) ) {
1257
						continue;
1258
					}
1259
1260
					give_update_option( $setting, $recipients );
1261
				}
1262
				break;
1263
1264
			default:
1265
				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 1231 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...
1266
				give_delete_option( $old_setting );
1267
		}
1268
	}
1269
}
1270
1271
/**
1272
 * Give version 1.8.9 upgrades
1273
 *
1274
 * @since 1.8.9
1275
 */
1276
function give_v1812_upgrades() {
1277
	/**
1278
	 * Validate number format settings.
1279
	 */
1280
	$give_settings        = give_get_settings();
1281
	$give_setting_updated = false;
1282
1283
	if ( $give_settings['thousands_separator'] === $give_settings['decimal_separator'] ) {
1284
		$give_settings['number_decimals']   = 0;
1285
		$give_settings['decimal_separator'] = '';
1286
		$give_setting_updated               = true;
1287
1288
	} elseif ( empty( $give_settings['decimal_separator'] ) ) {
1289
		$give_settings['number_decimals'] = 0;
1290
		$give_setting_updated             = true;
1291
1292
	} elseif ( 6 < absint( $give_settings['number_decimals'] ) ) {
1293
		$give_settings['number_decimals'] = 5;
1294
		$give_setting_updated             = true;
1295
	}
1296
1297
	if ( $give_setting_updated ) {
1298
		update_option( 'give_settings', $give_settings, false );
1299
	}
1300
}
1301
1302
1303
/**
1304
 * Give version 1.8.12 update
1305
 *
1306
 * Standardized amount values to six decimal
1307
 *
1308
 * @see        https://github.com/WordImpress/Give/issues/1849#issuecomment-315128602
1309
 *
1310
 * @since      1.8.12
1311
 */
1312
function give_v1812_update_amount_values_callback() {
1313
	/* @var Give_Updates $give_updates */
1314
	$give_updates = Give_Updates::get_instance();
1315
1316
	// form query.
1317
	$donation_forms = new WP_Query( array(
1318
			'paged'          => $give_updates->step,
1319
			'status'         => 'any',
1320
			'order'          => 'ASC',
1321
			'post_type'      => array( 'give_forms', 'give_payment' ),
1322
			'posts_per_page' => 20,
1323
		)
1324
	);
1325
	if ( $donation_forms->have_posts() ) {
1326
		$give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
1327
1328
		while ( $donation_forms->have_posts() ) {
1329
			$donation_forms->the_post();
1330
			global $post;
1331
1332
			$meta = get_post_meta( $post->ID );
1333
1334
			switch ( $post->post_type ) {
1335
				case 'give_forms':
1336
					// _give_set_price.
1337
					if ( ! empty( $meta['_give_set_price'][0] ) ) {
1338
						update_post_meta( $post->ID, '_give_set_price', give_sanitize_amount_for_db( $meta['_give_set_price'][0] ) );
1339
					}
1340
1341
					// _give_custom_amount_minimum.
1342
					if ( ! empty( $meta['_give_custom_amount_minimum'][0] ) ) {
1343
						update_post_meta( $post->ID, '_give_custom_amount_minimum', give_sanitize_amount_for_db( $meta['_give_custom_amount_minimum'][0] ) );
1344
					}
1345
1346
					// _give_levels_minimum_amount.
1347
					if ( ! empty( $meta['_give_levels_minimum_amount'][0] ) ) {
1348
						update_post_meta( $post->ID, '_give_levels_minimum_amount', give_sanitize_amount_for_db( $meta['_give_levels_minimum_amount'][0] ) );
1349
					}
1350
1351
					// _give_levels_maximum_amount.
1352
					if ( ! empty( $meta['_give_levels_maximum_amount'][0] ) ) {
1353
						update_post_meta( $post->ID, '_give_levels_maximum_amount', give_sanitize_amount_for_db( $meta['_give_levels_maximum_amount'][0] ) );
1354
					}
1355
1356
					// _give_set_goal.
1357
					if ( ! empty( $meta['_give_set_goal'][0] ) ) {
1358
						update_post_meta( $post->ID, '_give_set_goal', give_sanitize_amount_for_db( $meta['_give_set_goal'][0] ) );
1359
					}
1360
1361
					// _give_form_earnings.
1362
					if ( ! empty( $meta['_give_form_earnings'][0] ) ) {
1363
						update_post_meta( $post->ID, '_give_form_earnings', give_sanitize_amount_for_db( $meta['_give_form_earnings'][0] ) );
1364
					}
1365
1366
					// _give_custom_amount_minimum.
1367
					if ( ! empty( $meta['_give_donation_levels'][0] ) ) {
1368
						$donation_levels = unserialize( $meta['_give_donation_levels'][0] );
1369
1370
						foreach ( $donation_levels as $index => $level ) {
1371
							if ( empty( $level['_give_amount'] ) ) {
1372
								continue;
1373
							}
1374
1375
							$donation_levels[ $index ]['_give_amount'] = give_sanitize_amount_for_db( $level['_give_amount'] );
1376
						}
1377
1378
						$meta['_give_donation_levels'] = $donation_levels;
1379
						update_post_meta( $post->ID, '_give_donation_levels', $meta['_give_donation_levels'] );
1380
					}
1381
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
1382
1383
					break;
1384
1385
				case 'give_payment':
1386
					// _give_payment_total.
1387
					if ( ! empty( $meta['_give_payment_total'][0] ) ) {
1388
						update_post_meta( $post->ID, '_give_payment_total', give_sanitize_amount_for_db( $meta['_give_payment_total'][0] ) );
1389
					}
1390
1391
					break;
1392
			}
1393
		}
1394
1395
		/* Restore original Post Data */
1396
		wp_reset_postdata();
1397
	} else {
1398
		// The Update Ran.
1399
		give_set_upgrade_complete( 'v1812_update_amount_values' );
1400
	}
1401
}
1402
1403
1404
/**
1405
 * Give version 1.8.12 update
1406
 *
1407
 * Standardized amount values to six decimal for donor
1408
 *
1409
 * @see        https://github.com/WordImpress/Give/issues/1849#issuecomment-315128602
1410
 *
1411
 * @since      1.8.12
1412
 */
1413
function give_v1812_update_donor_purchase_value_callback() {
1414
	/* @var Give_Updates $give_updates */
1415
	$give_updates = Give_Updates::get_instance();
1416
1417
	// form query.
1418
	$donors = Give()->donors->get_donors( array(
1419
			'number' => 20,
1420
			'offset' => $give_updates->get_offset( 20 ),
1421
		)
1422
	);
1423
1424
	if ( ! empty( $donors ) ) {
1425
		$give_updates->set_percentage( Give()->donors->count(), $give_updates->get_offset( 20 ) );
1426
1427
		/* @var Object $donor */
1428
		foreach ( $donors as $donor ) {
1429
			Give()->donors->update( $donor->id, array( 'purchase_value' => give_sanitize_amount_for_db( $donor->purchase_value ) ) );
1430
		}
1431
	} else {
1432
		// The Update Ran.
1433
		give_set_upgrade_complete( 'v1812_update_donor_purchase_values' );
1434
	}
1435
}
1436
1437
/**
1438
 * Upgrade routine for updating user roles for existing donors.
1439
 *
1440
 * @since 1.8.13
1441
 */
1442
function give_v1813_update_donor_user_roles_callback() {
1443
	/* @var Give_Updates $give_updates */
1444
	$give_updates = Give_Updates::get_instance();
1445
1446
	// Fetch all the existing donors.
1447
	$donors = Give()->donors->get_donors( array(
1448
			'number' => 20,
1449
			'offset' => $give_updates->get_offset( 20 ),
1450
		)
1451
	);
1452
1453
	if ( ! empty( $donors ) ) {
1454
		$give_updates->set_percentage( Give()->donors->count(), $give_updates->get_offset( 20 ) );
1455
1456
		/* @var Object $donor */
1457
		foreach ( $donors as $donor ) {
1458
			$user_id = $donor->user_id;
1459
1460
			// Proceed, if donor is attached with user.
1461
			if ( $user_id ) {
1462
				$user = get_userdata( $user_id );
1463
1464
				// Update user role, if user has subscriber role.
1465
				if ( is_array( $user->roles ) && in_array( 'subscriber', $user->roles ) ) {
1466
					wp_update_user(
1467
						array(
1468
							'ID'   => $user_id,
1469
							'role' => 'give_donor',
1470
						)
1471
					);
1472
				}
1473
			}
1474
		}
1475
	} else {
1476
		// The Update Ran.
1477
		give_set_upgrade_complete( 'v1813_update_donor_user_roles' );
1478
	}
1479
}
1480
1481
1482
/**
1483
 * Version 1.8.13 automatic updates
1484
 *
1485
 * @since 1.8.13
1486
 */
1487
function give_v1813_upgrades() {
1488
	// Update admin setting.
1489
	give_update_option( 'donor_default_user_role', 'give_donor' );
1490
1491
	// Update Give roles.
1492
	$roles = new Give_Roles();
1493
	$roles->add_roles();
1494
	$roles->add_caps();
1495
}
1496
1497
/**
1498
 * Correct currency code for "Iranian Currency" for all of the payments.
1499
 *
1500
 * @since 1.8.17
1501
 */
1502
function give_v1817_update_donation_iranian_currency_code() {
1503
	/* @var Give_Updates $give_updates */
1504
	$give_updates = Give_Updates::get_instance();
1505
1506
	// form query.
1507
	$payments = new WP_Query( array(
1508
			'paged'          => $give_updates->step,
1509
			'status'         => 'any',
1510
			'order'          => 'ASC',
1511
			'post_type'      => array( 'give_payment' ),
1512
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1513
		)
1514
	);
1515
1516
	if ( $payments->have_posts() ) {
1517
		$give_updates->set_percentage( $payments->found_posts, ( $give_updates->step * 100 ) );
1518
1519
		while ( $payments->have_posts() ) {
1520
			$payments->the_post();
1521
1522
			$payment_meta = give_get_payment_meta( get_the_ID() );
1523
1524
			if ( 'RIAL' === $payment_meta['currency'] ) {
1525
				$payment_meta['currency'] = 'IRR';
1526
				give_update_meta( get_the_ID(), '_give_payment_meta', $payment_meta );
1527
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1528
1529
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1530
1531
	} else {
1532
		// The Update Ran.
1533
		give_set_upgrade_complete( 'v1817_update_donation_iranian_currency_code' );
1534
	}
1535
}
1536
1537
/**
1538
 * Correct currency code for "Iranian Currency" in Give setting.
1539
 * Version 1.8.17 automatic updates
1540
 *
1541
 * @since 1.8.17
1542
 */
1543
function give_v1817_upgrades() {
1544
	$give_settings = give_get_settings();
1545
1546
	if ( 'RIAL' === $give_settings['currency'] ) {
1547
		$give_settings['currency'] = 'IRR';
1548
		update_option( 'give_settings', $give_settings, false );
1549
	}
1550
}
1551
1552
/**
1553
 * Process Clean up of User Roles for more flexibility.
1554
 *
1555
 * @since 1.8.17
1556
 */
1557
function give_v1817_process_cleanup_user_roles() {
1558
1559
	global $wp_roles;
1560
1561
	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...
1562
		return;
1563
	}
1564
1565
	// Add Capabilities to user roles as required.
1566
	$add_caps = array(
1567
		'administrator' => array(
1568
			'view_give_payments',
1569
		),
1570
	);
1571
1572
	// Remove Capabilities to user roles as required.
1573
	$remove_caps = array(
1574
		'give_manager' => array(
1575
			'edit_others_pages',
1576
			'edit_others_posts',
1577
			'delete_others_pages',
1578
			'delete_others_posts',
1579
			'manage_categories',
1580
			'import',
1581
			'export',
1582
		),
1583
	);
1584
1585
	foreach ( $add_caps as $role => $caps ) {
1586
		foreach ( $caps as $cap ) {
1587
			$wp_roles->add_cap( $role, $cap );
1588
		}
1589
	}
1590
1591
	foreach ( $remove_caps as $role => $caps ) {
1592
		foreach ( $caps as $cap ) {
1593
			$wp_roles->remove_cap( $role, $cap );
1594
		}
1595
	}
1596
1597
}
1598
1599
/**
1600
 * Upgrade Routine - Clean up of User Roles for more flexibility.
1601
 *
1602
 * @since 1.8.17
1603
 */
1604
function give_v1817_cleanup_user_roles() {
1605
	/* @var Give_Updates $give_updates */
1606
	$give_updates = Give_Updates::get_instance();
1607
1608
	give_v1817_process_cleanup_user_roles();
1609
1610
	$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...
1611
1612
	// Create Give plugin roles.
1613
	$roles = new Give_Roles();
1614
	$roles->add_roles();
1615
	$roles->add_caps();
1616
1617
	give_set_upgrade_complete( 'v1817_cleanup_user_roles' );
1618
}
1619
1620
/**
1621
 * Automatic Upgrade for release 1.8.18.
1622
 *
1623
 * @since 1.8.18
1624
 */
1625
function give_v1818_upgrades() {
1626
1627
	// Remove email_access_installed from give_settings.
1628
	give_delete_option( 'email_access_installed' );
1629
}
1630
1631
/**
1632
 * Upgrade Routine - Assigns Custom Amount to existing donation of type set donation.
1633
 *
1634
 * @since 1.8.18
1635
 */
1636
function give_v1818_assign_custom_amount_set_donation() {
1637
1638
	/* @var Give_Updates $give_updates */
1639
	$give_updates = Give_Updates::get_instance();
1640
1641
	$donations = new WP_Query( array(
1642
			'paged'          => $give_updates->step,
1643
			'status'         => 'any',
1644
			'order'          => 'ASC',
1645
			'post_type'      => array( 'give_payment' ),
1646
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1647
		)
1648
	);
1649
1650
	if ( $donations->have_posts() ) {
1651
		$give_updates->set_percentage( $donations->found_posts, $give_updates->step * 100 );
1652
1653
		while ( $donations->have_posts() ) {
1654
			$donations->the_post();
1655
1656
			$form          = new Give_Donate_Form( give_get_meta( get_the_ID(), '_give_payment_form_id', true ) );
1657
			$donation_meta = give_get_payment_meta( get_the_ID() );
1658
1659
			// Update Donation meta with price_id set as custom, only if it is:
1660
			// 1. Donation Type = Set Donation.
1661
			// 2. Donation Price Id is not set to custom.
1662
			// 3. Form has not enabled custom price and donation amount assures that it is custom amount.
1663
			if (
1664
				$form->ID &&
1665
				$form->is_set_type_donation_form() &&
1666
				( 'custom' !== $donation_meta['price_id'] ) &&
1667
				$form->is_custom_price( give_get_meta( get_the_ID(), '_give_payment_total', true ) )
1668
			) {
1669
				$donation_meta['price_id'] = 'custom';
1670
				give_update_meta( get_the_ID(), '_give_payment_meta', $donation_meta );
1671
				give_update_meta( get_the_ID(), '_give_payment_price_id', 'custom' );
1672
			}
1673
		}
1674
1675
		wp_reset_postdata();
1676
	} else {
1677
		// Update Ran Successfully.
1678
		give_set_upgrade_complete( 'v1818_assign_custom_amount_set_donation' );
1679
	}
1680
}
1681
1682
/**
1683
 * Upgrade Routine - Removed Give Worker caps.
1684
 *
1685
 * See: https://github.com/WordImpress/Give/issues/2476
1686
 *
1687
 * @since 1.8.18
1688
 */
1689
function give_v1818_give_worker_role_cleanup() {
1690
1691
	/* @var Give_Updates $give_updates */
1692
	$give_updates = Give_Updates::get_instance();
1693
1694
	global $wp_roles;
1695
1696
	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...
1697
		return;
1698
	}
1699
1700
	// Remove Capabilities to user roles as required.
1701
	$remove_caps = array(
1702
		'give_worker' => array(
1703
			'delete_give_payments',
1704
			'delete_others_give_payments',
1705
			'delete_private_give_payments',
1706
			'delete_published_give_payments',
1707
			'edit_others_give_payments',
1708
			'edit_private_give_payments',
1709
			'edit_published_give_payments',
1710
			'read_private_give_payments',
1711
		),
1712
	);
1713
1714
	foreach ( $remove_caps as $role => $caps ) {
1715
		foreach ( $caps as $cap ) {
1716
			$wp_roles->remove_cap( $role, $cap );
1717
		}
1718
	}
1719
1720
	$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...
1721
1722
	// Create Give plugin roles.
1723
	$roles = new Give_Roles();
1724
	$roles->add_roles();
1725
	$roles->add_caps();
1726
1727
	give_set_upgrade_complete( 'v1818_give_worker_role_cleanup' );
1728
}
1729
1730
/**
1731
 *
1732
 * Upgrade form metadata for new metabox settings.
1733
 *
1734
 * @since  2.0
1735
 * @return void
1736
 */
1737
function give_v20_upgrades_form_metadata_callback() {
1738
	$give_updates = Give_Updates::get_instance();
1739
1740
	// form query
1741
	$forms = new WP_Query( array(
1742
			'paged'          => $give_updates->step,
1743
			'status'         => 'any',
1744
			'order'          => 'ASC',
1745
			'post_type'      => 'give_forms',
1746
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1747
		)
1748
	);
1749
1750
	if ( $forms->have_posts() ) {
1751
		$give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 100 ) );
1752
1753
		while ( $forms->have_posts() ) {
1754
			$forms->the_post();
1755
			global $post;
1756
1757
			// Update offline instruction email notification status.
1758
			$offline_instruction_notification_status = get_post_meta( get_the_ID(), '_give_customize_offline_donations', true );
1759
			$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...
1760
				'enabled',
1761
				'global',
1762
			) )
1763
				? $offline_instruction_notification_status
1764
				: 'global';
1765
			update_post_meta( get_the_ID(), '_give_offline-donation-instruction_notification', $offline_instruction_notification_status );
1766
1767
			// Update offline instruction email message.
1768
			update_post_meta(
1769
				get_the_ID(),
1770
				'_give_offline-donation-instruction_email_message',
1771
				get_post_meta(
1772
					get_the_ID(),
1773
					// @todo: Delete this option later ( version > 2.0 ).
1774
					'_give_offline_donation_email',
1775
					true
1776
				)
1777
			);
1778
1779
			// Update offline instruction email subject.
1780
			update_post_meta(
1781
				get_the_ID(),
1782
				'_give_offline-donation-instruction_email_subject',
1783
				get_post_meta(
1784
					get_the_ID(),
1785
					// @todo: Delete this option later ( version > 2.0 ).
1786
					'_give_offline_donation_subject',
1787
					true
1788
				)
1789
			);
1790
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
1791
1792
		}// End while().
1793
1794
		wp_reset_postdata();
1795
	} else {
1796
		// No more forms found, finish up.
1797
		give_set_upgrade_complete( 'v20_upgrades_form_metadata' );
1798
	}
1799
}
1800
1801
1802
/**
1803
 * Upgrade payment metadata for new metabox settings.
1804
 *
1805
 * @since  2.0
1806
 * @global wpdb $wpdb
1807
 * @return void
1808
 */
1809
function give_v20_upgrades_payment_metadata_callback() {
1810
	global $wpdb;
1811
	$give_updates = Give_Updates::get_instance();
1812
1813
	// form query
1814
	$forms = new WP_Query( array(
1815
			'paged'          => $give_updates->step,
1816
			'status'         => 'any',
1817
			'order'          => 'ASC',
1818
			'post_type'      => 'give_payment',
1819
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1820
		)
1821
	);
1822
1823
	if ( $forms->have_posts() ) {
1824
		$give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 100 ) );
1825
1826
		while ( $forms->have_posts() ) {
1827
			$forms->the_post();
1828
			global $post;
1829
1830
			// Split _give_payment_meta meta.
1831
			// @todo Remove _give_payment_meta after releases 2.0
1832
			$payment_meta = give_get_meta( $post->ID, '_give_payment_meta', true );
1833
1834
			if ( ! empty( $payment_meta ) ) {
1835
				_give_20_bc_split_and_save_give_payment_meta( $post->ID, $payment_meta );
1836
			}
1837
1838
			$deprecated_meta_keys = array(
1839
				'_give_payment_customer_id' => '_give_payment_donor_id',
1840
				'_give_payment_user_email'  => '_give_payment_donor_email',
1841
				'_give_payment_user_ip'     => '_give_payment_donor_ip',
1842
			);
1843
1844 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...
1845
				// Do not add new meta key if already exist.
1846
				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...
1847
					continue;
1848
				}
1849
1850
				$wpdb->insert(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
1851
					$wpdb->postmeta,
1852
					array(
1853
						'post_id'    => $post->ID,
1854
						'meta_key'   => $new_meta_key,
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
1855
						'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...
1856
					)
1857
				);
1858
			}
1859
1860
			// Bailout
1861 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...
1862
				/* @var Give_Donor $donor */
1863
				$donor = new Give_Donor( $donor_id );
1864
1865
				$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...
1866
				$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...
1867
				$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...
1868
				$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...
1869
				$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...
1870
				$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...
1871
1872
				// Save address.
1873
				$donor->add_address( 'billing[]', $address );
1874
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1875
1876
		}// End while().
1877
1878
		wp_reset_postdata();
1879
	} else {
1880
		// @todo Delete user id meta after releases 2.0
1881
		// $wpdb->get_var( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key=%s", '_give_payment_user_id' ) );
1882
1883
		// No more forms found, finish up.
1884
		give_set_upgrade_complete( 'v20_upgrades_payment_metadata' );
1885
	}
1886
}
1887
1888
1889
/**
1890
 * Upgrade logs data.
1891
 *
1892
 * @since  2.0
1893
 * @return void
1894
 */
1895
function give_v20_logs_upgrades_callback() {
1896
	global $wpdb;
1897
	$give_updates = Give_Updates::get_instance();
1898
1899
	// form query
1900
	$forms = new WP_Query( array(
1901
			'paged'          => $give_updates->step,
1902
			'order'          => 'DESC',
1903
			'post_type'      => 'give_log',
1904
			'post_status'    => 'any',
1905
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1906
		)
1907
	);
1908
1909
	if ( $forms->have_posts() ) {
1910
		$give_updates->set_percentage( $forms->found_posts, $give_updates->step * 100 );
1911
1912
		while ( $forms->have_posts() ) {
1913
			$forms->the_post();
1914
			global $post;
1915
1916
			if ( $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}give_logs WHERE ID=%d", $post->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...
1917
				continue;
1918
			}
1919
1920
			$term      = get_the_terms( $post->ID, 'give_log_type' );
1921
			$term      = ! is_wp_error( $term ) && ! empty( $term ) ? $term[0] : array();
1922
			$term_name = ! empty( $term ) ? $term->slug : '';
1923
1924
			$log_data = array(
1925
				'ID'           => $post->ID,
1926
				'log_title'    => $post->post_title,
1927
				'log_content'  => $post->post_content,
1928
				'log_parent'   => 0,
1929
				'log_type'     => $term_name,
1930
				'log_date'     => $post->post_date,
1931
				'log_date_gmt' => $post->post_date_gmt,
1932
			);
1933
			$log_meta = array();
1934
1935 View Code Duplication
			if ( $old_log_meta = get_post_meta( $post->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...
1936
				foreach ( $old_log_meta as $meta_key => $meta_value ) {
1937
					switch ( $meta_key ) {
1938
						case '_give_log_payment_id':
1939
							$log_data['log_parent']        = current( $meta_value );
1940
							$log_meta['_give_log_form_id'] = $post->post_parent;
1941
							break;
1942
1943
						default:
1944
							$log_meta[ $meta_key ] = current( $meta_value );
1945
					}
1946
				}
1947
			}
1948
1949
			if ( 'api_request' === $term_name ) {
1950
				$log_meta['_give_log_api_query'] = $post->post_excerpt;
1951
			}
1952
1953
			$wpdb->insert( "{$wpdb->prefix}give_logs", $log_data );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
1954
1955 View Code Duplication
			if ( ! empty( $log_meta ) ) {
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...
1956
				foreach ( $log_meta as $meta_key => $meta_value ) {
1957
					Give()->logs->logmeta_db->update_meta( $post->ID, $meta_key, $meta_value );
1958
				}
1959
			}
1960
1961
			$logIDs[] = $post->ID;
1962
		}// End while().
1963
1964
		wp_reset_postdata();
1965
	} else {
1966
		// @todo: Delete terms and taxonomy after releases 2.0.
1967
		/*$terms = get_terms( 'give_log_type', array( 'fields' => 'ids', 'hide_empty' => false ) );
1968
		if ( ! empty( $terms ) ) {
1969
			foreach ( $terms as $term ) {
1970
				wp_delete_term( $term, 'give_log_type' );
1971
			}
1972
		}*/
1973
1974
		// @todo: Delete logs after releases 2.0.
1975
		/*$logIDs = get_posts( array(
1976
				'order'          => 'DESC',
1977
				'post_type'      => 'give_log',
1978
				'post_status'    => 'any',
1979
				'posts_per_page' => - 1,
1980
				'fields'         => 'ids',
1981
			)
1982
		);*/
1983
1984
		/*if ( ! empty( $logIDs ) ) {
1985
			foreach ( $logIDs as $log ) {
1986
				// Delete term relationship and posts.
1987
				wp_delete_object_term_relationships( $log, 'give_log_type' );
1988
				wp_delete_post( $log, true );
1989
			}
1990
		}*/
1991
1992
		// @todo: Unregister taxonomy after releases 2.0.
1993
		/*unregister_taxonomy( 'give_log_type' );*/
1994
1995
		// Delete log cache.
1996
		Give()->logs->delete_cache();
1997
1998
		// No more forms found, finish up.
1999
		give_set_upgrade_complete( 'v20_logs_upgrades' );
2000
	}
2001
}
2002
2003
2004
/**
2005
 * Move payment and form metadata to new table
2006
 *
2007
 * @since  2.0
2008
 * @return void
2009
 */
2010
function give_v20_move_metadata_into_new_table_callback() {
2011
	global $wpdb;
2012
	$give_updates = Give_Updates::get_instance();
2013
2014
	// form query
2015
	$payments = new WP_Query( array(
2016
			'paged'          => $give_updates->step,
2017
			'status'         => 'any',
2018
			'order'          => 'ASC',
2019
			'post_type'      => array( 'give_forms', 'give_payment' ),
2020
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
2021
		)
2022
	);
2023
2024
	if ( $payments->have_posts() ) {
2025
		$give_updates->set_percentage( $payments->found_posts, $give_updates->step * 100 );
2026
2027
		while ( $payments->have_posts() ) {
2028
			$payments->the_post();
2029
			global $post;
2030
2031
			$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...
2032
				$wpdb->prepare(
2033
					"SELECT * FROM $wpdb->postmeta where post_id=%d",
2034
					get_the_ID()
2035
				),
2036
				ARRAY_A
2037
			);
2038
2039 View Code Duplication
			if ( ! empty( $meta_data ) ) {
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...
2040
				foreach ( $meta_data as $index => $data ) {
2041
					// Check for duplicate meta values.
2042
					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
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...
2043
						continue;
2044
					}
2045
2046
					switch ( $post->post_type ) {
2047
						case 'give_forms':
2048
							$data['form_id'] = $data['post_id'];
2049
							unset( $data['post_id'] );
2050
2051
							Give()->form_meta->insert( $data );
2052
							// @todo: delete form meta from post meta table after releases 2.0.
2053
							/*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2054
2055
							break;
2056
2057
						case 'give_payment':
2058
							$data['payment_id'] = $data['post_id'];
2059
							unset( $data['post_id'] );
2060
2061
							Give()->payment_meta->insert( $data );
2062
2063
							// @todo: delete donation meta from post meta table after releases 2.0.
2064
							/*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2065
2066
							break;
2067
					}
2068
				}
2069
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2070
2071
		}// End while().
2072
2073
		wp_reset_postdata();
2074
	} else {
2075
		// No more forms found, finish up.
2076
		give_set_upgrade_complete( 'v20_move_metadata_into_new_table' );
2077
	}
2078
2079
}
2080
2081
/**
2082
 * Upgrade routine for splitting donor name into first name and last name.
2083
 *
2084
 * @since 2.0
2085
 *
2086
 * @return void
2087
 */
2088
function give_v20_upgrades_donor_name() {
2089
	/* @var Give_Updates $give_updates */
2090
	$give_updates = Give_Updates::get_instance();
2091
2092
	$donors = Give()->donors->get_donors( array(
2093
		'paged'  => $give_updates->step,
2094
		'number' => 100,
2095
	) );
2096
2097
	if ( $donors ) {
2098
		$give_updates->set_percentage( count( $donors ), $give_updates->step * 100 );
2099
		// Loop through Donors
2100
		foreach ( $donors as $donor ) {
2101
2102
			$donor_name       = explode( ' ', $donor->name, 2 );
2103
			$donor_first_name = Give()->donor_meta->get_meta( $donor->id, '_give_donor_first_name' );
2104
			$donor_last_name  = Give()->donor_meta->get_meta( $donor->id, '_give_donor_last_name' );
2105
2106
			// If first name meta of donor is not created, then create it.
2107 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...
2108
				Give()->donor_meta->add_meta( $donor->id, '_give_donor_first_name', $donor_name[0] );
2109
			}
2110
2111
			// If last name meta of donor is not created, then create it.
2112 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...
2113
				Give()->donor_meta->add_meta( $donor->id, '_give_donor_last_name', $donor_name[1] );
2114
			}
2115
2116
			// If Donor is connected with WP User then update user meta.
2117 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...
2118
				if ( isset( $donor_name[0] ) ) {
2119
					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...
2120
				}
2121
				if ( isset( $donor_name[1] ) ) {
2122
					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...
2123
				}
2124
			}
2125
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2126
2127
	} else {
2128
		// The Update Ran.
2129
		give_set_upgrade_complete( 'v20_upgrades_donor_name' );
2130
	}
2131
2132
}
2133
2134
/**
2135
 * Upgrade routine for user addresses.
2136
 *
2137
 * @since 2.0
2138
 * @global wpdb $wpdb
2139
 *
2140
 * @return void
2141
 */
2142
function give_v20_upgrades_user_address() {
2143
	global $wpdb;
2144
2145
	/* @var Give_Updates $give_updates */
2146
	$give_updates = Give_Updates::get_instance();
2147
2148
	/* @var WP_User_Query $user_query */
2149
	$user_query = new WP_User_Query(
2150
		array(
2151
			'number' => 100,
2152
			'paged'  => $give_updates->step,
2153
		)
2154
	);
2155
2156
	$users = $user_query->get_results();
2157
2158
	if ( $users ) {
2159
		$give_updates->set_percentage( $user_query->get_total(), $give_updates->step * 100 );
2160
2161
		// Loop through Donors
2162
		foreach ( $users as $user ) {
2163
			/* @var Give_Donor $donor */
2164
			$donor = new Give_Donor( $user->ID, true );
2165
2166
			if ( ! $donor->id ) {
2167
				continue;
2168
			}
2169
2170
			$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...
2171
				$wpdb->prepare(
2172
					"
2173
					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...
2174
					WHERE user_id=%s
2175
					AND meta_key=%s
2176
					",
2177
					$user->ID,
2178
					'_give_user_address'
2179
				)
2180
			);
2181
2182 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...
2183
				$address = maybe_unserialize( $address );
2184
				$donor->add_address( 'personal', $address );
2185
				$donor->add_address( 'billing[]', $address );
2186
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
2187
2188
				// @todo: delete _give_user_address from user meta after releases 2.0.
2189
				/*delete_user_meta( $user->ID, '_give_user_address' );*/
2190
			}
2191
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2192
2193
	} else {
2194
		// The Update Ran.
2195
		give_set_upgrade_complete( 'v20_upgrades_user_address' );
2196
	}
2197
2198
}
2199
2200
/**
2201
 * Upgrade logs data.
2202
 *
2203
 * @since  2.0
2204
 * @global wpdb $wpdb
2205
 * @return void
2206
 */
2207
function give_v20_rename_donor_tables_callback() {
2208
	global $wpdb;
2209
2210
	/* @var Give_Updates $give_updates */
2211
	$give_updates = Give_Updates::get_instance();
2212
2213
	$tables = array(
2214
		"{$wpdb->prefix}give_customers"    => "{$wpdb->prefix}give_donors",
2215
		"{$wpdb->prefix}give_customermeta" => "{$wpdb->prefix}give_donormeta",
2216
	);
2217
2218
	// Alter customer table
2219
	foreach ( $tables as $old_table => $new_table ) {
2220
		if (
2221
			$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...
2222
			! $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...
2223
		) {
2224
			$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...
2225
2226
			if ( "{$wpdb->prefix}give_donormeta" === $new_table ) {
2227
				$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...
2228
			}
2229
		}
2230
	}
2231
2232
	$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...
2233
2234
	// No more forms found, finish up.
2235
	give_set_upgrade_complete( 'v20_rename_donor_tables' );
2236
2237
	// Re initiate donor classes.
2238
	Give()->donors     = new Give_DB_Donors();
2239
	Give()->donor_meta = new Give_DB_Donor_Meta();
2240
}
2241
2242
2243
/**
2244
 * Create missing meta tables.
2245
 *
2246
 * @since  2.0.1
2247
 * @global wpdb $wpdb
2248
 * @return void
2249
 */
2250
function give_v201_create_tables() {
2251
	global $wpdb;
2252
2253
	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...
2254
		Give()->payment_meta->create_table();
2255
	}
2256
2257
	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...
2258
		Give()->form_meta->create_table();
2259
	}
2260
2261
	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...
2262
		Give()->logs->log_db->create_table();
2263
	}
2264
2265
	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...
2266
		Give()->logs->logmeta_db->create_table();
2267
	}
2268
}
2269
2270
/**
2271
 * Upgrade payment metadata for new metabox settings.
2272
 *
2273
 * @since  2.0.1
2274
 * @global wpdb $wpdb
2275
 * @return void
2276
 */
2277
function give_v201_upgrades_payment_metadata_callback() {
2278
	global $wpdb, $post;
2279
	$give_updates = Give_Updates::get_instance();
2280
	give_v201_create_tables();
2281
2282
	$payments = $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...
2283
		"
2284
			SELECT ID FROM $wpdb->posts
2285
			WHERE 1=1
2286
			AND ( 
2287
  				$wpdb->posts.post_date >= '2018-01-08 00:00:00'
2288
			)
2289
			AND $wpdb->posts.post_type = 'give_payment'
2290
			AND {$wpdb->posts}.post_status IN ('" . implode( "','", array_keys( give_get_payment_statuses() ) ) . "')
2291
			ORDER BY $wpdb->posts.post_date ASC 
2292
			LIMIT 100
2293
			OFFSET " . $give_updates->get_offset( 100 )
2294
	);
2295
2296
	if ( ! empty( $payments ) ) {
2297
		$give_updates->set_percentage( give_get_total_post_type_count( 'give_payment' ), ( $give_updates->step * 100 ) );
2298
2299
		foreach ( $payments as $payment_id ) {
2300
			$post = get_post( $payment_id );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
2301
			setup_postdata( $post );
2302
2303
			// Do not add new meta keys if already refactored.
2304
			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...
2305
				continue;
2306
			}
2307
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
2308
2309
			// Split _give_payment_meta meta.
2310
			// @todo Remove _give_payment_meta after releases 2.0
2311
			$payment_meta = give_get_meta( $post->ID, '_give_payment_meta', true );
2312
2313
			if ( ! empty( $payment_meta ) ) {
2314
				_give_20_bc_split_and_save_give_payment_meta( $post->ID, $payment_meta );
2315
			}
2316
2317
			$deprecated_meta_keys = array(
2318
				'_give_payment_customer_id' => '_give_payment_donor_id',
2319
				'_give_payment_user_email'  => '_give_payment_donor_email',
2320
				'_give_payment_user_ip'     => '_give_payment_donor_ip',
2321
			);
2322
2323 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...
2324
				// Do not add new meta key if already exist.
2325
				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...
2326
					continue;
2327
				}
2328
2329
				$wpdb->insert(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
2330
					$wpdb->postmeta,
2331
					array(
2332
						'post_id'    => $post->ID,
2333
						'meta_key'   => $new_meta_key,
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
2334
						'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...
2335
					)
2336
				);
2337
			}
2338
2339
			// Bailout
2340 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...
2341
				/* @var Give_Donor $donor */
2342
				$donor = new Give_Donor( $donor_id );
2343
2344
				$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...
2345
				$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...
2346
				$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...
2347
				$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...
2348
				$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...
2349
				$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...
2350
2351
				// Save address.
2352
				$donor->add_address( 'billing[]', $address );
2353
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2354
2355
		}// End while().
2356
2357
		wp_reset_postdata();
2358
	} else {
2359
		// @todo Delete user id meta after releases 2.0
2360
		// $wpdb->get_var( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key=%s", '_give_payment_user_id' ) );
2361
2362
		// No more forms found, finish up.
2363
		give_set_upgrade_complete( 'v201_upgrades_payment_metadata' );
2364
	}
2365
}
2366
2367
/**
2368
 * Move payment and form metadata to new table
2369
 *
2370
 * @since  2.0.1
2371
 * @return void
2372
 */
2373
function give_v201_move_metadata_into_new_table_callback() {
2374
	global $wpdb, $post;
2375
	$give_updates = Give_Updates::get_instance();
2376
	give_v201_create_tables();
2377
2378
	$payments = $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...
2379
		"
2380
			SELECT ID FROM $wpdb->posts 
2381
			WHERE 1=1
2382
			AND ( $wpdb->posts.post_type = 'give_payment' OR $wpdb->posts.post_type = 'give_forms' )
2383
			AND {$wpdb->posts}.post_status IN ('" . implode( "','", array_keys( give_get_payment_statuses() ) ) . "')
2384
			ORDER BY $wpdb->posts.post_date ASC 
2385
			LIMIT 100
2386
			OFFSET " . $give_updates->get_offset( 100 )
2387
	);
2388
2389
	if ( ! empty( $payments ) ) {
2390
		$give_updates->set_percentage( give_get_total_post_type_count( array(
0 ignored issues
show
Documentation introduced by
array('give_forms', 'give_payment') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

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...
2391
			'give_forms',
2392
			'give_payment',
2393
		) ), $give_updates->step * 100 );
2394
2395
		foreach ( $payments as $payment_id ) {
2396
			$post = get_post( $payment_id );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
2397
			setup_postdata( $post );
2398
2399
			$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...
2400
				$wpdb->prepare(
2401
					"SELECT * FROM $wpdb->postmeta where post_id=%d",
2402
					get_the_ID()
2403
				),
2404
				ARRAY_A
2405
			);
2406
2407 View Code Duplication
			if ( ! empty( $meta_data ) ) {
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...
2408
				foreach ( $meta_data as $index => $data ) {
2409
					// Check for duplicate meta values.
2410
					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
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...
2411
						continue;
2412
					}
2413
2414
					switch ( $post->post_type ) {
2415
						case 'give_forms':
2416
							$data['form_id'] = $data['post_id'];
2417
							unset( $data['post_id'] );
2418
2419
							Give()->form_meta->insert( $data );
2420
							// @todo: delete form meta from post meta table after releases 2.0.
2421
							/*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2422
2423
							break;
2424
2425
						case 'give_payment':
2426
							$data['payment_id'] = $data['post_id'];
2427
							unset( $data['post_id'] );
2428
2429
							Give()->payment_meta->insert( $data );
2430
2431
							// @todo: delete donation meta from post meta table after releases 2.0.
2432
							/*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2433
2434
							break;
2435
					}
2436
				}
2437
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2438
2439
		}// End while().
2440
2441
		wp_reset_postdata();
2442
	} else {
2443
		// No more forms found, finish up.
2444
		give_set_upgrade_complete( 'v201_move_metadata_into_new_table' );
2445
	}
2446
2447
}
2448
2449
/**
2450
 * Move data to new log table.
2451
 *
2452
 * @since  2.0.1
2453
 * @return void
2454
 */
2455
function give_v201_logs_upgrades_callback() {
2456
	global $wpdb, $post;
2457
	$give_updates = Give_Updates::get_instance();
2458
	give_v201_create_tables();
2459
2460
	$logs = $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...
2461
		"
2462
			SELECT ID FROM $wpdb->posts 
2463
			WHERE 1=1
2464
			AND $wpdb->posts.post_type = 'give_log'
2465
			AND {$wpdb->posts}.post_status IN ('" . implode( "','", array_keys( give_get_payment_statuses() ) ) . "')
2466
			ORDER BY $wpdb->posts.post_date ASC 
2467
			LIMIT 100
2468
			OFFSET " . $give_updates->get_offset( 100 )
2469
	);
2470
2471
	if ( ! empty( $logs ) ) {
2472
		$give_updates->set_percentage( give_get_total_post_type_count( 'give_log' ), $give_updates->step * 100 );
2473
2474
		foreach ( $logs as $log_id ) {
2475
			$post = get_post( $log_id );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
2476
			setup_postdata( $post );
2477
2478
			if ( $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}give_logs WHERE ID=%d", $post->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...
2479
				continue;
2480
			}
2481
2482
			$term      = get_the_terms( $post->ID, 'give_log_type' );
2483
			$term      = ! is_wp_error( $term ) && ! empty( $term ) ? $term[0] : array();
2484
			$term_name = ! empty( $term ) ? $term->slug : '';
2485
2486
			$log_data = array(
2487
				'ID'           => $post->ID,
2488
				'log_title'    => $post->post_title,
2489
				'log_content'  => $post->post_content,
2490
				'log_parent'   => 0,
2491
				'log_type'     => $term_name,
2492
				'log_date'     => $post->post_date,
2493
				'log_date_gmt' => $post->post_date_gmt,
2494
			);
2495
			$log_meta = array();
2496
2497 View Code Duplication
			if ( $old_log_meta = get_post_meta( $post->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...
2498
				foreach ( $old_log_meta as $meta_key => $meta_value ) {
2499
					switch ( $meta_key ) {
2500
						case '_give_log_payment_id':
2501
							$log_data['log_parent']        = current( $meta_value );
2502
							$log_meta['_give_log_form_id'] = $post->post_parent;
2503
							break;
2504
2505
						default:
2506
							$log_meta[ $meta_key ] = current( $meta_value );
2507
					}
2508
				}
2509
			}
2510
2511
			if ( 'api_request' === $term_name ) {
2512
				$log_meta['_give_log_api_query'] = $post->post_excerpt;
2513
			}
2514
2515
			$wpdb->insert( "{$wpdb->prefix}give_logs", $log_data );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
2516
2517 View Code Duplication
			if ( ! empty( $log_meta ) ) {
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...
2518
				foreach ( $log_meta as $meta_key => $meta_value ) {
2519
					Give()->logs->logmeta_db->update_meta( $post->ID, $meta_key, $meta_value );
2520
				}
2521
			}
2522
2523
			$logIDs[] = $post->ID;
2524
		}// End while().
2525
2526
		wp_reset_postdata();
2527
	} else {
2528
		// Delete log cache.
2529
		Give()->logs->delete_cache();
2530
2531
		// No more forms found, finish up.
2532
		give_set_upgrade_complete( 'v201_logs_upgrades' );
2533
	}
2534
}
2535
2536
2537
/**
2538
 * Add missing donor.
2539
 *
2540
 * @since  2.0.1
2541
 * @return void
2542
 */
2543
function give_v201_add_missing_donors_callback() {
2544
	global $wpdb;
2545
	give_v201_create_tables();
2546
2547
	if ( $wpdb->query( $wpdb->prepare( "SHOW TABLES LIKE %s", "{$wpdb->prefix}give_customers" ) ) ) {
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...
2548
		$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...
2549
		$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...
2550
		$donor_data = array();
2551
2552
		if ( $missing_donors = array_diff( $customers, $donors ) ) {
2553
			foreach ( $missing_donors as $donor_id ) {
2554
				$donor_data[] = array(
2555
					'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...
2556
					'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...
2557
2558
				);
2559
			}
2560
		}
2561
2562
		if ( ! empty( $donor_data ) ) {
2563
			$donor_table_name      = Give()->donors->table_name;
2564
			$donor_meta_table_name = Give()->donor_meta->table_name;
2565
2566
			Give()->donors->table_name     = "{$wpdb->prefix}give_donors";
2567
			Give()->donor_meta->table_name = "{$wpdb->prefix}give_donormeta";
2568
2569
			foreach ( $donor_data as $donor ) {
2570
				$donor['info'][0] = (array) $donor['info'][0];
2571
2572
				// Prevent duplicate meta id issue.
2573
				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
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...
2574
					continue;
2575
				}
2576
2577
				$donor_id = Give()->donors->add( $donor['info'][0] );
2578
2579
				if ( ! empty( $donor['meta'] ) ) {
2580
					foreach ( $donor['meta'] as $donor_meta ) {
2581
						$donor_meta = (array) $donor_meta;
2582
2583
						// Prevent duplicate meta id issue.
2584
						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
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...
2585
							unset( $donor_meta['meta_id'] );
2586
						}
2587
2588
						$donor_meta['donor_id'] = $donor_meta['customer_id'];
2589
						unset( $donor_meta['customer_id'] );
2590
2591
						Give()->donor_meta->insert( $donor_meta );
2592
					}
2593
				}
2594
2595
				/**
2596
				 * Fix donor name and address
2597
				 */
2598
				$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...
2599
					$wpdb->prepare(
2600
						"
2601
					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...
2602
					WHERE user_id=%s
2603
					AND meta_key=%s
2604
					",
2605
						$donor['info'][0]['user_id'],
2606
						'_give_user_address'
2607
					)
2608
				);
2609
2610
				$donor = new Give_Donor( $donor_id );
2611
2612 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...
2613
					$address = maybe_unserialize( $address );
2614
					$donor->add_address( 'personal', $address );
2615
					$donor->add_address( 'billing[]', $address );
2616
				}
2617
2618
				$donor_name       = explode( ' ', $donor->name, 2 );
2619
				$donor_first_name = Give()->donor_meta->get_meta( $donor->id, '_give_donor_first_name' );
2620
				$donor_last_name  = Give()->donor_meta->get_meta( $donor->id, '_give_donor_last_name' );
2621
2622
				// If first name meta of donor is not created, then create it.
2623 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...
2624
					Give()->donor_meta->add_meta( $donor->id, '_give_donor_first_name', $donor_name[0] );
2625
				}
2626
2627
				// If last name meta of donor is not created, then create it.
2628 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...
2629
					Give()->donor_meta->add_meta( $donor->id, '_give_donor_last_name', $donor_name[1] );
2630
				}
2631
2632
				// If Donor is connected with WP User then update user meta.
2633 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...
2634
					if ( isset( $donor_name[0] ) ) {
2635
						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...
2636
					}
2637
					if ( isset( $donor_name[1] ) ) {
2638
						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...
2639
					}
2640
				}
2641
			}
2642
2643
			Give()->donors->table_name     = $donor_table_name;
2644
			Give()->donor_meta->table_name = $donor_meta_table_name;
2645
		}
2646
	}
2647
2648
	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...
2649
	give_set_upgrade_complete( 'v201_add_missing_donors' );
2650
}
2651
2652
2653
/**
2654
 * Version 2.0.3 automatic updates
2655
 *
2656
 * @since 2.0.3
2657
 */
2658
function give_v203_upgrades() {
2659
	global $wpdb;
2660
2661
	// Do not auto load option.
2662
	$wpdb->update( $wpdb->options, array( 'autoload' => 'no' ), array( 'option_name' => 'give_completed_upgrades' ) );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
2663
2664
	// Remove from cache.
2665
	$all_options = wp_load_alloptions();
2666
2667
	if ( isset( $all_options['give_completed_upgrades'] ) ) {
2668
		unset( $all_options['give_completed_upgrades'] );
2669
		wp_cache_set( 'alloptions', $all_options, 'options' );
2670
	}
2671
2672
}
2673
2674
2675
/**
2676
 * Version 2.2.0 automatic updates
2677
 *
2678
 * @since 2.2.0
2679
 */
2680
function give_v220_upgrades(){
2681
	global $wpdb;
2682
2683
	/**
2684
	 * Update 1
2685
	 *
2686
	 * Delete wp session data
2687
	 */
2688
	give_v220_delete_wp_session_data();
2689
2690
	/**
2691
	 * Update 2
2692
	 *
2693
	 * Rename payment table
2694
	 */
2695
	give_v220_rename_donation_meta_type_callback();
2696
2697
	/**
2698
	 * Update 2
2699
	 *
2700
	 * Set autoload to no to reduce result weight from WordPress query
2701
	 */
2702
2703
	$options = array(
2704
		'give_settings',
2705
		'give_version',
2706
		'give_version_upgraded_from',
2707
		'give_default_api_version',
2708
		'give_site_address_before_migrate',
2709
		'_give_table_check',
2710
		'give_recently_activated_addons',
2711
		'give_is_addon_activated',
2712
		'give_last_paypal_ipn_received',
2713
		'give_use_php_sessions',
2714
		'give_subscriptions',
2715
		'_give_subscriptions_edit_last'
2716
	);
2717
2718
	// Add all table version option name
2719
	// Add banner option *_active_by_user
2720
	$option_like = $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...
2721
		"
2722
		SELECT option_name
2723
		FROM $wpdb->options
2724
		WHERE option_name like '%give%'
2725
		AND (
2726
			option_name like '%_db_version%'
2727
			OR option_name like '%_active_by_user%'
2728
			OR option_name like '%_license_active%'
2729
		)
2730
		"
2731
	);
2732
2733
	if( ! empty( $option_like ) ) {
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...
2734
		$options = array_merge( $options, $option_like );
2735
	}
2736
2737
	$options_str =  '\'' . implode( "','", $options ) . '\'';
0 ignored issues
show
introduced by
Expected 1 space after "="; 2 found
Loading history...
2738
2739
	$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...
2740
		"
2741
		UPDATE $wpdb->options
2742
		SET autoload = 'no'
2743
		WHERE option_name IN ( {$options_str} )
2744
		"
2745
	);
2746
}
2747
2748
/**
2749
 * Version 2.2.1 automatic updates
2750
 *
2751
 * @since 2.2.1
2752
 */
2753
function give_v221_upgrades() {
2754
	global $wpdb;
2755
2756
	/**
2757
	 * Update  1
2758
	 *
2759
	 * Change column length
2760
	 */
2761
	$wpdb->query( "ALTER TABLE $wpdb->donors MODIFY email varchar(255) NOT NULL" );
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...
2762
}
2763
2764
/**
2765
 * Upgrade routine for 2.1 to set form closed status for all the donation forms.
2766
 *
2767
 * @since 2.1
2768
 */
2769
function give_v210_verify_form_status_upgrades_callback() {
2770
2771
	$give_updates = Give_Updates::get_instance();
2772
2773
	// form query.
2774
	$donation_forms = new WP_Query( array(
2775
			'paged'          => $give_updates->step,
2776
			'status'         => 'any',
2777
			'order'          => 'ASC',
2778
			'post_type'      => 'give_forms',
2779
			'posts_per_page' => 20,
2780
		)
2781
	);
2782
2783
	if ( $donation_forms->have_posts() ) {
2784
		$give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
2785
2786
		while ( $donation_forms->have_posts() ) {
2787
			$donation_forms->the_post();
2788
			$form_id = get_the_ID();
2789
2790
			$form_closed_status = give_get_meta( $form_id, '_give_form_status', true );
2791
			if ( empty( $form_closed_status ) ) {
2792
				give_set_form_closed_status( $form_id );
2793
			}
2794
		}
2795
2796
		/* Restore original Post Data */
2797
		wp_reset_postdata();
2798
2799
	} else {
2800
2801
		// The Update Ran.
2802
		give_set_upgrade_complete( 'v210_verify_form_status_upgrades' );
2803
	}
2804
}
2805
2806
/**
2807
 * Upgrade routine for 2.1.3 to delete meta which is not attach to any donation.
2808
 *
2809
 * @since 2.1
2810
 */
2811
function give_v213_delete_donation_meta_callback() {
2812
	global $wpdb;
2813
	$give_updates        = Give_Updates::get_instance();
2814
	$donation_meta_table = Give()->payment_meta->table_name;
2815
2816
	$donations = $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...
2817
		"
2818
		SELECT DISTINCT payment_id
2819
		FROM {$donation_meta_table}
2820
		LIMIT 20
2821
		OFFSET {$give_updates->get_offset( 20 )}
2822
		"
2823
	);
2824
2825
	if ( ! empty( $donations ) ) {
2826
		foreach ( $donations as $donation ) {
2827
			$donation_obj = get_post( $donation );
2828
2829
			if ( ! $donation_obj instanceof WP_Post ) {
0 ignored issues
show
Bug introduced by
The class WP_Post 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...
2830
				Give()->payment_meta->delete_all_meta( $donation );
2831
			}
2832
		}
2833
	} else {
2834
2835
		// The Update Ran.
2836
		give_set_upgrade_complete( 'v213_delete_donation_meta' );
2837
	}
2838
}
2839
2840
/**
2841
 * Rename donation meta type
2842
 *
2843
 * @see https://github.com/restrictcontentpro/restrict-content-pro/issues/1656
2844
 *
2845
 * @since 2.2.0
2846
 */
2847
function give_v220_rename_donation_meta_type_callback(){
2848
	global $wpdb;
2849
2850
	// Check upgrade before running.
2851
	if(
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...
2852
		give_has_upgrade_completed( 'v220_rename_donation_meta_type' )
2853
		|| ! $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...
2854
	) {
2855
		return;
2856
	}
2857
2858
	$wpdb->query( "ALTER TABLE {$wpdb->prefix}give_paymentmeta CHANGE COLUMN payment_id donation_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...
2859
	$wpdb->query( "ALTER TABLE {$wpdb->prefix}give_paymentmeta RENAME TO {$wpdb->prefix}give_donationmeta" );
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...
2860
2861
	give_set_upgrade_complete('v220_rename_donation_meta_type');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
2862
}
2863
2864
/**
2865
 * Adds 'view_give_payments' capability to 'give_manager' user role.
2866
 *
2867
 * @since 2.1.5
2868
 */
2869
function give_v215_update_donor_user_roles_callback() {
2870
2871
	$role = get_role( 'give_manager' );
2872
	$role->add_cap( 'view_give_payments' );
2873
2874
	give_set_upgrade_complete( 'v215_update_donor_user_roles' );
2875
}
2876
2877
2878
2879
/**
2880
 * Remove all wp session data from the options table, regardless of expiration.
2881
 *
2882
 * @since 2.2.0
2883
 *
2884
 * @global wpdb $wpdb
2885
 */
2886
function give_v220_delete_wp_session_data(){
2887
	global $wpdb;
2888
2889
	$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE '_wp_session_%'" );
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...
2890
}
2891
2892
2893
/**
2894
 * Update donor meta
2895
 * Set "_give_anonymous_donor" meta key to "0" if not exist
2896
 *
2897
 *
2898
 * @since 2.2.4
2899
 */
2900
function give_v224_update_donor_meta_callback() {
2901
	/* @var Give_Updates $give_updates */
2902
	$give_updates = Give_Updates::get_instance();
2903
2904
	$donor_count = Give()->donors->count( array(
2905
		'number' => -1,
2906
	) );
2907
2908
	$donors = Give()->donors->get_donors( array(
2909
		'paged'  => $give_updates->step,
2910
		'number' => 100,
2911
	) );
2912
2913
	if ( $donors ) {
2914
		$give_updates->set_percentage( $donor_count, $give_updates->step * 100 );
2915
		// Loop through Donors
2916
		foreach ( $donors as $donor ) {
2917
			$anonymous_metadata = Give()->donor_meta->get_meta( $donor->id, '_give_anonymous_donor', true );
2918
2919
			// If first name meta of donor is not created, then create it.
2920
			if ( ! in_array( $anonymous_metadata, array( '0', '1' ) ) ) {
2921
				Give()->donor_meta->add_meta( $donor->id, '_give_anonymous_donor', '0' );
2922
			}
2923
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2924
2925
	} else {
2926
		// The Update Ran.
2927
		give_set_upgrade_complete( 'v224_update_donor_meta' );
2928
	}
2929
}
2930
2931
2932
/**
2933
 * Move donor notes to comment table
2934
 *
2935
 * @since 2.3.0
2936
 */
2937
function give_v230_move_donor_note_callback() {
2938
	/* @var Give_Updates $give_updates */
2939
	$give_updates = Give_Updates::get_instance();
2940
2941
	$donor_count = Give()->donors->count( array(
2942
		'number' => - 1,
2943
	) );
2944
2945
	$donors = Give()->donors->get_donors( array(
2946
		'paged'  => $give_updates->step,
2947
		'number' => 100,
2948
	) );
2949
2950
	if ( $donors ) {
2951
		$give_updates->set_percentage( $donor_count, $give_updates->step * 100 );
2952
		// Loop through Donors
2953
		foreach ( $donors as $donor ) {
2954
			$notes = trim( Give()->donors->get_column( 'notes', $donor->id ) );
2955
2956
			// If first name meta of donor is not created, then create it.
2957
			if ( ! empty( $notes ) ) {
2958
				$notes = array_values( array_filter( array_map( 'trim', explode( "\n", $notes ) ), 'strlen' ) );
2959
2960
				foreach ( $notes as $note ) {
2961
					$note      = array_map( 'trim', explode( '-', $note ) );
2962
					$timestamp = strtotime( $note[0] );
2963
2964
					Give()->comment->db->add(
2965
						array(
2966
							'comment_content'  => $note[1],
2967
							'comment_date'     => date( 'Y-m-d H:i:s', $timestamp ),
2968
							'comment_date_gmt' => get_gmt_from_date( date( 'Y-m-d H:i:s', $timestamp ) ),
2969
							'comment_parent'   => $donor->id,
2970
							'comment_type'     => 'donor',
2971
						)
2972
					);
2973
				}
2974
			}
2975
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2976
2977
	} else {
2978
		// The Update Ran.
2979
		give_set_upgrade_complete( 'v230_move_donor_note' );
2980
	}
2981
}
2982
2983
/**
2984
 * Move donor notes to comment table
2985
 *
2986
 * @since 2.3.0
2987
 */
2988
function give_v230_move_donation_note_callback() {
2989
	global $wpdb;
2990
2991
	/* @var Give_Updates $give_updates */
2992
	$give_updates = Give_Updates::get_instance();
2993
2994
	$donation_note_count = $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...
2995
		$wpdb->prepare(
2996
			"
2997
			SELECT count(*)
2998
			FROM {$wpdb->comments}
2999
			WHERE comment_type=%s
3000
			",
3001
			'give_payment_note'
3002
		)
3003
	);
3004
3005
	$comments = $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...
3006
		$wpdb->prepare(
3007
			"
3008
			SELECT *
3009
			FROM {$wpdb->comments}
3010
			WHERE comment_type=%s
3011
			LIMIT 100
3012
			OFFSET %d
3013
			",
3014
			'give_payment_note',
3015
			$give_updates->get_offset( 100 )
3016
		)
3017
	);
3018
3019
	if ( $comments ) {
3020
		$give_updates->set_percentage( $donation_note_count, $give_updates->step * 100 );
3021
3022
		// Loop through Donors
3023
		foreach ( $comments as $comment ) {
3024
			$donation_id = $comment->comment_post_ID;
3025
			$form_id     = give_get_payment_form_id( $donation_id );
3026
3027
			$comment_id = Give()->comment->db->add(
3028
				array(
3029
					'comment_content'  => $comment->comment_content,
3030
					'comment_date'     => date( 'Y-m-d H:i:s', strtotime( $comment->comment_date ) ),
3031
					'comment_date_gmt' => get_gmt_from_date( date( 'Y-m-d H:i:s', strtotime( $comment->comment_date_gmt ) ) ),
3032
					'comment_parent'   => $comment->comment_post_ID,
3033
					'comment_type'     => 'donation',
3034
				)
3035
			);
3036
3037
			if ( $comment_meta = get_comment_meta( $comment->comment_ID ) ) {
3038
				foreach ( $comment_meta as $meta_key => $meta_value ) {
3039
					Give()->comment->db_meta->update_meta( $comment_id, $meta_key, $meta_value );
0 ignored issues
show
Bug introduced by
It seems like $comment_id defined by Give()->comment->db->add...t_type' => 'donation')) on line 3027 can also be of type boolean; however, Give_DB_Meta::update_meta() does only seem to accept integer, 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...
3040
				}
3041
			}
3042
3043
			Give()->comment->db_meta->update_meta( $comment_id, 'form_id', $form_id );
0 ignored issues
show
Bug introduced by
It seems like $comment_id defined by Give()->comment->db->add...t_type' => 'donation')) on line 3027 can also be of type boolean; however, Give_DB_Meta::update_meta() does only seem to accept integer, 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...
3044
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
3045
3046
	} else {
3047
		// The Update Ran.
3048
		give_set_upgrade_complete( 'v230_move_donation_note_3' );
3049
	}
3050
}
3051