diff --git a/src/wp-admin/includes/dashboard-on-this-day.php b/src/wp-admin/includes/dashboard-on-this-day.php index e9557a60720c8..948e128f72c59 100644 --- a/src/wp-admin/includes/dashboard-on-this-day.php +++ b/src/wp-admin/includes/dashboard-on-this-day.php @@ -7,48 +7,6 @@ * @since 7.1.0 */ -/** - * Registers the On This Day dashboard widget. - * - * Designed to be the single entry point called from the dashboard setup - * routine. The widget is always registered so that it remains available in - * Screen Options and keeps its user-customized position. When there are no - * matching posts, a marker class is added to the postbox so the widget can be - * hidden with CSS. - * - * @since 7.1.0 - */ -function wp_dashboard_on_this_day_setup() { - add_filter( 'postbox_classes_dashboard_wp_dashboard_on_this_day', 'wp_dashboard_on_this_day_postbox_classes' ); - - wp_add_dashboard_widget( - 'wp_dashboard_on_this_day', - __( 'On This Day' ), - 'wp_dashboard_on_this_day' - ); -} - -/** - * Hides the On This Day postbox when there are no posts to show. - * - * Adds the core `hidden` class so the widget stays registered — preserving its - * Screen Options entry and user-customized position — while being hidden when - * empty. A user can still reveal it via Screen Options, in which case the - * placeholder message is shown. - * - * @since 7.1.0 - * - * @param string[] $classes An array of postbox classes. - * @return string[] Filtered postbox classes. - */ -function wp_dashboard_on_this_day_postbox_classes( $classes ) { - if ( empty( wp_dashboard_on_this_day_get_posts() ) ) { - $classes[] = 'hidden'; - } - - return $classes; -} - /** * Renders the On This Day dashboard widget. * @@ -60,9 +18,20 @@ function wp_dashboard_on_this_day() { $posts = wp_dashboard_on_this_day_get_posts(); if ( empty( $posts ) ) { - // Placeholder shown when a user reveals the hidden widget via Screen - // Options on a day with no matching posts. - echo '

' . esc_html__( 'No posts were published on this day in previous years.' ) . '

'; + // Placeholder shown on a day with no matching posts in previous years. + echo '

'; + + if ( current_user_can( 'edit_posts' ) ) { + printf( + /* translators: %s: URL to the new post screen. */ + __( 'No posts were published on this day in previous years. Write one today, and be reminded about it next year.' ), + esc_url( admin_url( 'post-new.php' ) ) + ); + } else { + echo esc_html__( 'No posts were published on this day in previous years.' ); + } + + echo '

'; return; } diff --git a/src/wp-admin/includes/dashboard.php b/src/wp-admin/includes/dashboard.php index 5fdbaf7a4fa40..a0c2a23189644 100644 --- a/src/wp-admin/includes/dashboard.php +++ b/src/wp-admin/includes/dashboard.php @@ -89,11 +89,11 @@ function wp_dashboard_setup() { } // On This Day. - if ( ! function_exists( 'wp_dashboard_on_this_day_setup' ) ) { + if ( ! function_exists( 'wp_dashboard_on_this_day' ) ) { require_once ABSPATH . 'wp-admin/includes/dashboard-on-this-day.php'; } - wp_dashboard_on_this_day_setup(); + wp_add_dashboard_widget( 'wp_dashboard_on_this_day', __( 'On This Day' ), 'wp_dashboard_on_this_day' ); // WordPress Events and News. wp_add_dashboard_widget( 'dashboard_primary', __( 'WordPress Events and News' ), 'wp_dashboard_events_news' ); diff --git a/tests/phpunit/tests/admin/wpDashboardOnThisDay.php b/tests/phpunit/tests/admin/wpDashboardOnThisDay.php index a2b1cdbfaff1f..728b9bcef64d0 100644 --- a/tests/phpunit/tests/admin/wpDashboardOnThisDay.php +++ b/tests/phpunit/tests/admin/wpDashboardOnThisDay.php @@ -10,6 +10,8 @@ class Tests_Admin_wpDashboardOnThisDay extends WP_UnitTestCase { protected static int $other_user_id; + protected static int $subscriber_id; + public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { require_once ABSPATH . 'wp-admin/includes/dashboard-on-this-day.php'; @@ -25,30 +27,24 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 'role' => 'author', ) ); + self::$subscriber_id = $factory->user->create( + array( + 'display_name' => 'Reader', + 'role' => 'subscriber', + ) + ); } public static function wpTearDownAfterClass() { self::delete_user( self::$user_id ); self::delete_user( self::$other_user_id ); + self::delete_user( self::$subscriber_id ); } - public function tear_down() { - unset( $GLOBALS['wp_meta_boxes']['dashboard'] ); - - parent::tear_down(); - } - - /** - * Sets up the globals needed to register dashboard widgets. - */ - private function set_up_dashboard_screen() { - if ( ! function_exists( 'wp_add_dashboard_widget' ) ) { - require_once ABSPATH . 'wp-admin/includes/dashboard.php'; - } + public function set_up() { + parent::set_up(); set_current_screen( 'dashboard' ); - - $GLOBALS['wp_meta_boxes']['dashboard'] = array(); } /** @@ -119,71 +115,6 @@ private static function get_date_query_clause( string $date ): array { return _wp_dashboard_on_this_day_date_query_clause( new DateTimeImmutable( $date, wp_timezone() ) ); } - /** - * @ticket 65116 - * - * @covers ::wp_dashboard_on_this_day_setup - */ - public function test_setup_always_registers_widget_and_postbox_class_filter() { - $this->set_up_dashboard_screen(); - - wp_set_current_user( self::$user_id ); - - wp_dashboard_on_this_day_setup(); - - $dashboard_widgets = $GLOBALS['wp_meta_boxes']['dashboard']['normal']['core'] ?? array(); - - $this->assertArrayHasKey( 'wp_dashboard_on_this_day', $dashboard_widgets ); - $this->assertSame( 'On This Day', $dashboard_widgets['wp_dashboard_on_this_day']['title'] ); - $this->assertNotFalse( - has_filter( - 'postbox_classes_dashboard_wp_dashboard_on_this_day', - 'wp_dashboard_on_this_day_postbox_classes' - ) - ); - } - - /** - * @ticket 65116 - * - * @covers ::wp_dashboard_on_this_day_postbox_classes - */ - public function test_postbox_classes_hides_widget_without_matching_posts() { - wp_set_current_user( self::$user_id ); - - $this->assertContains( 'hidden', wp_dashboard_on_this_day_postbox_classes( array( '' ) ) ); - } - - /** - * @ticket 65116 - * - * @covers ::wp_dashboard_on_this_day_postbox_classes - */ - public function test_postbox_classes_does_not_hide_widget_with_matching_posts() { - wp_set_current_user( self::$user_id ); - $this->create_matching_post( self::$user_id ); - - $this->assertNotContains( 'hidden', wp_dashboard_on_this_day_postbox_classes( array( '' ) ) ); - } - - /** - * @ticket 65116 - * - * @covers ::wp_dashboard_on_this_day_setup - */ - public function test_setup_adds_dashboard_widget_with_matching_post_from_another_author() { - $this->set_up_dashboard_screen(); - - wp_set_current_user( self::$user_id ); - $this->create_matching_post( self::$other_user_id ); - - wp_dashboard_on_this_day_setup(); - - $dashboard_widgets = $GLOBALS['wp_meta_boxes']['dashboard']['normal']['core'] ?? array(); - - $this->assertArrayHasKey( 'wp_dashboard_on_this_day', $dashboard_widgets ); - } - /** * @ticket 65116 * @@ -255,9 +186,28 @@ public function test_widget_outputs_placeholder_without_matching_posts() { $output = ob_get_clean(); $this->assertStringContainsString( 'No posts were published on this day in previous years.', $output ); + $this->assertStringContainsString( 'Write one today', $output ); + $this->assertStringContainsString( admin_url( 'post-new.php' ), $output ); $this->assertStringNotContainsString( '