Make WordPress Core

Changeset 53506


Ignore:
Timestamp:
06/15/2022 10:41:32 AM (23 months ago)
Author:
spacedmonkey
Message:

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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r53484 r53506  
    74917491
    74927492/**
     7493 * Prime post parent post caches.
     7494 *
     7495 * @since 6.1.0
     7496 *
     7497 * @param WP_Post[] $posts Array of Post objects.
     7498 */
     7499function update_post_parent_caches( $posts ) {
     7500    $parent_ids = wp_list_pluck( $posts, 'post_parent' );
     7501    $parent_ids = array_map( 'absint', $parent_ids );
     7502    $parent_ids = array_unique( array_filter( $parent_ids ) );
     7503    if ( ! empty( $parent_ids ) ) {
     7504        _prime_post_caches( $parent_ids, false );
     7505    }
     7506}
     7507
     7508/**
    74937509 * Updates metadata cache for a list of post IDs.
    74947510 *
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r53499 r53506  
    370370        $posts = array();
    371371
     372        update_post_parent_caches( $query_result );
    372373        update_post_author_caches( $query_result );
    373374        if ( post_type_supports( $this->post_type, 'thumbnail' ) ) {
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r53499 r53506  
    17861786
    17871787        $this->assertSame( $formats, $data['schema']['properties']['format']['enum'] );
     1788    }
     1789
     1790    /**
     1791     * @ticket 55593
     1792     */
     1793    public function test_get_items_parent_ids_primed() {
     1794        $parent_id1 = self::$post_ids[0];
     1795        $parent_id2 = self::$post_ids[1];
     1796        $parent_ids = array( $parent_id2, $parent_id1 );
     1797
     1798        $this->factory->attachment->create_object(
     1799            DIR_TESTDATA . '/images/canola.jpg',
     1800            $parent_id1,
     1801            array(
     1802                'post_mime_type' => 'image/jpeg',
     1803                'post_excerpt'   => 'A sample caption 1',
     1804            )
     1805        );
     1806
     1807        $this->factory->attachment->create_object(
     1808            DIR_TESTDATA . '/images/canola.jpg',
     1809            $parent_id2,
     1810            array(
     1811                'post_mime_type' => 'image/jpeg',
     1812                'post_excerpt'   => 'A sample caption 2',
     1813            )
     1814        );
     1815
     1816        // Attachment creation warms parent ids. Needs clean up for test.
     1817        wp_cache_delete_multiple( $parent_ids, 'posts' );
     1818
     1819        $filter = new MockAction();
     1820        add_filter( 'update_post_metadata_cache', array( $filter, 'filter' ), 10, 2 );
     1821
     1822        $request = new WP_REST_Request( 'GET', '/wp/v2/media' );
     1823        rest_get_server()->dispatch( $request );
     1824
     1825        $args = $filter->get_args();
     1826        $last = end( $args );
     1827        $this->assertIsArray( $last, 'The last value is not an array' );
     1828        $this->assertEqualSets( $parent_ids, $last[1] );
    17881829    }
    17891830
Note: See TracChangeset for help on using the changeset viewer.