fix: flat files fatal on any snippet using namespace or declare - #482
Merged
Conversation
Since 3.10.0 the direct-access guard is prepended to every functions
snippet written to a flat file. PHP requires `declare` and `namespace`
to precede any other statement, so a snippet opening with either was
pushed out of position and fatalled on load:
Namespace declaration statement has to be the very first statement
or after any declare call in the script
The flat file is loaded on every request, so this takes down the entire
site — front end and wp-admin — and the guard runs before WordPress can
log anything, which is why nothing reaches the error log.
Insert the guard after the statement prologue instead: after any
`declare` statements and a namespace declaration, and inside the braces
when a namespace uses block syntax, since no code may sit outside
`namespace {}` blocks. Snippets with no prologue are wrapped exactly as
before, so existing flat files do not change.
TallblokeUK
force-pushed
the
fix/flat-file-namespace/core
branch
from
August 28, 2026 07:58
f725557 to
c90d7b9
Compare
Contributor
Download and install |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reported on Discord by ardittristan, who also diagnosed it correctly. Affects 3.10.0 and 4.0.0-beta.1; 3.9.6 is fine.
The bug
Since 3.10.0,
Functions_Snippet_Handler::wrap_code()prepends a direct-access guard to every functions snippet written to a flat file:3.9.6 wrote the code straight after the opening tag, with no guard:
PHP requires
declareandnamespaceto come before any other statement, so the guard pushes them out of position:The flat file is loaded on every request, so this takes down the whole site — front end and wp-admin. It also explains the "nothing in the error log" part of the report: the failure happens before WordPress can log anything.
The trigger is regeneration, not upgrade, which matches the report exactly — upgrading is quiet until something forces the files to be rewritten (deleting the folder, or saving any setting).
Three broken shapes, not one
namespace Foo;declare(strict_types=1);namespace Foo { }The fix
Insert the guard after the statement prologue rather than before it — after any
declarestatements and a namespace declaration, and inside the braces for block syntax. The prologue is located withtoken_get_all(), sonamespace\my_function()(the operator, not a declaration) is not mistaken for one.Snippets with no prologue are wrapped byte-for-byte as before, so existing flat files do not churn.
Testing
8 unit tests in
Functions_Snippet_Handler_Test, covering each prologue shape plus the namespace-operator case and a check that every shape keeps its guard. 5 of the 8 fail oncoretoday; the 3 that pass are the invariants the fix must not break. Because a misplaced namespace is a compile error, neithertoken_get_all( …, TOKEN_PARSE )norincludecan detect it in-process — including the file kills the test run outright — so the generated code is linted in a subprocess.Full suite: 155 tests, 0 failures.
phpcsclean.Verified end to end on WP 7.1 / PHP 8.3 with stock 3.10.0 and flat files enabled:
php -lon the generated fileThe last row is the guard doing its job — unchanged, and identical to a non-namespaced snippet's file, so nothing is weakened.
Related, not fixed here
The
code_snippets_php_snippet_file_codefilter present in 3.9.6 was dropped in the PSR-4 conversion (1377faeb) and has no replacement — zero hits oncore. That is a public extension point removed in a minor release; anyone hooking it is silently broken. Worth a separate decision on whether to restore it.Content_Snippet_Handlerhas the same prepend shape but closes with?>before the snippet body, and carried the guard in 3.9.6 too, so it is not a regression and is left alone.