Changeset 62977
- Timestamp:
- 08/03/2026 07:59:48 PM (6 weeks ago)
- Location:
- trunk
- Files:
-
- 3 edited
-
src/wp-admin/includes/dashboard-on-this-day.php (modified) (2 diffs)
-
src/wp-admin/includes/dashboard.php (modified) (1 diff)
-
tests/phpunit/tests/admin/wpDashboardOnThisDay.php (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/includes/dashboard-on-this-day.php
r62968 r62977 9 9 10 10 /** 11 * Registers the On This Day dashboard widget.12 *13 * Designed to be the single entry point called from the dashboard setup14 * routine. The widget is always registered so that it remains available in15 * Screen Options and keeps its user-customized position. When there are no16 * matching posts, a marker class is added to the postbox so the widget can be17 * hidden with CSS.18 *19 * @since 7.1.020 */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 its35 * Screen Options entry and user-customized position — while being hidden when36 * empty. A user can still reveal it via Screen Options, in which case the37 * placeholder message is shown.38 *39 * @since 7.1.040 *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 /**53 11 * Renders the On This Day dashboard widget. 54 12 * … … 61 19 62 20 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>'; 66 35 return; 67 36 } -
trunk/src/wp-admin/includes/dashboard.php
r62849 r62977 90 90 91 91 // On This Day. 92 if ( ! function_exists( 'wp_dashboard_on_this_day _setup' ) ) {92 if ( ! function_exists( 'wp_dashboard_on_this_day' ) ) { 93 93 require_once ABSPATH . 'wp-admin/includes/dashboard-on-this-day.php'; 94 94 } 95 95 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' ); 97 97 98 98 // WordPress Events and News. -
trunk/tests/phpunit/tests/admin/wpDashboardOnThisDay.php
r62968 r62977 11 11 protected static int $other_user_id; 12 12 13 protected static int $subscriber_id; 14 13 15 public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 14 16 require_once ABSPATH . 'wp-admin/includes/dashboard-on-this-day.php'; … … 24 26 'display_name' => 'Guest Writer', 25 27 'role' => 'author', 28 ) 29 ); 30 self::$subscriber_id = $factory->user->create( 31 array( 32 'display_name' => 'Reader', 33 'role' => 'subscriber', 26 34 ) 27 35 ); … … 31 39 self::delete_user( self::$user_id ); 32 40 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(); 48 46 49 47 set_current_screen( 'dashboard' ); 50 51 $GLOBALS['wp_meta_boxes']['dashboard'] = array();52 48 } 53 49 … … 118 114 private static function get_date_query_clause( string $date ): array { 119 115 return _wp_dashboard_on_this_day_date_query_clause( new DateTimeImmutable( $date, wp_timezone() ) ); 120 }121 122 /**123 * @ticket 65116124 *125 * @covers ::wp_dashboard_on_this_day_setup126 */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 65116148 *149 * @covers ::wp_dashboard_on_this_day_postbox_classes150 */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 65116159 *160 * @covers ::wp_dashboard_on_this_day_postbox_classes161 */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 65116171 *172 * @covers ::wp_dashboard_on_this_day_setup173 */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 116 } 186 117 … … 256 187 257 188 $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 ); 258 191 $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 ); 259 209 } 260 210 … … 406 356 */ 407 357 public function test_widget_includes_trimmed_excerpt_for_untitled_private_posts_authored_by_current_user() { 408 $this->set_up_dashboard_screen();409 410 358 wp_set_current_user( self::$user_id ); 411 359 … … 441 389 */ 442 390 public function test_widget_hides_untitled_post_excerpt_for_unreadable_posts() { 443 $this->set_up_dashboard_screen();444 445 391 wp_set_current_user( self::$user_id ); 446 392 … … 477 423 */ 478 424 public function test_widget_hides_untitled_post_excerpt_for_password_protected_posts() { 479 $this->set_up_dashboard_screen();480 481 425 wp_set_current_user( self::$user_id ); 482 426
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)