WC_Predictive_Search_Hook_Backbone

WooCommerce Predictive Search Hook Backbone.

Defined (1)

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

/classes/class-wc-predictive-search-backbone.php  
  1. class WC_Predictive_Search_Hook_Backbone 
  2. public function __construct() { 
  3.  
  4. // Add script into footer to hanlde the event from widget, popup 
  5. add_action( 'wp_enqueue_scripts', array( $this, 'register_plugin_scripts' ) ); 
  6. add_action( 'wp_enqueue_scripts', array( $this, 'include_result_shortcode_script' ), 11 ); 
  7.  
  8. // Include google fonts into header 
  9. add_action( 'wp_enqueue_scripts', array( $this, 'add_google_fonts'), 9 ); 
  10.  
  11. public function add_google_fonts() { 
  12. global $wc_predictive_search_fonts_face; 
  13.  
  14. $google_fonts = array(); 
  15.  
  16. global $wc_predictive_search_sidebar_template_settings; 
  17.  
  18. $google_fonts[] = $wc_predictive_search_sidebar_template_settings['sidebar_category_dropdown_font']['face']; 
  19. $google_fonts[] = $wc_predictive_search_sidebar_template_settings['sidebar_input_font']['face']; 
  20. $google_fonts[] = $wc_predictive_search_sidebar_template_settings['sidebar_popup_heading_font']['face']; 
  21. $google_fonts[] = $wc_predictive_search_sidebar_template_settings['sidebar_popup_product_name_font']['face']; 
  22. $google_fonts[] = $wc_predictive_search_sidebar_template_settings['sidebar_popup_product_sku_font']['face']; 
  23. $google_fonts[] = $wc_predictive_search_sidebar_template_settings['sidebar_popup_product_price_font']['face']; 
  24. $google_fonts[] = $wc_predictive_search_sidebar_template_settings['sidebar_popup_product_desc_font']['face']; 
  25. $google_fonts[] = $wc_predictive_search_sidebar_template_settings['sidebar_popup_product_stock_qty_font']['face']; 
  26. $google_fonts[] = $wc_predictive_search_sidebar_template_settings['sidebar_popup_product_category_font']['face']; 
  27. $google_fonts[] = $wc_predictive_search_sidebar_template_settings['sidebar_popup_seemore_font']['face']; 
  28. $google_fonts[] = $wc_predictive_search_sidebar_template_settings['sidebar_popup_more_link_font']['face']; 
  29.  
  30.  
  31. $wc_predictive_search_fonts_face->generate_google_webfonts( $google_fonts ); 
  32.  
  33. public function register_plugin_scripts() { 
  34. global $woocommerce_search_page_id; 
  35.  
  36. $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; 
  37. $ps_suffix = '.min'; 
  38. $ps_is_debug = get_option( 'woocommerce_search_is_debug', 'yes' ); 
  39. if ( 'yes' == $ps_is_debug ) { 
  40. $ps_suffix = ''; 
  41. ?> 
  42. <!-- Predictive Search Widget Template Registered --> 
  43. <script type="text/template" id="wc_psearch_tempTpl"> 
  44. <?php echo esc_js( "This's temp Template from Predictive Search" ); ?> 
  45. </script> 
  46. <?php 
  47. wc_ps_get_popup_item_tpl(); 
  48. wc_ps_get_popup_footer_sidebar_tpl(); 
  49. wc_ps_get_popup_footer_header_tpl(); 
  50. ?> 
  51.  
  52. <?php 
  53. // If don't have any plugin or theme register font awesome style then register it from plugin framework 
  54. if ( ! wp_style_is( 'font-awesome-styles', 'registered' ) ) { 
  55. global $wc_predictive_search_admin_interface; 
  56. $wc_predictive_search_admin_interface->register_fontawesome_style(); 
  57.  
  58. wp_register_style( 'wc-predictive-search-style', WOOPS_CSS_URL . '/wc_predictive_search.css', array( 'font-awesome-styles' ), WOOPS_VERSION, 'all' ); 
  59.  
  60. $_upload_dir = wp_upload_dir(); 
  61. global $wc_predictive_search_less; 
  62. $have_dynamic_style = false; 
  63. if ( file_exists( $_upload_dir['basedir'] . '/sass/'.$wc_predictive_search_less->css_file_name.'.min.css' ) ) { 
  64. $have_dynamic_style = true; 
  65. wp_register_style( 'wc-predictive-search-dynamic-style', str_replace(array('http:', 'https:'), '', $_upload_dir['baseurl'] ) . '/sass/'.$wc_predictive_search_less->css_file_name.'.min.css', array( 'wc-predictive-search-style' ), $wc_predictive_search_less->get_css_file_version(), 'all' ); 
  66.  
  67. wp_register_script( 'backbone.localStorage', WOOPS_JS_URL . '/backbone.localStorage.js', array( 'jquery', 'underscore', 'backbone' ) , '1.1.9', true ); 
  68. wp_register_script( 'wc-predictive-search-autocomplete-script', WOOPS_JS_URL . '/ajax-autocomplete/jquery.autocomplete.js', array( 'jquery', 'underscore', 'backbone', 'backbone.localStorage' ), WOOPS_VERSION, true ); 
  69. wp_register_script( 'wc-predictive-search-backbone', WOOPS_JS_URL . '/predictive-search.backbone.js', array( 'jquery', 'underscore', 'backbone' ), WOOPS_VERSION, true ); 
  70. wp_register_script( 'wc-predictive-search-popup-backbone', WOOPS_JS_URL . '/predictive-search-popup.backbone.min.js', array( 'jquery', 'underscore', 'backbone', 'wc-predictive-search-autocomplete-script', 'wc-predictive-search-backbone' ), WOOPS_VERSION, true ); 
  71.  
  72. wp_enqueue_style( 'wc-predictive-search-style' ); 
  73. if ( $have_dynamic_style ) { 
  74. wp_enqueue_style( 'wc-predictive-search-dynamic-style' ); 
  75. wp_enqueue_script( 'wc-predictive-search-popup-backbone' ); 
  76.  
  77. global $wc_ps_legacy_api; 
  78. $legacy_api_url = $wc_ps_legacy_api->get_legacy_api_url(); 
  79. $legacy_api_url = add_query_arg( 'action', 'get_result_popup', $legacy_api_url ); 
  80. $min_characters = 1; 
  81. $delay_time = 600; 
  82. $cache_timeout = 1; 
  83. wp_localize_script( 'wc-predictive-search-popup-backbone',  
  84. 'wc_ps_vars',  
  85. apply_filters( 'wc_ps_vars', array( 
  86. 'minChars' => $min_characters,  
  87. 'delay' => $delay_time,  
  88. 'cache_timeout' => $cache_timeout,  
  89. 'is_debug' => $ps_is_debug,  
  90. 'legacy_api_url' => $legacy_api_url,  
  91. 'search_page_url' => get_permalink( $woocommerce_search_page_id ),  
  92. 'permalink_structure' => get_option('permalink_structure' ) 
  93. ) ) 
  94. ); 
  95.  
  96. public function include_result_shortcode_script() { 
  97. global $wp_query; 
  98. global $post; 
  99. global $woocommerce_search_page_id; 
  100.  
  101. if ( $post && $post->ID != $woocommerce_search_page_id ) return ''; 
  102.  
  103. $current_lang = ''; 
  104. if ( class_exists('SitePress') ) { 
  105. $current_lang = ICL_LANGUAGE_CODE; 
  106.  
  107. $search_keyword = ''; 
  108. $search_in = 'product'; 
  109. $search_other = ''; 
  110. $cat_in = 'all'; 
  111.  
  112. if ( isset( $wp_query->query_vars['keyword'] ) ) $search_keyword = stripslashes( strip_tags( urldecode( $wp_query->query_vars['keyword'] ) ) ); 
  113. elseif ( isset( $_REQUEST['rs'] ) && trim( $_REQUEST['rs'] ) != '' ) $search_keyword = stripslashes( strip_tags( $_REQUEST['rs'] ) ); 
  114.  
  115. if ( isset( $wp_query->query_vars['search-in'] ) ) $search_in = stripslashes( strip_tags( urldecode( $wp_query->query_vars['search-in'] ) ) ); 
  116. elseif ( isset( $_REQUEST['search_in'] ) && trim( $_REQUEST['search_in'] ) != '' ) $search_in = stripslashes( strip_tags( $_REQUEST['search_in'] ) ); 
  117.  
  118. if ( isset( $wp_query->query_vars['search-other'] ) ) $search_other = stripslashes( strip_tags( urldecode( $wp_query->query_vars['search-other'] ) ) ); 
  119. elseif ( isset( $_REQUEST['search_other'] ) && trim( $_REQUEST['search_other'] ) != '' ) $search_other = stripslashes( strip_tags( $_REQUEST['search_other'] ) ); 
  120.  
  121. if ( isset( $wp_query->query_vars['cat-in'] ) ) $cat_in = stripslashes( strip_tags( urldecode( $wp_query->query_vars['cat-in'] ) ) ); 
  122. elseif ( isset( $_REQUEST['cat_in'] ) && trim( $_REQUEST['cat_in'] ) != '' ) $cat_in = stripslashes( strip_tags( $_REQUEST['cat_in'] ) ); 
  123.  
  124. $permalink_structure = get_option( 'permalink_structure' ); 
  125.  
  126. if ( $search_keyword == '' || $search_in == '' ) return; 
  127.  
  128. $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; 
  129. $ps_suffix = '.min'; 
  130. $ps_is_debug = get_option( 'woocommerce_search_is_debug', 'yes' ); 
  131. if ( 'yes' == $ps_is_debug ) { 
  132. $ps_suffix = ''; 
  133. ?> 
  134. <!-- Predictive Search Results Template Registered --> 
  135. <?php 
  136. wc_ps_get_results_item_tpl(); 
  137. wc_ps_get_results_footer_tpl(); 
  138. ?> 
  139.  
  140. <?php 
  141. wp_register_script( 'wc-predictive-search-results-backbone', WOOPS_JS_URL . '/predictive-search-results.backbone.min.js', array( 'jquery', 'underscore', 'backbone', 'wc-predictive-search-backbone' ), WOOPS_VERSION, true ); 
  142. wp_enqueue_script( 'wc-predictive-search-results-backbone' ); 
  143.  
  144. global $wc_ps_legacy_api; 
  145. $legacy_api_url = $wc_ps_legacy_api->get_legacy_api_url(); 
  146. $legacy_api_url = add_query_arg( 'action', 'get_results', $legacy_api_url ); 
  147. $legacy_api_url .= '&q=' . $search_keyword; 
  148. if ( ! empty( $cat_in ) ) $legacy_api_url .= '&cat_in=' . $cat_in; 
  149. else $legacy_api_url .= '&cat_in=all'; 
  150.  
  151. $product_term_id = 0; 
  152. $post_term_id = 0; 
  153.  
  154. $woocommerce_search_focus_enable = false; 
  155. $woocommerce_search_focus_plugin = false; 
  156.  
  157. $search_in_have_items = false; 
  158.  
  159. global $wc_predictive_search; 
  160.  
  161. $search_other_list = explode(", ", $search_other); 
  162. if ( ! is_array( $search_other_list ) ) { 
  163. $search_other_list = array(); 
  164.  
  165. global $ps_search_list, $ps_current_search_in; 
  166.  
  167. $ps_search_list = $search_all_list = $search_other_list; 
  168. $ps_current_search_in = $search_in; 
  169.  
  170. // Remove current search in on search other list first 
  171. $search_all_list = array_diff( $search_all_list, (array) $search_in ); 
  172. // Add current search in as first element from search other list 
  173. $search_all_list = array_merge( (array) $search_in, $search_all_list ); 
  174.  
  175. if ( count( $search_all_list ) > 0 ) { 
  176. foreach ( $search_all_list as $search_item ) { 
  177. if ( 'product' == $search_item ) { 
  178. $have_product = $wc_predictive_search->check_product_exsited( $search_keyword, $woocommerce_search_focus_enable, $woocommerce_search_focus_plugin, 'product', $product_term_id, $current_lang ); 
  179. if ( $have_product ) { 
  180. if ( ! $search_in_have_items ) { 
  181. $search_in_have_items = true; 
  182. $ps_current_search_in = $search_item; 
  183. } else { 
  184. $ps_search_list = array_diff( $ps_search_list, (array) $search_item ); 
  185. } elseif ( 'post' == $search_item ) { 
  186. $have_post = $wc_predictive_search->check_product_exsited( $search_keyword, $woocommerce_search_focus_enable, $woocommerce_search_focus_plugin, 'post', $post_term_id, $current_lang ); 
  187. if ( $have_post ) { 
  188. if ( ! $search_in_have_items ) { 
  189. $search_in_have_items = true; 
  190. $ps_current_search_in = $search_item; 
  191. } else { 
  192. $ps_search_list = array_diff( $ps_search_list, (array) $search_item ); 
  193. } elseif ( 'page' == $search_item ) { 
  194. $have_page = $wc_predictive_search->check_product_exsited( $search_keyword, $woocommerce_search_focus_enable, $woocommerce_search_focus_plugin, 'page', 0, $current_lang ); 
  195. if ( $have_page ) { 
  196. if ( ! $search_in_have_items ) { 
  197. $search_in_have_items = true; 
  198. $ps_current_search_in = $search_item; 
  199. } else { 
  200. $ps_search_list = array_diff( $ps_search_list, (array) $search_item ); 
  201.  
  202. $search_page_url = get_permalink( $woocommerce_search_page_id ); 
  203. $search_page_parsed = parse_url( $search_page_url ); 
  204. if ( $permalink_structure == '' ) { 
  205. $search_page_path = $search_page_parsed['path']; 
  206. $default_navigate = '?page_id='.$woocommerce_search_page_id.'&rs='.urlencode($search_keyword).'&search_in='.$ps_current_search_in.'&cat_in='.$cat_in.'&search_other='.$search_other; 
  207. } else { 
  208. $host_name = $search_page_parsed['host']; 
  209. $search_page_exploded = explode( $host_name , $search_page_url ); 
  210. $search_page_path = $search_page_exploded[1]; 
  211. $default_navigate = 'keyword/'.urlencode($search_keyword).'/search-in/'.$ps_current_search_in.'/cat-in/'.$cat_in.'/search-other/'.$search_other; 
  212.  
  213. wp_localize_script( 'wc-predictive-search-results-backbone', 'wc_ps_results_vars', apply_filters( 'wc_ps_results_vars', array( 'default_navigate' => $default_navigate, 'search_in' => $ps_current_search_in, 'ps_lang' => $current_lang, 'legacy_api_url' => $legacy_api_url, 'search_page_path' => $search_page_path, 'permalink_structure' => get_option('permalink_structure' ) ) ) );