Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/wp-includes/post-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/phpunit/tests/post/getPostClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
30 changes: 30 additions & 0 deletions tests/phpunit/tests/rest-api/rest-posts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down
Loading