Skip to content
Open
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
102 changes: 101 additions & 1 deletion src/wp-admin/includes/dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ function wp_dashboard_recent_posts( $args ) {

$today = current_time( 'Y-m-d' );
$tomorrow = current_datetime()->modify( '+1 day' )->format( 'Y-m-d' );
$year = current_time( 'Y' );
$year = (int) current_time( 'Y' );

while ( $posts->have_posts() ) {
$posts->the_post();
Expand Down Expand Up @@ -1053,6 +1053,106 @@ function wp_dashboard_recent_posts( $args ) {
}

echo '</ul>';

/*
* List posts published on this day in previous years if the final post
* in the recently published list is within the past year (minus 1 day).
*/
if ( 'published-posts' === $args['id'] && is_int( $time )
&& $time > ( current_time( 'U' ) - YEAR_IN_SECONDS + ( 1 * DAY_IN_SECONDS ) ) ) {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The time condition avoids running the On This Day query if the Recent posts list might already include a post from the same day in a previous year. If the query uses a date range, the number of days in seconds would increase.


$wp_otd_date = current_datetime();

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These variable names with otd would need editing, but I was more concerned with making them unique for now.

$wp_otd_clauses = array();
$window_before_days = 0; // Edit before and after if creating a range.
$window_after_days = 0;
$user_id = get_current_user_id();

for ( $offset = -$window_before_days; $offset <= $window_after_days; $offset++ ) {
$wp_otd_day_date = $wp_otd_date->modify( ( $offset >= 0 ? '+' : '' ) . $offset . ' days' );
$wp_otd_clauses[] = array(
'month' => (int) $wp_otd_day_date->format( 'n' ),
'day' => (int) $wp_otd_day_date->format( 'j' ),
);
}

$wp_otd_date_query = array(
'relation' => 'AND',
array(
'before' => array( 'year' => $year ),
),
array_merge(
array( 'relation' => 'OR' ),
$wp_otd_clauses
),
);

$wp_otd_args = array(
'author' => (int) $user_id,

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like PR 11630, this limits the posts to any created by (or assigned to) the current user instead of all posts on the site. The argument could be removed, either for the default query or for individual sites with the wp_dashboard_on_this_day_query_args filter.

'post_type' => 'post',
'post_status' => array( 'publish' ),
'posts_per_page' => 50,
'ignore_sticky_posts' => true,
'orderby' => 'date',
'order' => 'DESC',
'no_found_rows' => true,
'update_post_term_cache' => false,
'date_query' => $wp_otd_date_query,
);

/**
* Filters the arguments used to query posts for the On This Day dashboard widget.
*
* @since 7.1.0
*
* @param array $wp_otd_args WP_Query arguments.
* @param int $user_id The author ID the query is scoped to.
*/
$wp_otd_args = apply_filters( 'wp_dashboard_on_this_day_query_args', $wp_otd_args, $user_id );

$wp_otd_query = new WP_Query( $wp_otd_args );

if ( $wp_otd_query->have_posts() ) {

echo '<div id="wp-on-this-day" class="activity-block">';

echo '<h3>' . __( 'Published On This Day' ) . '</h3>';

echo '<ul>';

while ( $wp_otd_query->have_posts() ) {
$wp_otd_query->the_post();

$time = get_the_time( 'U' );

if ( ! is_int( $time ) ) {
/* translators: Date and time format for recent posts on the dashboard, from a different calendar year, see https://www.php.net/manual/datetime.format.php */
$date = get_the_date( __( 'M jS Y' ) );
} else {
/* translators: Date and time format for recent posts on the dashboard, from a different calendar year, see https://www.php.net/manual/datetime.format.php */
$date = date_i18n( __( 'M jS Y' ), $time );
}

// Use the post edit link for those who can edit, the permalink otherwise.
$recent_post_link = current_user_can( 'edit_post', get_the_ID() ) ? get_edit_post_link() : get_permalink();

$draft_or_post_title = _draft_or_post_title();
printf(
'<li><span>%1$s</span> <a href="%2$s" aria-label="%3$s">%4$s</a></li>',
$date,
$recent_post_link,
/* translators: %s: Post title. */
esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $draft_or_post_title ) ),
$draft_or_post_title
);

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied this from lines 1040 to 1052, and changed the date and time to only the date.

As a result, the link is for editing the post if the user has edit access. If the link is for viewing regardless of access, it would have no need for the ARIA label.

Suggested change
);
$draft_or_post_title = _draft_or_post_title();
printf(
'<li><span>%1$s</span> <a href="%2$s">%3$s</a></li>',
$date,
get_permalink(),
$draft_or_post_title
);

But then the Recent posts could indicate visually that links in the recent and future lists are for editing, to make the difference clearer.

			$draft_or_post_title = _draft_or_post_title();
			if ( current_user_can( 'edit_post', get_the_ID() ) ) {
				printf(
					'<li><span>%1$s</span> <a href="%2$s" aria-label="%3$s">%4$s</a></li>',
					/* translators: 1: Relative date, 2: Time. */
					sprintf( _x( '%1$s, %2$s', 'dashboard' ), $date, get_the_time() ),
					get_edit_post_link(),
					/* translators: %s: Post title. */
					esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $draft_or_post_title ) ),
					'<span class="dashicons dashicons-edit" aria-hidden="true"></span>' . $draft_or_post_title
				);
			} else {
				printf(
					'<li><span>%1$s</span> <a href="%2$s">%3$s</a></li>',
					/* translators: 1: Relative date, 2: Time. */
					sprintf( _x( '%1$s, %2$s', 'dashboard' ), $date, get_the_time() ),
					get_permalink(),
					$draft_or_post_title
				);
			}

}

echo '</ul>';
echo '</div>';
}

wp_reset_postdata();
}

echo '</div>';

} else {
Expand Down
Loading