From 479473b0f1f6f3db72e0d1a04bb0f5d5453a37a6 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Mon, 19 Dec 2022 13:16:13 +1100 Subject: [PATCH 01/23] Add original_deleted property. --- includes/classes/DistributorPost.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/includes/classes/DistributorPost.php b/includes/classes/DistributorPost.php index db667c1d2..acad1e03b 100644 --- a/includes/classes/DistributorPost.php +++ b/includes/classes/DistributorPost.php @@ -60,6 +60,13 @@ class DistributorPost { */ private $original_post_url = ''; + /** + * Whether the original post has been deleted. + * + * @var bool + */ + public $original_deleted = false; + /** * The direction of the connection. * @@ -129,6 +136,7 @@ public function __construct( $post ) { $this->is_linked = true; $this->original_post_id = $this->post->ID; $this->original_post_url = get_permalink( $this->post->ID ); + $this->original_deleted = false; return; } @@ -138,6 +146,7 @@ public function __construct( $post ) { $this->is_linked = ! get_post_meta( $post->ID, 'dt_unlinked', true ); $this->original_post_id = (int) $original_post_id; $this->original_post_url = get_post_meta( $post->ID, 'dt_original_post_url', true ); + $this->original_deleted = (bool) get_post_meta( $post->ID, 'dt_original_deleted', true ); // Determine the connection type. if ( get_post_meta( $post->ID, 'dt_original_blog_id', true ) ) { From f970b49d7c695e128fbfa6913d7a88224035837d Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Mon, 19 Dec 2022 14:32:37 +1100 Subject: [PATCH 02/23] Canonical URL getter in Distributor post object. --- includes/classes/DistributorPost.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/includes/classes/DistributorPost.php b/includes/classes/DistributorPost.php index acad1e03b..68ad80fa7 100644 --- a/includes/classes/DistributorPost.php +++ b/includes/classes/DistributorPost.php @@ -356,6 +356,34 @@ public function get_the_post_thumbnail( $size = 'post-thumbnail', $attr = '' ) { return get_the_post_thumbnail( $this->post, $size, $attr ); } + /** + * Get the post's canonical URL. + * + * For distributed posts, this is the permalink of the original post. For + * the original post, this is the result of get_permalink(). + * + * @param string $canonical_url The post's canonical URL. If specified, this will be returned + * if the canonical URL does not need to be replaced by the + * original source URL. + * @return string The post's canonical URL. + */ + public function get_canonical_url( $canonical_url = '' ) { + if ( + $this->is_source + || $this->original_deleted + || ! $this->is_linked + || ! $this->connection_id + || ! $this->original_post_url + ) { + if ( empty( $canonical_url ) ) { + return $this->get_permalink(); + } + return $canonical_url; + } + + return $this->original_post_url; + } + /** * Get the post's distributable meta data. * From 94743a6c49e08c6e23b403474bb7bca5e0d9df7f Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Mon, 19 Dec 2022 14:33:39 +1100 Subject: [PATCH 03/23] Move canonical filters to single location. --- includes/bootstrap.php | 2 + .../WordPressExternalConnection.php | 4 +- .../NetworkSiteConnection.php | 4 +- includes/hooks.php | 55 +++++++++++++++++++ 4 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 includes/hooks.php diff --git a/includes/bootstrap.php b/includes/bootstrap.php index a61098066..cb6608a42 100644 --- a/includes/bootstrap.php +++ b/includes/bootstrap.php @@ -121,6 +121,7 @@ function( $response ) { require_once __DIR__ . '/utils.php'; require_once __DIR__ . '/global-functions.php'; +require_once __DIR__ . '/hooks.php'; require_once __DIR__ . '/external-connection-cpt.php'; require_once __DIR__ . '/push-ui.php'; require_once __DIR__ . '/pull-ui.php'; @@ -268,6 +269,7 @@ function() { * We use setup functions to avoid unit testing WP_Mock strict mode errors. */ \Distributor\ExternalConnectionCPT\setup(); +\Distributor\Hooks\setup(); \Distributor\PushUI\setup(); \Distributor\PullUI\setup(); \Distributor\RestApi\setup(); diff --git a/includes/classes/ExternalConnections/WordPressExternalConnection.php b/includes/classes/ExternalConnections/WordPressExternalConnection.php index 85025b23b..75242fbdc 100644 --- a/includes/classes/ExternalConnections/WordPressExternalConnection.php +++ b/includes/classes/ExternalConnections/WordPressExternalConnection.php @@ -1010,8 +1010,8 @@ public static function bootstrap() { * @since 1.0 */ public static function canonicalize_front_end() { - add_filter( 'get_canonical_url', array( '\Distributor\ExternalConnections\WordPressExternalConnection', 'canonical_url' ), 10, 2 ); - add_filter( 'wpseo_canonical', array( '\Distributor\ExternalConnections\WordPressExternalConnection', 'wpseo_canonical_url' ) ); + // add_filter( 'get_canonical_url', array( '\Distributor\ExternalConnections\WordPressExternalConnection', 'canonical_url' ), 10, 2 ); + // add_filter( 'wpseo_canonical', array( '\Distributor\ExternalConnections\WordPressExternalConnection', 'wpseo_canonical_url' ) ); 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' ) ); diff --git a/includes/classes/InternalConnections/NetworkSiteConnection.php b/includes/classes/InternalConnections/NetworkSiteConnection.php index 63f62807f..89b357f79 100644 --- a/includes/classes/InternalConnections/NetworkSiteConnection.php +++ b/includes/classes/InternalConnections/NetworkSiteConnection.php @@ -922,8 +922,8 @@ public static function clear_authorized_sites_cache( $user_id = false ) { * @since 0.8 */ public static function canonicalize_front_end() { - add_filter( 'get_canonical_url', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'canonical_url' ), 10, 2 ); - add_filter( 'wpseo_canonical', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'wpseo_canonical_url' ) ); + // add_filter( 'get_canonical_url', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'canonical_url' ), 10, 2 ); + // add_filter( 'wpseo_canonical', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'wpseo_canonical_url' ) ); 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' ) ); diff --git a/includes/hooks.php b/includes/hooks.php new file mode 100644 index 000000000..41d7a7ee3 --- /dev/null +++ b/includes/hooks.php @@ -0,0 +1,55 @@ +get_canonical_url( $canonical_url ); +} + +/** + * Filter the Yoast SEO canonical URL for a distributed post. + * + * @since x.x.x + * + * @param string $canonical_url Canonical URL. + * @param Yoast\WP\SEO\Presentations\Indexable_Presentation $presentation Yoast SEO meta tag presenter. + * @return string Modified canonical URL. + */ +function wpseo_canonical( $canonical_url, $presentation ) { + if ( ! $presentation->source instanceof WP_Post ) { + return $canonical_url; + } + + return get_canonical_url( $canonical_url, $presentation->source ); +} From 2f34857c86084800aa0d298380088607722c4e57 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Mon, 19 Dec 2022 14:49:06 +1100 Subject: [PATCH 04/23] Filter Yoast SEO open-graph URL. --- .../WordPressExternalConnection.php | 2 +- .../NetworkSiteConnection.php | 2 +- includes/hooks.php | 21 ++++++++++++++++++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/includes/classes/ExternalConnections/WordPressExternalConnection.php b/includes/classes/ExternalConnections/WordPressExternalConnection.php index 75242fbdc..fc93f8c51 100644 --- a/includes/classes/ExternalConnections/WordPressExternalConnection.php +++ b/includes/classes/ExternalConnections/WordPressExternalConnection.php @@ -1012,7 +1012,7 @@ public static function bootstrap() { public static function canonicalize_front_end() { // add_filter( 'get_canonical_url', array( '\Distributor\ExternalConnections\WordPressExternalConnection', 'canonical_url' ), 10, 2 ); // add_filter( 'wpseo_canonical', array( '\Distributor\ExternalConnections\WordPressExternalConnection', 'wpseo_canonical_url' ) ); - add_filter( 'wpseo_opengraph_url', array( '\Distributor\ExternalConnections\WordPressExternalConnection', 'wpseo_og_url' ) ); + // 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 ); diff --git a/includes/classes/InternalConnections/NetworkSiteConnection.php b/includes/classes/InternalConnections/NetworkSiteConnection.php index 89b357f79..7795d69c8 100644 --- a/includes/classes/InternalConnections/NetworkSiteConnection.php +++ b/includes/classes/InternalConnections/NetworkSiteConnection.php @@ -924,7 +924,7 @@ public static function clear_authorized_sites_cache( $user_id = false ) { public static function canonicalize_front_end() { // add_filter( 'get_canonical_url', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'canonical_url' ), 10, 2 ); // add_filter( 'wpseo_canonical', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'wpseo_canonical_url' ) ); - add_filter( 'wpseo_opengraph_url', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'wpseo_og_url' ) ); + // 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 ); diff --git a/includes/hooks.php b/includes/hooks.php index 41d7a7ee3..7dcd1634d 100644 --- a/includes/hooks.php +++ b/includes/hooks.php @@ -19,7 +19,8 @@ function setup() { }; add_action( 'get_canonical_url', $n( 'get_canonical_url' ), 10, 2 ); - add_action( 'wpseo_canonical', $n( 'wpseo_canonical' ), 10, 3 ); + add_action( 'wpseo_canonical', $n( 'wpseo_canonical' ), 10, 2 ); + add_filter( 'wpseo_opengraph_url', $n( 'wpseo_opengraph_url' ), 10, 2 ); } /** @@ -53,3 +54,21 @@ function wpseo_canonical( $canonical_url, $presentation ) { return get_canonical_url( $canonical_url, $presentation->source ); } + +/** + * Filter the Yoast SEO OpenGraph URL for a distributed post. + * + * @since x.x.x + * + * @param string $og_url OpenGraph URL. + * @param Yoast\WP\SEO\Presentations\Indexable_Presentation $presentation Yoast SEO meta tag presenter. + * @return string Modified OpenGraph URL. + */ +function wpseo_opengraph_url( $og_url, $presentation ) { + if ( ! $presentation->source instanceof WP_Post ) { + return $og_url; + } + + $dt_post = new DistributorPost( $presentation->source ); + return $dt_post->get_permalink(); +} From cead7a6141cf62f8680a308a40f14e5dbed0e418 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Mon, 19 Dec 2022 15:01:30 +1100 Subject: [PATCH 05/23] Account for legacy format Yoast filters. --- includes/hooks.php | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/includes/hooks.php b/includes/hooks.php index 7dcd1634d..397e64490 100644 --- a/includes/hooks.php +++ b/includes/hooks.php @@ -43,16 +43,25 @@ function get_canonical_url( $canonical_url, $post ) { * * @since x.x.x * - * @param string $canonical_url Canonical URL. - * @param Yoast\WP\SEO\Presentations\Indexable_Presentation $presentation Yoast SEO meta tag presenter. + * @param string $canonical_url Canonical URL. + * @param Yoast\WP\SEO\Presentations\Indexable_Presentation|false $presentation Yoast SEO meta tag presenter. False: filter applied in legacy format. * @return string Modified canonical URL. */ -function wpseo_canonical( $canonical_url, $presentation ) { - if ( ! $presentation->source instanceof WP_Post ) { +function wpseo_canonical( $canonical_url, $presentation = false ) { + if ( false !== $presentation ) { + if ( ! $presentation->source instanceof WP_Post ) { + return $canonical_url; + } + + return get_canonical_url( $canonical_url, $presentation->source ); + } + + // Filter was called in legacy format. + if ( ! is_singular() ) { return $canonical_url; } - return get_canonical_url( $canonical_url, $presentation->source ); + return get_canonical_url( $canonical_url, get_post() ); } /** @@ -60,15 +69,25 @@ function wpseo_canonical( $canonical_url, $presentation ) { * * @since x.x.x * - * @param string $og_url OpenGraph URL. - * @param Yoast\WP\SEO\Presentations\Indexable_Presentation $presentation Yoast SEO meta tag presenter. + * @param string $og_url OpenGraph URL. + * @param Yoast\WP\SEO\Presentations\Indexable_Presentation|false $presentation Yoast SEO meta tag presenter. False: filter applied in legacy format. * @return string Modified OpenGraph URL. */ -function wpseo_opengraph_url( $og_url, $presentation ) { - if ( ! $presentation->source instanceof WP_Post ) { +function wpseo_opengraph_url( $og_url, $presentation = false ) { + if ( false !== $presentation ) { + if ( ! $presentation->source instanceof WP_Post ) { + return $og_url; + } + + $dt_post = new DistributorPost( $presentation->source ); + return $dt_post->get_permalink(); + } + + // Filter was called in legacy format. + if ( ! is_singular() ) { return $og_url; } - $dt_post = new DistributorPost( $presentation->source ); + $dt_post = new DistributorPost( get_post() ); return $dt_post->get_permalink(); } From 6b1ce5adc29bd7f498f8045adf36df23d01afe36 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Tue, 20 Dec 2022 09:02:03 +1100 Subject: [PATCH 06/23] Author display name overrides. --- includes/classes/DistributorPost.php | 28 +++++++++++ .../WordPressExternalConnection.php | 4 +- .../NetworkSiteConnection.php | 4 +- includes/hooks.php | 47 +++++++++++++++++++ 4 files changed, 79 insertions(+), 4 deletions(-) diff --git a/includes/classes/DistributorPost.php b/includes/classes/DistributorPost.php index 68ad80fa7..6a3197b59 100644 --- a/includes/classes/DistributorPost.php +++ b/includes/classes/DistributorPost.php @@ -384,6 +384,34 @@ public function get_canonical_url( $canonical_url = '' ) { return $this->original_post_url; } + /** + * Get the post's author name. + * + * For distributed posts this is the name of the original site. For the + * original post, this is the result of get_the_author(). + * + * @param string $author_name The post's author name. If specified, this will be returned if the + * author name does not need to be replaced by the original source name. + * @return string The post's author name. + */ + public function get_author_name( $author_name = '' ) { + if ( + $this->is_source + || $this->original_deleted + || ! $this->is_linked + || ! $this->connection_id + || ! $this->original_post_url + ) { + if ( empty( $author_name ) ) { + return get_the_author_meta( 'display_name', $this->post->post_author ); + } + return $author_name; + } + + $this->populate_source_site(); + return $this->source_site['name']; + } + /** * Get the post's distributable meta data. * diff --git a/includes/classes/ExternalConnections/WordPressExternalConnection.php b/includes/classes/ExternalConnections/WordPressExternalConnection.php index fc93f8c51..757425826 100644 --- a/includes/classes/ExternalConnections/WordPressExternalConnection.php +++ b/includes/classes/ExternalConnections/WordPressExternalConnection.php @@ -1013,8 +1013,8 @@ public static function canonicalize_front_end() { // add_filter( 'get_canonical_url', array( '\Distributor\ExternalConnections\WordPressExternalConnection', 'canonical_url' ), 10, 2 ); // add_filter( 'wpseo_canonical', array( '\Distributor\ExternalConnections\WordPressExternalConnection', 'wpseo_canonical_url' ) ); // 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( '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 ); } diff --git a/includes/classes/InternalConnections/NetworkSiteConnection.php b/includes/classes/InternalConnections/NetworkSiteConnection.php index 7795d69c8..33415d7bc 100644 --- a/includes/classes/InternalConnections/NetworkSiteConnection.php +++ b/includes/classes/InternalConnections/NetworkSiteConnection.php @@ -925,8 +925,8 @@ public static function canonicalize_front_end() { // add_filter( 'get_canonical_url', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'canonical_url' ), 10, 2 ); // add_filter( 'wpseo_canonical', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'wpseo_canonical_url' ) ); // 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( '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 ); } diff --git a/includes/hooks.php b/includes/hooks.php index 397e64490..2ea30ad00 100644 --- a/includes/hooks.php +++ b/includes/hooks.php @@ -8,6 +8,7 @@ namespace Distributor\Hooks; use Distributor\DistributorPost; +use Distributor\Utils; use WP_Post; /** @@ -21,6 +22,8 @@ function setup() { add_action( 'get_canonical_url', $n( 'get_canonical_url' ), 10, 2 ); add_action( 'wpseo_canonical', $n( 'wpseo_canonical' ), 10, 2 ); 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 ); } /** @@ -91,3 +94,47 @@ function wpseo_opengraph_url( $og_url, $presentation = false ) { $dt_post = new DistributorPost( get_post() ); return $dt_post->get_permalink(); } + +/** + * Filter the author name via the_author() for a distributed post. + * + * @since x.x.x + * + * @param string $display_name Author display name. + * @return string Modified author display name. + */ +function filter_the_author( $display_name ) { + $settings = Utils\get_settings(); + + if ( empty( $settings['override_author_byline'] ) ) { + return $display_name; + } + + // Ensure there is a global post object. + if ( ! get_post() ) { + return $display_name; + } + + $dt_post = new DistributorPost( get_post() ); + return $dt_post->get_author_name( $display_name ); +} + +/** + * Filter the author display name via get_the_author() for a distributed post. + * + * @since x.x.x + * + * @param string $display_name Author display name. + * @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 display name. + */ +function get_the_author_display_name( $display_name, $user_id, $original_user_id ) { + if ( false !== $original_user_id ) { + // get_the_author() was called for a specific user. + return $display_name; + } + + return filter_the_author( $display_name ); +} From a82444a3039df0f715cad5a7b1d662c56a753b43 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Tue, 20 Dec 2022 11:03:59 +1100 Subject: [PATCH 07/23] Filter the author URLs. --- includes/classes/DistributorPost.php | 28 +++++++++++ .../WordPressExternalConnection.php | 2 +- .../NetworkSiteConnection.php | 2 +- includes/hooks.php | 47 +++++++++++++++++++ 4 files changed, 77 insertions(+), 2 deletions(-) diff --git a/includes/classes/DistributorPost.php b/includes/classes/DistributorPost.php index 6a3197b59..81bc999b2 100644 --- a/includes/classes/DistributorPost.php +++ b/includes/classes/DistributorPost.php @@ -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. * diff --git a/includes/classes/ExternalConnections/WordPressExternalConnection.php b/includes/classes/ExternalConnections/WordPressExternalConnection.php index 757425826..c0bee33e9 100644 --- a/includes/classes/ExternalConnections/WordPressExternalConnection.php +++ b/includes/classes/ExternalConnections/WordPressExternalConnection.php @@ -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 ); } /** diff --git a/includes/classes/InternalConnections/NetworkSiteConnection.php b/includes/classes/InternalConnections/NetworkSiteConnection.php index 33415d7bc..a99a71e89 100644 --- a/includes/classes/InternalConnections/NetworkSiteConnection.php +++ b/includes/classes/InternalConnections/NetworkSiteConnection.php @@ -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 ); } /** diff --git a/includes/hooks.php b/includes/hooks.php index 2ea30ad00..d53a2b01b 100644 --- a/includes/hooks.php +++ b/includes/hooks.php @@ -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 ); + } /** @@ -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 ); +} From c8fca2a9ffd2697f683a217975597a375fe43d35 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Tue, 20 Dec 2022 12:36:43 +1100 Subject: [PATCH 08/23] Add tests for canonical method. --- tests/php/DistributorPostTest.php | 77 +++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/tests/php/DistributorPostTest.php b/tests/php/DistributorPostTest.php index 309e7e1b0..7034e97a8 100644 --- a/tests/php/DistributorPostTest.php +++ b/tests/php/DistributorPostTest.php @@ -1615,4 +1615,81 @@ public function test_post_data_with_blocks() { $this->assertSame( $to_json_expected, $to_json_actual ); } + + /** + * Test get_canonical_url() method. + * + * @covers ::get_canonical_url() + */ + public function test_get_canonical_url() { + $this->setup_post_mock(); + $this->setup_post_meta_mock( + array ( + 'dt_original_post_id' => array( '10' ), + 'dt_original_blog_id' => array( '2' ), + 'dt_syndicate_time' => array ( '1670383190' ), + 'dt_original_post_url' => array ( 'http://origin.example.org/?p=10' ), + ) + ); + + $dt_post = new DistributorPost( 1 ); + $canonical_url_actual = $dt_post->get_canonical_url(); + $canonical_url_expected = 'http://origin.example.org/?p=10'; + + $this->assertSame( $canonical_url_expected, $canonical_url_actual ); + } + + /** + * Test get_canonical_url() method. + * + * @covers ::get_canonical_url() + */ + public function test_get_canonical_url_unlinked() { + $this->setup_post_mock(); + $this->setup_post_meta_mock( + array ( + 'dt_original_post_id' => array( '10' ), + 'dt_original_blog_id' => array( '2' ), + 'dt_syndicate_time' => array ( '1670383190' ), + 'dt_original_post_url' => array ( 'http://origin.example.org/?p=10' ), + 'dt_unlinked' => array ( '1' ), + ) + ); + + \WP_Mock::userFunction( + 'get_permalink', + array( + 'return' => 'http://distributed.example.org/?p=1', + ) + ); + + $dt_post = new DistributorPost( 1 ); + $canonical_url_actual = $dt_post->get_canonical_url(); + $canonical_url_expected = 'http://distributed.example.org/?p=1'; + + $this->assertSame( $canonical_url_expected, $canonical_url_actual ); + } + + /** + * Test get_canonical_url() method. + * + * @covers ::get_canonical_url() + */ + public function test_get_canonical_url_source() { + $this->setup_post_mock(); + $this->setup_post_meta_mock( array() ); + + \WP_Mock::userFunction( + 'get_permalink', + array( + 'return' => 'http://source.example.org/?p=1', + ) + ); + + $dt_post = new DistributorPost( 1 ); + $canonical_url_actual = $dt_post->get_canonical_url(); + $canonical_url_expected = 'http://source.example.org/?p=1'; + + $this->assertSame( $canonical_url_expected, $canonical_url_actual ); + } } From 2f15f8d0d5d25109f2b186ce2d5c7fe949c25af4 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Tue, 20 Dec 2022 12:47:29 +1100 Subject: [PATCH 09/23] Tests for get_author_name() method. --- tests/php/DistributorPostTest.php | 88 +++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/tests/php/DistributorPostTest.php b/tests/php/DistributorPostTest.php index 7034e97a8..d555f6945 100644 --- a/tests/php/DistributorPostTest.php +++ b/tests/php/DistributorPostTest.php @@ -1692,4 +1692,92 @@ public function test_get_canonical_url_source() { $this->assertSame( $canonical_url_expected, $canonical_url_actual ); } + + /** + * Test get_author_name() method. + * + * @covers ::get_author_name() + */ + public function test_get_author_name() { + $this->setup_post_mock(); + $this->setup_post_meta_mock( + array ( + 'dt_original_post_id' => array( '10' ), + 'dt_original_site_name' => array( 'Test External, Pushed Origin' ), + 'dt_original_site_url' => array( 'http://origin.example.org/' ), + 'dt_original_post_url' => array( 'http://origin.example.org/?p=10' ), + 'dt_subscription_signature' => array( 'abcdefghijklmnopqrstuvwxyz' ), + 'dt_syndicate_time' => array( '1670384223' ), + 'dt_full_connection' => array( '1' ), + 'dt_original_source_id' => array( '2' ), + ) + ); + + $dt_post = new DistributorPost( 1 ); + $author_name_actual = $dt_post->get_author_name(); + $author_name_expected = 'Test External, Pushed Origin'; + + $this->assertSame( $author_name_expected, $author_name_actual ); + } + + /** + * Test get_author_name() method. + * + * @covers ::get_author_name() + */ + public function test_get_author_name_unlinked() { + $this->setup_post_mock(); + $this->setup_post_meta_mock( + array ( + 'dt_original_post_id' => array( '10' ), + 'dt_original_blog_id' => array( '2' ), + 'dt_syndicate_time' => array ( '1670383190' ), + 'dt_original_post_url' => array ( 'http://origin.example.org/?p=10' ), + 'dt_unlinked' => array ( '1' ), + ) + ); + + \WP_Mock::userFunction( + 'get_the_author_meta', + array( + 'return' => 'Unlinked author name', + ) + ); + + $dt_post = new DistributorPost( 1 ); + $author_name_actual = $dt_post->get_author_name(); + $author_name_expected = 'Unlinked author name'; + + $this->assertSame( $author_name_expected, $author_name_actual ); + } + + /** + * Test get_author_name() method. + * + * @covers ::get_author_name() + */ + public function test_get_author_name_source() { + $this->setup_post_mock(); + $this->setup_post_meta_mock( array() ); + + \WP_Mock::userFunction( + 'get_permalink', + array( + 'return' => 'http://source.example.org/?p=1', + ) + ); + + \WP_Mock::userFunction( + 'get_the_author_meta', + array( + 'return' => 'Original site author name', + ) + ); + + $dt_post = new DistributorPost( 1 ); + $author_name_actual = $dt_post->get_author_name(); + $author_name_expected = 'Original site author name'; + + $this->assertSame( $author_name_expected, $author_name_actual ); + } } From d3166c5d363d1f9fd60cfbc07e2142e4107a47d8 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Tue, 20 Dec 2022 13:09:56 +1100 Subject: [PATCH 10/23] Test get author link. --- tests/php/DistributorPostTest.php | 88 +++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/tests/php/DistributorPostTest.php b/tests/php/DistributorPostTest.php index d555f6945..58943ea85 100644 --- a/tests/php/DistributorPostTest.php +++ b/tests/php/DistributorPostTest.php @@ -1780,4 +1780,92 @@ public function test_get_author_name_source() { $this->assertSame( $author_name_expected, $author_name_actual ); } + + /** + * Test get_author_link() method. + * + * @covers ::get_author_link() + */ + public function test_get_author_link() { + $this->setup_post_mock(); + $this->setup_post_meta_mock( + array ( + 'dt_original_post_id' => array( '10' ), + 'dt_original_site_name' => array( 'Test External, Pushed Origin' ), + 'dt_original_site_url' => array( 'http://origin.example.org/' ), + 'dt_original_post_url' => array( 'http://origin.example.org/?p=10' ), + 'dt_subscription_signature' => array( 'abcdefghijklmnopqrstuvwxyz' ), + 'dt_syndicate_time' => array( '1670384223' ), + 'dt_full_connection' => array( '1' ), + 'dt_original_source_id' => array( '2' ), + ) + ); + + $dt_post = new DistributorPost( 1 ); + $author_link_actual = $dt_post->get_author_link(); + $author_link_expected = 'http://origin.example.org/'; + + $this->assertSame( $author_link_expected, $author_link_actual ); + } + + /** + * Test get_author_link() method. + * + * @covers ::get_author_link() + */ + public function test_get_author_link_unlinked() { + $this->setup_post_mock(); + $this->setup_post_meta_mock( + array ( + 'dt_original_post_id' => array( '10' ), + 'dt_original_blog_id' => array( '2' ), + 'dt_syndicate_time' => array ( '1670383190' ), + 'dt_original_post_url' => array ( 'http://origin.example.org/?p=10' ), + 'dt_unlinked' => array ( '1' ), + ) + ); + + \WP_Mock::userFunction( + 'get_author_posts_url', + array( + 'return' => 'http://destination.example.org/author/unlinked-author-name/', + ) + ); + + $dt_post = new DistributorPost( 1 ); + $author_link_actual = $dt_post->get_author_link(); + $author_link_expected = 'http://destination.example.org/author/unlinked-author-name/'; + + $this->assertSame( $author_link_expected, $author_link_actual ); + } + + /** + * Test get_author_link() method. + * + * @covers ::get_author_link() + */ + public function test_get_author_link_source() { + $this->setup_post_mock(); + $this->setup_post_meta_mock( array() ); + + \WP_Mock::userFunction( + 'get_permalink', + array( + 'return' => 'http://source.example.org/?p=1', + ) + ); + + \WP_Mock::userFunction( + 'get_author_posts_url', + array( + 'return' => 'http://source.example.org/author/original-author-name/', + ) + ); + + $dt_post = new DistributorPost( 1 ); + $author_link_actual = $dt_post->get_author_link(); + $author_link_expected = 'http://source.example.org/author/original-author-name/'; + + $this->assertSame( $author_link_expected, $author_link_actual ); + } } From 36cb2728cb4b8a8d252a15b2eae5432e4906b455 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Tue, 20 Dec 2022 14:00:07 +1100 Subject: [PATCH 11/23] Deprecate canonicalization functions in `WordPressExternalConnection`. --- .../WordPressExternalConnection.php | 128 +++++------------- 1 file changed, 34 insertions(+), 94 deletions(-) diff --git a/includes/classes/ExternalConnections/WordPressExternalConnection.php b/includes/classes/ExternalConnections/WordPressExternalConnection.php index c0bee33e9..8560f12bc 100644 --- a/includes/classes/ExternalConnections/WordPressExternalConnection.php +++ b/includes/classes/ExternalConnections/WordPressExternalConnection.php @@ -1001,157 +1001,97 @@ private function to_wp_post( $post ) { * @since 1.0 */ public static function bootstrap() { - add_action( 'template_redirect', array( '\Distributor\ExternalConnections\WordPressExternalConnection', 'canonicalize_front_end' ) ); } /** * Setup canonicalization on front end * * @since 1.0 + * @deprecated 2.0.0 */ public static function canonicalize_front_end() { - // add_filter( 'get_canonical_url', array( '\Distributor\ExternalConnections\WordPressExternalConnection', 'canonical_url' ), 10, 2 ); - // add_filter( 'wpseo_canonical', array( '\Distributor\ExternalConnections\WordPressExternalConnection', 'wpseo_canonical_url' ) ); - // 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 ); + _deprecated_function( __METHOD__, '2.0.0' ); } /** * Override author with site name on distributed post * + * @since 1.0 + * @deprecated 2.0.0 Use Distributor\Hooks\filter_author_link instead. + * * @param string $link Author link. * @param int $author_id Author ID. * @param string $author_nicename Author name. - * @since 1.0 * @return string */ public static function author_posts_url_distributed( $link, $author_id, $author_nicename ) { - global $post; - - if ( empty( $post ) ) { - return $link; - } - - $settings = Utils\get_settings(); - - if ( empty( $settings['override_author_byline'] ) ) { - return $link; - } - - $original_source_id = get_post_meta( $post->ID, 'dt_original_source_id', true ); - $original_site_url = get_post_meta( $post->ID, 'dt_original_site_url', true ); - $unlinked = (bool) get_post_meta( $post->ID, 'dt_unlinked', true ); - - if ( empty( $original_source_id ) || empty( $original_site_url ) || $unlinked ) { - return $link; - } - - return $original_site_url; + _deprecated_function( __METHOD__, '2.0.0', 'Distributor\Hooks\filter_author_link' ); + return \Distributor\Hooks\filter_author_link( $link ); } /** * Override author with site name on distributed post * - * @param string $author Author name. * @since 1.0 + * @deprecated 2.0.0 Use Distributor\Hooks\filter_the_author instead. + * + * @param string $author Author name. * @return string */ public static function the_author_distributed( $author ) { - global $post; - - if ( empty( $post ) ) { - return $author; - } - - $settings = Utils\get_settings(); - - if ( empty( $settings['override_author_byline'] ) ) { - return $author; - } - - $original_source_id = get_post_meta( $post->ID, 'dt_original_source_id', true ); - $original_site_name = get_post_meta( $post->ID, 'dt_original_site_name', true ); - $original_site_url = get_post_meta( $post->ID, 'dt_original_site_url', true ); - $unlinked = (bool) get_post_meta( $post->ID, 'dt_unlinked', true ); - - if ( empty( $original_source_id ) || empty( $original_site_url ) || empty( $original_site_name ) || $unlinked ) { - return $author; - } - - return $original_site_name; + _deprecated_function( __METHOD__, '2.0.0', 'Distributor\Hooks\filter_the_author' ); + return \Distributor\Hooks\filter_the_author( $author ); } /** * Make sure canonical url header is outputted * + * @since 1.0 + * @deprecated 2.0.0 Use Distributor\Hooks\get_canonical_url instead. + * * @param string $canonical_url Canonical URL. * @param object $post Post object. - * @since 1.0 * @return string */ public static function canonical_url( $canonical_url, $post ) { - $original_source_id = get_post_meta( $post->ID, 'dt_original_source_id', true ); - $original_post_url = get_post_meta( $post->ID, 'dt_original_post_url', true ); - $unlinked = (bool) get_post_meta( $post->ID, 'dt_unlinked', true ); - $original_deleted = (bool) get_post_meta( $post->ID, 'dt_original_post_deleted', true ); - - if ( empty( $original_source_id ) || empty( $original_post_url ) || $unlinked || $original_deleted ) { - return $canonical_url; - } - - return $original_post_url; + _deprecated_function( __METHOD__, '2.0.0', 'Distributor\Hooks\get_canonical_url' ); + return \Distributor\Hooks\get_canonical_url( $canonical_url, $post ); } /** * Handles the canonical URL change for distributed content when Yoast SEO is in use * - * @param string $canonical_url The Yoast WPSEO deduced canonical URL * @since 1.0 + * @deprecated 2.0.0 Use Distributor\Hooks\wpseo_canonical instead. + * + * @param string $canonical_url The Yoast WPSEO deduced canonical URL * @return string $canonical_url The updated distributor friendly URL */ public static function wpseo_canonical_url( $canonical_url ) { - - // Return as is if not on a singular page - taken from rel_canonical() - if ( ! is_singular() ) { - return $canonical_url; - } - - $id = get_queried_object_id(); - - // Return as is if we do not have a object id for context - taken from rel_canonical() - if ( 0 === $id ) { - return $canonical_url; - } - - $post = get_post( $id ); - - // Return as is if we don't have a valid post object - taken from wp_get_canonical_url() - if ( ! $post ) { - return $canonical_url; - } - - // Return as is if current post is not published - taken from wp_get_canonical_url() - if ( 'publish' !== $post->post_status ) { - return $canonical_url; + _deprecated_function( __METHOD__, '2.0.0', 'Distributor\Hooks\wpseo_canonical' ); + $presentation = false; + if ( is_singular() ) { + $source = get_post(); + $presentation = (object) array( 'source' => $source ); } - - return self::canonical_url( $canonical_url, $post ); + return \Distributor\Hooks\wpseo_canonical( $canonical_url, $presentation ); } /** * Handles the og:url change for distributed content when Yoast SEO is in use * - * @param string $og_url The Yoast WPSEO deduced OG URL which is a result of wpseo_canonical_url + * @deprecated 2.0.0 Use Distributor\Hooks\wpseo_opengraph_url instead. * + * @param string $og_url The Yoast WPSEO deduced OG URL which is a result of wpseo_canonical_url * @return string $og_url The updated distributor friendly URL */ - public static function wpseo_og_url( $og_url ) { + public static function wpseo_opengraph_url( $og_url ) { + _deprecated_function( __METHOD__, '2.0.0', 'Distributor\Hooks\wpseo_opengraph_url' ); + $presentation = false; if ( is_singular() ) { - $og_url = get_permalink(); + $source = get_post(); + $presentation = (object) array( 'source' => $source ); } - - return $og_url; + return \Distributor\Hooks\wpseo_opengraph_url( $og_url, $presentation ); } } From 09abc9d7fd2b075eefb60ca12903e92e5de099c0 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Tue, 20 Dec 2022 14:08:01 +1100 Subject: [PATCH 12/23] Deprecate canonicalization functions in `NetworkSiteConnection`. --- .../NetworkSiteConnection.php | 137 +++++------------- 1 file changed, 37 insertions(+), 100 deletions(-) diff --git a/includes/classes/InternalConnections/NetworkSiteConnection.php b/includes/classes/InternalConnections/NetworkSiteConnection.php index a99a71e89..c24120411 100644 --- a/includes/classes/InternalConnections/NetworkSiteConnection.php +++ b/includes/classes/InternalConnections/NetworkSiteConnection.php @@ -578,7 +578,6 @@ public function remote_get( $args = array() ) { * @since 0.8 */ public static function bootstrap() { - add_action( 'template_redirect', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'canonicalize_front_end' ) ); add_action( 'wp_after_insert_post', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'update_syndicated' ), 99 ); add_action( 'before_delete_post', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'separate_syndicated_on_delete' ) ); add_action( 'before_delete_post', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'remove_distributor_post_from_original' ) ); @@ -920,154 +919,92 @@ public static function clear_authorized_sites_cache( $user_id = false ) { * Setup canonicalization on front end * * @since 0.8 + * @deprecated 2.0.0 */ public static function canonicalize_front_end() { - // add_filter( 'get_canonical_url', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'canonical_url' ), 10, 2 ); - // add_filter( 'wpseo_canonical', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'wpseo_canonical_url' ) ); - // 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 ); + _deprecated_function( __METHOD__, '2.0.0' ); } /** * Override author with site name on distributed post * - * @param string $link Author link - * @param int $author_id Author id. - * @param string $author_nicename Author name. * @since 1.0 + * @deprecated 2.0.0 Use Distributor\Hooks\filter_author_link instead. + * + * @param string $link Author link. + * @param int $author_id Author ID. + * @param string $author_nicename Author name. * @return string */ public static function author_posts_url_distributed( $link, $author_id, $author_nicename ) { - global $post; - - if ( empty( $post ) ) { - return $link; - } - - $settings = Utils\get_settings(); - - if ( empty( $settings['override_author_byline'] ) ) { - return $link; - } - - $original_blog_id = get_post_meta( $post->ID, 'dt_original_blog_id', true ); - $original_post_id = get_post_meta( $post->ID, 'dt_original_post_id', true ); - $unlinked = (bool) get_post_meta( $post->ID, 'dt_unlinked', true ); - - if ( empty( $original_blog_id ) || empty( $original_post_id ) || $unlinked ) { - return $link; - } - - return get_home_url( $original_blog_id ); + _deprecated_function( __METHOD__, '2.0.0', 'Distributor\Hooks\filter_author_link' ); + return \Distributor\Hooks\filter_author_link( $link ); } /** * Override author with site name on distributed post * - * @param string $author Author name. * @since 1.0 + * @deprecated 2.0.0 Use Distributor\Hooks\filter_the_author instead. + * + * @param string $author Author name. * @return string */ public static function the_author_distributed( $author ) { - global $post; - - if ( empty( $post ) ) { - return $author; - } - - $settings = Utils\get_settings(); - - if ( empty( $settings['override_author_byline'] ) ) { - return $author; - } - - $original_blog_id = get_post_meta( $post->ID, 'dt_original_blog_id', true ); - $original_post_id = get_post_meta( $post->ID, 'dt_original_post_id', true ); - $unlinked = (bool) get_post_meta( $post->ID, 'dt_unlinked', true ); - - if ( empty( $original_blog_id ) || empty( $original_post_id ) || $unlinked ) { - return $author; - } - - $blog_details = get_blog_details( $original_blog_id ); - - return $blog_details->blogname; + _deprecated_function( __METHOD__, '2.0.0', 'Distributor\Hooks\filter_the_author' ); + return \Distributor\Hooks\filter_the_author( $author ); } /** * Make sure canonical url header is outputted * - * @param string $canonical_url Canonical url. - * @param object $post Post object. * @since 0.8 + * @deprecated 2.0.0 Use Distributor\Hooks\get_canonical_url instead. + * + * @param string $canonical_url Canonical URL. + * @param object $post Post object. * @return string */ public static function canonical_url( $canonical_url, $post ) { - $original_blog_id = get_post_meta( $post->ID, 'dt_original_blog_id', true ); - $original_post_id = get_post_meta( $post->ID, 'dt_original_post_id', true ); - $unlinked = (bool) get_post_meta( $post->ID, 'dt_unlinked', true ); - $original_deleted = (bool) get_post_meta( $post->ID, 'dt_original_post_deleted', true ); - - if ( empty( $original_blog_id ) || empty( $original_post_id ) || $unlinked || $original_deleted ) { - return $canonical_url; - } - - $original_post_url = get_post_meta( $post->ID, 'dt_original_post_url', true ); - - return $original_post_url; + _deprecated_function( __METHOD__, '2.0.0', 'Distributor\Hooks\get_canonical_url' ); + return \Distributor\Hooks\get_canonical_url( $canonical_url, $post ); } /** * Handles the canonical URL change for distributed content when Yoast SEO is in use * - * @param string $canonical_url The Yoast WPSEO deduced canonical URL * @since 1.0 + * @deprecated 2.0.0 Use Distributor\Hooks\wpseo_canonical instead. + * + * @param string $canonical_url The Yoast WPSEO deduced canonical URL * @return string $canonical_url The updated distributor friendly URL */ public static function wpseo_canonical_url( $canonical_url ) { - - // Return as is if not on a singular page - taken from rel_canonical() - if ( ! is_singular() ) { - $canonical_url; - } - - $id = get_queried_object_id(); - - // Return as is if we do not have a object id for context - taken from rel_canonical() - if ( 0 === $id ) { - return $canonical_url; - } - - $post = get_post( $id ); - - // Return as is if we don't have a valid post object - taken from wp_get_canonical_url() - if ( ! $post ) { - return $canonical_url; - } - - // Return as is if current post is not published - taken from wp_get_canonical_url() - if ( 'publish' !== $post->post_status ) { - return $canonical_url; + _deprecated_function( __METHOD__, '2.0.0', 'Distributor\Hooks\wpseo_canonical' ); + $presentation = false; + if ( is_singular() ) { + $source = get_post(); + $presentation = (object) array( 'source' => $source ); } - - return self::canonical_url( $canonical_url, $post ); + return \Distributor\Hooks\wpseo_canonical( $canonical_url, $presentation ); } /** * Handles the og:url change for distributed content when Yoast SEO is in use * - * @param string $og_url The Yoast WPSEO deduced OG URL which is a result of wpseo_canonical_url + * @deprecated 2.0.0 Use Distributor\Hooks\wpseo_opengraph_url instead. * + * @param string $og_url The Yoast WPSEO deduced OG URL which is a result of wpseo_canonical_url * @return string $og_url The updated distributor friendly URL */ - public static function wpseo_og_url( $og_url ) { + public static function wpseo_opengraph_url( $og_url ) { + _deprecated_function( __METHOD__, '2.0.0', 'Distributor\Hooks\wpseo_opengraph_url' ); + $presentation = false; if ( is_singular() ) { - $og_url = get_permalink(); + $source = get_post(); + $presentation = (object) array( 'source' => $source ); } - - return $og_url; + return \Distributor\Hooks\wpseo_opengraph_url( $og_url, $presentation ); } /** From fc19f0587509eba62c0eaff271853ad306a60ac1 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Tue, 20 Dec 2022 14:34:50 +1100 Subject: [PATCH 13/23] CS Fix. --- tests/php/DistributorPostTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/php/DistributorPostTest.php b/tests/php/DistributorPostTest.php index 58943ea85..c8e0020d8 100644 --- a/tests/php/DistributorPostTest.php +++ b/tests/php/DistributorPostTest.php @@ -72,7 +72,6 @@ public function setup_post_mock( $post_overrides = array() ) { $post = array_merge( $defaults, $post_overrides ); - \WP_Mock::userFunction( 'get_post', array( From 12677ad07c5f5d2e21ad291f85b3bdefa0966d03 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Tue, 20 Dec 2022 15:05:43 +1100 Subject: [PATCH 14/23] Tests for the internal hooks functions. --- tests/php/HooksTest.php | 231 +++++++++++++++++++++++++++++++++++ tests/php/bootstrap.php.dist | 1 + 2 files changed, 232 insertions(+) create mode 100644 tests/php/HooksTest.php diff --git a/tests/php/HooksTest.php b/tests/php/HooksTest.php new file mode 100644 index 000000000..868c074b1 --- /dev/null +++ b/tests/php/HooksTest.php @@ -0,0 +1,231 @@ + 1, + 'post_title' => 'Test Post', + 'post_content' => 'Test Content', + 'post_excerpt' => 'Test Excerpt', + 'post_status' => 'publish', + 'post_type' => 'post', + 'post_author' => 1, + 'post_date' => '2020-01-01 00:00:00', + 'post_date_gmt' => '2020-01-01 00:00:00', + 'post_modified' => '2020-01-01 00:00:00', + 'post_modified_gmt' => '2020-01-01 00:00:00', + 'post_parent' => 0, + 'post_mime_type' => '', + 'comment_count' => 0, + 'comment_status' => 'open', + 'ping_status' => 'open', + 'guid' => 'http://example.org/?p=1', + 'menu_order' => 0, + 'pinged' => '', + 'to_ping' => '', + 'post_password' => '', + 'post_name' => 'test-post', + 'post_content_filtered' => '', + ); + + $post = array_merge( $defaults, $post_overrides ); + + + \WP_Mock::userFunction( + 'get_post', + array( + 'return' => (object) $post, + ) + ); + } + + /** + * Helper function to mock get_post_meta. + */ + public function setup_post_meta_mock( $post_meta ) { + $get_post_meta = function( $post_id, $key = '', $single = false ) use ( $post_meta ) { + if ( empty( $key ) ) { + return $post_meta; + } + + if ( isset( $post_meta[ $key ] ) ) { + if ( $single ) { + return $post_meta[ $key ][0]; + } + return $post_meta[ $key ]; + } + + return ''; + }; + + \WP_Mock::userFunction( + 'get_post_meta', + array( + 'return' => $get_post_meta, + ) + ); + } + + /** + * Test get_canonical_url + * + * @since x.x.x + */ + public function test_get_canonical_url() { + $this->setup_post_mock(); + $this->setup_post_meta_mock(array()); + + \WP_Mock::userFunction( + 'get_permalink', + array( + 'return' => 'https://example.com/?p=1', + ) + ); + + \WP_Mock::userFunction( + 'is_singular', + array( + 'return' => true, + ) + ); + + $actual = Hooks\get_canonical_url( 'https://example.com/?p=1', (object) array( 'ID' => 1 ) ); + $this->assertSame( 'https://example.com/?p=1', $actual ); + } + + /** + * Test get_canonical_url + * + * @since x.x.x + */ + public function test_get_canonical_url_external_pushed() { + $this->setup_post_mock(); + $this->setup_post_meta_mock( + array ( + 'dt_original_post_id' => array( '10' ), + 'dt_original_site_name' => array( 'Test External, Pushed Origin' ), + 'dt_original_site_url' => array( 'http://origin.example.org/' ), + 'dt_original_post_url' => array( 'http://origin.example.org/?p=10' ), + 'dt_subscription_signature' => array( 'abcdefghijklmnopqrstuvwxyz' ), + 'dt_syndicate_time' => array( '1670384223' ), + 'dt_full_connection' => array( '1' ), + 'dt_original_source_id' => array( '2' ), + ) + ); + + \WP_Mock::userFunction( + 'get_permalink', + array( + 'return' => 'https://example.com/?p=1', + ) + ); + + \WP_Mock::userFunction( + 'is_singular', + array( + 'return' => true, + ) + ); + + $actual = Hooks\get_canonical_url( 'https://example.com/?p=1', (object) array( 'ID' => 1 ) ); + $this->assertSame( 'http://origin.example.org/?p=10', $actual ); + } + + /** + * Test get_canonical_url + * + * @since x.x.x + */ + public function test_get_canonical_url_external_pulled() { + $this->setup_post_mock(); + $this->setup_post_meta_mock( + array ( + 'dt_original_post_id' => array( '10' ), + 'dt_original_site_name' => array( 'Test External, Pulled Origin' ), + 'dt_original_site_url' => array( 'http://origin.example.org/' ), + 'dt_original_post_url' => array( 'http://origin.example.org/?p=11' ), + 'dt_subscription_signature' => array( 'abcdefghijklmnopqrstuvwxyz' ), + 'dt_syndicate_time' => array( '1670384223' ), + 'dt_full_connection' => array( '' ), + 'dt_original_source_id' => array( '3' ), + ) + ); + + \WP_Mock::userFunction( + 'get_permalink', + array( + 'return' => 'https://example.com/?p=1', + ) + ); + + \WP_Mock::userFunction( + 'is_singular', + array( + 'return' => true, + ) + ); + + $actual = Hooks\get_canonical_url( 'https://example.com/?p=1', (object) array( 'ID' => 1 ) ); + $this->assertSame( 'http://origin.example.org/?p=11', $actual ); + } + + /** + * Test get_canonical_url + * + * @since x.x.x + */ + public function test_get_canonical_url_internal() { + $this->setup_post_mock(); + $this->setup_post_meta_mock( + array ( + 'dt_original_post_id' => array( '10' ), + 'dt_original_blog_id' => array( '2' ), + 'dt_syndicate_time' => array ( '1670383190' ), + 'dt_original_post_url' => array ( 'http://origin.example.org/?p=12' ), + ) + ); + + \WP_Mock::userFunction( + 'get_permalink', + array( + 'return' => 'https://example.com/?p=1', + ) + ); + + \WP_Mock::userFunction( + 'is_singular', + array( + 'return' => true, + ) + ); + + $actual = Hooks\get_canonical_url( 'https://example.com/?p=1', (object) array( 'ID' => 1 ) ); + $this->assertSame( 'http://origin.example.org/?p=12', $actual ); + } +} diff --git a/tests/php/bootstrap.php.dist b/tests/php/bootstrap.php.dist index 36c49c7e1..371de0f5f 100644 --- a/tests/php/bootstrap.php.dist +++ b/tests/php/bootstrap.php.dist @@ -11,6 +11,7 @@ WP_Mock::bootstrap(); require_once __DIR__ . '/includes/common.php'; +require_once( __DIR__ . '/../../includes/hooks.php' ); require_once( __DIR__ . '/../../includes/utils.php' ); require_once( __DIR__ . '/../../includes/debug-info.php' ); require_once( __DIR__ . '/../../includes/subscriptions.php' ); From b1bf3e1aab7cb33fd06a880d00e178980d15defb Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Tue, 20 Dec 2022 15:27:58 +1100 Subject: [PATCH 15/23] Test WP SEO Canonical filters. --- tests/php/HooksTest.php | 137 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) diff --git a/tests/php/HooksTest.php b/tests/php/HooksTest.php index 868c074b1..10daa90dc 100644 --- a/tests/php/HooksTest.php +++ b/tests/php/HooksTest.php @@ -228,4 +228,141 @@ public function test_get_canonical_url_internal() { $actual = Hooks\get_canonical_url( 'https://example.com/?p=1', (object) array( 'ID' => 1 ) ); $this->assertSame( 'http://origin.example.org/?p=12', $actual ); } + + /** + * Test wpseo_canonical + * + * @since x.x.x + */ + public function test_wpseo_canonical() { + $this->setup_post_mock(); + $this->setup_post_meta_mock(array()); + + \WP_Mock::userFunction( + 'get_permalink', + array( + 'return' => 'https://example.com/?p=1', + ) + ); + + \WP_Mock::userFunction( + 'is_singular', + array( + 'return' => true, + ) + ); + + $actual = Hooks\wpseo_canonical( 'https://example.com/?p=1' ); + $this->assertSame( 'https://example.com/?p=1', $actual ); + } + + /** + * Test wpseo_canonical + * + * @since x.x.x + */ + public function test_wpseo_canonical_external_pushed() { + $this->setup_post_mock(); + $this->setup_post_meta_mock( + array ( + 'dt_original_post_id' => array( '10' ), + 'dt_original_site_name' => array( 'Test External, Pushed Origin' ), + 'dt_original_site_url' => array( 'http://origin.example.org/' ), + 'dt_original_post_url' => array( 'http://origin.example.org/?p=10' ), + 'dt_subscription_signature' => array( 'abcdefghijklmnopqrstuvwxyz' ), + 'dt_syndicate_time' => array( '1670384223' ), + 'dt_full_connection' => array( '1' ), + 'dt_original_source_id' => array( '2' ), + ) + ); + + \WP_Mock::userFunction( + 'get_permalink', + array( + 'return' => 'https://example.com/?p=1', + ) + ); + + \WP_Mock::userFunction( + 'is_singular', + array( + 'return' => true, + ) + ); + + $actual = Hooks\wpseo_canonical( 'https://example.com/?p=1' ); + $this->assertSame( 'http://origin.example.org/?p=10', $actual ); + } + + /** + * Test wpseo_canonical + * + * @since x.x.x + */ + public function test_wpseo_canonical_external_pulled() { + $this->setup_post_mock(); + $this->setup_post_meta_mock( + array ( + 'dt_original_post_id' => array( '10' ), + 'dt_original_site_name' => array( 'Test External, Pulled Origin' ), + 'dt_original_site_url' => array( 'http://origin.example.org/' ), + 'dt_original_post_url' => array( 'http://origin.example.org/?p=11' ), + 'dt_subscription_signature' => array( 'abcdefghijklmnopqrstuvwxyz' ), + 'dt_syndicate_time' => array( '1670384223' ), + 'dt_full_connection' => array( '' ), + 'dt_original_source_id' => array( '3' ), + ) + ); + + \WP_Mock::userFunction( + 'get_permalink', + array( + 'return' => 'https://example.com/?p=1', + ) + ); + + \WP_Mock::userFunction( + 'is_singular', + array( + 'return' => true, + ) + ); + + $actual = Hooks\wpseo_canonical( 'https://example.com/?p=1' ); + $this->assertSame( 'http://origin.example.org/?p=11', $actual ); + } + + /** + * Test wpseo_canonical + * + * @since x.x.x + */ + public function test_wpseo_canonical_internal() { + $this->setup_post_mock(); + $this->setup_post_meta_mock( + array ( + 'dt_original_post_id' => array( '10' ), + 'dt_original_blog_id' => array( '2' ), + 'dt_syndicate_time' => array ( '1670383190' ), + 'dt_original_post_url' => array ( 'http://origin.example.org/?p=12' ), + ) + ); + + \WP_Mock::userFunction( + 'get_permalink', + array( + 'return' => 'https://example.com/?p=1', + ) + ); + + \WP_Mock::userFunction( + 'is_singular', + array( + 'return' => true, + ) + ); + + $actual = Hooks\wpseo_canonical( 'https://example.com/?p=1' ); + $this->assertSame( 'http://origin.example.org/?p=12', $actual ); + } } From 62bfe08b0d4a7edf617f4a880a99d644f18859be Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Tue, 20 Dec 2022 15:28:46 +1100 Subject: [PATCH 16/23] Improve name of source tests. --- tests/php/HooksTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/php/HooksTest.php b/tests/php/HooksTest.php index 10daa90dc..8932ad5dd 100644 --- a/tests/php/HooksTest.php +++ b/tests/php/HooksTest.php @@ -97,7 +97,7 @@ public function setup_post_meta_mock( $post_meta ) { * * @since x.x.x */ - public function test_get_canonical_url() { + public function test_get_canonical_url_source() { $this->setup_post_mock(); $this->setup_post_meta_mock(array()); @@ -234,7 +234,7 @@ public function test_get_canonical_url_internal() { * * @since x.x.x */ - public function test_wpseo_canonical() { + public function test_wpseo_canonical_source() { $this->setup_post_mock(); $this->setup_post_meta_mock(array()); From a984fa5e5432dd50b2bf933723e938586ec983d7 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Tue, 20 Dec 2022 15:31:01 +1100 Subject: [PATCH 17/23] Tests for wpseo_opengraph_url --- tests/php/HooksTest.php | 137 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) diff --git a/tests/php/HooksTest.php b/tests/php/HooksTest.php index 8932ad5dd..37fcb5b74 100644 --- a/tests/php/HooksTest.php +++ b/tests/php/HooksTest.php @@ -365,4 +365,141 @@ public function test_wpseo_canonical_internal() { $actual = Hooks\wpseo_canonical( 'https://example.com/?p=1' ); $this->assertSame( 'http://origin.example.org/?p=12', $actual ); } + + /** + * Test wpseo_opengraph_url + * + * @since x.x.x + */ + public function test_wpseo_opengraph_url_source() { + $this->setup_post_mock(); + $this->setup_post_meta_mock(array()); + + \WP_Mock::userFunction( + 'get_permalink', + array( + 'return' => 'https://example.com/?p=1', + ) + ); + + \WP_Mock::userFunction( + 'is_singular', + array( + 'return' => true, + ) + ); + + $actual = Hooks\wpseo_opengraph_url( 'https://example.com/?p=1' ); + $this->assertSame( 'https://example.com/?p=1', $actual ); + } + + /** + * Test wpseo_opengraph_url + * + * @since x.x.x + */ + public function test_wpseo_opengraph_url_external_pushed() { + $this->setup_post_mock(); + $this->setup_post_meta_mock( + array ( + 'dt_original_post_id' => array( '10' ), + 'dt_original_site_name' => array( 'Test External, Pushed Origin' ), + 'dt_original_site_url' => array( 'http://origin.example.org/' ), + 'dt_original_post_url' => array( 'http://origin.example.org/?p=10' ), + 'dt_subscription_signature' => array( 'abcdefghijklmnopqrstuvwxyz' ), + 'dt_syndicate_time' => array( '1670384223' ), + 'dt_full_connection' => array( '1' ), + 'dt_original_source_id' => array( '2' ), + ) + ); + + \WP_Mock::userFunction( + 'get_permalink', + array( + 'return' => 'https://example.com/?p=1', + ) + ); + + \WP_Mock::userFunction( + 'is_singular', + array( + 'return' => true, + ) + ); + + $actual = Hooks\wpseo_opengraph_url( 'https://example.com/?p=1' ); + $this->assertSame( 'https://example.com/?p=1', $actual ); + } + + /** + * Test wpseo_opengraph_url + * + * @since x.x.x + */ + public function test_wpseo_opengraph_url_external_pulled() { + $this->setup_post_mock(); + $this->setup_post_meta_mock( + array ( + 'dt_original_post_id' => array( '10' ), + 'dt_original_site_name' => array( 'Test External, Pulled Origin' ), + 'dt_original_site_url' => array( 'http://origin.example.org/' ), + 'dt_original_post_url' => array( 'http://origin.example.org/?p=11' ), + 'dt_subscription_signature' => array( 'abcdefghijklmnopqrstuvwxyz' ), + 'dt_syndicate_time' => array( '1670384223' ), + 'dt_full_connection' => array( '' ), + 'dt_original_source_id' => array( '3' ), + ) + ); + + \WP_Mock::userFunction( + 'get_permalink', + array( + 'return' => 'https://example.com/?p=1', + ) + ); + + \WP_Mock::userFunction( + 'is_singular', + array( + 'return' => true, + ) + ); + + $actual = Hooks\wpseo_opengraph_url( 'https://example.com/?p=1' ); + $this->assertSame( 'https://example.com/?p=1', $actual ); + } + + /** + * Test wpseo_opengraph_url + * + * @since x.x.x + */ + public function test_wpseo_opengraph_url_internal() { + $this->setup_post_mock(); + $this->setup_post_meta_mock( + array ( + 'dt_original_post_id' => array( '10' ), + 'dt_original_blog_id' => array( '2' ), + 'dt_syndicate_time' => array ( '1670383190' ), + 'dt_original_post_url' => array ( 'http://origin.example.org/?p=12' ), + ) + ); + + \WP_Mock::userFunction( + 'get_permalink', + array( + 'return' => 'https://example.com/?p=1', + ) + ); + + \WP_Mock::userFunction( + 'is_singular', + array( + 'return' => true, + ) + ); + + $actual = Hooks\wpseo_opengraph_url( 'https://example.com/?p=1' ); + $this->assertSame( 'https://example.com/?p=1', $actual ); + } } From 51f5603000dd3f1a4b5bca0f9d9f62d68c7bfaed Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Tue, 20 Dec 2022 15:44:34 +1100 Subject: [PATCH 18/23] Mock wp_parse_args as wrapper for array_merge. --- tests/php/includes/common.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/php/includes/common.php b/tests/php/includes/common.php index 1fc4b1b0d..d0c59f04f 100644 --- a/tests/php/includes/common.php +++ b/tests/php/includes/common.php @@ -263,6 +263,19 @@ function wp_json_encode( $data, $options = 0, $depth = 512 ) { return json_encode( $data, $options, $depth ); } +/** + * Mock wp_parse_args() function. + * + * @since x.x.x + * + * @param array $settings Array of arguments. + * @param array $defaults Array of default arguments. + * @return array Array of parsed arguments. + */ +function wp_parse_args( $settings, $defaults ) { + return array_merge( $defaults, $settings ); +} + /** * Stub for remove_filter to avoid failure in test_remote_get() * From 23f8c624fa0d643abc9944ccbc225a6760b03beb Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Tue, 20 Dec 2022 15:44:43 +1100 Subject: [PATCH 19/23] filter_the_author tests. --- tests/php/HooksTest.php | 196 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 196 insertions(+) diff --git a/tests/php/HooksTest.php b/tests/php/HooksTest.php index 37fcb5b74..075919469 100644 --- a/tests/php/HooksTest.php +++ b/tests/php/HooksTest.php @@ -502,4 +502,200 @@ public function test_wpseo_opengraph_url_internal() { $actual = Hooks\wpseo_opengraph_url( 'https://example.com/?p=1' ); $this->assertSame( 'https://example.com/?p=1', $actual ); } + + /** + * Test filter_the_author + * + * @since x.x.x + */ + public function test_filter_the_author_source() { + $this->setup_post_mock(); + $this->setup_post_meta_mock(array()); + + \WP_Mock::userFunction( + 'get_permalink', + array( + 'return' => 'https://example.com/?p=1', + ) + ); + + \WP_Mock::userFunction( + 'is_singular', + array( + 'return' => true, + ) + ); + + \WP_Mock::userFunction( + 'get_option', + array( + 'return' => array(), + ) + ); + + $actual = Hooks\filter_the_author( 'Alexander Hamilton' ); + $this->assertSame( 'Alexander Hamilton', $actual ); + } + + /** + * Test filter_the_author + * + * @since x.x.x + */ + public function test_filter_the_author_external_pushed() { + $this->setup_post_mock(); + $this->setup_post_meta_mock( + array ( + 'dt_original_post_id' => array( '10' ), + 'dt_original_site_name' => array( 'Test External, Pushed Origin' ), + 'dt_original_site_url' => array( 'http://origin.example.org/' ), + 'dt_original_post_url' => array( 'http://origin.example.org/?p=10' ), + 'dt_subscription_signature' => array( 'abcdefghijklmnopqrstuvwxyz' ), + 'dt_syndicate_time' => array( '1670384223' ), + 'dt_full_connection' => array( '1' ), + 'dt_original_source_id' => array( '2' ), + ) + ); + + \WP_Mock::userFunction( + 'get_permalink', + array( + 'return' => 'https://example.com/?p=1', + ) + ); + + \WP_Mock::userFunction( + 'is_singular', + array( + 'return' => true, + ) + ); + + \WP_Mock::userFunction( + 'get_option', + array( + 'return' => array(), + ) + ); + + $actual = Hooks\filter_the_author( 'George Washington' ); + $this->assertSame( 'Test External, Pushed Origin', $actual ); + } + + /** + * Test filter_the_author + * + * @since x.x.x + */ + public function test_filter_the_author_external_pulled() { + $this->setup_post_mock(); + $this->setup_post_meta_mock( + array ( + 'dt_original_post_id' => array( '10' ), + 'dt_original_site_name' => array( 'Test External, Pulled Origin' ), + 'dt_original_site_url' => array( 'http://origin.example.org/' ), + 'dt_original_post_url' => array( 'http://origin.example.org/?p=11' ), + 'dt_subscription_signature' => array( 'abcdefghijklmnopqrstuvwxyz' ), + 'dt_syndicate_time' => array( '1670384223' ), + 'dt_full_connection' => array( '' ), + 'dt_original_source_id' => array( '3' ), + ) + ); + + \WP_Mock::userFunction( + 'get_permalink', + array( + 'return' => 'https://example.com/?p=1', + ) + ); + + \WP_Mock::userFunction( + 'is_singular', + array( + 'return' => true, + ) + ); + + \WP_Mock::userFunction( + 'get_option', + array( + 'return' => array(), + ) + ); + + $actual = Hooks\filter_the_author( 'James Madison' ); + $this->assertSame( 'Test External, Pulled Origin', $actual ); + } + + /** + * Test filter_the_author + * + * @since x.x.x + */ + public function test_filter_the_author_internal() { + $this->setup_post_mock(); + $this->setup_post_meta_mock( + array ( + 'dt_original_post_id' => array( '10' ), + 'dt_original_blog_id' => array( '2' ), + 'dt_syndicate_time' => array ( '1670383190' ), + 'dt_original_post_url' => array ( 'http://origin.example.org/?p=12' ), + ) + ); + + \WP_Mock::userFunction( + 'is_singular', + array( + 'return' => true, + ) + ); + + \WP_Mock::userFunction( + 'get_option', + array( + 'return' => array(), + ) + ); + + \WP_Mock::userFunction( + 'get_current_blog_id', + array( + 'return' => 1, + ) + ); + \WP_Mock::userFunction( 'switch_to_blog' ); + \WP_Mock::userFunction( 'restore_current_blog' ); + + \WP_Mock::userFunction( + 'get_bloginfo', + array( + 'return' => function( $info ) { + switch ( $info ) { + case 'name': + return 'Test Internal Origin'; + default: + return ''; + } + }, + ) + ); + + // Generic values for the origin site. + \WP_Mock::userFunction( + 'get_permalink', + array( + 'return' => 'http://origin.example.org/?p=10', + ) + ); + + \WP_Mock::userFunction( + 'home_url', + array( + 'return' => 'http://origin.example.org/', + ) + ); + + $actual = Hooks\filter_the_author( 'Aaron Burr' ); + $this->assertSame( 'Test Internal Origin', $actual ); + } } From 01d12b05b410856f6fad75a870aee42cad2ad854 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Tue, 20 Dec 2022 16:00:02 +1100 Subject: [PATCH 20/23] Test get_the_author_display_name --- tests/php/HooksTest.php | 208 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 208 insertions(+) diff --git a/tests/php/HooksTest.php b/tests/php/HooksTest.php index 075919469..dced26b91 100644 --- a/tests/php/HooksTest.php +++ b/tests/php/HooksTest.php @@ -698,4 +698,212 @@ public function test_filter_the_author_internal() { $actual = Hooks\filter_the_author( 'Aaron Burr' ); $this->assertSame( 'Test Internal Origin', $actual ); } + + /** + * Test get_the_author_display_name + * + * @since x.x.x + */ + public function test_get_the_author_display_name_source() { + $this->setup_post_mock(); + $this->setup_post_meta_mock(array()); + + \WP_Mock::userFunction( + 'get_permalink', + array( + 'return' => 'https://example.com/?p=1', + ) + ); + + \WP_Mock::userFunction( + 'is_singular', + array( + 'return' => true, + ) + ); + + \WP_Mock::userFunction( + 'get_option', + array( + 'return' => array(), + ) + ); + + $actual = Hooks\get_the_author_display_name( 'Alexander Hamilton', 1, false ); + $this->assertSame( 'Alexander Hamilton', $actual, 'Unexpected value when 1 current post author.' ); + + $actual = Hooks\get_the_author_display_name( 'Alexander Hamilton', 1, 1 ); + $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_external_pushed() { + $this->setup_post_mock(); + $this->setup_post_meta_mock( + array ( + 'dt_original_post_id' => array( '10' ), + 'dt_original_site_name' => array( 'Test External, Pushed Origin' ), + 'dt_original_site_url' => array( 'http://origin.example.org/' ), + 'dt_original_post_url' => array( 'http://origin.example.org/?p=10' ), + 'dt_subscription_signature' => array( 'abcdefghijklmnopqrstuvwxyz' ), + 'dt_syndicate_time' => array( '1670384223' ), + 'dt_full_connection' => array( '1' ), + 'dt_original_source_id' => array( '2' ), + ) + ); + + \WP_Mock::userFunction( + 'get_permalink', + array( + 'return' => 'https://example.com/?p=1', + ) + ); + + \WP_Mock::userFunction( + 'is_singular', + array( + 'return' => true, + ) + ); + + \WP_Mock::userFunction( + 'get_option', + array( + 'return' => array(), + ) + ); + + $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.' ); + } + + /** + * Test get_the_author_display_name + * + * @since x.x.x + */ + public function test_get_the_author_display_name_external_pulled() { + $this->setup_post_mock(); + $this->setup_post_meta_mock( + array ( + 'dt_original_post_id' => array( '10' ), + 'dt_original_site_name' => array( 'Test External, Pulled Origin' ), + 'dt_original_site_url' => array( 'http://origin.example.org/' ), + 'dt_original_post_url' => array( 'http://origin.example.org/?p=11' ), + 'dt_subscription_signature' => array( 'abcdefghijklmnopqrstuvwxyz' ), + 'dt_syndicate_time' => array( '1670384223' ), + 'dt_full_connection' => array( '' ), + 'dt_original_source_id' => array( '3' ), + ) + ); + + \WP_Mock::userFunction( + 'get_permalink', + array( + 'return' => 'https://example.com/?p=1', + ) + ); + + \WP_Mock::userFunction( + 'is_singular', + array( + 'return' => true, + ) + ); + + \WP_Mock::userFunction( + 'get_option', + array( + 'return' => array(), + ) + ); + + $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.' ); + } + + /** + * Test get_the_author_display_name + * + * @since x.x.x + */ + public function test_get_the_author_display_name_internal() { + $this->setup_post_mock(); + $this->setup_post_meta_mock( + array ( + 'dt_original_post_id' => array( '10' ), + 'dt_original_blog_id' => array( '2' ), + 'dt_syndicate_time' => array ( '1670383190' ), + 'dt_original_post_url' => array ( 'http://origin.example.org/?p=12' ), + ) + ); + + \WP_Mock::userFunction( + 'is_singular', + array( + 'return' => true, + ) + ); + + \WP_Mock::userFunction( + 'get_option', + array( + 'return' => array(), + ) + ); + + \WP_Mock::userFunction( + 'get_current_blog_id', + array( + 'return' => 1, + ) + ); + \WP_Mock::userFunction( 'switch_to_blog' ); + \WP_Mock::userFunction( 'restore_current_blog' ); + + \WP_Mock::userFunction( + 'get_bloginfo', + array( + 'return' => function( $info ) { + switch ( $info ) { + case 'name': + return 'Test Internal Origin'; + default: + return ''; + } + }, + ) + ); + + // Generic values for the origin site. + \WP_Mock::userFunction( + 'get_permalink', + array( + 'return' => 'http://origin.example.org/?p=10', + ) + ); + + \WP_Mock::userFunction( + 'home_url', + array( + 'return' => 'http://origin.example.org/', + ) + ); + + $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.' ); + } } From 4367aff1b17e110a03cb499668bbb07b3136ea52 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Wed, 21 Dec 2022 10:35:13 +1100 Subject: [PATCH 21/23] Move settings check to DistributorPost methods. --- includes/classes/DistributorPost.php | 10 ++++++++-- includes/hooks.php | 12 ------------ 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/includes/classes/DistributorPost.php b/includes/classes/DistributorPost.php index 81bc999b2..a5c396ec1 100644 --- a/includes/classes/DistributorPost.php +++ b/includes/classes/DistributorPost.php @@ -395,8 +395,11 @@ public function get_canonical_url( $canonical_url = '' ) { * @return string The post's author name. */ public function get_author_name( $author_name = '' ) { + $settings = Utils\get_settings(); + if ( - $this->is_source + empty( $settings['override_author_byline'] ) + || $this->is_source || $this->original_deleted || ! $this->is_linked || ! $this->connection_id @@ -423,8 +426,11 @@ public function get_author_name( $author_name = '' ) { * @return string The post's author link. */ public function get_author_link( $author_link = '' ) { + $settings = Utils\get_settings(); + if ( - $this->is_source + empty( $settings['override_author_byline'] ) + || $this->is_source || $this->original_deleted || ! $this->is_linked || ! $this->connection_id diff --git a/includes/hooks.php b/includes/hooks.php index d53a2b01b..0cba47591 100644 --- a/includes/hooks.php +++ b/includes/hooks.php @@ -107,12 +107,6 @@ function wpseo_opengraph_url( $og_url, $presentation = false ) { * @return string Modified author display name. */ function filter_the_author( $display_name ) { - $settings = Utils\get_settings(); - - if ( empty( $settings['override_author_byline'] ) ) { - return $display_name; - } - // Ensure there is a global post object. if ( ! get_post() ) { return $display_name; @@ -151,12 +145,6 @@ function get_the_author_display_name( $display_name, $user_id, $original_user_id * @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; From 6b55dc33ebccf409e5e8a9a901d50fe2de564653 Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Wed, 21 Dec 2022 10:50:35 +1100 Subject: [PATCH 22/23] Mock get_option() testing methods. --- tests/php/DistributorPostTest.php | 42 +++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/php/DistributorPostTest.php b/tests/php/DistributorPostTest.php index c8e0020d8..0873d6af9 100644 --- a/tests/php/DistributorPostTest.php +++ b/tests/php/DistributorPostTest.php @@ -1712,6 +1712,13 @@ public function test_get_author_name() { ) ); + \WP_Mock::userFunction( + 'get_option', + array( + 'return' => array(), + ) + ); + $dt_post = new DistributorPost( 1 ); $author_name_actual = $dt_post->get_author_name(); $author_name_expected = 'Test External, Pushed Origin'; @@ -1736,6 +1743,13 @@ public function test_get_author_name_unlinked() { ) ); + \WP_Mock::userFunction( + 'get_option', + array( + 'return' => array(), + ) + ); + \WP_Mock::userFunction( 'get_the_author_meta', array( @@ -1759,6 +1773,13 @@ public function test_get_author_name_source() { $this->setup_post_mock(); $this->setup_post_meta_mock( array() ); + \WP_Mock::userFunction( + 'get_option', + array( + 'return' => array(), + ) + ); + \WP_Mock::userFunction( 'get_permalink', array( @@ -1800,6 +1821,13 @@ public function test_get_author_link() { ) ); + \WP_Mock::userFunction( + 'get_option', + array( + 'return' => array(), + ) + ); + $dt_post = new DistributorPost( 1 ); $author_link_actual = $dt_post->get_author_link(); $author_link_expected = 'http://origin.example.org/'; @@ -1824,6 +1852,13 @@ public function test_get_author_link_unlinked() { ) ); + \WP_Mock::userFunction( + 'get_option', + array( + 'return' => array(), + ) + ); + \WP_Mock::userFunction( 'get_author_posts_url', array( @@ -1854,6 +1889,13 @@ public function test_get_author_link_source() { ) ); + \WP_Mock::userFunction( + 'get_option', + array( + 'return' => array(), + ) + ); + \WP_Mock::userFunction( 'get_author_posts_url', array( From 161f7b84caa1ead49c11cef927b123e79aaf294c Mon Sep 17 00:00:00 2001 From: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> Date: Wed, 21 Dec 2022 11:02:12 +1100 Subject: [PATCH 23/23] Add settings based tests for author getters. --- tests/php/DistributorPostTest.php | 92 +++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/tests/php/DistributorPostTest.php b/tests/php/DistributorPostTest.php index 0873d6af9..5f71546fa 100644 --- a/tests/php/DistributorPostTest.php +++ b/tests/php/DistributorPostTest.php @@ -1726,6 +1726,52 @@ public function test_get_author_name() { $this->assertSame( $author_name_expected, $author_name_actual ); } + /** + * Test get_author_name() method. + * + * @covers ::get_author_name() + */ + public function test_get_author_name_without_override() { + $this->setup_post_mock(); + $this->setup_post_meta_mock( + array ( + 'dt_original_post_id' => array( '10' ), + 'dt_original_site_name' => array( 'Test External, Pushed Origin' ), + 'dt_original_site_url' => array( 'http://origin.example.org/' ), + 'dt_original_post_url' => array( 'http://origin.example.org/?p=10' ), + 'dt_subscription_signature' => array( 'abcdefghijklmnopqrstuvwxyz' ), + 'dt_syndicate_time' => array( '1670384223' ), + 'dt_full_connection' => array( '1' ), + 'dt_original_source_id' => array( '2' ), + ) + ); + + \WP_Mock::userFunction( + 'get_option', + array( + 'return' => array( 'override_author_byline' => false ), + ) + ); + + \WP_Mock::userFunction( + 'get_the_author_meta', + array( + 'return' => 'Distributed site author name', + ) + ); + + $dt_post = new DistributorPost( 1 ); + $author_name_actual = $dt_post->get_author_name(); + $author_name_expected = 'Distributed site author name'; + + $this->assertSame( $author_name_expected, $author_name_actual, 'Author name expected to point to distributed site.' ); + + $author_name_actual = $dt_post->get_author_name( 'Filtered author name' ); + $author_name_expected = 'Filtered author name'; + + $this->assertSame( $author_name_expected, $author_name_actual, 'Author name expected to use filtered value.' ); + } + /** * Test get_author_name() method. * @@ -1835,6 +1881,52 @@ public function test_get_author_link() { $this->assertSame( $author_link_expected, $author_link_actual ); } + /** + * Test get_author_link() method. + * + * @covers ::get_author_link() + */ + public function test_get_author_link_without_override() { + $this->setup_post_mock(); + $this->setup_post_meta_mock( + array ( + 'dt_original_post_id' => array( '10' ), + 'dt_original_site_name' => array( 'Test External, Pushed Origin' ), + 'dt_original_site_url' => array( 'http://origin.example.org/' ), + 'dt_original_post_url' => array( 'http://origin.example.org/?p=10' ), + 'dt_subscription_signature' => array( 'abcdefghijklmnopqrstuvwxyz' ), + 'dt_syndicate_time' => array( '1670384223' ), + 'dt_full_connection' => array( '1' ), + 'dt_original_source_id' => array( '2' ), + ) + ); + + \WP_Mock::userFunction( + 'get_option', + array( + 'return' => array( 'override_author_byline' => false ), + ) + ); + + \WP_Mock::userFunction( + 'get_author_posts_url', + array( + 'return' => 'http://destination.example.org/author/author-name/', + ) + ); + + $dt_post = new DistributorPost( 1 ); + $author_link_actual = $dt_post->get_author_link(); + $author_link_expected = 'http://destination.example.org/author/author-name/'; + + $this->assertSame( $author_link_expected, $author_link_actual, 'Author link should be the local author link' ); + + $author_link_actual = $dt_post->get_author_link( 'http://pre-filtered.example.org/' ); + $author_link_expected = 'http://pre-filtered.example.org/'; + + $this->assertSame( $author_link_expected, $author_link_actual, 'Author link should be use prefiltered value.' ); + } + /** * Test get_author_link() method. *