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 | * 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 ) { |
||
| 90 | $args = wp_parse_args( $args, array( |
||
|
|
|||
| 91 | 'params' => array(), |
||
| 92 | 'plural' => '', |
||
| 93 | 'singular' => '', |
||
| 94 | 'ajax' => false, |
||
| 95 | 'screen' => null, |
||
| 96 | ) ); |
||
| 97 | |||
| 98 | $this->params = $args['params']; |
||
| 99 | |||
| 100 | $this->screen = convert_to_screen( $args['screen'] ); |
||
| 101 | |||
| 102 | add_filter( "manage_{$this->screen->id}_columns", array( $this, 'get_columns' ), 0 ); |
||
| 103 | |||
| 104 | if ( ! $args['plural'] ) { |
||
| 105 | $args['plural'] = $this->screen->base; |
||
| 106 | } |
||
| 107 | |||
| 108 | $args['plural'] = sanitize_key( $args['plural'] ); |
||
| 109 | $args['singular'] = sanitize_key( $args['singular'] ); |
||
| 110 | |||
| 111 | $this->_args = $args; |
||
| 112 | |||
| 113 | if ( $args['ajax'] ) { |
||
| 114 | // wp_enqueue_script( 'list-table' ); |
||
| 115 | add_action( 'admin_footer', array( $this, '_js_vars' ) ); |
||
| 116 | } |
||
| 117 | |||
| 118 | if ( empty( $this->modes ) ) { |
||
| 119 | $this->modes = array( |
||
| 120 | 'list' => __( 'List View' ), |
||
| 121 | 'excerpt' => __( 'Excerpt View' ), |
||
| 122 | ); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | public function ajax_user_can() { |
||
| 127 | return current_user_can( 'administrator' ); |
||
| 128 | } |
||
| 129 | |||
| 130 | public function get_columns() { |
||
| 131 | return array(); |
||
| 132 | } |
||
| 133 | |||
| 134 | public function display_rows() { |
||
| 135 | foreach ( $this->items as $item ) { |
||
| 136 | echo "\n\t", $this->single_row( $item ); |
||
| 137 | } |
||
| 138 | } |
||
| 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() { |
||
| 149 | die( 'function FrmListHelper::prepare_items() must be over-ridden in a sub-class.' ); |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @since 3.0 |
||
| 154 | */ |
||
| 155 | protected function get_param( $args ) { |
||
| 156 | return FrmAppHelper::get_simple_request( array( |
||
| 157 | 'param' => $args['param'], |
||
| 158 | 'default' => isset( $args['default'] ) ? $args['default'] : '', |
||
| 159 | 'sanitize' => isset( $args['sanitize'] ) ? $args['sanitize'] : 'sanitize_title', |
||
| 160 | 'type' => 'request', |
||
| 161 | ) ); |
||
| 162 | } |
||
| 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 ) { |
||
| 173 | $args = wp_parse_args( $args, array( |
||
| 174 | 'total_items' => 0, |
||
| 175 | 'total_pages' => 0, |
||
| 176 | 'per_page' => 0, |
||
| 177 | ) ); |
||
| 178 | |||
| 179 | if ( ! $args['total_pages'] && $args['per_page'] > 0 ) { |
||
| 180 | $args['total_pages'] = ceil( $args['total_items'] / $args['per_page'] ); |
||
| 181 | } |
||
| 182 | |||
| 183 | // Redirect if page number is invalid and headers are not already sent. |
||
| 184 | if ( ! headers_sent() && ! FrmAppHelper::wp_doing_ajax() && $args['total_pages'] > 0 && $this->get_pagenum() > $args['total_pages'] ) { |
||
| 185 | wp_redirect( add_query_arg( 'paged', $args['total_pages'] ) ); |
||
| 186 | exit; |
||
| 187 | } |
||
| 188 | |||
| 189 | $this->_pagination_args = $args; |
||
| 190 | } |
||
| 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 ) { |
||
| 203 | if ( 'page' == $key ) { |
||
| 204 | return $this->get_pagenum(); |
||
| 205 | } |
||
| 206 | |||
| 207 | if ( isset( $this->_pagination_args[ $key ] ) ) { |
||
| 208 | return $this->_pagination_args[ $key ]; |
||
| 209 | } |
||
| 210 | } |
||
| 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() { |
||
| 222 | } |
||
| 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() { |
||
| 231 | _e( 'No items found.' ); |
||
| 232 | } |
||
| 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 ) { |
||
| 244 | if ( empty( $_REQUEST['s'] ) && ! $this->has_items() ) { |
||
| 245 | return; |
||
| 246 | } |
||
| 247 | |||
| 248 | $input_id = $input_id . '-search-input'; |
||
| 249 | |||
| 250 | foreach ( array( 'orderby', 'order' ) as $search_params ) { |
||
| 251 | $this->hidden_search_inputs( $search_params ); |
||
| 252 | } |
||
| 253 | ?> |
||
| 254 | <p class="search-box"> |
||
| 255 | <label class="screen-reader-text" for="<?php echo esc_attr( $input_id ) ?>"><?php echo wp_kses( $text, array() ); ?>:</label> |
||
| 256 | <input type="search" id="<?php echo esc_attr( $input_id ) ?>" name="s" value="<?php _admin_search_query(); ?>" /> |
||
| 257 | <?php submit_button( $text, 'button', '', false, array( 'id' => 'search-submit' ) ); ?> |
||
| 258 | </p> |
||
| 259 | <?php |
||
| 260 | } |
||
| 261 | |||
| 262 | private function hidden_search_inputs( $param_name ) { |
||
| 263 | if ( ! empty( $_REQUEST[ $param_name ] ) ) { |
||
| 264 | echo '<input type="hidden" name="' . esc_attr( $param_name ) . '" value="' . esc_attr( $_REQUEST[ $param_name ] ) . '" />'; |
||
| 265 | } |
||
| 266 | } |
||
| 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() { |
||
| 278 | return array(); |
||
| 279 | } |
||
| 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() { |
||
| 288 | $views = $this->get_views(); |
||
| 289 | /** |
||
| 290 | * Filter the list of available list table views. |
||
| 291 | * |
||
| 292 | * The dynamic portion of the hook name, `$this->screen->id`, refers |
||
| 293 | * to the ID of the current screen, usually a string. |
||
| 294 | * |
||
| 295 | * @since 3.5.0 |
||
| 296 | * |
||
| 297 | * @param array $views An array of available list table views. |
||
| 298 | */ |
||
| 299 | $views = apply_filters( 'views_' . $this->screen->id, $views ); |
||
| 300 | |||
| 301 | if ( empty( $views ) ) { |
||
| 302 | return; |
||
| 303 | } |
||
| 304 | |||
| 305 | echo "<ul class='subsubsub'>\n"; |
||
| 306 | foreach ( $views as $class => $view ) { |
||
| 307 | $views[ $class ] = "\t<li class='$class'>$view"; |
||
| 308 | } |
||
| 309 | echo implode( " |</li>\n", $views ) . "</li>\n"; |
||
| 310 | echo '</ul>'; |
||
| 311 | } |
||
| 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() { |
||
| 323 | return array(); |
||
| 324 | } |
||
| 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 = '' ) { |
||
| 336 | if ( is_null( $this->_actions ) ) { |
||
| 337 | $no_new_actions = $this->get_bulk_actions(); |
||
| 338 | $this->_actions = $no_new_actions; |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Filter the list table Bulk Actions drop-down. |
||
| 342 | * |
||
| 343 | * The dynamic portion of the hook name, `$this->screen->id`, refers |
||
| 344 | * to the ID of the current screen, usually a string. |
||
| 345 | * |
||
| 346 | * This filter can currently only be used to remove bulk actions. |
||
| 347 | * |
||
| 348 | * @since 3.5.0 |
||
| 349 | * |
||
| 350 | * @param array $actions An array of the available bulk actions. |
||
| 351 | */ |
||
| 352 | $this->_actions = apply_filters( "bulk_actions-{$this->screen->id}", $this->_actions ); |
||
| 353 | $this->_actions = array_intersect_assoc( $this->_actions, $no_new_actions ); |
||
| 354 | $two = ''; |
||
| 355 | } else { |
||
| 356 | $two = '2'; |
||
| 357 | } |
||
| 358 | |||
| 359 | if ( empty( $this->_actions ) ) { |
||
| 360 | return; |
||
| 361 | } |
||
| 362 | |||
| 363 | echo "<label for='bulk-action-selector-" . esc_attr( $which ) . "' class='screen-reader-text'>" . esc_attr__( 'Select bulk action' ) . '</label>'; |
||
| 364 | echo "<select name='action" . esc_attr( $two ) . "' id='bulk-action-selector-" . esc_attr( $which ) . "'>\n"; |
||
| 365 | echo "<option value='-1' selected='selected'>" . esc_attr__( 'Bulk Actions' ) . "</option>\n"; |
||
| 366 | |||
| 367 | foreach ( $this->_actions as $name => $title ) { |
||
| 368 | $class = 'edit' == $name ? ' class="hide-if-no-js"' : ''; |
||
| 369 | |||
| 370 | echo "\t<option value='" . esc_attr( $name ) . "'$class>$title</option>\n"; |
||
| 371 | } |
||
| 372 | |||
| 373 | echo "</select>\n"; |
||
| 374 | |||
| 375 | submit_button( __( 'Apply' ), 'action', '', false, array( 'id' => "doaction$two" ) ); |
||
| 376 | echo "\n"; |
||
| 377 | } |
||
| 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() { |
||
| 398 | } |
||
| 399 | |||
| 400 | private static function get_bulk_action( $action_name ) { |
||
| 401 | $action = false; |
||
| 402 | $action_param = self::get_param( array( |
||
| 403 | 'param' => $action_name, |
||
| 404 | 'sanitize' => 'sanitize_text_field', |
||
| 405 | ) ); |
||
| 406 | if ( $action_param && -1 != $action_param ) { |
||
| 407 | $action = $action_param; |
||
| 408 | } |
||
| 409 | return $action; |
||
| 410 | } |
||
| 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 ) { |
||
| 423 | $action_count = count( $actions ); |
||
| 424 | $i = 0; |
||
| 425 | |||
| 426 | if ( ! $action_count ) { |
||
| 427 | return ''; |
||
| 428 | } |
||
| 429 | |||
| 430 | $out = '<div class="' . ( $always_visible ? 'row-actions visible' : 'row-actions' ) . '">'; |
||
| 431 | foreach ( $actions as $action => $link ) { |
||
| 432 | ++$i; |
||
| 433 | ( $i == $action_count ) ? $sep = '' : $sep = ' | '; |
||
| 434 | $out .= "<span class='$action'>$link$sep</span>"; |
||
| 435 | } |
||
| 436 | $out .= '</div>'; |
||
| 437 | |||
| 438 | $out .= '<button type="button" class="toggle-row"><span class="screen-reader-text">' . __( 'Show more details' ) . '</span></button>'; |
||
| 439 | |||
| 440 | return $out; |
||
| 441 | } |
||
| 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 ) { |
||
| 452 | ?> |
||
| 453 | <input type="hidden" name="mode" value="<?php echo esc_attr( $current_mode ); ?>" /> |
||
| 454 | <div class="view-switch"> |
||
| 455 | <?php |
||
| 456 | foreach ( $this->modes as $mode => $title ) { |
||
| 457 | $classes = array( 'view-' . $mode ); |
||
| 458 | if ( $current_mode == $mode ) { |
||
| 459 | $classes[] = 'current'; |
||
| 460 | } |
||
| 461 | |||
| 462 | printf( |
||
| 463 | "<a href='%s' class='%s' id='view-switch-$mode'><span class='screen-reader-text'>%s</span></a>\n", |
||
| 464 | esc_url( add_query_arg( 'mode', $mode ) ), |
||
| 465 | implode( ' ', $classes ), |
||
| 466 | $title |
||
| 467 | ); |
||
| 468 | } |
||
| 469 | ?> |
||
| 470 | </div> |
||
| 471 | <?php |
||
| 472 | } |
||
| 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() { |
||
| 490 | } |
||
| 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 ) { |
||
| 503 | $per_page = (int) get_user_option( $option ); |
||
| 504 | if ( empty( $per_page ) || $per_page < 1 ) { |
||
| 505 | $per_page = $default; |
||
| 506 | } |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Filter the number of items to be displayed on each page of the list table. |
||
| 510 | * |
||
| 511 | * The dynamic hook name, $option, refers to the `per_page` option depending |
||
| 512 | * on the type of list table in use. Possible values include: 'edit_comments_per_page', |
||
| 513 | * 'sites_network_per_page', 'site_themes_network_per_page', 'themes_network_per_page', |
||
| 514 | * 'users_network_per_page', 'edit_post_per_page', 'edit_page_per_page', |
||
| 515 | * 'edit_{$post_type}_per_page', etc. |
||
| 516 | * |
||
| 517 | * @since 2.9.0 |
||
| 518 | * |
||
| 519 | * @param int $per_page Number of items to be displayed. Default 20. |
||
| 520 | */ |
||
| 521 | return (int) apply_filters( $option, $per_page ); |
||
| 522 | } |
||
| 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 ) { |
||
| 533 | if ( empty( $this->_pagination_args ) ) { |
||
| 534 | return; |
||
| 535 | } |
||
| 536 | |||
| 537 | $total_items = $this->_pagination_args['total_items']; |
||
| 538 | $total_pages = $this->_pagination_args['total_pages']; |
||
| 539 | $infinite_scroll = false; |
||
| 540 | if ( isset( $this->_pagination_args['infinite_scroll'] ) ) { |
||
| 541 | $infinite_scroll = $this->_pagination_args['infinite_scroll']; |
||
| 542 | } |
||
| 543 | |||
| 544 | $output = '<span class="displaying-num">' . sprintf( _n( '%s item', '%s items', $total_items ), number_format_i18n( $total_items ) ) . '</span>'; |
||
| 545 | |||
| 546 | $current = $this->get_pagenum(); |
||
| 547 | |||
| 548 | $page_links = array(); |
||
| 549 | |||
| 550 | $total_pages_before = '<span class="paging-input">'; |
||
| 551 | $total_pages_after = '</span>'; |
||
| 552 | |||
| 553 | $disable = $this->disabled_pages( $total_pages ); |
||
| 554 | |||
| 555 | $page_links[] = $this->add_page_link( array( |
||
| 556 | 'page' => 'first', |
||
| 557 | 'arrow' => '«', |
||
| 558 | 'number' => '', |
||
| 559 | 'disabled' => $disable['first'], |
||
| 560 | ) ); |
||
| 561 | |||
| 562 | $page_links[] = $this->add_page_link( array( |
||
| 563 | 'page' => 'prev', |
||
| 564 | 'arrow' => '‹', |
||
| 565 | 'number' => max( 1, $current - 1 ), |
||
| 566 | 'disabled' => $disable['prev'], |
||
| 567 | ) ); |
||
| 568 | |||
| 569 | if ( 'bottom' == $which ) { |
||
| 570 | $html_current_page = $current; |
||
| 571 | $total_pages_before = '<span class="screen-reader-text">' . __( 'Current Page' ) . '</span><span id="table-paging" class="paging-input">'; |
||
| 572 | } else { |
||
| 573 | $html_current_page = sprintf( "%s<input class='current-page' id='current-page-selector' type='text' name='paged' value='%s' size='%d' aria-describedby='table-paging' />", |
||
| 574 | '<label for="current-page-selector" class="screen-reader-text">' . __( 'Current Page' ) . '</label>', |
||
| 575 | $current, |
||
| 576 | strlen( $total_pages ) |
||
| 577 | ); |
||
| 578 | } |
||
| 579 | $html_total_pages = sprintf( "<span class='total-pages'>%s</span>", number_format_i18n( $total_pages ) ); |
||
| 580 | $page_links[] = $total_pages_before . sprintf( _x( '%1$s of %2$s', 'paging' ), $html_current_page, $html_total_pages ) . $total_pages_after; |
||
| 581 | |||
| 582 | $page_links[] = $this->add_page_link( array( |
||
| 583 | 'page' => 'next', |
||
| 584 | 'arrow' => '›', |
||
| 585 | 'number' => min( $total_pages, $current + 1 ), |
||
| 586 | 'disabled' => $disable['next'], |
||
| 587 | ) ); |
||
| 588 | |||
| 589 | $page_links[] = $this->add_page_link( array( |
||
| 590 | 'page' => 'last', |
||
| 591 | 'arrow' => '»', |
||
| 592 | 'number' => $total_pages, |
||
| 593 | 'disabled' => $disable['last'], |
||
| 594 | ) ); |
||
| 595 | |||
| 596 | $pagination_links_class = 'pagination-links'; |
||
| 597 | if ( ! empty( $infinite_scroll ) ) { |
||
| 598 | $pagination_links_class = ' hide-if-js'; |
||
| 599 | } |
||
| 600 | $output .= "\n<span class='$pagination_links_class'>" . join( "\n", $page_links ) . '</span>'; |
||
| 601 | |||
| 602 | if ( $total_pages ) { |
||
| 603 | $page_class = $total_pages < 2 ? ' one-page' : ''; |
||
| 604 | } else { |
||
| 605 | $page_class = ' no-pages'; |
||
| 606 | } |
||
| 607 | $this->_pagination = "<div class='tablenav-pages" . esc_attr( $page_class ) . "'>$output</div>"; |
||
| 608 | |||
| 609 | echo $this->_pagination; |
||
| 610 | } |
||
| 611 | |||
| 612 | private function disabled_pages( $total_pages ) { |
||
| 613 | $current = $this->get_pagenum(); |
||
| 614 | $disable = array( |
||
| 615 | 'first' => false, |
||
| 616 | 'last' => false, |
||
| 617 | 'prev' => false, |
||
| 618 | 'next' => false, |
||
| 619 | ); |
||
| 620 | |||
| 621 | if ( $current == 1 ) { |
||
| 622 | $disable['first'] = true; |
||
| 623 | $disable['prev'] = true; |
||
| 624 | } elseif ( $current == 2 ) { |
||
| 625 | $disable['first'] = true; |
||
| 626 | } |
||
| 627 | |||
| 628 | if ( $current == $total_pages ) { |
||
| 629 | $disable['last'] = true; |
||
| 630 | $disable['next'] = true; |
||
| 631 | } elseif ( $current == $total_pages - 1 ) { |
||
| 632 | $disable['last'] = true; |
||
| 633 | } |
||
| 634 | |||
| 635 | return $disable; |
||
| 636 | } |
||
| 637 | |||
| 638 | private function link_label( $link ) { |
||
| 639 | $labels = array( |
||
| 640 | 'first' => __( 'First page' ), |
||
| 641 | 'last' => __( 'Last page' ), |
||
| 642 | 'prev' => __( 'Previous page' ), |
||
| 643 | 'next' => __( 'Next page' ), |
||
| 644 | ); |
||
| 645 | return $labels[ $link ]; |
||
| 646 | } |
||
| 647 | |||
| 648 | private function current_url() { |
||
| 649 | $current_url = set_url_scheme( 'http://' . FrmAppHelper::get_server_value( 'HTTP_HOST' ) . FrmAppHelper::get_server_value( 'REQUEST_URI' ) ); |
||
| 650 | |||
| 651 | return remove_query_arg( array( 'hotkeys_highlight_last', 'hotkeys_highlight_first' ), $current_url ); |
||
| 652 | } |
||
| 653 | |||
| 654 | private function add_page_link( $atts ) { |
||
| 655 | if ( $atts['disabled'] ) { |
||
| 656 | $link = $this->add_disabled_link( $atts['arrow'] ); |
||
| 657 | } else { |
||
| 658 | $link = $this->add_active_link( $atts ); |
||
| 659 | } |
||
| 660 | return $link; |
||
| 661 | } |
||
| 662 | |||
| 663 | private function add_disabled_link( $label ) { |
||
| 664 | return '<span class="tablenav-pages-navspan" aria-hidden="true">' . $label . '</span>'; |
||
| 665 | } |
||
| 666 | |||
| 667 | private function add_active_link( $atts ) { |
||
| 668 | $url = esc_url( add_query_arg( 'paged', $atts['number'], $this->current_url() ) ); |
||
| 669 | $label = $this->link_label( $atts['page'] ); |
||
| 670 | return sprintf( |
||
| 671 | "<a class='%s-page' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>", |
||
| 672 | $atts['page'], $url, $label, $atts['arrow'] |
||
| 673 | ); |
||
| 674 | } |
||
| 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() { |
||
| 690 | return array(); |
||
| 691 | } |
||
| 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() { |
||
| 702 | $columns = $this->get_columns(); |
||
| 703 | $column = ''; |
||
| 704 | |||
| 705 | // We need a primary defined so responsive views show something, |
||
| 706 | // so let's fall back to the first non-checkbox column. |
||
| 707 | foreach ( $columns as $col => $column_name ) { |
||
| 708 | if ( 'cb' === $col ) { |
||
| 709 | continue; |
||
| 710 | } |
||
| 711 | |||
| 712 | $column = $col; |
||
| 713 | break; |
||
| 714 | } |
||
| 715 | |||
| 716 | return $column; |
||
| 717 | } |
||
| 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() { |
||
| 728 | $columns = $this->get_columns(); |
||
| 729 | $default = $this->get_default_primary_column_name(); |
||
| 730 | |||
| 731 | // If the primary column doesn't exist fall back to the |
||
| 732 | // first non-checkbox column. |
||
| 733 | if ( ! isset( $columns[ $default ] ) ) { |
||
| 734 | $default = FrmListHelper::get_default_primary_column_name(); |
||
| 735 | } |
||
| 736 | |||
| 737 | /** |
||
| 738 | * Filter the name of the primary column for the current list table. |
||
| 739 | * |
||
| 740 | * @since 4.3.0 |
||
| 741 | * |
||
| 742 | * @param string $default Column name default for the specific list table, e.g. 'name'. |
||
| 743 | * @param string $context Screen ID for specific list table, e.g. 'plugins'. |
||
| 744 | */ |
||
| 745 | $column = apply_filters( 'list_table_primary_column', $default, $this->screen->id ); |
||
| 746 | |||
| 747 | if ( empty( $column ) || ! isset( $columns[ $column ] ) ) { |
||
| 748 | $column = $default; |
||
| 749 | } |
||
| 750 | |||
| 751 | return $column; |
||
| 752 | } |
||
| 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() { |
||
| 763 | // $_column_headers is already set / cached |
||
| 764 | if ( isset( $this->_column_headers ) && is_array( $this->_column_headers ) ) { |
||
| 765 | // Back-compat for list tables that have been manually setting $_column_headers for horse reasons. |
||
| 766 | // In 4.3, we added a fourth argument for primary column. |
||
| 767 | $column_headers = array( array(), array(), array(), $this->get_primary_column_name() ); |
||
| 768 | foreach ( $this->_column_headers as $key => $value ) { |
||
| 769 | $column_headers[ $key ] = $value; |
||
| 770 | } |
||
| 771 | |||
| 772 | return $column_headers; |
||
| 773 | } |
||
| 774 | |||
| 775 | $columns = get_column_headers( $this->screen ); |
||
| 776 | $hidden = get_hidden_columns( $this->screen ); |
||
| 777 | |||
| 778 | $sortable_columns = $this->get_sortable_columns(); |
||
| 779 | /** |
||
| 780 | * Filter the list table sortable columns for a specific screen. |
||
| 781 | * |
||
| 782 | * The dynamic portion of the hook name, `$this->screen->id`, refers |
||
| 783 | * to the ID of the current screen, usually a string. |
||
| 784 | * |
||
| 785 | * @since 3.5.0 |
||
| 786 | * |
||
| 787 | * @param array $sortable_columns An array of sortable columns. |
||
| 788 | */ |
||
| 789 | $_sortable = apply_filters( "manage_{$this->screen->id}_sortable_columns", $sortable_columns ); |
||
| 790 | |||
| 791 | $sortable = array(); |
||
| 792 | foreach ( $_sortable as $id => $data ) { |
||
| 793 | if ( empty( $data ) ) { |
||
| 794 | continue; |
||
| 795 | } |
||
| 796 | |||
| 797 | $data = (array) $data; |
||
| 798 | if ( ! isset( $data[1] ) ) { |
||
| 799 | $data[1] = false; |
||
| 800 | } |
||
| 801 | |||
| 802 | $sortable[ $id ] = $data; |
||
| 803 | } |
||
| 804 | |||
| 805 | $primary = $this->get_primary_column_name(); |
||
| 806 | $this->_column_headers = array( $columns, $hidden, $sortable, $primary ); |
||
| 807 | |||
| 808 | return $this->_column_headers; |
||
| 809 | } |
||
| 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() { |
||
| 820 | list ( $columns, $hidden ) = $this->get_column_info(); |
||
| 821 | $hidden = array_intersect( array_keys( $columns ), array_filter( $hidden ) ); |
||
| 822 | return count( $columns ) - count( $hidden ); |
||
| 823 | } |
||
| 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 ) { |
||
| 836 | list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info(); |
||
| 837 | |||
| 838 | $current_url = set_url_scheme( 'http://' . FrmAppHelper::get_server_value( 'HTTP_HOST' ) . FrmAppHelper::get_server_value( 'REQUEST_URI' ) ); |
||
| 839 | $current_url = remove_query_arg( 'paged', $current_url ); |
||
| 840 | |||
| 841 | if ( isset( $_GET['orderby'] ) ) { |
||
| 842 | $current_orderby = sanitize_text_field( $_GET['orderby'] ); |
||
| 843 | } else { |
||
| 844 | $current_orderby = ''; |
||
| 845 | } |
||
| 846 | |||
| 847 | if ( isset( $_GET['order'] ) && 'desc' == $_GET['order'] ) { |
||
| 848 | $current_order = 'desc'; |
||
| 849 | } else { |
||
| 850 | $current_order = 'asc'; |
||
| 851 | } |
||
| 852 | |||
| 853 | if ( ! empty( $columns['cb'] ) ) { |
||
| 854 | static $cb_counter = 1; |
||
| 855 | $columns['cb'] = '<label class="screen-reader-text" for="cb-select-all-' . $cb_counter . '">' . __( 'Select All' ) . '</label>' |
||
| 856 | . '<input id="cb-select-all-' . esc_attr( $cb_counter ) . '" type="checkbox" />'; |
||
| 857 | $cb_counter++; |
||
| 858 | } |
||
| 859 | |||
| 860 | foreach ( $columns as $column_key => $column_display_name ) { |
||
| 861 | $class = array( 'manage-column', "column-$column_key" ); |
||
| 862 | |||
| 863 | if ( in_array( $column_key, $hidden ) ) { |
||
| 864 | $class[] = 'hidden'; |
||
| 865 | } |
||
| 866 | |||
| 867 | if ( 'cb' == $column_key ) { |
||
| 868 | $class[] = 'check-column'; |
||
| 869 | } else if ( in_array( $column_key, array( 'posts', 'comments', 'links' ) ) ) { |
||
| 870 | $class[] = 'num'; |
||
| 871 | } |
||
| 872 | |||
| 873 | if ( $column_key === $primary ) { |
||
| 874 | $class[] = 'column-primary'; |
||
| 875 | } |
||
| 876 | |||
| 877 | if ( isset( $sortable[ $column_key ] ) ) { |
||
| 878 | list( $orderby, $desc_first ) = $sortable[ $column_key ]; |
||
| 879 | |||
| 880 | if ( $current_orderby == $orderby ) { |
||
| 881 | $order = 'asc' == $current_order ? 'desc' : 'asc'; |
||
| 882 | $class[] = 'sorted'; |
||
| 883 | $class[] = $current_order; |
||
| 884 | } else { |
||
| 885 | $order = $desc_first ? 'desc' : 'asc'; |
||
| 886 | $class[] = 'sortable'; |
||
| 887 | $class[] = $desc_first ? 'asc' : 'desc'; |
||
| 888 | } |
||
| 889 | |||
| 890 | $column_display_name = '<a href="' . esc_url( add_query_arg( compact( 'orderby', 'order' ), $current_url ) ) . '"><span>' . $column_display_name . '</span><span class="sorting-indicator"></span></a>'; |
||
| 891 | } |
||
| 892 | |||
| 893 | $tag = ( 'cb' === $column_key ) ? 'td' : 'th'; |
||
| 894 | $scope = ( 'th' === $tag ) ? 'scope="col"' : ''; |
||
| 895 | $id = $with_id ? "id='" . esc_attr( $column_key ) . "'" : ''; |
||
| 896 | |||
| 897 | if ( ! empty( $class ) ) { |
||
| 898 | $class = "class='" . join( ' ', $class ) . "'"; |
||
| 899 | } |
||
| 900 | |||
| 901 | echo "<$tag $scope $id $class>$column_display_name</$tag>"; |
||
| 902 | } |
||
| 903 | } |
||
| 904 | |||
| 905 | /** |
||
| 906 | * Display the table |
||
| 907 | * |
||
| 908 | * @since 2.0.18 |
||
| 909 | * @access public |
||
| 910 | */ |
||
| 911 | public function display() { |
||
| 936 | } |
||
| 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() { |
||
| 947 | return array( 'widefat', 'fixed', 'striped', $this->_args['plural'] ); |
||
| 948 | } |
||
| 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 ) { |
||
| 958 | if ( 'top' == $which ) { |
||
| 959 | wp_nonce_field( 'bulk-' . $this->_args['plural'] ); |
||
| 960 | } |
||
| 961 | ?> |
||
| 962 | <div class="tablenav <?php echo esc_attr( $which ); ?>"> |
||
| 963 | |||
| 964 | <div class="alignleft actions bulkactions"> |
||
| 965 | <?php $this->bulk_actions( $which ); ?> |
||
| 966 | </div> |
||
| 967 | <?php |
||
| 968 | $this->extra_tablenav( $which ); |
||
| 969 | $this->pagination( $which ); |
||
| 970 | ?> |
||
| 971 | |||
| 972 | <br class="clear" /> |
||
| 973 | </div> |
||
| 974 | <?php |
||
| 975 | } |
||
| 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() { |
||
| 994 | if ( $this->has_items() ) { |
||
| 995 | $this->display_rows(); |
||
| 996 | } else { |
||
| 997 | echo '<tr class="no-items"><td class="colspanchange" colspan="' . esc_attr( $this->get_column_count() ) . '">'; |
||
| 998 | $this->no_items(); |
||
| 999 | echo '</td></tr>'; |
||
| 1000 | } |
||
| 1001 | } |
||
| 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 ) { |
||
| 1015 | } |
||
| 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 ) { |
||
| 1063 | } |
||
| 1064 | } |
||
| 1065 | } |
||
| 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 ) { |
||
| 1080 | } |
||
| 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() { |
||
| 1114 | } |
||
| 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, |
||
| 1131 | } |
||
| 1132 | } |
||
| 1133 |