From 9612c26db32d2ecf271a30046e908507a23695a5 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 23 Feb 2016 22:26:28 +0000 Subject: [PATCH] Users: Introduce `_wp_get_current_user()` for improved backward compatibility. This new helper function is used by the pluggable functions `wp_get_current_user()` and `get_currentuserinfo()`, which was previously being called by the former before [36311]. Without it, infinite loops could be caused when plugins implement these functions, as they are now called the other way around. Fixes #19615. Built from https://develop.svn.wordpress.org/trunk@36651 git-svn-id: http://core.svn.wordpress.org/trunk@36618 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/pluggable-deprecated.php | 2 +- wp-includes/pluggable.php | 49 +------------------- wp-includes/user.php | 69 ++++++++++++++++++++++++++++ wp-includes/version.php | 2 +- 4 files changed, 72 insertions(+), 50 deletions(-) diff --git a/wp-includes/pluggable-deprecated.php b/wp-includes/pluggable-deprecated.php index 77690bf77d..466359e987 100644 --- a/wp-includes/pluggable-deprecated.php +++ b/wp-includes/pluggable-deprecated.php @@ -48,7 +48,7 @@ if ( !function_exists('get_currentuserinfo') ) : function get_currentuserinfo() { _deprecated_function( __FUNCTION__, '4.5', 'wp_get_current_user()' ); - return wp_get_current_user(); + return _wp_get_current_user(); } endif; diff --git a/wp-includes/pluggable.php b/wp-includes/pluggable.php index 860d8027df..5c467aab0a 100644 --- a/wp-includes/pluggable.php +++ b/wp-includes/pluggable.php @@ -65,54 +65,7 @@ if ( !function_exists('wp_get_current_user') ) : * @return WP_User Current WP_User instance. */ function wp_get_current_user() { - global $current_user; - - if ( ! empty( $current_user ) ) { - 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 $current_user; - } - - // $current_user has a junk value. Force to WP_User with ID 0. - $current_user = null; - wp_set_current_user( 0 ); - return $current_user; - } - - if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST ) { - wp_set_current_user( 0 ); - return $current_user; - } - - /** - * Filter the current user. - * - * The default filters use this to determine the current user from the - * request's cookies, if available. - * - * Returning a value of false will effectively short-circuit setting - * the current user. - * - * @since 3.9.0 - * - * @param int|bool $user_id User ID if one has been determined, false otherwise. - */ - $user_id = apply_filters( 'determine_current_user', false ); - if ( ! $user_id ) { - wp_set_current_user( 0 ); - return $current_user; - } - - wp_set_current_user( $user_id ); - - return $current_user; + return _wp_get_current_user(); } endif; diff --git a/wp-includes/user.php b/wp-includes/user.php index 8b1b0321b4..64f256f570 100644 --- a/wp-includes/user.php +++ b/wp-includes/user.php @@ -2406,3 +2406,72 @@ function wp_get_users_with_no_role() { return $users; } + +/** + * Retrieve the current user object. + * + * 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. + * + * This function is used by the pluggable functions {@see wp_get_current_user()} + * and {@see get_currentuserinfo()}, which is deprecated, for backward compatibility. + * + * @since 4.5.0 + * @access private + * + * @see https://core.trac.wordpress.org/ticket/19615 + * @global WP_User $current_user Checks if the current user is set. + * + * @return WP_User Current WP_User instance. + */ +function _wp_get_current_user() { + global $current_user; + + if ( ! empty( $current_user ) ) { + 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 $current_user; + } + + // $current_user has a junk value. Force to WP_User with ID 0. + $current_user = null; + wp_set_current_user( 0 ); + return $current_user; + } + + if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST ) { + wp_set_current_user( 0 ); + return $current_user; + } + + /** + * Filter the current user. + * + * The default filters use this to determine the current user from the + * request's cookies, if available. + * + * Returning a value of false will effectively short-circuit setting + * the current user. + * + * @since 3.9.0 + * + * @param int|bool $user_id User ID if one has been determined, false otherwise. + */ + $user_id = apply_filters( 'determine_current_user', false ); + if ( ! $user_id ) { + wp_set_current_user( 0 ); + return $current_user; + } + + wp_set_current_user( $user_id ); + + return $current_user; +} diff --git a/wp-includes/version.php b/wp-includes/version.php index 60062f1f2d..51d0bf736a 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.5-alpha-36650'; +$wp_version = '4.5-alpha-36651'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.