rdusnr

Untitled

Feb 23rd, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.12 KB | None | 0 0
  1.  
  2. if ( ! function_exists( 'kleo_ajax_search' ) ) {
  3. function kleo_ajax_search() {
  4. //if "s" input is missing exit
  5. if ( empty( $_REQUEST['s'] ) && empty( $_REQUEST['bbp_search'] ) ) {
  6. die();
  7. }
  8.  
  9. if ( ! empty( $_REQUEST['bbp_search'] ) ) {
  10. $search_string = $_REQUEST['bbp_search'];
  11. } else {
  12. $search_string = $_REQUEST['s'];
  13. }
  14.  
  15. $output = "";
  16. $context = "any";
  17. $defaults = array(
  18. 'numberposts' => 4,
  19. 'posts_per_page' => 20,
  20. 'post_type' => 'any',
  21. 'post_status' => 'publish',
  22. 'post_password' => '',
  23. 'suppress_filters' => false,
  24. 's' => $_REQUEST['s']
  25. );
  26.  
  27. if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] != '' ) {
  28. $context = explode( ",", $_REQUEST['context'] );
  29. $defaults['post_type'] = $context;
  30. }
  31. if ( ! empty( $defaults['post_type'] ) && is_array( $defaults['post_type'] ) ) {
  32. foreach ( $defaults['post_type'] as $ptk => $ptv ) {
  33. if ( $ptv == 'forum' ) {
  34. unset( $defaults['post_type'][ $ptk ] );
  35. break;
  36. }
  37. }
  38. }
  39.  
  40. $defaults = apply_filters( 'kleo_ajax_query_args', $defaults );
  41.  
  42. $the_query = new WP_Query( $defaults );
  43. $posts = $the_query->get_posts();
  44.  
  45. $members = array();
  46. $members['total'] = 0;
  47. $groups = array();
  48. $groups['total'] = 0;
  49. $forums = false;
  50.  
  51.  
  52. if ( function_exists( 'bp_is_active' ) && ( $context == "any" || in_array( "members", $context ) ) ) {
  53. $members = bp_core_get_users( array(
  54. 'search_terms' => $search_string,
  55. 'per_page' => $defaults['numberposts'],
  56. 'populate_extras' => false
  57. ) );
  58. }
  59.  
  60. if ( function_exists( 'bp_is_active' ) && bp_is_active( "groups" ) && ( $context == "any" || in_array( "groups", $context ) ) ) {
  61. $groups = groups_get_groups( array(
  62. 'search_terms' => $search_string,
  63. 'per_page' => $defaults['numberposts'],
  64. 'populate_extras' => false
  65. ) );
  66. }
  67.  
  68. if ( class_exists( 'bbPress' ) && ( $context == "any" || in_array( "forum", $context ) ) ) {
  69. $forums = kleo_bbp_get_replies( $search_string );
  70. }
  71.  
  72.  
  73. //if there are no posts, groups nor members
  74. if ( empty( $posts ) && $members['total'] == 0 && $groups['total'] == 0 && ! $forums ) {
  75. $output = "<div class='kleo_ajax_entry ajax_not_found'>";
  76. $output .= "<div class='ajax_search_content'>";
  77. $output .= "<i class='icon icon-attention-circled'></i> ";
  78. $output .= __( "Sorry, we haven't found anything based on your criteria.", 'kleo_framework' );
  79. $output .= "<br>";
  80. $output .= __( "Please try searching by different terms.", 'kleo_framework' );
  81. $output .= "</div>";
  82. $output .= "</div>";
  83. echo $output;
  84. die();
  85. }
  86.  
  87. /* Forums topics search */
  88. if ( ! empty( $forums ) ) {
  89. $output .= '<div class="kleo-ajax-part kleo-ajax-type-forums">';
  90. $output .= '<h4><span>' . __( "Forums", 'kleo_framework' ) . '</span></h4>';
  91.  
  92. $i = 0;
  93. foreach ( $forums as $fk => $forum ) {
  94.  
  95. $i ++;
  96. if ( $i <= 4 ) {
  97. $image = "<i class='icon icon-chat-1'></i>";
  98.  
  99. $output .= "<div class ='kleo_ajax_entry'>";
  100. $output .= "<div class='ajax_search_image'>$image</div>";
  101. $output .= "<div class='ajax_search_content'>";
  102. $output .= "<a href='" . $forum['url'] . "' class='search_title'>";
  103. $output .= $forum['name'];
  104. $output .= "</a>";
  105. //$output .= "<span class='search_excerpt'>";
  106. //$output .= $latest_activity;
  107. //$output .= "</span>";
  108. $output .= "</div>";
  109. $output .= "</div>";
  110. }
  111. }
  112. $output .= "<a class='ajax_view_all' href='" . esc_url( bbp_get_search_url() . "?bbp_search=" . $search_string ) . "'>" . __( 'View forum results', 'kleo_framework' ) . "</a>";
  113. $output .= "</div>";
  114. }
  115.  
  116.  
  117. //if there are members
  118. if ( $members['total'] != 0 ) {
  119.  
  120. $output .= '<div class="kleo-ajax-part kleo-ajax-type-members">';
  121. $output .= '<h4><span>' . __( "Members", 'kleo_framework' ) . '</span></h4>';
  122. foreach ( (array) $members['users'] as $member ) {
  123. $image = '<img src="' . bp_core_fetch_avatar( array(
  124. 'item_id' => $member->ID,
  125. 'width' => 25,
  126. 'height' => 25,
  127. 'html' => false
  128. ) ) . '" class="kleo-rounded" alt="">';
  129. if ( $update = bp_get_user_meta( $member->ID, 'bp_latest_update', true ) ) {
  130. $latest_activity = char_trim( trim( strip_tags( bp_create_excerpt( $update['content'], 50, "..." ) ) ) );
  131. } else {
  132. $latest_activity = '';
  133. }
  134. $output .= "<div class ='kleo_ajax_entry'>";
  135. $output .= "<div class='ajax_search_image'>$image</div>";
  136. $output .= "<div class='ajax_search_content'>";
  137. $output .= "<a href='" . bp_core_get_user_domain( $member->ID ) . "' class='search_title'>";
  138. $output .= $member->display_name;
  139. $output .= "</a>";
  140. $output .= "<span class='search_excerpt'>";
  141. $output .= $latest_activity;
  142. $output .= "</span>";
  143. $output .= "</div>";
  144. $output .= "</div>";
  145. }
  146. $output .= "<a class='ajax_view_all' href='" . esc_url( bp_get_members_directory_permalink() . "?s=" . $search_string ) . "'>" . __( 'View member results', 'kleo_framework' ) . "</a>";
  147. $output .= "</div>";
  148. }
  149.  
  150. //if there are groups
  151. if ( $groups['total'] != 0 ) {
  152.  
  153. $output .= '<div class="kleo-ajax-part kleo-ajax-type-groups">';
  154. $output .= '<h4><span>' . __( "Groups", 'kleo_framework' ) . '</span></h4>';
  155. foreach ( (array) $groups['groups'] as $group ) {
  156. $image = '<img src="' . bp_core_fetch_avatar( array(
  157. 'item_id' => $group->id,
  158. 'object' => 'group',
  159. 'width' => 25,
  160. 'height' => 25,
  161. 'html' => false
  162. ) ) . '" class="kleo-rounded" alt="">';
  163. $output .= "<div class ='kleo_ajax_entry'>";
  164. $output .= "<div class='ajax_search_image'>$image</div>";
  165. $output .= "<div class='ajax_search_content'>";
  166. $output .= "<a href='" . bp_get_group_permalink( $group ) . "' class='search_title'>";
  167. $output .= $group->name;
  168. $output .= "</a>";
  169. $output .= "</div>";
  170. $output .= "</div>";
  171. }
  172. $output .= "<a class='ajax_view_all' href='" . esc_url( bp_get_groups_directory_permalink() . "?s=" . $search_string ) . "'>" . __( 'View group results', 'kleo_framework' ) . "</a>";
  173. $output .= "</div>";
  174. }
  175.  
  176. //if there are posts
  177. if ( ! empty( $posts ) ) {
  178. $post_types = array();
  179. $post_type_obj = array();
  180. foreach ( $posts as $post ) {
  181. $post_types[ $post->post_type ][] = $post;
  182. if ( empty( $post_type_obj[ $post->post_type ] ) ) {
  183. $post_type_obj[ $post->post_type ] = get_post_type_object( $post->post_type );
  184. }
  185. }
  186.  
  187. foreach ( $post_types as $ptype => $post_type ) {
  188. $output .= '<div class="kleo-ajax-part kleo-ajax-type-' . esc_attr( $post_type_obj[ $ptype ]->name ) . '">';
  189. if ( isset( $post_type_obj[ $ptype ]->labels->name ) ) {
  190. $output .= "<h4><span>" . $post_type_obj[ $ptype ]->labels->name . "</span></h4>";
  191. } else {
  192. $output .= "<hr>";
  193. }
  194. $count = 0;
  195. foreach ( $post_type as $post ) {
  196.  
  197. $count ++;
  198. if ( $count > 4 ) {
  199. continue;
  200. }
  201. $format = get_post_format( $post->ID );
  202. if ( $img_url = kleo_get_post_thumbnail_url( $post->ID ) ) {
  203. $image = aq_resize( $img_url, 44, 44, true, true, true );
  204. if ( ! $image ) {
  205. $image = $img_url;
  206. }
  207. $image = '<img src="' . $image . '" class="kleo-rounded">';
  208. } else {
  209. if ( $format == 'video' ) {
  210. $image = "<i class='icon icon-video'></i>";
  211. } elseif ( $format == 'image' || $format == 'gallery' ) {
  212. $image = "<i class='icon icon-picture'></i>";
  213. } else {
  214. $image = "<i class='icon icon-link'></i>";
  215. }
  216. }
  217.  
  218. $excerpt = "";
  219.  
  220. if ( ! empty( $post->post_content ) ) {
  221. $excerpt = $post->post_content;
  222. $excerpt = preg_replace( "/\[(.*?)(?:(\/))?\](?:(.+?)\[\/\2\])?/s", '', $excerpt );
  223. $excerpt = char_trim( trim( strip_tags( $excerpt ) ), 40, "..." );
  224. }
  225. $link = apply_filters( 'kleo_custom_url', get_permalink( $post->ID ) );
  226. $classes = "format-" . $format;
  227. $output .= "<div class ='kleo_ajax_entry $classes'>";
  228. $output .= "<div class='ajax_search_image'>$image</div>";
  229. $output .= "<div class='ajax_search_content'>";
  230. $output .= "<a href='$link' class='search_title'>";
  231. $output .= get_the_title( $post->ID );
  232. $output .= "</a>";
  233. $output .= "<span class='search_excerpt'>";
  234. $output .= $excerpt;
  235. $output .= "</span>";
  236. $output .= "</div>";
  237. $output .= "</div>";
  238. }
  239. $output .= '</div>';
  240. }
  241.  
  242. $output .= "<a class='ajax_view_all' href='" . esc_url( home_url( '/' ) . '?s=' . $search_string ) . "'>" . __( 'View all results', 'kleo_framework' ) . "</a>";
  243. }
  244.  
  245.  
  246.  
  247. echo $output;
  248. die();
  249. }
  250. }
Advertisement
Add Comment
Please, Sign In to add comment