WC_Predictive_Search_Legacy_API

The WooCommerce Predictive Search LITE WC Predictive Search Legacy API class.

Defined (1)

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

/includes/class-legacy-api.php  
  1. class WC_Predictive_Search_Legacy_API { 
  2.  
  3. /** @var string $base the route base */ 
  4. protected $base = '/wc_ps_legacy_api'; 
  5. protected $base_tag = 'wc_ps_legacy_api'; 
  6.  
  7. /** 
  8. * Default contructor 
  9. */ 
  10. public function __construct() { 
  11. add_action( 'woocommerce_api_' . $this->base_tag, array( $this, 'wc_ps_api_handler' ) ); 
  12.  
  13. public function get_legacy_api_url() { 
  14.  
  15. $legacy_api_url = WC()->api_request_url( $this->base_tag ); 
  16. $legacy_api_url = str_replace( array( 'https:', 'http:' ), '', $legacy_api_url ); 
  17.  
  18. return apply_filters( 'wc_ps_legacy_api_url', $legacy_api_url ); 
  19.  
  20. public function wc_ps_api_handler() { 
  21. if ( isset( $_REQUEST['action'] ) ) { 
  22. $action = addslashes( trim( $_REQUEST['action'] ) ); 
  23. switch ( $action ) { 
  24. case 'get_result_popup' : 
  25. $this->get_result_popup(); 
  26. break; 
  27.  
  28. case 'get_results' : 
  29. $this->get_all_results(); 
  30. break; 
  31.  
  32. public function get_result_popup() { 
  33. @ini_set('display_errors', false ); 
  34. global $woocommerce_search_page_id; 
  35. global $wc_predictive_search; 
  36.  
  37. $current_lang = ''; 
  38. if ( class_exists('SitePress') ) { 
  39. $current_lang = $_REQUEST['ps_lang']; 
  40.  
  41. $rs_items = array(); 
  42. $row = 6; 
  43. $text_lenght = 100; 
  44. $show_price = 0; 
  45. $search_keyword = ''; 
  46. $cat_in = 'all'; 
  47. $widget_template = 'sidebar'; 
  48. $found_items = false; 
  49. $total_product = $total_post = $total_page = 0; 
  50. $items_search_default = WC_Predictive_Search_Widgets::get_items_search(); 
  51. $search_in_default = array(); 
  52. foreach ( $items_search_default as $key => $data ) { 
  53. if ( $data['number'] > 0 ) { 
  54. $search_in_default[$key] = $data['number']; 
  55. if ( isset($_REQUEST['row']) && $_REQUEST['row'] > 0) $row = stripslashes( strip_tags( $_REQUEST['row'] ) ); 
  56. if ( isset($_REQUEST['text_lenght']) && $_REQUEST['text_lenght'] >= 0) $text_lenght = stripslashes( strip_tags( $_REQUEST['text_lenght'] ) ); 
  57. if ( isset($_REQUEST['show_price']) && trim($_REQUEST['show_price']) != '') $show_price = stripslashes( strip_tags( $_REQUEST['show_price'] ) ); 
  58. if ( $show_price == 1 ) $show_price = true; else $show_price = false; 
  59. if ( isset($_REQUEST['q']) && trim($_REQUEST['q']) != '') $search_keyword = stripslashes( strip_tags( $_REQUEST['q'] ) ); 
  60. if ( isset($_REQUEST['cat_in']) && trim($_REQUEST['cat_in']) != '') $cat_in = stripslashes( strip_tags( $_REQUEST['cat_in'] ) ); 
  61. if ( isset($_REQUEST['search_in']) && trim($_REQUEST['search_in']) != '') $search_in = json_decode( stripslashes( $_REQUEST['search_in'] ), true ); 
  62. if ( ! is_array($search_in) || count($search_in) < 1 || array_sum($search_in) < 1) $search_in = $search_in_default; 
  63. if ( isset($_REQUEST['widget_template']) && trim($_REQUEST['widget_template']) != '' ) $widget_template = stripslashes( strip_tags( $_REQUEST['widget_template'] ) ); 
  64.  
  65. if ( $search_keyword != '' ) { 
  66. $search_list = array(); 
  67. foreach ($search_in as $key => $number) { 
  68. if ( ! isset( $items_search_default[$key] ) ) continue; 
  69. if ($number > 0) 
  70. $search_list[$key] = $key; 
  71.  
  72. $woocommerce_search_focus_enable = false; 
  73. $woocommerce_search_focus_plugin = false; 
  74.  
  75. $all_items = array(); 
  76. $product_list = array(); 
  77. $post_list = array(); 
  78. $page_list = array(); 
  79.  
  80. $permalink_structure = get_option( 'permalink_structure' ); 
  81.  
  82. $product_term_id = 0; 
  83. $post_term_id = 0; 
  84.  
  85. if ( isset( $search_in['product'] ) && $search_in['product'] > 0 ) { 
  86. $product_list = $wc_predictive_search->get_product_results( $search_keyword, $search_in['product'], 0, $woocommerce_search_focus_enable, $woocommerce_search_focus_plugin, $product_term_id, $text_lenght, $current_lang, true, $show_price ); 
  87. $total_product = $product_list['total']; 
  88. if ( $total_product > 0 ) { 
  89. $found_items = true; 
  90. $rs_items['product'] = $product_list['items']; 
  91.  
  92. if ( isset( $search_in['post'] ) && $search_in['post'] > 0 ) { 
  93. $post_list = $wc_predictive_search->get_post_results( $search_keyword, $search_in['post'], 0, $woocommerce_search_focus_enable, $woocommerce_search_focus_plugin, $post_term_id, $text_lenght, $current_lang, 'post' ); 
  94. $total_post = $post_list['total']; 
  95. if ( $total_post > 0 ) { 
  96. $found_items = true; 
  97. $rs_items['post'] = $post_list['items']; 
  98.  
  99. if ( isset( $search_in['page'] ) && $search_in['page'] > 0 ) { 
  100. $page_list = $wc_predictive_search->get_post_results( $search_keyword, $search_in['page'], 0, $woocommerce_search_focus_enable, $woocommerce_search_focus_plugin, 0, $text_lenght, $current_lang, 'page' ); 
  101. $total_page = $page_list['total']; 
  102. if ( $total_page > 0 ) { 
  103. $found_items = true; 
  104. $rs_items['page'] = $page_list['items']; 
  105.  
  106. if ( $found_items === false ) { 
  107. $all_items[] = array( 
  108. 'title' => wc_ps_ict_t__( 'Nothing found', __('Nothing found for that name. Try a different spelling or name.', 'woops') ),  
  109. 'keyword' => $search_keyword,  
  110. 'type' => 'nothing' 
  111. ); 
  112. } else { 
  113. foreach ( $search_in as $key => $number ) { 
  114. if ( $number > 0 ) { 
  115. if ( isset( $rs_items[$key] ) ) $all_items = array_merge( $all_items, $rs_items[$key] ); 
  116.  
  117. $search_other = $search_list; 
  118. if ( $total_product < 1 ) { unset($search_list['product']); unset($search_other['product']); 
  119. } elseif ($total_product <= $search_in['product']) { unset($search_list['product']); } 
  120.  
  121. if ( $total_post < 1 ) { unset($search_list['post']); unset($search_other['post']); 
  122. } elseif ($total_post <= $search_in['post']) { unset($search_list['post']); } 
  123.  
  124. if ( $total_page < 1 ) { unset($search_list['page']); unset($search_other['page']); 
  125. } elseif ($total_page <= $search_in['page']) { unset($search_list['page']); } 
  126.  
  127. if ( count( $search_list ) > 0 ) { 
  128. $rs_footer_html = ''; 
  129. foreach ($search_list as $other_rs) { 
  130. if ( $permalink_structure == '') 
  131. $search_in_parameter = '&search_in='.$other_rs; 
  132. else 
  133. $search_in_parameter = '/search-in/'.$other_rs; 
  134. if ( $permalink_structure == '') 
  135. $link_search = get_permalink( $woocommerce_search_page_id ).'&rs='. urlencode($search_keyword) .$search_in_parameter.'&search_other='.implode(", ", $search_other).'&cat_in='.$cat_in; 
  136. else 
  137. $link_search = rtrim( get_permalink( $woocommerce_search_page_id ), '/' ).'/keyword/'. urlencode($search_keyword) .$search_in_parameter.'/cat-in/'.$cat_in.'/search-other/'.implode(", ", $search_other); 
  138. $rs_item = '<a href="'.$link_search.'">'.$items_search_default[$other_rs]['name'].' <i class="fa fa-caret-right see_more_arrow" aria-hidden="true"></i></a>'; 
  139. $rs_footer_html .= "$rs_item"; 
  140.  
  141. $footertype = 'footerSidebar'; 
  142. if ( 'header' == $widget_template ) { 
  143. $footertype = 'footerHeader'; 
  144. $all_items[] = array( 
  145. 'title' => $search_keyword,  
  146. 'keyword' => $search_keyword,  
  147. 'description' => $rs_footer_html,  
  148. 'type' => $footertype 
  149. ); 
  150.  
  151. header( 'Content-Type: application/json', true, 200 ); 
  152. die( json_encode( $all_items ) ); 
  153. } else { 
  154. header( 'Content-Type: application/json', true, 200 ); 
  155. die( json_encode( array() ) ); 
  156.  
  157.  
  158. public function get_all_results() { 
  159. @ini_set('display_errors', false ); 
  160. global $wc_predictive_search; 
  161.  
  162. $current_lang = ''; 
  163. if ( class_exists('SitePress') ) { 
  164. $current_lang = $_REQUEST['ps_lang']; 
  165.  
  166. $psp = 1; 
  167. $row = 10; 
  168. $search_keyword = ''; 
  169. $cat_in = 'all'; 
  170. $search_in = 'product'; 
  171.  
  172. if ( get_option('woocommerce_search_result_items') > 0 ) $row = get_option('woocommerce_search_result_items'); 
  173.  
  174. if ( isset( $_REQUEST['psp'] ) && $_REQUEST['psp'] > 0 ) $psp = stripslashes( strip_tags( $_REQUEST['psp'] ) ); 
  175. if ( isset( $_REQUEST['q'] ) && trim( $_REQUEST['q'] ) != '' ) $search_keyword = stripslashes( strip_tags( $_REQUEST['q'] ) ); 
  176. if ( isset( $_REQUEST['cat_in'] ) && trim( $_REQUEST['cat_in'] ) != '' ) $cat_in = stripslashes( strip_tags( $_REQUEST['cat_in'] ) ); 
  177. if ( isset( $_REQUEST['search_in'] ) && trim( $_REQUEST['search_in'] ) != '' ) $search_in = stripslashes( strip_tags( $_REQUEST['search_in'] ) ); 
  178.  
  179. $item_list = array( 'total' => 0, 'items' => array() ); 
  180.  
  181. if ( $search_keyword != '' && $search_in != '') { 
  182. $show_sku = false; 
  183. $show_price = false; 
  184. $show_addtocart = false; 
  185. $show_categories = false; 
  186. $show_tags = false; 
  187. if ( get_option('woocommerce_search_sku_enable') == '' || get_option('woocommerce_search_sku_enable') == 'yes' ) $show_sku = true; 
  188. if ( get_option('woocommerce_search_price_enable') == '' || get_option('woocommerce_search_price_enable') == 'yes' ) $show_price = true; 
  189. if ( get_option('woocommerce_search_addtocart_enable') == '' || get_option('woocommerce_search_addtocart_enable') == 'yes' ) $show_addtocart = true; 
  190. if ( get_option('woocommerce_search_categories_enable') == '' || get_option('woocommerce_search_categories_enable') == 'yes' ) $show_categories = true; 
  191. if ( get_option('woocommerce_search_tags_enable') == '' || get_option('woocommerce_search_tags_enable') == 'yes' ) $show_tags = true; 
  192.  
  193. $text_lenght = get_option('woocommerce_search_text_lenght'); 
  194.  
  195. $product_term_id = 0; 
  196. $post_term_id = 0; 
  197.  
  198. $start = ( $psp - 1) * $row; 
  199.  
  200. $woocommerce_search_focus_enable = false; 
  201. $woocommerce_search_focus_plugin = false; 
  202.  
  203. if ( $search_in == 'product' ) { 
  204. $item_list = $wc_predictive_search->get_product_results( $search_keyword, $row, $start, $woocommerce_search_focus_enable, $woocommerce_search_focus_plugin, $product_term_id, $text_lenght, $current_lang, false, $show_price, $show_sku, $show_addtocart, $show_categories, $show_tags ); 
  205. } elseif ( $search_in == 'post' ) { 
  206. $item_list = $wc_predictive_search->get_post_results( $search_keyword, $row, $start, $woocommerce_search_focus_enable, $woocommerce_search_focus_plugin, $post_term_id, $text_lenght, $current_lang, 'post', false , $show_categories, $show_tags ); 
  207. } elseif ( $search_in == 'page' ) { 
  208. $item_list = $wc_predictive_search->get_post_results( $search_keyword, $row, $start, $woocommerce_search_focus_enable, $woocommerce_search_focus_plugin, 0, $text_lenght, $current_lang, 'page', false ); 
  209.  
  210. header( 'Content-Type: application/json', true, 200 ); 
  211. die( json_encode( $item_list ) ); 
  212.