From 327bdbcde7ded952355b086f74e6dc16be908acd Mon Sep 17 00:00:00 2001 From: Omar Alshaker Date: Sun, 2 Aug 2026 14:47:37 +0200 Subject: [PATCH 1/2] Dashboard: Add On This Day load more button --- src/js/_enqueues/wp/dashboard.js | 59 ++++++ src/wp-admin/admin-ajax.php | 1 + src/wp-admin/includes/ajax-actions.php | 46 +++++ .../includes/dashboard-on-this-day.php | 182 ++++++++++++------ tests/phpunit/includes/testcase-ajax.php | 1 + .../tests/admin/wpDashboardOnThisDay.php | 13 +- .../tests/ajax/wpAjaxDashboardOnThisDay.php | 68 +++++++ tests/qunit/wp-admin/js/dashboard.js | 74 +++++++ 8 files changed, 377 insertions(+), 67 deletions(-) create mode 100644 tests/phpunit/tests/ajax/wpAjaxDashboardOnThisDay.php diff --git a/src/js/_enqueues/wp/dashboard.js b/src/js/_enqueues/wp/dashboard.js index 0c92f1e1b3e47..3b3408aecf2e5 100644 --- a/src/js/_enqueues/wp/dashboard.js +++ b/src/js/_enqueues/wp/dashboard.js @@ -190,6 +190,65 @@ jQuery( function($) { }; window.quickPressLoad(); + /** + * Loads the remaining posts in the On This Day dashboard widget. + * + * @since 7.1.0 + */ + $( document ).on( 'click', '#wp_dashboard_on_this_day .wp-on-this-day-load-more button', function() { + var $button = $( this ), + $widget = $button.closest( '#wp_dashboard_on_this_day' ), + $posts = $widget.find( '#wp-on-this-day-posts' ); + + if ( $button.prop( 'disabled' ) ) { + return; + } + + $button.prop( 'disabled', true ); + + $.post( ajaxurl, { + action: 'dashboard-on-this-day-load-more', + _ajax_nonce: $button.attr( 'data-wp-on-this-day-nonce' ), + offset: $button.attr( 'data-wp-on-this-day-offset' ) + } ).done( function( response ) { + var $additional_years, + post_count; + + if ( ! response.success ) { + $button.prop( 'disabled', false ); + return; + } + + $additional_years = $( response.data.html ).filter( '.wp-on-this-day-year' ); + + $additional_years.each( function() { + var $year = $( this ), + year = $year.attr( 'data-wp-on-this-day-year' ), + $existing_year = $posts.children( '[data-wp-on-this-day-year="' + year + '"]' ); + + if ( $existing_year.length ) { + $existing_year.children( '.wp-on-this-day-year-posts' ).append( $year.children( '.wp-on-this-day-year-posts' ).children() ); + } else { + $posts.append( $year ); + } + } ); + + post_count = response.data.post_count; + $button.closest( '.wp-on-this-day-load-more' ).remove(); + + if ( post_count ) { + wp.a11y.speak( + wp.i18n.sprintf( + wp.i18n._n( '%s additional post loaded.', '%s additional posts loaded.', post_count ), + post_count + ) + ); + } + } ).fail( function() { + $button.prop( 'disabled', false ); + } ); + } ); + // Enable the dragging functionality of the widgets. $( '.meta-box-sortables' ).sortable( 'option', 'containment', '#wpwrap' ); diff --git a/src/wp-admin/admin-ajax.php b/src/wp-admin/admin-ajax.php index 3ad60f95766e3..9568fbc77f3b9 100644 --- a/src/wp-admin/admin-ajax.php +++ b/src/wp-admin/admin-ajax.php @@ -131,6 +131,7 @@ 'install-theme', 'get-post-thumbnail-html', 'get-community-events', + 'dashboard-on-this-day-load-more', 'edit-theme-plugin-file', 'wp-privacy-export-personal-data', 'wp-privacy-erase-personal-data', diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php index 3cda30f0d523f..017d0a5d42b9c 100644 --- a/src/wp-admin/includes/ajax-actions.php +++ b/src/wp-admin/includes/ajax-actions.php @@ -412,6 +412,52 @@ function wp_ajax_get_community_events() { } } +/** + * Loads the remaining posts for the On This Day dashboard widget. + * + * @since 7.1.0 + */ +function wp_ajax_dashboard_on_this_day_load_more() { + check_ajax_referer( 'wp_dashboard_on_this_day_load_more' ); + + if ( ! current_user_can( 'read' ) ) { + wp_die( -1 ); + } + + require_once ABSPATH . 'wp-admin/includes/dashboard-on-this-day.php'; + + $offset = isset( $_POST['offset'] ) ? absint( $_POST['offset'] ) : 10; + $count_query = _wp_dashboard_on_this_day_get_posts_query( + array( + 'posts_per_page' => 1, + ) + ); + $post_count = max( 0, (int) $count_query->found_posts - $offset ); + $posts = array(); + + if ( $post_count > 0 ) { + $query = _wp_dashboard_on_this_day_get_posts_query( + array( + 'posts_per_page' => $post_count, + 'offset' => $offset, + 'no_found_rows' => true, + ) + ); + $posts = $query->posts; + } + + ob_start(); + _wp_dashboard_on_this_day_render_posts( $posts ); + $html = ob_get_clean(); + + wp_send_json_success( + array( + 'html' => $html, + 'post_count' => count( $posts ), + ) + ); +} + /** * Handles dashboard widgets via AJAX. * diff --git a/src/wp-admin/includes/dashboard-on-this-day.php b/src/wp-admin/includes/dashboard-on-this-day.php index 1939f8ca4b0e3..8fcd62b311223 100644 --- a/src/wp-admin/includes/dashboard-on-this-day.php +++ b/src/wp-admin/includes/dashboard-on-this-day.php @@ -57,7 +57,9 @@ function wp_dashboard_on_this_day_postbox_classes( $classes ) { * @since 7.1.0 */ function wp_dashboard_on_this_day() { - $posts = wp_dashboard_on_this_day_get_posts(); + $query = _wp_dashboard_on_this_day_get_posts_query(); + $posts = $query->posts; + $post_count = (int) $query->found_posts; if ( empty( $posts ) ) { // Placeholder shown when a user reveals the hidden widget via Screen @@ -66,18 +68,7 @@ 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; - } + $displayed_post_count = count( $posts ); /* translators: Date format for the On This Day widget date, without year. See https://www.php.net/manual/datetime.format.php */ $date = '' . esc_html( wp_date( _x( 'F jS', 'on this day date format' ) ) ) . ''; @@ -107,46 +98,93 @@ function wp_dashboard_on_this_day() { } ?>

-