WC_Predictive_Search

WooCommerce Predictive Search.

Defined (1)

The class is defined in the following location(s).

/includes/class-wc-predictive-search.php  
  1. class WC_Predictive_Search 
  2.  
  3. public function __construct() { 
  4. // Set Predictive Search Tables 
  5. add_action( 'init', array( $this, 'set_tables_wpdbfix' ), 0 ); 
  6. add_action( 'switch_blog', array( $this, 'set_tables_wpdbfix' ), 0 ); 
  7.  
  8. public function install_databases() { 
  9. global $wc_ps_postmeta_data; 
  10. global $wc_ps_posts_data; 
  11. global $wc_ps_product_sku_data; 
  12. global $wc_ps_exclude_data; 
  13.  
  14. $wc_ps_posts_data->install_database(); 
  15. $wc_ps_postmeta_data->install_database(); 
  16. $wc_ps_product_sku_data->install_database(); 
  17. $wc_ps_exclude_data->install_database(); 
  18.  
  19. public function set_tables_wpdbfix() { 
  20. global $wc_ps_postmeta_data; 
  21. global $wc_ps_posts_data; 
  22. global $wc_ps_product_sku_data; 
  23. global $wc_ps_exclude_data; 
  24.  
  25. $wc_ps_posts_data->set_table_wpdbfix(); 
  26. $wc_ps_postmeta_data->set_table_wpdbfix(); 
  27. $wc_ps_product_sku_data->set_table_wpdbfix(); 
  28. $wc_ps_exclude_data->set_table_wpdbfix(); 
  29.  
  30. public function general_sql( $main_sql ) { 
  31.  
  32. $select_sql = ''; 
  33. if ( is_array( $main_sql['select'] ) && count( $main_sql['select'] ) > 0 ) { 
  34. $select_sql = implode( ', ', $main_sql['select'] ); 
  35. } elseif ( ! is_array( $main_sql['select'] ) ) { 
  36. $select_sql = $main_sql['select']; 
  37.  
  38. $from_sql = ''; 
  39. if ( is_array( $main_sql['from'] ) && count( $main_sql['from'] ) > 0 ) { 
  40. $from_sql = implode( ', ', $main_sql['from'] ); 
  41. } elseif ( ! is_array( $main_sql['from'] ) ) { 
  42. $from_sql = $main_sql['from']; 
  43.  
  44. $join_sql = ''; 
  45. if ( is_array( $main_sql['join'] ) && count( $main_sql['join'] ) > 0 ) { 
  46. $join_sql = implode( ' ', $main_sql['join'] ); 
  47. } elseif ( ! is_array( $main_sql['join'] ) ) { 
  48. $join_sql = $main_sql['join']; 
  49.  
  50. $where_sql = ''; 
  51. $where_search_sql = ''; 
  52. if ( is_array( $main_sql['where'] ) && count( $main_sql['where'] ) > 0 ) { 
  53. if ( isset( $main_sql['where']['search'] ) ) { 
  54. $where_search = $main_sql['where']['search']; 
  55. unset( $main_sql['where']['search'] ); 
  56. if ( is_array( $where_search ) && count( $where_search ) > 0 ) { 
  57. $where_search_sql = implode( ' ', $where_search ); 
  58. } elseif ( ! is_array( $where_search ) ) { 
  59. $where_search_sql = $where_search; 
  60. $where_sql = implode( ' ', $main_sql['where'] ); 
  61. } elseif ( ! is_array( $main_sql['where'] ) ) { 
  62. $where_sql = $main_sql['where']; 
  63.  
  64. $groupby_sql = ''; 
  65. if ( is_array( $main_sql['groupby'] ) && count( $main_sql['groupby'] ) > 0 ) { 
  66. $groupby_sql = implode( ', ', $main_sql['groupby'] ); 
  67. } elseif ( ! is_array( $main_sql['groupby'] ) ) { 
  68. $groupby_sql = $main_sql['groupby']; 
  69.  
  70. $orderby_sql = ''; 
  71. if ( is_array( $main_sql['orderby'] ) && count( $main_sql['orderby'] ) > 0 ) { 
  72. $orderby_sql = implode( ', ', $main_sql['orderby'] ); 
  73. } elseif ( ! is_array( $main_sql['orderby'] ) ) { 
  74. $orderby_sql = $main_sql['orderby']; 
  75.  
  76. $limit_sql = $main_sql['limit']; 
  77.  
  78. $sql = 'SELECT '; 
  79. if ( '' != trim( $select_sql ) ) { 
  80. $sql .= $select_sql; 
  81.  
  82. $sql .= ' FROM '; 
  83. if ( '' != trim( $from_sql ) ) { 
  84. $sql .= $from_sql . ' '; 
  85.  
  86. if ( '' != trim( $join_sql ) ) { 
  87. $sql .= $join_sql . ' '; 
  88.  
  89. if ( '' != trim( $where_sql ) || '' != trim( $where_search_sql ) ) { 
  90. $sql .= ' WHERE '; 
  91. $sql .= $where_sql . ' '; 
  92.  
  93. if ( '' != trim( $where_search_sql ) ) { 
  94. if ( '' != trim( $where_sql ) ) { 
  95. $sql .= ' AND ( ' . $where_search_sql . ' ) '; 
  96. } else { 
  97. $sql .= $where_search_sql; 
  98.  
  99. if ( '' != trim( $groupby_sql ) ) { 
  100. $sql .= ' GROUP BY '; 
  101. $sql .= $groupby_sql . ' '; 
  102.  
  103. if ( '' != trim( $orderby_sql ) ) { 
  104. $sql .= ' ORDER BY '; 
  105. $sql .= $orderby_sql . ' '; 
  106.  
  107. if ( '' != trim( $limit_sql ) ) { 
  108. $sql .= ' LIMIT '; 
  109. $sql .= $limit_sql . ' '; 
  110.  
  111. return $sql; 
  112.  
  113. public function get_product_search_sql( $search_keyword, $row, $start = 0, $woocommerce_search_focus_enable, $woocommerce_search_focus_plugin, $post_type = 'product', $term_id = 0, $current_lang = '', $check_exsited = false ) { 
  114. global $wpdb; 
  115.  
  116. $row += 1; 
  117.  
  118. $search_keyword_nospecial = preg_replace( "/[^a-zA-Z0-9_.\s]/", " ", $search_keyword ); 
  119. if ( $search_keyword == $search_keyword_nospecial ) { 
  120. $search_keyword_nospecial = ''; 
  121. } else { 
  122. $search_keyword_nospecial = $wpdb->esc_like( trim( $search_keyword_nospecial ) ); 
  123.  
  124. $search_keyword = $wpdb->esc_like( trim( $search_keyword ) ); 
  125.  
  126. $main_sql = array(); 
  127. $wpml_sql = array(); 
  128.  
  129. global $wc_ps_posts_data; 
  130. $main_sql = $wc_ps_posts_data->get_sql( $search_keyword, $search_keyword_nospecial, $post_type, $row, $start, $check_exsited ); 
  131.  
  132. if ( class_exists('SitePress') && '' != $current_lang ) { 
  133. $wpml_sql['join'] = " INNER JOIN ".$wpdb->prefix."icl_translations AS ic ON (ic.element_id = pp.post_id) "; 
  134. $wpml_sql['where'][] = " AND ic.language_code = '".$current_lang."' AND ic.element_type = 'post_{$post_type}' "; 
  135.  
  136. $main_sql = array_merge_recursive( $main_sql, $wpml_sql ); 
  137.  
  138. $sql = $this->general_sql( $main_sql ); 
  139.  
  140. return $sql; 
  141.  
  142. /** 
  143. * Check product is exsited from search term 
  144. */ 
  145. public function check_product_exsited( $search_keyword, $woocommerce_search_focus_enable, $woocommerce_search_focus_plugin, $post_type = 'product', $term_id = 0, $current_lang = '' ) { 
  146. global $wpdb; 
  147.  
  148. $sql = $this->get_product_search_sql( $search_keyword, 1, 0, $woocommerce_search_focus_enable, $woocommerce_search_focus_plugin, $post_type, $term_id, $current_lang, true ); 
  149.  
  150. $sql = "SELECT EXISTS( " . $sql . ")"; 
  151.  
  152. $have_item = $wpdb->get_var( $sql ); 
  153. if ( $have_item == '1' ) { 
  154. return true; 
  155. } else { 
  156. return false; 
  157.  
  158.  
  159. /** 
  160. * Get array product list 
  161. */ 
  162. public function get_product_results( $search_keyword, $row, $start = 0, $woocommerce_search_focus_enable, $woocommerce_search_focus_plugin, $product_term_id = 0, $text_lenght = 100, $current_lang = '', $include_header = true , $show_price = true, $show_sku = false, $show_addtocart = false, $show_categories = false, $show_tags = false ) { 
  163. global $wpdb; 
  164.  
  165. $have_product = $this->check_product_exsited( $search_keyword, $woocommerce_search_focus_enable, $woocommerce_search_focus_plugin, 'product', $product_term_id, $current_lang ); 
  166. if ( ! $have_product ) { 
  167. $item_list = array( 'total' => 0, 'search_in_name' => wc_ps_ict_t__( 'Product Name', __('Product Name', 'woops') ) ); 
  168. return $item_list; 
  169.  
  170. $sql = $this->get_product_search_sql( $search_keyword, $row, $start, $woocommerce_search_focus_enable, $woocommerce_search_focus_plugin, 'product', $product_term_id, $current_lang, false ); 
  171.  
  172. $search_products = $wpdb->get_results( $sql ); 
  173.  
  174. $total_product = count( $search_products ); 
  175. $item_list = array( 'total' => $total_product, 'search_in_name' => wc_ps_ict_t__( 'Product Name', __('Product Name', 'woops') ) ); 
  176. if ( $search_products && $total_product > 0 ) { 
  177. $item_list['items'] = array(); 
  178.  
  179. if ( $include_header ) { 
  180. $item_list['items'][] = array( 
  181. 'title' => wc_ps_ict_t__( 'Product Name', __('Product Name', 'woops') ),  
  182. 'keyword' => $search_keyword,  
  183. 'type' => 'header' 
  184. ); 
  185.  
  186. foreach ( $search_products as $product ) { 
  187.  
  188. $product_data = get_post( $product->post_id ); 
  189. $product_description = WC_Predictive_Search_Functions::woops_limit_words( strip_tags( WC_Predictive_Search_Functions::strip_shortcodes( strip_shortcodes ( $product_data->post_content ) ) ), $text_lenght, '...' ); 
  190. if ( trim( $product_description ) == '' ) $product_description = WC_Predictive_Search_Functions::woops_limit_words( strip_tags( WC_Predictive_Search_Functions::strip_shortcodes( strip_shortcodes( $product_data->post_excerpt ) ) ), $text_lenght, '...' ); 
  191.  
  192. $item_data = array( 
  193. 'title' => $product->post_title,  
  194. 'keyword' => $product->post_title,  
  195. 'url' => get_permalink( $product->post_id ),  
  196. 'image_url' => WC_Predictive_Search_Functions::get_product_thumbnail_url( $product->post_id, 'shop_catalog', 64, 64 ),  
  197. 'description' => $product_description,  
  198. 'type' => 'product' 
  199. ); 
  200.  
  201. if ( $show_price ) $item_data['price'] = WC_Predictive_Search_Functions::get_product_price( $product->post_id ); 
  202. if ( $show_sku ) { 
  203. global $wc_ps_product_sku_data; 
  204. $item_data['sku'] = stripslashes( $wc_ps_product_sku_data->get_item( $product->post_id ) ); 
  205. if ( $show_addtocart ) $item_data['addtocart'] = WC_Predictive_Search_Functions::get_product_addtocart( $product->post_id ); 
  206. if ( $show_categories ) $item_data['categories'] = WC_Predictive_Search_Functions::get_terms_object( $product->post_id, 'product_cat' ); 
  207. if ( $show_tags ) $item_data['tags'] = WC_Predictive_Search_Functions::get_terms_object( $product->post_id, 'product_tag' ); 
  208.  
  209. $item_list['items'][] = $item_data; 
  210.  
  211. $row-- ; 
  212. if ( $row < 1 ) break; 
  213.  
  214. return $item_list; 
  215.  
  216. /** 
  217. * Get array post list 
  218. */ 
  219. public function get_post_results( $search_keyword, $row, $start = 0, $woocommerce_search_focus_enable, $woocommerce_search_focus_plugin, $post_term_id = 0, $text_lenght = 100, $current_lang = '', $post_type = 'post', $include_header = true , $show_categories = false, $show_tags = false ) { 
  220. global $wpdb; 
  221.  
  222. $total_post = 0; 
  223. $have_post = $this->check_product_exsited( $search_keyword, $woocommerce_search_focus_enable, $woocommerce_search_focus_plugin, $post_type, $post_term_id, $current_lang ); 
  224. if ( ! $have_post ) { 
  225. $item_list = array( 'total' => $total_post, 'search_in_name' => ( $post_type == 'post' ) ? wc_ps_ict_t__( 'Posts', __('Posts', 'woops') ) : wc_ps_ict_t__( 'Pages', __('Pages', 'woops') ) ); 
  226. return $item_list; 
  227.  
  228. $sql = $this->get_product_search_sql( $search_keyword, $row, $start, $woocommerce_search_focus_enable, $woocommerce_search_focus_plugin, $post_type, $post_term_id, $current_lang, false ); 
  229.  
  230. $search_posts = $wpdb->get_results( $sql ); 
  231.  
  232. $total_post = count( $search_posts ); 
  233. $item_list = array( 'total' => $total_post, 'search_in_name' => ( $post_type == 'post' ) ? wc_ps_ict_t__( 'Posts', __('Posts', 'woops') ) : wc_ps_ict_t__( 'Pages', __('Pages', 'woops') ) ); 
  234. if ( $search_posts && $total_post > 0 ) { 
  235. $item_list['items'] = array(); 
  236.  
  237. if ( $include_header ) { 
  238. $item_list['items'][] = array( 
  239. 'title' => ( $post_type == 'post' ) ? wc_ps_ict_t__( 'Posts', __('Posts', 'woops') ) : wc_ps_ict_t__( 'Pages', __('Pages', 'woops') ),  
  240. 'keyword' => $search_keyword,  
  241. 'type' => 'header' 
  242. ); 
  243.  
  244. foreach ( $search_posts as $item ) { 
  245.  
  246. $post_data = get_post( $item->post_id ); 
  247. $item_description = WC_Predictive_Search_Functions::woops_limit_words( strip_tags( WC_Predictive_Search_Functions::strip_shortcodes( strip_shortcodes ( $post_data->post_content ) ) ), $text_lenght, '...' ); 
  248. if ( trim( $item_description ) == '' ) $item_description = WC_Predictive_Search_Functions::woops_limit_words( strip_tags( WC_Predictive_Search_Functions::strip_shortcodes( strip_shortcodes( $post_data->post_excerpt ) ) ), $text_lenght, '...' ); 
  249.  
  250. $item_data = array( 
  251. 'title' => $item->post_title,  
  252. 'keyword' => $item->post_title,  
  253. 'url' => get_permalink( $item->post_id ),  
  254. 'image_url' => WC_Predictive_Search_Functions::get_product_thumbnail_url( $item->post_id, 'shop_catalog', 64, 64 ),  
  255. 'description' => $item_description,  
  256. 'type' => $post_type 
  257. ); 
  258.  
  259. if ( $show_categories ) $item_data['categories'] = WC_Predictive_Search_Functions::get_terms_object( $item->post_id, 'category' ); 
  260. if ( $show_tags ) $item_data['tags'] = WC_Predictive_Search_Functions::get_terms_object( $item->post_id, 'post_tag' ); 
  261.  
  262. $item_list['items'][] = $item_data; 
  263.  
  264. $row-- ; 
  265. if ( $row < 1 ) break; 
  266.  
  267. return $item_list; 
  268.  
  269.  
  270. /** 
  271. * Get product categories dropdown 
  272. */ 
  273. public function get_product_categories_nested( $parent = 0, $child_space = '', $cats_excluded = NULL ) { 
  274.  
  275. $categories_list = array(); 
  276.  
  277. if ( is_null( $cats_excluded ) ) { 
  278. global $wc_ps_exclude_data; 
  279. $cats_excluded = $wc_ps_exclude_data->get_array_items( 'product_cat' ); 
  280.  
  281. $results_p_categories = get_terms( array( 
  282. 'taxonomy' => 'product_cat',  
  283. 'parent' => $parent,  
  284. 'exclude' => $cats_excluded,  
  285. ) ); 
  286.  
  287. if ( $results_p_categories ) { 
  288. foreach( $results_p_categories as $p_categories_data ) { 
  289. if ( in_array( $p_categories_data->term_id, $cats_excluded ) ) continue; 
  290.  
  291. $categories_list[$p_categories_data->term_id] = array( 
  292. 'name' => $child_space . $p_categories_data->name,  
  293. 'slug' => $p_categories_data->slug,  
  294. 'url' => get_term_link( $p_categories_data->slug, 'product_cat' ) 
  295. ); 
  296.  
  297. $child_categories_list = $this->get_product_categories_nested( $p_categories_data->term_id, $child_space . '   ', $cats_excluded ); 
  298.  
  299. $categories_list = array_merge( $categories_list, $child_categories_list ); 
  300.  
  301. return $categories_list; 
  302.