| Conditions | 25 |
| Paths | > 20000 |
| Total Lines | 129 |
| Code Lines | 83 |
| 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 |
||
| 245 | public static function save( $post_id, $data, $post ) { |
||
| 246 | $invoice = new WPInv_Invoice( $post_id ); |
||
| 247 | |||
| 248 | // Billing |
||
| 249 | $first_name = sanitize_text_field( $data['wpinv_first_name'] ); |
||
| 250 | $last_name = sanitize_text_field( $data['wpinv_last_name'] ); |
||
| 251 | $company = sanitize_text_field( $data['wpinv_company'] ); |
||
| 252 | $vat_number = sanitize_text_field( $data['wpinv_vat_number'] ); |
||
| 253 | $phone = sanitize_text_field( $data['wpinv_phone'] ); |
||
| 254 | $address = sanitize_text_field( $data['wpinv_address'] ); |
||
| 255 | $city = sanitize_text_field( $data['wpinv_city'] ); |
||
| 256 | $zip = sanitize_text_field( $data['wpinv_zip'] ); |
||
| 257 | $country = sanitize_text_field( $data['wpinv_country'] ); |
||
| 258 | $state = sanitize_text_field( $data['wpinv_state'] ); |
||
| 259 | |||
| 260 | // Details |
||
| 261 | $status = sanitize_text_field( $data['wpinv_status'] ); |
||
| 262 | $old_status = !empty( $data['original_post_status'] ) ? sanitize_text_field( $data['original_post_status'] ) : $status; |
||
| 263 | $number = sanitize_text_field( $data['wpinv_number'] ); |
||
| 264 | $due_date = isset( $data['wpinv_due_date'] ) ? sanitize_text_field( $data['wpinv_due_date'] ) : ''; |
||
| 265 | $date_created = isset( $data['date_created'] ) ? sanitize_text_field( $data['date_created'] ) : ''; |
||
| 266 | //$discounts = sanitize_text_field( $data['wpinv_discounts'] ); |
||
| 267 | //$discount = sanitize_text_field( $data['wpinv_discount'] ); |
||
| 268 | |||
| 269 | $disable_taxes = 0; |
||
| 270 | |||
| 271 | if ( ! empty( $data['disable_taxes'] ) ) { |
||
| 272 | $disable_taxes = 1; |
||
| 273 | } |
||
| 274 | |||
| 275 | $ip = $invoice->get_ip() ? $invoice->get_ip() : wpinv_get_ip(); |
||
| 276 | |||
| 277 | $invoice->set( 'due_date', $due_date ); |
||
| 278 | $invoice->set( 'first_name', $first_name ); |
||
| 279 | $invoice->set( 'last_name', $last_name ); |
||
| 280 | $invoice->set( 'company', $company ); |
||
| 281 | $invoice->set( 'vat_number', $vat_number ); |
||
| 282 | $invoice->set( 'phone', $phone ); |
||
| 283 | $invoice->set( 'address', $address ); |
||
| 284 | $invoice->set( 'city', $city ); |
||
| 285 | $invoice->set( 'zip', $zip ); |
||
| 286 | $invoice->set( 'country', $country ); |
||
| 287 | $invoice->set( 'state', $state ); |
||
| 288 | $invoice->set( 'status', $status ); |
||
| 289 | $invoice->set( 'set', $status ); |
||
| 290 | //$invoice->set( 'number', $number ); |
||
| 291 | //$invoice->set( 'discounts', $discounts ); |
||
| 292 | //$invoice->set( 'discount', $discount ); |
||
| 293 | $invoice->set( 'disable_taxes', $disable_taxes ); |
||
| 294 | $invoice->set( 'ip', $ip ); |
||
| 295 | $invoice->old_status = $_POST['original_post_status']; |
||
| 296 | |||
| 297 | $currency = $invoice->get_currency(); |
||
| 298 | if ( ! empty( $data['wpinv_currency'] ) ) { |
||
| 299 | $currency = sanitize_text_field( $data['wpinv_currency'] ); |
||
| 300 | } |
||
| 301 | |||
| 302 | if ( empty( $currency ) ) { |
||
| 303 | $currency = wpinv_get_currency(); |
||
| 304 | } |
||
| 305 | |||
| 306 | if ( ! $invoice->is_paid() ) { |
||
| 307 | $invoice->currency = $currency; |
||
| 308 | } |
||
| 309 | |||
| 310 | if ( !empty( $data['wpinv_gateway'] ) ) { |
||
| 311 | $invoice->set( 'gateway', sanitize_text_field( $data['wpinv_gateway'] ) ); |
||
| 312 | } |
||
| 313 | $saved = $invoice->save(); |
||
| 314 | |||
| 315 | $emails = ''; |
||
| 316 | if ( ! empty( $data['wpinv_cc'] ) ) { |
||
| 317 | $emails = wpinv_clean( $data['wpinv_cc'] ); |
||
| 318 | } |
||
| 319 | update_post_meta( $invoice->ID, 'wpinv_email_cc', $emails ); |
||
| 320 | |||
| 321 | if ( ! empty( $date_created ) && strtotime( $date_created, current_time( 'timestamp' ) ) ) { |
||
| 322 | |||
| 323 | $time = strtotime( $date_created, current_time( 'timestamp' ) ); |
||
| 324 | $date = date( 'Y-m-d H:i:s', $time ); |
||
| 325 | $date_gmt = get_gmt_from_date( $date ); |
||
| 326 | |||
| 327 | wp_update_post( |
||
| 328 | array( |
||
| 329 | 'ID' => $invoice->ID, |
||
| 330 | 'post_date' => $date, |
||
| 331 | 'post_date_gmt' => $date_gmt, |
||
| 332 | 'edit_date' => true, |
||
| 333 | ) |
||
| 334 | ); |
||
| 335 | |||
| 336 | $invoice->date = $date; |
||
| 337 | } |
||
| 338 | |||
| 339 | // Check for payment notes |
||
| 340 | if ( !empty( $data['invoice_note'] ) ) { |
||
| 341 | $note = wp_kses( $data['invoice_note'], array() ); |
||
| 342 | $note_type = sanitize_text_field( $data['invoice_note_type'] ); |
||
| 343 | $is_customer_note = $note_type == 'customer' ? 1 : 0; |
||
| 344 | |||
| 345 | wpinv_insert_payment_note( $invoice->ID, $note, $is_customer_note ); |
||
| 346 | } |
||
| 347 | |||
| 348 | // Update user address if empty. |
||
| 349 | if ( $saved && !empty( $invoice ) ) { |
||
| 350 | if ( $user_id = $invoice->get_user_id() ) { |
||
| 351 | $user_address = wpinv_get_user_address( $user_id, false ); |
||
| 352 | |||
| 353 | if (empty($user_address['first_name'])) { |
||
| 354 | update_user_meta( $user_id, '_wpinv_first_name', $first_name ); |
||
| 355 | update_user_meta( $user_id, '_wpinv_last_name', $last_name ); |
||
| 356 | } else if (empty($user_address['last_name']) && $user_address['first_name'] == $first_name) { |
||
| 357 | update_user_meta( $user_id, '_wpinv_last_name', $last_name ); |
||
| 358 | } |
||
| 359 | |||
| 360 | if (empty($user_address['address']) || empty($user_address['city']) || empty($user_address['state']) || empty($user_address['country'])) { |
||
| 361 | update_user_meta( $user_id, '_wpinv_address', $address ); |
||
| 362 | update_user_meta( $user_id, '_wpinv_city', $city ); |
||
| 363 | update_user_meta( $user_id, '_wpinv_state', $state ); |
||
| 364 | update_user_meta( $user_id, '_wpinv_country', $country ); |
||
| 365 | update_user_meta( $user_id, '_wpinv_zip', $zip ); |
||
| 366 | update_user_meta( $user_id, '_wpinv_phone', $phone ); |
||
| 367 | } |
||
| 368 | } |
||
| 369 | |||
| 370 | do_action( 'wpinv_invoice_metabox_saved', $invoice ); |
||
| 371 | } |
||
| 372 | |||
| 373 | return $saved; |
||
| 374 | } |
||
| 376 |