Make WordPress Core

Opened 11 years ago

Closed 11 years ago

#25005 closed defect (bug) (fixed)

Notice using get_sample_permalink()

Reported by: juliobox's profile juliobox Owned by: sergeybiryukov's profile 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 11 years ago.

Download all attachments as: .zip

Change History (10)

#1 follow-up: @alexvorn2
11 years ago

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

#2 in reply to: ↑ 1 @juliobox
11 years 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

#3 @alexvorn2
11 years ago

  • Type changed from defect (bug) to enhancement

#4 @rmccue
11 years 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 )

#5 follow-up: @ocean90
11 years 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.

#6 in reply to: ↑ 5 @alexvorn2
11 years 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?

#7 @markoheijnen
11 years 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.

@ocean90
11 years ago

#8 @ocean90
11 years 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()

#9 @SergeyBiryukov
11 years 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.