Test Failed
Pull Request — master (#2814)
by Devin
05:29
created

upgrade-functions.php ➔ give_v203_upgrades()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Loading history...
90
91
		case version_compare( $give_version, '2.0.2', '<' ) :
92
			// Remove 2.0.1 update to rerun on 2.0.2
93
			$completed_upgrades = give_get_completed_upgrades();
94
			$v201_updates = array(
95
				'v201_upgrades_payment_metadata',
96
				'v201_add_missing_donors',
97
				'v201_move_metadata_into_new_table',
98
				'v201_logs_upgrades'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
99
			);
100
101
			foreach ( $v201_updates as $v201_update ) {
102
				if( in_array( $v201_update, $completed_upgrades ) ) {
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...
103
					unset( $completed_upgrades[ array_search( $v201_update, $completed_upgrades )] );
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
104
				}
105
			}
106
107
			update_option( 'give_completed_upgrades', $completed_upgrades );
108
109
			// Do nothing on fresh install.
110
			if( ! doing_action( 'give_upgrades' ) ) {
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...
111
				give_v201_create_tables();
112
				Give_Updates::get_instance()->__health_background_update( Give_Updates::get_instance() );
113
				Give_Updates::$background_updater->dispatch();
114
			}
115
116
			$did_upgrade = true;
0 ignored issues
show
Unused Code introduced by
$did_upgrade is not used, you could remove the assignment.

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

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

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

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

Loading history...
117 1
118
		case version_compare( $give_version, '2.0.3', '<' ) :
119
			give_v203_upgrades();
120
			$did_upgrade = true;
121 1
	}
122
123 1
	if ( $did_upgrade ) {
124
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
125
	}
126
}
127
128
add_action( 'admin_init', 'give_do_automatic_upgrades' );
129
add_action( 'give_upgrades', 'give_do_automatic_upgrades' );
130
131
/**
132
 * Display Upgrade Notices.
133
 *
134
 * IMPORTANT: ALSO UPDATE INSTALL.PHP WITH THE ID OF THE UPGRADE ROUTINE SO IT DOES NOT AFFECT NEW INSTALLS.
135
 *
136
 * @since 1.0
137
 * @since 1.8.12 Update new update process code.
138 1
 *
139
 * @param Give_Updates $give_updates
140
 *
141
 * @return void
142 1
 */
143 1
function give_show_upgrade_notices( $give_updates ) {
144
	// v1.3.2 Upgrades
145
	$give_updates->register(
146 1
		array(
147
			'id'       => 'upgrade_give_payment_customer_id',
148 1
			'version'  => '1.3.2',
149
			'callback' => 'give_v132_upgrade_give_payment_customer_id',
150
		)
151
	);
152
153
	// v1.3.4 Upgrades ensure the user has gone through 1.3.4.
154
	$give_updates->register(
155
		array(
156
			'id'       => 'upgrade_give_offline_status',
157
			'depend'   => 'upgrade_give_payment_customer_id',
158
			'version'  => '1.3.4',
159 1
			'callback' => 'give_v134_upgrade_give_offline_status',
160
		)
161 1
	);
162
163
	// v1.8 form metadata upgrades.
164
	$give_updates->register(
165 1
		array(
166
			'id'       => 'v18_upgrades_form_metadata',
167
			'version'  => '1.8',
168
			'callback' => 'give_v18_upgrades_form_metadata',
169
		)
170
	);
171
172
	// v1.8.9 Upgrades
173
	$give_updates->register(
174
		array(
175
			'id'       => 'v189_upgrades_levels_post_meta',
176
			'version'  => '1.8.9',
177
			'callback' => 'give_v189_upgrades_levels_post_meta_callback',
178
		)
179
	);
180
181
	// v1.8.12 Upgrades
182
	$give_updates->register(
183
		array(
184
			'id'       => 'v1812_update_amount_values',
185
			'version'  => '1.8.12',
186
			'callback' => 'give_v1812_update_amount_values_callback',
187
		)
188
	);
189
190
	// v1.8.12 Upgrades
191
	$give_updates->register(
192
		array(
193
			'id'       => 'v1812_update_donor_purchase_values',
194
			'version'  => '1.8.12',
195
			'callback' => 'give_v1812_update_donor_purchase_value_callback',
196
		)
197
	);
198
199
	// v1.8.13 Upgrades for donor
200
	$give_updates->register(
201
		array(
202
			'id'       => 'v1813_update_donor_user_roles',
203
			'version'  => '1.8.13',
204
			'callback' => 'give_v1813_update_donor_user_roles_callback',
205
		)
206
	);
207
208
	// v1.8.17 Upgrades for donations.
209
	$give_updates->register( array(
210
		'id'       => 'v1817_update_donation_iranian_currency_code',
211
		'version'  => '1.8.17',
212
		'callback' => 'give_v1817_update_donation_iranian_currency_code',
213
	) );
214
215
	// v1.8.17 Upgrades for cleanup of user roles.
216
	$give_updates->register( array(
217
		'id'       => 'v1817_cleanup_user_roles',
218
		'version'  => '1.8.17',
219
		'callback' => 'give_v1817_cleanup_user_roles',
220
	) );
221
222
	// v1.8.18 Upgrades for assigning custom amount to existing set donations.
223
	$give_updates->register( array(
224
		'id'       => 'v1818_assign_custom_amount_set_donation',
225
		'version'  => '1.8.18',
226
		'callback' => 'give_v1818_assign_custom_amount_set_donation',
227
	) );
228
229
	// v1.8.18 Cleanup the Give Worker Role Caps.
230
	$give_updates->register( array(
231
		'id'       => 'v1818_give_worker_role_cleanup',
232
		'version'  => '1.8.18',
233
		'callback' => 'give_v1818_give_worker_role_cleanup',
234
	) );
235
236
	// v2.0.0 Upgrades
237
	$give_updates->register(
238
		array(
239
			'id'       => 'v20_upgrades_form_metadata',
240
			'version'  => '2.0.0',
241
			'callback' => 'give_v20_upgrades_form_metadata_callback',
242
		)
243
	);
244
245
	// v2.0.0 User Address Upgrades
246
	$give_updates->register(
247
		array(
248
			'id'       => 'v20_upgrades_user_address',
249
			'version'  => '2.0.0',
250
			'callback' => 'give_v20_upgrades_user_address',
251
		)
252
	);
253
254
	// v2.0.0 Upgrades
255
	$give_updates->register(
256
		array(
257
			'id'       => 'v20_upgrades_payment_metadata',
258
			'version'  => '2.0.0',
259
			'callback' => 'give_v20_upgrades_payment_metadata_callback',
260
		)
261
	);
262
263
	// v2.0.0 Upgrades
264
	$give_updates->register(
265
		array(
266
			'id'       => 'v20_logs_upgrades',
267
			'version'  => '2.0.0',
268
			'callback' => 'give_v20_logs_upgrades_callback',
269
270
		)
271
	);
272
273
	// v2.0.0 Donor Name Upgrades
274
	$give_updates->register(
275
		array(
276
			'id'       => 'v20_upgrades_donor_name',
277
			'version'  => '2.0.0',
278
			'callback' => 'give_v20_upgrades_donor_name',
279
		)
280
	);
281
282
	// v2.0.0 Upgrades
283
	$give_updates->register(
284
		array(
285
			'id'       => 'v20_move_metadata_into_new_table',
286
			'version'  => '2.0.0',
287
			'callback' => 'give_v20_move_metadata_into_new_table_callback',
288
			'depend'   => array( 'v20_upgrades_payment_metadata', 'v20_upgrades_form_metadata' ),
289
		)
290
	);
291
292
	// v2.0.0 Upgrades
293
	$give_updates->register(
294
		array(
295
			'id'       => 'v20_rename_donor_tables',
296
			'version'  => '2.0.0',
297
			'callback' => 'give_v20_rename_donor_tables_callback',
298
			'depend'   => array(
299
				'v20_move_metadata_into_new_table',
300
				'v20_logs_upgrades',
301
				'v20_upgrades_form_metadata',
302
				'v20_upgrades_payment_metadata',
303
				'v20_upgrades_user_address',
304
				'v20_upgrades_donor_name'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
305
			),
306
		)
307
	);
308
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
309
310
	// v2.0.1 Upgrades
311
	$give_updates->register(
312
		array(
313
			'id'       => 'v201_upgrades_payment_metadata',
314
			'version'  => '2.0.1',
315
			'callback' => 'give_v201_upgrades_payment_metadata_callback',
316
		)
317
	);
318
319
	// v2.0.1 Upgrades
320
	$give_updates->register(
321
		array(
322
			'id'       => 'v201_add_missing_donors',
323
			'version'  => '2.0.1',
324
			'callback' => 'give_v201_add_missing_donors_callback',
325
		)
326
	);
327
328
	// Run v2.0.0 Upgrades again in 2.0.1
329
	$give_updates->register(
330
		array(
331
			'id'       => 'v201_move_metadata_into_new_table',
332
			'version'  => '2.0.1',
333
			'callback' => 'give_v201_move_metadata_into_new_table_callback',
334
			'depend'   => array( 'v201_upgrades_payment_metadata', 'v201_add_missing_donors' ),
335
		)
336
	);
337
338
	// Run v2.0.0 Upgrades again in 2.0.1
339
	$give_updates->register(
340
		array(
341
			'id'       => 'v201_logs_upgrades',
342
			'version'  => '2.0.1',
343
			'callback' => 'give_v201_logs_upgrades_callback',
344
345
		)
346
	);
347
}
348
349
add_action( 'give_register_updates', 'give_show_upgrade_notices' );
350
351
/**
352
 * Triggers all upgrade functions
353
 *
354
 * This function is usually triggered via AJAX
355
 *
356
 * @since 1.0
357
 * @return void
358
 */
359
function give_trigger_upgrades() {
360
361 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...
362
		wp_die( esc_html__( 'You do not have permission to do Give upgrades.', 'give' ), esc_html__( 'Error', 'give' ), array(
363
			'response' => 403,
364
		) );
365
	}
366
367
	$give_version = get_option( 'give_version' );
368
369
	if ( ! $give_version ) {
370
		// 1.0 is the first version to use this option so we must add it.
371
		$give_version = '1.0';
372
		add_option( 'give_version', $give_version );
373
	}
374
375
	update_option( 'give_version', GIVE_VERSION );
376
	delete_option( 'give_doing_upgrade' );
377
378
	if ( DOING_AJAX ) {
379
		die( 'complete' );
380
	} // End if().
381
}
382
383
add_action( 'wp_ajax_give_trigger_upgrades', 'give_trigger_upgrades' );
384
385
386
/**
387
 * Upgrades the
388
 *
389
 * Standardizes the discrepancies between two metakeys `_give_payment_customer_id` and `_give_payment_donor_id`
390
 *
391
 * @since      1.3.2
392
 */
393
function give_v132_upgrade_give_payment_customer_id() {
394
	global $wpdb;
395
396
	/* @var Give_Updates $give_updates */
397
	$give_updates = Give_Updates::get_instance();
398
399
	// UPDATE DB METAKEYS.
400
	$sql   = "UPDATE $wpdb->postmeta SET meta_key = '_give_payment_customer_id' WHERE meta_key = '_give_payment_donor_id'";
401
	$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...
402
403
	$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...
404
	give_set_upgrade_complete( 'upgrade_give_payment_customer_id' );
405
}
406
407
408
/**
409
 * Upgrades the Offline Status
410
 *
411
 * Reverses the issue where offline donations in "pending" status where inappropriately marked as abandoned
412
 *
413
 * @since      1.3.4
414
 */
415
function give_v134_upgrade_give_offline_status() {
416
	global $wpdb;
417
418
	/* @var Give_Updates $give_updates */
419
	$give_updates = Give_Updates::get_instance();
420
421
	// Get abandoned offline payments.
422
	$select = "SELECT ID FROM $wpdb->posts p ";
423
	$join   = "LEFT JOIN $wpdb->postmeta m ON p.ID = m.post_id ";
424
	$where  = "WHERE p.post_type = 'give_payment' ";
425
	$where  .= "AND ( p.post_status = 'abandoned' )";
426
	$where  .= "AND ( m.meta_key = '_give_payment_gateway' AND m.meta_value = 'offline' )";
427
428
	$sql            = $select . $join . $where;
429
	$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...
430
431
	foreach ( $found_payments as $payment ) {
432
433
		// Only change ones marked abandoned since our release last week because the admin may have marked some abandoned themselves.
434
		$modified_time = get_post_modified_time( 'U', false, $payment );
435
436
		// 1450124863 =  12/10/2015 20:42:25.
437
		if ( $modified_time >= 1450124863 ) {
438
439
			give_update_payment_status( $payment, 'pending' );
440
441
		}
442
	}
443
444
	$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...
445
	give_set_upgrade_complete( 'upgrade_give_offline_status' );
446
}
447
448
449
/**
450
 * Cleanup User Roles
451
 *
452
 * This upgrade routine removes unused roles and roles with typos
453
 *
454
 * @since      1.5.2
455
 */
456
function give_v152_cleanup_users() {
457
458
	$give_version = get_option( 'give_version' );
459
460
	if ( ! $give_version ) {
461
		// 1.0 is the first version to use this option so we must add it.
462
		$give_version = '1.0';
463
	}
464
465
	$give_version = preg_replace( '/[^0-9.].*/', '', $give_version );
466
467
	// v1.5.2 Upgrades
468
	if ( version_compare( $give_version, '1.5.2', '<' ) || ! give_has_upgrade_completed( 'upgrade_give_user_caps_cleanup' ) ) {
469
470
		// Delete all caps with "ss".
471
		// Also delete all unused "campaign" roles.
472
		$delete_caps = array(
473
			'delete_give_formss',
474
			'delete_others_give_formss',
475
			'delete_private_give_formss',
476
			'delete_published_give_formss',
477
			'read_private_forms',
478
			'edit_give_formss',
479
			'edit_others_give_formss',
480
			'edit_private_give_formss',
481
			'edit_published_give_formss',
482
			'publish_give_formss',
483
			'read_private_give_formss',
484
			'assign_give_campaigns_terms',
485
			'delete_give_campaigns',
486
			'delete_give_campaigns_terms',
487
			'delete_give_campaignss',
488
			'delete_others_give_campaignss',
489
			'delete_private_give_campaignss',
490
			'delete_published_give_campaignss',
491
			'edit_give_campaigns',
492
			'edit_give_campaigns_terms',
493
			'edit_give_campaignss',
494
			'edit_others_give_campaignss',
495
			'edit_private_give_campaignss',
496
			'edit_published_give_campaignss',
497
			'manage_give_campaigns_terms',
498
			'publish_give_campaignss',
499
			'read_give_campaigns',
500
			'read_private_give_campaignss',
501
			'view_give_campaigns_stats',
502
			'delete_give_paymentss',
503
			'delete_others_give_paymentss',
504
			'delete_private_give_paymentss',
505
			'delete_published_give_paymentss',
506
			'edit_give_paymentss',
507
			'edit_others_give_paymentss',
508
			'edit_private_give_paymentss',
509
			'edit_published_give_paymentss',
510
			'publish_give_paymentss',
511
			'read_private_give_paymentss',
512
		);
513
514
		global $wp_roles;
515
		foreach ( $delete_caps as $cap ) {
516
			foreach ( array_keys( $wp_roles->roles ) as $role ) {
517
				$wp_roles->remove_cap( $role, $cap );
518
			}
519
		}
520
521
		// Create Give plugin roles.
522
		$roles = new Give_Roles();
523
		$roles->add_roles();
524
		$roles->add_caps();
525
526
		// The Update Ran.
527
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
528
		give_set_upgrade_complete( 'upgrade_give_user_caps_cleanup' );
529
		delete_option( 'give_doing_upgrade' );
530
531
	}// End if().
532
533
}
534
535
add_action( 'admin_init', 'give_v152_cleanup_users' );
536
537
/**
538
 * 1.6 Upgrade routine to create the customer meta table.
539
 *
540
 * @since  1.6
541
 * @return void
542
 */
543
function give_v16_upgrades() {
544
	// Create the donor databases.
545
	$donors_db = new Give_DB_Donors();
546
	$donors_db->create_table();
547
	$donor_meta = new Give_DB_Donor_Meta();
548
	$donor_meta->create_table();
549
}
550
551
/**
552
 * 1.7 Upgrades.
553
 *
554
 * a. Update license api data for plugin addons.
555
 * b. Cleanup user roles.
556
 *
557
 * @since  1.7
558
 * @return void
559
 */
560
function give_v17_upgrades() {
561
	// Upgrade license data.
562
	give_v17_upgrade_addon_license_data();
563
	give_v17_cleanup_roles();
564
}
565
566
/**
567
 * Upgrade license data
568
 *
569
 * @since 1.7
570
 */
571
function give_v17_upgrade_addon_license_data() {
572
	$give_options = give_get_settings();
573
574
	$api_url = 'https://givewp.com/give-sl-api/';
575
576
	// Get addons license key.
577
	$addons = array();
578
	foreach ( $give_options as $key => $value ) {
579
		if ( false !== strpos( $key, '_license_key' ) ) {
580
			$addons[ $key ] = $value;
581
		}
582
	}
583
584
	// Bailout: We do not have any addon license data to upgrade.
585
	if ( empty( $addons ) ) {
586
		return false;
587
	}
588
589
	foreach ( $addons as $key => $addon_license ) {
590
591
		// Get addon shortname.
592
		$shortname = str_replace( '_license_key', '', $key );
593
594
		// Addon license option name.
595
		$addon_license_option_name = $shortname . '_license_active';
596
597
		// bailout if license is empty.
598
		if ( empty( $addon_license ) ) {
599
			delete_option( $addon_license_option_name );
600
			continue;
601
		}
602
603
		// Get addon name.
604
		$addon_name       = array();
605
		$addon_name_parts = explode( '_', str_replace( 'give_', '', $shortname ) );
606
		foreach ( $addon_name_parts as $name_part ) {
607
608
			// Fix addon name
609
			switch ( $name_part ) {
610
				case 'authorizenet' :
611
					$name_part = 'authorize.net';
612
					break;
613
			}
614
615
			$addon_name[] = ucfirst( $name_part );
616
		}
617
618
		$addon_name = implode( ' ', $addon_name );
619
620
		// Data to send to the API.
621
		$api_params = array(
622
			'edd_action' => 'activate_license', // never change from "edd_" to "give_"!
623
			'license'    => $addon_license,
624
			'item_name'  => urlencode( $addon_name ),
625
			'url'        => home_url(),
626
		);
627
628
		// Call the API.
629
		$response = wp_remote_post(
630
			$api_url,
631
			array(
632
				'timeout'   => 15,
633
				'sslverify' => false,
634
				'body'      => $api_params,
635
			)
636
		);
637
638
		// Make sure there are no errors.
639
		if ( is_wp_error( $response ) ) {
640
			delete_option( $addon_license_option_name );
641
			continue;
642
		}
643
644
		// Tell WordPress to look for updates.
645
		set_site_transient( 'update_plugins', null );
646
647
		// Decode license data.
648
		$license_data = json_decode( wp_remote_retrieve_body( $response ) );
649
		update_option( $addon_license_option_name, $license_data );
650
	}// End foreach().
651
}
652
653
654
/**
655
 * Cleanup User Roles.
656
 *
657
 * This upgrade routine removes unused roles and roles with typos.
658
 *
659
 * @since      1.7
660
 */
661
function give_v17_cleanup_roles() {
662
663
	// Delete all caps with "_give_forms_" and "_give_payments_".
664
	// These roles have no usage; the proper is singular.
665
	$delete_caps = array(
666
		'view_give_forms_stats',
667
		'delete_give_forms_terms',
668
		'assign_give_forms_terms',
669
		'edit_give_forms_terms',
670
		'manage_give_forms_terms',
671
		'view_give_payments_stats',
672
		'manage_give_payments_terms',
673
		'edit_give_payments_terms',
674
		'assign_give_payments_terms',
675
		'delete_give_payments_terms',
676
	);
677
678
	global $wp_roles;
679
	foreach ( $delete_caps as $cap ) {
680
		foreach ( array_keys( $wp_roles->roles ) as $role ) {
681
			$wp_roles->remove_cap( $role, $cap );
682
		}
683
	}
684
685
	// Set roles again.
686
	$roles = new Give_Roles();
687
	$roles->add_roles();
688
	$roles->add_caps();
689
690
}
691
692
/**
693
 * 1.8 Upgrades.
694
 *
695
 * a. Upgrade checkbox settings to radio button settings.
696
 * a. Update form meta for new metabox settings.
697
 *
698
 * @since  1.8
699
 * @return void
700
 */
701
function give_v18_upgrades() {
702
	// Upgrade checkbox settings to radio button settings.
703
	give_v18_upgrades_core_setting();
704
}
705
706
/**
707
 * Upgrade core settings.
708
 *
709
 * @since  1.8
710
 * @return void
711
 */
712
function give_v18_upgrades_core_setting() {
713
	// Core settings which changes from checkbox to radio.
714
	$core_setting_names = array_merge(
715
		array_keys( give_v18_renamed_core_settings() ),
716
		array(
717
			'uninstall_on_delete',
718
			'scripts_footer',
719
			'test_mode',
720
			'email_access',
721
			'terms',
722
			'give_offline_donation_enable_billing_fields',
723
		)
724
	);
725
726
	// Bailout: If not any setting define.
727
	if ( $give_settings = get_option( 'give_settings' ) ) {
728
729
		$setting_changed = false;
730
731
		// Loop: check each setting field.
732
		foreach ( $core_setting_names as $setting_name ) {
733
			// New setting name.
734
			$new_setting_name = preg_replace( '/^(enable_|disable_)/', '', $setting_name );
735
736
			// Continue: If setting already set.
737
			if (
738
				array_key_exists( $new_setting_name, $give_settings )
739
				&& in_array( $give_settings[ $new_setting_name ], array( 'enabled', 'disabled' ) )
740
			) {
741
				continue;
742
			}
743
744
			// Set checkbox value to radio value.
745
			$give_settings[ $setting_name ] = ( ! empty( $give_settings[ $setting_name ] ) && 'on' === $give_settings[ $setting_name ] ? 'enabled' : 'disabled' );
746
747
			// @see https://github.com/WordImpress/Give/issues/1063.
748
			if ( false !== strpos( $setting_name, 'disable_' ) ) {
749
750
				$give_settings[ $new_setting_name ] = ( give_is_setting_enabled( $give_settings[ $setting_name ] ) ? 'disabled' : 'enabled' );
751
			} elseif ( false !== strpos( $setting_name, 'enable_' ) ) {
752
753
				$give_settings[ $new_setting_name ] = ( give_is_setting_enabled( $give_settings[ $setting_name ] ) ? 'enabled' : 'disabled' );
754
			}
755
756
			// Tell bot to update core setting to db.
757
			if ( ! $setting_changed ) {
758
				$setting_changed = true;
759
			}
760
		}
761
762
		// Update setting only if they changed.
763
		if ( $setting_changed ) {
764
			update_option( 'give_settings', $give_settings );
765
		}
766
	}// End if().
767
768
	give_set_upgrade_complete( 'v18_upgrades_core_setting' );
769
}
770
771
/**
772
 * Upgrade form metadata for new metabox settings.
773
 *
774
 * @since  1.8
775
 * @return void
776
 */
777
function give_v18_upgrades_form_metadata() {
778
	/* @var Give_Updates $give_updates */
779
	$give_updates = Give_Updates::get_instance();
780
781
	// form query
782
	$forms = new WP_Query( array(
783
			'paged'          => $give_updates->step,
784
			'status'         => 'any',
785
			'order'          => 'ASC',
786
			'post_type'      => 'give_forms',
787
			'posts_per_page' => 20,
788
		)
789
	);
790
791
	if ( $forms->have_posts() ) {
792
		$give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 20 ) );
793
794
		while ( $forms->have_posts() ) {
795
			$forms->the_post();
796
797
			// Form content.
798
			// Note in version 1.8 display content setting split into display content and content placement setting.
799
			// You can delete _give_content_option in future.
800
			$show_content = give_get_meta( get_the_ID(), '_give_content_option', true );
801
			if ( $show_content && ! give_get_meta( get_the_ID(), '_give_display_content', true ) ) {
802
				$field_value = ( 'none' !== $show_content ? 'enabled' : 'disabled' );
803
				give_update_meta( get_the_ID(), '_give_display_content', $field_value );
804
805
				$field_value = ( 'none' !== $show_content ? $show_content : 'give_pre_form' );
806
				give_update_meta( get_the_ID(), '_give_content_placement', $field_value );
807
			}
808
809
			// "Disable" Guest Donation. Checkbox.
810
			// See: https://github.com/WordImpress/Give/issues/1470.
811
			$guest_donation        = give_get_meta( get_the_ID(), '_give_logged_in_only', true );
812
			$guest_donation_newval = ( in_array( $guest_donation, array( 'yes', 'on' ) ) ? 'disabled' : 'enabled' );
813
			give_update_meta( get_the_ID(), '_give_logged_in_only', $guest_donation_newval );
814
815
			// Offline Donations.
816
			// See: https://github.com/WordImpress/Give/issues/1579.
817
			$offline_donation = give_get_meta( get_the_ID(), '_give_customize_offline_donations', true );
818
			if ( 'no' === $offline_donation ) {
819
				$offline_donation_newval = 'global';
820
			} elseif ( 'yes' === $offline_donation ) {
821
				$offline_donation_newval = 'enabled';
822
			} else {
823
				$offline_donation_newval = 'disabled';
824
			}
825
			give_update_meta( get_the_ID(), '_give_customize_offline_donations', $offline_donation_newval );
826
827
			// Convert yes/no setting field to enabled/disabled.
828
			$form_radio_settings = array(
829
				// Custom Amount.
830
				'_give_custom_amount',
831
832
				// Donation Gaol.
833
				'_give_goal_option',
834
835
				// Close Form.
836
				'_give_close_form_when_goal_achieved',
837
838
				// Term & conditions.
839
				'_give_terms_option',
840
841
				// Billing fields.
842
				'_give_offline_donation_enable_billing_fields_single',
843
			);
844
845
			foreach ( $form_radio_settings as $meta_key ) {
846
				// Get value.
847
				$field_value = give_get_meta( get_the_ID(), $meta_key, true );
848
849
				// Convert meta value only if it is in yes/no/none.
850
				if ( in_array( $field_value, array( 'yes', 'on', 'no', 'none' ) ) ) {
851
852
					$field_value = ( in_array( $field_value, array( 'yes', 'on' ) ) ? 'enabled' : 'disabled' );
853
					give_update_meta( get_the_ID(), $meta_key, $field_value );
854
				}
855
			}
856
		}// End while().
857
858
		wp_reset_postdata();
859
860
	} else {
861
		// No more forms found, finish up.
862
		give_set_upgrade_complete( 'v18_upgrades_form_metadata' );
863
	}
864
}
865
866
867
/**
868
 * Get list of core setting renamed in version 1.8.
869
 *
870
 * @since  1.8
871
 * @return array
872
 */
873
function give_v18_renamed_core_settings() {
874
	return array(
875
		'disable_paypal_verification' => 'paypal_verification',
876
		'disable_css'                 => 'css',
877
		'disable_welcome'             => 'welcome',
878
		'disable_forms_singular'      => 'forms_singular',
879
		'disable_forms_archives'      => 'forms_archives',
880
		'disable_forms_excerpt'       => 'forms_excerpt',
881
		'disable_form_featured_img'   => 'form_featured_img',
882
		'disable_form_sidebar'        => 'form_sidebar',
883
		'disable_admin_notices'       => 'admin_notices',
884
		'disable_the_content_filter'  => 'the_content_filter',
885
		'enable_floatlabels'          => 'floatlabels',
886
		'enable_categories'           => 'categories',
887
		'enable_tags'                 => 'tags',
888
	);
889
}
890
891
892
/**
893
 * Upgrade core settings.
894
 *
895
 * @since  1.8.7
896
 * @return void
897
 */
898
function give_v187_upgrades() {
899
	global $wpdb;
900
901
	/**
902
	 * Upgrade 1: Remove stat and cache transients.
903
	 */
904
	$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...
905
		$wpdb->prepare(
906
			"
907
					SELECT *
908
					FROM {$wpdb->options}
909
					WHERE (
910
					option_name LIKE %s
911
					OR option_name LIKE %s
912
					OR option_name LIKE %s
913
					OR option_name LIKE %s
914
					OR option_name LIKE %s
915
					OR option_name LIKE %s
916
					OR option_name LIKE %s
917
					OR option_name LIKE %s
918
					OR option_name LIKE %s
919
					OR option_name LIKE %s
920
					OR option_name LIKE %s
921
					OR option_name LIKE %s
922
					OR option_name LIKE %s
923
					)
924
					",
925
			array(
926
				'%_transient_give_stats_%',
927
				'give_cache%',
928
				'%_transient_give_add_ons_feed%',
929
				'%_transient__give_ajax_works' .
930
				'%_transient_give_total_api_keys%',
931
				'%_transient_give_i18n_give_promo_hide%',
932
				'%_transient_give_contributors%',
933
				'%_transient_give_estimated_monthly_stats%',
934
				'%_transient_give_earnings_total%',
935
				'%_transient_give_i18n_give_%',
936
				'%_transient__give_installed%',
937
				'%_transient__give_activation_redirect%',
938
				'%_transient__give_hide_license_notices_shortly_%',
939
				'%give_income_total%',
940
			)
941
		),
942
		1
943
	);
944
945
	// User related transients.
946
	$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...
947
		$wpdb->prepare(
948
			"SELECT user_id, meta_key
949
			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...
950
			WHERE meta_value=%s",
951
			'give_user_public_key'
952
		),
953
		ARRAY_A
954
	);
955
956
	if ( ! empty( $user_apikey_options ) ) {
957
		foreach ( $user_apikey_options as $user ) {
958
			$cached_options[] = '_transient_' . md5( 'give_api_user_' . $user['meta_key'] );
959
			$cached_options[] = '_transient_' . md5( 'give_api_user_public_key' . $user['user_id'] );
960
			$cached_options[] = '_transient_' . md5( 'give_api_user_secret_key' . $user['user_id'] );
961
		}
962
	}
963
964
	if ( ! empty( $cached_options ) ) {
965
		foreach ( $cached_options as $option ) {
966 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...
967
				case ( false !== strpos( $option, 'transient' ) ):
968
					$option = str_replace( '_transient_', '', $option );
969
					delete_transient( $option );
970
					break;
971
972
				default:
973
					delete_option( $option );
974
			}
975
		}
976
	}
977
}
978
979
/**
980
 * Update Capabilities for Give_Worker User Role.
981
 *
982
 * This upgrade routine will update access rights for Give_Worker User Role.
983
 *
984
 * @since      1.8.8
985
 */
986
function give_v188_upgrades() {
987
988
	global $wp_roles;
989
990
	// Get the role object.
991
	$give_worker = get_role( 'give_worker' );
992
993
	// A list of capabilities to add for give workers.
994
	$caps_to_add = array(
995
		'edit_posts',
996
		'edit_pages',
997
	);
998
999
	foreach ( $caps_to_add as $cap ) {
1000
		// Add the capability.
1001
		$give_worker->add_cap( $cap );
1002
	}
1003
1004
}
1005
1006
/**
1007
 * Update Post meta for minimum and maximum amount for multi level donation forms
1008
 *
1009
 * This upgrade routine adds post meta for give_forms CPT for multi level donation form.
1010
 *
1011
 * @since      1.8.9
1012
 */
1013
function give_v189_upgrades_levels_post_meta_callback() {
1014
	/* @var Give_Updates $give_updates */
1015
	$give_updates = Give_Updates::get_instance();
1016
1017
	// form query.
1018
	$donation_forms = new WP_Query( array(
1019
			'paged'          => $give_updates->step,
1020
			'status'         => 'any',
1021
			'order'          => 'ASC',
1022
			'post_type'      => 'give_forms',
1023
			'posts_per_page' => 20,
1024
		)
1025
	);
1026
1027
	if ( $donation_forms->have_posts() ) {
1028
		$give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
1029
1030
		while ( $donation_forms->have_posts() ) {
1031
			$donation_forms->the_post();
1032
			$form_id = get_the_ID();
1033
1034
			// Remove formatting from _give_set_price.
1035
			update_post_meta(
1036
				$form_id,
1037
				'_give_set_price',
1038
				give_sanitize_amount( get_post_meta( $form_id, '_give_set_price', true ) )
1039
			);
1040
1041
			// Remove formatting from _give_custom_amount_minimum.
1042
			update_post_meta(
1043
				$form_id,
1044
				'_give_custom_amount_minimum',
1045
				give_sanitize_amount( get_post_meta( $form_id, '_give_custom_amount_minimum', true ) )
1046
			);
1047
1048
			// Bailout.
1049
			if ( 'set' === get_post_meta( $form_id, '_give_price_option', true ) ) {
1050
				continue;
1051
			}
1052
1053
			$donation_levels = get_post_meta( $form_id, '_give_donation_levels', true );
1054
1055
			if ( ! empty( $donation_levels ) ) {
1056
1057
				foreach ( $donation_levels as $index => $donation_level ) {
1058
					if ( isset( $donation_level['_give_amount'] ) ) {
1059
						$donation_levels[ $index ]['_give_amount'] = give_sanitize_amount( $donation_level['_give_amount'] );
1060
					}
1061
				}
1062
1063
				update_post_meta( $form_id, '_give_donation_levels', $donation_levels );
1064
1065
				$donation_levels_amounts = wp_list_pluck( $donation_levels, '_give_amount' );
1066
1067
				$min_amount = min( $donation_levels_amounts );
1068
				$max_amount = max( $donation_levels_amounts );
1069
1070
				// Set Minimum and Maximum amount for Multi Level Donation Forms
1071
				give_update_meta( $form_id, '_give_levels_minimum_amount', $min_amount ? give_sanitize_amount( $min_amount ) : 0 );
1072
				give_update_meta( $form_id, '_give_levels_maximum_amount', $max_amount ? give_sanitize_amount( $max_amount ) : 0 );
1073
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1074
1075
		}
1076
1077
		/* Restore original Post Data */
1078
		wp_reset_postdata();
1079
	} else {
1080
		// The Update Ran.
1081
		give_set_upgrade_complete( 'v189_upgrades_levels_post_meta' );
1082
	}
1083
1084
}
1085
1086
1087
/**
1088
 * Give version 1.8.9 upgrades
1089
 *
1090
 * @since      1.8.9
1091
 */
1092
function give_v189_upgrades() {
1093
	/**
1094
	 * 1. Remove user license related notice show blocked ( Give_Notice will handle )
1095
	 */
1096
	global $wpdb;
1097
1098
	// Delete permanent notice blocker.
1099
	$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...
1100
		$wpdb->prepare(
1101
			"
1102
					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...
1103
					WHERE meta_key
1104
					LIKE '%%%s%%'
1105
					",
1106
			'_give_hide_license_notices_permanently'
1107
		)
1108
	);
1109
1110
	// Delete short notice blocker.
1111
	$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...
1112
		$wpdb->prepare(
1113
			"
1114
					DELETE FROM $wpdb->options
1115
					WHERE option_name
1116
					LIKE '%%%s%%'
1117
					",
1118
			'__give_hide_license_notices_shortly_'
1119
		)
1120
	);
1121
}
1122
1123
/**
1124
 * 2.0 Upgrades.
1125
 *
1126
 * @since  2.0
1127
 * @return void
1128
 */
1129
function give_v20_upgrades() {
1130
	// Update cache setting.
1131
	give_update_option( 'cache', 'enabled' );
1132
1133
	// Upgrade email settings.
1134
	give_v20_upgrades_email_setting();
1135
}
1136
1137
/**
1138
 * Move old email api settings to new email setting api for following emails:
1139
 *    1. new offline donation         [This was hard coded]
1140
 *    2. offline donation instruction
1141
 *    3. new donation
1142
 *    4. donation receipt
1143
 *
1144
 * @since 2.0
1145
 */
1146
function give_v20_upgrades_email_setting() {
1147
	$all_setting = give_get_settings();
1148
1149
	// Bailout on fresh install.
1150
	if ( empty( $all_setting ) ) {
1151
		return;
1152
	}
1153
1154
	$settings = array(
1155
		'offline_donation_subject'      => 'offline-donation-instruction_email_subject',
1156
		'global_offline_donation_email' => 'offline-donation-instruction_email_message',
1157
		'donation_subject'              => 'donation-receipt_email_subject',
1158
		'donation_receipt'              => 'donation-receipt_email_message',
1159
		'donation_notification_subject' => 'new-donation_email_subject',
1160
		'donation_notification'         => 'new-donation_email_message',
1161
		'admin_notice_emails'           => array(
1162
			'new-donation_recipient',
1163
			'new-offline-donation_recipient',
1164
			'new-donor-register_recipient',
1165
		),
1166
		'admin_notices'                 => 'new-donation_notification',
1167
	);
1168
1169
	foreach ( $settings as $old_setting => $new_setting ) {
1170
		// Do not update already modified
1171
		if ( ! is_array( $new_setting ) ) {
1172
			if ( array_key_exists( $new_setting, $all_setting ) || ! array_key_exists( $old_setting, $all_setting ) ) {
1173
				continue;
1174
			}
1175
		}
1176
1177
		switch ( $old_setting ) {
1178
			case 'admin_notices':
1179
				$notification_status = give_get_option( $old_setting, 'enabled' );
1180
1181
				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 1169 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...
1182
1183
				// @todo: Delete this option later ( version > 2.0 ), We need this for per form email addon.
1184
				// give_delete_option( $old_setting );
1185
1186
				break;
1187
1188
			// @todo: Delete this option later ( version > 2.0 ) because we need this for backward compatibility give_get_admin_notice_emails.
1189
			case 'admin_notice_emails':
1190
				$recipients = give_get_admin_notice_emails();
1191
1192
				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...
1193
					// bailout if setting already exist.
1194
					if ( array_key_exists( $setting, $all_setting ) ) {
1195
						continue;
1196
					}
1197
1198
					give_update_option( $setting, $recipients );
1199
				}
1200
				break;
1201
1202
			default:
1203
				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 1169 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...
1204
				give_delete_option( $old_setting );
1205
		}
1206
	}
1207
}
1208
1209
/**
1210
 * Give version 1.8.9 upgrades
1211
 *
1212
 * @since 1.8.9
1213
 */
1214
function give_v1812_upgrades() {
1215
	/**
1216
	 * Validate number format settings.
1217
	 */
1218
	$give_settings        = give_get_settings();
1219
	$give_setting_updated = false;
1220
1221
	if ( $give_settings['thousands_separator'] === $give_settings['decimal_separator'] ) {
1222
		$give_settings['number_decimals']   = 0;
1223
		$give_settings['decimal_separator'] = '';
1224
		$give_setting_updated               = true;
1225
1226
	} elseif ( empty( $give_settings['decimal_separator'] ) ) {
1227
		$give_settings['number_decimals'] = 0;
1228
		$give_setting_updated             = true;
1229
1230
	} elseif ( 6 < absint( $give_settings['number_decimals'] ) ) {
1231
		$give_settings['number_decimals'] = 5;
1232
		$give_setting_updated             = true;
1233
	}
1234
1235
	if ( $give_setting_updated ) {
1236
		update_option( 'give_settings', $give_settings );
1237
	}
1238
}
1239
1240
1241
/**
1242
 * Give version 1.8.12 update
1243
 *
1244
 * Standardized amount values to six decimal
1245
 *
1246
 * @see        https://github.com/WordImpress/Give/issues/1849#issuecomment-315128602
1247
 *
1248
 * @since      1.8.12
1249
 */
1250
function give_v1812_update_amount_values_callback() {
1251
	/* @var Give_Updates $give_updates */
1252
	$give_updates = Give_Updates::get_instance();
1253
1254
	// form query.
1255
	$donation_forms = new WP_Query( array(
1256
			'paged'          => $give_updates->step,
1257
			'status'         => 'any',
1258
			'order'          => 'ASC',
1259
			'post_type'      => array( 'give_forms', 'give_payment' ),
1260
			'posts_per_page' => 20,
1261
		)
1262
	);
1263
	if ( $donation_forms->have_posts() ) {
1264
		$give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
1265
1266
		while ( $donation_forms->have_posts() ) {
1267
			$donation_forms->the_post();
1268
			global $post;
1269
1270
			$meta = get_post_meta( $post->ID );
1271
1272
			switch ( $post->post_type ) {
1273
				case 'give_forms':
1274
					// _give_set_price.
1275
					if ( ! empty( $meta['_give_set_price'][0] ) ) {
1276
						update_post_meta( $post->ID, '_give_set_price', give_sanitize_amount_for_db( $meta['_give_set_price'][0] ) );
1277
					}
1278
1279
					// _give_custom_amount_minimum.
1280
					if ( ! empty( $meta['_give_custom_amount_minimum'][0] ) ) {
1281
						update_post_meta( $post->ID, '_give_custom_amount_minimum', give_sanitize_amount_for_db( $meta['_give_custom_amount_minimum'][0] ) );
1282
					}
1283
1284
					// _give_levels_minimum_amount.
1285
					if ( ! empty( $meta['_give_levels_minimum_amount'][0] ) ) {
1286
						update_post_meta( $post->ID, '_give_levels_minimum_amount', give_sanitize_amount_for_db( $meta['_give_levels_minimum_amount'][0] ) );
1287
					}
1288
1289
					// _give_levels_maximum_amount.
1290
					if ( ! empty( $meta['_give_levels_maximum_amount'][0] ) ) {
1291
						update_post_meta( $post->ID, '_give_levels_maximum_amount', give_sanitize_amount_for_db( $meta['_give_levels_maximum_amount'][0] ) );
1292
					}
1293
1294
					// _give_set_goal.
1295
					if ( ! empty( $meta['_give_set_goal'][0] ) ) {
1296
						update_post_meta( $post->ID, '_give_set_goal', give_sanitize_amount_for_db( $meta['_give_set_goal'][0] ) );
1297
					}
1298
1299
					// _give_form_earnings.
1300
					if ( ! empty( $meta['_give_form_earnings'][0] ) ) {
1301
						update_post_meta( $post->ID, '_give_form_earnings', give_sanitize_amount_for_db( $meta['_give_form_earnings'][0] ) );
1302
					}
1303
1304
					// _give_custom_amount_minimum.
1305
					if ( ! empty( $meta['_give_donation_levels'][0] ) ) {
1306
						$donation_levels = unserialize( $meta['_give_donation_levels'][0] );
1307
1308
						foreach ( $donation_levels as $index => $level ) {
1309
							if ( empty( $level['_give_amount'] ) ) {
1310
								continue;
1311
							}
1312
1313
							$donation_levels[ $index ]['_give_amount'] = give_sanitize_amount_for_db( $level['_give_amount'] );
1314
						}
1315
1316
						$meta['_give_donation_levels'] = $donation_levels;
1317
						update_post_meta( $post->ID, '_give_donation_levels', $meta['_give_donation_levels'] );
1318
					}
1319
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
1320
1321
					break;
1322
1323
				case 'give_payment':
1324
					// _give_payment_total.
1325
					if ( ! empty( $meta['_give_payment_total'][0] ) ) {
1326
						update_post_meta( $post->ID, '_give_payment_total', give_sanitize_amount_for_db( $meta['_give_payment_total'][0] ) );
1327
					}
1328
1329
					break;
1330
			}
1331
		}
1332
1333
		/* Restore original Post Data */
1334
		wp_reset_postdata();
1335
	} else {
1336
		// The Update Ran.
1337
		give_set_upgrade_complete( 'v1812_update_amount_values' );
1338
	}
1339
}
1340
1341
1342
/**
1343
 * Give version 1.8.12 update
1344
 *
1345
 * Standardized amount values to six decimal for donor
1346
 *
1347
 * @see        https://github.com/WordImpress/Give/issues/1849#issuecomment-315128602
1348
 *
1349
 * @since      1.8.12
1350
 */
1351
function give_v1812_update_donor_purchase_value_callback() {
1352
	/* @var Give_Updates $give_updates */
1353
	$give_updates = Give_Updates::get_instance();
1354
	$offset       = 1 === $give_updates->step ? 0 : $give_updates->step * 20;
1355
1356
	// form query.
1357
	$donors = Give()->donors->get_donors( array(
1358
			'number' => 20,
1359
			'offset' => $offset,
1360
		)
1361
	);
1362
1363
	if ( ! empty( $donors ) ) {
1364
		$give_updates->set_percentage( Give()->donors->count(), $offset );
1365
1366
		/* @var Object $donor */
1367
		foreach ( $donors as $donor ) {
1368
			Give()->donors->update( $donor->id, array( 'purchase_value' => give_sanitize_amount_for_db( $donor->purchase_value ) ) );
1369
		}
1370
	} else {
1371
		// The Update Ran.
1372
		give_set_upgrade_complete( 'v1812_update_donor_purchase_values' );
1373
	}
1374
}
1375
1376
/**
1377
 * Upgrade routine for updating user roles for existing donors.
1378
 *
1379
 * @since 1.8.13
1380
 */
1381
function give_v1813_update_donor_user_roles_callback() {
1382
	/* @var Give_Updates $give_updates */
1383
	$give_updates = Give_Updates::get_instance();
1384
	$offset       = 1 === $give_updates->step ? 0 : $give_updates->step * 20;
1385
1386
	// Fetch all the existing donors.
1387
	$donors = Give()->donors->get_donors( array(
1388
			'number' => 20,
1389
			'offset' => $offset,
1390
		)
1391
	);
1392
1393
	if ( ! empty( $donors ) ) {
1394
		$give_updates->set_percentage( Give()->donors->count(), ( $give_updates->step * 20 ) );
1395
1396
		/* @var Object $donor */
1397
		foreach ( $donors as $donor ) {
1398
			$user_id = $donor->user_id;
1399
1400
			// Proceed, if donor is attached with user.
1401
			if ( $user_id ) {
1402
				$user = get_userdata( $user_id );
1403
1404
				// Update user role, if user has subscriber role.
1405
				if ( is_array( $user->roles ) && in_array( 'subscriber', $user->roles ) ) {
1406
					wp_update_user(
1407
						array(
1408
							'ID'   => $user_id,
1409
							'role' => 'give_donor',
1410
						)
1411
					);
1412
				}
1413
			}
1414
		}
1415
	} else {
1416
		// The Update Ran.
1417
		give_set_upgrade_complete( 'v1813_update_donor_user_roles' );
1418
	}
1419
}
1420
1421
1422
/**
1423
 * Version 1.8.13 automatic updates
1424
 *
1425
 * @since 1.8.13
1426
 */
1427
function give_v1813_upgrades() {
1428
	// Update admin setting.
1429
	give_update_option( 'donor_default_user_role', 'give_donor' );
1430
1431
	// Update Give roles.
1432
	$roles = new Give_Roles();
1433
	$roles->add_roles();
1434
	$roles->add_caps();
1435
}
1436
1437
/**
1438
 * Correct currency code for "Iranian Currency" for all of the payments.
1439
 *
1440
 * @since 1.8.17
1441
 */
1442
function give_v1817_update_donation_iranian_currency_code() {
1443
	/* @var Give_Updates $give_updates */
1444
	$give_updates = Give_Updates::get_instance();
1445
1446
	// form query.
1447
	$payments = new WP_Query( array(
1448
			'paged'          => $give_updates->step,
1449
			'status'         => 'any',
1450
			'order'          => 'ASC',
1451
			'post_type'      => array( 'give_payment' ),
1452
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1453
		)
1454
	);
1455
1456
	if ( $payments->have_posts() ) {
1457
		$give_updates->set_percentage( $payments->found_posts, ( $give_updates->step * 100 ) );
1458
1459
		while ( $payments->have_posts() ) {
1460
			$payments->the_post();
1461
1462
			$payment_meta = give_get_payment_meta( get_the_ID() );
1463
1464
			if ( 'RIAL' === $payment_meta['currency'] ) {
1465
				$payment_meta['currency'] = 'IRR';
1466
				give_update_meta( get_the_ID(), '_give_payment_meta', $payment_meta );
1467
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1468
1469
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1470
1471
	} else {
1472
		// The Update Ran.
1473
		give_set_upgrade_complete( 'v1817_update_donation_iranian_currency_code' );
1474
	}
1475
}
1476
1477
/**
1478
 * Correct currency code for "Iranian Currency" in Give setting.
1479
 * Version 1.8.17 automatic updates
1480
 *
1481
 * @since 1.8.17
1482
 */
1483
function give_v1817_upgrades() {
1484
	$give_settings = give_get_settings();
1485
1486
	if ( 'RIAL' === $give_settings['currency'] ) {
1487
		$give_settings['currency'] = 'IRR';
1488
		update_option( 'give_settings', $give_settings );
1489
	}
1490
}
1491
1492
/**
1493
 * Process Clean up of User Roles for more flexibility.
1494
 *
1495
 * @since 1.8.17
1496
 */
1497
function give_v1817_process_cleanup_user_roles() {
1498
1499
	global $wp_roles;
1500
1501
	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...
1502
		return;
1503
	}
1504
1505
	// Add Capabilities to user roles as required.
1506
	$add_caps = array(
1507
		'administrator' => array(
1508
			'view_give_payments',
1509
		),
1510
	);
1511
1512
	// Remove Capabilities to user roles as required.
1513
	$remove_caps = array(
1514
		'give_manager' => array(
1515
			'edit_others_pages',
1516
			'edit_others_posts',
1517
			'delete_others_pages',
1518
			'delete_others_posts',
1519
			'manage_categories',
1520
			'import',
1521
			'export',
1522
		),
1523
	);
1524
1525
	foreach ( $add_caps as $role => $caps ) {
1526
		foreach ( $caps as $cap ) {
1527
			$wp_roles->add_cap( $role, $cap );
1528
		}
1529
	}
1530
1531
	foreach ( $remove_caps as $role => $caps ) {
1532
		foreach ( $caps as $cap ) {
1533
			$wp_roles->remove_cap( $role, $cap );
1534
		}
1535
	}
1536
1537
}
1538
1539
/**
1540
 * Upgrade Routine - Clean up of User Roles for more flexibility.
1541
 *
1542
 * @since 1.8.17
1543
 */
1544
function give_v1817_cleanup_user_roles() {
1545
	/* @var Give_Updates $give_updates */
1546
	$give_updates = Give_Updates::get_instance();
1547
1548
	give_v1817_process_cleanup_user_roles();
1549
1550
	$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...
1551
1552
	// Create Give plugin roles.
1553
	$roles = new Give_Roles();
1554
	$roles->add_roles();
1555
	$roles->add_caps();
1556
1557
	give_set_upgrade_complete( 'v1817_cleanup_user_roles' );
1558
}
1559
1560
/**
1561
 * Automatic Upgrade for release 1.8.18.
1562
 *
1563
 * @since 1.8.18
1564
 */
1565
function give_v1818_upgrades() {
1566
1567
	// Remove email_access_installed from give_settings.
1568
	give_delete_option( 'email_access_installed' );
1569
}
1570
1571
/**
1572
 * Upgrade Routine - Assigns Custom Amount to existing donation of type set donation.
1573
 *
1574
 * @since 1.8.18
1575
 */
1576
function give_v1818_assign_custom_amount_set_donation() {
1577
1578
	/* @var Give_Updates $give_updates */
1579
	$give_updates   = Give_Updates::get_instance();
1580
1581
	$donations = new WP_Query( array(
1582
			'paged'          => $give_updates->step,
1583
			'status'         => 'any',
1584
			'order'          => 'ASC',
1585
			'post_type'      => array( 'give_payment' ),
1586
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1587
		)
1588
	);
1589
1590
	if ( $donations->have_posts() ) {
1591
		$give_updates->set_percentage( $donations->found_posts, $give_updates->step * 100 );
1592
1593
		while ( $donations->have_posts() ) {
1594
			$donations->the_post();
1595
1596
			$form          = new Give_Donate_Form( give_get_meta( get_the_ID(), '_give_payment_form_id', true ) );
1597
			$donation_meta = give_get_payment_meta( get_the_ID() );
1598
1599
			// Update Donation meta with price_id set as custom, only if it is:
1600
			// 1. Donation Type = Set Donation.
1601
			// 2. Donation Price Id is not set to custom.
1602
			// 3. Form has not enabled custom price and donation amount assures that it is custom amount.
1603
			if (
1604
				$form->ID &&
1605
				$form->is_set_type_donation_form() &&
1606
				( 'custom' !== $donation_meta['price_id'] ) &&
1607
				$form->is_custom_price( give_get_meta( get_the_ID(), '_give_payment_total', true ) )
1608
			) {
1609
				$donation_meta['price_id'] = 'custom';
1610
				give_update_meta( get_the_ID(), '_give_payment_meta', $donation_meta );
1611
				give_update_meta( get_the_ID(), '_give_payment_price_id', 'custom' );
1612
			}
1613
		}
1614
1615
		wp_reset_postdata();
1616
	} else {
1617
		// Update Ran Successfully.
1618
		give_set_upgrade_complete( 'v1818_assign_custom_amount_set_donation' );
1619
	}
1620
}
1621
1622
/**
1623
 * Upgrade Routine - Removed Give Worker caps.
1624
 *
1625
 * See: https://github.com/WordImpress/Give/issues/2476
1626
 *
1627
 * @since 1.8.18
1628
 */
1629
function give_v1818_give_worker_role_cleanup(){
1630
1631
	/* @var Give_Updates $give_updates */
1632
	$give_updates = Give_Updates::get_instance();
1633
1634
	global $wp_roles;
1635
1636
	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...
1637
		return;
1638
	}
1639
1640
	// Remove Capabilities to user roles as required.
1641
	$remove_caps = array(
1642
		'give_worker' => array(
1643
			'delete_give_payments',
1644
			'delete_others_give_payments',
1645
			'delete_private_give_payments',
1646
			'delete_published_give_payments',
1647
			'edit_others_give_payments',
1648
			'edit_private_give_payments',
1649
			'edit_published_give_payments',
1650
			'read_private_give_payments',
1651
		),
1652
	);
1653
1654
	foreach ( $remove_caps as $role => $caps ) {
1655
		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...
1656
			$wp_roles->remove_cap( $role, $cap );
1657
		}
1658
	}
1659
1660
	$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...
1661
1662
	// Create Give plugin roles.
1663
	$roles = new Give_Roles();
1664
	$roles->add_roles();
1665
	$roles->add_caps();
1666
1667
	give_set_upgrade_complete( 'v1818_give_worker_role_cleanup' );
1668
}
1669
1670
/**
1671
 *
1672
 * Upgrade form metadata for new metabox settings.
1673
 *
1674
 * @since  2.0
1675
 * @return void
1676
 */
1677
function give_v20_upgrades_form_metadata_callback() {
1678
	$give_updates = Give_Updates::get_instance();
1679
1680
	// form query
1681
	$forms = new WP_Query( array(
1682
			'paged'          => $give_updates->step,
1683
			'status'         => 'any',
1684
			'order'          => 'ASC',
1685
			'post_type'      => 'give_forms',
1686
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1687
		)
1688
	);
1689
1690
	if ( $forms->have_posts() ) {
1691
		$give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 100 ) );
1692
1693
		while ( $forms->have_posts() ) {
1694
			$forms->the_post();
1695
			global $post;
1696
1697
			// Update offline instruction email notification status.
1698
			$offline_instruction_notification_status = get_post_meta( get_the_ID(), '_give_customize_offline_donations', true );
1699
			$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...
1700
				'enabled',
1701
				'global',
1702
			) )
1703
				? $offline_instruction_notification_status
1704
				: 'global';
1705
			update_post_meta( get_the_ID(), '_give_offline-donation-instruction_notification', $offline_instruction_notification_status );
1706
1707
			// Update offline instruction email message.
1708
			update_post_meta(
1709
				get_the_ID(),
1710
				'_give_offline-donation-instruction_email_message',
1711
				get_post_meta(
1712
					get_the_ID(),
1713
					// @todo: Delete this option later ( version > 2.0 ).
1714
					'_give_offline_donation_email',
1715
					true
1716
				)
1717
			);
1718
1719
			// Update offline instruction email subject.
1720
			update_post_meta(
1721
				get_the_ID(),
1722
				'_give_offline-donation-instruction_email_subject',
1723
				get_post_meta(
1724
					get_the_ID(),
1725
					// @todo: Delete this option later ( version > 2.0 ).
1726
					'_give_offline_donation_subject',
1727
					true
1728
				)
1729
			);
1730
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
1731
1732
		}// End while().
1733
1734
		wp_reset_postdata();
1735
	} else {
1736
		// No more forms found, finish up.
1737
		give_set_upgrade_complete( 'v20_upgrades_form_metadata' );
1738
	}
1739
}
1740
1741
1742
/**
1743
 * Upgrade payment metadata for new metabox settings.
1744
 *
1745
 * @since  2.0
1746
 * @global wpdb $wpdb
1747
 * @return void
1748
 */
1749
function give_v20_upgrades_payment_metadata_callback() {
1750
	global $wpdb;
1751
	$give_updates = Give_Updates::get_instance();
1752
1753
	// form query
1754
	$forms = new WP_Query( array(
1755
			'paged'          => $give_updates->step,
1756
			'status'         => 'any',
1757
			'order'          => 'ASC',
1758
			'post_type'      => 'give_payment',
1759
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1760
		)
1761
	);
1762
1763
	if ( $forms->have_posts() ) {
1764
		$give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 100 ) );
1765
1766
		while ( $forms->have_posts() ) {
1767
			$forms->the_post();
1768
			global $post;
1769
1770
			// Split _give_payment_meta meta.
1771
			// @todo Remove _give_payment_meta after releases 2.0
1772
			$payment_meta = give_get_meta( $post->ID, '_give_payment_meta', true );
1773
1774
			if ( ! empty( $payment_meta ) ) {
1775
				_give_20_bc_split_and_save_give_payment_meta( $post->ID, $payment_meta );
1776
			}
1777
1778
			$deprecated_meta_keys = array(
1779
				'_give_payment_customer_id' => '_give_payment_donor_id',
1780
				'_give_payment_user_email'  => '_give_payment_donor_email',
1781
				'_give_payment_user_ip'     => '_give_payment_donor_ip',
1782
			);
1783
1784 View Code Duplication
			foreach ( $deprecated_meta_keys as $old_meta_key => $new_meta_key ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
1785
				// Do not add new meta key if already exist.
1786
				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...
1787
					continue;
1788
				}
1789
1790
				$wpdb->insert(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
1791
					$wpdb->postmeta,
1792
					array(
1793
						'post_id' => $post->ID,
1794
						'meta_key' => $new_meta_key,
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
1795
						'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...
1796
					)
1797
				);
1798
			}
1799
1800
			// Bailout
1801 View Code Duplication
			if ( $donor_id = give_get_meta( $post->ID, '_give_payment_donor_id', true ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
1802
				/* @var Give_Donor $donor */
1803
				$donor = new Give_Donor( $donor_id );
1804
1805
				$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...
1806
				$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...
1807
				$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...
1808
				$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...
1809
				$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...
1810
				$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...
1811
1812
				// Save address.
1813
				$donor->add_address( 'billing[]', $address );
1814
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1815
1816
		}// End while().
1817
1818
		wp_reset_postdata();
1819
	} else {
1820
		// @todo Delete user id meta after releases 2.0
1821
		// $wpdb->get_var( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key=%s", '_give_payment_user_id' ) );
1822
1823
		// No more forms found, finish up.
1824
		give_set_upgrade_complete( 'v20_upgrades_payment_metadata' );
1825
	}
1826
}
1827
1828
1829
/**
1830
 * Upgrade logs data.
1831
 *
1832
 * @since  2.0
1833
 * @return void
1834
 */
1835
function give_v20_logs_upgrades_callback() {
1836
	global $wpdb;
1837
	$give_updates = Give_Updates::get_instance();
1838
1839
	// form query
1840
	$forms = new WP_Query( array(
1841
			'paged'          => $give_updates->step,
1842
			'order'          => 'DESC',
1843
			'post_type'      => 'give_log',
1844
			'post_status'    => 'any',
1845
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1846
		)
1847
	);
1848
1849
	if ( $forms->have_posts() ) {
1850
		$give_updates->set_percentage( $forms->found_posts, $give_updates->step * 100 );
1851
1852
		while ( $forms->have_posts() ) {
1853
			$forms->the_post();
1854
			global $post;
1855
1856
			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...
1857
				continue;
1858
			}
1859
1860
			$term      = get_the_terms( $post->ID, 'give_log_type' );
1861
			$term      = ! is_wp_error( $term ) && ! empty( $term ) ? $term[0] : array();
1862
			$term_name = ! empty( $term ) ? $term->slug : '';
1863
1864
			$log_data = array(
1865
				'ID'           => $post->ID,
1866
				'log_title'    => $post->post_title,
1867
				'log_content'  => $post->post_content,
1868
				'log_parent'   => 0,
1869
				'log_type'     => $term_name,
1870
				'log_date'     => $post->post_date,
1871
				'log_date_gmt' => $post->post_date_gmt,
1872
			);
1873
			$log_meta = array();
1874
1875
			if ( $old_log_meta = get_post_meta( $post->ID ) ) {
1876
				foreach ( $old_log_meta as $meta_key => $meta_value ) {
1877
					switch ( $meta_key ) {
1878
						case '_give_log_payment_id':
1879
							$log_data['log_parent']        = current( $meta_value );
1880
							$log_meta['_give_log_form_id'] = $post->post_parent;
1881
							break;
1882
1883
						default:
1884
							$log_meta[ $meta_key ] = current( $meta_value );
1885
					}
1886
				}
1887
			}
1888
1889
			if ( 'api_request' === $term_name ) {
1890
				$log_meta['_give_log_api_query'] = $post->post_excerpt;
1891
			}
1892
1893
			$wpdb->insert( "{$wpdb->prefix}give_logs", $log_data );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
1894
1895
			if ( ! empty( $log_meta ) ) {
1896
				foreach ( $log_meta as $meta_key => $meta_value ) {
1897
					Give()->logs->logmeta_db->update_meta( $post->ID, $meta_key, $meta_value );
1898
				}
1899
			}
1900
1901
			$logIDs[] = $post->ID;
1902
		}// End while().
1903
1904
		wp_reset_postdata();
1905
	} else {
1906
		// @todo: Delete terms and taxonomy after releases 2.0.
1907
		/*$terms = get_terms( 'give_log_type', array( 'fields' => 'ids', 'hide_empty' => false ) );
1908
		if ( ! empty( $terms ) ) {
1909
			foreach ( $terms as $term ) {
1910
				wp_delete_term( $term, 'give_log_type' );
1911
			}
1912
		}*/
1913
1914
		// @todo: Delete logs after releases 2.0.
1915
		/*$logIDs = get_posts( array(
1916
				'order'          => 'DESC',
1917
				'post_type'      => 'give_log',
1918
				'post_status'    => 'any',
1919
				'posts_per_page' => - 1,
1920
				'fields'         => 'ids',
1921
			)
1922
		);*/
1923
1924
		/*if ( ! empty( $logIDs ) ) {
1925
			foreach ( $logIDs as $log ) {
1926
				// Delete term relationship and posts.
1927
				wp_delete_object_term_relationships( $log, 'give_log_type' );
1928
				wp_delete_post( $log, true );
1929
			}
1930
		}*/
1931
1932
		// @todo: Unregister taxonomy after releases 2.0.
1933
		/*unregister_taxonomy( 'give_log_type' );*/
1934
1935
		// Delete log cache.
1936
		Give()->logs->delete_cache();
1937
1938
		// No more forms found, finish up.
1939
		give_set_upgrade_complete( 'v20_logs_upgrades' );
1940
	}
1941
}
1942
1943
1944
/**
1945
 * Move payment and form metadata to new table
1946
 *
1947
 * @since  2.0
1948
 * @return void
1949
 */
1950
function give_v20_move_metadata_into_new_table_callback() {
1951
	global $wpdb;
1952
	$give_updates = Give_Updates::get_instance();
1953
1954
	// form query
1955
	$payments = new WP_Query( array(
1956
			'paged'          => $give_updates->step,
1957
			'status'         => 'any',
1958
			'order'          => 'ASC',
1959
			'post_type'      => array( 'give_forms', 'give_payment' ),
1960
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1961
		)
1962
	);
1963
1964
	if ( $payments->have_posts() ) {
1965
		$give_updates->set_percentage( $payments->found_posts, $give_updates->step * 100 );
1966
1967
		while ( $payments->have_posts() ) {
1968
			$payments->the_post();
1969
			global $post;
1970
1971
			$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...
1972
				$wpdb->prepare(
1973
					"SELECT * FROM $wpdb->postmeta where post_id=%d",
1974
					get_the_ID()
1975
				),
1976
				ARRAY_A
1977
			);
1978
1979
			if ( ! empty( $meta_data ) ) {
1980
				foreach ( $meta_data as $index => $data ) {
1981
					// Check for duplicate meta values.
1982
					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...
1983
						continue;
1984
					}
1985
					
1986
					switch ( $post->post_type ) {
1987
						case 'give_forms':
1988
							$data['form_id'] = $data['post_id'];
1989
							unset( $data['post_id'] );
1990
1991
							Give()->form_meta->insert( $data );
1992
							// @todo: delete form meta from post meta table after releases 2.0.
1993
							/*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
1994
1995
							break;
1996
1997
						case 'give_payment':
1998
							$data['payment_id'] = $data['post_id'];
1999
							unset( $data['post_id'] );
2000
2001
							Give()->payment_meta->insert( $data );
2002
2003
							// @todo: delete donation meta from post meta table after releases 2.0.
2004
							/*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2005
2006
							break;
2007
					}
2008
				}
2009
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2010
2011
		}// End while().
2012
2013
		wp_reset_postdata();
2014
	} else {
2015
		// No more forms found, finish up.
2016
		give_set_upgrade_complete( 'v20_move_metadata_into_new_table' );
2017
	}
2018
2019
}
2020
2021
/**
2022
 * Upgrade routine for splitting donor name into first name and last name.
2023
 *
2024
 * @since 2.0
2025
 *
2026
 * @return void
2027
 */
2028
function give_v20_upgrades_donor_name() {
2029
	/* @var Give_Updates $give_updates */
2030
	$give_updates = Give_Updates::get_instance();
2031
2032
	$donors = Give()->donors->get_donors( array(
2033
		'paged'  => $give_updates->step,
2034
		'number' => 100,
2035
	) );
2036
2037
	if ( $donors ) {
2038
		$give_updates->set_percentage( count( $donors ), $give_updates->step * 100 );
2039
		// Loop through Donors
2040
		foreach ( $donors as $donor ) {
2041
2042
			$donor_name       = explode( ' ', $donor->name, 2 );
2043
			$donor_first_name = Give()->donor_meta->get_meta( $donor->id, '_give_donor_first_name' );
2044
			$donor_last_name  = Give()->donor_meta->get_meta( $donor->id, '_give_donor_last_name' );
2045
2046
			// If first name meta of donor is not created, then create it.
2047 View Code Duplication
			if ( ! $donor_first_name && isset( $donor_name[0] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

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

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

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

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

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

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

Loading history...
2058
				if ( isset( $donor_name[0] ) ) {
2059
					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...
2060
				}
2061
				if ( isset( $donor_name[1] ) ) {
2062
					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...
2063
				}
2064
			}
2065
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2066
2067
	} else {
2068
		// The Update Ran.
2069
		give_set_upgrade_complete( 'v20_upgrades_donor_name' );
2070
	}
2071
2072
}
2073
2074
/**
2075
 * Upgrade routine for user addresses.
2076
 *
2077
 * @since 2.0
2078
 * @global wpdb $wpdb
2079
 *
2080
 * @return void
2081
 */
2082
function give_v20_upgrades_user_address() {
2083
	global $wpdb;
2084
2085
	/* @var Give_Updates $give_updates */
2086
	$give_updates = Give_Updates::get_instance();
2087
2088
	/* @var WP_User_Query $user_query */
2089
	$user_query = new WP_User_Query(
2090
		array(
2091
			'number' => 100,
2092
			'paged'  => $give_updates->step,
2093
		)
2094
	);
2095
2096
	$users = $user_query->get_results();
2097
2098
	if ( $users ) {
2099
		$give_updates->set_percentage( $user_query->get_total(), $give_updates->step * 100 );
2100
2101
		// Loop through Donors
2102
		foreach ( $users as $user ) {
2103
			/* @var Give_Donor $donor */
2104
			$donor = new Give_Donor( $user->ID, true );
2105
2106
			if ( ! $donor->id ) {
2107
				continue;
2108
			}
2109
2110
			$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...
2111
				$wpdb->prepare(
2112
					"
2113
					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...
2114
					WHERE user_id=%s
2115
					AND meta_key=%s
2116
					",
2117
					$user->ID,
2118
					'_give_user_address'
2119
				)
2120
			);
2121
2122 View Code Duplication
			if ( ! empty( $address ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2123
				$address = maybe_unserialize( $address );
2124
				$donor->add_address( 'personal', $address );
2125
				$donor->add_address( 'billing[]', $address );
2126
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
2127
2128
				// @todo: delete _give_user_address from user meta after releases 2.0.
2129
				/*delete_user_meta( $user->ID, '_give_user_address' );*/
2130
			}
2131
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2132
2133
	} else {
2134
		// The Update Ran.
2135
		give_set_upgrade_complete( 'v20_upgrades_user_address' );
2136
	}
2137
2138
}
2139
2140
/**
2141
 * Upgrade logs data.
2142
 *
2143
 * @since  2.0
2144
 * @global wpdb $wpdb
2145
 * @return void
2146
 */
2147
function give_v20_rename_donor_tables_callback() {
2148
	global $wpdb;
2149
2150
	/* @var Give_Updates $give_updates */
2151
	$give_updates = Give_Updates::get_instance();
2152
2153
	$tables = array(
2154
		"{$wpdb->prefix}give_customers"    => "{$wpdb->prefix}give_donors",
2155
		"{$wpdb->prefix}give_customermeta" => "{$wpdb->prefix}give_donormeta",
2156
	);
2157
2158
	// Alter customer table
2159
	foreach ( $tables as $old_table => $new_table ) {
2160
		if (
2161
			$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...
2162
			! $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...
2163
		) {
2164
			$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...
2165
2166
			if ( "{$wpdb->prefix}give_donormeta" === $new_table ) {
2167
				$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...
2168
			}
2169
		}
2170
	}
2171
2172
	$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...
2173
2174
	// No more forms found, finish up.
2175
	give_set_upgrade_complete( 'v20_rename_donor_tables' );
2176
2177
	// Re initiate donor classes.
2178
	Give()->donors     = new Give_DB_Donors();
2179
	Give()->donor_meta = new Give_DB_Donor_Meta();
2180
}
2181
2182
2183
/**
2184
 * Create missing meta tables.
2185
 *
2186
 * @since  2.0.1
2187
 * @global wpdb $wpdb
2188
 * @return void
2189
 */
2190
function give_v201_create_tables(){
2191
	global $wpdb;
2192
2193
	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...
2194
		Give()->payment_meta->create_table();
2195
	}
2196
2197
	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...
2198
		Give()->form_meta->create_table();
2199
	}
2200
2201
	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...
2202
		Give()->logs->log_db->create_table();
2203
	}
2204
2205
	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...
2206
		Give()->logs->logmeta_db->create_table();
2207
	}
2208
}
2209
2210
/**
2211
 * Upgrade payment metadata for new metabox settings.
2212
 *
2213
 * @since  2.0.1
2214
 * @global wpdb $wpdb
2215
 * @return void
2216
 */
2217
function give_v201_upgrades_payment_metadata_callback() {
2218
	global $wpdb, $post;
2219
	$give_updates = Give_Updates::get_instance();
2220
	give_v201_create_tables();
2221
2222
	$payments = $wpdb->get_col(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
2223
		"
2224
			SELECT ID FROM $wpdb->posts
2225
			WHERE 1=1
2226
			AND ( 
2227
  				$wpdb->posts.post_date >= '2018-01-08 00:00:00'
2228
			)
2229
			AND $wpdb->posts.post_type = 'give_payment'
2230
			AND {$wpdb->posts}.post_status IN ('" . implode( "','", array_keys( give_get_payment_statuses() ) ) . "')
2231
			ORDER BY $wpdb->posts.post_date ASC 
2232
			LIMIT 100
2233
			OFFSET " . ( 1 === $give_updates->step ? 0 : ( $give_updates->step * 100 ) )
2234
	);
2235
2236
	if ( ! empty( $payments ) ) {
2237
		$give_updates->set_percentage( give_get_total_post_type_count( 'give_payment' ), ( $give_updates->step * 100 ) );
2238
2239
		foreach ( $payments as $payment_id ) {
2240
			$post = get_post( $payment_id );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
2241
			setup_postdata( $post );
2242
2243
			// Do not add new meta keys if already refactored.
2244
			if ( $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id=%d AND meta_key=%s", $post->ID, '_give_payment_donor_id' ) ) ) {
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
2245
				continue;
2246
			}
2247
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
2248
2249
			// Split _give_payment_meta meta.
2250
			// @todo Remove _give_payment_meta after releases 2.0
2251
			$payment_meta = give_get_meta( $post->ID, '_give_payment_meta', true );
2252
2253
			if ( ! empty( $payment_meta ) ) {
2254
				_give_20_bc_split_and_save_give_payment_meta( $post->ID, $payment_meta );
2255
			}
2256
2257
			$deprecated_meta_keys = array(
2258
				'_give_payment_customer_id' => '_give_payment_donor_id',
2259
				'_give_payment_user_email'  => '_give_payment_donor_email',
2260
				'_give_payment_user_ip'     => '_give_payment_donor_ip',
2261
			);
2262
2263 View Code Duplication
			foreach ( $deprecated_meta_keys as $old_meta_key => $new_meta_key ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2264
				// Do not add new meta key if already exist.
2265
				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...
2266
					continue;
2267
				}
2268
2269
				$wpdb->insert(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
2270
					$wpdb->postmeta,
2271
					array(
2272
						'post_id' => $post->ID,
2273
						'meta_key' => $new_meta_key,
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
2274
						'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...
2275
					)
2276
				);
2277
			}
2278
2279
			// Bailout
2280 View Code Duplication
			if ( $donor_id = give_get_meta( $post->ID, '_give_payment_donor_id', true ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2281
				/* @var Give_Donor $donor */
2282
				$donor = new Give_Donor( $donor_id );
2283
2284
				$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...
2285
				$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...
2286
				$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...
2287
				$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...
2288
				$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...
2289
				$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...
2290
2291
				// Save address.
2292
				$donor->add_address( 'billing[]', $address );
2293
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2294
2295
		}// End while().
2296
2297
		wp_reset_postdata();
2298
	} else {
2299
		// @todo Delete user id meta after releases 2.0
2300
		// $wpdb->get_var( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key=%s", '_give_payment_user_id' ) );
2301
2302
		// No more forms found, finish up.
2303
		give_set_upgrade_complete( 'v201_upgrades_payment_metadata' );
2304
	}
2305
}
2306
2307
/**
2308
 * Move payment and form metadata to new table
2309
 *
2310
 * @since  2.0.1
2311
 * @return void
2312
 */
2313
function give_v201_move_metadata_into_new_table_callback() {
2314
	global $wpdb, $post;
2315
	$give_updates = Give_Updates::get_instance();
2316
	give_v201_create_tables();
2317
2318
	$payments = $wpdb->get_col(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
2319
		"
2320
			SELECT ID FROM $wpdb->posts 
2321
			WHERE 1=1
2322
			AND ( $wpdb->posts.post_type = 'give_payment' OR $wpdb->posts.post_type = 'give_forms' )
2323
			AND {$wpdb->posts}.post_status IN ('" . implode( "','", array_keys( give_get_payment_statuses() ) ) . "')
2324
			ORDER BY $wpdb->posts.post_date ASC 
2325
			LIMIT 100
2326
			OFFSET " . ( 1 === $give_updates->step ? 0 : ( $give_updates->step * 100 ) )
2327
	);
2328
2329
	if ( ! empty( $payments ) ) {
2330
		$give_updates->set_percentage( give_get_total_post_type_count( array( 'give_forms', 'give_payment' ) ), $give_updates->step * 100 );
0 ignored issues
show
Documentation introduced by
array('give_forms', 'give_payment') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

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

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

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

function acceptsInteger($int) { }

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

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
2331
2332
		foreach ( $payments as $payment_id ) {
2333
			$post = get_post( $payment_id );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
2334
			setup_postdata( $post );
2335
2336
			$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...
2337
				$wpdb->prepare(
2338
					"SELECT * FROM $wpdb->postmeta where post_id=%d",
2339
					get_the_ID()
2340
				),
2341
				ARRAY_A
2342
			);
2343
2344
			if ( ! empty( $meta_data ) ) {
2345
				foreach ( $meta_data as $index => $data ) {
2346
					// Check for duplicate meta values.
2347
					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...
2348
						continue;
2349
					}
2350
2351
					switch ( $post->post_type ) {
2352
						case 'give_forms':
2353
							$data['form_id'] = $data['post_id'];
2354
							unset( $data['post_id'] );
2355
2356
							Give()->form_meta->insert( $data );
2357
							// @todo: delete form meta from post meta table after releases 2.0.
2358
							/*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2359
2360
							break;
2361
2362
						case 'give_payment':
2363
							$data['payment_id'] = $data['post_id'];
2364
							unset( $data['post_id'] );
2365
2366
							Give()->payment_meta->insert( $data );
2367
2368
							// @todo: delete donation meta from post meta table after releases 2.0.
2369
							/*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2370
2371
							break;
2372
					}
2373
				}
2374
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2375
2376
		}// End while().
2377
2378
		wp_reset_postdata();
2379
	} else {
2380
		// No more forms found, finish up.
2381
		give_set_upgrade_complete( 'v201_move_metadata_into_new_table' );
2382
	}
2383
2384
}
2385
2386
/**
2387
 * Move data to new log table.
2388
 *
2389
 * @since  2.0.1
2390
 * @return void
2391
 */
2392
function give_v201_logs_upgrades_callback() {
2393
	global $wpdb, $post;
2394
	$give_updates = Give_Updates::get_instance();
2395
	give_v201_create_tables();
2396
2397
	$logs = $wpdb->get_col(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
2398
		"
2399
			SELECT ID FROM $wpdb->posts 
2400
			WHERE 1=1
2401
			AND $wpdb->posts.post_type = 'give_log'
2402
			AND {$wpdb->posts}.post_status IN ('" . implode( "','", array_keys( give_get_payment_statuses() ) ) . "')
2403
			ORDER BY $wpdb->posts.post_date ASC 
2404
			LIMIT 100
2405
			OFFSET " . ( 1 === $give_updates->step ? 0 : ( $give_updates->step * 100 ) )
2406
	);
2407
2408
	if ( ! empty( $logs ) ) {
2409
		$give_updates->set_percentage( give_get_total_post_type_count( 'give_log' ), $give_updates->step * 100 );
2410
2411
		foreach ( $logs as $log_id ) {
2412
			$post = get_post( $log_id );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
2413
			setup_postdata( $post );
2414
2415
			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...
2416
				continue;
2417
			}
2418
2419
			$term      = get_the_terms( $post->ID, 'give_log_type' );
2420
			$term      = ! is_wp_error( $term ) && ! empty( $term ) ? $term[0] : array();
2421
			$term_name = ! empty( $term ) ? $term->slug : '';
2422
2423
			$log_data = array(
2424
				'ID'           => $post->ID,
2425
				'log_title'    => $post->post_title,
2426
				'log_content'  => $post->post_content,
2427
				'log_parent'   => 0,
2428
				'log_type'     => $term_name,
2429
				'log_date'     => $post->post_date,
2430
				'log_date_gmt' => $post->post_date_gmt,
2431
			);
2432
			$log_meta = array();
2433
2434
			if ( $old_log_meta = get_post_meta( $post->ID ) ) {
2435
				foreach ( $old_log_meta as $meta_key => $meta_value ) {
2436
					switch ( $meta_key ) {
2437
						case '_give_log_payment_id':
2438
							$log_data['log_parent']        = current( $meta_value );
2439
							$log_meta['_give_log_form_id'] = $post->post_parent;
2440
							break;
2441
2442
						default:
2443
							$log_meta[ $meta_key ] = current( $meta_value );
2444
					}
2445
				}
2446
			}
2447
2448
			if ( 'api_request' === $term_name ) {
2449
				$log_meta['_give_log_api_query'] = $post->post_excerpt;
2450
			}
2451
2452
			$wpdb->insert( "{$wpdb->prefix}give_logs", $log_data );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
2453
2454
			if ( ! empty( $log_meta ) ) {
2455
				foreach ( $log_meta as $meta_key => $meta_value ) {
2456
					Give()->logs->logmeta_db->update_meta( $post->ID, $meta_key, $meta_value );
2457
				}
2458
			}
2459
2460
			$logIDs[] = $post->ID;
2461
		}// End while().
2462
2463
		wp_reset_postdata();
2464
	} else {
2465
		// Delete log cache.
2466
		Give()->logs->delete_cache();
2467
2468
		// No more forms found, finish up.
2469
		give_set_upgrade_complete( 'v201_logs_upgrades' );
2470
	}
2471
}
2472
2473
2474
/**
2475
 * Add missing donor.
2476
 *
2477
 * @since  2.0.1
2478
 * @return void
2479
 */
2480
function give_v201_add_missing_donors_callback(){
2481
	global $wpdb;
2482
	give_v201_create_tables();
2483
2484
	if ( $wpdb->query( $wpdb->prepare( "SHOW TABLES LIKE %s", "{$wpdb->prefix}give_customers" ) ) ) {
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
Coding Style Comprehensibility introduced by
The string literal SHOW TABLES LIKE %s does not require double quotes, as per coding-style, please use single quotes.

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

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

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

<?php

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

print $doubleQuoted;

will print an indented: Single is Value

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

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

Loading history...
2485
		$customers  = wp_list_pluck( $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}give_customers" ), 'id' );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
2486
		$donors     = wp_list_pluck( $wpdb->get_results( "SELECT id FROM {$wpdb->prefix}give_donors" ), 'id' );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
2487
		$donor_data = array();
2488
2489
		if ( $missing_donors = array_diff( $customers, $donors ) ) {
2490
			foreach ( $missing_donors as $donor_id ) {
2491
				$donor_data[] = array(
2492
					'info' => $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}give_customers WHERE id=%d", $donor_id ) ),
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
2493
					'meta' => $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}give_customermeta WHERE customer_id=%d", $donor_id ) ),
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
2494
2495
				);
2496
			}
2497
		}
2498
2499
		if( ! empty( $donor_data ) ) {
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...
2500
			$donor_table_name = Give()->donors->table_name;
2501
			$donor_meta_table_name = Give()->donor_meta->table_name;
2502
2503
			Give()->donors->table_name = "{$wpdb->prefix}give_donors";
2504
			Give()->donor_meta->table_name = "{$wpdb->prefix}give_donormeta";
2505
2506
			foreach ( $donor_data as $donor ) {
2507
				$donor['info'][0] = (array) $donor['info'][0];
2508
2509
				// Prevent duplicate meta id issue.
2510
				if( $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}give_donors WHERE id=%d", $donor['info'][0]['id'] ) ) ){
0 ignored issues
show
introduced by
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...
2511
					continue;
2512
				}
2513
2514
				$donor_id = Give()->donors->add( $donor['info'][0] );
2515
2516
				if( ! empty( $donor['meta'] ) ) {
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...
2517
					foreach ( $donor['meta'] as $donor_meta ) {
2518
						$donor_meta = (array) $donor_meta;
2519
2520
						// Prevent duplicate meta id issue.
2521
						if( $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}give_donormeta WHERE meta_id=%d", $donor_meta['meta_id'] ) ) ){
0 ignored issues
show
introduced by
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...
2522
							unset( $donor_meta['meta_id'] );
2523
						}
2524
2525
						$donor_meta['donor_id'] = $donor_meta['customer_id'];
2526
						unset( $donor_meta['customer_id'] );
2527
2528
						Give()->donor_meta->insert( $donor_meta );
2529
					}
2530
				}
2531
2532
				/**
2533
				 * Fix donor name and address
2534
				 */
2535
				$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...
2536
					$wpdb->prepare(
2537
						"
2538
					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...
2539
					WHERE user_id=%s
2540
					AND meta_key=%s
2541
					",
2542
						$donor['info'][0]['user_id'],
2543
						'_give_user_address'
2544
					)
2545
				);
2546
2547
				$donor = new Give_Donor( $donor_id );
2548
2549 View Code Duplication
				if ( ! empty( $address ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2550
					$address = maybe_unserialize( $address );
2551
					$donor->add_address( 'personal', $address );
2552
					$donor->add_address( 'billing[]', $address );
2553
				}
2554
2555
				$donor_name       = explode( ' ', $donor->name, 2 );
2556
				$donor_first_name = Give()->donor_meta->get_meta( $donor->id, '_give_donor_first_name' );
2557
				$donor_last_name  = Give()->donor_meta->get_meta( $donor->id, '_give_donor_last_name' );
2558
2559
				// If first name meta of donor is not created, then create it.
2560 View Code Duplication
				if ( ! $donor_first_name && isset( $donor_name[0] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

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

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

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

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

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

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

Loading history...
2571
					if ( isset( $donor_name[0] ) ) {
2572
						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...
2573
					}
2574
					if ( isset( $donor_name[1] ) ) {
2575
						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...
2576
					}
2577
				}
2578
			}
2579
2580
			Give()->donors->table_name = $donor_table_name;
2581
			Give()->donor_meta->table_name = $donor_meta_table_name;
2582
		}
2583
	}
2584
2585
	Give_Updates::get_instance()->percentage = 100;
0 ignored issues
show
Documentation Bug introduced by
It seems like 100 of type integer is incompatible with the declared type array of property $percentage.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
2586
	give_set_upgrade_complete('v201_add_missing_donors' );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
2587
}
2588
2589
2590
/**
2591
 * Version 2.0.3 automatic updates
2592
 *
2593
 * @since 2.0.3
2594
 */
2595
function give_v203_upgrades(){
2596
	global $wpdb;
2597
2598
	// Do not auto load option.
2599
	$wpdb->update( $wpdb->options, array( 'autoload' => 'no' ), array( 'option_name' => 'give_completed_upgrades' ) );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
2600
2601
	// Remove from cache.
2602
	$all_options = wp_load_alloptions();
2603
2604
	if( isset( $all_options['give_completed_upgrades'] ) ) {
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...
2605
		unset( $all_options['give_completed_upgrades'] );
2606
		wp_cache_set( 'alloptions', $all_options, 'options' );
2607
	}
2608
2609
}