mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
Improve lazyloading of term metadata in WP_Query loops.
[34529] introduced lazyloading for the metadata belonging to terms matching posts in the main `WP_Query`. The current changeset improves this technique in the following ways: * Term meta lazyloading is now performed on the results of all `WP_Query` queries, not just the main query. * Fewer global variable touches and greater encapsulation. * The logic for looping through posts to identify terms is now only performed once per `WP_Query`. Props dlh, boonebgorges. See #34047. git-svn-id: https://develop.svn.wordpress.org/trunk@34704 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
601ace8b25
commit
e75f2c024f
@ -201,7 +201,6 @@ add_filter( 'pingback_ping_source_uri', 'pingback_ping_source_uri' );
|
||||
add_filter( 'xmlrpc_pingback_error', 'xmlrpc_pingback_error' );
|
||||
add_filter( 'title_save_pre', 'trim' );
|
||||
add_filter( 'get_comment_metadata', 'wp_lazyload_comment_meta', 10, 2 );
|
||||
add_filter( 'get_term_metadata', 'wp_lazyload_term_meta', 10, 2 );
|
||||
|
||||
add_filter( 'http_request_host_is_external', 'allowed_http_request_hosts', 10, 2 );
|
||||
|
||||
|
||||
@ -1302,6 +1302,15 @@ class WP_Query {
|
||||
*/
|
||||
public $thumbnails_cached = false;
|
||||
|
||||
/**
|
||||
* Whether the term meta cache for matched posts has been primed.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access protected
|
||||
* @var bool
|
||||
*/
|
||||
public $updated_term_meta_cache = false;
|
||||
|
||||
/**
|
||||
* Cached list of search stopwords.
|
||||
*
|
||||
@ -3541,6 +3550,11 @@ class WP_Query {
|
||||
if ( $this->posts )
|
||||
$this->posts = array_map( 'get_post', $this->posts );
|
||||
|
||||
|
||||
if ( $q['update_post_term_cache'] ) {
|
||||
add_action( 'get_term_metadata', array( $this, 'lazyload_term_meta' ), 10, 2 );
|
||||
}
|
||||
|
||||
if ( ! $q['suppress_filters'] ) {
|
||||
/**
|
||||
* Filter the raw post results array, prior to status checks.
|
||||
@ -4722,6 +4736,86 @@ class WP_Query {
|
||||
$this->setup_postdata( $this->post );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Lazy-loads termmeta for located posts.
|
||||
*
|
||||
* As a rule, term queries (`get_terms()` and `wp_get_object_terms()`) prime the metadata cache for matched
|
||||
* terms by default. However, this can cause a slight performance penalty, especially when that metadata is
|
||||
* not actually used. In the context of a `WP_Query` instance, we're able to avoid this potential penalty.
|
||||
* `update_object_term_cache()`, called from `update_post_caches()`, does not 'update_term_meta_cache'.
|
||||
* Instead, the first time `get_term_meta()` is called from within a `WP_Query` loop, the current method
|
||||
* detects the fact, and then primes the metadata cache for all terms attached to all posts in the loop,
|
||||
* with a single database query.
|
||||
*
|
||||
* This method is public so that it can be used as a filter callback. As a rule, there is no need to invoke it
|
||||
* directly, from either inside or outside the `WP_Query` object.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param null $check The `$check` param passed from the 'pre_term_metadata' hook.
|
||||
* @param int $term_id ID of the term whose metadata is being cached.
|
||||
* @return mixed In order not to short-circuit `get_metadata()`. Generally, this is `null`, but it could be
|
||||
* another value if filtered by a plugin.
|
||||
*/
|
||||
public function lazyload_term_meta( $check, $term_id ) {
|
||||
/*
|
||||
* We only do this once per `WP_Query` instance.
|
||||
* Can't use `remove_action()` because of non-unique object hashes.
|
||||
*/
|
||||
if ( $this->updated_term_meta_cache ) {
|
||||
return $check;
|
||||
}
|
||||
|
||||
// We can only lazyload if the entire post object is present.
|
||||
$posts = array();
|
||||
foreach ( $this->posts as $post ) {
|
||||
if ( $post instanceof WP_Post ) {
|
||||
$posts[] = $post;
|
||||
}
|
||||
}
|
||||
$_p = array();
|
||||
foreach ( $posts as $post ) {
|
||||
$_p[] = $post->ID;
|
||||
}
|
||||
|
||||
if ( ! empty( $posts ) ) {
|
||||
// Fetch cached term_ids for each post. Keyed by term_id for faster lookup.
|
||||
$term_ids = array();
|
||||
foreach ( $posts as $post ) {
|
||||
$taxonomies = get_object_taxonomies( $post->post_type );
|
||||
foreach ( $taxonomies as $taxonomy ) {
|
||||
// Term cache should already be primed by 'update_post_term_cache'.
|
||||
$terms = get_object_term_cache( $post->ID, $taxonomy );
|
||||
if ( false !== $terms ) {
|
||||
foreach ( $terms as $term ) {
|
||||
if ( ! isset( $term_ids[ $term->term_id ] ) ) {
|
||||
$term_ids[ $term->term_id ] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Only update the metadata cache for terms belonging to these posts if the term_id passed
|
||||
* to `get_term_meta()` matches one of those terms. This prevents a single call to
|
||||
* `get_term_meta()` from priming metadata for all `WP_Query` objects.
|
||||
*/
|
||||
if ( isset( $term_ids[ $term_id ] ) ) {
|
||||
update_termmeta_cache( array_keys( $term_ids ) );
|
||||
$this->updated_term_meta_cache = true;
|
||||
}
|
||||
}
|
||||
|
||||
// If no terms were found, there's no need to run this again.
|
||||
if ( empty( $term_ids ) ) {
|
||||
$this->updated_term_meta_cache = true;
|
||||
}
|
||||
|
||||
return $check;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -1588,63 +1588,6 @@ function update_termmeta_cache( $term_ids ) {
|
||||
return update_meta_cache( 'term', $term_ids );
|
||||
}
|
||||
|
||||
/**
|
||||
* Lazy-loads termmeta when inside of a `WP_Query` loop.
|
||||
*
|
||||
* As a rule, term queries (`get_terms()` and `wp_get_object_terms()`) prime the metadata cache for matched terms by
|
||||
* default. However, this can cause a slight performance penalty, especially when that metadata is not actually used.
|
||||
* In the context of a `WP_Query` loop, we're able to avoid this potential penalty. `update_object_term_cache()`,
|
||||
* called from `update_post_caches()`, does not 'update_term_meta_cache'. Instead, the first time `get_term_meta()` is
|
||||
* called from within a `WP_Query` loop, the current function detects the fact, and then primes the metadata cache for
|
||||
* all terms attached to all posts in the loop, with a single database query.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @param null $check The `$check` param passed from the 'pre_term_metadata' hook.
|
||||
* @param int $term_id ID of the term whose metadata is being cached.
|
||||
* @return null In order not to short-circuit `get_metadata()`.
|
||||
*/
|
||||
function wp_lazyload_term_meta( $check, $term_id ) {
|
||||
global $wp_query;
|
||||
|
||||
if ( $wp_query instanceof WP_Query && ! empty( $wp_query->posts ) && $wp_query->get( 'update_post_term_cache' ) ) {
|
||||
// We can only lazyload if the entire post object is present.
|
||||
$posts = array();
|
||||
foreach ( $wp_query->posts as $post ) {
|
||||
if ( $post instanceof WP_Post ) {
|
||||
$posts[] = $post;
|
||||
}
|
||||
}
|
||||
|
||||
if ( empty( $posts ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Fetch cached term_ids for each post. Keyed by term_id for faster lookup.
|
||||
$term_ids = array();
|
||||
foreach ( $posts as $post ) {
|
||||
$taxonomies = get_object_taxonomies( $post->post_type );
|
||||
foreach ( $taxonomies as $taxonomy ) {
|
||||
// No extra queries. Term cache should already be primed by 'update_post_term_cache'.
|
||||
$terms = get_object_term_cache( $post->ID, $taxonomy );
|
||||
if ( false !== $terms ) {
|
||||
foreach ( $terms as $term ) {
|
||||
if ( ! isset( $term_ids[ $term->term_id ] ) ) {
|
||||
$term_ids[ $term->term_id ] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( $term_ids ) {
|
||||
update_termmeta_cache( array_keys( $term_ids ) );
|
||||
}
|
||||
}
|
||||
|
||||
return $check;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if Term exists.
|
||||
*
|
||||
|
||||
@ -145,6 +145,58 @@ class Tests_Term_Meta extends WP_UnitTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 34073
|
||||
*/
|
||||
public function test_term_meta_should_be_lazy_loaded_only_for_the_queries_in_which_the_term_has_posts() {
|
||||
global $wpdb;
|
||||
|
||||
$posts = $this->factory->post->create_many( 3, array( 'post_status' => 'publish' ) );
|
||||
register_taxonomy( 'wptests_tax', 'post' );
|
||||
$terms = $this->factory->term->create_many( 6, array( 'taxonomy' => 'wptests_tax' ) );
|
||||
|
||||
wp_set_object_terms( $posts[0], array( $terms[0], $terms[1] ), 'wptests_tax' );
|
||||
wp_set_object_terms( $posts[1], array( $terms[2], $terms[3] ), 'wptests_tax' );
|
||||
wp_set_object_terms( $posts[2], array( $terms[0], $terms[4], $terms[5] ), 'wptests_tax' );
|
||||
|
||||
foreach ( $terms as $t ) {
|
||||
add_term_meta( $t, 'foo', 'bar' );
|
||||
}
|
||||
|
||||
$q0 = new WP_Query( array( 'p' => $posts[0] ) );
|
||||
$q1 = new WP_Query( array( 'p' => $posts[1] ) );
|
||||
$q2 = new WP_Query( array( 'p' => $posts[2] ) );
|
||||
|
||||
/*
|
||||
* $terms[0] belongs to both $posts[0] and $posts[2], so `get_term_meta( $terms[0] )` should prime
|
||||
* the cache for term matched by $q0 and $q2.
|
||||
*/
|
||||
|
||||
// First request will hit the database.
|
||||
$num_queries = $wpdb->num_queries;
|
||||
|
||||
// Prime caches.
|
||||
$this->assertSame( 'bar', get_term_meta( $terms[0], 'foo', true ) );
|
||||
|
||||
// Two queries: one for $q0 and one for $q2.
|
||||
$num_queries += 2;
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
|
||||
// Next requests should be in cache.
|
||||
$this->assertSame( 'bar', get_term_meta( $terms[1], 'foo', true ) );
|
||||
$this->assertSame( 'bar', get_term_meta( $terms[4], 'foo', true ) );
|
||||
$this->assertSame( 'bar', get_term_meta( $terms[5], 'foo', true ) );
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
|
||||
// Querying for $terms[2] will prime $terms[3] as well.
|
||||
$this->assertSame( 'bar', get_term_meta( $terms[2], 'foo', true ) );
|
||||
$num_queries++;
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
|
||||
$this->assertSame( 'bar', get_term_meta( $terms[3], 'foo', true ) );
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
}
|
||||
|
||||
public function test_adding_term_meta_should_bust_get_terms_cache() {
|
||||
$terms = $this->factory->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user