Skip to content

Commit 20b1cf6

Browse files
committed
Posts, Post Types: Add post_states_string filter for HTML string of post states.
Developed in #10000 Props paulbonneau, mukesh27, westonruter, SirLouen, dmsnell, brandbrilliance, shsajalchowdhury, aialvi, ugyensupport. Fixes #51403. git-svn-id: https://develop.svn.wordpress.org/trunk@60986 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 9b9049c commit 20b1cf6

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/wp-admin/includes/template.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2265,6 +2265,17 @@ function _post_states( $post, $display = true ) {
22652265
}
22662266
}
22672267

2268+
/**
2269+
* Filters the HTML string of post states.
2270+
*
2271+
* @since 6.9.0
2272+
*
2273+
* @param string $post_states_string The post states HTML string.
2274+
* @param string[] $post_states The post states.
2275+
* @param WP_Post $post The current post object.
2276+
*/
2277+
$post_states_string = apply_filters( 'post_states_string', $post_states_string, $post_states, $post );
2278+
22682279
if ( $display ) {
22692280
echo $post_states_string;
22702281
}

tests/phpunit/tests/post/getPostStatus.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,62 @@ public function data_get_post_status_after_trashing() {
164164
array( 'delete-attachment', 'publish', 'publish' ),
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
}

0 commit comments

Comments
 (0)