Permalink
Browse files

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
  • Loading branch information...
1 parent c255fe7 commit 46eb6f0c26e7075f8cf3e32579bc2fca91c90cc9 @nacin nacin committed Aug 20, 2012
Showing with 26 additions and 25 deletions.
  1. +26 −22 wp-includes/capabilities.php
  2. +0 −3 wp-includes/pluggable.php
View
48 wp-includes/capabilities.php
@@ -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
3 wp-includes/pluggable.php
@@ -133,9 +133,6 @@ function get_userdata( $user_id ) {
* @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 )

0 comments on commit 46eb6f0

Please sign in to comment.