Conditions | 35 |
Paths | 60 |
Total Lines | 87 |
Code Lines | 49 |
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 |
||
274 | public static function save( $post_id ) { |
||
275 | |||
276 | // Prepare the invoice. |
||
277 | $invoice = new WPInv_Invoice( $post_id ); |
||
278 | |||
279 | // Load new data. |
||
280 | $invoice->set_props( |
||
281 | array( |
||
282 | 'template' => isset( $_POST['wpinv_template'] ) ? wpinv_clean( $_POST['wpinv_template'] ) : null, |
||
283 | 'email_cc' => isset( $_POST['wpinv_cc'] ) ? wpinv_clean( $_POST['wpinv_cc'] ) : null, |
||
284 | 'disable_taxes' => ! empty( $_POST['disable_taxes'] ), |
||
285 | 'currency' => isset( $_POST['wpinv_currency'] ) ? wpinv_clean( $_POST['wpinv_currency'] ) : null, |
||
286 | 'gateway' => ( $invoice->needs_payment() && isset( $_POST['wpinv_gateway'] ) ) ? wpinv_clean( $_POST['wpinv_gateway'] ) : null, |
||
287 | 'address' => isset( $_POST['wpinv_address'] ) ? wpinv_clean( $_POST['wpinv_address'] ) : null, |
||
288 | 'vat_number' => isset( $_POST['wpinv_vat_number'] ) ? wpinv_clean( $_POST['wpinv_vat_number'] ) : null, |
||
289 | 'company' => isset( $_POST['wpinv_company'] ) ? wpinv_clean( $_POST['wpinv_company'] ) : null, |
||
290 | 'company_id' => isset( $_POST['wpinv_company_id'] ) ? wpinv_clean( $_POST['wpinv_company_id'] ) : null, |
||
291 | 'zip' => isset( $_POST['wpinv_zip'] ) ? wpinv_clean( $_POST['wpinv_zip'] ) : null, |
||
292 | 'state' => isset( $_POST['wpinv_state'] ) ? wpinv_clean( $_POST['wpinv_state'] ) : null, |
||
293 | 'city' => isset( $_POST['wpinv_city'] ) ? wpinv_clean( $_POST['wpinv_city'] ) : null, |
||
294 | 'country' => isset( $_POST['wpinv_country'] ) ? wpinv_clean( $_POST['wpinv_country'] ) : null, |
||
295 | 'phone' => isset( $_POST['wpinv_phone'] ) ? wpinv_clean( $_POST['wpinv_phone'] ) : null, |
||
296 | 'first_name' => isset( $_POST['wpinv_first_name'] ) ? wpinv_clean( $_POST['wpinv_first_name'] ) : null, |
||
297 | 'last_name' => isset( $_POST['wpinv_last_name'] ) ? wpinv_clean( $_POST['wpinv_last_name'] ) : null, |
||
298 | 'author' => isset( $_POST['post_author_override'] ) ? wpinv_clean( $_POST['post_author_override'] ) : null, |
||
299 | 'date_created' => isset( $_POST['date_created'] ) ? wpinv_clean( $_POST['date_created'] ) : null, |
||
300 | 'date_completed' => isset( $_POST['wpinv_date_completed'] ) ? wpinv_clean( $_POST['wpinv_date_completed'] ) : null, |
||
301 | 'due_date' => isset( $_POST['wpinv_due_date'] ) ? wpinv_clean( $_POST['wpinv_due_date'] ) : null, |
||
302 | 'number' => isset( $_POST['wpinv_number'] ) ? wpinv_clean( $_POST['wpinv_number'] ) : null, |
||
303 | 'status' => isset( $_POST['wpinv_status'] ) ? wpinv_clean( $_POST['wpinv_status'] ) : null, |
||
304 | ) |
||
305 | ); |
||
306 | |||
307 | // Discount code. |
||
308 | if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) { |
||
309 | |||
310 | if ( isset( $_POST['wpinv_discount_code'] ) ) { |
||
311 | $invoice->set_discount_code( wpinv_clean( $_POST['wpinv_discount_code'] ) ); |
||
|
|||
312 | } |
||
313 | |||
314 | $discount = new WPInv_Discount( $invoice->get_discount_code() ); |
||
315 | if ( $discount->exists() ) { |
||
316 | $invoice->add_discount( getpaid_calculate_invoice_discount( $invoice, $discount ) ); |
||
317 | } else { |
||
318 | $invoice->remove_discount( 'discount_code' ); |
||
319 | } |
||
320 | |||
321 | // Recalculate totals. |
||
322 | $invoice->recalculate_total(); |
||
323 | |||
324 | } |
||
325 | |||
326 | // If we're creating a new user... |
||
327 | if ( ! empty( $_POST['wpinv_new_user'] ) && is_email( stripslashes( $_POST['wpinv_email'] ) ) ) { |
||
328 | |||
329 | // Attempt to create the user. |
||
330 | $user = wpinv_create_user( sanitize_email( stripslashes( $_POST['wpinv_email'] ) ), $invoice->get_first_name() . $invoice->get_last_name() ); |
||
331 | |||
332 | // If successful, update the invoice author. |
||
333 | if ( is_numeric( $user ) ) { |
||
334 | $invoice->set_author( $user ); |
||
335 | } else { |
||
336 | wpinv_error_log( $user->get_error_message(), __( 'Invoice add new user', 'invoicing' ), __FILE__, __LINE__ ); |
||
337 | } |
||
338 | } |
||
339 | |||
340 | // Do not send new invoice notifications. |
||
341 | $GLOBALS['wpinv_skip_invoice_notification'] = true; |
||
342 | |||
343 | // Save the invoice. |
||
344 | $invoice->save(); |
||
345 | |||
346 | // Undo do not send new invoice notifications. |
||
347 | $GLOBALS['wpinv_skip_invoice_notification'] = false; |
||
348 | |||
349 | // (Maybe) send new user notification. |
||
350 | $should_send_notification = wpinv_get_option( 'disable_new_user_emails' ); |
||
351 | if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ) ) ) { |
||
352 | wp_send_new_user_notifications( $user, 'user' ); |
||
353 | } |
||
354 | |||
355 | if ( ! empty( $_POST['send_to_customer'] ) && ! $invoice->is_draft() ) { |
||
356 | getpaid()->get( 'invoice_emails' )->user_invoice( $invoice, true ); |
||
357 | } |
||
358 | |||
359 | // Fires after an invoice is saved. |
||
360 | do_action( 'wpinv_invoice_metabox_saved', $invoice ); |
||
361 | } |
||
363 |