Skip to content

JSON API: Update allowed_post_types#5487

Merged
lezama merged 5 commits intomasterfrom
update/json-api-allowed-post-types
Jan 6, 2017
Merged

JSON API: Update allowed_post_types#5487
lezama merged 5 commits intomasterfrom
update/json-api-allowed-post-types

Conversation

@lezama
Copy link
Copy Markdown
Contributor

@lezama lezama commented Nov 2, 2016

cc @aduth @jeherve @mtias

This PR aims to accomplish #5487 (comment)


if ( $post_type_object = get_post_type_object( $post_type ) ) {
if ( ! empty( $post_type_object->show_in_rest ) ) {
return true;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This should return $post_type_object->show_in_rest in case it's explicitly set to false

return true;
}
if ( ! empty( $post_type_object->publicly_queryable ) ) {
return true;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This should return $post_type_object->publicly_queryable in case it's explicitly set to false

}

// check for allowed types
if ( in_array( $post_type, $this->_get_whitelisted_post_types() ) )
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we run whitelist check first before testing the post type object?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

good call, I am going to change the order

if ( ! empty( $post_type_object->show_in_rest ) ) {
return $post_type_object->show_in_rest;
}
if ( ! empty( $post_type_object->publicly_queryable ) ) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should double check that the post type object always includes the publicly_queryable flag, since the effect we're after is to fall back to the public value if publicly_queryable is not explicitly set.

If no value is specified for exclude_from_search, publicly_queryable, show_in_nav_menus, or show_ui, they inherit their values from public.

https://codex.wordpress.org/Function_Reference/register_post_type

@jeherve jeherve added the Enhancement Changes to an existing feature — removing, adding, or changing parts of it label Nov 2, 2016
@jeherve
Copy link
Copy Markdown
Member

jeherve commented Nov 9, 2016

Fixes #5102

@enejb
Copy link
Copy Markdown
Member

enejb commented Jan 4, 2017

@lezama How do I test this PR?

@ebinnion
Copy link
Copy Markdown
Contributor

ebinnion commented Jan 4, 2017

@lezama Would you mind updating the PR description so we have a better idea of what exactly this PR does? From what I can tell, we're wanting to only sync public post types and ones that are shown in the API or UI. But, perhaps there are some other things I missed?

@aduth
Copy link
Copy Markdown
Member

aduth commented Jan 4, 2017

The expected changes from this pull request are:

  • The GET /sites/%s/post-types endpoint now returns two new properties for each post type: show_ui and publicly_queryable
  • Post types which have registered including the new (as of WordPress 4.7) show_in_rest flag will use that flag in determining whether a post type is allowed to be administered from the REST API
  • Post types which are otherwise publicly queryable (publicly_queryable in register_post_type, falling back to public) are also allowed to be administered from the REST API

By "administered from the REST API", I mean listed, created, deleted, etc.

The last of these bullet points is rather controversial as I see it, but is also most enabling for UIs to build on the REST API. It now opts in many more post types without explicitly being whitelisted merely by being public (previously requiring filter). public is false by default, so it would take a conscious decision on a plugin author's part.

'map_meta_cap' => 'map_meta_cap',
'cap' => 'capabilities',
'hierarchical' => 'hierarchical',
'show_in_ui' => 'show_in_ui',
Copy link
Copy Markdown
Contributor

@ebinnion ebinnion Jan 4, 2017

Choose a reason for hiding this comment

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

I noticed the show_in_ui value is coming back from the API as null. Should we make that return as a boolean?

screen shot 2017-01-04 at 5 12 16 pm

@ebinnion
Copy link
Copy Markdown
Contributor

ebinnion commented Jan 4, 2017

I'm getting this notice when testing:

[04-Jan-2017 23:28:31 UTC] PHP Notice:  Undefined property: WP_Post_Type::$show_in_ui in .../wp-content/plugins/jetpack/json-endpoints/class.wpcom-json-api-list-post-types-endpoint.php on line 60

@aduth
Copy link
Copy Markdown
Member

aduth commented Jan 4, 2017

@ebinnion
Copy link
Copy Markdown
Contributor

ebinnion commented Jan 4, 2017

Here are my results of testing with a CPT and a few variations:

With public set, this did show up in the API as expected.

function wporg_custom_post_type()
{
	register_post_type('wporg_product',
		[
			'labels'      => [
				'name'          => __('Products'),
				'singular_name' => __('Product'),
			],
			'public' => true,
			'has_archive' => true,
		]
	);
}
add_action('init', 'wporg_custom_post_type');

When public is false and show_in_rest is true, the new CPT doesn't show up.

function wporg_custom_post_type()
{
	register_post_type('wporg_product',
		[
			'labels'      => [
				'name'          => __('Products'),
				'singular_name' => __('Product'),
			],
			'has_archive' => true,
			'show_in_rest' => true,
		]
	);
}
add_action('init', 'wporg_custom_post_type');

When public is false and publicly_queryable is true, the new CPT doesn't get returned in the API request.

function wporg_custom_post_type()
{
	register_post_type('wporg_product',
		[
			'labels'      => [
				'name'          => __('Products'),
				'singular_name' => __('Product'),
			],
			'has_archive' => true,
			'publicly_queryable' => true,
		]
	);
}
add_action('init', 'wporg_custom_post_type');

@ebinnion ebinnion added [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. and removed [Status] Needs Review This PR is ready for review. labels Jan 4, 2017
@lezama
Copy link
Copy Markdown
Contributor Author

lezama commented Jan 5, 2017

Thanks for testing @ebinnion! just addressed your feedback.

@ebinnion
Copy link
Copy Markdown
Contributor

ebinnion commented Jan 5, 2017

With the latest changes, this tests well for me now. I used the code examples from my last comment, and the product CPT showed up when the CPT had public, publicly_queryable, or show_in_rest set to true, as expected. 👍

I did notice that revisions were showing, even though they aren't publicly_queryable. But, it looks like we whitelist that CPT to be in the API here, so all looks good.

🚢

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Very minor, but maybe we can add space between the key and value here so that => aligns with the ones above it.

@ebinnion ebinnion added [Status] Ready to Merge Go ahead, you can push that green button! and removed [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. labels Jan 5, 2017
@lezama
Copy link
Copy Markdown
Contributor Author

lezama commented Jan 5, 2017

@ebinnion, @aduth I am worried about the refactoring to is_post_type_allowed changing the behaviour of other post related endpoints. What do you think?

@ebinnion
Copy link
Copy Markdown
Contributor

ebinnion commented Jan 5, 2017

I did a quick pass through the API where I saw is_post_type_allowed() and nothign stuck out to me.

@ebinnion
Copy link
Copy Markdown
Contributor

ebinnion commented Jan 5, 2017

We should probably rebase this against master before merging since the PR was started in early November.

Update: I went ahead and rebased.

@ebinnion ebinnion force-pushed the update/json-api-allowed-post-types branch from 41b291f to 1f7eb99 Compare January 5, 2017 22:19
@lezama lezama merged commit 0e86f00 into master Jan 6, 2017
@lezama lezama deleted the update/json-api-allowed-post-types branch January 6, 2017 15:34
@dereksmart dereksmart restored the update/json-api-allowed-post-types branch January 9, 2017 15:52
@dereksmart
Copy link
Copy Markdown
Contributor

merged to 4.5 fc6aa76

@dereksmart dereksmart deleted the update/json-api-allowed-post-types branch January 9, 2017 15:53
jeherve added a commit that referenced this pull request Jan 9, 2017
dereksmart pushed a commit that referenced this pull request Jan 17, 2017
CHangelog: add #5457

Changelog: add #5487

Changelog: add #5708

Changelog: add #5879

Changelog: add #5932

Changelog: add #5963

Changelog: add #5968

Changelog: add #5996

Changelog: add #5998

Changelog: add #5999

Changelog: add #6012

Changelog: add #6013

Changelog: add #6014

Changelog: add #6015

Changelog: add #6023

Changelog: add #6024

Changelog: add #6030

Changelog: add #5465

CHangelog: add #6063

Changelog: add #6025

Changelog: add #5974

Changelog: add #6059

Changelog: add #6046

Changelog: add #5418

Changelog: move things around and add missing information.

Changelog: add #5565

Changelog: add #6087

Changelog: add #6095
dereksmart pushed a commit that referenced this pull request Jan 17, 2017
Changelog: add #5867

Changelog: add #5874

Changelog: add #5905

Changelog: add #5906

Changelog: add #5931

Changelog: add #5933

Changelog: add #5934

Bring over 4.4.2 changelog from branch-4.4

@see 18012a3

Changelog: add #5976, #5978, #5983

Changelog: add #5917

Changelog: add #5832

Changelog: add 4.4.2 release post link.

CHangelog: add #5457

Changelog: add #5487

Changelog: add #5708

Changelog: add #5879

Changelog: add #5932

Changelog: add #5963

Changelog: add #5968

Changelog: add #5996

Changelog: add #5998

Changelog: add #5999

Changelog: add #6012

Changelog: add #6013

Changelog: add #6014

Changelog: add #6015

Changelog: add #6023

Changelog: add #6024

Changelog: add #6030

Changelog: add #5465

CHangelog: add #6063

Changelog: add #6025

Changelog: add #5974

Changelog: add #6059

Changelog: add #6046

Changelog: add #5418

Changelog: move things around and add missing information.

Changelog: add #5565

Changelog: add #6087

Changelog: add #6095

Readme: add @tyxla to the list of contributors.

Improved changelog for your readability and enjoyment

updated the release date

finalizing the changelog with a few more edits
zinigor added a commit that referenced this pull request Jun 29, 2017
…d method.

This commits D5712. This is part of the synchronization of code between Jetpack and WordPress.com, see more here:
#5487

Merges r156753-wpcom.
dereksmart pushed a commit that referenced this pull request Mar 29, 2018
…d method.

This commits D5712. This is part of the synchronization of code between Jetpack and WordPress.com, see more here:
#5487

Merges r156753-wpcom.
@kraftbj kraftbj removed the [Status] Ready to Merge Go ahead, you can push that green button! label Oct 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Feature] WPCOM API Touches WP.com Files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants