Skip to content

Commit 2080a8d

Browse files
committed
Post Types: Add 'at_a_glance' property and update dashboard widget logic. Refresh: 45035.diff
1 parent 6cb3085 commit 2080a8d

File tree

4 files changed

+52
-13
lines changed

4 files changed

+52
-13
lines changed

src/wp-admin/includes/dashboard.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -302,22 +302,25 @@ function wp_dashboard_right_now() {
302302
<div class="main">
303303
<ul>
304304
<?php
305-
// Posts and Pages.
306-
foreach ( array( 'post', 'page' ) as $post_type ) {
307-
$num_posts = wp_count_posts( $post_type );
308-
309-
if ( $num_posts && $num_posts->publish ) {
310-
if ( 'post' === $post_type ) {
311-
/* translators: %s: Number of posts. */
312-
$text = _n( '%s Post', '%s Posts', $num_posts->publish );
313-
} else {
314-
/* translators: %s: Number of pages. */
315-
$text = _n( '%s Page', '%s Pages', $num_posts->publish );
316-
}
305+
// At a Glance Post Types.
306+
foreach ( get_post_types( array( 'at_a_glance' => true ) ) as $post_type ) {
307+
$num_posts = wp_count_posts( $post_type );
308+
$num_post_published = intval( $num_posts->publish );
317309

318-
$text = sprintf( $text, number_format_i18n( $num_posts->publish ) );
310+
if ( $num_posts && $num_post_published ) {
319311
$post_type_object = get_post_type_object( $post_type );
320312

313+
if ( ! $post_type_object ) {
314+
continue;
315+
}
316+
if ( 1 === $num_post_published ) {
317+
$post_label = $post_type_object->labels->singular_name;
318+
} else {
319+
$post_label = $post_type_object->labels->name;
320+
}
321+
/* translators: %d: Number of posts, %s post label. */
322+
$text = sprintf( '%d %s', number_format_i18n( $num_post_published ), $post_label );
323+
321324
if ( $post_type_object && current_user_can( $post_type_object->cap->edit_posts ) ) {
322325
printf( '<li class="%1$s-count"><a href="edit.php?post_type=%1$s">%2$s</a></li>', $post_type, $text );
323326
} else {

src/wp-includes/class-wp-post-type.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,15 @@ final class WP_Post_Type {
147147
*/
148148
public $show_in_menu = null;
149149

150+
/**
151+
* Makes this post type visible in the At a Glance dashboard widget.
152+
*
153+
* Default is the value of $show_in_menu.
154+
*
155+
* @var bool $at_a_glance
156+
*/
157+
public $at_a_glance = null;
158+
150159
/**
151160
* Makes this post type available for selection in navigation menus.
152161
*
@@ -553,6 +562,7 @@ public function set_props( $args ) {
553562
'rest_base' => false,
554563
'rest_namespace' => false,
555564
'rest_controller_class' => false,
565+
'at_a_glance' => null,
556566
'autosave_rest_controller_class' => false,
557567
'revisions_rest_controller_class' => false,
558568
'late_route_registration' => false,
@@ -591,6 +601,11 @@ public function set_props( $args ) {
591601
$args['show_in_menu'] = $args['show_ui'];
592602
}
593603

604+
// If not set, default to the setting for show_in_menu
605+
if ( null === $args['at_a_glance'] ) {
606+
$args['at_a_glance'] = (bool) $args['show_in_menu'];
607+
}
608+
594609
// If not set, default to the setting for 'show_in_menu'.
595610
if ( null === $args['show_in_admin_bar'] ) {
596611
$args['show_in_admin_bar'] = (bool) $args['show_in_menu'];

src/wp-includes/post.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ function create_initial_post_types() {
103103
),
104104
'public' => true,
105105
'show_ui' => true,
106+
'at_a_glance' => false,
106107
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
107108
'_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
108109
'capability_type' => 'post',

tests/phpunit/tests/post/types.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,4 +676,24 @@ public function test_register_post_type_override_is_embeddable() {
676676
);
677677
$this->assertFalse( $post_type->embeddable, 'Post type should not be embeddable even though it is public' );
678678
}
679+
680+
/**
681+
* @ticket 45035
682+
* @covers ::register_post_type()
683+
*/
684+
public function test_register_post_type_at_a_glance_should_default_to_value_of_show_in_menu() {
685+
/*
686+
* 'public' Default is false
687+
* 'show_ui' Default is null ('public')
688+
* 'show_in_menu' Default is null ('show_ui' > 'public')
689+
* 'at_a_glance' Default is null ('show_in_menu' > 'show_ui' > 'public')
690+
*/
691+
$args = register_post_type( $this->post_type, array( 'public' => $public = false ) );
692+
// Should fall back to 'show_in_menu'.
693+
$this->assertSame( $args->show_in_menu, $args->at_a_glance );
694+
// Should fall back to 'show_ui'.
695+
$this->assertSame( $args->show_ui, $args->at_a_glance );
696+
// Should fall back to 'public'.
697+
$this->assertSame( $public, $args->at_a_glance );
698+
}
679699
}

0 commit comments

Comments
 (0)