1 <?php
2 3 4 5 6 7 8 9 10
11 abstract class WC_Widget extends WP_Widget {
12
13 public $widget_cssclass;
14 public $widget_description;
15 public $widget_id;
16 public $widget_name;
17 public $settings;
18
19 20 21
22 public function __construct() {
23 $widget_ops = array(
24 'classname' => $this->widget_cssclass,
25 'description' => $this->widget_description
26 );
27
28 $this->WP_Widget( $this->widget_id, $this->widget_name, $widget_ops );
29
30 add_action( 'save_post', array( $this, 'flush_widget_cache' ) );
31 add_action( 'deleted_post', array( $this, 'flush_widget_cache' ) );
32 add_action( 'switch_theme', array( $this, 'flush_widget_cache' ) );
33 }
34
35 36 37
38 function get_cached_widget( $args ) {
39 $cache = wp_cache_get( $this->widget_id, 'widget' );
40
41 if ( ! is_array( $cache ) )
42 $cache = array();
43
44 if ( isset( $cache[ $args['widget_id'] ] ) ) {
45 echo $cache[ $args['widget_id'] ];
46 return true;
47 }
48
49 return false;
50 }
51
52 53 54
55 public function cache_widget( $args, $content ) {
56 $cache[ $args['widget_id'] ] = $content;
57
58 wp_cache_set( $this->widget_id, $cache, 'widget' );
59 }
60
61 62 63 64
65 public function flush_widget_cache() {
66 wp_cache_delete( $this->widget_id, 'widget' );
67 }
68
69 70 71 72 73 74 75 76 77
78 function update( $new_instance, $old_instance ) {
79 $instance = $old_instance;
80
81 if ( ! $this->settings )
82 return $instance;
83
84 foreach ( $this->settings as $key => $setting ) {
85 if ( isset( $new_instance[ $key ] ) ) {
86 $instance[ $key ] = sanitize_text_field( $new_instance[ $key ] );
87 } elseif ( 'checkbox' === $setting['type'] ) {
88 $instance[ $key ] = 0;
89 }
90 }
91
92 $this->flush_widget_cache();
93
94 return $instance;
95 }
96
97 98 99 100 101 102 103 104
105 function form( $instance ) {
106
107 if ( ! $this->settings )
108 return;
109
110 foreach ( $this->settings as $key => $setting ) {
111
112 $value = isset( $instance[ $key ] ) ? $instance[ $key ] : $setting['std'];
113
114 switch ( $setting['type'] ) {
115 case "text" :
116 ?>
117 <p>
118 <label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
119 <input class="widefat" 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 ); ?>" />
120 </p>
121 <?php
122 break;
123 case "number" :
124 ?>
125 <p>
126 <label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
127 <input class="widefat" 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 ); ?>" />
128 </p>
129 <?php
130 break;
131 case "select" :
132 ?>
133 <p>
134 <label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
135 <select class="widefat" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo $this->get_field_name( $key ); ?>">
136 <?php foreach ( $setting['options'] as $option_key => $option_value ) : ?>
137 <option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $option_key, $value ); ?>><?php echo esc_html( $option_value ); ?></option>
138 <?php endforeach; ?>
139 </select>
140 </p>
141 <?php
142 break;
143 case "checkbox" :
144 ?>
145 <p>
146 <input 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 ); ?> />
147 <label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
148 </p>
149 <?php
150 break;
151 }
152 }
153 }
154 }