-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(content-distribution): canonical url (#177)
- Loading branch information
1 parent
876351a
commit 5ca60ce
Showing
10 changed files
with
326 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
/** | ||
* Newspack Content Distribution Canonical URL Handler | ||
* | ||
* @package Newspack | ||
*/ | ||
|
||
namespace Newspack_Network\Content_Distribution; | ||
|
||
use Newspack_Network\Content_Distribution; | ||
|
||
/** | ||
* Class to filter the canonical URLs for distributed content. | ||
*/ | ||
class Canonical_Url { | ||
|
||
const OPTION_NAME = 'newspack_network_canonical_url'; | ||
|
||
/** | ||
* Initialize hooks. | ||
*/ | ||
public static function init() { | ||
add_filter( 'get_canonical_url', array( __CLASS__, 'filter_canonical_url' ), 10, 2 ); | ||
add_filter( 'wpseo_canonical', array( __CLASS__, 'wpseo_canonical_url' ), 10 ); | ||
} | ||
|
||
/** | ||
* Filters the canonical URL for distributed content. | ||
* | ||
* @param string $canonical_url Canonical URL. | ||
* @param object $post Post object. | ||
* | ||
* @return string | ||
*/ | ||
public static function filter_canonical_url( $canonical_url, $post ) { | ||
if ( ! Content_Distribution::is_post_incoming( $post ) ) { | ||
return $canonical_url; | ||
} | ||
|
||
$incoming_post = new Incoming_Post( $post->ID ); | ||
|
||
if ( ! $incoming_post->is_linked() ) { | ||
return $canonical_url; | ||
} | ||
|
||
$canonical_url = $incoming_post->get_original_post_url(); | ||
|
||
$base_url = get_option( self::OPTION_NAME, '' ); | ||
if ( $base_url ) { | ||
$canonical_url = str_replace( $incoming_post->get_original_site_url(), $base_url, $canonical_url ); | ||
} | ||
|
||
return $canonical_url; | ||
} | ||
|
||
/** | ||
* Handles the canonical URL change for distributed content when Yoast SEO is in use. | ||
* | ||
* @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; | ||
} | ||
|
||
return self::filter_canonical_url( $canonical_url, $post ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
/** | ||
* Class TestContentDistributionAdmin | ||
* | ||
* @package Newspack_Network | ||
*/ | ||
|
||
namespace Test\Content_Distribution; | ||
|
||
use Newspack_Network\Content_Distribution\Admin; | ||
|
||
/** | ||
* Test the Content Distribution Admin class. | ||
*/ | ||
class TestAdmin extends \WP_UnitTestCase { | ||
/** | ||
* Test default roles option value. | ||
*/ | ||
public function test_default_roles_options() { | ||
$roles = get_option( Admin::CAPABILITY_ROLES_OPTION_NAME ); | ||
$this->assertNotEmpty( $roles ); | ||
$this->assertContains( 'administrator', $roles ); | ||
$this->assertContains( 'editor', $roles ); | ||
$this->assertContains( 'author', $roles ); | ||
} | ||
|
||
/** | ||
* Test default roles capability. | ||
*/ | ||
public function test_default_roles_capability() { | ||
$default_roles = get_option( Admin::CAPABILITY_ROLES_OPTION_NAME ); | ||
$all_roles = wp_roles(); | ||
foreach ( $all_roles->roles as $role_key => $role ) { | ||
$role_obj = get_role( $role_key ); | ||
if ( in_array( $role_key, $default_roles, true ) ) { | ||
$this->assertTrue( $role_obj->has_cap( Admin::CAPABILITY ) ); | ||
} else { | ||
$this->assertFalse( $role_obj->has_cap( Admin::CAPABILITY ) ); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Test updating roles. | ||
*/ | ||
public function test_update_roles() { | ||
$roles = get_option( Admin::CAPABILITY_ROLES_OPTION_NAME ); | ||
$roles[] = 'contributor'; | ||
update_option( Admin::CAPABILITY_ROLES_OPTION_NAME, $roles ); | ||
|
||
$role_obj = get_role( 'contributor' ); | ||
$this->assertTrue( $role_obj->has_cap( Admin::CAPABILITY ) ); | ||
|
||
$roles = get_option( Admin::CAPABILITY_ROLES_OPTION_NAME ); | ||
$roles = array_diff( $roles, [ 'contributor' ] ); | ||
update_option( Admin::CAPABILITY_ROLES_OPTION_NAME, $roles ); | ||
|
||
$role_obj = get_role( 'contributor' ); | ||
$this->assertFalse( $role_obj->has_cap( Admin::CAPABILITY ) ); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
tests/unit-tests/content-distribution/test-canonical-url.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
/** | ||
* Class TestContentDistributionCanonicalUrl | ||
* | ||
* @package Newspack_Network | ||
*/ | ||
|
||
namespace Test\Content_Distribution; | ||
|
||
use Newspack_Network\Content_Distribution\Incoming_Post; | ||
|
||
/** | ||
* Test the Content Distribution Canonical URL class. | ||
*/ | ||
class TestCanonicalUrl extends \WP_UnitTestCase { | ||
/** | ||
* Test default canonical URL. | ||
*/ | ||
public function test_default_canonical_url() { | ||
$payload = get_sample_payload( '', get_bloginfo( 'url' ) ); | ||
$incoming_post = new Incoming_Post( $payload ); | ||
$post_id = $incoming_post->insert( $payload ); | ||
|
||
wp_publish_post( $post_id ); | ||
|
||
$this->assertEquals( $payload['post_url'], wp_get_canonical_url( get_post( $post_id ) ) ); | ||
} | ||
|
||
/** | ||
* Test custom canonical URL base. | ||
*/ | ||
public function test_custom_canonical_url() { | ||
update_option( 'newspack_network_canonical_url', 'https://custom.test' ); | ||
|
||
$payload = get_sample_payload( '', get_bloginfo( 'url' ) ); | ||
$incoming_post = new Incoming_Post( $payload ); | ||
$post_id = $incoming_post->insert( $payload ); | ||
|
||
wp_publish_post( $post_id ); | ||
|
||
$this->assertEquals( 'https://custom.test/2021/01/slug', wp_get_canonical_url( get_post( $post_id ) ) ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.