Skip to content

Commit

Permalink
Add missing AMP-to-AMP linking for Archives widget
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Apr 3, 2020
1 parent bdeb953 commit b6c6800
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
4 changes: 3 additions & 1 deletion includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1207,7 +1207,9 @@ function amp_get_content_sanitizers( $post = null ) {
'AMP_O2_Player_Sanitizer' => [],
'AMP_Audio_Sanitizer' => [],
'AMP_Playbuzz_Sanitizer' => [],
'AMP_Embed_Sanitizer' => [],
'AMP_Embed_Sanitizer' => [
'amp_to_amp_linking_enabled' => $amp_to_amp_linking_enabled,
],
'AMP_Iframe_Sanitizer' => [
'add_placeholder' => true,
'current_origin' => $current_origin,
Expand Down
33 changes: 26 additions & 7 deletions includes/embeds/class-amp-core-block-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,12 @@ public function ampify_cover_block( $block_content, $block ) {
/**
* Sanitize widgets that are not added via Gutenberg.
*
* @param Document $dom Document.
* @param Document $dom Document.
* @param array $args Args passed to sanitizer.
*/
public function sanitize_raw_embeds( Document $dom ) {
public function sanitize_raw_embeds( Document $dom, $args = [] ) {
$this->process_categories_widgets( $dom );
$this->process_archives_widgets( $dom );
$this->process_archives_widgets( $dom, $args );
}

/**
Expand All @@ -206,14 +207,19 @@ private function process_categories_widgets( Document $dom ) {
if ( ! $select instanceof DOMElement ) {
continue;
}
$script = $dom->xpath->query( './/script[ contains( text(), "onCatChange" ) ]', $select->parentNode->parentNode )->item( 0 );
$form = $select->parentNode;
if ( ! $form instanceof DOMElement || ! $form->parentNode instanceof DOMElement ) {
continue;
}
$script = $dom->xpath->query( './/script[ contains( text(), "onCatChange" ) ]', $form->parentNode )->item( 0 );
if ( ! $script instanceof DOMElement ) {
continue;
}

$count++;
$id = sprintf( 'amp-wp-widget-categories-%d', $count );
$select->parentNode->setAttribute( 'id', $id );

$form->setAttribute( 'id', $id );

AMP_DOM_Utils::add_amp_action( $select, 'change', sprintf( '%s.submit', $id ) );
$script->parentNode->removeChild( $script );
Expand All @@ -225,9 +231,10 @@ private function process_categories_widgets( Document $dom ) {
*
* @since 1.5.2
*
* @param Document $dom Select node retrieved from the widget.
* @param Document $dom Select node retrieved from the widget.
* @param array $args Args passed to sanitizer.
*/
private function process_archives_widgets( Document $dom ) {
private function process_archives_widgets( Document $dom, $args = [] ) {

$selects = $dom->xpath->query( '//select[ @name = "archive-dropdown" and starts-with( @id, "archives-dropdown-" ) ]' );
foreach ( $selects as $select ) {
Expand All @@ -241,6 +248,18 @@ private function process_archives_widgets( Document $dom ) {

AMP_DOM_Utils::add_amp_action( $select, 'change', 'AMP.navigateTo(url=event.value)' );
$script->parentNode->removeChild( $script );

// When AMP-to-AMP linking is enabled, ensure links go to the AMP version.
if ( ! empty( $args['amp_to_amp_linking_enabled'] ) ) {
foreach ( $dom->xpath->query( '//option[ @value != "" ]', $select ) as $option ) {
/**
* Option element.
*
* @var DOMElement $option
*/
$option->setAttribute( 'value', add_query_arg( amp_get_slug(), '', $option->getAttribute( 'value' ) ) );
}
}
}
}
}
2 changes: 1 addition & 1 deletion includes/sanitizers/class-amp-embed-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function sanitize() {

foreach ( $this->embed_handlers as $embed_handler ) {
if ( method_exists( $embed_handler, 'sanitize_raw_embeds' ) ) {
$embed_handler->sanitize_raw_embeds( $this->dom );
$embed_handler->sanitize_raw_embeds( $this->dom, $this->args );
}
}
}
Expand Down
19 changes: 18 additions & 1 deletion tests/php/test-class-amp-core-block-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ public function test_process_categories_widgets() {
*/
public function test_process_archives_widgets() {
$instance_count = 2;
$this->factory()->post->create( [ 'post_date' => '2010-01-01 01:01:01' ] );

ob_start();
the_widget(
Expand Down Expand Up @@ -290,7 +291,7 @@ public function test_process_archives_widgets() {

$embed = new AMP_Core_Block_Handler();
$embed->register_embed();
$embed->sanitize_raw_embeds( $dom );
$embed->sanitize_raw_embeds( $dom, [ 'amp_to_amp_linking_enabled' => true ] );

$error_count = 0;
$sanitizer = new AMP_Tag_And_Attribute_Sanitizer(
Expand All @@ -310,6 +311,22 @@ public function test_process_archives_widgets() {
foreach ( $selects as $select ) {
$this->assertTrue( $select->hasAttribute( 'on' ) );
$this->assertEquals( 'change:AMP.navigateTo(url=event.value)', $select->getAttribute( 'on' ) );

$options = $dom->xpath->query( '//option[ @value != "" ]', $select );

$this->assertGreaterThan( 0, $options->length );
foreach ( $options as $option ) {
/**
* Option element.
*
* @var DOMElement $option
*/
$query = wp_parse_url( $option->getAttribute( 'value' ), PHP_URL_QUERY );
$this->assertNotEmpty( $query );
$query_vars = [];
wp_parse_str( $query, $query_vars );
$this->assertArrayHasKey( amp_get_slug(), $query_vars );
}
}
}
}

0 comments on commit b6c6800

Please sign in to comment.