Make WordPress Core

Changeset 21687


Ignore:
Timestamp:
08/31/2012 05:16:46 PM (14 years ago)
Author:
ryan
Message:

Introduce WP_Screen::in_admin() for determining which admin the screen is in.
Change is_*_admin() to reference in_admin() with fallback to the WP_*_ADMIN constants during early bootstrap. This allows unit tests and ajax handlers to set the admin context.

fixes #21742

Location:
trunk
Files:
2 edited

Legend:

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

    r21457 r21687  
    245245
    246246        /**
     247         * Which admin the screen is in. network | user | site | false
     248         *
     249         * @since 3.5.0
     250         * @var string
     251         * @access protected
     252         */
     253        protected $in_admin;
     254
     255        /**
    247256         * Whether the screen is in the network admin.
    248257         *
    249          * @since 3.3.0
     258         * Deprecated. Use in_admin() instead.
     259         *
     260         * @since 3.3.0
     261         * @deprecated 3.5.0
    250262         * @var bool
    251263         * @access public
    … …  
    256268         * Whether the screen is in the user admin.
    257269         *
    258          * @since 3.3.0
     270         * Deprecated. Use in_admin() instead.
     271         *
     272         * @since 3.3.0
     273         * @deprecated 3.5.0
    259274         * @var bool
    260275         * @access public
    … …  
    378393
    379394                $post_type = $taxonomy = null;
    380                 $is_network = $is_user = false;
     395                $in_admin = false;
    381396                $action = '';
    382397
    … …  
    403418                        if ( '-network' == substr( $id, -8 ) ) {
    404419                                $id = substr( $id, 0, -8 );
    405                                 $is_network = true;
     420                                $in_admin = 'network';
    406421                        } elseif ( '-user' == substr( $id, -5 ) ) {
    407422                                $id = substr( $id, 0, -5 );
    408                                 $is_user = true;
     423                                $in_admin = 'user';
    409424                        }
    410425
    … …  
    420435                                }
    421436                        }
     437
     438                        if ( ! $in_admin )
     439                                $in_admin = 'site';
    422440                } else {
    423                         $is_network = is_network_admin();
    424                         $is_user = is_user_admin();
     441                        if ( defined( 'WP_NETWORK_ADMIN' ) && WP_NETWORK_ADMIN )
     442                                $in_admin = 'network';
     443                        elseif ( defined( 'WP_USER_ADMIN' ) && WP_USER_ADMIN )
     444                                $in_admin = 'user';
     445                        else
     446                                $in_admin = 'site';
    425447                }
    426448
    427449                if ( 'index' == $id )
    428450                        $id = 'dashboard';
     451                elseif ( 'front' == $id )
     452                        $in_admin = false;
    429453
    430454                $base = $id;
    … …  
    477501                }
    478502
    479                 if ( $is_network ) {
     503                if ( 'network' == $in_admin ) {
    480504                        $id   .= '-network';
    481505                        $base .= '-network';
    482                 } elseif ( $is_user ) {
     506                } elseif ( 'user' == $in_admin ) {
    483507                        $id   .= '-user';
    484508                        $base .= '-user';
    … …  
    498522                $screen->post_type  = (string) $post_type;
    499523                $screen->taxonomy   = (string) $taxonomy;
    500                 $screen->is_user    = $is_user;
    501                 $screen->is_network = $is_network;
     524                $screen->is_user    = ( 'user' == $in_admin );
     525                $screen->is_network = ( 'network' == $in_admin );
     526                $screen->in_admin   = $in_admin;
    502527
    503528                self::$_registry[ $id ] = $screen;
    … …  
    527552         */
    528553        private function __construct() {}
     554
     555        /**
     556         * Indicates whether the screen is in a particular admin
     557         *
     558         * @since 3.5.0
     559         *
     560         * @param string $admin The admin to check against (network | user | site).
     561         * If empty any of the three admins will result in true.
     562         * @return boolean True if the screen is in the indicated admin, false otherwise.
     563         *
     564         */
     565        public function in_admin( $admin = null ) {
     566                if ( empty( $admin ) )
     567                        return (bool) $this->in_admin;
     568
     569                return ( $admin == $this->in_admin );
     570        }
    529571
    530572        /**
  • trunk/wp-includes/load.php

    r21559 r21687  
    584584 */
    585585function is_admin() {
    586         if ( defined( 'WP_ADMIN' ) )
     586        if ( isset( $GLOBALS['current_screen'] ) )
     587                return $GLOBALS['current_screen']->in_admin();
     588        elseif ( defined( 'WP_ADMIN' ) )
    587589                return WP_ADMIN;
     590
    588591        return false;
    589592}
    … …  
    600603 */
    601604function is_blog_admin() {
    602         if ( defined( 'WP_BLOG_ADMIN' ) )
     605        if ( isset( $GLOBALS['current_screen'] ) )
     606                return $GLOBALS['current_screen']->in_admin( 'blog' );
     607        elseif ( defined( 'WP_BLOG_ADMIN' ) )
    603608                return WP_BLOG_ADMIN;
     609
    604610        return false;
    605611}
    … …  
    616622 */
    617623function is_network_admin() {
    618         if ( defined( 'WP_NETWORK_ADMIN' ) )
     624        if ( isset( $GLOBALS['current_screen'] ) )
     625                return $GLOBALS['current_screen']->in_admin( 'network' );
     626        elseif ( defined( 'WP_NETWORK_ADMIN' ) )
    619627                return WP_NETWORK_ADMIN;
     628
    620629        return false;
    621630}
    … …  
    632641 */
    633642function is_user_admin() {
    634         if ( defined( 'WP_USER_ADMIN' ) )
     643        if ( isset( $GLOBALS['current_screen'] ) )
     644                return $GLOBALS['current_screen']->in_admin( 'user' );
     645        elseif ( defined( 'WP_USER_ADMIN' ) )
    635646                return WP_USER_ADMIN;
     647
    636648        return false;
    637649}
Note: See TracChangeset for help on using the changeset viewer.