Test Failed
Push — master ( 21ce85...fda949 )
by Devin
06:42
created

upgrade-functions.php ➔ give_v201_upgrades_payment_metadata_callback()   B

Complexity

Conditions 8
Paths 15

Size

Total Lines 86

Duplication

Lines 29
Ratio 33.72 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
nc 15
nop 0
dl 29
loc 86
ccs 0
cts 0
cp 0
crap 72
rs 7.061
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Upgrade Functions
4
 *
5
 * @package     Give
6
 * @subpackage  Admin/Upgrades
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.0
10
 *
11
 * NOTICE: When adding new upgrade notices, please be sure to put the action into the upgrades array during install:
12
 * /includes/install.php @ Appox Line 156
13
 */
14
15
// Exit if accessed directly.
16
if ( ! defined( 'ABSPATH' ) ) {
17
	exit;
18
}
19
20
/**
21
 * Perform automatic database upgrades when necessary.
22
 *
23
 * @since  1.6
24
 * @return void
25
 */
26
function give_do_automatic_upgrades() {
27
	$did_upgrade  = false;
28
	$give_version = preg_replace( '/[^0-9.].*/', '', get_option( 'give_version' ) );
29
30
	if ( ! $give_version ) {
31
		// 1.0 is the first version to use this option so we must add it.
32
		$give_version = '1.0';
33
	}
34
35
	switch ( true ) {
36
37
		case version_compare( $give_version, '1.6', '<' ):
38
			give_v16_upgrades();
39
			$did_upgrade = true;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
40
41
		case version_compare( $give_version, '1.7', '<' ):
42
			give_v17_upgrades();
43
			$did_upgrade = true;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
44
45
		case version_compare( $give_version, '1.8', '<' ):
46
			give_v18_upgrades();
47
			$did_upgrade = true;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
48
49
		case version_compare( $give_version, '1.8.7', '<' ):
50
			give_v187_upgrades();
51
			$did_upgrade = true;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
52
53
		case version_compare( $give_version, '1.8.8', '<' ):
54
			give_v188_upgrades();
55
			$did_upgrade = true;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
56
57
		case version_compare( $give_version, '1.8.9', '<' ):
58
			give_v189_upgrades();
59
			$did_upgrade = true;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
60
61
		case version_compare( $give_version, '1.8.12', '<' ):
62
			give_v1812_upgrades();
63
			$did_upgrade = true;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
64
65
		case version_compare( $give_version, '1.8.13', '<' ):
66
			give_v1813_upgrades();
67
			$did_upgrade = true;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
68
69
		case version_compare( $give_version, '1.8.17', '<' ):
70
			give_v1817_upgrades();
71
			$did_upgrade = true;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
72
73
		case version_compare( $give_version, '1.8.18', '<' ):
74
			give_v1818_upgrades();
75
			$did_upgrade = true;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
76
77
		case version_compare( $give_version, '2.0', '<' ):
78
			give_v20_upgrades();
79
			$did_upgrade = true;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
80
81
		case version_compare( $give_version, '2.0.1', '<' ):
82
			// Do nothing on fresh install.
83
			if ( ! doing_action( 'give_upgrades' ) ) {
84
				give_v201_create_tables();
85
				Give_Updates::get_instance()->__health_background_update( Give_Updates::get_instance() );
86
				Give_Updates::$background_updater->dispatch();
87
			}
88
89
			$did_upgrade = true;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
90
91
		case version_compare( $give_version, '2.0.2', '<' ):
92
			// Remove 2.0.1 update to rerun on 2.0.2
93
			$completed_upgrades = give_get_completed_upgrades();
94
			$v201_updates       = array(
95
				'v201_upgrades_payment_metadata',
96
				'v201_add_missing_donors',
97
				'v201_move_metadata_into_new_table',
98
				'v201_logs_upgrades',
99
			);
100
101
			foreach ( $v201_updates as $v201_update ) {
102
				if ( in_array( $v201_update, $completed_upgrades ) ) {
103
					unset( $completed_upgrades[ array_search( $v201_update, $completed_upgrades ) ] );
104
				}
105
			}
106
107
			update_option( 'give_completed_upgrades', $completed_upgrades, false );
108
109
			// Do nothing on fresh install.
110
			if ( ! doing_action( 'give_upgrades' ) ) {
111
				give_v201_create_tables();
112
				Give_Updates::get_instance()->__health_background_update( Give_Updates::get_instance() );
113
				Give_Updates::$background_updater->dispatch();
114
			}
115
116
			$did_upgrade = true;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
117 1
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 1
122
		case version_compare( $give_version, '2.2.0', '<' ):
123 1
			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 1
139
/**
140
 * Display Upgrade Notices.
141
 *
142 1
 * IMPORTANT: ALSO UPDATE INSTALL.PHP WITH THE ID OF THE UPGRADE ROUTINE SO IT DOES NOT AFFECT NEW INSTALLS.
143 1
 *
144
 * @since 1.0
145
 * @since 1.8.12 Update new update process code.
146 1
 *
147
 * @param Give_Updates $give_updates
148 1
 *
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 1
	);
160
161 1
	// 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 1
			'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(
218
		array(
219
			'id'       => 'v1817_update_donation_iranian_currency_code',
220
			'version'  => '1.8.17',
221
			'callback' => 'give_v1817_update_donation_iranian_currency_code',
222
		)
223
	);
224
225
	// v1.8.17 Upgrades for cleanup of user roles.
226
	$give_updates->register(
227
		array(
228
			'id'       => 'v1817_cleanup_user_roles',
229
			'version'  => '1.8.17',
230
			'callback' => 'give_v1817_cleanup_user_roles',
231
		)
232
	);
233
234
	// v1.8.18 Upgrades for assigning custom amount to existing set donations.
235
	$give_updates->register(
236
		array(
237
			'id'       => 'v1818_assign_custom_amount_set_donation',
238
			'version'  => '1.8.18',
239
			'callback' => 'give_v1818_assign_custom_amount_set_donation',
240
		)
241
	);
242
243
	// v1.8.18 Cleanup the Give Worker Role Caps.
244
	$give_updates->register(
245
		array(
246
			'id'       => 'v1818_give_worker_role_cleanup',
247
			'version'  => '1.8.18',
248
			'callback' => 'give_v1818_give_worker_role_cleanup',
249
		)
250
	);
251
252
	// v2.0.0 Upgrades
253
	$give_updates->register(
254
		array(
255
			'id'       => 'v20_upgrades_form_metadata',
256
			'version'  => '2.0.0',
257
			'callback' => 'give_v20_upgrades_form_metadata_callback',
258
		)
259
	);
260
261
	// v2.0.0 User Address Upgrades
262
	$give_updates->register(
263
		array(
264
			'id'       => 'v20_upgrades_user_address',
265
			'version'  => '2.0.0',
266
			'callback' => 'give_v20_upgrades_user_address',
267
		)
268
	);
269
270
	// v2.0.0 Upgrades
271
	$give_updates->register(
272
		array(
273
			'id'       => 'v20_upgrades_payment_metadata',
274
			'version'  => '2.0.0',
275
			'callback' => 'give_v20_upgrades_payment_metadata_callback',
276
		)
277
	);
278
279
	// v2.0.0 Upgrades
280
	$give_updates->register(
281
		array(
282
			'id'       => 'v20_logs_upgrades',
283
			'version'  => '2.0.0',
284
			'callback' => 'give_v20_logs_upgrades_callback',
285
286
		)
287
	);
288
289
	// v2.0.0 Donor Name Upgrades
290
	$give_updates->register(
291
		array(
292
			'id'       => 'v20_upgrades_donor_name',
293
			'version'  => '2.0.0',
294
			'callback' => 'give_v20_upgrades_donor_name',
295
		)
296
	);
297
298
	// v2.0.0 Upgrades
299
	$give_updates->register(
300
		array(
301
			'id'       => 'v20_move_metadata_into_new_table',
302
			'version'  => '2.0.0',
303
			'callback' => 'give_v20_move_metadata_into_new_table_callback',
304
			'depend'   => array( 'v20_upgrades_payment_metadata', 'v20_upgrades_form_metadata' ),
305
		)
306
	);
307
308
	// v2.0.0 Upgrades
309
	$give_updates->register(
310
		array(
311
			'id'       => 'v20_rename_donor_tables',
312
			'version'  => '2.0.0',
313
			'callback' => 'give_v20_rename_donor_tables_callback',
314
			'depend'   => array(
315
				'v20_move_metadata_into_new_table',
316
				'v20_logs_upgrades',
317
				'v20_upgrades_form_metadata',
318
				'v20_upgrades_payment_metadata',
319
				'v20_upgrades_user_address',
320
				'v20_upgrades_donor_name',
321
			),
322
		)
323
	);
324
325
	// v2.0.1 Upgrades
326
	$give_updates->register(
327
		array(
328
			'id'       => 'v201_upgrades_payment_metadata',
329
			'version'  => '2.0.1',
330
			'callback' => 'give_v201_upgrades_payment_metadata_callback',
331
		)
332
	);
333
334
	// v2.0.1 Upgrades
335
	$give_updates->register(
336
		array(
337
			'id'       => 'v201_add_missing_donors',
338
			'version'  => '2.0.1',
339
			'callback' => 'give_v201_add_missing_donors_callback',
340
		)
341
	);
342
343
	// Run v2.0.0 Upgrades again in 2.0.1
344
	$give_updates->register(
345
		array(
346
			'id'       => 'v201_move_metadata_into_new_table',
347
			'version'  => '2.0.1',
348
			'callback' => 'give_v201_move_metadata_into_new_table_callback',
349
			'depend'   => array( 'v201_upgrades_payment_metadata', 'v201_add_missing_donors' ),
350
		)
351
	);
352
353
	// Run v2.0.0 Upgrades again in 2.0.1
354
	$give_updates->register(
355
		array(
356
			'id'       => 'v201_logs_upgrades',
357
			'version'  => '2.0.1',
358
			'callback' => 'give_v201_logs_upgrades_callback',
359
		)
360
	);
361
362
	// v2.1 Verify Form Status Upgrade.
363
	$give_updates->register(
364
		array(
365
			'id'       => 'v210_verify_form_status_upgrades',
366
			'version'  => '2.1.0',
367
			'callback' => 'give_v210_verify_form_status_upgrades_callback',
368
		)
369
	);
370
371
	// v2.1.3 Delete non attached donation meta.
372
	$give_updates->register(
373
		array(
374
			'id'       => 'v213_delete_donation_meta',
375
			'version'  => '2.1.3',
376
			'callback' => 'give_v213_delete_donation_meta_callback',
377
			'depends'  => array( 'v201_move_metadata_into_new_table' ),
378
		)
379
	);
380
381
	// v2.1.5 Add additional capability to the give_manager role.
382
	$give_updates->register(
383
		array(
384
			'id'       => 'v215_update_donor_user_roles',
385
			'version'  => '2.1.5',
386
			'callback' => 'give_v215_update_donor_user_roles_callback',
387
		)
388
	);
389
390
	// v2.2.4 set each donor to anonymous by default.
391
	$give_updates->register(
392
		array(
393
			'id'       => 'v224_update_donor_meta',
394
			'version'  => '2.2.4',
395
			'callback' => 'give_v224_update_donor_meta_callback',
396
		)
397
	);
398
}
399
400
add_action( 'give_register_updates', 'give_show_upgrade_notices' );
401
402
/**
403
 * Triggers all upgrade functions
404
 *
405
 * This function is usually triggered via AJAX
406
 *
407
 * @since 1.0
408
 * @return void
409
 */
410
function give_trigger_upgrades() {
411
412
	if ( ! current_user_can( 'manage_give_settings' ) ) {
413
		wp_die(
414
			esc_html__( 'You do not have permission to do Give upgrades.', 'give' ), esc_html__( 'Error', 'give' ), array(
415
				'response' => 403,
416
			)
417
		);
418
	}
419
420
	$give_version = get_option( 'give_version' );
421
422
	if ( ! $give_version ) {
423
		// 1.0 is the first version to use this option so we must add it.
424
		$give_version = '1.0';
425
		add_option( 'give_version', $give_version, '', false );
426
	}
427
428
	update_option( 'give_version', GIVE_VERSION, false );
429
	delete_option( 'give_doing_upgrade' );
430
431
	if ( DOING_AJAX ) {
432
		die( 'complete' );
433
	} // End if().
434
}
435
436
add_action( 'wp_ajax_give_trigger_upgrades', 'give_trigger_upgrades' );
437
438
439
/**
440
 * Upgrades the
441
 *
442
 * Standardizes the discrepancies between two metakeys `_give_payment_customer_id` and `_give_payment_donor_id`
443
 *
444
 * @since      1.3.2
445
 */
446
function give_v132_upgrade_give_payment_customer_id() {
447
	global $wpdb;
448
449
	/* @var Give_Updates $give_updates */
450
	$give_updates = Give_Updates::get_instance();
451
452
	// UPDATE DB METAKEYS.
453
	$sql   = "UPDATE $wpdb->postmeta SET meta_key = '_give_payment_customer_id' WHERE meta_key = '_give_payment_donor_id'";
454
	$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...
455
456
	$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...
457
	give_set_upgrade_complete( 'upgrade_give_payment_customer_id' );
458
}
459
460
461
/**
462
 * Upgrades the Offline Status
463
 *
464
 * Reverses the issue where offline donations in "pending" status where inappropriately marked as abandoned
465
 *
466
 * @since      1.3.4
467
 */
468
function give_v134_upgrade_give_offline_status() {
469
	global $wpdb;
470
471
	/* @var Give_Updates $give_updates */
472
	$give_updates = Give_Updates::get_instance();
473
474
	// Get abandoned offline payments.
475
	$select = "SELECT ID FROM $wpdb->posts p ";
476
	$join   = "LEFT JOIN $wpdb->postmeta m ON p.ID = m.post_id ";
477
	$where  = "WHERE p.post_type = 'give_payment' ";
478
	$where .= "AND ( p.post_status = 'abandoned' )";
479
	$where .= "AND ( m.meta_key = '_give_payment_gateway' AND m.meta_value = 'offline' )";
480
481
	$sql            = $select . $join . $where;
482
	$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...
483
484
	foreach ( $found_payments as $payment ) {
485
486
		// Only change ones marked abandoned since our release last week because the admin may have marked some abandoned themselves.
487
		$modified_time = get_post_modified_time( 'U', false, $payment );
488
489
		// 1450124863 =  12/10/2015 20:42:25.
490
		if ( $modified_time >= 1450124863 ) {
491
492
			give_update_payment_status( $payment, 'pending' );
493
494
		}
495
	}
496
497
	$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...
498
	give_set_upgrade_complete( 'upgrade_give_offline_status' );
499
}
500
501
502
/**
503
 * Cleanup User Roles
504
 *
505
 * This upgrade routine removes unused roles and roles with typos
506
 *
507
 * @since      1.5.2
508
 */
509
function give_v152_cleanup_users() {
510
511
	$give_version = get_option( 'give_version' );
512
513
	if ( ! $give_version ) {
514
		// 1.0 is the first version to use this option so we must add it.
515
		$give_version = '1.0';
516
	}
517
518
	$give_version = preg_replace( '/[^0-9.].*/', '', $give_version );
519
520
	// v1.5.2 Upgrades
521
	if ( version_compare( $give_version, '1.5.2', '<' ) || ! give_has_upgrade_completed( 'upgrade_give_user_caps_cleanup' ) ) {
522
523
		// Delete all caps with "ss".
524
		// Also delete all unused "campaign" roles.
525
		$delete_caps = array(
526
			'delete_give_formss',
527
			'delete_others_give_formss',
528
			'delete_private_give_formss',
529
			'delete_published_give_formss',
530
			'read_private_forms',
531
			'edit_give_formss',
532
			'edit_others_give_formss',
533
			'edit_private_give_formss',
534
			'edit_published_give_formss',
535
			'publish_give_formss',
536
			'read_private_give_formss',
537
			'assign_give_campaigns_terms',
538
			'delete_give_campaigns',
539
			'delete_give_campaigns_terms',
540
			'delete_give_campaignss',
541
			'delete_others_give_campaignss',
542
			'delete_private_give_campaignss',
543
			'delete_published_give_campaignss',
544
			'edit_give_campaigns',
545
			'edit_give_campaigns_terms',
546
			'edit_give_campaignss',
547
			'edit_others_give_campaignss',
548
			'edit_private_give_campaignss',
549
			'edit_published_give_campaignss',
550
			'manage_give_campaigns_terms',
551
			'publish_give_campaignss',
552
			'read_give_campaigns',
553
			'read_private_give_campaignss',
554
			'view_give_campaigns_stats',
555
			'delete_give_paymentss',
556
			'delete_others_give_paymentss',
557
			'delete_private_give_paymentss',
558
			'delete_published_give_paymentss',
559
			'edit_give_paymentss',
560
			'edit_others_give_paymentss',
561
			'edit_private_give_paymentss',
562
			'edit_published_give_paymentss',
563
			'publish_give_paymentss',
564
			'read_private_give_paymentss',
565
		);
566
567
		global $wp_roles;
568
		foreach ( $delete_caps as $cap ) {
569
			foreach ( array_keys( $wp_roles->roles ) as $role ) {
570
				$wp_roles->remove_cap( $role, $cap );
571
			}
572
		}
573
574
		// Create Give plugin roles.
575
		$roles = new Give_Roles();
576
		$roles->add_roles();
577
		$roles->add_caps();
578
579
		// The Update Ran.
580
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ), false );
581
		give_set_upgrade_complete( 'upgrade_give_user_caps_cleanup' );
582
		delete_option( 'give_doing_upgrade' );
583
584
	}// End if().
585
586
}
587
588
add_action( 'admin_init', 'give_v152_cleanup_users' );
589
590
/**
591
 * 1.6 Upgrade routine to create the customer meta table.
592
 *
593
 * @since  1.6
594
 * @return void
595
 */
596
function give_v16_upgrades() {
597
	// Create the donor databases.
598
	$donors_db = new Give_DB_Donors();
599
	$donors_db->create_table();
600
	$donor_meta = new Give_DB_Donor_Meta();
601
	$donor_meta->create_table();
602
}
603
604
/**
605
 * 1.7 Upgrades.
606
 *
607
 * a. Update license api data for plugin addons.
608
 * b. Cleanup user roles.
609
 *
610
 * @since  1.7
611
 * @return void
612
 */
613
function give_v17_upgrades() {
614
	// Upgrade license data.
615
	give_v17_upgrade_addon_license_data();
616
	give_v17_cleanup_roles();
617
}
618
619
/**
620
 * Upgrade license data
621
 *
622
 * @since 1.7
623
 */
624
function give_v17_upgrade_addon_license_data() {
625
	$give_options = give_get_settings();
626
627
	$api_url = 'https://givewp.com/give-sl-api/';
628
629
	// Get addons license key.
630
	$addons = array();
631
	foreach ( $give_options as $key => $value ) {
632
		if ( false !== strpos( $key, '_license_key' ) ) {
633
			$addons[ $key ] = $value;
634
		}
635
	}
636
637
	// Bailout: We do not have any addon license data to upgrade.
638
	if ( empty( $addons ) ) {
639
		return false;
640
	}
641
642
	foreach ( $addons as $key => $addon_license ) {
643
644
		// Get addon shortname.
645
		$shortname = str_replace( '_license_key', '', $key );
646
647
		// Addon license option name.
648
		$addon_license_option_name = $shortname . '_license_active';
649
650
		// bailout if license is empty.
651
		if ( empty( $addon_license ) ) {
652
			delete_option( $addon_license_option_name );
653
			continue;
654
		}
655
656
		// Get addon name.
657
		$addon_name       = array();
658
		$addon_name_parts = explode( '_', str_replace( 'give_', '', $shortname ) );
659
		foreach ( $addon_name_parts as $name_part ) {
660
661
			// Fix addon name
662
			switch ( $name_part ) {
663
				case 'authorizenet':
664
					$name_part = 'authorize.net';
665
					break;
666
			}
667
668
			$addon_name[] = ucfirst( $name_part );
669
		}
670
671
		$addon_name = implode( ' ', $addon_name );
672
673
		// Data to send to the API.
674
		$api_params = array(
675
			'edd_action' => 'activate_license', // never change from "edd_" to "give_"!
676
			'license'    => $addon_license,
677
			'item_name'  => urlencode( $addon_name ),
678
			'url'        => home_url(),
679
		);
680
681
		// Call the API.
682
		$response = wp_remote_post(
683
			$api_url,
684
			array(
685
				'timeout'   => 15,
686
				'sslverify' => false,
687
				'body'      => $api_params,
688
			)
689
		);
690
691
		// Make sure there are no errors.
692
		if ( is_wp_error( $response ) ) {
693
			delete_option( $addon_license_option_name );
694
			continue;
695
		}
696
697
		// Tell WordPress to look for updates.
698
		set_site_transient( 'update_plugins', null );
699
700
		// Decode license data.
701
		$license_data = json_decode( wp_remote_retrieve_body( $response ) );
702
		update_option( $addon_license_option_name, $license_data, false );
703
	}// End foreach().
704
}
705
706
707
/**
708
 * Cleanup User Roles.
709
 *
710
 * This upgrade routine removes unused roles and roles with typos.
711
 *
712
 * @since      1.7
713
 */
714
function give_v17_cleanup_roles() {
715
716
	// Delete all caps with "_give_forms_" and "_give_payments_".
717
	// These roles have no usage; the proper is singular.
718
	$delete_caps = array(
719
		'view_give_forms_stats',
720
		'delete_give_forms_terms',
721
		'assign_give_forms_terms',
722
		'edit_give_forms_terms',
723
		'manage_give_forms_terms',
724
		'view_give_payments_stats',
725
		'manage_give_payments_terms',
726
		'edit_give_payments_terms',
727
		'assign_give_payments_terms',
728
		'delete_give_payments_terms',
729
	);
730
731
	global $wp_roles;
732
	foreach ( $delete_caps as $cap ) {
733
		foreach ( array_keys( $wp_roles->roles ) as $role ) {
734
			$wp_roles->remove_cap( $role, $cap );
735
		}
736
	}
737
738
	// Set roles again.
739
	$roles = new Give_Roles();
740
	$roles->add_roles();
741
	$roles->add_caps();
742
743
}
744
745
/**
746
 * 1.8 Upgrades.
747
 *
748
 * a. Upgrade checkbox settings to radio button settings.
749
 * a. Update form meta for new metabox settings.
750
 *
751
 * @since  1.8
752
 * @return void
753
 */
754
function give_v18_upgrades() {
755
	// Upgrade checkbox settings to radio button settings.
756
	give_v18_upgrades_core_setting();
757
}
758
759
/**
760
 * Upgrade core settings.
761
 *
762
 * @since  1.8
763
 * @return void
764
 */
765
function give_v18_upgrades_core_setting() {
766
	// Core settings which changes from checkbox to radio.
767
	$core_setting_names = array_merge(
768
		array_keys( give_v18_renamed_core_settings() ),
769
		array(
770
			'uninstall_on_delete',
771
			'scripts_footer',
772
			'test_mode',
773
			'email_access',
774
			'terms',
775
			'give_offline_donation_enable_billing_fields',
776
		)
777
	);
778
779
	// Bailout: If not any setting define.
780
	if ( $give_settings = get_option( 'give_settings' ) ) {
781
782
		$setting_changed = false;
783
784
		// Loop: check each setting field.
785
		foreach ( $core_setting_names as $setting_name ) {
786
			// New setting name.
787
			$new_setting_name = preg_replace( '/^(enable_|disable_)/', '', $setting_name );
788
789
			// Continue: If setting already set.
790
			if (
791
				array_key_exists( $new_setting_name, $give_settings )
792
				&& in_array( $give_settings[ $new_setting_name ], array( 'enabled', 'disabled' ) )
793
			) {
794
				continue;
795
			}
796
797
			// Set checkbox value to radio value.
798
			$give_settings[ $setting_name ] = ( ! empty( $give_settings[ $setting_name ] ) && 'on' === $give_settings[ $setting_name ] ? 'enabled' : 'disabled' );
799
800
			// @see https://github.com/WordImpress/Give/issues/1063.
801
			if ( false !== strpos( $setting_name, 'disable_' ) ) {
802
803
				$give_settings[ $new_setting_name ] = ( give_is_setting_enabled( $give_settings[ $setting_name ] ) ? 'disabled' : 'enabled' );
804
			} elseif ( false !== strpos( $setting_name, 'enable_' ) ) {
805
806
				$give_settings[ $new_setting_name ] = ( give_is_setting_enabled( $give_settings[ $setting_name ] ) ? 'enabled' : 'disabled' );
807
			}
808
809
			// Tell bot to update core setting to db.
810
			if ( ! $setting_changed ) {
811
				$setting_changed = true;
812
			}
813
		}
814
815
		// Update setting only if they changed.
816
		if ( $setting_changed ) {
817
			update_option( 'give_settings', $give_settings, false );
818
		}
819
	}// End if().
820
821
	give_set_upgrade_complete( 'v18_upgrades_core_setting' );
822
}
823
824
/**
825
 * Upgrade form metadata for new metabox settings.
826
 *
827
 * @since  1.8
828
 * @return void
829
 */
830
function give_v18_upgrades_form_metadata() {
831
	/* @var Give_Updates $give_updates */
832
	$give_updates = Give_Updates::get_instance();
833
834
	// form query
835
	$forms = new WP_Query(
836
		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(
1073
		array(
1074
			'paged'          => $give_updates->step,
1075
			'status'         => 'any',
1076
			'order'          => 'ASC',
1077
			'post_type'      => 'give_forms',
1078
			'posts_per_page' => 20,
1079
		)
1080
	);
1081
1082
	if ( $donation_forms->have_posts() ) {
1083
		$give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
1084
1085
		while ( $donation_forms->have_posts() ) {
1086
			$donation_forms->the_post();
1087
			$form_id = get_the_ID();
1088
1089
			// Remove formatting from _give_set_price.
1090
			update_post_meta(
1091
				$form_id,
1092
				'_give_set_price',
1093
				give_sanitize_amount( get_post_meta( $form_id, '_give_set_price', true ) )
1094
			);
1095
1096
			// Remove formatting from _give_custom_amount_minimum.
1097
			update_post_meta(
1098
				$form_id,
1099
				'_give_custom_amount_minimum',
1100
				give_sanitize_amount( get_post_meta( $form_id, '_give_custom_amount_minimum', true ) )
1101
			);
1102
1103
			// Bailout.
1104
			if ( 'set' === get_post_meta( $form_id, '_give_price_option', true ) ) {
1105
				continue;
1106
			}
1107
1108
			$donation_levels = get_post_meta( $form_id, '_give_donation_levels', true );
1109
1110
			if ( ! empty( $donation_levels ) ) {
1111
1112
				foreach ( $donation_levels as $index => $donation_level ) {
1113
					if ( isset( $donation_level['_give_amount'] ) ) {
1114
						$donation_levels[ $index ]['_give_amount'] = give_sanitize_amount( $donation_level['_give_amount'] );
1115
					}
1116
				}
1117
1118
				update_post_meta( $form_id, '_give_donation_levels', $donation_levels );
1119
1120
				$donation_levels_amounts = wp_list_pluck( $donation_levels, '_give_amount' );
1121
1122
				$min_amount = min( $donation_levels_amounts );
1123
				$max_amount = max( $donation_levels_amounts );
1124
1125
				// Set Minimum and Maximum amount for Multi Level Donation Forms
1126
				give_update_meta( $form_id, '_give_levels_minimum_amount', $min_amount ? give_sanitize_amount( $min_amount ) : 0 );
1127
				give_update_meta( $form_id, '_give_levels_maximum_amount', $max_amount ? give_sanitize_amount( $max_amount ) : 0 );
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
				break;
1240
1241
			// @todo: Delete this option later ( version > 2.0 ) because we need this for backward compatibility give_get_admin_notice_emails.
1242
			case 'admin_notice_emails':
1243
				$recipients = give_get_admin_notice_emails();
1244
1245
				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...
1246
					// bailout if setting already exist.
1247
					if ( array_key_exists( $setting, $all_setting ) ) {
1248
						continue;
1249
					}
1250
1251
					give_update_option( $setting, $recipients );
1252
				}
1253
				break;
1254
1255
			default:
1256
				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...
1257
				give_delete_option( $old_setting );
1258
		}
1259
	}
1260
}
1261
1262
/**
1263
 * Give version 1.8.9 upgrades
1264
 *
1265
 * @since 1.8.9
1266
 */
1267
function give_v1812_upgrades() {
1268
	/**
1269
	 * Validate number format settings.
1270
	 */
1271
	$give_settings        = give_get_settings();
1272
	$give_setting_updated = false;
1273
1274
	if ( $give_settings['thousands_separator'] === $give_settings['decimal_separator'] ) {
1275
		$give_settings['number_decimals']   = 0;
1276
		$give_settings['decimal_separator'] = '';
1277
		$give_setting_updated               = true;
1278
1279
	} elseif ( empty( $give_settings['decimal_separator'] ) ) {
1280
		$give_settings['number_decimals'] = 0;
1281
		$give_setting_updated             = true;
1282
1283
	} elseif ( 6 < absint( $give_settings['number_decimals'] ) ) {
1284
		$give_settings['number_decimals'] = 5;
1285
		$give_setting_updated             = true;
1286
	}
1287
1288
	if ( $give_setting_updated ) {
1289
		update_option( 'give_settings', $give_settings, false );
1290
	}
1291
}
1292
1293
1294
/**
1295
 * Give version 1.8.12 update
1296
 *
1297
 * Standardized amount values to six decimal
1298
 *
1299
 * @see        https://github.com/WordImpress/Give/issues/1849#issuecomment-315128602
1300
 *
1301
 * @since      1.8.12
1302
 */
1303
function give_v1812_update_amount_values_callback() {
1304
	/* @var Give_Updates $give_updates */
1305
	$give_updates = Give_Updates::get_instance();
1306
1307
	// form query.
1308
	$donation_forms = new WP_Query(
1309
		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
1374
					break;
1375
1376
				case 'give_payment':
1377
					// _give_payment_total.
1378
					if ( ! empty( $meta['_give_payment_total'][0] ) ) {
1379
						update_post_meta( $post->ID, '_give_payment_total', give_sanitize_amount_for_db( $meta['_give_payment_total'][0] ) );
1380
					}
1381
1382
					break;
1383
			}
1384
		}
1385
1386
		/* Restore original Post Data */
1387
		wp_reset_postdata();
1388
	} else {
1389
		// The Update Ran.
1390
		give_set_upgrade_complete( 'v1812_update_amount_values' );
1391
	}
1392
}
1393
1394
1395
/**
1396
 * Give version 1.8.12 update
1397
 *
1398
 * Standardized amount values to six decimal for donor
1399
 *
1400
 * @see        https://github.com/WordImpress/Give/issues/1849#issuecomment-315128602
1401
 *
1402
 * @since      1.8.12
1403
 */
1404
function give_v1812_update_donor_purchase_value_callback() {
1405
	/* @var Give_Updates $give_updates */
1406
	$give_updates = Give_Updates::get_instance();
1407
1408
	// form query.
1409
	$donors = Give()->donors->get_donors(
1410
		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(
1440
		array(
1441
			'number' => 20,
1442
			'offset' => $give_updates->get_offset( 20 ),
1443
		)
1444
	);
1445
1446
	if ( ! empty( $donors ) ) {
1447
		$give_updates->set_percentage( Give()->donors->count(), $give_updates->get_offset( 20 ) );
1448
1449
		/* @var Object $donor */
1450
		foreach ( $donors as $donor ) {
1451
			$user_id = $donor->user_id;
1452
1453
			// Proceed, if donor is attached with user.
1454
			if ( $user_id ) {
1455
				$user = get_userdata( $user_id );
1456
1457
				// Update user role, if user has subscriber role.
1458
				if ( is_array( $user->roles ) && in_array( 'subscriber', $user->roles ) ) {
1459
					wp_update_user(
1460
						array(
1461
							'ID'   => $user_id,
1462
							'role' => 'give_donor',
1463
						)
1464
					);
1465
				}
1466
			}
1467
		}
1468
	} else {
1469
		// The Update Ran.
1470
		give_set_upgrade_complete( 'v1813_update_donor_user_roles' );
1471
	}
1472
}
1473
1474
1475
/**
1476
 * Version 1.8.13 automatic updates
1477
 *
1478
 * @since 1.8.13
1479
 */
1480
function give_v1813_upgrades() {
1481
	// Update admin setting.
1482
	give_update_option( 'donor_default_user_role', 'give_donor' );
1483
1484
	// Update Give roles.
1485
	$roles = new Give_Roles();
1486
	$roles->add_roles();
1487
	$roles->add_caps();
1488
}
1489
1490
/**
1491
 * Correct currency code for "Iranian Currency" for all of the payments.
1492
 *
1493
 * @since 1.8.17
1494
 */
1495
function give_v1817_update_donation_iranian_currency_code() {
1496
	/* @var Give_Updates $give_updates */
1497
	$give_updates = Give_Updates::get_instance();
1498
1499
	// form query.
1500
	$payments = new WP_Query(
1501
		array(
1502
			'paged'          => $give_updates->step,
1503
			'status'         => 'any',
1504
			'order'          => 'ASC',
1505
			'post_type'      => array( 'give_payment' ),
1506
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1507
		)
1508
	);
1509
1510
	if ( $payments->have_posts() ) {
1511
		$give_updates->set_percentage( $payments->found_posts, ( $give_updates->step * 100 ) );
1512
1513
		while ( $payments->have_posts() ) {
1514
			$payments->the_post();
1515
1516
			$payment_meta = give_get_payment_meta( get_the_ID() );
1517
1518
			if ( 'RIAL' === $payment_meta['currency'] ) {
1519
				$payment_meta['currency'] = 'IRR';
1520
				give_update_meta( get_the_ID(), '_give_payment_meta', $payment_meta );
1521
			}
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(
1634
		array(
1635
			'paged'          => $give_updates->step,
1636
			'status'         => 'any',
1637
			'order'          => 'ASC',
1638
			'post_type'      => array( 'give_payment' ),
1639
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1640
		)
1641
	);
1642
1643
	if ( $donations->have_posts() ) {
1644
		$give_updates->set_percentage( $donations->found_posts, $give_updates->step * 100 );
1645
1646
		while ( $donations->have_posts() ) {
1647
			$donations->the_post();
1648
1649
			$form          = new Give_Donate_Form( give_get_meta( get_the_ID(), '_give_payment_form_id', true ) );
1650
			$donation_meta = give_get_payment_meta( get_the_ID() );
1651
1652
			// Update Donation meta with price_id set as custom, only if it is:
1653
			// 1. Donation Type = Set Donation.
1654
			// 2. Donation Price Id is not set to custom.
1655
			// 3. Form has not enabled custom price and donation amount assures that it is custom amount.
1656
			if (
1657
				$form->ID &&
1658
				$form->is_set_type_donation_form() &&
1659
				( 'custom' !== $donation_meta['price_id'] ) &&
1660
				$form->is_custom_price( give_get_meta( get_the_ID(), '_give_payment_total', true ) )
1661
			) {
1662
				$donation_meta['price_id'] = 'custom';
1663
				give_update_meta( get_the_ID(), '_give_payment_meta', $donation_meta );
1664
				give_update_meta( get_the_ID(), '_give_payment_price_id', 'custom' );
1665
			}
1666
		}
1667
1668
		wp_reset_postdata();
1669
	} else {
1670
		// Update Ran Successfully.
1671
		give_set_upgrade_complete( 'v1818_assign_custom_amount_set_donation' );
1672
	}
1673
}
1674
1675
/**
1676
 * Upgrade Routine - Removed Give Worker caps.
1677
 *
1678
 * See: https://github.com/WordImpress/Give/issues/2476
1679
 *
1680
 * @since 1.8.18
1681
 */
1682
function give_v1818_give_worker_role_cleanup() {
1683
1684
	/* @var Give_Updates $give_updates */
1685
	$give_updates = Give_Updates::get_instance();
1686
1687
	global $wp_roles;
1688
1689
	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...
1690
		return;
1691
	}
1692
1693
	// Remove Capabilities to user roles as required.
1694
	$remove_caps = array(
1695
		'give_worker' => array(
1696
			'delete_give_payments',
1697
			'delete_others_give_payments',
1698
			'delete_private_give_payments',
1699
			'delete_published_give_payments',
1700
			'edit_others_give_payments',
1701
			'edit_private_give_payments',
1702
			'edit_published_give_payments',
1703
			'read_private_give_payments',
1704
		),
1705
	);
1706
1707
	foreach ( $remove_caps as $role => $caps ) {
1708
		foreach ( $caps as $cap ) {
1709
			$wp_roles->remove_cap( $role, $cap );
1710
		}
1711
	}
1712
1713
	$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...
1714
1715
	// Create Give plugin roles.
1716
	$roles = new Give_Roles();
1717
	$roles->add_roles();
1718
	$roles->add_caps();
1719
1720
	give_set_upgrade_complete( 'v1818_give_worker_role_cleanup' );
1721
}
1722
1723
/**
1724
 *
1725
 * Upgrade form metadata for new metabox settings.
1726
 *
1727
 * @since  2.0
1728
 * @return void
1729
 */
1730
function give_v20_upgrades_form_metadata_callback() {
1731
	$give_updates = Give_Updates::get_instance();
1732
1733
	// form query
1734
	$forms = new WP_Query(
1735
		array(
1736
			'paged'          => $give_updates->step,
1737
			'status'         => 'any',
1738
			'order'          => 'ASC',
1739
			'post_type'      => 'give_forms',
1740
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1741
		)
1742
	);
1743
1744
	if ( $forms->have_posts() ) {
1745
		$give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 100 ) );
1746
1747
		while ( $forms->have_posts() ) {
1748
			$forms->the_post();
1749
			global $post;
1750
1751
			// Update offline instruction email notification status.
1752
			$offline_instruction_notification_status = get_post_meta( get_the_ID(), '_give_customize_offline_donations', true );
1753
			$offline_instruction_notification_status = give_is_setting_enabled(
1754
				$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...
1755
					'enabled',
1756
					'global',
1757
				)
1758
			)
1759
				? $offline_instruction_notification_status
1760
				: 'global';
1761
			update_post_meta( get_the_ID(), '_give_offline-donation-instruction_notification', $offline_instruction_notification_status );
1762
1763
			// Update offline instruction email message.
1764
			update_post_meta(
1765
				get_the_ID(),
1766
				'_give_offline-donation-instruction_email_message',
1767
				get_post_meta(
1768
					get_the_ID(),
1769
					// @todo: Delete this option later ( version > 2.0 ).
1770
					'_give_offline_donation_email',
1771
					true
1772
				)
1773
			);
1774
1775
			// Update offline instruction email subject.
1776
			update_post_meta(
1777
				get_the_ID(),
1778
				'_give_offline-donation-instruction_email_subject',
1779
				get_post_meta(
1780
					get_the_ID(),
1781
					// @todo: Delete this option later ( version > 2.0 ).
1782
					'_give_offline_donation_subject',
1783
					true
1784
				)
1785
			);
1786
1787
		}// End while().
1788
1789
		wp_reset_postdata();
1790
	} else {
1791
		// No more forms found, finish up.
1792
		give_set_upgrade_complete( 'v20_upgrades_form_metadata' );
1793
	}
1794
}
1795
1796
1797
/**
1798
 * Upgrade payment metadata for new metabox settings.
1799
 *
1800
 * @since  2.0
1801
 * @global wpdb $wpdb
1802
 * @return void
1803
 */
1804
function give_v20_upgrades_payment_metadata_callback() {
1805
	global $wpdb;
1806
	$give_updates = Give_Updates::get_instance();
1807
1808
	// form query
1809
	$forms = new WP_Query(
1810
		array(
1811
			'paged'          => $give_updates->step,
1812
			'status'         => 'any',
1813
			'order'          => 'ASC',
1814
			'post_type'      => 'give_payment',
1815
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1816
		)
1817
	);
1818
1819
	if ( $forms->have_posts() ) {
1820
		$give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 100 ) );
1821
1822
		while ( $forms->have_posts() ) {
1823
			$forms->the_post();
1824
			global $post;
1825
1826
			// Split _give_payment_meta meta.
1827
			// @todo Remove _give_payment_meta after releases 2.0
1828
			$payment_meta = give_get_meta( $post->ID, '_give_payment_meta', true );
1829
1830
			if ( ! empty( $payment_meta ) ) {
1831
				_give_20_bc_split_and_save_give_payment_meta( $post->ID, $payment_meta );
1832
			}
1833
1834
			$deprecated_meta_keys = array(
1835
				'_give_payment_customer_id' => '_give_payment_donor_id',
1836
				'_give_payment_user_email'  => '_give_payment_donor_email',
1837
				'_give_payment_user_ip'     => '_give_payment_donor_ip',
1838
			);
1839
1840 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...
1841
				// Do not add new meta key if already exist.
1842
				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...
1843
					continue;
1844
				}
1845
1846
				$wpdb->insert(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
1847
					$wpdb->postmeta,
1848
					array(
1849
						'post_id'    => $post->ID,
1850
						'meta_key'   => $new_meta_key,
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
1851
						'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...
1852
					)
1853
				);
1854
			}
1855
1856
			// Bailout
1857 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...
1858
				/* @var Give_Donor $donor */
1859
				$donor = new Give_Donor( $donor_id );
1860
1861
				$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...
1862
				$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...
1863
				$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...
1864
				$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...
1865
				$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...
1866
				$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...
1867
1868
				// Save address.
1869
				$donor->add_address( 'billing[]', $address );
1870
			}
1871
		}// End while().
1872
1873
		wp_reset_postdata();
1874
	} else {
1875
		// @todo Delete user id meta after releases 2.0
1876
		// $wpdb->get_var( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key=%s", '_give_payment_user_id' ) );
1877
		// No more forms found, finish up.
1878
		give_set_upgrade_complete( 'v20_upgrades_payment_metadata' );
1879
	}
1880
}
1881
1882
1883
/**
1884
 * Upgrade logs data.
1885
 *
1886
 * @since  2.0
1887
 * @return void
1888
 */
1889
function give_v20_logs_upgrades_callback() {
1890
	global $wpdb;
1891
	$give_updates = Give_Updates::get_instance();
1892
1893
	// form query
1894
	$forms = new WP_Query(
1895
		array(
1896
			'paged'          => $give_updates->step,
1897
			'order'          => 'DESC',
1898
			'post_type'      => 'give_log',
1899
			'post_status'    => 'any',
1900
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1901
		)
1902
	);
1903
1904
	if ( $forms->have_posts() ) {
1905
		$give_updates->set_percentage( $forms->found_posts, $give_updates->step * 100 );
1906
1907
		while ( $forms->have_posts() ) {
1908
			$forms->the_post();
1909
			global $post;
1910
1911
			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...
1912
				continue;
1913
			}
1914
1915
			$term      = get_the_terms( $post->ID, 'give_log_type' );
1916
			$term      = ! is_wp_error( $term ) && ! empty( $term ) ? $term[0] : array();
1917
			$term_name = ! empty( $term ) ? $term->slug : '';
1918
1919
			$log_data = array(
1920
				'ID'           => $post->ID,
1921
				'log_title'    => $post->post_title,
1922
				'log_content'  => $post->post_content,
1923
				'log_parent'   => 0,
1924
				'log_type'     => $term_name,
1925
				'log_date'     => $post->post_date,
1926
				'log_date_gmt' => $post->post_date_gmt,
1927
			);
1928
			$log_meta = array();
1929
1930 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...
1931
				foreach ( $old_log_meta as $meta_key => $meta_value ) {
1932
					switch ( $meta_key ) {
1933
						case '_give_log_payment_id':
1934
							$log_data['log_parent']        = current( $meta_value );
1935
							$log_meta['_give_log_form_id'] = $post->post_parent;
1936
							break;
1937
1938
						default:
1939
							$log_meta[ $meta_key ] = current( $meta_value );
1940
					}
1941
				}
1942
			}
1943
1944
			if ( 'api_request' === $term_name ) {
1945
				$log_meta['_give_log_api_query'] = $post->post_excerpt;
1946
			}
1947
1948
			$wpdb->insert( "{$wpdb->prefix}give_logs", $log_data );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
1949
1950 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...
1951
				foreach ( $log_meta as $meta_key => $meta_value ) {
1952
					Give()->logs->logmeta_db->update_meta( $post->ID, $meta_key, $meta_value );
1953
				}
1954
			}
1955
1956
			$logIDs[] = $post->ID;
1957
		}// End while().
1958
1959
		wp_reset_postdata();
1960
	} else {
1961
		// @todo: Delete terms and taxonomy after releases 2.0.
1962
		/*
1963
		$terms = get_terms( 'give_log_type', array( 'fields' => 'ids', 'hide_empty' => false ) );
1964
		if ( ! empty( $terms ) ) {
1965
			foreach ( $terms as $term ) {
1966
				wp_delete_term( $term, 'give_log_type' );
1967
			}
1968
		}*/
1969
1970
		// @todo: Delete logs after releases 2.0.
1971
		/*
1972
		$logIDs = get_posts( array(
1973
				'order'          => 'DESC',
1974
				'post_type'      => 'give_log',
1975
				'post_status'    => 'any',
1976
				'posts_per_page' => - 1,
1977
				'fields'         => 'ids',
1978
			)
1979
		);*/
1980
1981
		/*
1982
		if ( ! empty( $logIDs ) ) {
1983
			foreach ( $logIDs as $log ) {
1984
				// Delete term relationship and posts.
1985
				wp_delete_object_term_relationships( $log, 'give_log_type' );
1986
				wp_delete_post( $log, true );
1987
			}
1988
		}*/
1989
1990
		// @todo: Unregister taxonomy after releases 2.0.
1991
		/*unregister_taxonomy( 'give_log_type' );*/
1992
1993
		// Delete log cache.
1994
		Give()->logs->delete_cache();
1995
1996
		// No more forms found, finish up.
1997
		give_set_upgrade_complete( 'v20_logs_upgrades' );
1998
	}
1999
}
2000
2001
2002
/**
2003
 * Move payment and form metadata to new table
2004
 *
2005
 * @since  2.0
2006
 * @return void
2007
 */
2008
function give_v20_move_metadata_into_new_table_callback() {
2009
	global $wpdb;
2010
	$give_updates = Give_Updates::get_instance();
2011
2012
	// form query
2013
	$payments = new WP_Query(
2014
		array(
2015
			'paged'          => $give_updates->step,
2016
			'status'         => 'any',
2017
			'order'          => 'ASC',
2018
			'post_type'      => array( 'give_forms', 'give_payment' ),
2019
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
2020
		)
2021
	);
2022
2023
	if ( $payments->have_posts() ) {
2024
		$give_updates->set_percentage( $payments->found_posts, $give_updates->step * 100 );
2025
2026
		while ( $payments->have_posts() ) {
2027
			$payments->the_post();
2028
			global $post;
2029
2030
			$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...
2031
				$wpdb->prepare(
2032
					"SELECT * FROM $wpdb->postmeta where post_id=%d",
2033
					get_the_ID()
2034
				),
2035
				ARRAY_A
2036
			);
2037
2038 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...
2039
				foreach ( $meta_data as $index => $data ) {
2040
					// Check for duplicate meta values.
2041
					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...
2042
						continue;
2043
					}
2044
2045
					switch ( $post->post_type ) {
2046
						case 'give_forms':
2047
							$data['form_id'] = $data['post_id'];
2048
							unset( $data['post_id'] );
2049
2050
							Give()->form_meta->insert( $data );
2051
							// @todo: delete form meta from post meta table after releases 2.0.
2052
							/*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2053
2054
							break;
2055
2056
						case 'give_payment':
2057
							$data['payment_id'] = $data['post_id'];
2058
							unset( $data['post_id'] );
2059
2060
							Give()->payment_meta->insert( $data );
2061
2062
							// @todo: delete donation meta from post meta table after releases 2.0.
2063
							/*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2064
2065
							break;
2066
					}
2067
				}
2068
			}
2069
		}// End while().
2070
2071
		wp_reset_postdata();
2072
	} else {
2073
		// No more forms found, finish up.
2074
		give_set_upgrade_complete( 'v20_move_metadata_into_new_table' );
2075
	}
2076
2077
}
2078
2079
/**
2080
 * Upgrade routine for splitting donor name into first name and last name.
2081
 *
2082
 * @since 2.0
2083
 *
2084
 * @return void
2085
 */
2086
function give_v20_upgrades_donor_name() {
2087
	/* @var Give_Updates $give_updates */
2088
	$give_updates = Give_Updates::get_instance();
2089
2090
	$donors = Give()->donors->get_donors(
2091
		array(
2092
			'paged'  => $give_updates->step,
2093
			'number' => 100,
2094
		)
2095
	);
2096
2097
	if ( $donors ) {
2098
		$give_updates->set_percentage( count( $donors ), $give_updates->step * 100 );
2099
		// Loop through Donors
2100
		foreach ( $donors as $donor ) {
2101
2102
			$donor_name       = explode( ' ', $donor->name, 2 );
2103
			$donor_first_name = Give()->donor_meta->get_meta( $donor->id, '_give_donor_first_name' );
2104
			$donor_last_name  = Give()->donor_meta->get_meta( $donor->id, '_give_donor_last_name' );
2105
2106
			// If first name meta of donor is not created, then create it.
2107 View Code Duplication
			if ( ! $donor_first_name && isset( $donor_name[0] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2108
				Give()->donor_meta->add_meta( $donor->id, '_give_donor_first_name', $donor_name[0] );
2109
			}
2110
2111
			// If last name meta of donor is not created, then create it.
2112 View Code Duplication
			if ( ! $donor_last_name && isset( $donor_name[1] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2113
				Give()->donor_meta->add_meta( $donor->id, '_give_donor_last_name', $donor_name[1] );
2114
			}
2115
2116
			// If Donor is connected with WP User then update user meta.
2117 View Code Duplication
			if ( $donor->user_id ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2118
				if ( isset( $donor_name[0] ) ) {
2119
					update_user_meta( $donor->user_id, 'first_name', $donor_name[0] );
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
2120
				}
2121
				if ( isset( $donor_name[1] ) ) {
2122
					update_user_meta( $donor->user_id, 'last_name', $donor_name[1] );
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
2123
				}
2124
			}
2125
		}
2126
	} else {
2127
		// The Update Ran.
2128
		give_set_upgrade_complete( 'v20_upgrades_donor_name' );
2129
	}
2130
2131
}
2132
2133
/**
2134
 * Upgrade routine for user addresses.
2135
 *
2136
 * @since 2.0
2137
 * @global wpdb $wpdb
2138
 *
2139
 * @return void
2140
 */
2141
function give_v20_upgrades_user_address() {
2142
	global $wpdb;
2143
2144
	/* @var Give_Updates $give_updates */
2145
	$give_updates = Give_Updates::get_instance();
2146
2147
	/* @var WP_User_Query $user_query */
2148
	$user_query = new WP_User_Query(
2149
		array(
2150
			'number' => 100,
2151
			'paged'  => $give_updates->step,
2152
		)
2153
	);
2154
2155
	$users = $user_query->get_results();
2156
2157
	if ( $users ) {
2158
		$give_updates->set_percentage( $user_query->get_total(), $give_updates->step * 100 );
2159
2160
		// Loop through Donors
2161
		foreach ( $users as $user ) {
2162
			/* @var Give_Donor $donor */
2163
			$donor = new Give_Donor( $user->ID, true );
2164
2165
			if ( ! $donor->id ) {
2166
				continue;
2167
			}
2168
2169
			$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...
2170
				$wpdb->prepare(
2171
					"
2172
					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...
2173
					WHERE user_id=%s
2174
					AND meta_key=%s
2175
					",
2176
					$user->ID,
2177
					'_give_user_address'
2178
				)
2179
			);
2180
2181 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...
2182
				$address = maybe_unserialize( $address );
2183
				$donor->add_address( 'personal', $address );
2184
				$donor->add_address( 'billing[]', $address );
2185
2186
				// @todo: delete _give_user_address from user meta after releases 2.0.
2187
				/*delete_user_meta( $user->ID, '_give_user_address' );*/
2188
			}
2189
		}
2190
	} else {
2191
		// The Update Ran.
2192
		give_set_upgrade_complete( 'v20_upgrades_user_address' );
2193
	}
2194
2195
}
2196
2197
/**
2198
 * Upgrade logs data.
2199
 *
2200
 * @since  2.0
2201
 * @global wpdb $wpdb
2202
 * @return void
2203
 */
2204
function give_v20_rename_donor_tables_callback() {
2205
	global $wpdb;
2206
2207
	/* @var Give_Updates $give_updates */
2208
	$give_updates = Give_Updates::get_instance();
2209
2210
	$tables = array(
2211
		"{$wpdb->prefix}give_customers"    => "{$wpdb->prefix}give_donors",
2212
		"{$wpdb->prefix}give_customermeta" => "{$wpdb->prefix}give_donormeta",
2213
	);
2214
2215
	// Alter customer table
2216
	foreach ( $tables as $old_table => $new_table ) {
2217
		if (
2218
			$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...
2219
			! $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...
2220
		) {
2221
			$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...
2222
2223
			if ( "{$wpdb->prefix}give_donormeta" === $new_table ) {
2224
				$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...
2225
			}
2226
		}
2227
	}
2228
2229
	$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...
2230
2231
	// No more forms found, finish up.
2232
	give_set_upgrade_complete( 'v20_rename_donor_tables' );
2233
2234
	// Re initiate donor classes.
2235
	Give()->donors     = new Give_DB_Donors();
2236
	Give()->donor_meta = new Give_DB_Donor_Meta();
2237
}
2238
2239
2240
/**
2241
 * Create missing meta tables.
2242
 *
2243
 * @since  2.0.1
2244
 * @global wpdb $wpdb
2245
 * @return void
2246
 */
2247
function give_v201_create_tables() {
2248
	global $wpdb;
2249
2250
	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...
2251
		Give()->payment_meta->create_table();
2252
	}
2253
2254
	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...
2255
		Give()->form_meta->create_table();
2256
	}
2257
2258
	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...
2259
		Give()->logs->log_db->create_table();
2260
	}
2261
2262
	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...
2263
		Give()->logs->logmeta_db->create_table();
2264
	}
2265
}
2266
2267
/**
2268
 * Upgrade payment metadata for new metabox settings.
2269
 *
2270
 * @since  2.0.1
2271
 * @global wpdb $wpdb
2272
 * @return void
2273
 */
2274
function give_v201_upgrades_payment_metadata_callback() {
2275
	global $wpdb, $post;
2276
	$give_updates = Give_Updates::get_instance();
2277
	give_v201_create_tables();
2278
2279
	$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...
2280
		"
2281
			SELECT ID FROM $wpdb->posts
2282
			WHERE 1=1
2283
			AND ( 
2284
  				$wpdb->posts.post_date >= '2018-01-08 00:00:00'
2285
			)
2286
			AND $wpdb->posts.post_type = 'give_payment'
2287
			AND {$wpdb->posts}.post_status IN ('" . implode( "','", array_keys( give_get_payment_statuses() ) ) . "')
2288
			ORDER BY $wpdb->posts.post_date ASC 
2289
			LIMIT 100
2290
			OFFSET " . $give_updates->get_offset( 100 )
2291
	);
2292
2293
	if ( ! empty( $payments ) ) {
2294
		$give_updates->set_percentage( give_get_total_post_type_count( 'give_payment' ), ( $give_updates->step * 100 ) );
2295
2296
		foreach ( $payments as $payment_id ) {
2297
			$post = get_post( $payment_id );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
2298
			setup_postdata( $post );
2299
2300
			// Do not add new meta keys if already refactored.
2301
			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...
2302
				continue;
2303
			}
2304
2305
			// Split _give_payment_meta meta.
2306
			// @todo Remove _give_payment_meta after releases 2.0
2307
			$payment_meta = give_get_meta( $post->ID, '_give_payment_meta', true );
2308
2309
			if ( ! empty( $payment_meta ) ) {
2310
				_give_20_bc_split_and_save_give_payment_meta( $post->ID, $payment_meta );
2311
			}
2312
2313
			$deprecated_meta_keys = array(
2314
				'_give_payment_customer_id' => '_give_payment_donor_id',
2315
				'_give_payment_user_email'  => '_give_payment_donor_email',
2316
				'_give_payment_user_ip'     => '_give_payment_donor_ip',
2317
			);
2318
2319 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...
2320
				// Do not add new meta key if already exist.
2321
				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...
2322
					continue;
2323
				}
2324
2325
				$wpdb->insert(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
2326
					$wpdb->postmeta,
2327
					array(
2328
						'post_id'    => $post->ID,
2329
						'meta_key'   => $new_meta_key,
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
2330
						'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...
2331
					)
2332
				);
2333
			}
2334
2335
			// Bailout
2336 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...
2337
				/* @var Give_Donor $donor */
2338
				$donor = new Give_Donor( $donor_id );
2339
2340
				$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...
2341
				$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...
2342
				$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...
2343
				$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...
2344
				$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...
2345
				$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...
2346
2347
				// Save address.
2348
				$donor->add_address( 'billing[]', $address );
2349
			}
2350
		}// End while().
2351
2352
		wp_reset_postdata();
2353
	} else {
2354
		// @todo Delete user id meta after releases 2.0
2355
		// $wpdb->get_var( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key=%s", '_give_payment_user_id' ) );
2356
		// No more forms found, finish up.
2357
		give_set_upgrade_complete( 'v201_upgrades_payment_metadata' );
2358
	}
2359
}
2360
2361
/**
2362
 * Move payment and form metadata to new table
2363
 *
2364
 * @since  2.0.1
2365
 * @return void
2366
 */
2367
function give_v201_move_metadata_into_new_table_callback() {
2368
	global $wpdb, $post;
2369
	$give_updates = Give_Updates::get_instance();
2370
	give_v201_create_tables();
2371
2372
	$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...
2373
		"
2374
			SELECT ID FROM $wpdb->posts 
2375
			WHERE 1=1
2376
			AND ( $wpdb->posts.post_type = 'give_payment' OR $wpdb->posts.post_type = 'give_forms' )
2377
			AND {$wpdb->posts}.post_status IN ('" . implode( "','", array_keys( give_get_payment_statuses() ) ) . "')
2378
			ORDER BY $wpdb->posts.post_date ASC 
2379
			LIMIT 100
2380
			OFFSET " . $give_updates->get_offset( 100 )
2381
	);
2382
2383
	if ( ! empty( $payments ) ) {
2384
		$give_updates->set_percentage(
2385
			give_get_total_post_type_count(
2386
				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...
2387
					'give_forms',
2388
					'give_payment',
2389
				)
2390
			), $give_updates->step * 100
2391
		);
2392
2393
		foreach ( $payments as $payment_id ) {
2394
			$post = get_post( $payment_id );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
2395
			setup_postdata( $post );
2396
2397
			$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...
2398
				$wpdb->prepare(
2399
					"SELECT * FROM $wpdb->postmeta where post_id=%d",
2400
					get_the_ID()
2401
				),
2402
				ARRAY_A
2403
			);
2404
2405 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...
2406
				foreach ( $meta_data as $index => $data ) {
2407
					// Check for duplicate meta values.
2408
					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...
2409
						continue;
2410
					}
2411
2412
					switch ( $post->post_type ) {
2413
						case 'give_forms':
2414
							$data['form_id'] = $data['post_id'];
2415
							unset( $data['post_id'] );
2416
2417
							Give()->form_meta->insert( $data );
2418
							// @todo: delete form meta from post meta table after releases 2.0.
2419
							/*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2420
2421
							break;
2422
2423
						case 'give_payment':
2424
							$data['payment_id'] = $data['post_id'];
2425
							unset( $data['post_id'] );
2426
2427
							Give()->payment_meta->insert( $data );
2428
2429
							// @todo: delete donation meta from post meta table after releases 2.0.
2430
							/*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2431
2432
							break;
2433
					}
2434
				}
2435
			}
2436
		}// End while().
2437
2438
		wp_reset_postdata();
2439
	} else {
2440
		// No more forms found, finish up.
2441
		give_set_upgrade_complete( 'v201_move_metadata_into_new_table' );
2442
	}
2443
2444
}
2445
2446
/**
2447
 * Move data to new log table.
2448
 *
2449
 * @since  2.0.1
2450
 * @return void
2451
 */
2452
function give_v201_logs_upgrades_callback() {
2453
	global $wpdb, $post;
2454
	$give_updates = Give_Updates::get_instance();
2455
	give_v201_create_tables();
2456
2457
	$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...
2458
		"
2459
			SELECT ID FROM $wpdb->posts 
2460
			WHERE 1=1
2461
			AND $wpdb->posts.post_type = 'give_log'
2462
			AND {$wpdb->posts}.post_status IN ('" . implode( "','", array_keys( give_get_payment_statuses() ) ) . "')
2463
			ORDER BY $wpdb->posts.post_date ASC 
2464
			LIMIT 100
2465
			OFFSET " . $give_updates->get_offset( 100 )
2466
	);
2467
2468
	if ( ! empty( $logs ) ) {
2469
		$give_updates->set_percentage( give_get_total_post_type_count( 'give_log' ), $give_updates->step * 100 );
2470
2471
		foreach ( $logs as $log_id ) {
2472
			$post = get_post( $log_id );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
2473
			setup_postdata( $post );
2474
2475
			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...
2476
				continue;
2477
			}
2478
2479
			$term      = get_the_terms( $post->ID, 'give_log_type' );
2480
			$term      = ! is_wp_error( $term ) && ! empty( $term ) ? $term[0] : array();
2481
			$term_name = ! empty( $term ) ? $term->slug : '';
2482
2483
			$log_data = array(
2484
				'ID'           => $post->ID,
2485
				'log_title'    => $post->post_title,
2486
				'log_content'  => $post->post_content,
2487
				'log_parent'   => 0,
2488
				'log_type'     => $term_name,
2489
				'log_date'     => $post->post_date,
2490
				'log_date_gmt' => $post->post_date_gmt,
2491
			);
2492
			$log_meta = array();
2493
2494 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...
2495
				foreach ( $old_log_meta as $meta_key => $meta_value ) {
2496
					switch ( $meta_key ) {
2497
						case '_give_log_payment_id':
2498
							$log_data['log_parent']        = current( $meta_value );
2499
							$log_meta['_give_log_form_id'] = $post->post_parent;
2500
							break;
2501
2502
						default:
2503
							$log_meta[ $meta_key ] = current( $meta_value );
2504
					}
2505
				}
2506
			}
2507
2508
			if ( 'api_request' === $term_name ) {
2509
				$log_meta['_give_log_api_query'] = $post->post_excerpt;
2510
			}
2511
2512
			$wpdb->insert( "{$wpdb->prefix}give_logs", $log_data );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
2513
2514 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...
2515
				foreach ( $log_meta as $meta_key => $meta_value ) {
2516
					Give()->logs->logmeta_db->update_meta( $post->ID, $meta_key, $meta_value );
2517
				}
2518
			}
2519
2520
			$logIDs[] = $post->ID;
2521
		}// End while().
2522
2523
		wp_reset_postdata();
2524
	} else {
2525
		// Delete log cache.
2526
		Give()->logs->delete_cache();
2527
2528
		// No more forms found, finish up.
2529
		give_set_upgrade_complete( 'v201_logs_upgrades' );
2530
	}
2531
}
2532
2533
2534
/**
2535
 * Add missing donor.
2536
 *
2537
 * @since  2.0.1
2538
 * @return void
2539
 */
2540
function give_v201_add_missing_donors_callback() {
2541
	global $wpdb;
2542
	give_v201_create_tables();
2543
2544
	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...
2545
		$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...
2546
		$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...
2547
		$donor_data = array();
2548
2549
		if ( $missing_donors = array_diff( $customers, $donors ) ) {
2550
			foreach ( $missing_donors as $donor_id ) {
2551
				$donor_data[] = array(
2552
					'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...
2553
					'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...
2554
2555
				);
2556
			}
2557
		}
2558
2559
		if ( ! empty( $donor_data ) ) {
2560
			$donor_table_name      = Give()->donors->table_name;
2561
			$donor_meta_table_name = Give()->donor_meta->table_name;
2562
2563
			Give()->donors->table_name     = "{$wpdb->prefix}give_donors";
2564
			Give()->donor_meta->table_name = "{$wpdb->prefix}give_donormeta";
2565
2566
			foreach ( $donor_data as $donor ) {
2567
				$donor['info'][0] = (array) $donor['info'][0];
2568
2569
				// Prevent duplicate meta id issue.
2570
				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...
2571
					continue;
2572
				}
2573
2574
				$donor_id = Give()->donors->add( $donor['info'][0] );
2575
2576
				if ( ! empty( $donor['meta'] ) ) {
2577
					foreach ( $donor['meta'] as $donor_meta ) {
2578
						$donor_meta = (array) $donor_meta;
2579
2580
						// Prevent duplicate meta id issue.
2581
						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...
2582
							unset( $donor_meta['meta_id'] );
2583
						}
2584
2585
						$donor_meta['donor_id'] = $donor_meta['customer_id'];
2586
						unset( $donor_meta['customer_id'] );
2587
2588
						Give()->donor_meta->insert( $donor_meta );
2589
					}
2590
				}
2591
2592
				/**
2593
				 * Fix donor name and address
2594
				 */
2595
				$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...
2596
					$wpdb->prepare(
2597
						"
2598
					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...
2599
					WHERE user_id=%s
2600
					AND meta_key=%s
2601
					",
2602
						$donor['info'][0]['user_id'],
2603
						'_give_user_address'
2604
					)
2605
				);
2606
2607
				$donor = new Give_Donor( $donor_id );
2608
2609 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...
2610
					$address = maybe_unserialize( $address );
2611
					$donor->add_address( 'personal', $address );
2612
					$donor->add_address( 'billing[]', $address );
2613
				}
2614
2615
				$donor_name       = explode( ' ', $donor->name, 2 );
2616
				$donor_first_name = Give()->donor_meta->get_meta( $donor->id, '_give_donor_first_name' );
2617
				$donor_last_name  = Give()->donor_meta->get_meta( $donor->id, '_give_donor_last_name' );
2618
2619
				// If first name meta of donor is not created, then create it.
2620 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...
2621
					Give()->donor_meta->add_meta( $donor->id, '_give_donor_first_name', $donor_name[0] );
2622
				}
2623
2624
				// If last name meta of donor is not created, then create it.
2625 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...
2626
					Give()->donor_meta->add_meta( $donor->id, '_give_donor_last_name', $donor_name[1] );
2627
				}
2628
2629
				// If Donor is connected with WP User then update user meta.
2630 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...
2631
					if ( isset( $donor_name[0] ) ) {
2632
						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...
2633
					}
2634
					if ( isset( $donor_name[1] ) ) {
2635
						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...
2636
					}
2637
				}
2638
			}
2639
2640
			Give()->donors->table_name     = $donor_table_name;
2641
			Give()->donor_meta->table_name = $donor_meta_table_name;
2642
		}
2643
	}
2644
2645
	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...
2646
	give_set_upgrade_complete( 'v201_add_missing_donors' );
2647
}
2648
2649
2650
/**
2651
 * Version 2.0.3 automatic updates
2652
 *
2653
 * @since 2.0.3
2654
 */
2655
function give_v203_upgrades() {
2656
	global $wpdb;
2657
2658
	// Do not auto load option.
2659
	$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...
2660
2661
	// Remove from cache.
2662
	$all_options = wp_load_alloptions();
2663
2664
	if ( isset( $all_options['give_completed_upgrades'] ) ) {
2665
		unset( $all_options['give_completed_upgrades'] );
2666
		wp_cache_set( 'alloptions', $all_options, 'options' );
2667
	}
2668
2669
}
2670
2671
2672
/**
2673
 * Version 2.2.0 automatic updates
2674
 *
2675
 * @since 2.2.0
2676
 */
2677
function give_v220_upgrades() {
2678
	global $wpdb;
2679
2680
	/**
2681
	 * Update 1
2682
	 *
2683
	 * Delete wp session data
2684
	 */
2685
	give_v220_delete_wp_session_data();
2686
2687
	/**
2688
	 * Update 2
2689
	 *
2690
	 * Rename payment table
2691
	 */
2692
	give_v220_rename_donation_meta_type_callback();
2693
2694
	/**
2695
	 * Update 2
2696
	 *
2697
	 * Set autoload to no to reduce result weight from WordPress query
2698
	 */
2699
2700
	$options = array(
2701
		'give_settings',
2702
		'give_version',
2703
		'give_version_upgraded_from',
2704
		'give_default_api_version',
2705
		'give_site_address_before_migrate',
2706
		'_give_table_check',
2707
		'give_recently_activated_addons',
2708
		'give_is_addon_activated',
2709
		'give_last_paypal_ipn_received',
2710
		'give_use_php_sessions',
2711
		'give_subscriptions',
2712
		'_give_subscriptions_edit_last',
2713
	);
2714
2715
	// Add all table version option name
2716
	// Add banner option *_active_by_user
2717
	$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...
2718
		"
2719
		SELECT option_name
2720
		FROM $wpdb->options
2721
		WHERE option_name like '%give%'
2722
		AND (
2723
			option_name like '%_db_version%'
2724
			OR option_name like '%_active_by_user%'
2725
			OR option_name like '%_license_active%'
2726
		)
2727
		"
2728
	);
2729
2730
	if ( ! empty( $option_like ) ) {
2731
		$options = array_merge( $options, $option_like );
2732
	}
2733
2734
	$options_str = '\'' . implode( "','", $options ) . '\'';
2735
2736
	$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...
2737
		"
2738
		UPDATE $wpdb->options
2739
		SET autoload = 'no'
2740
		WHERE option_name IN ( {$options_str} )
2741
		"
2742
	);
2743
}
2744
2745
/**
2746
 * Version 2.2.1 automatic updates
2747
 *
2748
 * @since 2.2.1
2749
 */
2750
function give_v221_upgrades() {
2751
	global $wpdb;
2752
2753
	/**
2754
	 * Update  1
2755
	 *
2756
	 * Change column length
2757
	 */
2758
	$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...
2759
}
2760
2761
/**
2762
 * Upgrade routine for 2.1 to set form closed status for all the donation forms.
2763
 *
2764
 * @since 2.1
2765
 */
2766
function give_v210_verify_form_status_upgrades_callback() {
2767
2768
	$give_updates = Give_Updates::get_instance();
2769
2770
	// form query.
2771
	$donation_forms = new WP_Query(
2772
		array(
2773
			'paged'          => $give_updates->step,
2774
			'status'         => 'any',
2775
			'order'          => 'ASC',
2776
			'post_type'      => 'give_forms',
2777
			'posts_per_page' => 20,
2778
		)
2779
	);
2780
2781
	if ( $donation_forms->have_posts() ) {
2782
		$give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
2783
2784
		while ( $donation_forms->have_posts() ) {
2785
			$donation_forms->the_post();
2786
			$form_id = get_the_ID();
2787
2788
			$form_closed_status = give_get_meta( $form_id, '_give_form_status', true );
2789
			if ( empty( $form_closed_status ) ) {
2790
				give_set_form_closed_status( $form_id );
2791
			}
2792
		}
2793
2794
		/* Restore original Post Data */
2795
		wp_reset_postdata();
2796
2797
	} else {
2798
2799
		// The Update Ran.
2800
		give_set_upgrade_complete( 'v210_verify_form_status_upgrades' );
2801
	}
2802
}
2803
2804
/**
2805
 * Upgrade routine for 2.1.3 to delete meta which is not attach to any donation.
2806
 *
2807
 * @since 2.1
2808
 */
2809
function give_v213_delete_donation_meta_callback() {
2810
	global $wpdb;
2811
	$give_updates        = Give_Updates::get_instance();
2812
	$donation_meta_table = Give()->payment_meta->table_name;
2813
2814
	$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...
2815
		"
2816
		SELECT DISTINCT payment_id
2817
		FROM {$donation_meta_table}
2818
		LIMIT 20
2819
		OFFSET {$give_updates->get_offset( 20 )}
2820
		"
2821
	);
2822
2823
	if ( ! empty( $donations ) ) {
2824
		foreach ( $donations as $donation ) {
2825
			$donation_obj = get_post( $donation );
2826
2827
			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...
2828
				Give()->payment_meta->delete_all_meta( $donation );
2829
			}
2830
		}
2831
	} else {
2832
2833
		// The Update Ran.
2834
		give_set_upgrade_complete( 'v213_delete_donation_meta' );
2835
	}
2836
}
2837
2838
/**
2839
 * Rename donation meta type
2840
 *
2841
 * @see https://github.com/restrictcontentpro/restrict-content-pro/issues/1656
2842
 *
2843
 * @since 2.2.0
2844
 */
2845
function give_v220_rename_donation_meta_type_callback() {
2846
	global $wpdb;
2847
2848
	// Check upgrade before running.
2849
	if (
2850
		give_has_upgrade_completed( 'v220_rename_donation_meta_type' )
2851
		|| ! $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...
2852
	) {
2853
		return;
2854
	}
2855
2856
	$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...
2857
	$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...
2858
2859
	give_set_upgrade_complete( 'v220_rename_donation_meta_type' );
2860
}
2861
2862
/**
2863
 * Adds 'view_give_payments' capability to 'give_manager' user role.
2864
 *
2865
 * @since 2.1.5
2866
 */
2867
function give_v215_update_donor_user_roles_callback() {
2868
2869
	$role = get_role( 'give_manager' );
2870
	$role->add_cap( 'view_give_payments' );
2871
2872
	give_set_upgrade_complete( 'v215_update_donor_user_roles' );
2873
}
2874
2875
2876
2877
/**
2878
 * Remove all wp session data from the options table, regardless of expiration.
2879
 *
2880
 * @since 2.2.0
2881
 *
2882
 * @global wpdb $wpdb
2883
 */
2884
function give_v220_delete_wp_session_data() {
2885
	global $wpdb;
2886
2887
	$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...
2888
}
2889
2890
2891
/**
2892
 * Update donor meta
2893
 * Set "_give_anonymous_donor" meta key to "0" if not exist
2894
 *
2895
 * @since 2.2.4
2896
 */
2897
function give_v224_update_donor_meta_callback() {
2898
	/* @var Give_Updates $give_updates */
2899
	$give_updates = Give_Updates::get_instance();
2900
2901
	$donor_count = Give()->donors->count(
2902
		array(
2903
			'number' => -1,
2904
		)
2905
	);
2906
2907
	$donors = Give()->donors->get_donors(
2908
		array(
2909
			'paged'  => $give_updates->step,
2910
			'number' => 100,
2911
		)
2912
	);
2913
2914
	if ( $donors ) {
2915
		$give_updates->set_percentage( $donor_count, $give_updates->step * 100 );
2916
		// Loop through Donors
2917
		foreach ( $donors as $donor ) {
2918
			$anonymous_metadata = Give()->donor_meta->get_meta( $donor->id, '_give_anonymous_donor', true );
2919
2920
			// If first name meta of donor is not created, then create it.
2921
			if ( ! in_array( $anonymous_metadata, array( '0', '1' ) ) ) {
2922
				Give()->donor_meta->add_meta( $donor->id, '_give_anonymous_donor', '0' );
2923
			}
2924
		}
2925
	} else {
2926
		// The Update Ran.
2927
		give_set_upgrade_complete( 'v224_update_donor_meta' );
2928
	}
2929
}
2930