Make WordPress Core

Ticket #43887: 43887.2.diff

File 43887.2.diff, 6.9 KB (added by birgire, 6 years ago)
  • src/wp-includes/blocks.php

    diff --git src/wp-includes/blocks.php src/wp-includes/blocks.php
    index 0bbc6a3..4795631 100644
    function _recurse_do_blocks( $blocks, $all_blocks ) { 
    261261        $rendered_content = preg_replace( '/<!--\s+\/?wp:.*?-->/m', '', $rendered_content );
    262262
    263263        return $rendered_content;
    264 }
    265  No newline at end of file
     264}
     265
     266/**
     267 * Returns the current version of the block format that the content string is using.
     268 *
     269 * If the string doesn't contain blocks, it returns 0.
     270 *
     271 * @since 5.0.0
     272 *
     273 * @param string $content Content to test.
     274 * @return int The block format version.
     275 */
     276function block_version( $content ) {
     277        return has_blocks( $content ) ? 1 : 0;
     278}
  • 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 3fbf169..6750a6b 100644
    class WP_REST_Posts_Controller extends WP_REST_Controller { 
    15091509
    15101510                if ( in_array( 'content', $fields, true ) ) {
    15111511                        $data['content'] = array(
    1512                                 'raw'       => $post->post_content,
     1512                                'raw'           => $post->post_content,
    15131513                                /** This filter is documented in wp-includes/post-template.php */
    1514                                 'rendered'  => post_password_required( $post ) ? '' : apply_filters( 'the_content', $post->post_content ),
    1515                                 'protected' => (bool) $post->post_password,
     1514                                'rendered'      => post_password_required( $post ) ? '' : apply_filters( 'the_content', $post->post_content ),
     1515                                'protected'     => (bool) $post->post_password,
     1516                                'block_version' => block_version( $post->post_content ),
    15161517                        );
    15171518                }
    15181519
    class WP_REST_Posts_Controller extends WP_REST_Controller { 
    20622063                                                                'context'     => array( 'view', 'edit' ),
    20632064                                                                'readonly'    => true,
    20642065                                                        ),
     2066                                                        'block_version' => array(
     2067                                                                'description' => __( 'Version of the content block format, used by the object.' ),
     2068                                                                'type'        => 'integer',
     2069                                                                'context'     => array( 'view', 'edit' ),
     2070                                                                'readonly'    => true,
     2071                                                        ),
    20652072                                                        'protected'       => array(
    20662073                                                                'description' => __( 'Whether the content is protected with a password.' ),
    20672074                                                                'type'        => 'boolean',
  • tests/phpunit/tests/blocks/block-type.php

    diff --git tests/phpunit/tests/blocks/block-type.php tests/phpunit/tests/blocks/block-type.php
    index 5eed28d..ba80ab6 100644
    class WP_Test_Block_Type extends WP_UnitTestCase { 
    310310
    311311                return json_encode( $attributes );
    312312        }
     313
     314        /**
     315         * Testing the block version.
     316         *
     317         * @ticket 43887
     318         *
     319         * @dataProvider data_block_version
     320         *
     321         * @param string|null $content  Content.
     322         * @param int         $expected Expected block version.
     323         */
     324        public function test_block_version( $content, $expected ) {
     325                $this->assertSame( $expected, block_version( $content ) );
     326        }
     327
     328        /**
     329         * Test cases for test_block_version().
     330         *
     331         * @since 5.0.0
     332         *
     333         * @return array {
     334         *     @type array {
     335         *         @type string|null Content.
     336         *         @type int         Expected block version.
     337         *     }
     338         * }
     339         */
     340        public function data_block_version() {
     341                return array(
     342                        // Null.
     343                        array( null, 0 ),
     344                        // Empty post content.
     345                        array( '', 0 ),
     346                        // Post content without blocks.
     347                        array( '<hr class="wp-block-separator" />', 0 ),
     348                        // Post content with a block.
     349                        array( '<!-- wp:core/separator -->', 1 ),
     350                        // Post content with a fake block.
     351                        array( '<!-- wp:core/fake --><!-- /wp:core/fake -->', 1 ),
     352                        // Post content with an invalid block.
     353                        array( '<!- - wp:core/separator -->', 0 ),
     354                );
     355        }
    313356}
  • 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 7969a84..7eb7c29 100644
    class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    13321332                $this->assertTrue( $data['excerpt']['protected'] );
    13331333        }
    13341334
     1335        /**
     1336         * The post response should have `block_version` indicate that block content is present when in view context.
     1337         *
     1338         * @ticket 43887
     1339         */
     1340        public function test_get_post_should_have_block_version_indicate_content_blocks_when_context_view() {
     1341                $post_id = $this->factory->post->create(
     1342                        array(
     1343                                'post_content' => '<!-- wp:core/separator -->',
     1344                        )
     1345                );
     1346
     1347                $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
     1348                $response = rest_get_server()->dispatch( $request );
     1349                $data = $response->get_data();
     1350                $this->assertSame( 1, $data['content']['block_version'] );
     1351        }
     1352
     1353        /**
     1354         * The post response should have `block_version` indicate that block content is present when in view context.
     1355         *
     1356         * @ticket 43887
     1357         */
     1358        public function test_get_post_should_have_block_version_indicate_no_content_blocks_when_context_view() {
     1359                $post_id = $this->factory->post->create(
     1360                        array(
     1361                                'post_content' => '<hr />',
     1362                        )
     1363                );
     1364
     1365                $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
     1366                $response = rest_get_server()->dispatch( $request );
     1367                $data = $response->get_data();
     1368                $this->assertSame( 0, $data['content']['block_version'] );
     1369        }
     1370
     1371        /**
     1372         * The post response should have `block_version` indicate that block content is present when in edit context.
     1373         *
     1374         * @ticket 43887
     1375         */
     1376        public function test_get_post_should_have_block_version_indicate_block_content_when_context_edit() {
     1377                wp_set_current_user( self::$editor_id );
     1378
     1379                $post_id = $this->factory->post->create(
     1380                        array(
     1381                                'post_content' => '<!-- wp:core/separator -->',
     1382                        )
     1383                );
     1384
     1385                $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
     1386                $request->set_param( 'context', 'edit' );
     1387                $response = rest_get_server()->dispatch( $request );
     1388                $data = $response->get_data();
     1389                $this->assertSame( 1, $data['content']['block_version'] );
     1390        }
     1391       
     1392        /**
     1393         * The post response should have `block_version` indicate that no block content is present when in edit context.
     1394         *
     1395         * @ticket 43887
     1396         */
     1397        public function test_get_post_should_have_block_version_indicate_no_block_content_when_context_edit() {
     1398                wp_set_current_user( self::$editor_id );
     1399
     1400                $post_id = $this->factory->post->create(
     1401                        array(
     1402                                'post_content' => '<hr />',
     1403                        )
     1404                );
     1405                $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
     1406                $request->set_param( 'context', 'edit' );
     1407                $response = rest_get_server()->dispatch( $request );
     1408                $data = $response->get_data();
     1409                $this->assertSame( 0, $data['content']['block_version'] );
     1410        }
     1411
    13351412        public function test_get_item_read_permission_custom_post_status_not_authenticated() {
    13361413                register_post_status( 'testpubstatus', array( 'public' => true ) );
    13371414                register_post_status( 'testprivtatus', array( 'public' => false ) );