Skip to content

Commit 552ed91

Browse files
Post Types: Add 'at_a_glance' property to post types and update dashboard widget display logic
1 parent 2183f23 commit 552ed91

5 files changed

Lines changed: 80 additions & 30 deletions

File tree

src/wp-admin/css/dashboard.css

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -445,22 +445,8 @@
445445
#dashboard_right_now li a:before,
446446
#dashboard_right_now li > span:before { /* get only the first level span to exclude screen-reader-text in mu-storage */
447447
padding: 0 5px 0 0;
448-
/* generic icon for items added by CPTs ? */
449-
content: "\f159";
450-
content: "\f159" / '';
451448
}
452449

453-
#dashboard_right_now .page-count a:before,
454-
#dashboard_right_now .page-count span:before {
455-
content: "\f105";
456-
content: "\f105" / '';
457-
}
458-
459-
#dashboard_right_now .post-count a:before,
460-
#dashboard_right_now .post-count span:before {
461-
content: "\f109";
462-
content: "\f109" / '';
463-
}
464450

465451
#dashboard_right_now .comment-count a:before {
466452
content: "\f101";

src/wp-admin/includes/dashboard.php

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -302,33 +302,52 @@ 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 Published post', '%s Published posts', $num_posts->publish );
305+
// At a Glance Post Types.
306+
foreach ( get_post_types( array( 'at_a_glance' => true ), 'objects' ) as $post_type_object ) {
307+
$post_type = $post_type_object->name;
308+
$num_posts = wp_count_posts( $post_type );
309+
$num_post_published = intval( $num_posts->publish );
310+
311+
if ( $num_posts && $num_post_published ) {
312+
if ( 1 === $num_post_published ) {
313+
$post_label = $post_type_object->labels->singular_name;
313314
} else {
314-
/* translators: %s: Number of pages. */
315-
$text = _n( '%s Published page', '%s Published pages', $num_posts->publish );
315+
$post_label = $post_type_object->labels->name;
316316
}
317+
$text = number_format_i18n( $num_post_published ) . ' ' . $post_label;
317318

318-
$text = sprintf( $text, number_format_i18n( $num_posts->publish ) );
319-
$post_type_object = get_post_type_object( $post_type );
319+
$icon_class = '';
320320

321-
if ( $post_type_object && current_user_can( $post_type_object->cap->edit_posts ) ) {
321+
if ( str_starts_with( $post_type_object->menu_icon, 'dashicons-' ) ) {
322+
$icon_class = $post_type_object->menu_icon;
323+
} elseif ( str_starts_with( $post_type_object->menu_icon, 'data:image/svg+xml;base64,' ) ) {
324+
printf(
325+
'<style>
326+
#dashboard_right_now li.%1$s-count a:before,
327+
#dashboard_right_now li.%1$s-count > span:before {
328+
content: url( \'%2$s\' );
329+
height: auto;
330+
width: 20px;
331+
}
332+
</style>',
333+
$post_type,
334+
$post_type_object->menu_icon
335+
);
336+
}
337+
338+
$class_attr = $icon_class ? sprintf( ' class="%s"', $icon_class ) : '';
339+
340+
if ( current_user_can( $post_type_object->cap->edit_posts ) ) {
322341
$url = add_query_arg(
323342
array(
324343
'post_status' => 'publish',
325344
'post_type' => $post_type,
326345
),
327346
admin_url( 'edit.php' )
328347
);
329-
printf( '<li class="%1$s-count"><a href="%2$s">%3$s</a></li>', $post_type, esc_url( $url ), esc_html( $text ) );
348+
printf( '<li class="%1$s-count"><a%3$s href="%2$s">%4$s</a></li>', $post_type, esc_url( $url ), $class_attr, esc_html( $text ) );
330349
} else {
331-
printf( '<li class="%1$s-count"><span>%2$s</span></li>', $post_type, $text );
350+
printf( '<li class="%1$s-count"><span%3$s>%2$s</span></li>', $post_type, esc_html( $text ), $class_attr );
332351
}
333352
}
334353
}

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,16 @@ 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+
* @since 7.0.0
156+
* @var bool $at_a_glance
157+
*/
158+
public $at_a_glance = null;
159+
150160
/**
151161
* Makes this post type available for selection in navigation menus.
152162
*
@@ -190,7 +200,7 @@ final class WP_Post_Type {
190200
* @since 4.6.0
191201
* @var string $menu_icon
192202
*/
193-
public $menu_icon = null;
203+
public $menu_icon;
194204

195205
/**
196206
* The string to use to build the read, edit, and delete capabilities.
@@ -539,6 +549,7 @@ public function set_props( $args ) {
539549
'show_in_admin_bar' => null,
540550
'menu_position' => null,
541551
'menu_icon' => null,
552+
'at_a_glance' => null,
542553
'capability_type' => 'post',
543554
'capabilities' => array(),
544555
'map_meta_cap' => null,
@@ -592,6 +603,16 @@ public function set_props( $args ) {
592603
$args['show_in_menu'] = $args['show_ui'];
593604
}
594605

606+
// If not set, default to the setting for show_in_menu.
607+
if ( null === $args['at_a_glance'] ) {
608+
$args['at_a_glance'] = (bool) $args['show_in_menu'];
609+
}
610+
611+
// If not set, default to the post icon.
612+
if ( null === $args['menu_icon'] ) {
613+
$args['menu_icon'] = 'dashicons-admin-post';
614+
}
615+
595616
// If not set, default to the setting for 'show_in_menu'.
596617
if ( null === $args['show_in_admin_bar'] ) {
597618
$args['show_in_admin_bar'] = (bool) $args['show_in_menu'];

src/wp-includes/post.php

Lines changed: 3 additions & 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',
@@ -1771,6 +1772,8 @@ function get_post_types( $args = array(), $output = 'names', $operator = 'and' )
17711772
* of a Dashicons helper class to use a font icon, e.g.
17721773
* 'dashicons-chart-pie'. Pass 'none' to leave div.wp-menu-image empty
17731774
* so an icon can be added via CSS. Defaults to use the posts icon.
1775+
* @type bool $at_a_glance Whether to display this post type in the 'At a Glance' dashboard widget.
1776+
* Default is value of $show_in_menu.
17741777
* @type string|array $capability_type The string to use to build the read, edit, and delete capabilities.
17751778
* May be passed as an array to allow for alternative plurals when using
17761779
* this argument as a base to construct the capabilities, e.g.

tests/phpunit/tests/post/types.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,4 +676,25 @@ 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+
*
683+
* @covers ::register_post_type()
684+
*/
685+
public function test_register_post_type_at_a_glance_should_default_to_value_of_show_in_menu() {
686+
/*
687+
* 'public' Default is false
688+
* 'show_ui' Default is null ('public')
689+
* 'show_in_menu' Default is null ('show_ui' > 'public')
690+
* 'at_a_glance' Default is null ('show_in_menu' > 'show_ui' > 'public')
691+
*/
692+
$args = register_post_type( $this->post_type, array( 'public' => $public = false ) );
693+
// Should fall back to 'show_in_menu'.
694+
$this->assertSame( $args->show_in_menu, $args->at_a_glance );
695+
// Should fall back to 'show_ui'.
696+
$this->assertSame( $args->show_ui, $args->at_a_glance );
697+
// Should fall back to 'public'.
698+
$this->assertSame( $public, $args->at_a_glance );
699+
}
679700
}

0 commit comments

Comments
 (0)