diff --git a/src/wp-includes/post-template.php b/src/wp-includes/post-template.php index edcc1e0f035ec..3d82ba81b641b 100644 --- a/src/wp-includes/post-template.php +++ b/src/wp-includes/post-template.php @@ -606,7 +606,10 @@ function get_post_class( $css_class = '', $post = null ) { */ $classes = apply_filters( 'post_class', $classes, $css_class, $post->ID ); - return array_unique( $classes ); + $classes = array_unique( $classes ); + $classes = array_values( $classes ); + + return $classes; } /** diff --git a/tests/phpunit/tests/post/getPostClass.php b/tests/phpunit/tests/post/getPostClass.php index 64f60a2636c2a..1ec18ade78dfd 100644 --- a/tests/phpunit/tests/post/getPostClass.php +++ b/tests/phpunit/tests/post/getPostClass.php @@ -135,4 +135,26 @@ public function test_taxonomy_classes_hit_cache() { $this->assertSame( $num_queries, get_num_queries() ); } + + /** + * @ticket 64247 + */ + public function test_list_return_value_when_duplicate_classes() { + + // Filter 'post_class' to add a duplicate which should be removed by `array_unique()`. + add_filter( + 'post_class', + function ( $classes ) { + return array_merge( + array( 'duplicate-class', 'duplicate-class' ), + $classes + ); + } + ); + + $class_list = get_post_class( 'original', $this->post_id ); + $this->assertTrue( array_is_list( $class_list ), 'Expected get_post_class() to return list.' ); + $this->assertContains( 'duplicate-class', $class_list ); + $this->assertContains( 'original', $class_list ); + } } diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index bd0b9a3af48b3..d701d12f9dd68 100644 --- a/tests/phpunit/tests/rest-api/rest-posts-controller.php +++ b/tests/phpunit/tests/rest-api/rest-posts-controller.php @@ -2775,6 +2775,36 @@ public function test_prepare_item_override_excerpt_length() { ); } + /** + * Test that the `class_list` property is a list. + * + * @ticket 64247 + * + * @covers WP_REST_Posts_Controller::prepare_item_for_response + */ + public function test_class_list_is_list() { + $post_id = self::factory()->post->create(); + + // Filter 'post_class' to add a duplicate which should be removed by `array_unique()`. + add_filter( + 'post_class', + function ( $classes ) { + return array_merge( + array( 'duplicate-class', 'duplicate-class' ), + $classes + ); + } + ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id ); + $response = rest_do_request( $request ); + $data = $response->get_data(); + + $this->assertArrayHasKey( 'class_list', $data ); + $this->assertContains( 'duplicate-class', $data['class_list'] ); + $this->assertTrue( array_is_list( $data['class_list'] ), 'Expected class_list to be a list.' ); + } + public function test_create_item() { wp_set_current_user( self::$editor_id );