Make WordPress Core


Ignore:
Timestamp:
10/20/2025 07:04:44 PM (8 weeks ago)
Author:
westonruter
Message:

Posts, Post Types: Add post_states_string filter for HTML string of post states.

Developed in https://github.com/WordPress/wordpress-develop/pull/10000

Props paulbonneau, mukesh27, westonruter, SirLouen, dmsnell, brandbrilliance, shsajalchowdhury, aialvi, ugyensupport.
Fixes #51403.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post/getPostStatus.php

    r51587 r60986  
    165165        );
    166166    }
     167
     168    /**
     169     * Ensure the `post_states_string` filter works to modify post state output.
     170     *
     171     * @ticket 51403
     172     *
     173     * @dataProvider data_filter_post_states_string_should_enable_post_state_html_output_modification
     174     *
     175     * @covers ::_post_states
     176     *
     177     * @param string $post_state The post state to test.
     178     */
     179    public function test_filter_post_states_string_should_enable_post_state_html_output_modification( $post_state ) {
     180        $post = get_post( self::$post_ids[ $post_state ] );
     181
     182        $original_output = _post_states( $post, false );
     183
     184        if ( count( get_post_states( $post ) ) === 0 ) {
     185            $text_to_append = '&mdash; <span class="post-state">Sample state</span>';
     186        } else {
     187            $text_to_append = '<span class="post-state">, Sample state</span>';
     188        }
     189
     190        add_filter(
     191            'post_states_string',
     192            function ( $post_states_string, $post_states, $filtered_post ) use ( $text_to_append, $post ) {
     193                $this->assertIsString( $post_states_string, 'Expected first filter arg to be a string.' );
     194                $this->assertIsArray( $post_states, 'Expected second filter arg to be an array.' );
     195                $this->assertInstanceOf( WP_Post::class, $filtered_post, 'Expected third filter arg to be a WP_Post' );
     196                $this->assertSame( $post->ID, $filtered_post->ID, 'Expected the third filter arg to be the same as the current post.' );
     197                return $post_states_string . $text_to_append;
     198            },
     199            10,
     200            3
     201        );
     202
     203        $output = _post_states( $post, false );
     204
     205        $this->assertSame( $original_output . $text_to_append, $output, 'Expected text to be appended to the original output.' );
     206    }
     207
     208    /**
     209     * Data provider for test_filter_post_states_string_should_enable_post_state_html_output_modification().
     210     *
     211     * @return array[] {
     212     *     @type string $post_state The post state to test.
     213     * }
     214     */
     215    public static function data_filter_post_states_string_should_enable_post_state_html_output_modification() {
     216        return array(
     217            array( 'publish' ),
     218            array( 'future' ),
     219            array( 'draft' ),
     220            array( 'auto-draft' ),
     221            array( 'trash' ),
     222            array( 'private' ),
     223        );
     224    }
    167225}
Note: See TracChangeset for help on using the changeset viewer.