Make WordPress Core


Ignore:
Timestamp:
09/19/2019 02:04:51 PM (4 years ago)
Author:
kadamwhite
Message:

REST API: Support dot.nested hierarchical properties in _fields query parameter.

Enable clients to opt-in to receipt of one or more specific sub-properties within a response, and not other sub-properties.
Skip potentially expensive filtering and processing for post resources which were explicitly not requested.

Props kadamwhite, TimothyBlynJacobs, dlh.
Fixes #42094.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r45811 r46184  
    16881688            array_keys( $response->get_data() )
    16891689        );
     1690    }
     1691
     1692    /**
     1693     * @ticket 42094
     1694     */
     1695    public function test_prepare_item_filters_content_when_needed() {
     1696        $filter_count   = 0;
     1697        $filter_content = function() use ( &$filter_count ) {
     1698            $filter_count++;
     1699            return '<p>Filtered content.</p>';
     1700        };
     1701        add_filter( 'the_content', $filter_content );
     1702
     1703        wp_set_current_user( self::$editor_id );
     1704        $endpoint = new WP_REST_Posts_Controller( 'post' );
     1705        $request  = new WP_REST_REQUEST( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     1706
     1707        $request->set_param( 'context', 'edit' );
     1708        $request->set_param( '_fields', 'content.rendered' );
     1709
     1710        $post     = get_post( self::$post_id );
     1711        $response = $endpoint->prepare_item_for_response( $post, $request );
     1712
     1713        remove_filter( 'the_content', $filter_content );
     1714
     1715        $this->assertEquals(
     1716            array(
     1717                'id'      => self::$post_id,
     1718                'content' => array(
     1719                    'rendered' => '<p>Filtered content.</p>',
     1720                ),
     1721            ),
     1722            $response->get_data()
     1723        );
     1724        $this->assertSame( 1, $filter_count );
     1725    }
     1726
     1727    /**
     1728     * @ticket 42094
     1729     */
     1730    public function test_prepare_item_skips_content_filter_if_not_needed() {
     1731        $filter_count   = 0;
     1732        $filter_content = function() use ( &$filter_count ) {
     1733            $filter_count++;
     1734            return '<p>Filtered content.</p>';
     1735        };
     1736        add_filter( 'the_content', $filter_content );
     1737
     1738        wp_set_current_user( self::$editor_id );
     1739        $endpoint = new WP_REST_Posts_Controller( 'post' );
     1740        $request  = new WP_REST_REQUEST( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     1741
     1742        $request->set_param( 'context', 'edit' );
     1743        $request->set_param( '_fields', 'content.raw' );
     1744
     1745        $post     = get_post( self::$post_id );
     1746        $response = $endpoint->prepare_item_for_response( $post, $request );
     1747
     1748        remove_filter( 'the_content', $filter_content );
     1749
     1750        $this->assertEquals(
     1751            array(
     1752                'id'      => $post->ID,
     1753                'content' => array(
     1754                    'raw' => $post->post_content,
     1755                ),
     1756            ),
     1757            $response->get_data()
     1758        );
     1759        $this->assertSame( 0, $filter_count );
    16901760    }
    16911761
Note: See TracChangeset for help on using the changeset viewer.