Skip to content

Commit

Permalink
Remove dependency on widget containers having class names
Browse files Browse the repository at this point in the history
Also make use of add_amp_action
  • Loading branch information
westonruter committed Apr 3, 2020
1 parent 32ccd6a commit bdeb953
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
36 changes: 14 additions & 22 deletions includes/embeds/class-amp-core-block-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,26 +201,22 @@ public function sanitize_raw_embeds( Document $dom ) {
private function process_categories_widgets( Document $dom ) {
static $count = 0;

$widget_containers = $dom->xpath->query( '//*[ contains( @class, "widget_categories" ) ]' );
foreach ( $widget_containers as $widget_container ) {
if ( ! $widget_container instanceof DOMElement ) {
$selects = $dom->xpath->query( '//form/select[ @name = "cat" ]' );
foreach ( $selects as $select ) {
if ( ! $select instanceof DOMElement ) {
continue;
}
$select = $dom->xpath->query( './/select[ @name = "cat" ]', $widget_container )->item( 0 );
$form = $widget_container->getElementsByTagName( 'form' )->item( 0 );
if ( ! $select instanceof DOMElement || ! $form instanceof DOMElement ) {
$script = $dom->xpath->query( './/script[ contains( text(), "onCatChange" ) ]', $select->parentNode->parentNode )->item( 0 );
if ( ! $script instanceof DOMElement ) {
continue;
}

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

$select->setAttribute( 'on', sprintf( 'change:%s.submit', $id ) );
$script = $dom->xpath->query( './/script[ contains( text(), "onCatChange" ) ]', $widget_container )->item( 0 );
if ( $script ) {
$script->parentNode->removeChild( $script );
}
AMP_DOM_Utils::add_amp_action( $select, 'change', sprintf( '%s.submit', $id ) );
$script->parentNode->removeChild( $script );
}
}

Expand All @@ -233,22 +229,18 @@ private function process_categories_widgets( Document $dom ) {
*/
private function process_archives_widgets( Document $dom ) {

$widget_containers = $dom->xpath->query( '//*[ contains( @class, "widget_archive" ) ]' );
foreach ( $widget_containers as $widget_container ) {
if ( ! $widget_container instanceof DOMElement ) {
$selects = $dom->xpath->query( '//select[ @name = "archive-dropdown" and starts-with( @id, "archives-dropdown-" ) ]' );
foreach ( $selects as $select ) {
if ( ! $select instanceof DOMElement ) {
continue;
}
$select = $dom->xpath->query( './/select[ @name = "archive-dropdown" ]', $widget_container )->item( 0 );
if ( ! $select instanceof DOMElement ) {
$script = $dom->xpath->query( './/script[ contains( text(), "onSelectChange" ) ]', $select->parentNode )->item( 0 );
if ( ! $script instanceof DOMElement ) {
continue;
}

AMP_DOM_Utils::add_amp_action( $select, 'change', 'AMP.navigateTo(url=event.value)' );

$script = $dom->xpath->query( './/script[ contains( text(), "onSelectChange" ) ]', $widget_container )->item( 0 );
if ( $script instanceof DOMElement ) {
$script->parentNode->removeChild( $script );
}
$script->parentNode->removeChild( $script );
}
}
}
40 changes: 26 additions & 14 deletions tests/php/test-class-amp-core-block-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,19 @@ public function test_process_categories_widgets() {
$instance_count = 2;

ob_start();
for ( $i = 0; $i < $instance_count; $i++ ) {
the_widget(
'WP_Widget_Categories',
[ 'dropdown' => '1' ],
[]
);
}
the_widget(
'WP_Widget_Categories',
[ 'dropdown' => '1' ],
[]
);
the_widget(
'WP_Widget_Categories',
[ 'dropdown' => '1' ],
[
'before_widget' => '<section>',
'after_widget' => '</section>',
]
);
$html = ob_get_clean();

$dom = AMP_DOM_Utils::get_dom_from_content( $html );
Expand Down Expand Up @@ -255,13 +261,19 @@ public function test_process_archives_widgets() {
$instance_count = 2;

ob_start();
for ( $i = 0; $i < $instance_count; $i++ ) {
the_widget(
'WP_Widget_Archives',
[ 'dropdown' => '1' ],
[]
);
}
the_widget(
'WP_Widget_Archives',
[ 'dropdown' => '1' ],
[]
);
the_widget(
'WP_Widget_Archives',
[ 'dropdown' => '1' ],
[
'before_widget' => '<section>',
'after_widget' => '</section>',
]
);
$html = ob_get_clean();

$dom = AMP_DOM_Utils::get_dom_from_content( $html );
Expand Down

0 comments on commit bdeb953

Please sign in to comment.