Move the optimization done to get_user_by() in [21376] higher up the stack, into map_meta_cap() and is_super_admin().

This provides nearly the same benefits without possibly receiving a stale object from get_userdata(),
which could affect authentication, and introduce side effects for plugins.

see #21120.



git-svn-id: http://core.svn.wordpress.org/trunk@21563 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Nacin 2012-08-20 21:58:34 +00:00
parent c255fe7e6e
commit 46eb6f0c26
2 changed files with 26 additions and 25 deletions

View File

@ -1016,12 +1016,13 @@ function map_meta_cap( $cap, $user_id ) {
break;
}
if ( '' != $post->post_author ) {
$post_author_data = get_userdata( $post->post_author );
} else {
// No author set yet, so default to current user for cap checks.
$post_author_data = get_userdata( $user_id );
}
$post_author_id = $post->post_author;
// If no author set yet, default to current user for cap checks.
if ( ! $post_author_id )
$post_author_id = $user_id;
$post_author_data = $post_author_id == get_current_user_id() ? wp_get_current_user() : get_userdata( $post_author_id );
// If the user is the author...
if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
@ -1065,14 +1066,14 @@ function map_meta_cap( $cap, $user_id ) {
break;
}
if ( '' != $post->post_author ) {
$post_author_data = get_userdata( $post->post_author );
} else {
// No author set yet, so default to current user for cap checks.
$post_author_data = get_userdata( $user_id );
}
$post_author_id = $post->post_author;
// If no author set yet, default to current user for cap checks.
if ( ! $post_author_id )
$post_author_id = $user_id;
$post_author_data = $post_author_id == get_current_user_id() ? wp_get_current_user() : get_userdata( $post_author_id );
//echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />";
// If the user is the author...
if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
// If the post is published...
@ -1119,12 +1120,13 @@ function map_meta_cap( $cap, $user_id ) {
break;
}
if ( '' != $post->post_author ) {
$post_author_data = get_userdata( $post->post_author );
} else {
// No author set yet, so default to current user for cap checks.
$post_author_data = get_userdata( $user_id );
}
$post_author_id = $post->post_author;
// If no author set yet, default to current user for cap checks.
if ( ! $post_author_id )
$post_author_id = $user_id;
$post_author_data = $post_author_id == get_current_user_id() ? wp_get_current_user() : get_userdata( $post_author_id );
if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID )
$caps[] = $post_type->cap->read;
@ -1423,10 +1425,12 @@ function get_super_admins() {
* @return bool True if the user is a site admin.
*/
function is_super_admin( $user_id = false ) {
if ( ! $user_id )
$user_id = get_current_user_id();
if ( ! $user_id || $user_id == get_current_user_id() )
$user = wp_get_current_user();
else
$user = get_userdata( $user_id );
if ( ! $user = get_userdata( $user_id ) )
if ( ! $user || ! $user->exists() )
return false;
if ( is_multisite() ) {

View File

@ -133,9 +133,6 @@ if ( !function_exists('get_user_by') ) :
* @return bool|object False on failure, WP_User object on success
*/
function get_user_by( $field, $value ) {
if ( 'id' === $field && (int) $value && get_current_user_id() === (int) $value )
return wp_get_current_user();
$userdata = WP_User::get_data_by( $field, $value );
if ( !$userdata )