Issues (4296)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

includes/payments/backward-compatibility.php (28 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Split _give_payment_meta to new Give core meta_keys.
4
 *
5
 * @since 2.0
6
 *
7
 * @param       $object_id
8
 * @param array $meta_value
9
 *
10
 * @return void
11
 */
12
function _give_20_bc_split_and_save_give_payment_meta( $object_id, $meta_value ) {
13
	// Bailout
14
	if ( empty( $meta_value ) ) {
15
		return;
16
	} elseif ( ! is_array( $meta_value ) ) {
17
		$meta_value = array();
18
	}
19
20
	remove_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10 );
21
22
	// Date payment meta.
23
	if ( ! empty( $meta_value['date'] ) ) {
24
		give_update_meta( $object_id, '_give_payment_date', $meta_value['date'] );
25
	}
26
27
	// Currency payment meta.
28
	if ( ! empty( $meta_value['currency'] ) ) {
29
		give_update_meta( $object_id, '_give_payment_currency', $meta_value['currency'] );
30
	}
31
32
	// User information.
33
	if ( ! empty( $meta_value['user_info'] ) ) {
34
		// Donor first name.
35
		if ( ! empty( $meta_value['user_info']['first_name'] ) ) {
36
			give_update_meta( $object_id, '_give_donor_billing_first_name', $meta_value['user_info']['first_name'] );
37
		}
38
39
		// Donor last name.
40
		if ( ! empty( $meta_value['user_info']['last_name'] ) ) {
41
			give_update_meta( $object_id, '_give_donor_billing_last_name', $meta_value['user_info']['last_name'] );
42
		}
43
44
		// Donor address payment meta.
45
		if ( ! empty( $meta_value['user_info']['address'] ) ) {
46
47
			// Address1.
48 View Code Duplication
			if ( ! empty( $meta_value['user_info']['address']['line1'] ) ) {
0 ignored issues
show
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...
49
				give_update_meta( $object_id, '_give_donor_billing_address1', $meta_value['user_info']['address']['line1'] );
50
			}
51
52
			// Address2.
53 View Code Duplication
			if ( ! empty( $meta_value['user_info']['address']['line2'] ) ) {
0 ignored issues
show
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...
54
				give_update_meta( $object_id, '_give_donor_billing_address2', $meta_value['user_info']['address']['line2'] );
55
			}
56
57
			// City.
58 View Code Duplication
			if ( ! empty( $meta_value['user_info']['address']['city'] ) ) {
0 ignored issues
show
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...
59
				give_update_meta( $object_id, '_give_donor_billing_city', $meta_value['user_info']['address']['city'] );
60
			}
61
62
			// Zip.
63 View Code Duplication
			if ( ! empty( $meta_value['user_info']['address']['zip'] ) ) {
0 ignored issues
show
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...
64
				give_update_meta( $object_id, '_give_donor_billing_zip', $meta_value['user_info']['address']['zip'] );
65
			}
66
67
			// State.
68 View Code Duplication
			if ( ! empty( $meta_value['user_info']['address']['state'] ) ) {
0 ignored issues
show
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...
69
				give_update_meta( $object_id, '_give_donor_billing_state', $meta_value['user_info']['address']['state'] );
70
			}
71
72
			// Country.
73 View Code Duplication
			if ( ! empty( $meta_value['user_info']['address']['country'] ) ) {
0 ignored issues
show
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...
74
				give_update_meta( $object_id, '_give_donor_billing_country', $meta_value['user_info']['address']['country'] );
75
			}
76
		}
77
	}// End if().
78
79
	add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
80
}
81
82
/**
83
 * Add backward compatibility to get meta value of _give_payment_meta meta key.
84
 *
85
 * @since 2.0
86
 *
87
 * @param       $object_id
88
 * @param array $meta_value
89
 *
90
 * @return array
91
 */
92
function _give_20_bc_give_payment_meta_value( $object_id, $meta_value ) {
93
	$cache_key = "_give_payment_meta_{$object_id}";
94
	$cache     = Give_Cache::get_db_query( $cache_key );
95
96
	if ( ! is_null( $cache ) ) {
97
		return $cache;
98
	}
99
100
	// Set default value to array.
101
	if ( ! is_array( $meta_value ) ) {
102
		$meta_value = array();
103
	}
104
105
	// Donation key.
106
	$meta_value['key'] = give_get_meta( $object_id, '_give_payment_purchase_key', true );
107
108
	// Donation form.
109
	$meta_value['form_title'] = give_get_meta( $object_id, '_give_payment_form_title', true );
110
111
	// Donor email.
112
	$meta_value['email'] = give_get_meta( $object_id, '_give_payment_donor_email', true );
113
	$meta_value['email'] = ! empty( $meta_value['email'] ) ?
114
		$meta_value['email'] :
115
		Give()->donors->get_column( 'email', give_get_payment_donor_id( $object_id ) );
116
117
	// Form id.
118
	$meta_value['form_id'] = give_get_meta( $object_id, '_give_payment_form_id', true );
119
120
	// Price id.
121
	$meta_value['price_id'] = give_get_meta( $object_id, '_give_payment_price_id', true );
122
123
	// Date.
124
	$meta_value['date'] = give_get_meta( $object_id, '_give_payment_date', true );
125
	$meta_value['date'] = ! empty( $meta_value['date'] ) ?
126
		$meta_value['date'] :
127
		get_post_field( 'post_date', $object_id );
128
129
	// Currency.
130
	$meta_value['currency'] = give_get_meta( $object_id, '_give_payment_currency', true );
131
132
	// Decode donor data.
133
	$donor_names = give_get_donor_name_by( give_get_meta( $object_id, '_give_payment_donor_id', true ), 'donor' );
134
	$donor_names = explode( ' ', $donor_names, 2 );
135
136
	// Donor first name.
137
	$donor_data['first_name'] = give_get_meta( $object_id, '_give_donor_billing_first_name', true );
138
	$donor_data['first_name'] = ! empty( $donor_data['first_name'] ) ?
139
		$donor_data['first_name'] :
140
		$donor_names[0];
141
142
	// Donor last name.
143
	$donor_data['last_name'] = give_get_meta( $object_id, '_give_donor_billing_last_name', true );
144
	$donor_data['last_name'] = ! empty( $donor_data['last_name'] ) ?
145
		$donor_data['last_name'] :
146
		( isset( $donor_names[1] ) ? $donor_names[1] : '' );
147
148
	// Donor email.
149
	$donor_data['email'] = $meta_value['email'];
150
151
	// User ID.
152
	$donor_data['id'] = give_get_payment_user_id( $object_id );
153
154
	$donor_data['address'] = false;
155
156
	// Address1.
157
	if ( $address1 = give_get_meta( $object_id, '_give_donor_billing_address1', true ) ) {
158
		$donor_data['address']['line1'] = $address1;
159
	}
160
161
	// Address2.
162
	if ( $address2 = give_get_meta( $object_id, '_give_donor_billing_address2', true ) ) {
163
		$donor_data['address']['line2'] = $address2;
164
	}
165
166
	// City.
167 View Code Duplication
	if ( $city = give_get_meta( $object_id, '_give_donor_billing_city', true ) ) {
0 ignored issues
show
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...
168
		$donor_data['address']['city'] = $city;
169
	}
170
171
	// Zip.
172 View Code Duplication
	if ( $zip = give_get_meta( $object_id, '_give_donor_billing_zip', true ) ) {
0 ignored issues
show
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...
173
		$donor_data['address']['zip'] = $zip;
174
	}
175
176
	// State.
177
	if ( $state = give_get_meta( $object_id, '_give_donor_billing_state', true ) ) {
178
		$donor_data['address']['state'] = $state;
179
	}
180
181
	// Country.
182
	if ( $country = give_get_meta( $object_id, '_give_donor_billing_country', true ) ) {
183
		$donor_data['address']['country'] = $country;
184
	}
185
186
	$meta_value['user_info'] = maybe_unserialize( $donor_data );
187
188
	Give_Cache::set_db_query( $cache_key, $meta_value );
189
190
	return $meta_value;
191
}
192
193
/**
194
 * Add backward compatibility old meta while saving.
195
 *  1. _give_payment_meta (split into multiple single meta keys)
196
 *  2. _give_payment_user_email (renamed to _give_payment_donor_email)
197
 *  3. _give_payment_customer_id (renamed to _give_payment_donor_id)
198
 *  4. give_payment_user_ip (renamed to give_payment_donor_ip)
199
 *
200
 * @since 2.0
201
 *
202
 * @param null|bool $check      Whether to allow updating metadata for the given type.
203
 * @param int       $object_id  Object ID.
204
 * @param string    $meta_key   Meta key.
205
 * @param mixed     $meta_value Meta value. Must be serializable if non-scalar.
206
 * @param mixed     $prev_value Optional. If specified, only update existing
207
 *                              metadata entries with the specified value.
208
 *                              Otherwise, update all entries.
209
 *
210
 * @return mixed
211
 */
212
function _give_20_bc_saving_old_payment_meta( $check, $object_id, $meta_key, $meta_value, $prev_value ) {
0 ignored issues
show
The parameter $prev_value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
213
	// Bailout.
214
	if (
215
		'give_payment' !== get_post_type( $object_id ) ||
216
		! in_array( $meta_key, array(
217
			'_give_payment_meta',
218
			'_give_payment_user_email',
219
			'_give_payment_customer_id',
220
			'give_payment_user_ip',
221
		) )
222
	) {
223
		return $check;
224
	}
225
226
	if ( '_give_payment_meta' === $meta_key ) {
227
		_give_20_bc_split_and_save_give_payment_meta( $object_id, $meta_value );
228
	} elseif ( '_give_payment_user_email' === $meta_key ) {
229
		give_update_meta( $object_id, '_give_payment_donor_email', $meta_value );
230
		$check = true;
231
	} elseif ( '_give_payment_customer_id' === $meta_key ) {
232
		give_update_meta( $object_id, '_give_payment_donor_id', $meta_value );
233
		$check = true;
234
	} elseif ( 'give_payment_user_ip' === $meta_key ) {
235
		give_update_meta( $object_id, '_give_payment_donor_ip', $meta_value );
236
		$check = true;
237
	}
238
239
	return $check;
240
}
241
242
add_filter( 'update_post_metadata', '_give_20_bc_saving_old_payment_meta', 10, 5 );
243
244
245
/**
246
 * Add backward compatibility to get old payment meta.
247
 *
248
 * @since 2.0
249
 *
250
 * @param $check
251
 * @param $object_id
252
 * @param $meta_key
253
 * @param $single
254
 *
255
 * @return mixed
256
 */
257
function _give_20_bc_get_old_payment_meta( $check, $object_id, $meta_key, $single ) {
258
	global $wpdb;
259
260
	// Deprecated meta keys.
261
	$old_meta_keys = array(
262
		'_give_payment_customer_id',
263
		'_give_payment_user_email',
264
		'_give_payment_user_ip',
265
	);
266
267
	// Add _give_payment_meta to backward compatibility
268
	if ( ! give_has_upgrade_completed( 'v20_upgrades_payment_metadata' ) ) {
269
		$old_meta_keys[] = '_give_payment_meta';
270
	}
271
272
	// Bailout.
273
	if (
274
		'give_payment' !== get_post_type( $object_id ) ||
275
		! in_array( $meta_key, $old_meta_keys )
276
	) {
277
		return $check;
278
	}
279
280
	$cache_key = "{$meta_key}_{$object_id}";
281
	$check     = Give_Cache::get_db_query( $cache_key );
282
283
	if ( is_null( $check ) ) {
284
		switch ( $meta_key ) {
285
286
			// Handle old meta keys.
287
			case '_give_payment_meta':
288
				remove_filter( 'get_post_metadata', '_give_20_bc_get_old_payment_meta' );
289
290
				// if ( $meta_value = give_get_meta( $object_id, '_give_payment_meta' ) ) {
291
				$meta_value = ! empty( $meta_value ) ?
292
					current( $meta_value ) :
293
					(array) maybe_unserialize(
294
						$wpdb->get_var(
0 ignored issues
show
Usage of a direct database call is discouraged.
Loading history...
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
295
							$wpdb->prepare(
296
								"
297
								SELECT meta_value
298
								FROM $wpdb->postmeta
299
								WHERE post_id=%d
300
								AND meta_key=%s
301
								",
302
								$object_id,
303
								'_give_payment_meta'
304
							)
305
						)
306
					);
307
				$check      = _give_20_bc_give_payment_meta_value( $object_id, $meta_value );
308
				// }
309
310
				add_filter( 'get_post_metadata', '_give_20_bc_get_old_payment_meta', 10, 5 );
311
312
				break;
313
314
			case '_give_payment_customer_id':
315
				if ( $donor_id = give_get_meta( $object_id, '_give_payment_donor_id', $single ) ) {
316
					$check = $donor_id;
317
				}
318
				break;
319
320
			case '_give_payment_user_email':
321
				if ( $donor_email = give_get_meta( $object_id, '_give_payment_donor_email', $single ) ) {
322
					$check = $donor_email;
323
				}
324
				break;
325
326
			case '_give_payment_user_ip':
327
				if ( $donor_ip = give_get_meta( $object_id, '_give_payment_donor_ip', $single ) ) {
328
					$check = $donor_ip;
329
				}
330
				break;
331
		}// End switch().
332
333
		Give_Cache::set_db_query( $cache_key, $check );
334
	}
335
336
	// Put result in an array on zero index.
337
	if ( ! is_null( $check ) ) {
338
		$check = array( $check );
339
	}
340
341
	return $check;
342
}
343
344
add_filter( 'get_post_metadata', '_give_20_bc_get_old_payment_meta', 10, 5 );
345
346
347
/**
348
 * Add backward compatibility to get new payment meta.
349
 *
350
 * @since 2.0
351
 *
352
 * @param $check
353
 * @param $object_id
354
 * @param $meta_key
355
 * @param $single
356
 *
357
 * @return mixed
358
 */
359
function _give_20_bc_get_new_payment_meta( $check, $object_id, $meta_key, $single ) {
360
	// Bailout: do not apply backward compatibility if upgrade done.
361
	if ( give_has_upgrade_completed( 'v20_upgrades_payment_metadata' ) ) {
362
		return $check;
363
	}
364
365
	global $wpdb;
366
	$new_meta_keys = array(
367
		'_give_payment_donor_id',
368
		'_give_payment_donor_email',
369
		'_give_payment_donor_ip',
370
		'_give_donor_billing_first_name',
371
		'_give_donor_billing_last_name',
372
		'_give_donor_billing_address1',
373
		'_give_donor_billing_address2',
374
		'_give_donor_billing_city',
375
		'_give_donor_billing_zip',
376
		'_give_donor_billing_state',
377
		'_give_donor_billing_country',
378
		'_give_payment_date',
379
		'_give_payment_currency',
380
	);
381
382
	// metadata_exists fx will cause of firing get_post_metadata filter again so remove it to prevent infinite loop.
383
	remove_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta' );
384
385
	// Bailout.
386
	if (
387
		'give_payment' !== get_post_type( $object_id ) ||
388
		! in_array( $meta_key, $new_meta_keys ) ||
389
		metadata_exists( 'post', $object_id, $meta_key )
390
	) {
391
		add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
392
393
		return $check;
394
	}
395
396
	add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
397
398
	$cache_key = "{$meta_key}_{$object_id}";
399
	$check    = Give_Cache::get_db_query( $cache_key );
400
401
	if ( is_null( $check ) ) {
402
		switch ( $meta_key ) {
403
404
			// Handle new meta keys.
405
			case '_give_payment_donor_id':
406
				$check = $wpdb->get_var(
0 ignored issues
show
Usage of a direct database call is discouraged.
Loading history...
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
407
					$wpdb->prepare(
408
						"SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id=%d AND meta_key=%s",
409
						$object_id,
410
						'_give_payment_customer_id'
411
					)
412
				);
413
				break;
414
415
			case '_give_payment_donor_email':
416
				$check = $wpdb->get_var(
0 ignored issues
show
Usage of a direct database call is discouraged.
Loading history...
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
417
					$wpdb->prepare(
418
						"SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id=%d AND meta_key=%s",
419
						$object_id,
420
						'_give_payment_user_email'
421
					)
422
				);
423
				break;
424
425
			case '_give_payment_donor_ip':
426
				$check = $wpdb->get_var(
0 ignored issues
show
Usage of a direct database call is discouraged.
Loading history...
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
427
					$wpdb->prepare(
428
						"SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id=%s AND meta_key=%s",
429
						$object_id,
430
						'_give_payment_user_ip'
431
					)
432
				);
433
				break;
434
435
			case '_give_donor_billing_first_name':
436
			case '_give_donor_billing_last_name':
437
			case '_give_donor_billing_address1':
438
			case '_give_donor_billing_address2':
439
			case '_give_donor_billing_city':
440
			case '_give_donor_billing_zip':
441
			case '_give_donor_billing_state':
442
			case '_give_donor_billing_country':
443
			case '_give_payment_date':
444
			case '_give_payment_currency':
445
				$donation_meta = Give_Cache::get_db_query( "_give_payment_meta_{$object_id}" );
446
447
				if ( is_null( $donation_meta ) ) {
448
					$donation_meta = $wpdb->get_var(
0 ignored issues
show
Usage of a direct database call is discouraged.
Loading history...
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
449
						$wpdb->prepare(
450
							"SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id=%d AND meta_key=%s",
451
							$object_id,
452
							'_give_payment_meta'
453
						)
454
					);
455
					$donation_meta = maybe_unserialize( $donation_meta );
456
					$donation_meta = ! is_array( $donation_meta ) ? array() : $donation_meta;
457
					Give_Cache::set_db_query( "_give_payment_meta_{$object_id}", $donation_meta );
458
				}
459
460
				// Get results.
461
				if ( empty( $donation_meta ) ) {
462
					$check = '';
463
				} elseif ( in_array( $meta_key, array( '_give_payment_date', '_give_payment_currency' ) ) ) {
464
					$payment_meta_key = str_replace( '_give_payment_', '', $meta_key );
465
466
					if ( isset( $donation_meta[ $payment_meta_key ] ) ) {
467
						$check = $donation_meta[ $payment_meta_key ];
468
					}
469
				} else {
470
					$payment_meta_key = str_replace( '_give_donor_billing_', '', $meta_key );
471
472
					switch ( $payment_meta_key ) {
473 View Code Duplication
						case 'address1':
0 ignored issues
show
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...
474
							if ( isset( $donation_meta['user_info']['address']['line1'] ) ) {
475
								$check = $donation_meta['user_info']['address']['line1'];
476
							}
477
							break;
478
479 View Code Duplication
						case 'address2':
0 ignored issues
show
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...
480
							if ( isset( $donation_meta['user_info']['address']['line2'] ) ) {
481
								$check = $donation_meta['user_info']['address']['line2'];
482
							}
483
							break;
484
485 View Code Duplication
						case 'first_name':
0 ignored issues
show
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...
486
							if ( isset( $donation_meta['user_info']['first_name'] ) ) {
487
								$check = $donation_meta['user_info']['first_name'];
488
							}
489
							break;
490
491 View Code Duplication
						case 'last_name':
0 ignored issues
show
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...
492
							if ( isset( $donation_meta['user_info']['last_name'] ) ) {
493
								$check = $donation_meta['user_info']['last_name'];
494
							}
495
							break;
496
497
						default:
498
							if ( isset( $donation_meta['user_info']['address'][ $payment_meta_key ] ) ) {
499
								$check = $donation_meta['user_info']['address'][ $payment_meta_key ];
500
							}
501
					}
502
				}
503
504
				break;
505
		}// End switch().
506
507
		// Set cache.
508
		Give_Cache::set_db_query( $cache_key, $check );
509
	}
510
511
	// Put result in an array on zero index.
512
	if ( ! $single ) {
513
		$check = array( $check );
514
	}
515
0 ignored issues
show
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
516
517
	return $check;
518
}
519
520
add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
521
522
523
/**
524
 * Add support for old payment meta keys.
525
 *
526
 * @since 2.0
527
 *
528
 * @param WP_Query $query
529
 *
530
 * @return void
531
 */
532
function _give_20_bc_support_deprecated_meta_key_query( $query ) {
533
	// Bailout.
534
	if ( give_has_upgrade_completed( 'v20_upgrades_payment_metadata' ) ) {
535
		return;
536
	}
537
538
	$new_meta_keys = array(
539
		'_give_payment_customer_id' => '_give_payment_donor_id',
540
		'_give_payment_user_email'  => '_give_payment_donor_email',
541
		// '_give_payment_user_ip'     => '_give_payment_donor_ip',
542
	);
543
544
	$deprecated_meta_keys = array_flip( $new_meta_keys );
545
546
	// Set meta keys.
547
	$meta_keys = array();
548
0 ignored issues
show
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
549
550
	// Bailout.
551
	if ( ! empty( $query->query_vars['meta_key'] ) ) {
552
		if ( in_array( $query->query_vars['meta_key'], $new_meta_keys ) ) {
553
			$meta_keys = $deprecated_meta_keys;
554
		} elseif ( in_array( $query->query_vars['meta_key'], $deprecated_meta_keys ) ) {
555
			$meta_keys = $new_meta_keys;
556
		}
557
558
		if ( ! empty( $meta_keys ) ) {
559
			// Set meta_query
560
			$query->set(
561
				'meta_query',
562
				array(
563
					'relation' => 'OR',
564
					array(
565
						'key'   => $query->query_vars['meta_key'],
566
						'value' => $query->query_vars['meta_value'],
567
					),
568
					array(
569
						'key'   => $meta_keys[ $query->query_vars['meta_key'] ],
570
						'value' => $query->query_vars['meta_value'],
571
					),
572
				)
573
			);
574
575
			// Unset single meta query.
576
			unset( $query->query_vars['meta_key'] );
577
			unset( $query->query_vars['meta_value'] );
578
		}
579
	} elseif (
580
		! empty( $query->query_vars['meta_query'] ) &&
581
		( 1 === count( $query->query_vars['meta_query'] ) )
582
	) {
583
		$meta_query = current( $query->query_vars['meta_query'] );
584
585
		if ( empty( $meta_query[0]['key'] ) ) {
586
			return;
587
		}
588
589
		if ( in_array( $meta_query[0]['key'], $new_meta_keys ) ) {
590
			$meta_keys = $deprecated_meta_keys;
591
		} elseif ( in_array( $meta_query[0]['key'], $deprecated_meta_keys ) ) {
592
			$meta_keys = $new_meta_keys;
593
		} else {
594
			return;
595
		}
596
597
		if ( ! empty( $meta_keys ) ) {
598
			// Set meta_query
599
			$query->set(
600
				'meta_query',
601
				array(
602
					'relation' => 'OR',
603
					array(
604
						'key'   => $query->query_vars['meta_query'][0]['key'],
605
						'value' => $query->query_vars['meta_query'][0]['value'],
606
					),
607
					array(
608
						'key'   => $meta_keys[ $query->query_vars['meta_query'][0]['key'] ],
609
						'value' => $query->query_vars['meta_query'][0]['value'],
610
					),
611
				)
612
			);
613
		}
614
	}
615
}
616
617
add_action( 'pre_get_posts', '_give_20_bc_support_deprecated_meta_key_query' );
618
619
620
/**
621
 * Save payment backward compatibility.
622
 * Note: some addon still can use user_info in set payment meta
623
 *       we will use this info to set first name, last name and address of donor
624
 *
625
 * @since 2.0
626
 *
627
 * @param Give_Payment $payment
628
 * @param string       $key
629
 */
630
function _give_20_bc_payment_save( $payment, $key ) {
631
	// Bailout.
632
	if ( ! give_has_upgrade_completed( 'v20_upgrades_payment_metadata' ) ) {
633
		return;
634
	}
635
636
	switch ( $key ) {
637
		case 'user_info':
638
			if ( empty( $payment->user_info ) ) {
639
				// Bailout.
640
				break;
641
			} elseif ( is_string( $payment->user_info ) ) {
642
				// Check if value serialize.
643
				$payment->user_info = maybe_unserialize( $payment->user_info );
644
			}
645
0 ignored issues
show
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
646
647
			// Save first name.
648
			if ( isset( $payment->user_info['first_name'] ) ) {
649
				$payment->update_meta( '_give_donor_billing_first_name', $payment->user_info['first_name'] );
650
			}
651
0 ignored issues
show
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
652
653
			// Save last name.
654
			if ( isset( $payment->user_info['last_name'] ) ) {
655
				$payment->update_meta( '_give_donor_billing_last_name', $payment->user_info['last_name'] );
656
			}
657
0 ignored issues
show
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
658
659
			// Save address.
660
			if ( ! empty( $payment->user_info['address'] ) ) {
661
				foreach ( $payment->user_info['address'] as $address_name => $address ) {
662
					switch ( $address_name ) {
663
						case 'line1':
664
							$payment->update_meta( '_give_donor_billing_address1', $address );
665
							break;
666
667
						case 'line2':
668
							$payment->update_meta( '_give_donor_billing_address2', $address );
669
							break;
670
671
						default:
672
							$payment->update_meta( "_give_donor_billing_{$address_name}", $address );
673
					}
674
				}
675
			}
676
677
			break;
678
	}
679
}
680
681
add_action( 'give_payment_save', '_give_20_bc_payment_save', 10, 2 );
682
683
684
/**
685
 * Delete pre upgrade cache for donations.
686
 *
687
 * @since 2.0
688
 *
689
 * @param $check
690
 * @param $object_id
691
 *
692
 * @return mixed
693
 */
694
function __give_20_bc_flush_cache( $check, $object_id ) {
695
	if (
696
		! give_has_upgrade_completed( 'v20_move_metadata_into_new_table' ) &&
697
		'give_payment' === get_post_type( $object_id )
698
	) {
699
		Give_Cache::delete_group( $object_id, 'give-donations' );
700
	}
701
702
	return $check;
703
}
704
705
add_action( 'update_post_metadata', '__give_20_bc_flush_cache', 9999, 2 );
706
add_action( 'add_post_metadata', '__give_20_bc_flush_cache', 9999, 2 );
707