Skip to content

Commit 7ce530a

Browse files
committed
Show excerpts for untitled On This Day posts
1 parent 81b2b5b commit 7ce530a

2 files changed

Lines changed: 109 additions & 10 deletions

File tree

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,35 @@ function wp_dashboard_on_this_day_postbox_classes( $classes ) {
4949
return $classes;
5050
}
5151

52+
/**
53+
* Gets a trimmed excerpt to display in place of a missing post title.
54+
*
55+
* Only returns text for posts that have no title, do not require a password,
56+
* and that the current user is allowed to read.
57+
*
58+
* @since 7.1.0
59+
* @access private
60+
*
61+
* @param WP_Post $post The current WP_Post object.
62+
* @return string The trimmed excerpt, or an empty string.
63+
*/
64+
function _wp_dashboard_on_this_day_get_no_title_excerpt( $post ) {
65+
if ( '' !== get_the_title( $post )
66+
|| post_password_required( $post )
67+
|| ! current_user_can( 'read_post', $post->ID )
68+
) {
69+
return '';
70+
}
71+
72+
$excerpt = get_the_excerpt( $post );
73+
74+
if ( '' === $excerpt || ! is_string( $excerpt ) ) {
75+
return '';
76+
}
77+
78+
return wp_trim_words( $excerpt, 15 );
79+
}
80+
5281
/**
5382
* Renders the On This Day dashboard widget.
5483
*
@@ -114,18 +143,22 @@ function wp_dashboard_on_this_day() {
114143
<ul>
115144
<?php foreach ( $year_posts as $year_post ) : ?>
116145
<?php
117-
$title = get_the_title( $year_post );
146+
$title = get_the_title( $year_post );
147+
$no_title_excerpt = '';
118148

119149
if ( '' === trim( $title ) ) {
120-
$title = __( '(no title)' );
150+
$title = __( '(no title)' );
151+
$no_title_excerpt = _wp_dashboard_on_this_day_get_no_title_excerpt( $year_post );
121152
}
122153

123154
$author_id = (int) $year_post->post_author;
124155
$author_name = $author_id > 0 ? (string) get_the_author_meta( 'display_name', $author_id ) : '';
125156
$show_author = '' !== trim( $author_name ) && get_current_user_id() !== $author_id;
126157
?>
127158
<li>
128-
<a href="<?php echo esc_url( get_permalink( $year_post ) ); ?>"><?php echo esc_html( $title ); ?></a>
159+
<a href="<?php echo esc_url( get_permalink( $year_post ) ); ?>"><?php echo esc_html( $title ); ?><?php if ( '' !== $no_title_excerpt ) : ?>
160+
<span class="trimmed-post-excerpt"><?php echo esc_html( $no_title_excerpt ); ?></span>
161+
<?php endif; ?></a>
129162
<?php if ( $show_author ) : ?>
130163
<?php
131164
echo '<span class="wp-on-this-day-post-author">' . esc_html(

‎tests/phpunit/tests/admin/wpOnThisDay.php‎

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,28 @@ private function set_up_dashboard_screen() {
3535
* @param string $title Post title.
3636
* @param int $years_ago Number of years before today.
3737
* @param string $time Post time.
38+
* @param array $post_args Additional post arguments.
3839
* @return int Post ID.
3940
*/
4041
private function create_matching_post(
4142
$author_id,
4243
$title = 'A memory from last year',
4344
$years_ago = 1,
44-
$time = '12:00:00'
45+
$time = '12:00:00',
46+
$post_args = array()
4547
) {
4648
$post_date = current_datetime()->modify( '-' . $years_ago . ' years' )->format( 'Y-m-d' ) . ' ' . $time;
4749

4850
return self::factory()->post->create(
49-
array(
50-
'post_author' => $author_id,
51-
'post_date' => $post_date,
52-
'post_date_gmt' => get_gmt_from_date( $post_date ),
53-
'post_status' => 'publish',
54-
'post_title' => $title,
51+
array_merge(
52+
array(
53+
'post_author' => $author_id,
54+
'post_date' => $post_date,
55+
'post_date_gmt' => get_gmt_from_date( $post_date ),
56+
'post_status' => 'publish',
57+
'post_title' => $title,
58+
),
59+
$post_args
5560
)
5661
);
5762
}
@@ -309,6 +314,67 @@ public function test_widget_groups_posts_by_year() {
309314
$this->assertStringContainsString( 'Late-night shipping log', $output );
310315
}
311316

317+
/**
318+
* @covers ::wp_dashboard_on_this_day
319+
* @covers ::_wp_dashboard_on_this_day_get_no_title_excerpt
320+
*/
321+
public function test_widget_includes_trimmed_excerpt_for_untitled_posts() {
322+
$user_id = self::factory()->user->create( array( 'role' => 'author' ) );
323+
wp_set_current_user( $user_id );
324+
325+
$words = array();
326+
for ( $n = 1; $n <= 20; $n++ ) {
327+
$words[] = 'word' . $n;
328+
}
329+
330+
$this->create_matching_post(
331+
$user_id,
332+
'',
333+
1,
334+
'12:00:00',
335+
array(
336+
'post_excerpt' => implode( ' ', $words ),
337+
)
338+
);
339+
340+
ob_start();
341+
wp_dashboard_on_this_day();
342+
$output = ob_get_clean();
343+
344+
$this->assertStringContainsString( '(no title)', $output );
345+
$this->assertStringContainsString( 'class="trimmed-post-excerpt"', $output );
346+
$this->assertStringContainsString( 'word15', $output, 'The 15th word should be present.' );
347+
$this->assertStringNotContainsString( 'word16', $output, 'The 16th word should be trimmed.' );
348+
$this->assertStringContainsString( '&hellip;', $output, 'The excerpt should end with an ellipsis.' );
349+
}
350+
351+
/**
352+
* @covers ::wp_dashboard_on_this_day
353+
* @covers ::_wp_dashboard_on_this_day_get_no_title_excerpt
354+
*/
355+
public function test_widget_hides_untitled_post_excerpt_for_password_protected_posts() {
356+
$user_id = self::factory()->user->create( array( 'role' => 'author' ) );
357+
wp_set_current_user( $user_id );
358+
359+
$this->create_matching_post(
360+
$user_id,
361+
'',
362+
1,
363+
'12:00:00',
364+
array(
365+
'post_excerpt' => 'Private anniversary memory.',
366+
'post_password' => 'secret',
367+
)
368+
);
369+
370+
ob_start();
371+
wp_dashboard_on_this_day();
372+
$output = ob_get_clean();
373+
374+
$this->assertStringNotContainsString( 'Private anniversary memory.', $output );
375+
$this->assertStringNotContainsString( 'class="trimmed-post-excerpt"', $output );
376+
}
377+
312378
/**
313379
* @covers ::wp_dashboard_on_this_day
314380
* @covers ::wp_dashboard_on_this_day_get_posts

0 commit comments

Comments
 (0)