Make WordPress Core

Opened 5 months ago

Closed 4 months ago

#61690 closed defect (bug) (fixed)

wp_calculate_image_srcset triggers warnings when WP_CONTENT_URL is a relative URL

Reported by: mattraines's profile mattraines Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 6.6.2 Priority: normal
Severity: normal Version: 6.6
Component: Upload Keywords: has-patch fixed-major dev-reviewed commit
Focuses: Cc:

Description

I'm aware that this is unsupported and WP_CONTENT_URL should be an absolute URL including protocol and hostname, but we like to use a path starting with / as it makes our data more portable for testing and development.

The warning is triggered by

<?php
$domain = $parsed['host'];

PHP Warning: Undefined array key "host" in .../wp-includes/media.php on line 1372

The warnings are new in 6.6.0.

Suggested fix:

Either

<?php
 if ( is_ssl() && str_contains(':') && ! str_starts_with( $image_baseurl, 'https' ) ) {
        // Since the `Host:` header might contain a port we should
        // compare it against the image URL using the same port.
        $parsed = parse_url( $image_baseurl );
        $domain = $parsed['host'];
        if ( isset( $parsed['port'] ) ) {
            $domain .= ':' . $parsed['port'];
        }
        if ( $_SERVER['HTTP_HOST'] === $domain ) {
            $image_baseurl = set_url_scheme( $image_baseurl, 'https' );
        }
    }

or

<?php
 if ( is_ssl() && ! str_starts_with( $image_baseurl, 'https' ) ) {
        // Since the `Host:` header might contain a port we should
        // compare it against the image URL using the same port.
        if ( $parsed = parse_url( $image_baseurl ) ) {
            $domain = $parsed['host'];
            if ( isset( $parsed['port'] ) ) {
                $domain .= ':' . $parsed['port'];
            }
            if ( $_SERVER['HTTP_HOST'] === $domain ) {
                $image_baseurl = set_url_scheme( $image_baseurl, 'https' );
            }
        }
    }

Change History (8)

#1 @mattraines
5 months ago

I have a workaround if anybody else is stung by this:

<?php
// wp_calculate_image_srcset does not work with relative WP_CONTENT_URL because it checks for
// URLs not beginning with https:// to determine whether to upgrade the paths to SSL.
// Add the site URL to the baseurl for uploads but only if called from wp_calculate_image_size.
// This allows us to continue using relative URLs elsewhere but does have the two downsides that:
//  1. it is rather slow
//  2. rendered HTML now includes absolute URLs
add_filter('upload_dir', function($upload_dir){
    if (in_array('wp_calculate_image_srcset', array_column(debug_backtrace(), 'function'))) {
        if (!str_contains($upload_dir['baseurl'], '://')) {
            $upload_dir['baseurl'] =
                trailingslashit(get_option('siteurl'))
                . ltrim($upload_dir['baseurl'], '/');
        }
    }
    return $upload_dir;
});

This ticket was mentioned in PR #7064 on WordPress/wordpress-develop by @narenin.


5 months ago
#2

  • Keywords has-patch added

#3 @SergeyBiryukov
5 months ago

  • Milestone changed from Awaiting Review to 6.7
  • Owner set to SergeyBiryukov
  • Status changed from new to reviewing

#4 @SergeyBiryukov
5 months ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 58773:

Media: Check if content URL includes a hostname in wp_calculate_image_srcset().

This resolves an Undefined array key "host" PHP warning if WP_CONTENT_URL is set to a relative URL.

Follow-up to [58097].

Props mattraines, narenin, pamprn, SergeyBiryukov.
Fixes #61690.

#5 @SergeyBiryukov
5 months ago

  • Keywords fixed-major added
  • Milestone changed from 6.7 to 6.6.2
  • Resolution fixed deleted
  • Status changed from closed to reopened

Hi there, thanks for the ticket! Reopening for 6.6.2 consideration.

Last edited 5 months ago by SergeyBiryukov (previous) (diff)

@SergeyBiryukov commented on PR #7064:


5 months ago
#6

Thanks for the PR! Merged in r58773.

#7 @hellofromTonya
4 months ago

  • Keywords dev-reviewed commit added

[58773] LGTM for backport to the 6.6 branch for 6.6.2.

#8 @hellofromTonya
4 months ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 58862:

Media: Check if content URL includes a hostname in wp_calculate_image_srcset().

This resolves an Undefined array key "host" PHP warning if WP_CONTENT_URL is set to a relative URL.

Follow-up to [58097].

Reviewed by hellofromTonya.
Merges [58773] to the 6.6 branch.

Props mattraines, narenin, pamprn, SergeyBiryukov.
Fixes #61690.

Note: See TracTickets for help on using tickets.