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

Add block data to REST API #2649

Closed
wants to merge 54 commits into
base: master
from
Commits
Jump to file or symbol
Failed to load files and symbols.
+1 −98
Diff settings

Always

Just for now

Viewing a subset of changes. View all
View
@@ -255,101 +255,4 @@ function gutenberg_wpautop_insert_post_data( $data ) {
return $data;
}
add_filter( 'the_content', 'do_blocks', 9 ); // BEFORE do_shortcode() and wpautop().
/**
* Extract the blocks from post content for the REST API post response.
*
* @since 1.1.0
*
* @param string $content The post content.
*
* @return array Array of block data.
*/
function gutenberg_add_blocks_to_post_resource( $content ) {
$registry = WP_Block_Type_Registry::get_instance();
$blocks = gutenberg_parse_blocks( $content );
$data = array();
// Loop thru the blocks, adding rendered content when available.
foreach ( $blocks as $block ) {
$block_name = isset( $block['blockName'] ) ? $block['blockName'] : null;
$attributes = is_array( $block['attrs'] ) ? $block['attrs'] : null;
$raw_content = isset( $block['rawContent'] ) ? $block['rawContent'] : null;
// Skip block if we didn’t get a valid block name.
if ( null === $block_name ) {
continue;
}
// Set up rendered content, if available.
$block['renderedContent'] = null;
$block_type = $registry->get_registered( $block_name );
if ( null !== $block_type ) {
$block['renderedContent'] = $block_type->render( $attributes, $raw_content );
}
// Add the item data.
$data[] = array(
'type' => $block_name,
'attributes' => $attributes,
'content' => $block['rawContent'],
'rendered' => $block['renderedContent'],
);
}
return $data;
}
/**
* Attach a post's block data callback to the REST API response.
*
* @since 1.1.0
*
* @param string | array $post_types Post type, or array of post types.
*/
function gutenberg_attach_block_response_callback( $post_types ) {
if ( empty( $post_types ) ) {
$post_types = 'post';
}
if ( ! is_array( $post_types ) ) {
$post_types = array( $post_types );
}
foreach ( $post_types as $post_type ) {
/**
* Filter whether a post type has its blocks data added the REST API response content.
*
* @since 1.1.0
*
* @param bool $blocks_show_in_rest Whether to show blocks in the REST API response.
* @param string $post_type The post type.
*/
if ( apply_filters( 'gutenberg_add_blocks_to_rest_for_post_type', true, $post_type ) ) {
add_filter( 'rest_prepare_' . $post_type, 'gutenberg_extract_blocks_from_post_content', 10, 3 );
}
}
}
gutenberg_attach_block_response_callback( array( 'post', 'page' ) );
/**
* Attach a post's block data to the REST API response.
*
* @since 1.1.0
*
* @param WP_REST_Response $response The response object.
* @param WP_Post $post The Post object.
*
* @return WP_REST_Response $response The filtered response object.
*/
function gutenberg_extract_blocks_from_post_content( $response, $post ) {
if ( ! $post ) {
return $response;
}
// Extract the block data from the post content.
$blocks = gutenberg_add_blocks_to_post_resource( $post->post_content );
$response->data['content']['blocks'] = $blocks;
return $response;
}
add_filter( 'wp_insert_post_data', 'gutenberg_wpautop_insert_post_data' );
ProTip! Use n and p to navigate between commits in a pull request.