From 3edc3b46f27b7cc8107af320e89993b365b291c5 Mon Sep 17 00:00:00 2001 From: Drew Jaynes Date: Tue, 29 Sep 2015 01:48:47 +0000 Subject: [PATCH] Introduce `get_the_author_posts_link()`, which returns the HTML link to the author page for the current post's author. `the_author_posts_link()` has in-turn been converted into a wrapper, with most of its logic moved to the new function. Adds tests for the new function. Props chipbennett, zrothauser, wonderboymusic, DrewAPicture. Fixes #30355. git-svn-id: https://develop.svn.wordpress.org/trunk@34677 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/author-template.php | 33 +++++++++----- tests/phpunit/tests/template/author.php | 60 +++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 12 deletions(-) create mode 100644 tests/phpunit/tests/template/author.php diff --git a/src/wp-includes/author-template.php b/src/wp-includes/author-template.php index 36d08bc81e..e1571dcc51 100644 --- a/src/wp-includes/author-template.php +++ b/src/wp-includes/author-template.php @@ -223,23 +223,17 @@ function the_author_posts() { } /** - * Display an HTML link to the author page of the author of the current post. + * Retrieves an HTML link to the author page of the current post's author. * - * Does just echo get_author_posts_url() function, like the others do. The - * reason for this, is that another function is used to help in printing the - * link to the author's posts. + * Returns an HTML-formatted link using get_author_posts_url(). * - * @link https://codex.wordpress.org/Template_Tags/the_author_posts_link - * @since 1.2.0 + * @since 4.4.0 * * @global object $authordata The current author's DB object. * - * @param string $deprecated Deprecated. + * @return string An HTML link to the author page. */ -function the_author_posts_link($deprecated = '') { - if ( !empty( $deprecated ) ) - _deprecated_argument( __FUNCTION__, '2.1' ); - +function get_the_author_posts_link() { global $authordata; if ( ! is_object( $authordata ) ) { return; @@ -259,7 +253,22 @@ function the_author_posts_link($deprecated = '') { * * @param string $link HTML link. */ - echo apply_filters( 'the_author_posts_link', $link ); + return apply_filters( 'the_author_posts_link', $link ); +} + +/** + * Displays an HTML link to the author page of the current post's author. + * + * @since 1.2.0 + * @since 4.4.0 Converted into a wrapper for get_the_author_posts_link() + * + * @param string $deprecated Unused. + */ +function the_author_posts_link( $deprecated = '' ) { + if ( ! empty( $deprecated ) ) { + _deprecated_argument( __FUNCTION__, '2.1' ); + } + echo get_the_author_posts_link(); } /** diff --git a/tests/phpunit/tests/template/author.php b/tests/phpunit/tests/template/author.php new file mode 100644 index 0000000000..98fa735632 --- /dev/null +++ b/tests/phpunit/tests/template/author.php @@ -0,0 +1,60 @@ +factory->user->create_and_get( array( + 'display_name' => 'Foo', + 'user_nicename' => 'bar' + ) ); + + $GLOBALS['authordata'] = $author->data; + + $link = get_the_author_posts_link(); + + $url = sprintf( 'http://%1$s/?author=%2$s', WP_TESTS_DOMAIN, $author->ID ); + + $this->assertContains( $url, $link ); + $this->assertContains( 'Posts by Foo', $link ); + $this->assertContains( '>Foo', $link ); + + unset( $GLOBALS['authordata'] ); + } + + /** + * @ticket 30355 + */ + public function test_get_the_author_posts_link_with_permalinks() { + global $wp_rewrite; + $wp_rewrite->init(); + $wp_rewrite->set_permalink_structure( '/%postname%/' ); + $wp_rewrite->flush_rules(); + + $author = $this->factory->user->create_and_get( array( + 'display_name' => 'Foo', + 'user_nicename' => 'bar' + ) ); + + $GLOBALS['authordata'] = $author; + + $link = get_the_author_posts_link(); + + $url = sprintf( 'http://%1$s/author/%2$s/', WP_TESTS_DOMAIN, $author->user_nicename ); + + $this->assertContains( $url, $link ); + $this->assertContains( 'Posts by Foo', $link ); + $this->assertContains( '>Foo', $link ); + + // Cleanup. + $wp_rewrite->set_permalink_structure( '' ); + unset( $GLOBALS['authordata'] ); + } +}