Opened 9 years ago
Closed 9 years ago
#33947 closed defect (bug) (fixed)
get_comment_class raises a PHP notice for non-registered users
Reported by: | walterebert | Owned by: | boonebgorges |
---|---|---|---|
Milestone: | 4.4 | Priority: | normal |
Severity: | normal | Version: | 4.4 |
Component: | Comments | Keywords: | has-unit-tests has-patch |
Focuses: | template | Cc: |
Description
Calling the function get_comment_class
raises a PHP notice, if a comment is from a non-registered user:
Notice: Trying to get property of non-object in /srv/www/htdocs/wp-includes/comment-template.php on line 444
I would propose to replace:
if ( $comment->user_id > 0 && $user = get_userdata( $comment->user_id ) ) {
With:
if ( ! empty( $comment->user_id ) && $user = get_userdata( $comment->user_id ) ) {
Attachments (4)
Change History (16)
#2
@
9 years ago
Hint: In this case we'd want to bail early and return an empty array if the comment object is invalid. It would also be nice to have test coverage for this if you'd like to try your hand at adding a unit test.
#3
@
9 years ago
It makes sense to me, to at least have a "comment" / "comment type" class, instead of no class at all.
I will look into writing a unit test for this.
#4
@
9 years ago
My patch includes:
- Verification if a comment is a WP_Comment object.
- Unit test for
get_comment_class
. - Format
get_comment_class
according to WordPress coding standards
#7
@
9 years ago
- Keywords has-unit-tests added
Hi @dipesh.kakadiya, as a matter of course, we don't fix coding standards in unrelated code in patches. Can you remove the extra whitespace changes?
#9
@
9 years ago
@DrewAPicture
Hi @dipesh.kakadiya, as a matter of course, we don't fix coding standards in unrelated code in patches. Can you remove the extra whitespace changes?
Revise patch as your suggested.
Hi @walterebert, welcome to Trac and thanks for the patch!
It seems like we should actually be checking if the
get_comment( $comment_id )
a few lines up is returning a valid comment object. My guess is the notice you're getting is probably from an invalid comment ID being passed and core not bailing if the comment is invalid.