Skip to content

Commit

Permalink
Remove hasEditForm attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed May 15, 2019
1 parent 3045a2d commit 86539f0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 37 deletions.
22 changes: 14 additions & 8 deletions lib/class-wp-rest-widget-updater-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,8 @@ private function is_valid_widget( $identifier, $is_callback_widget ) {
return false;
}
if ( $is_callback_widget ) {
global $wp_registered_widget_controls;
return isset( $wp_registered_widget_controls[ $identifier ]['callback'] ) &&
is_callable( $wp_registered_widget_controls[ $identifier ]['callback'] );
global $wp_registered_widgets;
return isset( $wp_registered_widgets[ $identifier ] );
}
global $wp_widget_factory;
return isset( $wp_widget_factory->widgets[ $identifier ] ) &&
Expand Down Expand Up @@ -128,11 +127,18 @@ private function parse_instance_changes( $id_base, $id, $instance_changes ) {
*/
private function compute_new_widget_handle_callback_widgets( $identifier, $instance_changes ) {
global $wp_registered_widget_controls;
$control = $wp_registered_widget_controls[ $identifier ];
$_POST = array_merge( $_POST, $instance_changes );
ob_start();
call_user_func_array( $control['callback'], $control['params'] );
$form = ob_get_clean();
$form = '';
if (
isset( $wp_registered_widget_controls[ $identifier ]['callback'] ) &&
is_callable( $wp_registered_widget_controls[ $identifier ]['callback'] )
) {
$control = $wp_registered_widget_controls[ $identifier ];
$_POST = array_merge( $_POST, $instance_changes );
ob_start();
call_user_func_array( $control['callback'], $control['params'] );
$form = ob_get_clean();
}

return rest_ensure_response(
array(
'instance' => array(),
Expand Down
17 changes: 1 addition & 16 deletions lib/widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ function gutenberg_legacy_widget_settings( $settings ) {

$has_permissions_to_manage_widgets = current_user_can( 'edit_theme_options' );
$available_legacy_widgets = array();
global $wp_widget_factory, $wp_registered_widgets, $wp_registered_widget_controls;
// Add widgets implemented as a class to the available_legacy_widgets widgets array.
// All widgets implemented as a class have an edit form.
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(
Expand All @@ -104,34 +102,21 @@ function gutenberg_legacy_widget_settings( $settings ) {
html_entity_decode( $widget_obj->widget_options['description'] ) :
null,
'isCallbackWidget' => false,
'hasEditForm' => true,
);
}
}
// Add widgets registered using wp_register_sidebar_widget.
foreach ( $wp_registered_widgets as $widget_id => $widget_obj ) {
// Skip instances of widgets that are implemented as classes.
if (
is_array( $widget_obj['callback'] ) &&
isset( $widget_obj['callback'][0] ) &&
( $widget_obj['callback'][0] instanceof WP_Widget )
) {
continue;
}
// By default widgets registered with wp_register_sidebar_widget don't have an edit form, but a form may be added.
$has_edit_form = false;
// Checks if an edit form was added.
if (
isset( $wp_registered_widget_controls[ $widget_id ]['callback'] ) &&
is_callable( $wp_registered_widget_controls[ $widget_id ]['callback'] )
) {
$has_edit_form = true;
}
$available_legacy_widgets[ $widget_id ] = array(
'name' => html_entity_decode( $widget_obj['name'] ),
'description' => html_entity_decode( wp_widget_description( $widget_id ) ),
'isCallbackWidget' => true,
'hasEditForm' => $has_edit_form,
);
}

Expand Down
10 changes: 7 additions & 3 deletions packages/block-library/src/legacy-widget/edit/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@ class LegacyWidgetEditHandler extends Component {

componentDidMount() {
this.isStillMounted = true;
this.requestWidgetUpdater();
this.requestWidgetUpdater( undefined, ( response ) => {
this.props.onInstanceChange( null, !! response.form );
} );
}

componentDidUpdate( prevProps ) {
if (
prevProps.instance !== this.props.instance &&
this.instanceUpdating !== this.props.instance
) {
this.requestWidgetUpdater();
this.requestWidgetUpdater( undefined, ( response ) => {
this.props.onInstanceChange( null, !! response.form );
} );
}
if ( this.instanceUpdating === this.props.instance ) {
this.instanceUpdating = null;
Expand Down Expand Up @@ -81,7 +85,7 @@ class LegacyWidgetEditHandler extends Component {
onInstanceChange( instanceChanges ) {
this.requestWidgetUpdater( instanceChanges, ( response ) => {
this.instanceUpdating = response.instance;
this.props.onInstanceChange( response.instance );
this.props.onInstanceChange( response.instance, !! response.form );
} );
}

Expand Down
24 changes: 17 additions & 7 deletions packages/block-library/src/legacy-widget/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class LegacyWidgetEdit extends Component {
constructor() {
super( ...arguments );
this.state = {
hasEditForm: true,
isPreview: false,
};
this.switchToEdit = this.switchToEdit.bind( this );
Expand All @@ -47,8 +48,8 @@ class LegacyWidgetEdit extends Component {
hasPermissionsToManageWidgets,
setAttributes,
} = this.props;
const { isPreview } = this.state;
const { identifier, isCallbackWidget, hasEditForm } = attributes;
const { isPreview, hasEditForm } = this.state;
const { identifier, isCallbackWidget } = attributes;
const widgetObject = identifier && availableLegacyWidgets[ identifier ];
if ( ! widgetObject ) {
let placeholderContent;
Expand All @@ -66,7 +67,6 @@ class LegacyWidgetEdit extends Component {
instance: {},
identifier: value,
isCallbackWidget: availableLegacyWidgets[ value ].isCallbackWidget,
hasEditForm: availableLegacyWidgets[ value ].hasEditForm,
} ) }
options={ [ { value: 'none', label: 'Select widget' } ].concat(
map( availableLegacyWidgets, ( widget, key ) => {
Expand Down Expand Up @@ -142,10 +142,17 @@ class LegacyWidgetEdit extends Component {
instance={ attributes.instance }
isCallbackWidget={ isCallbackWidget }
onInstanceChange={
( newInstance ) => {
this.props.setAttributes( {
instance: newInstance,
} );
( newInstance, newHasEditForm ) => {
if ( newInstance ) {
this.props.setAttributes( {
instance: newInstance,
} );
}
if ( newHasEditForm !== this.hasEditForm ) {
this.setState( {
hasEditForm: newHasEditForm,
} );
}
}
}
/>
Expand All @@ -161,6 +168,9 @@ class LegacyWidgetEdit extends Component {
instance: {},
identifier: undefined,
} );
this.setState( {
hasEditForm: true,
} );
}

switchToEdit() {
Expand Down
3 changes: 0 additions & 3 deletions packages/block-library/src/legacy-widget/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ function register_block_core_legacy_widget() {
'isCallbackWidget' => array(
'type' => 'boolean',
),
'hasEditForm' => array(
'type' => 'boolean',
),
),
'render_callback' => 'render_block_legacy_widget',
)
Expand Down

0 comments on commit 86539f0

Please sign in to comment.