Skip to content

Commit 63d9540

Browse files
committed
REST API: Include block_version on Post content object.
The `block_version` denotes which version of Blocks the `post_content` contains. Introduces new `block_version()` function for versioning Blocks. Merges [43770] from the 5.0 branch to trunk. Props danielbachhuber, birgire. Fixes #43887. git-svn-id: https://develop.svn.wordpress.org/trunk@44127 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 225f191 commit 63d9540

5 files changed

Lines changed: 157 additions & 34 deletions

File tree

src/wp-includes/blocks.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,17 @@ function _recurse_do_blocks( $blocks, $all_blocks ) {
262262

263263
return $rendered_content;
264264
}
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+
*/
276+
function block_version( $content ) {
277+
return has_blocks( $content ) ? 1 : 0;
278+
}

src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,10 +1521,11 @@ public function prepare_item_for_response( $post, $request ) {
15211521

15221522
if ( in_array( 'content', $fields, true ) ) {
15231523
$data['content'] = array(
1524-
'raw' => $post->post_content,
1524+
'raw' => $post->post_content,
15251525
/** This filter is documented in wp-includes/post-template.php */
1526-
'rendered' => post_password_required( $post ) ? '' : apply_filters( 'the_content', $post->post_content ),
1527-
'protected' => (bool) $post->post_password,
1526+
'rendered' => post_password_required( $post ) ? '' : apply_filters( 'the_content', $post->post_content ),
1527+
'protected' => (bool) $post->post_password,
1528+
'block_version' => block_version( $post->post_content ),
15281529
);
15291530
}
15301531

@@ -2062,18 +2063,24 @@ public function get_item_schema() {
20622063
'validate_callback' => null, // Note: validation implemented in self::prepare_item_for_database()
20632064
),
20642065
'properties' => array(
2065-
'raw' => array(
2066+
'raw' => array(
20662067
'description' => __( 'Content for the object, as it exists in the database.' ),
20672068
'type' => 'string',
20682069
'context' => array( 'edit' ),
20692070
),
2070-
'rendered' => array(
2071+
'rendered' => array(
20712072
'description' => __( 'HTML content for the object, transformed for display.' ),
20722073
'type' => 'string',
20732074
'context' => array( 'view', 'edit' ),
20742075
'readonly' => true,
20752076
),
2076-
'protected' => array(
2077+
'block_version' => array(
2078+
'description' => __( 'Version of the content block format used by the object.' ),
2079+
'type' => 'integer',
2080+
'context' => array( 'edit' ),
2081+
'readonly' => true,
2082+
),
2083+
'protected' => array(
20772084
'description' => __( 'Whether the content is protected with a password.' ),
20782085
'type' => 'boolean',
20792086
'context' => array( 'view', 'edit', 'embed' ),

tests/phpunit/tests/blocks/block-type.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,4 +310,47 @@ public function render_fake_block_with_content( $attributes, $content ) {
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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,6 +1554,65 @@ public function test_get_post_with_password_without_permission() {
15541554
$this->assertTrue( $data['excerpt']['protected'] );
15551555
}
15561556

1557+
/**
1558+
* The post response should not have `block_version` when in view context.
1559+
*
1560+
* @ticket 43887
1561+
*/
1562+
public function test_get_post_should_not_have_block_version_when_context_view() {
1563+
$post_id = $this->factory->post->create(
1564+
array(
1565+
'post_content' => '<!-- wp:core/separator -->',
1566+
)
1567+
);
1568+
1569+
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
1570+
$response = rest_get_server()->dispatch( $request );
1571+
$data = $response->get_data();
1572+
$this->assertFalse( isset( $data['content']['block_version'] ) );
1573+
}
1574+
1575+
/**
1576+
* The post response should have `block_version` indicate that block content is present when in edit context.
1577+
*
1578+
* @ticket 43887
1579+
*/
1580+
public function test_get_post_should_have_block_version_indicate_block_content_when_context_edit() {
1581+
wp_set_current_user( self::$editor_id );
1582+
1583+
$post_id = $this->factory->post->create(
1584+
array(
1585+
'post_content' => '<!-- wp:core/separator -->',
1586+
)
1587+
);
1588+
1589+
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
1590+
$request->set_param( 'context', 'edit' );
1591+
$response = rest_get_server()->dispatch( $request );
1592+
$data = $response->get_data();
1593+
$this->assertSame( 1, $data['content']['block_version'] );
1594+
}
1595+
1596+
/**
1597+
* The post response should have `block_version` indicate that no block content is present when in edit context.
1598+
*
1599+
* @ticket 43887
1600+
*/
1601+
public function test_get_post_should_have_block_version_indicate_no_block_content_when_context_edit() {
1602+
wp_set_current_user( self::$editor_id );
1603+
1604+
$post_id = $this->factory->post->create(
1605+
array(
1606+
'post_content' => '<hr />',
1607+
)
1608+
);
1609+
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', $post_id ) );
1610+
$request->set_param( 'context', 'edit' );
1611+
$response = rest_get_server()->dispatch( $request );
1612+
$data = $response->get_data();
1613+
$this->assertSame( 0, $data['content']['block_version'] );
1614+
}
1615+
15571616
public function test_get_item_read_permission_custom_post_status_not_authenticated() {
15581617
register_post_status( 'testpubstatus', array( 'public' => true ) );
15591618
register_post_status( 'testprivtatus', array( 'public' => false ) );

tests/qunit/fixtures/wp-api-generated.js

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4350,13 +4350,13 @@ mockedApiResponse.postRevisions = [
43504350
"author": 359,
43514351
"date": "2017-02-14T00:00:00",
43524352
"date_gmt": "2017-02-14T00:00:00",
4353-
"id": 36734,
4353+
"id": 3153,
43544354
"modified": "2017-02-14T00:00:00",
43554355
"modified_gmt": "2017-02-14T00:00:00",
4356-
"parent": 36733,
4357-
"slug": "36733-revision-v1",
4356+
"parent": 3152,
4357+
"slug": "3152-revision-v1",
43584358
"guid": {
4359-
"rendered": "http://example.org/?p=36734"
4359+
"rendered": "http://example.org/?p=3153"
43604360
},
43614361
"title": {
43624362
"rendered": "REST API Client Fixture: Post"
@@ -4370,7 +4370,7 @@ mockedApiResponse.postRevisions = [
43704370
"_links": {
43714371
"parent": [
43724372
{
4373-
"href": "http://example.org/index.php?rest_route=/wp/v2/posts/36733"
4373+
"href": "http://example.org/index.php?rest_route=/wp/v2/posts/3152"
43744374
}
43754375
]
43764376
}
@@ -4405,13 +4405,13 @@ mockedApiResponse.postAutosaves = [
44054405
"author": 359,
44064406
"date": "2017-02-14T00:00:00",
44074407
"date_gmt": "2017-02-14T00:00:00",
4408-
"id": 36735,
4408+
"id": 3154,
44094409
"modified": "2017-02-14T00:00:00",
44104410
"modified_gmt": "2017-02-14T00:00:00",
4411-
"parent": 36733,
4412-
"slug": "36733-autosave-v1",
4411+
"parent": 3152,
4412+
"slug": "3152-autosave-v1",
44134413
"guid": {
4414-
"rendered": "http://example.org/?p=36735"
4414+
"rendered": "http://example.org/?p=3154"
44154415
},
44164416
"title": {
44174417
"rendered": ""
@@ -4425,7 +4425,7 @@ mockedApiResponse.postAutosaves = [
44254425
"_links": {
44264426
"parent": [
44274427
{
4428-
"href": "http://example.org/index.php?rest_route=/wp/v2/posts/36733"
4428+
"href": "http://example.org/index.php?rest_route=/wp/v2/posts/3152"
44294429
}
44304430
]
44314431
}
@@ -4436,13 +4436,13 @@ mockedApiResponse.autosave = {
44364436
"author": 359,
44374437
"date": "2017-02-14T00:00:00",
44384438
"date_gmt": "2017-02-14T00:00:00",
4439-
"id": 36735,
4439+
"id": 3154,
44404440
"modified": "2017-02-14T00:00:00",
44414441
"modified_gmt": "2017-02-14T00:00:00",
4442-
"parent": 36733,
4443-
"slug": "36733-autosave-v1",
4442+
"parent": 3152,
4443+
"slug": "3152-autosave-v1",
44444444
"guid": {
4445-
"rendered": "http://example.org/?p=36735"
4445+
"rendered": "http://example.org/?p=3154"
44464446
},
44474447
"title": {
44484448
"rendered": ""
@@ -4610,13 +4610,13 @@ mockedApiResponse.pageRevisions = [
46104610
"author": 359,
46114611
"date": "2017-02-14T00:00:00",
46124612
"date_gmt": "2017-02-14T00:00:00",
4613-
"id": 36737,
4613+
"id": 3156,
46144614
"modified": "2017-02-14T00:00:00",
46154615
"modified_gmt": "2017-02-14T00:00:00",
4616-
"parent": 36736,
4617-
"slug": "36736-revision-v1",
4616+
"parent": 3155,
4617+
"slug": "3155-revision-v1",
46184618
"guid": {
4619-
"rendered": "http://example.org/?p=36737"
4619+
"rendered": "http://example.org/?p=3156"
46204620
},
46214621
"title": {
46224622
"rendered": "REST API Client Fixture: Page"
@@ -4630,7 +4630,7 @@ mockedApiResponse.pageRevisions = [
46304630
"_links": {
46314631
"parent": [
46324632
{
4633-
"href": "http://example.org/index.php?rest_route=/wp/v2/pages/36736"
4633+
"href": "http://example.org/index.php?rest_route=/wp/v2/pages/3155"
46344634
}
46354635
]
46364636
}
@@ -4665,13 +4665,13 @@ mockedApiResponse.pageAutosaves = [
46654665
"author": 359,
46664666
"date": "2017-02-14T00:00:00",
46674667
"date_gmt": "2017-02-14T00:00:00",
4668-
"id": 36738,
4668+
"id": 3157,
46694669
"modified": "2017-02-14T00:00:00",
46704670
"modified_gmt": "2017-02-14T00:00:00",
4671-
"parent": 36736,
4672-
"slug": "36736-autosave-v1",
4671+
"parent": 3155,
4672+
"slug": "3155-autosave-v1",
46734673
"guid": {
4674-
"rendered": "http://example.org/?p=36738"
4674+
"rendered": "http://example.org/?p=3157"
46754675
},
46764676
"title": {
46774677
"rendered": ""
@@ -4685,7 +4685,7 @@ mockedApiResponse.pageAutosaves = [
46854685
"_links": {
46864686
"parent": [
46874687
{
4688-
"href": "http://example.org/index.php?rest_route=/wp/v2/pages/36736"
4688+
"href": "http://example.org/index.php?rest_route=/wp/v2/pages/3155"
46894689
}
46904690
]
46914691
}
@@ -4696,13 +4696,13 @@ mockedApiResponse.pageAutosave = {
46964696
"author": 359,
46974697
"date": "2017-02-14T00:00:00",
46984698
"date_gmt": "2017-02-14T00:00:00",
4699-
"id": 36738,
4699+
"id": 3157,
47004700
"modified": "2017-02-14T00:00:00",
47014701
"modified_gmt": "2017-02-14T00:00:00",
4702-
"parent": 36736,
4703-
"slug": "36736-autosave-v1",
4702+
"parent": 3155,
4703+
"slug": "3155-autosave-v1",
47044704
"guid": {
4705-
"rendered": "http://example.org/?p=36738"
4705+
"rendered": "http://example.org/?p=3157"
47064706
},
47074707
"title": {
47084708
"rendered": ""

0 commit comments

Comments
 (0)