Test Failed
Push — feature/update-process ( bd4647 )
by Ravinder
04:45
created

upgrade-functions.php ➔ give_v201_create_tables_callback()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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