Skip to content

Respect 'publicly_queryable' post type setting.#2135

Merged
jasonbahl merged 7 commits intowp-graphql:developfrom
kellenmace:bug/#2134-respect-publicly-queryable-post-type-setting
Nov 2, 2021
Merged

Respect 'publicly_queryable' post type setting.#2135
jasonbahl merged 7 commits intowp-graphql:developfrom
kellenmace:bug/#2134-respect-publicly-queryable-post-type-setting

Conversation

@kellenmace
Copy link
Copy Markdown
Contributor

Fixes #2134.

What does this implement/fix? Explain your changes.

Consider a post as private if 'publicly_queryable' is set to false so that only authenticated users with the required capability can access it.

Does this close any currently open issues?

Yes, #2134.

Any other comments?

Please test this to double-check that it results in the intended behavior.

I also updated the paragraph at the top of the https://www.wpgraphql.com/docs/custom-post-types/#public-vs-private-data section of the docs to reflect this change (wp-admin edit page is here: https://content.wpgraphql.com/wp-admin/post.php?post=5116&action=edit).

@kellenmace
Copy link
Copy Markdown
Contributor Author

This line of code could have alternatively been written as:
if ( 'publish' === $this->data->post_status && ! empty( $this->post_type_object->publicly_queryable ) ) {
to account for $this->post_type_object possibly being set to null.

I think doing
if ( 'publish' === $this->data->post_status && $this->post_type_object && $this->post_type_object->publicly_queryable ) {
is a bit more readable, though, so I went with that. Matter of preference, though!

* Published content is public, not private
*/
if ( 'publish' === $this->data->post_status ) {
if ( 'publish' === $this->data->post_status && $this->post_type_object && $this->post_type_object->publicly_queryable ) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@kellenmace this is interesting.

I think if public is true, we need to respect it first.

Because post types can be public => true, publicly_queryable => false, such as the page post_type: https://github.com/WordPress/WordPress/blob/8447683474ebdee773fc4fca7b3e44dea45060f4/wp-includes/post.php#L45-L68, which appears to be breaking a lot of tests right now 🤔

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@kellenmace I pushed up a change so it's now:

if ( 'publish' === $this->data->post_status && $this->post_type_object && ( true === $this->post_type_object->public || true === $this->post_type_object->publicly_queryable ) ) {
  return false;
}

This means that if either public or publicly_queryable are true, then WPGraphQL should treat the entries as public.

This would allow for "utility" post_types, such as a Slideshow post_type or an FAQ post_type, etc to be registered as public=>false, publicly_queryable=>true and the entries would not get a public url, but would be queryable in WPGraphQL, so they could still be used to compose public facing pages, even without having their own public urls.

@coveralls
Copy link
Copy Markdown

coveralls commented Nov 2, 2021

Coverage Status

Coverage remained the same at 79.371% when pulling e849fc5 on kellenmace:bug/#2134-respect-publicly-queryable-post-type-setting into 7aa82cb on wp-graphql:develop.

@qlty-cloud-legacy
Copy link
Copy Markdown

Code Climate has analyzed commit e849fc5 and detected 0 issues on this pull request.

View more on Code Climate.

@jasonbahl jasonbahl merged commit bb72448 into wp-graphql:develop Nov 2, 2021
@jasonbahl jasonbahl mentioned this pull request Nov 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Respect publicly_queryable setting for post types

3 participants