mirror of
git://develop.git.wordpress.org/
synced 2025-02-11 10:30:44 +01:00
REST API: Prime caches for post parents in post REST API controller.
Prime caches for all post parents in the post REST API controller using the `_prime_post_caches` function. Post parent objects are required as part of the `check_read_permission` method’s permission check in post REST API controller. Props spacedmonkey, furi3r, peterwilsoncc, mitogh, madpixels. Fixes #55593. git-svn-id: https://develop.svn.wordpress.org/trunk@53506 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
770a909a5f
commit
9a488568a0
@ -7489,6 +7489,22 @@ function update_post_author_caches( $posts ) {
|
||||
cache_users( $author_ids );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prime post parent post caches.
|
||||
*
|
||||
* @since 6.1.0
|
||||
*
|
||||
* @param WP_Post[] $posts Array of Post objects.
|
||||
*/
|
||||
function update_post_parent_caches( $posts ) {
|
||||
$parent_ids = wp_list_pluck( $posts, 'post_parent' );
|
||||
$parent_ids = array_map( 'absint', $parent_ids );
|
||||
$parent_ids = array_unique( array_filter( $parent_ids ) );
|
||||
if ( ! empty( $parent_ids ) ) {
|
||||
_prime_post_caches( $parent_ids, false );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates metadata cache for a list of post IDs.
|
||||
*
|
||||
|
@ -369,6 +369,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
|
||||
|
||||
$posts = array();
|
||||
|
||||
update_post_parent_caches( $query_result );
|
||||
update_post_author_caches( $query_result );
|
||||
if ( post_type_supports( $this->post_type, 'thumbnail' ) ) {
|
||||
update_post_thumbnail_cache( $posts_query );
|
||||
|
@ -1787,6 +1787,47 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
|
||||
$this->assertSame( $formats, $data['schema']['properties']['format']['enum'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 55593
|
||||
*/
|
||||
public function test_get_items_parent_ids_primed() {
|
||||
$parent_id1 = self::$post_ids[0];
|
||||
$parent_id2 = self::$post_ids[1];
|
||||
$parent_ids = array( $parent_id2, $parent_id1 );
|
||||
|
||||
$this->factory->attachment->create_object(
|
||||
DIR_TESTDATA . '/images/canola.jpg',
|
||||
$parent_id1,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
'post_excerpt' => 'A sample caption 1',
|
||||
)
|
||||
);
|
||||
|
||||
$this->factory->attachment->create_object(
|
||||
DIR_TESTDATA . '/images/canola.jpg',
|
||||
$parent_id2,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
'post_excerpt' => 'A sample caption 2',
|
||||
)
|
||||
);
|
||||
|
||||
// Attachment creation warms parent ids. Needs clean up for test.
|
||||
wp_cache_delete_multiple( $parent_ids, 'posts' );
|
||||
|
||||
$filter = new MockAction();
|
||||
add_filter( 'update_post_metadata_cache', array( $filter, 'filter' ), 10, 2 );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/media' );
|
||||
rest_get_server()->dispatch( $request );
|
||||
|
||||
$args = $filter->get_args();
|
||||
$last = end( $args );
|
||||
$this->assertIsArray( $last, 'The last value is not an array' );
|
||||
$this->assertEqualSets( $parent_ids, $last[1] );
|
||||
}
|
||||
|
||||
public function test_get_item() {
|
||||
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
|
Loading…
x
Reference in New Issue
Block a user