From 3940109c7f1d62bff1034a8121773fa5662c8c2b Mon Sep 17 00:00:00 2001 From: Andrew Nacin Date: Tue, 11 Oct 2011 21:32:16 +0000 Subject: [PATCH] Store screen help and options as static data against WP_Screen. Individual screen objects no longer hold data it can't re-generate on construction or otherwise fetch. convert_to_screen() now returns a WP_Screen object. Various globals are gone. Introduces WP_Screen::get_option(). Allows for a formal factory to be introduced later. see #18785. git-svn-id: https://develop.svn.wordpress.org/trunk@18943 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-admin/includes/screen.php | 147 ++++++++++++++++++++--------------- 1 file changed, 85 insertions(+), 62 deletions(-) diff --git a/wp-admin/includes/screen.php b/wp-admin/includes/screen.php index d7b2dbf3b8..d5f12a9eb7 100644 --- a/wp-admin/includes/screen.php +++ b/wp-admin/includes/screen.php @@ -18,13 +18,12 @@ function get_column_headers( $screen ) { if ( is_string( $screen ) ) $screen = convert_to_screen( $screen ); - global $_wp_column_headers; + static $column_headers = array(); - if ( !isset( $_wp_column_headers[ $screen->id ] ) ) { - $_wp_column_headers[ $screen->id ] = apply_filters( 'manage_' . $screen->id . '_columns', array() ); - } + if ( ! isset( $column_headers[ $screen->id ] ) ) + $column_headers[ $screen->id ] = apply_filters( 'manage_' . $screen->id . '_columns', array() ); - return $_wp_column_headers[ $screen->id ]; + return $column_headers[ $screen->id ]; } /** @@ -49,7 +48,7 @@ function get_hidden_columns( $screen ) { * * @param unknown_type $screen */ -function meta_box_prefs($screen) { +function meta_box_prefs( $screen ) { global $wp_meta_boxes; if ( is_string($screen) ) @@ -232,7 +231,7 @@ function convert_to_screen( $screen ) { $screen .= '-user'; $screen = (string) apply_filters( 'screen_meta_screen', $screen ); - $screen = (object) array('id' => $screen, 'base' => $screen); + $screen = new WP_Screen( $screen ); return $screen; } @@ -248,16 +247,11 @@ function convert_to_screen( $screen ) { * * @todo: deprecate? */ -function add_contextual_help($screen, $help) { - global $_wp_contextual_help; +function add_contextual_help( $screen, $help ) { + if ( is_string( $screen ) ) + $screen = convert_to_screen( $screen ); - if ( is_string($screen) ) - $screen = convert_to_screen($screen); - - if ( !isset($_wp_contextual_help) ) - $_wp_contextual_help = array(); - - $_wp_contextual_help[$screen->id] = $help; + $screen->add_old_compat_help( $help ); } /** @@ -441,31 +435,35 @@ final class WP_Screen { /** * The help tab data associated with the screen, if any. - * - * @since 3.3.0 - * @var array - * @access private - */ - private $_help_tabs = array(); - - /** - * The help sidebar data associated with the screen, if any. + * + * @since 3.3.0 + * @var array + * @access private + */ + private static $_help_tabs = array(); + + /** + * The help sidebar data associated with screens, if any. * * @since 3.3.0 * @var string * @access private - */ - private $_help_sidebar = ''; + */ + private static $_help_sidebar = array(); /** - * The screen options associated with the screen, if any. + * Stores old string-based help. + */ + private static $_old_compat_help = array(); + + /** + * The screen options associated with screens, if any. * * @since 3.3.0 * @var array * @access private */ - private $_options = array(); - + private static $_options = array(); /** * Stores the result of the public show_screen_options function. @@ -553,6 +551,17 @@ final class WP_Screen { $this->base .= '-user'; $this->id .= '-user'; } + + if ( ! isset( self::$_help_tabs[ $this->id ] ) ) + self::$_help_tabs[ $this->id ] = array(); + if ( ! isset( self::$_help_sidebar[ $this->id ] ) ) + self::$_help_sidebar[ $this->id ] = ''; + if ( ! isset( self::$_options[ $this->id ] ) ) + self::$_options[ $this->id ] = array(); + } + + function add_old_compat_help( $help ) { + self::$_old_compat_help[ $this->id ] = $help; } /** @@ -579,7 +588,25 @@ final class WP_Screen { * @param mixed $args Option-dependent arguments. */ public function add_option( $option, $args = array() ) { - $this->_options[ $option ] = $args; + self::$_options[ $this->id ][ $option ] = $args; + } + + /** + * Gets the arguments for an option for the screen. + * + * @since 3.3.0 + * + * @param string + */ + public function get_option( $option, $key = false ) { + if ( ! isset( self::$_options[ $this->id ][ $option ] ) ) + return null; + if ( $key ) { + if ( isset( self::$_options[ $this->id ][ $option ][ $key ] ) ) + return self::$_options[ $this->id ][ $option ][ $key ]; + return null; + } + return self::$_options[ $this->id ][ $option ]; } /** @@ -610,7 +637,7 @@ final class WP_Screen { if ( ! $args['id'] || ! $args['title'] ) return; - $this->_help_tabs[] = $args; + self::$_help_tabs[ $this->id ][] = $args; } /** @@ -622,7 +649,7 @@ final class WP_Screen { * @param string $content Sidebar content in plain text or HTML. */ public function add_help_sidebar( $content ) { - $this->_help_sidebar = $content; + self::$_help_sidebar[ $this->id ] = $content; } /** @@ -633,17 +660,14 @@ final class WP_Screen { * @since 3.3.0 */ public function render_screen_meta() { - global $_wp_contextual_help; // Call old contextual_help_list filter. - if ( ! isset( $_wp_contextual_help ) ) - $_wp_contextual_help = array(); - $_wp_contextual_help = apply_filters( 'contextual_help_list', $_wp_contextual_help, $this ); + self::$_old_compat_help = apply_filters( 'contextual_help_list', self::$_old_compat_help ); - if ( isset( $_wp_contextual_help[ $this->id ] ) || ! $this->_help_tabs ) { + if ( isset( self::$_old_compat_help[ $this->id ] ) || empty(self::$_help_tabs[ $this->id ] ) ) { // Call old contextual_help filter. - if ( isset( $_wp_contextual_help[ $this->id ] ) ) - $contextual_help = apply_filters( 'contextual_help', $_wp_contextual_help[ $this->id ], $this->id, $this ); + if ( isset( self::$_old_compat_help[ $this->id ] ) ) + $contextual_help = apply_filters( 'contextual_help', self::$_old_compat_help[ $this->id ], $this->id ); if ( empty( $contextual_help ) ) { $default_help = __( 'Documentation' ); @@ -666,8 +690,8 @@ final class WP_Screen { 'title' => __('Screen Options'), 'callback' => array( $this, 'render_screen_options' ), ) ); - $_options_tab = array_pop( $this->_help_tabs ); - array_unshift( $this->_help_tabs, $_options_tab ); + $_options_tab = array_pop( self::$_help_tabs[ $this->id ] ); + array_unshift( self::$_help_tabs[ $this->id ], $_options_tab ); } // Time to render! @@ -677,7 +701,7 @@ final class WP_Screen {