Skip to content

Commit 9a568b7

Browse files
authored
Merge branch 'WordPress:trunk' into trunk
2 parents ca3a0cb + 58b28ff commit 9a568b7

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,14 @@ protected function create_item_from_url( $request ) {
641641
'tmp_name' => $tmp_file,
642642
);
643643

644+
$size_check = self::check_upload_size( $file_array );
645+
if ( is_wp_error( $size_check ) ) {
646+
if ( file_exists( $tmp_file ) ) {
647+
wp_delete_file( $tmp_file );
648+
}
649+
return $size_check;
650+
}
651+
644652
$attachment_id = media_handle_sideload( $file_array, $post_id );
645653

646654
if ( is_wp_error( $attachment_id ) ) {

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5330,6 +5330,70 @@ public function test_create_item_from_url_returns_error_on_download_failure() {
53305330
$this->assertSame( 500, $response->get_status() );
53315331
}
53325332

5333+
/**
5334+
* Verifies that the URL sideload path enforces the multisite maximum file
5335+
* size, for parity with the multipart and raw-body upload paths.
5336+
*
5337+
* @ticket 65517
5338+
* @group multisite
5339+
* @group ms-required
5340+
*
5341+
* @covers WP_REST_Attachments_Controller::create_item_from_url
5342+
* @covers WP_REST_Attachments_Controller::check_upload_size
5343+
*/
5344+
public function test_create_item_from_url_exceeds_multisite_max_filesize() {
5345+
$this->enable_client_side_media_processing();
5346+
5347+
wp_set_current_user( self::$superadmin_id );
5348+
update_site_option( 'fileupload_maxk', 1 );
5349+
update_site_option( 'upload_space_check_disabled', false );
5350+
5351+
// Ensure ample space is available so the file-size limit is what rejects it.
5352+
add_filter( 'pre_get_space_used', '__return_zero' );
5353+
add_filter( 'pre_http_request', array( $this, 'mock_image_download' ), 10, 3 );
5354+
5355+
$request = new WP_REST_Request( 'POST', '/wp/v2/media' );
5356+
$request->set_param( 'url', 'https://example.com/too-big.jpg' );
5357+
$request->set_param( 'generate_sub_sizes', false );
5358+
5359+
$response = rest_get_server()->dispatch( $request );
5360+
5361+
remove_filter( 'pre_http_request', array( $this, 'mock_image_download' ), 10 );
5362+
5363+
$this->assertErrorResponse( 'rest_upload_file_too_big', $response, 400 );
5364+
}
5365+
5366+
/**
5367+
* Verifies that the URL sideload path enforces the multisite site upload
5368+
* space quota, for parity with the multipart and raw-body upload paths.
5369+
*
5370+
* @ticket 65517
5371+
* @group multisite
5372+
* @group ms-required
5373+
*
5374+
* @covers WP_REST_Attachments_Controller::create_item_from_url
5375+
* @covers WP_REST_Attachments_Controller::check_upload_size
5376+
*/
5377+
public function test_create_item_from_url_exceeds_multisite_site_upload_space() {
5378+
$this->enable_client_side_media_processing();
5379+
5380+
wp_set_current_user( self::$superadmin_id );
5381+
add_filter( 'get_space_allowed', '__return_zero' );
5382+
update_site_option( 'upload_space_check_disabled', false );
5383+
5384+
add_filter( 'pre_http_request', array( $this, 'mock_image_download' ), 10, 3 );
5385+
5386+
$request = new WP_REST_Request( 'POST', '/wp/v2/media' );
5387+
$request->set_param( 'url', 'https://example.com/no-space.jpg' );
5388+
$request->set_param( 'generate_sub_sizes', false );
5389+
5390+
$response = rest_get_server()->dispatch( $request );
5391+
5392+
remove_filter( 'pre_http_request', array( $this, 'mock_image_download' ), 10 );
5393+
5394+
$this->assertErrorResponse( 'rest_upload_limited_space', $response, 400 );
5395+
}
5396+
53335397
/**
53345398
* Verifies that a URL with no usable path bails with a 400 before any
53355399
* download is attempted, rather than handing an empty filename to the

0 commit comments

Comments
 (0)