Skip to content

Commit 560d3f2

Browse files
author
Paul Bearne
committed
Add support for post status descriptions and admin visibility
This update introduces a `description` field and `show_in_admin_status_list` flag to post statuses, allowing better contextual explanations and visibility control in admin interfaces. Adjustments were made to REST API endpoints, schema, and UI components to incorporate these new properties effectively, and tests were added to ensure accurate functionality.
1 parent 6af4b97 commit 560d3f2

4 files changed

Lines changed: 139 additions & 9 deletions

File tree

‎src/wp-includes/post.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,7 @@ function create_initial_post_types() {
661661
'publish',
662662
array(
663663
'label' => _x( 'Published', 'post status' ),
664+
'description' => __( 'Visible to everyone.' ),
664665
'public' => true,
665666
'_builtin' => true, /* internal use only. */
666667
/* translators: %s: Number of published posts. */
@@ -675,6 +676,7 @@ function create_initial_post_types() {
675676
'future',
676677
array(
677678
'label' => _x( 'Scheduled', 'post status' ),
679+
'description' => __( 'Publish automatically on a chosen date.' ),
678680
'protected' => true,
679681
'_builtin' => true, /* internal use only. */
680682
/* translators: %s: Number of scheduled posts. */
@@ -689,6 +691,7 @@ function create_initial_post_types() {
689691
'draft',
690692
array(
691693
'label' => _x( 'Draft', 'post status' ),
694+
'description' => __( 'Not ready to publish.' ),
692695
'protected' => true,
693696
'_builtin' => true, /* internal use only. */
694697
/* translators: %s: Number of draft posts. */
@@ -704,6 +707,7 @@ function create_initial_post_types() {
704707
'pending',
705708
array(
706709
'label' => _x( 'Pending', 'post status' ),
710+
'description' => __( 'Waiting for review before publishing.' ),
707711
'protected' => true,
708712
'_builtin' => true, /* internal use only. */
709713
/* translators: %s: Number of pending posts. */
@@ -719,6 +723,7 @@ function create_initial_post_types() {
719723
'private',
720724
array(
721725
'label' => _x( 'Private', 'post status' ),
726+
'description' => __( 'Only visible to site admins and editors.' ),
722727
'private' => true,
723728
'_builtin' => true, /* internal use only. */
724729
/* translators: %s: Number of private posts. */
@@ -1424,6 +1429,8 @@ function _wp_privacy_statuses() {
14241429
*
14251430
* @type bool|string $label A descriptive name for the post status marked
14261431
* for translation. Defaults to value of $post_status.
1432+
* @type string $description A short description of what the post status does.
1433+
* Default empty string.
14271434
* @type array|false $label_count Nooped plural text from _n_noop() to provide the singular
14281435
* and plural forms of the label for counts. Default false
14291436
* which means the `$label` argument will be used for both
@@ -1464,6 +1471,7 @@ function register_post_status( $post_status, $args = array() ) {
14641471
// Args prefixed with an underscore are reserved for internal use.
14651472
$defaults = array(
14661473
'label' => false,
1474+
'description' => '',
14671475
'label_count' => false,
14681476
'exclude_from_search' => null,
14691477
'_builtin' => false,

‎src/wp-includes/rest-api/endpoints/class-wp-rest-post-statuses-controller.php‎

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,14 @@ public function prepare_item_for_response( $item, $request ) {
250250
$data['show_in_list'] = (bool) $status->show_in_admin_all_list;
251251
}
252252

253+
if ( in_array( 'show_in_admin_status_list', $fields, true ) ) {
254+
$data['show_in_admin_status_list'] = (bool) $status->show_in_admin_status_list;
255+
}
256+
257+
if ( in_array( 'description', $fields, true ) ) {
258+
$data['description'] = ! empty( $status->description ) ? $status->description : '';
259+
}
260+
253261
if ( in_array( 'slug', $fields, true ) ) {
254262
$data['slug'] = $status->name;
255263
}
@@ -302,49 +310,61 @@ public function get_item_schema() {
302310
'title' => 'status',
303311
'type' => 'object',
304312
'properties' => array(
305-
'name' => array(
313+
'name' => array(
306314
'description' => __( 'The title for the status.' ),
307315
'type' => 'string',
308316
'context' => array( 'embed', 'view', 'edit' ),
309317
'readonly' => true,
310318
),
311-
'private' => array(
319+
'private' => array(
312320
'description' => __( 'Whether posts with this status should be private.' ),
313321
'type' => 'boolean',
314322
'context' => array( 'edit' ),
315323
'readonly' => true,
316324
),
317-
'protected' => array(
325+
'protected' => array(
318326
'description' => __( 'Whether posts with this status should be protected.' ),
319327
'type' => 'boolean',
320328
'context' => array( 'edit' ),
321329
'readonly' => true,
322330
),
323-
'public' => array(
331+
'public' => array(
324332
'description' => __( 'Whether posts of this status should be shown in the front end of the site.' ),
325333
'type' => 'boolean',
326334
'context' => array( 'view', 'edit' ),
327335
'readonly' => true,
328336
),
329-
'queryable' => array(
337+
'queryable' => array(
330338
'description' => __( 'Whether posts with this status should be publicly-queryable.' ),
331339
'type' => 'boolean',
332340
'context' => array( 'view', 'edit' ),
333341
'readonly' => true,
334342
),
335-
'show_in_list' => array(
343+
'show_in_list' => array(
336344
'description' => __( 'Whether to include posts in the edit listing for their post type.' ),
337345
'type' => 'boolean',
338346
'context' => array( 'edit' ),
339347
'readonly' => true,
340348
),
341-
'slug' => array(
349+
'show_in_admin_status_list' => array(
350+
'description' => __( 'Whether to show the status in admin status lists.' ),
351+
'type' => 'boolean',
352+
'context' => array( 'view', 'edit' ),
353+
'readonly' => true,
354+
),
355+
'description' => array(
356+
'description' => __( 'A description of what the status is for.' ),
357+
'type' => 'string',
358+
'context' => array( 'view', 'edit' ),
359+
'readonly' => true,
360+
),
361+
'slug' => array(
342362
'description' => __( 'An alphanumeric identifier for the status.' ),
343363
'type' => 'string',
344364
'context' => array( 'embed', 'view', 'edit' ),
345365
'readonly' => true,
346366
),
347-
'date_floating' => array(
367+
'date_floating' => array(
348368
'description' => __( 'Whether posts of this status may have floating published dates.' ),
349369
'type' => 'boolean',
350370
'context' => array( 'view', 'edit' ),
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
/**
4+
* @group post
5+
*
6+
* @covers ::register_post_status
7+
*/
8+
class Tests_Post_RegisterPostStatus extends WP_UnitTestCase {
9+
10+
/**
11+
* Remove test statuses after each test.
12+
*/
13+
public function tear_down() {
14+
global $wp_post_statuses;
15+
16+
unset(
17+
$wp_post_statuses['test_custom_status'],
18+
$wp_post_statuses['test_custom_status_with_desc']
19+
);
20+
21+
parent::tear_down();
22+
}
23+
24+
/**
25+
* Test default property values when registering a post status.
26+
*
27+
* @ticket 3144
28+
*/
29+
public function test_register_post_status_defaults() {
30+
$status = register_post_status( 'test_custom_status' );
31+
32+
$this->assertSame( 'test_custom_status', $status->name );
33+
$this->assertSame( 'test_custom_status', $status->label );
34+
$this->assertSame( '', $status->description );
35+
$this->assertFalse( $status->public );
36+
$this->assertTrue( $status->internal );
37+
$this->assertFalse( $status->protected );
38+
$this->assertFalse( $status->private );
39+
$this->assertFalse( $status->publicly_queryable );
40+
$this->assertTrue( $status->exclude_from_search );
41+
$this->assertFalse( $status->show_in_admin_all_list );
42+
$this->assertFalse( $status->show_in_admin_status_list );
43+
$this->assertFalse( $status->date_floating );
44+
$this->assertFalse( $status->_builtin );
45+
}
46+
47+
/**
48+
* Test registering a post status with a custom description.
49+
*
50+
* @ticket 3144
51+
*/
52+
public function test_register_post_status_with_description() {
53+
$description = 'A custom post status description.';
54+
$status = register_post_status(
55+
'test_custom_status_with_desc',
56+
array(
57+
'label' => 'Custom Status',
58+
'description' => $description,
59+
'public' => true,
60+
)
61+
);
62+
63+
$this->assertSame( $description, $status->description );
64+
$this->assertSame( $status, get_post_status_object( 'test_custom_status_with_desc' ) );
65+
}
66+
67+
/**
68+
* Test that core post statuses have their descriptions populated.
69+
*
70+
* @ticket 3144
71+
*
72+
* @dataProvider data_core_post_statuses_descriptions
73+
*
74+
* @param string $status_name Post status name.
75+
* @param string $expected_description Expected description.
76+
*/
77+
public function test_core_post_statuses_descriptions( $status_name, $expected_description ) {
78+
$status = get_post_status_object( $status_name );
79+
80+
$this->assertNotNull( $status );
81+
$this->assertSame( $expected_description, $status->description );
82+
}
83+
84+
/**
85+
* Data provider for core post status descriptions.
86+
*
87+
* @return array<string, array{string, string}>
88+
*/
89+
public function data_core_post_statuses_descriptions() {
90+
return array(
91+
'publish' => array( 'publish', 'Visible to everyone.' ),
92+
'future' => array( 'future', 'Publish automatically on a chosen date.' ),
93+
'draft' => array( 'draft', 'Not ready to publish.' ),
94+
'pending' => array( 'pending', 'Waiting for review before publishing.' ),
95+
'private' => array( 'private', 'Only visible to site admins and editors.' ),
96+
);
97+
}
98+
}

‎tests/phpunit/tests/rest-api/rest-post-statuses-controller.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,15 @@ public function test_get_item_schema() {
151151
$response = rest_get_server()->dispatch( $request );
152152
$data = $response->get_data();
153153
$properties = $data['schema']['properties'];
154-
$this->assertCount( 8, $properties );
154+
$this->assertCount( 10, $properties );
155155
$this->assertArrayHasKey( 'name', $properties );
156156
$this->assertArrayHasKey( 'private', $properties );
157157
$this->assertArrayHasKey( 'protected', $properties );
158158
$this->assertArrayHasKey( 'public', $properties );
159159
$this->assertArrayHasKey( 'queryable', $properties );
160160
$this->assertArrayHasKey( 'show_in_list', $properties );
161+
$this->assertArrayHasKey( 'show_in_admin_status_list', $properties );
162+
$this->assertArrayHasKey( 'description', $properties );
161163
$this->assertArrayHasKey( 'slug', $properties );
162164
$this->assertArrayHasKey( 'date_floating', $properties );
163165
}
@@ -209,6 +211,8 @@ protected function check_post_status_obj( $status_obj, $data, $links ) {
209211
$this->assertSame( $status_obj->public, $data['public'] );
210212
$this->assertSame( $status_obj->publicly_queryable, $data['queryable'] );
211213
$this->assertSame( $status_obj->show_in_admin_all_list, $data['show_in_list'] );
214+
$this->assertSame( $status_obj->show_in_admin_status_list, $data['show_in_admin_status_list'] );
215+
$this->assertSame( $status_obj->description, $data['description'] );
212216
$this->assertSame( $status_obj->name, $data['slug'] );
213217
$this->assertSameSets(
214218
array(

0 commit comments

Comments
 (0)