Skip to content

Commit

Permalink
Add end point
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Apr 17, 2019
1 parent 95fdc02 commit 830973b
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 1 deletion.
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']
);
}
}
2 changes: 1 addition & 1 deletion lib/class-wp-rest-widget-updater-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class WP_REST_Widget_Updater_Controller extends WP_REST_Controller {
* @access public
*/
public function __construct() {
$this->namespace = 'wp/v2';
$this->namespace = '__experimental';
$this->rest_base = 'widgets';
}

Expand Down
4 changes: 4 additions & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
if ( ! class_exists( 'WP_REST_Widget_Updater_Controller' ) ) {
require dirname( __FILE__ ) . '/class-wp-rest-widget-updater-controller.php';
}
if ( ! class_exists( 'WP_REST_Sidebars_Controller' ) ) {
require dirname( __FILE__ ) . '/class-wp-rest-sidebars-controller.php';
}
/**
* End: Include for phase 2
*/
Expand All @@ -26,6 +29,7 @@

require dirname( __FILE__ ) . '/blocks.php';
require dirname( __FILE__ ) . '/client-assets.php';
require dirname( __FILE__ ) . '/register.php';
require dirname( __FILE__ ) . '/demo.php';
require dirname( __FILE__ ) . '/widgets.php';
require dirname( __FILE__ ) . '/widgets-page.php';
93 changes: 93 additions & 0 deletions lib/register.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* Initialization and wp-admin integration for the Gutenberg editor plugin.
*
* @package gutenberg
*/

if ( ! defined( 'ABSPATH' ) ) {
die( 'Silence is golden.' );
}

function gutenberg_output_block_widget( $options, $block ) {
echo $options['before_widget'];
echo render_block( $block );
echo $options['after_widget'];
}

function gutenberg_swap_out_sidebars_blocks_for_block_widgets( $sidebars_items ) {
global $wp_registered_widgets;

foreach ( $sidebars_items as $sidebar_id => $items ) {
foreach ( $items as $index => $item ) {
if ( ! is_array( $item ) || ! isset( $item['blockName'] ) ) {
continue;
}

$widget_id = 'block-widget-' . md5( serialize( $item ) );

$sidebars_items[ $sidebar_id ][ $index ] = $widget_id;

if ( isset( $wp_registered_widgets[ $widget_id ] ) ) {
continue;
}

wp_register_sidebar_widget(
$widget_id,
// TODO: Can we get the block's title somehow?
/* translators: %s: Name of the block */
sprintf( __( 'Block: %s', 'gutenberg' ), $item['blockName'] ),
'gutenberg_output_block_widget',
array(
'classname' => 'block-widget',
'description' => sprintf(
/* translators: %s: Name of the block */
__( 'Displays a ‘%s’ block.', 'gutenberg' ),
$item['blockName']
),
),
$item
);
}
}

return $sidebars_items;
}
add_filter( 'sidebars_widgets', 'gutenberg_swap_out_sidebars_blocks_for_block_widgets' );

function gutenberg_swap_out_sidebars_block_widgets_for_blocks( $sidebars_widgets ) {
global $wp_registered_widgets;

foreach ( $sidebars_widgets as $sidebar_id => $widgets ) {
foreach ( $widgets as $index => $widget_id ) {
if ( 0 !== strpos( $widget_id, 'block-widget-' ) ) {
continue;
}

if ( ! isset( $wp_registered_widgets[ $widget_id ] ) ) {
unset( $sidebars_widgets[ $sidebar_id ][ $index ] );
continue;
}

$block = $wp_registered_widgets[ $widget_id ]['params'][0];

$sidebars_widgets[ $sidebar_id ][ $index ] = $block;
}
}

return $sidebars_widgets;
}
add_filter( 'pre_update_option_sidebars_widgets', 'gutenberg_swap_out_sidebars_block_widgets_for_blocks' );

function gutenberg_get_sidebars_items() {
remove_filter( 'sidebars_widgets', 'gutenberg_swap_out_sidebars_blocks_for_block_widgets' );
$sidebars_widgets = wp_get_sidebars_widgets();
add_filter( 'sidebars_widgets', 'gutenberg_swap_out_sidebars_blocks_for_block_widgets' );
return $sidebars_widgets;
}

function gutenberg_set_sidebars_items( $sidebars_items ) {
remove_filter( 'pre_update_option_sidebars_widgets', 'gutenberg_swap_out_sidebars_block_widgets_for_blocks' );
wp_set_sidebars_widgets( $sidebars_items );
add_filter( 'pre_update_option_sidebars_widgets', 'gutenberg_swap_out_sidebars_block_widgets_for_blocks' );
}
6 changes: 6 additions & 0 deletions lib/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ function gutenberg_register_rest_widget_updater_routes() {
$widgets_controller->register_routes();
}
add_action( 'rest_api_init', 'gutenberg_register_rest_widget_updater_routes' );

function gutenberg_register_rest_sidebars_routes() {
$sidebar_controller = new WP_REST_Sidebars_Controller();
$sidebar_controller->register_routes();
}
add_action( 'rest_api_init', 'gutenberg_register_rest_sidebars_routes' );
/**
* End: Include for phase 2
*/

0 comments on commit 830973b

Please sign in to comment.