WordPress.org

Make WordPress Core

Changeset 51182


Ignore:
Timestamp:
06/18/2021 02:02:44 PM (3 months ago)
Author:
SergeyBiryukov
Message:

Themes: Make sure get_file_data() recognizes headers prefixed by <?php tag.

This allows for using headers in the format of <?php // Template Name: Something ?>, which previously could not be recognized correctly.

Props dd32, m_uysl, thomas-vitale, boblinthorst.
Fixes #33387.

Location:
trunk
Files:
1 added
2 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r51055 r51182  
    62886288
    62896289    foreach ( $all_headers as $field => $regex ) {
    6290         if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, $match ) && $match[1] ) {
     6290        if ( preg_match( '/^(?:[ \t]*<\?php)?[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, $match ) && $match[1] ) {
    62916291            $all_headers[ $field ] = _cleanup_header_comment( $match[1] );
    62926292        } else {
  • trunk/tests/phpunit/tests/file.php

    r48937 r51182  
    4545
    4646    /**
     47     * @ticket 19854
    4748     * @group plugins
    4849     * @group themes
    4950     */
    50     function test_get_file_data_cr_line_endings() {
    51         $headers  = array(
     51    function test_get_file_data_with_cr_line_endings() {
     52        $headers = array(
    5253            'SomeHeader'  => 'Some Header',
    5354            'Description' => 'Description',
    5455            'Author'      => 'Author',
    5556        );
    56         $actual   = get_file_data( DIR_TESTDATA . '/formatting/cr-line-endings-file-header.php', $headers );
     57
     58        $actual   = get_file_data( DIR_TESTDATA . '/formatting/file-header-cr-line-endings.php', $headers );
    5759        $expected = array(
    5860            'SomeHeader'  => 'Some header value!',
    5961            'Description' => 'This file is using CR line endings for a testcase.',
    6062            'Author'      => 'A Very Old Mac',
     63        );
     64
     65        foreach ( $actual as $header => $value ) {
     66            $this->assertSame( $expected[ $header ], $value, $header );
     67        }
     68    }
     69
     70    /**
     71     * @ticket 47186
     72     * @group plugins
     73     * @group themes
     74     */
     75    function test_get_file_data_with_php_open_tag_prefix() {
     76        $headers = array(
     77            'TemplateName' => 'Template Name',
     78        );
     79
     80        $actual   = get_file_data( DIR_TESTDATA . '/formatting/file-header-php-open-tag-prefix.php', $headers );
     81        $expected = array(
     82            'TemplateName' => 'Something',
    6183        );
    6284
Note: See TracChangeset for help on using the changeset viewer.