Make WordPress Core

Changeset 61269


Ignore:
Timestamp:
11/19/2025 06:01:22 AM (2 months ago)
Author:
westonruter
Message:

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 https://github.com/WordPress/wordpress-develop/pull/10515

Props dlh, mamaduka, westonruter.
Fixes #64247.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post-template.php

    r60222 r61269  
    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
  • trunk/tests/phpunit/tests/post/getPostClass.php

    r55745 r61269  
    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}
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r60251 r61269  
    27742774            'Incorrect word count in the excerpt. Expected the excerpt to contain 44 words (43 words plus an ellipsis), but a different word count was found.'
    27752775        );
     2776    }
     2777
     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.' );
    27762806    }
    27772807
Note: See TracChangeset for help on using the changeset viewer.