From 21134a0c89d12eade787f2de85f347f17fe09968 Mon Sep 17 00:00:00 2001 From: Helen Hou-Sandi Date: Thu, 12 Mar 2015 16:27:06 +0000 Subject: [PATCH] Allow `is_page_template()` to accept an array, as many other conditional tags do. props morganestes, tyxla, DrewAPicture. fixes #31271. git-svn-id: https://develop.svn.wordpress.org/trunk@31754 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/post-template.php | 15 +++++++++--- tests/phpunit/tests/query/conditionals.php | 28 ++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/post-template.php b/src/wp-includes/post-template.php index 427b0acf90..eb794fa025 100644 --- a/src/wp-includes/post-template.php +++ b/src/wp-includes/post-template.php @@ -1629,12 +1629,13 @@ function get_the_password_form( $post = 0 ) { * Whether currently in a page template. * * This template tag allows you to determine if you are in a page template. - * You can optionally provide a template name and then the check will be - * specific to that template. + * You can optionally provide a template name or array of template names + * and then the check will be specific to that template. * * @since 2.5.0 + * @since 4.2.0 The `$template` parameter was changed to accept an array of page templates. * - * @param string $template The specific template name if specific matching is required. + * @param string|array $template The specific template name or array of templates to match. * @return bool True on success, false on failure. */ function is_page_template( $template = '' ) { @@ -1649,6 +1650,14 @@ function is_page_template( $template = '' ) { if ( $template == $page_template ) return true; + if ( is_array( $template ) ) { + if ( ( in_array( 'default', $template, true ) && ! $page_template ) + || in_array( $page_template, $template, true ) + ) { + return true; + } + } + if ( 'default' == $template && ! $page_template ) return true; diff --git a/tests/phpunit/tests/query/conditionals.php b/tests/phpunit/tests/query/conditionals.php index e86d337821..3c273884c0 100644 --- a/tests/phpunit/tests/query/conditionals.php +++ b/tests/phpunit/tests/query/conditionals.php @@ -960,4 +960,32 @@ class Tests_Query_Conditionals extends WP_UnitTestCase { $this->assertFalse( $q->is_page( $p2_name ) ); $this->assertFalse( $q->is_page( $p2 ) ); } + + function test_is_page_template() { + $post_id = $this->factory->post->create( array( 'post_type' => 'page' ) ); + update_post_meta($post_id, '_wp_page_template', 'example.php'); + $this->go_to( "/?page_id=$post_id" ); + $this->assertTrue( is_page_template( 'example.php' ) ); + } + + /** + * @ticket 31271 + */ + function test_is_page_template_default() { + $post_id = $this->factory->post->create( array( 'post_type' => 'page' ) ); + $this->go_to( "/?page_id=$post_id" ); + $this->assertTrue( is_page_template( 'default' ) ); + $this->assertTrue( is_page_template( array( 'random', 'default' ) ) ); + } + + /** + * @ticket 31271 + */ + function test_is_page_template_array() { + $post_id = $this->factory->post->create( array( 'post_type' => 'page' ) ); + update_post_meta($post_id, '_wp_page_template', 'example.php'); + $this->go_to( "/?page_id=$post_id" ); + $this->assertFalse( is_page_template( array( 'test.php' ) ) ); + $this->assertTrue( is_page_template( array('test.php', 'example.php') ) ); + } }