Make WordPress Core


Ignore:
Timestamp:
02/01/2021 11:31:54 PM (4 years ago)
Author:
peterwilsoncc
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r50120 r50130  
    20202020    }
    20212021
     2022    if ( ! is_object( $post_type ) ) {
     2023        return false;
     2024    }
     2025
    20222026    return $post_type->publicly_queryable || ( $post_type->_builtin && $post_type->public );
     2027}
     2028
     2029/**
     2030 * Determine whether a post status is considered "viewable".
     2031 *
     2032 * For built-in post statuses such as publish and private, the 'public' value will be evaluted.
     2033 * For all others, the 'publicly_queryable' value will be used.
     2034 *
     2035 * @since 5.7.0
     2036 *
     2037 * @param string|stdClass $post_status Post status name or object.
     2038 * @return bool Whether the post status should be considered viewable.
     2039 */
     2040function is_post_status_viewable( $post_status ) {
     2041    if ( is_scalar( $post_status ) ) {
     2042        $post_status = get_post_status_object( $post_status );
     2043        if ( ! $post_status ) {
     2044            return false;
     2045        }
     2046    }
     2047
     2048    if (
     2049        ! is_object( $post_status ) ||
     2050        $post_status->internal ||
     2051        $post_status->protected
     2052    ) {
     2053        return false;
     2054    }
     2055
     2056    return $post_status->publicly_queryable || ( $post_status->_builtin && $post_status->public );
     2057}
     2058
     2059/**
     2060 * Determine whether a post is publicly viewable.
     2061 *
     2062 * Posts are considered publicly viewable if both the post status and post type
     2063 * are viewable.
     2064 *
     2065 * @since 5.7.0
     2066 *
     2067 * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post.
     2068 * @return bool Whether the post is publicly viewable.
     2069 */
     2070function is_post_publicly_viewable( $post = null ) {
     2071    $post = get_post( $post );
     2072
     2073    if ( ! $post ) {
     2074        return false;
     2075    }
     2076
     2077    $post_type   = get_post_type( $post );
     2078    $post_status = get_post_status( $post );
     2079
     2080    return is_post_type_viewable( $post_type ) && is_post_status_viewable( $post_status );
    20232081}
    20242082
Note: See TracChangeset for help on using the changeset viewer.