Make WordPress Core

Ticket #43887: 43887.3.2.diff

File 43887.3.2.diff, 11.5 KB (added by danielbachhuber, 6 years ago)
  • src/wp-includes/blocks.php

    diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php
    index 0bbc6a345c..4795631de7 100644
    a b 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 a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
    index 3fbf169b76..c2b707dfa0 100644
    a b 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( '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 a/tests/phpunit/tests/blocks/block-type.php b/tests/phpunit/tests/blocks/block-type.php
    index 5eed28d0df..ba80ab69a9 100644
    a b 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 a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php
    index 7969a84b45..10f6a6eebc 100644
    a b 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 not have `block_version` when in view context.
     1337         *
     1338         * @ticket 43887
     1339         */
     1340        public function test_get_post_should_not_have_block_version_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->assertFalse( isset( $data['content']['block_version'] ) );
     1351        }
     1352
     1353        /**
     1354         * The post response should have `block_version` indicate that block content is present when in edit context.
     1355         *
     1356         * @ticket 43887
     1357         */
     1358        public function test_get_post_should_have_block_version_indicate_block_content_when_context_edit() {
     1359                wp_set_current_user( self::$editor_id );
     1360
     1361                $post_id = $this->factory->post->create(
     1362                        array(
     1363                                'post_content' => '<!-- wp:core/separator -->',
     1364                        )
     1365                );
     1366
     1367                $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
     1368                $request->set_param( 'context', 'edit' );
     1369                $response = rest_get_server()->dispatch( $request );
     1370                $data = $response->get_data();
     1371                $this->assertSame( 1, $data['content']['block_version'] );
     1372        }
     1373       
     1374        /**
     1375         * The post response should have `block_version` indicate that no block content is present when in edit context.
     1376         *
     1377         * @ticket 43887
     1378         */
     1379        public function test_get_post_should_have_block_version_indicate_no_block_content_when_context_edit() {
     1380                wp_set_current_user( self::$editor_id );
     1381
     1382                $post_id = $this->factory->post->create(
     1383                        array(
     1384                                'post_content' => '<hr />',
     1385                        )
     1386                );
     1387                $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
     1388                $request->set_param( 'context', 'edit' );
     1389                $response = rest_get_server()->dispatch( $request );
     1390                $data = $response->get_data();
     1391                $this->assertSame( 0, $data['content']['block_version'] );
     1392        }
     1393
    13351394        public function test_get_item_read_permission_custom_post_status_not_authenticated() {
    13361395                register_post_status( 'testpubstatus', array( 'public' => true ) );
    13371396                register_post_status( 'testprivtatus', array( 'public' => false ) );
  • tests/qunit/fixtures/wp-api-generated.js

    diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js
    index 42cd329df9..8a236da00d 100644
    a b mockedApiResponse.postRevisions = [ 
    43424342        "author": 389,
    43434343        "date": "2017-02-14T00:00:00",
    43444344        "date_gmt": "2017-02-14T00:00:00",
    4345         "id": 36707,
     4345        "id": 36710,
    43464346        "modified": "2017-02-14T00:00:00",
    43474347        "modified_gmt": "2017-02-14T00:00:00",
    4348         "parent": 36706,
    4349         "slug": "36706-revision-v1",
     4348        "parent": 36709,
     4349        "slug": "36709-revision-v1",
    43504350        "guid": {
    4351             "rendered": "http://example.org/?p=36707"
     4351            "rendered": "http://example.org/?p=36710"
    43524352        },
    43534353        "title": {
    43544354            "rendered": "REST API Client Fixture: Post"
    mockedApiResponse.postRevisions = [ 
    43624362        "_links": {
    43634363            "parent": [
    43644364                {
    4365                     "href": "http://example.org/index.php?rest_route=/wp/v2/posts/36706"
     4365                    "href": "http://example.org/index.php?rest_route=/wp/v2/posts/36709"
    43664366                }
    43674367            ]
    43684368        }
    mockedApiResponse.postAutosaves = [ 
    43974397        "author": 389,
    43984398        "date": "2017-02-14T00:00:00",
    43994399        "date_gmt": "2017-02-14T00:00:00",
    4400         "id": 36708,
     4400        "id": 36711,
    44014401        "modified": "2017-02-14T00:00:00",
    44024402        "modified_gmt": "2017-02-14T00:00:00",
    4403         "parent": 36706,
    4404         "slug": "36706-autosave-v1",
     4403        "parent": 36709,
     4404        "slug": "36709-autosave-v1",
    44054405        "guid": {
    4406             "rendered": "http://example.org/?p=36708"
     4406            "rendered": "http://example.org/?p=36711"
    44074407        },
    44084408        "title": {
    44094409            "rendered": ""
    mockedApiResponse.postAutosaves = [ 
    44174417        "_links": {
    44184418            "parent": [
    44194419                {
    4420                     "href": "http://example.org/index.php?rest_route=/wp/v2/posts/36706"
     4420                    "href": "http://example.org/index.php?rest_route=/wp/v2/posts/36709"
    44214421                }
    44224422            ]
    44234423        }
    mockedApiResponse.autosave = { 
    44284428    "author": 389,
    44294429    "date": "2017-02-14T00:00:00",
    44304430    "date_gmt": "2017-02-14T00:00:00",
    4431     "id": 36708,
     4431    "id": 36711,
    44324432    "modified": "2017-02-14T00:00:00",
    44334433    "modified_gmt": "2017-02-14T00:00:00",
    4434     "parent": 36706,
    4435     "slug": "36706-autosave-v1",
     4434    "parent": 36709,
     4435    "slug": "36709-autosave-v1",
    44364436    "guid": {
    4437         "rendered": "http://example.org/?p=36708"
     4437        "rendered": "http://example.org/?p=36711"
    44384438    },
    44394439    "title": {
    44404440        "rendered": ""
    mockedApiResponse.pageRevisions = [ 
    46024602        "author": 389,
    46034603        "date": "2017-02-14T00:00:00",
    46044604        "date_gmt": "2017-02-14T00:00:00",
    4605         "id": 36710,
     4605        "id": 36713,
    46064606        "modified": "2017-02-14T00:00:00",
    46074607        "modified_gmt": "2017-02-14T00:00:00",
    4608         "parent": 36709,
    4609         "slug": "36709-revision-v1",
     4608        "parent": 36712,
     4609        "slug": "36712-revision-v1",
    46104610        "guid": {
    4611             "rendered": "http://example.org/?p=36710"
     4611            "rendered": "http://example.org/?p=36713"
    46124612        },
    46134613        "title": {
    46144614            "rendered": "REST API Client Fixture: Page"
    mockedApiResponse.pageRevisions = [ 
    46224622        "_links": {
    46234623            "parent": [
    46244624                {
    4625                     "href": "http://example.org/index.php?rest_route=/wp/v2/pages/36709"
     4625                    "href": "http://example.org/index.php?rest_route=/wp/v2/pages/36712"
    46264626                }
    46274627            ]
    46284628        }
    mockedApiResponse.pageAutosaves = [ 
    46574657        "author": 389,
    46584658        "date": "2017-02-14T00:00:00",
    46594659        "date_gmt": "2017-02-14T00:00:00",
    4660         "id": 36711,
     4660        "id": 36714,
    46614661        "modified": "2017-02-14T00:00:00",
    46624662        "modified_gmt": "2017-02-14T00:00:00",
    4663         "parent": 36709,
    4664         "slug": "36709-autosave-v1",
     4663        "parent": 36712,
     4664        "slug": "36712-autosave-v1",
    46654665        "guid": {
    4666             "rendered": "http://example.org/?p=36711"
     4666            "rendered": "http://example.org/?p=36714"
    46674667        },
    46684668        "title": {
    46694669            "rendered": ""
    mockedApiResponse.pageAutosaves = [ 
    46774677        "_links": {
    46784678            "parent": [
    46794679                {
    4680                     "href": "http://example.org/index.php?rest_route=/wp/v2/pages/36709"
     4680                    "href": "http://example.org/index.php?rest_route=/wp/v2/pages/36712"
    46814681                }
    46824682            ]
    46834683        }
    mockedApiResponse.pageAutosave = { 
    46884688    "author": 389,
    46894689    "date": "2017-02-14T00:00:00",
    46904690    "date_gmt": "2017-02-14T00:00:00",
    4691     "id": 36711,
     4691    "id": 36714,
    46924692    "modified": "2017-02-14T00:00:00",
    46934693    "modified_gmt": "2017-02-14T00:00:00",
    4694     "parent": 36709,
    4695     "slug": "36709-autosave-v1",
     4694    "parent": 36712,
     4695    "slug": "36712-autosave-v1",
    46964696    "guid": {
    4697         "rendered": "http://example.org/?p=36711"
     4697        "rendered": "http://example.org/?p=36714"
    46984698    },
    46994699    "title": {
    47004700        "rendered": ""