diff --git a/editor/components/meta-boxes/meta-boxes-area/index.js b/editor/components/meta-boxes/meta-boxes-area/index.js index ce1e974d9ff5aa..e8d7ee20396e95 100644 --- a/editor/components/meta-boxes/meta-boxes-area/index.js +++ b/editor/components/meta-boxes/meta-boxes-area/index.js @@ -3,12 +3,10 @@ */ import classnames from 'classnames'; import { connect } from 'react-redux'; -import jQuery from 'jquery'; /** * WordPress dependencies */ -import { addQueryArgs } from '@wordpress/url'; import { Component } from '@wordpress/element'; import { Spinner } from '@wordpress/components'; @@ -16,86 +14,58 @@ import { Spinner } from '@wordpress/components'; * Internal dependencies */ import './style.scss'; -import { handleMetaBoxReload, metaBoxLoaded } from '../../../store/actions'; -import { getMetaBox, isSavingPost } from '../../../store/selectors'; +import { isSavingMetaBoxes } from '../../../store/selectors'; class MetaBoxesArea extends Component { + /** + * @inheritdoc + */ constructor() { super( ...arguments ); - - this.state = { - loading: false, - }; - this.originalFormData = ''; this.bindNode = this.bindNode.bind( this ); } - bindNode( node ) { - this.node = node; - } - + /** + * @inheritdoc + */ componentDidMount() { - this.mounted = true; - this.fetchMetaboxes(); + this.form = document.querySelector( '.metabox-location-' + this.props.location ); + this.node.appendChild( this.form ); } + /** + * Get the meta box location form from the original location. + */ componentWillUnmount() { - this.mounted = false; document.querySelector( '#metaboxes' ).appendChild( this.form ); } - componentWillReceiveProps( nextProps ) { - if ( nextProps.isUpdating && ! this.props.isUpdating ) { - this.setState( { loading: true } ); - const { location } = nextProps; - const headers = new window.Headers(); - const fetchOptions = { - method: 'POST', - headers, - body: new window.FormData( this.form ), - credentials: 'include', - }; - - // Save the metaboxes - window.fetch( addQueryArgs( window._wpMetaBoxUrl, { meta_box: location } ), fetchOptions ) - .then( () => { - if ( ! this.mounted ) { - return false; - } - this.setState( { loading: false } ); - this.props.metaBoxReloaded( location ); - } ); - } - } - - fetchMetaboxes() { - const { location } = this.props; - this.form = document.querySelector( '.metabox-location-' + location ); - this.node.appendChild( this.form ); - this.form.onSubmit = ( event ) => event.preventDefault(); - this.originalFormData = this.getFormData(); - this.props.metaBoxLoaded( location ); - } - - getFormData() { - return jQuery( this.form ).serialize(); + /** + * Binds the metabox area container node. + * + * @param {Element} node DOM Node. + */ + bindNode( node ) { + this.node = node; } + /** + * @inheritdoc + */ render() { - const { location } = this.props; - const { loading } = this.state; + const { location, isSaving } = this.props; const classes = classnames( 'editor-meta-boxes-area', `is-${ location }`, { - 'is-loading': loading, + 'is-loading': isSaving, } ); return (
- { loading && } + { isSaving && }
@@ -103,22 +73,13 @@ class MetaBoxesArea extends Component { } } -function mapStateToProps( state, ownProps ) { - const metaBox = getMetaBox( state, ownProps.location ); - const { isUpdating } = metaBox; - - return { - isUpdating, - isPostSaving: isSavingPost( state ) ? true : false, - }; -} - -function mapDispatchToProps( dispatch ) { +/** + * @inheritdoc + */ +function mapStateToProps( state ) { return { - // Used to set the reference to the MetaBox in redux, fired when the component mounts. - metaBoxReloaded: ( location ) => dispatch( handleMetaBoxReload( location ) ), - metaBoxLoaded: ( location ) => dispatch( metaBoxLoaded( location ) ), + isSaving: isSavingMetaBoxes( state ), }; } -export default connect( mapStateToProps, mapDispatchToProps )( MetaBoxesArea ); +export default connect( mapStateToProps )( MetaBoxesArea ); diff --git a/editor/components/post-saved-state/index.js b/editor/components/post-saved-state/index.js index aba2cfdc3fff5d..5339e88138ceb6 100644 --- a/editor/components/post-saved-state/index.js +++ b/editor/components/post-saved-state/index.js @@ -27,6 +27,12 @@ import { hasMetaBoxes, } from '../../store/selectors'; +/** + * Component showing whether the post is saved or not and displaying save links. + * + * @param {Object} Props Component Props. + * @returns {WPElement} WordPress Element. + */ export function PostSavedState( { hasActiveMetaboxes, isNew, isPublished, isDirty, isSaving, isSaveable, status, onStatusChange, onSave } ) { const className = 'editor-post-saved-state'; diff --git a/editor/components/unsaved-changes-warning/index.js b/editor/components/unsaved-changes-warning/index.js index 6664aee4ec3db4..c9b78dab2c896e 100644 --- a/editor/components/unsaved-changes-warning/index.js +++ b/editor/components/unsaved-changes-warning/index.js @@ -3,6 +3,7 @@ */ import { connect } from 'react-redux'; import { some } from 'lodash'; +import jQuery from 'jquery'; /** * WordPress dependencies @@ -14,25 +15,41 @@ import { Component } from '@wordpress/element'; * Internal dependencies */ import { isEditedPostDirty, getMetaBoxes } from '../../store/selectors'; -import { getLocationHtml } from '../../edit-post/meta-boxes'; +import { getMetaBoxContainer } from '../../edit-post/meta-boxes'; class UnsavedChangesWarning extends Component { + /** + * @inheritdoc + */ constructor() { super( ...arguments ); this.warnIfUnsavedChanges = this.warnIfUnsavedChanges.bind( this ); } + /** + * @inheritdoc + */ componentDidMount() { window.addEventListener( 'beforeunload', this.warnIfUnsavedChanges ); } + /** + * @inheritdoc + */ componentWillUnmount() { window.removeEventListener( 'beforeunload', this.warnIfUnsavedChanges ); } + /** + * Warns the user if there are unsaved changes before leaving the editor. + * + * @param {Event} event Event Object. + * @returns {string?} Warning message. + */ warnIfUnsavedChanges( event ) { - const areMetaBoxesDirty = some( this.props.metaBoxes, ( metaBoxe, location ) => { - return metaBoxe.isActive && getLocationHtml( location ) !== metaBoxe.html; + const areMetaBoxesDirty = some( this.props.metaBoxes, ( metaBox, location ) => { + return metaBox.isActive && + jQuery( getMetaBoxContainer( location ) ).serialize() !== metaBox.html; } ); if ( this.props.isDirty || areMetaBoxesDirty ) { event.returnValue = __( 'You have unsaved changes. If you proceed, they will be lost.' ); @@ -40,6 +57,9 @@ class UnsavedChangesWarning extends Component { } } + /** + * @inheritdoc + */ render() { return null; } diff --git a/editor/edit-post/meta-boxes.js b/editor/edit-post/meta-boxes.js index df3580017db150..b952aa42f23e5d 100644 --- a/editor/edit-post/meta-boxes.js +++ b/editor/edit-post/meta-boxes.js @@ -1,16 +1,16 @@ /** - * Function returning the current Meta Boxes HTML in the editor + * Function returning the current Meta Boxes DOM Node in the editor * whether the meta box area is opened or not. - * This is not so clear, but I believe it's the only way to have this data synchronously + * If the MetaBox Area is visible returns it, and returns the original container instead. * - * @param {String} location Meta Box location - * @returns {String} HTML content + * @param {string} location Meta Box location. + * @returns {string} HTML content. */ -export const getLocationHtml = ( location ) => { - const area = document.querySelector( `.editor-meta-boxes-area.is-${ location } .editor-meta-boxes-area__container` ); +export const getMetaBoxContainer = ( location ) => { + const area = document.querySelector( `.editor-meta-boxes-area.is-${ location } .metabox-location-${ location }` ); if ( area ) { - return area.innerHTML; + return area; } - return document.querySelector( '.metabox-location-' + location ).innerHTML; + return document.querySelector( '#metaboxes .metabox-location-' + location ); }; diff --git a/editor/store/actions.js b/editor/store/actions.js index 0a40c87ed8760c..f0bdba753cb675 100644 --- a/editor/store/actions.js +++ b/editor/store/actions.js @@ -438,11 +438,11 @@ export function removeNotice( id ) { * not render the meta box area. * * Example: metaBoxes = { side: true, normal: false } - * This indicates that the sidebar has a meta box but the normal area does not. + * this indicates that the sidebar has a meta box but the normal area does not. * - * @param {Object} metaBoxes Whether meta box locations are active. + * @param {Object} metaBoxes Whether meta box locations are active. * - * @returns {Object} Action object. + * @returns {Object} Action object. */ export function initializeMetaBoxState( metaBoxes ) { return { @@ -452,41 +452,38 @@ export function initializeMetaBoxState( metaBoxes ) { } /** - * Returns an action object used to signify that a meta box finished reloading. - * - * @param {String} location Location of meta box: 'normal', 'side' or 'advanced'. + * Returns an action object used to request meta box update. * * @returns {Object} Action object. */ -export function handleMetaBoxReload( location ) { +export function requestMetaBoxUpdates() { return { - type: 'HANDLE_META_BOX_RELOAD', - location, + type: 'REQUEST_META_BOX_UPDATES', }; } /** - * Returns an action object used to signify that a meta box finished loading. - * - * @param {String} location Location of meta box: 'normal', 'side' or 'advanced'. + * Returns an action object used signal a successfull meta nox update. * * @returns {Object} Action object. */ -export function metaBoxLoaded( location ) { +export function metaBoxUpdatesSuccess() { return { - type: 'META_BOX_LOADED', - location, + type: 'META_BOX_UPDATES_SUCCESS', }; } /** - * Returns an action object used to request meta box update. + * Returns an action object used set the saved meta boxes data. + * This is used to check if the meta boxes have been touched when leaving the editor. * - * @returns {Object} Action object. + * @param {Object} dataPerLocation Meta Boxes Data per location. + * @returns {Object} Action object. */ -export function requestMetaBoxUpdates() { +export function metaBoxSetSavedData( dataPerLocation ) { return { - type: 'REQUEST_META_BOX_UPDATES', + type: 'META_BOX_SET_SAVED_DATA', + dataPerLocation, }; } diff --git a/editor/store/effects.js b/editor/store/effects.js index c1ed8e49bbbb7d..576ec63309d2d3 100644 --- a/editor/store/effects.js +++ b/editor/store/effects.js @@ -2,7 +2,7 @@ * External dependencies */ import { BEGIN, COMMIT, REVERT } from 'redux-optimist'; -import { get, includes, map, castArray, uniqueId } from 'lodash'; +import { get, includes, map, castArray, uniqueId, reduce, values } from 'lodash'; /** * WordPress dependencies @@ -36,9 +36,11 @@ import { savePost, editPost, requestMetaBoxUpdates, + metaBoxUpdatesSuccess, updateReusableBlock, saveReusableBlock, insertBlock, + metaBoxSetSavedData, } from './actions'; import { getCurrentPost, @@ -52,8 +54,10 @@ import { getBlock, getBlocks, getReusableBlock, + getMetaBoxes, POST_UPDATE_TRANSACTION_ID, } from './selectors'; +import { getMetaBoxContainer } from '../edit-post/meta-boxes'; /** * Module Constants @@ -451,4 +455,38 @@ export default { const message = spokenMessage || content; speak( message, 'assertive' ); }, + INITIALIZE_META_BOX_STATE( action, store ) { + const dataPerLocation = reduce( action.metaBoxes, ( memo, isActive, location ) => { + if ( isActive ) { + memo[ location ] = jQuery( getMetaBoxContainer( location ) ).serialize(); + } + return memo; + }, {} ); + store.dispatch( metaBoxSetSavedData( dataPerLocation ) ); + }, + REQUEST_META_BOX_UPDATES( action, store ) { + const dataPerLocation = reduce( getMetaBoxes( store.getState() ), ( memo, metabox, location ) => { + if ( metabox.isActive ) { + memo[ location ] = jQuery( getMetaBoxContainer( location ) ).serialize(); + } + return memo; + }, {} ); + store.dispatch( metaBoxSetSavedData( dataPerLocation ) ); + + // To save the metaboxes, we serialize each one of the location forms and combine them + // We also add the "common" hidden fields from the base .metabox-base-form + const formData = values( dataPerLocation ).concat( + jQuery( '.metabox-base-form' ).serialize() + ).join( '&' ); + const fetchOptions = { + method: 'POST', + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + body: formData, + credentials: 'include', + }; + + // Save the metaboxes + window.fetch( window._wpMetaBoxUrl, fetchOptions ) + .then( () => store.dispatch( metaBoxUpdatesSuccess() ) ); + }, }; diff --git a/editor/store/reducer.js b/editor/store/reducer.js index e64399bba96be4..7d4c664b5dcbc2 100644 --- a/editor/store/reducer.js +++ b/editor/store/reducer.js @@ -32,7 +32,6 @@ import { getBlockTypes, getBlockType } from '@wordpress/blocks'; import withHistory from '../utils/with-history'; import withChangeDetection from '../utils/with-change-detection'; import { PREFERENCES_DEFAULTS } from './defaults'; -import { getLocationHtml } from '../edit-post/meta-boxes'; /*** * Module constants @@ -662,49 +661,55 @@ const locations = [ const defaultMetaBoxState = locations.reduce( ( result, key ) => { result[ key ] = { isActive: false, - isUpdating: false, }; return result; }, {} ); +/** + * Reducer keeping track of the meta boxes saving state. + * + * @param {boolean} state Previous state. + * @param {Object } action Action Object. + * @returns {Object} Updated state. + */ +export function isSavingMetaBoxes( state = false, action ) { + switch ( action.type ) { + case 'REQUEST_META_BOX_UPDATES': + return true; + case 'META_BOX_UPDATES_SUCCESS': + return false; + default: + return state; + } +} + +/** + * + * Reducer keeping track of the meta boxes state. + * + * @param {boolean} state Previous state. + * @param {Object } action Action Object. + * @returns {Object} Updated state. + */ export function metaBoxes( state = defaultMetaBoxState, action ) { switch ( action.type ) { case 'INITIALIZE_META_BOX_STATE': return locations.reduce( ( newState, location ) => { newState[ location ] = { ...state[ location ], - isLoaded: false, isActive: action.metaBoxes[ location ], - html: getLocationHtml( location ), }; return newState; }, { ...state } ); - case 'META_BOX_LOADED': - return { - ...state, - [ action.location ]: { - ...state[ action.location ], - isLoaded: true, - isUpdating: false, - }, - }; - case 'HANDLE_META_BOX_RELOAD': - return { - ...state, - [ action.location ]: { - ...state[ action.location ], - isUpdating: false, - html: getLocationHtml( action.location ), - }, - }; - case 'REQUEST_META_BOX_UPDATES': - return mapValues( state, ( metaBox ) => { - return { - ...metaBox, - isUpdating: metaBox.isActive, + case 'META_BOX_SET_SAVED_DATA': + return locations.reduce( ( newState, location ) => { + newState[ location ] = { + ...state[ location ], + data: action.dataPerLocation[ location ], }; - } ); + return newState; + }, { ...state } ); default: return state; } @@ -825,6 +830,7 @@ export default optimist( combineReducers( { saving, notices, metaBoxes, + isSavingMetaBoxes, mobile, reusableBlocks, } ) ); diff --git a/editor/store/selectors.js b/editor/store/selectors.js index 20a387ab3b84ba..471fa1d67c4215 100644 --- a/editor/store/selectors.js +++ b/editor/store/selectors.js @@ -43,9 +43,8 @@ export function getEditorMode( state ) { /** * Returns the state of legacy meta boxes. * - * @param {Object} state Global application state - * - * @returns {Object} State of meta boxes. + * @param {Object} state Global application state. + * @returns {Object} State of meta boxes. */ export function getMetaBoxes( state ) { return state.metaBoxes; @@ -79,19 +78,14 @@ export const hasMetaBoxes = createSelector( ); /** - * Returns true if the the Meta Boxes are being saved + * Returns true if the the Meta Boxes are being saved. * - * @param {Object} state Global application state - * @return {Boolean} Whether the metaboxes are being saved. + * @param {Object} state Global application state. + * @returns {boolean} Whether the metaboxes are being saved. */ -export const isSavingMetaBoxes = createSelector( - ( state ) => { - return some( getMetaBoxes( state ), ( metaBox ) => { - return metaBox.isUpdating; - } ); - }, - ( state ) => state.metaBoxes, -); +export function isSavingMetaBoxes( state ) { + return state.isSavingMetaBoxes; +} /** * Returns the current active panel for the sidebar. diff --git a/editor/store/test/actions.js b/editor/store/test/actions.js index ac9e49fdeb9d86..f97c7abfe5f512 100644 --- a/editor/store/test/actions.js +++ b/editor/store/test/actions.js @@ -7,7 +7,6 @@ import { startTyping, stopTyping, requestMetaBoxUpdates, - handleMetaBoxReload, initializeMetaBoxState, fetchReusableBlocks, updateReusableBlock, @@ -51,7 +50,6 @@ import { createErrorNotice, createWarningNotice, removeNotice, - metaBoxLoaded, toggleFeature, } from '../actions'; @@ -514,16 +512,6 @@ describe( 'actions', () => { } ); } ); - describe( 'metaBoxLoaded', () => { - it( 'should return META_BOX_LOADED action', () => { - const location = 'normal'; - expect( metaBoxLoaded( location ) ).toEqual( { - type: 'META_BOX_LOADED', - location, - } ); - } ); - } ); - describe( 'toggleFeature', () => { it( 'should return TOGGLE_FEATURE action', () => { const feature = 'name'; @@ -542,15 +530,6 @@ describe( 'actions', () => { } ); } ); - describe( 'handleMetaBoxReload', () => { - it( 'should return the HANDLE_META_BOX_RELOAD action with a location and node', () => { - expect( handleMetaBoxReload( 'normal' ) ).toEqual( { - type: 'HANDLE_META_BOX_RELOAD', - location: 'normal', - } ); - } ); - } ); - describe( 'initializeMetaBoxState', () => { it( 'should return the META_BOX_STATE_CHANGED action with a hasChanged flag', () => { const metaBoxes = { diff --git a/editor/store/test/reducer.js b/editor/store/test/reducer.js index a207bf133672e2..243253fe4eab78 100644 --- a/editor/store/test/reducer.js +++ b/editor/store/test/reducer.js @@ -24,13 +24,14 @@ import { notices, blocksMode, blockInsertionPoint, + isSavingMetaBoxes, metaBoxes, reusableBlocks, } from '../reducer'; jest.mock( '../../edit-post/meta-boxes', () => { return { - getLocationHtml: () => 'meta boxes content', + getMetaBoxContainer: () => ( { innerHTML: 'meta boxes content' } ), }; } ); @@ -1242,21 +1243,43 @@ describe( 'state', () => { } ); } ); + describe( 'isSavingMetaBoxes', () => { + it( 'should return default state', () => { + const actual = isSavingMetaBoxes( undefined, {} ); + expect( actual ).toBe( false ); + } ); + + it( 'should set saving flag to true', () => { + const action = { + type: 'REQUEST_META_BOX_UPDATES', + }; + const actual = isSavingMetaBoxes( false, action ); + + expect( actual ).toBe( true ); + } ); + + it( 'should set saving flag to false', () => { + const action = { + type: 'META_BOX_UPDATES_SUCCESS', + }; + const actual = isSavingMetaBoxes( true, action ); + + expect( actual ).toBe( false ); + } ); + } ); + describe( 'metaBoxes()', () => { it( 'should return default state', () => { const actual = metaBoxes( undefined, {} ); const expected = { normal: { isActive: false, - isUpdating: false, }, side: { isActive: false, - isUpdating: false, }, advanced: { isActive: false, - isUpdating: false, }, }; @@ -1279,53 +1302,31 @@ describe( 'state', () => { const expected = { normal: { isActive: false, - isUpdating: false, - isLoaded: false, - html: 'meta boxes content', }, side: { isActive: true, - isUpdating: false, - isLoaded: false, - html: 'meta boxes content', }, advanced: { isActive: false, - isUpdating: false, - isLoaded: false, - html: 'meta boxes content', }, }; expect( actual ).toEqual( expected ); } ); - it( 'should switch updating to off', () => { + it( 'should set the meta boxes saved data', () => { const action = { - type: 'HANDLE_META_BOX_RELOAD', - location: 'normal', - }; - - const theMetaBoxes = metaBoxes( { normal: { isUpdating: true, isActive: false } }, action ); - const actual = theMetaBoxes.normal; - const expected = { - isActive: false, - isUpdating: false, - html: 'meta boxes content', - }; - - expect( actual ).toEqual( expected ); - } ); - - it( 'should switch updating to on', () => { - const action = { - type: 'REQUEST_META_BOX_UPDATES', + type: 'META_BOX_SET_SAVED_DATA', + dataPerLocation: { + side: 'a=b', + }, }; const theMetaBoxes = metaBoxes( { normal: { isActive: true }, side: { isActive: false } }, action ); expect( theMetaBoxes ).toEqual( { - normal: { isActive: true, isUpdating: true }, - side: { isActive: false, isUpdating: false }, + advanced: { data: undefined }, + normal: { isActive: true, data: undefined }, + side: { isActive: false, data: 'a=b' }, } ); } ); } ); diff --git a/editor/store/test/selectors.js b/editor/store/test/selectors.js index 7e0dae4d7ab9de..a82c80abd69e43 100644 --- a/editor/store/test/selectors.js +++ b/editor/store/test/selectors.js @@ -165,16 +165,7 @@ describe( 'selectors', () => { describe( 'isSavingMetaBoxes', () => { it( 'should return true if some meta boxes are saving', () => { const state = { - metaBoxes: { - normal: { - isActive: false, - isUpdating: false, - }, - side: { - isActive: true, - isUpdating: true, - }, - }, + isSavingMetaBoxes: true, }; expect( isSavingMetaBoxes( state ) ).toBe( true ); @@ -182,16 +173,7 @@ describe( 'selectors', () => { it( 'should return false if no meta boxes are saving', () => { const state = { - metaBoxes: { - normal: { - isActive: false, - isUpdating: false, - }, - side: { - isActive: true, - isUpdating: false, - }, - }, + isSavingMetaBoxes: false, }; expect( isSavingMetaBoxes( state ) ).toBe( false ); @@ -203,24 +185,20 @@ describe( 'selectors', () => { const state = { metaBoxes: { normal: { - isDirty: false, - isUpdating: false, + isActive: true, }, side: { - isDirty: false, - isUpdating: false, + isActive: true, }, }, }; expect( getMetaBoxes( state ) ).toEqual( { normal: { - isDirty: false, - isUpdating: false, + isActive: true, }, side: { - isDirty: false, - isUpdating: false, + isActive: true, }, } ); } ); @@ -232,21 +210,15 @@ describe( 'selectors', () => { metaBoxes: { normal: { isActive: false, - isDirty: false, - isUpdating: false, }, side: { isActive: true, - isDirty: false, - isUpdating: false, }, }, }; expect( getMetaBox( state, 'side' ) ).toEqual( { isActive: true, - isDirty: false, - isUpdating: false, } ); } ); } ); @@ -550,7 +522,7 @@ describe( 'selectors', () => { } ); describe( 'isCleanNewPost', () => { - const metaBoxes = { isDirty: false, isUpdating: false }; + const metaBoxes = {}; it( 'should return true when the post is not dirty and has not been saved before', () => { const state = { @@ -747,7 +719,7 @@ describe( 'selectors', () => { } ); describe( 'getDocumentTitle', () => { - const metaBoxes = { isDirty: false, isUpdating: false }; + const metaBoxes = {}; it( 'should return current title unedited existing post', () => { const state = { currentPost: { @@ -973,7 +945,7 @@ describe( 'selectors', () => { } ); describe( 'isEditedPostPublishable', () => { - const metaBoxes = { isDirty: false, isUpdating: false }; + const metaBoxes = {}; it( 'should return true for pending posts', () => { const state = { @@ -1964,6 +1936,7 @@ describe( 'selectors', () => { saving: { requesting: true, }, + isSavingMetaBoxes: false, }; expect( isSavingPost( state ) ).toBe( true ); @@ -1974,21 +1947,18 @@ describe( 'selectors', () => { saving: { requesting: false, }, + isSavingMetaBoxes: false, }; expect( isSavingPost( state ) ).toBe( false ); } ); - it( 'should return true if the post is notcurrently being saved but meta boxes are saving', () => { + it( 'should return true if the post is not currently being saved but meta boxes are saving', () => { const state = { saving: { requesting: false, }, - metaBoxes: { - normal: { - isUpdating: true, - }, - }, + isSavingMetaBoxes: true, }; expect( isSavingPost( state ) ).toBe( true ); diff --git a/lib/client-assets.php b/lib/client-assets.php index 20caf75b0f0151..f58040a9b66aa9 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -837,6 +837,7 @@ function gutenberg_editor_scripts_and_styles( $hook ) { 'post' => $post_to_edit['id'], 'action' => 'edit', 'classic-editor' => true, + 'meta_box' => true, ), $meta_box_url ); wp_localize_script( 'wp-editor', '_wpMetaBoxUrl', $meta_box_url ); diff --git a/lib/meta-box-partial-page.php b/lib/meta-box-partial-page.php index 6a55a062ba0f8c..8aecf98923c756 100644 --- a/lib/meta-box-partial-page.php +++ b/lib/meta-box-partial-page.php @@ -16,9 +16,8 @@ * @since 1.8.0 * * @param string $post_type Current post type. - * @param string $meta_box_context The context location of the meta box. Referred to as context in core. */ -function gutenberg_meta_box_save( $post_type, $meta_box_context ) { +function gutenberg_meta_box_save( $post_type ) { /** * Needs classic editor to be active. * @@ -45,33 +44,9 @@ function gutenberg_meta_box_save( $post_type, $meta_box_context ) { return; } - /** - * Prevent over firing of the meta box rendering. - * - * The hook do_action( 'do_meta_boxes', ... ) fires three times in - * edit-form-advanced.php - * - * To make sure we properly fire on all three meta box locations, except - * advanced, as advanced is tied in with normal for ease of use reasons, we - * need to verify that the action location/context matches our requests - * meta box location/context. We then exit early if they do not match. - * This will prevent execution thread from dieing, so the subsequent calls - * to do_meta_boxes can fire. - */ - if ( $_REQUEST['meta_box'] !== $meta_box_context ) { - return; - } - // Ths action is not needed since it's an XHR call. remove_action( 'admin_head', 'wp_admin_canonical_url' ); - - $location = $_REQUEST['meta_box']; - - if ( ! in_array( $_REQUEST['meta_box'], array( 'side', 'normal', 'advanced' ) ) ) { - wp_die( __( 'The `meta_box` parameter should be one of "side", "normal", or "advanced".', 'gutenberg' ) ); - } - - the_gutenberg_metaboxes( array( $location ) ); + the_gutenberg_metaboxes(); } add_action( 'do_meta_boxes', 'gutenberg_meta_box_save', 1000, 2 ); @@ -88,13 +63,10 @@ function gutenberg_meta_box_save( $post_type, $meta_box_context ) { * @hooked redirect_post_location priority 10 */ function gutenberg_meta_box_save_redirect( $location, $post_id ) { - if ( isset( $_REQUEST['gutenberg_meta_boxes'] ) - && isset( $_REQUEST['gutenberg_meta_box_location'] ) - && 'gutenberg_meta_boxes' === $_REQUEST['gutenberg_meta_boxes'] ) { - $meta_box_location = $_REQUEST['gutenberg_meta_box_location']; - $location = add_query_arg( + if ( isset( $_REQUEST['gutenberg_meta_boxes'] ) ) { + $location = add_query_arg( array( - 'meta_box' => $meta_box_location, + 'meta_box' => true, 'action' => 'edit', 'classic-editor' => true, 'post' => $post_id, @@ -313,10 +285,8 @@ function gutenberg_show_meta_box_warning( $callback ) { * Renders the WP meta boxes forms. * * @since 1.8.0 - * - * @param string $locations The metaboxes locations to render. */ -function the_gutenberg_metaboxes( $locations = array( 'advanced', 'normal', 'side' ) ) { +function the_gutenberg_metaboxes() { global $post, $current_screen, $wp_meta_boxes; // Handle meta box state. @@ -334,28 +304,29 @@ function the_gutenberg_metaboxes( $locations = array( 'advanced', 'normal', 'sid * @param array $wp_meta_boxes Global meta box state. */ $wp_meta_boxes = apply_filters( 'filter_gutenberg_meta_boxes', $wp_meta_boxes ); + $locations = array( 'side', 'normal', 'advanced' ); // Render meta boxes. - if ( ! empty( $locations ) ) { - foreach ( $locations as $location ) { - ?> -
-