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 WP_Screen 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 WP_Screen, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | final class WP_Screen { |
||
| 16 | /** |
||
| 17 | * Any action associated with the screen. 'add' for *-add.php and *-new.php screens. Empty otherwise. |
||
| 18 | * |
||
| 19 | * @since 3.3.0 |
||
| 20 | * @var string |
||
| 21 | * @access public |
||
| 22 | */ |
||
| 23 | public $action; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The base type of the screen. This is typically the same as $id but with any post types and taxonomies stripped. |
||
| 27 | * For example, for an $id of 'edit-post' the base is 'edit'. |
||
| 28 | * |
||
| 29 | * @since 3.3.0 |
||
| 30 | * @var string |
||
| 31 | * @access public |
||
| 32 | */ |
||
| 33 | public $base; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The number of columns to display. Access with get_columns(). |
||
| 37 | * |
||
| 38 | * @since 3.4.0 |
||
| 39 | * @var int |
||
| 40 | * @access private |
||
| 41 | */ |
||
| 42 | private $columns = 0; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The unique ID of the screen. |
||
| 46 | * |
||
| 47 | * @since 3.3.0 |
||
| 48 | * @var string |
||
| 49 | * @access public |
||
| 50 | */ |
||
| 51 | public $id; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Which admin the screen is in. network | user | site | false |
||
| 55 | * |
||
| 56 | * @since 3.5.0 |
||
| 57 | * @var string |
||
| 58 | * @access protected |
||
| 59 | */ |
||
| 60 | protected $in_admin; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Whether the screen is in the network admin. |
||
| 64 | * |
||
| 65 | * Deprecated. Use in_admin() instead. |
||
| 66 | * |
||
| 67 | * @since 3.3.0 |
||
| 68 | * @deprecated 3.5.0 |
||
| 69 | * @var bool |
||
| 70 | * @access public |
||
| 71 | */ |
||
| 72 | public $is_network; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Whether the screen is in the user admin. |
||
| 76 | * |
||
| 77 | * Deprecated. Use in_admin() instead. |
||
| 78 | * |
||
| 79 | * @since 3.3.0 |
||
| 80 | * @deprecated 3.5.0 |
||
| 81 | * @var bool |
||
| 82 | * @access public |
||
| 83 | */ |
||
| 84 | public $is_user; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The base menu parent. |
||
| 88 | * This is derived from $parent_file by removing the query string and any .php extension. |
||
| 89 | * $parent_file values of 'edit.php?post_type=page' and 'edit.php?post_type=post' have a $parent_base of 'edit'. |
||
| 90 | * |
||
| 91 | * @since 3.3.0 |
||
| 92 | * @var string |
||
| 93 | * @access public |
||
| 94 | */ |
||
| 95 | public $parent_base; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * The parent_file for the screen per the admin menu system. |
||
| 99 | * Some $parent_file values are 'edit.php?post_type=page', 'edit.php', and 'options-general.php'. |
||
| 100 | * |
||
| 101 | * @since 3.3.0 |
||
| 102 | * @var string |
||
| 103 | * @access public |
||
| 104 | */ |
||
| 105 | public $parent_file; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * The post type associated with the screen, if any. |
||
| 109 | * The 'edit.php?post_type=page' screen has a post type of 'page'. |
||
| 110 | * The 'edit-tags.php?taxonomy=$taxonomy&post_type=page' screen has a post type of 'page'. |
||
| 111 | * |
||
| 112 | * @since 3.3.0 |
||
| 113 | * @var string |
||
| 114 | * @access public |
||
| 115 | */ |
||
| 116 | public $post_type; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * The taxonomy associated with the screen, if any. |
||
| 120 | * The 'edit-tags.php?taxonomy=category' screen has a taxonomy of 'category'. |
||
| 121 | * @since 3.3.0 |
||
| 122 | * @var string |
||
| 123 | * @access public |
||
| 124 | */ |
||
| 125 | public $taxonomy; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * The help tab data associated with the screen, if any. |
||
| 129 | * |
||
| 130 | * @since 3.3.0 |
||
| 131 | * @var array |
||
| 132 | * @access private |
||
| 133 | */ |
||
| 134 | private $_help_tabs = array(); |
||
| 135 | |||
| 136 | /** |
||
| 137 | * The help sidebar data associated with screen, if any. |
||
| 138 | * |
||
| 139 | * @since 3.3.0 |
||
| 140 | * @var string |
||
| 141 | * @access private |
||
| 142 | */ |
||
| 143 | private $_help_sidebar = ''; |
||
| 144 | |||
| 145 | /** |
||
|
|
|||
| 146 | * The accessible hidden headings and text associated with the screen, if any. |
||
| 147 | * |
||
| 148 | * @since 4.4.0 |
||
| 149 | * @access private |
||
| 150 | * @var array |
||
| 151 | */ |
||
| 152 | private $_screen_reader_content = array(); |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Stores old string-based help. |
||
| 156 | * |
||
| 157 | * @static |
||
| 158 | * @access private |
||
| 159 | * |
||
| 160 | * @var array |
||
| 161 | */ |
||
| 162 | private static $_old_compat_help = array(); |
||
| 163 | |||
| 164 | /** |
||
| 165 | * The screen options associated with screen, if any. |
||
| 166 | * |
||
| 167 | * @since 3.3.0 |
||
| 168 | * @var array |
||
| 169 | * @access private |
||
| 170 | */ |
||
| 171 | private $_options = array(); |
||
| 172 | |||
| 173 | /** |
||
| 174 | * The screen object registry. |
||
| 175 | * |
||
| 176 | * @since 3.3.0 |
||
| 177 | * |
||
| 178 | * @static |
||
| 179 | * @access private |
||
| 180 | * |
||
| 181 | * @var array |
||
| 182 | */ |
||
| 183 | private static $_registry = array(); |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Stores the result of the public show_screen_options function. |
||
| 187 | * |
||
| 188 | * @since 3.3.0 |
||
| 189 | * @var bool |
||
| 190 | * @access private |
||
| 191 | */ |
||
| 192 | private $_show_screen_options; |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Stores the 'screen_settings' section of screen options. |
||
| 196 | * |
||
| 197 | * @since 3.3.0 |
||
| 198 | * @var string |
||
| 199 | * @access private |
||
| 200 | */ |
||
| 201 | private $_screen_settings; |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Fetches a screen object. |
||
| 205 | * |
||
| 206 | * @since 3.3.0 |
||
| 207 | * @access public |
||
| 208 | * |
||
| 209 | * @static |
||
| 210 | * |
||
| 211 | * @global string $hook_suffix |
||
| 212 | * |
||
| 213 | * @param string|WP_Screen $hook_name Optional. The hook name (also known as the hook suffix) used to determine the screen. |
||
| 214 | * Defaults to the current $hook_suffix global. |
||
| 215 | * @return WP_Screen Screen object. |
||
| 216 | */ |
||
| 217 | public static function get( $hook_name = '' ) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Makes the screen object the current screen. |
||
| 375 | * |
||
| 376 | * @see set_current_screen() |
||
| 377 | * @since 3.3.0 |
||
| 378 | * |
||
| 379 | * @global WP_Screen $current_screen |
||
| 380 | * @global string $taxnow |
||
| 381 | * @global string $typenow |
||
| 382 | */ |
||
| 383 | public function set_current_screen() { |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Constructor |
||
| 401 | * |
||
| 402 | * @since 3.3.0 |
||
| 403 | * @access private |
||
| 404 | */ |
||
| 405 | private function __construct() {} |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Indicates whether the screen is in a particular admin |
||
| 409 | * |
||
| 410 | * @since 3.5.0 |
||
| 411 | * |
||
| 412 | * @param string $admin The admin to check against (network | user | site). |
||
| 413 | * If empty any of the three admins will result in true. |
||
| 414 | * @return bool True if the screen is in the indicated admin, false otherwise. |
||
| 415 | */ |
||
| 416 | public function in_admin( $admin = null ) { |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Sets the old string-based contextual help for the screen for backward compatibility. |
||
| 425 | * |
||
| 426 | * @since 3.3.0 |
||
| 427 | * |
||
| 428 | * @static |
||
| 429 | * |
||
| 430 | * @param WP_Screen $screen A screen object. |
||
| 431 | * @param string $help Help text. |
||
| 432 | */ |
||
| 433 | public static function add_old_compat_help( $screen, $help ) { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Set the parent information for the screen. |
||
| 439 | * This is called in admin-header.php after the menu parent for the screen has been determined. |
||
| 440 | * |
||
| 441 | * @since 3.3.0 |
||
| 442 | * |
||
| 443 | * @param string $parent_file The parent file of the screen. Typically the $parent_file global. |
||
| 444 | */ |
||
| 445 | public function set_parentage( $parent_file ) { |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Adds an option for the screen. |
||
| 453 | * Call this in template files after admin.php is loaded and before admin-header.php is loaded to add screen options. |
||
| 454 | * |
||
| 455 | * @since 3.3.0 |
||
| 456 | * |
||
| 457 | * @param string $option Option ID |
||
| 458 | * @param mixed $args Option-dependent arguments. |
||
| 459 | */ |
||
| 460 | public function add_option( $option, $args = array() ) { |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Remove an option from the screen. |
||
| 466 | * |
||
| 467 | * @since 3.8.0 |
||
| 468 | * |
||
| 469 | * @param string $option Option ID. |
||
| 470 | */ |
||
| 471 | public function remove_option( $option ) { |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Remove all options from the screen. |
||
| 477 | * |
||
| 478 | * @since 3.8.0 |
||
| 479 | */ |
||
| 480 | public function remove_options() { |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Get the options registered for the screen. |
||
| 486 | * |
||
| 487 | * @since 3.8.0 |
||
| 488 | * |
||
| 489 | * @return array Options with arguments. |
||
| 490 | */ |
||
| 491 | public function get_options() { |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Gets the arguments for an option for the screen. |
||
| 497 | * |
||
| 498 | * @since 3.3.0 |
||
| 499 | * |
||
| 500 | * @param string $option Option name. |
||
| 501 | * @param string $key Optional. Specific array key for when the option is an array. |
||
| 502 | * Default false. |
||
| 503 | * @return string The option value if set, null otherwise. |
||
| 504 | */ |
||
| 505 | public function get_option( $option, $key = false ) { |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Gets the help tabs registered for the screen. |
||
| 518 | * |
||
| 519 | * @since 3.4.0 |
||
| 520 | * @since 4.4.0 Help tabs are ordered by their priority. |
||
| 521 | * |
||
| 522 | * @return array Help tabs with arguments. |
||
| 523 | */ |
||
| 524 | public function get_help_tabs() { |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Gets the arguments for a help tab. |
||
| 550 | * |
||
| 551 | * @since 3.4.0 |
||
| 552 | * |
||
| 553 | * @param string $id Help Tab ID. |
||
| 554 | * @return array Help tab arguments. |
||
| 555 | */ |
||
| 556 | public function get_help_tab( $id ) { |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Add a help tab to the contextual help for the screen. |
||
| 564 | * Call this on the load-$pagenow hook for the relevant screen. |
||
| 565 | * |
||
| 566 | * @since 3.3.0 |
||
| 567 | * @since 4.4.0 The `$priority` argument was added. |
||
| 568 | * |
||
| 569 | * @param array $args { |
||
| 570 | * Array of arguments used to display the help tab. |
||
| 571 | * |
||
| 572 | * @type string $title Title for the tab. Default false. |
||
| 573 | * @type string $id Tab ID. Must be HTML-safe. Default false. |
||
| 574 | * @type string $content Optional. Help tab content in plain text or HTML. Default empty string. |
||
| 575 | * @type string $callback Optional. A callback to generate the tab content. Default false. |
||
| 576 | * @type int $priority Optional. The priority of the tab, used for ordering. Default 10. |
||
| 577 | * } |
||
| 578 | */ |
||
| 579 | public function add_help_tab( $args ) { |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Removes a help tab from the contextual help for the screen. |
||
| 601 | * |
||
| 602 | * @since 3.3.0 |
||
| 603 | * |
||
| 604 | * @param string $id The help tab ID. |
||
| 605 | */ |
||
| 606 | public function remove_help_tab( $id ) { |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Removes all help tabs from the contextual help for the screen. |
||
| 612 | * |
||
| 613 | * @since 3.3.0 |
||
| 614 | */ |
||
| 615 | public function remove_help_tabs() { |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Gets the content from a contextual help sidebar. |
||
| 621 | * |
||
| 622 | * @since 3.4.0 |
||
| 623 | * |
||
| 624 | * @return string Contents of the help sidebar. |
||
| 625 | */ |
||
| 626 | public function get_help_sidebar() { |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Add a sidebar to the contextual help for the screen. |
||
| 632 | * Call this in template files after admin.php is loaded and before admin-header.php is loaded to add a sidebar to the contextual help. |
||
| 633 | * |
||
| 634 | * @since 3.3.0 |
||
| 635 | * |
||
| 636 | * @param string $content Sidebar content in plain text or HTML. |
||
| 637 | */ |
||
| 638 | public function set_help_sidebar( $content ) { |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Gets the number of layout columns the user has selected. |
||
| 644 | * |
||
| 645 | * The layout_columns option controls the max number and default number of |
||
| 646 | * columns. This method returns the number of columns within that range selected |
||
| 647 | * by the user via Screen Options. If no selection has been made, the default |
||
| 648 | * provisioned in layout_columns is returned. If the screen does not support |
||
| 649 | * selecting the number of layout columns, 0 is returned. |
||
| 650 | * |
||
| 651 | * @since 3.4.0 |
||
| 652 | * |
||
| 653 | * @return int Number of columns to display. |
||
| 654 | */ |
||
| 655 | public function get_columns() { |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Get the accessible hidden headings and text used in the screen. |
||
| 661 | * |
||
| 662 | * @since 4.4.0 |
||
| 663 | * |
||
| 664 | * @see set_screen_reader_content() For more information on the array format. |
||
| 665 | * |
||
| 666 | * @return array An associative array of screen reader text strings. |
||
| 667 | */ |
||
| 668 | public function get_screen_reader_content() { |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Get a screen reader text string. |
||
| 674 | * |
||
| 675 | * @since 4.4.0 |
||
| 676 | * |
||
| 677 | * @param string $key Screen reader text array named key. |
||
| 678 | * @return string Screen reader text string. |
||
| 679 | */ |
||
| 680 | public function get_screen_reader_text( $key ) { |
||
| 686 | |||
| 687 | /** |
||
| 688 | * Add accessible hidden headings and text for the screen. |
||
| 689 | * |
||
| 690 | * @since 4.4.0 |
||
| 691 | * |
||
| 692 | * @param array $content { |
||
| 693 | * An associative array of screen reader text strings. |
||
| 694 | * |
||
| 695 | * @type string $heading_views Screen reader text for the filter links heading. |
||
| 696 | * Default 'Filter items list'. |
||
| 697 | * @type string $heading_pagination Screen reader text for the pagination heading. |
||
| 698 | * Default 'Items list navigation'. |
||
| 699 | * @type string $heading_list Screen reader text for the items list heading. |
||
| 700 | * Default 'Items list'. |
||
| 701 | * } |
||
| 702 | */ |
||
| 703 | public function set_screen_reader_content( $content = array() ) { |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Remove all the accessible hidden headings and text for the screen. |
||
| 716 | * |
||
| 717 | * @since 4.4.0 |
||
| 718 | */ |
||
| 719 | public function remove_screen_reader_content() { |
||
| 722 | |||
| 723 | /** |
||
| 724 | * Render the screen's help section. |
||
| 725 | * |
||
| 726 | * This will trigger the deprecated filters for backward compatibility. |
||
| 727 | * |
||
| 728 | * @since 3.3.0 |
||
| 729 | * |
||
| 730 | * @global string $screen_layout_columns |
||
| 731 | */ |
||
| 732 | public function render_screen_meta() { |
||
| 902 | |||
| 903 | /** |
||
| 904 | * |
||
| 905 | * @global array $wp_meta_boxes |
||
| 906 | * |
||
| 907 | * @return bool |
||
| 908 | */ |
||
| 909 | public function show_screen_options() { |
||
| 963 | |||
| 964 | /** |
||
| 965 | * Render the screen options tab. |
||
| 966 | * |
||
| 967 | * @since 3.3.0 |
||
| 968 | * |
||
| 969 | * @param array $options { |
||
| 970 | * @type bool $wrap Whether the screen-options-wrap div will be included. Defaults to true. |
||
| 971 | * } |
||
| 972 | */ |
||
| 973 | public function render_screen_options( $options = array() ) { |
||
| 1018 | |||
| 1019 | /** |
||
| 1020 | * Render the meta boxes preferences. |
||
| 1021 | * |
||
| 1022 | * @since 4.4.0 |
||
| 1023 | * |
||
| 1024 | * @global array $wp_meta_boxes |
||
| 1025 | */ |
||
| 1026 | public function render_meta_boxes_preferences() { |
||
| 1056 | |||
| 1057 | /** |
||
| 1058 | * Render the list table columns preferences. |
||
| 1059 | * |
||
| 1060 | * @since 4.4.0 |
||
| 1061 | */ |
||
| 1062 | public function render_list_table_columns_preferences() { |
||
| 1101 | |||
| 1102 | /** |
||
| 1103 | * Render the option for number of columns on the page |
||
| 1104 | * |
||
| 1105 | * @since 3.3.0 |
||
| 1106 | */ |
||
| 1107 | public function render_screen_layout() { |
||
| 1130 | |||
| 1131 | /** |
||
| 1132 | * Render the items per page option |
||
| 1133 | * |
||
| 1134 | * @since 3.3.0 |
||
| 1135 | */ |
||
| 1136 | public function render_per_page_options() { |
||
| 1194 | |||
| 1195 | /** |
||
| 1196 | * Render the list table view mode preferences. |
||
| 1197 | * |
||
| 1198 | * @since 4.4.0 |
||
| 1199 | * |
||
| 1200 | * @global string $mode List table view mode. |
||
| 1201 | */ |
||
| 1202 | public function render_view_mode() { |
||
| 1244 | |||
| 1245 | /** |
||
| 1246 | * Render screen reader text. |
||
| 1247 | * |
||
| 1248 | * @since 4.4.0 |
||
| 1249 | * |
||
| 1250 | * @param string $key The screen reader text array named key. |
||
| 1251 | * @param string $tag Optional. The HTML tag to wrap the screen reader text. Default h2. |
||
| 1252 | */ |
||
| 1253 | public function render_screen_reader_content( $key = '', $tag = 'h2' ) { |
||
| 1260 | } |
||
| 1261 |