Completed
Push — issues/1132 ( 362bb5...578281 )
by Ravinder
16:46
created

backward-compatibility.php ➔ _give_20_bc_delete_cache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
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 206.

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
 *
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
103
	// Form id.
104
	$meta_value['form_id'] = give_get_meta( $object_id, '_give_payment_form_id', true );
105
106
	// Price id.
107
	$meta_value['price_id'] = give_get_meta( $object_id, '_give_payment_price_id', true );
108
109
	// Date.
110
	$meta_value['date'] = give_get_meta( $object_id, '_give_payment_date', true );
111
112
	// Currency.
113
	$meta_value['currency'] = give_get_meta( $object_id, '_give_payment_currency', true );
114
115
	// Decode donor data.
116
117
	// Donor first name.
118
	$donor_data['first_name'] = give_get_meta( $object_id, '_give_donor_billing_first_name', true, isset( $donor_data['first_name'] ) ? $donor_data['first_name'] : '' );
0 ignored issues
show
Bug introduced by
The variable $donor_data does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
119
120
	// Donor last name.
121
	$donor_data['last_name'] = give_get_meta( $object_id, '_give_donor_billing_last_name', true, isset( $donor_data['last_name'] ) ? $donor_data['last_name'] : '' );
122
123
	$donor_data['email'] = $meta_value['email'];
124
125
	// User ID.
126
	$donor_data['id'] = give_get_payment_user_id( $object_id );
127
128
	$donor_data['address'] = false;
129
130
	// Address1.
131
	if( $address1 = give_get_meta( $object_id, '_give_donor_billing_address1', true ) ) {
132
		$donor_data['address']['line1'] = $address1;
133
	}
134
135
	// Address2.
136
	if( $address2 = give_get_meta( $object_id, '_give_donor_billing_address2', true ) ) {
137
		$donor_data['address']['line2'] = $address2;
138
	}
139
140
	// City.
141
	if( $city = give_get_meta( $object_id, '_give_donor_billing_city', true ) ) {
142
		$donor_data['address']['city'] = $city;
143
	}
144
145
	// Zip.
146
	if( $zip = give_get_meta( $object_id, '_give_donor_billing_zip', true ) ) {
147
		$donor_data['address']['zip'] = $zip;
148
	}
149
150
	// State.
151
	if( $state = give_get_meta( $object_id, '_give_donor_billing_state', true ) ) {
152
		$donor_data['address']['state'] = $state;
153
	}
154
155
	// Country.
156
	if( $country = give_get_meta( $object_id, '_give_donor_billing_country', true ) ) {
157
		$donor_data['address']['country'] = $country;
158
	}
159
160
	$meta_value['user_info'] = maybe_unserialize( $donor_data );
161
162
	return $meta_value;
163
}
164
165
/**
166
 * Add backward compatibility old meta while saving.
167
 *  1. _give_payment_meta (split into multiple single meta keys)
168
 *  2. _give_payment_user_email (renamed to _give_payment_donor_email)
169
 *  3. _give_payment_customer_id (renamed to _give_payment_donor_id)
170
 *  4. give_payment_user_ip (renamed to give_payment_donor_ip)
171
 *
172
 * @since 2.0
173
 *
174
 * @param null|bool $check      Whether to allow updating metadata for the given type.
175
 * @param int       $object_id  Object ID.
176
 * @param string    $meta_key   Meta key.
177
 * @param mixed     $meta_value Meta value. Must be serializable if non-scalar.
178
 * @param mixed     $prev_value Optional. If specified, only update existing
179
 *                              metadata entries with the specified value.
180
 *                              Otherwise, update all entries.
181
 *
182
 * @return mixed
183
 */
184
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...
185
	// Bailout.
186
	if ( 'give_payment' !== get_post_type( $object_id ) || empty( $meta_value ) ) {
187
		return $check;
188
	}
189
190
	if ( '_give_payment_meta' === $meta_key ) {
191
		_give_20_bc_split_and_save_give_payment_meta( $object_id, $meta_value );
192
	} elseif ( '_give_payment_user_email' === $meta_key ) {
193
		give_update_meta( $object_id, '_give_payment_donor_email', $meta_value );
194
		$check = true;
195
	} elseif ( '_give_payment_customer_id' === $meta_key ) {
196
		give_update_meta( $object_id, '_give_payment_donor_id', $meta_value );
197
		$check = true;
198
	} elseif ( 'give_payment_user_ip' === $meta_key ) {
199
		give_update_meta( $object_id, '_give_payment_donor_ip', $meta_value );
200
		$check = true;
201
	}
202
203
	return $check;
204
}
205
206
add_filter( 'update_post_metadata', '_give_20_bc_saving_old_payment_meta', 10, 5 );
207
208
209
/**
210
 * Add backward compatibility to get old payment meta.
211
 *
212
 * @since 2.0
213
 *
214
 * @param $check
215
 * @param $object_id
216
 * @param $meta_key
217
 * @param $single
218
 *
219
 * @return mixed
220
 */
221
function _give_20_bc_get_old_payment_meta( $check, $object_id, $meta_key, $single ) {
222
	$old_meta_keys = array(
223
		'_give_payment_meta',
224
		'_give_payment_customer_id',
225
		'_give_payment_user_email',
226
		'_give_payment_user_ip',
227
	);
228
229
	// Bailout.
230
	if (
231
		'give_payment' !== get_post_type( $object_id ) ||
232
		! in_array( $meta_key, $old_meta_keys )
233
	) {
234
		return $check;
235
	}
236
237
	switch ( $meta_key ) {
238
239
		// Handle old meta keys.
240
		case '_give_payment_meta':
241
			remove_filter( 'get_post_metadata', '_give_20_bc_get_old_payment_meta' );
242
243
			if (
244
				! ( $check = Give_Cache::get( "give_20_bc_give_payment_meta_{$object_id}", true ) ) &&
245
				( $meta_value = give_get_meta( $object_id, '_give_payment_meta' ) )
246
			) {
247
				$check = _give_20_bc_give_payment_meta_value( $object_id, current( $meta_value ) );
248
249
				// Set cache to save queries.
250
				Give_Cache::set( "give_20_bc_give_payment_meta_{$object_id}", $check, HOUR_IN_SECONDS, true );
251
			}
252
			
253
			add_filter( 'get_post_metadata', '_give_20_bc_get_old_payment_meta', 10, 5 );
254
255
			break;
256
257
		case '_give_payment_customer_id':
258
			if ( $donor_id = give_get_meta( $object_id, '_give_payment_donor_id', $single ) ) {
259
				$check = $donor_id;
260
			}
261
			break;
262
263
		case '_give_payment_user_email':
264
			if ( $donor_email = give_get_meta( $object_id, '_give_payment_donor_email', $single ) ) {
265
				$check = $donor_email;
266
			}
267
			break;
268
269
		case '_give_payment_user_ip':
270
			if ( $donor_ip = give_get_meta( $object_id, '_give_payment_donor_ip', $single ) ) {
271
				$check = $donor_ip;
272
			}
273
			break;
274
	}// 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...
275
276
	// Put result in an array on zero index.
277
	if ( ! is_null( $check ) ) {
278
		$check = array( $check );
279
	}
280
281
	return $check;
282
}
283
284
add_filter( 'get_post_metadata', '_give_20_bc_get_old_payment_meta', 10, 5 );
285
286
287
/**
288
 * Add backward compatibility to get new payment meta.
289
 *
290
 * @since 2.0
291
 *
292
 * @param $check
293
 * @param $object_id
294
 * @param $meta_key
295
 * @param $single
296
 *
297
 * @return mixed
298
 */
299
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...
300
	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...
301
	$new_meta_keys = array(
302
		'_give_payment_donor_id',
303
		'_give_payment_donor_email',
304
		'_give_payment_donor_ip',
305
		'_give_donor_billing_first_name',
306
		'_give_donor_billing_last_name',
307
		'_give_donor_billing_address1',
308
		'_give_donor_billing_address2',
309
		'_give_donor_billing_city',
310
		'_give_donor_billing_zip',
311
		'_give_donor_billing_state',
312
		'_give_donor_billing_country',
313
		'_give_payment_date',
314
		'_give_payment_currency',
315
	);
316
317
	// metadata_exists fx will cause of firing get_post_metadata filter again so remove it to prevent infinite loop.
318
	remove_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta' );
319
320
	// Bailout.
321
	if (
322
		'give_payment' !== get_post_type( $object_id ) ||
323
		! in_array( $meta_key, $new_meta_keys ) ||
324
		metadata_exists( 'post', $object_id, $meta_key )
325
	) {
326
		add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
327
328
		return $check;
329
	}
330
331
	add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
332
333
	switch ( $meta_key ) {
334
335
		// Handle new meta keys.
336
		case '_give_payment_donor_id':
337
			$check = $wpdb->get_var(
338
				$wpdb->prepare(
339
					"SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id=%s AND meta_key=%s",
340
					$object_id,
341
					'_give_payment_customer_id'
342
				)
343
			);
344
345
			// Set new meta key to save queries.
346
			remove_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10 );
347
			give_update_meta( $object_id, '_give_payment_donor_id', $check );
348
			add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
349
			break;
350
351
		case '_give_payment_donor_email':
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_user_email'
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_email', $check );
363
			add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
364
			break;
365
366
		case '_give_payment_donor_ip':
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_ip'
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_ip', $check );
378
			add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
379
			break;
380
381
		case '_give_donor_billing_first_name':
382
		case '_give_donor_billing_last_name':
383
		case '_give_donor_billing_address1':
384
		case '_give_donor_billing_address2':
385
		case '_give_donor_billing_city':
386
		case '_give_donor_billing_zip':
387
		case '_give_donor_billing_state':
388
		case '_give_donor_billing_country':
389
		case '_give_payment_date':
390
		case '_give_payment_currency':
391
			$donation_meta = $wpdb->get_var(
392
				$wpdb->prepare(
393
					"SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id=%s AND meta_key=%s",
394
					$object_id,
395
					'_give_payment_meta'
396
				)
397
			);
398
			$donation_meta = maybe_unserialize( $donation_meta );
399
			$donation_meta = ! is_array( $donation_meta ) ? array() : $donation_meta;
400
401
			// Break payment meta to new meta keys.
402
			if ( ! empty( $donation_meta ) ) {
403
				remove_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10 );
404
				_give_20_bc_split_and_save_give_payment_meta( $object_id, $donation_meta );
405
				add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
406
			}
407
408
			// Get results.
409
			if ( empty( $donation_meta ) ) {
410
				$check = '';
411
			} elseif ( in_array( $meta_key, array( '_give_payment_date', '_give_payment_currency' ) ) ) {
412
				$meta_key = str_replace( '_give_payment_', '', $meta_key );
413
				if ( isset( $donation_meta[ $meta_key ] ) ) {
414
					$check = $donation_meta[ $meta_key ];
415
				}
416
			} else {
417
				$meta_key = str_replace( '_give_donor_billing_', '', $meta_key );
418
419
				switch ( $meta_key ) {
420
					case 'address1':
421
						if ( isset( $donation_meta['user_info']['address']['line1'] ) ) {
422
							$check = $donation_meta['user_info']['address']['line1'];
423
						}
424
						break;
425
426
					case 'address2':
427
						if ( isset( $donation_meta['user_info']['address']['line2'] ) ) {
428
							$check = $donation_meta['user_info']['address']['line2'];
429
						}
430
						break;
431
432
					default:
433
						if ( isset( $donation_meta['user_info']['address'][ $meta_key ] ) ) {
434
							$check = $donation_meta['user_info']['address'][ $meta_key ];
435
						}
436
				}
437
			}
438
439
			break;
440
	}// 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...
441
442
	// Put result in an array on zero index.
443
	if ( ! is_null( $check ) ) {
444
		$check = array( $check );
445
	}
446
447
	return $check;
448
}
449
450
add_filter( 'get_post_metadata', '_give_20_bc_get_new_payment_meta', 10, 5 );
451
452
/**
453
 * Delete payment cache on update.
454
 * This cache used for saving queries
455
 *
456
 * @since 2.0
457
 *
458
 * @param $payment_id
459
 */
460
function _give_20_bc_delete_cache( $payment_id ) {
461
	// Bailout.
462
	if ( wp_is_post_revision( $payment_id ) ) {
463
		return;
464
	}
465
466
	Give_Cache::delete( Give_Cache::get_options_like( "give_20_bc_give_payment_meta_$payment_id" ) );
467
}
468
469
add_action( 'save_post_give_payment', '_give_20_bc_delete_cache', 9999 );
470
add_action( 'give_update_edited_donation', '_give_20_bc_delete_cache', 9999 );
471