From 21f4a7e63d0403ea044920485a955eb2a40a310b Mon Sep 17 00:00:00 2001 From: Jorge Date: Tue, 9 Apr 2019 10:25:36 +0100 Subject: [PATCH] Remove hasEditForm attribute --- ...lass-wp-rest-widget-updater-controller.php | 22 ++++++++++------- lib/widgets.php | 17 +------------ .../src/legacy-widget/WidgetEditHandler.js | 10 +++++--- .../block-library/src/legacy-widget/edit.js | 24 +++++++++++++------ .../block-library/src/legacy-widget/index.php | 3 --- 5 files changed, 39 insertions(+), 37 deletions(-) diff --git a/lib/class-wp-rest-widget-updater-controller.php b/lib/class-wp-rest-widget-updater-controller.php index 5e528473517128..658dcdf15be47d 100644 --- a/lib/class-wp-rest-widget-updater-controller.php +++ b/lib/class-wp-rest-widget-updater-controller.php @@ -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 ] ) && @@ -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(), diff --git a/lib/widgets.php b/lib/widgets.php index 87ae38aa47a7a9..c08d31de9f3f11 100644 --- a/lib/widgets.php +++ b/lib/widgets.php @@ -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( @@ -104,13 +102,10 @@ 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] ) && @@ -118,20 +113,10 @@ function gutenberg_legacy_widget_settings( $settings ) { ) { 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, ); } diff --git a/packages/block-library/src/legacy-widget/WidgetEditHandler.js b/packages/block-library/src/legacy-widget/WidgetEditHandler.js index a24d4d422c16b7..a9877dc5592466 100644 --- a/packages/block-library/src/legacy-widget/WidgetEditHandler.js +++ b/packages/block-library/src/legacy-widget/WidgetEditHandler.js @@ -25,7 +25,9 @@ class WidgetEditHandler extends Component { componentDidMount() { this.isStillMounted = true; - this.requestWidgetUpdater(); + this.requestWidgetUpdater( undefined, ( response ) => { + this.props.onInstanceChange( null, !! response.form ); + } ); } componentDidUpdate( prevProps ) { @@ -33,7 +35,9 @@ class WidgetEditHandler extends Component { 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; @@ -81,7 +85,7 @@ class WidgetEditHandler extends Component { onInstanceChange( instanceChanges ) { this.requestWidgetUpdater( instanceChanges, ( response ) => { this.instanceUpdating = response.instance; - this.props.onInstanceChange( response.instance ); + this.props.onInstanceChange( response.instance, !! response.form ); } ); } diff --git a/packages/block-library/src/legacy-widget/edit.js b/packages/block-library/src/legacy-widget/edit.js index 348341b9848a33..ea207b0fbe9bd0 100644 --- a/packages/block-library/src/legacy-widget/edit.js +++ b/packages/block-library/src/legacy-widget/edit.js @@ -34,6 +34,7 @@ class LegacyWidgetEdit extends Component { constructor() { super( ...arguments ); this.state = { + hasEditForm: true, isPreview: false, }; this.switchToEdit = this.switchToEdit.bind( this ); @@ -48,8 +49,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; @@ -67,7 +68,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 ) => { @@ -143,10 +143,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, + } ); + } } } /> @@ -162,6 +169,9 @@ class LegacyWidgetEdit extends Component { instance: {}, identifier: undefined, } ); + this.setState( { + hasEditForm: true, + } ); } switchToEdit() { diff --git a/packages/block-library/src/legacy-widget/index.php b/packages/block-library/src/legacy-widget/index.php index 03c42bed6b97be..7f06f086aa7295 100644 --- a/packages/block-library/src/legacy-widget/index.php +++ b/packages/block-library/src/legacy-widget/index.php @@ -72,9 +72,6 @@ function register_block_core_legacy_widget() { 'isCallbackWidget' => array( 'type' => 'boolean', ), - 'hasEditForm' => array( - 'type' => 'boolean', - ), ), 'render_callback' => 'render_block_legacy_widget', )