Skip to content

Commit 6e91abe

Browse files
committed
Posts, Post Types: Ensure get_post_class() returns a list.
This avoids the REST API erroneously returning an object in the `class_list` property returned by `WP_REST_Posts_Controller::prepare_item_for_response()`. Developed in #10515 Props dlh, mamaduka, westonruter. Fixes #64247. git-svn-id: https://develop.svn.wordpress.org/trunk@61269 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 2cabb30 commit 6e91abe

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

src/wp-includes/post-template.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,10 @@ function get_post_class( $css_class = '', $post = null ) {
606606
*/
607607
$classes = apply_filters( 'post_class', $classes, $css_class, $post->ID );
608608

609-
return array_unique( $classes );
609+
$classes = array_unique( $classes );
610+
$classes = array_values( $classes );
611+
612+
return $classes;
610613
}
611614

612615
/**

tests/phpunit/tests/post/getPostClass.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,26 @@ public function test_taxonomy_classes_hit_cache() {
135135

136136
$this->assertSame( $num_queries, get_num_queries() );
137137
}
138+
139+
/**
140+
* @ticket 64247
141+
*/
142+
public function test_list_return_value_when_duplicate_classes() {
143+
144+
// Filter 'post_class' to add a duplicate which should be removed by `array_unique()`.
145+
add_filter(
146+
'post_class',
147+
function ( $classes ) {
148+
return array_merge(
149+
array( 'duplicate-class', 'duplicate-class' ),
150+
$classes
151+
);
152+
}
153+
);
154+
155+
$class_list = get_post_class( 'original', $this->post_id );
156+
$this->assertTrue( array_is_list( $class_list ), 'Expected get_post_class() to return list.' );
157+
$this->assertContains( 'duplicate-class', $class_list );
158+
$this->assertContains( 'original', $class_list );
159+
}
138160
}

tests/phpunit/tests/rest-api/rest-posts-controller.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2775,6 +2775,36 @@ public function test_prepare_item_override_excerpt_length() {
27752775
);
27762776
}
27772777

2778+
/**
2779+
* Test that the `class_list` property is a list.
2780+
*
2781+
* @ticket 64247
2782+
*
2783+
* @covers WP_REST_Posts_Controller::prepare_item_for_response
2784+
*/
2785+
public function test_class_list_is_list() {
2786+
$post_id = self::factory()->post->create();
2787+
2788+
// Filter 'post_class' to add a duplicate which should be removed by `array_unique()`.
2789+
add_filter(
2790+
'post_class',
2791+
function ( $classes ) {
2792+
return array_merge(
2793+
array( 'duplicate-class', 'duplicate-class' ),
2794+
$classes
2795+
);
2796+
}
2797+
);
2798+
2799+
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id );
2800+
$response = rest_do_request( $request );
2801+
$data = $response->get_data();
2802+
2803+
$this->assertArrayHasKey( 'class_list', $data );
2804+
$this->assertContains( 'duplicate-class', $data['class_list'] );
2805+
$this->assertTrue( array_is_list( $data['class_list'] ), 'Expected class_list to be a list.' );
2806+
}
2807+
27782808
public function test_create_item() {
27792809
wp_set_current_user( self::$editor_id );
27802810

0 commit comments

Comments
 (0)