From 757fd2c1e4807b32b2806d58c2eacebfa56b5ce8 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Wed, 11 Feb 2015 03:59:06 +0000 Subject: [PATCH] Always pass back the custom classes `get_post_class()` was called with, even if the post was not found. props F J Kaiser, Bueltge. fixes #22271. git-svn-id: https://develop.svn.wordpress.org/trunk@31408 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/post-template.php | 20 +++++++++++--------- tests/phpunit/tests/post/getPostClass.php | 9 +++++++++ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/wp-includes/post-template.php b/src/wp-includes/post-template.php index d5f839cf6d..c4050d9718 100644 --- a/src/wp-includes/post-template.php +++ b/src/wp-includes/post-template.php @@ -404,12 +404,20 @@ function post_class( $class = '', $post_id = null ) { * @return array Array of classes. */ function get_post_class( $class = '', $post_id = null ) { - $post = get_post($post_id); + $post = get_post( $post_id ); $classes = array(); - if ( empty($post) ) + if ( $class ) { + if ( ! is_array( $class ) ) { + $class = preg_split( '#\s+#', $class ); + } + $classes = array_map( 'esc_attr', $class ); + } + + if ( ! $post ) { return $classes; + } $classes[] = 'post-' . $post->ID; if ( ! is_admin() ) @@ -466,13 +474,7 @@ function get_post_class( $class = '', $post_id = null ) { } } - if ( !empty($class) ) { - if ( !is_array( $class ) ) - $class = preg_split('#\s+#', $class); - $classes = array_merge($classes, $class); - } - - $classes = array_map('esc_attr', $classes); + $classes = array_map( 'esc_attr', $classes ); /** * Filter the list of CSS classes for the current post. diff --git a/tests/phpunit/tests/post/getPostClass.php b/tests/phpunit/tests/post/getPostClass.php index ff05972c1b..9329f21be4 100644 --- a/tests/phpunit/tests/post/getPostClass.php +++ b/tests/phpunit/tests/post/getPostClass.php @@ -44,6 +44,15 @@ class Tests_Post_GetPostClass extends WP_UnitTestCase { $this->assertContains( 'wptests_tax-bar', $found ); } + /** + * @ticket 22271 + */ + public function test_with_custom_classes_and_no_post() { + $this->assertEquals( array(), get_post_class( '', null ) ); + $this->assertEquals( array( 'foo' ), get_post_class( 'foo', null ) ); + $this->assertEquals( array( 'foo', 'bar' ), get_post_class( array( 'foo', 'bar' ), null ) ); + } + /** * @group cache */