diff --git a/wp-includes/comment-functions.php b/wp-includes/comment-functions.php index 1380422d4a..453b5a8107 100644 --- a/wp-includes/comment-functions.php +++ b/wp-includes/comment-functions.php @@ -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 // diff --git a/wp-includes/comment-template.php b/wp-includes/comment-template.php index 7b6e44c094..abb87f656b 100644 --- a/wp-includes/comment-template.php +++ b/wp-includes/comment-template.php @@ -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; diff --git a/wp-includes/default-filters.php b/wp-includes/default-filters.php index e3e2ad0c12..30974c2cae 100644 --- a/wp-includes/default-filters.php +++ b/wp-includes/default-filters.php @@ -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 ); diff --git a/wp-includes/query.php b/wp-includes/query.php index 3ec605b513..f76c994ca1 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -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; + } } /** diff --git a/wp-includes/version.php b/wp-includes/version.php index 9dfdbc2e6c..1998764a9d 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.