mirror of
git://develop.git.wordpress.org/
synced 2025-01-18 05:18:42 +01:00
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:
parent
4b898c119b
commit
0ce91f72d9
@ -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’s website' ), get_the_author() ) ),
|
||||
get_the_author()
|
||||
esc_attr( sprintf( __( 'Visit %s’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();
|
||||
}
|
||||
|
@ -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() );
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user