Skip to content

Commit c5df375

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 2da7a59 commit c5df375

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
@@ -1305,6 +1305,7 @@ public static function get_filename_from_disposition( $disposition_header ) {
13051305
* Retrieves the query params for collections of attachments.
13061306
*
13071307
* @since 4.7.0
1308+
* @since 6.9.0 Extends the `orderby` request argument to support `mime_type`.
13081309
*
13091310
* @return array Query parameters for the attachment collection as an array.
13101311
*/
@@ -1327,6 +1328,8 @@ public function get_collection_params() {
13271328
'type' => 'string',
13281329
);
13291330

1331+
$params['orderby']['enum'][] = 'mime_type';
1332+
13301333
return $params;
13311334
}
13321335

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2682,4 +2682,47 @@ public function test_upload_svg_image() {
26822682

26832683
$this->assertTrue( $result );
26842684
}
2685+
2686+
/**
2687+
* Test that the `orderby` parameter works with the `mime_type` parameter.
2688+
*
2689+
* @ticket 64073
2690+
*/
2691+
public function test_get_items_orderby_mime_type() {
2692+
$jpeg_id = self::factory()->attachment->create_object(
2693+
self::$test_file,
2694+
0,
2695+
array(
2696+
'post_mime_type' => 'image/jpeg',
2697+
'post_excerpt' => 'A sample caption',
2698+
)
2699+
);
2700+
2701+
$png_id = self::factory()->attachment->create_object(
2702+
self::$test_file2,
2703+
0,
2704+
array(
2705+
'post_mime_type' => 'image/png',
2706+
'post_excerpt' => 'A sample caption',
2707+
)
2708+
);
2709+
2710+
$request = new WP_REST_Request( 'GET', '/wp/v2/media' );
2711+
2712+
// Check ordering. Default ORDER is DESC.
2713+
$request->set_param( 'orderby', 'mime_type' );
2714+
$response = rest_get_server()->dispatch( $request );
2715+
$data = $response->get_data();
2716+
$this->assertCount( 2, $data, 'Response count for orderby DESC mime_type is not 2' );
2717+
$this->assertSame( $png_id, $data[0]['id'], 'PNG ID not found in response for orderby DESC mime_type' );
2718+
$this->assertSame( $jpeg_id, $data[1]['id'], 'JPEG ID not found in response for orderby DESC mime_type' );
2719+
2720+
// ASC order.
2721+
$request->set_param( 'order', 'asc' );
2722+
$response = rest_get_server()->dispatch( $request );
2723+
$data = $response->get_data();
2724+
$this->assertCount( 2, $data, 'Response count for orderby ASC mime_type is not 2' );
2725+
$this->assertSame( $jpeg_id, $data[0]['id'], 'JPEG ID not found in response for orderby ASC mime_type' );
2726+
$this->assertSame( $png_id, $data[1]['id'], 'PNG ID not found in response for orderby ASC mime_type' );
2727+
}
26852728
}

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)