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

Eliminate amphtml link relation from AMP-to-AMP linking #6661

Merged
merged 14 commits into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions includes/sanitizers/class-amp-link-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,16 @@ private function process_element( DOMElement $element, $attribute_name ) {

$query_vars = [];

// Add rel=amphtml.
if ( ! $excluded ) {
$rel[] = Attribute::REL_AMPHTML;
$rel = array_diff(
$rel = array_diff(
$rel,
[ Attribute::REL_NOAMPHTML ]
);
$element->setAttribute( Attribute::REL, implode( ' ', $rel ) );
if ( ! empty( $rel ) ) {
$element->setAttribute( Attribute::REL, implode( ' ', $rel ) );
} else {
$element->removeAttribute( Attribute::REL );
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/MobileRedirection.php
Original file line number Diff line number Diff line change
Expand Up @@ -553,11 +553,11 @@ public function add_mobile_version_switcher_link() {

$is_amp = amp_is_request();
if ( $is_amp ) {
$rel = [ Attribute::REL_NOAMPHTML, Attribute::REL_NOFOLLOW ];
$rel = [ Attribute::REL_NOFOLLOW ];
$url = add_query_arg( QueryVar::NOAMP, QueryVar::NOAMP_MOBILE, $this->paired_routing->remove_endpoint( amp_get_current_url() ) );
$text = __( 'Exit mobile version', 'amp' );
} else {
$rel = [ Attribute::REL_AMPHTML ];
$rel = [];
$url = $this->get_current_amp_url();
$text = __( 'Go to mobile version', 'amp' );
}
Expand Down
2 changes: 1 addition & 1 deletion tests/php/src/MobileRedirectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ public function test_add_mobile_version_switcher( $template_mode, $is_amp, $is_c

$url = $is_amp ? amp_get_permalink( $post_id ) : get_permalink( $post_id );

$link_rel = $is_amp ? 'noamphtml nofollow' : 'amphtml';
$link_rel = $is_amp ? 'nofollow' : '';

if ( $is_paired_browsing ) {
$this->register_paired_browsing_service();
Expand Down
34 changes: 18 additions & 16 deletions tests/php/test-class-amp-link-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,12 @@ public function get_amp_to_amp_navigation_data() {
*
* @dataProvider get_amp_to_amp_navigation_data
* @covers AMP_Link_Sanitizer::process_links()
* @covers AMP_Link_Sanitizer::process_element()
*
* @param bool $paired Paired.
*/
public function test_amp_to_amp_navigation( $paired ) {
// Enable pretty permalinks to keep the AMP slug as the only query var.
global $wp_rewrite;
update_option( 'permalink_structure', '/%postname%/' );
$wp_rewrite->use_trailing_slashes = true;
$wp_rewrite->init();
$wp_rewrite->flush_rules();
$this->set_permalink_structure( '/%postname%/' );

$post_link = get_permalink(
self::factory()->post->create(
Expand All @@ -59,18 +55,18 @@ public function test_amp_to_amp_navigation( $paired ) {
'home-link' => [
'href' => home_url( '/' ),
'expected_amp' => true,
'expected_rel' => 'amphtml',
'expected_rel' => null,
],
'internal-link' => [
'href' => $post_link,
'expected_amp' => true,
'expected_rel' => 'amphtml',
'expected_rel' => null,
],
'non_amp_to_amp_rel' => [
'href' => $post_link,
'expected_amp' => false,
'rel' => 'noamphtml',
'expected_rel' => null,
'expected_rel' => 'noamphtml',
],
'two_rel' => [
'href' => $post_link,
Expand All @@ -88,6 +84,12 @@ public function test_amp_to_amp_navigation( $paired ) {
'href' => $post_link,
'expected_amp' => false,
'rel' => 'noamphtml ',
'expected_rel' => 'noamphtml ',
],
'empty_rel' => [
'href' => $post_link,
'expected_amp' => true,
'rel' => '',
'expected_rel' => null,
],
'excluded_amp_link' => [
Expand All @@ -101,10 +103,10 @@ public function test_amp_to_amp_navigation( $paired ) {
'expected_rel' => null,
],
'ugc-link' => [
'rel' => 'ugc',
'rel' => 'ugc nofollow',
'href' => home_url( '/some/user/generated/data/' ),
'expected_amp' => true,
'expected_rel' => 'ugc amphtml',
'expected_rel' => 'ugc nofollow',
],
'page-anchor' => [
'href' => '#top',
Expand All @@ -114,7 +116,7 @@ public function test_amp_to_amp_navigation( $paired ) {
'other-page-anchor' => [
'href' => $post_link . '#top',
'expected_amp' => true,
'expected_rel' => 'amphtml',
'expected_rel' => null,
],
'external-link' => [
'href' => 'https://external.example.com/',
Expand All @@ -125,7 +127,7 @@ public function test_amp_to_amp_navigation( $paired ) {
'href' => 'https://external.example.com/',
'expected_amp' => false,
'rel' => 'noamphtml',
'expected_rel' => null,
'expected_rel' => 'noamphtml',
],
'php-file-link' => [
'href' => site_url( '/wp-login.php' ),
Expand Down Expand Up @@ -159,7 +161,7 @@ public function test_amp_to_amp_navigation( $paired ) {
$html = sprintf( '<div id="wpadminbar"><a id="admin-bar-link" href="%s"></a></div>', esc_url( $admin_bar_link_href ) );
foreach ( $links as $id => $link_data ) {
$html .= sprintf( '<a id="%s" href="%s"', esc_attr( $id ), esc_attr( $link_data['href'] ) );
if ( isset( $link_data['rel'] ) ) {
if ( ! empty( $link_data['rel'] ) ) {
$html .= sprintf( ' rel="%s"', esc_attr( $link_data['rel'] ) );
}
$html .= '>Link</a>';
Expand All @@ -184,10 +186,10 @@ public function test_amp_to_amp_navigation( $paired ) {
foreach ( $links as $id => $link_data ) {
$element = $dom->getElementById( $id );
$this->assertInstanceOf( 'DOMElement', $element, "ID: $id" );
$rel = (string) $element->getAttribute( 'rel' );
if ( empty( $link_data['expected_rel'] ) ) {
$this->assertDoesNotMatchRegularExpression( '/(^|\s)amphtml(\s|$)/', $rel, "ID: $id" );
$this->assertFalse( $element->hasAttribute( 'rel' ), "ID: $id" );
} else {
$this->assertTrue( $element->hasAttribute( 'rel' ), "ID: $id" );
$this->assertEquals( $link_data['expected_rel'], $element->getAttribute( 'rel' ), "ID: $id" );
}

Expand Down