Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template Part block: Guard against get_block_file_template returning null #69309

Merged
merged 1 commit into from
Feb 25, 2025

Conversation

ockham
Copy link
Contributor

@ockham ockham commented Feb 25, 2025

What?

Add a guard to ensure $block_template->content exists before assigning its value to a variable.

Why?

The Template Part PHP code uses get_block_file_template() to fetch information about the given template part from the corresponding theme's block template file. However, that function can return null, and the code currently guard against that when attempting to get the returned $block_template object's ->content field.

How?

See above.

By keeping $content set to its initial null, a later check in the Template Part block code will issue a more user-friendly warning.

Testing Instructions

  • On a site that uses a block theme (e.g. TT5), open the Site Editor, and switch to the code editor.
  • Insert the block markup for a Post Template block that references a template part that doesn't exist (see below for an example).
  • Save the template and view the frontend. The error message should match the "After" screenshot further below.
<!-- wp:template-part {"slug":"post-meta"} /-->

Screenshots or screencast

Before After
image image

@ockham ockham added [Type] Bug An existing feature does not function as intended [Block] Template Part Affects the Template Parts Block labels Feb 25, 2025
@ockham ockham self-assigned this Feb 25, 2025
Copy link
Contributor

@paulopmt1 paulopmt1 left a comment

Choose a reason for hiding this comment

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

LGTM.

@ockham
Copy link
Contributor Author

ockham commented Feb 25, 2025

The "new" error message matches what the user sees in the editor:

image

@ockham ockham force-pushed the fix/template-part-block branch from 35821a7 to 1e47524 Compare February 25, 2025 16:02
@ockham ockham marked this pull request as ready for review February 25, 2025 16:04
Copy link

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: ockham <[email protected]>
Co-authored-by: paulopmt1 <[email protected]>

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

@ockham
Copy link
Contributor Author

ockham commented Feb 25, 2025

Flagging this for inclusion in WP 6.8 @Mamaduka @fabiankaegy 🙂

@ockham ockham merged commit ac34493 into trunk Feb 25, 2025
63 checks passed
@ockham ockham deleted the fix/template-part-block branch February 25, 2025 19:59
@github-actions github-actions bot added this to the Gutenberg 20.4 milestone Feb 25, 2025
ockham added a commit that referenced this pull request Feb 25, 2025
…g `null` (#69309)

Co-authored-by: ockham <[email protected]>
Co-authored-by: paulopmt1 <[email protected]>
Co-authored-by: josephscott <[email protected]>
@ockham
Copy link
Contributor Author

ockham commented Feb 25, 2025

I've cherry-picked ac34493 to the release/20.0 branch.

@Mamaduka
Copy link
Member

Thanks, @ockham!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Template Part Affects the Template Parts Block [Type] Bug An existing feature does not function as intended
Projects
Development

Successfully merging this pull request may close these issues.

3 participants