Skip to content

Commit

Permalink
Expose Gutenberg Data Format version in the REST API (#6436)
Browse files Browse the repository at this point in the history
* Add the block format version to the REST API post resource

* Add the block_format to the post content response.

* Add todo marker for including on schema

* Fix PHP 5.2 incompatibility

* Ensure this is an array to handle `isset()` behavior in 5.2
  • Loading branch information
pento authored and danielbachhuber committed Apr 27, 2018
1 parent a7b0835 commit 3c54f04
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
15 changes: 15 additions & 0 deletions lib/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,21 @@ function gutenberg_content_has_blocks( $content ) {
return false !== strpos( $content, '<!-- wp:' );
}

/**
* Returns the current version of the block format that the content string is using.
*
* If the string doesn't contain blocks, it returns 0.
*
* @since 2.8.0
* @see gutenberg_content_has_blocks()
*
* @param string $content Content to test.
* @return int The block format version.
*/
function gutenberg_content_block_version( $content ) {
return gutenberg_content_has_blocks( $content ) ? 1 : 0;
}

/**
* Adds a "Gutenberg" post state for post tables, if the post contains blocks.
*
Expand Down
29 changes: 27 additions & 2 deletions lib/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,17 +232,42 @@ function gutenberg_add_permalink_template_to_posts( $response, $post, $request )
return $response;
}

/**
* Add the block format version to post content in the post REST API response.
*
* @todo This will need to be registered to the schema too.
*
* @param WP_REST_Response $response WP REST API response of a post.
* @param WP_Post $post The post being returned.
* @param WP_REST_Request $request WP REST API request.
* @return WP_REST_Response Response containing the block_format.
*/
function gutenberg_add_block_format_to_post_content( $response, $post, $request ) {
if ( 'edit' !== $request['context'] ) {
return $response;
}

$response_data = $response->get_data();
if ( is_array( $response_data['content'] ) && isset( $response_data['content']['raw'] ) ) {
$response_data['content']['block_format'] = gutenberg_content_block_version( $response_data['content']['raw'] );
$response->set_data( $response_data );
}

return $response;
}

/**
* Whenever a post type is registered, ensure we're hooked into it's WP REST API response.
*
* @param string $post_type The newly registered post type.
* @return string That same post type.
*/
function gutenberg_register_permalink_template_function( $post_type ) {
function gutenberg_register_post_prepare_functions( $post_type ) {
add_filter( "rest_prepare_{$post_type}", 'gutenberg_add_permalink_template_to_posts', 10, 3 );
add_filter( "rest_prepare_{$post_type}", 'gutenberg_add_block_format_to_post_content', 10, 3 );
return $post_type;
}
add_filter( 'registered_post_type', 'gutenberg_register_permalink_template_function' );
add_filter( 'registered_post_type', 'gutenberg_register_post_prepare_functions' );

/**
* Includes the value for the 'viewable' attribute of a post type resource.
Expand Down

0 comments on commit 3c54f04

Please sign in to comment.