Make WordPress Core

Opened 5 years ago

Closed 21 months ago

Last modified 21 months ago

#45329 closed defect (bug) (fixed)

get_post_permalink tries is_wp_error on get_post which it will never be

Reported by: renegeuze's profile renegeuze Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 6.1 Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: needs-dev-note
Focuses: Cc:

Description

get_post_permalink incorrectly calls is_wp_error on the result of get_post($id). Instead it should check for !($post instanceof \WP_Post) or just $post === false, since at this location it won't ever be an array either.

Documentation is wrong too - but I assume that's generated from docBlock.

To reproduce: get_post_permalink('whatever-argument-that-does-not-resolve-an-existing-post');
Expected result: return false(or an actual WP_Error but other code like get_permalink() does not expect it)

Attachments (1)

45329.diff (897 bytes) - added by manzoorwani.jk 5 years ago.

Download all attachments as: .zip

Change History (8)

#1 @SergeyBiryukov
5 years ago

  • Component changed from General to Posts, Post Types
  • Keywords needs-patch needs-unit-tests added

@manzoorwani.jk
5 years ago

#2 @SergeyBiryukov
22 months ago

  • Milestone changed from Awaiting Review to 6.1

Hi there, welcome to WordPress Trac!

Thanks for the ticket, sorry it took so long for someone to get back to you.

Found this while working on #56243, still appears to be a valid issue.

#3 @SergeyBiryukov
21 months ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 53733:

Posts, Post Types: Correct the check for non-existing post in get_post_permalink().

The function was erroneously calling is_wp_error() on the result of a get_post() call, which returns null on failure, and never returns a WP_Error object.

Previously, passing a non-existing post ID to the function would result in a home URL being returned and a few Attempt to read property "post_type, post_name, hierarchical..." on null PHP warnings.

This commit ensures get_post_permalink() returns false on failure, which brings parity with get_permalink().

Includes a unit test to confirm the correct behavior.

Follow-up to [12923], [13023], [32606].

Props renegeuze, manzoorwani.jk.
Fixes #45329.

#4 @SergeyBiryukov
21 months ago

In 53734:

Docs: Add a @since note for get_post_permalink() returning false on failure.

Follow-up to [53733].

See #45329.

#5 @SergeyBiryukov
21 months ago

  • Keywords needs-patch needs-unit-tests removed

#6 @peterwilsoncc
21 months ago

  • Keywords needs-dev-note added

A call out for this on the other developer focused changed dev-note would be helpful.

This ticket was mentioned in Slack in #core by sergey. View the logs.


21 months ago

Note: See TracTickets for help on using tickets.