From 17ee62881a78fbcdf23bd90b95c91c6e5107841a Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Mon, 1 Feb 2021 23:33:02 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@50130 git-svn-id: http://core.svn.wordpress.org/trunk@49809 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/post.php | 58 +++++++++++++++++++++++++++++++++++++++++ wp-includes/version.php | 2 +- 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/wp-includes/post.php b/wp-includes/post.php index d7f7b25652..e1997e24b8 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -2019,9 +2019,67 @@ function is_post_type_viewable( $post_type ) { } } + if ( ! is_object( $post_type ) ) { + return false; + } + return $post_type->publicly_queryable || ( $post_type->_builtin && $post_type->public ); } +/** + * Determine whether a post status is considered "viewable". + * + * For built-in post statuses such as publish and private, the 'public' value will be evaluted. + * For all others, the 'publicly_queryable' value will be used. + * + * @since 5.7.0 + * + * @param string|stdClass $post_status Post status name or object. + * @return bool Whether the post status should be considered viewable. + */ +function is_post_status_viewable( $post_status ) { + if ( is_scalar( $post_status ) ) { + $post_status = get_post_status_object( $post_status ); + if ( ! $post_status ) { + return false; + } + } + + if ( + ! is_object( $post_status ) || + $post_status->internal || + $post_status->protected + ) { + return false; + } + + return $post_status->publicly_queryable || ( $post_status->_builtin && $post_status->public ); +} + +/** + * Determine whether a post is publicly viewable. + * + * Posts are considered publicly viewable if both the post status and post type + * are viewable. + * + * @since 5.7.0 + * + * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post. + * @return bool Whether the post is publicly viewable. + */ +function is_post_publicly_viewable( $post = null ) { + $post = get_post( $post ); + + if ( ! $post ) { + return false; + } + + $post_type = get_post_type( $post ); + $post_status = get_post_status( $post ); + + return is_post_type_viewable( $post_type ) && is_post_status_viewable( $post_status ); +} + /** * Retrieves an array of the latest posts, or posts matching the given criteria. * diff --git a/wp-includes/version.php b/wp-includes/version.php index f3652f0670..88b56f0942 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.7-alpha-50129'; +$wp_version = '5.7-alpha-50130'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.