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. $show_in_cat = 0; 
  46. $search_keyword = ''; 
  47. $last_found_search_term = ''; 
  48. $cat_in = 'all'; 
  49. $widget_template = 'sidebar'; 
  50. $found_items = false; 
  51. $total_product = $total_post = $total_page = 0; 
  52. $items_search_default = WC_Predictive_Search_Widgets::get_items_search(); 
  53. $search_in_default = array(); 
  54. foreach ( $items_search_default as $key => $data ) { 
  55. if ( $data['number'] > 0 ) { 
  56. $search_in_default[$key] = $data['number']; 
  57. if ( isset($_REQUEST['row']) && $_REQUEST['row'] > 0) $row = stripslashes( strip_tags( $_REQUEST['row'] ) ); 
  58. if ( isset($_REQUEST['text_lenght']) && $_REQUEST['text_lenght'] >= 0) $text_lenght = stripslashes( strip_tags( $_REQUEST['text_lenght'] ) ); 
  59. if ( isset($_REQUEST['show_price']) && trim($_REQUEST['show_price']) != '') $show_price = stripslashes( strip_tags( $_REQUEST['show_price'] ) ); 
  60. if ( $show_price == 1 ) $show_price = true; else $show_price = false; 
  61. if ( isset($_REQUEST['show_in_cat']) && trim($_REQUEST['show_in_cat']) != '') $show_in_cat = stripslashes( strip_tags( $_REQUEST['show_in_cat'] ) ); 
  62. if ( $show_in_cat == 1 ) $show_in_cat = true; else $show_in_cat = false; 
  63. if ( isset($_REQUEST['q']) && trim($_REQUEST['q']) != '') $search_keyword = stripslashes( strip_tags( $_REQUEST['q'] ) ); 
  64. if ( isset($_REQUEST['cat_in']) && trim($_REQUEST['cat_in']) != '') $cat_in = stripslashes( strip_tags( $_REQUEST['cat_in'] ) ); 
  65. if ( isset($_REQUEST['search_in']) && trim($_REQUEST['search_in']) != '') $search_in = json_decode( stripslashes( $_REQUEST['search_in'] ), true ); 
  66. if ( ! is_array($search_in) || count($search_in) < 1 || array_sum($search_in) < 1) $search_in = $search_in_default; 
  67. if ( isset($_REQUEST['widget_template']) && trim($_REQUEST['widget_template']) != '' ) $widget_template = stripslashes( strip_tags( $_REQUEST['widget_template'] ) ); 
  68.  
  69. if ( isset($_REQUEST['last_search_term']) && trim($_REQUEST['last_search_term']) != '') $last_found_search_term = stripslashes( strip_tags( $_REQUEST['last_search_term'] ) ); 
  70.  
  71. if ( $search_keyword != '' ) { 
  72. $search_list = array(); 
  73. foreach ($search_in as $key => $number) { 
  74. if ( ! isset( $items_search_default[$key] ) ) continue; 
  75. if ($number > 0) 
  76. $search_list[$key] = $key; 
  77.  
  78. $woocommerce_search_focus_enable = false; 
  79. $woocommerce_search_focus_plugin = false; 
  80.  
  81. $all_items = array(); 
  82. $product_list = array(); 
  83. $post_list = array(); 
  84. $page_list = array(); 
  85.  
  86. $permalink_structure = get_option( 'permalink_structure' ); 
  87.  
  88. $product_term_id = 0; 
  89. $post_term_id = 0; 
  90.  
  91. if ( isset( $search_in['product'] ) && $search_in['product'] > 0 ) { 
  92. $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, false, false, $show_in_cat ); 
  93. $total_product = $product_list['total']; 
  94. if ( $total_product > 0 ) { 
  95. $found_items = true; 
  96. $rs_items['product'] = $product_list['items']; 
  97.  
  98. if ( isset( $search_in['post'] ) && $search_in['post'] > 0 ) { 
  99. $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', true, $show_in_cat ); 
  100. $total_post = $post_list['total']; 
  101. if ( $total_post > 0 ) { 
  102. $found_items = true; 
  103. $rs_items['post'] = $post_list['items']; 
  104.  
  105. if ( isset( $search_in['page'] ) && $search_in['page'] > 0 ) { 
  106. $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' ); 
  107. $total_page = $page_list['total']; 
  108. if ( $total_page > 0 ) { 
  109. $found_items = true; 
  110. $rs_items['page'] = $page_list['items']; 
  111.  
  112. if ( $found_items === false ) { 
  113. $nothing_title = sprintf( wc_ps_ict_t__( 'Nothing found', __('Nothing found for "%s".', 'woocommerce-predictive-search' ) ), $search_keyword ); 
  114.  
  115. if ( '' != $last_found_search_term && $last_found_search_term != $search_keyword ) { 
  116. $nothing_title .= ' ' . sprintf( wc_ps_ict_t__( 'Last Found', __('Showing results for last found search term "%s".', 'woocommerce-predictive-search' ) ), $last_found_search_term ); 
  117. $all_items[] = array( 
  118. 'title' => $nothing_title,  
  119. 'keyword' => $search_keyword,  
  120. 'type' => 'nothing' 
  121. ); 
  122. } else { 
  123. foreach ( $search_in as $key => $number ) { 
  124. if ( $number > 0 ) { 
  125. if ( isset( $rs_items[$key] ) ) $all_items = array_merge( $all_items, $rs_items[$key] ); 
  126.  
  127. $search_other = $search_list; 
  128. if ( $total_product < 1 ) { unset($search_list['product']); unset($search_other['product']); 
  129. } elseif ($total_product <= $search_in['product']) { unset($search_list['product']); } 
  130.  
  131. if ( $total_post < 1 ) { unset($search_list['post']); unset($search_other['post']); 
  132. } elseif ($total_post <= $search_in['post']) { unset($search_list['post']); } 
  133.  
  134. if ( $total_page < 1 ) { unset($search_list['page']); unset($search_other['page']); 
  135. } elseif ($total_page <= $search_in['page']) { unset($search_list['page']); } 
  136.  
  137. if ( count( $search_list ) > 0 ) { 
  138. $rs_footer_html = ''; 
  139. foreach ($search_list as $other_rs) { 
  140. if ( $permalink_structure == '') 
  141. $search_in_parameter = '&search_in='.$other_rs; 
  142. else 
  143. $search_in_parameter = '/search-in/'.$other_rs; 
  144. if ( $permalink_structure == '') 
  145. $link_search = get_permalink( $woocommerce_search_page_id ).'&rs='. urlencode($search_keyword) .$search_in_parameter.'&search_other='.implode(", ", $search_other).'&cat_in='.$cat_in; 
  146. else 
  147. $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); 
  148. $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>'; 
  149. $rs_footer_html .= "$rs_item"; 
  150.  
  151. $footertype = 'footerSidebar'; 
  152. if ( 'header' == $widget_template ) { 
  153. $footertype = 'footerHeader'; 
  154. $all_items[] = array( 
  155. 'title' => $search_keyword,  
  156. 'keyword' => $search_keyword,  
  157. 'description' => $rs_footer_html,  
  158. 'type' => $footertype 
  159. ); 
  160.  
  161. header( 'Content-Type: application/json', true, 200 ); 
  162. die( json_encode( $all_items ) ); 
  163. } else { 
  164. header( 'Content-Type: application/json', true, 200 ); 
  165. die( json_encode( array() ) ); 
  166.  
  167.  
  168. public function get_all_results() { 
  169. @ini_set('display_errors', false ); 
  170. global $wc_predictive_search; 
  171.  
  172. $current_lang = ''; 
  173. if ( class_exists('SitePress') ) { 
  174. $current_lang = $_REQUEST['ps_lang']; 
  175.  
  176. $psp = 1; 
  177. $row = 10; 
  178. $search_keyword = ''; 
  179. $cat_in = 'all'; 
  180. $search_in = 'product'; 
  181.  
  182. if ( get_option('woocommerce_search_result_items') > 0 ) $row = get_option('woocommerce_search_result_items'); 
  183.  
  184. if ( isset( $_REQUEST['psp'] ) && $_REQUEST['psp'] > 0 ) $psp = stripslashes( strip_tags( $_REQUEST['psp'] ) ); 
  185. if ( isset( $_REQUEST['q'] ) && trim( $_REQUEST['q'] ) != '' ) $search_keyword = stripslashes( strip_tags( $_REQUEST['q'] ) ); 
  186. if ( isset( $_REQUEST['cat_in'] ) && trim( $_REQUEST['cat_in'] ) != '' ) $cat_in = stripslashes( strip_tags( $_REQUEST['cat_in'] ) ); 
  187. if ( isset( $_REQUEST['search_in'] ) && trim( $_REQUEST['search_in'] ) != '' ) $search_in = stripslashes( strip_tags( $_REQUEST['search_in'] ) ); 
  188.  
  189. $item_list = array( 'total' => 0, 'items' => array() ); 
  190.  
  191. if ( $search_keyword != '' && $search_in != '') { 
  192. $show_sku = false; 
  193. $show_price = false; 
  194. $show_addtocart = false; 
  195. $show_categories = false; 
  196. $show_tags = false; 
  197. if ( get_option('woocommerce_search_sku_enable') == '' || get_option('woocommerce_search_sku_enable') == 'yes' ) $show_sku = true; 
  198. if ( get_option('woocommerce_search_price_enable') == '' || get_option('woocommerce_search_price_enable') == 'yes' ) $show_price = true; 
  199. if ( get_option('woocommerce_search_addtocart_enable') == '' || get_option('woocommerce_search_addtocart_enable') == 'yes' ) $show_addtocart = true; 
  200. if ( get_option('woocommerce_search_categories_enable') == '' || get_option('woocommerce_search_categories_enable') == 'yes' ) $show_categories = true; 
  201. if ( get_option('woocommerce_search_tags_enable') == '' || get_option('woocommerce_search_tags_enable') == 'yes' ) $show_tags = true; 
  202.  
  203. $text_lenght = get_option('woocommerce_search_text_lenght'); 
  204.  
  205. $product_term_id = 0; 
  206. $post_term_id = 0; 
  207.  
  208. $start = ( $psp - 1) * $row; 
  209.  
  210. $woocommerce_search_focus_enable = false; 
  211. $woocommerce_search_focus_plugin = false; 
  212.  
  213. if ( $search_in == 'product' ) { 
  214. $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 ); 
  215. } elseif ( $search_in == 'post' ) { 
  216. $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 ); 
  217. } elseif ( $search_in == 'page' ) { 
  218. $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 ); 
  219.  
  220. header( 'Content-Type: application/json', true, 200 ); 
  221. die( json_encode( $item_list ) ); 
  222.