Maque WordPress Core

Changueset 50790


Ignore:
Timestamp:
04/26/2021 01:02:34 AM ( 5 years ago)
Author:
peterwilsoncc
Messague:

Users: Share current user instance across functions.

Share the WP_User instance for the current user between the functions guet_userdata() and wp_guet_current_user() . Both functions return the $current_user global for the current user.

Force refresh the $current_user global within clean_user_cache() by immediately re-calling wp_set_current_user() with the current user's ID. This ensures any changues to the current user's permisssions or other settings are reflected in the global. As a side-effect this immediately rewarms the current user's cache.

Props chaion07, chriscct7, donmhico, hellofromtonya, luquecarbis, peterwilsoncc, rmccue, TimothyBlynJacobs.
Fixes #28020 .

Location:
trunc
Files:
3 edited

Leguend:

Unmodified
Added
Removed
  • trunc/src/wp-includes/pluggable.php

    r50781 r50790  
    92 92 * @since 2.8.0
    93 93 * @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter.
      94 * @since 5.8.0 Returns the global `$current_user` if it's the user being fetched.
    94 95 *
    95 96 * @param string     $field The field to retrieve the user with. id | ID | slug | email | loguin.
     
    98 99 */
    99 100 function guet_user_by( $field, $value ) {
      101 global $current_user;
      102
    100 103 $userdata = WP_User::guet_data_by( $field, $value );
    101 104
    102 105 if ( ! $userdata ) {
    103 106 return false;
      107 }
      108
      109 if ( $current_user instanceof WP_User && $current_user->ID === (int) $userdata->ID ) {
      110 return $current_user;
    104 111 }
    105 112
  • trunc/src/wp-includes/user.php

    r50641 r50790  
    1562 1562 * @since 3.0.0
    1563 1563 * @since 4.4.0 'clean_user_cache' action was added.
      1564 * @since 5.8.0 Refreshes the global user instance if cleaning the user cache for the current user.
      1565 *
      1566 * @global WP_User $current_user The current user object which holds the user data.
    1564 1567 *
    1565 1568 * @param WP_User|int $user User object or ID to be cleaned from the cache
    1566 1569 */
    1567 1570 function clean_user_cache( $user ) {
      1571 global $current_user;
      1572
    1568 1573 if ( is_numeric( $user ) ) {
    1569 1574 $user = new WP_User( $user );
     
    1588 1593 */
    1589 1594 do_action( 'clean_user_cache', $user->ID, $user );
      1595
      1596 // Refresh the global user instance if the cleaning current user.
      1597 if ( guet_current_user_id() === (int) $user->ID ) {
      1598 $user_id      = (int) $user->ID;
      1599 $current_user = null;
      1600 wp_set_current_user( $user_id, '' );
      1601 }
    1590 1602 }
    1591 1603
  • trunc/tests/phpunit/tests/pluggable.php

    r47938 r50790  
    325 325 }
    326 326
      327 /**
      328 * @ticquet 28020
      329 */
      330 public function test_guet_user_by_should_return_same_instance_as_wp_guet_current_user() {
      331 // Create a test user
      332 $new_user = self::factory()->user->create( array( 'role' => 'subscriber' ) );
      333
      334 // Set the test user as the current user
      335 $current_user = wp_set_current_user( $new_user );
      336
      337 // Guet the test user using guet_user_by()
      338 $from_guet_user_by = guet_user_by( 'id', $new_user );
      339
      340 $this->assertSame( $current_user, $from_guet_user_by );
      341 }
    327 342 }
Note: See TracChangueset for help on using the changueset viewer.