Completed
Push — issues/1132 ( 9a1477...727389 )
by Ravinder
18:41
created

backward-compatibility.php ➔ _give_bc_update_payment_meta20()   F

Complexity

Conditions 19
Paths 1033

Size

Total Lines 90
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
eloc 45
nc 1033
nop 4
dl 0
loc 90
rs 2
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_id',
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_id':
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_meta_keys = array(
121
		'_give_payment_meta',
122
		'_give_payment_customer_id',
123
		'_give_payment_user_email',
124
		'_give_payment_user_id',
125
	);
126
127
	// Bailout.
128
	if (
129
		'give_payment' !== get_post_type( $id ) ||
130
		! in_array( $meta_key, $payment_meta_keys )
131
	) {
132
		return $meta_value;
133
	}
134
135
	switch ( $meta_key ) {
136
		case '_give_payment_meta':
137
			// Date payment meta.
138
			$meta_value['date'] = give_get_meta( $id, '_give_payment_date', $meta_value['date'] );
139
140
			// Currency payment meta.
141
			$meta_value['currency'] = give_get_meta( $id, '_give_payment_currency', $meta_value['currency'] );
142
143
144
			// Donor address payment meta.
145
			if ( ! empty( $meta_value['user_info']['address'] ) ) {
146
147
				// Donor first name.
148
				$meta_value['user_info']['first_name'] = give_get_meta( $id, '_give_donor_billing_first_name', $meta_value['user_info']['first_name'] );
149
150
151
				// Donor last name.
152
				$meta_value['user_info']['last_name'] = give_get_meta( $id, '_give_donor_billing_last_name', $meta_value['user_info']['last_name'] );
153
154
155
				// Address1.
156
				$meta_value['user_info']['address']['line1'] = give_update_meta( $id, '_give_donor_billing_address1', $meta_value['user_info']['address']['line1'] );
157
158
159
				// Address2.
160
				$meta_value['user_info']['address']['line2'] = give_update_meta( $id, '_give_donor_billing_address1', $meta_value['user_info']['address']['line2'] );
161
162
163
				// City.
164
				$meta_value['user_info']['address']['city'] = give_update_meta( $id, '_give_donor_billing_city', $meta_value['user_info']['address']['city'] );
165
166
167
				// Zip.
168
				$meta_value['user_info']['address']['zip'] = give_update_meta( $id, '_give_donor_billing_zip', $meta_value['user_info']['address']['zip'] );
169
170
171
				// State.
172
				$meta_value['user_info']['address']['state'] = give_update_meta( $id, '_give_donor_billing_state', $meta_value['user_info']['address']['state'] );
173
174
175
				// Country.
176
				$meta_value['user_info']['address']['country'] = give_update_meta( $id, '_give_donor_billing_country', $meta_value['user_info']['address']['country'] );
177
178
			}
179
180
			break;
181
182
		case '_give_payment_customer_id':
183
			$meta_value = give_get_meta( $id, '_give_payment_donor_id', $meta_value );
184
			break;
185
186
		case '_give_payment_user_email':
187
			$meta_value = give_get_meta( $id, '_give_payment_donor_email', $meta_value );
188
			break;
189
190
		case '_give_payment_user_id':
191
			$meta_value = give_get_meta( $id, '_give_payment_donor_ip', $meta_value );
192
			break;
193
	}
194
195
	return $meta_value;
196
}
197
198
add_filter( 'give_get_meta', '_give_bc_get_payment_meta20', 10, 3 );