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