Make WordPress Core

Changeset 62977


Ignore:
Timestamp:
08/03/2026 07:59:48 PM (6 weeks ago)
Author:
joedolson
Message:

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 https://github.com/WordPress/wordpress-develop/pull/12575

Props iamchitti, mirmpro, shailu25, ugyensupport, iamraju, nazmulasif, wildworks, joedolson, mukesh27, annezazu, paaljoachim, joen.
Fixes #65647.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/dashboard-on-this-day.php

    r62968 r62977  
    99
    1010/**
    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 
    52 /**
    5311 * Renders the On This Day dashboard widget.
    5412 *
     
    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        }
  • trunk/src/wp-admin/includes/dashboard.php

    r62849 r62977  
    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.
  • trunk/tests/phpunit/tests/admin/wpDashboardOnThisDay.php

    r62968 r62977  
    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';
     
    2426                                'display_name' => 'Guest Writer',
    2527                                'role'         => 'author',
     28                        )
     29                );
     30                self::$subscriber_id = $factory->user->create(
     31                        array(
     32                                'display_name' => 'Reader',
     33                                'role'         => 'subscriber',
    2634                        )
    2735                );
     
    3139                self::delete_user( self::$user_id );
    3240                self::delete_user( self::$other_user_id );
    33         }
    34 
    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                 }
     41                self::delete_user( self::$subscriber_id );
     42        }
     43
     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
     
    118114        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() ) );
    120         }
    121 
    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 );
    185116        }
    186117
     
    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 );
     192        }
     193
     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 );
    259209        }
    260210
     
    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
     
    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
     
    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
Note: See TracChangeset for help on using the changeset viewer.