@@ -1748,136 +1748,46 @@ private function is_exempted_amp_page() {
1748
1748
* @return void
1749
1749
*/
1750
1750
public function amp_load_hooks () {
1751
- if ( $ this ->is_exempted_amp_page () ) {
1751
+ if (
1752
+ ! class_exists ( 'Jetpack_AMP_Support ' )
1753
+ ||
1754
+ ! Jetpack_AMP_Support::is_amp_request ()
1755
+ ||
1756
+ $ this ->is_exempted_amp_page ()
1757
+ ) {
1758
+ // @todo Should also return if theme is not twentynineteen or twentytwenty?
1752
1759
return ;
1753
1760
}
1754
1761
1755
- if ( class_exists ( 'Jetpack_AMP_Support ' ) && Jetpack_AMP_Support::is_amp_request () ) {
1756
- $ template = self ::get_settings ()->render ;
1757
-
1758
- add_filter ( 'jetpack_infinite_scroll_load_scripts_and_styles ' , '__return_false ' );
1759
-
1760
- add_action ( 'template_redirect ' , array ( $ this , 'amp_start_output_buffering ' ), 0 );
1761
- add_action ( 'shutdown ' , array ( $ this , 'amp_output_buffer ' ), 1 );
1762
-
1763
- if ( is_callable ( "amp_ {$ template }_hooks " ) ) {
1764
- call_user_func ( "amp_ {$ template }_hooks " );
1765
- }
1766
-
1767
- // Warms up the amp next page markup.
1768
- // This should be done outside the output buffering callback started in the template_redirect.
1769
- $ this ->amp_get_footer_template ();
1770
- }
1771
- }
1772
-
1773
- /**
1774
- * Start the AMP output buffering.
1775
- *
1776
- * @return void
1777
- */
1778
- public function amp_start_output_buffering () {
1779
- ob_start ( array ( $ this , 'amp_finish_output_buffering ' ) );
1780
- }
1762
+ add_action ( 'wp_footer ' , array ( $ this , 'render_amp_next_page ' ) );
1781
1763
1782
- /**
1783
- * Flush the AMP output buffer.
1784
- *
1785
- * @return void
1786
- */
1787
- public function amp_output_buffer () {
1788
- if ( ob_get_contents () ) {
1789
- ob_end_flush ();
1790
- }
1791
- }
1764
+ add_filter ( 'jetpack_infinite_scroll_load_scripts_and_styles ' , '__return_false ' );
1792
1765
1793
- /**
1794
- * Filter the AMP output buffer contents.
1795
- *
1796
- * @param string $buffer Contents of the output buffer.
1797
- *
1798
- * @return string|false
1799
- */
1800
- public function amp_finish_output_buffering ( $ buffer ) {
1801
- // Hide WordPress admin bar on next page load.
1802
- $ buffer = preg_replace (
1803
- '/id="wpadminbar"/ ' ,
1804
- '$0 next-page-hide ' ,
1805
- $ buffer
1766
+ require_once __DIR__ . '/class-jetpack-amp-infinite-scroll-sanitizer.php ' ;
1767
+ add_filter (
1768
+ 'amp_content_sanitizers ' ,
1769
+ function ( $ sanitizers ) {
1770
+ $ sanitizers ['Jetpack_AMP_Infinite_Scroll_Sanitizer ' ] = array ();
1771
+ return $ sanitizers ;
1772
+ },
1773
+ 0
1806
1774
);
1807
1775
1808
- /**
1809
- * Get the theme footers.
1810
- *
1811
- * @module infinite-scroll
1812
- *
1813
- * @since 9.0.0
1814
- *
1815
- * @param array array() An array to store multiple markup entries to be added to the footer.
1816
- * @param string $buffer The contents of the output buffer.
1817
- */
1818
- $ footers = apply_filters ( 'jetpack_amp_infinite_footers ' , array (), $ buffer );
1819
-
1820
- /**
1821
- * Filter the output buffer.
1822
- * Themes can leverage this hook to add custom markup on next page load.
1823
- *
1824
- * @module infinite-scroll
1825
- *
1826
- * @since 9.0.0
1827
- *
1828
- * @param string $buffer The contents of the output buffer.
1829
- */
1830
- $ buffer = apply_filters ( 'jetpack_amp_infinite_output ' , $ buffer );
1776
+ $ template = self ::get_settings ()->render ;
1831
1777
1832
- // Add the amp next page markup.
1833
- $ buffer = preg_replace (
1834
- '~</body>~ ' ,
1835
- $ this ->amp_get_footer_template ( $ footers ) . '$0 ' ,
1836
- $ buffer
1837
- );
1838
-
1839
- return $ buffer ;
1840
- }
1841
-
1842
- /**
1843
- * Get AMP next page markup with the custom footers.
1844
- *
1845
- * @param string[] $footers The theme footers.
1846
- *
1847
- * @return string
1848
- */
1849
- protected function amp_get_footer_template ( $ footers = array () ) {
1850
- static $ template = null ;
1851
-
1852
- if ( null === $ template ) {
1853
- $ template = $ this ->amp_footer_template ();
1778
+ if ( is_callable ( "amp_ {$ template }_hooks " ) ) {
1779
+ call_user_func ( "amp_ {$ template }_hooks " );
1854
1780
}
1855
-
1856
- if ( empty ( $ footers ) ) {
1857
- return $ template ;
1858
- }
1859
-
1860
- return preg_replace (
1861
- '/%%footer%%/ ' ,
1862
- implode ( '' , $ footers ),
1863
- $ template
1864
- );
1865
1781
}
1866
1782
1867
1783
/**
1868
1784
* AMP Next Page markup.
1869
- *
1870
- * @return string
1871
1785
*/
1872
- protected function amp_footer_template () {
1873
- ob_start ();
1786
+ public function render_amp_next_page () {
1787
+ $ config = $ this -> amp_next_page ();
1874
1788
?>
1875
- <amp-next-page max-pages="<?php echo esc_attr ( $ this ->amp_get_max_pages () ); ?> ">
1876
- <script type="application/json">
1877
- [
1878
- <?php echo wp_json_encode ( $ this ->amp_next_page () ); ?>
1879
- ]
1880
- </script>
1789
+ <amp-next-page class="jetpack-infinite-scroll" max-pages="<?php echo esc_attr ( $ this ->amp_get_max_pages () ); ?> ">
1790
+ <script type="application/json"><?php echo wp_json_encode ( array ( $ config ) ); ?> </script>
1881
1791
<div separator>
1882
1792
<?php
1883
1793
echo wp_kses_post (
@@ -1915,11 +1825,9 @@ protected function amp_footer_template() {
1915
1825
</template>
1916
1826
</div>
1917
1827
<div footer>
1918
- %%footer%%
1919
1828
</div>
1920
1829
</amp-next-page>
1921
1830
<?php
1922
- return ob_get_clean ();
1923
1831
}
1924
1832
1925
1833
/**
0 commit comments