WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 3 months ago

#16956 new defect (bug)

Comments Being Pulled from Non-Existent Post Types

Reported by: sterlo Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 3.1
Component: Posts, Post Types Keywords: has-patch commit 3.9-early
Focuses: Cc:

Description

Originally on: #10461

I'm running standard LAMP on the latest trunk.

Just viewing the dashboard with no plugins activated:

Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 918 Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 919 Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 919 Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 922 Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 922 Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 918 Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 919 Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 919 Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 922 Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 922 Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 918 Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 919 Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 919 Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 922 Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 922 Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 918 Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 919 Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 919 Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 922 Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 922 Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 918 Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 919 Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 919 Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 922 Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 922 Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 918 Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 919 Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 919 Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 922 Notice: Trying to get property of non-object in /Users/grok/Projects/Local Development/wordpress/trunk/wp-includes/capabilities.php on line 922 

It's not recognizing "$post_type->cap" as valid.

And...here's why - I added this (/wp-includes/capabilities.php):

 918 echo "POST TYPE: Y U NO OBJECT?\n";
 919 var_dump($post_type);

And got:

POST TYPE: Y U NO OBJECT?
NULL

So in this context...the post type is null and the code was not expecting that.

Opening the actual $post object:

stdClass Object
(
    [ID] => 60
    [post_author] => 1
    [post_date] => 2011-01-28 19:46:23
    [post_date_gmt] => 2011-01-28 19:46:23
    [post_content] => CONTENT!
    [post_title] => I have it all!
    [post_excerpt] => 
    [post_status] => publish
    [comment_status] => open
    [ping_status] => open
    [post_password] => 
    [post_name] => i-have-it-all
    [to_ping] => 
    [pinged] => 
    [post_modified] => 2011-01-28 19:46:28
    [post_modified_gmt] => 2011-01-28 19:46:28
    [post_content_filtered] => 
    [post_parent] => 0
    [guid] => http://dev.wordpress.local/?post_type=staff_listing&p=60
    [menu_order] => 0
    [post_type] => staff_listing
    [post_mime_type] => 
    [comment_count] => 6
    [ancestors] => Array
        (
        )

    [filter] => raw
)

I think the problem might be custom post types or custom taxonomies...

So my custom post type in another plugin is creating: "?post_type=staff_listing".
And this post does show "[post_type] => staff_listing". BUT the plugin that had created these comments...is de-activated.

Activating the plugin resolves this issue.

So, whats a viable solution? Telling a developer to clean up after the plugin (removing content just because of deactivation), OR having WordPress not pull data (e.g. comments) that are assigned to other data (e.g. post types) that don't exist?

Old Code: Give me all comments.

New Code: Give me all comments that are tied to existing objects.

Attachments (3)

16956.diff (1.6 KB) - added by mitchoyoshitaka 19 months ago.
Patch v1
16956.2.diff (1.6 KB) - added by mitchoyoshitaka 11 months ago.
Patch v1 (fixed patchrot, 2013 May 9)
16956.3.diff (952 bytes) - added by DrewAPicture 8 months ago.
refresh following [24593]

Download all attachments as: .zip

Change History (27)

comment:1 kawauso3 years ago

  • Keywords dev-feedback added

comment:2 dd323 years ago

  • Component changed from General to Warnings/Notices
  • Keywords dev-feedback removed
  • Type changed from enhancement to defect (bug)

Closed #17839 as duplicate.

Noticed in the recent comments widget:

( ! ) Notice: Trying to get property of non-object in C:\www\wordpress-commit\wp-includes\capabilities.php on line 929
Call Stack
#	Time	Memory	Function	Location
1	0.0005	360328	{main}( )	..\index.php:0
2	0.3960	24624984	wp_dashboard( )	..\index.php:63
3	0.3960	24625272	do_meta_boxes( string(9), string(6), string(0) )	..\dashboard.php:221
4	0.8141	25014760	call_user_func ( string(28), string(0), array(4) )	..\template.php:970
5	0.8141	25014776	wp_dashboard_recent_comments( string(0), array(4) )	..\template.php:0
6	0.8171	25271192	current_user_can( string(9), string(3) )	..\dashboard.php:632
7	0.8171	25271784	call_user_func_array ( array(2), array(2) )	..\capabilities.php:1047
8	0.8171	25272048	WP_User->has_cap( string(9), string(3) )	..\capabilities.php:0
9	0.8172	25272448	call_user_func_array ( string(12), array(3) )	..\capabilities.php:729
10	0.8172	25272736	map_meta_cap( string(9), string(1), string(3) )	..\capabilities.php:0

Turns out, that it's a comment on a custom post type that's no longer registered.

As a result $post_type = get_post_type_object( $post->post_type ); fails to return a valid post type object (however, get_post() can still return the cpt) which results in the code operating on a null object.

comment:3 boonebgorges2 years ago

  • Cc boonebgorges@… added

Since the problem is related to map_meta_cap(), maybe it's best not to mess with the query but instead to solve the problem at the level of caps. Maybe when get_post_type_object( $post->post_type ) returns null, add 'do_not_allow' to $caps and bail?

comment:4 nacin2 years ago

Maybe when get_post_type_object( $post->post_type ) returns null, add 'do_not_allow' to $caps and bail?

At the very least, we should be checking for a null value there, yes.

mitchoyoshitaka19 months ago

Patch v1

comment:5 mitchoyoshitaka19 months ago

  • Cc mitcho@… added
  • Keywords has-patch added

Still an issue in trunk (3.5 alpha); attached patch based on boone and nacin's discussion. Patch fixes notices on my local trunk which were due to sterlo's initial situation (data from stray custom post types).

comment:7 SergeyBiryukov17 months ago

#22719 was marked as a duplicate.

comment:8 SergeyBiryukov16 months ago

#22934 was marked as a duplicate.

comment:9 ddavout11 months ago

  • Component changed from Warnings/Notices to Post Types

It is not so easy to apply a patch dating of 2 years ...

comment:10 SergeyBiryukov11 months ago

  • Milestone changed from Awaiting Review to 3.6

mitchoyoshitaka11 months ago

Patch v1 (fixed patchrot, 2013 May 9)

comment:11 mitchoyoshitaka11 months ago

Replying to ddavout:

It is not so easy to apply a patch dating of 2 years ...

Not sure which patch you were trying (mine was not 2 years old) but it might not have applied cleanly. I went ahead and updated it against trunk.

comment:12 follow-up: sterlo11 months ago

It's ALIVE!!!!

http://i.imgur.com/bhNML.gif

comment:13 in reply to: ↑ 12 mitchoyoshitaka11 months ago

Replying to sterlo:

It's ALIVE!!!!

Rumors of this ticket's death (or maybe my death) have been greatly exaggerated.

comment:14 nacin9 months ago

  • Keywords 3.7-early added
  • Milestone changed from 3.6 to Future Release

I like this patch, but this needs to be done carefully. Also, this should probably be paired with #13905. And the issues in #14530 should be considered.

Also — post types didn't always need to exist. There is some old code on WP.org that used the post_type field back from before there was a way to register them. Maybe we do nothing, but it's something to at least think about.

comment:15 wonderboymusic9 months ago

  • Milestone changed from Future Release to 3.7

these are all marked 3.7-early

comment:16 SergeyBiryukov8 months ago

#25173 was marked as a duplicate.

DrewAPicture8 months ago

refresh following [24593]

comment:17 DrewAPicture8 months ago

16956.3.diff is a refresh following [24593], which took care of the more visible notices (such as for comments).

comment:18 alexvorn28 months ago

  • Cc alexvornoffice@… added

comment:19 alexvorn28 months ago

maybe it would be better not to show comments from non-existent post types?

comment:20 ocean907 months ago

#25324 was marked as a duplicate.

comment:21 nacin6 months ago

  • Milestone changed from 3.7 to 3.8

A do_ not_allow patch needs a lot of testing and soaking.

comment:22 SergeyBiryukov4 months ago

#26366 was marked as a duplicate.

comment:23 nacin4 months ago

  • Keywords commit 3.9-early added; 3.7-early removed
  • Milestone changed from 3.8 to Future Release

I'm going to try this for 3.9.

comment:24 bobbingwide3 months ago

A simple alternative workaround for the original problem is to delete any comment which causes this message to appear.

Note: I haven't checked if this has already been reported but...
Whatever solution is eventually chosen for this ticket, almost exactly the same logic should apply for the tests against the post_status object ( $status_obj ) which are performed immediately after.
$status_obj can also be null if a post_status that was previously valid is no longer registered.

Note: See TracTickets for help on using tickets.