From 87d77bc24ac2f2df2933b96a7c91fa1ddb063045 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 23 Oct 2017 20:07:06 +0000 Subject: [PATCH] Themes: Show templates from both parent and child theme when calling `WP_Theme::get_post_templates()`. Props birgire for initial patch. Fixes #41717. git-svn-id: https://develop.svn.wordpress.org/trunk@41975 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-theme.php | 6 +--- tests/phpunit/tests/admin/includesTheme.php | 39 +++++++++++++++++++-- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/class-wp-theme.php b/src/wp-includes/class-wp-theme.php index 4dd0a75016..171d8b6c58 100644 --- a/src/wp-includes/class-wp-theme.php +++ b/src/wp-includes/class-wp-theme.php @@ -1036,7 +1036,7 @@ final class WP_Theme implements ArrayAccess { if ( ! is_array( $post_templates ) ) { $post_templates = array(); - $files = (array) $this->get_files( 'php', 1 ); + $files = (array) $this->get_files( 'php', 1, true); foreach ( $files as $file => $full_path ) { if ( ! preg_match( '|Template Name:(.*)$|mi', file_get_contents( $full_path ), $header ) ) { @@ -1091,10 +1091,6 @@ final class WP_Theme implements ArrayAccess { $post_templates = $this->get_post_templates(); $post_templates = isset( $post_templates[ $post_type ] ) ? $post_templates[ $post_type ] : array(); - if ( $this->parent() ) { - $post_templates += $this->parent()->get_page_templates( $post, $post_type ); - } - /** * Filters list of page templates for a theme. * diff --git a/tests/phpunit/tests/admin/includesTheme.php b/tests/phpunit/tests/admin/includesTheme.php index 223560d98d..86ec368d67 100644 --- a/tests/phpunit/tests/admin/includesTheme.php +++ b/tests/phpunit/tests/admin/includesTheme.php @@ -122,21 +122,56 @@ class Tests_Admin_includesTheme extends WP_UnitTestCase { switch_theme( $theme['Template'], $theme['Stylesheet'] ); $this->assertEqualSetsWithIndex( array( - 'Top Level' => 'template-top-level-post-types.php', - 'Sub Dir' => 'subdir/template-sub-dir-post-types.php', + 'Top Level' => 'template-top-level-post-types.php', + 'Sub Dir' => 'subdir/template-sub-dir-post-types.php', + 'Top Level In A Child Theme' => 'template-top-level-post-types-child.php', + 'Sub Dir In A Child Theme' => 'subdir/template-sub-dir-post-types-child.php', ), get_page_templates( null, 'foo' ) ); + $this->assertEqualSetsWithIndex( array( 'Top Level' => 'template-top-level-post-types.php', 'Sub Dir' => 'subdir/template-sub-dir-post-types.php', ), get_page_templates( null, 'post' ) ); + $this->assertEqualSetsWithIndex( array( 'Top Level' => 'template-top-level.php', 'Sub Dir' => 'subdir/template-sub-dir.php', 'This Template Header Is On One Line' => 'template-header.php', ), get_page_templates() ); + $this->assertEquals( array(), get_page_templates( null, 'bar' ) ); } + /** + * @ticket 41717 + */ + public function test_get_post_templates_child_theme() { + $theme = wp_get_theme( 'page-templates-child' ); + $this->assertNotEmpty( $theme ); + + switch_theme( $theme['Template'], $theme['Stylesheet'] ); + + $post_templates = $theme->get_post_templates(); + + $this->assertEqualSetsWithIndex( array( + 'template-top-level-post-types.php' => 'Top Level', + 'subdir/template-sub-dir-post-types.php' => 'Sub Dir', + 'template-top-level-post-types-child.php' => 'Top Level In A Child Theme', + 'subdir/template-sub-dir-post-types-child.php' => 'Sub Dir In A Child Theme', + ), $post_templates['foo'] ); + + $this->assertEqualSetsWithIndex( array( + 'template-top-level-post-types.php' => 'Top Level', + 'subdir/template-sub-dir-post-types.php' => 'Sub Dir', + ), $post_templates['post'] ); + + $this->assertEqualSetsWithIndex( array( + 'template-top-level.php' => 'Top Level', + 'subdir/template-sub-dir.php' => 'Sub Dir', + 'template-header.php' => 'This Template Header Is On One Line', + ), $post_templates['page'] ); + } + /** * Test that the list of theme features pulled from the WordPress.org API returns the expected data structure. *