WordPress.org

Make WordPress Core

Opened 8 months ago

Closed 8 months ago

#25005 closed defect (bug) (fixed)

Notice using get_sample_permalink()

Reported by: juliobox Owned by: SergeyBiryukov
Milestone: 3.7 Priority: normal
Severity: minor Version: 2.5
Component: Warnings/Notices Keywords: has-patch
Focuses: Cc:

Description

Hello
This is about a Notice in get_sample_permalink(), here the core:

function get_sample_permalink($id, $title = null, $name = null) {
	$post = get_post($id);
	if ( !$post->ID )
		return array('', '');
...

So if the $id is not correct, the $post->ID will throw a Notice: Trying to get property of non-object in file.php on line n
I think we can use a simple "!$post" or "is_a( $post, 'WP_Post' ) )" here.
Thanks for reading

Attachments (1)

25005.patch (888 bytes) - added by ocean90 8 months ago.

Download all attachments as: .zip

Change History (10)

comment:1 follow-up: alexvorn28 months ago

this will slow the process of the function, just use a correct ID :D

comment:2 in reply to: ↑ 1 juliobox8 months ago

Are you serious? ... Please ... don't be that guy.
Replying to alexvorn2:

this will slow the process of the function, just use a correct ID :D

comment:3 alexvorn28 months ago

  • Type changed from defect (bug) to enhancement

comment:4 rmccue8 months ago

  • Type changed from enhancement to defect (bug)

This is a bug, not an enhancement. We should be checking the value of $post here, probably just changing the if statement to if ( ! $post || ! $post->ID )

comment:5 follow-up: ocean908 months ago

  • Component changed from General to Warnings/Notices
  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to 3.7
  • Version set to 2.5

Can there be a post without an ID? ! $post should be enough.

[6983]/[6633]


alexvorn2, just stop it! That's not the first time.

comment:6 in reply to: ↑ 5 alexvorn28 months ago

Replying to ocean90:

Can there be a post without an ID? ! $post should be enough.

[6983]/[6633]


alexvorn2, just stop it! That's not the first time.

to stop what? is this related to this ticket?

comment:7 markoheijnen8 months ago

$post is enough. Otherwise get_post() should be checked what it will return. That is also why is_a() isn't needed. Also if it was needed $post instanceof WP_Post would be faster.

ocean908 months ago

comment:8 ocean908 months ago

  • Keywords has-patch added; needs-patch removed

25005.patch

  • ! $post check for get_sample_permalink() and get_sample_permalink_html()
  • Removes unused global $wpdb in get_sample_permalink_html()

comment:9 SergeyBiryukov8 months ago

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

In 25028:

Avoid PHP notices in get_sample_permalink() and get_sample_permalink_html(). Remove unused global reference. props ocean90. fixes #25005.

Note: See TracTickets for help on using tickets.