Skip to content

Commit

Permalink
Filter the author URLs.
Browse files Browse the repository at this point in the history
  • Loading branch information
peterwilsoncc committed Dec 20, 2022
1 parent 6b1ce5a commit a82444a
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
28 changes: 28 additions & 0 deletions includes/classes/DistributorPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,34 @@ public function get_author_name( $author_name = '' ) {
return $this->source_site['name'];
}

/**
* Get the post's author URL.
*
* For distributed posts this is a link to the original site. For the
* original post, this is the result of get_author_posts_url().
*
* @param string $author_link The author's posts URL. If specified, this will be returned if the
* author link does not need to be replaced by the original source name.
* @return string The post's author link.
*/
public function get_author_link( $author_link = '' ) {
if (
$this->is_source
|| $this->original_deleted
|| ! $this->is_linked
|| ! $this->connection_id
|| ! $this->original_post_url
) {
if ( empty( $author_link ) ) {
return get_author_posts_url( $this->post->post_author );
}
return $author_link;
}

$this->populate_source_site();
return $this->source_site['home_url'];
}

/**
* Get the post's distributable meta data.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ public static function canonicalize_front_end() {
// add_filter( 'wpseo_opengraph_url', array( '\Distributor\ExternalConnections\WordPressExternalConnection', 'wpseo_og_url' ) );
// add_filter( 'the_author', array( '\Distributor\ExternalConnections\WordPressExternalConnection', 'the_author_distributed' ) );
// add_filter( 'get_the_author_display_name', array( '\Distributor\ExternalConnections\WordPressExternalConnection', 'the_author_distributed' ) );
add_filter( 'author_link', array( '\Distributor\ExternalConnections\WordPressExternalConnection', 'author_posts_url_distributed' ), 10, 3 );
// add_filter( 'author_link', array( '\Distributor\ExternalConnections\WordPressExternalConnection', 'author_posts_url_distributed' ), 10, 3 );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ public static function canonicalize_front_end() {
// add_filter( 'wpseo_opengraph_url', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'wpseo_og_url' ) );
// add_filter( 'the_author', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'the_author_distributed' ) );
// add_filter( 'get_the_author_display_name', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'the_author_distributed' ) );
add_filter( 'author_link', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'author_posts_url_distributed' ), 10, 3 );
// add_filter( 'author_link', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'author_posts_url_distributed' ), 10, 3 );
}

/**
Expand Down
47 changes: 47 additions & 0 deletions includes/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ function setup() {
add_filter( 'wpseo_opengraph_url', $n( 'wpseo_opengraph_url' ), 10, 2 );
add_filter( 'the_author', $n( 'filter_the_author' ) );
add_filter( 'get_the_author_display_name', $n( 'get_the_author_display_name' ), 10, 3 );
add_filter( 'author_link', $n( 'filter_author_link' ) );
add_filter( 'get_the_author_user_url', $n( 'get_the_author_user_url' ), 10, 3 );

}

/**
Expand Down Expand Up @@ -138,3 +141,47 @@ function get_the_author_display_name( $display_name, $user_id, $original_user_id

return filter_the_author( $display_name );
}

/**
* Filter the author link for a distributed post.
*
* @since x.x.x
*
* @param string $link Author link.
* @return string Modified author link.
*/
function filter_author_link( $link ) {
$settings = Utils\get_settings();

if ( empty( $settings['override_author_byline'] ) ) {
return $link;
}

// Ensure there is a global post object.
if ( ! get_post() ) {
return $link;
}

$dt_post = new DistributorPost( get_post() );
return $dt_post->get_author_link( $link );
}

/**
* Filter the author page URL via get_the_author() for a distributed post.
*
* @since x.x.x
*
* @param string $author_url Author page URL.
* @param int $user_id User ID.
* @param int $original_user_id Original user ID for calling get_the_author(). False: get_the_author()
* was retrieve the author of the current post object.
* @return string Modified author page URL.
*/
function get_the_author_user_url( $author_url, $user_id, $original_user_id ) {
if ( false !== $original_user_id ) {
// get_the_author() was called for a specific user.
return $author_url;
}

return filter_author_link( $author_url );
}

0 comments on commit a82444a

Please sign in to comment.