Media: Support cropping remote images when URL fopen is disabled - #12751
Media: Support cropping remote images when URL fopen is disabled#12751ArkaPrabhaChowdhury wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates WordPress’s image cropping flow to reliably support cropping remote images even when allow_url_fopen is disabled, by routing remote fetches through the WordPress HTTP API and making the related PHPUnit test deterministic.
Changes:
- Download remote crop sources via
download_url()(HTTP API) when the source is a valid URL. - Delete the temporary downloaded source file after the image editor has loaded it.
- Replace the previous external-network-dependent crop test with an HTTP mock using
pre_http_request.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/wp-admin/includes/image.php |
Adds URL detection + download-to-temp logic before loading the image editor, then cleans up the temp file. |
tests/phpunit/tests/image/functions.php |
Updates the URL-cropping test to use a deterministic HTTP mock instead of fetching a real remote image. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| $mock_image_download = static function ( $response, $args ) { | ||
| copy( DIR_TESTDATA . '/images/canola.jpg', $args['filename'] ); | ||
|
|
||
| return array( | ||
| 'response' => array( | ||
| 'code' => 200, | ||
| 'message' => 'OK', | ||
| ), | ||
| 'headers' => array(), | ||
| 'cookies' => array(), | ||
| 'body' => '', | ||
| ); | ||
| }; | ||
|
|
||
| add_filter( 'pre_http_request', $mock_image_download, 10, 2 ); | ||
|
|
| @@ -653,9 +670,7 @@ public function test_wp_crop_image_with_url() { | |||
| DIR_TESTDATA . '/images/' . __FUNCTION__ . '.png' | |||
| ); | |||
|
|
|||
| if ( is_wp_error( $file ) && $file->get_error_code() === 'invalid_image' ) { | |||
| $this->markTestSkipped( 'Tests_Image_Functions::test_wp_crop_image_url() cannot access remote image.' ); | |||
| } | |||
| remove_filter( 'pre_http_request', $mock_image_download, 10 ); | |||
|
|
|||
| if ( wp_http_validate_url( $src ) ) { | ||
| if ( ! function_exists( 'download_url' ) ) { | ||
| require_once ABSPATH . 'wp-admin/includes/file.php'; | ||
| } | ||
|
|
||
| $tmp_file = download_url( $src ); | ||
| if ( is_wp_error( $tmp_file ) ) { | ||
| return $tmp_file; | ||
| } | ||
|
|
||
| $src = $tmp_file; | ||
| } |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Trac ticket: https://core.trac.wordpress.org/ticket/42064
Summary
Testing