Skip to content

Commit

Permalink
Options, Meta APIs: Improve the lazy loading meta API to include curr…
Browse files Browse the repository at this point in the history
…ent object id.

The existing lazy loading meta api, creates a queue of ids, to be primed, if the `get_comment_meta` or `get_term_meta` functions are called. However, it did not check to see if the requested id was in the queue, before prime all the ids in the queue. Now, it adds the id to the queue, is not already in the queue, saving a cache lookup / database query. 

Props spacedmonkey, peterwilsoncc, mukesh27, flixos90.
Fixes #57901.
Built from https://develop.svn.wordpress.org/trunk@55608


git-svn-id: http://core.svn.wordpress.org/trunk@55120 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information
spacedmonkey committed Mar 29, 2023
1 parent 90b94ae commit 2da4542
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
55 changes: 40 additions & 15 deletions wp-includes/class-wp-metadata-lazyloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ 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' ),
),
);
}
Expand Down Expand Up @@ -91,7 +91,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 +131,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 +149,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' );
}

/**
* 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;
}

// No need to run again for this set of comments.
$this->reset_queue( 'comment' );
$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;
}
}
2 changes: 1 addition & 1 deletion wp-includes/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.3-alpha-55607';
$wp_version = '6.3-alpha-55608';

/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
Expand Down

0 comments on commit 2da4542

Please sign in to comment.