Skip to content

Commit

Permalink
Widgets: Remove experimental flag, enable for themes that have not op…
Browse files Browse the repository at this point in the history
…ted out (#24843)
  • Loading branch information
noisysocks authored Aug 31, 2020
1 parent 6b87d10 commit dc4a0b3
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 103 deletions.
71 changes: 18 additions & 53 deletions gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,23 @@ function gutenberg_menu() {
'gutenberg'
);

if ( gutenberg_use_widgets_block_editor() ) {
add_theme_page(
__( 'Widgets', 'gutenberg' ),
__( 'Widgets', 'gutenberg' ),
'edit_theme_options',
'gutenberg-widgets',
'the_gutenberg_widgets'
);
$submenu['themes.php'] = array_filter(
$submenu['themes.php'],
function( $current_menu_item ) {
return isset( $current_menu_item[2] ) && 'widgets.php' !== $current_menu_item[2];
}
);
}

if ( get_option( 'gutenberg-experiments' ) ) {
if ( array_key_exists( 'gutenberg-widget-experiments', get_option( 'gutenberg-experiments' ) ) ) {
add_submenu_page(
'gutenberg',
__( 'Widgets (beta)', 'gutenberg' ),
__( 'Widgets (beta)', 'gutenberg' ),
'edit_theme_options',
'gutenberg-widgets',
'the_gutenberg_widgets'
);
}
if ( array_key_exists( 'gutenberg-navigation', get_option( 'gutenberg-experiments' ) ) ) {
add_submenu_page(
'gutenberg',
Expand Down Expand Up @@ -101,7 +107,7 @@ function gutenberg_menu() {
'the_gutenberg_experiments'
);
}
add_action( 'admin_menu', 'gutenberg_menu' );
add_action( 'admin_menu', 'gutenberg_menu', 9 );

/**
* Display a version notice and deactivate the Gutenberg plugin.
Expand Down Expand Up @@ -182,45 +188,4 @@ function register_site_icon_url( $response ) {

add_filter( 'rest_index', 'register_site_icon_url' );

/**
* Registers the WP_Widget_Block widget
*/
function gutenberg_register_widgets() {
if ( ! gutenberg_is_experiment_enabled( 'gutenberg-widget-experiments' ) ) {
return;
}

register_widget( 'WP_Widget_Block' );
// By default every widget on widgets.php is wrapped with a <form>.
// This means that you can sometimes end up with invalid HTML, e.g. when
// one of the widgets is a Search block.
//
// To fix the problem, let's add a filter that moves the form below the actual
// widget content.
global $pagenow;
if ( 'widgets.php' === $pagenow ) {
add_filter(
'dynamic_sidebar_params',
'gutenberg_override_sidebar_params_for_block_widget'
);
}
}

/**
* Overrides dynamic_sidebar_params to make sure Blocks are not wrapped in <form> tag.
*
* @param array $arg Dynamic sidebar params.
* @return array Updated dynamic sidebar params.
*/
function gutenberg_override_sidebar_params_for_block_widget( $arg ) {
if ( 'Block' === $arg[0]['widget_name'] ) {
$arg[0]['before_form'] = '';
$arg[0]['before_widget_content'] = '<div class="widget-content">';
$arg[0]['after_widget_content'] = '</div><form class="block-widget-form">';
$arg[0]['after_form'] = '</form>';
}

return $arg;
}

add_action( 'widgets_init', 'gutenberg_register_widgets' );
add_theme_support( 'widgets-block-editor' );
24 changes: 22 additions & 2 deletions lib/customizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ function gutenberg_customize_register( $wp_customize ) {
'sanitize_callback' => 'gutenberg_customize_sanitize',
)
);
if ( gutenberg_is_experiment_enabled( 'gutenberg-widget-experiments' ) ) {

if ( gutenberg_use_widgets_block_editor() ) {
$wp_customize->add_section(
'gutenberg_widget_blocks',
array( 'title' => __( 'Widget Blocks (Experimental)', 'gutenberg' ) )
array( 'title' => __( 'Widget Blocks', 'gutenberg' ) )
);
$wp_customize->add_control(
new WP_Customize_Widget_Blocks_Control(
Expand All @@ -74,6 +75,25 @@ function gutenberg_customize_register( $wp_customize ) {
}
add_action( 'customize_register', 'gutenberg_customize_register' );

/**
* Removes the core 'Widgets' panel from the Customizer if block based widgets are enabled.
*
* @param array $components Core Customizer components list.
* @return array (Maybe) modified components list.
*/
function gutenberg_remove_widgets_panel( $components ) {
if ( ! gutenberg_use_widgets_block_editor() ) {
return $components;
}

$i = array_search( 'widgets', $components, true );
if ( false !== $i ) {
unset( $components[ $i ] );
}
return $components;
}
add_filter( 'customize_loaded_components', 'gutenberg_remove_widgets_panel' );

/**
* Filters the Customizer widget settings arguments.
* This is needed because the Customizer registers settings for the raw registered widgets, without going through the `sidebars_widgets` filter.
Expand Down
12 changes: 0 additions & 12 deletions lib/experiments-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,6 @@ function gutenberg_initialize_experiments_settings() {
'gutenberg_display_experiment_section',
'gutenberg-experiments'
);
add_settings_field(
'gutenberg-widget-experiments',
__( 'Widgets', 'gutenberg' ),
'gutenberg_display_experiment_field',
'gutenberg-experiments',
'gutenberg_experiments_section',
array(
'label' => __( 'Enable Widgets screen and Legacy Widgets block', 'gutenberg' ),
'id' => 'gutenberg-widget-experiments',
)
);
add_settings_field(
'gutenberg-navigation',
__( 'Navigation', 'gutenberg' ),
Expand Down Expand Up @@ -131,7 +120,6 @@ function gutenberg_display_experiment_section() {
*/
function gutenberg_experiments_editor_settings( $settings ) {
$experiments_settings = array(
'__experimentalEnableLegacyWidgetBlock' => gutenberg_is_experiment_enabled( 'gutenberg-widget-experiments' ),
'__experimentalEnableFullSiteEditing' => gutenberg_is_experiment_enabled( 'gutenberg-full-site-editing' ),
'__experimentalEnableFullSiteEditingDemo' => gutenberg_is_experiment_enabled( 'gutenberg-full-site-editing-demo' ),
);
Expand Down
20 changes: 8 additions & 12 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@ function gutenberg_is_experiment_enabled( $name ) {
/**
* Start: Include for phase 2
*/
if ( gutenberg_is_experiment_enabled( 'gutenberg-widget-experiments' ) ) {
if ( ! class_exists( 'WP_REST_Widget_Utils_Controller' ) ) {
require dirname( __FILE__ ) . '/class-wp-rest-widget-utils-controller.php';
}
if ( ! class_exists( 'WP_REST_Sidebars_Controller' ) ) {
require_once dirname( __FILE__ ) . '/class-wp-rest-sidebars-controller.php';
}
if ( ! class_exists( 'WP_REST_Widget_Utils_Controller' ) ) {
require dirname( __FILE__ ) . '/class-wp-rest-widget-utils-controller.php';
}
if ( ! class_exists( 'WP_REST_Sidebars_Controller' ) ) {
require_once dirname( __FILE__ ) . '/class-wp-rest-sidebars-controller.php';
}
if ( ! class_exists( 'WP_REST_Block_Directory_Controller' ) ) {
require dirname( __FILE__ ) . '/class-wp-rest-block-directory-controller.php';
Expand Down Expand Up @@ -89,12 +87,10 @@ function gutenberg_is_experiment_enabled( $name ) {
if ( ! class_exists( 'WP_Block_List' ) ) {
require dirname( __FILE__ ) . '/class-wp-block-list.php';
}
if ( gutenberg_is_experiment_enabled( 'gutenberg-widget-experiments' ) ) {
if ( ! class_exists( 'WP_Widget_Block' ) ) {
require_once dirname( __FILE__ ) . '/class-wp-widget-block.php';
}
require_once dirname( __FILE__ ) . '/widgets-page.php';
if ( ! class_exists( 'WP_Widget_Block' ) ) {
require_once dirname( __FILE__ ) . '/class-wp-widget-block.php';
}
require_once dirname( __FILE__ ) . '/widgets-page.php';

require dirname( __FILE__ ) . '/compat.php';
require dirname( __FILE__ ) . '/utils.php';
Expand Down
12 changes: 4 additions & 8 deletions lib/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,8 @@ function gutenberg_filter_rest_prepare_theme( $response, $theme, $request ) {
* @since 5.0.0
*/
function gutenberg_register_rest_widget_updater_routes() {
if ( gutenberg_is_experiment_enabled( 'gutenberg-widget-experiments' ) ) {
$widget_forms = new WP_REST_Widget_Utils_Controller();
$widget_forms->register_routes();
}
$widget_forms = new WP_REST_Widget_Utils_Controller();
$widget_forms->register_routes();
}
add_action( 'rest_api_init', 'gutenberg_register_rest_widget_updater_routes' );

Expand Down Expand Up @@ -194,10 +192,8 @@ function gutenberg_register_plugins_endpoint() {
* Registers the Sidebars REST API routes.
*/
function gutenberg_register_sidebars_endpoint() {
if ( gutenberg_is_experiment_enabled( 'gutenberg-widget-experiments' ) ) {
$sidebars = new WP_REST_Sidebars_Controller();
$sidebars->register_routes();
}
$sidebars = new WP_REST_Sidebars_Controller();
$sidebars->register_routes();
}
add_action( 'rest_api_init', 'gutenberg_register_sidebars_endpoint' );

Expand Down
6 changes: 3 additions & 3 deletions lib/widgets-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @param string $page The page name the function is being called for, `'gutenberg_customizer'` for the Customizer.
*/
function the_gutenberg_widgets( $page = 'gutenberg_page_gutenberg-widgets' ) {
function the_gutenberg_widgets( $page = 'appearance_page_gutenberg-widgets' ) {
?>
<div
id="widgets-editor"
Expand Down Expand Up @@ -45,11 +45,11 @@ function gutenberg_widgets_init( $hook ) {
);
return;
}
if ( ! in_array( $hook, array( 'gutenberg_page_gutenberg-widgets', 'gutenberg_customizer' ), true ) ) {
if ( ! in_array( $hook, array( 'appearance_page_gutenberg-widgets', 'gutenberg_customizer' ), true ) ) {
return;
}

$initializer_name = 'gutenberg_page_gutenberg-widgets' === $hook
$initializer_name = 'appearance_page_gutenberg-widgets' === $hook
? 'initialize'
: 'customizerInitialize';

Expand Down
64 changes: 63 additions & 1 deletion lib/widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,30 @@ function gutenberg_is_block_editor() {
return ! empty( $screen ) &&
(
$screen->is_block_editor() ||
'gutenberg_page_gutenberg-widgets' === $screen->id ||
'appearance_page_gutenberg-widgets' === $screen->id ||
( function_exists( 'gutenberg_is_edit_site_page' ) && gutenberg_is_edit_site_page( $screen->id ) )
);
}

/**
* Whether or not to use the block editor to manage widgets. Defaults to true
* unless a theme has removed support for widgets-block-editor or a plugin has
* filtered the return value of this function.
*
* @return boolean Whether or not to use the block editor to manage widgets.
*/
function gutenberg_use_widgets_block_editor() {
/**
* Filters whether or not to use the block editor to manage widgets.
*
* @param boolean $use_widgets_block_editor Whether or not to use the block editor to manage widgets.
*/
return apply_filters(
'gutenberg_use_widgets_block_editor',
get_theme_support( 'widgets-block-editor' )
);
}

/**
* Emulates the Widgets screen `admin_print_styles` when at the block editor
* screen.
Expand Down Expand Up @@ -261,3 +280,46 @@ function gutenberg_enqueue_widget_scripts() {
}

add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_widget_scripts' );

/**
* Overrides dynamic_sidebar_params to make sure Blocks are not wrapped in <form> tag.
*
* @param array $arg Dynamic sidebar params.
* @return array Updated dynamic sidebar params.
*/
function gutenberg_override_sidebar_params_for_block_widget( $arg ) {
if ( 'Block' === $arg[0]['widget_name'] ) {
$arg[0]['before_form'] = '';
$arg[0]['before_widget_content'] = '<div class="widget-content">';
$arg[0]['after_widget_content'] = '</div><form class="block-widget-form">';
$arg[0]['after_form'] = '</form>';
}

return $arg;
}

/**
* Registers the WP_Widget_Block widget
*/
function gutenberg_register_widgets() {
if ( ! gutenberg_use_widgets_block_editor() ) {
return;
}

register_widget( 'WP_Widget_Block' );
// By default every widget on widgets.php is wrapped with a <form>.
// This means that you can sometimes end up with invalid HTML, e.g. when
// one of the widgets is a Search block.
//
// To fix the problem, let's add a filter that moves the form below the actual
// widget content.
global $pagenow;
if ( 'widgets.php' === $pagenow ) {
add_filter(
'dynamic_sidebar_params',
'gutenberg_override_sidebar_params_for_block_widget'
);
}
}

add_action( 'widgets_init', 'gutenberg_register_widgets' );
1 change: 0 additions & 1 deletion packages/block-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,6 @@ _Properties_
- _titlePlaceholder_ `string`: Empty title placeholder
- _codeEditingEnabled_ `boolean`: Whether or not the user can switch to the code editor
- _\_\_experimentalCanUserUseUnfilteredHTML_ `boolean`: Whether the user should be able to use unfiltered HTML or the HTML should be filtered e.g., to remove elements considered insecure like iframes.
- _\_\_experimentalEnableLegacyWidgetBlock_ `boolean`: Whether the user has enabled the Legacy Widget Block
- _\_\_experimentalBlockDirectory_ `boolean`: Whether the user has enabled the Block Directory
- _\_\_experimentalEnableFullSiteEditing_ `boolean`: Whether the user has enabled Full Site Editing
- _\_\_experimentalEnableFullSiteEditingDemo_ `boolean`: Whether the user has enabled Full Site Editing Demo Templates
Expand Down
2 changes: 0 additions & 2 deletions packages/block-editor/src/store/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export const PREFERENCES_DEFAULTS = {
* @property {string} titlePlaceholder Empty title placeholder
* @property {boolean} codeEditingEnabled Whether or not the user can switch to the code editor
* @property {boolean} __experimentalCanUserUseUnfilteredHTML Whether the user should be able to use unfiltered HTML or the HTML should be filtered e.g., to remove elements considered insecure like iframes.
* @property {boolean} __experimentalEnableLegacyWidgetBlock Whether the user has enabled the Legacy Widget Block
* @property {boolean} __experimentalBlockDirectory Whether the user has enabled the Block Directory
* @property {boolean} __experimentalEnableFullSiteEditing Whether the user has enabled Full Site Editing
* @property {boolean} __experimentalEnableFullSiteEditingDemo Whether the user has enabled Full Site Editing Demo Templates
Expand Down Expand Up @@ -152,7 +151,6 @@ export const SETTINGS_DEFAULTS = {
availableLegacyWidgets: {},
hasPermissionsToManageWidgets: false,
__experimentalCanUserUseUnfilteredHTML: false,
__experimentalEnableLegacyWidgetBlock: false,
__experimentalBlockDirectory: false,
__experimentalEnableFullSiteEditing: false,
__experimentalEnableFullSiteEditingDemo: false,
Expand Down
7 changes: 2 additions & 5 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,11 @@ export const registerCoreBlocks = () => {
export const __experimentalRegisterExperimentalCoreBlocks =
process.env.GUTENBERG_PHASE === 2
? ( settings ) => {
const {
__experimentalEnableLegacyWidgetBlock,
__experimentalEnableFullSiteEditing,
} = settings;
const { __experimentalEnableFullSiteEditing } = settings;

[
widgetArea,
__experimentalEnableLegacyWidgetBlock ? legacyWidget : null,
legacyWidget,
navigation,
navigationLink,

Expand Down
2 changes: 1 addition & 1 deletion packages/edit-widgets/src/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function Header( { isCustomizer } ) {
</NavigableMenu>
{ ! isCustomizer && (
<h1 className="edit-widgets-header__title">
{ __( 'Block Areas' ) } { __( '(experimental)' ) }
{ __( 'Block Areas' ) }
</h1>
) }
<div className="edit-widgets-header__actions">
Expand Down
2 changes: 1 addition & 1 deletion packages/edit-widgets/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ html.wp-toolbar {
background: $white;
}

body.gutenberg_page_gutenberg-widgets {
body.appearance_page_gutenberg-widgets {
@include wp-admin-reset( ".blocks-widgets-container" );
}

Expand Down
1 change: 0 additions & 1 deletion packages/editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ class EditorProvider extends Component {
'__experimentalBlockPatterns',
'__experimentalBlockPatternCategories',
'__experimentalEnableCustomSpacing',
'__experimentalEnableLegacyWidgetBlock',
'__experimentalEnableLinkColor',
'__experimentalEnableFullSiteEditing',
'__experimentalEnableFullSiteEditingDemo',
Expand Down
1 change: 0 additions & 1 deletion test/integration/full-content/full-content.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ describe( 'full post content fixture', () => {
);
unstable__bootstrapServerSideBlockDefinitions( blockDefinitions );
const settings = {
__experimentalEnableLegacyWidgetBlock: true,
__experimentalEnableFullSiteEditing: true,
};
// Load all hooks that modify blocks
Expand Down

0 comments on commit dc4a0b3

Please sign in to comment.