Completed
Push — master ( 3a19cf...b31e15 )
by Claudio
11:16
created

WC_Widget::update()   C

Complexity

Conditions 11
Paths 10

Size

Total Lines 32
Code Lines 19

Duplication

Lines 6
Ratio 18.75 %
Metric Value
dl 6
loc 32
rs 5.2653
cc 11
eloc 19
nc 10
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
			if ( 'number' === $setting['type'] ) {
146
				$instance[ $key ] = sanitize_text_field( $new_instance[ $key ] );
147
148 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...
149
					$instance[ $key ] = max( $instance[ $key ], $setting['min'] );
150
				}
151
152 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...
153
					$instance[ $key ] = min( $instance[ $key ], $setting['max'] );
154
				}
155
			} elseif ( 'textarea' === $setting['type'] ) {
156
				$instance[ $key ] = wp_kses( trim( wp_unslash( $new_instance[ $key ] ) ), wp_kses_allowed_html( 'post' ) );
157
			} elseif ( isset( $new_instance[ $key ] ) ) {
158
				$instance[ $key ] = sanitize_text_field( $new_instance[ $key ] );
159
			} elseif ( 'checkbox' === $setting['type'] ) {
160
				$instance[ $key ] = 0;
161
			}
162
		}
163
164
		$this->flush_widget_cache();
165
166
		return $instance;
167
	}
168
169
	/**
170
	 * form function.
171
	 *
172
	 * @see   WP_Widget->form
173
	 * @param array $instance
174
	 */
175
	public function form( $instance ) {
176
177
		if ( empty( $this->settings ) ) {
178
			return;
179
		}
180
181
		foreach ( $this->settings as $key => $setting ) {
182
183
			$class = isset( $setting['class'] ) ? $setting['class'] : '';
184
			$value = isset( $instance[ $key ] ) ? $instance[ $key ] : $setting['std'];
185
186
			switch ( $setting['type'] ) {
187
188 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...
189
					?>
190
					<p>
191
						<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
192
						<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 ); ?>" />
193
					</p>
194
					<?php
195
				break;
196
197
				case 'number' :
198
					?>
199
					<p>
200
						<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
201
						<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 ); ?>" />
202
					</p>
203
					<?php
204
				break;
205
206
				case 'select' :
207
					?>
208
					<p>
209
						<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
210
						<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 ); ?>">
211
							<?php foreach ( $setting['options'] as $option_key => $option_value ) : ?>
212
								<option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $option_key, $value ); ?>><?php echo esc_html( $option_value ); ?></option>
213
							<?php endforeach; ?>
214
						</select>
215
					</p>
216
					<?php
217
				break;
218
219
				case 'textarea' :
220
					?>
221
					<p>
222
						<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
223
						<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>
224
						<?php if ( isset( $setting['desc'] ) ) : ?>
225
							<small><?php echo esc_html( $setting['desc'] ); ?></small>
226
						<?php endif; ?>
227
					</p>
228
					<?php
229
				break;
230
231 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...
232
					?>
233
					<p>
234
						<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 ); ?> />
235
						<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
236
					</p>
237
					<?php
238
				break;
239
			}
240
		}
241
	}
242
}
243