mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
Options, Meta APIs: Improve the lazy loading meta API to include current 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. git-svn-id: https://develop.svn.wordpress.org/trunk@55608 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
c46a30eb1f
commit
7130af403f
@ -55,11 +55,11 @@ class WP_Metadata_Lazyloader {
|
||||
$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' ),
|
||||
),
|
||||
);
|
||||
}
|
||||
@ -91,7 +91,7 @@ class WP_Metadata_Lazyloader {
|
||||
}
|
||||
}
|
||||
|
||||
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.
|
||||
@ -131,20 +131,15 @@ class WP_Metadata_Lazyloader {
|
||||
* 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' );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -154,18 +149,48 @@ class WP_Metadata_Lazyloader {
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
||||
66
tests/phpunit/tests/query/lazyLoadCommentMeta.php
Normal file
66
tests/phpunit/tests/query/lazyLoadCommentMeta.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group comments
|
||||
* @group meta
|
||||
*/
|
||||
class Tests_Lazy_Load_Comment_Meta extends WP_UnitTestCase {
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected static $post_id;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected static $comment_ids = array();
|
||||
|
||||
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
|
||||
|
||||
self::$post_id = $factory->post->create();
|
||||
self::$comment_ids = $factory->comment->create_post_comments( self::$post_id, 11 );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 57901
|
||||
*
|
||||
* @covers ::wp_queue_comments_for_comment_meta_lazyload
|
||||
*/
|
||||
public function test_wp_queue_comments_for_comment_meta_lazyload() {
|
||||
$filter = new MockAction();
|
||||
add_filter( 'update_comment_metadata_cache', array( $filter, 'filter' ), 10, 2 );
|
||||
$comments = array_map( 'get_comment', self::$comment_ids );
|
||||
$comment_id = reset( self::$comment_ids );
|
||||
wp_queue_comments_for_comment_meta_lazyload( $comments );
|
||||
get_comment_meta( $comment_id );
|
||||
|
||||
$args = $filter->get_args();
|
||||
$first = reset( $args );
|
||||
$comment_meta_ids = end( $first );
|
||||
$this->assertSameSets( self::$comment_ids, $comment_meta_ids );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 57901
|
||||
*
|
||||
* @covers ::wp_queue_comments_for_comment_meta_lazyload
|
||||
*/
|
||||
public function test_wp_queue_comments_for_comment_meta_lazyload_new_comment() {
|
||||
$filter = new MockAction();
|
||||
add_filter( 'update_comment_metadata_cache', array( $filter, 'filter' ), 10, 2 );
|
||||
$comments = array_map( 'get_comment', self::$comment_ids );
|
||||
$comment_id = self::factory()->comment->create(
|
||||
array(
|
||||
'comment_post_ID' => self::$post_id,
|
||||
)
|
||||
);
|
||||
wp_queue_comments_for_comment_meta_lazyload( $comments );
|
||||
get_comment_meta( $comment_id );
|
||||
|
||||
$args = $filter->get_args();
|
||||
$first = reset( $args );
|
||||
$comment_meta_ids = end( $first );
|
||||
$this->assertContains( $comment_id, $comment_meta_ids );
|
||||
}
|
||||
}
|
||||
@ -108,6 +108,36 @@ class Test_Lazy_Load_Term_Meta extends WP_UnitTestCase {
|
||||
$this->assertSameSets( $term_ids, array( $term_id ) );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @ticket 57901
|
||||
*
|
||||
* @covers ::wp_queue_posts_for_term_meta_lazyload
|
||||
*/
|
||||
public function test_wp_queue_posts_for_term_meta_lazyload_insert_term() {
|
||||
$filter = new MockAction();
|
||||
add_filter( 'update_term_metadata_cache', array( $filter, 'filter' ), 10, 2 );
|
||||
|
||||
register_taxonomy( 'wptests_tax', 'post' );
|
||||
|
||||
$t1 = wp_insert_term( 'Foo', 'wptests_tax' );
|
||||
$term_id = $t1['term_id'];
|
||||
|
||||
new WP_Query(
|
||||
array(
|
||||
'post__in' => self::$post_ids,
|
||||
'lazy_load_term_meta' => true,
|
||||
)
|
||||
);
|
||||
|
||||
get_term_meta( $term_id );
|
||||
|
||||
$args = $filter->get_args();
|
||||
$first = reset( $args );
|
||||
$term_ids = end( $first );
|
||||
$this->assertContains( $term_id, $term_ids );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 57150
|
||||
* @covers ::wp_queue_posts_for_term_meta_lazyload
|
||||
@ -133,6 +163,6 @@ class Test_Lazy_Load_Term_Meta extends WP_UnitTestCase {
|
||||
$args = $filter->get_args();
|
||||
$first = reset( $args );
|
||||
$term_ids = end( $first );
|
||||
$this->assertNotContains( $remove_term_id, $term_ids );
|
||||
$this->assertContains( $remove_term_id, $term_ids );
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user