Make WordPress Core

Changeset 64301


Ignore:
Timestamp:
10/11/2026 09:24:39 PM (5 weeks ago)
Author:
joedolson
Message:

Admin: Add screen option to turn off moving meta boxes.

In most cases, changing the order and position of dashboard or post meta boxes is an occasional need. When it's not needed, the extra two controls for every meta box create a high number of extra tab stops and a busy design. The drag and drop can result in accidental moves and difficulty scrolling on mobile devices due to accidental touch effects.

Add a screen option that allows users to disable moving meta boxes, disabling mover buttons and drag and drop behavior. The setting is defaulted to enable moving meta boxes, so there is no change in the default experience for users.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/12627.

Props azaozz, pbiron, afercia, johnbillion, hellofromtonya, helen, knutsp, johnjamesjacoby, garrett-eclipse, margaretschneider, johnstonphilip, hedgefield, sanketparmar, poligilad, sabernhardt, tyxla, jillq, sarahricker, ryokuhi, poena, abrain, SergeyBiryukov, joedolson.
Fixes #50699, #51419.

Location:
trunk
Files:
1 added
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/js/_enqueues/admin/common.js

    r64282 r64301  
    18591859
    18601860                        // When the screen columns are changed, potentially disable sortables.
    1861                         $( '#screen-options-wrap input' ).on( 'click', this.maybeDisableSortables );
     1861                        $( '#screen-options-wrap input:not(.meta-box-reordering-toggle)' ).on( 'click', this.maybeDisableSortables );
    18621862                },
    18631863
    … …  
    18731873
    18741874                        if (
     1875                                $body.hasClass( 'meta-box-reordering-disabled' ) ||
    18751876                                ( width <= 782 ) ||
    18761877                                ( 1 >= $sortables.find( '.ui-sortable-handle:visible' ).length && jQuery( '.columns-prefs-1 input' ).prop( 'checked' ) )
  • trunk/src/js/_enqueues/admin/postbox.js

    r63600 r64301  
    105105                                firstOrLastPositionMessage;
    106106
     107                        if ( ! postboxes.metaBoxReorderingEnabled ) {
     108                                return;
     109                        }
     110
    107111                        if ( 'dashboard_browser_nag' === postboxId ) {
    108112                                return;
    … …  
    345349                                }
    346350                        });
     351
     352                        $( '.meta-box-reordering-toggle' ).on( 'click.postboxes', function() {
     353                                var enabled = $( this ).prop( 'checked' );
     354
     355                                postboxes.setMetaBoxReordering( enabled );
     356                                postboxes.save_meta_box_reordering_state( enabled );
     357                        } );
    347358                },
    348359
    … …  
    420431                        });
    421432
     433                        this.setMetaBoxReordering( this.isMetaBoxReorderingEnabled() );
     434
    422435                        if ( isMobile ) {
    423436                                $(document.body).on('orientationchange.postboxes', function(){ postboxes._pb_change(); });
    … …  
    436449                                $el.attr( 'aria-expanded', ! $el.closest( '.postbox' ).hasClass( 'closed' ) );
    437450                        });
     451                },
     452
     453                /**
     454                 * Checks whether meta box reordering is enabled.
     455                 *
     456                 * @since 7.2.0
     457                 *
     458                 * @return {boolean} Whether meta box reordering is enabled.
     459                 */
     460                isMetaBoxReorderingEnabled: function() {
     461                        var $toggle = $( '#meta-box-reordering' );
     462
     463                        if ( $toggle.length ) {
     464                                return $toggle.prop( 'checked' );
     465                        }
     466
     467                        return ! $( document.body ).hasClass( 'meta-box-reordering-disabled' );
     468                },
     469
     470                /**
     471                 * Enables or disables the meta box reordering UI.
     472                 *
     473                 * @since 7.2.0
     474                 *
     475                 * @param {boolean} enabled Whether reordering should be enabled.
     476                 * @return {void}
     477                 */
     478                setMetaBoxReordering: function( enabled ) {
     479                        var $sortables = $( '.meta-box-sortables' );
     480
     481                        this.metaBoxReorderingEnabled = enabled;
     482
     483                        $( document.body ).toggleClass( 'meta-box-reordering-disabled', ! this.metaBoxReorderingEnabled );
     484
     485                        if ( window.wpResponsive && window.wpResponsive.maybeDisableSortables ) {
     486                                window.wpResponsive.maybeDisableSortables();
     487                                return;
     488                        }
     489
     490                        if ( $sortables.hasClass( 'ui-sortable' ) ) {
     491                                $sortables
     492                                        .sortable( this.metaBoxReorderingEnabled ? 'enable' : 'disable' )
     493                                        .find( '.ui-sortable-handle' )
     494                                                .toggleClass( 'is-non-sortable', ! this.metaBoxReorderingEnabled );
     495                        }
    438496                },
    439497
    … …  
    473531                                function() {
    474532                                        wp.a11y.speak( __( 'Screen Options updated.' ) );
     533                                }
     534                        );
     535                },
     536
     537                /**
     538                 * Saves the meta box reordering setting to the server.
     539                 *
     540                 * @since 7.2.0
     541                 *
     542                 * @param {boolean} enabled Whether meta box reordering is enabled.
     543                 * @return {void}
     544                 */
     545                save_meta_box_reordering_state: function( enabled ) {
     546                        $.post(
     547                                ajaxurl,
     548                                {
     549                                        action: 'meta-box-reordering',
     550                                        enabled: enabled ? 1 : 0,
     551                                        screenoptionnonce: $( '#screenoptionnonce' ).val()
     552                                },
     553                                function() {
     554                                        wp.a11y.speak(
     555                                                enabled ?
     556                                                        __( 'Rearranging screen elements enabled.' ) :
     557                                                        __( 'Rearranging screen elements disabled.' )
     558                                        );
    475559                                }
    476560                        );
  • trunk/src/wp-admin/admin-ajax.php

    r58672 r64301  
    8080        'closed-postboxes',
    8181        'hidden-columns',
     82        'meta-box-reordering',
    8283        'update-welcome-panel',
    8384        'menu-get-metabox',
  • trunk/src/wp-admin/admin-header.php

    r64120 r64301  
    246246}
    247247
     248if ( ! wp_is_meta_box_reordering_enabled() ) {
     249        $admin_body_class .= ' meta-box-reordering-disabled';
     250}
     251
    248252$admin_body_class .= ' wp-theme-' . sanitize_html_class( get_template() );
    249253if ( is_child_theme() ) {
  • trunk/src/wp-admin/css/common.css

    r64229 r64301  
    19911991}
    19921992
     1993.additional-settings-prefs {
     1994        margin-top: 14px;
     1995}
     1996
    19931997/* This ID appears to be unused in Core. */
    19941998#number-of-columns {
    … …  
    22852289        color: #646970;
    22862290        width: 1.62rem;
     2291}
     2292
     2293.js.meta-box-reordering-disabled .postbox .handle-order-higher,
     2294.js.meta-box-reordering-disabled .postbox .handle-order-lower {
     2295        display: none;
    22872296}
    22882297
  • trunk/src/wp-admin/css/dashboard.css

    r63969 r64301  
    13801380}
    13811381
     1382.js.meta-box-reordering-disabled #dashboard-widgets .postbox-container .empty-container {
     1383        display: none;
     1384}
     1385
    13821386@media screen and (max-width: 870px) {
    13831387        /* @deprecated 5.9.0 -- Lists removed from welcome panel. */
  • trunk/src/wp-admin/includes/ajax-actions.php

    r63959 r64301  
    18761876
    18771877/**
     1878 * Handles the meta box reordering setting via Ajax.
     1879 *
     1880 * @since 7.2.0
     1881 */
     1882function wp_ajax_meta_box_reordering() {
     1883        check_ajax_referer( 'screen-options-nonce', 'screenoptionnonce' );
     1884
     1885        $enabled = isset( $_POST['enabled'] ) && '1' === (string) $_POST['enabled'];
     1886        update_user_option( get_current_user_id(), 'meta_box_reordering', $enabled ? 'enabled' : 'disabled' );
     1887
     1888        wp_die( 1 );
     1889}
     1890
     1891/**
    18781892 * Handles updating whether to display the welcome panel via AJAX.
    18791893 *
  • trunk/src/wp-admin/includes/class-wp-screen.php

    r63648 r64301  
    10071007                $this->_screen_settings = '';
    10081008
     1009                $additional_settings = '';
     1010                $has_meta_boxes      = $this->has_meta_boxes();
     1011
     1012                if ( $has_meta_boxes ) {
     1013                        $additional_settings .= $this->get_meta_box_reordering_option();
     1014                }
     1015
    10091016                if ( 'post' === $this->base ) {
    1010                         $expand                 = '<fieldset class="editor-expand hidden"><legend>' . __( 'Additional settings' ) . '</legend><label for="editor-expand-toggle">';
    1011                         $expand                .= '<input type="checkbox" id="editor-expand-toggle"' . checked( get_user_setting( 'editor_expand', 'on' ), 'on', false ) . ' />';
    1012                         $expand                .= __( 'Enable full-height editor and distraction-free functionality.' ) . '</label></fieldset>';
    1013                         $this->_screen_settings = $expand;
     1017                        $additional_settings .= '<span class="editor-expand hidden">';
     1018                        $additional_settings .= '<input type="checkbox" id="editor-expand-toggle"' . checked( get_user_setting( 'editor_expand', 'on' ), 'on', false ) . ' />';
     1019                        $additional_settings .= '<label for="editor-expand-toggle">' . __( 'Enable full-height editor and distraction-free functionality.' ) . '</label>';
     1020                        $additional_settings .= '</span>';
     1021                }
     1022
     1023                if ( $additional_settings ) {
     1024                        $fieldset_class = 'metabox-prefs additional-settings-prefs';
     1025
     1026                        // The editor expand setting is revealed by JavaScript, so keep the fieldset hidden when it is the only setting.
     1027                        if ( ! $has_meta_boxes ) {
     1028                                $fieldset_class .= ' editor-expand hidden';
     1029                        }
     1030
     1031                        $this->_screen_settings  = '<fieldset class="' . $fieldset_class . '">';
     1032                        $this->_screen_settings .= '<legend>' . __( 'Additional settings' ) . '</legend>';
     1033                        $this->_screen_settings .= $additional_settings;
     1034                        $this->_screen_settings .= '</fieldset>';
    10141035                }
    10151036
    … …  
    11231144                        <?php _e( 'Some screen elements can be shown or hidden by using the checkboxes.' ); ?>
    11241145                        <?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.' ); ?>
     1146                        <?php if ( $this->has_meta_boxes() ) : ?>
     1147                                <?php _e( 'Use the setting below to control whether screen elements can be rearranged.' ); ?>
     1148                        <?php endif; ?>
    11251149                </p>
    11261150                <div class="metabox-prefs-container">
    … …  
    11471171                </fieldset>
    11481172                <?php
     1173        }
     1174
     1175        /**
     1176         * Gets the option to enable or disable meta box reordering.
     1177         *
     1178         * @since 7.2.0
     1179         *
     1180         * @return string Meta box reordering option markup.
     1181         */
     1182        private function get_meta_box_reordering_option() {
     1183                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 ) . ' />'
     1184                        . '<label for="meta-box-reordering">' . __( 'Allow screen elements to be rearranged' ) . '</label>';
     1185        }
     1186
     1187        /**
     1188         * Checks whether the screen has meta boxes.
     1189         *
     1190         * @since 7.2.0
     1191         *
     1192         * @global array $wp_meta_boxes Global meta box state.
     1193         *
     1194         * @return bool Whether the screen has meta boxes.
     1195         */
     1196        private function has_meta_boxes() {
     1197                global $wp_meta_boxes;
     1198
     1199                return ! empty( $wp_meta_boxes[ $this->id ] );
    11491200        }
    11501201
  • trunk/src/wp-admin/includes/screen.php

    r61484 r64301  
    196196
    197197/**
     198 * Determines whether meta box reordering is enabled.
     199 *
     200 * @since 7.2.0
     201 *
     202 * @return bool Whether meta box reordering is enabled.
     203 */
     204function wp_is_meta_box_reordering_enabled() {
     205        $setting = get_user_option( 'meta_box_reordering' );
     206
     207        return false === $setting ? true : 'disabled' !== $setting;
     208}
     209
     210/**
    198211 * Register and configure an admin screen option
    199212 *
  • trunk/tests/phpunit/includes/testcase-ajax.php

    r63593 r64301  
    6767                'closed-postboxes',
    6868                'hidden-columns',
     69                'meta-box-reordering',
    6970                'update-welcome-panel',
    7071                'menu-get-metabox',
  • trunk/tests/phpunit/tests/admin/includesScreen.php

    r61484 r64301  
    261261        }
    262262
     263        public function test_meta_box_reordering_enabled_defaults_to_true() {
     264                wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
     265
     266                $this->assertTrue( wp_is_meta_box_reordering_enabled() );
     267        }
     268
     269        public function test_meta_box_reordering_enabled_respects_user_option() {
     270                $user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
     271                wp_set_current_user( $user_id );
     272                update_user_option( $user_id, 'meta_box_reordering', 'disabled' );
     273
     274                $this->assertFalse( wp_is_meta_box_reordering_enabled() );
     275        }
     276
     277        public function test_meta_box_reordering_option_is_rendered_for_screens_with_meta_boxes() {
     278                global $wp_meta_boxes;
     279
     280                $old_wp_meta_boxes = $wp_meta_boxes;
     281                set_current_screen( 'index.php' );
     282                $screen = get_current_screen();
     283
     284                add_meta_box( 'testbox1', 'Test Metabox', '__return_false', $screen );
     285
     286                try {
     287                        $screen->show_screen_options();
     288
     289                        ob_start();
     290                        $screen->render_screen_options();
     291                        $output = ob_get_clean();
     292                } finally {
     293                        $wp_meta_boxes = $old_wp_meta_boxes;
     294                }
     295
     296                $this->assertStringContainsString( 'id="meta-box-reordering"', $output );
     297                $this->assertStringContainsString( 'Additional settings', $output );
     298                $this->assertStringContainsString( '<label for="meta-box-reordering">Allow screen elements to be rearranged</label>', $output );
     299                $this->assertStringContainsString( 'Some screen elements can be shown or hidden by using the checkboxes.', $output );
     300                $this->assertStringContainsString( '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.', $output );
     301                $this->assertStringContainsString( 'Use the setting below to control whether screen elements can be rearranged.', $output );
     302        }
     303
     304        public function test_meta_box_reordering_and_editor_expand_options_share_additional_settings_fieldset() {
     305                global $wp_meta_boxes;
     306
     307                $old_wp_meta_boxes = $wp_meta_boxes;
     308
     309                // WP_Screen caches its settings per screen, so use a different post screen than the other tests.
     310                set_current_screen( 'page' );
     311                $screen = get_current_screen();
     312
     313                add_meta_box( 'testbox1', 'Test Metabox', '__return_false', $screen );
     314
     315                try {
     316                        $screen->show_screen_options();
     317
     318                        ob_start();
     319                        $screen->render_screen_options();
     320                        $output = ob_get_clean();
     321                } finally {
     322                        $wp_meta_boxes = $old_wp_meta_boxes;
     323                }
     324
     325                $this->assertSame( 1, substr_count( $output, '<legend>Additional settings</legend>' ) );
     326                $this->assertStringContainsString( '<fieldset class="metabox-prefs additional-settings-prefs">', $output );
     327                $this->assertStringContainsString( '<label for="meta-box-reordering">Allow screen elements to be rearranged</label>', $output );
     328                $this->assertStringContainsString( '<label for="editor-expand-toggle">Enable full-height editor and distraction-free functionality.</label>', $output );
     329        }
     330
     331        public function test_meta_box_reordering_option_is_not_rendered_for_screens_without_meta_boxes() {
     332                global $wp_meta_boxes;
     333
     334                $old_wp_meta_boxes = $wp_meta_boxes;
     335                set_current_screen( 'edit.php' );
     336                $screen = get_current_screen();
     337
     338                unset( $wp_meta_boxes[ $screen->id ] );
     339
     340                try {
     341                        $screen->show_screen_options();
     342
     343                        ob_start();
     344                        $screen->render_screen_options();
     345                        $output = ob_get_clean();
     346                } finally {
     347                        $wp_meta_boxes = $old_wp_meta_boxes;
     348                }
     349
     350                $this->assertStringNotContainsString( 'id="meta-box-reordering"', $output );
     351                $this->assertStringNotContainsString( 'Additional settings', $output );
     352        }
     353
     354        public function test_additional_settings_fieldset_is_hidden_when_editor_expand_is_the_only_option() {
     355                global $wp_meta_boxes;
     356
     357                $old_wp_meta_boxes = $wp_meta_boxes;
     358                set_current_screen( 'post.php' );
     359                $screen = get_current_screen();
     360
     361                unset( $wp_meta_boxes[ $screen->id ] );
     362
     363                try {
     364                        $screen->show_screen_options();
     365
     366                        ob_start();
     367                        $screen->render_screen_options();
     368                        $output = ob_get_clean();
     369                } finally {
     370                        $wp_meta_boxes = $old_wp_meta_boxes;
     371                }
     372
     373                $this->assertStringNotContainsString( 'id="meta-box-reordering"', $output );
     374                $this->assertStringContainsString( '<fieldset class="metabox-prefs additional-settings-prefs editor-expand hidden">', $output );
     375                $this->assertStringContainsString( 'Enable full-height editor and distraction-free functionality.', $output );
     376        }
     377
    263378        public function test_post_type_with_edit_prefix() {
    264379                register_post_type( 'edit-some-thing' );
Note: See TracChangeset for help on using the changeset viewer.