Skip to content
This repository has been archived by the owner on Mar 9, 2024. It is now read-only.

WP-r55671: Taxonomy: Always lazily load term meta. #276

Merged
merged 13 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/wp-admin/includes/class-wp-comments-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,6 @@ public function prepare_items() {
$_comments = get_comments( $args );

if ( is_array( $_comments ) ) {
update_comment_cache( $_comments );

$this->items = array_slice( $_comments, 0, $comments_per_page );
$this->extra_items = array_slice( $_comments, $comments_per_page );

Expand Down
6 changes: 5 additions & 1 deletion src/wp-includes/class-wp-comment-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -481,12 +481,16 @@ public function get_comments() {

$comment_ids = array_map( 'intval', $comment_ids );

if ( $this->query_vars['update_comment_meta_cache'] ) {
wp_lazyload_comment_meta( $comment_ids );
}

if ( 'ids' === $this->query_vars['fields'] ) {
$this->comments = $comment_ids;
return $this->comments;
}

_prime_comment_caches( $comment_ids, $this->query_vars['update_comment_meta_cache'] );
_prime_comment_caches( $comment_ids, false );

// Fetch full comment objects from the primed cache.
$_comments = array();
Expand Down
59 changes: 44 additions & 15 deletions src/wp-includes/class-wp-metadata-lazyloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,15 @@ public function __construct() {
$this->settings = array(
'term' => array(
'filter' => 'get_term_metadata',
'callback' => array( $this, 'lazyload_term_meta' ),
'callback' => array( $this, 'lazyload_meta_callback' ),
),
'comment' => array(
'filter' => 'get_comment_metadata',
'callback' => array( $this, 'lazyload_comment_meta' ),
'callback' => array( $this, 'lazyload_meta_callback' ),
),
'blog' => array(
'filter' => 'get_blog_metadata',
'callback' => array( $this, 'lazyload_meta_callback' ),
),
);
}
Expand Down Expand Up @@ -91,7 +95,7 @@ public function queue_objects( $object_type, $object_ids ) {
}
}

add_filter( $type_settings['filter'], $type_settings['callback'] );
add_filter( $type_settings['filter'], $type_settings['callback'], 10, 5 );

/**
* Fires after objects are added to the metadata lazy-load queue.
Expand Down Expand Up @@ -131,20 +135,15 @@ public function reset_queue( $object_type ) {
* is no need to invoke it directly.
*
* @since 4.5.0
* @deprecated 6.3.0 Use WP_Metadata_Lazyloader::lazyload_meta_callback() instead.
*
* @param mixed $check The `$check` param passed from the 'get_term_metadata' hook.
* @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 ) {
if ( ! empty( $this->pending_objects['term'] ) ) {
update_termmeta_cache( array_keys( $this->pending_objects['term'] ) );

// No need to run again for this set of terms.
$this->reset_queue( 'term' );
}

return $check;
_deprecated_function( __METHOD__, '6.3.0', 'WP_Metadata_Lazyloader::lazyload_meta_callback' );
return $this->lazyload_meta_callback( $check, 0, '', false, 'term' );
}

/**
Expand All @@ -154,18 +153,48 @@ public function lazyload_term_meta( $check ) {
* directly, from either inside or outside the `WP_Query` object.
*
* @since 4.5.0
* @deprecated 6.3.0 Use WP_Metadata_Lazyloader::lazyload_meta_callback() instead.
*
* @param mixed $check The `$check` param passed from the {@see 'get_comment_metadata'} hook.
* @return mixed The original value of `$check`, so as not to short-circuit `get_comment_metadata()`.
*/
public function lazyload_comment_meta( $check ) {
if ( ! empty( $this->pending_objects['comment'] ) ) {
update_meta_cache( 'comment', array_keys( $this->pending_objects['comment'] ) );
_deprecated_function( __METHOD__, '6.3.0', 'WP_Metadata_Lazyloader::lazyload_meta_callback' );
return $this->lazyload_meta_callback( $check, 0, '', false, 'comment' );
}

// No need to run again for this set of comments.
$this->reset_queue( 'comment' );
/**
* Lazy-loads meta for queued objects.
*
* 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.
*
* @since 6.3.0
*
* @param mixed $check The `$check` param passed from the 'get_*_metadata' hook.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Unused.
* @param bool $single Unused.
* @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
* or any other object type with an associated meta table.
* @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_meta_callback( $check, $object_id, $meta_key, $single, $meta_type ) {
if ( empty( $this->pending_objects[ $meta_type ] ) ) {
return $check;
}

$object_ids = array_keys( $this->pending_objects[ $meta_type ] );
if ( $object_id && ! in_array( $object_id, $object_ids, true ) ) {
$object_ids[] = $object_id;
}

update_meta_cache( $meta_type, $object_ids );

// No need to run again for this set of objects.
$this->reset_queue( $meta_type );

return $check;
}
}
11 changes: 3 additions & 8 deletions src/wp-includes/class-wp-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -2813,7 +2813,7 @@ public function get_posts() {
$comment_ids = $wpdb->get_col( $comments_request );
wp_cache_add( $cache_key, $comment_ids, 'comment' );
}
_prime_comment_caches( $comment_ids, false );
_prime_comment_caches( $comment_ids );

// Convert to WP_Comment.
/** @var WP_Comment[] */
Expand Down Expand Up @@ -3372,7 +3372,7 @@ public function get_posts() {
$comment_ids = $wpdb->get_col( $comments_request );
wp_cache_add( $comment_cache_key, $comment_ids, 'comment' );
}
_prime_comment_caches( $comment_ids, false );
_prime_comment_caches( $comment_ids );

// Convert to WP_Comment.
/** @var WP_Comment[] */
Expand Down Expand Up @@ -3487,11 +3487,6 @@ public function get_posts() {
}
}

// If comments have been fetched as part of the query, make sure comment meta lazy-loading is set up.
if ( ! empty( $this->comments ) ) {
wp_queue_comments_for_comment_meta_lazyload( $this->comments );
}

if ( ! $q['suppress_filters'] ) {
/**
* Filters the array of retrieved posts after they've been fetched and
Expand Down Expand Up @@ -4873,7 +4868,7 @@ public function lazyload_term_meta( $check, $term_id ) {
* Lazyload comment meta for comments in the loop.
*
* @since 4.4.0
* @deprecated 4.5.0 See wp_queue_comments_for_comment_meta_lazyload().
* @deprecated 4.5.0 See wp_lazyload_comment_meta().
*
* @param mixed $check
* @param int $comment_id
Expand Down
6 changes: 5 additions & 1 deletion src/wp-includes/class-wp-site-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,10 @@ public function get_sites() {

$site_ids = array_map( 'intval', $site_ids );

if ( $this->query_vars['update_site_meta_cache'] ) {
wp_lazyload_site_meta( $site_ids );
}

if ( 'ids' === $this->query_vars['fields'] ) {
$this->sites = $site_ids;

Expand All @@ -396,7 +400,7 @@ public function get_sites() {

// Prime site network caches.
if ( $this->query_vars['update_site_cache'] ) {
_prime_site_caches( $site_ids, $this->query_vars['update_site_meta_cache'] );
_prime_site_caches( $site_ids, false );
}

// Fetch full site objects from the primed cache.
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/class-wp-term-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ public function get_terms() {
// Prime termmeta cache.
if ( $args['update_term_meta_cache'] ) {
$term_ids = wp_list_pluck( $term_objects, 'term_id' );
update_termmeta_cache( $term_ids );
wp_lazyload_term_meta( $term_ids );
}

if ( 'all_with_object_id' === $_fields && ! empty( $args['object_ids'] ) ) {
Expand Down
3 changes: 0 additions & 3 deletions src/wp-includes/comment-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -1494,7 +1494,6 @@ function comments_template( $file = '/comments.php', $separate_comments = false
'status' => 'approve',
'post_id' => $post->ID,
'no_found_rows' => false,
'update_comment_meta_cache' => false, // We lazy-load comment meta for performance.
);

if ( get_option( 'thread_comments' ) ) {
Expand Down Expand Up @@ -2406,8 +2405,6 @@ function wp_list_comments( $args = array(), $comments = null ) {
$parsed_args['reverse_top_level'] = ( 'desc' === get_option( 'comment_order' ) );
}

wp_queue_comments_for_comment_meta_lazyload( $_comments );

if ( empty( $parsed_args['walker'] ) ) {
$walker = new Walker_Comment();
} else {
Expand Down
46 changes: 21 additions & 25 deletions src/wp-includes/comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,21 @@ function get_comment_meta( $comment_id, $key = '', $single = false ) {
return get_metadata( 'comment', $comment_id, $key, $single );
}

/**
* Queue comment meta for lazy-loading.
*
* @since 6.3.0
*
* @param array $comment_ids List of comment IDs.
*/
function wp_lazyload_comment_meta( array $comment_ids ) {
if ( empty( $comment_ids ) ) {
return;
}
$lazyloader = wp_metadata_lazyloader();
$lazyloader->queue_objects( 'comment', $comment_ids );
}

/**
* Updates comment meta field based on comment ID.
*
Expand All @@ -510,30 +525,6 @@ function update_comment_meta( $comment_id, $meta_key, $meta_value, $prev_value =
return update_metadata( 'comment', $comment_id, $meta_key, $meta_value, $prev_value );
}

/**
* Queues comments for metadata lazy-loading.
*
* @since 4.5.0
*
* @param WP_Comment[] $comments Array of comment objects.
*/
function wp_queue_comments_for_comment_meta_lazyload( $comments ) {
// Don't use `wp_list_pluck()` to avoid by-reference manipulation.
$comment_ids = array();
if ( is_array( $comments ) ) {
foreach ( $comments as $comment ) {
if ( $comment instanceof WP_Comment ) {
$comment_ids[] = $comment->comment_ID;
}
}
}

if ( $comment_ids ) {
$lazyloader = wp_metadata_lazyloader();
$lazyloader->queue_objects( 'comment', $comment_ids );
}
}

/**
* Sets the cookies used to store an unauthenticated commentator's identity. Typically used
* to recall previous comments by this commentator that are still held in moderation.
Expand Down Expand Up @@ -3330,6 +3321,7 @@ function update_comment_cache( $comments, $update_meta_cache = true ) {
*
* @since 4.4.0
* @since 6.1.0 This function is no longer marked as "private".
* @since 6.3.0 Use wp_lazyload_comment_meta() for lazy-loading of comment meta.
*
* @see update_comment_cache()
* @global wpdb $wpdb WordPress database abstraction object.
Expand All @@ -3344,7 +3336,11 @@ function _prime_comment_caches( $comment_ids, $update_meta_cache = true ) {
if ( ! empty( $non_cached_ids ) ) {
$fresh_comments = $wpdb->get_results( sprintf( "SELECT $wpdb->comments.* FROM $wpdb->comments WHERE comment_ID IN (%s)", implode( ',', array_map( 'intval', $non_cached_ids ) ) ) );

update_comment_cache( $fresh_comments, $update_meta_cache );
update_comment_cache( $fresh_comments, false );
}

if ( $update_meta_cache ) {
wp_lazyload_comment_meta( $comment_ids );
}
}

Expand Down
23 changes: 23 additions & 0 deletions src/wp-includes/deprecated.php
Original file line number Diff line number Diff line change
Expand Up @@ -4453,3 +4453,26 @@ function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' )
function wlwmanifest_link() {
_deprecated_function( __FUNCTION__, '6.3.0' );
}

/**
* Queues comments for metadata lazy-loading.
*
* @since 4.5.0
* @deprecated 6.3.0 Use wp_lazyload_comment_meta() instead.
*
* @param WP_Comment[] $comments Array of comment objects.
*/
function wp_queue_comments_for_comment_meta_lazyload( $comments ) {
_deprecated_function( __FUNCTION__, '6.3.0', 'wp_lazyload_comment_meta' );
// Don't use `wp_list_pluck()` to avoid by-reference manipulation.
$comment_ids = array();
if ( is_array( $comments ) ) {
foreach ( $comments as $comment ) {
if ( $comment instanceof WP_Comment ) {
$comment_ids[] = $comment->comment_ID;
}
}
}

wp_lazyload_comment_meta( $comment_ids );
}
2 changes: 2 additions & 0 deletions src/wp-includes/meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* @subpackage Meta
*/

require ABSPATH . WPINC . '/class-wp-metadata-lazyloader.php';

/**
* Adds metadata for the specified object.
*
Expand Down
22 changes: 21 additions & 1 deletion src/wp-includes/ms-site.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ function get_site( $site = null ) {
* @since 4.6.0
* @since 5.1.0 Introduced the `$update_meta_cache` parameter.
* @since 6.1.0 This function is no longer marked as "private".
* @since 6.3.0 Use wp_lazyload_site_meta() for lazy-loading of site meta.
*
* @see update_site_cache()
* @global wpdb $wpdb WordPress database abstraction object.
Expand All @@ -354,8 +355,27 @@ function _prime_site_caches( $ids, $update_meta_cache = true ) {
if ( ! empty( $non_cached_ids ) ) {
$fresh_sites = $wpdb->get_results( sprintf( "SELECT * FROM $wpdb->blogs WHERE blog_id IN (%s)", implode( ',', array_map( 'intval', $non_cached_ids ) ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared

update_site_cache( $fresh_sites, $update_meta_cache );
update_site_cache( $fresh_sites, false );
}

if ( $update_meta_cache ) {
wp_lazyload_site_meta( $ids );
}
}

/**
* Queue site meta for lazy-loading.
*
* @since 6.3.0
*
* @param array $site_ids List of site IDs.
*/
function wp_lazyload_site_meta( array $site_ids ) {
if ( empty( $site_ids ) ) {
return;
}
$lazyloader = wp_metadata_lazyloader();
$lazyloader->queue_objects( 'blog', $site_ids );
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -7561,10 +7561,7 @@ function wp_queue_posts_for_term_meta_lazyload( $posts ) {
}
}

if ( $term_ids ) {
$lazyloader = wp_metadata_lazyloader();
$lazyloader->queue_objects( 'term', $term_ids );
}
wp_lazyload_term_meta( $term_ids );
}

/**
Expand Down
Loading
Loading