diff --git a/src/js/_enqueues/admin/postbox.js b/src/js/_enqueues/admin/postbox.js index fec46d3f6d909..3b103a44a0441 100644 --- a/src/js/_enqueues/admin/postbox.js +++ b/src/js/_enqueues/admin/postbox.js @@ -355,6 +355,21 @@ postboxes.setMetaBoxReordering( enabled ); postboxes.save_meta_box_reordering_state( enabled ); } ); + + $( '.wp-on-this-day-toggle' ).on( 'click.postboxes', function() { + var enabled = $( this ).prop( 'checked' ); + + postboxes.setOnThisDayDisplay( enabled ); + postboxes.save_on_this_day_display( enabled ); + } ); + + $( '#wp-on-this-day-posts-form button' ).on( 'click.postboxes', function( event ) { + event.preventDefault(); + var enabled = '1' === $( this ).prop( 'value' ); + + postboxes.setOnThisDayDisplay( enabled ); + postboxes.save_on_this_day_display( enabled ); + } ); }, /** @@ -495,6 +510,26 @@ } }, + /** + * Enables or disables the On This Day display in the activity widget. + * + * @since 7.2.0 + * + * @param {boolean} enabled Whether On This Day should be enabled. + * @return {void} + */ + setOnThisDayDisplay: function( enabled ) { + var $onThisDayWidget = $( '#wp-on-this-day-posts' ), + $onThisDayList = $( '#wp-on-this-day-posts-list' ), + $onThisDayForm = $( '#wp-on-this-day-posts-form'), + $onThisDayScreenOption = $( '#wp-on-this-day-toggle' ); + + $onThisDayForm.hide(); // Always hidden once a choice is made. + $onThisDayWidget.toggle( enabled ).toggleClass( 'hidden', ! enabled ); + $onThisDayList.toggle( enabled ); + $onThisDayScreenOption.prop( 'checked', enabled ); + }, + /** * Saves the state of the postboxes to the server. * @@ -560,6 +595,32 @@ ); }, + /** + * Saves the On This Day setting to the server. + * + * @since 7.2.0 + * + * @param {boolean} enabled Whether On This Day activity is enabled. + * @return {void} + */ + save_on_this_day_display: function( enabled ) { + $.post( + ajaxurl, + { + action: 'on-this-day-display', + enabled: enabled ? 1 : 0, + screenoptionnonce: $( '#screenoptionnonce' ).val() + }, + function() { + wp.a11y.speak( + enabled ? + __( 'On This Day display is enabled.' ) : + __( 'On This Day display is disabled.' ) + ); + } + ); + }, + /** * Saves the order of the postboxes to the server. * diff --git a/src/wp-admin/admin-ajax.php b/src/wp-admin/admin-ajax.php index e62c285fa356c..0472c3bea45b8 100644 --- a/src/wp-admin/admin-ajax.php +++ b/src/wp-admin/admin-ajax.php @@ -79,6 +79,7 @@ 'closed-postboxes', 'hidden-columns', 'meta-box-reordering', + 'on-this-day-display', 'update-welcome-panel', 'menu-get-metabox', 'wp-link-ajax', diff --git a/src/wp-admin/css/dashboard.css b/src/wp-admin/css/dashboard.css index a79960cb39419..547251e8959b1 100644 --- a/src/wp-admin/css/dashboard.css +++ b/src/wp-admin/css/dashboard.css @@ -931,27 +931,28 @@ body #dashboard-widgets .postbox form .submit { #future-posts ul, #published-posts ul, -#on-this-day-posts ul { +#wp-on-this-day-posts ul { margin: 8px -12px 0 -12px; } #future-posts li, #published-posts li, -#on-this-day-posts li { +#wp-on-this-day-posts li { display: grid; grid-template-columns: clamp(160px, calc(2vw + 140px), 200px) auto; column-gap: 10px; color: #646970; - padding: 4px 12px; + padding: 7px 12px; + margin-bottom: 0; } #future-posts li:nth-child(odd), #published-posts li:nth-child(odd), -#on-this-day-posts li:nth-child(odd) { +#wp-on-this-day-posts-list li:nth-child(odd) { background-color: #f6f7f7; } -#on-this-day-posts .wp-on-this-day-post-author { +#wp-on-this-day-posts-list .wp-on-this-day-post-author { color: #646970; } @@ -966,6 +967,13 @@ body #dashboard-widgets .postbox form .submit { margin-bottom: 0; } +/* The last visible block, ignoring any hidden blocks that follow it. */ +.activity-block:nth-last-child(1 of :not(.hidden)), +.no-js .activity-block:nth-last-child(1 of :not(.hidden, .hide-if-no-js)) { + border-bottom: none; + margin-bottom: 0; +} + .activity-block .subsubsub li { color: #646970; } diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php index 03fefef0689ff..4a335d374ecd6 100644 --- a/src/wp-admin/includes/ajax-actions.php +++ b/src/wp-admin/includes/ajax-actions.php @@ -1818,6 +1818,22 @@ function wp_ajax_meta_box_reordering() { wp_die( 1 ); } +/** + * Handles the On This Day display setting via Ajax. + * + * @since 7.2.0 + * + * @return never + */ +function wp_ajax_on_this_day_display(): void { + check_ajax_referer( 'screen-options-nonce', 'screenoptionnonce' ); + + $enabled = isset( $_POST['enabled'] ) && is_string( $_POST['enabled'] ) && '1' === $_POST['enabled']; + update_user_option( get_current_user_id(), 'dashboard_show_on_this_day', $enabled ? 'opt-in' : 'opt-out' ); + + wp_die( 1 ); +} + /** * Handles updating whether to display the welcome panel via AJAX. * diff --git a/src/wp-admin/includes/class-wp-screen.php b/src/wp-admin/includes/class-wp-screen.php index 0008130355efb..be3ba6ae556bf 100644 --- a/src/wp-admin/includes/class-wp-screen.php +++ b/src/wp-admin/includes/class-wp-screen.php @@ -1011,6 +1011,7 @@ public function show_screen_options() { if ( $has_meta_boxes ) { $additional_settings .= $this->get_meta_box_reordering_option(); + $additional_settings .= $this->get_on_this_day_option(); } if ( 'post' === $this->base ) { @@ -1184,6 +1185,18 @@ private function get_meta_box_reordering_option() { . ''; } + /** + * Gets the option to enable or disable meta box reordering. + * + * @since 7.2.0 + * + * @return string Meta box reordering option markup. + */ + private function get_on_this_day_option(): string { + return '' + . ''; + } + /** * Checks whether the screen has meta boxes. * diff --git a/src/wp-admin/includes/dashboard.php b/src/wp-admin/includes/dashboard.php index 165b556f9eb85..ee2a6ef5e6fc5 100644 --- a/src/wp-admin/includes/dashboard.php +++ b/src/wp-admin/includes/dashboard.php @@ -1020,6 +1020,25 @@ function wp_dashboard_site_activity() { * @return bool False if no posts were found. True otherwise. */ function wp_dashboard_on_this_day( $posts = null ) { + if ( 'opt-out' === get_user_option( 'dashboard_show_on_this_day' ) ) { + $display_list = false; + $display_form = false; + } elseif ( 'opt-in' === get_user_option( 'dashboard_show_on_this_day' ) ) { + $display_list = true; + $display_form = false; + } else { + /* + * User has not yet chosen to opt-in or -out. + * + * As On This Day features can be distressing in some circumstances, + * users are prompted to opt-in or -out of the feature prior to the + * list of posts being displayed. If they have done neither a form is + * displayed prompting them to make a choice. + */ + $display_list = false; + $display_form = true; + } + if ( null === $posts ) { $posts = wp_dashboard_on_this_day_get_posts(); } @@ -1028,9 +1047,29 @@ function wp_dashboard_on_this_day( $posts = null ) { return false; } - echo '
'; - echo '

' . esc_html__( 'Published On This Day' ) . '

'; - echo '