Skip to content

Commit

Permalink
Remap response fields to match WordPress#2503
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Silverstein committed Sep 7, 2017
1 parent 9ce9ca4 commit 148469e
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/class-wp-rest-gutenberg-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,20 +355,32 @@ public function get_block_data_from_content( $content ) {
$registry = WP_Block_Type_Registry::get_instance();
$blocks = gutenberg_parse_blocks( $content );
$data = array();
$data_pre = 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'] : array();
$raw_content = isset( $block['rawContent'] ) ? $block['rawContent'] : null;
if ( $block_name ) {
if ( null !== $block_name ) {
$block_type = $registry->get_registered( $block_name );
if ( null !== $block_type ) {
$block['renderedContent'] = $block_type->render( $attributes, $raw_content );
}
$data_pre[] = $block;
}
$data[] = $block;
}

// Remap the block fields for the response.
foreach ( $data_pre as $block ) {
$data[] = array(
'type' => $block['blockName'],
'attributes' => $block['attrs'],
'content' => $block['rawContent'],
'rendered' => isset( $block['renderedContent'] ) ? $block['renderedContent'] : null,
);
}

return $data;
}
}

0 comments on commit 148469e

Please sign in to comment.