Make WordPress Core

Ticket #43887: 43887.diff

File 43887.diff, 5.8 KB (added by birgire, 7 years ago)
  • src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
    index 0661152..ae113a3 100644
    class WP_REST_Posts_Controller extends WP_REST_Controller { 
    15011501
    15021502                if ( ! empty( $schema['properties']['content'] ) ) {
    15031503                        $data['content'] = array(
    1504                                 'raw'       => $post->post_content,
     1504                                'raw'           => $post->post_content,
    15051505                                /** This filter is documented in wp-includes/post-template.php */
    1506                                 'rendered'  => post_password_required( $post ) ? '' : apply_filters( 'the_content', $post->post_content ),
    1507                                 'protected' => (bool) $post->post_password,
     1506                                'rendered'      => post_password_required( $post ) ? '' : apply_filters( 'the_content', $post->post_content ),
     1507                                'protected'     => (bool) $post->post_password,
     1508                                // @todo: Replace with `gutenberg_content_block_version()` (or adjust accordingly) when Gutenberg merges into core.
     1509                                'block_version' => ( false !== strpos( $post->post_content, '<!-- wp:' ) ) ? 1 : 0,
    15081510                        );
    15091511                }
    15101512
    class WP_REST_Posts_Controller extends WP_REST_Controller { 
    19281930                                                        'validate_callback' => null, // Note: validation implemented in self::prepare_item_for_database()
    19291931                                                ),
    19301932                                                'properties'  => array(
    1931                                                         'raw'       => array(
     1933                                                        'raw'           => array(
    19321934                                                                'description' => __( 'Content for the object, as it exists in the database.' ),
    19331935                                                                'type'        => 'string',
    19341936                                                                'context'     => array( 'edit' ),
    19351937                                                        ),
    1936                                                         'rendered'  => array(
     1938                                                        'rendered'      => array(
    19371939                                                                'description' => __( 'HTML content for the object, transformed for display.' ),
    19381940                                                                'type'        => 'string',
    19391941                                                                'context'     => array( 'view', 'edit' ),
    19401942                                                                'readonly'    => true,
    19411943                                                        ),
    1942                                                         'protected' => array(
     1944                                                        'block_version' => array(
     1945                                                                'description' => __( 'Version of the content block format, used by the object.' ),
     1946                                                                'type'        => 'integer',
     1947                                                                'context'     => array( 'view', 'edit' ),
     1948                                                                'readonly'    => true,
     1949                                                        ),
     1950                                                        'protected'     => array(
    19431951                                                                'description' => __( 'Whether the content is protected with a password.' ),
    19441952                                                                'type'        => 'boolean',
    19451953                                                                'context'     => array( 'view', 'edit', 'embed' ),
  • tests/phpunit/tests/rest-api/rest-posts-controller.php

    diff --git tests/phpunit/tests/rest-api/rest-posts-controller.php tests/phpunit/tests/rest-api/rest-posts-controller.php
    index 5152399..3008741 100644
    class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    14641464                $this->assertTrue( $data['excerpt']['protected'] );
    14651465        }
    14661466
     1467        /**
     1468         * The post response should have `block_version` indicate that block content is present when in view context.
     1469         *
     1470         * @ticket 43887
     1471         */
     1472        public function test_get_post_should_have_block_version_indicate_content_blocks_when_context_view() {
     1473                $post_id = $this->factory->post->create(
     1474                        array(
     1475                                'post_content' => '<!-- wp:separator -->',
     1476                        )
     1477                );
     1478
     1479                $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
     1480                $response = rest_get_server()->dispatch( $request );
     1481
     1482                $this->check_get_post_response( $response, 'view' );
     1483                $data = $response->get_data();
     1484
     1485                $this->assertSame( 1, $data['content']['block_version'] );
     1486        }
     1487
     1488        /**
     1489         * The post response should have `block_version` indicate that block content is present when in view context.
     1490         *
     1491         * @ticket 43887
     1492         */
     1493        public function test_get_post_should_have_block_version_indicate_no_content_blocks_when_context_view() {
     1494                $post_id = $this->factory->post->create(
     1495                        array(
     1496                                'post_content' => '<hr/>',
     1497                        )
     1498                );
     1499
     1500                $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
     1501                $response = rest_get_server()->dispatch( $request );
     1502
     1503                $this->check_get_post_response( $response, 'view' );
     1504                $data = $response->get_data();
     1505
     1506                $this->assertSame( 0, $data['content']['block_version'] );
     1507        }
     1508
     1509        /**
     1510         * The post response should have `block_version` indicate that block content is present when in edit context.
     1511         *
     1512         * @ticket 43887
     1513         */
     1514        public function test_get_post_should_have_block_version_indicate_block_content_when_context_edit() {
     1515                wp_set_current_user( self::$editor_id );
     1516
     1517                $post_id = $this->factory->post->create(
     1518                        array(
     1519                                'post_content' => '<!-- wp:separator -->',
     1520                        )
     1521                );
     1522
     1523                $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
     1524                $request->set_param( 'context', 'edit' );
     1525                $response = rest_get_server()->dispatch( $request );
     1526
     1527                $this->check_get_post_response( $response, 'edit' );
     1528                $data = $response->get_data();
     1529
     1530                $this->assertSame( 1, $data['content']['block_version'] );
     1531        }
     1532       
     1533        /**
     1534         * The post response should have `block_version` indicate that no block content is present when in edit context.
     1535         *
     1536         * @ticket 43887
     1537         */
     1538        public function test_get_post_should_have_block_version_indicate_no_block_content_when_context_edit() {
     1539                wp_set_current_user( self::$editor_id );
     1540
     1541                $post_id = $this->factory->post->create(
     1542                        array(
     1543                                'post_content' => '<hr/>',
     1544                        )
     1545                );
     1546
     1547                $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
     1548                $request->set_param( 'context', 'edit' );
     1549                $response = rest_get_server()->dispatch( $request );
     1550
     1551                $this->check_get_post_response( $response, 'edit' );
     1552                $data = $response->get_data();
     1553
     1554                $this->assertSame( 0, $data['content']['block_version'] );
     1555        }
     1556
    14671557        public function test_get_item_read_permission_custom_post_status_not_authenticated() {
    14681558                register_post_status( 'testpubstatus', array( 'public' => true ) );
    14691559                register_post_status( 'testprivtatus', array( 'public' => false ) );