WordPress.org

Make WordPress Core

Changeset 18943


Ignore:
Timestamp:
10/11/2011 09:32:16 PM (10 years ago)
Author:
nacin
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/screen.php

    r18941 r18943  
    1919        $screen = convert_to_screen( $screen );
    2020
    21     global $_wp_column_headers;
    22 
    23     if ( !isset( $_wp_column_headers[ $screen->id ] ) ) {
    24         $_wp_column_headers[ $screen->id ] = apply_filters( 'manage_' . $screen->id . '_columns', array() );
    25     }
    26 
    27     return $_wp_column_headers[ $screen->id ];
     21    static $column_headers = array();
     22
     23    if ( ! isset( $column_headers[ $screen->id ] ) )
     24        $column_headers[ $screen->id ] = apply_filters( 'manage_' . $screen->id . '_columns', array() );
     25
     26    return $column_headers[ $screen->id ];
    2827}
    2928
     
    5049 * @param unknown_type $screen
    5150 */
    52 function meta_box_prefs($screen) {
     51function meta_box_prefs( $screen ) {
    5352    global $wp_meta_boxes;
    5453
     
    233232
    234233    $screen = (string) apply_filters( 'screen_meta_screen', $screen );
    235     $screen = (object) array('id' => $screen, 'base' => $screen);
     234    $screen = new WP_Screen( $screen );
    236235    return $screen;
    237236}
     
    249248 * @todo: deprecate?
    250249 */
    251 function add_contextual_help($screen, $help) {
    252     global $_wp_contextual_help;
    253 
    254     if ( is_string($screen) )
    255         $screen = convert_to_screen($screen);
    256 
    257     if ( !isset($_wp_contextual_help) )
    258         $_wp_contextual_help = array();
    259 
    260     $_wp_contextual_help[$screen->id] = $help;
     250function add_contextual_help( $screen, $help ) {
     251    if ( is_string( $screen ) )
     252        $screen = convert_to_screen( $screen );
     253
     254    $screen->add_old_compat_help( $help );
    261255}
    262256
     
    442436    /**
    443437     * The help tab data associated with the screen, if any.
     438     *
     439     * @since 3.3.0
     440     * @var array
     441     * @access private
     442     */
     443    private static $_help_tabs = array();
     444 
     445    /**
     446     * The help sidebar data associated with screens, if any.
     447     *
     448     * @since 3.3.0
     449     * @var string
     450     * @access private
     451     */
     452    private static $_help_sidebar = array();
     453
     454    /**
     455     * Stores old string-based help.
     456     */
     457    private static $_old_compat_help = array();
     458
     459    /**
     460     * The screen options associated with screens, if any.
    444461     *
    445462     * @since 3.3.0
     
    447464     * @access private
    448465     */
    449     private $_help_tabs = array();
    450 
    451     /**
    452      * The help sidebar data associated with the screen, if any.
    453      *
    454      * @since 3.3.0
    455      * @var string
    456      * @access private
    457      */
    458     private $_help_sidebar = '';
    459 
    460     /**
    461      * The screen options associated with the screen, if any.
    462      *
    463      * @since 3.3.0
    464      * @var array
    465      * @access private
    466      */
    467     private $_options = array();
    468 
     466    private static $_options = array();
    469467
    470468    /**
     
    554552            $this->id .= '-user';
    555553        }
     554
     555        if ( ! isset( self::$_help_tabs[ $this->id ] ) )
     556            self::$_help_tabs[ $this->id ] = array();
     557        if ( ! isset( self::$_help_sidebar[ $this->id ] ) )
     558            self::$_help_sidebar[ $this->id ] = '';
     559        if ( ! isset( self::$_options[ $this->id ] ) )
     560            self::$_options[ $this->id ] = array();
     561    }
     562
     563    function add_old_compat_help( $help ) {
     564        self::$_old_compat_help[ $this->id ] = $help;   
    556565    }
    557566
     
    580589     */
    581590    public function add_option( $option, $args = array() ) {
    582         $this->_options[ $option ] = $args;
     591        self::$_options[ $this->id ][ $option ] = $args;
     592    }
     593
     594    /**
     595     * Gets the arguments for an option for the screen.
     596     *
     597     * @since 3.3.0
     598     *
     599     * @param string
     600     */
     601    public function get_option( $option, $key = false ) {
     602        if ( ! isset( self::$_options[ $this->id ][ $option ] ) )
     603            return null;
     604        if ( $key ) {
     605            if ( isset( self::$_options[ $this->id ][ $option ][ $key ] ) )
     606                return self::$_options[ $this->id ][ $option ][ $key ];
     607            return null;
     608        }
     609        return self::$_options[ $this->id ][ $option ];
    583610    }
    584611
     
    611638            return;
    612639
    613         $this->_help_tabs[] = $args;
     640        self::$_help_tabs[ $this->id ][] = $args;
    614641    }
    615642
     
    623650     */
    624651    public function add_help_sidebar( $content ) {
    625         $this->_help_sidebar = $content;
     652        self::$_help_sidebar[ $this->id ] = $content;
    626653    }
    627654
     
    634661     */
    635662    public function render_screen_meta() {
    636         global $_wp_contextual_help;
    637663
    638664        // Call old contextual_help_list filter.
    639         if ( ! isset( $_wp_contextual_help ) )
    640             $_wp_contextual_help = array();
    641         $_wp_contextual_help = apply_filters( 'contextual_help_list', $_wp_contextual_help, $this );
    642 
    643         if ( isset( $_wp_contextual_help[ $this->id ] ) || ! $this->_help_tabs ) {
     665        self::$_old_compat_help = apply_filters( 'contextual_help_list', self::$_old_compat_help );
     666
     667        if ( isset( self::$_old_compat_help[ $this->id ] ) || empty(self::$_help_tabs[ $this->id ] ) ) {
    644668            // Call old contextual_help filter.
    645             if ( isset( $_wp_contextual_help[ $this->id ] ) )
    646                 $contextual_help = apply_filters( 'contextual_help', $_wp_contextual_help[ $this->id ], $this->id, $this );
     669            if ( isset( self::$_old_compat_help[ $this->id ] ) )
     670                $contextual_help = apply_filters( 'contextual_help', self::$_old_compat_help[ $this->id ], $this->id );
    647671
    648672            if ( empty( $contextual_help ) ) {
     
    667691                'callback' => array( $this, 'render_screen_options' ),
    668692            ) );
    669             $_options_tab = array_pop( $this->_help_tabs );
    670             array_unshift( $this->_help_tabs, $_options_tab );
     693            $_options_tab = array_pop( self::$_help_tabs[ $this->id ] );
     694            array_unshift( self::$_help_tabs[ $this->id ], $_options_tab );
    671695        }
    672696
     
    678702                <div class="contextual-help-tabs">
    679703                    <ul>
    680                     <?php foreach ( $this->_help_tabs as $i => $tab ):
     704                    <?php foreach ( self::$_help_tabs[ $this->id ] as $i => $tab ):
    681705                        $link_id  = "tab-link-{$tab['id']}";
    682706                        $panel_id = "tab-panel-{$tab['id']}";
     
    693717                </div>
    694718
     719                <?php if ( ! empty( self::$_help_sidebar[ $this->id ] ) ) : ?>
    695720                <div class="contextual-help-sidebar">
    696                     <?php echo $this->_help_sidebar; ?>
     721                    <?php echo self::$_help_sidebar[ $this->id ]; ?>
    697722                </div>
     723                <?php endif; ?>
    698724
    699725                <div class="contextual-help-tabs-wrap">
    700                     <?php foreach ( $this->_help_tabs as $i => $tab ):
     726                    <?php foreach ( self::$_help_tabs[ $this->id ] as $i => $tab ):
    701727                        $panel_id = "tab-panel-{$tab['id']}";
    702728                        $classes  = ( $i == 0 ) ? 'active' : '';
     
    734760
    735761        // Check if there are per-page options.
    736         $show_screen = $show_screen || isset( $this->_options['per_page'] );
     762        $show_screen = $show_screen || $this->get_option('per_page');
    737763
    738764        $this->_screen_settings = apply_filters( 'screen_settings', '', $this );
     
    748774            $show_screen = true;
    749775
    750         if ( ! empty( $this->_options ) )
     776        if ( ! empty( self::$_options[ $this->id ] ) )
    751777            $show_screen = true;
    752778
     
    769795        <form id="adv-settings" action="" method="post">
    770796        <?php
    771         if ( isset( $this->_options['overview'] ) )
    772             echo $this->_options['overview'];
     797        if ( $this->get_option('overview') )
     798            echo $this->get_option('overview');
    773799        if ( isset( $wp_meta_boxes[ $this->id ] ) ) : ?>
    774800            <h5><?php _ex('Show on screen', 'Metaboxes') ?></h5>
     
    827853            $this->add_option( 'layout_columns', array('max' => $columns[ $this->id ] ) );
    828854
    829         if ( ! isset( $this->_options['layout_columns'] ) ) {
     855        if ( ! $this->get_option('layout_columns') ) {
    830856            $screen_layout_columns = 0;
    831857            return;
     
    833859
    834860        $screen_layout_columns = get_user_option("screen_layout_$this->id");
    835         $num = $this->_options['layout_columns']['max'];
     861        $num = $this->get_option( 'layout_columns', 'max' );
    836862
    837863        if ( ! $screen_layout_columns || 'auto' == $screen_layout_columns ) {
    838             if ( isset( $this->_options['layout_columns']['default'] ) )
    839                 $screen_layout_columns = $this->_options['layout_columns']['default'];
     864            if ( $this->get_option( 'layout_columns', 'default' ) )
     865                $screen_layout_columns = $this->get_option( 'layout_columns', 'default' );
    840866        }
    841867
     
    863889     */
    864890    function render_per_page_options() {
    865         if ( ! isset( $this->_options['per_page'] ) )
     891        if ( ! $this->get_option( 'per_page' ) )
    866892            return;
    867893
    868         $per_page_label = $this->_options['per_page']['label'];
    869 
    870         if ( empty( $this->_options['per_page']['option'] ) ) {
     894        $per_page_label = $this->get_option( 'per_page', 'label' );
     895
     896        $option = $this->get_option( 'per_page', 'option' );
     897        if ( ! $option )
    871898            $option = str_replace( '-', '_', "{$this->id}_per_page" );
    872         } else {
    873             $option = $this->_options['per_page']['option'];
    874         }
    875899
    876900        $per_page = (int) get_user_option( $option );
    877901        if ( empty( $per_page ) || $per_page < 1 ) {
    878             if ( isset($this->_options['per_page']['default']) )
    879                 $per_page = $this->_options['per_page']['default'];
    880             else
     902            $per_page = $this->get_option( 'per_page', 'default' );
     903            if ( ! $per_page )
    881904                $per_page = 20;
    882905        }
Note: See TracChangeset for help on using the changeset viewer.