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', 'woops') ) ),  
  6. 'post' => array( 'number' => 0, 'name' => wc_ps_ict_t__( 'Posts', __('Posts', 'woops') ) ),  
  7. 'page' => array( 'number' => 0, 'name' => wc_ps_ict_t__( 'Pages', __('Pages', 'woops') ) ) 
  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.", 'woops'),  
  16. 'customize_selective_refresh' => true,  
  17. ); 
  18. parent::__construct('products_predictive_search', __('WooCommerce Predictive Search', 'woops'), $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.  
  34. if ( class_exists('SitePress') ) { 
  35. $current_lang = ICL_LANGUAGE_CODE; 
  36. $search_box_texts = ( isset($instance['search_box_text']) ? $instance['search_box_text'] : array() ); 
  37. if ( !is_array($search_box_texts) ) $search_box_texts = get_option('woocommerce_search_box_text', array() ); 
  38. if ( is_array($search_box_texts) && isset($search_box_texts[$current_lang]) ) $search_box_text = esc_attr( stripslashes( trim( $search_box_texts[$current_lang] ) ) ); 
  39. else $search_box_text = ''; 
  40. } else { 
  41. $search_box_text = ( isset($instance['search_box_text']) ? $instance['search_box_text'] : '' ); 
  42. if ( is_array($search_box_text) || trim($search_box_text) == '' ) $search_box_text = get_option('woocommerce_search_box_text', '' ); 
  43. if ( is_array($search_box_text) ) $search_box_text = ''; 
  44.  
  45. echo $before_widget; 
  46. if ( $title ) 
  47. echo $before_title . $title . $after_title; 
  48. echo $this->woops_results_search_form($widget_id, $number_items, $text_lenght, $search_box_text, $show_catdropdown, $show_image, $show_price, $show_desc, $widget_template ); 
  49. echo $after_widget; 
  50.  
  51. 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, $widget_template = 'sidebar' ) { 
  52.  
  53. global $woocommerce_search_page_id; 
  54.  
  55. $ps_id = str_replace('products_predictive_search-', '', $widget_id); 
  56.  
  57. $row = 0; 
  58. if (!is_array($number_items) || count($number_items) < 1 || array_sum($number_items) < 1) { 
  59. $items_search_default = WC_Predictive_Search_Widgets::get_items_search(); 
  60. $number_items_default = array(); 
  61. foreach ($items_search_default as $key => $data) { 
  62. if ($data['number'] > 0) { 
  63. $number_items_default[$key] = $data['number']; 
  64. $number_items = $number_items_default; 
  65.  
  66. $common = ''; 
  67. $search_list = array(); 
  68. foreach ($number_items as $key => $number) { 
  69. if ($number > 0) { 
  70. $row += $number; 
  71. $row++; 
  72. $search_list[] = $key; 
  73. $search_in = json_encode($number_items); 
  74.  
  75. $ps_args = array( 
  76. 'search_box_text' => $search_box_text,  
  77. 'row' => $row,  
  78. 'text_lenght' => $text_lenght,  
  79. 'show_catdropdown' => $show_catdropdown,  
  80. 'widget_template' => $widget_template,  
  81. 'show_image' => $show_image,  
  82. 'show_price' => $show_price,  
  83. 'show_desc' => $show_desc,  
  84. 'search_in' => $search_in,  
  85. 'search_list' => $search_list,  
  86. ); 
  87.  
  88. $search_form = wc_ps_search_form( $ps_id, $widget_template, $ps_args, false ); 
  89.  
  90. return $search_form . '<div style="clear:both;"></div>'; 
  91.  
  92. function update( $new_instance, $old_instance ) { 
  93. $instance = $old_instance; 
  94. $instance['title'] = strip_tags($new_instance['title']); 
  95. $instance['number_items'] = $new_instance['number_items']; 
  96. $instance['text_lenght'] = strip_tags($new_instance['text_lenght']); 
  97. $instance['show_price'] = $new_instance['show_price']; 
  98. $instance['search_box_text'] = $new_instance['search_box_text']; 
  99. $instance['show_image'] = $new_instance['show_image']; 
  100. $instance['show_desc'] = $new_instance['show_desc']; 
  101. return $instance; 
  102.  
  103. function form( $instance ) { 
  104. wp_enqueue_script( 'jquery-ui-core' ); 
  105. wp_enqueue_script( 'jquery-ui-sortable' ); 
  106. wp_enqueue_script( 'jquery-ui-draggable' ); 
  107.  
  108. $global_search_box_text = get_option('woocommerce_search_box_text'); 
  109. $items_search_default = WC_Predictive_Search_Widgets::get_items_search(); 
  110. $number_items_default = array(); 
  111. foreach ($items_search_default as $key => $data) { 
  112. $number_items_default[$key] = $data['number']; 
  113. unset($key); 
  114. unset($data); 
  115. $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, 'widget_template' => 'sidebar', 'search_box_text' => $global_search_box_text ) ); 
  116. $title = strip_tags($instance['title']); 
  117. $number_items = $instance['number_items']; 
  118. if (!is_array($number_items) || count($number_items) < count($items_search_default) ) $number_items = $number_items_default; 
  119. $text_lenght = strip_tags($instance['text_lenght']); 
  120. $show_price = $instance['show_price']; 
  121. $show_catdropdown = $instance['show_catdropdown']; 
  122. $search_box_text = $instance['search_box_text']; 
  123. $widget_template = $instance['widget_template']; 
  124.  
  125. $show_image = $instance['show_image']; 
  126. $show_desc = $instance['show_desc']; 
  127. ?> 
  128. <style type="text/css"> 
  129. .item_heading{ width:130px; display:inline-block;} 
  130. ul.predictive_search_item li{padding-left:15px; background:url(<?php echo WOOPS_IMAGES_URL; ?>/sortable.gif) no-repeat left center; cursor:pointer;} 
  131. ul.predictive_search_item li.ui-sortable-placeholder{border:1px dotted #111; visibility:visible !important; background:none;} 
  132. ul.predictive_search_item li.ui-sortable-helper{background-color:#DDD;} 
  133. </style> 
  134. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'woops'); ?></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> 
  135. <?php 
  136. if ( class_exists('SitePress') ) { 
  137. if ( !is_array($search_box_text) ) $search_box_text = array(); 
  138. global $sitepress; 
  139. $active_languages = $sitepress->get_active_languages(); 
  140. if ( is_array($active_languages) && count($active_languages) > 0 ) { 
  141. foreach ( $active_languages as $language ) { 
  142. ?> 
  143. <p><label for="<?php echo $this->get_field_id('search_box_text'); ?>_<?php echo $language['code']; ?>"><?php _e('Search box text message', 'woops'); ?> (<?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> 
  144. <?php 
  145. } else { 
  146. if ( is_array($search_box_text) ) $search_box_text = ''; 
  147. ?> 
  148. <p><label for="<?php echo $this->get_field_id('search_box_text'); ?>"><?php _e('Search box text message:', 'woops'); ?></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> 
  149. <?php } ?> 
  150. <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", 'woops'); ?></p> 
  151. <ul class="ui-sortable predictive_search_item"> 
  152. <?php foreach ($number_items as $key => $value) { ?> 
  153. <?php if ( isset( $items_search_default[$key] ) ) { ?> 
  154. <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:50px;" /></li> 
  155. <?php } ?> 
  156. <?php } ?> 
  157. </ul> 
  158. <p> 
  159. <label for="<?php echo $this->get_field_id('widget_template'); ?>"><?php _e('Select Template:', 'woops'); ?></label> 
  160. <select id="<?php echo $this->get_field_id('widget_template'); ?>" name="widget_template" disabled="disabled"> 
  161. <option value="sidebar" selected="selected" ><?php _e('Widget', 'woops'); ?></option> 
  162. <option value="header"><?php _e('Header', 'woops'); ?></option> 
  163. </select> <span style="color: #f00; font-size: 11px;">* <?php _e('Premium Feature!', 'woops'); ?></span> 
  164. </p> 
  165. <p> 
  166. <label><input type="checkbox" name="show_catdropdown" value="1" disabled="disabled" /> <?php _e('Search in Product Category', 'woops'); ?></label> 
  167. <span style="color: #f00; font-size: 11px;">* <?php _e('Premium Feature!', 'woops'); ?></span> 
  168. </p> 
  169. <p> 
  170. <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', 'woops'); ?></label> 
  171. </p> 
  172. <p> 
  173. <label><input type="checkbox" name="<?php echo $this->get_field_name('show_price'); ?>" value="1" <?php checked( $show_price, 1 ); ?> /> <?php _e('Show Results Prices', 'woops'); ?></label> 
  174. </p> 
  175. <p> 
  176. <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', 'woops'); ?></label> 
  177. </p> 
  178. <p class="wc_ps_show_desc_container" style="<?php echo ( 0 == $show_desc ) ? 'display: none' : ''; ?>"> 
  179. <label for="<?php echo $this->get_field_id('text_lenght'); ?>"><?php _e('Character Count:', 'woops'); ?></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); ?>" /> 
  180. </p> 
  181. <script> 
  182. jQuery(document).ready(function() { 
  183. jQuery(".predictive_search_item").sortable(); 
  184. jQuery(document).on( 'change', ".wc_ps_show_desc", function() { 
  185. if ( jQuery(this).is(':checked') ) { 
  186. jQuery(this).parent('label').parent('p').siblings('.wc_ps_show_desc_container').show(); 
  187. } else { 
  188. jQuery(this).parent('label').parent('p').siblings('.wc_ps_show_desc_container').hide(); 
  189. }); 
  190. }); 
  191. </script> 
  192. <?php