Make WordPress Core


Ignore:
Timestamp:
05/11/2023 11:13:10 AM (2 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.