From f62112fa163a35a523aaec9ae7c49908788a2f12 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Tue, 15 Nov 2016 03:36:13 +0000 Subject: [PATCH] Posts, Post Types: Improve sanitisation of templates' post types. Prevents post type templates ignoring post types due to invalid characters. Each entry in the `Template Post Type` comment is run through `sanitize_key()` to match the sanitisation used by `register_post_type()`. Fixes #38766. git-svn-id: https://develop.svn.wordpress.org/trunk@39236 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-theme.php | 2 +- .../38766/no-trailing-period-post-types.php | 5 ++++ .../page-templates/38766/tilde-post-types.php | 7 +++++ .../38766/trailing-comma-post-types.php | 5 ++++ .../38766/trailing-period-post-types.php | 5 ++++ .../trailing-period-whitespace-post-types.php | 5 ++++ .../trailing-whitespace-period-post-types.php | 5 ++++ tests/phpunit/tests/admin/includesTheme.php | 26 +++++++++++++++++++ 8 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/data/themedir1/page-templates/38766/no-trailing-period-post-types.php create mode 100644 tests/phpunit/data/themedir1/page-templates/38766/tilde-post-types.php create mode 100644 tests/phpunit/data/themedir1/page-templates/38766/trailing-comma-post-types.php create mode 100644 tests/phpunit/data/themedir1/page-templates/38766/trailing-period-post-types.php create mode 100644 tests/phpunit/data/themedir1/page-templates/38766/trailing-period-whitespace-post-types.php create mode 100644 tests/phpunit/data/themedir1/page-templates/38766/trailing-whitespace-period-post-types.php diff --git a/src/wp-includes/class-wp-theme.php b/src/wp-includes/class-wp-theme.php index 8b3661a888..f161a63186 100644 --- a/src/wp-includes/class-wp-theme.php +++ b/src/wp-includes/class-wp-theme.php @@ -1038,7 +1038,7 @@ final class WP_Theme implements ArrayAccess { } foreach ( $types as $type ) { - $type = trim( $type ); + $type = sanitize_key( $type ); if ( ! isset( $post_templates[ $type ] ) ) { $post_templates[ $type ] = array(); } diff --git a/tests/phpunit/data/themedir1/page-templates/38766/no-trailing-period-post-types.php b/tests/phpunit/data/themedir1/page-templates/38766/no-trailing-period-post-types.php new file mode 100644 index 0000000000..2b6867109a --- /dev/null +++ b/tests/phpunit/data/themedir1/page-templates/38766/no-trailing-period-post-types.php @@ -0,0 +1,5 @@ +assertEquals( array(), get_page_templates( null, 'bar' ) ); } + /** + * @ticket 38766 + */ + function test_page_templates_for_post_types_with_trailing_periods() { + $theme = wp_get_theme( 'page-templates' ); + $this->assertNotEmpty( $theme ); + + switch_theme( $theme['Template'], $theme['Stylesheet'] ); + + $this->assertEqualSetsWithIndex( array( + 'No Trailing Period' => '38766/no-trailing-period-post-types.php', + 'Trailing Period.' => '38766/trailing-period-post-types.php', + 'Trailing Comma,' => '38766/trailing-comma-post-types.php', + 'Trailing Period, White Space.' => '38766/trailing-period-whitespace-post-types.php', + 'Trailing White Space, Period.' => '38766/trailing-whitespace-period-post-types.php', + 'Tilde in Post Type.' => '38766/tilde-post-types.php', + ), get_page_templates( null, 'period' ) ); + $this->assertEqualSetsWithIndex( array( + 'No Trailing Period' => '38766/no-trailing-period-post-types.php', + 'Trailing Period.' => '38766/trailing-period-post-types.php', + 'Trailing Comma,' => '38766/trailing-comma-post-types.php', + 'Trailing Period, White Space.' => '38766/trailing-period-whitespace-post-types.php', + 'Trailing White Space, Period.' => '38766/trailing-whitespace-period-post-types.php', + ), get_page_templates( null, 'full-stop' ) ); + } + /** * @ticket 38696 */