From 2816e8b8764d37024622ec964a2f4ac885208585 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 18 Jun 2021 14:02:44 +0000 Subject: [PATCH] Themes: Make sure `get_file_data()` recognizes headers prefixed by ``, which previously could not be recognized correctly. Props dd32, m_uysl, thomas-vitale, boblinthorst. Fixes #33387. git-svn-id: https://develop.svn.wordpress.org/trunk@51182 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/functions.php | 2 +- ...er.php => file-header-cr-line-endings.php} | 0 .../file-header-php-open-tag-prefix.php | 1 + tests/phpunit/tests/file.php | 28 +++++++++++++++++-- 4 files changed, 27 insertions(+), 4 deletions(-) rename tests/phpunit/data/formatting/{cr-line-endings-file-header.php => file-header-cr-line-endings.php} (100%) create mode 100644 tests/phpunit/data/formatting/file-header-php-open-tag-prefix.php diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 0ba059ba8e..71a0522139 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -6287,7 +6287,7 @@ function get_file_data( $file, $default_headers, $context = '' ) { } foreach ( $all_headers as $field => $regex ) { - if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, $match ) && $match[1] ) { + if ( preg_match( '/^(?:[ \t]*<\?php)?[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, $match ) && $match[1] ) { $all_headers[ $field ] = _cleanup_header_comment( $match[1] ); } else { $all_headers[ $field ] = ''; diff --git a/tests/phpunit/data/formatting/cr-line-endings-file-header.php b/tests/phpunit/data/formatting/file-header-cr-line-endings.php similarity index 100% rename from tests/phpunit/data/formatting/cr-line-endings-file-header.php rename to tests/phpunit/data/formatting/file-header-cr-line-endings.php diff --git a/tests/phpunit/data/formatting/file-header-php-open-tag-prefix.php b/tests/phpunit/data/formatting/file-header-php-open-tag-prefix.php new file mode 100644 index 0000000000..dc078607c3 --- /dev/null +++ b/tests/phpunit/data/formatting/file-header-php-open-tag-prefix.php @@ -0,0 +1 @@ + diff --git a/tests/phpunit/tests/file.php b/tests/phpunit/tests/file.php index 730d72c5c1..a127742120 100644 --- a/tests/phpunit/tests/file.php +++ b/tests/phpunit/tests/file.php @@ -44,16 +44,18 @@ class Tests_File extends WP_UnitTestCase { } /** + * @ticket 19854 * @group plugins * @group themes */ - function test_get_file_data_cr_line_endings() { - $headers = array( + function test_get_file_data_with_cr_line_endings() { + $headers = array( 'SomeHeader' => 'Some Header', 'Description' => 'Description', 'Author' => 'Author', ); - $actual = get_file_data( DIR_TESTDATA . '/formatting/cr-line-endings-file-header.php', $headers ); + + $actual = get_file_data( DIR_TESTDATA . '/formatting/file-header-cr-line-endings.php', $headers ); $expected = array( 'SomeHeader' => 'Some header value!', 'Description' => 'This file is using CR line endings for a testcase.', @@ -65,6 +67,26 @@ class Tests_File extends WP_UnitTestCase { } } + /** + * @ticket 47186 + * @group plugins + * @group themes + */ + function test_get_file_data_with_php_open_tag_prefix() { + $headers = array( + 'TemplateName' => 'Template Name', + ); + + $actual = get_file_data( DIR_TESTDATA . '/formatting/file-header-php-open-tag-prefix.php', $headers ); + $expected = array( + 'TemplateName' => 'Something', + ); + + foreach ( $actual as $header => $value ) { + $this->assertSame( $expected[ $header ], $value, $header ); + } + } + function is_unique_writable_file( $path, $filename ) { $fullpath = $path . DIRECTORY_SEPARATOR . $filename;