Completed
Push — master ( b96242...112f3b )
by Claudio
08:48
created

WC_Widget::widget_start()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.4286
cc 3
eloc 4
nc 2
nop 2
1
<?php
2
/**
3
 * Abstract Widget Class
4
 *
5
 * @author   WooThemes
6
 * @category Widgets
7
 * @package  WooCommerce/Abstracts
8
 * @version  2.5.0
9
 * @extends  WP_Widget
10
 */
11
abstract class WC_Widget extends WP_Widget {
12
13
	/**
14
	 * CSS class.
15
	 *
16
	 * @var string
17
	 */
18
	public $widget_cssclass;
19
20
	/**
21
	 * Widget description.
22
	 *
23
	 * @var string
24
	 */
25
	public $widget_description;
26
27
	/**
28
	 * Widget ID.
29
	 *
30
	 * @var string
31
	 */
32
	public $widget_id;
33
34
	/**
35
	 * Widget name.
36
	 *
37
	 * @var string
38
	 */
39
	public $widget_name;
40
41
	/**
42
	 * Settings.
43
	 *
44
	 * @var array
45
	 */
46
	public $settings;
47
48
	/**
49
	 * Constructor.
50
	 */
51
	public function __construct() {
52
53
		$widget_ops = array(
54
			'classname'   => $this->widget_cssclass,
55
			'description' => $this->widget_description
56
		);
57
58
		parent::__construct( $this->widget_id, $this->widget_name, $widget_ops );
59
60
		add_action( 'save_post', array( $this, 'flush_widget_cache' ) );
61
		add_action( 'deleted_post', array( $this, 'flush_widget_cache' ) );
62
		add_action( 'switch_theme', array( $this, 'flush_widget_cache' ) );
63
	}
64
65
	/**
66
	 * get_cached_widget function.
67
	 */
68
	public function get_cached_widget( $args ) {
69
70
		$cache = wp_cache_get( apply_filters( 'woocommerce_cached_widget_id', $this->widget_id ), 'widget' );
71
72
		if ( ! is_array( $cache ) ) {
73
			$cache = array();
74
		}
75
76
		if ( isset( $cache[ $args['widget_id'] ] ) ) {
77
			echo $cache[ $args['widget_id'] ];
78
			return true;
79
		}
80
81
		return false;
82
	}
83
84
	/**
85
	 * Cache the widget.
86
	 *
87
	 * @param  array $args
88
	 * @param  string $content
89
	 * @return string the content that was cached
90
	 */
91
	public function cache_widget( $args, $content ) {
92
		wp_cache_set( apply_filters( 'woocommerce_cached_widget_id', $this->widget_id ), array( $args['widget_id'] => $content ), 'widget' );
93
94
		return $content;
95
	}
96
97
	/**
98
	 * Flush the cache.
99
	 */
100
	public function flush_widget_cache() {
101
		wp_cache_delete( apply_filters( 'woocommerce_cached_widget_id', $this->widget_id ), 'widget' );
102
	}
103
104
	/**
105
	 * Output the html at the start of a widget.
106
	 *
107
	 * @param  array $args
108
	 * @return string
109
	 */
110
	public function widget_start( $args, $instance ) {
111
		echo $args['before_widget'];
112
113
		if ( $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base ) ) {
114
			echo $args['before_title'] . $title . $args['after_title'];
115
		}
116
	}
117
118
	/**
119
	 * Output the html at the end of a widget.
120
	 *
121
	 * @param  array $args
122
	 * @return string
123
	 */
124
	public function widget_end( $args ) {
125
		echo $args['after_widget'];
126
	}
127
128
	/**
129
	 * update function.
130
	 *
131
	 * @see    WP_Widget->update
132
	 * @param  array $new_instance
133
	 * @param  array $old_instance
134
	 * @return array
135
	 */
136
	public function update( $new_instance, $old_instance ) {
137
138
		$instance = $old_instance;
139
140
		if ( empty( $this->settings ) ) {
141
			return $instance;
142
		}
143
144
		foreach ( $this->settings as $key => $setting ) {
145
146
			if ( isset( $new_instance[ $key ] ) ) {
147
				$instance[ $key ] = sanitize_text_field( $new_instance[ $key ] );
148
			} elseif ( 'checkbox' === $setting['type'] ) {
149
				$instance[ $key ] = 0;
150
			}
151
		}
152
153
		$this->flush_widget_cache();
154
155
		return $instance;
156
	}
157
158
	/**
159
	 * form function.
160
	 *
161
	 * @see   WP_Widget->form
162
	 * @param array $instance
163
	 */
164
	public function form( $instance ) {
165
166
		if ( empty( $this->settings ) ) {
167
			return;
168
		}
169
170
		foreach ( $this->settings as $key => $setting ) {
171
172
			$class = isset( $setting['class'] ) ? $setting['class'] : '';
173
			$value = isset( $instance[ $key ] ) ? $instance[ $key ] : $setting['std'];
174
175
			switch ( $setting['type'] ) {
176
177 View Code Duplication
				case 'text' :
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
					?>
179
					<p>
180
						<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
181
						<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 ); ?>" />
182
					</p>
183
					<?php
184
				break;
185
186
				case 'number' :
187
					?>
188
					<p>
189
						<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
190
						<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 ); ?>" />
191
					</p>
192
					<?php
193
				break;
194
195
				case 'select' :
196
					?>
197
					<p>
198
						<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
199
						<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 ); ?>">
200
							<?php foreach ( $setting['options'] as $option_key => $option_value ) : ?>
201
								<option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $option_key, $value ); ?>><?php echo esc_html( $option_value ); ?></option>
202
							<?php endforeach; ?>
203
						</select>
204
					</p>
205
					<?php
206
				break;
207
208 View Code Duplication
				case 'checkbox' :
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
209
					?>
210
					<p>
211
						<input class="widefat <?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 ); ?> />
212
						<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
213
					</p>
214
					<?php
215
				break;
216
			}
217
		}
218
	}
219
}
220