Skip to content
Closed
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
3 changes: 2 additions & 1 deletion src/js/_enqueues/admin/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -1858,7 +1858,7 @@ $( function() {
$document.on( 'postbox-toggled', this.maybeDisableSortables );

// When the screen columns are changed, potentially disable sortables.
$( '#screen-options-wrap input' ).on( 'click', this.maybeDisableSortables );
$( '#screen-options-wrap input:not(.meta-box-reordering-toggle)' ).on( 'click', this.maybeDisableSortables );
},

/**
Expand All @@ -1872,6 +1872,7 @@ $( function() {
var width = navigator.userAgent.indexOf('AppleWebKit/') > -1 ? $window.width() : window.innerWidth;

if (
$body.hasClass( 'meta-box-reordering-disabled' ) ||
( width <= 782 ) ||
( 1 >= $sortables.find( '.ui-sortable-handle:visible' ).length && jQuery( '.columns-prefs-1 input' ).prop( 'checked' ) )
) {
Expand Down
84 changes: 84 additions & 0 deletions src/js/_enqueues/admin/postbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@
postboxWithinSortablesIndex = postboxesWithinSortables.index( postbox ),
firstOrLastPositionMessage;

if ( ! postboxes.metaBoxReorderingEnabled ) {
return;
}

if ( 'dashboard_browser_nag' === postboxId ) {
return;
}
Expand Down Expand Up @@ -344,6 +348,13 @@
postboxes.save_order( page );
}
});

$( '.meta-box-reordering-toggle' ).on( 'click.postboxes', function() {
var enabled = $( this ).prop( 'checked' );

postboxes.setMetaBoxReordering( enabled );
postboxes.save_meta_box_reordering_state( enabled );
} );
},

/**
Expand Down Expand Up @@ -419,6 +430,8 @@
}
});

this.setMetaBoxReordering( this.isMetaBoxReorderingEnabled() );

if ( isMobile ) {
$(document.body).on('orientationchange.postboxes', function(){ postboxes._pb_change(); });
this._pb_change();
Expand All @@ -437,6 +450,51 @@
});
},

/**
* Checks whether meta box reordering is enabled.
*
* @since 7.2.0
*
* @return {boolean} Whether meta box reordering is enabled.
*/
isMetaBoxReorderingEnabled: function() {
var $toggle = $( '#meta-box-reordering' );

if ( $toggle.length ) {
return $toggle.prop( 'checked' );
}

return ! $( document.body ).hasClass( 'meta-box-reordering-disabled' );
},

/**
* Enables or disables the meta box reordering UI.
*
* @since 7.2.0
*
* @param {boolean} enabled Whether reordering should be enabled.
* @return {void}
*/
setMetaBoxReordering: function( enabled ) {
var $sortables = $( '.meta-box-sortables' );

this.metaBoxReorderingEnabled = enabled;

$( document.body ).toggleClass( 'meta-box-reordering-disabled', ! this.metaBoxReorderingEnabled );

if ( window.wpResponsive && window.wpResponsive.maybeDisableSortables ) {
window.wpResponsive.maybeDisableSortables();
return;
}

if ( $sortables.hasClass( 'ui-sortable' ) ) {
$sortables
.sortable( this.metaBoxReorderingEnabled ? 'enable' : 'disable' )
.find( '.ui-sortable-handle' )
.toggleClass( 'is-non-sortable', ! this.metaBoxReorderingEnabled );
}
},

/**
* Saves the state of the postboxes to the server.
*
Expand Down Expand Up @@ -476,6 +534,32 @@
);
},

/**
* Saves the meta box reordering setting to the server.
*
* @since 7.2.0
*
* @param {boolean} enabled Whether meta box reordering is enabled.
* @return {void}
*/
save_meta_box_reordering_state: function( enabled ) {
$.post(
ajaxurl,
{
action: 'meta-box-reordering',
enabled: enabled ? 1 : 0,
screenoptionnonce: $( '#screenoptionnonce' ).val()
},
function() {
wp.a11y.speak(
enabled ?
__( 'Rearranging screen elements enabled.' ) :
__( 'Rearranging screen elements disabled.' )
);
}
);
},

/**
* Saves the order of the postboxes to the server.
*
Expand Down
1 change: 1 addition & 0 deletions src/wp-admin/admin-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
'add-user',
'closed-postboxes',
'hidden-columns',
'meta-box-reordering',
'update-welcome-panel',
'menu-get-metabox',
'wp-link-ajax',
Expand Down
4 changes: 4 additions & 0 deletions src/wp-admin/admin-header.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ function addLoadEvent( func ) {
$admin_body_class .= ' block-editor-page wp-embed-responsive';
}

if ( ! wp_is_meta_box_reordering_enabled() ) {
$admin_body_class .= ' meta-box-reordering-disabled';
}

$admin_body_class .= ' wp-theme-' . sanitize_html_class( get_template() );
if ( is_child_theme() ) {
$admin_body_class .= ' wp-child-theme-' . sanitize_html_class( get_stylesheet() );
Expand Down
9 changes: 9 additions & 0 deletions src/wp-admin/css/common.css
Original file line number Diff line number Diff line change
Expand Up @@ -1990,6 +1990,10 @@ p.auto-update-status {
line-height: 2.35;
}

.additional-settings-prefs {
margin-top: 14px;
}

/* This ID appears to be unused in Core. */
#number-of-columns {
display: inline-block;
Expand Down Expand Up @@ -2286,6 +2290,11 @@ html.wp-toolbar {
width: 1.62rem;
}

.js.meta-box-reordering-disabled .postbox .handle-order-higher,
.js.meta-box-reordering-disabled .postbox .handle-order-lower {
display: none;
}

/* Post box order buttons in the block editor meta boxes area. */
.edit-post-meta-boxes-area .postbox .handle-order-higher,
.edit-post-meta-boxes-area .postbox .handle-order-lower {
Expand Down
4 changes: 4 additions & 0 deletions src/wp-admin/css/dashboard.css
Original file line number Diff line number Diff line change
Expand Up @@ -1379,6 +1379,10 @@ a.rsswidget {
}
}

.js.meta-box-reordering-disabled #dashboard-widgets .postbox-container .empty-container {
display: none;
}

@media screen and (max-width: 870px) {
/* @deprecated 5.9.0 -- Lists removed from welcome panel. */
.welcome-panel .welcome-panel-column li {
Expand Down
14 changes: 14 additions & 0 deletions src/wp-admin/includes/ajax-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1874,6 +1874,20 @@ function wp_ajax_hidden_columns() {
wp_die( 1 );
}

/**
* Handles the meta box reordering setting via Ajax.
*
* @since 7.2.0
*/
function wp_ajax_meta_box_reordering() {
check_ajax_referer( 'screen-options-nonce', 'screenoptionnonce' );

$enabled = isset( $_POST['enabled'] ) && '1' === (string) $_POST['enabled'];
update_user_option( get_current_user_id(), 'meta_box_reordering', $enabled ? 'enabled' : 'disabled' );

wp_die( 1 );
}

/**
* Handles updating whether to display the welcome panel via AJAX.
*
Expand Down
59 changes: 55 additions & 4 deletions src/wp-admin/includes/class-wp-screen.php
Original file line number Diff line number Diff line change
Expand Up @@ -1006,11 +1006,32 @@ public function show_screen_options() {

$this->_screen_settings = '';

$additional_settings = '';
$has_meta_boxes = $this->has_meta_boxes();

if ( $has_meta_boxes ) {
$additional_settings .= $this->get_meta_box_reordering_option();
}

if ( 'post' === $this->base ) {
$expand = '<fieldset class="editor-expand hidden"><legend>' . __( 'Additional settings' ) . '</legend><label for="editor-expand-toggle">';
$expand .= '<input type="checkbox" id="editor-expand-toggle"' . checked( get_user_setting( 'editor_expand', 'on' ), 'on', false ) . ' />';
$expand .= __( 'Enable full-height editor and distraction-free functionality.' ) . '</label></fieldset>';
$this->_screen_settings = $expand;
$additional_settings .= '<span class="editor-expand hidden">';
$additional_settings .= '<input type="checkbox" id="editor-expand-toggle"' . checked( get_user_setting( 'editor_expand', 'on' ), 'on', false ) . ' />';
$additional_settings .= '<label for="editor-expand-toggle">' . __( 'Enable full-height editor and distraction-free functionality.' ) . '</label>';
$additional_settings .= '</span>';
}

if ( $additional_settings ) {
Comment thread
joedolson marked this conversation as resolved.
$fieldset_class = 'metabox-prefs additional-settings-prefs';

// The editor expand setting is revealed by JavaScript, so keep the fieldset hidden when it is the only setting.
if ( ! $has_meta_boxes ) {
$fieldset_class .= ' editor-expand hidden';
}

$this->_screen_settings = '<fieldset class="' . $fieldset_class . '">';
$this->_screen_settings .= '<legend>' . __( 'Additional settings' ) . '</legend>';
$this->_screen_settings .= $additional_settings;
$this->_screen_settings .= '</fieldset>';
}

/**
Expand Down Expand Up @@ -1122,6 +1143,9 @@ public function render_meta_boxes_preferences() {
<p>
<?php _e( 'Some screen elements can be shown or hidden by using the checkboxes.' ); ?>
<?php _e( 'Expand or collapse the elements by clicking on their headings, and arrange them by dragging their headings or by clicking on the up and down arrows.' ); ?>
<?php if ( $this->has_meta_boxes() ) : ?>
<?php _e( 'Use the setting below to control whether screen elements can be rearranged.' ); ?>
<?php endif; ?>
</p>
<div class="metabox-prefs-container">
<?php
Expand All @@ -1148,6 +1172,33 @@ public function render_meta_boxes_preferences() {
<?php
}

/**
* Gets the option to enable or disable meta box reordering.
*
* @since 7.2.0
*
* @return string Meta box reordering option markup.
*/
private function get_meta_box_reordering_option() {
return '<input class="meta-box-reordering-toggle" name="meta-box-reordering" type="checkbox" id="meta-box-reordering" value="enabled"' . checked( wp_is_meta_box_reordering_enabled(), true, false ) . ' />'
. '<label for="meta-box-reordering">' . __( 'Allow screen elements to be rearranged' ) . '</label>';
}

/**
* Checks whether the screen has meta boxes.
*
* @since 7.2.0
*
* @global array $wp_meta_boxes Global meta box state.
*
* @return bool Whether the screen has meta boxes.
*/
private function has_meta_boxes() {
global $wp_meta_boxes;

return ! empty( $wp_meta_boxes[ $this->id ] );
}

/**
* Renders the list table columns preferences.
*
Expand Down
13 changes: 13 additions & 0 deletions src/wp-admin/includes/screen.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,19 @@ function get_hidden_meta_boxes( $screen ) {
return apply_filters( 'hidden_meta_boxes', $hidden, $screen, $use_defaults );
}

/**
* Determines whether meta box reordering is enabled.
*
* @since 7.2.0
*
* @return bool Whether meta box reordering is enabled.
*/
function wp_is_meta_box_reordering_enabled() {
$setting = get_user_option( 'meta_box_reordering' );

return false === $setting ? true : 'disabled' !== $setting;
}

/**
* Register and configure an admin screen option
*
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/includes/testcase-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ abstract class WP_Ajax_UnitTestCase extends WP_UnitTestCase {
'add-user',
'closed-postboxes',
'hidden-columns',
'meta-box-reordering',
'update-welcome-panel',
'menu-get-metabox',
'wp-link-ajax',
Expand Down
Loading
Loading