diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-widget-types-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-widget-types-controller.php index 4fdd3ac1e2423..f09277c173927 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-widget-types-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-widget-types-controller.php @@ -225,7 +225,7 @@ protected function get_widgets() { } $widget['classname'] = ltrim( $classname, '_' ); - $widgets[] = $widget; + $widgets[ $widget['id'] ] = $widget; } return $widgets; diff --git a/tests/phpunit/tests/rest-api/rest-widget-types-controller.php b/tests/phpunit/tests/rest-api/rest-widget-types-controller.php index 884e0937f0545..fc4eddc2a80fd 100644 --- a/tests/phpunit/tests/rest-api/rest-widget-types-controller.php +++ b/tests/phpunit/tests/rest-api/rest-widget-types-controller.php @@ -61,6 +61,22 @@ public static function wpTearDownAfterClass() { self::delete_user( self::$subscriber_id ); } + private function setup_widget( $id_base, $number, $settings ) { + global $wp_widget_factory; + + $option_name = "widget_$id_base"; + update_option( + $option_name, + array( + $number => $settings, + ) + ); + + $widget_object = $wp_widget_factory->get_widget_object( $id_base ); + $widget_object->_set( $number ); + $widget_object->_register_one( $number ); + } + /** * @ticket 41683 */ @@ -106,7 +122,37 @@ public function test_get_items() { $widget_type = $endpoint->get_widget( $item['name'] ); $this->check_widget_type_object( $widget_type, $item, $item['_links'] ); } + } + /** + * @ticket 53305 + */ + public function test_get_items_removes_duplicates() { + wp_set_current_user( self::$admin_id ); + $this->setup_widget( + 'text', + 1, + array( + 'text' => 'Custom text test', + ) + ); + $this->setup_widget( + 'text', + 2, + array( + 'text' => 'Custom text test', + ) + ); + $request = new WP_REST_Request( 'GET', '/wp/v2/widget-types' ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $text_widgets = array_filter( + $data, + function( $widget ) { + return 'text' === $widget['id']; + } + ); + $this->assertCount( 1, $text_widgets ); } /**