Completed
Push — release/2.2 ( 98e896...b0db7e )
by Ravinder
724:37 queued 717:31
created

upgrade-functions.php ➔ give_v220_upgrades()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 60
rs 8.8727
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Loading history...
445
	give_set_upgrade_complete( 'upgrade_give_payment_customer_id' );
446
}
447
448
449
/**
450
 * Upgrades the Offline Status
451
 *
452
 * Reverses the issue where offline donations in "pending" status where inappropriately marked as abandoned
453
 *
454
 * @since      1.3.4
455
 */
456
function give_v134_upgrade_give_offline_status() {
457
	global $wpdb;
458
459
	/* @var Give_Updates $give_updates */
460
	$give_updates = Give_Updates::get_instance();
461
462
	// Get abandoned offline payments.
463
	$select = "SELECT ID FROM $wpdb->posts p ";
464
	$join   = "LEFT JOIN $wpdb->postmeta m ON p.ID = m.post_id ";
465
	$where  = "WHERE p.post_type = 'give_payment' ";
466
	$where  .= "AND ( p.post_status = 'abandoned' )";
467
	$where  .= "AND ( m.meta_key = '_give_payment_gateway' AND m.meta_value = 'offline' )";
468
469
	$sql            = $select . $join . $where;
470
	$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...
471
472
	foreach ( $found_payments as $payment ) {
473
474
		// Only change ones marked abandoned since our release last week because the admin may have marked some abandoned themselves.
475
		$modified_time = get_post_modified_time( 'U', false, $payment );
476
477
		// 1450124863 =  12/10/2015 20:42:25.
478
		if ( $modified_time >= 1450124863 ) {
479
480
			give_update_payment_status( $payment, 'pending' );
481
482
		}
483
	}
484
485
	$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...
486
	give_set_upgrade_complete( 'upgrade_give_offline_status' );
487
}
488
489
490
/**
491
 * Cleanup User Roles
492
 *
493
 * This upgrade routine removes unused roles and roles with typos
494
 *
495
 * @since      1.5.2
496
 */
497
function give_v152_cleanup_users() {
498
499
	$give_version = get_option( 'give_version' );
500
501
	if ( ! $give_version ) {
502
		// 1.0 is the first version to use this option so we must add it.
503
		$give_version = '1.0';
504
	}
505
506
	$give_version = preg_replace( '/[^0-9.].*/', '', $give_version );
507
508
	// v1.5.2 Upgrades
509
	if ( version_compare( $give_version, '1.5.2', '<' ) || ! give_has_upgrade_completed( 'upgrade_give_user_caps_cleanup' ) ) {
510
511
		// Delete all caps with "ss".
512
		// Also delete all unused "campaign" roles.
513
		$delete_caps = array(
514
			'delete_give_formss',
515
			'delete_others_give_formss',
516
			'delete_private_give_formss',
517
			'delete_published_give_formss',
518
			'read_private_forms',
519
			'edit_give_formss',
520
			'edit_others_give_formss',
521
			'edit_private_give_formss',
522
			'edit_published_give_formss',
523
			'publish_give_formss',
524
			'read_private_give_formss',
525
			'assign_give_campaigns_terms',
526
			'delete_give_campaigns',
527
			'delete_give_campaigns_terms',
528
			'delete_give_campaignss',
529
			'delete_others_give_campaignss',
530
			'delete_private_give_campaignss',
531
			'delete_published_give_campaignss',
532
			'edit_give_campaigns',
533
			'edit_give_campaigns_terms',
534
			'edit_give_campaignss',
535
			'edit_others_give_campaignss',
536
			'edit_private_give_campaignss',
537
			'edit_published_give_campaignss',
538
			'manage_give_campaigns_terms',
539
			'publish_give_campaignss',
540
			'read_give_campaigns',
541
			'read_private_give_campaignss',
542
			'view_give_campaigns_stats',
543
			'delete_give_paymentss',
544
			'delete_others_give_paymentss',
545
			'delete_private_give_paymentss',
546
			'delete_published_give_paymentss',
547
			'edit_give_paymentss',
548
			'edit_others_give_paymentss',
549
			'edit_private_give_paymentss',
550
			'edit_published_give_paymentss',
551
			'publish_give_paymentss',
552
			'read_private_give_paymentss',
553
		);
554
555
		global $wp_roles;
556
		foreach ( $delete_caps as $cap ) {
557
			foreach ( array_keys( $wp_roles->roles ) as $role ) {
558
				$wp_roles->remove_cap( $role, $cap );
559
			}
560
		}
561
562
		// Create Give plugin roles.
563
		$roles = new Give_Roles();
564
		$roles->add_roles();
565
		$roles->add_caps();
566
567
		// The Update Ran.
568
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ), false );
569
		give_set_upgrade_complete( 'upgrade_give_user_caps_cleanup' );
570
		delete_option( 'give_doing_upgrade' );
571
572
	}// End if().
573
574
}
575
576
add_action( 'admin_init', 'give_v152_cleanup_users' );
577
578
/**
579
 * 1.6 Upgrade routine to create the customer meta table.
580
 *
581
 * @since  1.6
582
 * @return void
583
 */
584
function give_v16_upgrades() {
585
	// Create the donor databases.
586
	$donors_db = new Give_DB_Donors();
587
	$donors_db->create_table();
588
	$donor_meta = new Give_DB_Donor_Meta();
589
	$donor_meta->create_table();
590
}
591
592
/**
593
 * 1.7 Upgrades.
594
 *
595
 * a. Update license api data for plugin addons.
596
 * b. Cleanup user roles.
597
 *
598
 * @since  1.7
599
 * @return void
600
 */
601
function give_v17_upgrades() {
602
	// Upgrade license data.
603
	give_v17_upgrade_addon_license_data();
604
	give_v17_cleanup_roles();
605
}
606
607
/**
608
 * Upgrade license data
609
 *
610
 * @since 1.7
611
 */
612
function give_v17_upgrade_addon_license_data() {
613
	$give_options = give_get_settings();
614
615
	$api_url = 'https://givewp.com/give-sl-api/';
616
617
	// Get addons license key.
618
	$addons = array();
619
	foreach ( $give_options as $key => $value ) {
620
		if ( false !== strpos( $key, '_license_key' ) ) {
621
			$addons[ $key ] = $value;
622
		}
623
	}
624
625
	// Bailout: We do not have any addon license data to upgrade.
626
	if ( empty( $addons ) ) {
627
		return false;
628
	}
629
630
	foreach ( $addons as $key => $addon_license ) {
631
632
		// Get addon shortname.
633
		$shortname = str_replace( '_license_key', '', $key );
634
635
		// Addon license option name.
636
		$addon_license_option_name = $shortname . '_license_active';
637
638
		// bailout if license is empty.
639
		if ( empty( $addon_license ) ) {
640
			delete_option( $addon_license_option_name );
641
			continue;
642
		}
643
644
		// Get addon name.
645
		$addon_name       = array();
646
		$addon_name_parts = explode( '_', str_replace( 'give_', '', $shortname ) );
647
		foreach ( $addon_name_parts as $name_part ) {
648
649
			// Fix addon name
650
			switch ( $name_part ) {
651
				case 'authorizenet' :
652
					$name_part = 'authorize.net';
653
					break;
654
			}
655
656
			$addon_name[] = ucfirst( $name_part );
657
		}
658
659
		$addon_name = implode( ' ', $addon_name );
660
661
		// Data to send to the API.
662
		$api_params = array(
663
			'edd_action' => 'activate_license', // never change from "edd_" to "give_"!
664
			'license'    => $addon_license,
665
			'item_name'  => urlencode( $addon_name ),
666
			'url'        => home_url(),
667
		);
668
669
		// Call the API.
670
		$response = wp_remote_post(
671
			$api_url,
672
			array(
673
				'timeout'   => 15,
674
				'sslverify' => false,
675
				'body'      => $api_params,
676
			)
677
		);
678
679
		// Make sure there are no errors.
680
		if ( is_wp_error( $response ) ) {
681
			delete_option( $addon_license_option_name );
682
			continue;
683
		}
684
685
		// Tell WordPress to look for updates.
686
		set_site_transient( 'update_plugins', null );
687
688
		// Decode license data.
689
		$license_data = json_decode( wp_remote_retrieve_body( $response ) );
690
		update_option( $addon_license_option_name, $license_data, false );
691
	}// End foreach().
692
}
693
694
695
/**
696
 * Cleanup User Roles.
697
 *
698
 * This upgrade routine removes unused roles and roles with typos.
699
 *
700
 * @since      1.7
701
 */
702
function give_v17_cleanup_roles() {
703
704
	// Delete all caps with "_give_forms_" and "_give_payments_".
705
	// These roles have no usage; the proper is singular.
706
	$delete_caps = array(
707
		'view_give_forms_stats',
708
		'delete_give_forms_terms',
709
		'assign_give_forms_terms',
710
		'edit_give_forms_terms',
711
		'manage_give_forms_terms',
712
		'view_give_payments_stats',
713
		'manage_give_payments_terms',
714
		'edit_give_payments_terms',
715
		'assign_give_payments_terms',
716
		'delete_give_payments_terms',
717
	);
718
719
	global $wp_roles;
720
	foreach ( $delete_caps as $cap ) {
721
		foreach ( array_keys( $wp_roles->roles ) as $role ) {
722
			$wp_roles->remove_cap( $role, $cap );
723
		}
724
	}
725
726
	// Set roles again.
727
	$roles = new Give_Roles();
728
	$roles->add_roles();
729
	$roles->add_caps();
730
731
}
732
733
/**
734
 * 1.8 Upgrades.
735
 *
736
 * a. Upgrade checkbox settings to radio button settings.
737
 * a. Update form meta for new metabox settings.
738
 *
739
 * @since  1.8
740
 * @return void
741
 */
742
function give_v18_upgrades() {
743
	// Upgrade checkbox settings to radio button settings.
744
	give_v18_upgrades_core_setting();
745
}
746
747
/**
748
 * Upgrade core settings.
749
 *
750
 * @since  1.8
751
 * @return void
752
 */
753
function give_v18_upgrades_core_setting() {
754
	// Core settings which changes from checkbox to radio.
755
	$core_setting_names = array_merge(
756
		array_keys( give_v18_renamed_core_settings() ),
757
		array(
758
			'uninstall_on_delete',
759
			'scripts_footer',
760
			'test_mode',
761
			'email_access',
762
			'terms',
763
			'give_offline_donation_enable_billing_fields',
764
		)
765
	);
766
767
	// Bailout: If not any setting define.
768
	if ( $give_settings = get_option( 'give_settings' ) ) {
769
770
		$setting_changed = false;
771
772
		// Loop: check each setting field.
773
		foreach ( $core_setting_names as $setting_name ) {
774
			// New setting name.
775
			$new_setting_name = preg_replace( '/^(enable_|disable_)/', '', $setting_name );
776
777
			// Continue: If setting already set.
778
			if (
779
				array_key_exists( $new_setting_name, $give_settings )
780
				&& in_array( $give_settings[ $new_setting_name ], array( 'enabled', 'disabled' ) )
781
			) {
782
				continue;
783
			}
784
785
			// Set checkbox value to radio value.
786
			$give_settings[ $setting_name ] = ( ! empty( $give_settings[ $setting_name ] ) && 'on' === $give_settings[ $setting_name ] ? 'enabled' : 'disabled' );
787
788
			// @see https://github.com/WordImpress/Give/issues/1063.
789
			if ( false !== strpos( $setting_name, 'disable_' ) ) {
790
791
				$give_settings[ $new_setting_name ] = ( give_is_setting_enabled( $give_settings[ $setting_name ] ) ? 'disabled' : 'enabled' );
792
			} elseif ( false !== strpos( $setting_name, 'enable_' ) ) {
793
794
				$give_settings[ $new_setting_name ] = ( give_is_setting_enabled( $give_settings[ $setting_name ] ) ? 'enabled' : 'disabled' );
795
			}
796
797
			// Tell bot to update core setting to db.
798
			if ( ! $setting_changed ) {
799
				$setting_changed = true;
800
			}
801
		}
802
803
		// Update setting only if they changed.
804
		if ( $setting_changed ) {
805
			update_option( 'give_settings', $give_settings, false );
806
		}
807
	}// End if().
808
809
	give_set_upgrade_complete( 'v18_upgrades_core_setting' );
810
}
811
812
/**
813
 * Upgrade form metadata for new metabox settings.
814
 *
815
 * @since  1.8
816
 * @return void
817
 */
818
function give_v18_upgrades_form_metadata() {
819
	/* @var Give_Updates $give_updates */
820
	$give_updates = Give_Updates::get_instance();
821
822
	// form query
823
	$forms = new WP_Query( array(
824
			'paged'          => $give_updates->step,
825
			'status'         => 'any',
826
			'order'          => 'ASC',
827
			'post_type'      => 'give_forms',
828
			'posts_per_page' => 20,
829
		)
830
	);
831
832
	if ( $forms->have_posts() ) {
833
		$give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 20 ) );
834
835
		while ( $forms->have_posts() ) {
836
			$forms->the_post();
837
838
			// Form content.
839
			// Note in version 1.8 display content setting split into display content and content placement setting.
840
			// You can delete _give_content_option in future.
841
			$show_content = give_get_meta( get_the_ID(), '_give_content_option', true );
842
			if ( $show_content && ! give_get_meta( get_the_ID(), '_give_display_content', true ) ) {
843
				$field_value = ( 'none' !== $show_content ? 'enabled' : 'disabled' );
844
				give_update_meta( get_the_ID(), '_give_display_content', $field_value );
845
846
				$field_value = ( 'none' !== $show_content ? $show_content : 'give_pre_form' );
847
				give_update_meta( get_the_ID(), '_give_content_placement', $field_value );
848
			}
849
850
			// "Disable" Guest Donation. Checkbox.
851
			// See: https://github.com/WordImpress/Give/issues/1470.
852
			$guest_donation        = give_get_meta( get_the_ID(), '_give_logged_in_only', true );
853
			$guest_donation_newval = ( in_array( $guest_donation, array( 'yes', 'on' ) ) ? 'disabled' : 'enabled' );
854
			give_update_meta( get_the_ID(), '_give_logged_in_only', $guest_donation_newval );
855
856
			// Offline Donations.
857
			// See: https://github.com/WordImpress/Give/issues/1579.
858
			$offline_donation = give_get_meta( get_the_ID(), '_give_customize_offline_donations', true );
859
			if ( 'no' === $offline_donation ) {
860
				$offline_donation_newval = 'global';
861
			} elseif ( 'yes' === $offline_donation ) {
862
				$offline_donation_newval = 'enabled';
863
			} else {
864
				$offline_donation_newval = 'disabled';
865
			}
866
			give_update_meta( get_the_ID(), '_give_customize_offline_donations', $offline_donation_newval );
867
868
			// Convert yes/no setting field to enabled/disabled.
869
			$form_radio_settings = array(
870
				// Custom Amount.
871
				'_give_custom_amount',
872
873
				// Donation Gaol.
874
				'_give_goal_option',
875
876
				// Close Form.
877
				'_give_close_form_when_goal_achieved',
878
879
				// Term & conditions.
880
				'_give_terms_option',
881
882
				// Billing fields.
883
				'_give_offline_donation_enable_billing_fields_single',
884
			);
885
886
			foreach ( $form_radio_settings as $meta_key ) {
887
				// Get value.
888
				$field_value = give_get_meta( get_the_ID(), $meta_key, true );
889
890
				// Convert meta value only if it is in yes/no/none.
891
				if ( in_array( $field_value, array( 'yes', 'on', 'no', 'none' ) ) ) {
892
893
					$field_value = ( in_array( $field_value, array( 'yes', 'on' ) ) ? 'enabled' : 'disabled' );
894
					give_update_meta( get_the_ID(), $meta_key, $field_value );
895
				}
896
			}
897
		}// End while().
898
899
		wp_reset_postdata();
900
901
	} else {
902
		// No more forms found, finish up.
903
		give_set_upgrade_complete( 'v18_upgrades_form_metadata' );
904
	}
905
}
906
907
908
/**
909
 * Get list of core setting renamed in version 1.8.
910
 *
911
 * @since  1.8
912
 * @return array
913
 */
914
function give_v18_renamed_core_settings() {
915
	return array(
916
		'disable_paypal_verification' => 'paypal_verification',
917
		'disable_css'                 => 'css',
918
		'disable_welcome'             => 'welcome',
919
		'disable_forms_singular'      => 'forms_singular',
920
		'disable_forms_archives'      => 'forms_archives',
921
		'disable_forms_excerpt'       => 'forms_excerpt',
922
		'disable_form_featured_img'   => 'form_featured_img',
923
		'disable_form_sidebar'        => 'form_sidebar',
924
		'disable_admin_notices'       => 'admin_notices',
925
		'disable_the_content_filter'  => 'the_content_filter',
926
		'enable_floatlabels'          => 'floatlabels',
927
		'enable_categories'           => 'categories',
928
		'enable_tags'                 => 'tags',
929
	);
930
}
931
932
933
/**
934
 * Upgrade core settings.
935
 *
936
 * @since  1.8.7
937
 * @return void
938
 */
939
function give_v187_upgrades() {
940
	global $wpdb;
941
942
	/**
943
	 * Upgrade 1: Remove stat and cache transients.
944
	 */
945
	$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...
946
		$wpdb->prepare(
947
			"
948
					SELECT *
949
					FROM {$wpdb->options}
950
					WHERE (
951
					option_name LIKE %s
952
					OR option_name LIKE %s
953
					OR option_name LIKE %s
954
					OR option_name LIKE %s
955
					OR option_name LIKE %s
956
					OR option_name LIKE %s
957
					OR option_name LIKE %s
958
					OR option_name LIKE %s
959
					OR option_name LIKE %s
960
					OR option_name LIKE %s
961
					OR option_name LIKE %s
962
					OR option_name LIKE %s
963
					OR option_name LIKE %s
964
					)
965
					",
966
			array(
967
				'%_transient_give_stats_%',
968
				'give_cache%',
969
				'%_transient_give_add_ons_feed%',
970
				'%_transient__give_ajax_works' .
971
				'%_transient_give_total_api_keys%',
972
				'%_transient_give_i18n_give_promo_hide%',
973
				'%_transient_give_contributors%',
974
				'%_transient_give_estimated_monthly_stats%',
975
				'%_transient_give_earnings_total%',
976
				'%_transient_give_i18n_give_%',
977
				'%_transient__give_installed%',
978
				'%_transient__give_activation_redirect%',
979
				'%_transient__give_hide_license_notices_shortly_%',
980
				'%give_income_total%',
981
			)
982
		),
983
		1
984
	);
985
986
	// User related transients.
987
	$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...
988
		$wpdb->prepare(
989
			"SELECT user_id, meta_key
990
			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...
991
			WHERE meta_value=%s",
992
			'give_user_public_key'
993
		),
994
		ARRAY_A
995
	);
996
997
	if ( ! empty( $user_apikey_options ) ) {
998
		foreach ( $user_apikey_options as $user ) {
999
			$cached_options[] = '_transient_' . md5( 'give_api_user_' . $user['meta_key'] );
1000
			$cached_options[] = '_transient_' . md5( 'give_api_user_public_key' . $user['user_id'] );
1001
			$cached_options[] = '_transient_' . md5( 'give_api_user_secret_key' . $user['user_id'] );
1002
		}
1003
	}
1004
1005
	if ( ! empty( $cached_options ) ) {
1006
		foreach ( $cached_options as $option ) {
1007 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...
1008
				case ( false !== strpos( $option, 'transient' ) ):
1009
					$option = str_replace( '_transient_', '', $option );
1010
					delete_transient( $option );
1011
					break;
1012
1013
				default:
1014
					delete_option( $option );
1015
			}
1016
		}
1017
	}
1018
}
1019
1020
/**
1021
 * Update Capabilities for Give_Worker User Role.
1022
 *
1023
 * This upgrade routine will update access rights for Give_Worker User Role.
1024
 *
1025
 * @since      1.8.8
1026
 */
1027
function give_v188_upgrades() {
1028
1029
	global $wp_roles;
1030
1031
	// Get the role object.
1032
	$give_worker = get_role( 'give_worker' );
1033
1034
	// A list of capabilities to add for give workers.
1035
	$caps_to_add = array(
1036
		'edit_posts',
1037
		'edit_pages',
1038
	);
1039
1040
	foreach ( $caps_to_add as $cap ) {
1041
		// Add the capability.
1042
		$give_worker->add_cap( $cap );
1043
	}
1044
1045
}
1046
1047
/**
1048
 * Update Post meta for minimum and maximum amount for multi level donation forms
1049
 *
1050
 * This upgrade routine adds post meta for give_forms CPT for multi level donation form.
1051
 *
1052
 * @since      1.8.9
1053
 */
1054
function give_v189_upgrades_levels_post_meta_callback() {
1055
	/* @var Give_Updates $give_updates */
1056
	$give_updates = Give_Updates::get_instance();
1057
1058
	// form query.
1059
	$donation_forms = new WP_Query( array(
1060
			'paged'          => $give_updates->step,
1061
			'status'         => 'any',
1062
			'order'          => 'ASC',
1063
			'post_type'      => 'give_forms',
1064
			'posts_per_page' => 20,
1065
		)
1066
	);
1067
1068
	if ( $donation_forms->have_posts() ) {
1069
		$give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
1070
1071
		while ( $donation_forms->have_posts() ) {
1072
			$donation_forms->the_post();
1073
			$form_id = get_the_ID();
1074
1075
			// Remove formatting from _give_set_price.
1076
			update_post_meta(
1077
				$form_id,
1078
				'_give_set_price',
1079
				give_sanitize_amount( get_post_meta( $form_id, '_give_set_price', true ) )
1080
			);
1081
1082
			// Remove formatting from _give_custom_amount_minimum.
1083
			update_post_meta(
1084
				$form_id,
1085
				'_give_custom_amount_minimum',
1086
				give_sanitize_amount( get_post_meta( $form_id, '_give_custom_amount_minimum', true ) )
1087
			);
1088
1089
			// Bailout.
1090
			if ( 'set' === get_post_meta( $form_id, '_give_price_option', true ) ) {
1091
				continue;
1092
			}
1093
1094
			$donation_levels = get_post_meta( $form_id, '_give_donation_levels', true );
1095
1096
			if ( ! empty( $donation_levels ) ) {
1097
1098
				foreach ( $donation_levels as $index => $donation_level ) {
1099
					if ( isset( $donation_level['_give_amount'] ) ) {
1100
						$donation_levels[ $index ]['_give_amount'] = give_sanitize_amount( $donation_level['_give_amount'] );
1101
					}
1102
				}
1103
1104
				update_post_meta( $form_id, '_give_donation_levels', $donation_levels );
1105
1106
				$donation_levels_amounts = wp_list_pluck( $donation_levels, '_give_amount' );
1107
1108
				$min_amount = min( $donation_levels_amounts );
1109
				$max_amount = max( $donation_levels_amounts );
1110
1111
				// Set Minimum and Maximum amount for Multi Level Donation Forms
1112
				give_update_meta( $form_id, '_give_levels_minimum_amount', $min_amount ? give_sanitize_amount( $min_amount ) : 0 );
1113
				give_update_meta( $form_id, '_give_levels_maximum_amount', $max_amount ? give_sanitize_amount( $max_amount ) : 0 );
1114
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1115
1116
		}
1117
1118
		/* Restore original Post Data */
1119
		wp_reset_postdata();
1120
	} else {
1121
		// The Update Ran.
1122
		give_set_upgrade_complete( 'v189_upgrades_levels_post_meta' );
1123
	}
1124
1125
}
1126
1127
1128
/**
1129
 * Give version 1.8.9 upgrades
1130
 *
1131
 * @since      1.8.9
1132
 */
1133
function give_v189_upgrades() {
1134
	/**
1135
	 * 1. Remove user license related notice show blocked ( Give_Notice will handle )
1136
	 */
1137
	global $wpdb;
1138
1139
	// Delete permanent notice blocker.
1140
	$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...
1141
		$wpdb->prepare(
1142
			"
1143
					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...
1144
					WHERE meta_key
1145
					LIKE '%%%s%%'
1146
					",
1147
			'_give_hide_license_notices_permanently'
1148
		)
1149
	);
1150
1151
	// Delete short notice blocker.
1152
	$wpdb->query(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
1153
		$wpdb->prepare(
1154
			"
1155
					DELETE FROM $wpdb->options
1156
					WHERE option_name
1157
					LIKE '%%%s%%'
1158
					",
1159
			'__give_hide_license_notices_shortly_'
1160
		)
1161
	);
1162
}
1163
1164
/**
1165
 * 2.0 Upgrades.
1166
 *
1167
 * @since  2.0
1168
 * @return void
1169
 */
1170
function give_v20_upgrades() {
1171
	// Update cache setting.
1172
	give_update_option( 'cache', 'enabled' );
1173
1174
	// Upgrade email settings.
1175
	give_v20_upgrades_email_setting();
1176
}
1177
1178
/**
1179
 * Move old email api settings to new email setting api for following emails:
1180
 *    1. new offline donation         [This was hard coded]
1181
 *    2. offline donation instruction
1182
 *    3. new donation
1183
 *    4. donation receipt
1184
 *
1185
 * @since 2.0
1186
 */
1187
function give_v20_upgrades_email_setting() {
1188
	$all_setting = give_get_settings();
1189
1190
	// Bailout on fresh install.
1191
	if ( empty( $all_setting ) ) {
1192
		return;
1193
	}
1194
1195
	$settings = array(
1196
		'offline_donation_subject'      => 'offline-donation-instruction_email_subject',
1197
		'global_offline_donation_email' => 'offline-donation-instruction_email_message',
1198
		'donation_subject'              => 'donation-receipt_email_subject',
1199
		'donation_receipt'              => 'donation-receipt_email_message',
1200
		'donation_notification_subject' => 'new-donation_email_subject',
1201
		'donation_notification'         => 'new-donation_email_message',
1202
		'admin_notice_emails'           => array(
1203
			'new-donation_recipient',
1204
			'new-offline-donation_recipient',
1205
			'new-donor-register_recipient',
1206
		),
1207
		'admin_notices'                 => 'new-donation_notification',
1208
	);
1209
1210
	foreach ( $settings as $old_setting => $new_setting ) {
1211
		// Do not update already modified
1212
		if ( ! is_array( $new_setting ) ) {
1213
			if ( array_key_exists( $new_setting, $all_setting ) || ! array_key_exists( $old_setting, $all_setting ) ) {
1214
				continue;
1215
			}
1216
		}
1217
1218
		switch ( $old_setting ) {
1219
			case 'admin_notices':
1220
				$notification_status = give_get_option( $old_setting, 'enabled' );
1221
1222
				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 1210 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...
1223
1224
				// @todo: Delete this option later ( version > 2.0 ), We need this for per form email addon.
1225
				// give_delete_option( $old_setting );
1226
1227
				break;
1228
1229
			// @todo: Delete this option later ( version > 2.0 ) because we need this for backward compatibility give_get_admin_notice_emails.
1230
			case 'admin_notice_emails':
1231
				$recipients = give_get_admin_notice_emails();
1232
1233
				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...
1234
					// bailout if setting already exist.
1235
					if ( array_key_exists( $setting, $all_setting ) ) {
1236
						continue;
1237
					}
1238
1239
					give_update_option( $setting, $recipients );
1240
				}
1241
				break;
1242
1243
			default:
1244
				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 1210 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...
1245
				give_delete_option( $old_setting );
1246
		}
1247
	}
1248
}
1249
1250
/**
1251
 * Give version 1.8.9 upgrades
1252
 *
1253
 * @since 1.8.9
1254
 */
1255
function give_v1812_upgrades() {
1256
	/**
1257
	 * Validate number format settings.
1258
	 */
1259
	$give_settings        = give_get_settings();
1260
	$give_setting_updated = false;
1261
1262
	if ( $give_settings['thousands_separator'] === $give_settings['decimal_separator'] ) {
1263
		$give_settings['number_decimals']   = 0;
1264
		$give_settings['decimal_separator'] = '';
1265
		$give_setting_updated               = true;
1266
1267
	} elseif ( empty( $give_settings['decimal_separator'] ) ) {
1268
		$give_settings['number_decimals'] = 0;
1269
		$give_setting_updated             = true;
1270
1271
	} elseif ( 6 < absint( $give_settings['number_decimals'] ) ) {
1272
		$give_settings['number_decimals'] = 5;
1273
		$give_setting_updated             = true;
1274
	}
1275
1276
	if ( $give_setting_updated ) {
1277
		update_option( 'give_settings', $give_settings, false );
1278
	}
1279
}
1280
1281
1282
/**
1283
 * Give version 1.8.12 update
1284
 *
1285
 * Standardized amount values to six decimal
1286
 *
1287
 * @see        https://github.com/WordImpress/Give/issues/1849#issuecomment-315128602
1288
 *
1289
 * @since      1.8.12
1290
 */
1291
function give_v1812_update_amount_values_callback() {
1292
	/* @var Give_Updates $give_updates */
1293
	$give_updates = Give_Updates::get_instance();
1294
1295
	// form query.
1296
	$donation_forms = new WP_Query( array(
1297
			'paged'          => $give_updates->step,
1298
			'status'         => 'any',
1299
			'order'          => 'ASC',
1300
			'post_type'      => array( 'give_forms', 'give_payment' ),
1301
			'posts_per_page' => 20,
1302
		)
1303
	);
1304
	if ( $donation_forms->have_posts() ) {
1305
		$give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
1306
1307
		while ( $donation_forms->have_posts() ) {
1308
			$donation_forms->the_post();
1309
			global $post;
1310
1311
			$meta = get_post_meta( $post->ID );
1312
1313
			switch ( $post->post_type ) {
1314
				case 'give_forms':
1315
					// _give_set_price.
1316
					if ( ! empty( $meta['_give_set_price'][0] ) ) {
1317
						update_post_meta( $post->ID, '_give_set_price', give_sanitize_amount_for_db( $meta['_give_set_price'][0] ) );
1318
					}
1319
1320
					// _give_custom_amount_minimum.
1321
					if ( ! empty( $meta['_give_custom_amount_minimum'][0] ) ) {
1322
						update_post_meta( $post->ID, '_give_custom_amount_minimum', give_sanitize_amount_for_db( $meta['_give_custom_amount_minimum'][0] ) );
1323
					}
1324
1325
					// _give_levels_minimum_amount.
1326
					if ( ! empty( $meta['_give_levels_minimum_amount'][0] ) ) {
1327
						update_post_meta( $post->ID, '_give_levels_minimum_amount', give_sanitize_amount_for_db( $meta['_give_levels_minimum_amount'][0] ) );
1328
					}
1329
1330
					// _give_levels_maximum_amount.
1331
					if ( ! empty( $meta['_give_levels_maximum_amount'][0] ) ) {
1332
						update_post_meta( $post->ID, '_give_levels_maximum_amount', give_sanitize_amount_for_db( $meta['_give_levels_maximum_amount'][0] ) );
1333
					}
1334
1335
					// _give_set_goal.
1336
					if ( ! empty( $meta['_give_set_goal'][0] ) ) {
1337
						update_post_meta( $post->ID, '_give_set_goal', give_sanitize_amount_for_db( $meta['_give_set_goal'][0] ) );
1338
					}
1339
1340
					// _give_form_earnings.
1341
					if ( ! empty( $meta['_give_form_earnings'][0] ) ) {
1342
						update_post_meta( $post->ID, '_give_form_earnings', give_sanitize_amount_for_db( $meta['_give_form_earnings'][0] ) );
1343
					}
1344
1345
					// _give_custom_amount_minimum.
1346
					if ( ! empty( $meta['_give_donation_levels'][0] ) ) {
1347
						$donation_levels = unserialize( $meta['_give_donation_levels'][0] );
1348
1349
						foreach ( $donation_levels as $index => $level ) {
1350
							if ( empty( $level['_give_amount'] ) ) {
1351
								continue;
1352
							}
1353
1354
							$donation_levels[ $index ]['_give_amount'] = give_sanitize_amount_for_db( $level['_give_amount'] );
1355
						}
1356
1357
						$meta['_give_donation_levels'] = $donation_levels;
1358
						update_post_meta( $post->ID, '_give_donation_levels', $meta['_give_donation_levels'] );
1359
					}
1360
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
1361
1362
					break;
1363
1364
				case 'give_payment':
1365
					// _give_payment_total.
1366
					if ( ! empty( $meta['_give_payment_total'][0] ) ) {
1367
						update_post_meta( $post->ID, '_give_payment_total', give_sanitize_amount_for_db( $meta['_give_payment_total'][0] ) );
1368
					}
1369
1370
					break;
1371
			}
1372
		}
1373
1374
		/* Restore original Post Data */
1375
		wp_reset_postdata();
1376
	} else {
1377
		// The Update Ran.
1378
		give_set_upgrade_complete( 'v1812_update_amount_values' );
1379
	}
1380
}
1381
1382
1383
/**
1384
 * Give version 1.8.12 update
1385
 *
1386
 * Standardized amount values to six decimal for donor
1387
 *
1388
 * @see        https://github.com/WordImpress/Give/issues/1849#issuecomment-315128602
1389
 *
1390
 * @since      1.8.12
1391
 */
1392
function give_v1812_update_donor_purchase_value_callback() {
1393
	/* @var Give_Updates $give_updates */
1394
	$give_updates = Give_Updates::get_instance();
1395
1396
	// form query.
1397
	$donors = Give()->donors->get_donors( array(
1398
			'number' => 20,
1399
			'offset' => $give_updates->get_offset( 20 ),
1400
		)
1401
	);
1402
1403
	if ( ! empty( $donors ) ) {
1404
		$give_updates->set_percentage( Give()->donors->count(), $give_updates->get_offset( 20 ) );
1405
1406
		/* @var Object $donor */
1407
		foreach ( $donors as $donor ) {
1408
			Give()->donors->update( $donor->id, array( 'purchase_value' => give_sanitize_amount_for_db( $donor->purchase_value ) ) );
1409
		}
1410
	} else {
1411
		// The Update Ran.
1412
		give_set_upgrade_complete( 'v1812_update_donor_purchase_values' );
1413
	}
1414
}
1415
1416
/**
1417
 * Upgrade routine for updating user roles for existing donors.
1418
 *
1419
 * @since 1.8.13
1420
 */
1421
function give_v1813_update_donor_user_roles_callback() {
1422
	/* @var Give_Updates $give_updates */
1423
	$give_updates = Give_Updates::get_instance();
1424
1425
	// Fetch all the existing donors.
1426
	$donors = Give()->donors->get_donors( array(
1427
			'number' => 20,
1428
			'offset' => $give_updates->get_offset( 20 ),
1429
		)
1430
	);
1431
1432
	if ( ! empty( $donors ) ) {
1433
		$give_updates->set_percentage( Give()->donors->count(), $give_updates->get_offset( 20 ) );
1434
1435
		/* @var Object $donor */
1436
		foreach ( $donors as $donor ) {
1437
			$user_id = $donor->user_id;
1438
1439
			// Proceed, if donor is attached with user.
1440
			if ( $user_id ) {
1441
				$user = get_userdata( $user_id );
1442
1443
				// Update user role, if user has subscriber role.
1444
				if ( is_array( $user->roles ) && in_array( 'subscriber', $user->roles ) ) {
1445
					wp_update_user(
1446
						array(
1447
							'ID'   => $user_id,
1448
							'role' => 'give_donor',
1449
						)
1450
					);
1451
				}
1452
			}
1453
		}
1454
	} else {
1455
		// The Update Ran.
1456
		give_set_upgrade_complete( 'v1813_update_donor_user_roles' );
1457
	}
1458
}
1459
1460
1461
/**
1462
 * Version 1.8.13 automatic updates
1463
 *
1464
 * @since 1.8.13
1465
 */
1466
function give_v1813_upgrades() {
1467
	// Update admin setting.
1468
	give_update_option( 'donor_default_user_role', 'give_donor' );
1469
1470
	// Update Give roles.
1471
	$roles = new Give_Roles();
1472
	$roles->add_roles();
1473
	$roles->add_caps();
1474
}
1475
1476
/**
1477
 * Correct currency code for "Iranian Currency" for all of the payments.
1478
 *
1479
 * @since 1.8.17
1480
 */
1481
function give_v1817_update_donation_iranian_currency_code() {
1482
	/* @var Give_Updates $give_updates */
1483
	$give_updates = Give_Updates::get_instance();
1484
1485
	// form query.
1486
	$payments = new WP_Query( array(
1487
			'paged'          => $give_updates->step,
1488
			'status'         => 'any',
1489
			'order'          => 'ASC',
1490
			'post_type'      => array( 'give_payment' ),
1491
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1492
		)
1493
	);
1494
1495
	if ( $payments->have_posts() ) {
1496
		$give_updates->set_percentage( $payments->found_posts, ( $give_updates->step * 100 ) );
1497
1498
		while ( $payments->have_posts() ) {
1499
			$payments->the_post();
1500
1501
			$payment_meta = give_get_payment_meta( get_the_ID() );
1502
1503
			if ( 'RIAL' === $payment_meta['currency'] ) {
1504
				$payment_meta['currency'] = 'IRR';
1505
				give_update_meta( get_the_ID(), '_give_payment_meta', $payment_meta );
1506
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1507
1508
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1509
1510
	} else {
1511
		// The Update Ran.
1512
		give_set_upgrade_complete( 'v1817_update_donation_iranian_currency_code' );
1513
	}
1514
}
1515
1516
/**
1517
 * Correct currency code for "Iranian Currency" in Give setting.
1518
 * Version 1.8.17 automatic updates
1519
 *
1520
 * @since 1.8.17
1521
 */
1522
function give_v1817_upgrades() {
1523
	$give_settings = give_get_settings();
1524
1525
	if ( 'RIAL' === $give_settings['currency'] ) {
1526
		$give_settings['currency'] = 'IRR';
1527
		update_option( 'give_settings', $give_settings, false );
1528
	}
1529
}
1530
1531
/**
1532
 * Process Clean up of User Roles for more flexibility.
1533
 *
1534
 * @since 1.8.17
1535
 */
1536
function give_v1817_process_cleanup_user_roles() {
1537
1538
	global $wp_roles;
1539
1540
	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...
1541
		return;
1542
	}
1543
1544
	// Add Capabilities to user roles as required.
1545
	$add_caps = array(
1546
		'administrator' => array(
1547
			'view_give_payments',
1548
		),
1549
	);
1550
1551
	// Remove Capabilities to user roles as required.
1552
	$remove_caps = array(
1553
		'give_manager' => array(
1554
			'edit_others_pages',
1555
			'edit_others_posts',
1556
			'delete_others_pages',
1557
			'delete_others_posts',
1558
			'manage_categories',
1559
			'import',
1560
			'export',
1561
		),
1562
	);
1563
1564
	foreach ( $add_caps as $role => $caps ) {
1565
		foreach ( $caps as $cap ) {
1566
			$wp_roles->add_cap( $role, $cap );
1567
		}
1568
	}
1569
1570
	foreach ( $remove_caps as $role => $caps ) {
1571
		foreach ( $caps as $cap ) {
1572
			$wp_roles->remove_cap( $role, $cap );
1573
		}
1574
	}
1575
1576
}
1577
1578
/**
1579
 * Upgrade Routine - Clean up of User Roles for more flexibility.
1580
 *
1581
 * @since 1.8.17
1582
 */
1583
function give_v1817_cleanup_user_roles() {
1584
	/* @var Give_Updates $give_updates */
1585
	$give_updates = Give_Updates::get_instance();
1586
1587
	give_v1817_process_cleanup_user_roles();
1588
1589
	$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...
1590
1591
	// Create Give plugin roles.
1592
	$roles = new Give_Roles();
1593
	$roles->add_roles();
1594
	$roles->add_caps();
1595
1596
	give_set_upgrade_complete( 'v1817_cleanup_user_roles' );
1597
}
1598
1599
/**
1600
 * Automatic Upgrade for release 1.8.18.
1601
 *
1602
 * @since 1.8.18
1603
 */
1604
function give_v1818_upgrades() {
1605
1606
	// Remove email_access_installed from give_settings.
1607
	give_delete_option( 'email_access_installed' );
1608
}
1609
1610
/**
1611
 * Upgrade Routine - Assigns Custom Amount to existing donation of type set donation.
1612
 *
1613
 * @since 1.8.18
1614
 */
1615
function give_v1818_assign_custom_amount_set_donation() {
1616
1617
	/* @var Give_Updates $give_updates */
1618
	$give_updates = Give_Updates::get_instance();
1619
1620
	$donations = new WP_Query( array(
1621
			'paged'          => $give_updates->step,
1622
			'status'         => 'any',
1623
			'order'          => 'ASC',
1624
			'post_type'      => array( 'give_payment' ),
1625
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1626
		)
1627
	);
1628
1629
	if ( $donations->have_posts() ) {
1630
		$give_updates->set_percentage( $donations->found_posts, $give_updates->step * 100 );
1631
1632
		while ( $donations->have_posts() ) {
1633
			$donations->the_post();
1634
1635
			$form          = new Give_Donate_Form( give_get_meta( get_the_ID(), '_give_payment_form_id', true ) );
1636
			$donation_meta = give_get_payment_meta( get_the_ID() );
1637
1638
			// Update Donation meta with price_id set as custom, only if it is:
1639
			// 1. Donation Type = Set Donation.
1640
			// 2. Donation Price Id is not set to custom.
1641
			// 3. Form has not enabled custom price and donation amount assures that it is custom amount.
1642
			if (
1643
				$form->ID &&
1644
				$form->is_set_type_donation_form() &&
1645
				( 'custom' !== $donation_meta['price_id'] ) &&
1646
				$form->is_custom_price( give_get_meta( get_the_ID(), '_give_payment_total', true ) )
1647
			) {
1648
				$donation_meta['price_id'] = 'custom';
1649
				give_update_meta( get_the_ID(), '_give_payment_meta', $donation_meta );
1650
				give_update_meta( get_the_ID(), '_give_payment_price_id', 'custom' );
1651
			}
1652
		}
1653
1654
		wp_reset_postdata();
1655
	} else {
1656
		// Update Ran Successfully.
1657
		give_set_upgrade_complete( 'v1818_assign_custom_amount_set_donation' );
1658
	}
1659
}
1660
1661
/**
1662
 * Upgrade Routine - Removed Give Worker caps.
1663
 *
1664
 * See: https://github.com/WordImpress/Give/issues/2476
1665
 *
1666
 * @since 1.8.18
1667
 */
1668
function give_v1818_give_worker_role_cleanup() {
1669
1670
	/* @var Give_Updates $give_updates */
1671
	$give_updates = Give_Updates::get_instance();
1672
1673
	global $wp_roles;
1674
1675
	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...
1676
		return;
1677
	}
1678
1679
	// Remove Capabilities to user roles as required.
1680
	$remove_caps = array(
1681
		'give_worker' => array(
1682
			'delete_give_payments',
1683
			'delete_others_give_payments',
1684
			'delete_private_give_payments',
1685
			'delete_published_give_payments',
1686
			'edit_others_give_payments',
1687
			'edit_private_give_payments',
1688
			'edit_published_give_payments',
1689
			'read_private_give_payments',
1690
		),
1691
	);
1692
1693
	foreach ( $remove_caps as $role => $caps ) {
1694
		foreach ( $caps as $cap ) {
1695
			$wp_roles->remove_cap( $role, $cap );
1696
		}
1697
	}
1698
1699
	$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...
1700
1701
	// Create Give plugin roles.
1702
	$roles = new Give_Roles();
1703
	$roles->add_roles();
1704
	$roles->add_caps();
1705
1706
	give_set_upgrade_complete( 'v1818_give_worker_role_cleanup' );
1707
}
1708
1709
/**
1710
 *
1711
 * Upgrade form metadata for new metabox settings.
1712
 *
1713
 * @since  2.0
1714
 * @return void
1715
 */
1716
function give_v20_upgrades_form_metadata_callback() {
1717
	$give_updates = Give_Updates::get_instance();
1718
1719
	// form query
1720
	$forms = new WP_Query( array(
1721
			'paged'          => $give_updates->step,
1722
			'status'         => 'any',
1723
			'order'          => 'ASC',
1724
			'post_type'      => 'give_forms',
1725
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1726
		)
1727
	);
1728
1729
	if ( $forms->have_posts() ) {
1730
		$give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 100 ) );
1731
1732
		while ( $forms->have_posts() ) {
1733
			$forms->the_post();
1734
			global $post;
1735
1736
			// Update offline instruction email notification status.
1737
			$offline_instruction_notification_status = get_post_meta( get_the_ID(), '_give_customize_offline_donations', true );
1738
			$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...
1739
				'enabled',
1740
				'global',
1741
			) )
1742
				? $offline_instruction_notification_status
1743
				: 'global';
1744
			update_post_meta( get_the_ID(), '_give_offline-donation-instruction_notification', $offline_instruction_notification_status );
1745
1746
			// Update offline instruction email message.
1747
			update_post_meta(
1748
				get_the_ID(),
1749
				'_give_offline-donation-instruction_email_message',
1750
				get_post_meta(
1751
					get_the_ID(),
1752
					// @todo: Delete this option later ( version > 2.0 ).
1753
					'_give_offline_donation_email',
1754
					true
1755
				)
1756
			);
1757
1758
			// Update offline instruction email subject.
1759
			update_post_meta(
1760
				get_the_ID(),
1761
				'_give_offline-donation-instruction_email_subject',
1762
				get_post_meta(
1763
					get_the_ID(),
1764
					// @todo: Delete this option later ( version > 2.0 ).
1765
					'_give_offline_donation_subject',
1766
					true
1767
				)
1768
			);
1769
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
1770
1771
		}// End while().
1772
1773
		wp_reset_postdata();
1774
	} else {
1775
		// No more forms found, finish up.
1776
		give_set_upgrade_complete( 'v20_upgrades_form_metadata' );
1777
	}
1778
}
1779
1780
1781
/**
1782
 * Upgrade payment metadata for new metabox settings.
1783
 *
1784
 * @since  2.0
1785
 * @global wpdb $wpdb
1786
 * @return void
1787
 */
1788
function give_v20_upgrades_payment_metadata_callback() {
1789
	global $wpdb;
1790
	$give_updates = Give_Updates::get_instance();
1791
1792
	// form query
1793
	$forms = new WP_Query( array(
1794
			'paged'          => $give_updates->step,
1795
			'status'         => 'any',
1796
			'order'          => 'ASC',
1797
			'post_type'      => 'give_payment',
1798
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1799
		)
1800
	);
1801
1802
	if ( $forms->have_posts() ) {
1803
		$give_updates->set_percentage( $forms->found_posts, ( $give_updates->step * 100 ) );
1804
1805
		while ( $forms->have_posts() ) {
1806
			$forms->the_post();
1807
			global $post;
1808
1809
			// Split _give_payment_meta meta.
1810
			// @todo Remove _give_payment_meta after releases 2.0
1811
			$payment_meta = give_get_meta( $post->ID, '_give_payment_meta', true );
1812
1813
			if ( ! empty( $payment_meta ) ) {
1814
				_give_20_bc_split_and_save_give_payment_meta( $post->ID, $payment_meta );
1815
			}
1816
1817
			$deprecated_meta_keys = array(
1818
				'_give_payment_customer_id' => '_give_payment_donor_id',
1819
				'_give_payment_user_email'  => '_give_payment_donor_email',
1820
				'_give_payment_user_ip'     => '_give_payment_donor_ip',
1821
			);
1822
1823 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...
1824
				// Do not add new meta key if already exist.
1825
				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...
1826
					continue;
1827
				}
1828
1829
				$wpdb->insert(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
1830
					$wpdb->postmeta,
1831
					array(
1832
						'post_id'    => $post->ID,
1833
						'meta_key'   => $new_meta_key,
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
1834
						'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...
1835
					)
1836
				);
1837
			}
1838
1839
			// Bailout
1840 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...
1841
				/* @var Give_Donor $donor */
1842
				$donor = new Give_Donor( $donor_id );
1843
1844
				$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...
1845
				$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...
1846
				$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...
1847
				$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...
1848
				$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...
1849
				$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...
1850
1851
				// Save address.
1852
				$donor->add_address( 'billing[]', $address );
1853
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
1854
1855
		}// End while().
1856
1857
		wp_reset_postdata();
1858
	} else {
1859
		// @todo Delete user id meta after releases 2.0
1860
		// $wpdb->get_var( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key=%s", '_give_payment_user_id' ) );
1861
1862
		// No more forms found, finish up.
1863
		give_set_upgrade_complete( 'v20_upgrades_payment_metadata' );
1864
	}
1865
}
1866
1867
1868
/**
1869
 * Upgrade logs data.
1870
 *
1871
 * @since  2.0
1872
 * @return void
1873
 */
1874
function give_v20_logs_upgrades_callback() {
1875
	global $wpdb;
1876
	$give_updates = Give_Updates::get_instance();
1877
1878
	// form query
1879
	$forms = new WP_Query( array(
1880
			'paged'          => $give_updates->step,
1881
			'order'          => 'DESC',
1882
			'post_type'      => 'give_log',
1883
			'post_status'    => 'any',
1884
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
1885
		)
1886
	);
1887
1888
	if ( $forms->have_posts() ) {
1889
		$give_updates->set_percentage( $forms->found_posts, $give_updates->step * 100 );
1890
1891
		while ( $forms->have_posts() ) {
1892
			$forms->the_post();
1893
			global $post;
1894
1895
			if ( $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}give_logs WHERE ID=%d", $post->ID ) ) ) {
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
1896
				continue;
1897
			}
1898
1899
			$term      = get_the_terms( $post->ID, 'give_log_type' );
1900
			$term      = ! is_wp_error( $term ) && ! empty( $term ) ? $term[0] : array();
1901
			$term_name = ! empty( $term ) ? $term->slug : '';
1902
1903
			$log_data = array(
1904
				'ID'           => $post->ID,
1905
				'log_title'    => $post->post_title,
1906
				'log_content'  => $post->post_content,
1907
				'log_parent'   => 0,
1908
				'log_type'     => $term_name,
1909
				'log_date'     => $post->post_date,
1910
				'log_date_gmt' => $post->post_date_gmt,
1911
			);
1912
			$log_meta = array();
1913
1914 View Code Duplication
			if ( $old_log_meta = get_post_meta( $post->ID ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
1915
				foreach ( $old_log_meta as $meta_key => $meta_value ) {
1916
					switch ( $meta_key ) {
1917
						case '_give_log_payment_id':
1918
							$log_data['log_parent']        = current( $meta_value );
1919
							$log_meta['_give_log_form_id'] = $post->post_parent;
1920
							break;
1921
1922
						default:
1923
							$log_meta[ $meta_key ] = current( $meta_value );
1924
					}
1925
				}
1926
			}
1927
1928
			if ( 'api_request' === $term_name ) {
1929
				$log_meta['_give_log_api_query'] = $post->post_excerpt;
1930
			}
1931
1932
			$wpdb->insert( "{$wpdb->prefix}give_logs", $log_data );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
1933
1934 View Code Duplication
			if ( ! empty( $log_meta ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
1935
				foreach ( $log_meta as $meta_key => $meta_value ) {
1936
					Give()->logs->logmeta_db->update_meta( $post->ID, $meta_key, $meta_value );
1937
				}
1938
			}
1939
1940
			$logIDs[] = $post->ID;
1941
		}// End while().
1942
1943
		wp_reset_postdata();
1944
	} else {
1945
		// @todo: Delete terms and taxonomy after releases 2.0.
1946
		/*$terms = get_terms( 'give_log_type', array( 'fields' => 'ids', 'hide_empty' => false ) );
1947
		if ( ! empty( $terms ) ) {
1948
			foreach ( $terms as $term ) {
1949
				wp_delete_term( $term, 'give_log_type' );
1950
			}
1951
		}*/
1952
1953
		// @todo: Delete logs after releases 2.0.
1954
		/*$logIDs = get_posts( array(
1955
				'order'          => 'DESC',
1956
				'post_type'      => 'give_log',
1957
				'post_status'    => 'any',
1958
				'posts_per_page' => - 1,
1959
				'fields'         => 'ids',
1960
			)
1961
		);*/
1962
1963
		/*if ( ! empty( $logIDs ) ) {
1964
			foreach ( $logIDs as $log ) {
1965
				// Delete term relationship and posts.
1966
				wp_delete_object_term_relationships( $log, 'give_log_type' );
1967
				wp_delete_post( $log, true );
1968
			}
1969
		}*/
1970
1971
		// @todo: Unregister taxonomy after releases 2.0.
1972
		/*unregister_taxonomy( 'give_log_type' );*/
1973
1974
		// Delete log cache.
1975
		Give()->logs->delete_cache();
1976
1977
		// No more forms found, finish up.
1978
		give_set_upgrade_complete( 'v20_logs_upgrades' );
1979
	}
1980
}
1981
1982
1983
/**
1984
 * Move payment and form metadata to new table
1985
 *
1986
 * @since  2.0
1987
 * @return void
1988
 */
1989
function give_v20_move_metadata_into_new_table_callback() {
1990
	global $wpdb;
1991
	$give_updates = Give_Updates::get_instance();
1992
1993
	// form query
1994
	$payments = new WP_Query( array(
1995
			'paged'          => $give_updates->step,
1996
			'status'         => 'any',
1997
			'order'          => 'ASC',
1998
			'post_type'      => array( 'give_forms', 'give_payment' ),
1999
			'posts_per_page' => 100,
0 ignored issues
show
introduced by
Detected high pagination limit, posts_per_page is set to 100
Loading history...
2000
		)
2001
	);
2002
2003
	if ( $payments->have_posts() ) {
2004
		$give_updates->set_percentage( $payments->found_posts, $give_updates->step * 100 );
2005
2006
		while ( $payments->have_posts() ) {
2007
			$payments->the_post();
2008
			global $post;
2009
2010
			$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...
2011
				$wpdb->prepare(
2012
					"SELECT * FROM $wpdb->postmeta where post_id=%d",
2013
					get_the_ID()
2014
				),
2015
				ARRAY_A
2016
			);
2017
2018 View Code Duplication
			if ( ! empty( $meta_data ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2019
				foreach ( $meta_data as $index => $data ) {
2020
					// Check for duplicate meta values.
2021
					if ( $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM " . ( 'give_forms' === $post->post_type ? $wpdb->formmeta : $wpdb->paymentmeta ) . " WHERE meta_id=%d", $data['meta_id'] ), ARRAY_A ) ) {
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

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

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

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

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

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

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

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

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

<?php

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

print $doubleQuoted;

will print an indented: Single is Value

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

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

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

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

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

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

<?php

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

print $doubleQuoted;

will print an indented: Single is Value

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

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

Loading history...
2022
						continue;
2023
					}
2024
2025
					switch ( $post->post_type ) {
2026
						case 'give_forms':
2027
							$data['form_id'] = $data['post_id'];
2028
							unset( $data['post_id'] );
2029
2030
							Give()->form_meta->insert( $data );
2031
							// @todo: delete form meta from post meta table after releases 2.0.
2032
							/*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2033
2034
							break;
2035
2036
						case 'give_payment':
2037
							$data['payment_id'] = $data['post_id'];
2038
							unset( $data['post_id'] );
2039
2040
							Give()->payment_meta->insert( $data );
2041
2042
							// @todo: delete donation meta from post meta table after releases 2.0.
2043
							/*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2044
2045
							break;
2046
					}
2047
				}
2048
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2049
2050
		}// End while().
2051
2052
		wp_reset_postdata();
2053
	} else {
2054
		// No more forms found, finish up.
2055
		give_set_upgrade_complete( 'v20_move_metadata_into_new_table' );
2056
	}
2057
2058
}
2059
2060
/**
2061
 * Upgrade routine for splitting donor name into first name and last name.
2062
 *
2063
 * @since 2.0
2064
 *
2065
 * @return void
2066
 */
2067
function give_v20_upgrades_donor_name() {
2068
	/* @var Give_Updates $give_updates */
2069
	$give_updates = Give_Updates::get_instance();
2070
2071
	$donors = Give()->donors->get_donors( array(
2072
		'paged'  => $give_updates->step,
2073
		'number' => 100,
2074
	) );
2075
2076
	if ( $donors ) {
2077
		$give_updates->set_percentage( count( $donors ), $give_updates->step * 100 );
2078
		// Loop through Donors
2079
		foreach ( $donors as $donor ) {
2080
2081
			$donor_name       = explode( ' ', $donor->name, 2 );
2082
			$donor_first_name = Give()->donor_meta->get_meta( $donor->id, '_give_donor_first_name' );
2083
			$donor_last_name  = Give()->donor_meta->get_meta( $donor->id, '_give_donor_last_name' );
2084
2085
			// If first name meta of donor is not created, then create it.
2086 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...
2087
				Give()->donor_meta->add_meta( $donor->id, '_give_donor_first_name', $donor_name[0] );
2088
			}
2089
2090
			// If last name meta of donor is not created, then create it.
2091 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...
2092
				Give()->donor_meta->add_meta( $donor->id, '_give_donor_last_name', $donor_name[1] );
2093
			}
2094
2095
			// If Donor is connected with WP User then update user meta.
2096 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...
2097
				if ( isset( $donor_name[0] ) ) {
2098
					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...
2099
				}
2100
				if ( isset( $donor_name[1] ) ) {
2101
					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...
2102
				}
2103
			}
2104
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2105
2106
	} else {
2107
		// The Update Ran.
2108
		give_set_upgrade_complete( 'v20_upgrades_donor_name' );
2109
	}
2110
2111
}
2112
2113
/**
2114
 * Upgrade routine for user addresses.
2115
 *
2116
 * @since 2.0
2117
 * @global wpdb $wpdb
2118
 *
2119
 * @return void
2120
 */
2121
function give_v20_upgrades_user_address() {
2122
	global $wpdb;
2123
2124
	/* @var Give_Updates $give_updates */
2125
	$give_updates = Give_Updates::get_instance();
2126
2127
	/* @var WP_User_Query $user_query */
2128
	$user_query = new WP_User_Query(
2129
		array(
2130
			'number' => 100,
2131
			'paged'  => $give_updates->step,
2132
		)
2133
	);
2134
2135
	$users = $user_query->get_results();
2136
2137
	if ( $users ) {
2138
		$give_updates->set_percentage( $user_query->get_total(), $give_updates->step * 100 );
2139
2140
		// Loop through Donors
2141
		foreach ( $users as $user ) {
2142
			/* @var Give_Donor $donor */
2143
			$donor = new Give_Donor( $user->ID, true );
2144
2145
			if ( ! $donor->id ) {
2146
				continue;
2147
			}
2148
2149
			$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...
2150
				$wpdb->prepare(
2151
					"
2152
					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...
2153
					WHERE user_id=%s
2154
					AND meta_key=%s
2155
					",
2156
					$user->ID,
2157
					'_give_user_address'
2158
				)
2159
			);
2160
2161 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...
2162
				$address = maybe_unserialize( $address );
2163
				$donor->add_address( 'personal', $address );
2164
				$donor->add_address( 'billing[]', $address );
2165
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
2166
2167
				// @todo: delete _give_user_address from user meta after releases 2.0.
2168
				/*delete_user_meta( $user->ID, '_give_user_address' );*/
2169
			}
2170
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2171
2172
	} else {
2173
		// The Update Ran.
2174
		give_set_upgrade_complete( 'v20_upgrades_user_address' );
2175
	}
2176
2177
}
2178
2179
/**
2180
 * Upgrade logs data.
2181
 *
2182
 * @since  2.0
2183
 * @global wpdb $wpdb
2184
 * @return void
2185
 */
2186
function give_v20_rename_donor_tables_callback() {
2187
	global $wpdb;
2188
2189
	/* @var Give_Updates $give_updates */
2190
	$give_updates = Give_Updates::get_instance();
2191
2192
	$tables = array(
2193
		"{$wpdb->prefix}give_customers"    => "{$wpdb->prefix}give_donors",
2194
		"{$wpdb->prefix}give_customermeta" => "{$wpdb->prefix}give_donormeta",
2195
	);
2196
2197
	// Alter customer table
2198
	foreach ( $tables as $old_table => $new_table ) {
2199
		if (
2200
			$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...
2201
			! $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...
2202
		) {
2203
			$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...
2204
2205
			if ( "{$wpdb->prefix}give_donormeta" === $new_table ) {
2206
				$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...
2207
			}
2208
		}
2209
	}
2210
2211
	$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...
2212
2213
	// No more forms found, finish up.
2214
	give_set_upgrade_complete( 'v20_rename_donor_tables' );
2215
2216
	// Re initiate donor classes.
2217
	Give()->donors     = new Give_DB_Donors();
2218
	Give()->donor_meta = new Give_DB_Donor_Meta();
2219
}
2220
2221
2222
/**
2223
 * Create missing meta tables.
2224
 *
2225
 * @since  2.0.1
2226
 * @global wpdb $wpdb
2227
 * @return void
2228
 */
2229
function give_v201_create_tables() {
2230
	global $wpdb;
2231
2232
	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...
2233
		Give()->payment_meta->create_table();
2234
	}
2235
2236
	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...
2237
		Give()->form_meta->create_table();
2238
	}
2239
2240
	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...
2241
		Give()->logs->log_db->create_table();
2242
	}
2243
2244
	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...
2245
		Give()->logs->logmeta_db->create_table();
2246
	}
2247
}
2248
2249
/**
2250
 * Upgrade payment metadata for new metabox settings.
2251
 *
2252
 * @since  2.0.1
2253
 * @global wpdb $wpdb
2254
 * @return void
2255
 */
2256
function give_v201_upgrades_payment_metadata_callback() {
2257
	global $wpdb, $post;
2258
	$give_updates = Give_Updates::get_instance();
2259
	give_v201_create_tables();
2260
2261
	$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...
2262
		"
2263
			SELECT ID FROM $wpdb->posts
2264
			WHERE 1=1
2265
			AND ( 
2266
  				$wpdb->posts.post_date >= '2018-01-08 00:00:00'
2267
			)
2268
			AND $wpdb->posts.post_type = 'give_payment'
2269
			AND {$wpdb->posts}.post_status IN ('" . implode( "','", array_keys( give_get_payment_statuses() ) ) . "')
2270
			ORDER BY $wpdb->posts.post_date ASC 
2271
			LIMIT 100
2272
			OFFSET " . $give_updates->get_offset( 100 )
2273
	);
2274
2275
	if ( ! empty( $payments ) ) {
2276
		$give_updates->set_percentage( give_get_total_post_type_count( 'give_payment' ), ( $give_updates->step * 100 ) );
2277
2278
		foreach ( $payments as $payment_id ) {
2279
			$post = get_post( $payment_id );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
2280
			setup_postdata( $post );
2281
2282
			// Do not add new meta keys if already refactored.
2283
			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...
2284
				continue;
2285
			}
2286
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
2287
2288
			// Split _give_payment_meta meta.
2289
			// @todo Remove _give_payment_meta after releases 2.0
2290
			$payment_meta = give_get_meta( $post->ID, '_give_payment_meta', true );
2291
2292
			if ( ! empty( $payment_meta ) ) {
2293
				_give_20_bc_split_and_save_give_payment_meta( $post->ID, $payment_meta );
2294
			}
2295
2296
			$deprecated_meta_keys = array(
2297
				'_give_payment_customer_id' => '_give_payment_donor_id',
2298
				'_give_payment_user_email'  => '_give_payment_donor_email',
2299
				'_give_payment_user_ip'     => '_give_payment_donor_ip',
2300
			);
2301
2302 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...
2303
				// Do not add new meta key if already exist.
2304
				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...
2305
					continue;
2306
				}
2307
2308
				$wpdb->insert(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
2309
					$wpdb->postmeta,
2310
					array(
2311
						'post_id'    => $post->ID,
2312
						'meta_key'   => $new_meta_key,
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
2313
						'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...
2314
					)
2315
				);
2316
			}
2317
2318
			// Bailout
2319 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...
2320
				/* @var Give_Donor $donor */
2321
				$donor = new Give_Donor( $donor_id );
2322
2323
				$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...
2324
				$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...
2325
				$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...
2326
				$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...
2327
				$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...
2328
				$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...
2329
2330
				// Save address.
2331
				$donor->add_address( 'billing[]', $address );
2332
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2333
2334
		}// End while().
2335
2336
		wp_reset_postdata();
2337
	} else {
2338
		// @todo Delete user id meta after releases 2.0
2339
		// $wpdb->get_var( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key=%s", '_give_payment_user_id' ) );
2340
2341
		// No more forms found, finish up.
2342
		give_set_upgrade_complete( 'v201_upgrades_payment_metadata' );
2343
	}
2344
}
2345
2346
/**
2347
 * Move payment and form metadata to new table
2348
 *
2349
 * @since  2.0.1
2350
 * @return void
2351
 */
2352
function give_v201_move_metadata_into_new_table_callback() {
2353
	global $wpdb, $post;
2354
	$give_updates = Give_Updates::get_instance();
2355
	give_v201_create_tables();
2356
2357
	$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...
2358
		"
2359
			SELECT ID FROM $wpdb->posts 
2360
			WHERE 1=1
2361
			AND ( $wpdb->posts.post_type = 'give_payment' OR $wpdb->posts.post_type = 'give_forms' )
2362
			AND {$wpdb->posts}.post_status IN ('" . implode( "','", array_keys( give_get_payment_statuses() ) ) . "')
2363
			ORDER BY $wpdb->posts.post_date ASC 
2364
			LIMIT 100
2365
			OFFSET " . $give_updates->get_offset( 100 )
2366
	);
2367
2368
	if ( ! empty( $payments ) ) {
2369
		$give_updates->set_percentage( give_get_total_post_type_count( array(
0 ignored issues
show
Documentation introduced by
array('give_forms', 'give_payment') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

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

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

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

function acceptsInteger($int) { }

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

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
2370
			'give_forms',
2371
			'give_payment',
2372
		) ), $give_updates->step * 100 );
2373
2374
		foreach ( $payments as $payment_id ) {
2375
			$post = get_post( $payment_id );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
2376
			setup_postdata( $post );
2377
2378
			$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...
2379
				$wpdb->prepare(
2380
					"SELECT * FROM $wpdb->postmeta where post_id=%d",
2381
					get_the_ID()
2382
				),
2383
				ARRAY_A
2384
			);
2385
2386 View Code Duplication
			if ( ! empty( $meta_data ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2387
				foreach ( $meta_data as $index => $data ) {
2388
					// Check for duplicate meta values.
2389
					if ( $result = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM " . ( 'give_forms' === $post->post_type ? $wpdb->formmeta : $wpdb->paymentmeta ) . " WHERE meta_id=%d", $data['meta_id'] ), ARRAY_A ) ) {
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

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

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

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

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

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

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

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

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

<?php

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

print $doubleQuoted;

will print an indented: Single is Value

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

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

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

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

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

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

<?php

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

print $doubleQuoted;

will print an indented: Single is Value

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

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

Loading history...
2390
						continue;
2391
					}
2392
2393
					switch ( $post->post_type ) {
2394
						case 'give_forms':
2395
							$data['form_id'] = $data['post_id'];
2396
							unset( $data['post_id'] );
2397
2398
							Give()->form_meta->insert( $data );
2399
							// @todo: delete form meta from post meta table after releases 2.0.
2400
							/*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2401
2402
							break;
2403
2404
						case 'give_payment':
2405
							$data['payment_id'] = $data['post_id'];
2406
							unset( $data['post_id'] );
2407
2408
							Give()->payment_meta->insert( $data );
2409
2410
							// @todo: delete donation meta from post meta table after releases 2.0.
2411
							/*delete_post_meta( get_the_ID(), $data['meta_key'] );*/
2412
2413
							break;
2414
					}
2415
				}
2416
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
2417
2418
		}// End while().
2419
2420
		wp_reset_postdata();
2421
	} else {
2422
		// No more forms found, finish up.
2423
		give_set_upgrade_complete( 'v201_move_metadata_into_new_table' );
2424
	}
2425
2426
}
2427
2428
/**
2429
 * Move data to new log table.
2430
 *
2431
 * @since  2.0.1
2432
 * @return void
2433
 */
2434
function give_v201_logs_upgrades_callback() {
2435
	global $wpdb, $post;
2436
	$give_updates = Give_Updates::get_instance();
2437
	give_v201_create_tables();
2438
2439
	$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...
2440
		"
2441
			SELECT ID FROM $wpdb->posts 
2442
			WHERE 1=1
2443
			AND $wpdb->posts.post_type = 'give_log'
2444
			AND {$wpdb->posts}.post_status IN ('" . implode( "','", array_keys( give_get_payment_statuses() ) ) . "')
2445
			ORDER BY $wpdb->posts.post_date ASC 
2446
			LIMIT 100
2447
			OFFSET " . $give_updates->get_offset( 100 )
2448
	);
2449
2450
	if ( ! empty( $logs ) ) {
2451
		$give_updates->set_percentage( give_get_total_post_type_count( 'give_log' ), $give_updates->step * 100 );
2452
2453
		foreach ( $logs as $log_id ) {
2454
			$post = get_post( $log_id );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
2455
			setup_postdata( $post );
2456
2457
			if ( $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}give_logs WHERE ID=%d", $post->ID ) ) ) {
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
2458
				continue;
2459
			}
2460
2461
			$term      = get_the_terms( $post->ID, 'give_log_type' );
2462
			$term      = ! is_wp_error( $term ) && ! empty( $term ) ? $term[0] : array();
2463
			$term_name = ! empty( $term ) ? $term->slug : '';
2464
2465
			$log_data = array(
2466
				'ID'           => $post->ID,
2467
				'log_title'    => $post->post_title,
2468
				'log_content'  => $post->post_content,
2469
				'log_parent'   => 0,
2470
				'log_type'     => $term_name,
2471
				'log_date'     => $post->post_date,
2472
				'log_date_gmt' => $post->post_date_gmt,
2473
			);
2474
			$log_meta = array();
2475
2476 View Code Duplication
			if ( $old_log_meta = get_post_meta( $post->ID ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2477
				foreach ( $old_log_meta as $meta_key => $meta_value ) {
2478
					switch ( $meta_key ) {
2479
						case '_give_log_payment_id':
2480
							$log_data['log_parent']        = current( $meta_value );
2481
							$log_meta['_give_log_form_id'] = $post->post_parent;
2482
							break;
2483
2484
						default:
2485
							$log_meta[ $meta_key ] = current( $meta_value );
2486
					}
2487
				}
2488
			}
2489
2490
			if ( 'api_request' === $term_name ) {
2491
				$log_meta['_give_log_api_query'] = $post->post_excerpt;
2492
			}
2493
2494
			$wpdb->insert( "{$wpdb->prefix}give_logs", $log_data );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
2495
2496 View Code Duplication
			if ( ! empty( $log_meta ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
2497
				foreach ( $log_meta as $meta_key => $meta_value ) {
2498
					Give()->logs->logmeta_db->update_meta( $post->ID, $meta_key, $meta_value );
2499
				}
2500
			}
2501
2502
			$logIDs[] = $post->ID;
2503
		}// End while().
2504
2505
		wp_reset_postdata();
2506
	} else {
2507
		// Delete log cache.
2508
		Give()->logs->delete_cache();
2509
2510
		// No more forms found, finish up.
2511
		give_set_upgrade_complete( 'v201_logs_upgrades' );
2512
	}
2513
}
2514
2515
2516
/**
2517
 * Add missing donor.
2518
 *
2519
 * @since  2.0.1
2520
 * @return void
2521
 */
2522
function give_v201_add_missing_donors_callback() {
2523
	global $wpdb;
2524
	give_v201_create_tables();
2525
2526
	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...
2527
		$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...
2528
		$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...
2529
		$donor_data = array();
2530
2531
		if ( $missing_donors = array_diff( $customers, $donors ) ) {
2532
			foreach ( $missing_donors as $donor_id ) {
2533
				$donor_data[] = array(
2534
					'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...
2535
					'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...
2536
2537
				);
2538
			}
2539
		}
2540
2541
		if ( ! empty( $donor_data ) ) {
2542
			$donor_table_name      = Give()->donors->table_name;
2543
			$donor_meta_table_name = Give()->donor_meta->table_name;
2544
2545
			Give()->donors->table_name     = "{$wpdb->prefix}give_donors";
2546
			Give()->donor_meta->table_name = "{$wpdb->prefix}give_donormeta";
2547
2548
			foreach ( $donor_data as $donor ) {
2549
				$donor['info'][0] = (array) $donor['info'][0];
2550
2551
				// Prevent duplicate meta id issue.
2552
				if ( $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}give_donors WHERE id=%d", $donor['info'][0]['id'] ) ) ) {
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
2553
					continue;
2554
				}
2555
2556
				$donor_id = Give()->donors->add( $donor['info'][0] );
2557
2558
				if ( ! empty( $donor['meta'] ) ) {
2559
					foreach ( $donor['meta'] as $donor_meta ) {
2560
						$donor_meta = (array) $donor_meta;
2561
2562
						// Prevent duplicate meta id issue.
2563
						if ( $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}give_donormeta WHERE meta_id=%d", $donor_meta['meta_id'] ) ) ) {
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
2564
							unset( $donor_meta['meta_id'] );
2565
						}
2566
2567
						$donor_meta['donor_id'] = $donor_meta['customer_id'];
2568
						unset( $donor_meta['customer_id'] );
2569
2570
						Give()->donor_meta->insert( $donor_meta );
2571
					}
2572
				}
2573
2574
				/**
2575
				 * Fix donor name and address
2576
				 */
2577
				$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...
2578
					$wpdb->prepare(
2579
						"
2580
					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...
2581
					WHERE user_id=%s
2582
					AND meta_key=%s
2583
					",
2584
						$donor['info'][0]['user_id'],
2585
						'_give_user_address'
2586
					)
2587
				);
2588
2589
				$donor = new Give_Donor( $donor_id );
2590
2591 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...
2592
					$address = maybe_unserialize( $address );
2593
					$donor->add_address( 'personal', $address );
2594
					$donor->add_address( 'billing[]', $address );
2595
				}
2596
2597
				$donor_name       = explode( ' ', $donor->name, 2 );
2598
				$donor_first_name = Give()->donor_meta->get_meta( $donor->id, '_give_donor_first_name' );
2599
				$donor_last_name  = Give()->donor_meta->get_meta( $donor->id, '_give_donor_last_name' );
2600
2601
				// If first name meta of donor is not created, then create it.
2602 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...
2603
					Give()->donor_meta->add_meta( $donor->id, '_give_donor_first_name', $donor_name[0] );
2604
				}
2605
2606
				// If last name meta of donor is not created, then create it.
2607 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...
2608
					Give()->donor_meta->add_meta( $donor->id, '_give_donor_last_name', $donor_name[1] );
2609
				}
2610
2611
				// If Donor is connected with WP User then update user meta.
2612 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...
2613
					if ( isset( $donor_name[0] ) ) {
2614
						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...
2615
					}
2616
					if ( isset( $donor_name[1] ) ) {
2617
						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...
2618
					}
2619
				}
2620
			}
2621
2622
			Give()->donors->table_name     = $donor_table_name;
2623
			Give()->donor_meta->table_name = $donor_meta_table_name;
2624
		}
2625
	}
2626
2627
	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...
2628
	give_set_upgrade_complete( 'v201_add_missing_donors' );
2629
}
2630
2631
2632
/**
2633
 * Version 2.0.3 automatic updates
2634
 *
2635
 * @since 2.0.3
2636
 */
2637
function give_v203_upgrades() {
2638
	global $wpdb;
2639
2640
	// Do not auto load option.
2641
	$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...
2642
2643
	// Remove from cache.
2644
	$all_options = wp_load_alloptions();
2645
2646
	if ( isset( $all_options['give_completed_upgrades'] ) ) {
2647
		unset( $all_options['give_completed_upgrades'] );
2648
		wp_cache_set( 'alloptions', $all_options, 'options' );
2649
	}
2650
2651
}
2652
2653
2654
/**
2655
 * Version 2.2.0 automatic updates
2656
 *
2657
 * @since 2.2.0
2658
 */
2659
function give_v220_upgrades(){
2660
	global $wpdb;
2661
2662
	/**
2663
	 * Update 1
2664
	 *
2665
	 * Delete wp session data
2666
	 */
2667
	give_v220_delete_wp_session_data();
2668
2669
	/**
2670
	 * Update 2
2671
	 *
2672
	 * Set autoload to no to reduce result weight from WordPress query
2673
	 */
2674
2675
	$options = array(
2676
		'give_settings',
2677
		'give_version',
2678
		'give_version_upgraded_from',
2679
		'give_default_api_version',
2680
		'give_site_address_before_migrate',
2681
		'_give_table_check',
2682
		'give_recently_activated_addons',
2683
		'give_is_addon_activated',
2684
		'give_last_paypal_ipn_received',
2685
		'give_use_php_sessions',
2686
		'give_subscriptions',
2687
		'_give_subscriptions_edit_last'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
2688
	);
2689
2690
	// Add all table version option name
2691
	// Add banner option *_active_by_user
2692
	$option_like = $wpdb->get_col(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
2693
		"
2694
		SELECT option_name
2695
		FROM $wpdb->options
2696
		WHERE option_name like '%give%'
2697
		AND (
2698
			option_name like '%_db_version%'
2699
			OR option_name like '%_active_by_user%'
2700
			OR option_name like '%_license_active%'
2701
		)
2702
		"
2703
	);
2704
2705
	if( ! empty( $option_like ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
2706
		$options = array_merge( $options, $option_like );
2707
	}
2708
2709
	$options_str =  '\'' . implode( "','", $options ) . '\'';
0 ignored issues
show
introduced by
Expected 1 space after "="; 2 found
Loading history...
2710
2711
	$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...
2712
		"
2713
		UPDATE $wpdb->options
2714
		SET autoload = 'no'
2715
		WHERE option_name IN ( {$options_str} )
2716
		"
2717
	);
2718
}
2719
2720
/**
2721
 * Upgrade routine for 2.1 to set form closed status for all the donation forms.
2722
 *
2723
 * @since 2.1
2724
 */
2725
function give_v210_verify_form_status_upgrades_callback() {
2726
2727
	$give_updates = Give_Updates::get_instance();
2728
2729
	// form query.
2730
	$donation_forms = new WP_Query( array(
2731
			'paged'          => $give_updates->step,
2732
			'status'         => 'any',
2733
			'order'          => 'ASC',
2734
			'post_type'      => 'give_forms',
2735
			'posts_per_page' => 20,
2736
		)
2737
	);
2738
2739
	if ( $donation_forms->have_posts() ) {
2740
		$give_updates->set_percentage( $donation_forms->found_posts, ( $give_updates->step * 20 ) );
2741
2742
		while ( $donation_forms->have_posts() ) {
2743
			$donation_forms->the_post();
2744
			$form_id = get_the_ID();
2745
2746
			$form_closed_status = give_get_meta( $form_id, '_give_form_status', true );
2747
			if ( empty( $form_closed_status ) ) {
2748
				give_set_form_closed_status( $form_id );
2749
			}
2750
		}
2751
2752
		/* Restore original Post Data */
2753
		wp_reset_postdata();
2754
2755
	} else {
2756
2757
		// The Update Ran.
2758
		give_set_upgrade_complete( 'v210_verify_form_status_upgrades' );
2759
	}
2760
}
2761
2762
/**
2763
 * Upgrade routine for 2.1.3 to delete meta which is not attach to any donation.
2764
 *
2765
 * @since 2.1
2766
 */
2767
function give_v213_delete_donation_meta_callback() {
2768
	global $wpdb;
2769
	$give_updates        = Give_Updates::get_instance();
2770
	$donation_meta_table = Give()->payment_meta->table_name;
2771
2772
	$donations = $wpdb->get_col(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
2773
		"
2774
		SELECT DISTINCT payment_id
2775
		FROM {$donation_meta_table}
2776
		LIMIT 20
2777
		OFFSET {$give_updates->get_offset( 20 )}
2778
		"
2779
	);
2780
2781
	if ( ! empty( $donations ) ) {
2782
		foreach ( $donations as $donation ) {
2783
			$donation_obj = get_post( $donation );
2784
2785
			if ( ! $donation_obj instanceof WP_Post ) {
0 ignored issues
show
Bug introduced by
The class WP_Post does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
2786
				Give()->payment_meta->delete_all_meta( $donation );
2787
			}
2788
		}
2789
	} else {
2790
2791
		// The Update Ran.
2792
		give_set_upgrade_complete( 'v213_delete_donation_meta' );
2793
	}
2794
}
2795
2796
/**
2797
 * Rename donation meta type
2798
 *
2799
 * @see https://github.com/restrictcontentpro/restrict-content-pro/issues/1656
2800
 *
2801
 * @since 2.1.3
2802
 */
2803
function give_v213_rename_donation_meta_type_callback(){
2804
	global $wpdb;
2805
	$give_updates = Give_Updates::get_instance();
2806
2807
	$wpdb->query( "ALTER TABLE {$wpdb->prefix}give_paymentmeta CHANGE COLUMN payment_id donation_id bigint(20)" );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
introduced by
Attempting a database schema change is highly discouraged.
Loading history...
2808
	$wpdb->query( "ALTER TABLE {$wpdb->prefix}give_paymentmeta RENAME TO {$wpdb->prefix}give_donationmeta" );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
introduced by
Attempting a database schema change is highly discouraged.
Loading history...
2809
2810
	give_set_upgrade_complete('v213_rename_donation_meta_type');
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
2811
	$give_updates->set_percentage(1, 1);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
2812
}
2813
2814
/**
2815
 * Adds 'view_give_payments' capability to 'give_manager' user role.
2816
 *
2817
 * @since 2.1.5
2818
 */
2819
function give_v215_update_donor_user_roles_callback() {
2820
2821
	$role = get_role( 'give_manager' );
2822
	$role->add_cap( 'view_give_payments' );
2823
2824
	give_set_upgrade_complete( 'v215_update_donor_user_roles' );
2825
}
2826
2827
2828
2829
/**
2830
 * Remove all wp session data from the options table, regardless of expiration.
2831
 *
2832
 * @since 2.2.0
2833
 *
2834
 * @global wpdb $wpdb
2835
 */
2836
function give_v220_delete_wp_session_data(){
2837
	global $wpdb;
2838
2839
	$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE '_wp_session_%'" );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
2840
}
2841