Conditions | 56 |
Paths | > 20000 |
Total Lines | 382 |
Lines | 3 |
Ratio | 0.79 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
28 | function give_update_payment_details( $data ) { |
||
29 | |||
30 | View Code Duplication | if ( ! current_user_can( 'edit_give_payments', $data['give_payment_id'] ) ) { |
|
|
|||
31 | wp_die( __( 'You do not have permission to edit payments.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) ); |
||
32 | } |
||
33 | |||
34 | check_admin_referer( 'give_update_payment_details_nonce' ); |
||
35 | |||
36 | // Retrieve the payment ID. |
||
37 | $payment_id = absint( $data['give_payment_id'] ); |
||
38 | |||
39 | /* @var Give_Payment $payment */ |
||
40 | $payment = new Give_Payment( $payment_id ); |
||
41 | |||
42 | $status = $data['give-payment-status']; |
||
43 | $date = sanitize_text_field( $data['give-payment-date'] ); |
||
44 | $hour = sanitize_text_field( $data['give-payment-time-hour'] ); |
||
45 | |||
46 | // Restrict to our high and low. |
||
47 | if ( $hour > 23 ) { |
||
48 | $hour = 23; |
||
49 | } elseif ( $hour < 0 ) { |
||
50 | $hour = 00; |
||
51 | } |
||
52 | |||
53 | $minute = sanitize_text_field( $data['give-payment-time-min'] ); |
||
54 | |||
55 | // Restrict to our high and low. |
||
56 | if ( $minute > 59 ) { |
||
57 | $minute = 59; |
||
58 | } elseif ( $minute < 0 ) { |
||
59 | $minute = 00; |
||
60 | } |
||
61 | |||
62 | $address = give_clean( $data['give-payment-address'][0] ); |
||
63 | |||
64 | $curr_total = $payment->total; |
||
65 | $new_total = give_maybe_sanitize_amount( ( ! empty( $data['give-payment-total'] ) ? $data['give-payment-total'] : 0 ) ); |
||
66 | $date = date( 'Y-m-d', strtotime( $date ) ) . ' ' . $hour . ':' . $minute . ':00'; |
||
67 | |||
68 | $curr_donor_id = sanitize_text_field( $data['give-current-donor'] ); |
||
69 | $new_donor_id = sanitize_text_field( $data['donor-id'] ); |
||
70 | |||
71 | /** |
||
72 | * Fires before updating edited donation. |
||
73 | * |
||
74 | * @since 1.0 |
||
75 | * @since 1.8.9 Changes hook name give_update_edited_purchase -> give_update_edited_donation |
||
76 | * |
||
77 | * @param int $payment_id The ID of the payment. |
||
78 | */ |
||
79 | do_action( 'give_update_edited_donation', $payment_id ); |
||
80 | |||
81 | $payment->date = $date; |
||
82 | $payment->anonymous = isset( $data['give_anonymous_donation'] ) ? absint( $data['give_anonymous_donation'] ) : 0; |
||
83 | |||
84 | |||
85 | $updated = $payment->save(); |
||
86 | |||
87 | if ( 0 === $updated ) { |
||
88 | wp_die( __( 'Error Updating Donation.', 'give' ), __( 'Error', 'give' ), array( 'response' => 400 ) ); |
||
89 | } |
||
90 | |||
91 | $donor_changed = false; |
||
92 | |||
93 | if ( isset( $data['give-new-donor'] ) && $data['give-new-donor'] == '1' ) { |
||
94 | |||
95 | $email = ! empty( $data['give-new-donor-email'] ) ? sanitize_text_field( $data['give-new-donor-email'] ) : ''; |
||
96 | $first_name = ! empty( $data['give-new-donor-first-name'] ) ? sanitize_text_field( $data['give-new-donor-first-name'] ) : ''; |
||
97 | $last_name = ! empty( $data['give-new-donor-last-name'] ) ? sanitize_text_field( $data['give-new-donor-last-name'] ) : ''; |
||
98 | $names = strip_tags( wp_unslash( trim( "{$first_name} {$last_name}" ) ) ); |
||
99 | |||
100 | if ( empty( $email ) || empty( $first_name ) ) { |
||
101 | wp_die( __( 'New Donor requires first name and email address.', 'give' ), __( 'Error', 'give' ), array( 'response' => 400 ) ); |
||
102 | } |
||
103 | |||
104 | $donor = new Give_Donor( $email ); |
||
105 | if ( empty( $donor->id ) ) { |
||
106 | $donor_data = array( 'name' => $names, 'email' => $email ); |
||
107 | $user_id = email_exists( $email ); |
||
108 | if ( false !== $user_id ) { |
||
109 | $donor_data['user_id'] = $user_id; |
||
110 | } |
||
111 | |||
112 | if ( ! $donor->create( $donor_data ) ) { |
||
113 | // Failed to create the new donor, assume the previous donor. |
||
114 | $donor_changed = false; |
||
115 | $donor = new Give_Donor( $curr_donor_id ); |
||
116 | give_set_error( 'give-payment-new-donor-fail', __( 'Error creating new donor.', 'give' ) ); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | // Create and Update Donor First Name and Last Name in Meta Fields. |
||
121 | $donor->update_meta( '_give_donor_first_name', $first_name ); |
||
122 | $donor->update_meta( '_give_donor_last_name', $last_name ); |
||
123 | |||
124 | $new_donor_id = $donor->id; |
||
125 | |||
126 | $previous_donor = new Give_Donor( $curr_donor_id ); |
||
127 | |||
128 | $donor_changed = true; |
||
129 | |||
130 | } elseif ( $curr_donor_id !== $new_donor_id ) { |
||
131 | |||
132 | $donor = new Give_Donor( $new_donor_id ); |
||
133 | $email = $donor->email; |
||
134 | $names = $donor->name; |
||
135 | |||
136 | $previous_donor = new Give_Donor( $curr_donor_id ); |
||
137 | |||
138 | $donor_changed = true; |
||
139 | |||
140 | } else { |
||
141 | $donor = new Give_Donor( $curr_donor_id ); |
||
142 | $email = $donor->email; |
||
143 | $names = $donor->name; |
||
144 | } |
||
145 | |||
146 | if ( $donor_changed ) { |
||
147 | |||
148 | // Setup first and last name from input values. |
||
149 | $first_name = $donor->get_first_name(); |
||
150 | $last_name = $donor->get_last_name(); |
||
151 | |||
152 | $payment->first_name = $first_name; |
||
153 | $payment->last_name = $last_name; |
||
154 | |||
155 | // Remove the stats and payment from the previous donor and attach it to the new donor. |
||
156 | $previous_donor->remove_payment( $payment_id, false ); |
||
157 | $donor->attach_payment( $payment_id, false ); |
||
158 | |||
159 | if ( 'publish' == $status ) { |
||
160 | |||
161 | // Reduce previous user donation count and amount. |
||
162 | $previous_donor->decrease_donation_count(); |
||
163 | $previous_donor->decrease_value( $curr_total ); |
||
164 | |||
165 | // If donation was completed adjust stats of new donors. |
||
166 | $donor->increase_purchase_count(); |
||
167 | $donor->increase_value( $new_total ); |
||
168 | } |
||
169 | |||
170 | $payment->customer_id = $donor->id; |
||
171 | } else { |
||
172 | |||
173 | if ( 'publish' === $status ) { |
||
174 | // Update user donation stat. |
||
175 | $donor->update_donation_value( $curr_total, $new_total ); |
||
176 | } |
||
177 | } |
||
178 | |||
179 | // Set new meta values. |
||
180 | $payment->user_id = $donor->user_id; |
||
181 | $payment->email = $donor->email; |
||
182 | $payment->address = $address; |
||
183 | $payment->total = $new_total; |
||
184 | |||
185 | // Check for payment notes. |
||
186 | if ( ! empty( $data['give-payment-note'] ) ) { |
||
187 | |||
188 | $note = wp_kses( $data['give-payment-note'], array() ); |
||
189 | give_insert_payment_note( $payment_id, $note ); |
||
190 | |||
191 | } |
||
192 | |||
193 | // Set new status. |
||
194 | $payment->status = $status; |
||
195 | |||
196 | // Adjust total store earnings if the payment total has been changed. |
||
197 | if ( $new_total !== $curr_total && 'publish' == $status ) { |
||
198 | |||
199 | if ( $new_total > $curr_total ) { |
||
200 | // Increase if our new total is higher. |
||
201 | $difference = $new_total - $curr_total; |
||
202 | give_increase_total_earnings( $difference ); |
||
203 | |||
204 | // Increase form earnings. |
||
205 | give_increase_earnings( $payment->form_id, $difference, $payment->ID ); |
||
206 | } elseif ( $curr_total > $new_total ) { |
||
207 | // Decrease if our new total is lower. |
||
208 | $difference = $curr_total - $new_total; |
||
209 | give_decrease_total_earnings( $difference ); |
||
210 | |||
211 | // Decrease form earnings. |
||
212 | give_decrease_form_earnings( $payment->form_id, $difference, $payment->ID ); |
||
213 | } |
||
214 | } |
||
215 | |||
216 | $payment->save(); |
||
217 | |||
218 | // Get new give form ID. |
||
219 | $new_form_id = absint( $data['give-payment-form-select'] ); |
||
220 | $current_form_id = absint( $payment->get_meta( '_give_payment_form_id' ) ); |
||
221 | |||
222 | // We are adding payment transfer code in last to remove any conflict with above functionality. |
||
223 | // For example: above code will automatically handle form stat (increase/decrease) when payment status changes. |
||
224 | // Check if user want to transfer current payment to new give form id. |
||
225 | if ( $new_form_id && $new_form_id != $current_form_id ) { |
||
226 | |||
227 | // Get new give form title. |
||
228 | $new_form_title = get_the_title( $new_form_id ); |
||
229 | |||
230 | // Update payment give form meta data. |
||
231 | $payment->update_meta( '_give_payment_form_id', $new_form_id ); |
||
232 | $payment->update_meta( '_give_payment_form_title', $new_form_title ); |
||
233 | |||
234 | // Update price id payment metadata. |
||
235 | if ( ! give_has_variable_prices( $new_form_id ) ) { |
||
236 | $payment->update_meta( '_give_payment_price_id', '' ); |
||
237 | } |
||
238 | |||
239 | // If donation was completed, adjust stats of forms. |
||
240 | if ( 'publish' == $status ) { |
||
241 | |||
242 | // Decrease sale of old give form. For other payment status. |
||
243 | $current_form = new Give_Donate_Form( $current_form_id ); |
||
244 | $current_form->decrease_sales(); |
||
245 | $current_form->decrease_earnings( $curr_total, $payment->ID ); |
||
246 | |||
247 | // Increase sale of new give form. |
||
248 | $new_form = new Give_Donate_Form( $new_form_id ); |
||
249 | $new_form->increase_sales(); |
||
250 | $new_form->increase_earnings( $new_total, $payment->ID ); |
||
251 | } |
||
252 | |||
253 | // Re setup payment to update new meta value in object. |
||
254 | $payment->update_payment_setup( $payment->ID ); |
||
255 | |||
256 | // Update form id in payment logs. |
||
257 | Give()->async_process->data( array( |
||
258 | 'data' => array( $new_form_id, $payment_id ), |
||
259 | 'hook' => 'give_update_log_form_id', |
||
260 | ) )->dispatch(); |
||
261 | } |
||
262 | |||
263 | // Update price id if current form is variable form. |
||
264 | /* @var Give_Donate_Form $form */ |
||
265 | $form = new Give_Donate_Form( $payment->form_id ); |
||
266 | |||
267 | if ( isset( $data['give-variable-price'] ) && $form->has_variable_prices() ) { |
||
268 | |||
269 | // Get payment meta data. |
||
270 | $payment_meta = $payment->get_meta(); |
||
271 | |||
272 | $price_info = array(); |
||
273 | $price_id = ''; |
||
274 | |||
275 | // Get price info |
||
276 | if( 0 <= $data['give-variable-price'] ) { |
||
277 | foreach ( $form->prices as $variable_price ) { |
||
278 | if( $new_total === give_maybe_sanitize_amount( $variable_price['_give_amount'] ) ) { |
||
279 | $price_info = $variable_price; |
||
280 | break; |
||
281 | } |
||
282 | } |
||
283 | } |
||
284 | |||
285 | // Set price id. |
||
286 | if( ! empty( $price_info ) ) { |
||
287 | $price_id = $data['give-variable-price']; |
||
288 | |||
289 | if( $data['give-variable-price'] !== $price_info['_give_id']['level_id'] ) { |
||
290 | // Set price id to amount match. |
||
291 | $price_id = $price_info['_give_id']['level_id']; |
||
292 | } |
||
293 | |||
294 | } elseif( $form->is_custom_price_mode() ){ |
||
295 | $price_id = 'custom'; |
||
296 | } |
||
297 | |||
298 | // Update payment meta data. |
||
299 | $payment_meta['price_id'] = $price_id; |
||
300 | |||
301 | // Update payment give form meta data. |
||
302 | $payment->update_meta( '_give_payment_price_id', $price_id ); |
||
303 | $payment->update_meta( '_give_payment_meta', $payment_meta ); |
||
304 | |||
305 | // Re setup payment to update new meta value in object. |
||
306 | $payment->update_payment_setup( $payment->ID ); |
||
307 | } |
||
308 | |||
309 | $comment_id = isset( $data['give_comment_id'] ) ? absint( $data['give_comment_id'] ) : 0; |
||
310 | $has_anonymous_setting_field = give_is_anonymous_donation_field_enabled( $payment->form_id ); |
||
311 | |||
312 | if ( $has_anonymous_setting_field ) { |
||
313 | give_update_meta( $payment->ID, '_give_anonymous_donation', $payment->anonymous ); |
||
314 | |||
315 | // Set donor as anonymous only if donor does not have any non anonymous donation. |
||
316 | $donations = Give()->donors->get_column_by( 'payment_ids', 'id', $payment->donor_id ); |
||
317 | $donations = ! empty( $donations ) ? explode( ',', $donations ) : array(); |
||
318 | $update_anonymous_donor_meta = false; |
||
319 | |||
320 | if( ! empty( $donations ) ) { |
||
321 | $non_anonymous_donations = new WP_Query( array( |
||
322 | 'post_type' => 'give_payment', |
||
323 | 'post_status' => 'publish', |
||
324 | 'post__in' => $donations, |
||
325 | 'fields' => 'ids', |
||
326 | 'meta_query' => array( |
||
327 | 'relation' => 'AND', |
||
328 | array( |
||
329 | 'key' => '_give_anonymous_donation', |
||
330 | 'value' => 0, |
||
331 | ), |
||
332 | array( |
||
333 | 'key' => '_give_payment_form_id', |
||
334 | 'value' => $payment->form_id, |
||
335 | ), |
||
336 | ), |
||
337 | ) ); |
||
338 | |||
339 | $update_anonymous_donor_meta = ! ( 0 < $non_anonymous_donations->found_posts ); |
||
340 | |||
341 | if( |
||
342 | 0 === absint( $non_anonymous_donations->found_posts ) |
||
343 | && $payment->anonymous |
||
344 | ) { |
||
345 | $update_anonymous_donor_meta = true; |
||
346 | } elseif ( |
||
347 | 1 === absint( $non_anonymous_donations->found_posts ) |
||
348 | && ! $payment->anonymous |
||
349 | ) { |
||
350 | $update_anonymous_donor_meta = true; |
||
351 | } |
||
352 | } |
||
353 | |||
354 | if ( $update_anonymous_donor_meta ) { |
||
355 | Give()->donor_meta->update_meta( $payment->donor_id, "_give_anonymous_donor_form_{$payment->form_id}", $payment->anonymous ); |
||
356 | } |
||
357 | |||
358 | // Update comment meta if admin is not updating comment. |
||
359 | if( $comment_id ) { |
||
360 | update_comment_meta( $comment_id, '_give_anonymous_donation', $payment->anonymous ); |
||
361 | } |
||
362 | } |
||
363 | |||
364 | // Update comment. |
||
365 | if ( give_is_donor_comment_field_enabled( $payment->form_id ) ) { |
||
366 | // We are access comment directly from $_POST because comment formatting remove because of give_clean in give_post_actions. |
||
367 | $data['give_comment'] = trim( $_POST['give_comment'] ); |
||
368 | |||
369 | if ( empty( $data['give_comment'] ) ) { |
||
370 | // Delete comment if empty |
||
371 | Give_Comment::delete( $comment_id, $payment_id, 'payment' ); |
||
372 | |||
373 | } else { |
||
374 | $comment_args = array( |
||
375 | 'comment_author_email' => $payment->email |
||
376 | ); |
||
377 | |||
378 | if ( $comment_id ) { |
||
379 | $comment_args['comment_ID'] = $comment_id; |
||
380 | } |
||
381 | |||
382 | give_insert_donor_donation_comment( |
||
383 | $payment->ID, |
||
384 | $payment->donor_id, |
||
385 | $data['give_comment'], |
||
386 | $comment_args |
||
387 | ); |
||
388 | } |
||
389 | |||
390 | $donor_has_comment = empty( $data['give_comment'] ) |
||
391 | ? ( $latest_comment = give_get_donor_latest_comment( $payment->donor_id ) && empty( $latest_comment ) ? '0' : '1' ) |
||
392 | : '1'; |
||
393 | |||
394 | Give()->donor_meta->update_meta( $payment->donor_id, '_give_has_comment', $donor_has_comment ); |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * Fires after updating edited donation. |
||
399 | * |
||
400 | * @since 1.0 |
||
401 | * @since 1.8.9 Changes hook name give_updated_edited_purchase -> give_updated_edited_donation |
||
402 | * |
||
403 | * @param int $payment_id The ID of the payment. |
||
404 | */ |
||
405 | do_action( 'give_updated_edited_donation', $payment_id ); |
||
406 | |||
407 | wp_safe_redirect( admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&view=view-payment-details&give-messages[]=payment-updated&id=' . $payment_id ) ); |
||
408 | exit; |
||
409 | } |
||
410 | |||
515 |
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.