Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Gutenberg Data Format version in the REST API #6436

Merged
merged 5 commits into from
Apr 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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