Test Failed
Push — issue/3627 ( 9db3c8...fa91e5 )
by Ravinder
1036:35 queued 1027:44
created

upgrade-functions.php ➔ give_v230_move_donor_note_callback()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Loading history...
117
118
		case version_compare( $give_version, '2.0.3', '<' ) :
119
			give_v203_upgrades();
120
			$did_upgrade = true;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

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

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

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

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

Loading history...
121
122
		case version_compare( $give_version, '2.2.0', '<' ) :
123
			give_v220_upgrades();
124
			$did_upgrade = true;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

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

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

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

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

Loading history...
125
126
		case version_compare( $give_version, '2.2.1', '<' ) :
127
			give_v221_upgrades();
128
			$did_upgrade = true;
129
	}
130
131
	if ( $did_upgrade ) {
132
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ), false );
133
	}
134
}
135
136
add_action( 'admin_init', 'give_do_automatic_upgrades' );
137
add_action( 'give_upgrades', 'give_do_automatic_upgrades' );
138
139
/**
140
 * Display Upgrade Notices.
141
 *
142
 * IMPORTANT: ALSO UPDATE INSTALL.PHP WITH THE ID OF THE UPGRADE ROUTINE SO IT DOES NOT AFFECT NEW INSTALLS.
143
 *
144
 * @since 1.0
145
 * @since 1.8.12 Update new update process code.
146
 *
147
 * @param Give_Updates $give_updates
148
 *
149
 * @return void
150
 */
151
function give_show_upgrade_notices( $give_updates ) {
152
	// v1.3.2 Upgrades
153
	$give_updates->register(
154
		array(
155
			'id'       => 'upgrade_give_payment_customer_id',
156
			'version'  => '1.3.2',
157
			'callback' => 'give_v132_upgrade_give_payment_customer_id',
158
		)
159
	);
160
161
	// v1.3.4 Upgrades ensure the user has gone through 1.3.4.
162
	$give_updates->register(
163
		array(
164
			'id'       => 'upgrade_give_offline_status',
165
			'depend'   => 'upgrade_give_payment_customer_id',
166
			'version'  => '1.3.4',
167
			'callback' => 'give_v134_upgrade_give_offline_status',
168
		)
169
	);
170
171
	// v1.8 form metadata upgrades.
172
	$give_updates->register(
173
		array(
174
			'id'       => 'v18_upgrades_form_metadata',
175
			'version'  => '1.8',
176
			'callback' => 'give_v18_upgrades_form_metadata',
177
		)
178
	);
179
180
	// v1.8.9 Upgrades
181
	$give_updates->register(
182
		array(
183
			'id'       => 'v189_upgrades_levels_post_meta',
184
			'version'  => '1.8.9',
185
			'callback' => 'give_v189_upgrades_levels_post_meta_callback',
186
		)
187
	);
188
189
	// v1.8.12 Upgrades
190
	$give_updates->register(
191
		array(
192
			'id'       => 'v1812_update_amount_values',
193
			'version'  => '1.8.12',
194
			'callback' => 'give_v1812_update_amount_values_callback',
195
		)
196
	);
197
198
	// v1.8.12 Upgrades
199
	$give_updates->register(
200
		array(
201
			'id'       => 'v1812_update_donor_purchase_values',
202
			'version'  => '1.8.12',
203
			'callback' => 'give_v1812_update_donor_purchase_value_callback',
204
		)
205
	);
206
207
	// v1.8.13 Upgrades for donor
208
	$give_updates->register(
209
		array(
210
			'id'       => 'v1813_update_donor_user_roles',
211
			'version'  => '1.8.13',
212
			'callback' => 'give_v1813_update_donor_user_roles_callback',
213
		)
214
	);
215
216
	// v1.8.17 Upgrades for donations.
217
	$give_updates->register( array(
218
		'id'       => 'v1817_update_donation_iranian_currency_code',
219
		'version'  => '1.8.17',
220
		'callback' => 'give_v1817_update_donation_iranian_currency_code',
221
	) );
222
223
	// v1.8.17 Upgrades for cleanup of user roles.
224
	$give_updates->register( array(
225
		'id'       => 'v1817_cleanup_user_roles',
226
		'version'  => '1.8.17',
227
		'callback' => 'give_v1817_cleanup_user_roles',
228
	) );
229
230
	// v1.8.18 Upgrades for assigning custom amount to existing set donations.
231
	$give_updates->register( array(
232
		'id'       => 'v1818_assign_custom_amount_set_donation',
233
		'version'  => '1.8.18',
234
		'callback' => 'give_v1818_assign_custom_amount_set_donation',
235
	) );
236
237
	// v1.8.18 Cleanup the Give Worker Role Caps.
238
	$give_updates->register( array(
239
		'id'       => 'v1818_give_worker_role_cleanup',
240
		'version'  => '1.8.18',
241
		'callback' => 'give_v1818_give_worker_role_cleanup',
242
	) );
243
244
	// v2.0.0 Upgrades
245
	$give_updates->register(
246
		array(
247
			'id'       => 'v20_upgrades_form_metadata',
248
			'version'  => '2.0.0',
249
			'callback' => 'give_v20_upgrades_form_metadata_callback',
250
		)
251
	);
252
253
	// v2.0.0 User Address Upgrades
254
	$give_updates->register(
255
		array(
256
			'id'       => 'v20_upgrades_user_address',
257
			'version'  => '2.0.0',
258
			'callback' => 'give_v20_upgrades_user_address',
259
		)
260
	);
261
262
	// v2.0.0 Upgrades
263
	$give_updates->register(
264
		array(
265
			'id'       => 'v20_upgrades_payment_metadata',
266
			'version'  => '2.0.0',
267
			'callback' => 'give_v20_upgrades_payment_metadata_callback',
268
		)
269
	);
270
271
	// v2.0.0 Upgrades
272
	$give_updates->register(
273
		array(
274
			'id'       => 'v20_logs_upgrades',
275
			'version'  => '2.0.0',
276
			'callback' => 'give_v20_logs_upgrades_callback',
277
278
		)
279
	);
280
281
	// v2.0.0 Donor Name Upgrades
282
	$give_updates->register(
283
		array(
284
			'id'       => 'v20_upgrades_donor_name',
285
			'version'  => '2.0.0',
286
			'callback' => 'give_v20_upgrades_donor_name',
287
		)
288
	);
289
290
	// v2.0.0 Upgrades
291
	$give_updates->register(
292
		array(
293
			'id'       => 'v20_move_metadata_into_new_table',
294
			'version'  => '2.0.0',
295
			'callback' => 'give_v20_move_metadata_into_new_table_callback',
296
			'depend'   => array( 'v20_upgrades_payment_metadata', 'v20_upgrades_form_metadata' ),
297
		)
298
	);
299
300
	// v2.0.0 Upgrades
301
	$give_updates->register(
302
		array(
303
			'id'       => 'v20_rename_donor_tables',
304
			'version'  => '2.0.0',
305
			'callback' => 'give_v20_rename_donor_tables_callback',
306
			'depend'   => array(
307
				'v20_move_metadata_into_new_table',
308
				'v20_logs_upgrades',
309
				'v20_upgrades_form_metadata',
310
				'v20_upgrades_payment_metadata',
311
				'v20_upgrades_user_address',
312
				'v20_upgrades_donor_name',
313
			),
314
		)
315
	);
316
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
317
318
	// v2.0.1 Upgrades
319
	$give_updates->register(
320
		array(
321
			'id'       => 'v201_upgrades_payment_metadata',
322
			'version'  => '2.0.1',
323
			'callback' => 'give_v201_upgrades_payment_metadata_callback',
324
		)
325
	);
326
327
	// v2.0.1 Upgrades
328
	$give_updates->register(
329
		array(
330
			'id'       => 'v201_add_missing_donors',
331
			'version'  => '2.0.1',
332
			'callback' => 'give_v201_add_missing_donors_callback',
333
		)
334
	);
335
336
	// Run v2.0.0 Upgrades again in 2.0.1
337
	$give_updates->register(
338
		array(
339
			'id'       => 'v201_move_metadata_into_new_table',
340
			'version'  => '2.0.1',
341
			'callback' => 'give_v201_move_metadata_into_new_table_callback',
342
			'depend'   => array( 'v201_upgrades_payment_metadata', 'v201_add_missing_donors' ),
343
		)
344
	);
345
346
	// Run v2.0.0 Upgrades again in 2.0.1
347
	$give_updates->register(
348
		array(
349
			'id'       => 'v201_logs_upgrades',
350
			'version'  => '2.0.1',
351
			'callback' => 'give_v201_logs_upgrades_callback',
352
		)
353
	);
354
355
	// v2.1 Verify Form Status Upgrade.
356
	$give_updates->register(
357
		array(
358
			'id'       => 'v210_verify_form_status_upgrades',
359
			'version'  => '2.1.0',
360
			'callback' => 'give_v210_verify_form_status_upgrades_callback',
361
		)
362
	);
363
364
	// v2.1.3 Delete non attached donation meta.
365
	$give_updates->register(
366
		array(
367
			'id'       => 'v213_delete_donation_meta',
368
			'version'  => '2.1.3',
369
			'callback' => 'give_v213_delete_donation_meta_callback',
370
			'depends'  => array( 'v201_move_metadata_into_new_table' )
371
		)
372
	);
373
374
	// v2.1.5 Add additional capability to the give_manager role.
375
	$give_updates->register(
376
		array(
377
			'id'       => 'v215_update_donor_user_roles',
378
			'version'  => '2.1.5',
379
			'callback' => 'give_v215_update_donor_user_roles_callback',
380
		)
381
	);
382
383
	// v2.1.5 Add additional capability to the give_manager role.
384
	$give_updates->register(
385
		array(
386
			'id'       => 'v224_update_donor_meta',
387
			'version'  => '2.2.4',
388
			'callback' => 'give_v224_update_donor_meta_callback',
389
		)
390
	);
391
392
	// v2.3.0 Move donor notes to custom comment table.
393
	$give_updates->register(
394
		array(
395
			'id'       => 'v230_move_donor_note',
396
			'version'  => '2.3.0',
397
			'callback' => 'give_v230_move_donor_note_callback',
398
		)
399
	);
400
}
401
402
add_action( 'give_register_updates', 'give_show_upgrade_notices' );
403
404
/**
405
 * Triggers all upgrade functions
406
 *
407
 * This function is usually triggered via AJAX
408
 *
409
 * @since 1.0
410
 * @return void
411
 */
412
function give_trigger_upgrades() {
413
414 View Code Duplication
	if ( ! current_user_can( 'manage_give_settings' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
415
		wp_die( esc_html__( 'You do not have permission to do Give upgrades.', 'give' ), esc_html__( 'Error', 'give' ), array(
416
			'response' => 403,
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( array(
836
			'paged'          => $give_updates->step,
837
			'status'         => 'any',
838
			'order'          => 'ASC',
839
			'post_type'      => 'give_forms',
840
			'posts_per_page' => 20,
841
		)
842
	);
843
844
	if ( $forms->have_posts() ) {
845
		$give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 20 ) );
846
847
		while ( $forms->have_posts() ) {
848
			$forms->the_post();
849
850
			// Form content.
851
			// Note in version 1.8 display content setting split into display content and content placement setting.
852
			// You can delete _give_content_option in future.
853
			$show_content = give_get_meta( get_the_ID(), '_give_content_option', true );
854
			if ( $show_content && ! give_get_meta( get_the_ID(), '_give_display_content', true ) ) {
855
				$field_value = ( 'none' !== $show_content ? 'enabled' : 'disabled' );
856
				give_update_meta( get_the_ID(), '_give_display_content', $field_value );
857
858
				$field_value = ( 'none' !== $show_content ? $show_content : 'give_pre_form' );
859
				give_update_meta( get_the_ID(), '_give_content_placement', $field_value );
860
			}
861
862
			// "Disable" Guest Donation. Checkbox.
863
			// See: https://github.com/WordImpress/Give/issues/1470.
864
			$guest_donation        = give_get_meta( get_the_ID(), '_give_logged_in_only', true );
865
			$guest_donation_newval = ( in_array( $guest_donation, array( 'yes', 'on' ) ) ? 'disabled' : 'enabled' );
866
			give_update_meta( get_the_ID(), '_give_logged_in_only', $guest_donation_newval );
867
868
			// Offline Donations.
869
			// See: https://github.com/WordImpress/Give/issues/1579.
870
			$offline_donation = give_get_meta( get_the_ID(), '_give_customize_offline_donations', true );
871
			if ( 'no' === $offline_donation ) {
872
				$offline_donation_newval = 'global';
873
			} elseif ( 'yes' === $offline_donation ) {
874
				$offline_donation_newval = 'enabled';
875
			} else {
876
				$offline_donation_newval = 'disabled';
877
			}
878
			give_update_meta( get_the_ID(), '_give_customize_offline_donations', $offline_donation_newval );
879
880
			// Convert yes/no setting field to enabled/disabled.
881
			$form_radio_settings = array(
882
				// Custom Amount.
883
				'_give_custom_amount',
884
885
				// Donation Gaol.
886
				'_give_goal_option',
887
888
				// Close Form.
889
				'_give_close_form_when_goal_achieved',
890
891
				// Term & conditions.
892
				'_give_terms_option',
893
894
				// Billing fields.
895
				'_give_offline_donation_enable_billing_fields_single',
896
			);
897
898
			foreach ( $form_radio_settings as $meta_key ) {
899
				// Get value.
900
				$field_value = give_get_meta( get_the_ID(), $meta_key, true );
901
902
				// Convert meta value only if it is in yes/no/none.
903
				if ( in_array( $field_value, array( 'yes', 'on', 'no', 'none' ) ) ) {
904
905
					$field_value = ( in_array( $field_value, array( 'yes', 'on' ) ) ? 'enabled' : 'disabled' );
906
					give_update_meta( get_the_ID(), $meta_key, $field_value );
907
				}
908
			}
909
		}// End while().
910
911
		wp_reset_postdata();
912
913
	} else {
914
		// No more forms found, finish up.
915
		give_set_upgrade_complete( 'v18_upgrades_form_metadata' );
916
	}
917
}
918
919
920
/**
921
 * Get list of core setting renamed in version 1.8.
922
 *
923
 * @since  1.8
924
 * @return array
925
 */
926
function give_v18_renamed_core_settings() {
927
	return array(
928
		'disable_paypal_verification' => 'paypal_verification',
929
		'disable_css'                 => 'css',
930
		'disable_welcome'             => 'welcome',
931
		'disable_forms_singular'      => 'forms_singular',
932
		'disable_forms_archives'      => 'forms_archives',
933
		'disable_forms_excerpt'       => 'forms_excerpt',
934
		'disable_form_featured_img'   => 'form_featured_img',
935
		'disable_form_sidebar'        => 'form_sidebar',
936
		'disable_admin_notices'       => 'admin_notices',
937
		'disable_the_content_filter'  => 'the_content_filter',
938
		'enable_floatlabels'          => 'floatlabels',
939
		'enable_categories'           => 'categories',
940
		'enable_tags'                 => 'tags',
941
	);
942
}
943
944
945
/**
946
 * Upgrade core settings.
947
 *
948
 * @since  1.8.7
949
 * @return void
950
 */
951
function give_v187_upgrades() {
952
	global $wpdb;
953
954
	/**
955
	 * Upgrade 1: Remove stat and cache transients.
956
	 */
957
	$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...
958
		$wpdb->prepare(
959
			"
960
					SELECT *
961
					FROM {$wpdb->options}
962
					WHERE (
963
					option_name LIKE %s
964
					OR 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
					)
977
					",
978
			array(
979
				'%_transient_give_stats_%',
980
				'give_cache%',
981
				'%_transient_give_add_ons_feed%',
982
				'%_transient__give_ajax_works' .
983
				'%_transient_give_total_api_keys%',
984
				'%_transient_give_i18n_give_promo_hide%',
985
				'%_transient_give_contributors%',
986
				'%_transient_give_estimated_monthly_stats%',
987
				'%_transient_give_earnings_total%',
988
				'%_transient_give_i18n_give_%',
989
				'%_transient__give_installed%',
990
				'%_transient__give_activation_redirect%',
991
				'%_transient__give_hide_license_notices_shortly_%',
992
				'%give_income_total%',
993
			)
994
		),
995
		1
996
	);
997
998
	// User related transients.
999
	$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...
1000
		$wpdb->prepare(
1001
			"SELECT user_id, meta_key
1002
			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...
1003
			WHERE meta_value=%s",
1004
			'give_user_public_key'
1005
		),
1006
		ARRAY_A
1007
	);
1008
1009
	if ( ! empty( $user_apikey_options ) ) {
1010
		foreach ( $user_apikey_options as $user ) {
1011
			$cached_options[] = '_transient_' . md5( 'give_api_user_' . $user['meta_key'] );
1012
			$cached_options[] = '_transient_' . md5( 'give_api_user_public_key' . $user['user_id'] );
1013
			$cached_options[] = '_transient_' . md5( 'give_api_user_secret_key' . $user['user_id'] );
1014
		}
1015
	}
1016
1017
	if ( ! empty( $cached_options ) ) {
1018
		foreach ( $cached_options as $option ) {
1019 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...
1020
				case ( false !== strpos( $option, 'transient' ) ):
1021
					$option = str_replace( '_transient_', '', $option );
1022
					delete_transient( $option );
1023
					break;
1024
1025
				default:
1026
					delete_option( $option );
1027
			}
1028
		}
1029
	}
1030
}
1031
1032
/**
1033
 * Update Capabilities for Give_Worker User Role.
1034
 *
1035
 * This upgrade routine will update access rights for Give_Worker User Role.
1036
 *
1037
 * @since      1.8.8
1038
 */
1039
function give_v188_upgrades() {
1040
1041
	global $wp_roles;
1042
1043
	// Get the role object.
1044
	$give_worker = get_role( 'give_worker' );
1045
1046
	// A list of capabilities to add for give workers.
1047
	$caps_to_add = array(
1048
		'edit_posts',
1049
		'edit_pages',
1050
	);
1051
1052
	foreach ( $caps_to_add as $cap ) {
1053
		// Add the capability.
1054
		$give_worker->add_cap( $cap );
1055
	}
1056
1057
}
1058
1059
/**
1060
 * Update Post meta for minimum and maximum amount for multi level donation forms
1061
 *
1062
 * This upgrade routine adds post meta for give_forms CPT for multi level donation form.
1063
 *
1064
 * @since      1.8.9
1065
 */
1066
function give_v189_upgrades_levels_post_meta_callback() {
1067
	/* @var Give_Updates $give_updates */
1068
	$give_updates = Give_Updates::get_instance();
1069
1070
	// form query.
1071
	$donation_forms = new WP_Query( array(
1072
			'paged'          => $give_updates->step,
1073
			'status'         => 'any',
1074
			'order'          => 'ASC',
1075
			'post_type'      => 'give_forms',
1076
			'posts_per_page' => 20,
1077
		)
1078
	);
1079
1080
	if ( $donation_forms->have_posts() ) {
1081
		$give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
1082
1083
		while ( $donation_forms->have_posts() ) {
1084
			$donation_forms->the_post();
1085
			$form_id = get_the_ID();
1086
1087
			// Remove formatting from _give_set_price.
1088
			update_post_meta(
1089
				$form_id,
1090
				'_give_set_price',
1091
				give_sanitize_amount( get_post_meta( $form_id, '_give_set_price', true ) )
1092
			);
1093
1094
			// Remove formatting from _give_custom_amount_minimum.
1095
			update_post_meta(
1096
				$form_id,
1097
				'_give_custom_amount_minimum',
1098
				give_sanitize_amount( get_post_meta( $form_id, '_give_custom_amount_minimum', true ) )
1099
			);
1100
1101
			// Bailout.
1102
			if ( 'set' === get_post_meta( $form_id, '_give_price_option', true ) ) {
1103
				continue;
1104
			}
1105
1106
			$donation_levels = get_post_meta( $form_id, '_give_donation_levels', true );
1107
1108
			if ( ! empty( $donation_levels ) ) {
1109
1110
				foreach ( $donation_levels as $index => $donation_level ) {
1111
					if ( isset( $donation_level['_give_amount'] ) ) {
1112
						$donation_levels[ $index ]['_give_amount'] = give_sanitize_amount( $donation_level['_give_amount'] );
1113
					}
1114
				}
1115
1116
				update_post_meta( $form_id, '_give_donation_levels', $donation_levels );
1117
1118
				$donation_levels_amounts = wp_list_pluck( $donation_levels, '_give_amount' );
1119
1120
				$min_amount = min( $donation_levels_amounts );
1121
				$max_amount = max( $donation_levels_amounts );
1122
1123
				// Set Minimum and Maximum amount for Multi Level Donation Forms
1124
				give_update_meta( $form_id, '_give_levels_minimum_amount', $min_amount ? give_sanitize_amount( $min_amount ) : 0 );
1125
				give_update_meta( $form_id, '_give_levels_maximum_amount', $max_amount ? give_sanitize_amount( $max_amount ) : 0 );
1126
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1127
1128
		}
1129
1130
		/* Restore original Post Data */
1131
		wp_reset_postdata();
1132
	} else {
1133
		// The Update Ran.
1134
		give_set_upgrade_complete( 'v189_upgrades_levels_post_meta' );
1135
	}
1136
1137
}
1138
1139
1140
/**
1141
 * Give version 1.8.9 upgrades
1142
 *
1143
 * @since      1.8.9
1144
 */
1145
function give_v189_upgrades() {
1146
	/**
1147
	 * 1. Remove user license related notice show blocked ( Give_Notice will handle )
1148
	 */
1149
	global $wpdb;
1150
1151
	// Delete permanent notice blocker.
1152
	$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...
1153
		$wpdb->prepare(
1154
			"
1155
					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...
1156
					WHERE meta_key
1157
					LIKE '%%%s%%'
1158
					",
1159
			'_give_hide_license_notices_permanently'
1160
		)
1161
	);
1162
1163
	// Delete short notice blocker.
1164
	$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...
1165
		$wpdb->prepare(
1166
			"
1167
					DELETE FROM $wpdb->options
1168
					WHERE option_name
1169
					LIKE '%%%s%%'
1170
					",
1171
			'__give_hide_license_notices_shortly_'
1172
		)
1173
	);
1174
}
1175
1176
/**
1177
 * 2.0 Upgrades.
1178
 *
1179
 * @since  2.0
1180
 * @return void
1181
 */
1182
function give_v20_upgrades() {
1183
	// Update cache setting.
1184
	give_update_option( 'cache', 'enabled' );
1185
1186
	// Upgrade email settings.
1187
	give_v20_upgrades_email_setting();
1188
}
1189
1190
/**
1191
 * Move old email api settings to new email setting api for following emails:
1192
 *    1. new offline donation         [This was hard coded]
1193
 *    2. offline donation instruction
1194
 *    3. new donation
1195
 *    4. donation receipt
1196
 *
1197
 * @since 2.0
1198
 */
1199
function give_v20_upgrades_email_setting() {
1200
	$all_setting = give_get_settings();
1201
1202
	// Bailout on fresh install.
1203
	if ( empty( $all_setting ) ) {
1204
		return;
1205
	}
1206
1207
	$settings = array(
1208
		'offline_donation_subject'      => 'offline-donation-instruction_email_subject',
1209
		'global_offline_donation_email' => 'offline-donation-instruction_email_message',
1210
		'donation_subject'              => 'donation-receipt_email_subject',
1211
		'donation_receipt'              => 'donation-receipt_email_message',
1212
		'donation_notification_subject' => 'new-donation_email_subject',
1213
		'donation_notification'         => 'new-donation_email_message',
1214
		'admin_notice_emails'           => array(
1215
			'new-donation_recipient',
1216
			'new-offline-donation_recipient',
1217
			'new-donor-register_recipient',
1218
		),
1219
		'admin_notices'                 => 'new-donation_notification',
1220
	);
1221
1222
	foreach ( $settings as $old_setting => $new_setting ) {
1223
		// Do not update already modified
1224
		if ( ! is_array( $new_setting ) ) {
1225
			if ( array_key_exists( $new_setting, $all_setting ) || ! array_key_exists( $old_setting, $all_setting ) ) {
1226
				continue;
1227
			}
1228
		}
1229
1230
		switch ( $old_setting ) {
1231
			case 'admin_notices':
1232
				$notification_status = give_get_option( $old_setting, 'enabled' );
1233
1234
				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 1222 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...
1235
1236
				// @todo: Delete this option later ( version > 2.0 ), We need this for per form email addon.
1237
				// give_delete_option( $old_setting );
1238
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 1222 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( array(
1309
			'paged'          => $give_updates->step,
1310
			'status'         => 'any',
1311
			'order'          => 'ASC',
1312
			'post_type'      => array( 'give_forms', 'give_payment' ),
1313
			'posts_per_page' => 20,
1314
		)
1315
	);
1316
	if ( $donation_forms->have_posts() ) {
1317
		$give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
1318
1319
		while ( $donation_forms->have_posts() ) {
1320
			$donation_forms->the_post();
1321
			global $post;
1322
1323
			$meta = get_post_meta( $post->ID );
1324
1325
			switch ( $post->post_type ) {
1326
				case 'give_forms':
1327
					// _give_set_price.
1328
					if ( ! empty( $meta['_give_set_price'][0] ) ) {
1329
						update_post_meta( $post->ID, '_give_set_price', give_sanitize_amount_for_db( $meta['_give_set_price'][0] ) );
1330
					}
1331
1332
					// _give_custom_amount_minimum.
1333
					if ( ! empty( $meta['_give_custom_amount_minimum'][0] ) ) {
1334
						update_post_meta( $post->ID, '_give_custom_amount_minimum', give_sanitize_amount_for_db( $meta['_give_custom_amount_minimum'][0] ) );
1335
					}
1336
1337
					// _give_levels_minimum_amount.
1338
					if ( ! empty( $meta['_give_levels_minimum_amount'][0] ) ) {
1339
						update_post_meta( $post->ID, '_give_levels_minimum_amount', give_sanitize_amount_for_db( $meta['_give_levels_minimum_amount'][0] ) );
1340
					}
1341
1342
					// _give_levels_maximum_amount.
1343
					if ( ! empty( $meta['_give_levels_maximum_amount'][0] ) ) {
1344
						update_post_meta( $post->ID, '_give_levels_maximum_amount', give_sanitize_amount_for_db( $meta['_give_levels_maximum_amount'][0] ) );
1345
					}
1346
1347
					// _give_set_goal.
1348
					if ( ! empty( $meta['_give_set_goal'][0] ) ) {
1349
						update_post_meta( $post->ID, '_give_set_goal', give_sanitize_amount_for_db( $meta['_give_set_goal'][0] ) );
1350
					}
1351
1352
					// _give_form_earnings.
1353
					if ( ! empty( $meta['_give_form_earnings'][0] ) ) {
1354
						update_post_meta( $post->ID, '_give_form_earnings', give_sanitize_amount_for_db( $meta['_give_form_earnings'][0] ) );
1355
					}
1356
1357
					// _give_custom_amount_minimum.
1358
					if ( ! empty( $meta['_give_donation_levels'][0] ) ) {
1359
						$donation_levels = unserialize( $meta['_give_donation_levels'][0] );
1360
1361
						foreach ( $donation_levels as $index => $level ) {
1362
							if ( empty( $level['_give_amount'] ) ) {
1363
								continue;
1364
							}
1365
1366
							$donation_levels[ $index ]['_give_amount'] = give_sanitize_amount_for_db( $level['_give_amount'] );
1367
						}
1368
1369
						$meta['_give_donation_levels'] = $donation_levels;
1370
						update_post_meta( $post->ID, '_give_donation_levels', $meta['_give_donation_levels'] );
1371
					}
1372
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
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( array(
1410
			'number' => 20,
1411
			'offset' => $give_updates->get_offset( 20 ),
1412
		)
1413
	);
1414
1415
	if ( ! empty( $donors ) ) {
1416
		$give_updates->set_percentage( Give()->donors->count(), $give_updates->get_offset( 20 ) );
1417
1418
		/* @var Object $donor */
1419
		foreach ( $donors as $donor ) {
1420
			Give()->donors->update( $donor->id, array( 'purchase_value' => give_sanitize_amount_for_db( $donor->purchase_value ) ) );
1421
		}
1422
	} else {
1423
		// The Update Ran.
1424
		give_set_upgrade_complete( 'v1812_update_donor_purchase_values' );
1425
	}
1426
}
1427
1428
/**
1429
 * Upgrade routine for updating user roles for existing donors.
1430
 *
1431
 * @since 1.8.13
1432
 */
1433
function give_v1813_update_donor_user_roles_callback() {
1434
	/* @var Give_Updates $give_updates */
1435
	$give_updates = Give_Updates::get_instance();
1436
1437
	// Fetch all the existing donors.
1438
	$donors = Give()->donors->get_donors( array(
1439
			'number' => 20,
1440
			'offset' => $give_updates->get_offset( 20 ),
1441
		)
1442
	);
1443
1444
	if ( ! empty( $donors ) ) {
1445
		$give_updates->set_percentage( Give()->donors->count(), $give_updates->get_offset( 20 ) );
1446
1447
		/* @var Object $donor */
1448
		foreach ( $donors as $donor ) {
1449
			$user_id = $donor->user_id;
1450
1451
			// Proceed, if donor is attached with user.
1452
			if ( $user_id ) {
1453
				$user = get_userdata( $user_id );
1454
1455
				// Update user role, if user has subscriber role.
1456
				if ( is_array( $user->roles ) && in_array( 'subscriber', $user->roles ) ) {
1457
					wp_update_user(
1458
						array(
1459
							'ID'   => $user_id,
1460
							'role' => 'give_donor',
1461
						)
1462
					);
1463
				}
1464
			}
1465
		}
1466
	} else {
1467
		// The Update Ran.
1468
		give_set_upgrade_complete( 'v1813_update_donor_user_roles' );
1469
	}
1470
}
1471
1472
1473
/**
1474
 * Version 1.8.13 automatic updates
1475
 *
1476
 * @since 1.8.13
1477
 */
1478
function give_v1813_upgrades() {
1479
	// Update admin setting.
1480
	give_update_option( 'donor_default_user_role', 'give_donor' );
1481
1482
	// Update Give roles.
1483
	$roles = new Give_Roles();
1484
	$roles->add_roles();
1485
	$roles->add_caps();
1486
}
1487
1488
/**
1489
 * Correct currency code for "Iranian Currency" for all of the payments.
1490
 *
1491
 * @since 1.8.17
1492
 */
1493
function give_v1817_update_donation_iranian_currency_code() {
1494
	/* @var Give_Updates $give_updates */
1495
	$give_updates = Give_Updates::get_instance();
1496
1497
	// form query.
1498
	$payments = new WP_Query( array(
1499
			'paged'          => $give_updates->step,
1500
			'status'         => 'any',
1501
			'order'          => 'ASC',
1502
			'post_type'      => array( 'give_payment' ),
1503
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1504
		)
1505
	);
1506
1507
	if ( $payments->have_posts() ) {
1508
		$give_updates->set_percentage( $payments->found_posts, ( $give_updates->step * 100 ) );
1509
1510
		while ( $payments->have_posts() ) {
1511
			$payments->the_post();
1512
1513
			$payment_meta = give_get_payment_meta( get_the_ID() );
1514
1515
			if ( 'RIAL' === $payment_meta['currency'] ) {
1516
				$payment_meta['currency'] = 'IRR';
1517
				give_update_meta( get_the_ID(), '_give_payment_meta', $payment_meta );
1518
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1519
1520
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1521
1522
	} else {
1523
		// The Update Ran.
1524
		give_set_upgrade_complete( 'v1817_update_donation_iranian_currency_code' );
1525
	}
1526
}
1527
1528
/**
1529
 * Correct currency code for "Iranian Currency" in Give setting.
1530
 * Version 1.8.17 automatic updates
1531
 *
1532
 * @since 1.8.17
1533
 */
1534
function give_v1817_upgrades() {
1535
	$give_settings = give_get_settings();
1536
1537
	if ( 'RIAL' === $give_settings['currency'] ) {
1538
		$give_settings['currency'] = 'IRR';
1539
		update_option( 'give_settings', $give_settings, false );
1540
	}
1541
}
1542
1543
/**
1544
 * Process Clean up of User Roles for more flexibility.
1545
 *
1546
 * @since 1.8.17
1547
 */
1548
function give_v1817_process_cleanup_user_roles() {
1549
1550
	global $wp_roles;
1551
1552
	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...
1553
		return;
1554
	}
1555
1556
	// Add Capabilities to user roles as required.
1557
	$add_caps = array(
1558
		'administrator' => array(
1559
			'view_give_payments',
1560
		),
1561
	);
1562
1563
	// Remove Capabilities to user roles as required.
1564
	$remove_caps = array(
1565
		'give_manager' => array(
1566
			'edit_others_pages',
1567
			'edit_others_posts',
1568
			'delete_others_pages',
1569
			'delete_others_posts',
1570
			'manage_categories',
1571
			'import',
1572
			'export',
1573
		),
1574
	);
1575
1576
	foreach ( $add_caps as $role => $caps ) {
1577
		foreach ( $caps as $cap ) {
1578
			$wp_roles->add_cap( $role, $cap );
1579
		}
1580
	}
1581
1582
	foreach ( $remove_caps as $role => $caps ) {
1583
		foreach ( $caps as $cap ) {
1584
			$wp_roles->remove_cap( $role, $cap );
1585
		}
1586
	}
1587
1588
}
1589
1590
/**
1591
 * Upgrade Routine - Clean up of User Roles for more flexibility.
1592
 *
1593
 * @since 1.8.17
1594
 */
1595
function give_v1817_cleanup_user_roles() {
1596
	/* @var Give_Updates $give_updates */
1597
	$give_updates = Give_Updates::get_instance();
1598
1599
	give_v1817_process_cleanup_user_roles();
1600
1601
	$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...
1602
1603
	// Create Give plugin roles.
1604
	$roles = new Give_Roles();
1605
	$roles->add_roles();
1606
	$roles->add_caps();
1607
1608
	give_set_upgrade_complete( 'v1817_cleanup_user_roles' );
1609
}
1610
1611
/**
1612
 * Automatic Upgrade for release 1.8.18.
1613
 *
1614
 * @since 1.8.18
1615
 */
1616
function give_v1818_upgrades() {
1617
1618
	// Remove email_access_installed from give_settings.
1619
	give_delete_option( 'email_access_installed' );
1620
}
1621
1622
/**
1623
 * Upgrade Routine - Assigns Custom Amount to existing donation of type set donation.
1624
 *
1625
 * @since 1.8.18
1626
 */
1627
function give_v1818_assign_custom_amount_set_donation() {
1628
1629
	/* @var Give_Updates $give_updates */
1630
	$give_updates = Give_Updates::get_instance();
1631
1632
	$donations = new WP_Query( array(
1633
			'paged'          => $give_updates->step,
1634
			'status'         => 'any',
1635
			'order'          => 'ASC',
1636
			'post_type'      => array( 'give_payment' ),
1637
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1638
		)
1639
	);
1640
1641
	if ( $donations->have_posts() ) {
1642
		$give_updates->set_percentage( $donations->found_posts, $give_updates->step * 100 );
1643
1644
		while ( $donations->have_posts() ) {
1645
			$donations->the_post();
1646
1647
			$form          = new Give_Donate_Form( give_get_meta( get_the_ID(), '_give_payment_form_id', true ) );
1648
			$donation_meta = give_get_payment_meta( get_the_ID() );
1649
1650
			// Update Donation meta with price_id set as custom, only if it is:
1651
			// 1. Donation Type = Set Donation.
1652
			// 2. Donation Price Id is not set to custom.
1653
			// 3. Form has not enabled custom price and donation amount assures that it is custom amount.
1654
			if (
1655
				$form->ID &&
1656
				$form->is_set_type_donation_form() &&
1657
				( 'custom' !== $donation_meta['price_id'] ) &&
1658
				$form->is_custom_price( give_get_meta( get_the_ID(), '_give_payment_total', true ) )
1659
			) {
1660
				$donation_meta['price_id'] = 'custom';
1661
				give_update_meta( get_the_ID(), '_give_payment_meta', $donation_meta );
1662
				give_update_meta( get_the_ID(), '_give_payment_price_id', 'custom' );
1663
			}
1664
		}
1665
1666
		wp_reset_postdata();
1667
	} else {
1668
		// Update Ran Successfully.
1669
		give_set_upgrade_complete( 'v1818_assign_custom_amount_set_donation' );
1670
	}
1671
}
1672
1673
/**
1674
 * Upgrade Routine - Removed Give Worker caps.
1675
 *
1676
 * See: https://github.com/WordImpress/Give/issues/2476
1677
 *
1678
 * @since 1.8.18
1679
 */
1680
function give_v1818_give_worker_role_cleanup() {
1681
1682
	/* @var Give_Updates $give_updates */
1683
	$give_updates = Give_Updates::get_instance();
1684
1685
	global $wp_roles;
1686
1687
	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...
1688
		return;
1689
	}
1690
1691
	// Remove Capabilities to user roles as required.
1692
	$remove_caps = array(
1693
		'give_worker' => array(
1694
			'delete_give_payments',
1695
			'delete_others_give_payments',
1696
			'delete_private_give_payments',
1697
			'delete_published_give_payments',
1698
			'edit_others_give_payments',
1699
			'edit_private_give_payments',
1700
			'edit_published_give_payments',
1701
			'read_private_give_payments',
1702
		),
1703
	);
1704
1705
	foreach ( $remove_caps as $role => $caps ) {
1706
		foreach ( $caps as $cap ) {
1707
			$wp_roles->remove_cap( $role, $cap );
1708
		}
1709
	}
1710
1711
	$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...
1712
1713
	// Create Give plugin roles.
1714
	$roles = new Give_Roles();
1715
	$roles->add_roles();
1716
	$roles->add_caps();
1717
1718
	give_set_upgrade_complete( 'v1818_give_worker_role_cleanup' );
1719
}
1720
1721
/**
1722
 *
1723
 * Upgrade form metadata for new metabox settings.
1724
 *
1725
 * @since  2.0
1726
 * @return void
1727
 */
1728
function give_v20_upgrades_form_metadata_callback() {
1729
	$give_updates = Give_Updates::get_instance();
1730
1731
	// form query
1732
	$forms = new WP_Query( array(
1733
			'paged'          => $give_updates->step,
1734
			'status'         => 'any',
1735
			'order'          => 'ASC',
1736
			'post_type'      => 'give_forms',
1737
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1738
		)
1739
	);
1740
1741
	if ( $forms->have_posts() ) {
1742
		$give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 100 ) );
1743
1744
		while ( $forms->have_posts() ) {
1745
			$forms->the_post();
1746
			global $post;
1747
1748
			// Update offline instruction email notification status.
1749
			$offline_instruction_notification_status = get_post_meta( get_the_ID(), '_give_customize_offline_donations', true );
1750
			$offline_instruction_notification_status = give_is_setting_enabled( $offline_instruction_notification_status, array(
0 ignored issues
show
Documentation introduced by
array('enabled', 'global') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string|null.

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

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

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

function acceptsInteger($int) { }

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

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1751
				'enabled',
1752
				'global',
1753
			) )
1754
				? $offline_instruction_notification_status
1755
				: 'global';
1756
			update_post_meta( get_the_ID(), '_give_offline-donation-instruction_notification', $offline_instruction_notification_status );
1757
1758
			// Update offline instruction email message.
1759
			update_post_meta(
1760
				get_the_ID(),
1761
				'_give_offline-donation-instruction_email_message',
1762
				get_post_meta(
1763
					get_the_ID(),
1764
					// @todo: Delete this option later ( version > 2.0 ).
1765
					'_give_offline_donation_email',
1766
					true
1767
				)
1768
			);
1769
1770
			// Update offline instruction email subject.
1771
			update_post_meta(
1772
				get_the_ID(),
1773
				'_give_offline-donation-instruction_email_subject',
1774
				get_post_meta(
1775
					get_the_ID(),
1776
					// @todo: Delete this option later ( version > 2.0 ).
1777
					'_give_offline_donation_subject',
1778
					true
1779
				)
1780
			);
1781
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
1782
1783
		}// End while().
1784
1785
		wp_reset_postdata();
1786
	} else {
1787
		// No more forms found, finish up.
1788
		give_set_upgrade_complete( 'v20_upgrades_form_metadata' );
1789
	}
1790
}
1791
1792
1793
/**
1794
 * Upgrade payment metadata for new metabox settings.
1795
 *
1796
 * @since  2.0
1797
 * @global wpdb $wpdb
1798
 * @return void
1799
 */
1800
function give_v20_upgrades_payment_metadata_callback() {
1801
	global $wpdb;
1802
	$give_updates = Give_Updates::get_instance();
1803
1804
	// form query
1805
	$forms = new WP_Query( array(
1806
			'paged'          => $give_updates->step,
1807
			'status'         => 'any',
1808
			'order'          => 'ASC',
1809
			'post_type'      => 'give_payment',
1810
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1811
		)
1812
	);
1813
1814
	if ( $forms->have_posts() ) {
1815
		$give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 100 ) );
1816
1817
		while ( $forms->have_posts() ) {
1818
			$forms->the_post();
1819
			global $post;
1820
1821
			// Split _give_payment_meta meta.
1822
			// @todo Remove _give_payment_meta after releases 2.0
1823
			$payment_meta = give_get_meta( $post->ID, '_give_payment_meta', true );
1824
1825
			if ( ! empty( $payment_meta ) ) {
1826
				_give_20_bc_split_and_save_give_payment_meta( $post->ID, $payment_meta );
1827
			}
1828
1829
			$deprecated_meta_keys = array(
1830
				'_give_payment_customer_id' => '_give_payment_donor_id',
1831
				'_give_payment_user_email'  => '_give_payment_donor_email',
1832
				'_give_payment_user_ip'     => '_give_payment_donor_ip',
1833
			);
1834
1835 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...
1836
				// Do not add new meta key if already exist.
1837
				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...
1838
					continue;
1839
				}
1840
1841
				$wpdb->insert(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
1842
					$wpdb->postmeta,
1843
					array(
1844
						'post_id'    => $post->ID,
1845
						'meta_key'   => $new_meta_key,
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
1846
						'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...
1847
					)
1848
				);
1849
			}
1850
1851
			// Bailout
1852 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...
1853
				/* @var Give_Donor $donor */
1854
				$donor = new Give_Donor( $donor_id );
1855
1856
				$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...
1857
				$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...
1858
				$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...
1859
				$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...
1860
				$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...
1861
				$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...
1862
1863
				// Save address.
1864
				$donor->add_address( 'billing[]', $address );
1865
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1866
1867
		}// End while().
1868
1869
		wp_reset_postdata();
1870
	} else {
1871
		// @todo Delete user id meta after releases 2.0
1872
		// $wpdb->get_var( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key=%s", '_give_payment_user_id' ) );
1873
1874
		// No more forms found, finish up.
1875
		give_set_upgrade_complete( 'v20_upgrades_payment_metadata' );
1876
	}
1877
}
1878
1879
1880
/**
1881
 * Upgrade logs data.
1882
 *
1883
 * @since  2.0
1884
 * @return void
1885
 */
1886
function give_v20_logs_upgrades_callback() {
1887
	global $wpdb;
1888
	$give_updates = Give_Updates::get_instance();
1889
1890
	// form query
1891
	$forms = new WP_Query( array(
1892
			'paged'          => $give_updates->step,
1893
			'order'          => 'DESC',
1894
			'post_type'      => 'give_log',
1895
			'post_status'    => 'any',
1896
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1897
		)
1898
	);
1899
1900
	if ( $forms->have_posts() ) {
1901
		$give_updates->set_percentage( $forms->found_posts, $give_updates->step * 100 );
1902
1903
		while ( $forms->have_posts() ) {
1904
			$forms->the_post();
1905
			global $post;
1906
1907
			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...
1908
				continue;
1909
			}
1910
1911
			$term      = get_the_terms( $post->ID, 'give_log_type' );
1912
			$term      = ! is_wp_error( $term ) && ! empty( $term ) ? $term[0] : array();
1913
			$term_name = ! empty( $term ) ? $term->slug : '';
1914
1915
			$log_data = array(
1916
				'ID'           => $post->ID,
1917
				'log_title'    => $post->post_title,
1918
				'log_content'  => $post->post_content,
1919
				'log_parent'   => 0,
1920
				'log_type'     => $term_name,
1921
				'log_date'     => $post->post_date,
1922
				'log_date_gmt' => $post->post_date_gmt,
1923
			);
1924
			$log_meta = array();
1925
1926 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...
1927
				foreach ( $old_log_meta as $meta_key => $meta_value ) {
1928
					switch ( $meta_key ) {
1929
						case '_give_log_payment_id':
1930
							$log_data['log_parent']        = current( $meta_value );
1931
							$log_meta['_give_log_form_id'] = $post->post_parent;
1932
							break;
1933
1934
						default:
1935
							$log_meta[ $meta_key ] = current( $meta_value );
1936
					}
1937
				}
1938
			}
1939
1940
			if ( 'api_request' === $term_name ) {
1941
				$log_meta['_give_log_api_query'] = $post->post_excerpt;
1942
			}
1943
1944
			$wpdb->insert( "{$wpdb->prefix}give_logs", $log_data );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
1945
1946 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...
1947
				foreach ( $log_meta as $meta_key => $meta_value ) {
1948
					Give()->logs->logmeta_db->update_meta( $post->ID, $meta_key, $meta_value );
1949
				}
1950
			}
1951
1952
			$logIDs[] = $post->ID;
1953
		}// End while().
1954
1955
		wp_reset_postdata();
1956
	} else {
1957
		// @todo: Delete terms and taxonomy after releases 2.0.
1958
		/*$terms = get_terms( 'give_log_type', array( 'fields' => 'ids', 'hide_empty' => false ) );
1959
		if ( ! empty( $terms ) ) {
1960
			foreach ( $terms as $term ) {
1961
				wp_delete_term( $term, 'give_log_type' );
1962
			}
1963
		}*/
1964
1965
		// @todo: Delete logs after releases 2.0.
1966
		/*$logIDs = get_posts( array(
1967
				'order'          => 'DESC',
1968
				'post_type'      => 'give_log',
1969
				'post_status'    => 'any',
1970
				'posts_per_page' => - 1,
1971
				'fields'         => 'ids',
1972
			)
1973
		);*/
1974
1975
		/*if ( ! empty( $logIDs ) ) {
1976
			foreach ( $logIDs as $log ) {
1977
				// Delete term relationship and posts.
1978
				wp_delete_object_term_relationships( $log, 'give_log_type' );
1979
				wp_delete_post( $log, true );
1980
			}
1981
		}*/
1982
1983
		// @todo: Unregister taxonomy after releases 2.0.
1984
		/*unregister_taxonomy( 'give_log_type' );*/
1985
1986
		// Delete log cache.
1987
		Give()->logs->delete_cache();
1988
1989
		// No more forms found, finish up.
1990
		give_set_upgrade_complete( 'v20_logs_upgrades' );
1991
	}
1992
}
1993
1994
1995
/**
1996
 * Move payment and form metadata to new table
1997
 *
1998
 * @since  2.0
1999
 * @return void
2000
 */
2001
function give_v20_move_metadata_into_new_table_callback() {
2002
	global $wpdb;
2003
	$give_updates = Give_Updates::get_instance();
2004
2005
	// form query
2006
	$payments = new WP_Query( array(
2007
			'paged'          => $give_updates->step,
2008
			'status'         => 'any',
2009
			'order'          => 'ASC',
2010
			'post_type'      => array( 'give_forms', 'give_payment' ),
2011
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
2012
		)
2013
	);
2014
2015
	if ( $payments->have_posts() ) {
2016
		$give_updates->set_percentage( $payments->found_posts, $give_updates->step * 100 );
2017
2018
		while ( $payments->have_posts() ) {
2019
			$payments->the_post();
2020
			global $post;
2021
2022
			$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...
2023
				$wpdb->prepare(
2024
					"SELECT * FROM $wpdb->postmeta where post_id=%d",
2025
					get_the_ID()
2026
				),
2027
				ARRAY_A
2028
			);
2029
2030 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...
2031
				foreach ( $meta_data as $index => $data ) {
2032
					// Check for duplicate meta values.
2033
					if ( $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM " . ( 'give_forms' === $post->post_type ? $wpdb->formmeta : $wpdb->paymentmeta ) . " WHERE meta_id=%d", $data['meta_id'] ), ARRAY_A ) ) {
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

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

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

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

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

Loading history...
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
Coding Style Comprehensibility introduced by
The string literal SELECT * FROM does not require double quotes, as per coding-style, please use single quotes.

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

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

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

<?php

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

print $doubleQuoted;

will print an indented: Single is Value

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

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

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

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

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

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

<?php

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

print $doubleQuoted;

will print an indented: Single is Value

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

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

Loading history...
2034
						continue;
2035
					}
2036
2037
					switch ( $post->post_type ) {
2038
						case 'give_forms':
2039
							$data['form_id'] = $data['post_id'];
2040
							unset( $data['post_id'] );
2041
2042
							Give()->form_meta->insert( $data );
2043
							// @todo: delete form meta from post meta table after releases 2.0.
2044
							/*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2045
2046
							break;
2047
2048
						case 'give_payment':
2049
							$data['payment_id'] = $data['post_id'];
2050
							unset( $data['post_id'] );
2051
2052
							Give()->payment_meta->insert( $data );
2053
2054
							// @todo: delete donation meta from post meta table after releases 2.0.
2055
							/*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2056
2057
							break;
2058
					}
2059
				}
2060
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2061
2062
		}// End while().
2063
2064
		wp_reset_postdata();
2065
	} else {
2066
		// No more forms found, finish up.
2067
		give_set_upgrade_complete( 'v20_move_metadata_into_new_table' );
2068
	}
2069
2070
}
2071
2072
/**
2073
 * Upgrade routine for splitting donor name into first name and last name.
2074
 *
2075
 * @since 2.0
2076
 *
2077
 * @return void
2078
 */
2079
function give_v20_upgrades_donor_name() {
2080
	/* @var Give_Updates $give_updates */
2081
	$give_updates = Give_Updates::get_instance();
2082
2083
	$donors = Give()->donors->get_donors( array(
2084
		'paged'  => $give_updates->step,
2085
		'number' => 100,
2086
	) );
2087
2088
	if ( $donors ) {
2089
		$give_updates->set_percentage( count( $donors ), $give_updates->step * 100 );
2090
		// Loop through Donors
2091
		foreach ( $donors as $donor ) {
2092
2093
			$donor_name       = explode( ' ', $donor->name, 2 );
2094
			$donor_first_name = Give()->donor_meta->get_meta( $donor->id, '_give_donor_first_name' );
2095
			$donor_last_name  = Give()->donor_meta->get_meta( $donor->id, '_give_donor_last_name' );
2096
2097
			// If first name meta of donor is not created, then create it.
2098 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...
2099
				Give()->donor_meta->add_meta( $donor->id, '_give_donor_first_name', $donor_name[0] );
2100
			}
2101
2102
			// If last name meta of donor is not created, then create it.
2103 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...
2104
				Give()->donor_meta->add_meta( $donor->id, '_give_donor_last_name', $donor_name[1] );
2105
			}
2106
2107
			// If Donor is connected with WP User then update user meta.
2108 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...
2109
				if ( isset( $donor_name[0] ) ) {
2110
					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...
2111
				}
2112
				if ( isset( $donor_name[1] ) ) {
2113
					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...
2114
				}
2115
			}
2116
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2117
2118
	} else {
2119
		// The Update Ran.
2120
		give_set_upgrade_complete( 'v20_upgrades_donor_name' );
2121
	}
2122
2123
}
2124
2125
/**
2126
 * Upgrade routine for user addresses.
2127
 *
2128
 * @since 2.0
2129
 * @global wpdb $wpdb
2130
 *
2131
 * @return void
2132
 */
2133
function give_v20_upgrades_user_address() {
2134
	global $wpdb;
2135
2136
	/* @var Give_Updates $give_updates */
2137
	$give_updates = Give_Updates::get_instance();
2138
2139
	/* @var WP_User_Query $user_query */
2140
	$user_query = new WP_User_Query(
2141
		array(
2142
			'number' => 100,
2143
			'paged'  => $give_updates->step,
2144
		)
2145
	);
2146
2147
	$users = $user_query->get_results();
2148
2149
	if ( $users ) {
2150
		$give_updates->set_percentage( $user_query->get_total(), $give_updates->step * 100 );
2151
2152
		// Loop through Donors
2153
		foreach ( $users as $user ) {
2154
			/* @var Give_Donor $donor */
2155
			$donor = new Give_Donor( $user->ID, true );
2156
2157
			if ( ! $donor->id ) {
2158
				continue;
2159
			}
2160
2161
			$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...
2162
				$wpdb->prepare(
2163
					"
2164
					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...
2165
					WHERE user_id=%s
2166
					AND meta_key=%s
2167
					",
2168
					$user->ID,
2169
					'_give_user_address'
2170
				)
2171
			);
2172
2173 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...
2174
				$address = maybe_unserialize( $address );
2175
				$donor->add_address( 'personal', $address );
2176
				$donor->add_address( 'billing[]', $address );
2177
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
2178
2179
				// @todo: delete _give_user_address from user meta after releases 2.0.
2180
				/*delete_user_meta( $user->ID, '_give_user_address' );*/
2181
			}
2182
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2183
2184
	} else {
2185
		// The Update Ran.
2186
		give_set_upgrade_complete( 'v20_upgrades_user_address' );
2187
	}
2188
2189
}
2190
2191
/**
2192
 * Upgrade logs data.
2193
 *
2194
 * @since  2.0
2195
 * @global wpdb $wpdb
2196
 * @return void
2197
 */
2198
function give_v20_rename_donor_tables_callback() {
2199
	global $wpdb;
2200
2201
	/* @var Give_Updates $give_updates */
2202
	$give_updates = Give_Updates::get_instance();
2203
2204
	$tables = array(
2205
		"{$wpdb->prefix}give_customers"    => "{$wpdb->prefix}give_donors",
2206
		"{$wpdb->prefix}give_customermeta" => "{$wpdb->prefix}give_donormeta",
2207
	);
2208
2209
	// Alter customer table
2210
	foreach ( $tables as $old_table => $new_table ) {
2211
		if (
2212
			$wpdb->query( $wpdb->prepare( "SHOW TABLES LIKE %s", $old_table ) ) &&
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
Coding Style Comprehensibility introduced by
The string literal SHOW TABLES LIKE %s does not require double quotes, as per coding-style, please use single quotes.

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

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

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

<?php

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

print $doubleQuoted;

will print an indented: Single is Value

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

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

Loading history...
2213
			! $wpdb->query( $wpdb->prepare( "SHOW TABLES LIKE %s", $new_table ) )
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
Coding Style Comprehensibility introduced by
The string literal SHOW TABLES LIKE %s does not require double quotes, as per coding-style, please use single quotes.

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

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

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

<?php

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

print $doubleQuoted;

will print an indented: Single is Value

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

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

Loading history...
2214
		) {
2215
			$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...
2216
2217
			if ( "{$wpdb->prefix}give_donormeta" === $new_table ) {
2218
				$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...
2219
			}
2220
		}
2221
	}
2222
2223
	$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...
2224
2225
	// No more forms found, finish up.
2226
	give_set_upgrade_complete( 'v20_rename_donor_tables' );
2227
2228
	// Re initiate donor classes.
2229
	Give()->donors     = new Give_DB_Donors();
2230
	Give()->donor_meta = new Give_DB_Donor_Meta();
2231
}
2232
2233
2234
/**
2235
 * Create missing meta tables.
2236
 *
2237
 * @since  2.0.1
2238
 * @global wpdb $wpdb
2239
 * @return void
2240
 */
2241
function give_v201_create_tables() {
2242
	global $wpdb;
2243
2244
	if ( ! $wpdb->query( $wpdb->prepare( "SHOW TABLES LIKE %s", "{$wpdb->prefix}give_paymentmeta" ) ) ) {
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
Coding Style Comprehensibility introduced by
The string literal SHOW TABLES LIKE %s does not require double quotes, as per coding-style, please use single quotes.

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

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

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

<?php

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

print $doubleQuoted;

will print an indented: Single is Value

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

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

Loading history...
2245
		Give()->payment_meta->create_table();
2246
	}
2247
2248
	if ( ! $wpdb->query( $wpdb->prepare( "SHOW TABLES LIKE %s", "{$wpdb->prefix}give_formmeta" ) ) ) {
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
Coding Style Comprehensibility introduced by
The string literal SHOW TABLES LIKE %s does not require double quotes, as per coding-style, please use single quotes.

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

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

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

<?php

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

print $doubleQuoted;

will print an indented: Single is Value

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

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

Loading history...
2249
		Give()->form_meta->create_table();
2250
	}
2251
2252
	if ( ! $wpdb->query( $wpdb->prepare( "SHOW TABLES LIKE %s", "{$wpdb->prefix}give_logs" ) ) ) {
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
Coding Style Comprehensibility introduced by
The string literal SHOW TABLES LIKE %s does not require double quotes, as per coding-style, please use single quotes.

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

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

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

<?php

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

print $doubleQuoted;

will print an indented: Single is Value

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

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

Loading history...
2253
		Give()->logs->log_db->create_table();
2254
	}
2255
2256
	if ( ! $wpdb->query( $wpdb->prepare( "SHOW TABLES LIKE %s", "{$wpdb->prefix}give_logmeta" ) ) ) {
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
Coding Style Comprehensibility introduced by
The string literal SHOW TABLES LIKE %s does not require double quotes, as per coding-style, please use single quotes.

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

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

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

<?php

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

print $doubleQuoted;

will print an indented: Single is Value

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

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

Loading history...
2257
		Give()->logs->logmeta_db->create_table();
2258
	}
2259
}
2260
2261
/**
2262
 * Upgrade payment metadata for new metabox settings.
2263
 *
2264
 * @since  2.0.1
2265
 * @global wpdb $wpdb
2266
 * @return void
2267
 */
2268
function give_v201_upgrades_payment_metadata_callback() {
2269
	global $wpdb, $post;
2270
	$give_updates = Give_Updates::get_instance();
2271
	give_v201_create_tables();
2272
2273
	$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...
2274
		"
2275
			SELECT ID FROM $wpdb->posts
2276
			WHERE 1=1
2277
			AND ( 
2278
  				$wpdb->posts.post_date >= '2018-01-08 00:00:00'
2279
			)
2280
			AND $wpdb->posts.post_type = 'give_payment'
2281
			AND {$wpdb->posts}.post_status IN ('" . implode( "','", array_keys( give_get_payment_statuses() ) ) . "')
2282
			ORDER BY $wpdb->posts.post_date ASC 
2283
			LIMIT 100
2284
			OFFSET " . $give_updates->get_offset( 100 )
2285
	);
2286
2287
	if ( ! empty( $payments ) ) {
2288
		$give_updates->set_percentage( give_get_total_post_type_count( 'give_payment' ), ( $give_updates->step * 100 ) );
2289
2290
		foreach ( $payments as $payment_id ) {
2291
			$post = get_post( $payment_id );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
2292
			setup_postdata( $post );
2293
2294
			// Do not add new meta keys if already refactored.
2295
			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...
2296
				continue;
2297
			}
2298
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
2299
2300
			// Split _give_payment_meta meta.
2301
			// @todo Remove _give_payment_meta after releases 2.0
2302
			$payment_meta = give_get_meta( $post->ID, '_give_payment_meta', true );
2303
2304
			if ( ! empty( $payment_meta ) ) {
2305
				_give_20_bc_split_and_save_give_payment_meta( $post->ID, $payment_meta );
2306
			}
2307
2308
			$deprecated_meta_keys = array(
2309
				'_give_payment_customer_id' => '_give_payment_donor_id',
2310
				'_give_payment_user_email'  => '_give_payment_donor_email',
2311
				'_give_payment_user_ip'     => '_give_payment_donor_ip',
2312
			);
2313
2314 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...
2315
				// Do not add new meta key if already exist.
2316
				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...
2317
					continue;
2318
				}
2319
2320
				$wpdb->insert(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
2321
					$wpdb->postmeta,
2322
					array(
2323
						'post_id'    => $post->ID,
2324
						'meta_key'   => $new_meta_key,
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
2325
						'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...
2326
					)
2327
				);
2328
			}
2329
2330
			// Bailout
2331 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...
2332
				/* @var Give_Donor $donor */
2333
				$donor = new Give_Donor( $donor_id );
2334
2335
				$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...
2336
				$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...
2337
				$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...
2338
				$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...
2339
				$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...
2340
				$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...
2341
2342
				// Save address.
2343
				$donor->add_address( 'billing[]', $address );
2344
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2345
2346
		}// End while().
2347
2348
		wp_reset_postdata();
2349
	} else {
2350
		// @todo Delete user id meta after releases 2.0
2351
		// $wpdb->get_var( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key=%s", '_give_payment_user_id' ) );
2352
2353
		// No more forms found, finish up.
2354
		give_set_upgrade_complete( 'v201_upgrades_payment_metadata' );
2355
	}
2356
}
2357
2358
/**
2359
 * Move payment and form metadata to new table
2360
 *
2361
 * @since  2.0.1
2362
 * @return void
2363
 */
2364
function give_v201_move_metadata_into_new_table_callback() {
2365
	global $wpdb, $post;
2366
	$give_updates = Give_Updates::get_instance();
2367
	give_v201_create_tables();
2368
2369
	$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...
2370
		"
2371
			SELECT ID FROM $wpdb->posts 
2372
			WHERE 1=1
2373
			AND ( $wpdb->posts.post_type = 'give_payment' OR $wpdb->posts.post_type = 'give_forms' )
2374
			AND {$wpdb->posts}.post_status IN ('" . implode( "','", array_keys( give_get_payment_statuses() ) ) . "')
2375
			ORDER BY $wpdb->posts.post_date ASC 
2376
			LIMIT 100
2377
			OFFSET " . $give_updates->get_offset( 100 )
2378
	);
2379
2380
	if ( ! empty( $payments ) ) {
2381
		$give_updates->set_percentage( give_get_total_post_type_count( array(
0 ignored issues
show
Documentation introduced by
array('give_forms', 'give_payment') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

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

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

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

function acceptsInteger($int) { }

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

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
2382
			'give_forms',
2383
			'give_payment',
2384
		) ), $give_updates->step * 100 );
2385
2386
		foreach ( $payments as $payment_id ) {
2387
			$post = get_post( $payment_id );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
2388
			setup_postdata( $post );
2389
2390
			$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...
2391
				$wpdb->prepare(
2392
					"SELECT * FROM $wpdb->postmeta where post_id=%d",
2393
					get_the_ID()
2394
				),
2395
				ARRAY_A
2396
			);
2397
2398 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...
2399
				foreach ( $meta_data as $index => $data ) {
2400
					// Check for duplicate meta values.
2401
					if ( $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM " . ( 'give_forms' === $post->post_type ? $wpdb->formmeta : $wpdb->paymentmeta ) . " WHERE meta_id=%d", $data['meta_id'] ), ARRAY_A ) ) {
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

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

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

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

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

Loading history...
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
Coding Style Comprehensibility introduced by
The string literal SELECT * FROM does not require double quotes, as per coding-style, please use single quotes.

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

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

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

<?php

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

print $doubleQuoted;

will print an indented: Single is Value

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

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

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

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

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

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

<?php

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

print $doubleQuoted;

will print an indented: Single is Value

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

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

Loading history...
2402
						continue;
2403
					}
2404
2405
					switch ( $post->post_type ) {
2406
						case 'give_forms':
2407
							$data['form_id'] = $data['post_id'];
2408
							unset( $data['post_id'] );
2409
2410
							Give()->form_meta->insert( $data );
2411
							// @todo: delete form meta from post meta table after releases 2.0.
2412
							/*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2413
2414
							break;
2415
2416
						case 'give_payment':
2417
							$data['payment_id'] = $data['post_id'];
2418
							unset( $data['post_id'] );
2419
2420
							Give()->payment_meta->insert( $data );
2421
2422
							// @todo: delete donation meta from post meta table after releases 2.0.
2423
							/*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2424
2425
							break;
2426
					}
2427
				}
2428
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2429
2430
		}// End while().
2431
2432
		wp_reset_postdata();
2433
	} else {
2434
		// No more forms found, finish up.
2435
		give_set_upgrade_complete( 'v201_move_metadata_into_new_table' );
2436
	}
2437
2438
}
2439
2440
/**
2441
 * Move data to new log table.
2442
 *
2443
 * @since  2.0.1
2444
 * @return void
2445
 */
2446
function give_v201_logs_upgrades_callback() {
2447
	global $wpdb, $post;
2448
	$give_updates = Give_Updates::get_instance();
2449
	give_v201_create_tables();
2450
2451
	$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...
2452
		"
2453
			SELECT ID FROM $wpdb->posts 
2454
			WHERE 1=1
2455
			AND $wpdb->posts.post_type = 'give_log'
2456
			AND {$wpdb->posts}.post_status IN ('" . implode( "','", array_keys( give_get_payment_statuses() ) ) . "')
2457
			ORDER BY $wpdb->posts.post_date ASC 
2458
			LIMIT 100
2459
			OFFSET " . $give_updates->get_offset( 100 )
2460
	);
2461
2462
	if ( ! empty( $logs ) ) {
2463
		$give_updates->set_percentage( give_get_total_post_type_count( 'give_log' ), $give_updates->step * 100 );
2464
2465
		foreach ( $logs as $log_id ) {
2466
			$post = get_post( $log_id );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
2467
			setup_postdata( $post );
2468
2469
			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...
2470
				continue;
2471
			}
2472
2473
			$term      = get_the_terms( $post->ID, 'give_log_type' );
2474
			$term      = ! is_wp_error( $term ) && ! empty( $term ) ? $term[0] : array();
2475
			$term_name = ! empty( $term ) ? $term->slug : '';
2476
2477
			$log_data = array(
2478
				'ID'           => $post->ID,
2479
				'log_title'    => $post->post_title,
2480
				'log_content'  => $post->post_content,
2481
				'log_parent'   => 0,
2482
				'log_type'     => $term_name,
2483
				'log_date'     => $post->post_date,
2484
				'log_date_gmt' => $post->post_date_gmt,
2485
			);
2486
			$log_meta = array();
2487
2488 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...
2489
				foreach ( $old_log_meta as $meta_key => $meta_value ) {
2490
					switch ( $meta_key ) {
2491
						case '_give_log_payment_id':
2492
							$log_data['log_parent']        = current( $meta_value );
2493
							$log_meta['_give_log_form_id'] = $post->post_parent;
2494
							break;
2495
2496
						default:
2497
							$log_meta[ $meta_key ] = current( $meta_value );
2498
					}
2499
				}
2500
			}
2501
2502
			if ( 'api_request' === $term_name ) {
2503
				$log_meta['_give_log_api_query'] = $post->post_excerpt;
2504
			}
2505
2506
			$wpdb->insert( "{$wpdb->prefix}give_logs", $log_data );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
2507
2508 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...
2509
				foreach ( $log_meta as $meta_key => $meta_value ) {
2510
					Give()->logs->logmeta_db->update_meta( $post->ID, $meta_key, $meta_value );
2511
				}
2512
			}
2513
2514
			$logIDs[] = $post->ID;
2515
		}// End while().
2516
2517
		wp_reset_postdata();
2518
	} else {
2519
		// Delete log cache.
2520
		Give()->logs->delete_cache();
2521
2522
		// No more forms found, finish up.
2523
		give_set_upgrade_complete( 'v201_logs_upgrades' );
2524
	}
2525
}
2526
2527
2528
/**
2529
 * Add missing donor.
2530
 *
2531
 * @since  2.0.1
2532
 * @return void
2533
 */
2534
function give_v201_add_missing_donors_callback() {
2535
	global $wpdb;
2536
	give_v201_create_tables();
2537
2538
	if ( $wpdb->query( $wpdb->prepare( "SHOW TABLES LIKE %s", "{$wpdb->prefix}give_customers" ) ) ) {
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
Coding Style Comprehensibility introduced by
The string literal SHOW TABLES LIKE %s does not require double quotes, as per coding-style, please use single quotes.

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

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

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

<?php

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

print $doubleQuoted;

will print an indented: Single is Value

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

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

Loading history...
2539
		$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...
2540
		$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...
2541
		$donor_data = array();
2542
2543
		if ( $missing_donors = array_diff( $customers, $donors ) ) {
2544
			foreach ( $missing_donors as $donor_id ) {
2545
				$donor_data[] = array(
2546
					'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...
2547
					'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...
2548
2549
				);
2550
			}
2551
		}
2552
2553
		if ( ! empty( $donor_data ) ) {
2554
			$donor_table_name      = Give()->donors->table_name;
2555
			$donor_meta_table_name = Give()->donor_meta->table_name;
2556
2557
			Give()->donors->table_name     = "{$wpdb->prefix}give_donors";
2558
			Give()->donor_meta->table_name = "{$wpdb->prefix}give_donormeta";
2559
2560
			foreach ( $donor_data as $donor ) {
2561
				$donor['info'][0] = (array) $donor['info'][0];
2562
2563
				// Prevent duplicate meta id issue.
2564
				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...
2565
					continue;
2566
				}
2567
2568
				$donor_id = Give()->donors->add( $donor['info'][0] );
2569
2570
				if ( ! empty( $donor['meta'] ) ) {
2571
					foreach ( $donor['meta'] as $donor_meta ) {
2572
						$donor_meta = (array) $donor_meta;
2573
2574
						// Prevent duplicate meta id issue.
2575
						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...
2576
							unset( $donor_meta['meta_id'] );
2577
						}
2578
2579
						$donor_meta['donor_id'] = $donor_meta['customer_id'];
2580
						unset( $donor_meta['customer_id'] );
2581
2582
						Give()->donor_meta->insert( $donor_meta );
2583
					}
2584
				}
2585
2586
				/**
2587
				 * Fix donor name and address
2588
				 */
2589
				$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...
2590
					$wpdb->prepare(
2591
						"
2592
					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...
2593
					WHERE user_id=%s
2594
					AND meta_key=%s
2595
					",
2596
						$donor['info'][0]['user_id'],
2597
						'_give_user_address'
2598
					)
2599
				);
2600
2601
				$donor = new Give_Donor( $donor_id );
2602
2603 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...
2604
					$address = maybe_unserialize( $address );
2605
					$donor->add_address( 'personal', $address );
2606
					$donor->add_address( 'billing[]', $address );
2607
				}
2608
2609
				$donor_name       = explode( ' ', $donor->name, 2 );
2610
				$donor_first_name = Give()->donor_meta->get_meta( $donor->id, '_give_donor_first_name' );
2611
				$donor_last_name  = Give()->donor_meta->get_meta( $donor->id, '_give_donor_last_name' );
2612
2613
				// If first name meta of donor is not created, then create it.
2614 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...
2615
					Give()->donor_meta->add_meta( $donor->id, '_give_donor_first_name', $donor_name[0] );
2616
				}
2617
2618
				// If last name meta of donor is not created, then create it.
2619 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...
2620
					Give()->donor_meta->add_meta( $donor->id, '_give_donor_last_name', $donor_name[1] );
2621
				}
2622
2623
				// If Donor is connected with WP User then update user meta.
2624 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...
2625
					if ( isset( $donor_name[0] ) ) {
2626
						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...
2627
					}
2628
					if ( isset( $donor_name[1] ) ) {
2629
						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...
2630
					}
2631
				}
2632
			}
2633
2634
			Give()->donors->table_name     = $donor_table_name;
2635
			Give()->donor_meta->table_name = $donor_meta_table_name;
2636
		}
2637
	}
2638
2639
	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...
2640
	give_set_upgrade_complete( 'v201_add_missing_donors' );
2641
}
2642
2643
2644
/**
2645
 * Version 2.0.3 automatic updates
2646
 *
2647
 * @since 2.0.3
2648
 */
2649
function give_v203_upgrades() {
2650
	global $wpdb;
2651
2652
	// Do not auto load option.
2653
	$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...
2654
2655
	// Remove from cache.
2656
	$all_options = wp_load_alloptions();
2657
2658
	if ( isset( $all_options['give_completed_upgrades'] ) ) {
2659
		unset( $all_options['give_completed_upgrades'] );
2660
		wp_cache_set( 'alloptions', $all_options, 'options' );
2661
	}
2662
2663
}
2664
2665
2666
/**
2667
 * Version 2.2.0 automatic updates
2668
 *
2669
 * @since 2.2.0
2670
 */
2671
function give_v220_upgrades(){
2672
	global $wpdb;
2673
2674
	/**
2675
	 * Update 1
2676
	 *
2677
	 * Delete wp session data
2678
	 */
2679
	give_v220_delete_wp_session_data();
2680
2681
	/**
2682
	 * Update 2
2683
	 *
2684
	 * Rename payment table
2685
	 */
2686
	give_v220_rename_donation_meta_type_callback();
2687
2688
	/**
2689
	 * Update 2
2690
	 *
2691
	 * Set autoload to no to reduce result weight from WordPress query
2692
	 */
2693
2694
	$options = array(
2695
		'give_settings',
2696
		'give_version',
2697
		'give_version_upgraded_from',
2698
		'give_default_api_version',
2699
		'give_site_address_before_migrate',
2700
		'_give_table_check',
2701
		'give_recently_activated_addons',
2702
		'give_is_addon_activated',
2703
		'give_last_paypal_ipn_received',
2704
		'give_use_php_sessions',
2705
		'give_subscriptions',
2706
		'_give_subscriptions_edit_last'
2707
	);
2708
2709
	// Add all table version option name
2710
	// Add banner option *_active_by_user
2711
	$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...
2712
		"
2713
		SELECT option_name
2714
		FROM $wpdb->options
2715
		WHERE option_name like '%give%'
2716
		AND (
2717
			option_name like '%_db_version%'
2718
			OR option_name like '%_active_by_user%'
2719
			OR option_name like '%_license_active%'
2720
		)
2721
		"
2722
	);
2723
2724
	if( ! empty( $option_like ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
2725
		$options = array_merge( $options, $option_like );
2726
	}
2727
2728
	$options_str =  '\'' . implode( "','", $options ) . '\'';
0 ignored issues
show
introduced by
Expected 1 space after "="; 2 found
Loading history...
2729
2730
	$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...
2731
		"
2732
		UPDATE $wpdb->options
2733
		SET autoload = 'no'
2734
		WHERE option_name IN ( {$options_str} )
2735
		"
2736
	);
2737
}
2738
2739
/**
2740
 * Version 2.2.1 automatic updates
2741
 *
2742
 * @since 2.2.1
2743
 */
2744
function give_v221_upgrades() {
2745
	global $wpdb;
2746
2747
	/**
2748
	 * Update  1
2749
	 *
2750
	 * Change column length
2751
	 */
2752
	$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...
2753
}
2754
2755
/**
2756
 * Upgrade routine for 2.1 to set form closed status for all the donation forms.
2757
 *
2758
 * @since 2.1
2759
 */
2760
function give_v210_verify_form_status_upgrades_callback() {
2761
2762
	$give_updates = Give_Updates::get_instance();
2763
2764
	// form query.
2765
	$donation_forms = new WP_Query( array(
2766
			'paged'          => $give_updates->step,
2767
			'status'         => 'any',
2768
			'order'          => 'ASC',
2769
			'post_type'      => 'give_forms',
2770
			'posts_per_page' => 20,
2771
		)
2772
	);
2773
2774
	if ( $donation_forms->have_posts() ) {
2775
		$give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
2776
2777
		while ( $donation_forms->have_posts() ) {
2778
			$donation_forms->the_post();
2779
			$form_id = get_the_ID();
2780
2781
			$form_closed_status = give_get_meta( $form_id, '_give_form_status', true );
2782
			if ( empty( $form_closed_status ) ) {
2783
				give_set_form_closed_status( $form_id );
2784
			}
2785
		}
2786
2787
		/* Restore original Post Data */
2788
		wp_reset_postdata();
2789
2790
	} else {
2791
2792
		// The Update Ran.
2793
		give_set_upgrade_complete( 'v210_verify_form_status_upgrades' );
2794
	}
2795
}
2796
2797
/**
2798
 * Upgrade routine for 2.1.3 to delete meta which is not attach to any donation.
2799
 *
2800
 * @since 2.1
2801
 */
2802
function give_v213_delete_donation_meta_callback() {
2803
	global $wpdb;
2804
	$give_updates        = Give_Updates::get_instance();
2805
	$donation_meta_table = Give()->payment_meta->table_name;
2806
2807
	$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...
2808
		"
2809
		SELECT DISTINCT payment_id
2810
		FROM {$donation_meta_table}
2811
		LIMIT 20
2812
		OFFSET {$give_updates->get_offset( 20 )}
2813
		"
2814
	);
2815
2816
	if ( ! empty( $donations ) ) {
2817
		foreach ( $donations as $donation ) {
2818
			$donation_obj = get_post( $donation );
2819
2820
			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...
2821
				Give()->payment_meta->delete_all_meta( $donation );
2822
			}
2823
		}
2824
	} else {
2825
2826
		// The Update Ran.
2827
		give_set_upgrade_complete( 'v213_delete_donation_meta' );
2828
	}
2829
}
2830
2831
/**
2832
 * Rename donation meta type
2833
 *
2834
 * @see https://github.com/restrictcontentpro/restrict-content-pro/issues/1656
2835
 *
2836
 * @since 2.2.0
2837
 */
2838
function give_v220_rename_donation_meta_type_callback(){
2839
	global $wpdb;
2840
2841
	// Check upgrade before running.
2842
	if(
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
2843
		give_has_upgrade_completed( 'v220_rename_donation_meta_type' )
2844
		|| ! $wpdb->query( $wpdb->prepare( "SHOW TABLES LIKE %s", "{$wpdb->prefix}give_paymentmeta" ) )
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
Coding Style Comprehensibility introduced by
The string literal SHOW TABLES LIKE %s does not require double quotes, as per coding-style, please use single quotes.

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

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

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

<?php

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

print $doubleQuoted;

will print an indented: Single is Value

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

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

Loading history...
2845
	) {
2846
		return;
2847
	}
2848
2849
	$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...
2850
	$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...
2851
2852
	give_set_upgrade_complete('v220_rename_donation_meta_type');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
2853
}
2854
2855
/**
2856
 * Adds 'view_give_payments' capability to 'give_manager' user role.
2857
 *
2858
 * @since 2.1.5
2859
 */
2860
function give_v215_update_donor_user_roles_callback() {
2861
2862
	$role = get_role( 'give_manager' );
2863
	$role->add_cap( 'view_give_payments' );
2864
2865
	give_set_upgrade_complete( 'v215_update_donor_user_roles' );
2866
}
2867
2868
2869
2870
/**
2871
 * Remove all wp session data from the options table, regardless of expiration.
2872
 *
2873
 * @since 2.2.0
2874
 *
2875
 * @global wpdb $wpdb
2876
 */
2877
function give_v220_delete_wp_session_data(){
2878
	global $wpdb;
2879
2880
	$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...
2881
}
2882
2883
2884
/**
2885
 * Update donor meta
2886
 * Set "_give_anonymous_donor" meta key to "0" if not exist
2887
 *
2888
 *
2889
 * @since 2.2.4
2890
 */
2891
function give_v224_update_donor_meta_callback() {
2892
	/* @var Give_Updates $give_updates */
2893
	$give_updates = Give_Updates::get_instance();
2894
2895
	$donor_count = Give()->donors->count( array(
2896
		'number' => -1,
2897
	) );
2898
2899
	$donors = Give()->donors->get_donors( array(
2900
		'paged'  => $give_updates->step,
2901
		'number' => 100,
2902
	) );
2903
2904
	if ( $donors ) {
2905
		$give_updates->set_percentage( $donor_count, $give_updates->step * 100 );
2906
		// Loop through Donors
2907
		foreach ( $donors as $donor ) {
2908
			$anonymous_metadata = Give()->donor_meta->get_meta( $donor->id, '_give_anonymous_donor', true );
2909
2910
			// If first name meta of donor is not created, then create it.
2911
			if ( ! in_array( $anonymous_metadata, array( '0', '1' ) ) ) {
2912
				Give()->donor_meta->add_meta( $donor->id, '_give_anonymous_donor', '0' );
2913
			}
2914
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2915
2916
	} else {
2917
		// The Update Ran.
2918
		give_set_upgrade_complete( 'v224_update_donor_meta' );
2919
	}
2920
}
2921
2922
2923
/**
2924
 * Move donor notes to comment table
2925
 *
2926
 * @since 2.3.0
2927
 */
2928
function give_v230_move_donor_note_callback() {
2929
	/* @var Give_Updates $give_updates */
2930
	$give_updates = Give_Updates::get_instance();
2931
2932
	$donor_count = Give()->donors->count( array(
2933
		'number' => - 1,
2934
	) );
2935
2936
	$donors = Give()->donors->get_donors( array(
2937
		'paged'  => $give_updates->step,
2938
		'number' => 100,
2939
	) );
2940
2941
	if ( $donors ) {
2942
		$give_updates->set_percentage( $donor_count, $give_updates->step * 100 );
2943
		// Loop through Donors
2944
		foreach ( $donors as $donor ) {
2945
			$notes = trim( Give()->donors->get_column( 'notes', $donor->id ) );
2946
2947
			// If first name meta of donor is not created, then create it.
2948
			if ( ! empty( $notes ) ) {
2949
				$notes = array_values( array_filter( array_map( 'trim', explode( "\n", $notes ) ), 'strlen' ) );
2950
2951
				foreach ( $notes as $note ) {
2952
					$note      = array_map( 'trim', explode( '-', $note ) );
2953
					$timestamp = strtotime( $note[0] );
2954
2955
					Give()->comment->db->add(
2956
						array(
2957
							'comment_content'  => $note[1],
2958
							'comment_date'     => date( 'Y-m-d H:i:s', $timestamp ),
2959
							'comment_date_gmt' => get_gmt_from_date( date( 'Y-m-d H:i:s', $timestamp ) ),
2960
							'comment_parent'   => "donor_{$donor->id}",
2961
						)
2962
					);
2963
				}
2964
			}
2965
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2966
2967
	} else {
2968
		// The Update Ran.
2969
		give_set_upgrade_complete( 'v230_move_donor_note' );
2970
	}
2971
}
2972