Make WordPress Core

Opened 5 years ago

Closed 4 years ago

Last modified 4 years ago

#49380 closed enhancement (fixed)

Introduce `is_post_status_viewable()` and `is_post_viewable()` functions.

Reported by: peterwilsoncc's profile peterwilsoncc Owned by: peterwilsoncc's profile peterwilsoncc
Milestone: 5.7 Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: has-patch has-unit-tests has-dev-note
Focuses: Cc:

Description (last modified by SergeyBiryukov)

WordPress currently has the function is_post_type_viewable() to determine if a post type is visible to anonymous users via the publicly_queryable setting when registering the post type.

To enable easily determining when a post should be publicly viewable, I propose the following functions be introduced.

is_post_status_viewable( array|string $post_status ) : bool

Determines if post status is publicly viewable based on public/protected/publicly_queryable settings when registered.

is_post_viewable( int|WP_Post $post = null ) : bool

Determines if a post is viewable based on the outcome of is_post_type_viewable() and is_post_status_viewable().

Defaults to current post in loop, ie result of get_post().

Attachments (1)

49380.diff (10.2 KB) - added by peterwilsoncc 4 years ago.

Download all attachments as: .zip

Change History (19)

#2 @SergeyBiryukov
5 years ago

  • Description modified (diff)

whyisjake commented on PR #217:


5 years ago
#3

Words.

#4 @whyisjake
5 years ago

  • Keywords has-unit-tests has-patch added; needs-patch needs-unit-tests removed
  • Milestone changed from Awaiting Review to 5.5
  • Owner set to whyisjake
  • Status changed from new to accepted

I like the direction here!

#5 @SergeyBiryukov
5 years ago

The PR looks good to me.

#6 @whyisjake
5 years ago

  • Keywords commit added

#7 @peterwilsoncc
4 years ago

Need to review this further, as "viewable" actually refers to publicly queryable, as opposed to public, I just need to double check is_post_viewable() makes sense.

I think is_*_public() functions would also make sense but that can be another ticket for another day.

#8 @peterwilsoncc
4 years ago

  • Keywords close added; has-unit-tests has-patch commit removed
  • Milestone changed from 5.5 to Future Release
  • Owner changed from whyisjake to peterwilsoncc
  • Status changed from accepted to assigned

Reviewing WP::parse_query, it doesn't look like the post status object's publicly_queryable property is used anywhere.

Removing from milestone and the commit keyword as a result. I'll close it once I've got clarification.

#9 @peterwilsoncc
4 years ago

  • Keywords close removed
  • Milestone Future Release deleted

@SergeyBiryukov @whyisjake

Closing this off as post_status is not publicly queryable (it's in WP::$private_query_vars rather than $public_query_vars).

This can probably be dealt with if Post Statuses move beyond the simple standard class.

#10 @peterwilsoncc
4 years ago

  • Resolution set to wontfix
  • Status changed from assigned to closed

#11 @peterwilsoncc
4 years ago

  • Milestone set to 5.7
  • Resolution wontfix deleted
  • Status changed from closed to reopened

I realised this was necessary while working on #5272, reopening.

This ticket was mentioned in Slack in #core by hellofromtonya. View the logs.


4 years ago

This ticket was mentioned in Slack in #core by peterwilsoncc. View the logs.


4 years ago

This ticket was mentioned in PR #971 on WordPress/wordpress-develop by peterwilsoncc.


4 years ago
#14

  • Keywords has-patch has-unit-tests added

@peterwilsoncc
4 years ago

#15 @peterwilsoncc
4 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 50130:

Posts, Post Types: Additional functions to check if a post is publicly viewable.

Introduces is_post_status_viewable() as a sibling to is_post_type_viewable(). Internal and protected statuses are never considered viewable. For built in posts statuses the public attribute is checked, for custom statuses the publicly_queryable attribute is checked.

Introduces is_post_publicly_viewable() for determining if an individual post can be viewed by logged out users. A post is considered viewable if both is_post_status_viewable() and is_post_type_viewable() return true for the post's attributes.

Additionally modifies is_post_type_viewable() to return false if an unregistered post type is passed to the function to avoid attempting to access properties on a non-object.

Props peterwilsoncc, SergeyBiryukov, whyisjake, TimothyBlynJacobs.
Fixes #49380.

#16 @hellofromTonya
4 years ago

  • Keywords needs-dev-note added

Needs dev note for the new functions.

#18 @Cybr
4 years ago

Since taxonomies behave much like posts and have a similarly named function, is_taxonomy_viewable(), will those receive changes akin to this ticket, or has this ticket/patch left an accidental discrepancy?

Edit: I reckon it a bit too late that terms don't have statuses. Ignore me :)

Last edited 4 years ago by Cybr (previous) (diff)
Note: See TracTickets for help on using tickets.