|
104 | 104 | postboxWithinSortablesIndex = postboxesWithinSortables.index( postbox ), |
105 | 105 | firstOrLastPositionMessage; |
106 | 106 |
|
| 107 | + if ( ! postboxes.metaBoxReorderingEnabled ) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
107 | 111 | if ( 'dashboard_browser_nag' === postboxId ) { |
108 | 112 | return; |
109 | 113 | } |
|
344 | 348 | postboxes.save_order( page ); |
345 | 349 | } |
346 | 350 | }); |
| 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 | + } ); |
347 | 358 | }, |
348 | 359 |
|
349 | 360 | /** |
|
419 | 430 | } |
420 | 431 | }); |
421 | 432 |
|
| 433 | + this.setMetaBoxReordering( this.isMetaBoxReorderingEnabled() ); |
| 434 | + |
422 | 435 | if ( isMobile ) { |
423 | 436 | $(document.body).on('orientationchange.postboxes', function(){ postboxes._pb_change(); }); |
424 | 437 | this._pb_change(); |
|
437 | 450 | }); |
438 | 451 | }, |
439 | 452 |
|
| 453 | + /** |
| 454 | + * Checks whether meta box reordering is enabled. |
| 455 | + * |
| 456 | + * @since 7.1.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.1.0 |
| 474 | + * |
| 475 | + * @param {boolean} enabled Whether reordering should be enabled. |
| 476 | + * @return {void} |
| 477 | + */ |
| 478 | + setMetaBoxReordering: function( enabled ) { |
| 479 | + var $body = $( document.body ), |
| 480 | + $orderButtons = $( '.postbox .handle-order-higher, .postbox .handle-order-lower' ); |
| 481 | + |
| 482 | + this.metaBoxReorderingEnabled = !! enabled; |
| 483 | + |
| 484 | + $body |
| 485 | + .toggleClass( 'meta-box-reordering-enabled', this.metaBoxReorderingEnabled ) |
| 486 | + .toggleClass( 'meta-box-reordering-disabled', ! this.metaBoxReorderingEnabled ); |
| 487 | + |
| 488 | + if ( this.metaBoxReorderingEnabled ) { |
| 489 | + $orderButtons |
| 490 | + .prop( 'disabled', false ) |
| 491 | + .removeAttr( 'tabindex aria-hidden' ); |
| 492 | + this.updateOrderButtonsProperties(); |
| 493 | + } else { |
| 494 | + $orderButtons |
| 495 | + .prop( 'disabled', true ) |
| 496 | + .attr( { |
| 497 | + 'aria-hidden': 'true', |
| 498 | + tabindex: '-1' |
| 499 | + } ); |
| 500 | + } |
| 501 | + |
| 502 | + if ( window.wpResponsive && window.wpResponsive.maybeDisableSortables ) { |
| 503 | + window.wpResponsive.maybeDisableSortables(); |
| 504 | + return; |
| 505 | + } |
| 506 | + |
| 507 | + try { |
| 508 | + $( '.meta-box-sortables' ) |
| 509 | + .sortable( this.metaBoxReorderingEnabled ? 'enable' : 'disable' ) |
| 510 | + .find( '.ui-sortable-handle' ) |
| 511 | + .toggleClass( 'is-non-sortable', ! this.metaBoxReorderingEnabled ); |
| 512 | + } catch ( e ) {} |
| 513 | + }, |
| 514 | + |
440 | 515 | /** |
441 | 516 | * Saves the state of the postboxes to the server. |
442 | 517 | * |
|
476 | 551 | ); |
477 | 552 | }, |
478 | 553 |
|
| 554 | + /** |
| 555 | + * Saves the meta box reordering setting to the server. |
| 556 | + * |
| 557 | + * @since 7.1.0 |
| 558 | + * |
| 559 | + * @memberof postboxes |
| 560 | + * |
| 561 | + * @param {boolean} enabled Whether meta box reordering is enabled. |
| 562 | + * @return {void} |
| 563 | + */ |
| 564 | + save_meta_box_reordering_state : function( enabled ) { |
| 565 | + $.post( |
| 566 | + ajaxurl, |
| 567 | + { |
| 568 | + action: 'meta-box-reordering', |
| 569 | + enabled: enabled ? 1 : 0, |
| 570 | + screenoptionnonce: $( '#screenoptionnonce' ).val() |
| 571 | + }, |
| 572 | + function() { |
| 573 | + wp.a11y.speak( __( 'Screen Options updated.' ) ); |
| 574 | + } |
| 575 | + ); |
| 576 | + }, |
| 577 | + |
479 | 578 | /** |
480 | 579 | * Saves the order of the postboxes to the server. |
481 | 580 | * |
|
0 commit comments