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

Conversation

youknowriad
Copy link
Contributor

closes #4843 and closes #4663

This PR fixes two related issues:

  • The meta boxes save was being triggered even if we didn't have any meta box enabled
  • The meta boxes save was resetting the ping/comments status due to legacy code on the backend. I ensured the post status and ping status are passed to the meta box request to avoid this.

Testing instructions

  • Add some metaboxes
  • Save your post
  • Refresh the page
  • The ping and comment status should stay enabled unless you disable them explicitely.

@youknowriad youknowriad force-pushed the fix/saving-metabox-reset-comment-status branch 2 times, most recently from 74bbf8d to 4c72798 Compare February 7, 2018 13:05
@youknowriad youknowriad requested a review from a team February 7, 2018 13:09
@youknowriad youknowriad force-pushed the fix/saving-metabox-reset-comment-status branch from 4c72798 to 645f67d Compare February 7, 2018 13:17
@youknowriad
Copy link
Contributor Author

youknowriad commented Feb 8, 2018

⌛️ I don't blame you 🙂 no one wants to review meta boxes.

@@ -498,11 +501,18 @@ export default {
}, {} );
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 additionData = []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can express it also as:

const additionalData = [
    post[ 'comment_status' ] && `comment_status=${ post[ 'comment_status' ] }`,
    post[ 'ping_status' ] && `ping_status=${ post[ 'ping_status' ] }`,
].filter( Boolean );

I learned this trick with filter recently :)

Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good. I haven't tested yet.

@@ -489,7 +492,7 @@ export default {
}, {} );
store.dispatch( setMetaBoxSavedData( dataPerLocation ) );
},
REQUEST_META_BOX_UPDATES( action, store ) {
REQUEST_META_BOX_UPDATES( { post }, store ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does post need to come through the action, or could we assume that the value in store.getState() would be up to date by now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's probably the same, I'll remove the post from the action

Copy link
Member

@gziolo gziolo Feb 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it? I mean, it might be the same. It all depends how all those dispatch actions are processed. This is quite tricky because you dispatch action in the middle of another dispatch. At the moment when you trigger requestMetaBoxUpdates action, the payload of requestPostUpdateSuccess is still processed, so there might be a different value in the store at that time. I think actions are dispatched asynchronously which means it should all end up as expected.

@@ -498,11 +501,18 @@ export default {
}, {} );
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 additionData = []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: "addition" -> "additional"

const formData = values( dataPerLocation )
.concat( jQuery( '.metabox-base-form' ).serialize() )
.concat( additionData )
.join( '&' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside: Would it be easier to construct this with FormData instead?

Unfortunately not possible to pass an object to its constructor, needs to iterate with FormData#append.†

But it can be passed directly into fetch's body property.

Above jQuery.serialize would need to be revised to jQuery.serializeArray.

I think FormData manages encoding too, unlike the current code here where we concatenate post[ 'comment_status' ] into the form without first encodeURIComponent (not likely a real-world issue).

† There are helpers like object-to-formdata

Copy link
Member

@gziolo gziolo Feb 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m afraid FormData doesn’t work properly with our codebase. We polyfill fetch with very limited support for FormData. At least I had a few issues with it a few weeks back. I don’t remember the details though 😃

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note the gaps for Edge and Safari: https://developer.mozilla.org/en-US/docs/Web/API/FormData.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried something similar the problem is that serializeArray doesn't return a nested array when we have array parameters instead it returns something like { "fields[foo]": "bar" } which is not the correct object we want.

// Additional data needed for backwards compatibility.
// If we do not provide this data the post will be overriden with the default values.
const additionData = []
.concat( post[ 'comment_status' ] ? [ `comment_status=${ post[ 'comment_status' ] }` ] : [] )
Copy link
Member

@aduth aduth Feb 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary square brackets here on property access. Surprised this isn't a lint error.

@youknowriad
Copy link
Contributor Author

I'm merging this one as it's a necessary fix for the release.

@youknowriad youknowriad merged commit 4d835c0 into master Feb 13, 2018
@youknowriad youknowriad deleted the fix/saving-metabox-reset-comment-status branch February 13, 2018 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants