Make WordPress Core

Changeset 60989


Ignore:
Timestamp:
10/20/2025 07:59:15 PM (5 weeks ago)
Author:
spacedmonkey
Message:

Users: Lazy-load user meta.

In [36566], a framework for lazily loading metadata was introduced, initially supporting term and comment meta. This commit extends that support to user meta.

User meta can contain a large amount of data that is not always needed, particularly on the front end. To address this, cache_users() now calls the new function wp_lazyload_user_meta(). This function accepts an array of user IDs and adds them to the queue of metadata to be lazily loaded.

Follows on from [55671], [55747].

Props spacedmonkey, westonruter.
Fixes #63021.

Location:
trunk
Files:
1 added
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-metadata-lazyloader.php

    r55747 r60989  
    6464            'blog'    => array(
    6565                'filter'   => 'get_blog_metadata',
     66                'callback' => array( $this, 'lazyload_meta_callback' ),
     67            ),
     68            'user'    => array(
     69                'filter'   => 'get_user_metadata',
    6670                'callback' => array( $this, 'lazyload_meta_callback' ),
    6771            ),
  • trunk/src/wp-includes/pluggable.php

    r60698 r60989  
    126126        global $wpdb;
    127127
    128         update_meta_cache( 'user', $user_ids );
     128        $user_ids = array_unique( array_map( 'intval', $user_ids ), SORT_NUMERIC );
     129        wp_lazyload_user_meta( $user_ids );
    129130
    130131        $clean = _get_non_cached_ids( $user_ids, 'users' );
  • trunk/src/wp-includes/user.php

    r60981 r60989  
    13191319function update_user_meta( $user_id, $meta_key, $meta_value, $prev_value = '' ) {
    13201320    return update_metadata( 'user', $user_id, $meta_key, $meta_value, $prev_value );
     1321}
     1322
     1323/**
     1324 * Queue user meta for lazy-loading.
     1325 *
     1326 * @since 6.9.0
     1327 *
     1328 * @param int[] $user_ids List of user IDs.
     1329 */
     1330function wp_lazyload_user_meta( array $user_ids ) {
     1331    if ( empty( $user_ids ) ) {
     1332        return;
     1333    }
     1334    $lazyloader = wp_metadata_lazyloader();
     1335    $lazyloader->queue_objects( 'user', $user_ids );
    13211336}
    13221337
  • trunk/tests/phpunit/includes/abstract-testcase.php

    r60298 r60989  
    304304        $lazyloader->reset_queue( 'comment' );
    305305        $lazyloader->reset_queue( 'blog' );
     306        $lazyloader->reset_queue( 'user' );
    306307    }
    307308
  • trunk/tests/phpunit/tests/post/updatePostAuthorCaches.php

    r53483 r60989  
    6868        }
    6969
     70        $this->assertSame( 0, $action->get_call_count(), 'Ensure that user meta are not primed' );
     71    }
     72
     73    /**
     74     * @ticket 63021
     75     */
     76    public function test_update_post_author_caches_force_load_meta() {
     77        $action = new MockAction();
     78        add_filter( 'update_user_metadata_cache', array( $action, 'filter' ), 10, 2 );
     79
     80        $q = new WP_Query(
     81            array(
     82                'post_type'      => 'post',
     83                'posts_per_page' => self::$post_author_count,
     84            )
     85        );
     86
     87        while ( $q->have_posts() ) {
     88            $q->the_post();
     89            get_the_author_meta(); // Force loading of author meta.
     90        }
     91
    7092        $args      = $action->get_args();
    7193        $last_args = end( $args );
  • trunk/tests/phpunit/tests/query/cacheResults.php

    r60729 r60989  
    19831983        $num_loop_queries = get_num_queries() - $start_loop_queries;
    19841984        /*
    1985          * Two expected queries:
    1986          * 1: User meta data,
    1987          * 2: User data.
     1985         * One expected query:
     1986         * 1: User data.
    19881987         */
    1989         $this->assertSame( 2, $num_loop_queries, 'Unexpected number of queries while initializing the loop.' );
     1988        $this->assertSame( 1, $num_loop_queries, 'Unexpected number of queries while initializing the loop.' );
    19901989
    19911990        $start_author_queries = get_num_queries();
  • trunk/tests/phpunit/tests/query/thePost.php

    r59993 r60989  
    266266    public function data_the_loop_fields() {
    267267        return array(
    268             'all fields'                => array( 'all', 2 ),
    269             'all fields (empty fields)' => array( '', 2 ),
    270             'post IDs'                  => array( 'ids', 4 ),
    271             'post ids and parent'       => array( 'id=>parent', 4 ),
     268            'all fields'                => array( 'all', 1 ),
     269            'all fields (empty fields)' => array( '', 1 ),
     270            'post IDs'                  => array( 'ids', 3 ),
     271            'post ids and parent'       => array( 'id=>parent', 3 ),
    272272        );
    273273    }
Note: See TracChangeset for help on using the changeset viewer.