From 4b7f0618459162d230b9e9a07fdae302eb71ff77 Mon Sep 17 00:00:00 2001 From: Andy Palmer Date: Thu, 20 Nov 2025 09:43:19 +0000 Subject: [PATCH] Insert WP_CACHE after strict_types declaration in wp-config.php 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 --- PgCache_Environment.php | 24 +++- tests/test-wp-config-directive.php | 185 +++++++++++++++++++++++++++++ 2 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 tests/test-wp-config-directive.php diff --git a/PgCache_Environment.php b/PgCache_Environment.php index 52741cd4c..1af625595 100644 --- a/PgCache_Environment.php +++ b/PgCache_Environment.php @@ -482,8 +482,30 @@ private function wp_config_add_directive() { } $new_config_data = $this->wp_config_remove_from_content( $config_data ); + + /* + * Insert the WP_CACHE directive immediately after the opening PHP tag. When a + * strict_types declaration follows the tag, insert after that declaration instead, + * so the directive never displaces strict_types from being the first statement in + * the file (which is a fatal error). Comments between the tag and the declaration, + * and additional directives inside it (e.g. "declare(strict_types=1, ticks=1)"), + * are all tolerated. + */ $new_config_data = preg_replace( - '~<\?(php)?~', + '~ + <\?(?:php)? # Opening PHP tag: "wp_config_addon(), $new_config_data, 1 diff --git a/tests/test-wp-config-directive.php b/tests/test-wp-config-directive.php new file mode 100644 index 000000000..75b9c0e61 --- /dev/null +++ b/tests/test-wp-config-directive.php @@ -0,0 +1,185 @@ + 0 ? 1 : 0 );