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 ActionScheduler_Abstract_ListTable 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 ActionScheduler_Abstract_ListTable, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | abstract class ActionScheduler_Abstract_ListTable extends WP_List_Table { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The table name |
||
| 28 | */ |
||
| 29 | protected $table_name; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Package name, used in translations |
||
| 33 | */ |
||
| 34 | protected $package; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * How many items do we render per page? |
||
| 38 | */ |
||
| 39 | protected $items_per_page = 10; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Enables search in this table listing. If this array |
||
| 43 | * is empty it means the listing is not searchable. |
||
| 44 | */ |
||
| 45 | protected $search_by = array(); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Columns to show in the table listing. It is a key => value pair. The |
||
| 49 | * key must much the table column name and the value is the label, which is |
||
| 50 | * automatically translated. |
||
| 51 | */ |
||
| 52 | protected $columns = array(); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Defines the row-actions. It expects an array where the key |
||
| 56 | * is the column name and the value is an array of actions. |
||
| 57 | * |
||
| 58 | * The array of actions are key => value, where key is the method name |
||
| 59 | * (with the prefix row_action_<key>) and the value is the label |
||
| 60 | * and title. |
||
| 61 | */ |
||
| 62 | protected $row_actions = array(); |
||
| 63 | |||
| 64 | /** |
||
| 65 | * The Primary key of our table |
||
| 66 | */ |
||
| 67 | protected $ID = 'ID'; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Enables sorting, it expects an array |
||
| 71 | * of columns (the column names are the values) |
||
| 72 | */ |
||
| 73 | protected $sort_by = array(); |
||
| 74 | |||
| 75 | protected $filter_by = array(); |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var array The status name => count combinations for this table's items. Used to display status filters. |
||
| 79 | */ |
||
| 80 | protected $status_counts = array(); |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var array Notices to display when loading the table. Array of arrays of form array( 'class' => {updated|error}, 'message' => 'This is the notice text display.' ). |
||
| 84 | */ |
||
| 85 | protected $admin_notices = array(); |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var string Localised string displayed in the <h1> element above the able. |
||
| 89 | */ |
||
| 90 | protected $table_header; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Enables bulk actions. It must be an array where the key is the action name |
||
| 94 | * and the value is the label (which is translated automatically). It is important |
||
| 95 | * to notice that it will check that the method exists (`bulk_$name`) and will throw |
||
| 96 | * an exception if it does not exists. |
||
| 97 | * |
||
| 98 | * This class will automatically check if the current request has a bulk action, will do the |
||
| 99 | * validations and afterwards will execute the bulk method, with two arguments. The first argument |
||
| 100 | * is the array with primary keys, the second argument is a string with a list of the primary keys, |
||
| 101 | * escaped and ready to use (with `IN`). |
||
| 102 | */ |
||
| 103 | protected $bulk_actions = array(); |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Makes translation easier, it basically just wraps |
||
| 107 | * `_x` with some default (the package name) |
||
| 108 | */ |
||
| 109 | protected function translate( $text, $context = '' ) { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Reads `$this->bulk_actions` and returns an array that WP_List_Table understands. It |
||
| 115 | * also validates that the bulk method handler exists. It throws an exception because |
||
| 116 | * this is a library meant for developers and missing a bulk method is a development-time error. |
||
| 117 | */ |
||
| 118 | protected function get_bulk_actions() { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Checks if the current request has a bulk action. If that is the case it will validate and will |
||
| 134 | * execute the bulk method handler. Regardless if the action is valid or not it will redirect to |
||
| 135 | * the previous page removing the current arguments that makes this request a bulk action. |
||
| 136 | */ |
||
| 137 | protected function process_bulk_action() { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Default code for deleting entries. We trust ids_sql because it is |
||
| 163 | * validated already by process_bulk_action() |
||
| 164 | */ |
||
| 165 | protected function bulk_delete( array $ids, $ids_sql ) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Prepares the _column_headers property which is used by WP_Table_List at rendering. |
||
| 173 | * It merges the columns and the sortable columns. |
||
| 174 | */ |
||
| 175 | protected function prepare_column_headers() { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Reads $this->sort_by and returns the columns name in a format that WP_Table_List |
||
| 185 | * expects |
||
| 186 | */ |
||
| 187 | public function get_sortable_columns() { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Returns the columns names for rendering. It adds a checkbox for selecting everything |
||
| 197 | * as the first column |
||
| 198 | */ |
||
| 199 | public function get_columns() { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get prepared LIMIT clause for items query |
||
| 210 | * |
||
| 211 | * @global wpdb $wpdb |
||
| 212 | * |
||
| 213 | * @return string Prepared LIMIT clause for items query. |
||
| 214 | */ |
||
| 215 | protected function get_items_query_limit() { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Returns the number of items to offset/skip for this current view. |
||
| 224 | * |
||
| 225 | * @return int |
||
| 226 | */ |
||
| 227 | protected function get_items_offset() { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Get prepared OFFSET clause for items query |
||
| 241 | * |
||
| 242 | * @global wpdb $wpdb |
||
| 243 | * |
||
| 244 | * @return string Prepared OFFSET clause for items query. |
||
| 245 | */ |
||
| 246 | protected function get_items_query_offset() { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Prepares the ORDER BY sql statement. It uses `$this->sort_by` to know which |
||
| 254 | * columns are sortable. This requests validates the orderby $_GET parameter is a valid |
||
| 255 | * column and sortable. It will also use order (ASC|DESC) using DESC by default. |
||
| 256 | */ |
||
| 257 | protected function get_items_query_order() { |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Return the sortable column specified for this request to order the results by, if any. |
||
| 270 | * |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | protected function get_request_orderby() { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Return the sortable column order specified for this request. |
||
| 288 | * |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | protected function get_request_order() { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Return the status filter for this request, if any. |
||
| 304 | * |
||
| 305 | * @return string |
||
| 306 | */ |
||
| 307 | protected function get_request_status() { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Return the search filter for this request, if any. |
||
| 314 | * |
||
| 315 | * @return string |
||
| 316 | */ |
||
| 317 | protected function get_request_search_query() { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Process and return the columns name. This is meant for using with SQL, this means it |
||
| 324 | * always includes the primary key. |
||
| 325 | * |
||
| 326 | * @return array |
||
| 327 | */ |
||
| 328 | protected function get_table_columns() { |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Check if the current request is doing a "full text" search. If that is the case |
||
| 339 | * prepares the SQL to search texts using LIKE. |
||
| 340 | * |
||
| 341 | * If the current request does not have any search or if this list table does not support |
||
| 342 | * that feature it will return an empty string. |
||
| 343 | * |
||
| 344 | * TODO: |
||
| 345 | * - Improve search doing LIKE by word rather than by phrases. |
||
| 346 | * |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | protected function get_items_query_search() { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Prepares the SQL to filter rows by the options defined at `$this->filter_by`. Before trusting |
||
| 365 | * any data sent by the user it validates that it is a valid option. |
||
| 366 | */ |
||
| 367 | protected function get_items_query_filters() { |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Prepares the data to feed WP_Table_List. |
||
| 390 | * |
||
| 391 | * This has the core for selecting, sorting and filting data. To keep the code simple |
||
| 392 | * its logic is split among many methods (get_items_query_*). |
||
| 393 | * |
||
| 394 | * Beside populating the items this function will also count all the records that matches |
||
| 395 | * the filtering criteria and will do fill the pagination variables. |
||
| 396 | */ |
||
| 397 | public function prepare_items() { |
||
| 440 | |||
| 441 | public function extra_tablenav( $which ) { |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Set the data for displaying. It will attempt to unserialize (There is a chance that some columns |
||
| 471 | * are serialized). This can be override in child classes for futher data transformation. |
||
| 472 | */ |
||
| 473 | protected function set_items( array $items ) { |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Renders the checkbox for each row, this is the first column and it is named ID regardless |
||
| 482 | * of how the primary key is named (to keep the code simpler). The bulk actions will do the proper |
||
| 483 | * name transformation though using `$this->ID`. |
||
| 484 | */ |
||
| 485 | public function column_cb( $row ) { |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Renders the row-actions. |
||
| 491 | * |
||
| 492 | * This method renders the action menu, it reads the definition from the $row_actions property, |
||
| 493 | * and it checks that the row action method exists before rendering it. |
||
| 494 | * |
||
| 495 | * @param array $row Row to render |
||
| 496 | * @param $column_name Current row |
||
| 497 | * @return |
||
| 498 | */ |
||
| 499 | protected function maybe_render_actions( $row, $column_name ) { |
||
| 527 | |||
| 528 | protected function process_row_actions() { |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Default column formatting, it will escape everythig for security. |
||
| 551 | */ |
||
| 552 | public function column_default( $item, $column_name ) { |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Display the table heading and search query, if any |
||
| 560 | */ |
||
| 561 | protected function display_header() { |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Display the table heading and search query, if any |
||
| 571 | */ |
||
| 572 | protected function display_admin_notices() { |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Prints the available statuses so the user can click to filter. |
||
| 582 | */ |
||
| 583 | protected function display_filter_by_status() { |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Renders the table list, we override the original class to render the table inside a form |
||
| 618 | * and to render any needed HTML (like the search box). By doing so the callee of a function can simple |
||
| 619 | * forget about any extra HTML. |
||
| 620 | */ |
||
| 621 | protected function display_table() { |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Render the list table page, including header, notices, status filters and table. |
||
| 638 | */ |
||
| 639 | public function display_page() { |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Get the text to display in the search box on the list table. |
||
| 652 | */ |
||
| 653 | protected function get_search_box_placeholder() { |
||
| 656 | } |
||
| 657 |