blog-display.php 7.7 KB

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