Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
18 changes: 16 additions & 2 deletions src/wp-admin/includes/dashboard-on-this-day.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,32 @@ 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)' );

if ( current_user_can( 'read_post', $year_post->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;
$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
130 changes: 121 additions & 9 deletions tests/phpunit/tests/admin/wpDashboardOnThisDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
);
}
Expand Down Expand Up @@ -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() {
Comment thread
alshakero marked this conversation as resolved.
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( '&hellip;', $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 );
}
Comment thread
alshakero marked this conversation as resolved.

/**
* @covers ::wp_dashboard_on_this_day
* @covers ::wp_dashboard_on_this_day_get_posts
*/
Expand All @@ -353,8 +453,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