Skip to content

Commit 118e11a

Browse files
authored
AMP infinite scroll add hidden (#17778)
1 parent e649dab commit 118e11a

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

modules/infinite-scroll/class-jetpack-amp-infinite-scroll-sanitizer.php

+21-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ final class Jetpack_AMP_Infinite_Scroll_Sanitizer extends AMP_Base_Sanitizer {
1515
* @var array {
1616
* @type string $footer_xpaths
1717
* @type string[] $next_page_hide_xpaths
18+
* @type string[] $hidden_xpaths
1819
* }
1920
*/
2021
protected $args;
@@ -67,6 +68,7 @@ public function sanitize() {
6768
}
6869

6970
$this->hide_next_page_elements();
71+
$this->hide_hidden_elements();
7072
}
7173

7274
/**
@@ -83,7 +85,25 @@ private function hide_next_page_elements() {
8385
foreach ( $xpaths as $next_page_hide_xpath ) {
8486
/** @var DOMElement $element */
8587
foreach ( $this->xpath->query( $next_page_hide_xpath ) as $element ) {
86-
$element->setAttribute( 'next-page-hide', '' ); // @todo Also hidden?
88+
$element->setAttribute( 'next-page-hide', '' );
89+
}
90+
}
91+
}
92+
93+
/**
94+
* Hide elements on initial load.
95+
*/
96+
private function hide_hidden_elements() {
97+
if ( isset( $this->args['hidden_xpaths'] ) && is_array( $this->args['hidden_xpaths'] ) ) {
98+
$xpaths = $this->args['hidden_xpaths'];
99+
} else {
100+
$xpaths = array();
101+
}
102+
103+
foreach ( $xpaths as $hidden_xpath ) {
104+
/** @var DOMElement $element */
105+
foreach ( $this->xpath->query( $hidden_xpath ) as $element ) {
106+
$element->setAttribute( 'hidden', '' );
87107
}
88108
}
89109
}

modules/theme-tools/compat/twentynineteen.php

+3
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ function twentynineteen_filter_amp_infinite_scroll_sanitizers( $sanitizers ) {
156156
'//*[ @id = "masthead" ]',
157157
'//*[ contains( @class, "navigation pagination" ) ]',
158158
),
159+
'hidden_xpaths' => array(
160+
'//*[ contains( @class, "navigation pagination" ) ]',
161+
),
159162
)
160163
);
161164

modules/theme-tools/compat/twentytwenty.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ function twentytwenty_filter_amp_infinite_scroll_sanitizers( $sanitizers ) {
185185
$sanitizers['Jetpack_AMP_Infinite_Scroll_Sanitizer'] = array_merge(
186186
$sanitizers['Jetpack_AMP_Infinite_Scroll_Sanitizer'],
187187
array(
188-
// Formerly twentynineteen_amp_infinite_footers.
188+
// Formerly twentytwenty_amp_infinite_footers.
189189
'footer_xpaths' => array(
190190
'//div[ contains( @class, "footer-nav-widgets-wrapper" ) ]',
191191
'//footer[ @id = "site-footer" ]',
@@ -194,6 +194,9 @@ function twentytwenty_filter_amp_infinite_scroll_sanitizers( $sanitizers ) {
194194
'//*[ @id = "site-header" ]',
195195
'//*[ contains( @class, "pagination-wrapper" ) ]',
196196
),
197+
'hidden_xpaths' => array(
198+
'//*[ contains( @class, "pagination-wrapper" ) ]',
199+
),
197200
)
198201
);
199202

0 commit comments

Comments
 (0)