Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 1 addition & 44 deletions src/wp-admin/includes/dashboard-on-this-day.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -60,8 +18,7 @@ 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.
// Placeholder shown on a day with no matching posts in previous years.
echo '<p>' . esc_html__( 'No posts were published on this day in previous years.' ) . '</p>';
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/wp-admin/includes/dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down
84 changes: 0 additions & 84 deletions tests/phpunit/tests/admin/wpDashboardOnThisDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,6 @@ public static function wpTearDownAfterClass() {
self::delete_user( self::$other_user_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';
}

set_current_screen( 'dashboard' );

$GLOBALS['wp_meta_boxes']['dashboard'] = array();
}

/**
* Creates a published post on the widget's prior-year calendar day.
*
Expand Down Expand Up @@ -114,71 +95,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
*
Expand Down
Loading