Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if the post is distributed, and if it is apply the author filter #1035

Merged
merged 7 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions includes/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ function filter_the_author( $display_name ) {
* @return string Modified author display name.
*/
function get_the_author_display_name( $display_name, $user_id, $original_user_id ) {
if ( false !== $original_user_id ) {
$current_post = get_post();
$is_distributed = empty( $current_post ) ? false : Utils\is_distributed_post( $current_post );

if ( ! $is_distributed && false !== $original_user_id ) {
// get_the_author() was called for a specific user.
return $display_name;
}
Expand Down Expand Up @@ -166,7 +169,10 @@ function filter_author_link( $link ) {
* @return string Modified author page URL.
*/
function get_the_author_user_url( $author_url, $user_id, $original_user_id ) {
if ( false !== $original_user_id ) {
$current_post = get_post();
$is_distributed = empty( $current_post ) ? false : Utils\is_distributed_post( $current_post );

if ( ! $is_distributed && false !== $original_user_id ) {
// get_the_author() was called for a specific user.
return $author_url;
}
Expand Down
18 changes: 18 additions & 0 deletions includes/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -1182,3 +1182,21 @@ function remote_http_request( $url, $args = array(), $fallback = '', $threshold

return wp_remote_request( $url, $args );
}

/**
* Determines if a post is distributed.
*
* @since x.x.x
*
* @param int|\WP_Post $post The post object or ID been checked.
peterwilsoncc marked this conversation as resolved.
Show resolved Hide resolved
* @return bool True if the post is distributed, false otherwise.
*/
function is_distributed_post( $post ) {
$post = get_post( $post );
if ( ! $post ) {
return false;
}
$post_id = $post->ID;
$original_post_id = get_post_meta( $post_id, 'dt_original_post_id', true );
return ! empty( $original_post_id );
}
22 changes: 13 additions & 9 deletions tests/php/HooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,19 @@ public function test_get_the_author_display_name_source() {
$this->assertSame( 'Alexander Hamilton', $actual, 'Unexpected value when getting specific author.' );
}

/**
* Test get_the_author_display_name
*
* @since x.x.x
*/
public function test_get_the_author_display_name_no_post() {
$actual = Hooks\get_the_author_display_name( 'George Washington', 1, false );
$this->assertSame( 'George Washington', $actual, 'Unexpected value when 1 current post author.' );

$actual = Hooks\get_the_author_display_name( 'George Washington', 1, 1 );
$this->assertSame( 'George Washington', $actual, 'Unexpected value when getting specific author.' );
}

/**
* Test get_the_author_display_name
*
Expand Down Expand Up @@ -785,9 +798,6 @@ public function test_get_the_author_display_name_external_pushed() {

$actual = Hooks\get_the_author_display_name( 'George Washington', 1, false );
$this->assertSame( 'Test External, Pushed Origin', $actual, 'Unexpected value when getting current post author.' );

$actual = Hooks\get_the_author_display_name( 'George Washington', 1, 1 );
$this->assertSame( 'George Washington', $actual, 'Unexpected value when getting specific author.' );
}

/**
Expand Down Expand Up @@ -833,9 +843,6 @@ public function test_get_the_author_display_name_external_pulled() {

$actual = Hooks\get_the_author_display_name( 'James Madison', 1, false );
$this->assertSame( 'Test External, Pulled Origin', $actual, 'Unexpected value when getting current post author.' );

$actual = Hooks\get_the_author_display_name( 'James Madison', 1, 1 );
$this->assertSame( 'James Madison', $actual, 'Unexpected value when getting specific author.' );
}

/**
Expand Down Expand Up @@ -908,8 +915,5 @@ public function test_get_the_author_display_name_internal() {

$actual = Hooks\get_the_author_display_name( 'Aaron Burr', 1, false );
$this->assertSame( 'Test Internal Origin', $actual, 'Unexpected value when getting current post author.' );

$actual = Hooks\get_the_author_display_name( 'Aaron Burr', 1, 1 );
$this->assertSame( 'Aaron Burr', $actual, 'Unexpected value when getting specific author.' );
}
}