fix: Add fallback sizes to MediaItemSizeEnum when intermediate sizes are disabled #3433
+102
−10
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What bug does this fix? Explain your changes.
The Bug
On WordPress VIP and similar platforms where intermediate image sizes are disabled,
MediaItemSizeEnumwas being registered with zero values. This creates an invalid GraphQL schema because the GraphQL specification requires that enums must define at least one value.Error Message:
This breaks:
Root Cause
The breaking change introduced in commit e5a9fe0 removed fallback support for image sizes. The
get_image_sizes()method now solely relies onget_intermediate_image_sizes(), which returns an empty array on WordPress VIP (and possibly other environments) where intermediate sizes are disabled by platform policy.The Fix
Added fallback logic to ensure
MediaItemSizeEnumalways has at least one value:get_intermediate_image_sizes()returns empty, populate with standard WordPress image sizes:thumbnail,medium,medium_large,large, andfullfullsize (0x0 dimensions since it represents the original uploaded image)wp_get_attachment_image_src()) gracefully resolve at runtimeThis maintains backward compatibility while ensuring schema validity on all platforms.
Does this close any currently open issues?
Closes #3432
Testing Strategy
This PR follows the recommended commit structure:
Commit 1: Failing Test
Commit:
58fc33a- Add test reproducing MediaItemSizeEnum empty values bug on VIPThe test simulates VIP environment by filtering
get_intermediate_image_sizesto return empty array, then performs schema introspection to verifyMediaItemSizeEnumhas at least one value.This commit will show failing tests in CI ❌
Commit 2: Implementation Fix
Commit:
7db8c1bd- Add fallback image sizes to MediaItemSizeEnumImplements the fix by adding fallback logic to
get_image_sizes()method with comprehensive inline documentation.This commit will show passing tests in CI ✅
Test Results
58fc33ademonstrates the bug with a failing test (see: https://github.com/wp-graphql/wp-graphql/actions/runs/18928825922)7db8c1bdresolves the bug and now the tests all pass (see: https://github.com/wp-graphql/wp-graphql/actions/runs/18928894854)Before/After Examples
Before (Buggy Behavior):
On WordPress VIP with no intermediate sizes:
Result: Schema validation error -
Enum type MediaItemSizeEnum must define one or more valuesAfter (Fixed Behavior):
Result:
{ "data": { "__type": { "name": "MediaItemSizeEnum", "enumValues": [ { "name": "THUMBNAIL", "description": "Small image preview suitable for thumbnails and listings. (150x150)" }, { "name": "MEDIUM", "description": "Medium image preview typically suitable for listings and detail views. (300x300)" }, { "name": "MEDIUM_LARGE", "description": "Medium-to-large image preview suitable for listings and detail views. (768x0)" }, { "name": "LARGE", "description": "Large image preview suitable for detail views. (1024x1024)" }, { "name": "FULL", "description": "Full-size image." } ] } } }Additional Context
get_intermediate_image_sizes()returns emptyDocumentation Added
Comprehensive inline comments explain: