Skip to content

Commit 5203dbf

Browse files
committed
REST API: Extend orderby parameter to support mime_type for attachments.
This update allows the `orderby` request argument in the attachments controller to include `mime_type`, enhancing the sorting capabilities of media items. Additionally, a new test has been added to verify the correct functionality of this feature. Fixes trac 64073.
1 parent 8c2ec29 commit 5203dbf

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,7 @@ public static function get_filename_from_disposition( $disposition_header ) {
13421342
* Retrieves the query params for collections of attachments.
13431343
*
13441344
* @since 4.7.0
1345+
* @since 6.9.0 Extends the `orderby` request argument to support `mime_type`.
13451346
*
13461347
* @return array Query parameters for the attachment collection as an array.
13471348
*/
@@ -1364,6 +1365,8 @@ public function get_collection_params() {
13641365
'type' => 'string',
13651366
);
13661367

1368+
$params['orderby']['enum'][] = 'mime_type';
1369+
13671370
return $params;
13681371
}
13691372

tests/phpunit/tests/rest-api/rest-attachments-controller.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2827,4 +2827,47 @@ public function test_edit_image_vertical_flip_only() {
28272827
// The controller converts the integer values to booleans: 0 !== (int) 1 = true.
28282828
$this->assertSame( array( true, false ), WP_Image_Editor_Mock::$spy['flip'][0], 'Vertical flip of the image is not identical.' );
28292829
}
2830+
2831+
/**
2832+
* Test that the `orderby` parameter works with the `mime_type` parameter.
2833+
*
2834+
* @ticket 64073
2835+
*/
2836+
public function test_get_items_orderby_mime_type() {
2837+
$jpeg_id = self::factory()->attachment->create_object(
2838+
self::$test_file,
2839+
0,
2840+
array(
2841+
'post_mime_type' => 'image/jpeg',
2842+
'post_excerpt' => 'A sample caption',
2843+
)
2844+
);
2845+
2846+
$png_id = self::factory()->attachment->create_object(
2847+
self::$test_file2,
2848+
0,
2849+
array(
2850+
'post_mime_type' => 'image/png',
2851+
'post_excerpt' => 'A sample caption',
2852+
)
2853+
);
2854+
2855+
$request = new WP_REST_Request( 'GET', '/wp/v2/media' );
2856+
2857+
// Check ordering. Default ORDER is DESC.
2858+
$request->set_param( 'orderby', 'mime_type' );
2859+
$response = rest_get_server()->dispatch( $request );
2860+
$data = $response->get_data();
2861+
$this->assertCount( 2, $data, 'Response count for orderby DESC mime_type is not 2' );
2862+
$this->assertSame( $png_id, $data[0]['id'], 'PNG ID not found in response for orderby DESC mime_type' );
2863+
$this->assertSame( $jpeg_id, $data[1]['id'], 'JPEG ID not found in response for orderby DESC mime_type' );
2864+
2865+
// ASC order.
2866+
$request->set_param( 'order', 'asc' );
2867+
$response = rest_get_server()->dispatch( $request );
2868+
$data = $response->get_data();
2869+
$this->assertCount( 2, $data, 'Response count for orderby ASC mime_type is not 2' );
2870+
$this->assertSame( $jpeg_id, $data[0]['id'], 'JPEG ID not found in response for orderby ASC mime_type' );
2871+
$this->assertSame( $png_id, $data[1]['id'], 'PNG ID not found in response for orderby ASC mime_type' );
2872+
}
28302873
}

tests/qunit/fixtures/wp-api-generated.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2903,7 +2903,8 @@ mockedApiResponse.Schema = {
29032903
"relevance",
29042904
"slug",
29052905
"include_slugs",
2906-
"title"
2906+
"title",
2907+
"mime_type"
29072908
],
29082909
"required": false
29092910
},

0 commit comments

Comments
 (0)