Skip to content
Open
40 changes: 37 additions & 3 deletions src/wp-admin/includes/dashboard-on-this-day.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,33 @@ function wp_dashboard_on_this_day_postbox_classes( $classes ) {
return $classes;
}

/**
* Gets a trimmed excerpt to display in place of a missing post title.
*
* Only returns text for posts that have no title and do not require a password.
*
Comment on lines +53 to +56
* @since 7.1.0
* @access private
*
* @param WP_Post $post The current WP_Post object.
* @return string The trimmed excerpt, or an empty string.
*/
function _wp_dashboard_on_this_day_get_no_title_excerpt( $post ) {
Comment thread
alshakero marked this conversation as resolved.
if ( '' !== get_the_title( $post )
|| post_password_required( $post )
) {
Comment thread
alshakero marked this conversation as resolved.
return '';
}

$excerpt = get_the_excerpt( $post );

if ( '' === $excerpt || ! is_string( $excerpt ) ) {
return '';
}

return wp_trim_words( $excerpt, 15 );
}

/**
* Renders the On This Day dashboard widget.
*
Expand Down Expand Up @@ -114,18 +141,25 @@ function wp_dashboard_on_this_day() {
<ul>
<?php foreach ( $year_posts as $year_post ) : ?>
<?php
$title = get_the_title( $year_post );
$title = get_the_title( $year_post );
$no_title_excerpt = '';

if ( '' === trim( $title ) ) {
$title = __( '(no title)' );
$title = __( '(no title)' );
$no_title_excerpt = _wp_dashboard_on_this_day_get_no_title_excerpt( $year_post );
}

$author_id = (int) $year_post->post_author;
$author_name = $author_id > 0 ? (string) get_the_author_meta( 'display_name', $author_id ) : '';
$show_author = '' !== trim( $author_name ) && get_current_user_id() !== $author_id;
?>
<li>
<a href="<?php echo esc_url( get_permalink( $year_post ) ); ?>"><?php echo esc_html( $title ); ?></a>
<a href="<?php echo esc_url( get_permalink( $year_post ) ); ?>">
<?php echo esc_html( $title ); ?>
<?php if ( '' !== $no_title_excerpt ) : ?>
<span class="trimmed-post-excerpt"><?php echo esc_html( $no_title_excerpt ); ?></span>
<?php endif; ?>
</a>
<?php if ( $show_author ) : ?>
<?php
echo '<span class="wp-on-this-day-post-author">' . esc_html(
Expand Down
132 changes: 123 additions & 9 deletions tests/phpunit/tests/admin/wpOnThisDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,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(
$author_id,
$title = 'A memory from last year',
$years_ago = 1,
$time = '12:00:00'
$time = '12:00:00',
$post_args = array()
) {
$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
)
);
}
Expand Down Expand Up @@ -309,6 +314,103 @@ public function test_widget_groups_posts_by_year() {
$this->assertStringContainsString( 'Late-night shipping log', $output );
}

/**
* @covers ::wp_dashboard_on_this_day
* @covers ::_wp_dashboard_on_this_day_get_no_title_excerpt
*/
public function test_widget_includes_trimmed_excerpt_for_untitled_posts() {
Comment thread
alshakero marked this conversation as resolved.
$user_id = self::factory()->user->create( array( 'role' => 'author' ) );
wp_set_current_user( $user_id );

$words = array();
for ( $n = 1; $n <= 20; $n++ ) {
$words[] = 'word' . $n;
}

$this->create_matching_post(
$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( '&hellip;', $output, 'The excerpt should end with an ellipsis.' );
}

/**
* @covers ::wp_dashboard_on_this_day
* @covers ::_wp_dashboard_on_this_day_get_no_title_excerpt
*/
public function test_widget_includes_trimmed_excerpt_for_untitled_private_posts_authored_by_current_user() {
$this->set_up_dashboard_screen();

$user_id = self::factory()->user->create( array( 'role' => 'author' ) );
wp_set_current_user( $user_id );

$this->create_matching_post(
$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 );
}

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

$this->create_matching_post(
$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 );
}
Comment thread
alshakero marked this conversation as resolved.

/**
* @covers ::wp_dashboard_on_this_day
* @covers ::wp_dashboard_on_this_day_get_posts
Expand All @@ -326,8 +428,20 @@ public function test_widget_limits_posts_to_ten() {
$output = ob_get_clean();

$this->assertStringContainsString( '10 posts have been published on <strong>' . wp_date( 'F jS' ) . '</strong>:', $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;
}
}
Loading