Skip to content

Commit

Permalink
Posts/Post Types: Add filter to is_post_type_viewable().
Browse files Browse the repository at this point in the history
Introduces a new filter `'is_post_type_viewable'` which allows overriding the check. The expected filtered value is a boolean. As filtered values can change, including the data type, this commit includes a `is_bool()` check, thus ensuring backwards-compatibility.

Follow-up to [33666], [36402].

Props audrasjb, deepaklalwani, hellofromTonya, peterwilsoncc, powerbuoy, sergeybiryukov.
Fixes #49628.
Built from https://develop.svn.wordpress.org/trunk@52024


git-svn-id: http://core.svn.wordpress.org/trunk@51616 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information
hellofromtonya committed Nov 5, 2021
1 parent c5662ee commit 862f6e9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
20 changes: 19 additions & 1 deletion wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -2086,6 +2086,7 @@ function set_post_type( $post_id = 0, $post_type = 'post' ) {
* @since 4.4.0
* @since 4.5.0 Added the ability to pass a post type name in addition to object.
* @since 4.6.0 Converted the `$post_type` parameter to accept a `WP_Post_Type` object.
* @since 5.9.0 Added `is_post_type_viewable` hook to filter the result.
*
* @param string|WP_Post_Type $post_type Post type name or object.
* @return bool Whether the post type should be considered viewable.
Expand All @@ -2102,7 +2103,24 @@ function is_post_type_viewable( $post_type ) {
return false;
}

return $post_type->publicly_queryable || ( $post_type->_builtin && $post_type->public );
$is_viewable = $post_type->publicly_queryable || ( $post_type->_builtin && $post_type->public );

/**
* Filters whether a post type is considered "viewable".
*
* @since 5.9.0
*
* @param bool $is_viewable Whether the post type is "viewable".
* @param WP_Post_Type $post_type Post type object.
*/
$is_viewable = apply_filters( 'is_post_type_viewable', $is_viewable, $post_type );

// Make sure the filtered value is a boolean type before returning it.
if ( ! is_bool( $is_viewable ) ) {
return false;
}

return $is_viewable;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion wp-includes/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.9-alpha-52023';
$wp_version = '5.9-alpha-52024';

/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
Expand Down

0 comments on commit 862f6e9

Please sign in to comment.