Make WordPress Core

Changeset 53147


Ignore:
Timestamp:
04/11/2022 11:36:56 PM (2 years ago)
Author:
davidbaumwald
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/author-template.php

    r52568 r53147  
    225225 * @since 3.0.0
    226226 *
     227 * @global WP_User $authordata The current author's data.
     228 *
    227229 * @return string|null An HTML link if the author's url exist in user meta,
    228230 *                     else the result of get_the_author().
     
    230232function get_the_author_link() {
    231233    if ( get_the_author_meta( 'url' ) ) {
    232         return sprintf(
     234        global $authordata;
     235
     236        $author_url          = get_the_author_meta( 'url' );
     237        $author_display_name = get_the_author();
     238
     239        $link = sprintf(
    233240            '<a href="%1$s" title="%2$s" rel="author external">%3$s</a>',
    234             esc_url( get_the_author_meta( 'url' ) ),
     241            esc_url( $author_url ),
    235242            /* translators: %s: Author's display name. */
    236             esc_attr( sprintf( __( 'Visit %s&#8217;s website' ), get_the_author() ) ),
    237             get_the_author()
     243            esc_attr( sprintf( __( 'Visit %s&#8217;s website' ), $author_display_name ) ),
     244            $author_display_name
    238245        );
     246
     247        /**
     248         * Filters the author URL link HTML.
     249         *
     250         * @since 6.0.0
     251         *
     252         * @param string  $link       The default rendered author HTML link.
     253         * @param string  $author_url Author's URL.
     254         * @param WP_User $authordata Author user data.
     255         */
     256        return apply_filters( 'the_author_link', $link, $author_url, $authordata );
    239257    } else {
    240258        return get_the_author();
  • trunk/tests/phpunit/tests/user/author.php

    r52389 r53147  
    2020                'display_name' => 'Test Author',
    2121                'description'  => 'test_author',
     22                'user_url'     => 'http://example.com',
    2223            )
    2324        );
     
    145146    }
    146147
     148    /**
     149     * @ticket 51859
     150     *
     151     * @covers ::get_the_author_link
     152     */
     153    public function test_get_the_author_link() {
     154        $author_url          = get_the_author_meta( 'url' );
     155        $author_display_name = get_the_author();
     156
     157        $link = get_the_author_link();
     158
     159        $this->assertStringContainsString( $author_url, $link, 'The link does not contain the author URL' );
     160        $this->assertStringContainsString( $author_display_name, $link, 'The link does not contain the author display name' );
     161    }
     162
     163    /**
     164     * @ticket 51859
     165     *
     166     * @covers ::get_the_author_link
     167     */
     168    public function test_filtered_get_the_author_link() {
     169        $filter = new MockAction();
     170
     171        add_filter( 'the_author_link', array( &$filter, 'filter' ) );
     172
     173        get_the_author_link();
     174
     175        $this->assertSame( 1, $filter->get_call_count() );
     176        $this->assertSame( array( 'the_author_link' ), $filter->get_tags() );
     177    }
    147178}
Note: See TracChangeset for help on using the changeset viewer.