diff --git a/lib/class-wp-rest-sidebars-controller.php b/lib/class-wp-rest-sidebars-controller.php index 971a7dad4a7bfa..d5715ca5587ba6 100644 --- a/lib/class-wp-rest-sidebars-controller.php +++ b/lib/class-wp-rest-sidebars-controller.php @@ -142,7 +142,7 @@ public function update_item( $request ) { $sidebar_widgets_ids = array(); foreach ( $input_widgets as $input_widget ) { ob_start(); - if ( isset( $wp_registered_widget_updates[ $input_widget['id_base'] ] ) ) { + if ( isset( $input_widget['id_base'] ) && isset( $wp_registered_widget_updates[ $input_widget['id_base'] ] ) ) { // Class-based widget. $update_control = $wp_registered_widget_updates[ $input_widget['id_base'] ]; if ( ! isset( $input_widget['id'] ) ) { @@ -176,11 +176,23 @@ public function update_item( $request ) { $wp_registered_widgets[ $input_widget['id'] ]['callback'][0] = $new_object; } } - } elseif ( $wp_registered_widget_updates[ $input_widget['id'] ] ) { - // Old-style widget. - $update_control = $wp_registered_widget_updates[ $input_widget['id'] ]; - $_POST = wp_slash( $input_widget['settings'] ); - call_user_func( $update_control['callback'] ); + } else { + $registered_widget_id = null; + if ( isset( $wp_registered_widget_updates[ $input_widget['id'] ] ) ) { + $registered_widget_id = $input_widget['id']; + } else { + $numberless_id = substr( $input_widget['id'], 0, strrpos( $input_widget['id'], '-' ) ); + if ( isset( $wp_registered_widget_updates[ $numberless_id ] ) ) { + $registered_widget_id = $numberless_id; + } + } + + if ( $registered_widget_id ) { + // Old-style widget. + $update_control = $wp_registered_widget_updates[ $registered_widget_id ]; + $_POST = wp_slash( $input_widget['settings'] ); + call_user_func( $update_control['callback'] ); + } } ob_end_clean(); @@ -213,6 +225,7 @@ public function get_items( $request ) { $data[] = $this->prepare_item_for_response( $sidebar, $request )->get_data(); } + return rest_ensure_response( $data ); } @@ -350,7 +363,7 @@ public static function get_widgets( $sidebar_id ) { * Prepare a single sidebar output for response * * @param array $raw_sidebar Sidebar instance. - * @param WP_REST_Request $request Request object. + * @param WP_REST_Request $request Request object. * * @return WP_REST_Response $data */ @@ -390,7 +403,7 @@ public function prepare_item_for_response( $raw_sidebar, $request ) { foreach ( $schema['properties']['widgets']['items']['properties'] as $property_id => $property ) { if ( isset( $widget[ $property_id ] ) && gettype( $widget[ $property_id ] ) === $property['type'] ) { $widget_data[ $property_id ] = $widget[ $property_id ]; - } elseif ( 'settings' === $property_id && 'array' === gettype( $widget[ $property_id ] ) ) { + } elseif ( 'settings' === $property_id && isset( $widget[ $property_id ] ) && 'array' === gettype( $widget[ $property_id ] ) ) { $widget_data[ $property_id ] = $widget['settings']; } elseif ( isset( $property['default'] ) ) { $widget_data[ $property_id ] = $property['default']; diff --git a/phpunit/class-rest-sidebars-controller-test.php b/phpunit/class-rest-sidebars-controller-test.php index cfd9dfa85392bc..8059cb2fcb4da1 100644 --- a/phpunit/class-rest-sidebars-controller-test.php +++ b/phpunit/class-rest-sidebars-controller-test.php @@ -378,6 +378,98 @@ public function test_update_item() { ); } + /** + * + */ + public function test_update_item_legacy_widget_1() { + $this->do_test_update_item_legacy_widget( 'testwidget-1' ); + } + + /** + * + */ + public function test_update_item_legacy_widget_2() { + $this->do_test_update_item_legacy_widget( 'testwidget' ); + } + + /** + * + */ + public function do_test_update_item_legacy_widget( $widget_id ) { + // @TODO: Use @dataProvider instead (it doesn't work with custom constructors like the one we have in this class) + wp_register_widget_control( + $widget_id, + 'WP test widget', + function() { + $settings = get_option( 'widget_testwidget' ); + + // check if anything's been sent. + if ( isset( $_POST['update_testwidget'] ) ) { + $settings['id'] = $_POST['test_id']; + $settings['title'] = $_POST['test_title']; + + update_option( 'widget_testwidget', $settings ); + } + }, + 100, + 200 + ); + wp_register_sidebar_widget( + $widget_id, + 'WP test widget', + function() { + $settings = get_option( 'widget_testwidget' ) ? get_option( 'widget_testwidget' ) : array( + 'id' => '', + 'title' => '', + ); + echo '