Users: Deprecate the get_currentuserinfo() pluggable function.

It encourages an ugly pattern like `global $userdata; get_currentuserinfo();` in plugins/themes. `wp_get_current_user()` should be used instead, e.g. `$current_user = wp_get_current_user();`.

Props scribu for initial patch.
Fixes #19615.

git-svn-id: https://develop.svn.wordpress.org/trunk@36311 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Pascal Birchler 2016-01-15 10:15:21 +00:00
parent 053733f8c0
commit c7ae3fb3ee
3 changed files with 28 additions and 28 deletions

View File

@ -35,6 +35,23 @@ function set_current_user($id, $name = '') {
}
endif;
if ( !function_exists('get_currentuserinfo') ) :
/**
* Populate global variables with information about the currently logged in user.
*
* @since 0.71
* @deprecated 4.5.0 Use wp_get_current_user()
* @see wp_get_current_user()
*
* @return bool|WP_User False on XMLRPC Request and invalid auth cookie, WP_User instance otherwise.
*/
function get_currentuserinfo() {
_deprecated_function( __FUNCTION__, '4.5', 'wp_get_current_user()' );
return wp_get_current_user();
}
endif;
if ( !function_exists('get_userdatabylogin') ) :
/**
* Retrieve user info by login name.
@ -188,4 +205,4 @@ if ( ! class_exists( 'wp_atom_server', false ) ) {
_deprecated_function( __CLASS__ . '::' . $name, '3.5', 'the Atom Publishing Protocol plugin' );
}
}
}
}

View File

@ -54,48 +54,30 @@ if ( !function_exists('wp_get_current_user') ) :
/**
* Retrieve the current user object.
*
* @since 2.0.3
*
* @global WP_User $current_user
*
* @return WP_User Current user WP_User object
*/
function wp_get_current_user() {
global $current_user;
get_currentuserinfo();
return $current_user;
}
endif;
if ( !function_exists('get_currentuserinfo') ) :
/**
* Populate global variables with information about the currently logged in user.
*
* Will set the current user, if the current user is not set. The current user
* will be set to the logged-in person. If no user is logged-in, then it will
* set the current user to 0, which is invalid and won't have any permissions.
*
* @since 0.71
* @since 2.0.3
*
* @global WP_User $current_user Checks if the current user is set
* @global WP_User $current_user Checks if the current user is set.
*
* @return false|void False on XML-RPC Request and invalid auth cookie.
* @return bool|WP_User WP_User instance on success, false on XMLRPC Request and invalid auth cookie.
*/
function get_currentuserinfo() {
function wp_get_current_user() {
global $current_user;
if ( ! empty( $current_user ) ) {
if ( $current_user instanceof WP_User )
return;
if ( $current_user instanceof WP_User ) {
return $current_user;
}
// Upgrade stdClass to WP_User
if ( is_object( $current_user ) && isset( $current_user->ID ) ) {
$cur_id = $current_user->ID;
$current_user = null;
wp_set_current_user( $cur_id );
return;
return $current_user;
}
// $current_user has a junk value. Force to WP_User with ID 0.
@ -129,6 +111,8 @@ function get_currentuserinfo() {
}
wp_set_current_user( $user_id );
return $current_user;
}
endif;

View File

@ -127,7 +127,6 @@ class Tests_Pluggable extends WP_UnitTestCase {
// wp-includes/pluggable.php:
'wp_set_current_user' => array( 'id', 'name' => '' ),
'wp_get_current_user' => array(),
'get_currentuserinfo' => array(),
'get_userdata' => array( 'user_id' ),
'get_user_by' => array( 'field', 'value' ),
'cache_users' => array( 'user_ids' ),