Test Failed
Push — backup/issues/1132 ( b1d18b )
by Ravinder
05:29
created

backward-compatibility.php ➔ _give_20_bc_payment_save()   C

Complexity

Conditions 11
Paths 19

Size

Total Lines 50
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 25
nc 19
nop 2
dl 0
loc 50
rs 5.4893
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
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 View Code Duplication
			if ( ! empty( $meta_value['user_info']['address']['line1'] ) ) {
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...
47
				give_update_meta( $object_id, '_give_donor_billing_address1', $meta_value['user_info']['address']['line1'] );
48
			}
49
50
			// Address2.
51 View Code Duplication
			if ( ! empty( $meta_value['user_info']['address']['line2'] ) ) {
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...
52
				give_update_meta( $object_id, '_give_donor_billing_address2', $meta_value['user_info']['address']['line2'] );
53
			}
54
55
			// City.
56 View Code Duplication
			if ( ! empty( $meta_value['user_info']['address']['city'] ) ) {
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...
57
				give_update_meta( $object_id, '_give_donor_billing_city', $meta_value['user_info']['address']['city'] );
58
			}
59
60
			// Zip.
61 View Code Duplication
			if ( ! empty( $meta_value['user_info']['address']['zip'] ) ) {
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...
62
				give_update_meta( $object_id, '_give_donor_billing_zip', $meta_value['user_info']['address']['zip'] );
63
			}
64
65
			// State.
66 View Code Duplication
			if ( ! empty( $meta_value['user_info']['address']['state'] ) ) {
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...
67
				give_update_meta( $object_id, '_give_donor_billing_state', $meta_value['user_info']['address']['state'] );
68
			}
69
70
			// Country.
71 View Code Duplication
			if ( ! empty( $meta_value['user_info']['address']['country'] ) ) {
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...
72
				give_update_meta( $object_id, '_give_donor_billing_country', $meta_value['user_info']['address']['country'] );
73
			}
74
		}
75
	}// End if().
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 );
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 View Code Duplication
	if ( $city = give_get_meta( $object_id, '_give_donor_billing_city', 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...
157
		$donor_data['address']['city'] = $city;
158
	}
159
160
	// Zip.
161 View Code Duplication
	if ( $zip = give_get_meta( $object_id, '_give_donor_billing_zip', 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...
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 (
202
		'give_payment' !== get_post_type( $object_id ) ||
203
		! in_array( $meta_key, array(
204
			'_give_payment_meta',
205
			'_give_payment_user_email',
206
			'_give_payment_customer_id',
207
			'give_payment_user_ip',
208
		) )
209
	) {
210
		return $check;
211
	}
212
213
	if ( '_give_payment_meta' === $meta_key ) {
214
		_give_20_bc_split_and_save_give_payment_meta( $object_id, $meta_value );
215
	} elseif ( '_give_payment_user_email' === $meta_key ) {
216
		give_update_meta( $object_id, '_give_payment_donor_email', $meta_value );
217
		$check = true;
218
	} elseif ( '_give_payment_customer_id' === $meta_key ) {
219
		give_update_meta( $object_id, '_give_payment_donor_id', $meta_value );
220
		$check = true;
221
	} elseif ( 'give_payment_user_ip' === $meta_key ) {
222
		give_update_meta( $object_id, '_give_payment_donor_ip', $meta_value );
223
		$check = true;
224
	}
225
226
	return $check;
227
}
228
229
add_filter( 'update_post_metadata', '_give_20_bc_saving_old_payment_meta', 10, 5 );
230
231
232
/**
233
 * Add backward compatibility to get old payment meta.
234
 *
235
 * @since 2.0
236
 *
237
 * @param $check
238
 * @param $object_id
239
 * @param $meta_key
240
 * @param $single
241
 *
242
 * @return mixed
243
 */
244
function _give_20_bc_get_old_payment_meta( $check, $object_id, $meta_key, $single ) {
245
	// Deprecated meta keys.
246
	$old_meta_keys = array(
247
		'_give_payment_customer_id',
248
		'_give_payment_user_email',
249
		'_give_payment_user_ip',
250
	);
251
252
	// Add _give_payment_meta to backward compatibility
253
	if( ! give_has_upgrade_completed( 'v20_upgrades_payment_metadata' ) ) {
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...
254
		$old_meta_keys[] = '_give_payment_meta';
255
	}
256
257
	// Bailout.
258
	if (
259
		'give_payment' !== get_post_type( $object_id ) ||
260
		! in_array( $meta_key, $old_meta_keys )
261
	) {
262
		return $check;
263
	}
264
265
	switch ( $meta_key ) {
266
267
		// Handle old meta keys.
268
		case '_give_payment_meta':
269
			remove_filter( 'get_post_metadata', '_give_20_bc_get_old_payment_meta' );
270
271
			// if ( $meta_value = give_get_meta( $object_id, '_give_payment_meta' ) ) {
272
				$meta_value = ! empty( $meta_value ) ? current( $meta_value ) : array();
273
				$check      = _give_20_bc_give_payment_meta_value( $object_id, $meta_value );
274
			// }
275
276
			add_filter( 'get_post_metadata', '_give_20_bc_get_old_payment_meta', 10, 5 );
277
278
			break;
279
280
		case '_give_payment_customer_id':
281
			if ( $donor_id = give_get_meta( $object_id, '_give_payment_donor_id', $single ) ) {
282
				$check = $donor_id;
283
			}
284
			break;
285
286
		case '_give_payment_user_email':
287
			if ( $donor_email = give_get_meta( $object_id, '_give_payment_donor_email', $single ) ) {
288
				$check = $donor_email;
289
			}
290
			break;
291
292
		case '_give_payment_user_ip':
293
			if ( $donor_ip = give_get_meta( $object_id, '_give_payment_donor_ip', $single ) ) {
294
				$check = $donor_ip;
295
			}
296
			break;
297
	}// End switch().
298
299
	// Put result in an array on zero index.
300
	if ( ! is_null( $check ) ) {
301
		$check = array( $check );
302
	}
303
304
	return $check;
305
}
306
307
add_filter( 'get_post_metadata', '_give_20_bc_get_old_payment_meta', 10, 5 );
308
309
310
/**
311
 * Add backward compatibility to get new payment meta.
312
 *
313
 * @since 2.0
314
 *
315
 * @param $check
316
 * @param $object_id
317
 * @param $meta_key
318
 * @param $single
319
 *
320
 * @return mixed
321
 */
322
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...
323
	// Bailout: do not apply backward compatibility if upgrade done.
324
	if ( give_has_upgrade_completed( 'v20_upgrades_payment_metadata' ) ) {
325
		return $check;
326
	}
327
	
328
	global $wpdb;
329
	$new_meta_keys = array(
330
		'_give_payment_donor_id',
331
		'_give_payment_donor_email',
332
		'_give_payment_donor_ip',
333
		'_give_donor_billing_first_name',
334
		'_give_donor_billing_last_name',
335
		'_give_donor_billing_address1',
336
		'_give_donor_billing_address2',
337
		'_give_donor_billing_city',
338
		'_give_donor_billing_zip',
339
		'_give_donor_billing_state',
340
		'_give_donor_billing_country',
341
		'_give_payment_date',
342
		'_give_payment_currency',
343
	);
344
345
	// metadata_exists fx will cause of firing get_post_metadata filter again so remove it to prevent infinite loop.
346
	remove_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta' );
347
348
	// Bailout.
349
	if (
350
		'give_payment' !== get_post_type( $object_id ) ||
351
		! in_array( $meta_key, $new_meta_keys ) ||
352
		metadata_exists( 'post', $object_id, $meta_key )
353
	) {
354
		add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
355
356
		return $check;
357
	}
358
359
	add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
360
361
	switch ( $meta_key ) {
362
363
		// Handle new meta keys.
364 View Code Duplication
		case '_give_payment_donor_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...
365
			$check = $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...
366
				$wpdb->prepare(
367
					"SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id=%d AND meta_key=%s",
368
					$object_id,
369
					'_give_payment_customer_id'
370
				)
371
			);
372
373
			// Set new meta key to save queries.
374
			remove_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10 );
375
			give_update_meta( $object_id, '_give_payment_donor_id', $check );
376
			add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
377
			break;
378
379 View Code Duplication
		case '_give_payment_donor_email':
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...
380
			$check = $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...
381
				$wpdb->prepare(
382
					"SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id=%d AND meta_key=%s",
383
					$object_id,
384
					'_give_payment_user_email'
385
				)
386
			);
387
388
			// Set new meta key to save queries.
389
			remove_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10 );
390
			give_update_meta( $object_id, '_give_payment_donor_email', $check );
391
			add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
392
			break;
393
394 View Code Duplication
		case '_give_payment_donor_ip':
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...
395
			$check = $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...
396
				$wpdb->prepare(
397
					"SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id=%s AND meta_key=%s",
398
					$object_id,
399
					'_give_payment_user_ip'
400
				)
401
			);
402
403
			// Set new meta key to save queries.
404
			remove_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10 );
405
			give_update_meta( $object_id, '_give_payment_donor_ip', $check );
406
			add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
407
			break;
408
409
		case '_give_donor_billing_first_name':
410
		case '_give_donor_billing_last_name':
411
		case '_give_donor_billing_address1':
412
		case '_give_donor_billing_address2':
413
		case '_give_donor_billing_city':
414
		case '_give_donor_billing_zip':
415
		case '_give_donor_billing_state':
416
		case '_give_donor_billing_country':
417
		case '_give_payment_date':
418
		case '_give_payment_currency':
419
			$donation_meta = $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...
420
				$wpdb->prepare(
421
					"SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id=%d AND meta_key=%s",
422
					$object_id,
423
					'_give_payment_meta'
424
				)
425
			);
426
			$donation_meta = maybe_unserialize( $donation_meta );
427
			$donation_meta = ! is_array( $donation_meta ) ? array() : $donation_meta;
428
429
			// Break payment meta to new meta keys.
430
			if ( ! empty( $donation_meta ) ) {
431
				remove_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10 );
432
				_give_20_bc_split_and_save_give_payment_meta( $object_id, $donation_meta );
433
				add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
434
			}
435
436
			// Get results.
437
			if ( empty( $donation_meta ) ) {
438
				$check = '';
439
			} elseif ( in_array( $meta_key, array( '_give_payment_date', '_give_payment_currency' ) ) ) {
440
				$meta_key = str_replace( '_give_payment_', '', $meta_key );
441
				if ( isset( $donation_meta[ $meta_key ] ) ) {
442
					$check = $donation_meta[ $meta_key ];
443
				}
444
			} else {
445
				$meta_key = str_replace( '_give_donor_billing_', '', $meta_key );
446
447
				switch ( $meta_key ) {
448 View Code Duplication
					case 'address1':
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...
449
						if ( isset( $donation_meta['user_info']['address']['line1'] ) ) {
450
							$check = $donation_meta['user_info']['address']['line1'];
451
						}
452
						break;
453
454 View Code Duplication
					case 'address2':
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...
455
						if ( isset( $donation_meta['user_info']['address']['line2'] ) ) {
456
							$check = $donation_meta['user_info']['address']['line2'];
457
						}
458
						break;
459
460
					default:
461
						if ( isset( $donation_meta['user_info']['address'][ $meta_key ] ) ) {
462
							$check = $donation_meta['user_info']['address'][ $meta_key ];
463
						}
464
				}
465
			}
466
467
			break;
468
	}// End switch().
469
470
	// Put result in an array on zero index.
471
	if ( ! is_null( $check ) ) {
472
		$check = array( $check );
473
	}
474
475
	return $check;
476
}
477
478
add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
479
480
481
/**
482
 * Add support for old payment meta keys.
483
 *
484
 * @since 2.0
485
 *
486
 * @param WP_Query $query
487
 *
488
 * @return void
489
 */
490
function _give_20_bc_support_deprecated_meta_key_query( $query ) {
491
	// Bailout.
492
	if( give_has_upgrade_completed( 'v20_upgrades_payment_metadata' ) ) {
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...
493
		return;
494
	}
495
496
	$new_meta_keys = array(
497
		'_give_payment_customer_id' => '_give_payment_donor_id',
498
		'_give_payment_user_email'  => '_give_payment_donor_email',
499
		// '_give_payment_user_ip'     => '_give_payment_donor_ip',
500
	);
501
502
	$deprecated_meta_keys = array_flip( $new_meta_keys );
503
504
	// Set meta keys.
505
	$meta_keys = array();
506
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
507
508
	// Bailout.
509
	if ( ! empty( $query->query_vars['meta_key'] ) ) {
510
		if ( in_array( $query->query_vars['meta_key'], $new_meta_keys ) ) {
511
			$meta_keys = $deprecated_meta_keys;
512
		} elseif ( in_array( $query->query_vars['meta_key'], $deprecated_meta_keys ) ) {
513
			$meta_keys = $new_meta_keys;
514
		}
515
516
		if ( ! empty( $meta_keys ) ) {
517
			// Set meta_query
518
			$query->set(
519
				'meta_query',
520
				array(
521
					'relation' => 'OR',
522
					array(
523
						'key'   => $query->query_vars['meta_key'],
524
						'value' => $query->query_vars['meta_value'],
525
					),
526
					array(
527
						'key'   => $meta_keys[ $query->query_vars['meta_key'] ],
528
						'value' => $query->query_vars['meta_value'],
529
					),
530
				)
531
			);
532
533
			// Unset single meta query.
534
			unset( $query->query_vars['meta_key'] );
535
			unset( $query->query_vars['meta_value'] );
536
		}
537
	} elseif (
538
		! empty( $query->query_vars['meta_query'] ) &&
539
		( 1 === count( $query->query_vars['meta_query'] ) )
540
	) {
541
		if ( in_array( $query->query_vars['meta_query'][0]['key'], $new_meta_keys ) ) {
542
			$meta_keys = $deprecated_meta_keys;
543
		} elseif ( in_array( $query->query_vars['meta_query'][0]['key'], $deprecated_meta_keys ) ) {
544
			$meta_keys = $new_meta_keys;
545
		} else {
546
			return;
547
		}
548
549
		if ( ! empty( $meta_keys ) ) {
550
			// Set meta_query
551
			$query->set(
552
				'meta_query',
553
				array(
554
					'relation' => 'OR',
555
					array(
556
						'key'   => $query->query_vars['meta_query'][0]['key'],
557
						'value' => $query->query_vars['meta_query'][0]['value'],
558
					),
559
					array(
560
						'key'   => $meta_keys[ $query->query_vars['meta_query'][0]['key'] ],
561
						'value' => $query->query_vars['meta_query'][0]['value'],
562
					),
563
				)
564
			);
565
		}
566
	}
567
}
568
569
add_action( 'pre_get_posts', '_give_20_bc_support_deprecated_meta_key_query' );
570
571
572
/**
573
 * Save payment backward compatibility.
574
 * Note: some addon still can use user_info in set payment meta
575
 *       we will use this info to set first name, last name and address of donor
576
 * 
577
 * @since 2.0
578
 *
579
 * @param Give_Payment $payment
580
 * @param string $key
581
 */
582
function _give_20_bc_payment_save( $payment, $key ){
583
	// Bailout.
584
	if( ! give_has_upgrade_completed( 'v20_upgrades_payment_metadata' ) ) {
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...
585
		return;
586
	}
587
588
	switch ( $key ) {
589
		case 'user_info':
590
			if( empty( $payment->user_info ) ) {
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...
591
				// Bailout.
592
				break;
593
			}elseif( is_string( $payment->user_info ) ) {
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...
594
				// Check if value serialize.
595
				$payment->user_info = maybe_unserialize( $payment->user_info );
596
			}
597
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
598
599
			// Save first name.
600
			if( isset( $payment->user_info['first_name'] ) ) {
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...
601
				$payment->update_meta( '_give_donor_billing_first_name', $payment->user_info['first_name'] );
602
			}
603
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
604
605
			// Save last name.
606
			if( isset( $payment->user_info['last_name'] ) ) {
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...
607
				$payment->update_meta( '_give_donor_billing_last_name', $payment->user_info['last_name'] );
608
			}
609
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
610
611
			// Save address.
612
			if( ! empty( $payment->user_info['address'] ) ) {
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...
613
				foreach ( $payment->user_info['address'] as $address_name => $address ) {
614
					switch ( $address_name ) {
615
						case 'line1':
616
							$payment->update_meta( '_give_donor_billing_address1', $address );
617
							break;
618
619
						case 'line2':
620
							$payment->update_meta( '_give_donor_billing_address2', $address );
621
							break;
622
623
						default:
624
							$payment->update_meta( "_give_donor_billing_{$address_name}", $address );
625
					}
626
				}
627
			}
628
629
			break;
630
	}
631
}
632
add_action( 'give_payment_save', '_give_20_bc_payment_save', 10, 2 );
633