diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php
index 09969c81da..92ba74fb55 100644
--- a/src/wp-includes/comment-template.php
+++ b/src/wp-includes/comment-template.php
@@ -293,8 +293,13 @@ function comment_author_IP( $comment_ID = 0 ) {
*/
function get_comment_author_url( $comment_ID = 0 ) {
$comment = get_comment( $comment_ID );
- $url = ('http://' == $comment->comment_author_url) ? '' : $comment->comment_author_url;
- $url = esc_url( $url, array('http', 'https') );
+ $url = '';
+ $id = 0;
+ if ( ! empty( $comment ) ) {
+ $author_url = ( 'http://' == $comment->comment_author_url ) ? '' : $comment->comment_author_url;
+ $url = esc_url( $author_url, array( 'http', 'https' ) );
+ $id = $comment->ID;
+ }
/**
* Filter the comment author's URL.
@@ -306,7 +311,7 @@ function get_comment_author_url( $comment_ID = 0 ) {
* @param int $comment_ID The comment ID.
* @param WP_Comment $comment The comment object.
*/
- return apply_filters( 'get_comment_author_url', $url, $comment->comment_ID, $comment );
+ return apply_filters( 'get_comment_author_url', $url, $id, $comment );
}
/**
@@ -345,17 +350,20 @@ function comment_author_url( $comment_ID = 0 ) {
* in the order of $before, link, and finally $after.
*
* @since 1.5.0
+ * @since 4.6.0 The `$comment` parameter was added.
*
- * @param string $linktext Optional. The text to display instead of the comment
- * author's email address. Default empty.
- * @param string $before Optional. The text or HTML to display before the email link.
- * Default empty.
- * @param string $after Optional. The text or HTML to display after the email link.
- * Default empty.
+ * @param string $linktext Optional. The text to display instead of the comment
+ * author's email address. Default empty.
+ * @param string $before Optional. The text or HTML to display before the email link.
+ * Default empty.
+ * @param string $after Optional. The 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 The HTML link between the $before and $after parameters.
*/
-function get_comment_author_url_link( $linktext = '', $before = '', $after = '' ) {
- $url = get_comment_author_url();
+function get_comment_author_url_link( $linktext = '', $before = '', $after = '', $comment = 0 ) {
+ $url = get_comment_author_url( $comment );
$display = ($linktext != '') ? $linktext : $url;
$display = str_replace( 'http://www.', '', $display );
$display = str_replace( 'http://', '', $display );
@@ -380,16 +388,19 @@ function get_comment_author_url_link( $linktext = '', $before = '', $after = ''
* Displays the HTML link of the url of the author of the current comment.
*
* @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 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_url_link( $linktext = '', $before = '', $after = '' ) {
- echo get_comment_author_url_link( $linktext, $before, $after );
+function comment_author_url_link( $linktext = '', $before = '', $after = '', $comment = 0 ) {
+ echo get_comment_author_url_link( $linktext, $before, $after, $comment );
}
/**
diff --git a/tests/phpunit/tests/comment/getCommentAuthorUrlLink.php b/tests/phpunit/tests/comment/getCommentAuthorUrlLink.php
new file mode 100644
index 0000000000..dd59edca15
--- /dev/null
+++ b/tests/phpunit/tests/comment/getCommentAuthorUrlLink.php
@@ -0,0 +1,86 @@
+comment->create_post_comments( 0, 1 );
+ self::$comments = array_map( 'get_comment', $comment_ids );
+ }
+
+ public static function wpTearDownAfterClass() {
+ foreach ( self::$comments as $comment ) {
+ wp_delete_comment( $comment, true );
+ }
+ }
+
+ protected function parseCommentAuthorUrl( $comment, $linktext = '' ) {
+ if ( empty( $linktext ) ) {
+ $linktext = rtrim( preg_replace( '#http://(www\.)?#', '', $comment->comment_author_url ), '/' );
+ }
+ return sprintf(
+ '%s',
+ $comment->comment_author_url,
+ $linktext
+ );
+ }
+
+ public function test_no_comment() {
+ $url_link = get_comment_author_url_link();
+
+ $this->assertEquals( "", $url_link );
+ }
+
+ public function test_global_comment() {
+ $GLOBALS['comment'] = $comment = reset( self::$comments );
+
+ $url_link = get_comment_author_url_link();
+ $link = $this->parseCommentAuthorUrl( $comment );
+ $this->assertEquals( $link, $url_link );
+ }
+
+ public function test_comment_arg() {
+ $comment = reset( self::$comments );
+
+ $url_link = get_comment_author_url_link( '', '', '', $comment );
+ $link = $this->parseCommentAuthorUrl( $comment );
+ $this->assertEquals( $link, $url_link );
+ }
+
+ public function test_linktext() {
+ $comment = reset( self::$comments );
+
+ $url_link = get_comment_author_url_link( 'Burrito', '', '', $comment );
+ $link = $this->parseCommentAuthorUrl( $comment, 'Burrito' );
+ $this->assertEquals( $link, $url_link );
+ }
+
+ public function test_before() {
+ $comment = reset( self::$comments );
+
+ $url_link = get_comment_author_url_link( 'Burrito', 'I would love a ', '', $comment );
+ $link = 'I would love a ' . $this->parseCommentAuthorUrl( $comment, 'Burrito' );
+ $this->assertEquals( $link, $url_link );
+ }
+
+ public function test_after() {
+ $comment = reset( self::$comments );
+
+ $url_link = get_comment_author_url_link( 'Burrito', '', ' is my favorite word.', $comment );
+ $link = $this->parseCommentAuthorUrl( $comment, 'Burrito' ) . ' is my favorite word.';
+ $this->assertEquals( $link, $url_link );
+ }
+
+ public function test_before_after() {
+ $comment = reset( self::$comments );
+
+ $url_link = get_comment_author_url_link( 'Burrito', 'I would love a ', ' right now.', $comment );
+ $link = 'I would love a ' . $this->parseCommentAuthorUrl( $comment, 'Burrito' ) . ' right now.';
+ $this->assertEquals( $link, $url_link );
+ }
+}