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

Use post abstraction to override content. #988

Merged
merged 23 commits into from
Jan 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
479473b
Add original_deleted property.
peterwilsoncc Dec 19, 2022
f970b49
Canonical URL getter in Distributor post object.
peterwilsoncc Dec 19, 2022
94743a6
Move canonical filters to single location.
peterwilsoncc Dec 19, 2022
2f34857
Filter Yoast SEO open-graph URL.
peterwilsoncc Dec 19, 2022
cead7a6
Account for legacy format Yoast filters.
peterwilsoncc Dec 19, 2022
6b1ce5a
Author display name overrides.
peterwilsoncc Dec 19, 2022
a82444a
Filter the author URLs.
peterwilsoncc Dec 20, 2022
c8fca2a
Add tests for canonical method.
peterwilsoncc Dec 20, 2022
2f15f8d
Tests for get_author_name() method.
peterwilsoncc Dec 20, 2022
d3166c5
Test get author link.
peterwilsoncc Dec 20, 2022
36cb272
Deprecate canonicalization functions in `WordPressExternalConnection`.
peterwilsoncc Dec 20, 2022
09abc9d
Deprecate canonicalization functions in `NetworkSiteConnection`.
peterwilsoncc Dec 20, 2022
fc19f05
CS Fix.
peterwilsoncc Dec 20, 2022
12677ad
Tests for the internal hooks functions.
peterwilsoncc Dec 20, 2022
b1bf3e1
Test WP SEO Canonical filters.
peterwilsoncc Dec 20, 2022
62bfe08
Improve name of source tests.
peterwilsoncc Dec 20, 2022
a984fa5
Tests for wpseo_opengraph_url
peterwilsoncc Dec 20, 2022
51f5603
Mock wp_parse_args as wrapper for array_merge.
peterwilsoncc Dec 20, 2022
23f8c62
filter_the_author tests.
peterwilsoncc Dec 20, 2022
01d12b0
Test get_the_author_display_name
peterwilsoncc Dec 20, 2022
4367aff
Move settings check to DistributorPost methods.
peterwilsoncc Dec 20, 2022
6b55dc3
Mock get_option() testing methods.
peterwilsoncc Dec 20, 2022
161f7b8
Add settings based tests for author getters.
peterwilsoncc Dec 21, 2022
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
2 changes: 2 additions & 0 deletions includes/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
Expand Down
99 changes: 99 additions & 0 deletions includes/classes/DistributorPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}

Expand All @@ -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 ) ) {
Expand Down Expand Up @@ -347,6 +356,96 @@ 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 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 = '' ) {
$settings = Utils\get_settings();

if (
empty( $settings['override_author_byline'] )
|| $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 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 = '' ) {
$settings = Utils\get_settings();

if (
empty( $settings['override_author_byline'] )
|| $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
128 changes: 34 additions & 94 deletions includes/classes/ExternalConnections/WordPressExternalConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
Loading