Skip to content

Commit 4799c40

Browse files
authored
Merge branch 'WordPress:trunk' into trunk
2 parents d2686fb + 78aea8b commit 4799c40

2 files changed

Lines changed: 83 additions & 4 deletions

File tree

src/wp-includes/class-wp-theme.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,13 +1338,22 @@ public function get_post_templates() {
13381338
$files = (array) $this->get_files( 'php', 1, true );
13391339

13401340
foreach ( $files as $file => $full_path ) {
1341-
if ( ! preg_match( '|Template Name:(.*)$|mi', file_get_contents( $full_path ), $header ) ) {
1341+
$headers = get_file_data(
1342+
$full_path,
1343+
array(
1344+
'TemplateName' => 'Template Name',
1345+
'TemplatePostType' => 'Template Post Type',
1346+
),
1347+
'theme'
1348+
);
1349+
1350+
if ( ! $headers['TemplateName'] ) {
13421351
continue;
13431352
}
13441353

13451354
$types = array( 'page' );
1346-
if ( preg_match( '|Template Post Type:(.*)$|mi', file_get_contents( $full_path ), $type ) ) {
1347-
$types = explode( ',', _cleanup_header_comment( $type[1] ) );
1355+
if ( $headers['TemplatePostType'] ) {
1356+
$types = explode( ',', $headers['TemplatePostType'] );
13481357
}
13491358

13501359
foreach ( $types as $type ) {
@@ -1353,7 +1362,7 @@ public function get_post_templates() {
13531362
$post_templates[ $type ] = array();
13541363
}
13551364

1356-
$post_templates[ $type ][ $file ] = _cleanup_header_comment( $header[1] );
1365+
$post_templates[ $type ][ $file ] = $headers['TemplateName'];
13571366
}
13581367
}
13591368

tests/phpunit/tests/theme/wpThemeGetPostTemplates.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,74 @@ public function test_get_post_templates_child_theme() {
4747
$post_templates['page']
4848
);
4949
}
50+
51+
/**
52+
* @ticket 42513
53+
*/
54+
public function test_get_post_templates_caches_results() {
55+
$theme = wp_get_theme( 'page-templates' );
56+
$this->assertNotEmpty( $theme );
57+
58+
$filter = new MockAction();
59+
add_filter( 'extra_theme_headers', array( $filter, 'filter' ) );
60+
61+
// First call populates the cache.
62+
$first_result = $theme->get_post_templates();
63+
$this->assertNotEmpty( $first_result );
64+
$filter_call_count = $filter->get_call_count();
65+
$this->assertGreaterThan( 0, $filter_call_count, 'The `extra_theme_headers` filter should be called at least once.' );
66+
67+
// Second call should return the same result from cache.
68+
$second_result = $theme->get_post_templates();
69+
$this->assertSame( $first_result, $second_result );
70+
$this->assertSame( $filter_call_count, $filter->get_call_count(), 'The `extra_theme_headers` filter should not have extra calls.' );
71+
}
72+
73+
/**
74+
* @ticket 42513
75+
*/
76+
public function test_get_post_templates_clears_cache_on_theme_switch() {
77+
$theme = wp_get_theme( 'page-templates' );
78+
$this->assertNotEmpty( $theme );
79+
80+
$filter = new MockAction();
81+
add_filter( 'extra_theme_headers', array( $filter, 'filter' ) );
82+
83+
// Populate cache.
84+
$theme->get_post_templates();
85+
86+
$filter_call_count = $filter->get_call_count();
87+
$this->assertGreaterThan( 0, $filter_call_count, 'The `extra_theme_headers` filter should be called at least once.' );
88+
89+
$child_theme = wp_get_theme( 'page-templates-child' );
90+
switch_theme( $child_theme['Template'], $child_theme['Stylesheet'] );
91+
92+
$child_templates = $child_theme->get_post_templates();
93+
94+
// Child theme should include its own templates.
95+
$this->assertArrayHasKey( 'foo', $child_templates );
96+
$this->assertArrayHasKey( 'template-top-level-post-types-child.php', $child_templates['foo'] );
97+
$this->assertGreaterThan( $filter_call_count, $filter->get_call_count(), 'The `extra_theme_headers` filter should have extra calls.' );
98+
}
99+
100+
/**
101+
* @ticket 42513
102+
*/
103+
public function test_get_post_templates_uses_get_file_data() {
104+
$theme = wp_get_theme( 'page-templates' );
105+
$this->assertNotEmpty( $theme );
106+
107+
$filter = new MockAction();
108+
add_filter( 'extra_theme_headers', array( $filter, 'filter' ) );
109+
110+
$post_templates = $theme->get_post_templates();
111+
112+
// Verify single-line header format is parsed correctly via get_file_data().
113+
$this->assertArrayHasKey( 'page', $post_templates );
114+
$this->assertArrayHasKey( 'template-header.php', $post_templates['page'] );
115+
$this->assertSame( 'This Template Header Is On One Line', $post_templates['page']['template-header.php'] );
116+
117+
// Verify the `extra_theme_headers` filter is called.
118+
$this->assertGreaterThan( 0, $filter->get_call_count(), 'The `extra_theme_headers` filter should be called at least once.' );
119+
}
50120
}

0 commit comments

Comments
 (0)