Skip to content

Editor: Guard PostViewLink against post types without a labels object#79160

Merged
Mamaduka merged 4 commits into
WordPress:trunkfrom
Mayank-Tripathi32:fix/62918-post-view-link-undefined-labels
Jun 16, 2026
Merged

Editor: Guard PostViewLink against post types without a labels object#79160
Mamaduka merged 4 commits into
WordPress:trunkfrom
Mayank-Tripathi32:fix/62918-post-view-link-undefined-labels

Conversation

@Mayank-Tripathi32

@Mayank-Tripathi32 Mayank-Tripathi32 commented Jun 12, 2026

Copy link
Copy Markdown
Member

Do not merge yet, largely still testing.

What?

Closes #62918

Stops PostViewLink from crashing when the current post type is registered without a labels object (e.g. wp_template_part, or custom post types declared without the labels arg). The button now falls back to the default View post label instead.

Why?

packages/editor/src/components/post-view-link/index.js:27 read postType?.labels.view_item. The ?. only short-circuits on postType, so if postType is defined but postType.labels is undefined, accessing .view_item throws TypeError: Cannot read properties of undefined. The reporter hit this on a custom theme; @JNBG also identified wp_template_part as a real-world post type that lacks labels / supports.

How?

  • PostViewLink: extend the optional chain to postType?.labels?.view_item so a missing labels object falls through to the existing || __( 'View post' ) fallback.
  • Add a unit test suite for PostViewLink (none existed) covering the regression and the happy path / hidden states.
  • Add a regression test in PostTypeSupportCheck confirming it already returns null (via the supports = {} default) when the post type has no supports argument — this was the second symptom in the report.

Testing Instructions

  1. npm run test:unit packages/editor/src/components/post-view-link
  2. npm run test:unit packages/editor/src/components/post-type-support-check
  3. Manual: in a plugin, register a post type without the labels argument, 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

  • Yes, Claude

@github-actions github-actions Bot added the [Package] Editor /packages/editor label Jun 12, 2026
@github-actions

github-actions Bot commented Jun 12, 2026

Copy link
Copy Markdown

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: Mayank-Tripathi32 <[email protected]>
Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: JNBG <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@Mayank-Tripathi32

Copy link
Copy Markdown
Member Author

While working on this I grepped for the same pattern and found the optional chain stops short of labels in a few more places, so they'd hit the same TypeError for a post type whose REST response has no labels:

  • packages/editor/src/components/post-url/index.js (L61) — postType?.labels.view_item
  • packages/block-library/src/post-date/edit.js (L213, L226) — postType?.labels.singular_name
  • packages/block-library/src/post-featured-image/edit.js (L269, L273) — postType?.labels.singular_name
  • packages/editor/src/components/post-card-panel/index.js (L171) — labels?.name.toLowerCase() (labels is guarded but .name isn't)

For context on when labels is actually missing: it's only exposed on the post-types REST schema in the edit context, so it's undefined whenever a type renders in the editor without its edit-context labels (custom/re-registered post types, capability edge cases) — i.e. the same situation reported here. Stock post/page with full edit caps are unaffected, which is why these don't surface on the common path.

I can extend this PR to guard all of them (it's the same one-character ?. fix in each), once we confirm path forward as these are not affecting directly.

@Mamaduka Mamaduka added the [Type] Bug An existing feature does not function as intended label Jun 13, 2026
Comment on lines +27 to +31
// 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,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think the inline comment here isn't needed.

Comment on lines +16 to +20
jest.mock( '@wordpress/data/src/components/use-select', () => {
// Allow tweaking the returned data on each test.
const mock = jest.fn();
return mock;
} );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We can just return jest.fn(), no need for variable assignment or inline comment.

Comment on lines +52 to +54
// 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Similarly, we can drop the inline comment and just link to the issue.

Comment on lines +58 to +60
// Regression test for #62918: some custom post types (e.g.
// `wp_template_part`) are registered without a `supports` argument,
// so `postType.supports` is undefined.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.
@Mayank-Tripathi32

Copy link
Copy Markdown
Member Author

While working on this I grepped for the same pattern and found the optional chain stops short of labels in a few more places, so they'd hit the same TypeError for a post type whose REST response has no labels:

  • packages/editor/src/components/post-url/index.js (L61) — postType?.labels.view_item
  • packages/block-library/src/post-date/edit.js (L213, L226) — postType?.labels.singular_name
  • packages/block-library/src/post-featured-image/edit.js (L269, L273) — postType?.labels.singular_name
  • packages/editor/src/components/post-card-panel/index.js (L171) — labels?.name.toLowerCase() (labels is guarded but .name isn't)

For context on when labels is actually missing: it's only exposed on the post-types REST schema in the edit context, so it's undefined whenever a type renders in the editor without its edit-context labels (custom/re-registered post types, capability edge cases) — i.e. the same situation reported here. Stock post/page with full edit caps are unaffected, which is why these don't surface on the common path.

I can extend this PR to guard all of them (it's the same one-character ?. fix in each), once we confirm path forward as these are not affecting directly.

@Mamaduka I have addressed the comments, do you have any input on this?

@Mamaduka

Copy link
Copy Markdown
Member

@Mayank-Tripathi32, those need to be checked case by case. I viewed a couple of listed files, and they're calling getPostType( postType ) and those requests are made in context=edit by default.

  • labels - even if the post type doesn't declare custom labels, default values will be provided.
  • support - similar here, post type will have more than one flag if it's available in the editor and via the REST API.

Happy to check again, if you've reproduction steps on how those components can trigger an error for a user.

@github-actions github-actions Bot added the [Package] Block library /packages/block-library label Jun 15, 2026
@Mamaduka

Copy link
Copy Markdown
Member

I guess extra checks don't cost anything, but as I said, I don't think they're needed without actual reproduction steps.

@Mamaduka Mamaduka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's merge this. If you find more cases like this, please create an issue with reproduction steps before moving forward.

Thanks, @Mayank-Tripathi32!

@Mamaduka Mamaduka merged commit 6cf51dc into WordPress:trunk Jun 16, 2026
42 checks passed
@github-actions github-actions Bot added this to the Gutenberg 23.5 milestone Jun 16, 2026
@Mayank-Tripathi32

Mayank-Tripathi32 commented Jun 16, 2026

Copy link
Copy Markdown
Member Author

Thanks.

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

Labels

[Package] Block library /packages/block-library [Package] Editor /packages/editor [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PostViewLink Component breaks on undefined labels

2 participants