diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index 967778a853..3f177bdc46 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -138,15 +138,18 @@ function comment_author_email( $comment_ID = 0 ) { * address and use it for their own means good and bad. * * @since 0.71 + * @since 4.6.0 The `$comment` parameter was added. * * @param string $linktext Optional. Text to display instead of the comment author's email address. * Default empty. * @param string $before Optional. Text or HTML to display before the email link. Default empty. * @param string $after Optional. Text or HTML to display after the email link. Default empty. + * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object. Default is the current comment. */ -function comment_author_email_link( $linktext = '', $before = '', $after = '' ) { - if ( $link = get_comment_author_email_link( $linktext, $before, $after ) ) +function comment_author_email_link( $linktext = '', $before = '', $after = '', $comment = null ) { + if ( $link = get_comment_author_email_link( $linktext, $before, $after, $comment ) ) { echo $link; + } } /** @@ -159,15 +162,18 @@ function comment_author_email_link( $linktext = '', $before = '', $after = '' ) * address and use it for their own means good and bad. * * @since 2.7.0 + * @since 4.6.0 The `$comment` parameter was added. * * @param string $linktext Optional. Text to display instead of the comment author's email address. * Default empty. * @param string $before Optional. Text or HTML to display before the email link. Default empty. * @param string $after Optional. Text or HTML to display after the email link. Default empty. + * @param int|WP_Comment $comment Optional. Comment ID or WP_Comment object. Default is the current comment. * @return string */ -function get_comment_author_email_link( $linktext = '', $before = '', $after = '' ) { - $comment = get_comment(); +function get_comment_author_email_link( $linktext = '', $before = '', $after = '', $comment = null ) { + $comment = get_comment( $comment ); + /** * Filter the comment author's email for display. * @@ -181,6 +187,7 @@ function get_comment_author_email_link( $linktext = '', $before = '', $after = ' * @param WP_Comment $comment The comment object. */ $email = apply_filters( 'comment_email', $comment->comment_author_email, $comment ); + if ((!empty($email)) && ($email != '@')) { $display = ($linktext != '') ? $linktext : $email; $return = $before; diff --git a/tests/phpunit/tests/comment/getCommentAuthorEmailLink.php b/tests/phpunit/tests/comment/getCommentAuthorEmailLink.php new file mode 100644 index 0000000000..82a73079ab --- /dev/null +++ b/tests/phpunit/tests/comment/getCommentAuthorEmailLink.php @@ -0,0 +1,99 @@ +comment->create_and_get( array( + 'comment_author_email' => 'foo@example.org' + ) ); + } + + public static function wpTearDownAfterClass() { + wp_delete_comment( self::$comment->comment_ID, true ); + } + + public function test_global_comment_with_default_parameters() { + $expected = 'foo@example.org'; + + $this->assertEquals( $expected, get_comment_author_email_link() ); + } + + /** + * @ticket 36571 + */ + public function test_all_parameters() { + unset( $GLOBALS['comment'] ); + + $linktext = rand_str( 10 ); + $before = rand_str( 5 ); + $after = rand_str( 6 ); + $comment = self::factory()->comment->create_and_get( array( + 'comment_author_email' => $email = 'baz@example.org' + ) ); + + $expected = sprintf( '%1$s%3$s%4$s', $before, $email, $linktext, $after ); + + $this->assertEquals( $expected, get_comment_author_email_link( $linktext, $before, $after, $comment ) ); + } + + public function test_all_parameters_with_global_comment() { + $linktext = rand_str( 10 ); + $before = rand_str( 5 ); + $after = rand_str( 6 ); + + $expected = sprintf( '%1$s%2$s%3$s', $before, $linktext, $after ); + + $this->assertEquals( $expected, get_comment_author_email_link( $linktext, $before, $after ) ); + } + + public function test_linktext() { + $expected = sprintf( '%1$s', $linktext = rand_str( 12 ) ); + + $this->assertEquals( $expected, get_comment_author_email_link( $linktext ) ); + } + + public function test_before() { + $expected = sprintf( '%1$sfoo@example.org', $before = rand_str( 5 ) ); + + $this->assertEquals( $expected, get_comment_author_email_link( '', $before ) ); + } + + public function test_after() { + $expected = sprintf( 'foo@example.org%1$s', $after = rand_str( 5 ) ); + + $this->assertEquals( $expected, get_comment_author_email_link( '', '', $after ) ); + } + + /** + * @ticket 36571 + */ + public function test_comment_param_should_override_global() { + $comment = self::factory()->comment->create_and_get( array( + 'comment_author_email' => $email = 'bar@example.org' + ) ); + + $expected = sprintf( '%2$s', $email, $email ); + + $this->assertEquals( $expected, get_comment_author_email_link( '', '', '', $comment ) ); + } +}