Skip to content

Commit 2664880

Browse files
committed
Hide when there are no posts
1 parent b4c40ed commit 2664880

2 files changed

Lines changed: 147 additions & 5 deletions

File tree

src/wp-admin/includes/class-wp-dashboard-widget-on-this-day.php

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ class WP_Dashboard_Widget_On_This_Day {
7474
* @since 7.1.0
7575
*/
7676
public static function register_widget() {
77+
if ( ! self::has_posts_for_user( get_current_user_id() ) ) {
78+
return;
79+
}
80+
7781
wp_enqueue_style( 'on-this-day' );
7882
wp_enqueue_script( 'on-this-day' );
7983

@@ -119,14 +123,14 @@ public static function render_dashboard_widget() {
119123

120124
$cached = wp_cache_get_salted( $cache_key, self::CACHE_GROUP, $cache_salt );
121125
if ( ! is_string( $cached ) ) {
122-
$posts = self::get_posts( $user_id );
126+
$posts = self::get_cached_posts( $user_id );
123127

124-
ob_start();
125128
if ( empty( $posts ) ) {
126-
self::render_empty_state();
127-
} else {
128-
self::render_posts( $posts );
129+
return;
129130
}
131+
132+
ob_start();
133+
self::render_posts( $posts );
130134
$cached = ob_get_clean();
131135

132136
wp_cache_set_salted( $cache_key, $cached, self::CACHE_GROUP, $cache_salt, DAY_IN_SECONDS );
@@ -194,6 +198,49 @@ public static function get_posts( $user_id ) {
194198
return $query->posts;
195199
}
196200

201+
/**
202+
* Determines whether an author has posts available for the widget.
203+
*
204+
* @since 7.1.0
205+
*
206+
* @param int $user_id Author ID to query posts for.
207+
* @return bool True when matching posts exist, false otherwise.
208+
*/
209+
public static function has_posts_for_user( $user_id ) {
210+
return ! empty( self::get_cached_posts( $user_id ) );
211+
}
212+
213+
/**
214+
* Retrieves cached posts for the widget.
215+
*
216+
* @since 7.1.0
217+
*
218+
* @param int $user_id Author ID to query posts for.
219+
* @return WP_Post[] Array of posts ordered by newest first.
220+
*/
221+
protected static function get_cached_posts( $user_id ) {
222+
$cache_salt = array(
223+
current_time( 'Y-m-d' ),
224+
wp_cache_get_last_changed( 'posts' ),
225+
);
226+
$cache_key = sprintf(
227+
'query_posts:v%d:%d',
228+
self::CACHE_VERSION,
229+
(int) $user_id
230+
);
231+
232+
$cached = wp_cache_get_salted( $cache_key, self::CACHE_GROUP, $cache_salt );
233+
if ( is_array( $cached ) ) {
234+
return $cached;
235+
}
236+
237+
$posts = self::get_posts( $user_id );
238+
239+
wp_cache_set_salted( $cache_key, $posts, self::CACHE_GROUP, $cache_salt, DAY_IN_SECONDS );
240+
241+
return $posts;
242+
}
243+
197244
/**
198245
* Builds date query clauses for each day in the 7-day window
199246
* centered on today.

tests/phpunit/tests/admin/wpOnThisDay.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
2323
}
2424
}
2525

26+
public function tear_down() {
27+
unset( $GLOBALS['wp_meta_boxes']['dashboard'] );
28+
wp_dequeue_style( 'on-this-day' );
29+
wp_dequeue_script( 'on-this-day' );
30+
31+
parent::tear_down();
32+
}
33+
2634
/**
2735
* Invokes WP_Dashboard_Widget_On_This_Day::extract_excerpt_text().
2836
*
@@ -34,6 +42,93 @@ private static function extract_excerpt_text( $source, $max_chars = 160 ) {
3442
return self::$extract_excerpt_text->invoke( null, $source, $max_chars );
3543
}
3644

45+
/**
46+
* Sets up the globals needed to register dashboard widgets.
47+
*/
48+
private function set_up_dashboard_screen() {
49+
if ( ! function_exists( 'wp_add_dashboard_widget' ) ) {
50+
require_once ABSPATH . 'wp-admin/includes/dashboard.php';
51+
}
52+
53+
set_current_screen( 'dashboard' );
54+
55+
$GLOBALS['wp_meta_boxes']['dashboard'] = array();
56+
57+
wp_dequeue_style( 'on-this-day' );
58+
wp_dequeue_script( 'on-this-day' );
59+
}
60+
61+
/**
62+
* Creates a published post in the widget's prior-year date window.
63+
*
64+
* @param int $author_id Author ID.
65+
* @return int Post ID.
66+
*/
67+
private function create_matching_post( $author_id ) {
68+
$post_date = current_datetime()->modify( '-1 year' )->format( 'Y-m-d H:i:s' );
69+
70+
return self::factory()->post->create(
71+
array(
72+
'post_author' => $author_id,
73+
'post_date' => $post_date,
74+
'post_date_gmt' => get_gmt_from_date( $post_date ),
75+
'post_status' => 'publish',
76+
'post_title' => 'A memory from last year',
77+
)
78+
);
79+
}
80+
81+
/**
82+
* @covers ::register_widget
83+
*/
84+
public function test_register_widget_does_not_add_dashboard_widget_without_matching_posts() {
85+
$this->set_up_dashboard_screen();
86+
87+
$user_id = self::factory()->user->create( array( 'role' => 'author' ) );
88+
wp_set_current_user( $user_id );
89+
90+
WP_Dashboard_Widget_On_This_Day::register_widget();
91+
92+
$dashboard_widgets = $GLOBALS['wp_meta_boxes']['dashboard']['normal']['core'] ?? array();
93+
94+
$this->assertArrayNotHasKey( 'wp_dashboard_on_this_day', $dashboard_widgets );
95+
$this->assertFalse( wp_style_is( 'on-this-day', 'enqueued' ) );
96+
$this->assertFalse( wp_script_is( 'on-this-day', 'enqueued' ) );
97+
}
98+
99+
/**
100+
* @covers ::register_widget
101+
*/
102+
public function test_register_widget_adds_dashboard_widget_with_matching_posts() {
103+
$this->set_up_dashboard_screen();
104+
105+
$user_id = self::factory()->user->create( array( 'role' => 'author' ) );
106+
wp_set_current_user( $user_id );
107+
$this->create_matching_post( $user_id );
108+
109+
WP_Dashboard_Widget_On_This_Day::register_widget();
110+
111+
$dashboard_widgets = $GLOBALS['wp_meta_boxes']['dashboard']['normal']['core'] ?? array();
112+
113+
$this->assertArrayHasKey( 'wp_dashboard_on_this_day', $dashboard_widgets );
114+
$this->assertTrue( wp_style_is( 'on-this-day', 'enqueued' ) );
115+
$this->assertTrue( wp_script_is( 'on-this-day', 'enqueued' ) );
116+
}
117+
118+
/**
119+
* @covers ::render_dashboard_widget
120+
*/
121+
public function test_render_dashboard_widget_outputs_nothing_without_matching_posts() {
122+
$user_id = self::factory()->user->create( array( 'role' => 'author' ) );
123+
wp_set_current_user( $user_id );
124+
125+
ob_start();
126+
WP_Dashboard_Widget_On_This_Day::render_dashboard_widget();
127+
$output = ob_get_clean();
128+
129+
$this->assertSame( '', $output );
130+
}
131+
37132
/**
38133
* @dataProvider data_extract_excerpt_text_strips_html_formatting
39134
*

0 commit comments

Comments
 (0)