Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions src/wp-admin/css/dashboard.css
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,11 @@ body #dashboard-widgets .postbox form .submit {
color: #646970;
}

/* Align the pagination with the widget content instead of the list table default. */
#wp_dashboard_on_this_day .tablenav-pages {
float: none;
}

/* Browse happy box */

#dashboard-widgets #dashboard_browser_nag.postbox .inside {
Expand Down
257 changes: 178 additions & 79 deletions src/wp-admin/includes/dashboard-on-this-day.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,28 @@ function wp_dashboard_on_this_day_postbox_classes( $classes ) {
* Renders the On This Day dashboard widget.
*
* Outputs the matching posts grouped by publication year, newest year first.
* Posts are paginated, with the requested page read from the
* `on-this-day-page` query argument.
*
* @since 7.1.0
*/
function wp_dashboard_on_this_day() {
$posts = wp_dashboard_on_this_day_get_posts();
$current_page = isset( $_GET['on-this-day-page'] ) ? max( 1, absint( $_GET['on-this-day-page'] ) ) : 1;
$query = _wp_dashboard_on_this_day_get_posts_query(
array(
'paged' => $current_page,
)
);

// A page number past the last page falls back to the first page.
if ( empty( $query->posts ) && $current_page > 1 ) {
$current_page = 1;
$query = _wp_dashboard_on_this_day_get_posts_query();
}

$posts = $query->posts;
$post_count = (int) $query->found_posts;
$total_pages = (int) $query->max_num_pages;

if ( empty( $posts ) ) {
// Placeholder shown when a user reveals the hidden widget via Screen
Expand All @@ -66,19 +83,6 @@ function wp_dashboard_on_this_day() {
return;
}

$posts_by_year = array();
$post_count = count( $posts );

foreach ( $posts as $post ) {
$year = get_the_date( 'Y', $post );

if ( ! isset( $posts_by_year[ $year ] ) ) {
$posts_by_year[ $year ] = array();
}

$posts_by_year[ $year ][] = $post;
}

/* translators: Date format for the On This Day widget date, without year. See https://www.php.net/manual/datetime.format.php */
$date = '<strong>' . esc_html( wp_date( _x( 'F jS', 'on this day date format' ) ) ) . '</strong>';
?>
Expand Down Expand Up @@ -107,76 +111,170 @@ function wp_dashboard_on_this_day() {
}
?>
</p>
<ul>
<?php foreach ( $posts_by_year as $year => $year_posts ) : ?>
<li>
<h3><?php echo esc_html( $year ); ?></h3>
<ul>
<?php foreach ( $year_posts as $year_post ) : ?>
<?php
$title = get_the_title( $year_post );
$no_title_excerpt = '';
<ul class="wp-on-this-day-posts">
<?php _wp_dashboard_on_this_day_render_posts( $posts ); ?>
</ul>
<?php
if ( $total_pages > 1 ) :
// The fragment scrolls back to the widget once the dashboard reloads.
$page_url = admin_url( 'index.php' ) . '#wp_dashboard_on_this_day';

if ( '' === trim( $title ) ) {
$title = __( '(no title)' );
/*
* The page field and total page count are combined with the same
* string list tables use, so the order can be translated.
*/
$paging_text = sprintf(
/* translators: 1: Current page number field, 2: Total number of pages. */
_x( '%1$s of %2$s', 'paging' ),
sprintf(
'<input class="current-page" id="wp-on-this-day-page-selector" type="text" name="on-this-day-page" value="%1$s" size="%2$d" aria-describedby="wp-on-this-day-paging" />',
esc_attr( $current_page ),
strlen( (string) $total_pages )
),
'<span class="total-pages">' . esc_html( number_format_i18n( $total_pages ) ) . '</span>'
);
?>
<form class="tablenav" method="get" action="<?php echo esc_url( $page_url ); ?>">
<div class="tablenav-pages">
<span class="pagination-links">
<?php if ( 1 === $current_page ) : ?>
<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&lsaquo;</span>
<?php else : ?>
<a class="prev-page button" href="<?php echo esc_url( add_query_arg( 'on-this-day-page', $current_page - 1, $page_url ) ); ?>">
<span class="screen-reader-text"><?php esc_html_e( 'Previous page' ); ?></span>
<span aria-hidden="true">&lsaquo;</span>
</a>
<?php endif; ?>
<span class="paging-input">
<label class="screen-reader-text" for="wp-on-this-day-page-selector"><?php esc_html_e( 'Current Page' ); ?></label>
<span class="tablenav-paging-text" id="wp-on-this-day-paging">
<?php echo $paging_text; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Values are escaped above. ?>
</span>
</span>
<?php if ( $current_page < $total_pages ) : ?>
<a class="next-page button" href="<?php echo esc_url( add_query_arg( 'on-this-day-page', $current_page + 1, $page_url ) ); ?>">
<span class="screen-reader-text"><?php esc_html_e( 'Next page' ); ?></span>
<span aria-hidden="true">&rsaquo;</span>
</a>
<?php else : ?>
<span class="tablenav-pages-navspan button disabled" aria-hidden="true">&rsaquo;</span>
<?php endif; ?>
</span>
</div>
</form>
<?php endif; ?>
</div>
<?php
}

if ( current_user_can( 'read_post', $year_post->ID ) && ! post_password_required( $year_post ) ) {
$excerpt = get_the_excerpt( $year_post );
/**
* Renders On This Day posts grouped by publication year.
*
* @since 7.1.0
* @access private
*
* @param WP_Post[] $posts Posts to render.
*/
function _wp_dashboard_on_this_day_render_posts( $posts ) {
$posts_by_year = array();

if ( is_string( $excerpt ) && '' !== $excerpt ) {
$no_title_excerpt = wp_trim_words( $excerpt, 15 );
}
}
foreach ( $posts as $post ) {
$year = get_the_date( 'Y', $post );

if ( ! isset( $posts_by_year[ $year ] ) ) {
$posts_by_year[ $year ] = array();
}

$posts_by_year[ $year ][] = $post;
}

foreach ( $posts_by_year as $year => $year_posts ) {
?>
<li class="wp-on-this-day-year">
<h3><?php echo esc_html( $year ); ?></h3>
<ul class="wp-on-this-day-year-posts">
<?php foreach ( $year_posts as $year_post ) : ?>
<?php
$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;
$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 ); ?>
<?php
if ( '' !== $no_title_excerpt ) {
echo esc_html( $no_title_excerpt );
}
?>
<li>
<a href="<?php echo esc_url( get_permalink( $year_post ) ); ?>">
<?php echo esc_html( $title ); ?>
<?php
if ( '' !== $no_title_excerpt ) {
echo esc_html( $no_title_excerpt );
}
?>
</a>
<?php if ( $show_author ) : ?>
<?php
echo '<span class="wp-on-this-day-post-author">' . esc_html(
sprintf(
/* translators: %s: Post author's display name. */
__( 'by %s' ),
$author_name
)
) . '</span>';
?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php
</a>
<?php if ( $show_author ) : ?>
<?php
echo '<span class="wp-on-this-day-post-author">' . esc_html(
sprintf(
/* translators: %s: Post author's display name. */
__( 'by %s' ),
$author_name
)
) . '</span>';
?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</li>
<?php
}
}

/**
* Retrieves published posts from all authors that were published on this
* calendar day in previous years.
*
* The date constraint matches today's month and day, combined with a
* `before` clause anchored to January 1 of the current year. Up to ten posts
* are returned; use the `wp_dashboard_on_this_day_query_args` filter to change
* the limit. Results are cached by WP_Query's native query caching.
* `before` clause anchored to January 1 of the current year. Ten posts are
* displayed per page; use the `wp_dashboard_on_this_day_query_args` filter to
* change the limit. Results are cached by WP_Query's native query caching.
*
* @since 7.1.0
*
* @return WP_Post[] Array of posts ordered by newest first.
*/
function wp_dashboard_on_this_day_get_posts() {
$query = _wp_dashboard_on_this_day_get_posts_query(
array(
'no_found_rows' => true,
)
);

return $query->posts;
}

/**
* Retrieves the WP_Query for On This Day posts.
*
* @since 7.1.0
* @access private
*
* @param array $query_args Additional query arguments.
* @return WP_Query Query for matching posts.
*/
function _wp_dashboard_on_this_day_get_posts_query( $query_args = array() ) {
$today = current_datetime();
$year = (int) $today->format( 'Y' );
$date_query = array(
Expand All @@ -187,17 +285,20 @@ function wp_dashboard_on_this_day_get_posts() {
_wp_dashboard_on_this_day_date_query_clause( $today ),
);

$args = array(
'post_type' => 'post',
'post_status' => array( 'publish' ),
'posts_per_page' => 10,
'ignore_sticky_posts' => true,
'orderby' => 'date',
'order' => 'DESC',
'no_found_rows' => true,
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'date_query' => $date_query,
$args = array_merge(
array(
'post_type' => 'post',
'post_status' => array( 'publish' ),
'posts_per_page' => 10,
'ignore_sticky_posts' => true,
'orderby' => 'date',
'order' => 'DESC',
'no_found_rows' => false,
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'date_query' => $date_query,
),
$query_args
);

/**
Expand All @@ -209,9 +310,7 @@ function wp_dashboard_on_this_day_get_posts() {
*/
$args = apply_filters( 'wp_dashboard_on_this_day_query_args', $args );

$query = new WP_Query( $args );

return $query->posts;
return new WP_Query( $args );
}

/**
Expand Down
Loading
Loading