Update wp-config.php regex to handle strict types declaration - #1250
Open
andyexeter wants to merge 1 commit into
Open
Update wp-config.php regex to handle strict types declaration#1250andyexeter wants to merge 1 commit into
andyexeter wants to merge 1 commit into
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1250 +/- ##
========================================
Coverage 1.52% 1.52%
Complexity 19723 19723
========================================
Files 634 634
Lines 98658 98658
========================================
Hits 1509 1509
Misses 97149 97149 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
andyexeter
force-pushed
the
wp-config-directive-fix
branch
from
July 1, 2026 09:25
e98f2d4 to
afe9f5c
Compare
W3TC inserts the WP_CACHE directive immediately after the opening PHP
tag in wp-config.php. On installs that declare strict_types this pushed
the directive above the declaration, triggering a fatal error:
strict_types declaration must be the very first statement in the script
Update the insertion regex to place the directive after a strict_types
declaration when one is present. It now also tolerates comments between
the opening tag and the declaration, and additional directives in the
same declare (e.g. declare(strict_types=1, ticks=1)), while leaving a
non-strict_types declare (e.g. declare(ticks=1)) and plain docblocks to
receive the directive right after the opening tag as before.
Add tests/test-wp-config-directive.php, a standalone regression test
covering the strict_types, comment, multi-directive and backward-compat
cases.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
andyexeter
force-pushed
the
wp-config-directive-fix
branch
from
July 2, 2026 15:43
afe9f5c to
4b7f061
Compare
Contributor
Author
|
@cssjoe would you mind taking a look at this when you get a chance? It's a small fix for a fatal we hit on I've just pushed an update that hardens the regex (handles comments before the declare and multi-directive declares) and added a standalone regression test, so the earlier codecov 0%-coverage flag is covered now. |
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.
Summary
When W3TC enables page caching it adds the
WP_CACHEconstant towp-config.php, inserting it immediately after the opening<?php(or<?) tag. On installs that usedeclare(strict_types=1);this places the constant above the declaration, which is a fatal error:Fix
Update the insertion regex in
PgCache_Environment::wp_config_add_directive()so that, when astrict_typesdeclaration follows the opening tag, theWP_CACHEdirective is inserted after that declaration — keepingstrict_typesas the first statement in the file.The regex is deliberately conservative and handles the real-world variations:
/* */), line (//) and hash (#) — since PHP allows a docblock beforedeclare(strict_types=1).declare(strict_types=1, ticks=1)anddeclare(ticks=1, strict_types=1).strict_types(e.g.declare(ticks=1), which has no first-statement restriction) and a plain WordPress docblock with no declare both still receive the directive right after the opening tag, exactly as before.Test
Adds
tests/test-wp-config-directive.php, a standalone regression test (same convention as the othertests/test-*.phpfiles — run withphp tests/test-wp-config-directive.php). It mirrors the insertion regex and asserts the exact output for 12 scenarios (19 assertions), covering:<?php/ short<?tags (directive follows the tag);strict_typeson its own line and on the same line as the tag;DECLAREwith internal whitespace;declare(ticks=1)and plain-docblock cases;Each
strict_typescase also asserts thatstrict_typesstill precedes the insertedWP_CACHEdefine.Verified end-to-end that the generated
wp-config.phpis valid PHP and definesWP_CACHEfor the strict_types + docblock and multi-directive cases (which previously produced a fatal).