Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MetaBoxes: Saving meta boxes don't reset the comment/ping status #4865

Merged
merged 3 commits into from
Feb 13, 2018
Merged
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
2 changes: 1 addition & 1 deletion editor/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ export function initializeMetaBoxState( metaBoxes ) {
/**
* Returns an action object used to request meta box update.
*
* @return {Object} Action object.
* @return {Object} Action object.
*/
export function requestMetaBoxUpdates() {
return {
Expand Down
23 changes: 18 additions & 5 deletions editor/store/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import {
getBlocks,
getReusableBlock,
getMetaBoxes,
hasMetaBoxes,
POST_UPDATE_TRANSACTION_ID,
} from './selectors';
import { getMetaBoxContainer } from '../utils/meta-boxes';
Expand Down Expand Up @@ -151,7 +152,9 @@ export default {
}

// Update dirty meta boxes.
dispatch( requestMetaBoxUpdates() );
if ( hasMetaBoxes( store.getState() ) ) {
dispatch( requestMetaBoxUpdates() );
}

if ( get( window.history.state, 'id' ) !== post.id ) {
window.history.replaceState(
Expand Down Expand Up @@ -490,19 +493,29 @@ export default {
store.dispatch( setMetaBoxSavedData( dataPerLocation ) );
},
REQUEST_META_BOX_UPDATES( action, store ) {
const dataPerLocation = reduce( getMetaBoxes( store.getState() ), ( memo, metabox, location ) => {
const state = store.getState();
const dataPerLocation = reduce( getMetaBoxes( state ), ( memo, metabox, location ) => {
if ( metabox.isActive ) {
memo[ location ] = jQuery( getMetaBoxContainer( location ) ).serialize();
}
return memo;
}, {} );
store.dispatch( setMetaBoxSavedData( dataPerLocation ) );

// Additional data needed for backwards compatibility.
// If we do not provide this data the post will be overriden with the default values.
const post = getCurrentPost( state );
const additionalData = [
post.comment_status && `comment_status=${ post.comment_status }`,
post.ping_status && `ping_status=${ post.ping_status }`,
].filter( Boolean );

// 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 formData = values( dataPerLocation )
.concat( jQuery( '.metabox-base-form' ).serialize() )
.concat( additionalData )
.join( '&' );
const fetchOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
Expand Down
18 changes: 13 additions & 5 deletions editor/store/test/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,19 +323,23 @@ describe( 'effects', () => {

it( 'should dispatch meta box updates on success for dirty meta boxes', () => {
const dispatch = jest.fn();
const store = { getState: () => {}, dispatch };
const store = { getState: () => ( {
metaBoxes: { side: { isActive: true } },
} ), dispatch };

const post = getDraftPost();

handler( { post: post, previousPost: post }, store );

expect( dispatch ).toHaveBeenCalledTimes( 1 );
expect( dispatch ).toHaveBeenCalledWith( requestMetaBoxUpdates() );
expect( dispatch ).toHaveBeenCalledWith( requestMetaBoxUpdates( post ) );
} );

it( 'should dispatch notices when publishing or scheduling a post', () => {
const dispatch = jest.fn();
const store = { getState: () => {}, dispatch };
const store = { getState: () => ( {
metaBoxes: { side: { isActive: true } },
} ), dispatch };

const previousPost = getDraftPost();
const post = getPublishedPost();
Expand All @@ -357,7 +361,9 @@ describe( 'effects', () => {

it( 'should dispatch notices when reverting a published post to a draft', () => {
const dispatch = jest.fn();
const store = { getState: () => {}, dispatch };
const store = { getState: () => ( {
metaBoxes: { side: { isActive: true } },
} ), dispatch };

const previousPost = getPublishedPost();
const post = getDraftPost();
Expand All @@ -383,7 +389,9 @@ describe( 'effects', () => {

it( 'should dispatch notices when just updating a published post again', () => {
const dispatch = jest.fn();
const store = { getState: () => {}, dispatch };
const store = { getState: () => ( {
metaBoxes: { side: { isActive: true } },
} ), dispatch };

const previousPost = getPublishedPost();
const post = getPublishedPost();
Expand Down