diff --git a/src/wp-includes/pluggable.php b/src/wp-includes/pluggable.php index fc0356aedf..a166ab017a 100644 --- a/src/wp-includes/pluggable.php +++ b/src/wp-includes/pluggable.php @@ -1550,7 +1550,7 @@ function wp_notify_moderator($comment_id) { $user = get_userdata( $post->post_author ); // Send to the administration and to the post author if the author can modify the comment. $emails = array( get_option( 'admin_email' ) ); - if ( user_can( $user->ID, 'edit_comment', $comment_id ) && ! empty( $user->user_email ) ) { + if ( $user && user_can( $user->ID, 'edit_comment', $comment_id ) && ! empty( $user->user_email ) ) { if ( 0 !== strcasecmp( $user->user_email, get_option( 'admin_email' ) ) ) $emails[] = $user->user_email; } diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index 854619572d..b12474ebfb 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -148,4 +148,19 @@ class Tests_Comment extends WP_UnitTestCase { unset( $_SERVER['REMOTE_ADDR'] ); } } + + /** + * @ticket 32566 + */ + public function test_wp_notify_moderator_should_not_throw_notice_when_post_author_is_0() { + $p = $this->factory->post->create( array( + 'post_author' => 0, + ) ); + + $c = $this->factory->comment->create( array( + 'comment_post_ID' => $p, + ) ); + + $this->assertTrue( wp_notify_moderator( $c ) ); + } }