Editor: Guard PostViewLink against post types without a labels object#79160
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the 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. |
|
While working on this I grepped for the same pattern and found the optional chain stops short of
For context on when I can extend this PR to guard all of them (it's the same one-character |
| // Guard against post types that don't expose `labels` (e.g. | ||
| // `wp_template_part` or custom post types registered without | ||
| // the `labels` argument). Falling back to the default | ||
| // translatable label below. | ||
| label: postType?.labels?.view_item, |
There was a problem hiding this comment.
I think the inline comment here isn't needed.
| jest.mock( '@wordpress/data/src/components/use-select', () => { | ||
| // Allow tweaking the returned data on each test. | ||
| const mock = jest.fn(); | ||
| return mock; | ||
| } ); |
There was a problem hiding this comment.
We can just return jest.fn(), no need for variable assignment or inline comment.
| // Regression test for #62918: some custom post types are registered | ||
| // without a `labels` object (e.g. `wp_template_part`). Accessing | ||
| // `labels.view_item` on those used to throw. |
There was a problem hiding this comment.
Similarly, we can drop the inline comment and just link to the issue.
| // Regression test for #62918: some custom post types (e.g. | ||
| // `wp_template_part`) are registered without a `supports` argument, | ||
| // so `postType.supports` is undefined. |
There was a problem hiding this comment.
Same, no inline comment is needed. The test description should be enough.
Remove the inline comments added in this PR per review, and collapse the use-select jest.mock factory to a direct `() => jest.fn()`. No functional change; both test suites still pass.
@Mamaduka I have addressed the comments, do you have any input on this? |
|
@Mayank-Tripathi32, those need to be checked case by case. I viewed a couple of listed files, and they're calling
Happy to check again, if you've reproduction steps on how those components can trigger an error for a user. |
|
I guess extra checks don't cost anything, but as I said, I don't think they're needed without actual reproduction steps. |
Mamaduka
left a comment
There was a problem hiding this comment.
Let's merge this. If you find more cases like this, please create an issue with reproduction steps before moving forward.
Thanks, @Mayank-Tripathi32!
|
Thanks. |
Do not merge yet, largely still testing.
What?
Closes #62918
Stops
PostViewLinkfrom crashing when the current post type is registered without alabelsobject (e.g.wp_template_part, or custom post types declared without thelabelsarg). The button now falls back to the defaultView postlabel instead.Why?
packages/editor/src/components/post-view-link/index.js:27readpostType?.labels.view_item. The?.only short-circuits onpostType, so ifpostTypeis defined butpostType.labelsis undefined, accessing.view_itemthrowsTypeError: Cannot read properties of undefined. The reporter hit this on a custom theme; @JNBG also identifiedwp_template_partas a real-world post type that lackslabels/supports.How?
PostViewLink: extend the optional chain topostType?.labels?.view_itemso a missinglabelsobject falls through to the existing|| __( 'View post' )fallback.PostViewLink(none existed) covering the regression and the happy path / hidden states.PostTypeSupportCheckconfirming it already returns null (via thesupports = {}default) when the post type has nosupportsargument — this was the second symptom in the report.Testing Instructions
npm run test:unit packages/editor/src/components/post-view-linknpm run test:unit packages/editor/src/components/post-type-support-checklabelsargument, then open it in the editor — the View button should display "View post" instead of crashing the editor.Testing Instructions for Keyboard
No interaction changes.
AI