暫無描述

twentytwenty.php 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /**
  3. * Jetpack Compatibility File
  4. * See: https://jetpack.com/
  5. *
  6. * @package automattic/jetpack
  7. */
  8. /**
  9. * Add Jetpack extra functionality to Twenty Twenty.
  10. *
  11. * See: https://jetpack.com/support/infinite-scroll/
  12. * See: https://jetpack.com/support/responsive-videos/
  13. * See: https://jetpack.com/support/content-options/
  14. */
  15. function twentytwenty_jetpack_setup() {
  16. /**
  17. * Add theme support for Infinite Scroll.
  18. */
  19. add_theme_support(
  20. 'infinite-scroll',
  21. array(
  22. 'type' => 'click',
  23. 'container' => 'site-content',
  24. 'render' => 'twentytwenty_infinite_scroll_render',
  25. 'footer' => 'site-content',
  26. 'footer_widgets' => array(
  27. 'sidebar-1',
  28. 'sidebar-2',
  29. ),
  30. )
  31. );
  32. // Add theme support for Content Options.
  33. add_theme_support(
  34. 'jetpack-content-options',
  35. array(
  36. 'post-details' => array(
  37. 'stylesheet' => 'twentytwenty-style',
  38. 'date' => '.post-date',
  39. 'categories' => '.entry-categories',
  40. 'tags' => '.post-tags',
  41. 'author' => '.post-author',
  42. ),
  43. 'featured-images' => array(
  44. 'archive' => true,
  45. 'post' => true,
  46. 'page' => true,
  47. 'fallback' => false,
  48. ),
  49. )
  50. );
  51. /**
  52. * Add theme support for geo-location.
  53. */
  54. add_theme_support( 'jetpack-geo-location' );
  55. }
  56. add_action( 'after_setup_theme', 'twentytwenty_jetpack_setup' );
  57. /**
  58. * Custom render function for Infinite Scroll.
  59. */
  60. function twentytwenty_infinite_scroll_render() {
  61. while ( have_posts() ) {
  62. echo '<hr class="post-separator styled-separator is-style-wide section-inner" aria-hidden="true" />';
  63. the_post();
  64. get_template_part( 'template-parts/content', get_post_type() );
  65. }
  66. }
  67. /**
  68. * Remove Sharing buttons and Likes from excerpts that are used as intro on single post views.
  69. */
  70. function twentytwenty_no_sharing_on_excerpts() {
  71. if ( is_single() ) {
  72. // Remove sharing buttons.
  73. remove_filter( 'the_excerpt', 'sharing_display', 19 );
  74. // Remove Likes.
  75. if ( class_exists( 'Jetpack_Likes' ) ) {
  76. remove_filter( 'the_excerpt', array( Jetpack_Likes::init(), 'post_likes' ), 30, 1 );
  77. }
  78. }
  79. }
  80. add_action( 'loop_start', 'twentytwenty_no_sharing_on_excerpts' );
  81. /**
  82. * We do not need to display the Likes Heading here.
  83. *
  84. * @param string $heading Headline structure.
  85. * @param string $title Title.
  86. * @param string $module Module name.
  87. */
  88. function twentytwenty_no_likes_heading( $heading, $title, $module ) {
  89. if ( 'likes' === $module ) {
  90. return '';
  91. }
  92. return $heading;
  93. }
  94. add_filter( 'jetpack_sharing_headline_html', 'twentytwenty_no_likes_heading', 10, 3 );
  95. /**
  96. * Disable Ads in post excerpts, that are used as intro on single post views.
  97. */
  98. add_filter( 'wordads_excerpt_disable', '__return_true' );
  99. /**
  100. * Add our compat CSS file for Infinite Scroll and custom widget stylings and such.
  101. * Set the version equal to filemtime for development builds, and the JETPACK__VERSION for production
  102. * or skip it entirely for wpcom.
  103. */
  104. function twentytwenty_enqueue_jetpack_style() {
  105. $version = Jetpack::is_development_version()
  106. ? filemtime( JETPACK__PLUGIN_DIR . 'modules/theme-tools/compat/twentytwenty.css' )
  107. : JETPACK__VERSION;
  108. wp_enqueue_style( 'twentytwenty-jetpack', plugins_url( 'twentytwenty.css', __FILE__ ), array(), $version );
  109. wp_style_add_data( 'twentytwenty-jetpack', 'rtl', 'replace' );
  110. }
  111. add_action( 'wp_enqueue_scripts', 'twentytwenty_enqueue_jetpack_style' );
  112. /**
  113. * Add inline custom CSS with custom accent color if there is any set.
  114. */
  115. function twentytwenty_infinity_accent_color_css() {
  116. // Bail early if no custom color was set.
  117. if (
  118. 'custom' !== get_theme_mod( 'accent_hue_active' )
  119. || empty( get_theme_mod( 'accent_accessible_colors' ) )
  120. ) {
  121. return;
  122. }
  123. $color_info = get_theme_mod( 'accent_accessible_colors' );
  124. $custom_css = sprintf(
  125. '
  126. .infinite-scroll #site-content #infinite-handle span button,
  127. .infinite-scroll #site-content #infinite-handle span button:hover,
  128. .infinite-scroll #site-content #infinite-handle span button:focus {
  129. background: %1$s;
  130. color: %2$s;
  131. }
  132. #site-content .entry-content div.sharedaddy h3.sd-title,
  133. #site-content .entry-content h3.sd-title,
  134. #site-content .entry-content #jp-relatedposts h3.jp-relatedposts-headline {
  135. color: %3$s;
  136. }
  137. ',
  138. $color_info['content']['accent'],
  139. $color_info['content']['background'],
  140. $color_info['content']['secondary']
  141. );
  142. // Add our custom style to the existing Twenty Twenty CSS compat file.
  143. wp_add_inline_style( 'twentytwenty-jetpack', $custom_css );
  144. }
  145. add_action( 'wp_enqueue_scripts', 'twentytwenty_infinity_accent_color_css' );
  146. /**
  147. * Load AMP theme specific hooks for infinite scroll.
  148. *
  149. * @return void
  150. */
  151. function amp_twentytwenty_infinite_scroll_render_hooks() {
  152. add_filter( 'jetpack_amp_infinite_footers', 'twentytwenty_amp_infinite_footers', 10, 2 );
  153. add_filter( 'jetpack_amp_infinite_output', 'twentytwenty_amp_infinite_output' );
  154. add_filter( 'jetpack_amp_infinite_separator', 'twentytwenty_amp_infinite_separator' );
  155. add_filter( 'jetpack_amp_infinite_older_posts', 'twentytwenty_amp_infinite_older_posts' );
  156. }
  157. /**
  158. * Get the theme specific footers.
  159. *
  160. * @param array $footers The footers of the themes.
  161. * @param string $buffer Contents of the output buffer.
  162. *
  163. * @return mixed
  164. */
  165. function twentytwenty_amp_infinite_footers( $footers, $buffer ) {
  166. // Collect the footer wrapper.
  167. preg_match(
  168. '/<div class="footer-nav-widgets-wrapper.*<!-- .footer-nav-widgets-wrapper -->/s',
  169. $buffer,
  170. $footer
  171. );
  172. $footers[] = reset( $footer );
  173. // Collect the footer wrapper.
  174. preg_match(
  175. '/<footer id="site-footer".*<!-- #site-footer -->/s',
  176. $buffer,
  177. $footer
  178. );
  179. $footers[] = reset( $footer );
  180. return $footers;
  181. }
  182. /**
  183. * Hide and remove various elements from next page load.
  184. *
  185. * @param string $buffer Contents of the output buffer.
  186. *
  187. * @return string
  188. */
  189. function twentytwenty_amp_infinite_output( $buffer ) {
  190. // Hide site header on next page load.
  191. $buffer = preg_replace(
  192. '/id="site-header"/',
  193. '$0 next-page-hide',
  194. $buffer
  195. );
  196. // Hide pagination on next page load.
  197. $buffer = preg_replace(
  198. '/class=".*pagination-wrapper.*"/',
  199. '$0 next-page-hide hidden',
  200. $buffer
  201. );
  202. // Remove the footer as it will be added back to amp next page footer.
  203. $buffer = preg_replace(
  204. '/<div class="footer-nav-widgets-wrapper.*<!-- .footer-nav-widgets-wrapper -->/s',
  205. '',
  206. $buffer
  207. );
  208. // Remove the footer as it will be added back to amp next page footer.
  209. $buffer = preg_replace(
  210. '/<footer id="site-footer".*<!-- #site-footer -->/s',
  211. '',
  212. $buffer
  213. );
  214. return $buffer;
  215. }
  216. /**
  217. * Filter the AMP infinite scroll separator
  218. *
  219. * @return string
  220. */
  221. function twentytwenty_amp_infinite_separator() {
  222. ob_start();
  223. ?>
  224. <hr class="post-separator styled-separator is-style-wide section-inner" aria-hidden="true">
  225. <?php
  226. return ob_get_clean();
  227. }
  228. /**
  229. * Filter the AMP infinite scroll older posts button
  230. *
  231. * @return string
  232. */
  233. function twentytwenty_amp_infinite_older_posts() {
  234. ob_start();
  235. ?>
  236. <div id="infinite-handle" class="read-more-button-wrap">
  237. <span>
  238. <a href="{{url}}" class="more-link" rel="amphtml">
  239. <span class="faux-button">
  240. <?php esc_html_e( 'Older posts', 'jetpack' ); ?>
  241. </span>
  242. </a>
  243. </span>
  244. </div>
  245. <?php
  246. return ob_get_clean();
  247. }