-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Proposal: Add On This Day Widget #11630
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+607
−0
Closed
Changes from all commits
Commits
Show all changes
65 commits
Select commit
Hold shift + click to select a range
c5067f3
Add On This Day Widget
alshakero e1b0ba0
Lint
alshakero 5b7cbcf
Refactor CSS
alshakero 45571e4
Cleaner CSS
alshakero 6bd17f6
Remove unneeded file
alshakero c1fcec2
Remove extra space
alshakero bdf30db
Merge branch 'trunk' into add/on-this-day-widget
alshakero b53e3b5
Add caching for fetching and rendering
alshakero 63c8422
Merge branch 'add/on-this-day-widget' of https://github.com/alshakero…
alshakero 4865fc0
Redesign
alshakero a32a13f
Fix on mobile
alshakero a79feb4
Render date pill only on the header of the postbox (not the title)
alshakero 3ea2323
Design fine tuning
alshakero eceeec1
Merge branch 'trunk' into add/on-this-day-widget
alshakero aa09b83
Switch to WP_Query
alshakero 05c5a23
Remove #[AllowDynamicProperties] attribute from WP_On_This_Day class
alshakero 488310b
Add an adjustable window size
alshakero 9747fb5
Merge branch 'trunk' into add/on-this-day-widget
alshakero dfc24f2
Merge branch 'trunk' into add/on-this-day-widget
alshakero bcf3daa
Address feedback
alshakero f62cfb3
Merge branch 'add/on-this-day-widget' of https://github.com/alshakero…
alshakero e446635
Don't concat two strings to make a sentence
alshakero 4f6b9bc
Switch to `wp_scrub_utf8`
alshakero 7dea82e
Bring back success message config
alshakero 5b46861
Fix toggle indicator collapse on On This Day widget
escapemanuele 6e960aa
Merge branch 'trunk' into add/on-this-day-widget
alshakero afc4ac2
Move away from CSS prop to `attr` for simpler escaping
alshakero efd8b31
Use `sprintf` instead
alshakero 3d7ea6f
Refine On This Day widget escaping and rendered markup
escapemanuele ded7416
Run PHPCBF
alshakero 2aa110e
Merge branch 'trunk' into add/on-this-day-widget
alshakero 993c4fb
Switch to `WP_HTML_Tag_Processor` and use CSS for adding ellipsis ins…
alshakero d20b27d
Merge branch 'add/on-this-day-widget' of https://github.com/alshakero…
alshakero 5a3fb28
Merge branch 'trunk' into add/on-this-day-widget
alshakero 2c80205
Don't add whitespace for non-block tags and add tests
alshakero 903f2c3
Merge branch 'add/on-this-day-widget' of https://github.com/alshakero…
alshakero d458d25
Merge branch 'trunk' into add/on-this-day-widget
alshakero 39ed253
Remove excess whitespace
alshakero 01e5164
Merge branch 'add/on-this-day-widget' of https://github.com/alshakero…
alshakero 3be969a
Address feedback
alshakero 165261c
Merge branch 'trunk' into add/on-this-day-widget
alshakero 67ff936
Redesign:
alshakero e165fc7
Merge branch 'trunk' into add/on-this-day-widget
alshakero f0b6fb9
Cleaner empty state
alshakero d1479ff
Merge branch 'trunk' into add/on-this-day-widget
alshakero 0658426
Merge branch 'trunk' into add/on-this-day-widget
alshakero 242b0d5
Address feedback
alshakero 2f00c14
Merge branch 'trunk' into add/on-this-day-widget
alshakero b4c40ed
Fix file path in tests
alshakero 6c28025
Address all feedback
alshakero 3b4c244
Merge branch 'trunk' into add/on-this-day-widget
alshakero 7e172ea
Merge branch 'trunk' into add/on-this-day-widget
alshakero c5e11d5
Merge branch 'trunk' into add/on-this-day-widget
alshakero 85cec58
Merge branch 'trunk' into add/on-this-day-widget
alshakero c9934f4
Merge branch 'trunk' into add/on-this-day-widget
alshakero 0c3571b
Address feedback
alshakero 5fe2fca
Address @peterwilsoncc's feedback
alshakero 49aa78e
Address feedback
alshakero 3075bf5
Merge branch 'trunk' into add/on-this-day-widget
alshakero 657f55f
Merge branch 'trunk' into add/on-this-day-widget
alshakero ed7b83e
Address feedback
alshakero 9213f82
Merge branch 'add/on-this-day-widget' of https://github.com/alshakero…
alshakero 4ee1483
Address @peterwilsoncc's feedback
alshakero 2403471
Use filter to add classname
alshakero 1f77949
Merge branch 'trunk' into add/on-this-day-widget
alshakero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,238 @@ | ||
| <?php | ||
| /** | ||
| * WordPress Dashboard "On This Day" widget. | ||
| * | ||
| * @package WordPress | ||
| * @subpackage Administration | ||
| * @since 7.1.0 | ||
| */ | ||
|
|
||
| /** | ||
| * Registers the On This Day dashboard widget. | ||
| * | ||
| * Designed to be the single entry point called from the dashboard setup | ||
| * routine. The widget is always registered so that it remains available in | ||
| * Screen Options and keeps its user-customized position. When there are no | ||
| * matching posts, a marker class is added to the postbox so the widget can be | ||
| * hidden with CSS. | ||
| * | ||
| * @since 7.1.0 | ||
| */ | ||
| function wp_dashboard_on_this_day_setup() { | ||
| add_filter( 'postbox_classes_dashboard_wp_dashboard_on_this_day', 'wp_dashboard_on_this_day_postbox_classes' ); | ||
|
|
||
| wp_add_dashboard_widget( | ||
| 'wp_dashboard_on_this_day', | ||
| __( 'On This Day' ), | ||
| 'wp_dashboard_on_this_day' | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Hides the On This Day postbox when there are no posts to show. | ||
| * | ||
| * Adds the core `hidden` class so the widget stays registered — preserving its | ||
| * Screen Options entry and user-customized position — while being hidden when | ||
| * empty. A user can still reveal it via Screen Options, in which case the | ||
| * placeholder message is shown. | ||
| * | ||
| * @since 7.1.0 | ||
| * | ||
| * @param string[] $classes An array of postbox classes. | ||
| * @return string[] Filtered postbox classes. | ||
| */ | ||
| function wp_dashboard_on_this_day_postbox_classes( $classes ) { | ||
| if ( empty( wp_dashboard_on_this_day_get_posts() ) ) { | ||
| $classes[] = 'hidden'; | ||
| } | ||
|
|
||
| return $classes; | ||
| } | ||
|
|
||
| /** | ||
| * Renders the On This Day dashboard widget. | ||
| * | ||
| * Outputs the matching posts grouped by publication year, newest year first. | ||
| * | ||
| * @since 7.1.0 | ||
| */ | ||
| function wp_dashboard_on_this_day() { | ||
| $posts = wp_dashboard_on_this_day_get_posts(); | ||
|
|
||
| if ( empty( $posts ) ) { | ||
| // Placeholder shown when a user reveals the hidden widget via Screen | ||
| // Options on a day with no matching posts. | ||
| echo '<p>' . esc_html__( 'No posts were published on this day in previous years.' ) . '</p>'; | ||
| 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>'; | ||
| ?> | ||
| <div class="wp-on-this-day-widget"> | ||
| <p> | ||
| <?php | ||
| if ( 1 === $post_count ) { | ||
| printf( | ||
| /* translators: %s: Date, without year. */ | ||
| esc_html__( 'One post has been published on %s:' ), | ||
| $date // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Date is escaped above. | ||
| ); | ||
| } else { | ||
| printf( | ||
| esc_html( | ||
| /* translators: 1: Number of posts, 2: Date, without year. */ | ||
| _n( | ||
| '%1$s post has been published on %2$s:', | ||
| '%1$s posts have been published on %2$s:', | ||
| $post_count | ||
| ) | ||
| ), | ||
| esc_html( number_format_i18n( $post_count ) ), | ||
| $date // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Date is escaped above. | ||
| ); | ||
| } | ||
| ?> | ||
| </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 ); | ||
|
|
||
| if ( '' === trim( $title ) ) { | ||
| $title = __( '(no title)' ); | ||
| } | ||
|
|
||
| $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> | ||
| <?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 | ||
| } | ||
|
|
||
| /** | ||
| * 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. | ||
| * | ||
| * @since 7.1.0 | ||
| * | ||
| * @return WP_Post[] Array of posts ordered by newest first. | ||
| */ | ||
| function wp_dashboard_on_this_day_get_posts() { | ||
| $today = current_datetime(); | ||
| $year = (int) $today->format( 'Y' ); | ||
| $date_query = array( | ||
| 'relation' => 'AND', | ||
| array( | ||
| 'before' => array( 'year' => $year ), | ||
| ), | ||
| _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, | ||
|
t-hamano marked this conversation as resolved.
|
||
| 'update_post_meta_cache' => false, | ||
| 'date_query' => $date_query, | ||
| ); | ||
|
|
||
| /** | ||
| * Filters the arguments used to query posts for the On This Day dashboard widget. | ||
| * | ||
| * @since 7.1.0 | ||
| * | ||
| * @param array $args WP_Query arguments. | ||
| */ | ||
| $args = apply_filters( 'wp_dashboard_on_this_day_query_args', $args ); | ||
|
|
||
| $query = new WP_Query( $args ); | ||
|
|
||
| return $query->posts; | ||
| } | ||
|
|
||
| /** | ||
| * Builds the date query clause for today's anniversary date. | ||
| * | ||
| * On February 28 in a non-leap year, February 29 posts are included so | ||
| * leap-day anniversaries still appear. | ||
| * | ||
| * @since 7.1.0 | ||
| * @access private | ||
| * | ||
| * @param DateTimeInterface $date Date to build the clause for. | ||
| * @return array Date query clause. | ||
| */ | ||
| function _wp_dashboard_on_this_day_date_query_clause( $date ) { | ||
| $month = (int) $date->format( 'm' ); | ||
| $day = (int) $date->format( 'd' ); | ||
| $clause = array( | ||
| 'month' => $month, | ||
| 'day' => $day, | ||
| ); | ||
|
|
||
| // Display leap day posts on Feb 28 in non leap years. | ||
| if ( | ||
| 28 === $day | ||
| && 2 === $month | ||
| && false === (bool) $date->format( 'L' ) | ||
| ) { | ||
| $clause = array( | ||
| 'relation' => 'OR', | ||
| $clause, | ||
| array( | ||
| 'month' => 2, | ||
| 'day' => 29, | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| return $clause; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.