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( 'plugins_loaded', 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', 'woocommerce-predictive-search' ) ) ); 
  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', 'woocommerce-predictive-search' ) ) ); 
  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', 'woocommerce-predictive-search' ) ),  
  182. 'keyword' => $search_keyword,  
  183. 'type' => 'header' 
  184. ); 
  185.  
  186. $current_db_version = get_option( 'woocommerce_db_version', null ); 
  187.  
  188. foreach ( $search_products as $current_product ) { 
  189.  
  190. $product_id = $current_product->post_id; 
  191. global $product; 
  192. global $post; 
  193.  
  194. if ( version_compare( $current_db_version, '2.0', '<' ) && null !== $current_db_version ) { 
  195. $product = new WC_Product( $product_id ); 
  196. } elseif ( version_compare( WC()->version, '2.2.0', '<' ) ) { 
  197. $product = get_product( $product_id ); 
  198. } else { 
  199. $product = wc_get_product( $product_id ); 
  200.  
  201. $post = get_post( $product_id ); 
  202.  
  203. $product_description = WC_Predictive_Search_Functions::woops_limit_words( strip_tags( WC_Predictive_Search_Functions::strip_shortcodes( strip_shortcodes ( $post->post_content ) ) ), $text_lenght, '...' ); 
  204. if ( trim( $product_description ) == '' ) $product_description = WC_Predictive_Search_Functions::woops_limit_words( strip_tags( WC_Predictive_Search_Functions::strip_shortcodes( strip_shortcodes( $post->post_excerpt ) ) ), $text_lenght, '...' ); 
  205.  
  206. $availability = $product->get_availability(); 
  207. $availability_html = empty( $availability['availability'] ) ? '' : '<span class="stock ' . esc_attr( $availability['class'] ) . '">' . esc_html( $availability['availability'] ) . '</span>'; 
  208.  
  209. $item_data = array( 
  210. 'title' => $current_product->post_title,  
  211. 'keyword' => $current_product->post_title,  
  212. 'url' => $product->get_permalink(),  
  213. 'image_url' => WC_Predictive_Search_Functions::get_product_thumbnail_url( $product_id, 0, 'shop_catalog', 64, 64 ),  
  214. 'description' => $product_description,  
  215. 'stock' => $availability_html,  
  216. 'type' => 'product' 
  217. ); 
  218.  
  219. if ( $show_price ) $item_data['price'] = $product->get_price_html(); 
  220. if ( $show_sku ) { 
  221. global $wc_ps_product_sku_data; 
  222. $item_data['sku'] = stripslashes( $wc_ps_product_sku_data->get_item( $product_id ) ); 
  223. if ( $show_addtocart ) $item_data['addtocart'] = WC_Predictive_Search_Functions::get_product_addtocart( $product ); 
  224. if ( $show_categories ) $item_data['categories'] = WC_Predictive_Search_Functions::get_terms_object( $product_id, 'product_cat' ); 
  225. if ( $show_tags ) $item_data['tags'] = WC_Predictive_Search_Functions::get_terms_object( $product_id, 'product_tag' ); 
  226.  
  227. $item_list['items'][] = $item_data; 
  228.  
  229. $row-- ; 
  230. if ( $row < 1 ) break; 
  231.  
  232. return $item_list; 
  233.  
  234. /** 
  235. * Get array post list 
  236. */ 
  237. 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 ) { 
  238. global $wpdb; 
  239.  
  240. $total_post = 0; 
  241. $have_post = $this->check_product_exsited( $search_keyword, $woocommerce_search_focus_enable, $woocommerce_search_focus_plugin, $post_type, $post_term_id, $current_lang ); 
  242. if ( ! $have_post ) { 
  243. $item_list = array( 'total' => $total_post, 'search_in_name' => ( $post_type == 'post' ) ? wc_ps_ict_t__( 'Posts', __('Posts', 'woocommerce-predictive-search' ) ) : wc_ps_ict_t__( 'Pages', __('Pages', 'woocommerce-predictive-search' ) ) ); 
  244. return $item_list; 
  245.  
  246. $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 ); 
  247.  
  248. $search_posts = $wpdb->get_results( $sql ); 
  249.  
  250. $total_post = count( $search_posts ); 
  251. $item_list = array( 'total' => $total_post, 'search_in_name' => ( $post_type == 'post' ) ? wc_ps_ict_t__( 'Posts', __('Posts', 'woocommerce-predictive-search' ) ) : wc_ps_ict_t__( 'Pages', __('Pages', 'woocommerce-predictive-search' ) ) ); 
  252. if ( $search_posts && $total_post > 0 ) { 
  253. $item_list['items'] = array(); 
  254.  
  255. if ( $include_header ) { 
  256. $item_list['items'][] = array( 
  257. 'title' => ( $post_type == 'post' ) ? wc_ps_ict_t__( 'Posts', __('Posts', 'woocommerce-predictive-search' ) ) : wc_ps_ict_t__( 'Pages', __('Pages', 'woocommerce-predictive-search' ) ),  
  258. 'keyword' => $search_keyword,  
  259. 'type' => 'header' 
  260. ); 
  261.  
  262. foreach ( $search_posts as $item ) { 
  263.  
  264. $post_data = get_post( $item->post_id ); 
  265. $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, '...' ); 
  266. 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, '...' ); 
  267.  
  268. $item_data = array( 
  269. 'title' => $item->post_title,  
  270. 'keyword' => $item->post_title,  
  271. 'url' => get_permalink( $item->post_id ),  
  272. 'image_url' => WC_Predictive_Search_Functions::get_product_thumbnail_url( $item->post_id, 0, 'shop_catalog', 64, 64 ),  
  273. 'description' => $item_description,  
  274. 'type' => $post_type 
  275. ); 
  276.  
  277. if ( $show_categories ) $item_data['categories'] = WC_Predictive_Search_Functions::get_terms_object( $item->post_id, 'category' ); 
  278. if ( $show_tags ) $item_data['tags'] = WC_Predictive_Search_Functions::get_terms_object( $item->post_id, 'post_tag' ); 
  279.  
  280. $item_list['items'][] = $item_data; 
  281.  
  282. $row-- ; 
  283. if ( $row < 1 ) break; 
  284.  
  285. return $item_list; 
  286.  
  287.  
  288. /** 
  289. * Get product categories dropdown 
  290. */ 
  291. public function get_product_categories_nested( $parent = 0, $child_space = '', $cats_excluded = NULL ) { 
  292. global $wp_version; 
  293.  
  294. $categories_list = array(); 
  295.  
  296. if ( is_null( $cats_excluded ) ) { 
  297. global $wc_ps_exclude_data; 
  298. $cats_excluded = $wc_ps_exclude_data->get_array_items( 'product_cat' ); 
  299.  
  300. if ( version_compare( $wp_version, '4.5.0', '<' ) ) { 
  301. $top_categories = get_terms( 'product_cat', array( 
  302. 'hierarchical' => true,  
  303. 'exclude' => $cats_excluded,  
  304. 'parent' => 0,  
  305. ) ); 
  306. } else { 
  307. $top_categories = get_terms( array( 
  308. 'taxonomy' => 'product_cat',  
  309. 'hierarchical' => true,  
  310. 'exclude' => $cats_excluded,  
  311. 'parent' => 0,  
  312. ) ); 
  313.  
  314. if ( ! empty( $top_categories ) && ! is_wp_error( $top_categories ) ) { 
  315.  
  316. foreach( $top_categories as $p_categories_data ) { 
  317. if ( in_array( $p_categories_data->term_id, $cats_excluded ) ) continue; 
  318.  
  319. $child_space = ''; 
  320.  
  321. $category_data = array( 
  322. 'name' => $child_space . $p_categories_data->name,  
  323. 'slug' => $p_categories_data->slug,  
  324. 'url' => get_term_link( $p_categories_data->slug, 'product_cat' ) 
  325. ); 
  326.  
  327. $categories_list[$p_categories_data->term_id] = $category_data; 
  328.  
  329. $child_p_categories = get_terms( 'product_cat', array( 
  330. 'hierarchical' => true,  
  331. 'exclude' => $cats_excluded,  
  332. 'child_of' => $p_categories_data->term_id,  
  333. ) ); 
  334.  
  335. if ( ! empty( $child_p_categories ) && ! is_wp_error( $child_p_categories ) ) { 
  336.  
  337. $current_top_cat = $p_categories_data->term_id; 
  338. $current_parent_cat = $p_categories_data->term_id; 
  339.  
  340. $child_space = '   '; 
  341. foreach( $child_p_categories as $p_categories_data ) { 
  342. if ( in_array( $p_categories_data->term_id, $cats_excluded ) ) continue; 
  343.  
  344. if ( $current_top_cat == $p_categories_data->parent ) { 
  345. $child_space = '   '; 
  346. } elseif( $current_parent_cat == $p_categories_data->parent ) { 
  347. $child_space .= ''; 
  348. } else { 
  349. $child_space .= '   '; 
  350.  
  351. $current_parent_cat = $p_categories_data->parent; 
  352.  
  353. $category_data = array( 
  354. 'name' => $child_space . $p_categories_data->name,  
  355. 'slug' => $p_categories_data->slug,  
  356. 'url' => get_term_link( $p_categories_data->slug, 'product_cat' ) 
  357. ); 
  358.  
  359. $categories_list[$p_categories_data->term_id] = $category_data; 
  360.  
  361. return $categories_list; 
  362.