Formatting: Make get_the_author_link pluggable.

Adds a new filter to alter the output of `get_the_author_link`.  This change also adds unit tests for the new filter.

Props dshanske, donmhico, audrasjb, peterwilsoncc, SergeyBiryukov.
Fixes #51859.

git-svn-id: https://develop.svn.wordpress.org/trunk@53147 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
David Baumwald 2022-04-11 23:36:56 +00:00
parent 4b898c119b
commit 0ce91f72d9
2 changed files with 53 additions and 4 deletions

View File

@ -224,18 +224,36 @@ function the_author_meta( $field = '', $user_id = false ) {
*
* @since 3.0.0
*
* @global WP_User $authordata The current author's data.
*
* @return string|null An HTML link if the author's url exist in user meta,
* else the result of get_the_author().
*/
function get_the_author_link() {
if ( get_the_author_meta( 'url' ) ) {
return sprintf(
global $authordata;
$author_url = get_the_author_meta( 'url' );
$author_display_name = get_the_author();
$link = sprintf(
'<a href="%1$s" title="%2$s" rel="author external">%3$s</a>',
esc_url( get_the_author_meta( 'url' ) ),
esc_url( $author_url ),
/* translators: %s: Author's display name. */
esc_attr( sprintf( __( 'Visit %s&#8217;s website' ), get_the_author() ) ),
get_the_author()
esc_attr( sprintf( __( 'Visit %s&#8217;s website' ), $author_display_name ) ),
$author_display_name
);
/**
* Filters the author URL link HTML.
*
* @since 6.0.0
*
* @param string $link The default rendered author HTML link.
* @param string $author_url Author's URL.
* @param WP_User $authordata Author user data.
*/
return apply_filters( 'the_author_link', $link, $author_url, $authordata );
} else {
return get_the_author();
}

View File

@ -19,6 +19,7 @@ class Tests_User_Author_Template extends WP_UnitTestCase {
'user_login' => 'test_author',
'display_name' => 'Test Author',
'description' => 'test_author',
'user_url' => 'http://example.com',
)
);
@ -144,4 +145,34 @@ class Tests_User_Author_Template extends WP_UnitTestCase {
unset( $GLOBALS['authordata'] );
}
/**
* @ticket 51859
*
* @covers ::get_the_author_link
*/
public function test_get_the_author_link() {
$author_url = get_the_author_meta( 'url' );
$author_display_name = get_the_author();
$link = get_the_author_link();
$this->assertStringContainsString( $author_url, $link, 'The link does not contain the author URL' );
$this->assertStringContainsString( $author_display_name, $link, 'The link does not contain the author display name' );
}
/**
* @ticket 51859
*
* @covers ::get_the_author_link
*/
public function test_filtered_get_the_author_link() {
$filter = new MockAction();
add_filter( 'the_author_link', array( &$filter, 'filter' ) );
get_the_author_link();
$this->assertSame( 1, $filter->get_call_count() );
$this->assertSame( array( 'the_author_link' ), $filter->get_tags() );
}
}