From 9d9a57ebfae3fda9248303bcb750a6fcc3b68e53 Mon Sep 17 00:00:00 2001 From: Sukhendu Sekhar Guria Date: Mon, 20 Apr 2026 17:52:27 +0530 Subject: [PATCH 1/3] General: Support single-line PHP template headers in get_file_data() --- src/wp-includes/functions.php | 8 +++++-- .../template-header-short-tag.php | 1 + tests/phpunit/tests/file.php | 24 +++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 tests/phpunit/data/themedir1/page-templates/template-header-short-tag.php diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 7d71c8c56963d..63e17fdd663aa 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -6954,8 +6954,12 @@ function get_file_data( $file, $default_headers, $context = '' ) { $file_data = ''; } - // Make sure we catch CR-only line endings. - $file_data = str_replace( "\r", "\n", $file_data ); + // Make sure we catch CR-only line endings and strip PHP open/close tags. + $file_data = str_replace( + array( "\r", '' ), + array( "\n", '', '', '' ), + $file_data + ); /** * Filters extra file headers by context. diff --git a/tests/phpunit/data/themedir1/page-templates/template-header-short-tag.php b/tests/phpunit/data/themedir1/page-templates/template-header-short-tag.php new file mode 100644 index 0000000000000..b2353ea6a4502 --- /dev/null +++ b/tests/phpunit/data/themedir1/page-templates/template-header-short-tag.php @@ -0,0 +1 @@ + diff --git a/tests/phpunit/tests/file.php b/tests/phpunit/tests/file.php index 9a29e4e67669a..a001d810cae7b 100644 --- a/tests/phpunit/tests/file.php +++ b/tests/phpunit/tests/file.php @@ -95,6 +95,30 @@ public function test_get_file_data_with_php_open_tag_prefix() { } } + /** + * Tests that get_file_data() supports single-line headers with a 'Template Name' ); + $actual = get_file_data( DIR_TESTDATA . '/themedir1/page-templates/template-header.php', $headers ); + $this->assertSame( 'This Template Header Is On One Line', $actual['TemplateName'] ); + } + + /** + * Tests that get_file_data() supports single-line headers with a 'Template Name' ); + $actual = get_file_data( DIR_TESTDATA . '/themedir1/page-templates/template-header-short-tag.php', $headers ); + $this->assertSame( 'Short Tag Template', $actual['TemplateName'] ); + } + private function is_unique_writable_file( $path, $filename ) { $fullpath = $path . DIRECTORY_SEPARATOR . $filename; From 45f31d77d08036a15e07561667ab05efaea04a49 Mon Sep 17 00:00:00 2001 From: Sukhendu Sekhar Guria Date: Mon, 20 Apr 2026 17:57:38 +0530 Subject: [PATCH 2/3] Fix PHPCS errors --- src/wp-includes/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 63e17fdd663aa..7a8abb2b6b176 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -6957,7 +6957,7 @@ function get_file_data( $file, $default_headers, $context = '' ) { // Make sure we catch CR-only line endings and strip PHP open/close tags. $file_data = str_replace( array( "\r", '' ), - array( "\n", '', '', '' ), + array( "\n", '', '', '' ), $file_data ); From e788129306b1da49bb69e2fa8496671a955b8d8f Mon Sep 17 00:00:00 2001 From: Sukhendu Sekhar Guria Date: Mon, 20 Apr 2026 18:14:41 +0530 Subject: [PATCH 3/3] Tests: Move short-open-tag test fixture out of page-templates directory --- .../file-header-short-open-tag.php} | 0 tests/phpunit/tests/file.php | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename tests/phpunit/data/{themedir1/page-templates/template-header-short-tag.php => formatting/file-header-short-open-tag.php} (100%) diff --git a/tests/phpunit/data/themedir1/page-templates/template-header-short-tag.php b/tests/phpunit/data/formatting/file-header-short-open-tag.php similarity index 100% rename from tests/phpunit/data/themedir1/page-templates/template-header-short-tag.php rename to tests/phpunit/data/formatting/file-header-short-open-tag.php diff --git a/tests/phpunit/tests/file.php b/tests/phpunit/tests/file.php index a001d810cae7b..1f416a8a74b64 100644 --- a/tests/phpunit/tests/file.php +++ b/tests/phpunit/tests/file.php @@ -115,7 +115,7 @@ public function test_get_file_data_with_single_line_php_open_tag() { */ public function test_get_file_data_with_single_line_short_open_tag() { $headers = array( 'TemplateName' => 'Template Name' ); - $actual = get_file_data( DIR_TESTDATA . '/themedir1/page-templates/template-header-short-tag.php', $headers ); + $actual = get_file_data( DIR_TESTDATA . '/formatting/file-header-short-open-tag.php', $headers ); $this->assertSame( 'Short Tag Template', $actual['TemplateName'] ); }