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

Fatal Error in PHP 8.1 when a template part reference is missing #54352

Closed
tomjn opened this issue Sep 11, 2023 · 8 comments · Fixed by #54354
Closed

Fatal Error in PHP 8.1 when a template part reference is missing #54352

tomjn opened this issue Sep 11, 2023 · 8 comments · Fixed by #54354
Labels
[Block] Template Part Affects the Template Parts Block Needs Testing Needs further testing to be confirmed. [Type] Bug An existing feature does not function as intended

Comments

@tomjn
Copy link
Contributor

tomjn commented Sep 11, 2023

Description

Originally raised here: https://core.trac.wordpress.org/ticket/59318

If a block theme template references a template part via a template part block, but no part exists with the matching slug, a deprecation error occurs due to a null value being sent to preg replace.

In my particular case, the theme attribute was missing for unknown reasons on some templates loading the footer template part, and pages with those templates would trigger these fatal errors

Importantly, a check for this already exists, but only runs if the site is in debugging mode.

	// WP_DEBUG_DISPLAY must only be honored when WP_DEBUG. This precedent
	// is set in `wp_debug_mode()`.
	$is_debug = WP_DEBUG && WP_DEBUG_DISPLAY;

	if ( is_null( $content ) && $is_debug ) {

During debugging $content is indeed null but $is_debug is false, so the failure case is unhandled and null is passed to shortcode_unautop leading to the crash

Step-by-step reproduction instructions

Place this in a template:

<!-- wp:template-part {"slug":"footer","tagName":"footer"} /-->

Notice that in my situation the reason this was not working was the missing theme attribute:

<!-- wp:template-part {"slug":"footer","theme":"my-theme"} /-->

It found the template part once theme had been added, but it should have failed gracefully and returned blank content instead of generating PHP fatal errors.

Screenshots, screen recording, code snippet

No response

Environment info

WP 6.3, PHP 8.1

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@jordesign jordesign added [Type] Bug An existing feature does not function as intended Needs Testing Needs further testing to be confirmed. [Block] Template Part Affects the Template Parts Block labels Sep 12, 2023
@getdave
Copy link
Contributor

getdave commented Sep 27, 2023

I've done some sleuthing around this.

There are two PRs which look to contribute towards fixing this:

These need review so I'm moving this to Needs Review.

@github-actions
Copy link

Hi,
This issue has gone 30 days without any activity. This means it is time for a check-in to make sure it is still relevant. If you are still experiencing this issue with the latest versions, you can help the project by responding to confirm the problem and by providing any updated reproduction steps.
Thanks for helping out.

@github-actions github-actions bot added the [Status] Stale Gives the original author opportunity to update before closing. Can be reopened as needed. label Oct 28, 2023
@tomjn
Copy link
Contributor Author

tomjn commented Oct 28, 2023

Just updating that this is still an issue

@github-actions github-actions bot removed the [Status] Stale Gives the original author opportunity to update before closing. Can be reopened as needed. label Oct 29, 2023
@annezazu
Copy link
Contributor

annezazu commented Feb 8, 2024

Punting this from the 6.5 release as we are less than a week away from beta 1 and it's not clear where the associated PR is: #54354 This was done after a review by the 6.5 core editor triage and tech leads.

@tomjn
Copy link
Contributor Author

tomjn commented Feb 8, 2024

The summary is that the PR does what it needs to do and it makes obvious sense to add the check, but people struggled to reproduce the original error in order to test

@tomjn
Copy link
Contributor Author

tomjn commented Feb 8, 2024

As best as I understand the bug, it can be phrased in two ways:

  • the code that load template parts assumes the template was found and only handles the failure case when in debug mode, leading to a crash in production environments
  • if a template part block is missing a theme attribute it isn't found leading to a crash

@getdave
Copy link
Contributor

getdave commented Feb 8, 2024

Punting this from the 6.5 release as we are less than a week away from beta 1 and it's not clear where the associated PR is: #54354 This was done after a review by the 6.5 core editor triage and tech leads.

Hi @annezazu. This was my fault. I believe we should attempt to resolve for 6.5.

I have re-tested the associated PR and it appears to resolve the Issue. I have asked for a confidence check from a few folks before we merge.

Again apologies for the confusion regarding triage.

@annezazu
Copy link
Contributor

annezazu commented Feb 8, 2024

Thanks for jumping back in with more context! Let's do it. This is part of the beauty of triaging is to uncover and move forward these things too.

getdave pushed a commit that referenced this issue Feb 9, 2024
… non-debug environments (#54354)

* Fix #54352 prevent php 8.1 fatal when template parts are not found in non-debug environments

If the template part is not found content will be null which will cause fatal errors in PHP 8.1 when passed to shortcode_unautop, but this case is already handled if debug mode is turned on. This changeset allows it to be handled in all environments, not just when the admin has turned on WP_DEBUG and display debug messages

* Update packages/block-library/src/template-part/index.php

Co-authored-by: Anton Vlasenko <43744263+anton-vlasenko@users.noreply.github.com>

* Simplifies the check for a non-existent template part on render

Incorporate feedback from @hellofromtonya https://github.com/WordPress/gutenberg/pull/54354/files#r1483032128

---------

Co-authored-by: getdave <get_dave@git.wordpress.org>
Co-authored-by: anton-vlasenko <antonvlasenko@git.wordpress.org>
Co-authored-by: hellofromtonya <hellofromtonya@git.wordpress.org>
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 Needs Testing Needs further testing to be confirmed. [Type] Bug An existing feature does not function as intended
Projects
Status: Done
4 participants