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
34 changes: 5 additions & 29 deletions src/wp-admin/includes/dashboard-on-this-day.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,21 @@
* 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.
* routine. The widget is always registered and always visible, keeping its
* Screen Options entry consistent with what is shown on screen. When there are
* no matching posts, a placeholder message is displayed. Users who do not want
* the widget can hide it via Screen Options, and that preference is preserved.
*
* @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 +37,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
29 changes: 1 addition & 28 deletions tests/phpunit/tests/admin/wpOnThisDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private static function get_date_query_clause( $date ) {
/**
* @covers ::wp_dashboard_on_this_day_setup
*/
public function test_setup_always_registers_widget_and_postbox_class_filter() {
public function test_setup_always_registers_widget() {
$this->set_up_dashboard_screen();

$user_id = self::factory()->user->create( array( 'role' => 'author' ) );
Expand All @@ -106,33 +106,6 @@ public function test_setup_always_registers_widget_and_postbox_class_filter() {

$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'
)
);
}

/**
* @covers ::wp_dashboard_on_this_day_postbox_classes
*/
public function test_postbox_classes_hides_widget_without_matching_posts() {
$user_id = self::factory()->user->create( array( 'role' => 'author' ) );
wp_set_current_user( $user_id );

$this->assertContains( 'hidden', wp_dashboard_on_this_day_postbox_classes( array( '' ) ) );
}

/**
* @covers ::wp_dashboard_on_this_day_postbox_classes
*/
public function test_postbox_classes_does_not_hide_widget_with_matching_posts() {
$user_id = self::factory()->user->create( array( 'role' => 'author' ) );
wp_set_current_user( $user_id );
$this->create_matching_post( $user_id );

$this->assertNotContains( 'hidden', wp_dashboard_on_this_day_postbox_classes( array( '' ) ) );
}

/**
Expand Down
Loading