Complex classes like FrmListHelper 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 FrmListHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class FrmListHelper { |
||
| 7 | /** |
||
| 8 | * The current list of items |
||
| 9 | * |
||
| 10 | * @since 2.0.18 |
||
| 11 | * @var array |
||
| 12 | * @access public |
||
| 13 | */ |
||
| 14 | public $items; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @since 4.07 |
||
| 18 | */ |
||
| 19 | public $total_items = false; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Various information about the current table |
||
| 23 | * |
||
| 24 | * @since 2.0.18 |
||
| 25 | * @var array |
||
| 26 | * @access protected |
||
| 27 | */ |
||
| 28 | protected $_args; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Various information needed for displaying the pagination |
||
| 32 | * |
||
| 33 | * @since 2.0.18 |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $_pagination_args = array(); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The current screen |
||
| 40 | * |
||
| 41 | * @since 2.0.18 |
||
| 42 | * @var object |
||
| 43 | * @access protected |
||
| 44 | */ |
||
| 45 | protected $screen; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Cached bulk actions |
||
| 49 | * |
||
| 50 | * @since 2.0.18 |
||
| 51 | * @var array |
||
| 52 | * @access private |
||
| 53 | */ |
||
| 54 | private $_actions; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Cached pagination output |
||
| 58 | * |
||
| 59 | * @since 2.0.18 |
||
| 60 | * @var string |
||
| 61 | * @access private |
||
| 62 | */ |
||
| 63 | private $_pagination; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The view switcher modes. |
||
| 67 | * |
||
| 68 | * @since 2.0.18 |
||
| 69 | * @var array |
||
| 70 | * @access protected |
||
| 71 | */ |
||
| 72 | protected $modes = array(); |
||
| 73 | |||
| 74 | /** |
||
| 75 | * |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $params; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Stores the value returned by ->get_column_info() |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | protected $_column_headers; |
||
| 86 | |||
| 87 | protected $compat_fields = array( '_args', '_pagination_args', 'screen', '_actions', '_pagination' ); |
||
| 88 | |||
| 89 | protected $compat_methods = array( |
||
| 90 | 'set_pagination_args', |
||
| 91 | 'get_views', |
||
| 92 | 'get_bulk_actions', |
||
| 93 | 'bulk_actions', |
||
| 94 | 'row_actions', |
||
| 95 | 'view_switcher', |
||
| 96 | 'get_items_per_page', |
||
| 97 | 'pagination', |
||
| 98 | 'get_sortable_columns', |
||
| 99 | 'get_column_info', |
||
| 100 | 'get_table_classes', |
||
| 101 | 'display_tablenav', |
||
| 102 | 'extra_tablenav', |
||
| 103 | 'single_row_columns', |
||
| 104 | ); |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Construct the table object |
||
| 108 | */ |
||
| 109 | public function __construct( $args ) { |
||
| 148 | |||
| 149 | public function ajax_user_can() { |
||
| 152 | |||
| 153 | public function get_columns() { |
||
| 156 | |||
| 157 | public function display_rows() { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Prepares the list of items for displaying. |
||
| 165 | * |
||
| 166 | * @uses FrmListHelper::set_pagination_args() |
||
| 167 | * |
||
| 168 | * @since 2.0.18 |
||
| 169 | * @access public |
||
| 170 | * @abstract |
||
| 171 | */ |
||
| 172 | public function prepare_items() { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @since 3.0 |
||
| 178 | */ |
||
| 179 | protected function get_param( $args ) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * An internal method that sets all the necessary pagination arguments |
||
| 192 | * |
||
| 193 | * @param array $args An associative array with information about the pagination |
||
| 194 | * |
||
| 195 | * @access protected |
||
| 196 | * |
||
| 197 | * @param array|string $args |
||
| 198 | */ |
||
| 199 | protected function set_pagination_args( $args ) { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Access the pagination args. |
||
| 224 | * |
||
| 225 | * @since 2.0.18 |
||
| 226 | * @access public |
||
| 227 | * |
||
| 228 | * @param string $key Pagination argument to retrieve. Common values include 'total_items', |
||
| 229 | * 'total_pages', 'per_page', or 'infinite_scroll'. |
||
| 230 | * |
||
| 231 | * @return int Number of items that correspond to the given pagination argument. |
||
| 232 | */ |
||
| 233 | public function get_pagination_arg( $key ) { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Whether the table has items to display or not |
||
| 245 | * |
||
| 246 | * @since 2.0.18 |
||
| 247 | * @access public |
||
| 248 | * |
||
| 249 | * @return bool |
||
| 250 | */ |
||
| 251 | public function has_items() { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Message to be displayed when there are no items |
||
| 257 | * |
||
| 258 | * @since 2.0.18 |
||
| 259 | * @access public |
||
| 260 | */ |
||
| 261 | public function no_items() { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Display the search box. |
||
| 267 | * |
||
| 268 | * @since 2.0.18 |
||
| 269 | * @access public |
||
| 270 | * |
||
| 271 | * @param string $text The search button text |
||
| 272 | * @param string $input_id The search input id |
||
| 273 | */ |
||
| 274 | public function search_box( $text, $input_id ) { |
||
| 285 | |||
| 286 | private function hidden_search_inputs( $param_name ) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Get an associative array ( id => link ) with the list |
||
| 295 | * of views available on this table. |
||
| 296 | * |
||
| 297 | * @since 2.0.18 |
||
| 298 | * @access protected |
||
| 299 | * |
||
| 300 | * @return array |
||
| 301 | */ |
||
| 302 | protected function get_views() { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Display the list of views available on this table. |
||
| 308 | * |
||
| 309 | * @since 2.0.18 |
||
| 310 | * @access public |
||
| 311 | */ |
||
| 312 | public function views() { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Get an associative array ( option_name => option_title ) with the list |
||
| 340 | * of bulk actions available on this table. |
||
| 341 | * |
||
| 342 | * @since 2.0.18 |
||
| 343 | * @access protected |
||
| 344 | * |
||
| 345 | * @return array |
||
| 346 | */ |
||
| 347 | protected function get_bulk_actions() { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Display the bulk actions dropdown. |
||
| 353 | * |
||
| 354 | * @since 2.0.18 |
||
| 355 | * @access protected |
||
| 356 | * |
||
| 357 | * @param string $which The location of the bulk actions: 'top' or 'bottom'. |
||
| 358 | * This is designated as optional for backwards-compatibility. |
||
| 359 | */ |
||
| 360 | protected function bulk_actions( $which = '' ) { |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @return string if empty there will be no confirmation pop up |
||
| 415 | */ |
||
| 416 | protected function confirm_bulk_delete() { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Get the current action selected from the bulk actions dropdown. |
||
| 422 | * |
||
| 423 | * @since 2.0.18 |
||
| 424 | * @access public |
||
| 425 | * |
||
| 426 | * @return string|false The action name or False if no action was selected |
||
| 427 | */ |
||
| 428 | public function current_action() { |
||
| 440 | |||
| 441 | private static function get_bulk_action( $action_name ) { |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Generate row actions div |
||
| 458 | * |
||
| 459 | * @since 2.0.18 |
||
| 460 | * @access protected |
||
| 461 | * |
||
| 462 | * @param array $actions The list of actions |
||
| 463 | * @param bool $always_visible Whether the actions should be always visible |
||
| 464 | * |
||
| 465 | * @return string |
||
| 466 | */ |
||
| 467 | protected function row_actions( $actions, $always_visible = false ) { |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Display a view switcher |
||
| 491 | * |
||
| 492 | * @since 2.0.18 |
||
| 493 | * @access protected |
||
| 494 | * |
||
| 495 | * @param string $current_mode |
||
| 496 | */ |
||
| 497 | protected function view_switcher( $current_mode ) { |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Get the current page number |
||
| 522 | * |
||
| 523 | * @since 2.0.18 |
||
| 524 | * @access public |
||
| 525 | * |
||
| 526 | * @return int |
||
| 527 | */ |
||
| 528 | public function get_pagenum() { |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Get number of items to display on a single page |
||
| 540 | * |
||
| 541 | * @since 2.0.18 |
||
| 542 | * @access protected |
||
| 543 | * |
||
| 544 | * @param string $option |
||
| 545 | * @param int $default |
||
| 546 | * |
||
| 547 | * @return int |
||
| 548 | */ |
||
| 549 | protected function get_items_per_page( $option, $default = 20 ) { |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Display the pagination. |
||
| 573 | * |
||
| 574 | * @since 2.0.18 |
||
| 575 | * @access protected |
||
| 576 | * |
||
| 577 | * @param string $which |
||
| 578 | */ |
||
| 579 | protected function pagination( $which ) { |
||
| 670 | |||
| 671 | private function disabled_pages( $total_pages ) { |
||
| 696 | |||
| 697 | private function link_label( $link ) { |
||
| 707 | |||
| 708 | private function current_url() { |
||
| 713 | |||
| 714 | private function add_page_link( $atts ) { |
||
| 723 | |||
| 724 | private function add_disabled_link( $label ) { |
||
| 727 | |||
| 728 | private function add_active_link( $atts ) { |
||
| 740 | |||
| 741 | /** |
||
| 742 | * Get a list of sortable columns. The format is: |
||
| 743 | * 'internal-name' => 'orderby' |
||
| 744 | * or |
||
| 745 | * 'internal-name' => array( 'orderby', true ) |
||
| 746 | * |
||
| 747 | * The second format will make the initial sorting order be descending |
||
| 748 | * |
||
| 749 | * @since 2.0.18 |
||
| 750 | * @access protected |
||
| 751 | * |
||
| 752 | * @return array |
||
| 753 | */ |
||
| 754 | protected function get_sortable_columns() { |
||
| 757 | |||
| 758 | /** |
||
| 759 | * Gets the name of the default primary column. |
||
| 760 | * |
||
| 761 | * @since 4.3.0 |
||
| 762 | * @access protected |
||
| 763 | * |
||
| 764 | * @return string Name of the default primary column, in this case, an empty string. |
||
| 765 | */ |
||
| 766 | protected function get_default_primary_column_name() { |
||
| 783 | |||
| 784 | /** |
||
| 785 | * Gets the name of the primary column. |
||
| 786 | * |
||
| 787 | * @since 4.3.0 |
||
| 788 | * @access protected |
||
| 789 | * |
||
| 790 | * @return string The name of the primary column. |
||
| 791 | */ |
||
| 792 | protected function get_primary_column_name() { |
||
| 818 | |||
| 819 | /** |
||
| 820 | * Get a list of all, hidden and sortable columns, with filter applied |
||
| 821 | * |
||
| 822 | * @since 2.0.18 |
||
| 823 | * @access protected |
||
| 824 | * |
||
| 825 | * @return array |
||
| 826 | */ |
||
| 827 | protected function get_column_info() { |
||
| 876 | |||
| 877 | /** |
||
| 878 | * Return number of visible columns |
||
| 879 | * |
||
| 880 | * @since 2.0.18 |
||
| 881 | * @access public |
||
| 882 | * |
||
| 883 | * @return int |
||
| 884 | */ |
||
| 885 | public function get_column_count() { |
||
| 891 | |||
| 892 | /** |
||
| 893 | * Print column headers, accounting for hidden and sortable columns. |
||
| 894 | * |
||
| 895 | * @since 2.0.18 |
||
| 896 | * @access public |
||
| 897 | * |
||
| 898 | * @staticvar int $cb_counter |
||
| 899 | * |
||
| 900 | * @param bool $with_id Whether to set the id attribute or not |
||
| 901 | */ |
||
| 902 | public function print_column_headers( $with_id = true ) { |
||
| 975 | |||
| 976 | /** |
||
| 977 | * Display the table |
||
| 978 | * |
||
| 979 | * @since 2.0.18 |
||
| 980 | * @access public |
||
| 981 | */ |
||
| 982 | public function display() { |
||
| 1011 | |||
| 1012 | /** |
||
| 1013 | * Get a list of CSS classes for the list table table tag. |
||
| 1014 | * |
||
| 1015 | * @since 2.0.18 |
||
| 1016 | * @access protected |
||
| 1017 | * |
||
| 1018 | * @return array List of CSS classes for the table tag. |
||
| 1019 | */ |
||
| 1020 | protected function get_table_classes() { |
||
| 1023 | |||
| 1024 | /** |
||
| 1025 | * Generate the table navigation above or below the table |
||
| 1026 | * |
||
| 1027 | * @since 2.0.18 |
||
| 1028 | * @access protected |
||
| 1029 | * |
||
| 1030 | * @param string $which |
||
| 1031 | */ |
||
| 1032 | protected function display_tablenav( $which ) { |
||
| 1058 | |||
| 1059 | /** |
||
| 1060 | * Use this to exclude the footer labels and bulk items. |
||
| 1061 | * When close together, it feels like duplicates. |
||
| 1062 | * |
||
| 1063 | * @since 4.07 |
||
| 1064 | */ |
||
| 1065 | protected function has_min_items( $limit = 5 ) { |
||
| 1068 | |||
| 1069 | /** |
||
| 1070 | * Extra controls to be displayed between bulk actions and pagination |
||
| 1071 | * |
||
| 1072 | * @since 2.0.18 |
||
| 1073 | * @access protected |
||
| 1074 | * |
||
| 1075 | * @param string $which |
||
| 1076 | */ |
||
| 1077 | protected function extra_tablenav( $which ) { |
||
| 1079 | |||
| 1080 | /** |
||
| 1081 | * Generate the tbody element for the list table. |
||
| 1082 | * |
||
| 1083 | * @since 2.0.18 |
||
| 1084 | * @access public |
||
| 1085 | */ |
||
| 1086 | public function display_rows_or_placeholder() { |
||
| 1095 | |||
| 1096 | /** |
||
| 1097 | * Generates content for a single row of the table |
||
| 1098 | * |
||
| 1099 | * @since 2.0.18 |
||
| 1100 | * @access public |
||
| 1101 | * |
||
| 1102 | * @param object $item The current item |
||
| 1103 | */ |
||
| 1104 | public function single_row( $item ) { |
||
| 1109 | |||
| 1110 | /** |
||
| 1111 | * Generates the columns for a single row of the table |
||
| 1112 | * |
||
| 1113 | * @since 2.0.18 |
||
| 1114 | * @access protected |
||
| 1115 | * |
||
| 1116 | * @param object $item The current item |
||
| 1117 | */ |
||
| 1118 | protected function single_row_columns( $item ) { |
||
| 1159 | |||
| 1160 | /** |
||
| 1161 | * Generates and display row actions links for the list table. |
||
| 1162 | * |
||
| 1163 | * @since 4.3.0 |
||
| 1164 | * @access protected |
||
| 1165 | * |
||
| 1166 | * @param object $item The item being acted upon. |
||
| 1167 | * @param string $column_name Current column name. |
||
| 1168 | * @param string $primary Primary column name. |
||
| 1169 | * |
||
| 1170 | * @return string The row actions output. In this case, an empty string. |
||
| 1171 | */ |
||
| 1172 | protected function handle_row_actions( $item, $column_name, $primary ) { |
||
| 1175 | |||
| 1176 | /** |
||
| 1177 | * Handle an incoming ajax request (called from admin-ajax.php) |
||
| 1178 | * |
||
| 1179 | * @since 2.0.18 |
||
| 1180 | * @access public |
||
| 1181 | */ |
||
| 1182 | public function ajax_response() { |
||
| 1210 | |||
| 1211 | /** |
||
| 1212 | * Send required variables to JavaScript land |
||
| 1213 | * |
||
| 1214 | * @access public |
||
| 1215 | */ |
||
| 1216 | public function _js_vars() { |
||
| 1227 | } |
||
| 1228 |