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 refunded payments |
||
| 73 | * |
||
| 74 | * @var int |
||
| 75 | * @since 1.0 |
||
| 76 | */ |
||
| 77 | public $refunded_count; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Total number of failed payments |
||
| 81 | * |
||
| 82 | * @var int |
||
| 83 | * @since 1.0 |
||
| 84 | */ |
||
| 85 | public $failed_count; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Total number of revoked payments |
||
| 89 | * |
||
| 90 | * @var int |
||
| 91 | * @since 1.0 |
||
| 92 | */ |
||
| 93 | public $revoked_count; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Total number of cancelled payments |
||
| 97 | * |
||
| 98 | * @var int |
||
| 99 | * @since 1.4 |
||
| 100 | */ |
||
| 101 | public $cancelled_count; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Total number of abandoned payments |
||
| 105 | * |
||
| 106 | * @var int |
||
| 107 | * @since 1.6 |
||
| 108 | */ |
||
| 109 | public $abandoned_count; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Get things started |
||
| 113 | * |
||
| 114 | * @since 1.0 |
||
| 115 | * @uses Give_Payment_History_Table::get_payment_counts() |
||
| 116 | * @see WP_List_Table::__construct() |
||
| 117 | */ |
||
| 118 | public function __construct() { |
||
| 133 | |||
| 134 | public function advanced_filters() { |
||
| 135 | $start_date = isset( $_GET['start-date'] ) ? sanitize_text_field( $_GET['start-date'] ) : null; |
||
| 136 | $end_date = isset( $_GET['end-date'] ) ? sanitize_text_field( $_GET['end-date'] ) : null; |
||
| 137 | $status = isset( $_GET['status'] ) ? $_GET['status'] : ''; |
||
| 138 | ?> |
||
| 139 | <div id="give-payment-filters"> |
||
| 140 | <span id="give-payment-date-filters"> |
||
| 141 | <label for="start-date" class="give-start-date-label"><?php _e( 'Start Date:', 'give' ); ?></label> |
||
| 142 | <input type="text" id="start-date" name="start-date" class="give_datepicker" value="<?php echo $start_date; ?>" placeholder="mm/dd/yyyy"/> |
||
| 143 | <label for="end-date" class="give-end-date-label"><?php _e( 'End Date:', 'give' ); ?></label> |
||
| 144 | <input type="text" id="end-date" name="end-date" class="give_datepicker" value="<?php echo $end_date; ?>" placeholder="mm/dd/yyyy"/> |
||
| 145 | <input type="submit" class="button-secondary" value="<?php _e( 'Apply', 'give' ); ?>"/> |
||
| 146 | </span> |
||
| 147 | <?php if ( ! empty( $status ) ) : ?> |
||
| 148 | <input type="hidden" name="status" value="<?php echo esc_attr( $status ); ?>"/> |
||
| 149 | <?php endif; ?> |
||
| 150 | <?php if ( ! empty( $start_date ) || ! empty( $end_date ) ) : ?> |
||
| 151 | <a href="<?php echo admin_url( 'edit.php?post_type=give_forms&page=give-payment-history' ); ?>" class="button-secondary"><?php _e( 'Clear Filter', 'give' ); ?></a> |
||
| 152 | <?php endif; ?> |
||
| 153 | <?php $this->search_box( __( 'Search', 'give' ), 'give-payments' ); ?> |
||
| 154 | </div> |
||
| 155 | |||
| 156 | <?php |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Show the search field |
||
| 161 | * |
||
| 162 | * @since 1.0 |
||
| 163 | * @access public |
||
| 164 | * |
||
| 165 | * @param string $text Label for the search box |
||
| 166 | * @param string $input_id ID of the search box |
||
| 167 | * |
||
| 168 | * @return void |
||
| 169 | */ |
||
| 170 | public function search_box( $text, $input_id ) { |
||
| 171 | if ( empty( $_REQUEST['s'] ) && ! $this->has_items() ) { |
||
| 172 | return; |
||
| 173 | } |
||
| 174 | |||
| 175 | $input_id = $input_id . '-search-input'; |
||
| 176 | |||
| 177 | if ( ! empty( $_REQUEST['orderby'] ) ) { |
||
| 178 | echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />'; |
||
| 179 | } |
||
| 180 | if ( ! empty( $_REQUEST['order'] ) ) { |
||
| 181 | echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />'; |
||
| 182 | } |
||
| 183 | ?> |
||
| 184 | <p class="search-box"> |
||
| 185 | <?php do_action( 'give_payment_history_search' ); ?> |
||
| 186 | <label class="screen-reader-text" for="<?php echo $input_id ?>"><?php echo $text; ?>:</label> |
||
| 187 | <input type="search" id="<?php echo $input_id ?>" name="s" value="<?php _admin_search_query(); ?>"/> |
||
| 188 | <?php submit_button( $text, 'button', false, false, array( 'ID' => 'search-submit' ) ); ?><br/> |
||
| 189 | </p> |
||
| 190 | <?php |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Retrieve the view types |
||
| 195 | * |
||
| 196 | * @access public |
||
| 197 | * @since 1.0 |
||
| 198 | * @return array $views All the views available |
||
| 199 | */ |
||
| 200 | public function get_views() { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Retrieve the table columns |
||
| 252 | * |
||
| 253 | * @access public |
||
| 254 | * @since 1.0 |
||
| 255 | * @return array $columns Array of all the list table columns |
||
| 256 | */ |
||
| 257 | public function get_columns() { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Retrieve the table's sortable columns |
||
| 275 | * |
||
| 276 | * @access public |
||
| 277 | * @since 1.0 |
||
| 278 | * @return array Array of all the sortable columns |
||
| 279 | */ |
||
| 280 | public function get_sortable_columns() { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Gets the name of the primary column. |
||
| 292 | * |
||
| 293 | * @since 1.5 |
||
| 294 | * @access protected |
||
| 295 | * |
||
| 296 | * @return string Name of the primary column. |
||
| 297 | */ |
||
| 298 | protected function get_primary_column_name() { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * This function renders most of the columns in the list table. |
||
| 304 | * |
||
| 305 | * @access public |
||
| 306 | * @since 1.0 |
||
| 307 | * |
||
| 308 | * @param array $item Contains all the data of the discount code |
||
| 309 | * @param string $column_name The name of the column |
||
| 310 | * |
||
| 311 | * @return string Column Name |
||
| 312 | */ |
||
| 313 | public function column_default( $payment, $column_name ) { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Render the Email Column |
||
| 352 | * |
||
| 353 | * @access public |
||
| 354 | * @since 1.0 |
||
| 355 | * |
||
| 356 | * @param array $payment Contains all the data of the payment |
||
| 357 | * |
||
| 358 | * @return string Data shown in the Email column |
||
| 359 | */ |
||
| 360 | public function column_email( $payment ) { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Render the checkbox column |
||
| 398 | * |
||
| 399 | * @access public |
||
| 400 | * @since 1.0 |
||
| 401 | * |
||
| 402 | * @param array $payment Contains all the data for the checkbox column |
||
| 403 | * |
||
| 404 | * @return string Displays a checkbox |
||
| 405 | */ |
||
| 406 | public function column_cb( $payment ) { |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Render the ID column |
||
| 416 | * |
||
| 417 | * @access public |
||
| 418 | * @since 1.0 |
||
| 419 | * |
||
| 420 | * @param array $payment Contains all the data for the checkbox column |
||
| 421 | * |
||
| 422 | * @return string Displays a checkbox |
||
| 423 | */ |
||
| 424 | public function column_ID( $payment ) { |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Render the User Column |
||
| 430 | * |
||
| 431 | * @access public |
||
| 432 | * @since 1.0 |
||
| 433 | * |
||
| 434 | * @param array $payment Contains all the data of the payment |
||
| 435 | * |
||
| 436 | * @return string Data shown in the User column |
||
| 437 | */ |
||
| 438 | public function column_donor( $payment ) { |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Retrieve the bulk actions |
||
| 455 | * |
||
| 456 | * @access public |
||
| 457 | * @since 1.0 |
||
| 458 | * @return array $actions Array of the bulk actions |
||
| 459 | */ |
||
| 460 | public function get_bulk_actions() { |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Process the bulk actions |
||
| 478 | * |
||
| 479 | * @access public |
||
| 480 | * @since 1.0 |
||
| 481 | * @return void |
||
| 482 | */ |
||
| 483 | public function process_bulk_action() { |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Retrieve the payment counts |
||
| 545 | * |
||
| 546 | * @access public |
||
| 547 | * @since 1.0 |
||
| 548 | * @return void |
||
| 549 | */ |
||
| 550 | public function get_payment_counts() { |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Retrieve all the data for all the payments |
||
| 592 | * |
||
| 593 | * @access public |
||
| 594 | * @since 1.0 |
||
| 595 | * @return array $payment_data Array of all the data for the payments |
||
| 596 | */ |
||
| 597 | public function payments_data() { |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Setup the final data for the table |
||
| 648 | * |
||
| 649 | * @access public |
||
| 650 | * @since 1.0 |
||
| 651 | * @uses Give_Payment_History_Table::get_columns() |
||
| 652 | * @uses Give_Payment_History_Table::get_sortable_columns() |
||
| 653 | * @uses Give_Payment_History_Table::payments_data() |
||
| 654 | * @uses WP_List_Table::get_pagenum() |
||
| 655 | * @uses WP_List_Table::set_pagination_args() |
||
| 656 | * @return void |
||
| 657 | */ |
||
| 658 | public function prepare_items() { |
||
| 713 | } |
||
| 714 |
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.