WC_Predictive_Search_Widgets

WooCommerce Predictive Search Widget.

Defined (1)

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

/widget/wc-predictive-search-widgets.php  
  1. class WC_Predictive_Search_Widgets extends WP_Widget  
  2.  
  3. public static function get_items_search() { 
  4. $items_search = array( 
  5. 'product' => array( 'number' => 6, 'name' => wc_ps_ict_t__( 'Product Name', __('Product Name', 'woocommerce-predictive-search' ) ) ),  
  6. 'post' => array( 'number' => 0, 'name' => wc_ps_ict_t__( 'Posts', __('Posts', 'woocommerce-predictive-search' ) ) ),  
  7. 'page' => array( 'number' => 0, 'name' => wc_ps_ict_t__( 'Pages', __('Pages', 'woocommerce-predictive-search' ) ) ) 
  8. ); 
  9.  
  10. return $items_search; 
  11.  
  12. function __construct() { 
  13. $widget_ops = array( 
  14. 'classname' => 'widget_products_predictive_search',  
  15. 'description' => __( "User sees search results as they type in a dropdown - links through to 'All Search Results Page' that features endless scroll.", 'woocommerce-predictive-search' ),  
  16. 'customize_selective_refresh' => true,  
  17. ); 
  18. parent::__construct('products_predictive_search', __('WooCommerce Predictive Search', 'woocommerce-predictive-search' ), $widget_ops); 
  19.  
  20. function widget( $args, $instance ) { 
  21. extract($args); 
  22. $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base); 
  23. $number_items = $instance['number_items']; 
  24. if (!is_array($number_items) || count($number_items) < 1 ) $number_items = array(); 
  25. if(!isset($instance['text_lenght']) || $instance['text_lenght'] < 0) $text_lenght = 100;  
  26. else $text_lenght = $instance['text_lenght']; 
  27. $show_catdropdown = 0; 
  28. $show_price = empty($instance['show_price']) ? 0 : $instance['show_price']; 
  29. $widget_template = 'sidebar'; 
  30.  
  31. $show_image = empty($instance['show_image']) ? 0 : $instance['show_image']; 
  32. $show_desc = empty($instance['show_desc']) ? 0 : $instance['show_desc']; 
  33. $show_in_cat = empty($instance['show_in_cat']) ? 0 : $instance['show_in_cat']; 
  34.  
  35. if ( class_exists('SitePress') ) { 
  36. $current_lang = ICL_LANGUAGE_CODE; 
  37. $search_box_texts = ( isset($instance['search_box_text']) ? $instance['search_box_text'] : array() ); 
  38. if ( !is_array($search_box_texts) ) $search_box_texts = get_option('woocommerce_search_box_text', array() ); 
  39. if ( is_array($search_box_texts) && isset($search_box_texts[$current_lang]) ) $search_box_text = esc_attr( stripslashes( trim( $search_box_texts[$current_lang] ) ) ); 
  40. else $search_box_text = ''; 
  41. } else { 
  42. $search_box_text = ( isset($instance['search_box_text']) ? $instance['search_box_text'] : '' ); 
  43. if ( is_array($search_box_text) || trim($search_box_text) == '' ) $search_box_text = get_option('woocommerce_search_box_text', '' ); 
  44. if ( is_array($search_box_text) ) $search_box_text = ''; 
  45.  
  46. echo $before_widget; 
  47. if ( $title ) 
  48. echo $before_title . $title . $after_title; 
  49. echo $this->woops_results_search_form($widget_id, $number_items, $text_lenght, $search_box_text, $show_catdropdown, $show_image, $show_price, $show_desc, $show_in_cat, $widget_template ); 
  50. echo $after_widget; 
  51.  
  52. public static function woops_results_search_form($widget_id, $number_items=array(), $text_lenght=100, $search_box_text = '', $show_catdropdown = 1, $show_image = 1, $show_price = 1, $show_desc = 1, $show_in_cat = 1, $widget_template = 'sidebar' ) { 
  53.  
  54. global $woocommerce_search_page_id; 
  55.  
  56. $ps_id = str_replace('products_predictive_search-', '', $widget_id); 
  57.  
  58. $row = 0; 
  59. if (!is_array($number_items) || count($number_items) < 1 || array_sum($number_items) < 1) { 
  60. $items_search_default = WC_Predictive_Search_Widgets::get_items_search(); 
  61. $number_items_default = array(); 
  62. foreach ($items_search_default as $key => $data) { 
  63. if ($data['number'] > 0) { 
  64. $number_items_default[$key] = $data['number']; 
  65. $number_items = $number_items_default; 
  66.  
  67. $common = ''; 
  68. $search_list = array(); 
  69. foreach ($number_items as $key => $number) { 
  70. if ($number > 0) { 
  71. $row += $number; 
  72. $row++; 
  73. $search_list[] = $key; 
  74. $search_in = json_encode($number_items); 
  75.  
  76. $ps_args = array( 
  77. 'search_box_text' => $search_box_text,  
  78. 'row' => $row,  
  79. 'text_lenght' => $text_lenght,  
  80. 'show_catdropdown' => $show_catdropdown,  
  81. 'widget_template' => $widget_template,  
  82. 'show_image' => $show_image,  
  83. 'show_price' => $show_price,  
  84. 'show_desc' => $show_desc,  
  85. 'show_in_cat' => $show_in_cat,  
  86. 'search_in' => $search_in,  
  87. 'search_list' => $search_list,  
  88. ); 
  89.  
  90. $search_form = wc_ps_search_form( $ps_id, $widget_template, $ps_args, false ); 
  91.  
  92. return $search_form . '<div style="clear:both;"></div>'; 
  93.  
  94. function update( $new_instance, $old_instance ) { 
  95. $instance = $old_instance; 
  96. $instance['title'] = strip_tags($new_instance['title']); 
  97. $instance['number_items'] = $new_instance['number_items']; 
  98. $instance['text_lenght'] = strip_tags($new_instance['text_lenght']); 
  99. $instance['show_price'] = $new_instance['show_price']; 
  100. $instance['search_box_text'] = $new_instance['search_box_text']; 
  101. $instance['show_image'] = $new_instance['show_image']; 
  102. $instance['show_desc'] = $new_instance['show_desc']; 
  103. $instance['show_in_cat'] = $new_instance['show_in_cat']; 
  104. return $instance; 
  105.  
  106. function form( $instance ) { 
  107. wp_enqueue_script( 'jquery-ui-core' ); 
  108. wp_enqueue_script( 'jquery-ui-sortable' ); 
  109. wp_enqueue_script( 'jquery-ui-draggable' ); 
  110.  
  111. $global_search_box_text = get_option('woocommerce_search_box_text'); 
  112. $items_search_default = WC_Predictive_Search_Widgets::get_items_search(); 
  113. $items_search_default = array_merge( $items_search_default, array( 
  114. 'p_sku' => array( 'number' => 0, 'name' => wc_ps_ict_t__( 'Product SKU', __( 'Product SKU', 'woocommerce-predictive-search' ) ) ),  
  115. 'p_cat' => array( 'number' => 0, 'name' => wc_ps_ict_t__( 'Product Categories', __('Product Categories', 'woocommerce-predictive-search' ) ) ),  
  116. 'p_tag' => array( 'number' => 0, 'name' => wc_ps_ict_t__( 'Product Tags', __('Product Tags', 'woocommerce-predictive-search' ) ) ),  
  117. ) ); 
  118. $number_items_default = array(); 
  119. foreach ($items_search_default as $key => $data) { 
  120. $number_items_default[$key] = $data['number']; 
  121. unset($key); 
  122. unset($data); 
  123. $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'number_items' => $number_items_default, 'text_lenght' => 100, 'show_price' => 1, 'show_catdropdown' => 1, 'show_image' => 1, 'show_desc' => 1, 'show_in_cat' => 1, 'widget_template' => 'sidebar', 'search_box_text' => $global_search_box_text ) ); 
  124. $title = strip_tags($instance['title']); 
  125. $number_items = $instance['number_items']; 
  126. if (!is_array($number_items) || count($number_items) < count($items_search_default) ) $number_items = $number_items_default; 
  127. $text_lenght = strip_tags($instance['text_lenght']); 
  128. $show_price = $instance['show_price']; 
  129. $show_catdropdown = $instance['show_catdropdown']; 
  130. $search_box_text = $instance['search_box_text']; 
  131. $widget_template = $instance['widget_template']; 
  132.  
  133. $show_image = $instance['show_image']; 
  134. $show_desc = $instance['show_desc']; 
  135. $show_in_cat = $instance['show_in_cat']; 
  136. ?> 
  137. <style type="text/css"> 
  138. .item_heading{ width:120px; display:inline-block;} 
  139. ul.predictive_search_item li{padding-left:12px; background:url(<?php echo WOOPS_IMAGES_URL; ?>/sortable.gif) no-repeat left center; cursor:pointer;} 
  140. ul.predictive_search_item li.ui-sortable-placeholder{border:1px dotted #111; visibility:visible !important; background:none;} 
  141. ul.predictive_search_item li.ui-sortable-helper{background-color:#DDD;} 
  142. </style> 
  143. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'woocommerce-predictive-search' ); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p> 
  144. <?php 
  145. if ( class_exists('SitePress') ) { 
  146. if ( !is_array($search_box_text) ) $search_box_text = array(); 
  147. global $sitepress; 
  148. $active_languages = $sitepress->get_active_languages(); 
  149. if ( is_array($active_languages) && count($active_languages) > 0 ) { 
  150. foreach ( $active_languages as $language ) { 
  151. ?> 
  152. <p><label for="<?php echo $this->get_field_id('search_box_text'); ?>_<?php echo $language['code']; ?>"><?php _e('Search box text message', 'woocommerce-predictive-search' ); ?> (<?php echo $language['display_name']; ?>)</label> <input class="widefat" id="<?php echo $this->get_field_id('search_box_text'); ?>_<?php echo $language['code']; ?>" name="<?php echo $this->get_field_name('search_box_text'); ?>[<?php echo $language['code']; ?>]" type="text" value="<?php if ( isset( $search_box_text[$language['code'] ] ) ) esc_attr_e( $search_box_text[$language['code']] ); ?>" /></p> 
  153. <?php 
  154. } else { 
  155. if ( is_array($search_box_text) ) $search_box_text = ''; 
  156. ?> 
  157. <p><label for="<?php echo $this->get_field_id('search_box_text'); ?>"><?php _e('Search box text message:', 'woocommerce-predictive-search' ); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('search_box_text'); ?>" name="<?php echo $this->get_field_name('search_box_text'); ?>" type="text" value="<?php echo esc_attr($search_box_text); ?>" /></p> 
  158. <?php } ?> 
  159. <p><?php _e("Activate search 'types' for this widget by entering the number of results to show in the widget dropdown. <empty> = not activated. Sort order by drag and drop", 'woocommerce-predictive-search' ); ?></p> 
  160. <ul class="ui-sortable predictive_search_item"> 
  161. <?php foreach ($number_items as $key => $value) { ?> 
  162. <?php if ( isset( $items_search_default[$key] ) ) { ?> 
  163. <li><span class="item_heading"><label for="search_<?php echo $key; ?>"><?php echo $items_search_default[$key]['name']; ?></label></span> <input id="search_<?php echo $key; ?>" name="<?php echo $this->get_field_name('number_items'); ?>[<?php echo $key; ?>]" type="text" value="<?php echo esc_attr($value); ?>" style="width:40px;" <?php if( in_array( $key, array( 'p_sku', 'p_cat', 'p_tag' ) ) ) { ?>readonly="readonly"<?php } ?> /> <?php if( in_array( $key, array( 'p_sku', 'p_cat', 'p_tag' ) ) ) { ?> <span style="color: #f00; font-size: 11px; white-space: nowrap;">* <?php _e('Premium Feature!', 'woocommerce-predictive-search' ); ?></span><?php } ?></li> 
  164. <?php } ?> 
  165. <?php } ?> 
  166. </ul> 
  167. <p> 
  168. <label for="<?php echo $this->get_field_id('widget_template'); ?>"><?php _e('Select Template:', 'woocommerce-predictive-search' ); ?></label> 
  169. <select id="<?php echo $this->get_field_id('widget_template'); ?>" name="widget_template" disabled="disabled"> 
  170. <option value="sidebar" selected="selected" ><?php _e('Widget', 'woocommerce-predictive-search' ); ?></option> 
  171. <option value="header"><?php _e('Header', 'woocommerce-predictive-search' ); ?></option> 
  172. </select> <span style="color: #f00; font-size: 11px;">* <?php _e('Premium Feature!', 'woocommerce-predictive-search' ); ?></span> 
  173. </p> 
  174. <p> 
  175. <label><input type="checkbox" name="show_catdropdown" value="1" disabled="disabled" /> <?php _e('Search in Product Category Feature', 'woocommerce-predictive-search' ); ?></label> 
  176. <span style="color: #f00; font-size: 11px;">* <?php _e('Premium Feature!', 'woocommerce-predictive-search' ); ?></span> 
  177. </p> 
  178. <p> 
  179. <label><input type="checkbox" name="<?php echo $this->get_field_name('show_image'); ?>" value="1" <?php checked( $show_image, 1 ); ?> /> <?php _e('Show Results Images', 'woocommerce-predictive-search' ); ?></label> 
  180. </p> 
  181. <p> 
  182. <label><input type="checkbox" name="<?php echo $this->get_field_name('show_price'); ?>" value="1" <?php checked( $show_price, 1 ); ?> /> <?php _e('Product Results - Show Prices', 'woocommerce-predictive-search' ); ?></label> 
  183. </p> 
  184. <p> 
  185. <label><input class="wc_ps_show_desc" type="checkbox" name="<?php echo $this->get_field_name('show_desc'); ?>" value="1" <?php checked( $show_desc, 1 ); ?> /> <?php _e('Show Results Description', 'woocommerce-predictive-search' ); ?></label> 
  186. </p> 
  187. <p class="wc_ps_show_desc_container" style="<?php echo ( 0 == $show_desc ) ? 'display: none' : ''; ?>"> 
  188. <label for="<?php echo $this->get_field_id('text_lenght'); ?>"><?php _e('Character Count:', 'woocommerce-predictive-search' ); ?></label> <input style="width:50px;" id="<?php echo $this->get_field_id('text_lenght'); ?>" name="<?php echo $this->get_field_name('text_lenght'); ?>" type="text" value="<?php echo esc_attr($text_lenght); ?>" /> 
  189. </p> 
  190. <p> 
  191. <label><input type="checkbox" name="<?php echo $this->get_field_name('show_in_cat'); ?>" value="1" <?php checked( $show_in_cat, 1 ); ?> /> <?php _e('Product Results - Show Categories', 'woocommerce-predictive-search' ); ?></label> 
  192. </p> 
  193. <script> 
  194. jQuery(document).ready(function() { 
  195. jQuery(".predictive_search_item").sortable(); 
  196. jQuery(document).on( 'change', ".wc_ps_show_desc", function() { 
  197. if ( jQuery(this).is(':checked') ) { 
  198. jQuery(this).parent('label').parent('p').siblings('.wc_ps_show_desc_container').show(); 
  199. } else { 
  200. jQuery(this).parent('label').parent('p').siblings('.wc_ps_show_desc_container').hide(); 
  201. }); 
  202. }); 
  203. </script> 
  204. <?php