Skip to content
Open
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 @@ -1845,7 +1845,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 @@ -1859,6 +1859,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
82 changes: 82 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.1.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.1.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,30 @@
);
},

/**
* Saves the meta box reordering setting to the server.
*
* @since 7.1.0
*
* @memberof postboxes
*
* @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( __( 'Screen Options updated.' ) );
}
);
},

/**
* 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 @@ -214,6 +214,10 @@
$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
17 changes: 17 additions & 0 deletions src/wp-admin/css/common.css
Original file line number Diff line number Diff line change
Expand Up @@ -1962,6 +1962,18 @@ p.auto-update-status {
line-height: 2.35;
}

.metabox-prefs > p {
margin: 0 0 8px;
}

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

.additional-settings-prefs legend {
padding-bottom: 2px;
}

#number-of-columns {
display: inline-block;
vertical-align: middle;
Expand Down Expand Up @@ -2259,6 +2271,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 @@ -1408,6 +1408,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 @@ -1856,6 +1856,20 @@ function wp_ajax_hidden_columns() {
wp_die( 1 );
}

/**
* Handles the meta box reordering setting via Ajax.
*
* @since 7.1.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' );

Comment thread
j111q marked this conversation as resolved.
wp_die( 1 );
}

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

$this->_screen_settings = '';

$additional_settings = '';
$show_meta_box_reordering_option = $this->show_meta_box_reordering_options();

if ( $show_meta_box_reordering_option ) {
$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;
if ( $show_meta_box_reordering_option ) {
$additional_settings .= '<label class="editor-expand hidden" for="editor-expand-toggle">';
$additional_settings .= '<input type="checkbox" id="editor-expand-toggle"' . checked( get_user_setting( 'editor_expand', 'on' ), 'on', false ) . ' /> ';
$additional_settings .= __( 'Enable full-height editor and distraction-free functionality.' ) . '</label>';
} else {
$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;
}
}

if ( $additional_settings ) {
$this->_screen_settings = '<fieldset class="metabox-prefs additional-settings-prefs">';
$this->_screen_settings .= '<legend>' . __( 'Additional settings' ) . '</legend>';
$this->_screen_settings .= $additional_settings;
$this->_screen_settings .= '</fieldset>';
}

/**
Expand All @@ -1023,7 +1043,7 @@ public function show_screen_options() {
*/
$this->_screen_settings = apply_filters( 'screen_settings', $this->_screen_settings, $this );

if ( $this->_screen_settings || $this->_options ) {
if ( $this->_screen_settings || $this->_options || $show_meta_box_reordering_option ) {
$show_screen = true;
}

Expand Down Expand Up @@ -1122,6 +1142,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->show_meta_box_reordering_options() ) : ?>
<?php _e( 'Use the setting below to control whether boxes can be rearranged.' ); ?>
<?php endif; ?>
</p>
<div class="metabox-prefs-container">
<?php
Expand All @@ -1148,6 +1171,35 @@ public function render_meta_boxes_preferences() {
<?php
}

/**
* Gets the option to enable or disable meta box reordering.
*
* @since 7.1.0
*
* @return string Meta box reordering option markup.
*/
private function get_meta_box_reordering_option() {
return '<label for="meta-box-reordering">'
. '<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 ) . ' /> '
. __( 'Allow boxes to be rearranged' )
. '</label>';
}

/**
* Determines whether to show the meta box reordering option.
*
* @since 7.1.0
*
* @global array $wp_meta_boxes Global meta box state.
*
* @return bool Whether to show the option.
*/
private function show_meta_box_reordering_options() {
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.1.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