[wp-trac] [WordPress Trac] #49628: Add is_post_type_viewable filter
WordPress Trac
noreply at wordpress.org
Mon Nov 8 00:10:51 UTC 2021
#49628: Add is_post_type_viewable filter
---------------------------------------------+-----------------------------
Reporter: powerbuoy | Owner: hellofromTonya
Type: feature request | Status: closed
Priority: normal | Milestone: 5.9
Component: Posts, Post Types | Version: 5.3.2
Severity: normal | Resolution: fixed
Keywords: has-patch needs-dev-note commit | Focuses: administration
---------------------------------------------+-----------------------------
Comment (by hellofromTonya):
Replying to [comment:16 Cybr]:
> 1. The documentation clearly states it should be a boolean.
> 2. The return value can be cast to a boolean, which should suffice:
`return (bool) $is_viewable;`
Type casting to boolean (and any type) is not the same nor is it as
predictable as checking that the expected data type is actually returned.
For example, if a callback in the wild returns an object or an array with
an element in it (`array( false )`), type casting returns `true`. What
happens if nothing is returned when the non-filtered state is `true`? Type
casting would set it to `false`.
PHP is going in the direction of throwing notices and later fatal errors
for type mismatches. PHP 8.1 shows us the trend where passing the wrong
data type to a native PHP function will result in a deprecation notice and
then later in PHP 9 a fatal error. If this trend continues, it's
reasonable to leap forward to where type casting non-scalars could also
follow that trend.
These types of strict checks guard Core, users, and extenders.
Yes, this does add 3 extra opcodes. Benchmarking the added type check
would yield a tiny microsecond or less as it uses inexpensive native PHP
logic.
In the future, type safe filters will come to Core (maybe in 6.0). When it
does, this code can be safely replaced with it.
What do you think @Cybr?
--
Ticket URL: <https://core.trac.wordpress.org/ticket/49628#comment:17>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list