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
This commit is contained in:
Peter Wilson 2021-02-01 23:33:02 +00:00
parent 315b1c185d
commit 17ee62881a
2 changed files with 59 additions and 1 deletions

View File

@ -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 ); 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. * Retrieves an array of the latest posts, or posts matching the given criteria.
* *

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @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. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.