Make WordPress Core

Changeset 55747


Ignore:
Timestamp:
05/11/2023 11:13:10 AM (3 years ago)
Author:
spacedmonkey
Message:

Networks and Sites: Lazy load site meta.

In [36566] a framework to lazily load metadata was introduced. This supported term and comment meta by default. In this commit, extends support for site ( blog ) meta. Site meta is not heavily used by core and is used by developers to extend multisite. In this change, _prime_site_caches and WP_Site_Query now call the new function wp_lazyload_site_meta. The function wp_lazyload_site_meta accepts an array of ids and adds them to the queue of metadata to be lazily loaded. The function get_blogs_of_user was updated to now lazily load site meta.

Follow on from [55671].

Props spacedmonkey, johnjamesjacoby, peterwilsoncc, mukesh27.
Fixes #58185.

Location:
trunk
Files:
6 edited

Legend:

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

    r55608 r55747  
    6262                                'callback' => array( $this, 'lazyload_meta_callback' ),
    6363                        ),
     64                        'blog'    => array(
     65                                'filter'   => 'get_blog_metadata',
     66                                'callback' => array( $this, 'lazyload_meta_callback' ),
     67                        ),
    6468                );
    6569        }
  • trunk/src/wp-includes/class-wp-site-query.php

    r55526 r55747  
    389389                $site_ids = array_map( 'intval', $site_ids );
    390390
     391                if ( $this->query_vars['update_site_meta_cache'] ) {
     392                        wp_lazyload_site_meta( $site_ids );
     393                }
     394
    391395                if ( 'ids' === $this->query_vars['fields'] ) {
    392396                        $this->sites = $site_ids;
     
    397401                // Prime site network caches.
    398402                if ( $this->query_vars['update_site_cache'] ) {
    399                         _prime_site_caches( $site_ids, $this->query_vars['update_site_meta_cache'] );
     403                        _prime_site_caches( $site_ids, false );
    400404                }
    401405
  • trunk/src/wp-includes/ms-site.php

    r55702 r55747  
    341341 * @since 5.1.0 Introduced the `$update_meta_cache` parameter.
    342342 * @since 6.1.0 This function is no longer marked as "private".
     343 * @since 6.3.0 Use wp_lazyload_site_meta() for lazy-loading of site meta.
    343344 *
    344345 * @see update_site_cache()
     
    355356                $fresh_sites = $wpdb->get_results( sprintf( "SELECT * FROM $wpdb->blogs WHERE blog_id IN (%s)", implode( ',', array_map( 'intval', $non_cached_ids ) ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
    356357
    357                 update_site_cache( $fresh_sites, $update_meta_cache );
    358         }
     358                update_site_cache( $fresh_sites, false );
     359        }
     360
     361        if ( $update_meta_cache ) {
     362                wp_lazyload_site_meta( $ids );
     363        }
     364}
     365
     366/**
     367 * Queue site meta for lazy-loading.
     368 *
     369 * @since 6.3.0
     370 *
     371 * @param array $site_ids List of site IDs.
     372 */
     373function wp_lazyload_site_meta( array $site_ids ) {
     374        if ( empty( $site_ids ) ) {
     375                return;
     376        }
     377        $lazyloader = wp_metadata_lazyloader();
     378        $lazyloader->queue_objects( 'blog', $site_ids );
    359379}
    360380
  • trunk/src/wp-includes/user.php

    r55703 r55747  
    10051005        if ( ! empty( $site_ids ) ) {
    10061006                $args = array(
    1007                         'number'                 => '',
    1008                         'site__in'               => $site_ids,
    1009                         'update_site_meta_cache' => false,
     1007                        'number'   => '',
     1008                        'site__in' => $site_ids,
    10101009                );
    10111010                if ( ! $all ) {
  • trunk/tests/phpunit/includes/abstract-testcase.php

    r55741 r55747  
    282282                $lazyloader->reset_queue( 'term' );
    283283                $lazyloader->reset_queue( 'comment' );
     284                $lazyloader->reset_queue( 'blog' );
    284285        }
    285286
  • trunk/tests/phpunit/tests/multisite/siteMeta.php

    r55745 r55747  
    249249                        $num_queries = get_num_queries();
    250250                        get_site_meta( self::$site_id, 'foo', true );
    251                         $this->assertSame( $num_queries, get_num_queries() );
     251                        $this->assertSame( 1, get_num_queries() - $num_queries );
     252                }
     253
     254                /**
     255                 * @ticket 58185
     256                 */
     257                public function test_lazy_load_site_meta() {
     258                        if ( ! is_site_meta_supported() ) {
     259                                $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
     260                        }
     261
     262                        $filter = new MockAction();
     263                        add_filter( 'update_blog_metadata_cache', array( $filter, 'filter' ), 10, 2 );
     264
     265                        $q = new WP_Site_Query(
     266                                array(
     267                                        'ID' => self::$site_id,
     268                                )
     269                        );
     270
     271                        $this->assertSameSets( array( (string) self::$site_id ), wp_list_pluck( $q->sites, 'blog_id' ), 'Site query should return the first test site' );
     272
     273                        $q = new WP_Site_Query(
     274                                array(
     275                                        'ID' => self::$site_id2,
     276                                )
     277                        );
     278
     279                        $this->assertSameSets( array( (string) self::$site_id2 ), wp_list_pluck( $q->sites, 'blog_id' ), 'Site query should return the second test site' );
     280
     281                        get_site_meta( self::$site_id2 );
     282
     283                        $args     = $filter->get_args();
     284                        $first    = reset( $args );
     285                        $site_ids = end( $first );
     286                        $this->assertSameSets( $site_ids, array( self::$site_id, self::$site_id2 ), 'This should have two site\'s meta' );
     287                }
     288
     289                /**
     290                 * @ticket 58185
     291                 */
     292                public function test_lazy_load_site_meta_fields_id() {
     293                        if ( ! is_site_meta_supported() ) {
     294                                $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
     295                        }
     296
     297                        $filter = new MockAction();
     298                        add_filter( 'update_blog_metadata_cache', array( $filter, 'filter' ), 10, 2 );
     299
     300                        $q = new WP_Site_Query(
     301                                array(
     302                                        'ID'     => self::$site_id,
     303                                        'fields' => 'ids',
     304                                )
     305                        );
     306
     307                        $this->assertSameSets( array( self::$site_id ), $q->sites, 'Site query should return the first test site' );
     308
     309                        $q = new WP_Site_Query(
     310                                array(
     311                                        'ID'     => self::$site_id2,
     312                                        'fields' => 'ids',
     313                                )
     314                        );
     315
     316                        $this->assertSameSets( array( self::$site_id2 ), $q->sites, 'Site query should return the second test site' );
     317
     318                        get_site_meta( self::$site_id2 );
     319
     320                        $args     = $filter->get_args();
     321                        $first    = reset( $args );
     322                        $site_ids = end( $first );
     323                        $this->assertSameSets( $site_ids, array( self::$site_id, self::$site_id2 ), 'This should have two sites meta' );
    252324                }
    253325
     
    268340                        $num_queries = get_num_queries();
    269341                        get_site_meta( self::$site_id, 'foo', true );
    270                         $this->assertSame( $num_queries + 1, get_num_queries() );
     342                        $this->assertSame( 1, get_num_queries() - $num_queries );
    271343                }
    272344
Note: See TracChangeset for help on using the changeset viewer.