[wp-trac] [WordPress Trac] #58535: Undefined array key "file" in function wp_calculate_image_srcset()

WordPress Trac noreply at wordpress.org
Wed Oct 30 21:26:14 UTC 2024


#58535: Undefined array key "file" in function wp_calculate_image_srcset()
--------------------------+-----------------------------
 Reporter:  martinjhenne  |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Future Release
Component:  General       |     Version:
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:
--------------------------+-----------------------------

Comment (by debarghyabanerjee):

 This is strange, as there is a check :


 {{{
 $image_meta = apply_filters( 'wp_calculate_image_srcset_meta',
 $image_meta, $size_array, $image_src, $attachment_id );

 if ( empty( $image_meta['sizes'] ) || ! isset( $image_meta['file'] ) ||
 strlen( $image_meta['file'] ) < 4 ) {
         return false;
 }
 }}}

 I went through the core codebase and checked all the instances of the
 $image_meta['file'], and in every function it's checking if the array key
 `file` is defined or not, except one function, and I am working on a patch
 for it.

 Ideally, in `wp_calculate_image_srcset` function, this check shouldn't let
 it pass if there is an undefined array key `file` in `$image_meta`,
 @martinjhenne I wonder why you are getting the warnings.

 Could this be coming from the `$image_meta['sizes']`, which we are looping
 through and then trying to access the `file` of each size?


 {{{
 foreach ( $image_sizes as $image ) {
         $is_src = false;

         // Check if image meta isn't corrupted.
         if ( ! is_array( $image ) ) {
                 continue;
         }

         // If the file name is part of the `src`, we've confirmed a match.
         if ( ! $src_matched && str_contains( $image_src, $dirname .
 $image['file'] ) ) {
                 $src_matched = true;
                 $is_src      = true;
         }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/58535#comment:6>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list