Repository navigation
Template Part: Fix boolean operator in description fallback - #86102
westonruter merged 14 commits into
Conversation
🤖 PR meta 🤖🎉 PropsIf 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:
Updated as activity occurs, without notifying anyone named here. Add the |
72d84b8 to
6dcbc53
Compare
westonruter
left a comment
There was a problem hiding this comment.
This makes sense. It was seemingly committed thinking of the behavior in JavaScript, but PHP behaves differently: https://3v4l.org/ihhLU#v8.5.11
Resolves the conflict in tools/phpstan/baseline.neon by regenerating it with `composer phpstan:baselines` on the merged tree. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: f6bd014a-b8bb-45fb-a7bb-f386806f2350
…ack. The description is rendered as React text, not HTML, so the ` ` entity would be shown literally as six characters in the block card and inserter preview of any template part without a description, and would also make every such template part match an inserter search for "nbsp". A U+00A0 character keeps the value non-falsy, which is what prevents the fallback to the block's own description, while rendering as blank. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: f6bd014a-b8bb-45fb-a7bb-f386806f2350
|
🤖 Comment by Claude Opus 5.5 · medium effort Two commits were pushed to this branch:
ReviewThe fix is correct in intent: 1. The
|
westonruter
left a comment
There was a problem hiding this comment.
Based on Claude's findings, I'm changing to request changes.
…escription fallback
|
I have added the unit test coverage and changelog entry per the review findings:
|
|
I have pushed an update addressing the CI failures:
|
Deepak-Bhojwani
left a comment
There was a problem hiding this comment.
Tested this locally against current trunk (6504787).
The branch conflicts with trunk in tools/phpstan/baseline.neon and packages/block-library/CHANGELOG.md. To test, I merged it into trunk, kept trunk's versions of both files, and ran composer phpstan:baselines:
- The only change to the baseline from
trunkis the removal of thebooleanOr.rightAlwaysTrueentry fortemplate-part/index.php, with no new errors. composer phpstanthen passes with no errors and no unmatched ignored errors.
On the code:
'' !== trim( $template_part->description )is safe without a cast, sinceWP_Block_Template::$descriptionis declared asstringwith a default of''.$template_part->area ?? ''is justified, sinceWP_Block_Template::$areaisstring|null.
One point from the earlier review that still seems open is #3: the description now reaches the client for the first time, but unlike the title, it is not passed through decodeEntities() in packages/block-library/src/template-part/index.js. A description like Header & navigation would be shown with the literal entity. That could be handled here or in a follow-up.
The CHANGELOG.md entry will also need to be moved when resolving the conflict, since new entries have been added under Bug Fixes on trunk.
Environment: Windows 11, PHP 8.2.29, Composer 2.11.1, stubs generated from wordpress-develop@27307a20f9ba. I did not run the new PHPUnit test, since I don't have wp-env available here.
Resolve the conflict in tools/phpstan/baseline.neon by keeping the removals from both sides: trunk dropped the navigation, CSS rules store, and style engine entries, while this branch dropped the template part entry. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: ce1705f2-5544-4c8e-842a-9ada9a5d974c
The template part variation description test adds the theme support, but the base test case's tear_down() does not restore theme features, so the support leaked into every later test in the run. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: ce1705f2-5544-4c8e-842a-9ada9a5d974c
The @ticket annotation refers to a Core Trac ticket, so a Gutenberg PR number there points at an unrelated ticket. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: ce1705f2-5544-4c8e-842a-9ada9a5d974c
The fallback to Core's unprefixed build_template_part_block_instance_variations() could only run when the plugin's build is missing, and then it would test Core's copy, which still has the buggy || operator, instead of the plugin's code. Calling the gutenberg_-prefixed function directly makes a missing build fail loudly, matching the other tests in phpunit/blocks. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: ce1705f2-5544-4c8e-842a-9ada9a5d974c
Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: ce1705f2-5544-4c8e-842a-9ada9a5d974c
The description check trims the value, so a description made only of whitespace also falls back to a non-breaking space. Cover that case, and assert that wp_insert_post() stores the whitespace excerpt unchanged, so the test keeps exercising the trim. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: ce1705f2-5544-4c8e-842a-9ada9a5d974c
Say what changes for users, that template part descriptions now show in the inserter, rather than describing the boolean operator in the code. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: ce1705f2-5544-4c8e-842a-9ada9a5d974c
What?
See #86088
Replaces the boolean
||operator with a ternary check inbuild_template_part_block_instance_variations()for the template part description fallback, and removes the resolved entry from the PHPStan baseline.Why?
In PHP,
'description' => $template_part->description || ' 'evaluates the right side of||as a boolean operator, resulting intrue(a boolean) rather than the template part's actual description or the non-breaking space fallback string. PHPStan flags this withbooleanOr.rightAlwaysTrue.Using a proper ternary fallback
! empty( $template_part->description ) ? $template_part->description : ' 'preserves the actual description string when available and only falls back to' 'when empty. This resolves 1 of the errors initially baselined in #66693.How?
packages/block-library/src/template-part/index.phpto use! empty( $template_part->description ) ? $template_part->description : ' '.booleanOr.rightAlwaysTrueentry forpackages/block-library/src/template-part/index.phpfromtools/phpstan/baseline.neon.Testing Instructions
composer phpstanand confirm it passes with no unmatched ignored errors.composer phpstan:baselinesand confirmtools/phpstan/baseline.neonhas no further changes.Testing Instructions for Keyboard
Not applicable. There are no UI changes.
Screenshots or screencast
Not applicable.
Use of AI Tools
I used Claude Code to identify the affected occurrence and draft the change. I reviewed the diff and take responsibility for it.