Skip to content

Commit

Permalink
Don't update the image src if we don't have an image URL. Better fall…
Browse files Browse the repository at this point in the history
…back if we can't find a resized image
  • Loading branch information
dkotter committed Oct 11, 2024
1 parent 49d66e6 commit 3778154
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions includes/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,10 @@ function update_image_tag( string $content, array $media_item, int $image_id ) {
$classes = explode( ' ', $processor->get_attribute( 'class' ) ?? ' ' );

// Only process the image that matches the old ID.
if ( ! is_array( $classes ) || ! in_array( 'wp-image-' . $media_item['id'], $classes, true ) ) {
if (
! is_array( $classes ) ||
! in_array( 'wp-image-' . $media_item['id'], $classes, true )
) {
continue;
}

Expand All @@ -1078,7 +1081,19 @@ function ( $image_class ) {
$image_size = str_replace( 'size-', '', $size_class );
}

$processor->set_attribute( 'src', wp_get_attachment_image_url( $image_id, $image_size ) );
$src = wp_get_attachment_image_url( $image_id, $image_size );

// If the image size can't be found, try to get the full size.
if ( ! $src ) {
$src = wp_get_attachment_image_url( $image_id, 'full' );

// If we still don't have an image, skip this block.
if ( ! $src ) {
continue;
}
}

$processor->set_attribute( 'src', $src );
$processor->add_class( 'wp-image-' . $image_id );
$processor->remove_class( 'wp-image-' . $media_item['id'] );
$processor->remove_attribute( 'srcset' );
Expand Down Expand Up @@ -1114,7 +1129,19 @@ function update_image_block( array $blocks, array $media_item, int $image_id ) {

// Use the HTML API to update the image src and class.
if ( $processor->next_tag( 'img' ) ) {
$processor->set_attribute( 'src', wp_get_attachment_image_url( $image_id, $image_size ) );
$src = wp_get_attachment_image_url( $image_id, $image_size );

// If the image size can't be found, try to get the full size.
if ( ! $src ) {
$src = wp_get_attachment_image_url( $image_id, 'full' );

// If we still don't have an image, skip this block.
if ( ! $src ) {
continue;
}
}

$processor->set_attribute( 'src', $src );
$processor->add_class( 'wp-image-' . $image_id );
$processor->remove_class( 'wp-image-' . $media_item['id'] );

Expand Down

0 comments on commit 3778154

Please sign in to comment.