Geen omschrijving

blog-display.php 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. /**
  3. * The functions to display Content or Excerpt in a theme.
  4. */
  5. /**
  6. * If the theme doesn't support 'jetpack-content-options', don't continue.
  7. */
  8. if ( ! current_theme_supports( 'jetpack-content-options' ) ) {
  9. return;
  10. }
  11. /**
  12. * Get the Blog Display setting.
  13. * If theme is using both 'Content' and 'Excerpt' then this setting will be called 'Mixed'.
  14. */
  15. $options = get_theme_support( 'jetpack-content-options' );
  16. $blog_display = ( ! empty( $options[0]['blog-display'] ) ) ? $options[0]['blog-display'] : null;
  17. $blog_display = preg_grep( '/^(content|excerpt)$/', (array) $blog_display );
  18. sort( $blog_display );
  19. $blog_display = implode( ', ', $blog_display );
  20. $blog_display = ( 'content, excerpt' === $blog_display ) ? 'mixed' : $blog_display;
  21. /**
  22. * If the theme doesn't support 'jetpack-content-options[ 'blog-display' ]', don't continue.
  23. */
  24. if ( ! in_array( $blog_display, array( 'content', 'excerpt', 'mixed' ) ) ) {
  25. return;
  26. }
  27. /**
  28. * Apply Content filters.
  29. *
  30. * @since 9.7.0 Deprecated $content parameter.
  31. *
  32. * @param string $content Post content. Deprecated.
  33. */
  34. function jetpack_blog_display_custom_excerpt( $content = '' ) {
  35. if ( ! empty( $content ) ) {
  36. _doing_it_wrong(
  37. 'jetpack_blog_display_custom_excerpt',
  38. esc_html__( 'You do not need to pass a $content parameter anymore.', 'jetpack' ),
  39. 'jetpack-9.7.0'
  40. );
  41. }
  42. $post = get_post();
  43. if ( empty( $post ) ) {
  44. return '';
  45. }
  46. if ( empty( $post->post_excerpt ) ) {
  47. $text = strip_shortcodes( $post->post_content );
  48. $text = str_replace( ']]>', ']]&gt;', $text );
  49. $text = strip_tags( $text );
  50. /** This filter is documented in wp-includes/formatting.php */
  51. $excerpt_length = apply_filters( 'excerpt_length', 55 );
  52. /** This filter is documented in wp-includes/formatting.php */
  53. $excerpt_more = apply_filters( 'excerpt_more', ' ' . '[...]' );
  54. /*
  55. * translators: If your word count is based on single characters (e.g. East Asian characters),
  56. * enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
  57. * Do not translate into your own language.
  58. */
  59. if ( strpos( _x( 'words', 'Word count type. Do not translate!', 'jetpack' ), 'characters' ) === 0 && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) {
  60. $text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' );
  61. preg_match_all( '/./u', $text, $words );
  62. $words = array_slice( $words[0], 0, $excerpt_length + 1 );
  63. $sep = '';
  64. } else {
  65. $words = preg_split( "/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY );
  66. $sep = ' ';
  67. }
  68. if ( count( $words ) > $excerpt_length ) {
  69. array_pop( $words );
  70. $text = implode( $sep, $words );
  71. $text = $text . $excerpt_more;
  72. } else {
  73. $text = implode( $sep, $words );
  74. }
  75. } else {
  76. $text = wp_kses_post( $post->post_excerpt );
  77. }
  78. return sprintf( '<p>%s</p>', $text );
  79. }
  80. /**
  81. * Display Excerpt instead of Content.
  82. */
  83. function jetpack_the_content_to_the_excerpt( $content ) {
  84. if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) {
  85. if ( post_password_required() ) {
  86. $excerpt = sprintf( '<p>%s</p>', esc_html__( 'There is no excerpt because this is a protected post.', 'jetpack' ) );
  87. } else {
  88. $excerpt = jetpack_blog_display_custom_excerpt();
  89. }
  90. }
  91. if ( empty( $excerpt ) ) {
  92. return $content;
  93. } else {
  94. return $excerpt;
  95. }
  96. }
  97. /**
  98. * Display Content instead of Excerpt.
  99. */
  100. function jetpack_the_excerpt_to_the_content( $content ) {
  101. if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) {
  102. ob_start();
  103. the_content(
  104. sprintf(
  105. wp_kses(
  106. /* translators: %s: Name of current post. Only visible to screen readers */
  107. __( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'jetpack' ),
  108. array(
  109. 'span' => array(
  110. 'class' => array(),
  111. ),
  112. )
  113. ),
  114. get_the_title()
  115. )
  116. );
  117. $content = ob_get_clean();
  118. }
  119. return $content;
  120. }
  121. /**
  122. * Display both Content and Excerpt instead of Content in the Customizer so live preview can switch between them.
  123. */
  124. function jetpack_the_content_customizer( $content ) {
  125. $class = jetpack_the_content_customizer_class();
  126. if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) {
  127. if ( post_password_required() ) {
  128. $excerpt = sprintf( '<p>%s</p>', esc_html__( 'There is no excerpt because this is a protected post.', 'jetpack' ) );
  129. } else {
  130. $excerpt = jetpack_blog_display_custom_excerpt();
  131. }
  132. }
  133. if ( empty( $excerpt ) ) {
  134. return $content;
  135. } else {
  136. return sprintf( '<div class="jetpack-blog-display %s jetpack-the-content">%s</div><div class="jetpack-blog-display %s jetpack-the-excerpt">%s</div>', $class, $content, $class, $excerpt );
  137. }
  138. }
  139. /**
  140. * Display both Content and Excerpt instead of Excerpt in the Customizer so live preview can switch between them.
  141. */
  142. function jetpack_the_excerpt_customizer( $excerpt ) {
  143. if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) {
  144. ob_start();
  145. the_content(
  146. sprintf(
  147. wp_kses(
  148. /* translators: %s: Name of current post. Only visible to screen readers */
  149. __( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'jetpack' ),
  150. array(
  151. 'span' => array(
  152. 'class' => array(),
  153. ),
  154. )
  155. ),
  156. get_the_title()
  157. )
  158. );
  159. $content = ob_get_clean();
  160. }
  161. if ( empty( $content ) ) {
  162. return $excerpt;
  163. } else {
  164. return sprintf( '<div class="jetpack-blog-display jetpack-the-content">%s</div><div class="jetpack-blog-display jetpack-the-excerpt">%s</div>', $content, $excerpt );
  165. }
  166. }
  167. /**
  168. * Display Content instead of Excerpt in the Customizer when theme uses a 'Mixed' display.
  169. */
  170. function jetpack_the_excerpt_mixed_customizer( $content ) {
  171. if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) {
  172. jetpack_the_content_customizer_class( 'output-the-excerpt' );
  173. ob_start();
  174. the_content();
  175. $content = ob_get_clean();
  176. }
  177. return $content;
  178. }
  179. /**
  180. * Returns a class value, `output-the-content` by default.
  181. * Used for themes with a 'Mixed' Blog Display so we can tell which output is by default.
  182. */
  183. function jetpack_the_content_customizer_class( $new_class = null ) {
  184. static $class;
  185. if ( isset( $new_class ) ) {
  186. // Assign a new class and return.
  187. $class = $new_class;
  188. } elseif ( isset( $class ) ) {
  189. // Reset the class after getting value.
  190. $prev_class = $class;
  191. $class = null;
  192. return $prev_class;
  193. } else {
  194. // Return default class value.
  195. return 'output-the-content';
  196. }
  197. }
  198. if ( is_customize_preview() ) {
  199. /*
  200. * Display Content and Excerpt if the default Blog Display is 'Content'
  201. * and we are in the Customizer.
  202. */
  203. if ( 'content' === $blog_display ) {
  204. add_filter( 'the_content', 'jetpack_the_content_customizer' );
  205. }
  206. /*
  207. * Display Content and Excerpt if the default Blog Display is 'Excerpt'
  208. * and we are in the Customizer.
  209. */
  210. if ( 'excerpt' === $blog_display ) {
  211. add_filter( 'the_excerpt', 'jetpack_the_excerpt_customizer' );
  212. }
  213. /*
  214. * Display Content and Excerpt if the default Blog Display is 'Mixed'
  215. * and we are in the Customizer.
  216. */
  217. if ( 'mixed' === $blog_display ) {
  218. add_filter( 'the_content', 'jetpack_the_content_customizer' );
  219. add_filter( 'the_excerpt', 'jetpack_the_excerpt_mixed_customizer' );
  220. }
  221. } else {
  222. $display_option = get_option( 'jetpack_content_blog_display', $blog_display );
  223. /*
  224. * Display Excerpt if the default Blog Display is 'Content'
  225. * or default Blog Display is 'Mixed'
  226. * and the Option picked is 'Post Excerpt'
  227. * and we aren't in the Customizer.
  228. */
  229. if ( ( 'content' === $blog_display || 'mixed' === $blog_display ) && 'excerpt' === $display_option ) {
  230. add_filter( 'the_content', 'jetpack_the_content_to_the_excerpt' );
  231. }
  232. /*
  233. * Display Content if the default Blog Display is 'Excerpt'
  234. * or default Blog Display is 'Mixed'
  235. * and the Option picked is 'Full Post'
  236. * and we aren't in the Customizer.
  237. */
  238. if ( ( 'excerpt' === $blog_display || 'mixed' === $blog_display ) && 'content' === $display_option ) {
  239. add_filter( 'the_excerpt', 'jetpack_the_excerpt_to_the_content' );
  240. }
  241. }