From e6b7c6b2d4c32091ec49f22eaad33fcd74d26d44 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Wed, 23 Sep 2015 14:08:47 +0000 Subject: [PATCH] Bail early when invalid ID is passed to `get_comment_class()`. This helps to avoid PHP notices later in the function. Props walterebert, dipesh.kakadiya, DrewAPicture. Fixes #33947. git-svn-id: https://develop.svn.wordpress.org/trunk@34454 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/comment-template.php | 7 +++++-- tests/phpunit/tests/comment/getCommentClass.php | 7 +++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index 79b51c1a98..f8ebaecdcc 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -433,10 +433,13 @@ function comment_class( $class = '', $comment = null, $post_id = null, $echo = t function get_comment_class( $class = '', $comment_id = null, $post_id = null ) { global $comment_alt, $comment_depth, $comment_thread_alt; - $comment = get_comment($comment_id); - $classes = array(); + $comment = get_comment( $comment_id ); + if ( ! $comment ) { + return $classes; + } + // Get the comment type (comment, trackback), $classes[] = ( empty( $comment->comment_type ) ) ? 'comment' : $comment->comment_type; diff --git a/tests/phpunit/tests/comment/getCommentClass.php b/tests/phpunit/tests/comment/getCommentClass.php index c7ce64923b..5ca3bc41ea 100644 --- a/tests/phpunit/tests/comment/getCommentClass.php +++ b/tests/phpunit/tests/comment/getCommentClass.php @@ -36,4 +36,11 @@ class Tests_Comment_GetCommentClass extends WP_UnitTestCase { $this->assertContains( 'foo', $classes ); $this->assertContains( 'bar', $classes ); } + + /** + * @ticket 33947 + */ + public function test_should_return_an_empty_array_for_invalid_comment_id() { + $this->assertSame( array(), get_comment_class( 'foo', 12345 ) ); + } }