WC_Widget

Abstract Widget Class.

Defined (1)

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

/includes/abstracts/abstract-wc-widget.php  
  1. abstract class WC_Widget extends WP_Widget { 
  2.  
  3. /** 
  4. * CSS class. 
  5. * @var string 
  6. */ 
  7. public $widget_cssclass; 
  8.  
  9. /** 
  10. * Widget description. 
  11. * @var string 
  12. */ 
  13. public $widget_description; 
  14.  
  15. /** 
  16. * Widget ID. 
  17. * @var string 
  18. */ 
  19. public $widget_id; 
  20.  
  21. /** 
  22. * Widget name. 
  23. * @var string 
  24. */ 
  25. public $widget_name; 
  26.  
  27. /** 
  28. * Settings. 
  29. * @var array 
  30. */ 
  31. public $settings; 
  32.  
  33. /** 
  34. * Constructor. 
  35. */ 
  36. public function __construct() { 
  37. $widget_ops = array( 
  38. 'classname' => $this->widget_cssclass,  
  39. 'description' => $this->widget_description,  
  40. 'customize_selective_refresh' => true,  
  41. ); 
  42.  
  43. parent::__construct( $this->widget_id, $this->widget_name, $widget_ops ); 
  44.  
  45. add_action( 'save_post', array( $this, 'flush_widget_cache' ) ); 
  46. add_action( 'deleted_post', array( $this, 'flush_widget_cache' ) ); 
  47. add_action( 'switch_theme', array( $this, 'flush_widget_cache' ) ); 
  48.  
  49. /** 
  50. * Get cached widget. 
  51. * @param array $args 
  52. * @return bool true if the widget is cached otherwise false 
  53. */ 
  54. public function get_cached_widget( $args ) { 
  55.  
  56. $cache = wp_cache_get( apply_filters( 'woocommerce_cached_widget_id', $this->widget_id ), 'widget' ); 
  57.  
  58. if ( ! is_array( $cache ) ) { 
  59. $cache = array(); 
  60.  
  61. if ( isset( $cache[ $args['widget_id'] ] ) ) { 
  62. echo $cache[ $args['widget_id'] ]; 
  63. return true; 
  64.  
  65. return false; 
  66.  
  67. /** 
  68. * Cache the widget. 
  69. * @param array $args 
  70. * @param string $content 
  71. * @return string the content that was cached 
  72. */ 
  73. public function cache_widget( $args, $content ) { 
  74. wp_cache_set( apply_filters( 'woocommerce_cached_widget_id', $this->widget_id ), array( $args['widget_id'] => $content ), 'widget' ); 
  75.  
  76. return $content; 
  77.  
  78. /** 
  79. * Flush the cache. 
  80. */ 
  81. public function flush_widget_cache() { 
  82. wp_cache_delete( apply_filters( 'woocommerce_cached_widget_id', $this->widget_id ), 'widget' ); 
  83.  
  84. /** 
  85. * Output the html at the start of a widget. 
  86. * @param array $args 
  87. * @return string 
  88. */ 
  89. public function widget_start( $args, $instance ) { 
  90. echo $args['before_widget']; 
  91.  
  92. if ( $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base ) ) { 
  93. echo $args['before_title'] . $title . $args['after_title']; 
  94.  
  95. /** 
  96. * Output the html at the end of a widget. 
  97. * @param array $args 
  98. * @return string 
  99. */ 
  100. public function widget_end( $args ) { 
  101. echo $args['after_widget']; 
  102.  
  103. /** 
  104. * Updates a particular instance of a widget. 
  105. * @see WP_Widget->update 
  106. * @param array $new_instance 
  107. * @param array $old_instance 
  108. * @return array 
  109. */ 
  110. public function update( $new_instance, $old_instance ) { 
  111.  
  112. $instance = $old_instance; 
  113.  
  114. if ( empty( $this->settings ) ) { 
  115. return $instance; 
  116.  
  117. // Loop settings and get values to save. 
  118. foreach ( $this->settings as $key => $setting ) { 
  119. if ( ! isset( $setting['type'] ) ) { 
  120. continue; 
  121.  
  122. // Format the value based on settings type. 
  123. switch ( $setting['type'] ) { 
  124. case 'number' : 
  125. $instance[ $key ] = absint( $new_instance[ $key ] ); 
  126.  
  127. if ( isset( $setting['min'] ) && '' !== $setting['min'] ) { 
  128. $instance[ $key ] = max( $instance[ $key ], $setting['min'] ); 
  129.  
  130. if ( isset( $setting['max'] ) && '' !== $setting['max'] ) { 
  131. $instance[ $key ] = min( $instance[ $key ], $setting['max'] ); 
  132. break; 
  133. case 'textarea' : 
  134. $instance[ $key ] = wp_kses( trim( wp_unslash( $new_instance[ $key ] ) ), wp_kses_allowed_html( 'post' ) ); 
  135. break; 
  136. case 'checkbox' : 
  137. $instance[ $key ] = empty( $new_instance[ $key ] ) ? 0 : 1; 
  138. break; 
  139. default: 
  140. $instance[ $key ] = sanitize_text_field( $new_instance[ $key ] ); 
  141. break; 
  142.  
  143. /** 
  144. * Sanitize the value of a setting. 
  145. */ 
  146. $instance[ $key ] = apply_filters( 'woocommerce_widget_settings_sanitize_option', $instance[ $key ], $new_instance, $key, $setting ); 
  147.  
  148. $this->flush_widget_cache(); 
  149.  
  150. return $instance; 
  151.  
  152. /** 
  153. * Outputs the settings update form. 
  154. * @see WP_Widget->form 
  155. * @param array $instance 
  156. */ 
  157. public function form( $instance ) { 
  158.  
  159. if ( empty( $this->settings ) ) { 
  160. return; 
  161.  
  162. foreach ( $this->settings as $key => $setting ) { 
  163.  
  164. $class = isset( $setting['class'] ) ? $setting['class'] : ''; 
  165. $value = isset( $instance[ $key ] ) ? $instance[ $key ] : $setting['std']; 
  166.  
  167. switch ( $setting['type'] ) { 
  168.  
  169. case 'text' : 
  170. ?> 
  171. <p> 
  172. <label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label> 
  173. <input class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo $this->get_field_name( $key ); ?>" type="text" value="<?php echo esc_attr( $value ); ?>" /> 
  174. </p> 
  175. <?php 
  176. break; 
  177.  
  178. case 'number' : 
  179. ?> 
  180. <p> 
  181. <label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label> 
  182. <input class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo $this->get_field_name( $key ); ?>" type="number" step="<?php echo esc_attr( $setting['step'] ); ?>" min="<?php echo esc_attr( $setting['min'] ); ?>" max="<?php echo esc_attr( $setting['max'] ); ?>" value="<?php echo esc_attr( $value ); ?>" /> 
  183. </p> 
  184. <?php 
  185. break; 
  186.  
  187. case 'select' : 
  188. ?> 
  189. <p> 
  190. <label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label> 
  191. <select class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo $this->get_field_name( $key ); ?>"> 
  192. <?php foreach ( $setting['options'] as $option_key => $option_value ) : ?> 
  193. <option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $option_key, $value ); ?>><?php echo esc_html( $option_value ); ?></option> 
  194. <?php endforeach; ?> 
  195. </select> 
  196. </p> 
  197. <?php 
  198. break; 
  199.  
  200. case 'textarea' : 
  201. ?> 
  202. <p> 
  203. <label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label> 
  204. <textarea class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo $this->get_field_name( $key ); ?>" cols="20" rows="3"><?php echo esc_textarea( $value ); ?></textarea> 
  205. <?php if ( isset( $setting['desc'] ) ) : ?> 
  206. <small><?php echo esc_html( $setting['desc'] ); ?></small> 
  207. <?php endif; ?> 
  208. </p> 
  209. <?php 
  210. break; 
  211.  
  212. case 'checkbox' : 
  213. ?> 
  214. <p> 
  215. <input class="checkbox <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" type="checkbox" value="1" <?php checked( $value, 1 ); ?> /> 
  216. <label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label> 
  217. </p> 
  218. <?php 
  219. break; 
  220.  
  221. // Default: run an action 
  222. default : 
  223. do_action( 'woocommerce_widget_field_' . $setting['type'], $key, $value, $setting, $instance ); 
  224. break;