From 0ce91f72d94c6617539a6d2b55b31a7b0f5b9bee Mon Sep 17 00:00:00 2001 From: David Baumwald Date: Mon, 11 Apr 2022 23:36:56 +0000 Subject: [PATCH] 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 --- src/wp-includes/author-template.php | 26 ++++++++++++++++++++---- tests/phpunit/tests/user/author.php | 31 +++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/author-template.php b/src/wp-includes/author-template.php index e129e6f701..3f28492377 100644 --- a/src/wp-includes/author-template.php +++ b/src/wp-includes/author-template.php @@ -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( '%3$s', - 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(); } diff --git a/tests/phpunit/tests/user/author.php b/tests/phpunit/tests/user/author.php index 1a624ec833..1c9732d7d2 100644 --- a/tests/phpunit/tests/user/author.php +++ b/tests/phpunit/tests/user/author.php @@ -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() ); + } }