wc-meta-box-functions.php ➔ woocommerce_wp_select()   F
last analyzed

Complexity

Conditions 14
Paths 768

Size

Total Lines 38
Code Lines 22

Duplication

Lines 17
Ratio 44.74 %

Importance

Changes 0
Metric Value
cc 14
dl 17
loc 38
rs 2.9875
c 0
b 0
f 0
eloc 22
nc 768
nop 1

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
* WooCommerce Meta Box Functions
4
*
5
* @author      WooThemes
6
* @category    Core
7
* @package     WooCommerce/Admin/Functions
8
* @version     2.3.0
9
*/
10
if ( ! defined( 'ABSPATH' ) ) {
11
	exit; // Exit if accessed directly
12
}
13
14
/**
15
 * Output a text input box.
16
 *
17
 * @param array $field
18
 */
19
function woocommerce_wp_text_input( $field ) {
20
	global $thepostid, $post;
21
22
	$thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
23
	$field['placeholder']   = isset( $field['placeholder'] ) ? $field['placeholder'] : '';
24
	$field['class']         = isset( $field['class'] ) ? $field['class'] : 'short';
25
	$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
26
	$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
27
	$field['value']         = isset( $field['value'] ) ? $field['value'] : get_post_meta( $thepostid, $field['id'], true );
28
	$field['name']          = isset( $field['name'] ) ? $field['name'] : $field['id'];
29
	$field['type']          = isset( $field['type'] ) ? $field['type'] : 'text';
30
	$data_type              = empty( $field['data_type'] ) ? '' : $field['data_type'];
31
32
	switch ( $data_type ) {
33
		case 'price' :
34
			$field['class'] .= ' wc_input_price';
35
			$field['value']  = wc_format_localized_price( $field['value'] );
36
			break;
37
		case 'decimal' :
38
			$field['class'] .= ' wc_input_decimal';
39
			$field['value']  = wc_format_localized_decimal( $field['value'] );
40
			break;
41
		case 'stock' :
42
			$field['class'] .= ' wc_input_stock';
43
			$field['value']  = wc_stock_amount( $field['value'] );
44
			break;
45
		case 'url' :
46
			$field['class'] .= ' wc_input_url';
47
			$field['value']  = esc_url( $field['value'] );
48
			break;
49
50
		default :
51
			break;
52
	}
53
54
	// Custom attribute handling
55
	$custom_attributes = array();
56
57 View Code Duplication
	if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) {
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...
58
59
		foreach ( $field['custom_attributes'] as $attribute => $value ){
60
			$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
61
		}
62
	}
63
64
	echo '<p class="form-field ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '"><label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label><input type="' . esc_attr( $field['type'] ) . '" class="' . esc_attr( $field['class'] ) . '" style="' . esc_attr( $field['style'] ) . '" name="' . esc_attr( $field['name'] ) . '" id="' . esc_attr( $field['id'] ) . '" value="' . esc_attr( $field['value'] ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '" ' . implode( ' ', $custom_attributes ) . ' /> ';
65
66 View Code Duplication
	if ( ! empty( $field['description'] ) ) {
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...
67
68
		if ( isset( $field['desc_tip'] ) && false !== $field['desc_tip'] ) {
69
			echo wc_help_tip( $field['description'] );
70
		} else {
71
			echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>';
72
		}
73
	}
74
	echo '</p>';
75
}
76
77
/**
78
 * Output a hidden input box.
79
 *
80
 * @param array $field
81
 */
82
function woocommerce_wp_hidden_input( $field ) {
83
	global $thepostid, $post;
84
85
	$thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
86
	$field['value'] = isset( $field['value'] ) ? $field['value'] : get_post_meta( $thepostid, $field['id'], true );
87
	$field['class'] = isset( $field['class'] ) ? $field['class'] : '';
88
89
	echo '<input type="hidden" class="' . esc_attr( $field['class'] ) . '" name="' . esc_attr( $field['id'] ) . '" id="' . esc_attr( $field['id'] ) . '" value="' . esc_attr( $field['value'] ) .  '" /> ';
90
}
91
92
/**
93
 * Output a textarea input box.
94
 *
95
 * @param array $field
96
 */
97
function woocommerce_wp_textarea_input( $field ) {
98
	global $thepostid, $post;
99
100
	$thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
101
	$field['placeholder']   = isset( $field['placeholder'] ) ? $field['placeholder'] : '';
102
	$field['class']         = isset( $field['class'] ) ? $field['class'] : 'short';
103
	$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
104
	$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
105
	$field['value']         = isset( $field['value'] ) ? $field['value'] : get_post_meta( $thepostid, $field['id'], true );
106
107
	// Custom attribute handling
108
	$custom_attributes = array();
109
110 View Code Duplication
	if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) {
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...
111
112
		foreach ( $field['custom_attributes'] as $attribute => $value ){
113
			$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
114
		}
115
	}
116
117
	echo '<p class="form-field ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '"><label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label><textarea class="' . esc_attr( $field['class'] ) . '" style="' . esc_attr( $field['style'] ) . '"  name="' . esc_attr( $field['id'] ) . '" id="' . esc_attr( $field['id'] ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '" rows="2" cols="20" ' . implode( ' ', $custom_attributes ) . '>' . esc_textarea( $field['value'] ) . '</textarea> ';
118
119 View Code Duplication
	if ( ! empty( $field['description'] ) ) {
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...
120
121
		if ( isset( $field['desc_tip'] ) && false !== $field['desc_tip'] ) {
122
			echo wc_help_tip( $field['description'] );
123
		} else {
124
			echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>';
125
		}
126
	}
127
	echo '</p>';
128
}
129
130
/**
131
 * Output a checkbox input box.
132
 *
133
 * @param array $field
134
 */
135
function woocommerce_wp_checkbox( $field ) {
136
	global $thepostid, $post;
137
138
	$thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
139
	$field['class']         = isset( $field['class'] ) ? $field['class'] : 'checkbox';
140
	$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
141
	$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
142
	$field['value']         = isset( $field['value'] ) ? $field['value'] : get_post_meta( $thepostid, $field['id'], true );
143
	$field['cbvalue']       = isset( $field['cbvalue'] ) ? $field['cbvalue'] : 'yes';
144
	$field['name']          = isset( $field['name'] ) ? $field['name'] : $field['id'];
145
146
	// Custom attribute handling
147
	$custom_attributes = array();
148
149 View Code Duplication
	if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) {
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...
150
151
		foreach ( $field['custom_attributes'] as $attribute => $value ){
152
			$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
153
		}
154
	}
155
156
	echo '<p class="form-field ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '"><label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label><input type="checkbox" class="' . esc_attr( $field['class'] ) . '" style="' . esc_attr( $field['style'] ) . '" name="' . esc_attr( $field['name'] ) . '" id="' . esc_attr( $field['id'] ) . '" value="' . esc_attr( $field['cbvalue'] ) . '" ' . checked( $field['value'], $field['cbvalue'], false ) . '  ' . implode( ' ', $custom_attributes ) . '/> ';
157
158 View Code Duplication
	if ( ! empty( $field['description'] ) ) {
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
160
		if ( isset( $field['desc_tip'] ) && false !== $field['desc_tip'] ) {
161
			echo wc_help_tip( $field['description'] );
162
		} else {
163
			echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>';
164
		}
165
	}
166
167
	echo '</p>';
168
}
169
170
/**
171
 * Output a select input box.
172
 *
173
 * @param array $field
174
 */
175
function woocommerce_wp_select( $field ) {
176
	global $thepostid, $post;
177
178
	$thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
179
	$field['class']         = isset( $field['class'] ) ? $field['class'] : 'select short';
180
	$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
181
	$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
182
	$field['value']         = isset( $field['value'] ) ? $field['value'] : get_post_meta( $thepostid, $field['id'], true );
183
	$field['name']          = isset( $field['name'] ) ? $field['name'] : $field['id'];
184
185
	// Custom attribute handling
186
	$custom_attributes = array();
187
188 View Code Duplication
	if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) {
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
		foreach ( $field['custom_attributes'] as $attribute => $value ){
191
			$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
192
		}
193
	}
194
195
	echo '<p class="form-field ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '"><label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label><select id="' . esc_attr( $field['id'] ) . '" name="' . esc_attr( $field['name'] ) . '" class="' . esc_attr( $field['class'] ) . '" style="' . esc_attr( $field['style'] ) . '" ' . implode( ' ', $custom_attributes ) . '>';
196
197 View Code Duplication
	foreach ( $field['options'] as $key => $value ) {
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...
198
		echo '<option value="' . esc_attr( $key ) . '" ' . selected( esc_attr( $field['value'] ), esc_attr( $key ), false ) . '>' . esc_html( $value ) . '</option>';
199
	}
200
201
	echo '</select> ';
202
203 View Code Duplication
	if ( ! empty( $field['description'] ) ) {
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...
204
205
		if ( isset( $field['desc_tip'] ) && false !== $field['desc_tip'] ) {
206
			echo wc_help_tip( $field['description'] );
207
		} else {
208
			echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>';
209
		}
210
	}
211
	echo '</p>';
212
}
213
214
/**
215
 * Output a radio input box.
216
 *
217
 * @param array $field
218
 */
219
function woocommerce_wp_radio( $field ) {
220
	global $thepostid, $post;
221
222
	$thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
223
	$field['class']         = isset( $field['class'] ) ? $field['class'] : 'select short';
224
	$field['style']         = isset( $field['style'] ) ? $field['style'] : '';
225
	$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
226
	$field['value']         = isset( $field['value'] ) ? $field['value'] : get_post_meta( $thepostid, $field['id'], true );
227
	$field['name']          = isset( $field['name'] ) ? $field['name'] : $field['id'];
228
229
	echo '<fieldset class="form-field ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '"><legend>' . wp_kses_post( $field['label'] ) . '</legend><ul class="wc-radios">';
230
231
	foreach ( $field['options'] as $key => $value ) {
232
233
		echo '<li><label><input
234
				name="' . esc_attr( $field['name'] ) . '"
235
				value="' . esc_attr( $key ) . '"
236
				type="radio"
237
				class="' . esc_attr( $field['class'] ) . '"
238
				style="' . esc_attr( $field['style'] ) . '"
239
				' . checked( esc_attr( $field['value'] ), esc_attr( $key ), false ) . '
240
				/> ' . esc_html( $value ) . '</label>
241
		</li>';
242
	}
243
	echo '</ul>';
244
245 View Code Duplication
	if ( ! empty( $field['description'] ) ) {
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...
246
247
		if ( isset( $field['desc_tip'] ) && false !== $field['desc_tip'] ) {
248
			echo wc_help_tip( $field['description'] );
249
		} else {
250
			echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>';
251
		}
252
	}
253
254
	echo '</fieldset>';
255
}
256