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