Skip to content

Conversation

sarthak-19
Copy link
Contributor

Fix: #2191

Problem

When users disable responsive image srcset generation via add_filter('wp_calculate_image_srcset', '__return_false'), the picture element functionality was broken - no <source> elements were being generated for modern image formats like AVIF and WebP.

This resulted in the picture element only containing the fallback <img> tag, defeating the purpose of serving modern image formats.

Root Cause

The picture element generation logic only had two paths:

  1. With srcset: Generate responsive <source> elements with full srcset attributes
  2. Without srcset/sizes: Generate simple <source> elements using webp_uploads_get_mime_type_image()

However, there was a gap when the IMG tag had srcset and sizes attributes, but wp_calculate_image_srcset() returned false. In this case, the code attempted the first path but failed silently when srcset generation was disabled.

…is disabled

- Add fallback to create simple source elements without srcset when wp_calculate_image_srcset returns false
- - Enhance test coverage to verify source elements are created when srcset generation is disabled
Copy link

codecov bot commented Oct 5, 2025

Codecov Report

❌ Patch coverage is 0% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 68.62%. Comparing base (88bda34) to head (de0278b).
⚠️ Report is 37 commits behind head on trunk.

Files with missing lines Patch % Lines
plugins/webp-uploads/picture-element.php 0.00% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            trunk    #2201      +/-   ##
==========================================
- Coverage   68.68%   68.62%   -0.06%     
==========================================
  Files          90       90              
  Lines        8093     8100       +7     
==========================================
  Hits         5559     5559              
- Misses       2534     2541       +7     
Flag Coverage Δ
multisite 68.62% <0.00%> (-0.06%) ⬇️
single 35.38% <0.00%> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

is_string( $sizes ) ? sprintf( ' sizes="%s"', esc_attr( $sizes ) ) : ''
);
} else {
// Fallback when wp_calculate_image_srcset returns false - generate simple source without srcset.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SOURCE still has a srcset (as it can't have src).

Suggested change
// Fallback when wp_calculate_image_srcset returns false - generate simple source without srcset.
// Fallback when wp_calculate_image_srcset returns false - generate simple non-responsive srcset.

$this->assertStringContainsString( '.webp', $picture_processor->get_attribute( 'srcset' ), 'Make sure the Picture source srcset should return WEBP images.' );
}

// Ensure at least one source element was created (addresses GitHub issue with wp_calculate_image_srcset disabled).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be nice to add a separate test that sets the wp_calculate_image_srcset filter to return false and verifies the source generation.

@adamsilverstein
Copy link
Member

adamsilverstein commented Oct 8, 2025

@sarthak-19 - thanks for the PR! Can you check this comment - #2191 (comment)

Can you share the HTML you are seeing output Before and After your patch, this will help in understanding the nature of the problem more clearly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Modern Image Formats - wp_calculate_image_srcset => false => no SOURCE element

3 participants