Skip to content

Commit bc1a628

Browse files
committed
Refactor media type and mime type handling in WP REST Attachments Controller to enforce array input. Updated tests to validate new behavior and ensure proper error responses for invalid parameters.
1 parent 434e98a commit bc1a628

File tree

3 files changed

+24
-48
lines changed

3 files changed

+24
-48
lines changed

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,24 +86,16 @@ protected function prepare_items_query( $prepared_args = array(), $request = nul
8686
$all_mime_types = array();
8787
$media_types = $this->get_media_types();
8888

89-
if ( ! empty( $request['media_type'] ) ) {
90-
$media_type_input = is_array( $request['media_type'] )
91-
? $request['media_type']
92-
: explode( ',', $request['media_type'] );
93-
94-
foreach ( array_map( 'trim', $media_type_input ) as $type ) {
89+
if ( ! empty( $request['media_type'] ) && is_array( $request['media_type'] ) ) {
90+
foreach ( $request['media_type'] as $type ) {
9591
if ( isset( $media_types[ $type ] ) ) {
9692
$all_mime_types = array_merge( $all_mime_types, $media_types[ $type ] );
9793
}
9894
}
9995
}
10096

101-
if ( ! empty( $request['mime_type'] ) ) {
102-
$mime_type_input = is_array( $request['mime_type'] )
103-
? $request['mime_type']
104-
: explode( ',', $request['mime_type'] );
105-
106-
foreach ( array_map( 'trim', $mime_type_input ) as $mime_type ) {
97+
if ( ! empty( $request['mime_type'] ) && is_array( $request['mime_type'] ) ) {
98+
foreach ( $request['mime_type'] as $mime_type ) {
10799
$parts = explode( '/', $mime_type );
108100
if ( isset( $media_types[ $parts[0] ] ) && in_array( $mime_type, $media_types[ $parts[0] ], true ) ) {
109101
$all_mime_types[] = $mime_type;
@@ -1338,7 +1330,7 @@ public function get_collection_params() {
13381330
$params['media_type'] = array(
13391331
'default' => null,
13401332
'description' => __( 'Limit result set to attachments of a particular media type or media types.' ),
1341-
'type' => array( 'string', 'array' ),
1333+
'type' => 'array',
13421334
'items' => array(
13431335
'type' => 'string',
13441336
'enum' => $media_types,
@@ -1348,9 +1340,10 @@ public function get_collection_params() {
13481340
$params['mime_type'] = array(
13491341
'default' => null,
13501342
'description' => __( 'Limit result set to attachments of a particular MIME type or MIME types.' ),
1351-
'type' => array( 'string', 'array' ),
1343+
'type' => 'array',
13521344
'items' => array(
13531345
'type' => 'string',
1346+
'enum' => get_allowed_mime_types(),
13541347
),
13551348
);
13561349

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

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -522,11 +522,7 @@ public function test_get_items_multiple_media_types() {
522522
// Test invalid media type mixed with valid ones.
523523
$request->set_param( 'media_type', 'image,invalid,video' );
524524
$response = rest_get_server()->dispatch( $request );
525-
$data = $response->get_data();
526-
$this->assertCount( 2, $data, 'Response count for multiple media types with comma-separated string is not 2' );
527-
$ids = wp_list_pluck( $data, 'id' );
528-
$this->assertContains( $image_id, $ids, 'Image ID not found in response for multiple media types with comma-separated string and invalid media type' );
529-
$this->assertContains( $video_id, $ids, 'Video ID not found in response for multiple media types with comma-separated string and invalid media type' );
525+
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
530526
}
531527

532528
/**
@@ -559,14 +555,6 @@ public function test_get_items_multiple_mime_types_and_combination() {
559555
)
560556
);
561557

562-
$rtf_id = self::factory()->attachment->create_object(
563-
self::$test_rtf_file,
564-
0,
565-
array(
566-
'post_mime_type' => 'application/rtf',
567-
)
568-
);
569-
570558
$request = new WP_REST_Request( 'GET', '/wp/v2/media' );
571559

572560
// Test single MIME type
@@ -595,18 +583,10 @@ public function test_get_items_multiple_mime_types_and_combination() {
595583
$this->assertContains( $jpeg_id, $ids, 'JPEG ID not found in response for multiple MIME types with array format' );
596584
$this->assertContains( $mp4_id, $ids, 'MP4 ID not found in response for multiple MIME types with array format' );
597585

598-
// Test multiple media types with multiple MIME types.
599-
$request->set_param( 'media_type', 'image,video' );
600-
$request->set_param( 'mime_type', 'application/rtf' );
586+
// Test invalid mime type mixed with valid ones.
587+
$request->set_param( 'mime_type', array( 'video/mp4', 'cat/gif' ) );
601588
$response = rest_get_server()->dispatch( $request );
602-
$data = $response->get_data();
603-
604-
$this->assertCount( 4, $data, 'Response count for multiple media types with multiple MIME types is not 4' );
605-
$ids = wp_list_pluck( $data, 'id' );
606-
$this->assertContains( $jpeg_id, $ids, 'JPEG ID not found in response for multiple media types with multiple MIME types' );
607-
$this->assertContains( $png_id, $ids, 'PNG ID not found in response for multiple media types with multiple MIME types' );
608-
$this->assertContains( $mp4_id, $ids, 'MP4 ID not found in response for multiple media types with multiple MIME types' );
609-
$this->assertContains( $rtf_id, $ids, 'RTF ID not found in response for multiple media types with multiple MIME types' );
589+
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
610590
}
611591

612592
/**
@@ -649,6 +629,14 @@ public function test_get_items_with_media_type_and_media_types() {
649629
)
650630
);
651631

632+
$rtf_id = self::factory()->attachment->create_object(
633+
self::$test_rtf_file,
634+
0,
635+
array(
636+
'post_mime_type' => 'application/rtf',
637+
)
638+
);
639+
652640
// Test combination of single media type and single mime type parameters.
653641
$request = new WP_REST_Request( 'GET', '/wp/v2/media' );
654642
$request->set_param( 'media_type', 'image' );
@@ -691,16 +679,17 @@ public function test_get_items_with_media_type_and_media_types() {
691679
// Test combination of multiple media types and multiple mime type parameters.
692680
$request = new WP_REST_Request( 'GET', '/wp/v2/media' );
693681
$request->set_param( 'media_type', 'audio,video' );
694-
$request->set_param( 'mime_type', array( 'image/jpeg', 'image/png' ) );
682+
$request->set_param( 'mime_type', array( 'image/jpeg', 'image/png', 'application/rtf' ) );
695683
$response = rest_get_server()->dispatch( $request );
696684
$data = $response->get_data();
697685
$ids = wp_list_pluck( $data, 'id' );
698686

699-
$this->assertCount( 4, $data, 'Response count for combination of multiple media type and multiple mime type parameters is not 3' );
687+
$this->assertCount( 5, $data, 'Response count for combination of multiple media type and multiple mime type parameters is not 3' );
700688
$this->assertContains( $audio_id, $ids, 'Audio ID not found in response' );
701689
$this->assertContains( $jpeg_id, $ids, 'JPEG ID not found in response' );
702690
$this->assertContains( $video_id, $ids, 'Video ID not found in response' );
703691
$this->assertContains( $png_id, $ids, 'PNG ID not found in response' );
692+
$this->assertContains( $rtf_id, $ids, 'RTF ID not found in response' );
704693
}
705694

706695
public function test_get_items_mime_type() {

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2964,10 +2964,7 @@ mockedApiResponse.Schema = {
29642964
"media_type": {
29652965
"default": null,
29662966
"description": "Limit result set to attachments of a particular media type or media types.",
2967-
"type": [
2968-
"string",
2969-
"array"
2970-
],
2967+
"type": "array",
29712968
"items": {
29722969
"type": "string",
29732970
"enum": [
@@ -2983,10 +2980,7 @@ mockedApiResponse.Schema = {
29832980
"mime_type": {
29842981
"default": null,
29852982
"description": "Limit result set to attachments of a particular MIME type or MIME types.",
2986-
"type": [
2987-
"string",
2988-
"array"
2989-
],
2983+
"type": "array",
29902984
"items": {
29912985
"type": "string"
29922986
},

0 commit comments

Comments
 (0)