From 6194de49dcd79c9aeae7bd96722d69f752076f6f Mon Sep 17 00:00:00 2001 From: Timothy Jacobs Date: Sat, 2 Oct 2021 21:43:26 +0000 Subject: [PATCH] REST API: Sort widget types by their id. Fixes #53303. Props spacedmonkey. git-svn-id: https://develop.svn.wordpress.org/trunk@51882 602fd350-edb4-49c9-b593-d223f7449a82 --- .../class-wp-rest-widget-types-controller.php | 2 ++ .../rest-api/rest-widget-types-controller.php | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) 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 3940566467..82c3a5255e 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 @@ -237,6 +237,8 @@ class WP_REST_Widget_Types_Controller extends WP_REST_Controller { $widgets[ $widget['id'] ] = $widget; } + ksort( $widgets ); + 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 fd0b3007ec..a7d1d47d61 100644 --- a/tests/phpunit/tests/rest-api/rest-widget-types-controller.php +++ b/tests/phpunit/tests/rest-api/rest-widget-types-controller.php @@ -125,6 +125,22 @@ class WP_Test_REST_Widget_Types_Controller extends WP_Test_REST_Controller_Testc } } + /** + * @ticket 53303 + */ + public function test_get_items_ordering() { + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/widget-types' ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $this->assertGreaterThan( 1, count( $data ) ); + $ids = wp_list_pluck( $data, 'id' ); + $sorted = $ids; + sort( $sorted ); + + $this->assertSame( $sorted, $ids ); + } + /** * @ticket 53305 */