Total Complexity | 52 |
Total Lines | 808 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like GetPaid_Meta_Box_Invoice_Items often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GetPaid_Meta_Box_Invoice_Items, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class GetPaid_Meta_Box_Invoice_Items { |
||
18 | |||
19 | public static function get_columns( $invoice ) { |
||
47 | } |
||
48 | |||
49 | public static function output( $post, $invoice = false ) { |
||
50 | |||
51 | if ( apply_filters( 'getpaid_use_new_invoice_items_metabox', false ) ) { |
||
52 | return self::output2( $post ); |
||
|
|||
53 | } |
||
54 | |||
55 | $post_id = ! empty( $post->ID ) ? $post->ID : 0; |
||
56 | $invoice = $invoice instanceof WPInv_Invoice ? $invoice : new WPInv_Invoice( $post_id ); |
||
57 | $use_taxes = $invoice->is_taxable() && wpinv_use_taxes(); |
||
58 | $item_types = apply_filters( 'wpinv_item_types_for_quick_add_item', wpinv_get_item_types(), $post ); |
||
59 | $columns = self::get_columns( $invoice ); |
||
60 | $cols = count( $columns ); |
||
61 | $class = ''; |
||
62 | |||
63 | unset( $item_types['adv'] ); |
||
64 | unset( $item_types['package'] ); |
||
65 | |||
66 | if ( $invoice->is_paid() ) { |
||
67 | $class .= ' wpinv-paid'; |
||
68 | } |
||
69 | |||
70 | if ( $invoice->is_refunded() ) { |
||
71 | $class .= ' wpinv-refunded'; |
||
72 | } |
||
73 | |||
74 | if ( $invoice->is_recurring() ) { |
||
75 | $class .= ' wpi-recurring'; |
||
76 | } |
||
77 | |||
78 | $refund_url = wp_nonce_url( |
||
79 | add_query_arg( |
||
80 | array( |
||
81 | 'getpaid-admin-action' => 'refund_invoice', |
||
82 | 'invoice_id' => $invoice->get_id(), |
||
83 | ) |
||
84 | ), |
||
85 | 'getpaid-nonce', |
||
86 | 'getpaid-nonce' |
||
87 | ); |
||
88 | ?> |
||
89 | |||
90 | <div class="wpinv-items-wrap<?php echo esc_attr( $class ); ?>" id="wpinv_items_wrap" data-status="<?php echo esc_attr( $invoice->get_status() ); ?>"> |
||
91 | <table id="wpinv_items" class="wpinv-items" cellspacing="0" cellpadding="0"> |
||
92 | |||
93 | <thead> |
||
94 | <tr> |
||
95 | <?php foreach ( $columns as $key => $label ) : ?> |
||
96 | <th class=" |
||
97 | <?php |
||
98 | echo esc_attr( $key ); |
||
99 | echo 'total' == $key || 'qty' == $key ? ' hide-if-amount' : ''; |
||
100 | ?> |
||
101 | "><?php echo wp_kses_post( $label ); ?></th> |
||
102 | <?php endforeach; ?> |
||
103 | </tr> |
||
104 | </thead> |
||
105 | |||
106 | <tbody class="wpinv-line-items"> |
||
107 | <?php |
||
108 | foreach ( $invoice->get_items() as $int => $item ) { |
||
109 | self::output_row( $columns, $item, $invoice, $int % 2 == 0 ? 'even' : 'odd' ); |
||
110 | } |
||
111 | ?> |
||
112 | </tbody> |
||
113 | |||
114 | <tfoot class="wpinv-totals"> |
||
115 | <tr> |
||
116 | <td colspan="<?php echo (int) $cols; ?>" style="padding:0;border:0"> |
||
117 | <div id="wpinv-quick-add"> |
||
118 | <table cellspacing="0" cellpadding="0"> |
||
119 | <tr> |
||
120 | <td class="id"> </td> |
||
121 | <td class="title"> |
||
122 | |||
123 | <div class="wp-clearfix"> |
||
124 | <label class="wpi-item-name"> |
||
125 | <span class="input-text-wrap"> |
||
126 | <input type="text" style="width: 100%" placeholder="<?php esc_attr_e( 'Item Name', 'invoicing' ); ?>" class="wpinv-quick-item-name" name="_wpinv_quick[name]"> |
||
127 | </span> |
||
128 | </label> |
||
129 | </div> |
||
130 | |||
131 | <div class="wp-clearfix"> |
||
132 | <label class="wpi-item-price"> |
||
133 | <span class="input-text-wrap"> |
||
134 | <input type="text" style="width: 200px" placeholder="<?php esc_attr_e( 'Item Price', 'invoicing' ); ?>" class="wpinv-quick-item-price" name="_wpinv_quick[price]"> |
||
135 | × <input type="text" style="width: 140px" placeholder="<?php esc_attr_e( 'Item Quantity', 'invoicing' ); ?>" class="wpinv-quick-item-qty" name="_wpinv_quick[qty]"> |
||
136 | </span> |
||
137 | </label> |
||
138 | </div> |
||
139 | |||
140 | <div class="wp-clearfix"> |
||
141 | <label class="wpi-item-name"> |
||
142 | <span class="input-text-wrap"> |
||
143 | <textarea rows="4" style="width: 100%" placeholder="<?php esc_attr_e( 'Item Description', 'invoicing' ); ?>" class="wpinv-quick-item-description" name="_wpinv_quick[description]"></textarea> |
||
144 | </span> |
||
145 | </label> |
||
146 | </div> |
||
147 | |||
148 | <div class="wp-clearfix"> |
||
149 | <label class="wpi-item-type"> |
||
150 | <span class="input-text-wrap"> |
||
151 | <?php |
||
152 | wpinv_html_select( |
||
153 | array( |
||
154 | 'options' => $item_types, |
||
155 | 'name' => '_wpinv_quick[type]', |
||
156 | 'id' => '_wpinv_quick_type', |
||
157 | 'selected' => 'custom', |
||
158 | 'show_option_all' => false, |
||
159 | 'show_option_none' => false, |
||
160 | 'class' => 'gdmbx2-text-medium wpinv-quick-type', |
||
161 | ) |
||
162 | ); |
||
163 | ?> |
||
164 | </span> |
||
165 | </label> |
||
166 | </div> |
||
167 | |||
168 | <?php if ( $use_taxes ) : ?> |
||
169 | <div class="wp-clearfix"> |
||
170 | <label class="wpi-vat-rule"> |
||
171 | <span class="input-text-wrap"> |
||
172 | <?php |
||
173 | wpinv_html_select( |
||
174 | array( |
||
175 | 'options' => array_merge( |
||
176 | array( '' => __( 'Select VAT Rule', 'invoicing' ) ), |
||
177 | getpaid_get_tax_rules() |
||
178 | ), |
||
179 | 'name' => '_wpinv_quick[vat_rule]', |
||
180 | 'id' => '_wpinv_quick_vat_rule', |
||
181 | 'show_option_all' => false, |
||
182 | 'show_option_none' => false, |
||
183 | 'class' => 'gdmbx2-text-medium wpinv-quick-vat-rule', |
||
184 | 'selected' => 'digital', |
||
185 | ) |
||
186 | ); |
||
187 | ?> |
||
188 | </span> |
||
189 | </label> |
||
190 | </div> |
||
191 | <div class="wp-clearfix"> |
||
192 | <label class="wpi-vat-class"> |
||
193 | <span class="input-text-wrap"> |
||
194 | <?php |
||
195 | wpinv_html_select( |
||
196 | array( |
||
197 | 'options' => array_merge( |
||
198 | array( '' => __( 'Select VAT Class', 'invoicing' ) ), |
||
199 | getpaid_get_tax_classes() |
||
200 | ), |
||
201 | 'name' => '_wpinv_quick[vat_class]', |
||
202 | 'id' => '_wpinv_quick_vat_class', |
||
203 | 'show_option_all' => false, |
||
204 | 'show_option_none' => false, |
||
205 | 'class' => 'gdmbx2-text-medium wpinv-quick-vat-class', |
||
206 | 'selected' => '_standard', |
||
207 | ) |
||
208 | ); |
||
209 | ?> |
||
210 | </span> |
||
211 | </label> |
||
212 | </div> |
||
213 | <?php endif; ?> |
||
214 | |||
215 | <div class="wp-clearfix bsui"> |
||
216 | <?php |
||
217 | aui()->input( |
||
218 | array( |
||
219 | 'type' => 'checkbox', |
||
220 | 'name' => '_wpinv_quick[one-time]', |
||
221 | 'id' => '_wpinv_quick-one-time', |
||
222 | 'label' => __( "One time item (won't be saved to regular items list)", 'invoicing' ), |
||
223 | 'value' => 1, |
||
224 | 'no_wrap' => true, |
||
225 | 'checked' => false, |
||
226 | ), |
||
227 | true |
||
228 | ); |
||
229 | ?> |
||
230 | </div> |
||
231 | |||
232 | <div class="wp-clearfix"> |
||
233 | <label class="wpi-item-actions"> |
||
234 | <span class="input-text-wrap"> |
||
235 | <input type="button" value="Save" class="button button-primary" id="wpinv-save-item"><input type="button" value="Cancel" class="button button-secondary" id="wpinv-cancel-item"> |
||
236 | </span> |
||
237 | </label> |
||
238 | </div> |
||
239 | </td> |
||
240 | </tr> |
||
241 | </table> |
||
242 | </div> |
||
243 | </td> |
||
244 | </tr> |
||
245 | <tr class="totals"> |
||
246 | <td colspan="<?php echo ( (int) $cols - 4 ); ?>"></td> |
||
247 | <td colspan="4"> |
||
248 | <table cellspacing="0" cellpadding="0"> |
||
249 | <tr class="subtotal"> |
||
250 | <td class="name"><?php esc_html_e( 'Sub Total:', 'invoicing' ); ?></td> |
||
251 | <td class="total"><?php wpinv_the_price( $invoice->get_subtotal(), $invoice->get_currency() ); ?></td> |
||
252 | <td class="action"></td> |
||
253 | </tr> |
||
254 | <tr class="discount"> |
||
255 | <td class="name"><?php esc_html_e( 'Discount:', 'invoicing' ); ?></td> |
||
256 | <td class="total"><?php wpinv_the_price( $invoice->get_total_discount(), $invoice->get_currency() ); ?></td> |
||
257 | <td class="action"></td> |
||
258 | </tr> |
||
259 | <?php if ( $use_taxes ) : ?> |
||
260 | <?php if ( is_array( $taxes = $invoice->get_taxes() ) && wpinv_display_individual_tax_rates() ) { ?> |
||
261 | <?php foreach ( $taxes as $tax_key => $tax_item ) { ?> |
||
262 | <tr class="tax"> |
||
263 | <td class="name"><?php echo esc_html( $invoice->get_tax_item_name( $tax_key, $tax_item, ':' ) ); ?></td> |
||
264 | <td class="total"><?php wpinv_the_price( $invoice->get_tax_item_amount( $tax_key, $tax_item ), $invoice->get_currency() ); ?></td> |
||
265 | <td class="action"></td> |
||
266 | </tr> |
||
267 | <?php } ?> |
||
268 | <?php } else { ?> |
||
269 | <tr class="tax"> |
||
270 | <td class="name"><?php esc_html_e( 'Tax:', 'invoicing' ); ?></td> |
||
271 | <td class="total"><?php wpinv_the_price( $invoice->get_total_tax(), $invoice->get_currency() ); ?></td> |
||
272 | <td class="action"></td> |
||
273 | </tr> |
||
274 | <?php } ?> |
||
275 | <?php endif; ?> |
||
276 | <tr class="total"> |
||
277 | <td class="name"><?php esc_html_e( 'Total:', 'invoicing' ); ?></td> |
||
278 | <td class="total"><?php wpinv_the_price( $invoice->get_total(), $invoice->get_currency() ); ?></td> |
||
279 | <td class="action"></td> |
||
280 | </tr> |
||
281 | </table> |
||
282 | </td> |
||
283 | </tr> |
||
284 | </tfoot> |
||
285 | |||
286 | </table> |
||
287 | |||
288 | <!-- Add items to an invoice --> |
||
289 | <div class="bsui"> |
||
290 | <div class="modal fade" id="getpaid-refund-invoice-modal" tabindex="-1" role="dialog" aria-labelledby="getpaid-refund-invoice-modal-label" aria-hidden="true"> |
||
291 | <div class="modal-dialog modal-dialog-centered" role="document"> |
||
292 | <div class="modal-content"> |
||
293 | <div class="modal-header"> |
||
294 | <h5 class="modal-title" id="getpaid-refund-invoice-modal-label"><?php esc_html_e( 'Refund Payment', 'invoicing' ); ?></h5> |
||
295 | <button type="button" class="close btn-close" data-bs-dismiss="modal" data-dismiss="modal" aria-label="<?php esc_html_e( 'Close', 'invoicing' ); ?>"> |
||
296 | <?php if ( empty( $GLOBALS['aui_bs5'] ) ) : ?> |
||
297 | <span aria-hidden="true">×</span> |
||
298 | <?php endif; ?> |
||
299 | </button> |
||
300 | </div> |
||
301 | <div class="modal-body"> |
||
302 | <p> |
||
303 | <?php esc_html_e( 'Are you sure you want to refund this payment?', 'invoicing' ); ?> |
||
304 | </p> |
||
305 | <?php if ( getpaid_payment_gateway_supports( $invoice->get_gateway(), 'refunds' ) ) : ?> |
||
306 | <?php |
||
307 | aui()->input( |
||
308 | array( |
||
309 | 'type' => 'checkbox', |
||
310 | 'name' => 'getpaid_refund_remote', |
||
311 | 'id' => 'getpaid_refund_remote', |
||
312 | 'label' => sprintf( |
||
313 | 'Refund payment in %s', |
||
314 | wpinv_get_gateway_admin_label( $invoice->get_gateway() ) |
||
315 | ), |
||
316 | 'value' => 1, |
||
317 | 'class' => 'getpaid-refund-field', |
||
318 | ), |
||
319 | true |
||
320 | ); |
||
321 | ?> |
||
322 | <?php endif; ?> |
||
323 | |||
324 | <?php if ( getpaid_get_invoice_subscriptions( $invoice ) ) : ?> |
||
325 | <?php |
||
326 | aui()->input( |
||
327 | array( |
||
328 | 'type' => 'checkbox', |
||
329 | 'name' => 'getpaid_cancel_subscription', |
||
330 | 'id' => 'getpaid_cancel_subscription', |
||
331 | 'label' => __( 'Cancel subscription', 'invoicing' ), |
||
332 | 'value' => 1, |
||
333 | 'class' => 'getpaid-refund-field', |
||
334 | ), |
||
335 | true |
||
336 | ); |
||
337 | ?> |
||
338 | <?php endif; ?> |
||
339 | </div> |
||
340 | <div class="modal-footer"> |
||
341 | <button type="button" class="btn btn-secondary getpaid-cancel" data-bs-dismiss="modal" data-dismiss="modal"><?php esc_html_e( 'Cancel', 'invoicing' ); ?></button> |
||
342 | <a |
||
343 | href="<?php echo esc_url_raw( $refund_url ); ?>" |
||
344 | data-href="<?php echo esc_url_raw( $refund_url ); ?>" |
||
345 | class="btn btn-primary getpaid-refund-payment-button" |
||
346 | ><?php esc_html_e( 'Refund', 'invoicing' ); ?></a> |
||
347 | <script> |
||
348 | // Update the refund URL when the user changes the refund options. |
||
349 | jQuery( function( $ ) { |
||
350 | $( '.getpaid-refund-field' ).on( 'change', function() { |
||
351 | var $this = $( this ), |
||
352 | $modal = $this.closest( '.modal' ), |
||
353 | $button = $modal.find( '.getpaid-refund-payment-button' ), |
||
354 | href = $button.data( 'href' ); |
||
355 | |||
356 | $( '.getpaid-refund-field:checked' ).each( function() { |
||
357 | href = href.replace( 'getpaid-admin-action=refund_invoice', 'getpaid-admin-action=refund_invoice&' + $( this ).attr( 'name' ) + '=1' ); |
||
358 | } ); |
||
359 | |||
360 | $button.attr( 'href', href ); |
||
361 | } ); |
||
362 | } ); |
||
363 | </script> |
||
364 | </div> |
||
365 | </div> |
||
366 | </div> |
||
367 | </div> |
||
368 | </div> |
||
369 | |||
370 | <div class="wpinv-actions"> |
||
371 | <?php |
||
372 | if ( $invoice->is_paid() ) { |
||
373 | |||
374 | printf( |
||
375 | '<span class="bsui"><button type="button" class="button button-primary" data-toggle="modal" data-bs-toggle="modal" data-bs-target="#getpaid-refund-invoice-modal" data-target="#getpaid-refund-invoice-modal">%s</button></span>', |
||
376 | esc_html__( 'Refund', 'invoicing' ) |
||
377 | ); |
||
378 | } elseif ( ! $invoice->is_refunded() ) { |
||
379 | wpinv_item_dropdown( |
||
380 | array( |
||
381 | 'name' => 'wpinv_invoice_item', |
||
382 | 'id' => 'wpinv_invoice_item', |
||
383 | 'show_recurring' => true, |
||
384 | 'class' => 'wpi_select2', |
||
385 | ) |
||
386 | ); |
||
387 | |||
388 | echo ' ' . '<button type="button" class="button button-primary" id="wpinv-add-item">' . sprintf( esc_html__( 'Add item to %s', 'invoicing' ), esc_html( $invoice->get_label() ) ) . '</button>'; |
||
389 | echo ' ' . '<button type="button" class="button button-primary" id="wpinv-new-item">' . esc_html__( 'Create new item', 'invoicing' ) . '</button>'; |
||
390 | echo ' ' . '<button type="button" class="button button-primary wpinv-flr" id="wpinv-recalc-totals">' . esc_html__( 'Recalculate Totals', 'invoicing' ) . '</button>'; |
||
391 | |||
392 | } |
||
393 | ?> |
||
394 | <?php do_action( 'wpinv_invoice_items_actions', $invoice ); ?> |
||
395 | </div> |
||
396 | </div> |
||
397 | <?php |
||
398 | } |
||
399 | |||
400 | public static function output_row( $columns, $item, $invoice, $class = 'even' ) { |
||
401 | |||
402 | ?> |
||
403 | <tr class="item item-<?php echo esc_attr( $class ); ?>" data-item-id="<?php echo esc_attr( $item->get_id() ); ?>"> |
||
404 | <?php foreach ( array_keys( $columns ) as $column ) : ?> |
||
405 | <td class=" |
||
406 | <?php |
||
407 | echo esc_attr( $column ); |
||
408 | echo 'total' == $column || 'qty' == $column ? ' hide-if-amount' : ''; |
||
409 | ?> |
||
410 | "> |
||
411 | <?php |
||
412 | switch ( $column ) { |
||
413 | case 'id': |
||
414 | echo (int) $item->get_id(); |
||
415 | break; |
||
416 | case 'title': |
||
417 | printf( |
||
418 | '<a href="%s" target="_blank">%s</a>', |
||
419 | esc_url( get_edit_post_link( $item->get_id() ) ), |
||
420 | esc_html( $item->get_raw_name() ) |
||
421 | ); |
||
422 | |||
423 | $summary = apply_filters( 'getpaid_admin_invoice_line_item_summary', $item->get_description(), $item, $invoice ); |
||
424 | if ( $summary !== '' ) { |
||
425 | printf( |
||
426 | '<span class="meta">%s</span>', |
||
427 | wp_kses_post( wpautop( $summary ) ) |
||
428 | ); |
||
429 | } |
||
430 | |||
431 | printf( |
||
432 | '<input type="hidden" value="%s" name="getpaid_items[%s][name]" class="getpaid-recalculate-prices-on-change" />', |
||
433 | esc_attr( $item->get_raw_name() ), |
||
434 | (int) $item->get_id() |
||
435 | ); |
||
436 | |||
437 | printf( |
||
438 | '<textarea style="display: none;" name="getpaid_items[%s][description]" class="getpaid-recalculate-prices-on-change">%s</textarea>', |
||
439 | (int) $item->get_id(), |
||
440 | esc_attr( $item->get_description() ) |
||
441 | ); |
||
442 | |||
443 | break; |
||
444 | case 'price': |
||
445 | printf( |
||
446 | '<input type="text" value="%s" name="getpaid_items[%s][price]" style="width: 100px;" class="getpaid-admin-invoice-item-price getpaid-recalculate-prices-on-change" />', |
||
447 | esc_attr( getpaid_unstandardize_amount( $item->get_price() ) ), |
||
448 | (int) $item->get_id() |
||
449 | ); |
||
450 | |||
451 | break; |
||
452 | case 'qty': |
||
453 | printf( |
||
454 | '<input type="text" style="width: 100px;" value="%s" name="getpaid_items[%s][quantity]" class="getpaid-admin-invoice-item-quantity getpaid-recalculate-prices-on-change" />', |
||
455 | floatval( $item->get_quantity() ), |
||
456 | (int) $item->get_id() |
||
457 | ); |
||
458 | |||
459 | break; |
||
460 | case 'total': |
||
461 | wpinv_the_price( $item->get_sub_total(), $invoice->get_currency() ); |
||
462 | |||
463 | break; |
||
464 | case 'tax': |
||
465 | echo floatval( wpinv_round_amount( getpaid_get_invoice_tax_rate( $invoice, $item ), 2 ) ) . '%'; |
||
466 | |||
467 | break; |
||
468 | case 'action': |
||
469 | if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) { |
||
470 | echo '<i class="fa fa-trash wpinv-item-remove"></i>'; |
||
471 | } |
||
472 | break; |
||
473 | } |
||
474 | do_action( 'getpaid_admin_edit_invoice_item_' . $column, $item, $invoice ); |
||
475 | ?> |
||
476 | </td> |
||
477 | <?php endforeach; ?> |
||
478 | </tr> |
||
479 | <?php |
||
480 | } |
||
481 | |||
482 | /** |
||
483 | * Output the metabox. |
||
484 | * |
||
485 | * @param WP_Post $post |
||
486 | */ |
||
487 | public static function output2( $post ) { |
||
825 | </div> |
||
826 | </div> |
||
827 | </div> |
||
834 |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.