Completed
Push — issue/3613v2 ( 6db57d )
by Ravinder
1303:17 queued 1297:33
created

upgrade-functions.php ➔ give_v224_update_donor_meta_callback()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 30
rs 9.44
c 0
b 0
f 0
1
<?php
2
/**
3
 * Upgrade Functions
4
 *
5
 * @package     Give
6
 * @subpackage  Admin/Upgrades
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.0
10
 *
11
 * NOTICE: When adding new upgrade notices, please be sure to put the action into the upgrades array during install:
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.1.5 Add additional capability to the give_manager role.
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.2.4 Associate form IDs with donor meta of anonymous donations.
393
	$give_updates->register(
394
		array(
395
			'id'       => 'v224_update_donor_meta_forms_id',
396
			'version'  => '2.2.4',
397
			'callback' => 'give_v224_update_donor_meta_forms_id_callback',
398
			'depend'   => 'v224_update_donor_meta',
399
		)
400
	);
401
}
402
403
add_action( 'give_register_updates', 'give_show_upgrade_notices' );
404
405
/**
406
 * Triggers all upgrade functions
407
 *
408
 * This function is usually triggered via AJAX
409
 *
410
 * @since 1.0
411
 * @return void
412
 */
413
function give_trigger_upgrades() {
414
415 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...
416
		wp_die( esc_html__( 'You do not have permission to do Give upgrades.', 'give' ), esc_html__( 'Error', 'give' ), array(
417
			'response' => 403,
418
		) );
419
	}
420
421
	$give_version = get_option( 'give_version' );
422
423
	if ( ! $give_version ) {
424
		// 1.0 is the first version to use this option so we must add it.
425
		$give_version = '1.0';
426
		add_option( 'give_version', $give_version, '', false );
427
	}
428
429
	update_option( 'give_version', GIVE_VERSION, false );
430
	delete_option( 'give_doing_upgrade' );
431
432
	if ( DOING_AJAX ) {
433
		die( 'complete' );
434
	} // End if().
435
}
436
437
add_action( 'wp_ajax_give_trigger_upgrades', 'give_trigger_upgrades' );
438
439
440
/**
441
 * Upgrades the
442
 *
443
 * Standardizes the discrepancies between two metakeys `_give_payment_customer_id` and `_give_payment_donor_id`
444
 *
445
 * @since      1.3.2
446
 */
447
function give_v132_upgrade_give_payment_customer_id() {
448
	global $wpdb;
449
450
	/* @var Give_Updates $give_updates */
451
	$give_updates = Give_Updates::get_instance();
452
453
	// UPDATE DB METAKEYS.
454
	$sql   = "UPDATE $wpdb->postmeta SET meta_key = '_give_payment_customer_id' WHERE meta_key = '_give_payment_donor_id'";
455
	$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...
456
457
	$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...
458
	give_set_upgrade_complete( 'upgrade_give_payment_customer_id' );
459
}
460
461
462
/**
463
 * Upgrades the Offline Status
464
 *
465
 * Reverses the issue where offline donations in "pending" status where inappropriately marked as abandoned
466
 *
467
 * @since      1.3.4
468
 */
469
function give_v134_upgrade_give_offline_status() {
470
	global $wpdb;
471
472
	/* @var Give_Updates $give_updates */
473
	$give_updates = Give_Updates::get_instance();
474
475
	// Get abandoned offline payments.
476
	$select = "SELECT ID FROM $wpdb->posts p ";
477
	$join   = "LEFT JOIN $wpdb->postmeta m ON p.ID = m.post_id ";
478
	$where  = "WHERE p.post_type = 'give_payment' ";
479
	$where  .= "AND ( p.post_status = 'abandoned' )";
480
	$where  .= "AND ( m.meta_key = '_give_payment_gateway' AND m.meta_value = 'offline' )";
481
482
	$sql            = $select . $join . $where;
483
	$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...
484
485
	foreach ( $found_payments as $payment ) {
486
487
		// Only change ones marked abandoned since our release last week because the admin may have marked some abandoned themselves.
488
		$modified_time = get_post_modified_time( 'U', false, $payment );
489
490
		// 1450124863 =  12/10/2015 20:42:25.
491
		if ( $modified_time >= 1450124863 ) {
492
493
			give_update_payment_status( $payment, 'pending' );
494
495
		}
496
	}
497
498
	$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...
499
	give_set_upgrade_complete( 'upgrade_give_offline_status' );
500
}
501
502
503
/**
504
 * Cleanup User Roles
505
 *
506
 * This upgrade routine removes unused roles and roles with typos
507
 *
508
 * @since      1.5.2
509
 */
510
function give_v152_cleanup_users() {
511
512
	$give_version = get_option( 'give_version' );
513
514
	if ( ! $give_version ) {
515
		// 1.0 is the first version to use this option so we must add it.
516
		$give_version = '1.0';
517
	}
518
519
	$give_version = preg_replace( '/[^0-9.].*/', '', $give_version );
520
521
	// v1.5.2 Upgrades
522
	if ( version_compare( $give_version, '1.5.2', '<' ) || ! give_has_upgrade_completed( 'upgrade_give_user_caps_cleanup' ) ) {
523
524
		// Delete all caps with "ss".
525
		// Also delete all unused "campaign" roles.
526
		$delete_caps = array(
527
			'delete_give_formss',
528
			'delete_others_give_formss',
529
			'delete_private_give_formss',
530
			'delete_published_give_formss',
531
			'read_private_forms',
532
			'edit_give_formss',
533
			'edit_others_give_formss',
534
			'edit_private_give_formss',
535
			'edit_published_give_formss',
536
			'publish_give_formss',
537
			'read_private_give_formss',
538
			'assign_give_campaigns_terms',
539
			'delete_give_campaigns',
540
			'delete_give_campaigns_terms',
541
			'delete_give_campaignss',
542
			'delete_others_give_campaignss',
543
			'delete_private_give_campaignss',
544
			'delete_published_give_campaignss',
545
			'edit_give_campaigns',
546
			'edit_give_campaigns_terms',
547
			'edit_give_campaignss',
548
			'edit_others_give_campaignss',
549
			'edit_private_give_campaignss',
550
			'edit_published_give_campaignss',
551
			'manage_give_campaigns_terms',
552
			'publish_give_campaignss',
553
			'read_give_campaigns',
554
			'read_private_give_campaignss',
555
			'view_give_campaigns_stats',
556
			'delete_give_paymentss',
557
			'delete_others_give_paymentss',
558
			'delete_private_give_paymentss',
559
			'delete_published_give_paymentss',
560
			'edit_give_paymentss',
561
			'edit_others_give_paymentss',
562
			'edit_private_give_paymentss',
563
			'edit_published_give_paymentss',
564
			'publish_give_paymentss',
565
			'read_private_give_paymentss',
566
		);
567
568
		global $wp_roles;
569
		foreach ( $delete_caps as $cap ) {
570
			foreach ( array_keys( $wp_roles->roles ) as $role ) {
571
				$wp_roles->remove_cap( $role, $cap );
572
			}
573
		}
574
575
		// Create Give plugin roles.
576
		$roles = new Give_Roles();
577
		$roles->add_roles();
578
		$roles->add_caps();
579
580
		// The Update Ran.
581
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ), false );
582
		give_set_upgrade_complete( 'upgrade_give_user_caps_cleanup' );
583
		delete_option( 'give_doing_upgrade' );
584
585
	}// End if().
586
587
}
588
589
add_action( 'admin_init', 'give_v152_cleanup_users' );
590
591
/**
592
 * 1.6 Upgrade routine to create the customer meta table.
593
 *
594
 * @since  1.6
595
 * @return void
596
 */
597
function give_v16_upgrades() {
598
	// Create the donor databases.
599
	$donors_db = new Give_DB_Donors();
600
	$donors_db->create_table();
601
	$donor_meta = new Give_DB_Donor_Meta();
602
	$donor_meta->create_table();
603
}
604
605
/**
606
 * 1.7 Upgrades.
607
 *
608
 * a. Update license api data for plugin addons.
609
 * b. Cleanup user roles.
610
 *
611
 * @since  1.7
612
 * @return void
613
 */
614
function give_v17_upgrades() {
615
	// Upgrade license data.
616
	give_v17_upgrade_addon_license_data();
617
	give_v17_cleanup_roles();
618
}
619
620
/**
621
 * Upgrade license data
622
 *
623
 * @since 1.7
624
 */
625
function give_v17_upgrade_addon_license_data() {
626
	$give_options = give_get_settings();
627
628
	$api_url = 'https://givewp.com/give-sl-api/';
629
630
	// Get addons license key.
631
	$addons = array();
632
	foreach ( $give_options as $key => $value ) {
633
		if ( false !== strpos( $key, '_license_key' ) ) {
634
			$addons[ $key ] = $value;
635
		}
636
	}
637
638
	// Bailout: We do not have any addon license data to upgrade.
639
	if ( empty( $addons ) ) {
640
		return false;
641
	}
642
643
	foreach ( $addons as $key => $addon_license ) {
644
645
		// Get addon shortname.
646
		$shortname = str_replace( '_license_key', '', $key );
647
648
		// Addon license option name.
649
		$addon_license_option_name = $shortname . '_license_active';
650
651
		// bailout if license is empty.
652
		if ( empty( $addon_license ) ) {
653
			delete_option( $addon_license_option_name );
654
			continue;
655
		}
656
657
		// Get addon name.
658
		$addon_name       = array();
659
		$addon_name_parts = explode( '_', str_replace( 'give_', '', $shortname ) );
660
		foreach ( $addon_name_parts as $name_part ) {
661
662
			// Fix addon name
663
			switch ( $name_part ) {
664
				case 'authorizenet' :
665
					$name_part = 'authorize.net';
666
					break;
667
			}
668
669
			$addon_name[] = ucfirst( $name_part );
670
		}
671
672
		$addon_name = implode( ' ', $addon_name );
673
674
		// Data to send to the API.
675
		$api_params = array(
676
			'edd_action' => 'activate_license', // never change from "edd_" to "give_"!
677
			'license'    => $addon_license,
678
			'item_name'  => urlencode( $addon_name ),
679
			'url'        => home_url(),
680
		);
681
682
		// Call the API.
683
		$response = wp_remote_post(
684
			$api_url,
685
			array(
686
				'timeout'   => 15,
687
				'sslverify' => false,
688
				'body'      => $api_params,
689
			)
690
		);
691
692
		// Make sure there are no errors.
693
		if ( is_wp_error( $response ) ) {
694
			delete_option( $addon_license_option_name );
695
			continue;
696
		}
697
698
		// Tell WordPress to look for updates.
699
		set_site_transient( 'update_plugins', null );
700
701
		// Decode license data.
702
		$license_data = json_decode( wp_remote_retrieve_body( $response ) );
703
		update_option( $addon_license_option_name, $license_data, false );
704
	}// End foreach().
705
}
706
707
708
/**
709
 * Cleanup User Roles.
710
 *
711
 * This upgrade routine removes unused roles and roles with typos.
712
 *
713
 * @since      1.7
714
 */
715
function give_v17_cleanup_roles() {
716
717
	// Delete all caps with "_give_forms_" and "_give_payments_".
718
	// These roles have no usage; the proper is singular.
719
	$delete_caps = array(
720
		'view_give_forms_stats',
721
		'delete_give_forms_terms',
722
		'assign_give_forms_terms',
723
		'edit_give_forms_terms',
724
		'manage_give_forms_terms',
725
		'view_give_payments_stats',
726
		'manage_give_payments_terms',
727
		'edit_give_payments_terms',
728
		'assign_give_payments_terms',
729
		'delete_give_payments_terms',
730
	);
731
732
	global $wp_roles;
733
	foreach ( $delete_caps as $cap ) {
734
		foreach ( array_keys( $wp_roles->roles ) as $role ) {
735
			$wp_roles->remove_cap( $role, $cap );
736
		}
737
	}
738
739
	// Set roles again.
740
	$roles = new Give_Roles();
741
	$roles->add_roles();
742
	$roles->add_caps();
743
744
}
745
746
/**
747
 * 1.8 Upgrades.
748
 *
749
 * a. Upgrade checkbox settings to radio button settings.
750
 * a. Update form meta for new metabox settings.
751
 *
752
 * @since  1.8
753
 * @return void
754
 */
755
function give_v18_upgrades() {
756
	// Upgrade checkbox settings to radio button settings.
757
	give_v18_upgrades_core_setting();
758
}
759
760
/**
761
 * Upgrade core settings.
762
 *
763
 * @since  1.8
764
 * @return void
765
 */
766
function give_v18_upgrades_core_setting() {
767
	// Core settings which changes from checkbox to radio.
768
	$core_setting_names = array_merge(
769
		array_keys( give_v18_renamed_core_settings() ),
770
		array(
771
			'uninstall_on_delete',
772
			'scripts_footer',
773
			'test_mode',
774
			'email_access',
775
			'terms',
776
			'give_offline_donation_enable_billing_fields',
777
		)
778
	);
779
780
	// Bailout: If not any setting define.
781
	if ( $give_settings = get_option( 'give_settings' ) ) {
782
783
		$setting_changed = false;
784
785
		// Loop: check each setting field.
786
		foreach ( $core_setting_names as $setting_name ) {
787
			// New setting name.
788
			$new_setting_name = preg_replace( '/^(enable_|disable_)/', '', $setting_name );
789
790
			// Continue: If setting already set.
791
			if (
792
				array_key_exists( $new_setting_name, $give_settings )
793
				&& in_array( $give_settings[ $new_setting_name ], array( 'enabled', 'disabled' ) )
794
			) {
795
				continue;
796
			}
797
798
			// Set checkbox value to radio value.
799
			$give_settings[ $setting_name ] = ( ! empty( $give_settings[ $setting_name ] ) && 'on' === $give_settings[ $setting_name ] ? 'enabled' : 'disabled' );
800
801
			// @see https://github.com/WordImpress/Give/issues/1063.
802
			if ( false !== strpos( $setting_name, 'disable_' ) ) {
803
804
				$give_settings[ $new_setting_name ] = ( give_is_setting_enabled( $give_settings[ $setting_name ] ) ? 'disabled' : 'enabled' );
805
			} elseif ( false !== strpos( $setting_name, 'enable_' ) ) {
806
807
				$give_settings[ $new_setting_name ] = ( give_is_setting_enabled( $give_settings[ $setting_name ] ) ? 'enabled' : 'disabled' );
808
			}
809
810
			// Tell bot to update core setting to db.
811
			if ( ! $setting_changed ) {
812
				$setting_changed = true;
813
			}
814
		}
815
816
		// Update setting only if they changed.
817
		if ( $setting_changed ) {
818
			update_option( 'give_settings', $give_settings, false );
819
		}
820
	}// End if().
821
822
	give_set_upgrade_complete( 'v18_upgrades_core_setting' );
823
}
824
825
/**
826
 * Upgrade form metadata for new metabox settings.
827
 *
828
 * @since  1.8
829
 * @return void
830
 */
831
function give_v18_upgrades_form_metadata() {
832
	/* @var Give_Updates $give_updates */
833
	$give_updates = Give_Updates::get_instance();
834
835
	// form query
836
	$forms = new WP_Query( array(
837
			'paged'          => $give_updates->step,
838
			'status'         => 'any',
839
			'order'          => 'ASC',
840
			'post_type'      => 'give_forms',
841
			'posts_per_page' => 20,
842
		)
843
	);
844
845
	if ( $forms->have_posts() ) {
846
		$give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 20 ) );
847
848
		while ( $forms->have_posts() ) {
849
			$forms->the_post();
850
851
			// Form content.
852
			// Note in version 1.8 display content setting split into display content and content placement setting.
853
			// You can delete _give_content_option in future.
854
			$show_content = give_get_meta( get_the_ID(), '_give_content_option', true );
855
			if ( $show_content && ! give_get_meta( get_the_ID(), '_give_display_content', true ) ) {
856
				$field_value = ( 'none' !== $show_content ? 'enabled' : 'disabled' );
857
				give_update_meta( get_the_ID(), '_give_display_content', $field_value );
858
859
				$field_value = ( 'none' !== $show_content ? $show_content : 'give_pre_form' );
860
				give_update_meta( get_the_ID(), '_give_content_placement', $field_value );
861
			}
862
863
			// "Disable" Guest Donation. Checkbox.
864
			// See: https://github.com/WordImpress/Give/issues/1470.
865
			$guest_donation        = give_get_meta( get_the_ID(), '_give_logged_in_only', true );
866
			$guest_donation_newval = ( in_array( $guest_donation, array( 'yes', 'on' ) ) ? 'disabled' : 'enabled' );
867
			give_update_meta( get_the_ID(), '_give_logged_in_only', $guest_donation_newval );
868
869
			// Offline Donations.
870
			// See: https://github.com/WordImpress/Give/issues/1579.
871
			$offline_donation = give_get_meta( get_the_ID(), '_give_customize_offline_donations', true );
872
			if ( 'no' === $offline_donation ) {
873
				$offline_donation_newval = 'global';
874
			} elseif ( 'yes' === $offline_donation ) {
875
				$offline_donation_newval = 'enabled';
876
			} else {
877
				$offline_donation_newval = 'disabled';
878
			}
879
			give_update_meta( get_the_ID(), '_give_customize_offline_donations', $offline_donation_newval );
880
881
			// Convert yes/no setting field to enabled/disabled.
882
			$form_radio_settings = array(
883
				// Custom Amount.
884
				'_give_custom_amount',
885
886
				// Donation Gaol.
887
				'_give_goal_option',
888
889
				// Close Form.
890
				'_give_close_form_when_goal_achieved',
891
892
				// Term & conditions.
893
				'_give_terms_option',
894
895
				// Billing fields.
896
				'_give_offline_donation_enable_billing_fields_single',
897
			);
898
899
			foreach ( $form_radio_settings as $meta_key ) {
900
				// Get value.
901
				$field_value = give_get_meta( get_the_ID(), $meta_key, true );
902
903
				// Convert meta value only if it is in yes/no/none.
904
				if ( in_array( $field_value, array( 'yes', 'on', 'no', 'none' ) ) ) {
905
906
					$field_value = ( in_array( $field_value, array( 'yes', 'on' ) ) ? 'enabled' : 'disabled' );
907
					give_update_meta( get_the_ID(), $meta_key, $field_value );
908
				}
909
			}
910
		}// End while().
911
912
		wp_reset_postdata();
913
914
	} else {
915
		// No more forms found, finish up.
916
		give_set_upgrade_complete( 'v18_upgrades_form_metadata' );
917
	}
918
}
919
920
921
/**
922
 * Get list of core setting renamed in version 1.8.
923
 *
924
 * @since  1.8
925
 * @return array
926
 */
927
function give_v18_renamed_core_settings() {
928
	return array(
929
		'disable_paypal_verification' => 'paypal_verification',
930
		'disable_css'                 => 'css',
931
		'disable_welcome'             => 'welcome',
932
		'disable_forms_singular'      => 'forms_singular',
933
		'disable_forms_archives'      => 'forms_archives',
934
		'disable_forms_excerpt'       => 'forms_excerpt',
935
		'disable_form_featured_img'   => 'form_featured_img',
936
		'disable_form_sidebar'        => 'form_sidebar',
937
		'disable_admin_notices'       => 'admin_notices',
938
		'disable_the_content_filter'  => 'the_content_filter',
939
		'enable_floatlabels'          => 'floatlabels',
940
		'enable_categories'           => 'categories',
941
		'enable_tags'                 => 'tags',
942
	);
943
}
944
945
946
/**
947
 * Upgrade core settings.
948
 *
949
 * @since  1.8.7
950
 * @return void
951
 */
952
function give_v187_upgrades() {
953
	global $wpdb;
954
955
	/**
956
	 * Upgrade 1: Remove stat and cache transients.
957
	 */
958
	$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...
959
		$wpdb->prepare(
960
			"
961
					SELECT *
962
					FROM {$wpdb->options}
963
					WHERE (
964
					option_name LIKE %s
965
					OR option_name LIKE %s
966
					OR option_name LIKE %s
967
					OR option_name LIKE %s
968
					OR option_name LIKE %s
969
					OR option_name LIKE %s
970
					OR option_name LIKE %s
971
					OR option_name LIKE %s
972
					OR 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
					)
978
					",
979
			array(
980
				'%_transient_give_stats_%',
981
				'give_cache%',
982
				'%_transient_give_add_ons_feed%',
983
				'%_transient__give_ajax_works' .
984
				'%_transient_give_total_api_keys%',
985
				'%_transient_give_i18n_give_promo_hide%',
986
				'%_transient_give_contributors%',
987
				'%_transient_give_estimated_monthly_stats%',
988
				'%_transient_give_earnings_total%',
989
				'%_transient_give_i18n_give_%',
990
				'%_transient__give_installed%',
991
				'%_transient__give_activation_redirect%',
992
				'%_transient__give_hide_license_notices_shortly_%',
993
				'%give_income_total%',
994
			)
995
		),
996
		1
997
	);
998
999
	// User related transients.
1000
	$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...
1001
		$wpdb->prepare(
1002
			"SELECT user_id, meta_key
1003
			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...
1004
			WHERE meta_value=%s",
1005
			'give_user_public_key'
1006
		),
1007
		ARRAY_A
1008
	);
1009
1010
	if ( ! empty( $user_apikey_options ) ) {
1011
		foreach ( $user_apikey_options as $user ) {
1012
			$cached_options[] = '_transient_' . md5( 'give_api_user_' . $user['meta_key'] );
1013
			$cached_options[] = '_transient_' . md5( 'give_api_user_public_key' . $user['user_id'] );
1014
			$cached_options[] = '_transient_' . md5( 'give_api_user_secret_key' . $user['user_id'] );
1015
		}
1016
	}
1017
1018
	if ( ! empty( $cached_options ) ) {
1019
		foreach ( $cached_options as $option ) {
1020 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...
1021
				case ( false !== strpos( $option, 'transient' ) ):
1022
					$option = str_replace( '_transient_', '', $option );
1023
					delete_transient( $option );
1024
					break;
1025
1026
				default:
1027
					delete_option( $option );
1028
			}
1029
		}
1030
	}
1031
}
1032
1033
/**
1034
 * Update Capabilities for Give_Worker User Role.
1035
 *
1036
 * This upgrade routine will update access rights for Give_Worker User Role.
1037
 *
1038
 * @since      1.8.8
1039
 */
1040
function give_v188_upgrades() {
1041
1042
	global $wp_roles;
1043
1044
	// Get the role object.
1045
	$give_worker = get_role( 'give_worker' );
1046
1047
	// A list of capabilities to add for give workers.
1048
	$caps_to_add = array(
1049
		'edit_posts',
1050
		'edit_pages',
1051
	);
1052
1053
	foreach ( $caps_to_add as $cap ) {
1054
		// Add the capability.
1055
		$give_worker->add_cap( $cap );
1056
	}
1057
1058
}
1059
1060
/**
1061
 * Update Post meta for minimum and maximum amount for multi level donation forms
1062
 *
1063
 * This upgrade routine adds post meta for give_forms CPT for multi level donation form.
1064
 *
1065
 * @since      1.8.9
1066
 */
1067
function give_v189_upgrades_levels_post_meta_callback() {
1068
	/* @var Give_Updates $give_updates */
1069
	$give_updates = Give_Updates::get_instance();
1070
1071
	// form query.
1072
	$donation_forms = new WP_Query( array(
1073
			'paged'          => $give_updates->step,
1074
			'status'         => 'any',
1075
			'order'          => 'ASC',
1076
			'post_type'      => 'give_forms',
1077
			'posts_per_page' => 20,
1078
		)
1079
	);
1080
1081
	if ( $donation_forms->have_posts() ) {
1082
		$give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
1083
1084
		while ( $donation_forms->have_posts() ) {
1085
			$donation_forms->the_post();
1086
			$form_id = get_the_ID();
1087
1088
			// Remove formatting from _give_set_price.
1089
			update_post_meta(
1090
				$form_id,
1091
				'_give_set_price',
1092
				give_sanitize_amount( get_post_meta( $form_id, '_give_set_price', true ) )
1093
			);
1094
1095
			// Remove formatting from _give_custom_amount_minimum.
1096
			update_post_meta(
1097
				$form_id,
1098
				'_give_custom_amount_minimum',
1099
				give_sanitize_amount( get_post_meta( $form_id, '_give_custom_amount_minimum', true ) )
1100
			);
1101
1102
			// Bailout.
1103
			if ( 'set' === get_post_meta( $form_id, '_give_price_option', true ) ) {
1104
				continue;
1105
			}
1106
1107
			$donation_levels = get_post_meta( $form_id, '_give_donation_levels', true );
1108
1109
			if ( ! empty( $donation_levels ) ) {
1110
1111
				foreach ( $donation_levels as $index => $donation_level ) {
1112
					if ( isset( $donation_level['_give_amount'] ) ) {
1113
						$donation_levels[ $index ]['_give_amount'] = give_sanitize_amount( $donation_level['_give_amount'] );
1114
					}
1115
				}
1116
1117
				update_post_meta( $form_id, '_give_donation_levels', $donation_levels );
1118
1119
				$donation_levels_amounts = wp_list_pluck( $donation_levels, '_give_amount' );
1120
1121
				$min_amount = min( $donation_levels_amounts );
1122
				$max_amount = max( $donation_levels_amounts );
1123
1124
				// Set Minimum and Maximum amount for Multi Level Donation Forms
1125
				give_update_meta( $form_id, '_give_levels_minimum_amount', $min_amount ? give_sanitize_amount( $min_amount ) : 0 );
1126
				give_update_meta( $form_id, '_give_levels_maximum_amount', $max_amount ? give_sanitize_amount( $max_amount ) : 0 );
1127
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1128
1129
		}
1130
1131
		/* Restore original Post Data */
1132
		wp_reset_postdata();
1133
	} else {
1134
		// The Update Ran.
1135
		give_set_upgrade_complete( 'v189_upgrades_levels_post_meta' );
1136
	}
1137
1138
}
1139
1140
1141
/**
1142
 * Give version 1.8.9 upgrades
1143
 *
1144
 * @since      1.8.9
1145
 */
1146
function give_v189_upgrades() {
1147
	/**
1148
	 * 1. Remove user license related notice show blocked ( Give_Notice will handle )
1149
	 */
1150
	global $wpdb;
1151
1152
	// Delete permanent notice blocker.
1153
	$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...
1154
		$wpdb->prepare(
1155
			"
1156
					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...
1157
					WHERE meta_key
1158
					LIKE '%%%s%%'
1159
					",
1160
			'_give_hide_license_notices_permanently'
1161
		)
1162
	);
1163
1164
	// Delete short notice blocker.
1165
	$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...
1166
		$wpdb->prepare(
1167
			"
1168
					DELETE FROM $wpdb->options
1169
					WHERE option_name
1170
					LIKE '%%%s%%'
1171
					",
1172
			'__give_hide_license_notices_shortly_'
1173
		)
1174
	);
1175
}
1176
1177
/**
1178
 * 2.0 Upgrades.
1179
 *
1180
 * @since  2.0
1181
 * @return void
1182
 */
1183
function give_v20_upgrades() {
1184
	// Update cache setting.
1185
	give_update_option( 'cache', 'enabled' );
1186
1187
	// Upgrade email settings.
1188
	give_v20_upgrades_email_setting();
1189
}
1190
1191
/**
1192
 * Move old email api settings to new email setting api for following emails:
1193
 *    1. new offline donation         [This was hard coded]
1194
 *    2. offline donation instruction
1195
 *    3. new donation
1196
 *    4. donation receipt
1197
 *
1198
 * @since 2.0
1199
 */
1200
function give_v20_upgrades_email_setting() {
1201
	$all_setting = give_get_settings();
1202
1203
	// Bailout on fresh install.
1204
	if ( empty( $all_setting ) ) {
1205
		return;
1206
	}
1207
1208
	$settings = array(
1209
		'offline_donation_subject'      => 'offline-donation-instruction_email_subject',
1210
		'global_offline_donation_email' => 'offline-donation-instruction_email_message',
1211
		'donation_subject'              => 'donation-receipt_email_subject',
1212
		'donation_receipt'              => 'donation-receipt_email_message',
1213
		'donation_notification_subject' => 'new-donation_email_subject',
1214
		'donation_notification'         => 'new-donation_email_message',
1215
		'admin_notice_emails'           => array(
1216
			'new-donation_recipient',
1217
			'new-offline-donation_recipient',
1218
			'new-donor-register_recipient',
1219
		),
1220
		'admin_notices'                 => 'new-donation_notification',
1221
	);
1222
1223
	foreach ( $settings as $old_setting => $new_setting ) {
1224
		// Do not update already modified
1225
		if ( ! is_array( $new_setting ) ) {
1226
			if ( array_key_exists( $new_setting, $all_setting ) || ! array_key_exists( $old_setting, $all_setting ) ) {
1227
				continue;
1228
			}
1229
		}
1230
1231
		switch ( $old_setting ) {
1232
			case 'admin_notices':
1233
				$notification_status = give_get_option( $old_setting, 'enabled' );
1234
1235
				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 1223 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...
1236
1237
				// @todo: Delete this option later ( version > 2.0 ), We need this for per form email addon.
1238
				// give_delete_option( $old_setting );
1239
1240
				break;
1241
1242
			// @todo: Delete this option later ( version > 2.0 ) because we need this for backward compatibility give_get_admin_notice_emails.
1243
			case 'admin_notice_emails':
1244
				$recipients = give_get_admin_notice_emails();
1245
1246
				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...
1247
					// bailout if setting already exist.
1248
					if ( array_key_exists( $setting, $all_setting ) ) {
1249
						continue;
1250
					}
1251
1252
					give_update_option( $setting, $recipients );
1253
				}
1254
				break;
1255
1256
			default:
1257
				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 1223 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...
1258
				give_delete_option( $old_setting );
1259
		}
1260
	}
1261
}
1262
1263
/**
1264
 * Give version 1.8.9 upgrades
1265
 *
1266
 * @since 1.8.9
1267
 */
1268
function give_v1812_upgrades() {
1269
	/**
1270
	 * Validate number format settings.
1271
	 */
1272
	$give_settings        = give_get_settings();
1273
	$give_setting_updated = false;
1274
1275
	if ( $give_settings['thousands_separator'] === $give_settings['decimal_separator'] ) {
1276
		$give_settings['number_decimals']   = 0;
1277
		$give_settings['decimal_separator'] = '';
1278
		$give_setting_updated               = true;
1279
1280
	} elseif ( empty( $give_settings['decimal_separator'] ) ) {
1281
		$give_settings['number_decimals'] = 0;
1282
		$give_setting_updated             = true;
1283
1284
	} elseif ( 6 < absint( $give_settings['number_decimals'] ) ) {
1285
		$give_settings['number_decimals'] = 5;
1286
		$give_setting_updated             = true;
1287
	}
1288
1289
	if ( $give_setting_updated ) {
1290
		update_option( 'give_settings', $give_settings, false );
1291
	}
1292
}
1293
1294
1295
/**
1296
 * Give version 1.8.12 update
1297
 *
1298
 * Standardized amount values to six decimal
1299
 *
1300
 * @see        https://github.com/WordImpress/Give/issues/1849#issuecomment-315128602
1301
 *
1302
 * @since      1.8.12
1303
 */
1304
function give_v1812_update_amount_values_callback() {
1305
	/* @var Give_Updates $give_updates */
1306
	$give_updates = Give_Updates::get_instance();
1307
1308
	// form query.
1309
	$donation_forms = new WP_Query( array(
1310
			'paged'          => $give_updates->step,
1311
			'status'         => 'any',
1312
			'order'          => 'ASC',
1313
			'post_type'      => array( 'give_forms', 'give_payment' ),
1314
			'posts_per_page' => 20,
1315
		)
1316
	);
1317
	if ( $donation_forms->have_posts() ) {
1318
		$give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
1319
1320
		while ( $donation_forms->have_posts() ) {
1321
			$donation_forms->the_post();
1322
			global $post;
1323
1324
			$meta = get_post_meta( $post->ID );
1325
1326
			switch ( $post->post_type ) {
1327
				case 'give_forms':
1328
					// _give_set_price.
1329
					if ( ! empty( $meta['_give_set_price'][0] ) ) {
1330
						update_post_meta( $post->ID, '_give_set_price', give_sanitize_amount_for_db( $meta['_give_set_price'][0] ) );
1331
					}
1332
1333
					// _give_custom_amount_minimum.
1334
					if ( ! empty( $meta['_give_custom_amount_minimum'][0] ) ) {
1335
						update_post_meta( $post->ID, '_give_custom_amount_minimum', give_sanitize_amount_for_db( $meta['_give_custom_amount_minimum'][0] ) );
1336
					}
1337
1338
					// _give_levels_minimum_amount.
1339
					if ( ! empty( $meta['_give_levels_minimum_amount'][0] ) ) {
1340
						update_post_meta( $post->ID, '_give_levels_minimum_amount', give_sanitize_amount_for_db( $meta['_give_levels_minimum_amount'][0] ) );
1341
					}
1342
1343
					// _give_levels_maximum_amount.
1344
					if ( ! empty( $meta['_give_levels_maximum_amount'][0] ) ) {
1345
						update_post_meta( $post->ID, '_give_levels_maximum_amount', give_sanitize_amount_for_db( $meta['_give_levels_maximum_amount'][0] ) );
1346
					}
1347
1348
					// _give_set_goal.
1349
					if ( ! empty( $meta['_give_set_goal'][0] ) ) {
1350
						update_post_meta( $post->ID, '_give_set_goal', give_sanitize_amount_for_db( $meta['_give_set_goal'][0] ) );
1351
					}
1352
1353
					// _give_form_earnings.
1354
					if ( ! empty( $meta['_give_form_earnings'][0] ) ) {
1355
						update_post_meta( $post->ID, '_give_form_earnings', give_sanitize_amount_for_db( $meta['_give_form_earnings'][0] ) );
1356
					}
1357
1358
					// _give_custom_amount_minimum.
1359
					if ( ! empty( $meta['_give_donation_levels'][0] ) ) {
1360
						$donation_levels = unserialize( $meta['_give_donation_levels'][0] );
1361
1362
						foreach ( $donation_levels as $index => $level ) {
1363
							if ( empty( $level['_give_amount'] ) ) {
1364
								continue;
1365
							}
1366
1367
							$donation_levels[ $index ]['_give_amount'] = give_sanitize_amount_for_db( $level['_give_amount'] );
1368
						}
1369
1370
						$meta['_give_donation_levels'] = $donation_levels;
1371
						update_post_meta( $post->ID, '_give_donation_levels', $meta['_give_donation_levels'] );
1372
					}
1373
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
1374
1375
					break;
1376
1377
				case 'give_payment':
1378
					// _give_payment_total.
1379
					if ( ! empty( $meta['_give_payment_total'][0] ) ) {
1380
						update_post_meta( $post->ID, '_give_payment_total', give_sanitize_amount_for_db( $meta['_give_payment_total'][0] ) );
1381
					}
1382
1383
					break;
1384
			}
1385
		}
1386
1387
		/* Restore original Post Data */
1388
		wp_reset_postdata();
1389
	} else {
1390
		// The Update Ran.
1391
		give_set_upgrade_complete( 'v1812_update_amount_values' );
1392
	}
1393
}
1394
1395
1396
/**
1397
 * Give version 1.8.12 update
1398
 *
1399
 * Standardized amount values to six decimal for donor
1400
 *
1401
 * @see        https://github.com/WordImpress/Give/issues/1849#issuecomment-315128602
1402
 *
1403
 * @since      1.8.12
1404
 */
1405
function give_v1812_update_donor_purchase_value_callback() {
1406
	/* @var Give_Updates $give_updates */
1407
	$give_updates = Give_Updates::get_instance();
1408
1409
	// form query.
1410
	$donors = Give()->donors->get_donors( array(
1411
			'number' => 20,
1412
			'offset' => $give_updates->get_offset( 20 ),
1413
		)
1414
	);
1415
1416
	if ( ! empty( $donors ) ) {
1417
		$give_updates->set_percentage( Give()->donors->count(), $give_updates->get_offset( 20 ) );
1418
1419
		/* @var Object $donor */
1420
		foreach ( $donors as $donor ) {
1421
			Give()->donors->update( $donor->id, array( 'purchase_value' => give_sanitize_amount_for_db( $donor->purchase_value ) ) );
1422
		}
1423
	} else {
1424
		// The Update Ran.
1425
		give_set_upgrade_complete( 'v1812_update_donor_purchase_values' );
1426
	}
1427
}
1428
1429
/**
1430
 * Upgrade routine for updating user roles for existing donors.
1431
 *
1432
 * @since 1.8.13
1433
 */
1434
function give_v1813_update_donor_user_roles_callback() {
1435
	/* @var Give_Updates $give_updates */
1436
	$give_updates = Give_Updates::get_instance();
1437
1438
	// Fetch all the existing donors.
1439
	$donors = Give()->donors->get_donors( array(
1440
			'number' => 20,
1441
			'offset' => $give_updates->get_offset( 20 ),
1442
		)
1443
	);
1444
1445
	if ( ! empty( $donors ) ) {
1446
		$give_updates->set_percentage( Give()->donors->count(), $give_updates->get_offset( 20 ) );
1447
1448
		/* @var Object $donor */
1449
		foreach ( $donors as $donor ) {
1450
			$user_id = $donor->user_id;
1451
1452
			// Proceed, if donor is attached with user.
1453
			if ( $user_id ) {
1454
				$user = get_userdata( $user_id );
1455
1456
				// Update user role, if user has subscriber role.
1457
				if ( is_array( $user->roles ) && in_array( 'subscriber', $user->roles ) ) {
1458
					wp_update_user(
1459
						array(
1460
							'ID'   => $user_id,
1461
							'role' => 'give_donor',
1462
						)
1463
					);
1464
				}
1465
			}
1466
		}
1467
	} else {
1468
		// The Update Ran.
1469
		give_set_upgrade_complete( 'v1813_update_donor_user_roles' );
1470
	}
1471
}
1472
1473
1474
/**
1475
 * Version 1.8.13 automatic updates
1476
 *
1477
 * @since 1.8.13
1478
 */
1479
function give_v1813_upgrades() {
1480
	// Update admin setting.
1481
	give_update_option( 'donor_default_user_role', 'give_donor' );
1482
1483
	// Update Give roles.
1484
	$roles = new Give_Roles();
1485
	$roles->add_roles();
1486
	$roles->add_caps();
1487
}
1488
1489
/**
1490
 * Correct currency code for "Iranian Currency" for all of the payments.
1491
 *
1492
 * @since 1.8.17
1493
 */
1494
function give_v1817_update_donation_iranian_currency_code() {
1495
	/* @var Give_Updates $give_updates */
1496
	$give_updates = Give_Updates::get_instance();
1497
1498
	// form query.
1499
	$payments = new WP_Query( array(
1500
			'paged'          => $give_updates->step,
1501
			'status'         => 'any',
1502
			'order'          => 'ASC',
1503
			'post_type'      => array( 'give_payment' ),
1504
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1505
		)
1506
	);
1507
1508
	if ( $payments->have_posts() ) {
1509
		$give_updates->set_percentage( $payments->found_posts, ( $give_updates->step * 100 ) );
1510
1511
		while ( $payments->have_posts() ) {
1512
			$payments->the_post();
1513
1514
			$payment_meta = give_get_payment_meta( get_the_ID() );
1515
1516
			if ( 'RIAL' === $payment_meta['currency'] ) {
1517
				$payment_meta['currency'] = 'IRR';
1518
				give_update_meta( get_the_ID(), '_give_payment_meta', $payment_meta );
1519
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1520
1521
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1522
1523
	} else {
1524
		// The Update Ran.
1525
		give_set_upgrade_complete( 'v1817_update_donation_iranian_currency_code' );
1526
	}
1527
}
1528
1529
/**
1530
 * Correct currency code for "Iranian Currency" in Give setting.
1531
 * Version 1.8.17 automatic updates
1532
 *
1533
 * @since 1.8.17
1534
 */
1535
function give_v1817_upgrades() {
1536
	$give_settings = give_get_settings();
1537
1538
	if ( 'RIAL' === $give_settings['currency'] ) {
1539
		$give_settings['currency'] = 'IRR';
1540
		update_option( 'give_settings', $give_settings, false );
1541
	}
1542
}
1543
1544
/**
1545
 * Process Clean up of User Roles for more flexibility.
1546
 *
1547
 * @since 1.8.17
1548
 */
1549
function give_v1817_process_cleanup_user_roles() {
1550
1551
	global $wp_roles;
1552
1553
	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...
1554
		return;
1555
	}
1556
1557
	// Add Capabilities to user roles as required.
1558
	$add_caps = array(
1559
		'administrator' => array(
1560
			'view_give_payments',
1561
		),
1562
	);
1563
1564
	// Remove Capabilities to user roles as required.
1565
	$remove_caps = array(
1566
		'give_manager' => array(
1567
			'edit_others_pages',
1568
			'edit_others_posts',
1569
			'delete_others_pages',
1570
			'delete_others_posts',
1571
			'manage_categories',
1572
			'import',
1573
			'export',
1574
		),
1575
	);
1576
1577
	foreach ( $add_caps as $role => $caps ) {
1578
		foreach ( $caps as $cap ) {
1579
			$wp_roles->add_cap( $role, $cap );
1580
		}
1581
	}
1582
1583
	foreach ( $remove_caps as $role => $caps ) {
1584
		foreach ( $caps as $cap ) {
1585
			$wp_roles->remove_cap( $role, $cap );
1586
		}
1587
	}
1588
1589
}
1590
1591
/**
1592
 * Upgrade Routine - Clean up of User Roles for more flexibility.
1593
 *
1594
 * @since 1.8.17
1595
 */
1596
function give_v1817_cleanup_user_roles() {
1597
	/* @var Give_Updates $give_updates */
1598
	$give_updates = Give_Updates::get_instance();
1599
1600
	give_v1817_process_cleanup_user_roles();
1601
1602
	$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...
1603
1604
	// Create Give plugin roles.
1605
	$roles = new Give_Roles();
1606
	$roles->add_roles();
1607
	$roles->add_caps();
1608
1609
	give_set_upgrade_complete( 'v1817_cleanup_user_roles' );
1610
}
1611
1612
/**
1613
 * Automatic Upgrade for release 1.8.18.
1614
 *
1615
 * @since 1.8.18
1616
 */
1617
function give_v1818_upgrades() {
1618
1619
	// Remove email_access_installed from give_settings.
1620
	give_delete_option( 'email_access_installed' );
1621
}
1622
1623
/**
1624
 * Upgrade Routine - Assigns Custom Amount to existing donation of type set donation.
1625
 *
1626
 * @since 1.8.18
1627
 */
1628
function give_v1818_assign_custom_amount_set_donation() {
1629
1630
	/* @var Give_Updates $give_updates */
1631
	$give_updates = Give_Updates::get_instance();
1632
1633
	$donations = new WP_Query( array(
1634
			'paged'          => $give_updates->step,
1635
			'status'         => 'any',
1636
			'order'          => 'ASC',
1637
			'post_type'      => array( 'give_payment' ),
1638
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1639
		)
1640
	);
1641
1642
	if ( $donations->have_posts() ) {
1643
		$give_updates->set_percentage( $donations->found_posts, $give_updates->step * 100 );
1644
1645
		while ( $donations->have_posts() ) {
1646
			$donations->the_post();
1647
1648
			$form          = new Give_Donate_Form( give_get_meta( get_the_ID(), '_give_payment_form_id', true ) );
1649
			$donation_meta = give_get_payment_meta( get_the_ID() );
1650
1651
			// Update Donation meta with price_id set as custom, only if it is:
1652
			// 1. Donation Type = Set Donation.
1653
			// 2. Donation Price Id is not set to custom.
1654
			// 3. Form has not enabled custom price and donation amount assures that it is custom amount.
1655
			if (
1656
				$form->ID &&
1657
				$form->is_set_type_donation_form() &&
1658
				( 'custom' !== $donation_meta['price_id'] ) &&
1659
				$form->is_custom_price( give_get_meta( get_the_ID(), '_give_payment_total', true ) )
1660
			) {
1661
				$donation_meta['price_id'] = 'custom';
1662
				give_update_meta( get_the_ID(), '_give_payment_meta', $donation_meta );
1663
				give_update_meta( get_the_ID(), '_give_payment_price_id', 'custom' );
1664
			}
1665
		}
1666
1667
		wp_reset_postdata();
1668
	} else {
1669
		// Update Ran Successfully.
1670
		give_set_upgrade_complete( 'v1818_assign_custom_amount_set_donation' );
1671
	}
1672
}
1673
1674
/**
1675
 * Upgrade Routine - Removed Give Worker caps.
1676
 *
1677
 * See: https://github.com/WordImpress/Give/issues/2476
1678
 *
1679
 * @since 1.8.18
1680
 */
1681
function give_v1818_give_worker_role_cleanup() {
1682
1683
	/* @var Give_Updates $give_updates */
1684
	$give_updates = Give_Updates::get_instance();
1685
1686
	global $wp_roles;
1687
1688
	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...
1689
		return;
1690
	}
1691
1692
	// Remove Capabilities to user roles as required.
1693
	$remove_caps = array(
1694
		'give_worker' => array(
1695
			'delete_give_payments',
1696
			'delete_others_give_payments',
1697
			'delete_private_give_payments',
1698
			'delete_published_give_payments',
1699
			'edit_others_give_payments',
1700
			'edit_private_give_payments',
1701
			'edit_published_give_payments',
1702
			'read_private_give_payments',
1703
		),
1704
	);
1705
1706
	foreach ( $remove_caps as $role => $caps ) {
1707
		foreach ( $caps as $cap ) {
1708
			$wp_roles->remove_cap( $role, $cap );
1709
		}
1710
	}
1711
1712
	$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...
1713
1714
	// Create Give plugin roles.
1715
	$roles = new Give_Roles();
1716
	$roles->add_roles();
1717
	$roles->add_caps();
1718
1719
	give_set_upgrade_complete( 'v1818_give_worker_role_cleanup' );
1720
}
1721
1722
/**
1723
 *
1724
 * Upgrade form metadata for new metabox settings.
1725
 *
1726
 * @since  2.0
1727
 * @return void
1728
 */
1729
function give_v20_upgrades_form_metadata_callback() {
1730
	$give_updates = Give_Updates::get_instance();
1731
1732
	// form query
1733
	$forms = new WP_Query( array(
1734
			'paged'          => $give_updates->step,
1735
			'status'         => 'any',
1736
			'order'          => 'ASC',
1737
			'post_type'      => 'give_forms',
1738
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1739
		)
1740
	);
1741
1742
	if ( $forms->have_posts() ) {
1743
		$give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 100 ) );
1744
1745
		while ( $forms->have_posts() ) {
1746
			$forms->the_post();
1747
			global $post;
1748
1749
			// Update offline instruction email notification status.
1750
			$offline_instruction_notification_status = get_post_meta( get_the_ID(), '_give_customize_offline_donations', true );
1751
			$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...
1752
				'enabled',
1753
				'global',
1754
			) )
1755
				? $offline_instruction_notification_status
1756
				: 'global';
1757
			update_post_meta( get_the_ID(), '_give_offline-donation-instruction_notification', $offline_instruction_notification_status );
1758
1759
			// Update offline instruction email message.
1760
			update_post_meta(
1761
				get_the_ID(),
1762
				'_give_offline-donation-instruction_email_message',
1763
				get_post_meta(
1764
					get_the_ID(),
1765
					// @todo: Delete this option later ( version > 2.0 ).
1766
					'_give_offline_donation_email',
1767
					true
1768
				)
1769
			);
1770
1771
			// Update offline instruction email subject.
1772
			update_post_meta(
1773
				get_the_ID(),
1774
				'_give_offline-donation-instruction_email_subject',
1775
				get_post_meta(
1776
					get_the_ID(),
1777
					// @todo: Delete this option later ( version > 2.0 ).
1778
					'_give_offline_donation_subject',
1779
					true
1780
				)
1781
			);
1782
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
1783
1784
		}// End while().
1785
1786
		wp_reset_postdata();
1787
	} else {
1788
		// No more forms found, finish up.
1789
		give_set_upgrade_complete( 'v20_upgrades_form_metadata' );
1790
	}
1791
}
1792
1793
1794
/**
1795
 * Upgrade payment metadata for new metabox settings.
1796
 *
1797
 * @since  2.0
1798
 * @global wpdb $wpdb
1799
 * @return void
1800
 */
1801
function give_v20_upgrades_payment_metadata_callback() {
1802
	global $wpdb;
1803
	$give_updates = Give_Updates::get_instance();
1804
1805
	// form query
1806
	$forms = new WP_Query( array(
1807
			'paged'          => $give_updates->step,
1808
			'status'         => 'any',
1809
			'order'          => 'ASC',
1810
			'post_type'      => 'give_payment',
1811
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1812
		)
1813
	);
1814
1815
	if ( $forms->have_posts() ) {
1816
		$give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 100 ) );
1817
1818
		while ( $forms->have_posts() ) {
1819
			$forms->the_post();
1820
			global $post;
1821
1822
			// Split _give_payment_meta meta.
1823
			// @todo Remove _give_payment_meta after releases 2.0
1824
			$payment_meta = give_get_meta( $post->ID, '_give_payment_meta', true );
1825
1826
			if ( ! empty( $payment_meta ) ) {
1827
				_give_20_bc_split_and_save_give_payment_meta( $post->ID, $payment_meta );
1828
			}
1829
1830
			$deprecated_meta_keys = array(
1831
				'_give_payment_customer_id' => '_give_payment_donor_id',
1832
				'_give_payment_user_email'  => '_give_payment_donor_email',
1833
				'_give_payment_user_ip'     => '_give_payment_donor_ip',
1834
			);
1835
1836 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...
1837
				// Do not add new meta key if already exist.
1838
				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...
1839
					continue;
1840
				}
1841
1842
				$wpdb->insert(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
1843
					$wpdb->postmeta,
1844
					array(
1845
						'post_id'    => $post->ID,
1846
						'meta_key'   => $new_meta_key,
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
1847
						'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...
1848
					)
1849
				);
1850
			}
1851
1852
			// Bailout
1853 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...
1854
				/* @var Give_Donor $donor */
1855
				$donor = new Give_Donor( $donor_id );
1856
1857
				$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...
1858
				$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...
1859
				$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...
1860
				$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...
1861
				$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...
1862
				$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...
1863
1864
				// Save address.
1865
				$donor->add_address( 'billing[]', $address );
1866
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1867
1868
		}// End while().
1869
1870
		wp_reset_postdata();
1871
	} else {
1872
		// @todo Delete user id meta after releases 2.0
1873
		// $wpdb->get_var( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key=%s", '_give_payment_user_id' ) );
1874
1875
		// No more forms found, finish up.
1876
		give_set_upgrade_complete( 'v20_upgrades_payment_metadata' );
1877
	}
1878
}
1879
1880
1881
/**
1882
 * Upgrade logs data.
1883
 *
1884
 * @since  2.0
1885
 * @return void
1886
 */
1887
function give_v20_logs_upgrades_callback() {
1888
	global $wpdb;
1889
	$give_updates = Give_Updates::get_instance();
1890
1891
	// form query
1892
	$forms = new WP_Query( array(
1893
			'paged'          => $give_updates->step,
1894
			'order'          => 'DESC',
1895
			'post_type'      => 'give_log',
1896
			'post_status'    => 'any',
1897
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1898
		)
1899
	);
1900
1901
	if ( $forms->have_posts() ) {
1902
		$give_updates->set_percentage( $forms->found_posts, $give_updates->step * 100 );
1903
1904
		while ( $forms->have_posts() ) {
1905
			$forms->the_post();
1906
			global $post;
1907
1908
			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...
1909
				continue;
1910
			}
1911
1912
			$term      = get_the_terms( $post->ID, 'give_log_type' );
1913
			$term      = ! is_wp_error( $term ) && ! empty( $term ) ? $term[0] : array();
1914
			$term_name = ! empty( $term ) ? $term->slug : '';
1915
1916
			$log_data = array(
1917
				'ID'           => $post->ID,
1918
				'log_title'    => $post->post_title,
1919
				'log_content'  => $post->post_content,
1920
				'log_parent'   => 0,
1921
				'log_type'     => $term_name,
1922
				'log_date'     => $post->post_date,
1923
				'log_date_gmt' => $post->post_date_gmt,
1924
			);
1925
			$log_meta = array();
1926
1927 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...
1928
				foreach ( $old_log_meta as $meta_key => $meta_value ) {
1929
					switch ( $meta_key ) {
1930
						case '_give_log_payment_id':
1931
							$log_data['log_parent']        = current( $meta_value );
1932
							$log_meta['_give_log_form_id'] = $post->post_parent;
1933
							break;
1934
1935
						default:
1936
							$log_meta[ $meta_key ] = current( $meta_value );
1937
					}
1938
				}
1939
			}
1940
1941
			if ( 'api_request' === $term_name ) {
1942
				$log_meta['_give_log_api_query'] = $post->post_excerpt;
1943
			}
1944
1945
			$wpdb->insert( "{$wpdb->prefix}give_logs", $log_data );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
1946
1947 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...
1948
				foreach ( $log_meta as $meta_key => $meta_value ) {
1949
					Give()->logs->logmeta_db->update_meta( $post->ID, $meta_key, $meta_value );
1950
				}
1951
			}
1952
1953
			$logIDs[] = $post->ID;
1954
		}// End while().
1955
1956
		wp_reset_postdata();
1957
	} else {
1958
		// @todo: Delete terms and taxonomy after releases 2.0.
1959
		/*$terms = get_terms( 'give_log_type', array( 'fields' => 'ids', 'hide_empty' => false ) );
1960
		if ( ! empty( $terms ) ) {
1961
			foreach ( $terms as $term ) {
1962
				wp_delete_term( $term, 'give_log_type' );
1963
			}
1964
		}*/
1965
1966
		// @todo: Delete logs after releases 2.0.
1967
		/*$logIDs = get_posts( array(
1968
				'order'          => 'DESC',
1969
				'post_type'      => 'give_log',
1970
				'post_status'    => 'any',
1971
				'posts_per_page' => - 1,
1972
				'fields'         => 'ids',
1973
			)
1974
		);*/
1975
1976
		/*if ( ! empty( $logIDs ) ) {
1977
			foreach ( $logIDs as $log ) {
1978
				// Delete term relationship and posts.
1979
				wp_delete_object_term_relationships( $log, 'give_log_type' );
1980
				wp_delete_post( $log, true );
1981
			}
1982
		}*/
1983
1984
		// @todo: Unregister taxonomy after releases 2.0.
1985
		/*unregister_taxonomy( 'give_log_type' );*/
1986
1987
		// Delete log cache.
1988
		Give()->logs->delete_cache();
1989
1990
		// No more forms found, finish up.
1991
		give_set_upgrade_complete( 'v20_logs_upgrades' );
1992
	}
1993
}
1994
1995
1996
/**
1997
 * Move payment and form metadata to new table
1998
 *
1999
 * @since  2.0
2000
 * @return void
2001
 */
2002
function give_v20_move_metadata_into_new_table_callback() {
2003
	global $wpdb;
2004
	$give_updates = Give_Updates::get_instance();
2005
2006
	// form query
2007
	$payments = new WP_Query( array(
2008
			'paged'          => $give_updates->step,
2009
			'status'         => 'any',
2010
			'order'          => 'ASC',
2011
			'post_type'      => array( 'give_forms', 'give_payment' ),
2012
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
2013
		)
2014
	);
2015
2016
	if ( $payments->have_posts() ) {
2017
		$give_updates->set_percentage( $payments->found_posts, $give_updates->step * 100 );
2018
2019
		while ( $payments->have_posts() ) {
2020
			$payments->the_post();
2021
			global $post;
2022
2023
			$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...
2024
				$wpdb->prepare(
2025
					"SELECT * FROM $wpdb->postmeta where post_id=%d",
2026
					get_the_ID()
2027
				),
2028
				ARRAY_A
2029
			);
2030
2031 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...
2032
				foreach ( $meta_data as $index => $data ) {
2033
					// Check for duplicate meta values.
2034
					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...
2035
						continue;
2036
					}
2037
2038
					switch ( $post->post_type ) {
2039
						case 'give_forms':
2040
							$data['form_id'] = $data['post_id'];
2041
							unset( $data['post_id'] );
2042
2043
							Give()->form_meta->insert( $data );
2044
							// @todo: delete form meta from post meta table after releases 2.0.
2045
							/*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2046
2047
							break;
2048
2049
						case 'give_payment':
2050
							$data['payment_id'] = $data['post_id'];
2051
							unset( $data['post_id'] );
2052
2053
							Give()->payment_meta->insert( $data );
2054
2055
							// @todo: delete donation meta from post meta table after releases 2.0.
2056
							/*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2057
2058
							break;
2059
					}
2060
				}
2061
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2062
2063
		}// End while().
2064
2065
		wp_reset_postdata();
2066
	} else {
2067
		// No more forms found, finish up.
2068
		give_set_upgrade_complete( 'v20_move_metadata_into_new_table' );
2069
	}
2070
2071
}
2072
2073
/**
2074
 * Upgrade routine for splitting donor name into first name and last name.
2075
 *
2076
 * @since 2.0
2077
 *
2078
 * @return void
2079
 */
2080
function give_v20_upgrades_donor_name() {
2081
	/* @var Give_Updates $give_updates */
2082
	$give_updates = Give_Updates::get_instance();
2083
2084
	$donors = Give()->donors->get_donors( array(
2085
		'paged'  => $give_updates->step,
2086
		'number' => 100,
2087
	) );
2088
2089
	if ( $donors ) {
2090
		$give_updates->set_percentage( count( $donors ), $give_updates->step * 100 );
2091
		// Loop through Donors
2092
		foreach ( $donors as $donor ) {
2093
2094
			$donor_name       = explode( ' ', $donor->name, 2 );
2095
			$donor_first_name = Give()->donor_meta->get_meta( $donor->id, '_give_donor_first_name' );
2096
			$donor_last_name  = Give()->donor_meta->get_meta( $donor->id, '_give_donor_last_name' );
2097
2098
			// If first name meta of donor is not created, then create it.
2099 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...
2100
				Give()->donor_meta->add_meta( $donor->id, '_give_donor_first_name', $donor_name[0] );
2101
			}
2102
2103
			// If last name meta of donor is not created, then create it.
2104 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...
2105
				Give()->donor_meta->add_meta( $donor->id, '_give_donor_last_name', $donor_name[1] );
2106
			}
2107
2108
			// If Donor is connected with WP User then update user meta.
2109 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...
2110
				if ( isset( $donor_name[0] ) ) {
2111
					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...
2112
				}
2113
				if ( isset( $donor_name[1] ) ) {
2114
					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...
2115
				}
2116
			}
2117
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2118
2119
	} else {
2120
		// The Update Ran.
2121
		give_set_upgrade_complete( 'v20_upgrades_donor_name' );
2122
	}
2123
2124
}
2125
2126
/**
2127
 * Upgrade routine for user addresses.
2128
 *
2129
 * @since 2.0
2130
 * @global wpdb $wpdb
2131
 *
2132
 * @return void
2133
 */
2134
function give_v20_upgrades_user_address() {
2135
	global $wpdb;
2136
2137
	/* @var Give_Updates $give_updates */
2138
	$give_updates = Give_Updates::get_instance();
2139
2140
	/* @var WP_User_Query $user_query */
2141
	$user_query = new WP_User_Query(
2142
		array(
2143
			'number' => 100,
2144
			'paged'  => $give_updates->step,
2145
		)
2146
	);
2147
2148
	$users = $user_query->get_results();
2149
2150
	if ( $users ) {
2151
		$give_updates->set_percentage( $user_query->get_total(), $give_updates->step * 100 );
2152
2153
		// Loop through Donors
2154
		foreach ( $users as $user ) {
2155
			/* @var Give_Donor $donor */
2156
			$donor = new Give_Donor( $user->ID, true );
2157
2158
			if ( ! $donor->id ) {
2159
				continue;
2160
			}
2161
2162
			$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...
2163
				$wpdb->prepare(
2164
					"
2165
					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...
2166
					WHERE user_id=%s
2167
					AND meta_key=%s
2168
					",
2169
					$user->ID,
2170
					'_give_user_address'
2171
				)
2172
			);
2173
2174 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...
2175
				$address = maybe_unserialize( $address );
2176
				$donor->add_address( 'personal', $address );
2177
				$donor->add_address( 'billing[]', $address );
2178
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
2179
2180
				// @todo: delete _give_user_address from user meta after releases 2.0.
2181
				/*delete_user_meta( $user->ID, '_give_user_address' );*/
2182
			}
2183
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2184
2185
	} else {
2186
		// The Update Ran.
2187
		give_set_upgrade_complete( 'v20_upgrades_user_address' );
2188
	}
2189
2190
}
2191
2192
/**
2193
 * Upgrade logs data.
2194
 *
2195
 * @since  2.0
2196
 * @global wpdb $wpdb
2197
 * @return void
2198
 */
2199
function give_v20_rename_donor_tables_callback() {
2200
	global $wpdb;
2201
2202
	/* @var Give_Updates $give_updates */
2203
	$give_updates = Give_Updates::get_instance();
2204
2205
	$tables = array(
2206
		"{$wpdb->prefix}give_customers"    => "{$wpdb->prefix}give_donors",
2207
		"{$wpdb->prefix}give_customermeta" => "{$wpdb->prefix}give_donormeta",
2208
	);
2209
2210
	// Alter customer table
2211
	foreach ( $tables as $old_table => $new_table ) {
2212
		if (
2213
			$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...
2214
			! $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...
2215
		) {
2216
			$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...
2217
2218
			if ( "{$wpdb->prefix}give_donormeta" === $new_table ) {
2219
				$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...
2220
			}
2221
		}
2222
	}
2223
2224
	$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...
2225
2226
	// No more forms found, finish up.
2227
	give_set_upgrade_complete( 'v20_rename_donor_tables' );
2228
2229
	// Re initiate donor classes.
2230
	Give()->donors     = new Give_DB_Donors();
2231
	Give()->donor_meta = new Give_DB_Donor_Meta();
2232
}
2233
2234
2235
/**
2236
 * Create missing meta tables.
2237
 *
2238
 * @since  2.0.1
2239
 * @global wpdb $wpdb
2240
 * @return void
2241
 */
2242
function give_v201_create_tables() {
2243
	global $wpdb;
2244
2245
	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...
2246
		Give()->payment_meta->create_table();
2247
	}
2248
2249
	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...
2250
		Give()->form_meta->create_table();
2251
	}
2252
2253
	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...
2254
		Give()->logs->log_db->create_table();
2255
	}
2256
2257
	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...
2258
		Give()->logs->logmeta_db->create_table();
2259
	}
2260
}
2261
2262
/**
2263
 * Upgrade payment metadata for new metabox settings.
2264
 *
2265
 * @since  2.0.1
2266
 * @global wpdb $wpdb
2267
 * @return void
2268
 */
2269
function give_v201_upgrades_payment_metadata_callback() {
2270
	global $wpdb, $post;
2271
	$give_updates = Give_Updates::get_instance();
2272
	give_v201_create_tables();
2273
2274
	$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...
2275
		"
2276
			SELECT ID FROM $wpdb->posts
2277
			WHERE 1=1
2278
			AND ( 
2279
  				$wpdb->posts.post_date >= '2018-01-08 00:00:00'
2280
			)
2281
			AND $wpdb->posts.post_type = 'give_payment'
2282
			AND {$wpdb->posts}.post_status IN ('" . implode( "','", array_keys( give_get_payment_statuses() ) ) . "')
2283
			ORDER BY $wpdb->posts.post_date ASC 
2284
			LIMIT 100
2285
			OFFSET " . $give_updates->get_offset( 100 )
2286
	);
2287
2288
	if ( ! empty( $payments ) ) {
2289
		$give_updates->set_percentage( give_get_total_post_type_count( 'give_payment' ), ( $give_updates->step * 100 ) );
2290
2291
		foreach ( $payments as $payment_id ) {
2292
			$post = get_post( $payment_id );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
2293
			setup_postdata( $post );
2294
2295
			// Do not add new meta keys if already refactored.
2296
			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...
2297
				continue;
2298
			}
2299
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
2300
2301
			// Split _give_payment_meta meta.
2302
			// @todo Remove _give_payment_meta after releases 2.0
2303
			$payment_meta = give_get_meta( $post->ID, '_give_payment_meta', true );
2304
2305
			if ( ! empty( $payment_meta ) ) {
2306
				_give_20_bc_split_and_save_give_payment_meta( $post->ID, $payment_meta );
2307
			}
2308
2309
			$deprecated_meta_keys = array(
2310
				'_give_payment_customer_id' => '_give_payment_donor_id',
2311
				'_give_payment_user_email'  => '_give_payment_donor_email',
2312
				'_give_payment_user_ip'     => '_give_payment_donor_ip',
2313
			);
2314
2315 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...
2316
				// Do not add new meta key if already exist.
2317
				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...
2318
					continue;
2319
				}
2320
2321
				$wpdb->insert(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
2322
					$wpdb->postmeta,
2323
					array(
2324
						'post_id'    => $post->ID,
2325
						'meta_key'   => $new_meta_key,
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
2326
						'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...
2327
					)
2328
				);
2329
			}
2330
2331
			// Bailout
2332 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...
2333
				/* @var Give_Donor $donor */
2334
				$donor = new Give_Donor( $donor_id );
2335
2336
				$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...
2337
				$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...
2338
				$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...
2339
				$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...
2340
				$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...
2341
				$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...
2342
2343
				// Save address.
2344
				$donor->add_address( 'billing[]', $address );
2345
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2346
2347
		}// End while().
2348
2349
		wp_reset_postdata();
2350
	} else {
2351
		// @todo Delete user id meta after releases 2.0
2352
		// $wpdb->get_var( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key=%s", '_give_payment_user_id' ) );
2353
2354
		// No more forms found, finish up.
2355
		give_set_upgrade_complete( 'v201_upgrades_payment_metadata' );
2356
	}
2357
}
2358
2359
/**
2360
 * Move payment and form metadata to new table
2361
 *
2362
 * @since  2.0.1
2363
 * @return void
2364
 */
2365
function give_v201_move_metadata_into_new_table_callback() {
2366
	global $wpdb, $post;
2367
	$give_updates = Give_Updates::get_instance();
2368
	give_v201_create_tables();
2369
2370
	$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...
2371
		"
2372
			SELECT ID FROM $wpdb->posts 
2373
			WHERE 1=1
2374
			AND ( $wpdb->posts.post_type = 'give_payment' OR $wpdb->posts.post_type = 'give_forms' )
2375
			AND {$wpdb->posts}.post_status IN ('" . implode( "','", array_keys( give_get_payment_statuses() ) ) . "')
2376
			ORDER BY $wpdb->posts.post_date ASC 
2377
			LIMIT 100
2378
			OFFSET " . $give_updates->get_offset( 100 )
2379
	);
2380
2381
	if ( ! empty( $payments ) ) {
2382
		$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...
2383
			'give_forms',
2384
			'give_payment',
2385
		) ), $give_updates->step * 100 );
2386
2387
		foreach ( $payments as $payment_id ) {
2388
			$post = get_post( $payment_id );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
2389
			setup_postdata( $post );
2390
2391
			$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...
2392
				$wpdb->prepare(
2393
					"SELECT * FROM $wpdb->postmeta where post_id=%d",
2394
					get_the_ID()
2395
				),
2396
				ARRAY_A
2397
			);
2398
2399 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...
2400
				foreach ( $meta_data as $index => $data ) {
2401
					// Check for duplicate meta values.
2402
					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...
2403
						continue;
2404
					}
2405
2406
					switch ( $post->post_type ) {
2407
						case 'give_forms':
2408
							$data['form_id'] = $data['post_id'];
2409
							unset( $data['post_id'] );
2410
2411
							Give()->form_meta->insert( $data );
2412
							// @todo: delete form meta from post meta table after releases 2.0.
2413
							/*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2414
2415
							break;
2416
2417
						case 'give_payment':
2418
							$data['payment_id'] = $data['post_id'];
2419
							unset( $data['post_id'] );
2420
2421
							Give()->payment_meta->insert( $data );
2422
2423
							// @todo: delete donation meta from post meta table after releases 2.0.
2424
							/*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2425
2426
							break;
2427
					}
2428
				}
2429
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2430
2431
		}// End while().
2432
2433
		wp_reset_postdata();
2434
	} else {
2435
		// No more forms found, finish up.
2436
		give_set_upgrade_complete( 'v201_move_metadata_into_new_table' );
2437
	}
2438
2439
}
2440
2441
/**
2442
 * Move data to new log table.
2443
 *
2444
 * @since  2.0.1
2445
 * @return void
2446
 */
2447
function give_v201_logs_upgrades_callback() {
2448
	global $wpdb, $post;
2449
	$give_updates = Give_Updates::get_instance();
2450
	give_v201_create_tables();
2451
2452
	$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...
2453
		"
2454
			SELECT ID FROM $wpdb->posts 
2455
			WHERE 1=1
2456
			AND $wpdb->posts.post_type = 'give_log'
2457
			AND {$wpdb->posts}.post_status IN ('" . implode( "','", array_keys( give_get_payment_statuses() ) ) . "')
2458
			ORDER BY $wpdb->posts.post_date ASC 
2459
			LIMIT 100
2460
			OFFSET " . $give_updates->get_offset( 100 )
2461
	);
2462
2463
	if ( ! empty( $logs ) ) {
2464
		$give_updates->set_percentage( give_get_total_post_type_count( 'give_log' ), $give_updates->step * 100 );
2465
2466
		foreach ( $logs as $log_id ) {
2467
			$post = get_post( $log_id );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
2468
			setup_postdata( $post );
2469
2470
			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...
2471
				continue;
2472
			}
2473
2474
			$term      = get_the_terms( $post->ID, 'give_log_type' );
2475
			$term      = ! is_wp_error( $term ) && ! empty( $term ) ? $term[0] : array();
2476
			$term_name = ! empty( $term ) ? $term->slug : '';
2477
2478
			$log_data = array(
2479
				'ID'           => $post->ID,
2480
				'log_title'    => $post->post_title,
2481
				'log_content'  => $post->post_content,
2482
				'log_parent'   => 0,
2483
				'log_type'     => $term_name,
2484
				'log_date'     => $post->post_date,
2485
				'log_date_gmt' => $post->post_date_gmt,
2486
			);
2487
			$log_meta = array();
2488
2489 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...
2490
				foreach ( $old_log_meta as $meta_key => $meta_value ) {
2491
					switch ( $meta_key ) {
2492
						case '_give_log_payment_id':
2493
							$log_data['log_parent']        = current( $meta_value );
2494
							$log_meta['_give_log_form_id'] = $post->post_parent;
2495
							break;
2496
2497
						default:
2498
							$log_meta[ $meta_key ] = current( $meta_value );
2499
					}
2500
				}
2501
			}
2502
2503
			if ( 'api_request' === $term_name ) {
2504
				$log_meta['_give_log_api_query'] = $post->post_excerpt;
2505
			}
2506
2507
			$wpdb->insert( "{$wpdb->prefix}give_logs", $log_data );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
2508
2509 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...
2510
				foreach ( $log_meta as $meta_key => $meta_value ) {
2511
					Give()->logs->logmeta_db->update_meta( $post->ID, $meta_key, $meta_value );
2512
				}
2513
			}
2514
2515
			$logIDs[] = $post->ID;
2516
		}// End while().
2517
2518
		wp_reset_postdata();
2519
	} else {
2520
		// Delete log cache.
2521
		Give()->logs->delete_cache();
2522
2523
		// No more forms found, finish up.
2524
		give_set_upgrade_complete( 'v201_logs_upgrades' );
2525
	}
2526
}
2527
2528
2529
/**
2530
 * Add missing donor.
2531
 *
2532
 * @since  2.0.1
2533
 * @return void
2534
 */
2535
function give_v201_add_missing_donors_callback() {
2536
	global $wpdb;
2537
	give_v201_create_tables();
2538
2539
	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...
2540
		$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...
2541
		$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...
2542
		$donor_data = array();
2543
2544
		if ( $missing_donors = array_diff( $customers, $donors ) ) {
2545
			foreach ( $missing_donors as $donor_id ) {
2546
				$donor_data[] = array(
2547
					'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...
2548
					'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...
2549
2550
				);
2551
			}
2552
		}
2553
2554
		if ( ! empty( $donor_data ) ) {
2555
			$donor_table_name      = Give()->donors->table_name;
2556
			$donor_meta_table_name = Give()->donor_meta->table_name;
2557
2558
			Give()->donors->table_name     = "{$wpdb->prefix}give_donors";
2559
			Give()->donor_meta->table_name = "{$wpdb->prefix}give_donormeta";
2560
2561
			foreach ( $donor_data as $donor ) {
2562
				$donor['info'][0] = (array) $donor['info'][0];
2563
2564
				// Prevent duplicate meta id issue.
2565
				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...
2566
					continue;
2567
				}
2568
2569
				$donor_id = Give()->donors->add( $donor['info'][0] );
2570
2571
				if ( ! empty( $donor['meta'] ) ) {
2572
					foreach ( $donor['meta'] as $donor_meta ) {
2573
						$donor_meta = (array) $donor_meta;
2574
2575
						// Prevent duplicate meta id issue.
2576
						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...
2577
							unset( $donor_meta['meta_id'] );
2578
						}
2579
2580
						$donor_meta['donor_id'] = $donor_meta['customer_id'];
2581
						unset( $donor_meta['customer_id'] );
2582
2583
						Give()->donor_meta->insert( $donor_meta );
2584
					}
2585
				}
2586
2587
				/**
2588
				 * Fix donor name and address
2589
				 */
2590
				$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...
2591
					$wpdb->prepare(
2592
						"
2593
					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...
2594
					WHERE user_id=%s
2595
					AND meta_key=%s
2596
					",
2597
						$donor['info'][0]['user_id'],
2598
						'_give_user_address'
2599
					)
2600
				);
2601
2602
				$donor = new Give_Donor( $donor_id );
2603
2604 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...
2605
					$address = maybe_unserialize( $address );
2606
					$donor->add_address( 'personal', $address );
2607
					$donor->add_address( 'billing[]', $address );
2608
				}
2609
2610
				$donor_name       = explode( ' ', $donor->name, 2 );
2611
				$donor_first_name = Give()->donor_meta->get_meta( $donor->id, '_give_donor_first_name' );
2612
				$donor_last_name  = Give()->donor_meta->get_meta( $donor->id, '_give_donor_last_name' );
2613
2614
				// If first name meta of donor is not created, then create it.
2615 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...
2616
					Give()->donor_meta->add_meta( $donor->id, '_give_donor_first_name', $donor_name[0] );
2617
				}
2618
2619
				// If last name meta of donor is not created, then create it.
2620 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...
2621
					Give()->donor_meta->add_meta( $donor->id, '_give_donor_last_name', $donor_name[1] );
2622
				}
2623
2624
				// If Donor is connected with WP User then update user meta.
2625 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...
2626
					if ( isset( $donor_name[0] ) ) {
2627
						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...
2628
					}
2629
					if ( isset( $donor_name[1] ) ) {
2630
						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...
2631
					}
2632
				}
2633
			}
2634
2635
			Give()->donors->table_name     = $donor_table_name;
2636
			Give()->donor_meta->table_name = $donor_meta_table_name;
2637
		}
2638
	}
2639
2640
	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...
2641
	give_set_upgrade_complete( 'v201_add_missing_donors' );
2642
}
2643
2644
2645
/**
2646
 * Version 2.0.3 automatic updates
2647
 *
2648
 * @since 2.0.3
2649
 */
2650
function give_v203_upgrades() {
2651
	global $wpdb;
2652
2653
	// Do not auto load option.
2654
	$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...
2655
2656
	// Remove from cache.
2657
	$all_options = wp_load_alloptions();
2658
2659
	if ( isset( $all_options['give_completed_upgrades'] ) ) {
2660
		unset( $all_options['give_completed_upgrades'] );
2661
		wp_cache_set( 'alloptions', $all_options, 'options' );
2662
	}
2663
2664
}
2665
2666
2667
/**
2668
 * Version 2.2.0 automatic updates
2669
 *
2670
 * @since 2.2.0
2671
 */
2672
function give_v220_upgrades(){
2673
	global $wpdb;
2674
2675
	/**
2676
	 * Update 1
2677
	 *
2678
	 * Delete wp session data
2679
	 */
2680
	give_v220_delete_wp_session_data();
2681
2682
	/**
2683
	 * Update 2
2684
	 *
2685
	 * Rename payment table
2686
	 */
2687
	give_v220_rename_donation_meta_type_callback();
2688
2689
	/**
2690
	 * Update 2
2691
	 *
2692
	 * Set autoload to no to reduce result weight from WordPress query
2693
	 */
2694
2695
	$options = array(
2696
		'give_settings',
2697
		'give_version',
2698
		'give_version_upgraded_from',
2699
		'give_default_api_version',
2700
		'give_site_address_before_migrate',
2701
		'_give_table_check',
2702
		'give_recently_activated_addons',
2703
		'give_is_addon_activated',
2704
		'give_last_paypal_ipn_received',
2705
		'give_use_php_sessions',
2706
		'give_subscriptions',
2707
		'_give_subscriptions_edit_last'
2708
	);
2709
2710
	// Add all table version option name
2711
	// Add banner option *_active_by_user
2712
	$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...
2713
		"
2714
		SELECT option_name
2715
		FROM $wpdb->options
2716
		WHERE option_name like '%give%'
2717
		AND (
2718
			option_name like '%_db_version%'
2719
			OR option_name like '%_active_by_user%'
2720
			OR option_name like '%_license_active%'
2721
		)
2722
		"
2723
	);
2724
2725
	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...
2726
		$options = array_merge( $options, $option_like );
2727
	}
2728
2729
	$options_str =  '\'' . implode( "','", $options ) . '\'';
0 ignored issues
show
introduced by
Expected 1 space after "="; 2 found
Loading history...
2730
2731
	$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...
2732
		"
2733
		UPDATE $wpdb->options
2734
		SET autoload = 'no'
2735
		WHERE option_name IN ( {$options_str} )
2736
		"
2737
	);
2738
}
2739
2740
/**
2741
 * Version 2.2.1 automatic updates
2742
 *
2743
 * @since 2.2.1
2744
 */
2745
function give_v221_upgrades() {
2746
	global $wpdb;
2747
2748
	/**
2749
	 * Update  1
2750
	 *
2751
	 * Change column length
2752
	 */
2753
	$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...
2754
}
2755
2756
/**
2757
 * Upgrade routine for 2.1 to set form closed status for all the donation forms.
2758
 *
2759
 * @since 2.1
2760
 */
2761
function give_v210_verify_form_status_upgrades_callback() {
2762
2763
	$give_updates = Give_Updates::get_instance();
2764
2765
	// form query.
2766
	$donation_forms = new WP_Query( array(
2767
			'paged'          => $give_updates->step,
2768
			'status'         => 'any',
2769
			'order'          => 'ASC',
2770
			'post_type'      => 'give_forms',
2771
			'posts_per_page' => 20,
2772
		)
2773
	);
2774
2775
	if ( $donation_forms->have_posts() ) {
2776
		$give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
2777
2778
		while ( $donation_forms->have_posts() ) {
2779
			$donation_forms->the_post();
2780
			$form_id = get_the_ID();
2781
2782
			$form_closed_status = give_get_meta( $form_id, '_give_form_status', true );
2783
			if ( empty( $form_closed_status ) ) {
2784
				give_set_form_closed_status( $form_id );
2785
			}
2786
		}
2787
2788
		/* Restore original Post Data */
2789
		wp_reset_postdata();
2790
2791
	} else {
2792
2793
		// The Update Ran.
2794
		give_set_upgrade_complete( 'v210_verify_form_status_upgrades' );
2795
	}
2796
}
2797
2798
/**
2799
 * Upgrade routine for 2.1.3 to delete meta which is not attach to any donation.
2800
 *
2801
 * @since 2.1
2802
 */
2803
function give_v213_delete_donation_meta_callback() {
2804
	global $wpdb;
2805
	$give_updates        = Give_Updates::get_instance();
2806
	$donation_meta_table = Give()->payment_meta->table_name;
2807
2808
	$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...
2809
		"
2810
		SELECT DISTINCT payment_id
2811
		FROM {$donation_meta_table}
2812
		LIMIT 20
2813
		OFFSET {$give_updates->get_offset( 20 )}
2814
		"
2815
	);
2816
2817
	if ( ! empty( $donations ) ) {
2818
		foreach ( $donations as $donation ) {
2819
			$donation_obj = get_post( $donation );
2820
2821
			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...
2822
				Give()->payment_meta->delete_all_meta( $donation );
2823
			}
2824
		}
2825
	} else {
2826
2827
		// The Update Ran.
2828
		give_set_upgrade_complete( 'v213_delete_donation_meta' );
2829
	}
2830
}
2831
2832
/**
2833
 * Rename donation meta type
2834
 *
2835
 * @see https://github.com/restrictcontentpro/restrict-content-pro/issues/1656
2836
 *
2837
 * @since 2.2.0
2838
 */
2839
function give_v220_rename_donation_meta_type_callback(){
2840
	global $wpdb;
2841
2842
	// Check upgrade before running.
2843
	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...
2844
		give_has_upgrade_completed( 'v220_rename_donation_meta_type' )
2845
		|| ! $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...
2846
	) {
2847
		return;
2848
	}
2849
2850
	$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...
2851
	$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...
2852
2853
	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...
2854
}
2855
2856
/**
2857
 * Adds 'view_give_payments' capability to 'give_manager' user role.
2858
 *
2859
 * @since 2.1.5
2860
 */
2861
function give_v215_update_donor_user_roles_callback() {
2862
2863
	$role = get_role( 'give_manager' );
2864
	$role->add_cap( 'view_give_payments' );
2865
2866
	give_set_upgrade_complete( 'v215_update_donor_user_roles' );
2867
}
2868
2869
2870
2871
/**
2872
 * Remove all wp session data from the options table, regardless of expiration.
2873
 *
2874
 * @since 2.2.0
2875
 *
2876
 * @global wpdb $wpdb
2877
 */
2878
function give_v220_delete_wp_session_data(){
2879
	global $wpdb;
2880
2881
	$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...
2882
}
2883
2884
2885
/**
2886
 * Update donor meta
2887
 * Set "_give_anonymous_donor" meta key to "0" if not exist
2888
 *
2889
 *
2890
 * @since 2.2.4
2891
 */
2892
function give_v224_update_donor_meta_callback() {
2893
	/* @var Give_Updates $give_updates */
2894
	$give_updates = Give_Updates::get_instance();
2895
2896
	$donor_count = Give()->donors->count( array(
2897
		'number' => -1,
2898
	) );
2899
2900
	$donors = Give()->donors->get_donors( array(
2901
		'paged'  => $give_updates->step,
2902
		'number' => 100,
2903
	) );
2904
2905
	if ( $donors ) {
2906
		$give_updates->set_percentage( $donor_count, $give_updates->step * 100 );
2907
		// Loop through Donors
2908
		foreach ( $donors as $donor ) {
2909
			$anonymous_metadata = Give()->donor_meta->get_meta( $donor->id, '_give_anonymous_donor', true );
2910
2911
			// If first name meta of donor is not created, then create it.
2912
			if ( ! in_array( $anonymous_metadata, array( '0', '1' ) ) ) {
2913
				Give()->donor_meta->add_meta( $donor->id, '_give_anonymous_donor', '0' );
2914
			}
2915
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2916
2917
	} else {
2918
		// The Update Ran.
2919
		give_set_upgrade_complete( 'v224_update_donor_meta' );
2920
	}
2921
}
2922
2923
2924
/**
2925
 * Update donor meta
2926
 * Set "_give_anonymous_donor_forms" meta key if not exist
2927
 *
2928
 *
2929
 * @since 2.2.4
2930
 */
2931
function give_v224_update_donor_meta_forms_id_callback() {
2932
	$give_updates = Give_Updates::get_instance();
2933
2934
	$donations = new WP_Query( array(
2935
			'paged'          => $give_updates->step,
2936
			'status'         => 'any',
2937
			'order'          => 'ASC',
2938
			'post_type'      => array( 'give_payment' ),
2939
			'posts_per_page' => 20,
2940
		)
2941
	);
2942
2943
	if ( $donations->have_posts() ) {
2944
		$give_updates->set_percentage( $donations->found_posts, $give_updates->step * 100 );
2945
2946
		while ( $donations->have_posts() ) {
2947
			$donations->the_post();
2948
2949
			if ( give_is_anonymous_donation( get_the_ID() ) ) {
2950
				$form_id    = give_get_meta( get_the_ID(), '_give_payment_form_id', true );
2951
				$donor_id   = give_get_meta( get_the_ID(), '_give_payment_donor_id', true );
2952
				$donor_meta = (array) Give()->donor_meta->get_meta( $donor_id, '_give_anonymous_donor_forms', true );
2953
2954
				if ( ! in_array( $form_id, $donor_meta ) ) {
2955
					$donor_meta[] = $form_id;
2956
					Give()->donor_meta->update_meta( $donor_id, '_give_anonymous_donor_forms', $donor_meta );
2957
				}
2958
			}
2959
		}
2960
2961
		wp_reset_postdata();
2962
	} else {
2963
		give_set_upgrade_complete( 'v224_update_donor_meta_forms_id' );
2964
	}
2965
}
2966