Skip to content

Commit

Permalink
Use HTML to represent blocks in the Sidebar API
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks authored and jorgefilipecosta committed Apr 17, 2019
1 parent 5b98643 commit 5d82530
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 50 deletions.
32 changes: 32 additions & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,35 @@ function gutenberg_reregister_core_block_types() {
}
}
add_action( 'init', 'gutenberg_reregister_core_block_types' );

function serialize_blocks( $blocks ) {
return implode( array_map( 'serialize_block', $blocks ) );
}

function serialize_block( $block ) {
$name = $block['blockName'];
if ( 0 === strpos( $name, 'core/' ) ) {
$name = substr( $name, strlen( 'core/' ) );
}

if ( empty( $block['attrs'] ) ) {
$opening_tag_suffix = '';
} else {
$opening_tag_suffix = ' ' . json_encode( $block['attrs'] );
}

if ( empty( $block['innerHTML'] ) ) {
return sprintf(
'<!-- wp:%s%s /-->',
$name,
$opening_tag_suffix
);
} else {
return sprintf(
'<!-- wp:%1$s%2$s -->%3$s<!-- /wp:%1$s -->',
$name,
$opening_tag_suffix,
$block['innerHTML']
);
}
}
79 changes: 29 additions & 50 deletions lib/class-wp-rest-sidebars-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ public function get_item_permissions_check( $request ) {
}

public function get_item( $request ) {
$data = $this->get_sidebar_data( $request['id'] );
return rest_ensure_response( $data );
return rest_ensure_response( $this->get_sidebar_data( $request['id'] ) );
}

public function update_item_permissions_check( $request ) {
Expand All @@ -96,13 +95,12 @@ public function update_item_permissions_check( $request ) {
}

public function update_item( $request ) {
$result = $this->update_sidebar_blocks( $request['id'], $request );
if ( is_wp_error( $result ) ) {
return $result;
$status = $this->update_sidebar_data( $request['id'], $request );
if ( is_wp_error( $status ) ) {
return $status;
}

$data = $this->get_sidebar_data( $request['id'] );
return rest_ensure_response( $data );
return rest_ensure_response( $this->get_sidebar_data( $request['id'] ) );
}

// TODO: Add schema
Expand All @@ -119,44 +117,33 @@ protected function get_sidebar_data( $sidebar_id ) {
}

$sidebar = $wp_registered_sidebars[ $sidebar_id ];

// TODO: How should we format blocks in the REST API? Or should we send down
// HTML so that we're consistent with the /posts and /blocks endpoints?
$blocks = array();
$blocks = array();

$sidebars_items = gutenberg_get_sidebars_items();
if ( ! empty( $sidebars_items[ $sidebar_id ] ) ) {
foreach ( $sidebars_items[ $sidebar_id ] as $item ) {
if ( is_array( $item ) && isset( $item['blockName'] ) ) {
$blocks[] = array(
'name' => $item['blockName'],
'attributes' => $item['attrs'],
'innerBlocks' => $item['innerBlocks'],
'innerHTML' => $item['innerHTML'],
'innerContent' => $item['innerContent'],
);
$blocks[] = $item;
} else {
$blocks[] = array(
'name' => 'core/legacy-widget',
'attributes' => array(
'blockName' => 'core/legacy-widget',
'attrs' => array(
'identifier' => $item,
'instance' => $this->get_sidebars_widget_instance( $sidebar, $item ),
),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
'innerHTML' => '',
);
}
}
}

return array_merge(
$sidebar,
array( 'blocks' => $blocks )
array( 'content' => serialize_blocks( $blocks ) )
);
}

protected function update_sidebar_blocks( $sidebar_id, $request ) {
protected function update_sidebar_data( $sidebar_id, $request ) {
global $wp_registered_sidebars;

if ( ! isset( $wp_registered_sidebars[ $sidebar_id ] ) ) {
Expand All @@ -167,39 +154,29 @@ protected function update_sidebar_blocks( $sidebar_id, $request ) {
);
}

if ( isset( $request['blocks'] ) && is_array( $request['blocks'] ) ) {
if ( isset( $request['content'] ) && is_string( $request['content'] ) ) {
$items = array();

foreach ( $request['blocks'] as $block ) {
if (
! isset( $block['name'] ) || ! is_string( $block['name'] ) ||
! isset( $block['attributes'] ) || ! is_array( $block['attributes'] ) ||
! isset( $block['innerBlocks'] ) || ! is_array( $block['innerBlocks'] ) ||
! isset( $block['innerHTML'] ) || ! is_string( $block['innerHTML'] ) ||
! isset( $block['innerContent'] ) || ! is_array( $block['innerContent'] )
) {
$blocks = parse_blocks( $request['content'] );
foreach ( $blocks as $block ) {
if ( ! isset( $block['blockName'] ) ) {
continue;
}

if (
'core/legacy-widget' === $block['name'] &&
isset( $block['attributes']['identifier'] ) &&
isset( $block['attributes']['instance'] )
'core/legacy-widget' === $block['blockName'] &&
isset( $block['attrs']['identifier'] )
) {
$items[] = $block['attributes']['identifier'];

$this->update_widget_instance(
$block['attributes']['identifier'],
$block['attributes']['instance']
);
$items[] = $block['attrs']['identifier'];

if ( isset( $block['attrs']['instance'] ) ) {
$this->update_widget_instance(
$block['attrs']['identifier'],
$block['attrs']['instance']
);
}
} else {
$items[] = array(
'blockName' => $block['name'],
'attrs' => $block['attributes'],
'innerBlocks' => $block['innerBlocks'],
'innerHTML' => $block['innerHTML'],
'innerContent' => $block['innerContent'],
);
$items[] = $block;
}
}

Expand All @@ -208,6 +185,8 @@ protected function update_sidebar_blocks( $sidebar_id, $request ) {
array( $sidebar_id => $items )
) );
}

return true;
}

private function get_sidebars_widget_instance( $sidebar, $id ) {
Expand Down

0 comments on commit 5d82530

Please sign in to comment.