Make WordPress Core

Ticket #18785: 18785.6.diff

File 18785.6.diff, 14.4 KB (added by nacin, 14 years ago)

Refresh after [19050], [19051].

  • wp-admin/includes/screen.php

     
    113113 * @since 3.0.0
    114114 *
    115115 * @param string $hook_name The hook name (also known as the hook suffix) used to determine the screen.
    116  * @return object An object containing the safe screen name and id
     116 * @return WP_Screen Screen object.
    117117 */
    118 function convert_to_screen( $hook_suffix ) {
    119         $screen = str_replace( array('.php', '-new', '-add', '-network', '-user' ), '', $hook_suffix);
    120 
    121         if ( is_network_admin() )
    122                 $screen .= '-network';
    123         elseif ( is_user_admin() )
    124                 $screen .= '-user';
    125 
    126         $screen = (string) apply_filters( 'screen_meta_screen', $screen );
    127         $screen = (object) array( 'id' => $screen, 'base' => $screen );
    128         return $screen;
     118function convert_to_screen( $hook_name ) {
     119        return WP_Screen::get( $hook_name );
    129120}
    130121
    131122/**
     
    201192                else
    202193                        $icon_id = $screen->base;
    203194
    204                 if ( ! empty( $screen->post_type ) && 'page' == $screen->post_type )
     195                if ( 'page' == $screen->post_type )
    205196                        $icon_id = 'edit-pages';
    206197
    207                 if ( ! empty( $screen->post_type ) )
     198                if ( $screen->post_type )
    208199                        $class .= ' ' . sanitize_html_class( 'icon32-posts-' . $screen->post_type );
    209200        }
    210201
     
    233224 * @since 3.0.0
    234225 * @uses $current_screen
    235226 *
    236  * @param string $hook_name Optional. The hook name (also known as the hook suffix) used to determine the screen.
     227 * @param mixed $hook_name Optional. The hook name (also known as the hook suffix) used to determine the screen,
     228 *      or an existing screen object.
    237229 */
    238230function set_current_screen( $hook_name =  '' ) {
    239         global $current_screen;
    240 
    241         $current_screen = new WP_Screen( $hook_name );
    242 
    243         $current_screen = apply_filters('current_screen', $current_screen);
     231        WP_Screen::get( $hook_name )->set_current_screen();
    244232}
    245233
    246234/**
     
    320308        /**
    321309         * The post type associated with the screen, if any.
    322310         * The 'edit.php?post_type=page' screen has a post type of 'page'.
     311         * The 'edit-tags.php?taxonomy=$taxonomy&post_type=page' screen has a post type of 'page'.
    323312         *
    324313         * @since 3.3.0
    325314         * @var string
     
    343332         * @var array
    344333         * @access private
    345334         */
    346         private static $_help_tabs = array();
     335        private $_help_tabs = array();
    347336 
    348337        /**
    349          * The help sidebar data associated with screens, if any.
     338         * The help sidebar data associated with screen, if any.
    350339         *
    351340         * @since 3.3.0
    352341         * @var string
    353342         * @access private
    354343         */
    355         private static $_help_sidebar = array();
     344        private $_help_sidebar = '';
    356345
    357346        /**
    358347         * Stores old string-based help.
     
    360349        private static $_old_compat_help = array();
    361350
    362351        /**
    363          * The screen options associated with screens, if any.
     352         * The screen options associated with screen, if any.
    364353         *
    365354         * @since 3.3.0
    366355         * @var array
    367356         * @access private
    368357         */
    369         private static $_options = array();
     358        private $_options = array();
    370359
    371360        /**
     361         * The screen object registry.
     362         *
     363         * @since 3.3.0
     364         * @var array
     365         * @access private
     366         */
     367        private static $_registry = array();
     368
     369        /**
    372370         * Stores the result of the public show_screen_options function.
    373371         *
    374372         * @since 3.3.0
     
    386384         */
    387385        private $_screen_settings;
    388386
    389         /**
    390          * Constructor
    391          *
    392          * @since 3.3.0
    393          *
    394          * @param string $id A screen id.  If empty, the $hook_suffix global is used to derive the ID.
    395          */
    396         public function __construct( $id = '' ) {
    397                 global $hook_suffix, $typenow, $taxnow;
     387        /**
     388         * Fetches a screen object.
     389         *
     390         * @since 3.3.0
     391         * @access public
     392         *
     393         * @param string $hook_name Optional. The hook name (also known as the hook suffix) used to determine the screen.
     394         *      Defaults to the current $hook_suffix global.
     395         * @return WP_Screen Screen object.
     396         */
     397        public function get( $hook_name = '' ) {
     398 
     399                if ( is_a( $hook_name, 'WP_Screen' ) )
     400                        return $hook_name;
     401 
     402                $action = $post_type = $taxonomy = '';
    398403
    399                 $action = '';
     404                if ( $hook_name ) {
     405                        if ( '-network' == substr( $hook_name, -8 ) )
     406                                $hook_name = str_replace( '-network', '', $hook_name );
     407                        elseif ( '-user' == substr( $hook_name, -5 ) )
     408                                $hook_name = str_replace( '-user', '', $hook_name );
     409                        $id = sanitize_key( $hook_name );
     410                        if ( false !== strpos( $id, '-' ) ) {
     411                                list( $id, $second ) = explode( '-', $id, 2 );
     412                                if ( taxonomy_exists( $second ) ) {
     413                                        $id = 'edit-tags';
     414                                        $taxonomy = $second;
     415                                } elseif ( post_type_exists( $second ) ) {
     416                                        $post_type = $second;
     417                                } else {
     418                                        $id .= '-' . $second;
     419                                }
     420                        }
     421                } else {
     422                        $id = $GLOBALS['hook_suffix'];
     423                        $id = str_replace( '.php', '', $id );
     424                        if ( in_array( substr( $id, -4 ), array( '-add', '-new' ) ) )
     425                                $action = 'add';
     426                        $id = str_replace( array( '-new', '-add' ), '', $id );
     427                }
     428 
     429                if ( 'index' == $id )
     430                        $id = 'dashboard';
     431 
     432                $base = $id;
     433 
     434                // If this is the current screen, see if we can be more accurate for post types and taxonomies.
     435                if ( ! $hook_name ) {
     436                        switch ( $base ) {
     437                                case 'post' :
     438                                        if ( isset( $_GET['post'] ) )
     439                                                $post_id = (int) $_GET['post'];
     440                                        elseif ( isset( $_POST['post_ID'] ) )
     441                                                $post_id = (int) $_POST['post_ID'];
     442                                        else
     443                                                $post_id = 0;
    400444
    401                 if ( empty( $id ) ) {
    402                         $screen = $hook_suffix;
    403                         $screen = str_replace('.php', '', $screen);
    404                         if ( preg_match('/-add|-new$/', $screen) )
    405                                 $action = 'add';
    406                         $screen = str_replace('-new', '', $screen);
    407                         $screen = str_replace('-add', '', $screen);
    408                         $this->id = $this->base = $screen;
    409                 } else {
    410                         $id = sanitize_key( $id );
    411                         if ( false !== strpos($id, '-') ) {
    412                                 list( $id, $typenow ) = explode('-', $id, 2);
    413                                 if ( taxonomy_exists( $typenow ) ) {
    414                                         $id = 'edit-tags';
    415                                         $taxnow = $typenow;
    416                                         $typenow = '';
    417                                 }
     445                                        if ( $post_id ) {
     446                                                $post = get_post( $post_id );
     447                                                if ( $post )
     448                                                        $post_type = $post->post_type;
     449                                        } elseif ( isset( $_POST['post_type'] ) && post_type_exists( $_POST['post_type'] ) ) {
     450                                                $post_type = $_GET['post_type'];
     451                                        } elseif ( $action == 'add' && isset( $_GET['post_type'] ) && post_type_exists( $_GET['post_type'] ) ) {
     452                                                $post_type = $_GET['post_type'];
     453                                        }
     454                                        break;
     455                                case 'edit' :
     456                                        if ( isset( $_GET['post_type'] ) && post_type_exists( $_GET['post_type'] ) )
     457                                                $post_type = $_GET['post_type'];
     458                                        break;
     459                                case 'edit-tags' :
     460                                        if ( isset( $_REQUEST['taxonomy'] ) && taxonomy_exists( $_REQUEST['taxonomy'] ) )
     461                                                $taxonomy = $_REQUEST['taxonomy'];
     462                                        if ( isset( $_REQUEST['post_type'] ) && post_type_exists( $_REQUEST['post_type'] ) )
     463                                                $post_type = $_REQUEST['post_type'];
     464                                        else
     465                                                $post_type = 'post';
     466                                        break;
    418467                        }
    419                         $this->id = $this->base = $id;
     468                }
     469 
     470                switch ( $base ) {
     471                        case 'post' :
     472                                if ( ! $post_type )
     473                                        $post_type = 'post';
     474                                $id = $post_type;
     475                                break;
     476                        case 'edit' :
     477                                if ( ! $post_type )
     478                                        $post_type = 'post';
     479                                $id .= '-' . $post_type;
     480                                break;
     481                        case 'edit-tags' :
     482                                if ( ! $taxonomy )
     483                                        $taxonomy = 'post_tag';
     484                                $id = 'edit-' . $taxonomy;
     485                                break;
    420486                }
     487 
     488                if ( is_network_admin() ) {
     489                        $id   .= '-network';
     490                        $base .= '-network';
     491                } elseif ( is_user_admin() ) {
     492                        $id   .= '-user';
     493                        $base .= '-user';
     494                }
     495 
     496                if ( isset( self::$_registry[ $id ] ) )
     497                        return self::$_registry[ $id ];
    421498
    422                 $this->action = $action;
     499                $screen = new WP_Screen();
     500                $screen->id         = $id;
     501                $screen->base       = $base;
     502                $screen->action     = $action;
     503                $screen->post_type  = $post_type;
     504                $screen->taxonomy   = $taxonomy;
     505                $screen->is_user    = is_user_admin();
     506                $screen->is_network = is_network_admin();
    423507
    424                 // Map index to dashboard
    425                 if ( 'index' == $this->base )
    426                         $this->base = 'dashboard';
    427                 if ( 'index' == $this->id )
    428                         $this->id = 'dashboard';
     508                self::$_registry[ $id ] = $screen;
    429509
    430                 if ( 'edit' == $this->id ) {
    431                         if ( empty($typenow) )
    432                                 $typenow = 'post';
    433                         $this->id .= '-' . $typenow;
    434                         $this->post_type = $typenow;
    435                 } elseif ( 'post' == $this->id ) {
    436                         if ( empty($typenow) )
    437                                 $typenow = 'post';
    438                         $this->id = $typenow;
    439                         $this->post_type = $typenow;
    440                 } elseif ( 'edit-tags' == $this->id ) {
    441                         if ( empty($taxnow) )
    442                                 $taxnow = 'post_tag';
    443                         $this->id = 'edit-' . $taxnow;
    444                         $this->taxonomy = $taxnow;
    445                 }
    446 
    447                 $this->is_network = is_network_admin();
    448                 $this->is_user = is_user_admin();
    449 
    450                 if ( $this->is_network ) {
    451                         $this->base .= '-network';
    452                         $this->id .= '-network';
    453                 } elseif ( $this->is_user ) {
    454                         $this->base .= '-user';
    455                         $this->id .= '-user';
    456                 }
    457 
    458                 if ( ! isset( self::$_help_tabs[ $this->id ] ) )
    459                         self::$_help_tabs[ $this->id ] = array();
    460                 if ( ! isset( self::$_help_sidebar[ $this->id ] ) )
    461                         self::$_help_sidebar[ $this->id ] = '';
    462                 if ( ! isset( self::$_options[ $this->id ] ) )
    463                         self::$_options[ $this->id ] = array();
     510                return $screen;
     511        }
     512 
     513        /**
     514         * Makes the screen object the current screen.
     515         *
     516         * @see set_current_screen()
     517         * @since 3.3.0
     518         */
     519        function set_current_screen() {
     520                global $current_screen, $taxnow, $typenow;
     521                $current_screen = $this;
     522                $taxnow = $this->taxonomy;
     523                $typenow = $this->post_type;
     524                $current_screen = apply_filters( 'current_screen', $current_screen );
    464525        }
    465526
    466527        /**
     528         * Constructor
     529         *
     530         * @since 3.3.0
     531         * @access private
     532         */
     533        private function __construct() {}
     534
     535        /**
    467536         * Sets the old string-based contextual help for the screen.
    468537         *
    469538         * For backwards compatibility.
     
    501570         * @param mixed $args Option-dependent arguments.
    502571         */
    503572        public function add_option( $option, $args = array() ) {
    504                 self::$_options[ $this->id ][ $option ] = $args;
     573                $this->_options[ $option ] = $args;
    505574        }
    506575
    507576        /**
     
    512581         * @param string
    513582         */
    514583        public function get_option( $option, $key = false ) {
    515                 if ( ! isset( self::$_options[ $this->id ][ $option ] ) )
     584                if ( ! isset( $this->_options[ $option ] ) )
    516585                        return null;
    517586                if ( $key ) {
    518                         if ( isset( self::$_options[ $this->id ][ $option ][ $key ] ) )
    519                                 return self::$_options[ $this->id ][ $option ][ $key ];
     587                        if ( isset( $this->_options[ $option ][ $key ] ) )
     588                                return $this->_options[ $option ][ $key ];
    520589                        return null;
    521590                }
    522                 return self::$_options[ $this->id ][ $option ];
     591                return $this->_options[ $option ];
    523592        }
    524593
    525594        /**
     
    550619                if ( ! $args['id'] || ! $args['title'] )
    551620                        return;
    552621
    553                 self::$_help_tabs[ $this->id ][] = $args;
     622                $this->_help_tabs[] = $args;
    554623        }
    555624
    556625        /**
     
    562631         * @param string $content Sidebar content in plain text or HTML.
    563632         */
    564633        public function add_help_sidebar( $content ) {
    565                 self::$_help_sidebar[ $this->id ] = $content;
     634                $this->_help_sidebar = $content;
    566635        }
    567636
    568637        /**
     
    577646                // Call old contextual_help_list filter.
    578647                self::$_old_compat_help = apply_filters( 'contextual_help_list', self::$_old_compat_help, $this );
    579648
    580                 if ( isset( self::$_old_compat_help[ $this->id ] ) || empty(self::$_help_tabs[ $this->id ] ) ) {
     649                if ( isset( self::$_old_compat_help[ $this->id ] ) || empty($this->_help_tabs ) ) {
    581650                        // Call old contextual_help filter.
    582651                        if ( isset( self::$_old_compat_help[ $this->id ] ) )
    583652                                $contextual_help = apply_filters( 'contextual_help', self::$_old_compat_help[ $this->id ], $this->id, $this );
     
    603672                                'title'    => __('Screen Options'),
    604673                                'callback' => array( $this, 'render_screen_options' ),
    605674                        ) );
    606                         $_options_tab = array_pop( self::$_help_tabs[ $this->id ] );
    607                         array_unshift( self::$_help_tabs[ $this->id ], $_options_tab );
     675                        $_options_tab = array_pop( $this->_help_tabs );
     676                        array_unshift( $this->_help_tabs, $_options_tab );
    608677                }
    609678
    610679                // Time to render!
     
    614683                        <div id="contextual-help-wrap" class="hidden">
    615684                                <div class="contextual-help-tabs">
    616685                                        <ul>
    617                                         <?php foreach ( self::$_help_tabs[ $this->id ] as $i => $tab ):
     686                                        <?php foreach ( $this->_help_tabs as $i => $tab ):
    618687                                                $link_id  = "tab-link-{$tab['id']}";
    619688                                                $panel_id = "tab-panel-{$tab['id']}";
    620689                                                $classes  = ( $i == 0 ) ? 'active' : '';
     
    629698                                        </ul>
    630699                                </div>
    631700
    632                                 <?php if ( ! empty( self::$_help_sidebar[ $this->id ] ) ) : ?>
     701                                <?php if ( ! empty( $this->_help_sidebar ) ) : ?>
    633702                                <div class="contextual-help-sidebar">
    634                                         <?php echo self::$_help_sidebar[ $this->id ]; ?>
     703                                        <?php echo self::$this->_help_sidebar; ?>
    635704                                </div>
    636705                                <?php endif; ?>
    637706
    638707                                <div class="contextual-help-tabs-wrap">
    639                                         <?php foreach ( self::$_help_tabs[ $this->id ] as $i => $tab ):
     708                                        <?php foreach ( $this->_help_tabs as $i => $tab ):
    640709                                                $panel_id = "tab-panel-{$tab['id']}";
    641710                                                $classes  = ( $i == 0 ) ? 'active' : '';
    642711                                                $classes .= ' help-tab-content';
     
    677746                                break;
    678747                }
    679748
    680                 if ( $this->_screen_settings || self::$_options[ $this->id ] )
     749                if ( $this->_screen_settings || $this->_options )
    681750                        $show_screen = true;
    682751
    683752                $this->_show_screen_options = apply_filters( 'screen_options_show_screen', $show_screen, $this );
  • wp-admin/post.php

     
    1616
    1717wp_reset_vars(array('action', 'safe_mode', 'withcomments', 'posts', 'content', 'edited_post_title', 'comment_error', 'profile', 'trackback_url', 'excerpt', 'showcomments', 'commentstart', 'commentend', 'commentorder'));
    1818
    19 if ( isset($_GET['post']) )
    20         $post_id = (int) $_GET['post'];
    21 elseif ( isset($_POST['post_ID']) )
    22         $post_id = (int) $_POST['post_ID'];
    23 else
    24         $post_id = 0;
    25 $post_ID = $post_id;
    26 $post = null;
    27 $post_type_object = null;
    28 $post_type = null;
    29 if ( $post_id ) {
    30         $post = get_post($post_id);
    31         if ( $post ) {
    32                 $post_type_object = get_post_type_object($post->post_type);
    33                 if ( $post_type_object ) {
    34                         $post_type = $post->post_type;
    35                         $current_screen->post_type = $post->post_type;
    36                         $current_screen->id = $current_screen->post_type;
    37                 }
    38         }
    39 } elseif ( isset($_POST['post_type']) ) {
    40         $post_type_object = get_post_type_object($_POST['post_type']);
    41         if ( $post_type_object ) {
    42                 $post_type = $post_type_object->name;
    43                 $current_screen->post_type = $post_type;
    44                 $current_screen->id = $current_screen->post_type;
    45         }
     19if ( isset( $_GET['post'] ) )
     20        $post_id = $post_ID = (int) $_GET['post'];
     21elseif ( isset( $_POST['post_ID'] ) )
     22        $post_id = $post_ID = (int) $_POST['post_ID'];
     23 else
     24        $post_id = $post_ID = 0;
     25
     26$post = $post_type = $post_type_object = null;
     27
     28if ( $post_id )
     29        $post = get_post( $post_id );
     30
     31if ( $post ) {
     32        $post_type = $post->post_type;
     33        $post_type_object = get_post_type_object( $post_type );
    4634}
    4735
    4836/**