Posts/Post Types: Introduce 'is_post_status_viewable ' filter in `is_post_status_viewable()`.

Introduces a new filter 'is_post_status_viewable' which allows overriding the check.

The function's return signature is a boolean type. This commit ensures
the return signature remains unchanged by requirinng a strict boolean
type of the returned filtered value.

Why?
* To maintain this signature and backwards-compatibility.
* To future prepare for PHP 8.1 and beyond.

An explanation is included in the filter's DocBlock.

Follow-up to [50130].

Props audrasjb, hellofromTonya, peterwilsoncc.
Fixes #54375.
Built from https://develop.svn.wordpress.org/trunk@52043


git-svn-id: http://core.svn.wordpress.org/trunk@51635 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
hellofromTonya 2021-11-08 15:21:59 +00:00
parent ebca9841e8
commit cc39667fc1
2 changed files with 19 additions and 2 deletions

View File

@ -2157,6 +2157,7 @@ function is_post_type_viewable( $post_type ) {
* For all others, the 'publicly_queryable' value will be used. * For all others, the 'publicly_queryable' value will be used.
* *
* @since 5.7.0 * @since 5.7.0
* @since 5.9.0 Added `is_post_status_viewable` hook to filter the result.
* *
* @param string|stdClass $post_status Post status name or object. * @param string|stdClass $post_status Post status name or object.
* @return bool Whether the post status should be considered viewable. * @return bool Whether the post status should be considered viewable.
@ -2177,7 +2178,23 @@ function is_post_status_viewable( $post_status ) {
return false; return false;
} }
return $post_status->publicly_queryable || ( $post_status->_builtin && $post_status->public ); $is_viewable = $post_status->publicly_queryable || ( $post_status->_builtin && $post_status->public );
/**
* Filters whether a post status is considered "viewable".
*
* The returned filtered value must be a boolean type to ensure
* `is_post_status_viewable()` only returns a boolean. This strictness
* is by design to maintain backwards-compatibility and guard against
* potential type errors in PHP 8.1+. Non-boolean values (even falsey
* and truthy values) will result in the function returning false.
*
* @since 5.9.0
*
* @param bool $is_viewable Whether the post status is "viewable" (strict type).
* @param stdClass $post_status Post status object.
*/
return true === apply_filters( 'is_post_status_viewable', $is_viewable, $post_status );
} }
/** /**

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.9-alpha-52042'; $wp_version = '5.9-alpha-52043';
/** /**
* 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.