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

Custom Post type: If param 'publicly_queryable' = 'false' the editor shows "Updating failed" notice. #12677

Closed
heldervilela opened this issue Dec 7, 2018 · 10 comments · Fixed by #12800
Assignees
Labels
[Feature] Saving Related to saving functionality REST API Interaction Related to REST API [Type] Bug An existing feature does not function as intended

Comments

@heldervilela
Copy link

If the custom post type is 'publicly_queryable' => false the editor show the notice "Updating failed" when it tries to save the draft.

notice

Because of this error:

error

Let me know if more info is needed.

Additional context

  • WordPress 5.0.
@danielbachhuber
Copy link
Member

Thanks for the report, @heldervilela.

In your Developer Tools, can you take a screenshot of and share the API request / response itself? I'm curious whether the JSON is properly formed, and what sort of HTTP response you're getting from WordPress.

@danielbachhuber danielbachhuber added [Status] Needs More Info Follow-up required in order to be actionable. REST API Interaction Related to REST API labels Dec 7, 2018
@heldervilela
Copy link
Author

@danielbachhuber I'm glad to help

From what I saw when comparing with another CPT the answer is the same.

Request:
request

Response:
response

@danielbachhuber danielbachhuber added [Type] Bug An existing feature does not function as intended [Feature] Saving Related to saving functionality and removed [Status] Needs More Info Follow-up required in order to be actionable. labels Dec 7, 2018
@danielbachhuber danielbachhuber added this to the WordPress 5.0.1 milestone Dec 7, 2018
@danielbachhuber
Copy link
Member

Oh, interesting. And there's no preview_link value because the post isn't publicly_queryable.

I think the Gutenberg JavaScript shouldn't be trying to access the preview_link in this case. I've flagged this as a bug for WordPress 5.0.1.

@aduth
Copy link
Member

aduth commented Dec 8, 2018

Would you be able to share the full code of your custom post type registration?

I've been unable to reproduce the save failure / error notice using the following code:

<?php

/**
 * Plugin Name: Demo CPT
 */
add_action( 'init', function() {
	register_post_type( 'book', [
		'label' => 'Book',
		'show_in_rest' => true,
		'public' => true,
		'publicly_queryable' => false,
		'supports' => [ 'title', 'editor' ],
	] );
} );

@heldervilela
Copy link
Author

You have to add revisions to supports.

@danielbachhuber
Copy link
Member

I can reproduce the issue with WordPress 5.0 and Gutenberg the plugin at b7e343d

add_action( 'init', function() {
	register_post_type( 'book', [
		'label' => 'Book',
		'show_in_rest' => true,
		'public' => true,
		'publicly_queryable' => false,
		'supports' => [ 'title', 'editor', 'revisions' ],
	] );
} );

Here's a GIF of my experience:

autosaveprivate

And here's the final state:

image

@danielbachhuber
Copy link
Member

Ah, here's the earlier report of this too: https://core.trac.wordpress.org/ticket/45404

@aduth
Copy link
Member

aduth commented Dec 11, 2018

The error happens here, where the passed url is undefined, thus indexOf will throw:

const queryStringIndex = url.indexOf( '?' );

This happens with the fallback case of the editor's handling of save completion, where it attempts to set a query parameter to the post url:

return action.post.preview_link || addQueryArgs( action.post.link, { preview: true } );

The problems are:

  • post is a revision, so it doesn't include link
  • preview_link is set on post, but it is an empty string (the || triggers the RHS logic because the falsiness of empty string)

The reason it is assigned as an empty string can be tracked to the preview_link assignment in the autosaves controller:

https://github.com/WordPress/wordpress-develop/blob/5.0/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php

The value is null because when a post type is publicly_queryable => false, it is not "viewable":

https://github.com/WordPress/wordpress-develop/blob/5.0/src/wp-includes/link-template.php#L1235-L1243
https://github.com/WordPress/wordpress-develop/blob/5.0/src/wp-includes/post.php#L1781

There's a few action items here:

  • addQueryArgs should never throw. Based on the equivalent behavior from add_query_arg, I suspect it should return the string with only query arguments.
  • The previewLink reducer should not return a new value if neither preview_link nor link are usable
    • Verify no regressions in preview behavior if the preview link can't be assigned
  • Consider: Should preview_link be assigned even if 'publicly_queryable' => false ? I'm unsure of the repercussions of changing get_preview_post_link

@aduth
Copy link
Member

aduth commented Dec 11, 2018

Verify no regressions in preview behavior if the preview link can't be assigned

We already handle this: The preview button will not be shown if the post type is not viewable, following the same logic noted previously considering publicly_queryable.

@aduth
Copy link
Member

aduth commented Dec 11, 2018

  • addQueryArgs should never throw. Based on the equivalent behavior from add_query_arg, I suspect it should return the string with only query arguments.

See: #12803

  • The previewLink reducer should not return a new value if neither preview_link nor link are usable

See: #12800

@aduth aduth self-assigned this Dec 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Saving Related to saving functionality REST API Interaction Related to REST API [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants