Test Failed
Push — release/2.3.0 ( 9db3c8 )
by Ravinder
1087:27 queued 1077:41
created

upgrade-functions.php ➔ give_v224_update_donor_meta_callback()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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