Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Give_Payment_History_Table 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Give_Payment_History_Table, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class Give_Payment_History_Table extends WP_List_Table { |
||
30 | |||
31 | /** |
||
32 | * Number of results to show per page |
||
33 | * |
||
34 | * @var string |
||
35 | * @since 1.0 |
||
36 | */ |
||
37 | public $per_page = 30; |
||
38 | |||
39 | /** |
||
40 | * URL of this page |
||
41 | * |
||
42 | * @var string |
||
43 | * @since 1.0.1 |
||
44 | */ |
||
45 | public $base_url; |
||
46 | |||
47 | /** |
||
48 | * Total number of payments |
||
49 | * |
||
50 | * @var int |
||
51 | * @since 1.0 |
||
52 | */ |
||
53 | public $total_count; |
||
54 | |||
55 | /** |
||
56 | * Total number of complete payments |
||
57 | * |
||
58 | * @var int |
||
59 | * @since 1.0 |
||
60 | */ |
||
61 | public $complete_count; |
||
62 | |||
63 | /** |
||
64 | * Total number of pending payments |
||
65 | * |
||
66 | * @var int |
||
67 | * @since 1.0 |
||
68 | */ |
||
69 | public $pending_count; |
||
70 | |||
71 | /** |
||
72 | * Total number of processing payments |
||
73 | * |
||
74 | * @var int |
||
75 | * @since 1.8.9 |
||
76 | */ |
||
77 | public $processing_count; |
||
78 | |||
79 | /** |
||
80 | * Total number of refunded payments |
||
81 | * |
||
82 | * @var int |
||
83 | * @since 1.0 |
||
84 | */ |
||
85 | public $refunded_count; |
||
86 | |||
87 | /** |
||
88 | * Total number of failed payments |
||
89 | * |
||
90 | * @var int |
||
91 | * @since 1.0 |
||
92 | */ |
||
93 | public $failed_count; |
||
94 | |||
95 | /** |
||
96 | * Total number of revoked payments |
||
97 | * |
||
98 | * @var int |
||
99 | * @since 1.0 |
||
100 | */ |
||
101 | public $revoked_count; |
||
102 | |||
103 | /** |
||
104 | * Total number of cancelled payments |
||
105 | * |
||
106 | * @var int |
||
107 | * @since 1.4 |
||
108 | */ |
||
109 | public $cancelled_count; |
||
110 | |||
111 | /** |
||
112 | * Total number of abandoned payments |
||
113 | * |
||
114 | * @var int |
||
115 | * @since 1.6 |
||
116 | */ |
||
117 | public $abandoned_count; |
||
118 | |||
119 | /** |
||
120 | * Total number of pre-approved payments |
||
121 | * |
||
122 | * @var int |
||
123 | * @since 1.8.13 |
||
124 | */ |
||
125 | public $preapproval_count; |
||
126 | |||
127 | /** |
||
128 | * Get things started. |
||
129 | * |
||
130 | * @since 1.0 |
||
131 | * @uses Give_Payment_History_Table::get_payment_counts() |
||
132 | * @see WP_List_Table::__construct() |
||
133 | */ |
||
134 | public function __construct() { |
||
149 | |||
150 | /** |
||
151 | * Add donation search filter. |
||
152 | * |
||
153 | * @return void |
||
154 | */ |
||
155 | public function advanced_filters() { |
||
223 | |||
224 | /** |
||
225 | * Show the search field |
||
226 | * |
||
227 | * @param string $text Label for the search box. |
||
228 | * @param string $input_id ID of the search box. |
||
229 | * |
||
230 | * @since 1.0 |
||
231 | * @access public |
||
232 | * |
||
233 | * @return void |
||
234 | */ |
||
235 | public function search_box( $text, $input_id ) { |
||
273 | |||
274 | /** |
||
275 | * Retrieve the view types |
||
276 | * |
||
277 | * @access public |
||
278 | * @since 1.0 |
||
279 | * |
||
280 | * @return array $views All the views available |
||
281 | */ |
||
282 | public function get_views() { |
||
283 | |||
284 | $current = isset( $_GET['status'] ) ? $_GET['status'] : ''; |
||
285 | $views = array(); |
||
286 | $tabs = array( |
||
287 | 'all' => array( |
||
288 | 'total_count', |
||
289 | __( 'All', 'give' ), |
||
290 | ), |
||
291 | 'publish' => array( |
||
292 | 'complete_count', |
||
293 | __( 'Completed', 'give' ), |
||
294 | ), |
||
295 | 'pending' => array( |
||
296 | 'pending_count', |
||
297 | __( 'Pending', 'give' ), |
||
298 | ), |
||
299 | 'processing' => array( |
||
300 | 'processing_count', |
||
301 | __( 'Processing', 'give' ), |
||
302 | ), |
||
303 | 'refunded' => array( |
||
304 | 'refunded_count', |
||
305 | __( 'Refunded', 'give' ), |
||
306 | ), |
||
307 | 'revoked' => array( |
||
308 | 'revoked_count', |
||
309 | __( 'Revoked', 'give' ), |
||
310 | ), |
||
311 | 'failed' => array( |
||
312 | 'failed_count', |
||
313 | __( 'Failed', 'give' ), |
||
314 | ), |
||
315 | 'cancelled' => array( |
||
316 | 'cancelled_count', |
||
317 | __( 'Cancelled', 'give' ), |
||
318 | ), |
||
319 | 'abandoned' => array( |
||
320 | 'abandoned_count', |
||
321 | __( 'Abandoned', 'give' ), |
||
322 | ), |
||
323 | 'preapproval' => array( |
||
324 | 'preapproval_count', |
||
325 | __( 'Preapproval Pending', 'give' ), |
||
326 | ), |
||
327 | ); |
||
328 | |||
329 | /** |
||
330 | * Remove Query from Args of the URL that are being pass to Donation Status. |
||
331 | * |
||
332 | * @since 1.8.18 |
||
333 | */ |
||
334 | $args = (array) apply_filters( 'give_payments_table_status_remove_query_arg', array( 'paged', '_wpnonce', '_wp_http_referer' ) ); |
||
335 | |||
336 | // Build URL. |
||
337 | $staus_url = remove_query_arg( $args ); |
||
338 | |||
339 | foreach ( $tabs as $key => $tab ) { |
||
340 | $count_key = $tab[0]; |
||
341 | $name = $tab[1]; |
||
342 | $count = $this->$count_key; |
||
343 | |||
344 | /** |
||
345 | * Filter can be used to show all the status inside the donation tabs. |
||
346 | * |
||
347 | * Filter can be used to show all the status inside the donation submenu tabs return true to show all the tab. |
||
348 | * |
||
349 | * @param string $key Current view tab value. |
||
350 | * @param int $count Number of donation inside the tab. |
||
351 | * |
||
352 | * @since 1.8.12 |
||
353 | */ |
||
354 | if ( 'all' === $key || $key === $current || apply_filters( 'give_payments_table_show_all_status', 0 < $count, $key, $count ) ) { |
||
355 | |||
356 | $staus_url = 'all' === $key ? |
||
357 | add_query_arg( array( 'status' => false ), $staus_url ) : |
||
358 | add_query_arg( array( 'status' => $key ), $staus_url ); |
||
359 | |||
360 | $views[ $key ] = sprintf( |
||
361 | '<a href="%s"%s>%s <span class="count">(%s)</span></a>', |
||
362 | esc_url( $staus_url ), |
||
363 | ( ( 'all' === $key && empty( $current ) ) ) ? ' class="current"' : ( $current == $key ? 'class="current"' : '' ), |
||
364 | $name, |
||
365 | $count |
||
366 | ); |
||
367 | } |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * Filter the donation listing page views. |
||
372 | * |
||
373 | * @since 1.0 |
||
374 | * |
||
375 | * @param array $views |
||
376 | * @param Give_Payment_History_Table |
||
377 | */ |
||
378 | return apply_filters( 'give_payments_table_views', $views, $this ); |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * Retrieve the table columns |
||
383 | * |
||
384 | * @access public |
||
385 | * @since 1.0 |
||
386 | * |
||
387 | * @return array $columns Array of all the list table columns |
||
388 | */ |
||
389 | public function get_columns() { |
||
405 | |||
406 | /** |
||
407 | * Retrieve the table's sortable columns |
||
408 | * |
||
409 | * @access public |
||
410 | * @since 1.0 |
||
411 | * |
||
412 | * @return array Array of all the sortable columns |
||
413 | */ |
||
414 | public function get_sortable_columns() { |
||
425 | |||
426 | /** |
||
427 | * Gets the name of the primary column. |
||
428 | * |
||
429 | * @since 1.5 |
||
430 | * @access protected |
||
431 | * |
||
432 | * @return string Name of the primary column. |
||
433 | */ |
||
434 | protected function get_primary_column_name() { |
||
437 | |||
438 | /** |
||
439 | * This function renders most of the columns in the list table. |
||
440 | * |
||
441 | * @param Give_Payment $payment Payment ID. |
||
442 | * @param string $column_name The name of the column. |
||
443 | * |
||
444 | * @access public |
||
445 | * @since 1.0 |
||
446 | * |
||
447 | * @return string Column Name |
||
448 | */ |
||
449 | public function column_default( $payment, $column_name ) { |
||
506 | |||
507 | /** |
||
508 | * Get donor email html. |
||
509 | * |
||
510 | * @param object $payment Contains all the data of the payment. |
||
511 | * |
||
512 | * @access public |
||
513 | * @since 1.0 |
||
514 | * |
||
515 | * @return string Data shown in the Email column |
||
516 | */ |
||
517 | public function get_donor_email( $payment ) { |
||
529 | |||
530 | /** |
||
531 | * Get Row Actions |
||
532 | * |
||
533 | * @param object $payment Payment Data. |
||
534 | * |
||
535 | * @since 1.6 |
||
536 | * |
||
537 | * @return array $actions |
||
538 | */ |
||
539 | function get_row_actions( $payment ) { |
||
580 | |||
581 | |||
582 | /** |
||
583 | * Get payment status html. |
||
584 | * |
||
585 | * @param object $payment Contains all the data of the payment. |
||
586 | * |
||
587 | * @access public |
||
588 | * @since 1.0 |
||
589 | * |
||
590 | * @return string Data shown in the Email column |
||
591 | */ |
||
592 | function get_payment_status( $payment ) { |
||
604 | |||
605 | /** |
||
606 | * Get checkbox html. |
||
607 | * |
||
608 | * @param object $payment Contains all the data for the checkbox column. |
||
609 | * |
||
610 | * @access public |
||
611 | * @since 1.0 |
||
612 | * |
||
613 | * @return string Displays a checkbox. |
||
614 | */ |
||
615 | public function column_cb( $payment ) { |
||
618 | |||
619 | /** |
||
620 | * Get payment ID html. |
||
621 | * |
||
622 | * @param object $payment Contains all the data for the checkbox column. |
||
623 | * @access public |
||
624 | * @since 1.0 |
||
625 | * |
||
626 | * @return string Displays a checkbox. |
||
627 | */ |
||
628 | public function get_payment_id( $payment ) { |
||
631 | |||
632 | /** |
||
633 | * Get donor html. |
||
634 | * |
||
635 | * @param object $payment Contains all the data of the payment. |
||
636 | * |
||
637 | * @access public |
||
638 | * @since 1.0 |
||
639 | * |
||
640 | * @return string Data shown in the User column |
||
641 | */ |
||
642 | public function get_donor( $payment ) { |
||
669 | |||
670 | /** |
||
671 | * Retrieve the bulk actions |
||
672 | * |
||
673 | * @access public |
||
674 | * @since 1.0 |
||
675 | * |
||
676 | * @return array $actions Array of the bulk actions |
||
677 | */ |
||
678 | public function get_bulk_actions() { |
||
695 | |||
696 | /** |
||
697 | * Process the bulk actions |
||
698 | * |
||
699 | * @access public |
||
700 | * @since 1.0 |
||
701 | * |
||
702 | * @return void |
||
703 | */ |
||
704 | public function process_bulk_action() { |
||
777 | |||
778 | /** |
||
779 | * Retrieve the payment counts |
||
780 | * |
||
781 | * @access public |
||
782 | * @since 1.0 |
||
783 | * |
||
784 | * @return object |
||
785 | */ |
||
786 | public function get_payment_counts() { |
||
832 | |||
833 | /** |
||
834 | * Retrieve all the data for all the payments. |
||
835 | * |
||
836 | * @access public |
||
837 | * @since 1.0 |
||
838 | * |
||
839 | * @return array objects in array containing all the data for the payments |
||
840 | */ |
||
841 | public function payments_data() { |
||
900 | |||
901 | /** |
||
902 | * Setup the final data for the table |
||
903 | * |
||
904 | * @access public |
||
905 | * @since 1.0 |
||
906 | * @uses Give_Payment_History_Table::get_columns() |
||
907 | * @uses Give_Payment_History_Table::get_sortable_columns() |
||
908 | * @uses Give_Payment_History_Table::payments_data() |
||
909 | * @uses WP_List_Table::get_pagenum() |
||
910 | * @uses WP_List_Table::set_pagination_args() |
||
911 | * |
||
912 | * @return void |
||
913 | */ |
||
914 | public function prepare_items() { |
||
984 | } |
||
985 |