Improve lazyloading of comment meta in `WP_Query` loops.
Lazy-loading logic is moved to a method on `WP_Query`. This makes it possible for comment feeds to take advantage of metadata lazyloading, in addition to comments loaded via `comments_template()`. This new technique parallels the termmeta lazyloading technique introduced in [34704]. Fixes #34047. Built from https://develop.svn.wordpress.org/trunk@34711 git-svn-id: http://core.svn.wordpress.org/trunk@34675 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
21752923f0
commit
654eeb3785
|
@ -2452,30 +2452,6 @@ function _prime_comment_caches( $comment_ids, $update_meta_cache = true ) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Lazy load comment meta when inside of a `WP_Query` loop.
|
||||
*
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @param null $check The `$check` param passed from the 'pre_comment_metadata' hook.
|
||||
* @param int $comment_id ID of the comment whose metadata is being cached.
|
||||
* @return null In order not to short-circuit `get_metadata()`.
|
||||
*/
|
||||
function wp_lazyload_comment_meta( $check, $comment_id ) {
|
||||
global $wp_query;
|
||||
|
||||
if ( ! empty( $wp_query->comments ) ) {
|
||||
// Don't use `wp_list_pluck()` to avoid by-reference manipulation.
|
||||
$comment_ids = array();
|
||||
foreach ( $wp_query->comments as $comment ) {
|
||||
$comment_ids[] = $comment->comment_ID;
|
||||
}
|
||||
update_meta_cache( 'comment', $comment_ids );
|
||||
}
|
||||
|
||||
return $check;
|
||||
}
|
||||
|
||||
//
|
||||
// Internal
|
||||
//
|
||||
|
|
|
@ -1320,6 +1320,10 @@ function comments_template( $file = '/comments.php', $separate_comments = false
|
|||
* @param int $post_ID Post ID.
|
||||
*/
|
||||
$wp_query->comments = apply_filters( 'comments_array', $comments_flat, $post->ID );
|
||||
|
||||
// Set up lazy-loading for comment metadata.
|
||||
add_action( 'get_comment_metadata', array( $wp_query, 'lazyload_comment_meta' ), 10, 2 );
|
||||
|
||||
$comments = &$wp_query->comments;
|
||||
$wp_query->comment_count = count($wp_query->comments);
|
||||
$wp_query->max_num_comment_pages = $comment_query->max_num_pages;
|
||||
|
|
|
@ -200,7 +200,6 @@ add_filter( 'nav_menu_meta_box_object', '_wp_nav_menu_meta_box_object' );
|
|||
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( 'http_request_host_is_external', 'allowed_http_request_hosts', 10, 2 );
|
||||
|
||||
|
|
|
@ -1311,6 +1311,15 @@ class WP_Query {
|
|||
*/
|
||||
public $updated_term_meta_cache = false;
|
||||
|
||||
/**
|
||||
* Whether the comment meta cache for matched posts has been primed.
|
||||
*
|
||||
* @since 4.4.0
|
||||
* @access protected
|
||||
* @var bool
|
||||
*/
|
||||
public $updated_comment_meta_cache = false;
|
||||
|
||||
/**
|
||||
* Cached list of search stopwords.
|
||||
*
|
||||
|
@ -3682,6 +3691,11 @@ class WP_Query {
|
|||
}
|
||||
}
|
||||
|
||||
// If comments have been fetched as part of the query, make sure comment meta lazy-loading is set up.
|
||||
if ( ! empty( $this->comments ) ) {
|
||||
add_action( 'get_comment_metadata', array( $this, 'lazyload_comment_meta' ), 10, 2 );
|
||||
}
|
||||
|
||||
if ( ! $q['suppress_filters'] ) {
|
||||
/**
|
||||
* Filter the array of retrieved posts after they've been fetched and
|
||||
|
@ -4816,6 +4830,50 @@ class WP_Query {
|
|||
|
||||
return $check;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lazy-load comment meta when inside of a `WP_Query` loop.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* @param null $check The `$check` param passed from the 'pre_comment_metadata' hook.
|
||||
* @param int $comment_id ID of the comment whose metadata is being cached.
|
||||
* @return null In order not to short-circuit `get_metadata()`.
|
||||
*/
|
||||
public function lazyload_comment_meta( $check, $comment_id ) {
|
||||
/*
|
||||
* We only do this once per `WP_Query` instance.
|
||||
* Can't use `remove_action()` because of non-unique object hashes.
|
||||
*/
|
||||
if ( $this->updated_comment_meta_cache ) {
|
||||
return $check;
|
||||
}
|
||||
|
||||
// Don't use `wp_list_pluck()` to avoid by-reference manipulation.
|
||||
$comment_ids = array();
|
||||
if ( is_array( $this->comments ) ) {
|
||||
foreach ( $this->comments as $comment ) {
|
||||
$comment_ids[] = $comment->comment_ID;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Only update the metadata cache for comments belonging to these posts if the comment_id passed
|
||||
* to `get_comment_meta()` matches one of those comments. This prevents a single call to
|
||||
* `get_comment_meta()` from priming metadata for all `WP_Query` objects.
|
||||
*/
|
||||
if ( in_array( $comment_id, $comment_ids ) ) {
|
||||
update_meta_cache( 'comment', $comment_ids );
|
||||
$this->updated_comment_meta_cache = true;
|
||||
} elseif ( empty( $comment_ids ) ) {
|
||||
$this->updated_comment_meta_cache = true;
|
||||
}
|
||||
|
||||
return $check;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '4.4-alpha-34710';
|
||||
$wp_version = '4.4-alpha-34711';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
|
Loading…
Reference in New Issue