Skip to content

Commit

Permalink
Allow core widgets to be used in the legacy widgets block (although n…
Browse files Browse the repository at this point in the history
…ot selectable).
  • Loading branch information
jorgefilipecosta committed May 30, 2019
1 parent 1a6660c commit f256604
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
23 changes: 11 additions & 12 deletions lib/widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,17 @@ function gutenberg_get_legacy_widget_settings() {
$available_legacy_widgets = array();
global $wp_widget_factory, $wp_registered_widgets;
foreach ( $wp_widget_factory->widgets as $class => $widget_obj ) {
if ( ! in_array( $class, $core_widgets ) ) {
$available_legacy_widgets[ $class ] = array(
'name' => html_entity_decode( $widget_obj->name ),
// wp_widget_description is not being used because its input parameter is a Widget Id.
// Widgets id's reference to a specific widget instance.
// Here we are iterating on all the available widget classes even if no widget instance exists for them.
'description' => isset( $widget_obj->widget_options['description'] ) ?
html_entity_decode( $widget_obj->widget_options['description'] ) :
null,
'isCallbackWidget' => false,
);
}
$available_legacy_widgets[ $class ] = array(
'name' => html_entity_decode( $widget_obj->name ),
// wp_widget_description is not being used because its input parameter is a Widget Id.
// Widgets id's reference to a specific widget instance.
// Here we are iterating on all the available widget classes even if no widget instance exists for them.
'description' => isset( $widget_obj->widget_options['description'] ) ?
html_entity_decode( $widget_obj->widget_options['description'] ) :
null,
'isCallbackWidget' => false,
'isHidden' => in_array( $class, $core_widgets, true ),
);
}
foreach ( $wp_registered_widgets as $widget_id => $widget_obj ) {
if (
Expand Down
13 changes: 10 additions & 3 deletions packages/block-library/src/legacy-widget/edit/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/**
* External dependencies
*/
import { map } from 'lodash';
import {
map,
pickBy,
} from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -47,6 +50,10 @@ class LegacyWidgetEdit extends Component {
hasPermissionsToManageWidgets,
setAttributes,
} = this.props;
const visibleLegacyWidgets = pickBy(
availableLegacyWidgets,
( { isHidden } ) => ! isHidden
);
const { isPreview } = this.state;
const { identifier, isCallbackWidget } = attributes;
const widgetObject = identifier && availableLegacyWidgets[ identifier ];
Expand All @@ -55,7 +62,7 @@ class LegacyWidgetEdit extends Component {

if ( ! hasPermissionsToManageWidgets ) {
placeholderContent = __( 'You don\'t have permissions to use widgets on this site.' );
} else if ( availableLegacyWidgets.length === 0 ) {
} else if ( visibleLegacyWidgets.length === 0 ) {
placeholderContent = __( 'There are no widgets available.' );
} else {
placeholderContent = (
Expand All @@ -68,7 +75,7 @@ class LegacyWidgetEdit extends Component {
isCallbackWidget: availableLegacyWidgets[ value ].isCallbackWidget,
} ) }
options={ [ { value: 'none', label: 'Select widget' } ].concat(
map( availableLegacyWidgets, ( widget, key ) => {
map( visibleLegacyWidgets, ( widget, key ) => {
return {
value: key,
label: widget.name,
Expand Down

0 comments on commit f256604

Please sign in to comment.