From 46eb6f0c26e7075f8cf3e32579bc2fca91c90cc9 Mon Sep 17 00:00:00 2001
From: Andrew Nacin <wp@andrewnacin.com>
Date: Mon, 20 Aug 2012 21:58:34 +0000
Subject: [PATCH] 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
---
 wp-includes/capabilities.php | 48 +++++++++++++++++++-----------------
 wp-includes/pluggable.php    |  3 ---
 2 files changed, 26 insertions(+), 25 deletions(-)

diff --git a/wp-includes/capabilities.php b/wp-includes/capabilities.php
index 293fba2c99..49579c9fea 100644
--- a/wp-includes/capabilities.php
+++ b/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() ) {
diff --git a/wp-includes/pluggable.php b/wp-includes/pluggable.php
index 8e823b443c..2fc6bc9a1b 100644
--- a/wp-includes/pluggable.php
+++ b/wp-includes/pluggable.php
@@ -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 )