Skip to content

Media: Support cropping remote images when URL fopen is disabled - #12751

Draft
ArkaPrabhaChowdhury wants to merge 2 commits into
WordPress:trunkfrom
ArkaPrabhaChowdhury:trac-42064-crop-image-without-url-fopen
Draft

Media: Support cropping remote images when URL fopen is disabled#12751
ArkaPrabhaChowdhury wants to merge 2 commits into
WordPress:trunkfrom
ArkaPrabhaChowdhury:trac-42064-crop-image-without-url-fopen

Conversation

@ArkaPrabhaChowdhury

Copy link
Copy Markdown

Trac ticket: https://core.trac.wordpress.org/ticket/42064

Summary

  • download remote crop sources through the WordPress HTTP API
  • remove the temporary source file after the image editor loads it
  • replace the external-network crop test with a deterministic HTTP mock

Testing

  • PHP lint for both changed files
  • PHPCBF and PHPCS for both changed files
  • focused PHPUnit with allow_url_fopen=0: 1 test, 4 assertions
  • Tests_Image_Functions: 113 tests, 211 assertions

Copilot AI review requested due to automatic review settings July 29, 2026 08:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread tests/phpunit/tests/image/functions.php Outdated
Comment on lines +645 to +660
$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 );

Comment thread tests/phpunit/tests/image/functions.php Outdated
Comment on lines 661 to 674
@@ -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 );

Comment on lines +44 to +55
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;
}
@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Copilot AI review requested due to automatic review settings July 29, 2026 08:55

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants