Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ public function get_endpoint_args_for_item_schema( $method = WP_REST_Server::CRE
*
* @since 4.7.0
* @since 6.9.0 Extends the `media_type` and `mime_type` request arguments to support array values.
* @since 7.3.0 Matches the `media_type` and `mime_type` request arguments against the stored type.
*
* @param array $prepared_args Optional. Array of prepared arguments. Default empty array.
* @param WP_REST_Request $request Optional. Request to prepare items for.
Expand All @@ -362,23 +363,24 @@ protected function prepare_items_query( $prepared_args = array(), $request = nul
}

$all_mime_types = array();
$media_types = $this->get_media_types();

if ( ! empty( $request['media_type'] ) && is_array( $request['media_type'] ) ) {
foreach ( $request['media_type'] as $type ) {
if ( isset( $media_types[ $type ] ) ) {
$all_mime_types = array_merge( $all_mime_types, $media_types[ $type ] );
}
}
// WP_Query matches a media type, such as "image", against every stored "image/*" type.
$all_mime_types = $request['media_type'];
}

if ( ! empty( $request['mime_type'] ) && is_array( $request['mime_type'] ) ) {
foreach ( $request['mime_type'] as $mime_type ) {
$parts = explode( '/', $mime_type );
if ( isset( $media_types[ $parts[0] ] ) && in_array( $mime_type, $media_types[ $parts[0] ], true ) ) {
// Only exact types are matched; a value without a subtype or with wildcards matches nothing.
if ( is_string( $mime_type ) && preg_match( '#^[-.a-zA-Z0-9]+/[-+.a-zA-Z0-9]+$#', $mime_type ) ) {
$all_mime_types[] = $mime_type;
}
}

// A MIME type filter without matching values returns no attachments.
if ( empty( $all_mime_types ) ) {
$query_args['post__in'] = array( 0 );
}
}

if ( ! empty( $all_mime_types ) ) {
Expand Down
142 changes: 142 additions & 0 deletions tests/phpunit/tests/rest-api/rest-attachments-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,148 @@ public function test_get_items_mime_type() {
$this->assertSame( $id1, $data[0]['id'] );
}

/**
* Tests that the `mime_type` filter matches the stored type, even when the
* current user cannot upload files of that type.
*
* @ticket 66893
*/
public function test_get_items_mime_type_matches_type_the_user_cannot_upload() {
$html_id = self::factory()->attachment->create_object(
'page.html',
0,
array(
'post_mime_type' => 'text/html',
)
);
self::factory()->attachment->create_object(
self::$test_file,
0,
array(
'post_mime_type' => 'image/jpeg',
)
);

wp_set_current_user( self::$author_id );
$this->assertArrayNotHasKey( 'htm|html', get_allowed_mime_types(), 'The author should not be allowed to upload HTML files.' );

$request = new WP_REST_Request( 'GET', '/wp/v2/media' );
$request->set_param( 'mime_type', array( 'text/html' ) );
$response = rest_get_server()->dispatch( $request );

$this->assertSame( 200, $response->get_status(), 'The request should succeed.' );
$this->assertSame( array( $html_id ), wp_list_pluck( $response->get_data(), 'id' ), 'Only the HTML attachment should be returned.' );
}

/**
* Tests that a `mime_type` filter that matches no attachment returns no items.
*
* @ticket 66893
* @dataProvider data_get_items_mime_type_without_matches
*
* @param string $mime_type MIME type filter value.
*/
public function test_get_items_mime_type_without_matches_returns_no_items( $mime_type ) {
self::factory()->attachment->create_object(
self::$test_file,
0,
array(
'post_mime_type' => 'image/jpeg',
)
);

$request = new WP_REST_Request( 'GET', '/wp/v2/media' );
$request->set_param( 'mime_type', array( $mime_type ) );
$response = rest_get_server()->dispatch( $request );

$this->assertSame( 200, $response->get_status(), 'The request should succeed.' );
$this->assertSame( array(), $response->get_data(), 'No attachments should be returned.' );
}

/**
* Data provider for test_get_items_mime_type_without_matches_returns_no_items().
*
* @return array<string, array{ 0: string }>
*/
public function data_get_items_mime_type_without_matches() {
return array(
'type that cannot be uploaded' => array( 'application/x-unknown' ),
'value without a subtype' => array( 'image' ),
'wildcard subtype' => array( 'image/*' ),
'SQL wildcard' => array( '%' ),
);
}

/**
* Tests that a `mime_type` filter without matches does not affect the
* `media_type` filter it is combined with.
*
* @ticket 66893
*/
public function test_get_items_mime_type_without_matches_and_media_type() {
$jpeg_id = self::factory()->attachment->create_object(
self::$test_file,
0,
array(
'post_mime_type' => 'image/jpeg',
)
);
self::factory()->attachment->create_object(
self::$test_video_file,
0,
array(
'post_mime_type' => 'video/mp4',
)
);

$request = new WP_REST_Request( 'GET', '/wp/v2/media' );
$request->set_param( 'media_type', array( 'image' ) );
$request->set_param( 'mime_type', array( 'application/x-unknown' ) );
$response = rest_get_server()->dispatch( $request );

$this->assertSame( 200, $response->get_status(), 'The request should succeed.' );
$this->assertSame( array( $jpeg_id ), wp_list_pluck( $response->get_data(), 'id' ), 'Only the image attachment should be returned.' );
}

/**
* Tests that the `media_type` filter matches every stored type of the group,
* including types that can no longer be uploaded.
*
* @ticket 66893
*/
public function test_get_items_media_type_matches_type_that_cannot_be_uploaded() {
$jpeg_id = self::factory()->attachment->create_object(
self::$test_file,
0,
array(
'post_mime_type' => 'image/jpeg',
)
);
$legacy_id = self::factory()->attachment->create_object(
'legacy.img',
0,
array(
'post_mime_type' => 'image/x-legacy',
)
);
self::factory()->attachment->create_object(
self::$test_video_file,
0,
array(
'post_mime_type' => 'video/mp4',
)
);

$this->assertNotContains( 'image/x-legacy', get_allowed_mime_types(), 'The legacy type should not be allowed for uploads.' );

$request = new WP_REST_Request( 'GET', '/wp/v2/media' );
$request->set_param( 'media_type', array( 'image' ) );
$response = rest_get_server()->dispatch( $request );

$this->assertSame( 200, $response->get_status(), 'The request should succeed.' );
$this->assertSameSets( array( $jpeg_id, $legacy_id ), wp_list_pluck( $response->get_data(), 'id' ), 'Both image attachments should be returned.' );
}

public function test_get_items_parent() {
$post_id = self::factory()->post->create( array( 'post_title' => 'Test Post' ) );
$attachment_id = self::factory()->attachment->create_object(
Expand Down
Loading