| Conditions | 29 |
| Paths | > 20000 |
| Total Lines | 189 |
| Code Lines | 106 |
| Lines | 0 |
| Ratio | 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 |
||
| 29 | function give_update_payment_details( $data ) { |
||
| 30 | |||
| 31 | if ( ! current_user_can( 'edit_give_payments', $data['give_payment_id'] ) ) { |
||
| 32 | wp_die( __( 'You do not have permission to edit this payment record', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) ); |
||
| 33 | } |
||
| 34 | |||
| 35 | check_admin_referer( 'give_update_payment_details_nonce' ); |
||
| 36 | |||
| 37 | // Retrieve the payment ID |
||
| 38 | $payment_id = absint( $data['give_payment_id'] ); |
||
| 39 | |||
| 40 | // Retrieve existing payment meta |
||
| 41 | $meta = give_get_payment_meta( $payment_id ); |
||
| 42 | $user_info = give_get_payment_meta_user_info( $payment_id ); |
||
| 43 | |||
| 44 | $status = $data['give-payment-status']; |
||
| 45 | $user_id = isset( $data['give-payment-user-id'] ) ? intval( $data['give-payment-user-id'] ) : ''; |
||
| 46 | $date = sanitize_text_field( $data['give-payment-date'] ); |
||
| 47 | $hour = sanitize_text_field( $data['give-payment-time-hour'] ); |
||
| 48 | $form_id = give_get_payment_form_id($payment_id); |
||
| 49 | |||
| 50 | // Restrict to our high and low |
||
| 51 | if ( $hour > 23 ) { |
||
| 52 | $hour = 23; |
||
| 53 | } elseif ( $hour < 0 ) { |
||
| 54 | $hour = 00; |
||
| 55 | } |
||
| 56 | |||
| 57 | $minute = sanitize_text_field( $data['give-payment-time-min'] ); |
||
| 58 | |||
| 59 | // Restrict to our high and low |
||
| 60 | if ( $minute > 59 ) { |
||
| 61 | $minute = 59; |
||
| 62 | } elseif ( $minute < 0 ) { |
||
| 63 | $minute = 00; |
||
| 64 | } |
||
| 65 | |||
| 66 | $address = array_map( 'trim', $data['give-payment-address'][0] ); |
||
| 67 | $date = date( 'Y-m-d', strtotime( $date ) ) . ' ' . $hour . ':' . $minute . ':00'; |
||
| 68 | $curr_total = give_sanitize_amount( give_get_payment_amount( $payment_id ) ); |
||
| 69 | $new_total = give_sanitize_amount( $_POST['give-payment-total'] ); |
||
| 70 | $curr_customer_id = sanitize_text_field( $data['give-current-customer'] ); |
||
| 71 | $new_customer_id = sanitize_text_field( $data['customer-id'] ); |
||
| 72 | |||
| 73 | do_action( 'give_update_edited_purchase', $payment_id ); |
||
| 74 | |||
| 75 | // Update main payment record |
||
| 76 | $updated = wp_update_post( array( |
||
| 77 | 'ID' => $payment_id, |
||
| 78 | 'edit_date' => true, |
||
| 79 | 'post_date' => $date |
||
| 80 | ) ); |
||
| 81 | |||
| 82 | if ( 0 === $updated ) { |
||
| 83 | wp_die( esc_attr__( 'Error Updating Payment', 'give' ), esc_attr__( 'Error', 'give' ), array( 'response' => 400 ) ); |
||
| 84 | } |
||
| 85 | |||
| 86 | $customer_changed = false; |
||
| 87 | |||
| 88 | if ( isset( $data['give-new-customer'] ) && $data['give-new-customer'] == '1' ) { |
||
| 89 | |||
| 90 | $email = isset( $data['give-new-customer-email'] ) ? sanitize_text_field( $data['give-new-customer-email'] ) : ''; |
||
| 91 | $names = isset( $data['give-new-customer-name'] ) ? sanitize_text_field( $data['give-new-customer-name'] ) : ''; |
||
| 92 | |||
| 93 | if ( empty( $email ) || empty( $names ) ) { |
||
| 94 | wp_die( esc_attr__( 'New Customers require a name and email address', 'give' ) ); |
||
| 95 | } |
||
| 96 | |||
| 97 | $customer = new Give_Customer( $email ); |
||
| 98 | if ( empty( $customer->id ) ) { |
||
| 99 | $customer_data = array( 'name' => $names, 'email' => $email ); |
||
| 100 | $user_id = email_exists( $email ); |
||
| 101 | if ( false !== $user_id ) { |
||
| 102 | $customer_data['user_id'] = $user_id; |
||
| 103 | } |
||
| 104 | |||
| 105 | if ( ! $customer->create( $customer_data ) ) { |
||
| 106 | // Failed to crete the new donor, assume the previous donor |
||
| 107 | $customer_changed = false; |
||
| 108 | $customer = new Give_Customer( $curr_customer_id ); |
||
| 109 | give_set_error( 'give-payment-new-customer-fail', __( 'Error creating new donor', 'give' ) ); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | $new_customer_id = $customer->id; |
||
| 114 | |||
| 115 | $previous_customer = new Give_Customer( $curr_customer_id ); |
||
| 116 | |||
| 117 | $customer_changed = true; |
||
| 118 | |||
| 119 | } elseif ( $curr_customer_id !== $new_customer_id ) { |
||
| 120 | |||
| 121 | $customer = new Give_Customer( $new_customer_id ); |
||
| 122 | $email = $customer->email; |
||
| 123 | $names = $customer->name; |
||
| 124 | |||
| 125 | $previous_customer = new Give_Customer( $curr_customer_id ); |
||
| 126 | |||
| 127 | $customer_changed = true; |
||
| 128 | |||
| 129 | } else { |
||
| 130 | |||
| 131 | $customer = new Give_Customer( $curr_customer_id ); |
||
| 132 | $email = $customer->email; |
||
| 133 | $names = $customer->name; |
||
| 134 | |||
| 135 | } |
||
| 136 | |||
| 137 | |||
| 138 | // Setup first and last name from input values |
||
| 139 | $names = explode( ' ', $names ); |
||
| 140 | $first_name = ! empty( $names[0] ) ? $names[0] : ''; |
||
| 141 | $last_name = ''; |
||
| 142 | if ( ! empty( $names[1] ) ) { |
||
| 143 | unset( $names[0] ); |
||
| 144 | $last_name = implode( ' ', $names ); |
||
| 145 | } |
||
| 146 | |||
| 147 | |||
| 148 | if ( $customer_changed ) { |
||
| 149 | |||
| 150 | // Remove the stats and payment from the previous customer and attach it to the new customer |
||
| 151 | $previous_customer->remove_payment( $payment_id, false ); |
||
| 152 | $customer->attach_payment( $payment_id, false ); |
||
| 153 | |||
| 154 | // If purchase was completed and not ever refunded, adjust stats of customers |
||
| 155 | if ( 'revoked' == $status || 'publish' == $status ) { |
||
| 156 | |||
| 157 | $previous_customer->decrease_purchase_count(); |
||
| 158 | $previous_customer->decrease_value( $new_total ); |
||
| 159 | |||
| 160 | $customer->increase_purchase_count(); |
||
| 161 | $customer->increase_value( $new_total ); |
||
| 162 | } |
||
| 163 | |||
| 164 | update_post_meta( $payment_id, '_give_payment_customer_id', $customer->id ); |
||
| 165 | |||
| 166 | } |
||
| 167 | |||
| 168 | |||
| 169 | // Set new meta values |
||
| 170 | $user_info['id'] = $customer->user_id; |
||
| 171 | $user_info['email'] = $customer->email; |
||
| 172 | $user_info['first_name'] = $first_name; |
||
| 173 | $user_info['last_name'] = $last_name; |
||
| 174 | $user_info['address'] = $address; |
||
| 175 | $meta['user_info'] = $user_info; |
||
| 176 | |||
| 177 | |||
| 178 | // Check for payment notes |
||
| 179 | if ( ! empty( $data['give-payment-note'] ) ) { |
||
| 180 | |||
| 181 | $note = wp_kses( $data['give-payment-note'], array() ); |
||
| 182 | give_insert_payment_note( $payment_id, $note ); |
||
| 183 | |||
| 184 | } |
||
| 185 | |||
| 186 | // Set new status |
||
| 187 | give_update_payment_status( $payment_id, $status ); |
||
| 188 | |||
| 189 | give_update_payment_meta( $payment_id, '_give_payment_user_id', $customer->user_id ); |
||
| 190 | give_update_payment_meta( $payment_id, '_give_payment_user_email', $customer->email ); |
||
| 191 | give_update_payment_meta( $payment_id, '_give_payment_meta', $meta ); |
||
| 192 | give_update_payment_meta( $payment_id, '_give_payment_total', $new_total ); |
||
| 193 | |||
| 194 | // Adjust total store earnings if the payment total has been changed |
||
| 195 | if ( $new_total !== $curr_total && ( 'publish' == $status || 'revoked' == $status ) ) { |
||
| 196 | |||
| 197 | if ( $new_total > $curr_total ) { |
||
| 198 | // Increase if our new total is higher |
||
| 199 | $difference = $new_total - $curr_total; |
||
| 200 | give_increase_total_earnings( $difference ); |
||
| 201 | $form = new Give_Donate_Form( $form_id ); |
||
| 202 | $form->increase_earnings( $difference ); |
||
| 203 | } elseif ( $curr_total > $new_total ) { |
||
| 204 | // Decrease if our new total is lower |
||
| 205 | $difference = $curr_total - $new_total; |
||
| 206 | give_decrease_total_earnings( $difference ); |
||
| 207 | $form = new Give_Donate_Form( $form_id ); |
||
| 208 | $form->decrease_earnings( $difference ); |
||
| 209 | } |
||
| 210 | |||
| 211 | } |
||
| 212 | |||
| 213 | do_action( 'give_updated_edited_purchase', $payment_id ); |
||
| 214 | |||
| 215 | wp_safe_redirect( admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&view=view-order-details&give-message=payment-updated&id=' . $payment_id ) ); |
||
| 216 | exit; |
||
| 217 | } |
||
| 218 | |||
| 321 | add_action( 'wp_ajax_give_delete_payment_note', 'give_ajax_delete_payment_note' ); |
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.