Skip to content

Commit 681177d

Browse files
committed
WIP3
1 parent 3f94289 commit 681177d

File tree

4 files changed

+118
-233
lines changed

4 files changed

+118
-233
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/**
3+
* Infinite scroll sanitizer for AMP pages.
4+
*
5+
* @package Jetpack
6+
* @since 9.1.0
7+
*/
8+
9+
/**
10+
* This class makes the necessary changes to an AMP page when making an amp-next-page request.
11+
*/
12+
final class Jetpack_AMP_Infinite_Scroll_Sanitizer extends AMP_Base_Sanitizer {
13+
14+
/**
15+
* @var array {
16+
* @type string $footer_xpaths
17+
* @type string[] $next_page_hide_xpaths
18+
* }
19+
*/
20+
protected $args;
21+
22+
/**
23+
* XPath.
24+
*
25+
* @var DOMXPath
26+
*/
27+
private $xpath;
28+
29+
/**
30+
* Sanitize.
31+
*/
32+
public function sanitize() {
33+
$this->xpath = new DOMXPath( $this->dom );
34+
35+
// Abort if there is no amp-next-page in the document.
36+
$next_page_element = $this->xpath->query( '//amp-next-page[ @class = "jetpack-infinite-scroll" ]' )->item( 0 );
37+
if ( ! $next_page_element instanceof DOMElement ) {
38+
return;
39+
}
40+
41+
// Abort amp-next-page if no footer element discovered.
42+
$footer_elements = array();
43+
if ( ! empty( $this->args['footer_xpaths'] ) ) {
44+
foreach ( $this->args['footer_xpaths'] as $footer_xpath ) {
45+
$footer_elements = array_merge( $footer_elements, iterator_to_array( $this->xpath->query( $footer_xpath ) ) );
46+
}
47+
}
48+
if ( empty( $footer_elements ) ) {
49+
return;
50+
}
51+
52+
// Abort if the amp-next-page lacks a div[footer] element.
53+
$footer_container = $this->xpath->query( './div[ @footer ]', $next_page_element )->item( 0 );
54+
if ( ! $footer_container instanceof DOMElement ) {
55+
return;
56+
}
57+
58+
// Make sure amp-next-page is at the end of the body.
59+
$body = $this->dom->getElementsByTagName( 'body' )->item( 0 );
60+
$next_page_element->parentNode->removeChild( $next_page_element );
61+
$body->appendChild( $next_page_element );
62+
63+
// Move the footer to be inside of <amp-next-page>.
64+
foreach ( $footer_elements as $footer_element ) {
65+
$footer_element->parentNode->removeChild( $footer_element );
66+
$footer_container->appendChild( $footer_element );
67+
}
68+
69+
$this->hide_next_page_elements();
70+
}
71+
72+
/**
73+
* Hide next page elements.
74+
*/
75+
private function hide_next_page_elements() {
76+
if ( isset( $this->args['next_page_hide_xpaths'] ) && is_array( $this->args['next_page_hide_xpaths'] ) ) {
77+
$xpaths = $this->args['next_page_hide_xpaths'];
78+
} else {
79+
$xpaths = array();
80+
}
81+
$xpaths[] = '//div[ @id = "wpadminbar" ]';
82+
83+
foreach ( $xpaths as $next_page_hide_xpath ) {
84+
/** @var DOMElement $element */
85+
foreach ( $this->xpath->query( $next_page_hide_xpath ) as $element ) {
86+
$element->setAttribute( 'next-page-hide', '' ); // @todo Also hidden?
87+
}
88+
}
89+
}
90+
}

modules/infinite-scroll/infinity.php

+3-111
Original file line numberDiff line numberDiff line change
@@ -1775,124 +1775,16 @@ function ( $sanitizers ) {
17751775

17761776
$template = self::get_settings()->render;
17771777

1778-
//add_action( 'template_redirect', array( $this, 'amp_start_output_buffering' ), 0 );
1779-
//add_action( 'shutdown', array( $this, 'amp_output_buffer' ), 1 );
1780-
1781-
// @todo No? e.g. amp_twentynineteen_infinite_scroll_render_hooks().
1782-
if ( is_callable( "amp_{$template}_hooks" ) ) {
1783-
call_user_func( "amp_{$template}_hooks" );
1784-
}
1785-
1786-
// Warms up the amp next page markup.
1787-
// This should be done outside the output buffering callback started in the template_redirect.
1788-
// $this->amp_get_footer_template();
1789-
}
1790-
1791-
///**
1792-
// * Start the AMP output buffering.
1793-
// *
1794-
// * @return void
1795-
// */
1796-
//public function amp_start_output_buffering() {
1797-
// ob_start( array( $this, 'amp_finish_output_buffering' ) );
1798-
//}
1799-
1800-
///**
1801-
// * Flush the AMP output buffer.
1802-
// *
1803-
// * @return void
1804-
// */
1805-
//public function amp_output_buffer() {
1806-
// if ( ob_get_contents() ) {
1807-
// ob_end_flush();
1808-
// }
1809-
//}
1810-
1811-
/**
1812-
* Filter the AMP output buffer contents.
1813-
*
1814-
* @param string $buffer Contents of the output buffer.
1815-
*
1816-
* @return string|false
1817-
*/
1818-
public function amp_finish_output_buffering( $buffer ) {
1819-
// @todo Wrong! There should be a query param which is added to the next page request which causes the admin bar to be hidden.
1820-
// Hide WordPress admin bar on next page load.
1821-
$buffer = preg_replace(
1822-
'/id="wpadminbar"/',
1823-
'$0 next-page-hide',
1824-
$buffer
1825-
);
1826-
1827-
/**
1828-
* Get the theme footers.
1829-
*
1830-
* @module infinite-scroll
1831-
*
1832-
* @since 9.0.0
1833-
*
1834-
* @param array array() An array to store multiple markup entries to be added to the footer.
1835-
* @param string $buffer The contents of the output buffer.
1836-
*/
1837-
$footers = apply_filters( 'jetpack_amp_infinite_footers', array(), $buffer );
1838-
1839-
/**
1840-
* Filter the output buffer.
1841-
* Themes can leverage this hook to add custom markup on next page load.
1842-
*
1843-
* @module infinite-scroll
1844-
*
1845-
* @since 9.0.0
1846-
*
1847-
* @param string $buffer The contents of the output buffer.
1848-
*/
1849-
$buffer = apply_filters( 'jetpack_amp_infinite_output', $buffer );
1850-
1851-
// Add the amp next page markup.
1852-
$buffer = preg_replace(
1853-
'~</body>~',
1854-
$this->amp_get_footer_template( $footers ) . '$0',
1855-
$buffer
1856-
);
1857-
1858-
return $buffer;
1778+
if ( is_callable( "amp_{$template}_hooks" ) ) {
1779+
call_user_func( "amp_{$template}_hooks" );
1780+
}
18591781
}
18601782

1861-
/**
1862-
* Get AMP next page markup with the custom footers.
1863-
*
1864-
* @param string[] $footers The theme footers.
1865-
*
1866-
* @return string
1867-
*/
1868-
// protected function amp_get_footer_template( $footers = array() ) {
1869-
// static $template = null;
1870-
//
1871-
// if ( null === $template ) {
1872-
// $template = $this->render_amp_next_page();
1873-
// }
1874-
//
1875-
// if ( empty( $footers ) ) {
1876-
// return $template;
1877-
// }
1878-
//
1879-
// return preg_replace(
1880-
// '/%%footer%%/',
1881-
// implode( '', $footers ),
1882-
// $template
1883-
// );
1884-
// }
1885-
18861783
/**
18871784
* AMP Next Page markup.
18881785
*/
18891786
public function render_amp_next_page() {
18901787
$config = $this->amp_next_page();
1891-
1892-
// if ( ! empty( $config['url'] ) ) {
1893-
// $config['url'] = add_query_arg( 'amp_next_page', '1', $config['url'] );
1894-
// }
1895-
18961788
?>
18971789
<amp-next-page class="jetpack-infinite-scroll" max-pages="<?php echo esc_attr( $this->amp_get_max_pages() ); ?>">
18981790
<script type="application/json"><?php echo wp_json_encode( array( $config ) ); ?></script>

modules/theme-tools/compat/twentynineteen.php

+3-61
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,6 @@ function twentynineteen_jetpack_body_classes( $classes ) {
131131
* @return void
132132
*/
133133
function amp_twentynineteen_infinite_scroll_render_hooks() {
134-
//add_filter( 'jetpack_amp_infinite_footers', 'twentynineteen_amp_infinite_footers', 10, 2 );
135-
//add_filter( 'jetpack_amp_infinite_output', 'twentynineteen_amp_infinite_output' );
136134
add_filter( 'jetpack_amp_infinite_older_posts', 'twentynineteen_amp_infinite_older_posts' );
137135
}
138136

@@ -151,7 +149,9 @@ function twentynineteen_filter_amp_infinite_scroll_sanitizers( $sanitizers ) {
151149
$sanitizers['Jetpack_AMP_Infinite_Scroll_Sanitizer'],
152150
array(
153151
// Formerly twentynineteen_amp_infinite_footers.
154-
'footer_xpath' => '//footer[ @id = "colophon" ]',
152+
'footer_xpaths' => array(
153+
'//footer[ @id = "colophon" ]',
154+
),
155155
'next_page_hide_xpaths' => array(
156156
'//*[ @id = "masthead" ]',
157157
'//*[ contains( @class, "navigation pagination" ) ]',
@@ -163,64 +163,6 @@ function twentynineteen_filter_amp_infinite_scroll_sanitizers( $sanitizers ) {
163163
}
164164
add_filter( 'amp_content_sanitizers', 'twentynineteen_filter_amp_infinite_scroll_sanitizers' );
165165

166-
/**
167-
* Get the theme specific footers.
168-
*
169-
* @deprecated
170-
* @param array $footers The footers of the themes.
171-
* @param string $buffer Contents of the output buffer.
172-
*
173-
* @return mixed
174-
*/
175-
function twentynineteen_amp_infinite_footers( $footers, $buffer ) {
176-
_deprecated_function( __FUNCTION__, 'jetpack-9.1' );
177-
178-
// Collect the footer wrapper.
179-
preg_match(
180-
'/<footer id="colophon".*<!-- #colophon -->/s',
181-
$buffer,
182-
$footer
183-
);
184-
$footers[] = reset( $footer );
185-
186-
return $footers;
187-
}
188-
189-
/**
190-
* Hide and remove various elements from next page load.
191-
*
192-
* @deprecated
193-
* @param string $buffer Contents of the output buffer.
194-
*
195-
* @return string
196-
*/
197-
function twentynineteen_amp_infinite_output( $buffer ) {
198-
_deprecated_function( __FUNCTION__, 'jetpack-9.1' );
199-
200-
// Hide site header on next page load.
201-
$buffer = preg_replace(
202-
'/id="masthead"/',
203-
'$0 next-page-hide',
204-
$buffer
205-
);
206-
207-
// Hide pagination on next page load.
208-
$buffer = preg_replace(
209-
'/class=".*navigation pagination.*"/',
210-
'$0 next-page-hide hidden',
211-
$buffer
212-
);
213-
214-
// Remove the footer as it will be added back to amp next page footer.
215-
$buffer = preg_replace(
216-
'/<footer id="colophon".*<!-- #colophon -->/s',
217-
'',
218-
$buffer
219-
);
220-
221-
return $buffer;
222-
}
223-
224166
/**
225167
* Filter the AMP infinite scroll older posts button
226168
*

0 commit comments

Comments
 (0)