| Total Complexity | 138 |
| Total Lines | 1125 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
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.
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 | * Various information about the current table |
||
| 18 | * |
||
| 19 | * @since 2.0.18 |
||
| 20 | * @var array |
||
| 21 | * @access protected |
||
| 22 | */ |
||
| 23 | protected $_args; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Various information needed for displaying the pagination |
||
| 27 | * |
||
| 28 | * @since 2.0.18 |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $_pagination_args = array(); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The current screen |
||
| 35 | * |
||
| 36 | * @since 2.0.18 |
||
| 37 | * @var object |
||
| 38 | * @access protected |
||
| 39 | */ |
||
| 40 | protected $screen; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Cached bulk actions |
||
| 44 | * |
||
| 45 | * @since 2.0.18 |
||
| 46 | * @var array |
||
| 47 | * @access private |
||
| 48 | */ |
||
| 49 | private $_actions; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Cached pagination output |
||
| 53 | * |
||
| 54 | * @since 2.0.18 |
||
| 55 | * @var string |
||
| 56 | * @access private |
||
| 57 | */ |
||
| 58 | private $_pagination; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The view switcher modes. |
||
| 62 | * |
||
| 63 | * @since 2.0.18 |
||
| 64 | * @var array |
||
| 65 | * @access protected |
||
| 66 | */ |
||
| 67 | protected $modes = array(); |
||
| 68 | |||
| 69 | /** |
||
| 70 | * |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | protected $params; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Stores the value returned by ->get_column_info() |
||
| 77 | * |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | protected $_column_headers; |
||
| 81 | |||
| 82 | protected $compat_fields = array( '_args', '_pagination_args', 'screen', '_actions', '_pagination' ); |
||
| 83 | |||
| 84 | protected $compat_methods = array( 'set_pagination_args', 'get_views', 'get_bulk_actions', 'bulk_actions', 'row_actions', 'view_switcher', 'get_items_per_page', 'pagination', 'get_sortable_columns', 'get_column_info', 'get_table_classes', 'display_tablenav', 'extra_tablenav', 'single_row_columns' ); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Construct the table object |
||
| 88 | */ |
||
| 89 | public function __construct( $args ) { |
||
| 125 | |||
| 126 | public function ajax_user_can() { |
||
| 129 | |||
| 130 | public function get_columns() { |
||
| 133 | |||
| 134 | public function display_rows() { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Prepares the list of items for displaying. |
||
| 142 | * @uses FrmListHelper::set_pagination_args() |
||
| 143 | * |
||
| 144 | * @since 2.0.18 |
||
| 145 | * @access public |
||
| 146 | * @abstract |
||
| 147 | */ |
||
| 148 | public function prepare_items() { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @since 3.0 |
||
| 154 | */ |
||
| 155 | protected function get_param( $args ) { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * An internal method that sets all the necessary pagination arguments |
||
| 166 | * |
||
| 167 | * @param array $args An associative array with information about the pagination |
||
| 168 | * @access protected |
||
| 169 | * |
||
| 170 | * @param array|string $args |
||
| 171 | */ |
||
| 172 | protected function set_pagination_args( $args ) { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Access the pagination args. |
||
| 194 | * |
||
| 195 | * @since 2.0.18 |
||
| 196 | * @access public |
||
| 197 | * |
||
| 198 | * @param string $key Pagination argument to retrieve. Common values include 'total_items', |
||
| 199 | * 'total_pages', 'per_page', or 'infinite_scroll'. |
||
| 200 | * @return int Number of items that correspond to the given pagination argument. |
||
| 201 | */ |
||
| 202 | public function get_pagination_arg( $key ) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Whether the table has items to display or not |
||
| 214 | * |
||
| 215 | * @since 2.0.18 |
||
| 216 | * @access public |
||
| 217 | * |
||
| 218 | * @return bool |
||
| 219 | */ |
||
| 220 | public function has_items() { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Message to be displayed when there are no items |
||
| 226 | * |
||
| 227 | * @since 2.0.18 |
||
| 228 | * @access public |
||
| 229 | */ |
||
| 230 | public function no_items() { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Display the search box. |
||
| 236 | * |
||
| 237 | * @since 2.0.18 |
||
| 238 | * @access public |
||
| 239 | * |
||
| 240 | * @param string $text The search button text |
||
| 241 | * @param string $input_id The search input id |
||
| 242 | */ |
||
| 243 | public function search_box( $text, $input_id ) { |
||
| 261 | |||
| 262 | private function hidden_search_inputs( $param_name ) { |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Get an associative array ( id => link ) with the list |
||
| 270 | * of views available on this table. |
||
| 271 | * |
||
| 272 | * @since 2.0.18 |
||
| 273 | * @access protected |
||
| 274 | * |
||
| 275 | * @return array |
||
| 276 | */ |
||
| 277 | protected function get_views() { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Display the list of views available on this table. |
||
| 283 | * |
||
| 284 | * @since 2.0.18 |
||
| 285 | * @access public |
||
| 286 | */ |
||
| 287 | public function views() { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Get an associative array ( option_name => option_title ) with the list |
||
| 315 | * of bulk actions available on this table. |
||
| 316 | * |
||
| 317 | * @since 2.0.18 |
||
| 318 | * @access protected |
||
| 319 | * |
||
| 320 | * @return array |
||
| 321 | */ |
||
| 322 | protected function get_bulk_actions() { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Display the bulk actions dropdown. |
||
| 328 | * |
||
| 329 | * @since 2.0.18 |
||
| 330 | * @access protected |
||
| 331 | * |
||
| 332 | * @param string $which The location of the bulk actions: 'top' or 'bottom'. |
||
| 333 | * This is designated as optional for backwards-compatibility. |
||
| 334 | */ |
||
| 335 | protected function bulk_actions( $which = '' ) { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Get the current action selected from the bulk actions dropdown. |
||
| 381 | * |
||
| 382 | * @since 2.0.18 |
||
| 383 | * @access public |
||
| 384 | * |
||
| 385 | * @return string|false The action name or False if no action was selected |
||
| 386 | */ |
||
| 387 | public function current_action() { |
||
| 399 | |||
| 400 | private static function get_bulk_action( $action_name ) { |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Generate row actions div |
||
| 414 | * |
||
| 415 | * @since 2.0.18 |
||
| 416 | * @access protected |
||
| 417 | * |
||
| 418 | * @param array $actions The list of actions |
||
| 419 | * @param bool $always_visible Whether the actions should be always visible |
||
| 420 | * @return string |
||
| 421 | */ |
||
| 422 | protected function row_actions( $actions, $always_visible = false ) { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Display a view switcher |
||
| 445 | * |
||
| 446 | * @since 2.0.18 |
||
| 447 | * @access protected |
||
| 448 | * |
||
| 449 | * @param string $current_mode |
||
| 450 | */ |
||
| 451 | protected function view_switcher( $current_mode ) { |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Get the current page number |
||
| 476 | * |
||
| 477 | * @since 2.0.18 |
||
| 478 | * @access public |
||
| 479 | * |
||
| 480 | * @return int |
||
| 481 | */ |
||
| 482 | public function get_pagenum() { |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Get number of items to display on a single page |
||
| 494 | * |
||
| 495 | * @since 2.0.18 |
||
| 496 | * @access protected |
||
| 497 | * |
||
| 498 | * @param string $option |
||
| 499 | * @param int $default |
||
| 500 | * @return int |
||
| 501 | */ |
||
| 502 | protected function get_items_per_page( $option, $default = 20 ) { |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Display the pagination. |
||
| 526 | * |
||
| 527 | * @since 2.0.18 |
||
| 528 | * @access protected |
||
| 529 | * |
||
| 530 | * @param string $which |
||
| 531 | */ |
||
| 532 | protected function pagination( $which ) { |
||
| 611 | |||
| 612 | private function disabled_pages( $total_pages ) { |
||
| 637 | |||
| 638 | private function link_label( $link ) { |
||
| 647 | |||
| 648 | private function current_url() { |
||
| 653 | |||
| 654 | private function add_page_link( $atts ) { |
||
| 662 | |||
| 663 | private function add_disabled_link( $label ) { |
||
| 666 | |||
| 667 | private function add_active_link( $atts ) { |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Get a list of sortable columns. The format is: |
||
| 678 | * 'internal-name' => 'orderby' |
||
| 679 | * or |
||
| 680 | * 'internal-name' => array( 'orderby', true ) |
||
| 681 | * |
||
| 682 | * The second format will make the initial sorting order be descending |
||
| 683 | * |
||
| 684 | * @since 2.0.18 |
||
| 685 | * @access protected |
||
| 686 | * |
||
| 687 | * @return array |
||
| 688 | */ |
||
| 689 | protected function get_sortable_columns() { |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Gets the name of the default primary column. |
||
| 695 | * |
||
| 696 | * @since 4.3.0 |
||
| 697 | * @access protected |
||
| 698 | * |
||
| 699 | * @return string Name of the default primary column, in this case, an empty string. |
||
| 700 | */ |
||
| 701 | protected function get_default_primary_column_name() { |
||
| 718 | |||
| 719 | /** |
||
| 720 | * Gets the name of the primary column. |
||
| 721 | * |
||
| 722 | * @since 4.3.0 |
||
| 723 | * @access protected |
||
| 724 | * |
||
| 725 | * @return string The name of the primary column. |
||
| 726 | */ |
||
| 727 | protected function get_primary_column_name() { |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Get a list of all, hidden and sortable columns, with filter applied |
||
| 756 | * |
||
| 757 | * @since 2.0.18 |
||
| 758 | * @access protected |
||
| 759 | * |
||
| 760 | * @return array |
||
| 761 | */ |
||
| 762 | protected function get_column_info() { |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Return number of visible columns |
||
| 813 | * |
||
| 814 | * @since 2.0.18 |
||
| 815 | * @access public |
||
| 816 | * |
||
| 817 | * @return int |
||
| 818 | */ |
||
| 819 | public function get_column_count() { |
||
| 824 | |||
| 825 | /** |
||
| 826 | * Print column headers, accounting for hidden and sortable columns. |
||
| 827 | * |
||
| 828 | * @since 2.0.18 |
||
| 829 | * @access public |
||
| 830 | * |
||
| 831 | * @staticvar int $cb_counter |
||
| 832 | * |
||
| 833 | * @param bool $with_id Whether to set the id attribute or not |
||
| 834 | */ |
||
| 835 | public function print_column_headers( $with_id = true ) { |
||
| 904 | |||
| 905 | /** |
||
| 906 | * Display the table |
||
| 907 | * |
||
| 908 | * @since 2.0.18 |
||
| 909 | * @access public |
||
| 910 | */ |
||
| 911 | public function display() { |
||
| 937 | |||
| 938 | /** |
||
| 939 | * Get a list of CSS classes for the list table table tag. |
||
| 940 | * |
||
| 941 | * @since 2.0.18 |
||
| 942 | * @access protected |
||
| 943 | * |
||
| 944 | * @return array List of CSS classes for the table tag. |
||
| 945 | */ |
||
| 946 | protected function get_table_classes() { |
||
| 949 | |||
| 950 | /** |
||
| 951 | * Generate the table navigation above or below the table |
||
| 952 | * |
||
| 953 | * @since 2.0.18 |
||
| 954 | * @access protected |
||
| 955 | * @param string $which |
||
| 956 | */ |
||
| 957 | protected function display_tablenav( $which ) { |
||
| 976 | |||
| 977 | /** |
||
| 978 | * Extra controls to be displayed between bulk actions and pagination |
||
| 979 | * |
||
| 980 | * @since 2.0.18 |
||
| 981 | * @access protected |
||
| 982 | * |
||
| 983 | * @param string $which |
||
| 984 | */ |
||
| 985 | protected function extra_tablenav( $which ) {} |
||
| 986 | |||
| 987 | /** |
||
| 988 | * Generate the tbody element for the list table. |
||
| 989 | * |
||
| 990 | * @since 2.0.18 |
||
| 991 | * @access public |
||
| 992 | */ |
||
| 993 | public function display_rows_or_placeholder() { |
||
| 1002 | |||
| 1003 | /** |
||
| 1004 | * Generates content for a single row of the table |
||
| 1005 | * |
||
| 1006 | * @since 2.0.18 |
||
| 1007 | * @access public |
||
| 1008 | * |
||
| 1009 | * @param object $item The current item |
||
| 1010 | */ |
||
| 1011 | public function single_row( $item ) { |
||
| 1016 | |||
| 1017 | /** |
||
| 1018 | * Generates the columns for a single row of the table |
||
| 1019 | * |
||
| 1020 | * @since 2.0.18 |
||
| 1021 | * @access protected |
||
| 1022 | * |
||
| 1023 | * @param object $item The current item |
||
| 1024 | */ |
||
| 1025 | protected function single_row_columns( $item ) { |
||
| 1066 | |||
| 1067 | /** |
||
| 1068 | * Generates and display row actions links for the list table. |
||
| 1069 | * |
||
| 1070 | * @since 4.3.0 |
||
| 1071 | * @access protected |
||
| 1072 | * |
||
| 1073 | * @param object $item The item being acted upon. |
||
| 1074 | * @param string $column_name Current column name. |
||
| 1075 | * @param string $primary Primary column name. |
||
| 1076 | * @return string The row actions output. In this case, an empty string. |
||
| 1077 | */ |
||
| 1078 | protected function handle_row_actions( $item, $column_name, $primary ) { |
||
| 1081 | |||
| 1082 | /** |
||
| 1083 | * Handle an incoming ajax request (called from admin-ajax.php) |
||
| 1084 | * |
||
| 1085 | * @since 2.0.18 |
||
| 1086 | * @access public |
||
| 1087 | */ |
||
| 1088 | public function ajax_response() { |
||
| 1115 | |||
| 1116 | /** |
||
| 1117 | * Send required variables to JavaScript land |
||
| 1118 | * |
||
| 1119 | * @access public |
||
| 1120 | */ |
||
| 1121 | public function _js_vars() { |
||
| 1122 | $args = array( |
||
| 1123 | 'class' => get_class( $this ), |
||
| 1124 | 'screen' => array( |
||
| 1125 | 'id' => $this->screen->id, |
||
| 1126 | 'base' => $this->screen->base, |
||
| 1127 | ), |
||
| 1128 | ); |
||
| 1129 | |||
| 1130 | printf( "<script type='text/javascript'>list_args = %s;</script>\n", wp_json_encode( $args ) ); |
||
| 1131 | } |
||
| 1133 |