Test Failed
Push — issues/1944 ( 3accab...d3ad41 )
by Ravinder
04:09
created

backward-compatibility.php ➔ __give_20_bc_flush_cache()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 2
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
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
	$cache_key = "_give_payment_meta_{$object_id}";
90
	$cache     = Give_Cache::get_db_query( $cache_key );
91
92
	if ( ! is_null( $cache ) ) {
93
		return $cache;
94
	}
95
96
	// Set default value to array.
97
	if ( ! is_array( $meta_value ) ) {
98
		$meta_value = array();
99
	}
100
101
	// Donation key.
102
	$meta_value['key'] = give_get_meta( $object_id, '_give_payment_purchase_key', true );
103
104
	// Donation form.
105
	$meta_value['form_title'] = give_get_meta( $object_id, '_give_payment_form_title', true );
106
107
	// Donor email.
108
	$meta_value['email'] = give_get_meta( $object_id, '_give_payment_donor_email', true );
109
	$meta_value['email'] = ! empty( $meta_value['email'] ) ?
110
		$meta_value['email'] :
111
		Give()->donors->get_column( 'email', give_get_payment_donor_id( $object_id ) );
112
113
	// Form id.
114
	$meta_value['form_id'] = give_get_meta( $object_id, '_give_payment_form_id', true );
115
116
	// Price id.
117
	$meta_value['price_id'] = give_get_meta( $object_id, '_give_payment_price_id', true );
118
119
	// Date.
120
	$meta_value['date'] = give_get_meta( $object_id, '_give_payment_date', true );
121
	$meta_value['date'] = ! empty( $meta_value['date'] ) ?
122
		$meta_value['date'] :
123
		get_post_field( 'post_date', $object_id );
124
125
	// Currency.
126
	$meta_value['currency'] = give_get_meta( $object_id, '_give_payment_currency', true );
127
128
	// Decode donor data.
129
	$donor_names = give_get_donor_name_by( give_get_meta( $object_id, '_give_payment_donor_id', true ), 'donor' );
130
	$donor_names = explode( ' ', $donor_names, 2 );
131
132
	// Donor first name.
133
	$donor_data['first_name'] = give_get_meta( $object_id, '_give_donor_billing_first_name', true );
134
	$donor_data['first_name'] = ! empty( $donor_data['first_name'] ) ?
135
		$donor_data['first_name'] :
136
		$donor_names[0];
137
138
	// Donor last name.
139
	$donor_data['last_name'] = give_get_meta( $object_id, '_give_donor_billing_last_name', true );
140
	$donor_data['last_name'] = ! empty( $donor_data['last_name'] ) ?
141
		$donor_data['last_name'] :
142
		( isset( $donor_names[1] ) ? $donor_names[1] : '' );
143
144
	// Donor email.
145
	$donor_data['email'] = $meta_value['email'];
146
147
	// User ID.
148
	$donor_data['id'] = give_get_payment_user_id( $object_id );
149
150
	$donor_data['address'] = false;
151
152
	// Address1.
153
	if ( $address1 = give_get_meta( $object_id, '_give_donor_billing_address1', true ) ) {
154
		$donor_data['address']['line1'] = $address1;
155
	}
156
157
	// Address2.
158
	if ( $address2 = give_get_meta( $object_id, '_give_donor_billing_address2', true ) ) {
159
		$donor_data['address']['line2'] = $address2;
160
	}
161
162
	// City.
163 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...
164
		$donor_data['address']['city'] = $city;
165
	}
166
167
	// Zip.
168 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...
169
		$donor_data['address']['zip'] = $zip;
170
	}
171
172
	// State.
173
	if ( $state = give_get_meta( $object_id, '_give_donor_billing_state', true ) ) {
174
		$donor_data['address']['state'] = $state;
175
	}
176
177
	// Country.
178
	if ( $country = give_get_meta( $object_id, '_give_donor_billing_country', true ) ) {
179
		$donor_data['address']['country'] = $country;
180
	}
181
182
	$meta_value['user_info'] = maybe_unserialize( $donor_data );
183
184
	Give_Cache::set_db_query( $cache_key, $meta_value );
185
186
	return $meta_value;
187
}
188
189
/**
190
 * Add backward compatibility old meta while saving.
191
 *  1. _give_payment_meta (split into multiple single meta keys)
192
 *  2. _give_payment_user_email (renamed to _give_payment_donor_email)
193
 *  3. _give_payment_customer_id (renamed to _give_payment_donor_id)
194
 *  4. give_payment_user_ip (renamed to give_payment_donor_ip)
195
 *
196
 * @since 2.0
197
 *
198
 * @param null|bool $check      Whether to allow updating metadata for the given type.
199
 * @param int       $object_id  Object ID.
200
 * @param string    $meta_key   Meta key.
201
 * @param mixed     $meta_value Meta value. Must be serializable if non-scalar.
202
 * @param mixed     $prev_value Optional. If specified, only update existing
203
 *                              metadata entries with the specified value.
204
 *                              Otherwise, update all entries.
205
 *
206
 * @return mixed
207
 */
208
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...
209
	// Bailout.
210
	if (
211
		'give_payment' !== get_post_type( $object_id ) ||
212
		! in_array( $meta_key, array(
213
			'_give_payment_meta',
214
			'_give_payment_user_email',
215
			'_give_payment_customer_id',
216
			'give_payment_user_ip',
217
		) )
218
	) {
219
		return $check;
220
	}
221
222
	if ( '_give_payment_meta' === $meta_key ) {
223
		_give_20_bc_split_and_save_give_payment_meta( $object_id, $meta_value );
224
	} elseif ( '_give_payment_user_email' === $meta_key ) {
225
		give_update_meta( $object_id, '_give_payment_donor_email', $meta_value );
226
		$check = true;
227
	} elseif ( '_give_payment_customer_id' === $meta_key ) {
228
		give_update_meta( $object_id, '_give_payment_donor_id', $meta_value );
229
		$check = true;
230
	} elseif ( 'give_payment_user_ip' === $meta_key ) {
231
		give_update_meta( $object_id, '_give_payment_donor_ip', $meta_value );
232
		$check = true;
233
	}
234
235
	return $check;
236
}
237
238
add_filter( 'update_post_metadata', '_give_20_bc_saving_old_payment_meta', 10, 5 );
239
240
241
/**
242
 * Add backward compatibility to get old payment meta.
243
 *
244
 * @since 2.0
245
 *
246
 * @param $check
247
 * @param $object_id
248
 * @param $meta_key
249
 * @param $single
250
 *
251
 * @return mixed
252
 */
253
function _give_20_bc_get_old_payment_meta( $check, $object_id, $meta_key, $single ) {
254
	// Deprecated meta keys.
255
	$old_meta_keys = array(
256
		'_give_payment_customer_id',
257
		'_give_payment_user_email',
258
		'_give_payment_user_ip',
259
	);
260
261
	// Add _give_payment_meta to backward compatibility
262
	if ( ! give_has_upgrade_completed( 'v20_upgrades_payment_metadata' ) ) {
263
		$old_meta_keys[] = '_give_payment_meta';
264
	}
265
266
	// Bailout.
267
	if (
268
		'give_payment' !== get_post_type( $object_id ) ||
269
		! in_array( $meta_key, $old_meta_keys )
270
	) {
271
		return $check;
272
	}
273
274
	$cache_key = "{$meta_key}_{$object_id}";
275
	$cache     = Give_Cache::get_db_query( $cache_key );
276
277
	if ( is_null( $cache ) ) {
278
		switch ( $meta_key ) {
279
280
			// Handle old meta keys.
281
			case '_give_payment_meta':
282
				remove_filter( 'get_post_metadata', '_give_20_bc_get_old_payment_meta' );
283
284
				// if ( $meta_value = give_get_meta( $object_id, '_give_payment_meta' ) ) {
285
				$meta_value = ! empty( $meta_value ) ? current( $meta_value ) : array();
286
				$check      = _give_20_bc_give_payment_meta_value( $object_id, $meta_value );
287
				// }
288
289
				add_filter( 'get_post_metadata', '_give_20_bc_get_old_payment_meta', 10, 5 );
290
291
				break;
292
293
			case '_give_payment_customer_id':
294
				if ( $donor_id = give_get_meta( $object_id, '_give_payment_donor_id', $single ) ) {
295
					$check = $donor_id;
296
				}
297
				break;
298
299
			case '_give_payment_user_email':
300
				if ( $donor_email = give_get_meta( $object_id, '_give_payment_donor_email', $single ) ) {
301
					$check = $donor_email;
302
				}
303
				break;
304
305
			case '_give_payment_user_ip':
306
				if ( $donor_ip = give_get_meta( $object_id, '_give_payment_donor_ip', $single ) ) {
307
					$check = $donor_ip;
308
				}
309
				break;
310
		}// End switch().
311
312
		Give_Cache::set_db_query( $cache_key, $check );
313
	}
314
315
	// Put result in an array on zero index.
316
	if ( ! is_null( $check ) ) {
317
		$check = array( $check );
318
	}
319
320
	return $check;
321
}
322
323
add_filter( 'get_post_metadata', '_give_20_bc_get_old_payment_meta', 10, 5 );
324
325
326
/**
327
 * Add backward compatibility to get new payment meta.
328
 *
329
 * @since 2.0
330
 *
331
 * @param $check
332
 * @param $object_id
333
 * @param $meta_key
334
 * @param $single
335
 *
336
 * @return mixed
337
 */
338
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...
339
	// Bailout: do not apply backward compatibility if upgrade done.
340
	if ( give_has_upgrade_completed( 'v20_upgrades_payment_metadata' ) ) {
341
		return $check;
342
	}
343
344
	global $wpdb;
345
	$new_meta_keys = array(
346
		'_give_payment_donor_id',
347
		'_give_payment_donor_email',
348
		'_give_payment_donor_ip',
349
		'_give_donor_billing_first_name',
350
		'_give_donor_billing_last_name',
351
		'_give_donor_billing_address1',
352
		'_give_donor_billing_address2',
353
		'_give_donor_billing_city',
354
		'_give_donor_billing_zip',
355
		'_give_donor_billing_state',
356
		'_give_donor_billing_country',
357
		'_give_payment_date',
358
		'_give_payment_currency',
359
	);
360
361
	// metadata_exists fx will cause of firing get_post_metadata filter again so remove it to prevent infinite loop.
362
	remove_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta' );
363
364
	// Bailout.
365
	if (
366
		'give_payment' !== get_post_type( $object_id ) ||
367
		! in_array( $meta_key, $new_meta_keys ) ||
368
		metadata_exists( 'post', $object_id, $meta_key )
369
	) {
370
		add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
371
372
		return $check;
373
	}
374
375
	add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
376
377
	$cache_key = "{$meta_key}_{$object_id}";
378
	$cache     = Give_Cache::get_db_query( $cache_key );
379
380
	if ( is_null($cache) ) {
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...
381
		switch ( $meta_key ) {
382
383
			// Handle new meta keys.
384 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...
385
				$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...
386
					$wpdb->prepare(
387
						"SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id=%d AND meta_key=%s",
388
						$object_id,
389
						'_give_payment_customer_id'
390
					)
391
				);
392
393
				// Set new meta key to save queries.
394
				remove_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10 );
395
				give_update_meta( $object_id, '_give_payment_donor_id', $check );
396
				add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
397
				break;
398
399 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...
400
				$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...
401
					$wpdb->prepare(
402
						"SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id=%d AND meta_key=%s",
403
						$object_id,
404
						'_give_payment_user_email'
405
					)
406
				);
407
408
				// Set new meta key to save queries.
409
				remove_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10 );
410
				give_update_meta( $object_id, '_give_payment_donor_email', $check );
411
				add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
412
				break;
413
414 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...
415
				$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...
416
					$wpdb->prepare(
417
						"SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id=%s AND meta_key=%s",
418
						$object_id,
419
						'_give_payment_user_ip'
420
					)
421
				);
422
423
				// Set new meta key to save queries.
424
				remove_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10 );
425
				give_update_meta( $object_id, '_give_payment_donor_ip', $check );
426
				add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
427
				break;
428
429
			case '_give_donor_billing_first_name':
430
			case '_give_donor_billing_last_name':
431
			case '_give_donor_billing_address1':
432
			case '_give_donor_billing_address2':
433
			case '_give_donor_billing_city':
434
			case '_give_donor_billing_zip':
435
			case '_give_donor_billing_state':
436
			case '_give_donor_billing_country':
437
			case '_give_payment_date':
438
			case '_give_payment_currency':
439
				$cache_key = "_give_payment_meta_{$object_id}";
440
				$cache     = Give_Cache::get_db_query( $cache_key );
441
442
				if ( is_null($cache) ) {
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...
443
					$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...
444
						$wpdb->prepare(
445
							"SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id=%d AND meta_key=%s",
446
							$object_id,
447
							'_give_payment_meta'
448
						)
449
					);
450
					$donation_meta = maybe_unserialize( $donation_meta );
451
					$donation_meta = ! is_array( $donation_meta ) ? array() : $donation_meta;
452
					Give_Cache::set_db_query( $cache_key, $donation_meta );
453
				}
454
455
				// Get results.
456
				if ( empty( $donation_meta ) ) {
457
					$check = '';
458
				} elseif ( in_array( $meta_key, array( '_give_payment_date', '_give_payment_currency' ) ) ) {
459
					$meta_key = str_replace( '_give_payment_', '', $meta_key );
460
					if ( isset( $donation_meta[ $meta_key ] ) ) {
461
						$check = $donation_meta[ $meta_key ];
462
					}
463
				} else {
464
					$meta_key = str_replace( '_give_donor_billing_', '', $meta_key );
465
466
					switch ( $meta_key ) {
467 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...
468
							if ( isset( $donation_meta['user_info']['address']['line1'] ) ) {
469
								$check = $donation_meta['user_info']['address']['line1'];
470
							}
471
							break;
472
473 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...
474
							if ( isset( $donation_meta['user_info']['address']['line2'] ) ) {
475
								$check = $donation_meta['user_info']['address']['line2'];
476
							}
477
							break;
478
479
						default:
480
							if ( isset( $donation_meta['user_info']['address'][ $meta_key ] ) ) {
481
								$check = $donation_meta['user_info']['address'][ $meta_key ];
482
							}
483
					}
484
				}
485
486
				break;
487
		}// End switch().
488
489
		// Set cache.
490
		Give_Cache::set_db_query( $cache_key, $check );
491
	}
492
493
	// Put result in an array on zero index.
494
	if ( ! is_null( $check ) ) {
495
		$check = array( $check );
496
	}
497
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 3 empty lines
Loading history...
498
499
500
	return $check;
501
}
502
503
add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
504
505
506
/**
507
 * Add support for old payment meta keys.
508
 *
509
 * @since 2.0
510
 *
511
 * @param WP_Query $query
512
 *
513
 * @return void
514
 */
515
function _give_20_bc_support_deprecated_meta_key_query( $query ) {
516
	// Bailout.
517
	if ( give_has_upgrade_completed( 'v20_upgrades_payment_metadata' ) ) {
518
		return;
519
	}
520
521
	$new_meta_keys = array(
522
		'_give_payment_customer_id' => '_give_payment_donor_id',
523
		'_give_payment_user_email'  => '_give_payment_donor_email',
524
		// '_give_payment_user_ip'     => '_give_payment_donor_ip',
525
	);
526
527
	$deprecated_meta_keys = array_flip( $new_meta_keys );
528
529
	// Set meta keys.
530
	$meta_keys = array();
531
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
532
533
	// Bailout.
534
	if ( ! empty( $query->query_vars['meta_key'] ) ) {
535
		if ( in_array( $query->query_vars['meta_key'], $new_meta_keys ) ) {
536
			$meta_keys = $deprecated_meta_keys;
537
		} elseif ( in_array( $query->query_vars['meta_key'], $deprecated_meta_keys ) ) {
538
			$meta_keys = $new_meta_keys;
539
		}
540
541
		if ( ! empty( $meta_keys ) ) {
542
			// Set meta_query
543
			$query->set(
544
				'meta_query',
545
				array(
546
					'relation' => 'OR',
547
					array(
548
						'key'   => $query->query_vars['meta_key'],
549
						'value' => $query->query_vars['meta_value'],
550
					),
551
					array(
552
						'key'   => $meta_keys[ $query->query_vars['meta_key'] ],
553
						'value' => $query->query_vars['meta_value'],
554
					),
555
				)
556
			);
557
558
			// Unset single meta query.
559
			unset( $query->query_vars['meta_key'] );
560
			unset( $query->query_vars['meta_value'] );
561
		}
562
	} elseif (
563
		! empty( $query->query_vars['meta_query'] ) &&
564
		( 1 === count( $query->query_vars['meta_query'] ) )
565
	) {
566
		if ( in_array( $query->query_vars['meta_query'][0]['key'], $new_meta_keys ) ) {
567
			$meta_keys = $deprecated_meta_keys;
568
		} elseif ( in_array( $query->query_vars['meta_query'][0]['key'], $deprecated_meta_keys ) ) {
569
			$meta_keys = $new_meta_keys;
570
		} else {
571
			return;
572
		}
573
574
		if ( ! empty( $meta_keys ) ) {
575
			// Set meta_query
576
			$query->set(
577
				'meta_query',
578
				array(
579
					'relation' => 'OR',
580
					array(
581
						'key'   => $query->query_vars['meta_query'][0]['key'],
582
						'value' => $query->query_vars['meta_query'][0]['value'],
583
					),
584
					array(
585
						'key'   => $meta_keys[ $query->query_vars['meta_query'][0]['key'] ],
586
						'value' => $query->query_vars['meta_query'][0]['value'],
587
					),
588
				)
589
			);
590
		}
591
	}
592
}
593
594
add_action( 'pre_get_posts', '_give_20_bc_support_deprecated_meta_key_query' );
595
596
597
/**
598
 * Save payment backward compatibility.
599
 * Note: some addon still can use user_info in set payment meta
600
 *       we will use this info to set first name, last name and address of donor
601
 *
602
 * @since 2.0
603
 *
604
 * @param Give_Payment $payment
605
 * @param string       $key
606
 */
607
function _give_20_bc_payment_save( $payment, $key ) {
608
	// Bailout.
609
	if ( ! give_has_upgrade_completed( 'v20_upgrades_payment_metadata' ) ) {
610
		return;
611
	}
612
613
	switch ( $key ) {
614
		case 'user_info':
615
			if ( empty( $payment->user_info ) ) {
616
				// Bailout.
617
				break;
618
			} elseif ( is_string( $payment->user_info ) ) {
619
				// Check if value serialize.
620
				$payment->user_info = maybe_unserialize( $payment->user_info );
621
			}
622
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
623
624
			// Save first name.
625
			if ( isset( $payment->user_info['first_name'] ) ) {
626
				$payment->update_meta( '_give_donor_billing_first_name', $payment->user_info['first_name'] );
627
			}
628
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
629
630
			// Save last name.
631
			if ( isset( $payment->user_info['last_name'] ) ) {
632
				$payment->update_meta( '_give_donor_billing_last_name', $payment->user_info['last_name'] );
633
			}
634
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
635
636
			// Save address.
637
			if ( ! empty( $payment->user_info['address'] ) ) {
638
				foreach ( $payment->user_info['address'] as $address_name => $address ) {
639
					switch ( $address_name ) {
640
						case 'line1':
641
							$payment->update_meta( '_give_donor_billing_address1', $address );
642
							break;
643
644
						case 'line2':
645
							$payment->update_meta( '_give_donor_billing_address2', $address );
646
							break;
647
648
						default:
649
							$payment->update_meta( "_give_donor_billing_{$address_name}", $address );
650
					}
651
				}
652
			}
653
654
			break;
655
	}
656
}
657
658
add_action( 'give_payment_save', '_give_20_bc_payment_save', 10, 2 );
659
660
661
/**
662
 * Delete pre upgrade cache for donations.
663
 *
664
 * @since 2.0
665
 *
666
 * @param $check
667
 * @param $object_id
668
 *
669
 * @return mixed
670
 */
671
function __give_20_bc_flush_cache( $check, $object_id ) {
672
	if (
673
		! give_has_upgrade_completed( 'v20_move_metadata_into_new_table' ) &&
674
		'give_payment' === get_post_type( $object_id )
675
	) {
676
		Give_Cache::delete_group( $object_id, 'give-donations' );
677
	}
678
679
	return $check;
680
}
681
682
add_action( 'update_postmeta', '__give_20_bc_flush_cache', 9999, 2 );
683