Skip to content

Commit 75c7c45

Browse files
author
KMchaudhary
committed
Fix the url missmatch of virtual image source set
1 parent 43a8cd1 commit 75c7c45

1 file changed

Lines changed: 9 additions & 47 deletions

File tree

inc/classes/class-media-library-ajax.php

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -921,56 +921,18 @@ public function filter_virtual_media_srcset( $sources, $size_array, $image_src,
921921
// Use the current image URL as the base for all subsizes.
922922
$base_url = trailingslashit( untrailingslashit( dirname( $image_src ) ) );
923923

924-
// Skip srcset entirely when the requested size is the thumbnail variant.
925-
if ( ! empty( $image_meta['sizes']['thumbnail'] ) ) {
926-
$thumb_meta = $image_meta['sizes']['thumbnail'];
927-
$is_thumb = false;
928-
929-
if ( isset( $size_array[0], $size_array[1], $thumb_meta['width'], $thumb_meta['height'] )
930-
&& (int) $size_array[0] === (int) $thumb_meta['width']
931-
&& (int) $size_array[1] === (int) $thumb_meta['height']
932-
) {
933-
$is_thumb = true;
934-
} elseif ( isset( $thumb_meta['file'] ) ) {
935-
$thumb_file = $thumb_meta['file'];
936-
$thumb_url = $this->is_valid_url( $thumb_file ) ? $thumb_file : $base_url . ltrim( $thumb_file, '/' );
937-
$is_thumb = ! empty( $image_src ) && $thumb_url === $image_src;
938-
}
939-
940-
if ( $is_thumb ) {
941-
return false;
942-
}
943-
}
944-
945-
// Rebuild the sources array using full URLs.
946-
foreach ( $image_meta['sizes'] as $size_data ) {
947-
if ( empty( $size_data['file'] ) || empty( $size_data['width'] ) ) {
948-
continue;
949-
}
924+
// Rebuild sources array for virtual media.
925+
foreach ( $sources as &$source ) {
950926

951-
$width = (int) $size_data['width'];
927+
// Get last string after the last slash in the file url.
928+
$file_basename = basename( $source['url'] );
929+
930+
// Rebuild the full URL using the base URL and the file basename.
931+
$url = $base_url . ltrim( $file_basename, '/' );
952932

953-
$file = $size_data['file'];
954-
955-
// If the file already is a URL, use it. Otherwise append it to the base URL.
956-
if ( $this->is_valid_url( $file ) ) {
957-
// Get last string after the last slash in the file url.
958-
$file_basename = basename( $file );
959-
// Rebuild the full URL using the base URL and the file basename.
960-
$url = $base_url . ltrim( $file_basename, '/' );
961-
} else {
962-
$url = $base_url . ltrim( $file, '/' );
963-
}
964-
965-
// Override or set the source keyed by width.
966-
$sources[ $width ] = array(
967-
'url' => esc_url_raw( $url ),
968-
'descriptor' => 'w',
969-
'value' => $width,
970-
);
933+
$source['url'] = esc_url( $url );
971934
}
972-
973-
ksort( $sources );
935+
unset( $source ); // Break the reference.
974936

975937
return $sources;
976938
}

0 commit comments

Comments
 (0)