Repository navigation
Fix fatal error when passing a float column width to rtrim() - #2725
westonruter wants to merge 13 commits into
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @UfBt12qJNZKcw. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
|
🤖 Comment by Claude Opus 5.5 Origin of the bugThere are two parts: Gutenberg changed the attribute's type, and the plugin later made that change fatal. 1. Gutenberg changed the Column
Block deprecations only run in the editor. Server-side render filters receive the raw stored attributes, so 2. On the plugin side, the The string cast in this PR addresses the cause and not just the symptom. The legacy numeric format always meant a percentage from 0 to 100, so Other faulty assumptions in
|
Block context is not type-coerced, and a third-party block can provide `postId` from a raw attribute via `providesContext`. With strict types, a numeric string there would cause a TypeError when passed to `auto_sizes_get_featured_image_attachment_id()`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Block attributes are not type-coerced on the server, so `align` is passed through exactly as stored in the block comment delimiter. With strict types, a non-string value would cause a TypeError when passed to the `string $align` parameter of `auto_sizes_calculate_better_sizes()`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## trunk #2725 +/- ##
=======================================
Coverage 70.84% 70.84%
=======================================
Files 91 91
Lines 7967 7975 +8
=======================================
+ Hits 5644 5650 +6
- Misses 2323 2325 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
The raw `align` attribute of a container block is copied into the `max_alignment` context, which is later passed to the `string $max_alignment` parameter of `auto_sizes_calculate_better_sizes()`. Block attributes are not type-coerced on the server, so with strict types a non-string value would cause a TypeError. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Test ReportPatch tested: #2725 (head 9784590) Environment
Steps
Results
The AI assistance: Yes. Tool: Claude Code. Used for: scripting the Playground reproduction and drafting this report; I reviewed the results. |
|
@mukeshpanchal27 How does this look? |
mukeshpanchal27
left a comment
There was a problem hiding this comment.
Thanks for the fix. The (string) cast stops the rtrim() TypeError for float widths, and the other casts look safe.
Two notes that sit outside the diff lines:
- The PHPDoc array shape near line 81 still says
width?: string. It should bewidth?: string|float, so PHPStan matches the new comment. - This bug existed before the PR. The range guards at lines 232, 258 and 381 use
$x > 0.0 || $x < 1.0, which is true for every number. They should use&&. Because of this, a width of 0 or a width above 1 goes straight into the pixel calculation. This could be a follow-up PR.
| if ( isset( $block['attrs']['width'] ) && '' !== $block['attrs']['width'] ) { | ||
| $current_width = floatval( rtrim( $block['attrs']['width'], '%' ) ) / 100; | ||
| // Note: In old versions of the Columns block, the percentage width may be a float. So the string cast is required. | ||
| $current_width = floatval( rtrim( (string) $block['attrs']['width'], '%' ) ) / 100; |
There was a problem hiding this comment.
Could we add a regression test that renders a core/column with a numeric width such as 33.33? Without a test, the TypeError could come back in a later refactor.
A smaller point: on PHP 7.4, (string) 33.33 depends on the locale and can return 33,33. Then floatval() returns 33. One option is to skip the string round-trip for numbers:
$width = $block['attrs']['width'];
$current_width = ( is_numeric( $width ) ? (float) $width : floatval( rtrim( (string) $width, '%' ) ) ) / 100;Also, the Column block allows px, em, rem and vw widths. A 250px width becomes 2.5 here. This problem existed before the PR, but it may be worth a TODO.
| if ( in_array( $block['blockName'], $provider_blocks, true ) ) { | ||
| // Normalize default alignment values. | ||
| $alignment = isset( $block['attrs']['align'] ) && '' !== $block['attrs']['align'] ? $block['attrs']['align'] : 'default'; | ||
| $alignment = isset( $block['attrs']['align'] ) && '' !== $block['attrs']['align'] ? (string) $block['attrs']['align'] : 'default'; |
There was a problem hiding this comment.
The cast makes the type a string, but it does not check the value against AUTO_SIZES_CONSTRAINTS. A value like 'none' still causes an "undefined array key" warning when $constraints[ $alignment ] is read. Should we fall back to 'default' when the key is missing? The same applies to the image block align at line 106.
| $alignment = $block->attributes['align'] ?? ''; | ||
| $alignment = (string) ( $block->attributes['align'] ?? '' ); | ||
| $width = isset( $block->attributes['width'] ) ? (int) $block->attributes['width'] : 0; | ||
| $max_alignment = $block->context['max_alignment'] ?? ''; |
There was a problem hiding this comment.
max_alignment and container_relative_width are passed into typed string and float parameters without a cast. A plugin that filters the block context could still cause a TypeError here. Should we cast them too, so this PR covers them as well?
Old versions of the Columns block store the column `width` as a number, such as `66.66`, instead of a string like `66.66%`. Passing that number to `rtrim()` caused a TypeError. This test renders such a column so the error does not come back. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On PHP 7.4, casting a float to string uses the current locale. With a locale such as de_DE, `(string) 33.33` becomes `33,33`, and `floatval()` then returns `33`. Numeric widths are now cast to float directly, and only string widths go through `rtrim()`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The Column block also allows `px`, `em`, `rem` and `vw` widths. The old code stripped only `%`, so a `250px` width became a relative width of 2.5, and the `sizes` value was 2.5 times too large. Only numeric and percentage widths are now parsed. Other units use the equally divided width, the same as a column with no width set. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Casting `align` to string fixes the type, but a value such as `none` is not a key in `AUTO_SIZES_CONSTRAINTS`. Reading that key raised an "undefined array key" warning, and the comparison then used null. Unknown `align` values now fall back to `default`, and an unknown `max_alignment` falls back to `full`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`max_alignment` and `container_relative_width` come from block context, which plugins can filter with any value. They were passed to the typed `string` and `float` parameters of `auto_sizes_calculate_better_sizes()` without a check, so an invalid value caused a TypeError. Invalid values now fall back to an empty max alignment and a relative width of 1.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The image block `width` attribute is a string in current versions of the block, but older versions store it as a number. The shape now allows both. It also lists the `id` and `sizeSlug` attributes that the function reads, so PHPStan checks them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The checks used `$width > 0.0 || $width < 1.0`, which is true for every number. A column width of `0%` gave `sizes="(max-width: 0px) 100vw, 0px"`, and a width of `150%` gave a value larger than the layout. Column widths outside the range of 0 to 100% now fall back to the full container width. The sizes calculation only scales the layout width when the relative width is above 0 and at most 1. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
🤖 Comment by Claude Opus 5.5 @westonruter I pushed a few follow-up commits to this branch, based on my review:
Each fix is in its own commit with a test. Please take a look and let me know if you would like any of these changes done differently. |
Summary
Fixes #2724
Use of AI Tools
None initially, but then review and additional commits provided by Claude Opus 5.5.