Completed
Push — issues/1132 ( bc164b...1192dc )
by Ravinder
18:12
created

backward-compatibility.php ➔ _give_20_bc_payment_save()   D

Complexity

Conditions 10
Paths 18

Size

Total Lines 45
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 23
nc 18
nop 2
dl 0
loc 45
rs 4.8196
c 0
b 0
f 0

How to fix   Complexity   

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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 12 and the first side effect is on line 221.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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
	// Date payment meta.
21
	if ( ! empty( $meta_value['date'] ) ) {
22
		give_update_meta( $object_id, '_give_payment_date', $meta_value['date'] );
23
	}
24
25
	// Currency payment meta.
26
	if ( ! empty( $meta_value['currency'] ) ) {
27
		give_update_meta( $object_id, '_give_payment_currency', $meta_value['currency'] );
28
	}
29
30
	// User information.
31
	if ( ! empty( $meta_value['user_info'] ) ) {
32
		// Donor first name.
33
		if ( ! empty( $meta_value['user_info']['first_name'] ) ) {
34
			give_update_meta( $object_id, '_give_donor_billing_first_name', $meta_value['user_info']['first_name'] );
35
		}
36
37
		// Donor last name.
38
		if ( ! empty( $meta_value['user_info']['last_name'] ) ) {
39
			give_update_meta( $object_id, '_give_donor_billing_last_name', $meta_value['user_info']['last_name'] );
40
		}
41
42
		// Donor address payment meta.
43
		if ( ! empty( $meta_value['user_info']['address'] ) ) {
44
45
			// Address1.
46
			if ( ! empty( $meta_value['user_info']['address']['line1'] ) ) {
47
				give_update_meta( $object_id, '_give_donor_billing_address1', $meta_value['user_info']['address']['line1'] );
48
			}
49
50
			// Address2.
51
			if ( ! empty( $meta_value['user_info']['address']['line2'] ) ) {
52
				give_update_meta( $object_id, '_give_donor_billing_address2', $meta_value['user_info']['address']['line2'] );
53
			}
54
55
			// City.
56
			if ( ! empty( $meta_value['user_info']['address']['city'] ) ) {
57
				give_update_meta( $object_id, '_give_donor_billing_city', $meta_value['user_info']['address']['city'] );
58
			}
59
60
			// Zip.
61
			if ( ! empty( $meta_value['user_info']['address']['zip'] ) ) {
62
				give_update_meta( $object_id, '_give_donor_billing_zip', $meta_value['user_info']['address']['zip'] );
63
			}
64
65
			// State.
66
			if ( ! empty( $meta_value['user_info']['address']['state'] ) ) {
67
				give_update_meta( $object_id, '_give_donor_billing_state', $meta_value['user_info']['address']['state'] );
68
			}
69
70
			// Country.
71
			if ( ! empty( $meta_value['user_info']['address']['country'] ) ) {
72
				give_update_meta( $object_id, '_give_donor_billing_country', $meta_value['user_info']['address']['country'] );
73
			}
74
		}
75
	}// End if().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
76
}
77
78
/**
79
 * Add backward compatibility to get meta value of _give_payment_meta meta key.
80
 *
81
 * @since 2.0
82
 *
83
 * @param       $object_id
84
 * @param array $meta_value
85
 *
86
 * @return array
87
 */
88
function _give_20_bc_give_payment_meta_value( $object_id, $meta_value ) {
89
	// Set default value to array.
90
	if ( ! is_array( $meta_value ) ) {
91
		$meta_value = array();
92
	}
93
94
	// Donation key.
95
	$meta_value['key'] = give_get_meta( $object_id, '_give_payment_purchase_key', true );
96
97
	// Donation form.
98
	$meta_value['form_title'] = give_get_meta( $object_id, '_give_payment_form_title', true );
99
100
	// Donor email.
101
	$meta_value['email'] = give_get_meta( $object_id, '_give_payment_donor_email', true );
102
	$meta_value['email'] = ! empty( $meta_value['email'] ) ?
103
		$meta_value['email'] :
104
		Give()->donors->get_column( 'email', give_get_payment_donor_id( $object_id ) );
105
106
	// Form id.
107
	$meta_value['form_id'] = give_get_meta( $object_id, '_give_payment_form_id', true );
108
109
	// Price id.
110
	$meta_value['price_id'] = give_get_meta( $object_id, '_give_payment_price_id', true );
111
112
	// Date.
113
	$meta_value['date'] = give_get_meta( $object_id, '_give_payment_date', true );
114
	$meta_value['date'] = ! empty( $meta_value['date'] ) ?
115
		$meta_value['date'] :
116
		get_post_field( 'post_date', $object_id );
117
118
	// Currency.
119
	$meta_value['currency'] = give_get_meta( $object_id, '_give_payment_currency', true );
120
121
	// Decode donor data.
122
	$donor_names = give_get_donor_name_by( give_get_meta( $object_id, '_give_payment_donor_id', true ), 'donor' );
123
	$donor_names = explode( ' ', $donor_names, 2 );
124
125
	// Donor first name.
126
	$donor_data['first_name'] = give_get_meta( $object_id, '_give_donor_billing_first_name', true );
0 ignored issues
show
Coding Style Comprehensibility introduced by
$donor_data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $donor_data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
127
	$donor_data['first_name'] = ! empty( $donor_data['first_name'] ) ?
128
		$donor_data['first_name'] :
129
		$donor_names[0];
130
131
	// Donor last name.
132
	$donor_data['last_name'] = give_get_meta( $object_id, '_give_donor_billing_last_name', true );
133
	$donor_data['last_name'] = ! empty( $donor_data['last_name'] ) ?
134
		$donor_data['last_name'] :
135
		( isset( $donor_names[1] ) ? $donor_names[1] : '' );
136
137
	// Donor email.
138
	$donor_data['email'] = $meta_value['email'];
139
140
	// User ID.
141
	$donor_data['id'] = give_get_payment_user_id( $object_id );
142
143
	$donor_data['address'] = false;
144
145
	// Address1.
146
	if ( $address1 = give_get_meta( $object_id, '_give_donor_billing_address1', true ) ) {
147
		$donor_data['address']['line1'] = $address1;
148
	}
149
150
	// Address2.
151
	if ( $address2 = give_get_meta( $object_id, '_give_donor_billing_address2', true ) ) {
152
		$donor_data['address']['line2'] = $address2;
153
	}
154
155
	// City.
156
	if ( $city = give_get_meta( $object_id, '_give_donor_billing_city', true ) ) {
157
		$donor_data['address']['city'] = $city;
158
	}
159
160
	// Zip.
161
	if ( $zip = give_get_meta( $object_id, '_give_donor_billing_zip', true ) ) {
162
		$donor_data['address']['zip'] = $zip;
163
	}
164
165
	// State.
166
	if ( $state = give_get_meta( $object_id, '_give_donor_billing_state', true ) ) {
167
		$donor_data['address']['state'] = $state;
168
	}
169
170
	// Country.
171
	if ( $country = give_get_meta( $object_id, '_give_donor_billing_country', true ) ) {
172
		$donor_data['address']['country'] = $country;
173
	}
174
175
	$meta_value['user_info'] = maybe_unserialize( $donor_data );
176
177
	return $meta_value;
178
}
179
180
/**
181
 * Add backward compatibility old meta while saving.
182
 *  1. _give_payment_meta (split into multiple single meta keys)
183
 *  2. _give_payment_user_email (renamed to _give_payment_donor_email)
184
 *  3. _give_payment_customer_id (renamed to _give_payment_donor_id)
185
 *  4. give_payment_user_ip (renamed to give_payment_donor_ip)
186
 *
187
 * @since 2.0
188
 *
189
 * @param null|bool $check      Whether to allow updating metadata for the given type.
190
 * @param int       $object_id  Object ID.
191
 * @param string    $meta_key   Meta key.
192
 * @param mixed     $meta_value Meta value. Must be serializable if non-scalar.
193
 * @param mixed     $prev_value Optional. If specified, only update existing
194
 *                              metadata entries with the specified value.
195
 *                              Otherwise, update all entries.
196
 *
197
 * @return mixed
198
 */
199
function _give_20_bc_saving_old_payment_meta( $check, $object_id, $meta_key, $meta_value, $prev_value ) {
0 ignored issues
show
Unused Code introduced by
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...
200
	// Bailout.
201
	if ( 'give_payment' !== get_post_type( $object_id ) || empty( $meta_value ) ) {
202
		return $check;
203
	}
204
205
	if ( '_give_payment_meta' === $meta_key ) {
206
		_give_20_bc_split_and_save_give_payment_meta( $object_id, $meta_value );
207
	} elseif ( '_give_payment_user_email' === $meta_key ) {
208
		give_update_meta( $object_id, '_give_payment_donor_email', $meta_value );
209
		$check = true;
210
	} elseif ( '_give_payment_customer_id' === $meta_key ) {
211
		give_update_meta( $object_id, '_give_payment_donor_id', $meta_value );
212
		$check = true;
213
	} elseif ( 'give_payment_user_ip' === $meta_key ) {
214
		give_update_meta( $object_id, '_give_payment_donor_ip', $meta_value );
215
		$check = true;
216
	}
217
218
	return $check;
219
}
220
221
add_filter( 'update_post_metadata', '_give_20_bc_saving_old_payment_meta', 10, 5 );
222
223
224
/**
225
 * Add backward compatibility to get old payment meta.
226
 *
227
 * @since 2.0
228
 *
229
 * @param $check
230
 * @param $object_id
231
 * @param $meta_key
232
 * @param $single
233
 *
234
 * @return mixed
235
 */
236
function _give_20_bc_get_old_payment_meta( $check, $object_id, $meta_key, $single ) {
237
	$old_meta_keys = array(
238
		'_give_payment_meta',
239
		'_give_payment_customer_id',
240
		'_give_payment_user_email',
241
		'_give_payment_user_ip',
242
	);
243
244
	// Bailout.
245
	if (
246
		'give_payment' !== get_post_type( $object_id ) ||
247
		! in_array( $meta_key, $old_meta_keys )
248
	) {
249
		return $check;
250
	}
251
252
	switch ( $meta_key ) {
253
254
		// Handle old meta keys.
255
		case '_give_payment_meta':
256
			remove_filter( 'get_post_metadata', '_give_20_bc_get_old_payment_meta' );
257
258
			// if ( $meta_value = give_get_meta( $object_id, '_give_payment_meta' ) ) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
259
				$meta_value = ! empty( $meta_value ) ? current( $meta_value ) : array();
0 ignored issues
show
Bug introduced by
The variable $meta_value seems only to be defined at a later point. As such the call to empty() seems to always evaluate to true.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
260
				$check      = _give_20_bc_give_payment_meta_value( $object_id, $meta_value );
261
			// }
262
263
			add_filter( 'get_post_metadata', '_give_20_bc_get_old_payment_meta', 10, 5 );
264
265
			break;
266
267
		case '_give_payment_customer_id':
268
			if ( $donor_id = give_get_meta( $object_id, '_give_payment_donor_id', $single ) ) {
269
				$check = $donor_id;
270
			}
271
			break;
272
273
		case '_give_payment_user_email':
274
			if ( $donor_email = give_get_meta( $object_id, '_give_payment_donor_email', $single ) ) {
275
				$check = $donor_email;
276
			}
277
			break;
278
279
		case '_give_payment_user_ip':
280
			if ( $donor_ip = give_get_meta( $object_id, '_give_payment_donor_ip', $single ) ) {
281
				$check = $donor_ip;
282
			}
283
			break;
284
	}// End switch().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
285
286
	// Put result in an array on zero index.
287
	if ( ! is_null( $check ) ) {
288
		$check = array( $check );
289
	}
290
291
	return $check;
292
}
293
294
add_filter( 'get_post_metadata', '_give_20_bc_get_old_payment_meta', 10, 5 );
295
296
297
/**
298
 * Add backward compatibility to get new payment meta.
299
 *
300
 * @since 2.0
301
 *
302
 * @param $check
303
 * @param $object_id
304
 * @param $meta_key
305
 * @param $single
306
 *
307
 * @return mixed
308
 */
309
function _give_20_bc_get_new_payment_meta( $check, $object_id, $meta_key, $single ) {
0 ignored issues
show
Unused Code introduced by
The parameter $single 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...
310
	// Bailout: do not apply backward compatibility if upgrade done.
311
	if ( give_has_upgrade_completed( 'v20_upgrades_form_metadata' ) ) {
312
		return $check;
313
	}
314
315
	global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
316
	$new_meta_keys = array(
317
		'_give_payment_donor_id',
318
		'_give_payment_donor_email',
319
		'_give_payment_donor_ip',
320
		'_give_donor_billing_first_name',
321
		'_give_donor_billing_last_name',
322
		'_give_donor_billing_address1',
323
		'_give_donor_billing_address2',
324
		'_give_donor_billing_city',
325
		'_give_donor_billing_zip',
326
		'_give_donor_billing_state',
327
		'_give_donor_billing_country',
328
		'_give_payment_date',
329
		'_give_payment_currency',
330
	);
331
332
	// metadata_exists fx will cause of firing get_post_metadata filter again so remove it to prevent infinite loop.
333
	remove_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta' );
334
335
	// Bailout.
336
	if (
337
		'give_payment' !== get_post_type( $object_id ) ||
338
		! in_array( $meta_key, $new_meta_keys ) ||
339
		metadata_exists( 'post', $object_id, $meta_key )
340
	) {
341
		add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
342
343
		return $check;
344
	}
345
346
	add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
347
348
	switch ( $meta_key ) {
349
350
		// Handle new meta keys.
351
		case '_give_payment_donor_id':
352
			$check = $wpdb->get_var(
353
				$wpdb->prepare(
354
					"SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id=%s AND meta_key=%s",
355
					$object_id,
356
					'_give_payment_customer_id'
357
				)
358
			);
359
360
			// Set new meta key to save queries.
361
			remove_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10 );
362
			give_update_meta( $object_id, '_give_payment_donor_id', $check );
363
			add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
364
			break;
365
366
		case '_give_payment_donor_email':
367
			$check = $wpdb->get_var(
368
				$wpdb->prepare(
369
					"SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id=%s AND meta_key=%s",
370
					$object_id,
371
					'_give_payment_user_email'
372
				)
373
			);
374
375
			// Set new meta key to save queries.
376
			remove_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10 );
377
			give_update_meta( $object_id, '_give_payment_donor_email', $check );
378
			add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
379
			break;
380
381
		case '_give_payment_donor_ip':
382
			$check = $wpdb->get_var(
383
				$wpdb->prepare(
384
					"SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id=%s AND meta_key=%s",
385
					$object_id,
386
					'_give_payment_user_ip'
387
				)
388
			);
389
390
			// Set new meta key to save queries.
391
			remove_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10 );
392
			give_update_meta( $object_id, '_give_payment_donor_ip', $check );
393
			add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
394
			break;
395
396
		case '_give_donor_billing_first_name':
397
		case '_give_donor_billing_last_name':
398
		case '_give_donor_billing_address1':
399
		case '_give_donor_billing_address2':
400
		case '_give_donor_billing_city':
401
		case '_give_donor_billing_zip':
402
		case '_give_donor_billing_state':
403
		case '_give_donor_billing_country':
404
		case '_give_payment_date':
405
		case '_give_payment_currency':
406
			$donation_meta = $wpdb->get_var(
407
				$wpdb->prepare(
408
					"SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id=%s AND meta_key=%s",
409
					$object_id,
410
					'_give_payment_meta'
411
				)
412
			);
413
			$donation_meta = maybe_unserialize( $donation_meta );
414
			$donation_meta = ! is_array( $donation_meta ) ? array() : $donation_meta;
415
416
			// Break payment meta to new meta keys.
417
			if ( ! empty( $donation_meta ) ) {
418
				remove_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10 );
419
				_give_20_bc_split_and_save_give_payment_meta( $object_id, $donation_meta );
420
				add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
421
			}
422
423
			// Get results.
424
			if ( empty( $donation_meta ) ) {
425
				$check = '';
426
			} elseif ( in_array( $meta_key, array( '_give_payment_date', '_give_payment_currency' ) ) ) {
427
				$meta_key = str_replace( '_give_payment_', '', $meta_key );
428
				if ( isset( $donation_meta[ $meta_key ] ) ) {
429
					$check = $donation_meta[ $meta_key ];
430
				}
431
			} else {
432
				$meta_key = str_replace( '_give_donor_billing_', '', $meta_key );
433
434
				switch ( $meta_key ) {
435
					case 'address1':
436
						if ( isset( $donation_meta['user_info']['address']['line1'] ) ) {
437
							$check = $donation_meta['user_info']['address']['line1'];
438
						}
439
						break;
440
441
					case 'address2':
442
						if ( isset( $donation_meta['user_info']['address']['line2'] ) ) {
443
							$check = $donation_meta['user_info']['address']['line2'];
444
						}
445
						break;
446
447
					default:
448
						if ( isset( $donation_meta['user_info']['address'][ $meta_key ] ) ) {
449
							$check = $donation_meta['user_info']['address'][ $meta_key ];
450
						}
451
				}
452
			}
453
454
			break;
455
	}// End switch().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
456
457
	// Put result in an array on zero index.
458
	if ( ! is_null( $check ) ) {
459
		$check = array( $check );
460
	}
461
462
	return $check;
463
}
464
465
add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
466
467
468
/**
469
 * Add support for old payment meta keys.
470
 *
471
 * @since 2.0
472
 *
473
 * @param WP_Query $query
474
 *
475
 * @return void
476
 */
477
function _give_20_bc_support_deprecated_meta_key_query( $query ) {
478
	$new_meta_keys = array(
479
		'_give_payment_customer_id' => '_give_payment_donor_id',
480
		'_give_payment_user_email'  => '_give_payment_donor_email',
481
		// '_give_payment_user_ip'     => '_give_payment_donor_ip',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
482
	);
483
484
	$deprecated_meta_keys = array_flip( $new_meta_keys );
485
486
	// Set meta keys.
487
	$meta_keys = array();
488
489
490
	// Bailout.
491
	if ( ! empty( $query->query_vars['meta_key'] ) ) {
492
		if ( in_array( $query->query_vars['meta_key'], $new_meta_keys ) ) {
493
			$meta_keys = $deprecated_meta_keys;
494
		} elseif ( in_array( $query->query_vars['meta_key'], $deprecated_meta_keys ) ) {
495
			$meta_keys = $new_meta_keys;
496
		}
497
498
		if ( ! empty( $meta_keys ) ) {
499
			// Set meta_query
500
			$query->set(
501
				'meta_query',
502
				array(
503
					'relation' => 'OR',
504
					array(
505
						'key'   => $query->query_vars['meta_key'],
506
						'value' => $query->query_vars['meta_value'],
507
					),
508
					array(
509
						'key'   => $meta_keys[ $query->query_vars['meta_key'] ],
510
						'value' => $query->query_vars['meta_value'],
511
					),
512
				)
513
			);
514
515
			// Unset single meta query.
516
			unset( $query->query_vars['meta_key'] );
517
			unset( $query->query_vars['meta_value'] );
518
		}
519
	} elseif (
520
		! empty( $query->query_vars['meta_query'] ) &&
521
		( 1 === count( $query->query_vars['meta_query'] ) )
522
	) {
523
		if ( in_array( $query->query_vars['meta_query'][0]['key'], $new_meta_keys ) ) {
524
			$meta_keys = $deprecated_meta_keys;
525
		} elseif ( in_array( $query->query_vars['meta_query'][0]['key'], $deprecated_meta_keys ) ) {
526
			$meta_keys = $new_meta_keys;
527
		} else {
528
			return;
529
		}
530
531
		if ( ! empty( $meta_keys ) ) {
532
			// Set meta_query
533
			$query->set(
534
				'meta_query',
535
				array(
536
					'relation' => 'OR',
537
					array(
538
						'key'   => $query->query_vars['meta_query'][0]['key'],
539
						'value' => $query->query_vars['meta_query'][0]['value'],
540
					),
541
					array(
542
						'key'   => $meta_keys[ $query->query_vars['meta_query'][0]['key'] ],
543
						'value' => $query->query_vars['meta_query'][0]['value'],
544
					),
545
				)
546
			);
547
		}
548
	}
549
}
550
551
add_action( 'pre_get_posts', '_give_20_bc_support_deprecated_meta_key_query' );
552
553
554
/**
555
 * Save payment backward compatibility.
556
 * Note: some addon still can use user_info in set payment meta
557
 *       we will use this infor to set first name, last name and address of donor
558
 * 
559
 * @since 2.0
560
 *
561
 * @param Give_Payment $payment
562
 * @param string $key
563
 */
564
function _give_20_bc_payment_save( $payment, $key ){
565
	switch ( $key ) {
566
		case 'user_info':
567
			if( empty( $payment->user_info ) ) {
568
				// Bailout.
569
				break;
570
			}elseif( is_string( $payment->user_info ) ) {
571
				// Check if value serialize.
572
				$payment->user_info = maybe_unserialize( $payment->user_info );
573
			}
574
575
576
			// Save first name.
577
			if( isset( $payment->user_info['first_name'] ) ) {
578
				$payment->update_meta( '_give_donor_billing_first_name', $payment->user_info['first_name'] );
579
			}
580
581
582
			// Save last name.
583
			if( isset( $payment->user_info['last_name'] ) ) {
584
				$payment->update_meta( '_give_donor_billing_last_name', $payment->user_info['last_name'] );
585
			}
586
587
588
			// Save address.
589
			if( ! empty( $payment->user_info['address'] ) ) {
590
				foreach ( $payment->user_info['address'] as $address_name => $address ) {
591
					switch ( $address_name ) {
592
						case 'line1':
593
							$payment->update_meta( '_give_donor_billing_address1', $address );
594
							break;
595
596
						case 'line2':
597
							$payment->update_meta( '_give_donor_billing_address2', $address );
598
							break;
599
600
						default:
601
							$payment->update_meta( "_give_donor_billing_{$address_name}", $address );
602
					}
603
				}
604
			}
605
606
			break;
607
	}
608
}
609
add_action( 'give_payment_save', '_give_20_bc_payment_save', 10, 2 );
610