Test Failed
Push — feature/upgrdae-auto-pause ( 36ce2e...015823 )
by Ravinder
04:41
created

upgrade-functions.php ➔ give_v203_upgrades()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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