rdusnr

Untitled

Jul 19th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.16 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' => array('publish','inherit'),
  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( $post->post_type == 'attachment') {
  184. $img_url = wp_get_attachment_thumb_url( $post->ID );
  185. $image = '<img src="'.aq_resize( $img_url, 44, 44, true, true, true ).'" class="kleo-rounded"/>';
  186. } else {
  187. if ($img_url = kleo_get_post_thumbnail_url($post->ID)) {
  188. $image = aq_resize($img_url, 44, 44, true, true, true);
  189. if (!$image) {
  190. $image = $img_url;
  191. }
  192. $image = '<img src="' . $image . '" class="kleo-rounded">';
  193. } else {
  194. if ($format == 'video') {
  195. $image = "<i class='icon icon-video'></i>";
  196. } elseif ($format == 'image' || $format == 'gallery') {
  197. $image = "<i class='icon icon-picture'></i>";
  198. } else {
  199. $image = "<i class='icon icon-link'></i>";
  200. }
  201. }
  202. }
  203. $excerpt = "";
  204.  
  205. if ( ! empty( $post->post_content ) ) {
  206. $excerpt = $post->post_content;
  207. $excerpt = preg_replace( "/\[(.*?)(?:(\/))?\](?:(.+?)\[\/\2\])?/s", '', $excerpt );
  208. $excerpt = wp_strip_all_tags($excerpt); //added to remove gogole adsense code from search excerpt
  209. $excerpt = char_trim( trim( strip_tags( $excerpt ) ), 40, "..." );
  210. }
  211. $link = apply_filters( 'kleo_custom_url', get_permalink( $post->ID ) );
  212. $classes = "format-" . $format;
  213. $output .= "<div class ='kleo_ajax_entry $classes'>";
  214. $output .= "<div class='ajax_search_image'>$image</div>";
  215. $output .= "<div class='ajax_search_content'>";
  216. $output .= "<a href='$link' class='search_title'>";
  217. $output .= get_the_title( $post->ID );
  218. $output .= "</a>";
  219. $output .= "<span class='search_excerpt'>";
  220. $output .= $excerpt;
  221. $output .= "</span>";
  222. $output .= "</div>";
  223. $output .= "</div>";
  224. }
  225. $output .= '</div>';
  226. }
  227.  
  228. if ( ! empty( $post_type_str ) ) {
  229. if ( count( $post_type_str ) > 1 ) {
  230. $search_str_posts = '&post_type[]=' . implode( ',', $post_type_str );
  231. } else {
  232. $search_str_posts = '&post_type=' . implode( ',', $post_type_str );
  233. }
  234.  
  235. } else {
  236. $search_str_posts = '';
  237. }
  238.  
  239. $output .= "<a class='ajax_view_all' href='" . esc_url( home_url( '/' ) . '?s=' . $search_string ) . $search_str_posts . "'>" . __( 'View all results', 'kleo_framework' ) . "</a>";
  240. }
  241.  
  242. /* Forums topics search */
  243. if ( ! empty( $forums ) ) {
  244. $output .= '<div class="kleo-ajax-part kleo-ajax-type-forums">';
  245. $output .= '<h4><span>' . __( "Forums", 'kleo_framework' ) . '</span></h4>';
  246.  
  247. $i = 0;
  248. foreach ( $forums as $fk => $forum ) {
  249.  
  250. $i ++;
  251. if ( $i <= 4 ) {
  252. $image = "<i class='icon icon-chat-1'></i>";
  253.  
  254. $output .= "<div class ='kleo_ajax_entry'>";
  255. $output .= "<div class='ajax_search_image'>$image</div>";
  256. $output .= "<div class='ajax_search_content'>";
  257. $output .= "<a href='" . $forum['url'] . "' class='search_title'>";
  258. $output .= $forum['name'];
  259. $output .= "</a>";
  260. //$output .= "<span class='search_excerpt'>";
  261. //$output .= $latest_activity;
  262. //$output .= "</span>";
  263. $output .= "</div>";
  264. $output .= "</div>";
  265. }
  266. }
  267. $output .= "<a class='ajax_view_all' href='" . esc_url( bbp_get_search_url() . "?bbp_search=" . $search_string ) . "'>" . __( 'View forum results', 'kleo_framework' ) . "</a>";
  268. $output .= "</div>";
  269. }
  270.  
  271.  
  272. echo $output;
  273. die();
  274. }
  275. }
Add Comment
Please, Sign In to add comment