| Conditions | 36 |
| Paths | > 20000 |
| Total Lines | 149 |
| Code Lines | 86 |
| 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 |
||
| 26 | function give_edit_customer( $args ) { |
||
| 27 | $customer_edit_role = apply_filters( 'give_edit_customers_role', 'edit_give_payments' ); |
||
| 28 | |||
| 29 | if ( ! is_admin() || ! current_user_can( $customer_edit_role ) ) { |
||
| 30 | wp_die( __( 'You do not have permission to edit this donor.', 'give' ) ); |
||
| 31 | } |
||
| 32 | |||
| 33 | if ( empty( $args ) ) { |
||
| 34 | return; |
||
| 35 | } |
||
| 36 | |||
| 37 | $customer_info = $args['customerinfo']; |
||
| 38 | $customer_id = (int) $args['customerinfo']['id']; |
||
| 39 | $nonce = $args['_wpnonce']; |
||
| 40 | |||
| 41 | if ( ! wp_verify_nonce( $nonce, 'edit-customer' ) ) { |
||
| 42 | wp_die( __( 'Cheatin\' eh?!', 'give' ) ); |
||
| 43 | } |
||
| 44 | |||
| 45 | $customer = new Give_Customer( $customer_id ); |
||
| 46 | if ( empty( $customer->id ) ) { |
||
| 47 | return false; |
||
| 48 | } |
||
| 49 | |||
| 50 | $defaults = array( |
||
| 51 | 'name' => '', |
||
| 52 | 'email' => '', |
||
| 53 | 'user_id' => 0 |
||
| 54 | ); |
||
| 55 | |||
| 56 | $customer_info = wp_parse_args( $customer_info, $defaults ); |
||
| 57 | |||
| 58 | if ( ! is_email( $customer_info['email'] ) ) { |
||
| 59 | give_set_error( 'give-invalid-email', __( 'Please enter a valid email address.', 'give' ) ); |
||
| 60 | } |
||
| 61 | |||
| 62 | if ( (int) $customer_info['user_id'] != (int) $customer->user_id ) { |
||
| 63 | |||
| 64 | // Make sure we don't already have this user attached to a customer |
||
| 65 | if ( ! empty( $customer_info['user_id'] ) && false !== Give()->customers->get_customer_by( 'user_id', $customer_info['user_id'] ) ) { |
||
| 66 | give_set_error( 'give-invalid-customer-user_id', sprintf( __( 'The User ID %d is already associated with a different donor.', 'give' ), $customer_info['user_id'] ) ); |
||
| 67 | } |
||
| 68 | |||
| 69 | // Make sure it's actually a user |
||
| 70 | $user = get_user_by( 'id', $customer_info['user_id'] ); |
||
| 71 | if ( ! empty( $customer_info['user_id'] ) && false === $user ) { |
||
| 72 | give_set_error( 'give-invalid-user_id', sprintf( __( 'The User ID %d does not exist. Please assign an existing user.', 'give' ), $customer_info['user_id'] ) ); |
||
| 73 | } |
||
| 74 | |||
| 75 | } |
||
| 76 | |||
| 77 | // Record this for later |
||
| 78 | $previous_user_id = $customer->user_id; |
||
| 79 | |||
| 80 | if ( give_get_errors() ) { |
||
| 81 | return; |
||
| 82 | } |
||
| 83 | |||
| 84 | // Setup the customer address, if present |
||
| 85 | $address = array(); |
||
| 86 | if ( intval( $customer_info['user_id'] ) > 0 ) { |
||
| 87 | |||
| 88 | $current_address = get_user_meta( $customer_info['user_id'], '_give_user_address', true ); |
||
| 89 | |||
| 90 | if ( false === $current_address ) { |
||
| 91 | $address['line1'] = isset( $customer_info['line1'] ) ? $customer_info['line1'] : ''; |
||
| 92 | $address['line2'] = isset( $customer_info['line2'] ) ? $customer_info['line2'] : ''; |
||
| 93 | $address['city'] = isset( $customer_info['city'] ) ? $customer_info['city'] : ''; |
||
| 94 | $address['country'] = isset( $customer_info['country'] ) ? $customer_info['country'] : ''; |
||
| 95 | $address['zip'] = isset( $customer_info['zip'] ) ? $customer_info['zip'] : ''; |
||
| 96 | $address['state'] = isset( $customer_info['state'] ) ? $customer_info['state'] : ''; |
||
| 97 | } else { |
||
| 98 | $current_address = wp_parse_args( $current_address, array( |
||
| 99 | 'line1', |
||
| 100 | 'line2', |
||
| 101 | 'city', |
||
| 102 | 'zip', |
||
| 103 | 'state', |
||
| 104 | 'country' |
||
| 105 | ) ); |
||
| 106 | $address['line1'] = isset( $customer_info['line1'] ) ? $customer_info['line1'] : $current_address['line1']; |
||
| 107 | $address['line2'] = isset( $customer_info['line2'] ) ? $customer_info['line2'] : $current_address['line2']; |
||
| 108 | $address['city'] = isset( $customer_info['city'] ) ? $customer_info['city'] : $current_address['city']; |
||
| 109 | $address['country'] = isset( $customer_info['country'] ) ? $customer_info['country'] : $current_address['country']; |
||
| 110 | $address['zip'] = isset( $customer_info['zip'] ) ? $customer_info['zip'] : $current_address['zip']; |
||
| 111 | $address['state'] = isset( $customer_info['state'] ) ? $customer_info['state'] : $current_address['state']; |
||
| 112 | } |
||
| 113 | |||
| 114 | } |
||
| 115 | |||
| 116 | // Sanitize the inputs |
||
| 117 | $customer_data = array(); |
||
| 118 | $customer_data['name'] = strip_tags( stripslashes( $customer_info['name'] ) ); |
||
| 119 | $customer_data['email'] = $customer_info['email']; |
||
| 120 | $customer_data['user_id'] = $customer_info['user_id']; |
||
| 121 | |||
| 122 | $customer_data = apply_filters( 'give_edit_customer_info', $customer_data, $customer_id ); |
||
| 123 | $address = apply_filters( 'give_edit_customer_address', $address, $customer_id ); |
||
| 124 | |||
| 125 | $customer_data = array_map( 'sanitize_text_field', $customer_data ); |
||
| 126 | $address = array_map( 'sanitize_text_field', $address ); |
||
| 127 | |||
| 128 | do_action( 'give_pre_edit_customer', $customer_id, $customer_data, $address ); |
||
| 129 | |||
| 130 | $output = array(); |
||
| 131 | $previous_email = $customer->email; |
||
| 132 | |||
| 133 | if ( $customer->update( $customer_data ) ) { |
||
| 134 | |||
| 135 | if ( ! empty( $customer->user_id ) && $customer->user_id > 0 ) { |
||
| 136 | update_user_meta( $customer->user_id, '_give_user_address', $address ); |
||
| 137 | } |
||
| 138 | |||
| 139 | // Update some payment meta if we need to |
||
| 140 | $payments_array = explode( ',', $customer->payment_ids ); |
||
| 141 | |||
| 142 | if ( $customer->email != $previous_email ) { |
||
| 143 | foreach ( $payments_array as $payment_id ) { |
||
| 144 | give_update_payment_meta( $payment_id, 'email', $customer->email ); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | if ( $customer->user_id != $previous_user_id ) { |
||
| 149 | foreach ( $payments_array as $payment_id ) { |
||
| 150 | give_update_payment_meta( $payment_id, '_give_payment_user_id', $customer->user_id ); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | $output['success'] = true; |
||
| 155 | $customer_data = array_merge( $customer_data, $address ); |
||
| 156 | $output['customer_info'] = $customer_data; |
||
| 157 | |||
| 158 | } else { |
||
| 159 | |||
| 160 | $output['success'] = false; |
||
| 161 | |||
| 162 | } |
||
| 163 | |||
| 164 | do_action( 'give_post_edit_customer', $customer_id, $customer_data ); |
||
| 165 | |||
| 166 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
||
| 167 | header( 'Content-Type: application/json' ); |
||
| 168 | echo json_encode( $output ); |
||
| 169 | wp_die(); |
||
| 170 | } |
||
| 171 | |||
| 172 | return $output; |
||
| 173 | |||
| 174 | } |
||
| 175 | |||
| 404 |
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.