PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/Avada/includes/class-avada-blog.php

https://gitlab.com/webkod3r/tripolis
PHP | 236 lines | 132 code | 77 blank | 27 comment | 48 complexity | 296d5d7d16fabfb7cedb4dd2eb07d14f MD5 | raw file
  1. <?php
  2. class Avada_Blog {
  3. public function __construct() {
  4. add_filter( 'excerpt_length', array( $this, 'excerpt_length' ), 999 );
  5. add_action( 'pre_get_posts', array( $this, 'alter_search_loop' ), 1 );
  6. if ( ! is_admin() ) {
  7. add_filter( 'pre_get_posts', array( $this, 'search_filter' ) );
  8. add_filter( 'pre_get_posts', array( $this, 'empty_search_filter' ) );
  9. }
  10. }
  11. /**
  12. * Modify the default excerpt length
  13. */
  14. public function excerpt_length( $length ) {
  15. // Normal blog posts excerpt length
  16. if ( ! is_null( Avada()->settings->get( 'excerpt_length_blog' ) ) ) {
  17. $length = Avada()->settings->get( 'excerpt_length_blog' );
  18. }
  19. // Search results excerpt length
  20. if ( is_search() ) {
  21. $length = Avada()->settings->get( 'excerpt_length_blog' );
  22. }
  23. return $length;
  24. }
  25. /**
  26. * Apply post per page on search pages
  27. */
  28. public function alter_search_loop( $query ) {
  29. if ( ! is_admin() && $query->is_main_query() && $query->is_search() && Avada()->settings->get( 'search_results_per_page' ) ) {
  30. $query->set( 'posts_per_page', Avada()->settings->get( 'search_results_per_page' ) );
  31. }
  32. }
  33. /**
  34. * Apply filters to the search query.
  35. * Determines if we only want to display posts/pages and changes the query accordingly
  36. */
  37. public function search_filter( $query ) {
  38. if ( is_search() && $query->is_search ) {
  39. // Show only posts in search results
  40. if ( 'Only Posts' == Avada()->settings->get( 'search_content' ) ) {
  41. $query->set('post_type', 'post');
  42. }
  43. // Show only pages in search results
  44. elseif ( 'Only Pages' == Avada()->settings->get( 'search_content' ) ) {
  45. $query->set( 'post_type', 'page' );
  46. }
  47. }
  48. return $query;
  49. }
  50. /**
  51. * make wordpress respect the search template on an empty search
  52. */
  53. public function empty_search_filter( $query ) {
  54. if ( isset( $_GET['s'] ) && empty( $_GET['s'] ) && $query->is_main_query() ) {
  55. $query->is_search = true;
  56. $query->is_home = false;
  57. }
  58. return $query;
  59. }
  60. /**
  61. * get the content of the post
  62. * strip it and apply any changes required to the excerpt first.
  63. */
  64. public function get_content_stripped_and_excerpted( $excerpt_length, $content ) {
  65. $pattern = get_shortcode_regex();
  66. $content = preg_replace_callback( "/$pattern/s", 'avada_extract_shortcode_contents', $content );
  67. $content = explode( ' ', $content, $excerpt_length + 1 );
  68. if ( $excerpt_length < count( $content ) ) {
  69. array_pop( $content );
  70. }
  71. $content = implode( ' ',$content );
  72. $content = preg_replace( '~(?:\[/?)[^/\]]+/?\]~s', '', $content ); // strip shortcodes and keep the content
  73. $content = str_replace( ']]>', ']]&gt;', $content );
  74. $content = strip_tags( $content );
  75. $content = str_replace( array( '"', "'" ), array( '&quot;', '&#39;' ), $content );
  76. $content = trim( $content );
  77. return $content;
  78. }
  79. /**
  80. * Retrieve the content and apply and read-more modifications needed.
  81. */
  82. public function content( $limit, $strip_html ) {
  83. global $more;
  84. $content = '';
  85. // Sanitizing the limit value
  86. $limit = ( ! $limit && $limit != 0 ) ? 285 : intval( $limit );
  87. $test_strip_html = $strip_html;
  88. $test_strip_html = ( $strip_html == "true" || $strip_html == true ) ? true : false;
  89. $custom_excerpt = false;
  90. $post = get_post( get_the_ID() );
  91. $pos = strpos( $post->post_content, '<!--more-->' );
  92. $readmore = ( Avada()->settings->get( 'link_read_more' ) ) ? ' <a href="' . get_permalink( get_the_ID() ) . '">&#91;...&#93;</a>' : ' &#91;...&#93;';
  93. $readmore = ( Avada()->settings->get( 'disable_excerpts' ) ) ? '' : $readmore;
  94. if ( $test_strip_html ) {
  95. $more = 0;
  96. $raw_content = wp_strip_all_tags( get_the_content( '{{read_more_placeholder}}' ), '<p>' );
  97. // Strip out all attributes
  98. $raw_content = preg_replace('/<(\w+)[^>]*>/', '<$1>', $raw_content);
  99. $raw_content = str_replace( '{{read_more_placeholder}}', $read_more, $raw_content );
  100. if ( $post->post_excerpt || false !== $pos ) {
  101. $raw_content = ( ! $pos ) ? wp_strip_all_tags( rtrim( get_the_excerpt(), '[&hellip;]' ), '<p>' ) . $read_more : $raw_content;
  102. $custom_excerpt = true;
  103. }
  104. } else {
  105. $more = 0;
  106. $raw_content = get_the_content( $readmore );
  107. if ( $post->post_excerpt || false !== $pos ) {
  108. $raw_content = ( ! $pos ) ? rtrim( get_the_excerpt(), '[&hellip;]' ) . $readmore : $raw_content;
  109. $custom_excerpt = true;
  110. }
  111. }
  112. if ( $raw_content && ! $custom_excerpt ) {
  113. $pattern = get_shortcode_regex();
  114. $content = preg_replace_callback( "/$pattern/s", 'avada_extract_shortcode_contents', $raw_content );
  115. if ( 'Characters' == Avada()->settings->get( 'excerpt_base' ) ) {
  116. $content = mb_substr( $content, 0, $limit );
  117. $content .= ( $limit != 0 && ! Avada()->settings->get( 'disable_excerpts' ) ) ? $readmore : '';
  118. } else {
  119. $content = explode( ' ', $content, $limit + 1 );
  120. if ( $limit < count( $content ) ) {
  121. array_pop( $content );
  122. $content = implode( ' ',$content );
  123. if ( ! Avada()->settings->get( 'disable_excerpts' ) ) {
  124. $content .= ( $limit != 0 ) ? $readmore : '';
  125. }
  126. } else {
  127. $content = implode( ' ',$content );
  128. }
  129. }
  130. if ( $limit != 0 && ! $test_strip_html ) {
  131. $content = apply_filters( 'the_content', $content );
  132. $content = str_replace( ']]>', ']]&gt;', $content );
  133. } else {
  134. $content = sprintf( '<p>%s</p>', $content );
  135. }
  136. $strip_html_class = ( $test_strip_html ) ? 'strip-html' : '';
  137. $content = sprintf( '<div class="excerpt-container %s">%s</div>', $strip_html_class, do_shortcode( $content ) );
  138. return $content;
  139. }
  140. if ( true == $custom_excerpt ) {
  141. $pattern = get_shortcode_regex();
  142. $content = preg_replace_callback( "/$pattern/s", 'avada_extract_shortcode_contents', $raw_content );
  143. if ( true == $test_strip_html ) {
  144. $content = apply_filters( 'the_content', $content );
  145. $content = str_replace( ']]>', ']]&gt;', $content );
  146. $content = '<div class="excerpt-container strip-html">' . do_shortcode( $content ) . '</div>';
  147. } else {
  148. $content = apply_filters( 'the_content', $content );
  149. $content = str_replace( ']]>', ']]&gt;', $content );
  150. }
  151. }
  152. if ( has_excerpt() ) {
  153. $content = do_shortcode( get_the_excerpt() );
  154. $content = '<p>' . $content . '</p>';
  155. }
  156. return $content;
  157. }
  158. }
  159. // Omit closing PHP tag to avoid "Headers already sent" issues.