rdusnr

Untitled

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