Brak opisu

twentynineteen.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * Jetpack Compatibility File
  4. * See: https://jetpack.com/
  5. */
  6. function twentynineteen_jetpack_setup() {
  7. /**
  8. * Add theme support for Infinite Scroll.
  9. */
  10. add_theme_support( 'infinite-scroll', array(
  11. 'type' => 'click',
  12. 'container' => 'main',
  13. 'render' => 'twentynineteen_infinite_scroll_render',
  14. 'footer' => 'page',
  15. ) );
  16. /**
  17. * Add theme support for Responsive Videos.
  18. */
  19. add_theme_support( 'jetpack-responsive-videos' );
  20. /**
  21. * Add theme support for geo-location.
  22. */
  23. add_theme_support( 'jetpack-geo-location' );
  24. /**
  25. * Add theme support for Content Options.
  26. */
  27. add_theme_support( 'jetpack-content-options', array(
  28. 'blog-display' => array( 'content', 'excerpt' ),
  29. 'post-details' => array(
  30. 'stylesheet' => 'twentynineteen-style',
  31. 'date' => '.posted-on',
  32. 'categories' => '.cat-links',
  33. 'tags' => '.tags-links',
  34. 'author' => '.byline',
  35. 'comment' => '.comments-link',
  36. ),
  37. 'featured-images' => array(
  38. 'archive' => true,
  39. 'post' => true,
  40. 'page' => true,
  41. ),
  42. ) );
  43. }
  44. add_action( 'after_setup_theme', 'twentynineteen_jetpack_setup' );
  45. /**
  46. * Custom render function for Infinite Scroll.
  47. */
  48. function twentynineteen_infinite_scroll_render() {
  49. while ( have_posts() ) {
  50. the_post();
  51. get_template_part( 'template-parts/content/content' );
  52. }
  53. }
  54. function twentynineteen_init_jetpack() {
  55. /**
  56. * Add our compat CSS file for Infinite Scroll and custom widget stylings and such.
  57. * Set the version equal to filemtime for development builds, and the JETPACK__VERSION for production
  58. * or skip it entirely for wpcom.
  59. */
  60. if ( ! is_admin() ) {
  61. $version = false;
  62. if ( method_exists( 'Jetpack', 'is_development_version' ) ) {
  63. $version = Jetpack::is_development_version() ? filemtime( plugin_dir_path( __FILE__ ) . 'twentynineteen.css' ) : JETPACK__VERSION;
  64. }
  65. wp_enqueue_style( 'twentynineteen-jetpack', plugins_url( 'twentynineteen.css', __FILE__ ), array(), $version );
  66. wp_style_add_data( 'twentynineteen-jetpack', 'rtl', 'replace' );
  67. }
  68. }
  69. add_action( 'init', 'twentynineteen_init_jetpack' );
  70. /**
  71. * Alter gallery widget default width.
  72. */
  73. function twentynineteen_gallery_widget_content_width( $width ) {
  74. return 390;
  75. }
  76. add_filter( 'gallery_widget_content_width', 'twentynineteen_gallery_widget_content_width' );
  77. /**
  78. * Alter featured-image default visibility for content-options.
  79. */
  80. function twentynineteen_override_post_thumbnail( $width ) {
  81. $options = get_theme_support( 'jetpack-content-options' );
  82. $featured_images = ( ! empty( $options[0]['featured-images'] ) ) ? $options[0]['featured-images'] : null;
  83. $settings = array(
  84. 'post-default' => ( isset( $featured_images['post-default'] ) && false === $featured_images['post-default'] ) ? '' : 1,
  85. 'page-default' => ( isset( $featured_images['page-default'] ) && false === $featured_images['page-default'] ) ? '' : 1,
  86. );
  87. $settings = array_merge( $settings, array(
  88. 'post-option' => get_option( 'jetpack_content_featured_images_post', $settings['post-default'] ),
  89. 'page-option' => get_option( 'jetpack_content_featured_images_page', $settings['page-default'] ),
  90. ) );
  91. if ( ( ! $settings['post-option'] && is_single() )
  92. || ( ! $settings['page-option'] && is_singular() && is_page() ) ) {
  93. return false;
  94. } else {
  95. return ! post_password_required() && ! is_attachment() && has_post_thumbnail();
  96. }
  97. }
  98. add_filter( 'twentynineteen_can_show_post_thumbnail', 'twentynineteen_override_post_thumbnail', 10, 2 );
  99. /**
  100. * Adds custom classes to the array of body classes.
  101. *
  102. * @param array $classes Classes for the body element.
  103. * @return array
  104. */
  105. function twentynineteen_jetpack_body_classes( $classes ) {
  106. // Adds a class if we're in the Customizer
  107. if ( is_customize_preview() ) :
  108. $classes[] = 'twentynineteen-customizer';
  109. endif;
  110. return $classes;
  111. }
  112. add_filter( 'body_class', 'twentynineteen_jetpack_body_classes' );
  113. /**
  114. * Load AMP theme specific hooks for infinite scroll.
  115. *
  116. * @return void
  117. */
  118. function amp_twentynineteen_infinite_scroll_render_hooks() {
  119. add_filter( 'jetpack_amp_infinite_footers', 'twentynineteen_amp_infinite_footers', 10, 2 );
  120. add_filter( 'jetpack_amp_infinite_output', 'twentynineteen_amp_infinite_output' );
  121. add_filter( 'jetpack_amp_infinite_older_posts', 'twentynineteen_amp_infinite_older_posts' );
  122. }
  123. /**
  124. * Get the theme specific footers.
  125. *
  126. * @param array $footers The footers of the themes.
  127. * @param string $buffer Contents of the output buffer.
  128. *
  129. * @return mixed
  130. */
  131. function twentynineteen_amp_infinite_footers( $footers, $buffer ) {
  132. // Collect the footer wrapper.
  133. preg_match(
  134. '/<footer id="colophon".*<!-- #colophon -->/s',
  135. $buffer,
  136. $footer
  137. );
  138. $footers[] = reset( $footer );
  139. return $footers;
  140. }
  141. /**
  142. * Hide and remove various elements from next page load.
  143. *
  144. * @param string $buffer Contents of the output buffer.
  145. *
  146. * @return string
  147. */
  148. function twentynineteen_amp_infinite_output( $buffer ) {
  149. // Hide site header on next page load.
  150. $buffer = preg_replace(
  151. '/id="masthead"/',
  152. '$0 next-page-hide',
  153. $buffer
  154. );
  155. // Hide pagination on next page load.
  156. $buffer = preg_replace(
  157. '/class=".*navigation pagination.*"/',
  158. '$0 next-page-hide hidden',
  159. $buffer
  160. );
  161. // Remove the footer as it will be added back to amp next page footer.
  162. $buffer = preg_replace(
  163. '/<footer id="colophon".*<!-- #colophon -->/s',
  164. '',
  165. $buffer
  166. );
  167. return $buffer;
  168. }
  169. /**
  170. * Filter the AMP infinite scroll older posts button
  171. *
  172. * @return string
  173. */
  174. function twentynineteen_amp_infinite_older_posts() {
  175. ob_start();
  176. ?>
  177. <div id="infinite-handle" style="text-align: center;">
  178. <span>
  179. <a href="{{url}}">
  180. <button>
  181. <?php esc_html_e( 'Older posts', 'jetpack' ); ?>
  182. </button>
  183. </a>
  184. </span>
  185. </div>
  186. <?php
  187. return ob_get_clean();
  188. }