Comments: add a `$comment` parameter to `get_comment_author_url_link()`.

Add unit tests (none exist).

Props flixos90, wonderboymusic.
Fixes #36573.


git-svn-id: https://develop.svn.wordpress.org/trunk@37305 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2016-04-22 22:44:33 +00:00
parent 7dd7992179
commit a776b0b6df
2 changed files with 116 additions and 19 deletions

View File

@ -293,8 +293,13 @@ function comment_author_IP( $comment_ID = 0 ) {
*/ */
function get_comment_author_url( $comment_ID = 0 ) { function get_comment_author_url( $comment_ID = 0 ) {
$comment = get_comment( $comment_ID ); $comment = get_comment( $comment_ID );
$url = ('http://' == $comment->comment_author_url) ? '' : $comment->comment_author_url; $url = '';
$url = esc_url( $url, array('http', 'https') ); $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. * 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 int $comment_ID The comment ID.
* @param WP_Comment $comment The comment object. * @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. * in the order of $before, link, and finally $after.
* *
* @since 1.5.0 * @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 * @param string $linktext Optional. The text to display instead of the comment
* author's email address. Default empty. * author's email address. Default empty.
* @param string $before Optional. The text or HTML to display before the email link. * @param string $before Optional. The text or HTML to display before the email link.
* Default empty. * Default empty.
* @param string $after Optional. The text or HTML to display after the email link. * @param string $after Optional. The text or HTML to display after the email link.
* Default empty. * 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. * @return string The HTML link between the $before and $after parameters.
*/ */
function get_comment_author_url_link( $linktext = '', $before = '', $after = '' ) { function get_comment_author_url_link( $linktext = '', $before = '', $after = '', $comment = 0 ) {
$url = get_comment_author_url(); $url = get_comment_author_url( $comment );
$display = ($linktext != '') ? $linktext : $url; $display = ($linktext != '') ? $linktext : $url;
$display = str_replace( 'http://www.', '', $display ); $display = str_replace( 'http://www.', '', $display );
$display = str_replace( 'http://', '', $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. * Displays the HTML link of the url of the author of the current comment.
* *
* @since 0.71 * @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 * @param string $linktext Optional. Text to display instead of the comment author's
* email address. Default empty. * email address. Default empty.
* @param string $before Optional. Text or HTML to display before the email link. * @param string $before Optional. Text or HTML to display before the email link.
* Default empty. * Default empty.
* @param string $after Optional. Text or HTML to display after the email link. * @param string $after Optional. Text or HTML to display after the email link.
* Default empty. * 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 = '' ) { function comment_author_url_link( $linktext = '', $before = '', $after = '', $comment = 0 ) {
echo get_comment_author_url_link( $linktext, $before, $after ); echo get_comment_author_url_link( $linktext, $before, $after, $comment );
} }
/** /**

View File

@ -0,0 +1,86 @@
<?php
/**
* @group comment
*/
class Tests_Comment_GetCommentAuthorUrlLink extends WP_UnitTestCase {
protected static $comments = array();
public static function wpSetUpBeforeClass( $factory ) {
unset( $GLOBALS['comment'] );
$comment_ids = $factory->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(
'<a href=\'%s\' rel=\'external\'>%s</a>',
$comment->comment_author_url,
$linktext
);
}
public function test_no_comment() {
$url_link = get_comment_author_url_link();
$this->assertEquals( "<a href='' rel='external'></a>", $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 );
}
}