Completed
Push — issues/1132 ( 727389...280b2f )
by Ravinder
18:26
created

backward-compatibility.php ➔ _give_bc_get_payment_meta20()   D

Complexity

Conditions 13
Paths 10

Size

Total Lines 101
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 48
nc 10
nop 3
dl 0
loc 101
rs 4.9922
c 0
b 0
f 0

How to fix   Long Method    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 14 and the first side effect is on line 105.

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
 * Add backward compatibility to payment meta while saving.
4
 *
5
 * @since 2.0
6
 *
7
 * @param bool   $status
8
 * @param int    $id
9
 * @param string $meta_key
10
 * @param mixed  $meta_value
11
 *
12
 * @return mixed
13
 */
14
function _give_bc_update_payment_meta20( $status, $id, $meta_key, $meta_value ) {
15
	$payment_meta_keys = array(
16
		'_give_payment_meta',
17
		'_give_payment_customer_id',
18
		'_give_payment_user_email',
19
		'_give_payment_user_ip',
20
	);
21
22
	// Bailout.
23
	if (
24
		! $status ||
25
		'give_payment' !== get_post_type( $id ) ||
26
		! in_array( $meta_key, $payment_meta_keys )
27
	) {
28
		return $status;
29
	}
30
31
	switch ( $meta_key ) {
32
		case '_give_payment_meta':
33
			// Date payment meta.
34
			if ( ! empty( $meta_value['date'] ) ) {
35
				give_update_meta( $id, '_give_payment_date', $meta_value['date'] );
36
			}
37
38
			// Currency payment meta.
39
			if ( ! empty( $meta_value['currency'] ) ) {
40
				give_update_meta( $id, '_give_payment_currency', $meta_value['currency'] );
41
			}
42
43
			// Donor address payment meta.
44
			if ( ! empty( $meta_value['user_info']['address'] ) ) {
45
46
				// Donor first name.
47
				if ( ! empty( $meta_value['user_info']['first_name'] ) ) {
48
					give_update_meta( $id, '_give_donor_billing_first_name', $meta_value['user_info']['first_name'] );
49
				}
50
51
				// Donor last name.
52
				if ( ! empty( $meta_value['user_info']['last_name'] ) ) {
53
					give_update_meta( $id, '_give_donor_billing_last_name', $meta_value['user_info']['last_name'] );
54
				}
55
56
				// Address1.
57
				if ( ! empty( $meta_value['user_info']['address']['line1'] ) ) {
58
					give_update_meta( $id, '_give_donor_billing_address1', $meta_value['user_info']['address']['line1'] );
59
				}
60
61
				// Address2.
62
				if ( ! empty( $meta_value['user_info']['address']['line2'] ) ) {
63
					give_update_meta( $id, '_give_donor_billing_address2', $meta_value['user_info']['address']['line2'] );
64
				}
65
66
				// City.
67
				if ( ! empty( $meta_value['user_info']['address']['city'] ) ) {
68
					give_update_meta( $id, '_give_donor_billing_city', $meta_value['user_info']['address']['city'] );
69
				}
70
71
				// Zip.
72
				if ( ! empty( $meta_value['user_info']['address']['zip'] ) ) {
73
					give_update_meta( $id, '_give_donor_billing_zip', $meta_value['user_info']['address']['zip'] );
74
				}
75
76
				// State.
77
				if ( ! empty( $meta_value['user_info']['address']['state'] ) ) {
78
					give_update_meta( $id, '_give_donor_billing_state', $meta_value['user_info']['address']['state'] );
79
				}
80
81
				// Country.
82
				if ( ! empty( $meta_value['user_info']['address']['country'] ) ) {
83
					give_update_meta( $id, '_give_donor_billing_country', $meta_value['user_info']['address']['country'] );
84
				}
85
			}
86
87
			break;
88
89
		case '_give_payment_customer_id':
90
			give_update_meta( $id, '_give_payment_donor_id', $meta_value );
91
			break;
92
93
		case '_give_payment_user_email':
94
			give_update_meta( $id, '_give_payment_donor_email', $meta_value );
95
			break;
96
97
		case '_give_payment_user_ip':
98
			give_update_meta( $id, '_give_payment_donor_ip', $meta_value );
99
			break;
100
	}
101
102
	return $status;
103
}
104
105
add_filter( 'give_update_meta', '_give_bc_update_payment_meta20', 10, 4 );
106
107
108
/**
109
 * Add backward compatibility to payment meta while fetching data.
110
 *
111
 * @since 2.0
112
 *
113
 * @param mixed  $meta_value
114
 * @param int    $id
115
 * @param string $meta_key
116
 *
117
 * @return mixed
118
 */
119
function _give_bc_get_payment_meta20( $meta_value, $id, $meta_key ) {
120
	$payment_old_meta_keys = array(
121
		'_give_payment_meta',
122
		'_give_payment_customer_id',
123
		'_give_payment_user_email',
124
		'_give_payment_user_ip',
125
	);
126
127
	$payment_new_meta_keys = array(
128
		'_give_payment_donor_id',
129
		'_give_payment_donor_email',
130
		'_give_payment_donor_ip',
131
	);
132
133
	// Bailout.
134
	if (
135
		'give_payment' !== get_post_type( $id ) ||
136
		! in_array( $meta_key, $payment_old_meta_keys ) ||
137
		( in_array( $meta_key, $payment_new_meta_keys ) && ! empty( $meta_value ) )
138
	) {
139
		return $meta_value;
140
	}
141
142
	switch ( $meta_key ) {
143
144
		// Handle new meta keys.
145
		case '_give_payment_donor_id':
146
			$meta_value = get_post_meta( $id, '_give_payment_customer_id', true );
147
			break;
148
149
		case '_give_payment_donor_email':
150
			$meta_value = get_post_meta( $id, '_give_payment_user_email', true );
151
			break;
152
153
		case '_give_payment_donor_ip':
154
			$meta_value = get_post_meta( $id, '_give_payment_user_ip', true );
155
			break;
156
157
158
		// Handle old meta keys.
159
		case '_give_payment_meta':
160
			// Date payment meta.
161
			$meta_value['date'] = give_get_meta( $id, '_give_payment_date', $meta_value['date'] );
162
163
			// Currency payment meta.
164
			$meta_value['currency'] = give_get_meta( $id, '_give_payment_currency', $meta_value['currency'] );
165
166
167
			// Donor address payment meta.
168
			if ( ! empty( $meta_value['user_info']['address'] ) ) {
169
170
				// Donor first name.
171
				$meta_value['user_info']['first_name'] = give_get_meta( $id, '_give_donor_billing_first_name', $meta_value['user_info']['first_name'] );
172
173
174
				// Donor last name.
175
				$meta_value['user_info']['last_name'] = give_get_meta( $id, '_give_donor_billing_last_name', $meta_value['user_info']['last_name'] );
176
177
178
				// Address1.
179
				$meta_value['user_info']['address']['line1'] = give_get_meta( $id, '_give_donor_billing_address1', $meta_value['user_info']['address']['line1'] );
180
181
182
				// Address2.
183
				$meta_value['user_info']['address']['line2'] = give_get_meta( $id, '_give_donor_billing_address1', $meta_value['user_info']['address']['line2'] );
184
185
186
				// City.
187
				$meta_value['user_info']['address']['city'] = give_get_meta( $id, '_give_donor_billing_city', $meta_value['user_info']['address']['city'] );
188
189
190
				// Zip.
191
				$meta_value['user_info']['address']['zip'] = give_get_meta( $id, '_give_donor_billing_zip', $meta_value['user_info']['address']['zip'] );
192
193
194
				// State.
195
				$meta_value['user_info']['address']['state'] = give_get_meta( $id, '_give_donor_billing_state', $meta_value['user_info']['address']['state'] );
196
197
198
				// Country.
199
				$meta_value['user_info']['address']['country'] = give_get_meta( $id, '_give_donor_billing_country', $meta_value['user_info']['address']['country'] );
200
201
			}
202
203
			break;
204
205
		case '_give_payment_customer_id':
206
			$meta_value = give_get_meta( $id, '_give_payment_donor_id', $meta_value );
207
			break;
208
209
		case '_give_payment_user_email':
210
			$meta_value = give_get_meta( $id, '_give_payment_donor_email', $meta_value );
211
			break;
212
213
		case '_give_payment_user_id':
214
			$meta_value = give_get_meta( $id, '_give_payment_donor_ip', $meta_value );
215
			break;
216
	}
217
218
	return $meta_value;
219
}
220
221
add_filter( 'give_get_meta', '_give_bc_get_payment_meta20', 10, 3 );