mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
In [34529] introduced lazy loading of term meta. However, this was only in the context of `WP_Query`. Other parts of the codebase, like `WP_Term_Query` did not lazily load term meta. In this change, calls to `update_termmeta_cache` are now replaced with `wp_lazyload_term_meta`, that instead of priming term meta caches, just adds them to the queue to be primed it ever called. This results in far less database queries, as there a number of places where term meta is being primed unnecessarily and never used. Adding everything to the term meta queue, also means that if term meta is used, that is all loaded in a single database / cache call. Props spacedmonkey, mukesh27, peterwilsoncc. Fixes #57645. git-svn-id: https://develop.svn.wordpress.org/trunk@55671 602fd350-edb4-49c9-b593-d223f7449a82
170 lines
4.2 KiB
PHP
170 lines
4.2 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @group query
|
|
* @group taxonomy
|
|
* @group meta
|
|
*/
|
|
class Test_Lazy_Load_Term_Meta extends WP_UnitTestCase {
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected static $post_ids = array();
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected static $term_ids = array();
|
|
|
|
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
|
|
$post_type = 'post';
|
|
self::$post_ids = $factory->post->create_many(
|
|
3,
|
|
array(
|
|
'post_type' => $post_type,
|
|
'post_status' => 'publish',
|
|
)
|
|
);
|
|
$taxonomies = get_object_taxonomies( $post_type, 'object' );
|
|
foreach ( self::$post_ids as $post_id ) {
|
|
foreach ( $taxonomies as $taxonomy ) {
|
|
if ( ! $taxonomy->_builtin ) {
|
|
continue;
|
|
}
|
|
$terms = $factory->term->create_many( 3, array( 'taxonomy' => $taxonomy->name ) );
|
|
self::$term_ids = array_merge( self::$term_ids, $terms );
|
|
foreach ( $terms as $term ) {
|
|
add_term_meta( $term, wp_rand(), 'test' );
|
|
}
|
|
wp_set_object_terms( $post_id, $terms, $taxonomy->name );
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @ticket 57150
|
|
* @covers ::wp_queue_posts_for_term_meta_lazyload
|
|
*/
|
|
public function test_wp_queue_posts_for_term_meta_lazyload() {
|
|
$this->reset_lazyload_queue();
|
|
$filter = new MockAction();
|
|
add_filter( 'update_term_metadata_cache', array( $filter, 'filter' ), 10, 2 );
|
|
new WP_Query(
|
|
array(
|
|
'post__in' => self::$post_ids,
|
|
'lazy_load_term_meta' => true,
|
|
)
|
|
);
|
|
|
|
get_term_meta( end( self::$term_ids ) );
|
|
|
|
$args = $filter->get_args();
|
|
$first = reset( $args );
|
|
$term_ids = end( $first );
|
|
$this->assertSameSets( $term_ids, self::$term_ids );
|
|
}
|
|
|
|
/**
|
|
* @ticket 57150
|
|
* @covers ::wp_queue_posts_for_term_meta_lazyload
|
|
*/
|
|
public function test_wp_queue_posts_for_term_meta_lazyload_update_post_term_cache() {
|
|
$filter = new MockAction();
|
|
add_filter( 'update_term_metadata_cache', array( $filter, 'filter' ), 10, 2 );
|
|
new WP_Query(
|
|
array(
|
|
'post__in' => self::$post_ids,
|
|
'lazy_load_term_meta' => true,
|
|
'update_post_term_cache' => false,
|
|
)
|
|
);
|
|
|
|
get_term_meta( end( self::$term_ids ) );
|
|
|
|
$args = $filter->get_args();
|
|
$first = reset( $args );
|
|
$term_ids = end( $first );
|
|
$this->assertSameSets( $term_ids, self::$term_ids );
|
|
}
|
|
|
|
/**
|
|
* @ticket 57150
|
|
* @covers ::wp_queue_posts_for_term_meta_lazyload
|
|
*/
|
|
public function test_wp_queue_posts_for_term_meta_lazyload_false() {
|
|
$filter = new MockAction();
|
|
add_filter( 'update_term_metadata_cache', array( $filter, 'filter' ), 10, 2 );
|
|
new WP_Query(
|
|
array(
|
|
'post__in' => self::$post_ids,
|
|
'lazy_load_term_meta' => false,
|
|
)
|
|
);
|
|
|
|
$term_id = end( self::$term_ids );
|
|
get_term_meta( $term_id );
|
|
|
|
$args = $filter->get_args();
|
|
$first = reset( $args );
|
|
$term_ids = end( $first );
|
|
$this->assertSameSets( $term_ids, array( $term_id ) );
|
|
}
|
|
|
|
|
|
/**
|
|
* @ticket 57901
|
|
*
|
|
* @covers ::wp_queue_posts_for_term_meta_lazyload
|
|
*/
|
|
public function test_wp_queue_posts_for_term_meta_lazyload_insert_term() {
|
|
$filter = new MockAction();
|
|
add_filter( 'update_term_metadata_cache', array( $filter, 'filter' ), 10, 2 );
|
|
|
|
register_taxonomy( 'wptests_tax', 'post' );
|
|
|
|
$t1 = wp_insert_term( 'Foo', 'wptests_tax' );
|
|
$term_id = $t1['term_id'];
|
|
|
|
new WP_Query(
|
|
array(
|
|
'post__in' => self::$post_ids,
|
|
'lazy_load_term_meta' => true,
|
|
)
|
|
);
|
|
|
|
get_term_meta( $term_id );
|
|
|
|
$args = $filter->get_args();
|
|
$first = reset( $args );
|
|
$term_ids = end( $first );
|
|
$this->assertContains( $term_id, $term_ids );
|
|
}
|
|
|
|
/**
|
|
* @ticket 57150
|
|
* @covers ::wp_queue_posts_for_term_meta_lazyload
|
|
*/
|
|
public function test_wp_queue_posts_for_term_meta_lazyload_delete_term() {
|
|
$filter = new MockAction();
|
|
add_filter( 'update_term_metadata_cache', array( $filter, 'filter' ), 10, 2 );
|
|
|
|
$remove_term_id = end( self::$term_ids );
|
|
$term = get_term( $remove_term_id );
|
|
wp_delete_term( $remove_term_id, $term->taxonomy );
|
|
|
|
new WP_Query(
|
|
array(
|
|
'post__in' => self::$post_ids,
|
|
'lazy_load_term_meta' => true,
|
|
)
|
|
);
|
|
|
|
$term_id = end( self::$term_ids );
|
|
get_term_meta( $term_id );
|
|
|
|
$args = $filter->get_args();
|
|
$first = reset( $args );
|
|
$term_ids = end( $first );
|
|
$this->assertContains( $remove_term_id, $term_ids );
|
|
}
|
|
}
|