Make WordPress Core


Ignore:
Timestamp:
02/23/2016 10:25:32 PM (9 years ago)
Author:
swissspidy
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/user.php

    r36617 r36651  
    24072407    return $users;
    24082408}
     2409
     2410/**
     2411 * Retrieve the current user object.
     2412 *
     2413 * Will set the current user, if the current user is not set. The current user
     2414 * will be set to the logged-in person. If no user is logged-in, then it will
     2415 * set the current user to 0, which is invalid and won't have any permissions.
     2416 *
     2417 * This function is used by the pluggable functions {@see wp_get_current_user()}
     2418 * and {@see get_currentuserinfo()}, which is deprecated, for backward compatibility.
     2419 *
     2420 * @since 4.5.0
     2421 * @access private
     2422 *
     2423 * @see https://core.trac.wordpress.org/ticket/19615
     2424 * @global WP_User $current_user Checks if the current user is set.
     2425 *
     2426 * @return WP_User Current WP_User instance.
     2427 */
     2428function _wp_get_current_user() {
     2429    global $current_user;
     2430
     2431    if ( ! empty( $current_user ) ) {
     2432        if ( $current_user instanceof WP_User ) {
     2433            return $current_user;
     2434        }
     2435
     2436        // Upgrade stdClass to WP_User
     2437        if ( is_object( $current_user ) && isset( $current_user->ID ) ) {
     2438            $cur_id = $current_user->ID;
     2439            $current_user = null;
     2440            wp_set_current_user( $cur_id );
     2441            return $current_user;
     2442        }
     2443
     2444        // $current_user has a junk value. Force to WP_User with ID 0.
     2445        $current_user = null;
     2446        wp_set_current_user( 0 );
     2447        return $current_user;
     2448    }
     2449
     2450    if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST ) {
     2451        wp_set_current_user( 0 );
     2452        return $current_user;
     2453    }
     2454
     2455    /**
     2456     * Filter the current user.
     2457     *
     2458     * The default filters use this to determine the current user from the
     2459     * request's cookies, if available.
     2460     *
     2461     * Returning a value of false will effectively short-circuit setting
     2462     * the current user.
     2463     *
     2464     * @since 3.9.0
     2465     *
     2466     * @param int|bool $user_id User ID if one has been determined, false otherwise.
     2467     */
     2468    $user_id = apply_filters( 'determine_current_user', false );
     2469    if ( ! $user_id ) {
     2470        wp_set_current_user( 0 );
     2471        return $current_user;
     2472    }
     2473
     2474    wp_set_current_user( $user_id );
     2475
     2476    return $current_user;
     2477}
Note: See TracChangeset for help on using the changeset viewer.