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 = '' ) { |
||
| 218 | if ( $hook_name instanceof WP_Screen ) { |
||
| 219 | return $hook_name; |
||
| 220 | } |
||
| 221 | |||
| 222 | $post_type = $taxonomy = null; |
||
| 223 | $in_admin = false; |
||
| 224 | $action = ''; |
||
| 225 | |||
| 226 | if ( $hook_name ) |
||
| 227 | $id = $hook_name; |
||
| 228 | else |
||
| 229 | $id = $GLOBALS['hook_suffix']; |
||
| 230 | |||
| 231 | // For those pesky meta boxes. |
||
| 232 | if ( $hook_name && post_type_exists( $hook_name ) ) { |
||
| 233 | $post_type = $id; |
||
| 234 | $id = 'post'; // changes later. ends up being $base. |
||
| 235 | } else { |
||
| 236 | if ( '.php' == substr( $id, -4 ) ) |
||
| 237 | $id = substr( $id, 0, -4 ); |
||
| 238 | |||
| 239 | if ( 'post-new' == $id || 'link-add' == $id || 'media-new' == $id || 'user-new' == $id ) { |
||
| 240 | $id = substr( $id, 0, -4 ); |
||
| 241 | $action = 'add'; |
||
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | if ( ! $post_type && $hook_name ) { |
||
| 246 | if ( '-network' == substr( $id, -8 ) ) { |
||
| 247 | $id = substr( $id, 0, -8 ); |
||
| 248 | $in_admin = 'network'; |
||
| 249 | } elseif ( '-user' == substr( $id, -5 ) ) { |
||
| 250 | $id = substr( $id, 0, -5 ); |
||
| 251 | $in_admin = 'user'; |
||
| 252 | } |
||
| 253 | |||
| 254 | $id = sanitize_key( $id ); |
||
| 255 | if ( 'edit-comments' != $id && 'edit-tags' != $id && 'edit-' == substr( $id, 0, 5 ) ) { |
||
| 256 | $maybe = substr( $id, 5 ); |
||
| 257 | if ( taxonomy_exists( $maybe ) ) { |
||
| 258 | $id = 'edit-tags'; |
||
| 259 | $taxonomy = $maybe; |
||
| 260 | } elseif ( post_type_exists( $maybe ) ) { |
||
| 261 | $id = 'edit'; |
||
| 262 | $post_type = $maybe; |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | if ( ! $in_admin ) |
||
| 267 | $in_admin = 'site'; |
||
| 268 | } else { |
||
| 269 | if ( defined( 'WP_NETWORK_ADMIN' ) && WP_NETWORK_ADMIN ) |
||
| 270 | $in_admin = 'network'; |
||
| 271 | elseif ( defined( 'WP_USER_ADMIN' ) && WP_USER_ADMIN ) |
||
| 272 | $in_admin = 'user'; |
||
| 273 | else |
||
| 274 | $in_admin = 'site'; |
||
| 275 | } |
||
| 276 | |||
| 277 | if ( 'index' == $id ) |
||
| 278 | $id = 'dashboard'; |
||
| 279 | elseif ( 'front' == $id ) |
||
| 280 | $in_admin = false; |
||
| 281 | |||
| 282 | $base = $id; |
||
| 283 | |||
| 284 | // If this is the current screen, see if we can be more accurate for post types and taxonomies. |
||
| 285 | if ( ! $hook_name ) { |
||
| 286 | View Code Duplication | if ( isset( $_REQUEST['post_type'] ) ) |
|
| 287 | $post_type = post_type_exists( $_REQUEST['post_type'] ) ? $_REQUEST['post_type'] : false; |
||
| 288 | View Code Duplication | if ( isset( $_REQUEST['taxonomy'] ) ) |
|
| 289 | $taxonomy = taxonomy_exists( $_REQUEST['taxonomy'] ) ? $_REQUEST['taxonomy'] : false; |
||
| 290 | |||
| 291 | switch ( $base ) { |
||
| 292 | case 'post' : |
||
| 293 | if ( isset( $_GET['post'] ) ) |
||
| 294 | $post_id = (int) $_GET['post']; |
||
| 295 | elseif ( isset( $_POST['post_ID'] ) ) |
||
| 296 | $post_id = (int) $_POST['post_ID']; |
||
| 297 | else |
||
| 298 | $post_id = 0; |
||
| 299 | |||
| 300 | if ( $post_id ) { |
||
| 301 | $post = get_post( $post_id ); |
||
| 302 | if ( $post ) |
||
| 303 | $post_type = $post->post_type; |
||
| 304 | } |
||
| 305 | break; |
||
| 306 | case 'edit-tags' : |
||
| 307 | case 'term' : |
||
| 308 | if ( null === $post_type && is_object_in_taxonomy( 'post', $taxonomy ? $taxonomy : 'post_tag' ) ) |
||
| 309 | $post_type = 'post'; |
||
| 310 | break; |
||
| 311 | } |
||
| 312 | } |
||
| 313 | |||
| 314 | switch ( $base ) { |
||
| 315 | case 'post' : |
||
| 316 | if ( null === $post_type ) |
||
| 317 | $post_type = 'post'; |
||
| 318 | $id = $post_type; |
||
| 319 | break; |
||
| 320 | case 'edit' : |
||
| 321 | if ( null === $post_type ) |
||
| 322 | $post_type = 'post'; |
||
| 323 | $id .= '-' . $post_type; |
||
| 324 | break; |
||
| 325 | case 'edit-tags' : |
||
| 326 | case 'term' : |
||
| 327 | if ( null === $taxonomy ) |
||
| 328 | $taxonomy = 'post_tag'; |
||
| 329 | // The edit-tags ID does not contain the post type. Look for it in the request. |
||
| 330 | View Code Duplication | if ( null === $post_type ) { |
|
| 331 | $post_type = 'post'; |
||
| 332 | if ( isset( $_REQUEST['post_type'] ) && post_type_exists( $_REQUEST['post_type'] ) ) |
||
| 333 | $post_type = $_REQUEST['post_type']; |
||
| 334 | } |
||
| 335 | |||
| 336 | $id = 'edit-' . $taxonomy; |
||
| 337 | break; |
||
| 338 | } |
||
| 339 | |||
| 340 | if ( 'network' == $in_admin ) { |
||
| 341 | $id .= '-network'; |
||
| 342 | $base .= '-network'; |
||
| 343 | } elseif ( 'user' == $in_admin ) { |
||
| 344 | $id .= '-user'; |
||
| 345 | $base .= '-user'; |
||
| 346 | } |
||
| 347 | |||
| 348 | if ( isset( self::$_registry[ $id ] ) ) { |
||
| 349 | $screen = self::$_registry[ $id ]; |
||
| 350 | if ( $screen === get_current_screen() ) |
||
| 351 | return $screen; |
||
| 352 | } else { |
||
| 353 | $screen = new WP_Screen(); |
||
| 354 | $screen->id = $id; |
||
| 355 | } |
||
| 356 | |||
| 357 | $screen->base = $base; |
||
| 358 | $screen->action = $action; |
||
| 359 | $screen->post_type = (string) $post_type; |
||
| 360 | $screen->taxonomy = (string) $taxonomy; |
||
| 361 | $screen->is_user = ( 'user' == $in_admin ); |
||
| 362 | $screen->is_network = ( 'network' == $in_admin ); |
||
| 363 | $screen->in_admin = $in_admin; |
||
| 364 | |||
| 365 | self::$_registry[ $id ] = $screen; |
||
| 366 | |||
| 367 | return $screen; |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Makes the screen object the current screen. |
||
| 372 | * |
||
| 373 | * @see set_current_screen() |
||
| 374 | * @since 3.3.0 |
||
| 375 | * |
||
| 376 | * @global WP_Screen $current_screen |
||
| 377 | * @global string $taxnow |
||
| 378 | * @global string $typenow |
||
| 379 | */ |
||
| 380 | public function set_current_screen() { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Constructor |
||
| 398 | * |
||
| 399 | * @since 3.3.0 |
||
| 400 | * @access private |
||
| 401 | */ |
||
| 402 | private function __construct() {} |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Indicates whether the screen is in a particular admin |
||
| 406 | * |
||
| 407 | * @since 3.5.0 |
||
| 408 | * |
||
| 409 | * @param string $admin The admin to check against (network | user | site). |
||
| 410 | * If empty any of the three admins will result in true. |
||
| 411 | * @return bool True if the screen is in the indicated admin, false otherwise. |
||
| 412 | */ |
||
| 413 | public function in_admin( $admin = null ) { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Sets the old string-based contextual help for the screen for backward compatibility. |
||
| 422 | * |
||
| 423 | * @since 3.3.0 |
||
| 424 | * |
||
| 425 | * @static |
||
| 426 | * |
||
| 427 | * @param WP_Screen $screen A screen object. |
||
| 428 | * @param string $help Help text. |
||
| 429 | */ |
||
| 430 | public static function add_old_compat_help( $screen, $help ) { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Set the parent information for the screen. |
||
| 436 | * This is called in admin-header.php after the menu parent for the screen has been determined. |
||
| 437 | * |
||
| 438 | * @since 3.3.0 |
||
| 439 | * |
||
| 440 | * @param string $parent_file The parent file of the screen. Typically the $parent_file global. |
||
| 441 | */ |
||
| 442 | public function set_parentage( $parent_file ) { |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Adds an option for the screen. |
||
| 450 | * Call this in template files after admin.php is loaded and before admin-header.php is loaded to add screen options. |
||
| 451 | * |
||
| 452 | * @since 3.3.0 |
||
| 453 | * |
||
| 454 | * @param string $option Option ID |
||
| 455 | * @param mixed $args Option-dependent arguments. |
||
| 456 | */ |
||
| 457 | public function add_option( $option, $args = array() ) { |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Remove an option from the screen. |
||
| 463 | * |
||
| 464 | * @since 3.8.0 |
||
| 465 | * |
||
| 466 | * @param string $option Option ID. |
||
| 467 | */ |
||
| 468 | public function remove_option( $option ) { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Remove all options from the screen. |
||
| 474 | * |
||
| 475 | * @since 3.8.0 |
||
| 476 | */ |
||
| 477 | public function remove_options() { |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Get the options registered for the screen. |
||
| 483 | * |
||
| 484 | * @since 3.8.0 |
||
| 485 | * |
||
| 486 | * @return array Options with arguments. |
||
| 487 | */ |
||
| 488 | public function get_options() { |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Gets the arguments for an option for the screen. |
||
| 494 | * |
||
| 495 | * @since 3.3.0 |
||
| 496 | * |
||
| 497 | * @param string $option Option name. |
||
| 498 | * @param string $key Optional. Specific array key for when the option is an array. |
||
| 499 | * Default false. |
||
| 500 | * @return string The option value if set, null otherwise. |
||
| 501 | */ |
||
| 502 | public function get_option( $option, $key = false ) { |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Gets the help tabs registered for the screen. |
||
| 515 | * |
||
| 516 | * @since 3.4.0 |
||
| 517 | * @since 4.4.0 Help tabs are ordered by their priority. |
||
| 518 | * |
||
| 519 | * @return array Help tabs with arguments. |
||
| 520 | */ |
||
| 521 | public function get_help_tabs() { |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Gets the arguments for a help tab. |
||
| 547 | * |
||
| 548 | * @since 3.4.0 |
||
| 549 | * |
||
| 550 | * @param string $id Help Tab ID. |
||
| 551 | * @return array Help tab arguments. |
||
| 552 | */ |
||
| 553 | public function get_help_tab( $id ) { |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Add a help tab to the contextual help for the screen. |
||
| 561 | * Call this on the load-$pagenow hook for the relevant screen. |
||
| 562 | * |
||
| 563 | * @since 3.3.0 |
||
| 564 | * @since 4.4.0 The `$priority` argument was added. |
||
| 565 | * |
||
| 566 | * @param array $args { |
||
| 567 | * Array of arguments used to display the help tab. |
||
| 568 | * |
||
| 569 | * @type string $title Title for the tab. Default false. |
||
| 570 | * @type string $id Tab ID. Must be HTML-safe. Default false. |
||
| 571 | * @type string $content Optional. Help tab content in plain text or HTML. Default empty string. |
||
| 572 | * @type string $callback Optional. A callback to generate the tab content. Default false. |
||
| 573 | * @type int $priority Optional. The priority of the tab, used for ordering. Default 10. |
||
| 574 | * } |
||
| 575 | */ |
||
| 576 | public function add_help_tab( $args ) { |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Removes a help tab from the contextual help for the screen. |
||
| 598 | * |
||
| 599 | * @since 3.3.0 |
||
| 600 | * |
||
| 601 | * @param string $id The help tab ID. |
||
| 602 | */ |
||
| 603 | public function remove_help_tab( $id ) { |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Removes all help tabs from the contextual help for the screen. |
||
| 609 | * |
||
| 610 | * @since 3.3.0 |
||
| 611 | */ |
||
| 612 | public function remove_help_tabs() { |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Gets the content from a contextual help sidebar. |
||
| 618 | * |
||
| 619 | * @since 3.4.0 |
||
| 620 | * |
||
| 621 | * @return string Contents of the help sidebar. |
||
| 622 | */ |
||
| 623 | public function get_help_sidebar() { |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Add a sidebar to the contextual help for the screen. |
||
| 629 | * 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. |
||
| 630 | * |
||
| 631 | * @since 3.3.0 |
||
| 632 | * |
||
| 633 | * @param string $content Sidebar content in plain text or HTML. |
||
| 634 | */ |
||
| 635 | public function set_help_sidebar( $content ) { |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Gets the number of layout columns the user has selected. |
||
| 641 | * |
||
| 642 | * The layout_columns option controls the max number and default number of |
||
| 643 | * columns. This method returns the number of columns within that range selected |
||
| 644 | * by the user via Screen Options. If no selection has been made, the default |
||
| 645 | * provisioned in layout_columns is returned. If the screen does not support |
||
| 646 | * selecting the number of layout columns, 0 is returned. |
||
| 647 | * |
||
| 648 | * @since 3.4.0 |
||
| 649 | * |
||
| 650 | * @return int Number of columns to display. |
||
| 651 | */ |
||
| 652 | public function get_columns() { |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Get the accessible hidden headings and text used in the screen. |
||
| 658 | * |
||
| 659 | * @since 4.4.0 |
||
| 660 | * |
||
| 661 | * @see set_screen_reader_content() For more information on the array format. |
||
| 662 | * |
||
| 663 | * @return array An associative array of screen reader text strings. |
||
| 664 | */ |
||
| 665 | public function get_screen_reader_content() { |
||
| 666 | return $this->_screen_reader_content; |
||
| 667 | } |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Get a screen reader text string. |
||
| 671 | * |
||
| 672 | * @since 4.4.0 |
||
| 673 | * |
||
| 674 | * @param string $key Screen reader text array named key. |
||
| 675 | * @return string Screen reader text string. |
||
| 676 | */ |
||
| 677 | public function get_screen_reader_text( $key ) { |
||
| 678 | if ( ! isset( $this->_screen_reader_content[ $key ] ) ) { |
||
| 679 | return null; |
||
| 680 | } |
||
| 681 | return $this->_screen_reader_content[ $key ]; |
||
| 682 | } |
||
| 683 | |||
| 684 | /** |
||
| 685 | * Add accessible hidden headings and text for the screen. |
||
| 686 | * |
||
| 687 | * @since 4.4.0 |
||
| 688 | * |
||
| 689 | * @param array $content { |
||
| 690 | * An associative array of screen reader text strings. |
||
| 691 | * |
||
| 692 | * @type string $heading_views Screen reader text for the filter links heading. |
||
| 693 | * Default 'Filter items list'. |
||
| 694 | * @type string $heading_pagination Screen reader text for the pagination heading. |
||
| 695 | * Default 'Items list navigation'. |
||
| 696 | * @type string $heading_list Screen reader text for the items list heading. |
||
| 697 | * Default 'Items list'. |
||
| 698 | * } |
||
| 699 | */ |
||
| 700 | public function set_screen_reader_content( $content = array() ) { |
||
| 701 | $defaults = array( |
||
| 702 | 'heading_views' => __( 'Filter items list' ), |
||
| 703 | 'heading_pagination' => __( 'Items list navigation' ), |
||
| 704 | 'heading_list' => __( 'Items list' ), |
||
| 705 | ); |
||
| 706 | $content = wp_parse_args( $content, $defaults ); |
||
| 707 | |||
| 708 | $this->_screen_reader_content = $content; |
||
| 709 | } |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Remove all the accessible hidden headings and text for the screen. |
||
| 713 | * |
||
| 714 | * @since 4.4.0 |
||
| 715 | */ |
||
| 716 | public function remove_screen_reader_content() { |
||
| 717 | $this->_screen_reader_content = array(); |
||
| 718 | } |
||
| 719 | |||
| 720 | /** |
||
| 721 | * Render the screen's help section. |
||
| 722 | * |
||
| 723 | * This will trigger the deprecated filters for backward compatibility. |
||
| 724 | * |
||
| 725 | * @since 3.3.0 |
||
| 726 | * |
||
| 727 | * @global string $screen_layout_columns |
||
| 728 | */ |
||
| 729 | public function render_screen_meta() { |
||
| 730 | |||
| 731 | /** |
||
| 732 | * Filters the legacy contextual help list. |
||
| 733 | * |
||
| 734 | * @since 2.7.0 |
||
| 735 | * @deprecated 3.3.0 Use get_current_screen()->add_help_tab() or |
||
| 736 | * get_current_screen()->remove_help_tab() instead. |
||
| 737 | * |
||
| 738 | * @param array $old_compat_help Old contextual help. |
||
| 739 | * @param WP_Screen $this Current WP_Screen instance. |
||
| 740 | */ |
||
| 741 | self::$_old_compat_help = apply_filters( 'contextual_help_list', self::$_old_compat_help, $this ); |
||
| 742 | |||
| 743 | $old_help = isset( self::$_old_compat_help[ $this->id ] ) ? self::$_old_compat_help[ $this->id ] : ''; |
||
| 744 | |||
| 745 | /** |
||
| 746 | * Filters the legacy contextual help text. |
||
| 747 | * |
||
| 748 | * @since 2.7.0 |
||
| 749 | * @deprecated 3.3.0 Use get_current_screen()->add_help_tab() or |
||
| 750 | * get_current_screen()->remove_help_tab() instead. |
||
| 751 | * |
||
| 752 | * @param string $old_help Help text that appears on the screen. |
||
| 753 | * @param string $screen_id Screen ID. |
||
| 754 | * @param WP_Screen $this Current WP_Screen instance. |
||
| 755 | * |
||
| 756 | */ |
||
| 757 | $old_help = apply_filters( 'contextual_help', $old_help, $this->id, $this ); |
||
| 758 | |||
| 759 | // Default help only if there is no old-style block of text and no new-style help tabs. |
||
| 760 | if ( empty( $old_help ) && ! $this->get_help_tabs() ) { |
||
| 761 | |||
| 762 | /** |
||
| 763 | * Filters the default legacy contextual help text. |
||
| 764 | * |
||
| 765 | * @since 2.8.0 |
||
| 766 | * @deprecated 3.3.0 Use get_current_screen()->add_help_tab() or |
||
| 767 | * get_current_screen()->remove_help_tab() instead. |
||
| 768 | * |
||
| 769 | * @param string $old_help_default Default contextual help text. |
||
| 770 | */ |
||
| 771 | $default_help = apply_filters( 'default_contextual_help', '' ); |
||
| 772 | if ( $default_help ) |
||
| 773 | $old_help = '<p>' . $default_help . '</p>'; |
||
| 774 | } |
||
| 775 | |||
| 776 | if ( $old_help ) { |
||
| 777 | $this->add_help_tab( array( |
||
| 778 | 'id' => 'old-contextual-help', |
||
| 779 | 'title' => __('Overview'), |
||
| 780 | 'content' => $old_help, |
||
| 781 | ) ); |
||
| 782 | } |
||
| 783 | |||
| 784 | $help_sidebar = $this->get_help_sidebar(); |
||
| 785 | |||
| 786 | $help_class = 'hidden'; |
||
| 787 | if ( ! $help_sidebar ) |
||
| 788 | $help_class .= ' no-sidebar'; |
||
| 789 | |||
| 790 | // Time to render! |
||
| 791 | ?> |
||
| 792 | <div id="screen-meta" class="metabox-prefs"> |
||
| 793 | |||
| 794 | <div id="contextual-help-wrap" class="<?php echo esc_attr( $help_class ); ?>" tabindex="-1" aria-label="<?php esc_attr_e('Contextual Help Tab'); ?>"> |
||
| 795 | <div id="contextual-help-back"></div> |
||
| 796 | <div id="contextual-help-columns"> |
||
| 797 | <div class="contextual-help-tabs"> |
||
| 798 | <ul> |
||
| 799 | <?php |
||
| 800 | $class = ' class="active"'; |
||
| 801 | foreach ( $this->get_help_tabs() as $tab ) : |
||
| 802 | $link_id = "tab-link-{$tab['id']}"; |
||
| 803 | $panel_id = "tab-panel-{$tab['id']}"; |
||
| 804 | ?> |
||
| 805 | |||
| 806 | <li id="<?php echo esc_attr( $link_id ); ?>"<?php echo $class; ?>> |
||
| 807 | <a href="<?php echo esc_url( "#$panel_id" ); ?>" aria-controls="<?php echo esc_attr( $panel_id ); ?>"> |
||
| 808 | <?php echo esc_html( $tab['title'] ); ?> |
||
| 809 | </a> |
||
| 810 | </li> |
||
| 811 | <?php |
||
| 812 | $class = ''; |
||
| 813 | endforeach; |
||
| 814 | ?> |
||
| 815 | </ul> |
||
| 816 | </div> |
||
| 817 | |||
| 818 | <?php if ( $help_sidebar ) : ?> |
||
| 819 | <div class="contextual-help-sidebar"> |
||
| 820 | <?php echo $help_sidebar; ?> |
||
| 821 | </div> |
||
| 822 | <?php endif; ?> |
||
| 823 | |||
| 824 | <div class="contextual-help-tabs-wrap"> |
||
| 825 | <?php |
||
| 826 | $classes = 'help-tab-content active'; |
||
| 827 | foreach ( $this->get_help_tabs() as $tab ): |
||
| 828 | $panel_id = "tab-panel-{$tab['id']}"; |
||
| 829 | ?> |
||
| 830 | |||
| 831 | <div id="<?php echo esc_attr( $panel_id ); ?>" class="<?php echo $classes; ?>"> |
||
| 832 | <?php |
||
| 833 | // Print tab content. |
||
| 834 | echo $tab['content']; |
||
| 835 | |||
| 836 | // If it exists, fire tab callback. |
||
| 837 | if ( ! empty( $tab['callback'] ) ) |
||
| 838 | call_user_func_array( $tab['callback'], array( $this, $tab ) ); |
||
| 839 | ?> |
||
| 840 | </div> |
||
| 841 | <?php |
||
| 842 | $classes = 'help-tab-content'; |
||
| 843 | endforeach; |
||
| 844 | ?> |
||
| 845 | </div> |
||
| 846 | </div> |
||
| 847 | </div> |
||
| 848 | <?php |
||
| 849 | // Setup layout columns |
||
| 850 | |||
| 851 | /** |
||
| 852 | * Filters the array of screen layout columns. |
||
| 853 | * |
||
| 854 | * This hook provides back-compat for plugins using the back-compat |
||
| 855 | * Filters instead of add_screen_option(). |
||
| 856 | * |
||
| 857 | * @since 2.8.0 |
||
| 858 | * |
||
| 859 | * @param array $empty_columns Empty array. |
||
| 860 | * @param string $screen_id Screen ID. |
||
| 861 | * @param WP_Screen $this Current WP_Screen instance. |
||
| 862 | */ |
||
| 863 | $columns = apply_filters( 'screen_layout_columns', array(), $this->id, $this ); |
||
| 864 | |||
| 865 | if ( ! empty( $columns ) && isset( $columns[ $this->id ] ) ) |
||
| 866 | $this->add_option( 'layout_columns', array('max' => $columns[ $this->id ] ) ); |
||
| 867 | |||
| 868 | if ( $this->get_option( 'layout_columns' ) ) { |
||
| 869 | $this->columns = (int) get_user_option("screen_layout_$this->id"); |
||
| 870 | |||
| 871 | if ( ! $this->columns && $this->get_option( 'layout_columns', 'default' ) ) |
||
| 872 | $this->columns = $this->get_option( 'layout_columns', 'default' ); |
||
| 873 | } |
||
| 874 | $GLOBALS[ 'screen_layout_columns' ] = $this->columns; // Set the global for back-compat. |
||
| 875 | |||
| 876 | // Add screen options |
||
| 877 | if ( $this->show_screen_options() ) |
||
| 878 | $this->render_screen_options(); |
||
| 879 | ?> |
||
| 880 | </div> |
||
| 881 | <?php |
||
| 882 | if ( ! $this->get_help_tabs() && ! $this->show_screen_options() ) |
||
| 883 | return; |
||
| 884 | ?> |
||
| 885 | <div id="screen-meta-links"> |
||
| 886 | <?php if ( $this->get_help_tabs() ) : ?> |
||
| 887 | <div id="contextual-help-link-wrap" class="hide-if-no-js screen-meta-toggle"> |
||
| 888 | <button type="button" id="contextual-help-link" class="button show-settings" aria-controls="contextual-help-wrap" aria-expanded="false"><?php _e( 'Help' ); ?></button> |
||
| 889 | </div> |
||
| 890 | <?php endif; |
||
| 891 | if ( $this->show_screen_options() ) : ?> |
||
| 892 | <div id="screen-options-link-wrap" class="hide-if-no-js screen-meta-toggle"> |
||
| 893 | <button type="button" id="show-settings-link" class="button show-settings" aria-controls="screen-options-wrap" aria-expanded="false"><?php _e( 'Screen Options' ); ?></button> |
||
| 894 | </div> |
||
| 895 | <?php endif; ?> |
||
| 896 | </div> |
||
| 897 | <?php |
||
| 898 | } |
||
| 899 | |||
| 900 | /** |
||
| 901 | * |
||
| 902 | * @global array $wp_meta_boxes |
||
| 903 | * |
||
| 904 | * @return bool |
||
| 905 | */ |
||
| 906 | public function show_screen_options() { |
||
| 959 | |||
| 960 | /** |
||
| 961 | * Render the screen options tab. |
||
| 962 | * |
||
| 963 | * @since 3.3.0 |
||
| 964 | * |
||
| 965 | * @param array $options { |
||
| 966 | * @type bool $wrap Whether the screen-options-wrap div will be included. Defaults to true. |
||
| 967 | * } |
||
| 968 | */ |
||
| 969 | public function render_screen_options( $options = array() ) { |
||
| 1014 | |||
| 1015 | /** |
||
| 1016 | * Render the meta boxes preferences. |
||
| 1017 | * |
||
| 1018 | * @since 4.4.0 |
||
| 1019 | * |
||
| 1020 | * @global array $wp_meta_boxes |
||
| 1021 | */ |
||
| 1022 | public function render_meta_boxes_preferences() { |
||
| 1052 | |||
| 1053 | /** |
||
| 1054 | * Render the list table columns preferences. |
||
| 1055 | * |
||
| 1056 | * @since 4.4.0 |
||
| 1057 | */ |
||
| 1058 | public function render_list_table_columns_preferences() { |
||
| 1097 | |||
| 1098 | /** |
||
| 1099 | * Render the option for number of columns on the page |
||
| 1100 | * |
||
| 1101 | * @since 3.3.0 |
||
| 1102 | */ |
||
| 1103 | public function render_screen_layout() { |
||
| 1104 | if ( ! $this->get_option( 'layout_columns' ) ) { |
||
| 1105 | return; |
||
| 1106 | } |
||
| 1107 | |||
| 1108 | $screen_layout_columns = $this->get_columns(); |
||
| 1109 | $num = $this->get_option( 'layout_columns', 'max' ); |
||
| 1110 | |||
| 1111 | ?> |
||
| 1112 | <fieldset class='columns-prefs'> |
||
| 1113 | <legend class="screen-layout"><?php _e( 'Layout' ); ?></legend><?php |
||
| 1114 | for ( $i = 1; $i <= $num; ++$i ): |
||
| 1115 | ?> |
||
| 1116 | <label class="columns-prefs-<?php echo $i; ?>"> |
||
| 1117 | <input type='radio' name='screen_columns' value='<?php echo esc_attr( $i ); ?>' |
||
| 1118 | <?php checked( $screen_layout_columns, $i ); ?> /> |
||
| 1119 | <?php printf( _n( '%s column', '%s columns', $i ), number_format_i18n( $i ) ); ?> |
||
| 1120 | </label> |
||
| 1121 | <?php |
||
| 1122 | endfor; ?> |
||
| 1123 | </fieldset> |
||
| 1124 | <?php |
||
| 1125 | } |
||
| 1126 | |||
| 1127 | /** |
||
| 1128 | * Render the items per page option |
||
| 1129 | * |
||
| 1130 | * @since 3.3.0 |
||
| 1131 | */ |
||
| 1132 | public function render_per_page_options() { |
||
| 1190 | |||
| 1191 | /** |
||
| 1192 | * Render the list table view mode preferences. |
||
| 1193 | * |
||
| 1194 | * @since 4.4.0 |
||
| 1195 | */ |
||
| 1196 | public function render_view_mode() { |
||
| 1238 | |||
| 1239 | /** |
||
| 1240 | * Render screen reader text. |
||
| 1241 | * |
||
| 1242 | * @since 4.4.0 |
||
| 1243 | * |
||
| 1244 | * @param string $key The screen reader text array named key. |
||
| 1245 | * @param string $tag Optional. The HTML tag to wrap the screen reader text. Default h2. |
||
| 1246 | */ |
||
| 1247 | public function render_screen_reader_content( $key = '', $tag = 'h2' ) { |
||
| 1254 | } |
||
| 1255 |