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


git-svn-id: https://develop.svn.wordpress.org/trunk@21687 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan Boren 2012-08-31 17:16:46 +00:00
parent cb60d4a2fa
commit 7907be8f15
2 changed files with 67 additions and 13 deletions

View File

@ -243,10 +243,22 @@ final class WP_Screen {
*/
public $id;
/**
* Which admin the screen is in. network | user | site | false
*
* @since 3.5.0
* @var string
* @access protected
*/
protected $in_admin;
/**
* Whether the screen is in the network admin.
*
* Deprecated. Use in_admin() instead.
*
* @since 3.3.0
* @deprecated 3.5.0
* @var bool
* @access public
*/
@ -255,7 +267,10 @@ final class WP_Screen {
/**
* Whether the screen is in the user admin.
*
* Deprecated. Use in_admin() instead.
*
* @since 3.3.0
* @deprecated 3.5.0
* @var bool
* @access public
*/
@ -377,7 +392,7 @@ final class WP_Screen {
return $hook_name;
$post_type = $taxonomy = null;
$is_network = $is_user = false;
$in_admin = false;
$action = '';
if ( $hook_name )
@ -402,10 +417,10 @@ final class WP_Screen {
if ( ! $post_type && $hook_name ) {
if ( '-network' == substr( $id, -8 ) ) {
$id = substr( $id, 0, -8 );
$is_network = true;
$in_admin = 'network';
} elseif ( '-user' == substr( $id, -5 ) ) {
$id = substr( $id, 0, -5 );
$is_user = true;
$in_admin = 'user';
}
$id = sanitize_key( $id );
@ -419,13 +434,22 @@ final class WP_Screen {
$post_type = $maybe;
}
}
if ( ! $in_admin )
$in_admin = 'site';
} else {
$is_network = is_network_admin();
$is_user = is_user_admin();
if ( defined( 'WP_NETWORK_ADMIN' ) && WP_NETWORK_ADMIN )
$in_admin = 'network';
elseif ( defined( 'WP_USER_ADMIN' ) && WP_USER_ADMIN )
$in_admin = 'user';
else
$in_admin = 'site';
}
if ( 'index' == $id )
$id = 'dashboard';
elseif ( 'front' == $id )
$in_admin = false;
$base = $id;
@ -476,10 +500,10 @@ final class WP_Screen {
break;
}
if ( $is_network ) {
if ( 'network' == $in_admin ) {
$id .= '-network';
$base .= '-network';
} elseif ( $is_user ) {
} elseif ( 'user' == $in_admin ) {
$id .= '-user';
$base .= '-user';
}
@ -497,8 +521,9 @@ final class WP_Screen {
$screen->action = $action;
$screen->post_type = (string) $post_type;
$screen->taxonomy = (string) $taxonomy;
$screen->is_user = $is_user;
$screen->is_network = $is_network;
$screen->is_user = ( 'user' == $in_admin );
$screen->is_network = ( 'network' == $in_admin );
$screen->in_admin = $in_admin;
self::$_registry[ $id ] = $screen;
@ -527,6 +552,23 @@ final class WP_Screen {
*/
private function __construct() {}
/**
* Indicates whether the screen is in a particular admin
*
* @since 3.5.0
*
* @param string $admin The admin to check against (network | user | site).
* If empty any of the three admins will result in true.
* @return boolean True if the screen is in the indicated admin, false otherwise.
*
*/
public function in_admin( $admin = null ) {
if ( empty( $admin ) )
return (bool) $this->in_admin;
return ( $admin == $this->in_admin );
}
/**
* Sets the old string-based contextual help for the screen.
*

View File

@ -583,8 +583,11 @@ function wp_clone( $object ) {
* @return bool True if inside WordPress administration pages.
*/
function is_admin() {
if ( defined( 'WP_ADMIN' ) )
if ( isset( $GLOBALS['current_screen'] ) )
return $GLOBALS['current_screen']->in_admin();
elseif ( defined( 'WP_ADMIN' ) )
return WP_ADMIN;
return false;
}
@ -599,8 +602,11 @@ function is_admin() {
* @return bool True if inside WordPress network administration pages.
*/
function is_blog_admin() {
if ( defined( 'WP_BLOG_ADMIN' ) )
if ( isset( $GLOBALS['current_screen'] ) )
return $GLOBALS['current_screen']->in_admin( 'blog' );
elseif ( defined( 'WP_BLOG_ADMIN' ) )
return WP_BLOG_ADMIN;
return false;
}
@ -615,8 +621,11 @@ function is_blog_admin() {
* @return bool True if inside WordPress network administration pages.
*/
function is_network_admin() {
if ( defined( 'WP_NETWORK_ADMIN' ) )
if ( isset( $GLOBALS['current_screen'] ) )
return $GLOBALS['current_screen']->in_admin( 'network' );
elseif ( defined( 'WP_NETWORK_ADMIN' ) )
return WP_NETWORK_ADMIN;
return false;
}
@ -631,8 +640,11 @@ function is_network_admin() {
* @return bool True if inside WordPress user administration pages.
*/
function is_user_admin() {
if ( defined( 'WP_USER_ADMIN' ) )
if ( isset( $GLOBALS['current_screen'] ) )
return $GLOBALS['current_screen']->in_admin( 'user' );
elseif ( defined( 'WP_USER_ADMIN' ) )
return WP_USER_ADMIN;
return false;
}