Skip to content

Commit

Permalink
Fix errors caused by backporting Gutenberg features to WordPress Core (
Browse files Browse the repository at this point in the history
…#32176)

Including the Widgets editor and template mode.

Co-authored-by: Riad Benguella <[email protected]>
Co-authored-by: Kai Hao <[email protected]>
Co-authored-by: André <[email protected]>
Co-authored-by: ntsekouras <[email protected]>
Co-authored-by: Bernie Reiter <[email protected]>
Co-authored-by: Ella van Durpe <[email protected]>
  • Loading branch information
7 people committed May 26, 2021
1 parent 2a4c246 commit f5066f7
Show file tree
Hide file tree
Showing 24 changed files with 375 additions and 295 deletions.
4 changes: 2 additions & 2 deletions lib/class-wp-rest-block-editor-settings-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public function get_items_permissions_check( $request ) {// phpcs:ignore Variabl
* @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
*/
public function get_items( $request ) {
$editor_context = new WP_Block_Editor_Context();
$settings = gutenberg_get_block_editor_settings( array(), $editor_context );
$editor_context = new WP_Block_Editor_Context();
$settings = gutenberg_get_block_editor_settings( array(), $editor_context );

return rest_ensure_response( $settings );
}
Expand Down
2 changes: 1 addition & 1 deletion lib/class-wp-rest-widget-types-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ protected function get_widgets() {
$widgets = array();

foreach ( $wp_registered_widgets as $widget ) {
$parsed_id = gutenberg_parse_widget_id( $widget['id'] );
$parsed_id = wp_parse_widget_id( $widget['id'] );
$widget_object = gutenberg_get_widget_object( $parsed_id['id_base'] );

$widget['id'] = $parsed_id['id_base'];
Expand Down
24 changes: 12 additions & 12 deletions lib/class-wp-rest-widgets-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function get_item_permissions_check( $request ) { // phpcs:ignore Variabl
*/
public function get_item( $request ) {
$widget_id = $request['id'];
$sidebar_id = gutenberg_find_widgets_sidebar( $widget_id );
$sidebar_id = wp_find_widgets_sidebar( $widget_id );

if ( is_null( $sidebar_id ) ) {
return new WP_Error(
Expand Down Expand Up @@ -192,7 +192,7 @@ public function create_item( $request ) {
return $widget_id;
}

gutenberg_assign_widget_to_sidebar( $widget_id, $sidebar_id );
wp_assign_widget_to_sidebar( $widget_id, $sidebar_id );

$request['context'] = 'edit';

Expand Down Expand Up @@ -229,10 +229,10 @@ public function update_item_permissions_check( $request ) { // phpcs:ignore Vari
*/
public function update_item( $request ) {
$widget_id = $request['id'];
$sidebar_id = gutenberg_find_widgets_sidebar( $widget_id );
$sidebar_id = wp_find_widgets_sidebar( $widget_id );

// Allow sidebar to be unset or missing when widget is not a WP_Widget.
$parsed_id = gutenberg_parse_widget_id( $widget_id );
$parsed_id = wp_parse_widget_id( $widget_id );
$widget_object = gutenberg_get_widget_object( $parsed_id['id_base'] );
if ( is_null( $sidebar_id ) && $widget_object ) {
return new WP_Error(
Expand All @@ -255,7 +255,7 @@ public function update_item( $request ) {
if ( $request->has_param( 'sidebar' ) ) {
if ( $sidebar_id !== $request['sidebar'] ) {
$sidebar_id = $request['sidebar'];
gutenberg_assign_widget_to_sidebar( $widget_id, $sidebar_id );
wp_assign_widget_to_sidebar( $widget_id, $sidebar_id );
}
}

Expand Down Expand Up @@ -286,7 +286,7 @@ public function delete_item_permissions_check( $request ) { // phpcs:ignore Vari
*/
public function delete_item( $request ) {
$widget_id = $request['id'];
$sidebar_id = gutenberg_find_widgets_sidebar( $widget_id );
$sidebar_id = wp_find_widgets_sidebar( $widget_id );

if ( is_null( $sidebar_id ) ) {
return new WP_Error(
Expand All @@ -300,15 +300,15 @@ public function delete_item( $request ) {

if ( $request['force'] ) {
$prepared = $this->prepare_item_for_response( compact( 'widget_id', 'sidebar_id' ), $request );
gutenberg_assign_widget_to_sidebar( $widget_id, '' );
wp_assign_widget_to_sidebar( $widget_id, '' );
$prepared->set_data(
array(
'deleted' => true,
'previous' => $prepared->get_data(),
)
);
} else {
gutenberg_assign_widget_to_sidebar( $widget_id, 'wp_inactive_widgets' );
wp_assign_widget_to_sidebar( $widget_id, 'wp_inactive_widgets' );
$prepared = $this->prepare_item_for_response(
array(
'sidebar_id' => 'wp_inactive_widgets',
Expand Down Expand Up @@ -359,7 +359,7 @@ protected function save_widget( $request ) {
if ( isset( $request['id'] ) ) {
// Saving an existing widget.
$id = $request['id'];
$parsed_id = gutenberg_parse_widget_id( $id );
$parsed_id = wp_parse_widget_id( $id );
$id_base = $parsed_id['id_base'];
$number = isset( $parsed_id['number'] ) ? $parsed_id['number'] : null;
$widget_object = gutenberg_get_widget_object( $id_base );
Expand Down Expand Up @@ -495,7 +495,7 @@ public function prepare_item_for_response( $item, $request ) {
}

$widget = $wp_registered_widgets[ $widget_id ];
$parsed_id = gutenberg_parse_widget_id( $widget_id );
$parsed_id = wp_parse_widget_id( $widget_id );
$fields = $this->get_fields_for_response( $request );

$prepared = array(
Expand All @@ -511,11 +511,11 @@ public function prepare_item_for_response( $item, $request ) {
rest_is_field_included( 'rendered', $fields ) &&
'wp_inactive_widgets' !== $sidebar_id
) {
$prepared['rendered'] = trim( gutenberg_render_widget( $widget_id, $sidebar_id ) );
$prepared['rendered'] = trim( wp_render_widget( $widget_id, $sidebar_id ) );
}

if ( rest_is_field_included( 'rendered_form', $fields ) ) {
$rendered_form = gutenberg_render_widget_control( $widget_id );
$rendered_form = wp_render_widget_control( $widget_id );
if ( ! is_null( $rendered_form ) ) {
$prepared['rendered_form'] = trim( $rendered_form );
}
Expand Down
8 changes: 4 additions & 4 deletions lib/full-site-editing/block-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ function _gutenberg_add_template_part_area_info( $template_info ) {
*
* @return array block references to the passed blocks and their inner blocks.
*/
function _flatten_blocks( &$blocks ) {
function _gutenberg_flatten_blocks( &$blocks ) {
$all_blocks = array();
$queue = array();
foreach ( $blocks as &$block ) {
Expand Down Expand Up @@ -175,12 +175,12 @@ function _flatten_blocks( &$blocks ) {
*
* @return string Updated wp_template content.
*/
function _inject_theme_attribute_in_content( $template_content ) {
function _gutenberg_inject_theme_attribute_in_content( $template_content ) {
$has_updated_content = false;
$new_content = '';
$template_blocks = parse_blocks( $template_content );

$blocks = _flatten_blocks( $template_blocks );
$blocks = _gutenberg_flatten_blocks( $template_blocks );
foreach ( $blocks as &$block ) {
if (
'core/template-part' === $block['blockName'] &&
Expand Down Expand Up @@ -218,7 +218,7 @@ function _gutenberg_build_template_result_from_file( $template_file, $template_t
$template = new WP_Block_Template();
$template->id = $theme . '//' . $template_file['slug'];
$template->theme = $theme;
$template->content = _inject_theme_attribute_in_content( $template_content );
$template->content = _gutenberg_inject_theme_attribute_in_content( $template_content );
$template->slug = $template_file['slug'];
$template->source = 'theme';
$template->type = $template_type;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* REST API: WP_REST_Templates_Controller class
* REST API: Gutenberg_REST_Templates_Controller class
*
* @package Gutenberg
* @subpackage REST_API
Expand All @@ -9,7 +9,7 @@
/**
* Base Templates REST API Controller.
*/
class WP_REST_Templates_Controller extends WP_REST_Controller {
class Gutenberg_REST_Templates_Controller extends WP_REST_Controller {
/**
* Post type.
*
Expand Down Expand Up @@ -335,22 +335,23 @@ public function delete_item( $request ) {
* @return stdClass Changes to pass to wp_update_post.
*/
protected function prepare_item_for_database( $request ) {
$template = $request['id'] ? gutenberg_get_block_template( $request['id'], $this->post_type ) : null;
$changes = new stdClass();
$changes->post_name = $template->slug;
$template = $request['id'] ? gutenberg_get_block_template( $request['id'], $this->post_type ) : null;
$changes = new stdClass();
if ( null === $template ) {
$changes->post_type = $this->post_type;
$changes->post_status = 'publish';
$changes->tax_input = array(
'wp_theme' => isset( $request['theme'] ) ? $request['content'] : wp_get_theme()->get_stylesheet(),
'wp_theme' => isset( $request['theme'] ) ? $request['theme'] : wp_get_theme()->get_stylesheet(),
);
} elseif ( 'custom' !== $template->source ) {
$changes->post_name = $template->slug;
$changes->post_type = $this->post_type;
$changes->post_status = 'publish';
$changes->tax_input = array(
'wp_theme' => $template->theme,
);
} else {
$changes->post_name = $template->slug;
$changes->ID = $template->wp_id;
$changes->post_status = 'publish';
}
Expand Down
2 changes: 1 addition & 1 deletion lib/full-site-editing/edit-site-export.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function _remove_theme_attribute_from_content( $template_content ) {
$new_content = '';
$template_blocks = parse_blocks( $template_content );

$blocks = _flatten_blocks( $template_blocks );
$blocks = _gutenberg_flatten_blocks( $template_blocks );
foreach ( $blocks as $key => $block ) {
if ( 'core/template-part' === $block['blockName'] && isset( $block['attrs']['theme'] ) ) {
unset( $blocks[ $key ]['attrs']['theme'] );
Expand Down
2 changes: 1 addition & 1 deletion lib/full-site-editing/template-parts.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function gutenberg_register_template_part_post_type() {
'show_in_admin_bar' => false,
'show_in_rest' => true,
'rest_base' => 'template-parts',
'rest_controller_class' => 'WP_REST_Templates_Controller',
'rest_controller_class' => 'Gutenberg_REST_Templates_Controller',
'map_meta_cap' => true,
'supports' => array(
'title',
Expand Down
2 changes: 1 addition & 1 deletion lib/full-site-editing/templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function gutenberg_register_template_post_type() {
'show_in_admin_bar' => false,
'show_in_rest' => true,
'rest_base' => 'templates',
'rest_controller_class' => 'WP_REST_Templates_Controller',
'rest_controller_class' => 'Gutenberg_REST_Templates_Controller',
'capability_type' => array( 'template', 'templates' ),
'map_meta_cap' => true,
'supports' => array(
Expand Down
11 changes: 6 additions & 5 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ function gutenberg_is_experiment_enabled( $name ) {
if ( ! class_exists( 'WP_Rest_Customizer_Nonces' ) ) {
require_once __DIR__ . '/class-wp-rest-customizer-nonces.php';
}
if ( ! class_exists( 'WP_REST_Templates_Controller' ) ) {
require_once __DIR__ . '/full-site-editing/class-wp-rest-templates-controller.php';
}
require_once __DIR__ . '/full-site-editing/class-gutenberg-rest-templates-controller.php';
if ( ! class_exists( 'WP_REST_Block_Editor_Settings_Controller' ) ) {
require_once dirname( __FILE__ ) . '/class-wp-rest-block-editor-settings-controller.php';
}
Expand All @@ -73,7 +71,10 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/rest-api.php';
}

if ( ! class_exists( 'WP_Widget_Block' ) ) {
// We can't use class_exists( 'WP_Widget_Block' ) because core loads widgets
// *after* plugins, so test for wp_use_widgets_block_editor() which we know
// implies the existence of WP_Widget_Block.
if ( ! function_exists( 'wp_use_widgets_block_editor' ) ) {
require_once __DIR__ . '/class-wp-widget-block.php';
}

Expand All @@ -84,7 +85,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/utils.php';
require __DIR__ . '/editor-settings.php';

if ( ! class_exists( 'WP_Block_Template ' ) ) {
if ( ! class_exists( 'WP_Block_Template' ) ) {
require __DIR__ . '/full-site-editing/class-wp-block-template.php';
}

Expand Down
Loading

0 comments on commit f5066f7

Please sign in to comment.