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

Add check for author in post data before submission #15375

Merged

Conversation

Projects
None yet
3 participants
@alexsanford
Copy link
Contributor

commented May 1, 2019

Description

Previously, the submission code on the Post Edit page assumed that the Post data from the REST API contained a post_author field. But this is not the case for CPT's that do not include author in their supports array. This PR only includes the author in the submission if the field exists.

A real-world example of this causing a bug is here: Automattic/sensei#2525

How has this been tested?

To reproduce the bug on master, add the following code snippet to a site:

add_action( 'init', function() {
	$args = array(
		'labels'       => [ 'name' => 'Books', 'singular_name' => 'Book' ],
		'public'       => true,
		'supports'     => [ 'title', 'editor', 'excerpt', 'thumbnail', 'revisions' ],
		'show_in_rest' => true,
	);
	
	register_post_type( 'book', $args );
} );

function book_metabox_content() {
	echo 'This is a metabox for Books';
}

add_action( 'add_meta_boxes', function() {
	add_meta_box( 'book-meta', 'Book Meta', 'book_metabox_content', 'book', 'side', 'default' );
} );

Then log in as a user with the author role and try to create/update a Book, while watching the Network tab of the developer console. You should see a 500 error in the request to update the metaboxes, and the progress indicator on the page will show indefinitely.

Switching to this branch should cause the request to succeed.

More details

Before this PR, in the case described above, the JS code would send a value of undefined in the post_author field when submitting the metabox data. In WordPress, this would be converted to 0 here and then cause an error condition here.

With the change in this PR, the condition here is run instead, which fixes the bug.

Types of changes

Bug fix

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
Add check for author in post data before submission
Previously, the submission code on the Post Edit page assumed that the
Post data from the REST API contained an author field. But this is not
the case for CPT's that do not include `author` in their `supports`
array. This commit only includes the author in the submission if it
exists.
@youknowriad
Copy link
Contributor

left a comment

LGTM

@youknowriad youknowriad merged commit 5c87bce into WordPress:master May 7, 2019

1 check passed

Travis CI - Pull Request Build Passed
Details

@alexsanford alexsanford deleted the alexsanford:fix/cpt-edit-without-author branch May 7, 2019

@youknowriad youknowriad added this to the 5.7 (Gutenberg) milestone May 10, 2019

alexsanford added a commit to Automattic/sensei that referenced this pull request May 16, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.