Skip to content

Commit 6693ea1

Browse files
committed
Widgets: Always render the On This Day widget.
While the intention was to only render the On This Day widget when it returned results, this proved to create a variety of implementation complications and some significant points of confusion for users. Remove the conditional rendering of the On This Day widget. When active without posts, display a message inviting the user to publish a new post. Developed in WordPress#12575 Props iamchitti, mirmpro, shailu25, ugyensupport, iamraju, nazmulasif, wildworks, joedolson, mukesh27, annezazu, paaljoachim, joen. Fixes #65647. git-svn-id: https://develop.svn.wordpress.org/trunk@62977 602fd350-edb4-49c9-b593-d223f7449a82
1 parent aea966d commit 6693ea1

3 files changed

Lines changed: 46 additions & 133 deletions

File tree

‎src/wp-admin/includes/dashboard-on-this-day.php‎

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,6 @@
77
* @since 7.1.0
88
*/
99

10-
/**
11-
* Registers the On This Day dashboard widget.
12-
*
13-
* Designed to be the single entry point called from the dashboard setup
14-
* routine. The widget is always registered so that it remains available in
15-
* Screen Options and keeps its user-customized position. When there are no
16-
* matching posts, a marker class is added to the postbox so the widget can be
17-
* hidden with CSS.
18-
*
19-
* @since 7.1.0
20-
*/
21-
function wp_dashboard_on_this_day_setup() {
22-
add_filter( 'postbox_classes_dashboard_wp_dashboard_on_this_day', 'wp_dashboard_on_this_day_postbox_classes' );
23-
24-
wp_add_dashboard_widget(
25-
'wp_dashboard_on_this_day',
26-
__( 'On This Day' ),
27-
'wp_dashboard_on_this_day'
28-
);
29-
}
30-
31-
/**
32-
* Hides the On This Day postbox when there are no posts to show.
33-
*
34-
* Adds the core `hidden` class so the widget stays registered — preserving its
35-
* Screen Options entry and user-customized position — while being hidden when
36-
* empty. A user can still reveal it via Screen Options, in which case the
37-
* placeholder message is shown.
38-
*
39-
* @since 7.1.0
40-
*
41-
* @param string[] $classes An array of postbox classes.
42-
* @return string[] Filtered postbox classes.
43-
*/
44-
function wp_dashboard_on_this_day_postbox_classes( $classes ) {
45-
if ( empty( wp_dashboard_on_this_day_get_posts() ) ) {
46-
$classes[] = 'hidden';
47-
}
48-
49-
return $classes;
50-
}
51-
5210
/**
5311
* Renders the On This Day dashboard widget.
5412
*
@@ -60,9 +18,20 @@ function wp_dashboard_on_this_day() {
6018
$posts = wp_dashboard_on_this_day_get_posts();
6119

6220
if ( empty( $posts ) ) {
63-
// Placeholder shown when a user reveals the hidden widget via Screen
64-
// Options on a day with no matching posts.
65-
echo '<p>' . esc_html__( 'No posts were published on this day in previous years.' ) . '</p>';
21+
// Placeholder shown on a day with no matching posts in previous years.
22+
echo '<p>';
23+
24+
if ( current_user_can( 'edit_posts' ) ) {
25+
printf(
26+
/* translators: %s: URL to the new post screen. */
27+
__( 'No posts were published on this day in previous years. <a href="%s">Write one today</a>, and be reminded about it next year.' ),
28+
esc_url( admin_url( 'post-new.php' ) )
29+
);
30+
} else {
31+
echo esc_html__( 'No posts were published on this day in previous years.' );
32+
}
33+
34+
echo '</p>';
6635
return;
6736
}
6837

‎src/wp-admin/includes/dashboard.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ function wp_dashboard_setup() {
8989
}
9090

9191
// On This Day.
92-
if ( ! function_exists( 'wp_dashboard_on_this_day_setup' ) ) {
92+
if ( ! function_exists( 'wp_dashboard_on_this_day' ) ) {
9393
require_once ABSPATH . 'wp-admin/includes/dashboard-on-this-day.php';
9494
}
9595

96-
wp_dashboard_on_this_day_setup();
96+
wp_add_dashboard_widget( 'wp_dashboard_on_this_day', __( 'On This Day' ), 'wp_dashboard_on_this_day' );
9797

9898
// WordPress Events and News.
9999
wp_add_dashboard_widget( 'dashboard_primary', __( 'WordPress Events and News' ), 'wp_dashboard_events_news' );

‎tests/phpunit/tests/admin/wpDashboardOnThisDay.php‎

Lines changed: 30 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class Tests_Admin_wpDashboardOnThisDay extends WP_UnitTestCase {
1010

1111
protected static int $other_user_id;
1212

13+
protected static int $subscriber_id;
14+
1315
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
1416
require_once ABSPATH . 'wp-admin/includes/dashboard-on-this-day.php';
1517

@@ -25,30 +27,24 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
2527
'role' => 'author',
2628
)
2729
);
30+
self::$subscriber_id = $factory->user->create(
31+
array(
32+
'display_name' => 'Reader',
33+
'role' => 'subscriber',
34+
)
35+
);
2836
}
2937

3038
public static function wpTearDownAfterClass() {
3139
self::delete_user( self::$user_id );
3240
self::delete_user( self::$other_user_id );
41+
self::delete_user( self::$subscriber_id );
3342
}
3443

35-
public function tear_down() {
36-
unset( $GLOBALS['wp_meta_boxes']['dashboard'] );
37-
38-
parent::tear_down();
39-
}
40-
41-
/**
42-
* Sets up the globals needed to register dashboard widgets.
43-
*/
44-
private function set_up_dashboard_screen() {
45-
if ( ! function_exists( 'wp_add_dashboard_widget' ) ) {
46-
require_once ABSPATH . 'wp-admin/includes/dashboard.php';
47-
}
44+
public function set_up() {
45+
parent::set_up();
4846

4947
set_current_screen( 'dashboard' );
50-
51-
$GLOBALS['wp_meta_boxes']['dashboard'] = array();
5248
}
5349

5450
/**
@@ -119,71 +115,6 @@ private static function get_date_query_clause( string $date ): array {
119115
return _wp_dashboard_on_this_day_date_query_clause( new DateTimeImmutable( $date, wp_timezone() ) );
120116
}
121117

122-
/**
123-
* @ticket 65116
124-
*
125-
* @covers ::wp_dashboard_on_this_day_setup
126-
*/
127-
public function test_setup_always_registers_widget_and_postbox_class_filter() {
128-
$this->set_up_dashboard_screen();
129-
130-
wp_set_current_user( self::$user_id );
131-
132-
wp_dashboard_on_this_day_setup();
133-
134-
$dashboard_widgets = $GLOBALS['wp_meta_boxes']['dashboard']['normal']['core'] ?? array();
135-
136-
$this->assertArrayHasKey( 'wp_dashboard_on_this_day', $dashboard_widgets );
137-
$this->assertSame( 'On This Day', $dashboard_widgets['wp_dashboard_on_this_day']['title'] );
138-
$this->assertNotFalse(
139-
has_filter(
140-
'postbox_classes_dashboard_wp_dashboard_on_this_day',
141-
'wp_dashboard_on_this_day_postbox_classes'
142-
)
143-
);
144-
}
145-
146-
/**
147-
* @ticket 65116
148-
*
149-
* @covers ::wp_dashboard_on_this_day_postbox_classes
150-
*/
151-
public function test_postbox_classes_hides_widget_without_matching_posts() {
152-
wp_set_current_user( self::$user_id );
153-
154-
$this->assertContains( 'hidden', wp_dashboard_on_this_day_postbox_classes( array( '' ) ) );
155-
}
156-
157-
/**
158-
* @ticket 65116
159-
*
160-
* @covers ::wp_dashboard_on_this_day_postbox_classes
161-
*/
162-
public function test_postbox_classes_does_not_hide_widget_with_matching_posts() {
163-
wp_set_current_user( self::$user_id );
164-
$this->create_matching_post( self::$user_id );
165-
166-
$this->assertNotContains( 'hidden', wp_dashboard_on_this_day_postbox_classes( array( '' ) ) );
167-
}
168-
169-
/**
170-
* @ticket 65116
171-
*
172-
* @covers ::wp_dashboard_on_this_day_setup
173-
*/
174-
public function test_setup_adds_dashboard_widget_with_matching_post_from_another_author() {
175-
$this->set_up_dashboard_screen();
176-
177-
wp_set_current_user( self::$user_id );
178-
$this->create_matching_post( self::$other_user_id );
179-
180-
wp_dashboard_on_this_day_setup();
181-
182-
$dashboard_widgets = $GLOBALS['wp_meta_boxes']['dashboard']['normal']['core'] ?? array();
183-
184-
$this->assertArrayHasKey( 'wp_dashboard_on_this_day', $dashboard_widgets );
185-
}
186-
187118
/**
188119
* @ticket 65116
189120
*
@@ -255,9 +186,28 @@ public function test_widget_outputs_placeholder_without_matching_posts() {
255186
$output = ob_get_clean();
256187

257188
$this->assertStringContainsString( 'No posts were published on this day in previous years.', $output );
189+
$this->assertStringContainsString( 'Write one today', $output );
190+
$this->assertStringContainsString( admin_url( 'post-new.php' ), $output );
258191
$this->assertStringNotContainsString( '<ul>', $output );
259192
}
260193

194+
/**
195+
* @ticket 65116
196+
*
197+
* @covers ::wp_dashboard_on_this_day
198+
*/
199+
public function test_widget_placeholder_omits_link_without_edit_posts_capability() {
200+
wp_set_current_user( self::$subscriber_id );
201+
202+
ob_start();
203+
wp_dashboard_on_this_day();
204+
$output = ob_get_clean();
205+
206+
$this->assertStringContainsString( 'No posts were published on this day in previous years.', $output );
207+
$this->assertStringNotContainsString( 'Write one today', $output );
208+
$this->assertStringNotContainsString( admin_url( 'post-new.php' ), $output );
209+
}
210+
261211
/**
262212
* @ticket 65116
263213
*
@@ -405,8 +355,6 @@ public function test_widget_does_not_append_excerpt_to_titled_posts() {
405355
* @covers ::wp_dashboard_on_this_day
406356
*/
407357
public function test_widget_includes_trimmed_excerpt_for_untitled_private_posts_authored_by_current_user() {
408-
$this->set_up_dashboard_screen();
409-
410358
wp_set_current_user( self::$user_id );
411359

412360
$this->create_matching_post(
@@ -440,8 +388,6 @@ public function test_widget_includes_trimmed_excerpt_for_untitled_private_posts_
440388
* @covers ::wp_dashboard_on_this_day
441389
*/
442390
public function test_widget_hides_untitled_post_excerpt_for_unreadable_posts() {
443-
$this->set_up_dashboard_screen();
444-
445391
wp_set_current_user( self::$user_id );
446392

447393
$post_id = $this->create_matching_post(
@@ -476,8 +422,6 @@ public function test_widget_hides_untitled_post_excerpt_for_unreadable_posts() {
476422
* @covers ::wp_dashboard_on_this_day
477423
*/
478424
public function test_widget_hides_untitled_post_excerpt_for_password_protected_posts() {
479-
$this->set_up_dashboard_screen();
480-
481425
wp_set_current_user( self::$user_id );
482426

483427
$this->create_matching_post(

0 commit comments

Comments
 (0)