WC_Widget::get_cached_widget()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
eloc 8
nc 4
nop 1
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
		$widget_ops = array(
53
			'classname'   => $this->widget_cssclass,
54
			'description' => $this->widget_description,
55
			'customize_selective_refresh' => true
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.
67
	 *
68
	 * @param  array $args
69
	 * @return bool true if the widget is cached otherwise false
70
	 */
71
	public function get_cached_widget( $args ) {
72
73
		$cache = wp_cache_get( apply_filters( 'woocommerce_cached_widget_id', $this->widget_id ), 'widget' );
74
75
		if ( ! is_array( $cache ) ) {
76
			$cache = array();
77
		}
78
79
		if ( isset( $cache[ $args['widget_id'] ] ) ) {
80
			echo $cache[ $args['widget_id'] ];
81
			return true;
82
		}
83
84
		return false;
85
	}
86
87
	/**
88
	 * Cache the widget.
89
	 *
90
	 * @param  array $args
91
	 * @param  string $content
92
	 * @return string the content that was cached
93
	 */
94
	public function cache_widget( $args, $content ) {
95
		wp_cache_set( apply_filters( 'woocommerce_cached_widget_id', $this->widget_id ), array( $args['widget_id'] => $content ), 'widget' );
96
97
		return $content;
98
	}
99
100
	/**
101
	 * Flush the cache.
102
	 */
103
	public function flush_widget_cache() {
104
		wp_cache_delete( apply_filters( 'woocommerce_cached_widget_id', $this->widget_id ), 'widget' );
105
	}
106
107
	/**
108
	 * Output the html at the start of a widget.
109
	 *
110
	 * @param  array $args
111
	 * @return string
112
	 */
113
	public function widget_start( $args, $instance ) {
114
		echo $args['before_widget'];
115
116
		if ( $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base ) ) {
117
			echo $args['before_title'] . $title . $args['after_title'];
118
		}
119
	}
120
121
	/**
122
	 * Output the html at the end of a widget.
123
	 *
124
	 * @param  array $args
125
	 * @return string
126
	 */
127
	public function widget_end( $args ) {
128
		echo $args['after_widget'];
129
	}
130
131
	/**
132
	 * Updates a particular instance of a widget.
133
	 *
134
	 * @see    WP_Widget->update
135
	 * @param  array $new_instance
136
	 * @param  array $old_instance
137
	 * @return array
138
	 */
139
	public function update( $new_instance, $old_instance ) {
140
141
		$instance = $old_instance;
142
143
		if ( empty( $this->settings ) ) {
144
			return $instance;
145
		}
146
147
		// Loop settings and get values to save.
148
		foreach ( $this->settings as $key => $setting ) {
149
			if ( ! isset( $setting['type'] ) ) {
150
				continue;
151
			}
152
153
			// Format the value based on settings type.
154
			switch ( $setting['type'] ) {
155
				case 'number' :
156
					$instance[ $key ] = absint( $new_instance[ $key ] );
157
158 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...
159
						$instance[ $key ] = max( $instance[ $key ], $setting['min'] );
160
					}
161
162 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...
163
						$instance[ $key ] = min( $instance[ $key ], $setting['max'] );
164
					}
165
				break;
166
				case 'textarea' :
167
					$instance[ $key ] = wp_kses( trim( wp_unslash( $new_instance[ $key ] ) ), wp_kses_allowed_html( 'post' ) );
168
				break;
169
				case 'checkbox' :
170
					$instance[ $key ] = empty( $new_instance[ $key ] ) ? 0 : 1;
171
				break;
172
				default:
173
					$instance[ $key ] = sanitize_text_field( $new_instance[ $key ] );
174
				break;
175
			}
176
177
			/**
178
			 * Sanitize the value of a setting.
179
			 */
180
			$instance[ $key ] = apply_filters( 'woocommerce_widget_settings_sanitize_option', $instance[ $key ], $new_instance, $key, $setting );
181
		}
182
183
		$this->flush_widget_cache();
184
185
		return $instance;
186
	}
187
188
	/**
189
	 * Outputs the settings update form.
190
	 *
191
	 * @see   WP_Widget->form
192
	 * @param array $instance
193
	 */
194
	public function form( $instance ) {
195
196
		if ( empty( $this->settings ) ) {
197
			return;
198
		}
199
200
		foreach ( $this->settings as $key => $setting ) {
201
202
			$class = isset( $setting['class'] ) ? $setting['class'] : '';
203
			$value = isset( $instance[ $key ] ) ? $instance[ $key ] : $setting['std'];
204
205
			switch ( $setting['type'] ) {
206
207 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...
208
					?>
209
					<p>
210
						<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
211
						<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 ); ?>" />
212
					</p>
213
					<?php
214
				break;
215
216
				case 'number' :
217
					?>
218
					<p>
219
						<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
220
						<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 ); ?>" />
221
					</p>
222
					<?php
223
				break;
224
225
				case 'select' :
226
					?>
227
					<p>
228
						<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
229
						<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 ); ?>">
230
							<?php foreach ( $setting['options'] as $option_key => $option_value ) : ?>
231
								<option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $option_key, $value ); ?>><?php echo esc_html( $option_value ); ?></option>
232
							<?php endforeach; ?>
233
						</select>
234
					</p>
235
					<?php
236
				break;
237
238
				case 'textarea' :
239
					?>
240
					<p>
241
						<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
242
						<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>
243
						<?php if ( isset( $setting['desc'] ) ) : ?>
244
							<small><?php echo esc_html( $setting['desc'] ); ?></small>
245
						<?php endif; ?>
246
					</p>
247
					<?php
248
				break;
249
250 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...
251
					?>
252
					<p>
253
						<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 ); ?> />
254
						<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
255
					</p>
256
					<?php
257
				break;
258
259
				// Default: run an action
260
				default :
261
					do_action( 'woocommerce_widget_field_' . $setting['type'], $key, $value, $setting, $instance );
262
				break;
263
			}
264
		}
265
	}
266
}
267