diff --git a/src/wp-admin/includes/dashboard-on-this-day.php b/src/wp-admin/includes/dashboard-on-this-day.php
index 1939f8ca4b0e3..aadfcd1ff7511 100644
--- a/src/wp-admin/includes/dashboard-on-this-day.php
+++ b/src/wp-admin/includes/dashboard-on-this-day.php
@@ -114,10 +114,19 @@ function wp_dashboard_on_this_day() {
ID ) && ! post_password_required( $year_post ) ) {
+ $excerpt = get_the_excerpt( $year_post );
+
+ if ( is_string( $excerpt ) && '' !== $excerpt ) {
+ $no_title_excerpt = wp_trim_words( $excerpt, 15 );
+ }
+ }
}
$author_id = (int) $year_post->post_author;
@@ -125,7 +134,12 @@ function wp_dashboard_on_this_day() {
$show_author = '' !== trim( $author_name ) && get_current_user_id() !== $author_id;
?>
-
-
+
+
+
+
+
+
' . esc_html(
diff --git a/tests/phpunit/tests/admin/wpDashboardOnThisDay.php b/tests/phpunit/tests/admin/wpDashboardOnThisDay.php
index 471324f9648ec..66a332b8e6340 100644
--- a/tests/phpunit/tests/admin/wpDashboardOnThisDay.php
+++ b/tests/phpunit/tests/admin/wpDashboardOnThisDay.php
@@ -58,23 +58,28 @@ private function set_up_dashboard_screen() {
* @param string $title Post title.
* @param int $years_ago Number of years before today.
* @param string $time Post time.
+ * @param array $post_args Additional post arguments.
* @return int Post ID.
*/
private function create_matching_post(
int $author_id,
string $title = 'A memory from last year',
int $years_ago = 1,
- string $time = '12:00:00'
+ string $time = '12:00:00',
+ array $post_args = array()
): int {
$post_date = current_datetime()->modify( '-' . $years_ago . ' years' )->format( 'Y-m-d' ) . ' ' . $time;
return self::factory()->post->create(
- array(
- 'post_author' => $author_id,
- 'post_date' => $post_date,
- 'post_date_gmt' => get_gmt_from_date( $post_date ),
- 'post_status' => 'publish',
- 'post_title' => $title,
+ array_merge(
+ array(
+ 'post_author' => $author_id,
+ 'post_date' => $post_date,
+ 'post_date_gmt' => get_gmt_from_date( $post_date ),
+ 'post_status' => 'publish',
+ 'post_title' => $title,
+ ),
+ $post_args
)
);
}
@@ -338,6 +343,101 @@ public function test_widget_groups_posts_by_year() {
/**
* @ticket 65116
*
+ * @covers ::wp_dashboard_on_this_day
+ */
+ public function test_widget_includes_trimmed_excerpt_for_untitled_posts() {
+ wp_set_current_user( self::$user_id );
+
+ $words = array();
+ for ( $n = 1; $n <= 20; $n++ ) {
+ $words[] = 'word' . $n;
+ }
+
+ $this->create_matching_post(
+ self::$user_id,
+ '',
+ 1,
+ '12:00:00',
+ array(
+ 'post_excerpt' => implode( ' ', $words ),
+ )
+ );
+
+ ob_start();
+ wp_dashboard_on_this_day();
+ $output = ob_get_clean();
+
+ $this->assertStringContainsString( '(no title)', $output );
+ $this->assertStringContainsString( 'class="trimmed-post-excerpt"', $output );
+ $this->assertStringContainsString( 'word15', $output, 'The 15th word should be present.' );
+ $this->assertStringNotContainsString( 'word16', $output, 'The 16th word should be trimmed.' );
+ $this->assertStringContainsString( '…', $output, 'The excerpt should end with an ellipsis.' );
+ }
+
+ /**
+ * @ticket 65116
+ *
+ * @covers ::wp_dashboard_on_this_day
+ */
+ public function test_widget_includes_trimmed_excerpt_for_untitled_private_posts_authored_by_current_user() {
+ $this->set_up_dashboard_screen();
+
+ wp_set_current_user( self::$user_id );
+
+ $this->create_matching_post(
+ self::$user_id,
+ '',
+ 1,
+ '12:00:00',
+ array(
+ 'post_excerpt' => 'Readable private anniversary memory.',
+ 'post_status' => 'private',
+ )
+ );
+
+ add_filter( 'wp_dashboard_on_this_day_query_args', array( $this, 'filter_on_this_day_query_private_posts' ) );
+
+ ob_start();
+ try {
+ wp_dashboard_on_this_day();
+ $output = ob_get_clean();
+ } finally {
+ remove_filter( 'wp_dashboard_on_this_day_query_args', array( $this, 'filter_on_this_day_query_private_posts' ) );
+ }
+
+ $this->assertStringContainsString( '(no title)', $output );
+ $this->assertStringContainsString( 'class="trimmed-post-excerpt"', $output );
+ $this->assertStringContainsString( 'Readable private anniversary memory.', $output );
+ }
+
+ /**
+ * @ticket 65116
+ *
+ * @covers ::wp_dashboard_on_this_day
+ */
+ public function test_widget_hides_untitled_post_excerpt_for_password_protected_posts() {
+ wp_set_current_user( self::$user_id );
+
+ $this->create_matching_post(
+ self::$user_id,
+ '',
+ 1,
+ '12:00:00',
+ array(
+ 'post_excerpt' => 'Private anniversary memory.',
+ 'post_password' => 'secret',
+ )
+ );
+
+ ob_start();
+ wp_dashboard_on_this_day();
+ $output = ob_get_clean();
+
+ $this->assertStringNotContainsString( 'Private anniversary memory.', $output );
+ $this->assertStringNotContainsString( 'class="trimmed-post-excerpt"', $output );
+ }
+
+ /**
* @covers ::wp_dashboard_on_this_day
* @covers ::wp_dashboard_on_this_day_get_posts
*/
@@ -353,8 +453,20 @@ public function test_widget_limits_posts_to_ten() {
$output = ob_get_clean();
$this->assertStringContainsString( '10 posts have been published on ' . wp_date( 'F jS' ) . ':', $output );
- $this->assertStringContainsString( 'Anniversary post 1<', $output );
- $this->assertStringContainsString( 'Anniversary post 10<', $output );
+ $this->assertMatchesRegularExpression( '/>\s*Anniversary post 1\s*<\/a>/', $output );
+ $this->assertMatchesRegularExpression( '/>\s*Anniversary post 10\s*<\/a>/', $output );
$this->assertStringNotContainsString( 'Anniversary post 11', $output );
}
+
+ /**
+ * Filters the On This Day query to include private posts.
+ *
+ * @param array $args WP_Query arguments.
+ * @return array Filtered query arguments.
+ */
+ public function filter_on_this_day_query_private_posts( $args ) {
+ $args['post_status'] = array( 'private' );
+
+ return $args;
+ }
}