WordPress.org

Make WordPress Core

Opened 3 months ago

Closed 2 months ago

Last modified 6 weeks ago

#54375 closed enhancement (fixed)

Add is_post_status_viewable filter

Reported by: peterwilsoncc Owned by: hellofromTonya
Milestone: 5.9 Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: needs-patch commit has-dev-note
Focuses: Cc:

Description

Related to #49628.

Add a filter to the is_post_status_viewable() function to allow theme and plugin developers to override the default value.

In some circumstances a developer may require the checks use different conditions to the default.

Attachments (4)

54375.diff (1.3 KB) - added by audrasjb 3 months ago.
Posts, Post Types: Introduce is_post_status_viewable hook to filter the result of the is_post_status_viewable() function.
54375.1.diff (1.1 KB) - added by hellofromTonya 2 months ago.
Simplifies the strict boolean type check to conserve memory and processing.
54375.2.diff (1.4 KB) - added by peterwilsoncc 2 months ago.
54375.3.diff (1.5 KB) - added by hellofromTonya 2 months ago.
Explanation improvement in 54375.4.diff.

Download all attachments as: .zip

Change History (12)

@audrasjb
3 months ago

Posts, Post Types: Introduce is_post_status_viewable hook to filter the result of the is_post_status_viewable() function.

@hellofromTonya
2 months ago

Simplifies the strict boolean type check to conserve memory and processing.

#1 @hellofromTonya
2 months ago

  • Keywords commit added

#2 @hellofromTonya
2 months ago

  • Owner set to hellofromTonya
  • Status changed from new to reviewing

#3 @peterwilsoncc
2 months ago

Functional changes in 54375.1.diff look good to me.

In 54375.2.diff I've added an explanation that the filter must return true, for the same reasons as discussed on #49628.

@hellofromTonya
2 months ago

Explanation improvement in 54375.4.diff.

#4 @hellofromTonya
2 months ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 52043:

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.

#5 @SergeyBiryukov
2 months ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

It seems like this is a more common pattern in core to ensure a boolean return type from a filter:

return (bool) apply_filters( ... );

It's used in at least five other instances and appears to achieve the same goal. Is there a reason not follow that pattern here? Personally, I find it much easier to navigate the code later when similar things are done in a consistent way :)

The same applies to [52045].

#6 follow-up: @hellofromTonya
2 months ago

@SergeyBiryukov See the explanation here as to why type casting is unpredictable and why a strict boolean check is used instead. Please note, strict boolean checks are also in Core. This types of checks are more future proof, predictable, and stable than type casting.

#7 in reply to: ↑ 6 @SergeyBiryukov
2 months ago

  • Resolution set to fixed
  • Status changed from reopened to closed

Replying to hellofromTonya:

See the explanation here as to why type casting is unpredictable and why a strict boolean check is used instead.

Thank you for pointing me to that discussion! Knowing the context helps a lot, I appreciate the explanation.

Re-closing this to keep the discussion in one place. Will comment on the other ticket once I have an informed opinion.

#8 @audrasjb
6 weeks ago

  • Keywords has-dev-note added; needs-dev-note removed
Note: See TracTickets for help on using tickets.