[wp-trac] [WordPress Trac] #61690: wp_calculate_image_srcset triggers warnings when WP_CONTENT_URL is a relative URL

WordPress Trac noreply at wordpress.org
Thu Jul 18 09:50:09 UTC 2024


#61690: wp_calculate_image_srcset triggers warnings when WP_CONTENT_URL is a
relative URL
--------------------------+-----------------------------
 Reporter:  mattraines    |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Upload        |    Version:  6.6
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 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
 <?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
 <?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
 <?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'
 );
             }
         }
     }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/61690>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list