Skip to content

Commit d792cbd

Browse files
General: Make sure get_file_data() recognizes headers prefixed by a <? tag.
This allows for using both of the following single-line headers formats: {{{ <?php // Template Name: Full-Width ?> }}} or with a PHP short open tag: {{{ <? # Template Name: Full-Width ?> }}} Previously, only the first one would be recognized correctly. Follow-up to [51182], [62802]. Props birgire, gschoppe, sukhendu2002, SergeyBiryukov. Fixes #42517. git-svn-id: https://develop.svn.wordpress.org/trunk@62803 602fd350-edb4-49c9-b593-d223f7449a82
1 parent c38a1b7 commit d792cbd

3 files changed

Lines changed: 20 additions & 8 deletions

File tree

src/wp-includes/functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7051,7 +7051,7 @@ function get_file_data( $file, $default_headers, $context = '' ) {
70517051
}
70527052

70537053
foreach ( $all_headers as $field => $regex ) {
7054-
if ( preg_match( '/^(?:[ \t]*<\?php)?[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, $match ) && $match[1] ) {
7054+
if ( preg_match( '/^(?:[ \t]*<\?(?:php)?)?[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, $match ) && $match[1] ) {
70557055
$all_headers[ $field ] = _cleanup_header_comment( $match[1] );
70567056
} else {
70577057
$all_headers[ $field ] = '';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<? # Template Name: Something ?>

tests/phpunit/tests/functions/getFileData.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ class Tests_Functions_GetFileData extends WP_UnitTestCase {
1616
* Tests get_file_data().
1717
*
1818
* @ticket 19854
19+
* @ticket 42517
1920
* @ticket 47186
2021
*
2122
* @dataProvider data_test_get_data_file
2223
*
23-
* @param string $file File path, relative to the test data directory.
24-
* @param array $headers Default headers.
25-
* @param string $context Context.
26-
* @param array $expected Expected headers.
24+
* @param string $file File path, relative to the test data directory.
25+
* @param array<string, string> $headers Default headers.
26+
* @param string $context Context.
27+
* @param array<string, string> $expected Expected headers.
2728
*/
2829
public function test_get_file_data( $file, $headers, $context, $expected ) {
2930
$actual = get_file_data( DIR_TESTDATA . $file, $headers, $context );
@@ -47,7 +48,7 @@ public function test_get_file_data( $file, $headers, $context, $expected ) {
4748
*/
4849
public function data_test_get_data_file() {
4950
return array(
50-
'theme headers' => array(
51+
'theme headers' => array(
5152
'file' => '/themedir1/default/style.css',
5253
'headers' => array(
5354
'Name' => 'Theme Name',
@@ -67,7 +68,7 @@ public function data_test_get_data_file() {
6768
'AuthorURI' => 'http://binarybonsai.com/',
6869
),
6970
),
70-
'cr line endings' => array(
71+
'cr line endings' => array(
7172
'file' => '/formatting/file-header-cr-line-endings.php',
7273
'headers' => array(
7374
'SomeHeader' => 'Some Header',
@@ -81,7 +82,7 @@ public function data_test_get_data_file() {
8182
'Author' => 'A Very Old Mac',
8283
),
8384
),
84-
'php open tag prefix' => array(
85+
'php open tag prefix' => array(
8586
'file' => '/formatting/file-header-php-open-tag-prefix.php',
8687
'headers' => array(
8788
'TemplateName' => 'Template Name',
@@ -91,6 +92,16 @@ public function data_test_get_data_file() {
9192
'TemplateName' => 'Something',
9293
),
9394
),
95+
'php short open tag prefix' => array(
96+
'file' => '/formatting/file-header-php-short-open-tag-prefix.php',
97+
'headers' => array(
98+
'TemplateName' => 'Template Name',
99+
),
100+
'context' => '',
101+
'expected' => array(
102+
'TemplateName' => 'Something',
103+
),
104+
),
94105
);
95106
}
96107
}

0 commit comments

Comments
 (0)