Completed
Pull Request — master (#9764)
by Shiva
07:33
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
		// Loop settings and get values to save.
145
		foreach ( $this->settings as $key => $setting ) {
146
			if ( ! isset( $new_instance[ $key ] ) || ! isset( $setting['type'] ) ) {
147
				continue;
148
			}
149
150
			// Format the value based on settings type.
151
			switch ( $setting['type'] ) {
152
				case 'number' :
153
					$instance[ $key ] = absint( $new_instance[ $key ] );
154
155 View Code Duplication
					if ( isset( $setting['min'] ) && '' !== $setting['min'] ) {
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...
156
						$instance[ $key ] = max( $instance[ $key ], $setting['min'] );
157
					}
158
159 View Code Duplication
					if ( isset( $setting['max'] ) && '' !== $setting['max'] ) {
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...
160
						$instance[ $key ] = min( $instance[ $key ], $setting['max'] );
161
					}
162
				break;
163
				case 'checkbox' :
164
					$instance[ $key ] = is_null( $new_instance[ $key ] );
165
					break;
166
				case 'textarea' :
167
					$instance[ $key ] = wp_kses_post( trim( $new_instance[ $key ] ) );
168
				break;
169
				default:
170
					$instance[ $key ] = sanitize_text_field( $new_instance[ $key ] );
171
					break;
172
			}
173
		}
174
175
		$this->flush_widget_cache();
176
177
		return $instance;
178
	}
179
180
	/**
181
	 * form function.
182
	 *
183
	 * @see   WP_Widget->form
184
	 * @param array $instance
185
	 */
186
	public function form( $instance ) {
187
188
		if ( empty( $this->settings ) ) {
189
			return;
190
		}
191
192
		foreach ( $this->settings as $key => $setting ) {
193
194
			$class = isset( $setting['class'] ) ? $setting['class'] : '';
195
			$value = isset( $instance[ $key ] ) ? $instance[ $key ] : $setting['std'];
196
197
			switch ( $setting['type'] ) {
198
199 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...
200
					?>
201
					<p>
202
						<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
203
						<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 ); ?>" />
204
					</p>
205
					<?php
206
				break;
207
208
				case 'number' :
209
					?>
210
					<p>
211
						<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
212
						<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 ); ?>" />
213
					</p>
214
					<?php
215
				break;
216
217
				case 'select' :
218
					?>
219
					<p>
220
						<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
221
						<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 ); ?>">
222
							<?php foreach ( $setting['options'] as $option_key => $option_value ) : ?>
223
								<option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $option_key, $value ); ?>><?php echo esc_html( $option_value ); ?></option>
224
							<?php endforeach; ?>
225
						</select>
226
					</p>
227
					<?php
228
				break;
229
230 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...
231
					?>
232
					<p>
233
						<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 ); ?> />
234
						<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
235
					</p>
236
					<?php
237
				break;
238
239
				case 'textarea' :
240
					?>
241
					<p>
242
						<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
243
						<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>
244
						<?php if ( isset( $setting['desc'] ) ) : ?>
245
							<small><?php echo esc_html( $setting['desc'] ); ?></small>
246
						<?php endif; ?>
247
					</p>
248
					<?php
249
				break;
250
			}
251
		}
252
	}
253
}
254