Make WordPress Core

Ticket #20372: 20372.3.diff

File 20372.3.diff, 2.0 KB (added by ryan, 13 years ago)

Sanity check current_user global in get_currentuserinfo()

  • wp-includes/pluggable.php

     
    2727function wp_set_current_user($id, $name = '') {
    2828        global $current_user;
    2929
    30         if ( isset($current_user) && ($id == $current_user->ID) )
     30        if ( isset( $current_user ) && ( $current_user instanceof WP_User ) && ( $id == $current_user->ID ) )
    3131                return $current_user;
    3232
    33         $current_user = new WP_User($id, $name);
     33        $current_user = new WP_User( $id, $name );
    3434
    35         setup_userdata($current_user->ID);
     35        setup_userdata( $current_user->ID );
    3636
    3737        do_action('set_current_user');
    3838
     
    7575        global $current_user;
    7676
    7777        if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST ) {
    78                 wp_set_current_user(0);
     78                wp_set_current_user( 0 );
    7979                return false;
    8080        }
    8181
    82         if ( ! empty($current_user) )
    83                 return;
     82        if ( ! empty( $current_user ) ) {
     83                if ( $current_user instanceof WP_User )
     84                        return;
    8485
     86                // Upgrade stdClass to WP_User
     87                if ( is_object( $current_user ) && isset( $current_user->ID ) ) {
     88                        // wp_set_current_user() is not used here since it short-circuits if the
     89                        // IDs match.
     90                        $cur_id = $current_user->ID;
     91                        $current_user = null;
     92                        wp_set_current_user( $cur_id );
     93                        return;
     94                }
     95
     96                // $current_user has a junk value. Force to WP_User with ID 0.
     97                $current_user = null;
     98                wp_set_current_user( 0 );
     99                return false;
     100        }
     101
    85102        if ( ! $user = wp_validate_auth_cookie() ) {
    86                  if ( is_blog_admin() || is_network_admin() || empty($_COOKIE[LOGGED_IN_COOKIE]) || !$user = wp_validate_auth_cookie($_COOKIE[LOGGED_IN_COOKIE], 'logged_in') ) {
    87                         wp_set_current_user(0);
     103                 if ( is_blog_admin() || is_network_admin() || empty( $_COOKIE[LOGGED_IN_COOKIE] ) || !$user = wp_validate_auth_cookie( $_COOKIE[LOGGED_IN_COOKIE], 'logged_in' ) ) {
     104                        wp_set_current_user( 0 );
    88105                        return false;
    89106                 }
    90107        }
    91108
    92         wp_set_current_user($user);
     109        wp_set_current_user( $user );
    93110}
    94111endif;
    95112