1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Abstract Settings API Class |
4
|
|
|
* |
5
|
|
|
* Admin Settings API used by Integrations, Shipping Methods, and Payment Gateways. |
6
|
|
|
* |
7
|
|
|
* @class WC_Settings_API |
8
|
|
|
* @version 2.6.0 |
9
|
|
|
* @package WooCommerce/Abstracts |
10
|
|
|
* @category Abstract Class |
11
|
|
|
* @author WooThemes |
12
|
|
|
*/ |
13
|
|
|
abstract class WC_Settings_API { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The plugin ID. Used for option names. |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
public $plugin_id = 'woocommerce_'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* ID of the class extending the settings API. Used in option names. |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
public $id = ''; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Validation errors. |
29
|
|
|
* @var array of strings |
30
|
|
|
*/ |
31
|
|
|
public $errors = array(); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Setting values. |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
public $settings = array(); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Form option fields. |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
public $form_fields = array(); |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The posted settings data. When empty, $_POST data will be used. |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
protected $data = array(); |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get the form fields after they are initialized. |
53
|
|
|
* @return array of options |
54
|
|
|
*/ |
55
|
|
|
public function get_form_fields() { |
56
|
|
|
return apply_filters( 'woocommerce_settings_api_form_fields_' . $this->id, array_map( array( $this, 'set_defaults' ), $this->form_fields ) ); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Set default required properties for each field. |
61
|
|
|
* @param array |
62
|
|
|
*/ |
63
|
|
|
private function set_defaults( $field ) { |
64
|
|
|
if ( ! isset( $field['default'] ) ) { |
65
|
|
|
$field['default'] = ''; |
66
|
|
|
} |
67
|
|
|
return $field; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Output the admin options table. |
72
|
|
|
*/ |
73
|
|
|
public function admin_options() { |
74
|
|
|
echo '<table class="form-table">' . $this->generate_settings_html( $this->get_form_fields(), false ) . '</table>'; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Return the name of the option in the WP DB. |
79
|
|
|
* @since 2.6.0 |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function get_option_key() { |
83
|
|
|
return $this->plugin_id . $this->id . '_settings'; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get a fields type. Defaults to "text" if not set. |
88
|
|
|
* @param array $field |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function get_field_type( $field ) { |
92
|
|
|
return empty( $field['type'] ) ? 'text' : $field['type']; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get a fields default value. Defaults to "" if not set. |
97
|
|
|
* @param array $field |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function get_field_default( $field ) { |
101
|
|
|
return empty( $field['default'] ) ? '' : $field['default']; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get a field's posted and validated value. |
106
|
|
|
* @param string $key |
107
|
|
|
* @param array $field |
108
|
|
|
* @param array $post_data |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function get_field_value( $key, $field, $post_data = array() ) { |
112
|
|
|
$type = $this->get_field_type( $field ); |
113
|
|
|
$field_key = $this->get_field_key( $key ); |
114
|
|
|
$post_data = empty( $post_data ) ? $_POST : $post_data; |
115
|
|
|
$value = isset( $post_data[ $field_key ] ) ? $post_data[ $field_key ] : null; |
116
|
|
|
|
117
|
|
|
// Look for a validate_FIELDID_field method for special handling |
118
|
|
View Code Duplication |
if ( is_callable( array( $this, 'validate_' . $key . '_field' ) ) ) { |
119
|
|
|
return $this->{'validate_' . $key . '_field'}( $key, $value ); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// Look for a validate_FIELDTYPE_field method |
123
|
|
View Code Duplication |
if ( is_callable( array( $this, 'validate_' . $type . '_field' ) ) ) { |
124
|
|
|
return $this->{'validate_' . $type . '_field'}( $key, $value ); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// Fallback to text |
128
|
|
|
return $this->validate_text_field( $key, $value ); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Sets the POSTed data. This method can be used to set specific data, instead |
133
|
|
|
* of taking it from the $_POST array. |
134
|
|
|
* @param array data |
135
|
|
|
*/ |
136
|
|
|
public function set_post_data( $data = array() ) { |
137
|
|
|
$this->data = $data; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Returns the POSTed data, to be used to save the settings. |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
|
|
public function get_post_data() { |
145
|
|
|
if ( ! empty( $this->data ) && is_array( $this->data ) ) { |
146
|
|
|
return $this->data; |
147
|
|
|
} |
148
|
|
|
return $_POST; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Processes and saves options. |
153
|
|
|
* If there is an error thrown, will continue to save and validate fields, but will leave the erroring field out. |
154
|
|
|
* @return bool was anything saved? |
155
|
|
|
*/ |
156
|
|
|
public function process_admin_options() { |
157
|
|
|
$this->init_settings(); |
158
|
|
|
|
159
|
|
|
$post_data = $this->get_post_data(); |
160
|
|
|
|
161
|
|
View Code Duplication |
foreach ( $this->get_form_fields() as $key => $field ) { |
|
|
|
|
162
|
|
|
if ( 'title' !== $this->get_field_type( $field ) ) { |
163
|
|
|
try { |
164
|
|
|
$this->settings[ $key ] = $this->get_field_value( $key, $field, $post_data ); |
165
|
|
|
} catch ( Exception $e ) { |
166
|
|
|
$this->add_error( $e->getMessage() ); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return update_option( $this->get_option_key(), apply_filters( 'woocommerce_settings_api_sanitized_fields_' . $this->id, $this->settings ) ); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Add an error message for display in admin on save. |
176
|
|
|
* @param string $error |
177
|
|
|
*/ |
178
|
|
|
public function add_error( $error ) { |
179
|
|
|
$this->errors[] = $error; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get admin error messages. |
184
|
|
|
*/ |
185
|
|
|
public function get_errors() { |
186
|
|
|
return $this->errors; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Display admin error messages. |
191
|
|
|
*/ |
192
|
|
|
public function display_errors() { |
193
|
|
|
if ( $this->get_errors() ) { |
194
|
|
|
echo '<div id="woocommerce_errors" class="error notice is-dismissible">'; |
195
|
|
|
foreach ( $this->get_errors() as $error ) { |
196
|
|
|
echo '<p>' . wp_kses_post( $error ) . '</p>'; |
197
|
|
|
} |
198
|
|
|
echo '</div>'; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Initialise Settings. |
204
|
|
|
* |
205
|
|
|
* Store all settings in a single database entry |
206
|
|
|
* and make sure the $settings array is either the default |
207
|
|
|
* or the settings stored in the database. |
208
|
|
|
* |
209
|
|
|
* @since 1.0.0 |
210
|
|
|
* @uses get_option(), add_option() |
211
|
|
|
*/ |
212
|
|
View Code Duplication |
public function init_settings() { |
|
|
|
|
213
|
|
|
$this->settings = get_option( $this->get_option_key(), null ); |
214
|
|
|
|
215
|
|
|
// If there are no settings defined, use defaults. |
216
|
|
|
if ( ! is_array( $this->settings ) ) { |
217
|
|
|
$form_fields = $this->get_form_fields(); |
218
|
|
|
$this->settings = array_merge( array_fill_keys( array_keys( $form_fields ), '' ), wp_list_pluck( $form_fields, 'default' ) ); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* get_option function. |
224
|
|
|
* |
225
|
|
|
* Gets an option from the settings API, using defaults if necessary to prevent undefined notices. |
226
|
|
|
* |
227
|
|
|
* @param string $key |
228
|
|
|
* @param mixed $empty_value |
229
|
|
|
* @return string The value specified for the option or a default value for the option. |
230
|
|
|
*/ |
231
|
|
View Code Duplication |
public function get_option( $key, $empty_value = null ) { |
|
|
|
|
232
|
|
|
if ( empty( $this->settings ) ) { |
233
|
|
|
$this->init_settings(); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
// Get option default if unset. |
237
|
|
|
if ( ! isset( $this->settings[ $key ] ) ) { |
238
|
|
|
$form_fields = $this->get_form_fields(); |
239
|
|
|
$this->settings[ $key ] = isset( $form_fields[ $key ] ) ? $this->get_field_default( $form_fields[ $key ] ) : ''; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
if ( ! is_null( $empty_value ) && '' === $this->settings[ $key ] ) { |
243
|
|
|
$this->settings[ $key ] = $empty_value; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
return $this->settings[ $key ]; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Prefix key for settings. |
251
|
|
|
* |
252
|
|
|
* @param mixed $key |
253
|
|
|
* @return string |
254
|
|
|
*/ |
255
|
|
|
public function get_field_key( $key ) { |
256
|
|
|
return $this->plugin_id . $this->id . '_' . $key; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Generate Settings HTML. |
261
|
|
|
* |
262
|
|
|
* Generate the HTML for the fields on the "settings" screen. |
263
|
|
|
* |
264
|
|
|
* @param array $form_fields (default: array()) |
265
|
|
|
* @since 1.0.0 |
266
|
|
|
* @uses method_exists() |
267
|
|
|
* @return string the html for the settings |
268
|
|
|
*/ |
269
|
|
|
public function generate_settings_html( $form_fields = array(), $echo = true ) { |
270
|
|
|
if ( empty( $form_fields ) ) { |
271
|
|
|
$form_fields = $this->get_form_fields(); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
$html = ''; |
275
|
|
|
foreach ( $form_fields as $k => $v ) { |
276
|
|
|
$type = $this->get_field_type( $v ); |
277
|
|
|
|
278
|
|
|
if ( method_exists( $this, 'generate_' . $type . '_html' ) ) { |
279
|
|
|
$html .= $this->{'generate_' . $type . '_html'}( $k, $v ); |
280
|
|
|
} else { |
281
|
|
|
$html .= $this->generate_text_html( $k, $v ); |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
if ( $echo ) { |
286
|
|
|
echo $html; |
287
|
|
|
} else { |
288
|
|
|
return $html; |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Get HTML for tooltips. |
294
|
|
|
* |
295
|
|
|
* @param array $data |
296
|
|
|
* @return string |
297
|
|
|
*/ |
298
|
|
|
public function get_tooltip_html( $data ) { |
299
|
|
|
if ( $data['desc_tip'] === true ) { |
300
|
|
|
$tip = $data['description']; |
301
|
|
|
} elseif ( ! empty( $data['desc_tip'] ) ) { |
302
|
|
|
$tip = $data['desc_tip']; |
303
|
|
|
} else { |
304
|
|
|
$tip = ''; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
return $tip ? wc_help_tip( $tip, true ) : ''; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Get HTML for descriptions. |
312
|
|
|
* |
313
|
|
|
* @param array $data |
314
|
|
|
* @return string |
315
|
|
|
*/ |
316
|
|
|
public function get_description_html( $data ) { |
317
|
|
View Code Duplication |
if ( $data['desc_tip'] === true ) { |
|
|
|
|
318
|
|
|
$description = ''; |
319
|
|
|
} elseif ( ! empty( $data['desc_tip'] ) ) { |
320
|
|
|
$description = $data['description']; |
321
|
|
|
} elseif ( ! empty( $data['description'] ) ) { |
322
|
|
|
$description = $data['description']; |
323
|
|
|
} else { |
324
|
|
|
$description = ''; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
return $description ? '<p class="description">' . wp_kses_post( $description ) . '</p>' . "\n" : ''; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Get custom attributes. |
332
|
|
|
* |
333
|
|
|
* @param array $data |
334
|
|
|
* @return string |
335
|
|
|
*/ |
336
|
|
|
public function get_custom_attribute_html( $data ) { |
337
|
|
|
$custom_attributes = array(); |
338
|
|
|
|
339
|
|
View Code Duplication |
if ( ! empty( $data['custom_attributes'] ) && is_array( $data['custom_attributes'] ) ) { |
|
|
|
|
340
|
|
|
foreach ( $data['custom_attributes'] as $attribute => $attribute_value ) { |
341
|
|
|
$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"'; |
342
|
|
|
} |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
return implode( ' ', $custom_attributes ); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Generate Text Input HTML. |
350
|
|
|
* |
351
|
|
|
* @param mixed $key |
352
|
|
|
* @param mixed $data |
353
|
|
|
* @since 1.0.0 |
354
|
|
|
* @return string |
355
|
|
|
*/ |
356
|
|
View Code Duplication |
public function generate_text_html( $key, $data ) { |
|
|
|
|
357
|
|
|
$field_key = $this->get_field_key( $key ); |
358
|
|
|
$defaults = array( |
359
|
|
|
'title' => '', |
360
|
|
|
'disabled' => false, |
361
|
|
|
'class' => '', |
362
|
|
|
'css' => '', |
363
|
|
|
'placeholder' => '', |
364
|
|
|
'type' => 'text', |
365
|
|
|
'desc_tip' => false, |
366
|
|
|
'description' => '', |
367
|
|
|
'custom_attributes' => array(), |
368
|
|
|
); |
369
|
|
|
|
370
|
|
|
$data = wp_parse_args( $data, $defaults ); |
371
|
|
|
|
372
|
|
|
ob_start(); |
373
|
|
|
?> |
374
|
|
|
<tr valign="top"> |
375
|
|
|
<th scope="row" class="titledesc"> |
376
|
|
|
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> |
377
|
|
|
<?php echo $this->get_tooltip_html( $data ); ?> |
378
|
|
|
</th> |
379
|
|
|
<td class="forminp"> |
380
|
|
|
<fieldset> |
381
|
|
|
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> |
382
|
|
|
<input class="input-text regular-input <?php echo esc_attr( $data['class'] ); ?>" type="<?php echo esc_attr( $data['type'] ); ?>" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="<?php echo esc_attr( $this->get_option( $key ) ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?> /> |
383
|
|
|
<?php echo $this->get_description_html( $data ); ?> |
384
|
|
|
</fieldset> |
385
|
|
|
</td> |
386
|
|
|
</tr> |
387
|
|
|
<?php |
388
|
|
|
|
389
|
|
|
return ob_get_clean(); |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* Generate Price Input HTML. |
394
|
|
|
* |
395
|
|
|
* @param mixed $key |
396
|
|
|
* @param mixed $data |
397
|
|
|
* @since 1.0.0 |
398
|
|
|
* @return string |
399
|
|
|
*/ |
400
|
|
View Code Duplication |
public function generate_price_html( $key, $data ) { |
|
|
|
|
401
|
|
|
$field_key = $this->get_field_key( $key ); |
402
|
|
|
$defaults = array( |
403
|
|
|
'title' => '', |
404
|
|
|
'disabled' => false, |
405
|
|
|
'class' => '', |
406
|
|
|
'css' => '', |
407
|
|
|
'placeholder' => '', |
408
|
|
|
'type' => 'text', |
409
|
|
|
'desc_tip' => false, |
410
|
|
|
'description' => '', |
411
|
|
|
'custom_attributes' => array(), |
412
|
|
|
); |
413
|
|
|
|
414
|
|
|
$data = wp_parse_args( $data, $defaults ); |
415
|
|
|
|
416
|
|
|
ob_start(); |
417
|
|
|
?> |
418
|
|
|
<tr valign="top"> |
419
|
|
|
<th scope="row" class="titledesc"> |
420
|
|
|
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> |
421
|
|
|
<?php echo $this->get_tooltip_html( $data ); ?> |
422
|
|
|
</th> |
423
|
|
|
<td class="forminp"> |
424
|
|
|
<fieldset> |
425
|
|
|
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> |
426
|
|
|
<input class="wc_input_price input-text regular-input <?php echo esc_attr( $data['class'] ); ?>" type="text" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="<?php echo esc_attr( wc_format_localized_price( $this->get_option( $key ) ) ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?> /> |
427
|
|
|
<?php echo $this->get_description_html( $data ); ?> |
428
|
|
|
</fieldset> |
429
|
|
|
</td> |
430
|
|
|
</tr> |
431
|
|
|
<?php |
432
|
|
|
|
433
|
|
|
return ob_get_clean(); |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* Generate Decimal Input HTML. |
438
|
|
|
* |
439
|
|
|
* @param mixed $key |
440
|
|
|
* @param mixed $data |
441
|
|
|
* @since 1.0.0 |
442
|
|
|
* @return string |
443
|
|
|
*/ |
444
|
|
View Code Duplication |
public function generate_decimal_html( $key, $data ) { |
|
|
|
|
445
|
|
|
$field_key = $this->get_field_key( $key ); |
446
|
|
|
$defaults = array( |
447
|
|
|
'title' => '', |
448
|
|
|
'disabled' => false, |
449
|
|
|
'class' => '', |
450
|
|
|
'css' => '', |
451
|
|
|
'placeholder' => '', |
452
|
|
|
'type' => 'text', |
453
|
|
|
'desc_tip' => false, |
454
|
|
|
'description' => '', |
455
|
|
|
'custom_attributes' => array(), |
456
|
|
|
); |
457
|
|
|
|
458
|
|
|
$data = wp_parse_args( $data, $defaults ); |
459
|
|
|
|
460
|
|
|
ob_start(); |
461
|
|
|
?> |
462
|
|
|
<tr valign="top"> |
463
|
|
|
<th scope="row" class="titledesc"> |
464
|
|
|
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> |
465
|
|
|
<?php echo $this->get_tooltip_html( $data ); ?> |
466
|
|
|
</th> |
467
|
|
|
<td class="forminp"> |
468
|
|
|
<fieldset> |
469
|
|
|
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> |
470
|
|
|
<input class="wc_input_decimal input-text regular-input <?php echo esc_attr( $data['class'] ); ?>" type="text" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="<?php echo esc_attr( wc_format_localized_decimal( $this->get_option( $key ) ) ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?> /> |
471
|
|
|
<?php echo $this->get_description_html( $data ); ?> |
472
|
|
|
</fieldset> |
473
|
|
|
</td> |
474
|
|
|
</tr> |
475
|
|
|
<?php |
476
|
|
|
|
477
|
|
|
return ob_get_clean(); |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* Generate Password Input HTML. |
482
|
|
|
* |
483
|
|
|
* @param mixed $key |
484
|
|
|
* @param mixed $data |
485
|
|
|
* @since 1.0.0 |
486
|
|
|
* @return string |
487
|
|
|
*/ |
488
|
|
|
public function generate_password_html( $key, $data ) { |
489
|
|
|
$data['type'] = 'password'; |
490
|
|
|
return $this->generate_text_html( $key, $data ); |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
/** |
494
|
|
|
* Generate Color Picker Input HTML. |
495
|
|
|
* |
496
|
|
|
* @param mixed $key |
497
|
|
|
* @param mixed $data |
498
|
|
|
* @since 1.0.0 |
499
|
|
|
* @return string |
500
|
|
|
*/ |
501
|
|
|
public function generate_color_html( $key, $data ) { |
502
|
|
|
$field_key = $this->get_field_key( $key ); |
503
|
|
|
$defaults = array( |
504
|
|
|
'title' => '', |
505
|
|
|
'disabled' => false, |
506
|
|
|
'class' => '', |
507
|
|
|
'css' => '', |
508
|
|
|
'placeholder' => '', |
509
|
|
|
'desc_tip' => false, |
510
|
|
|
'description' => '', |
511
|
|
|
'custom_attributes' => array(), |
512
|
|
|
); |
513
|
|
|
|
514
|
|
|
$data = wp_parse_args( $data, $defaults ); |
515
|
|
|
|
516
|
|
|
ob_start(); |
517
|
|
|
?> |
518
|
|
|
<tr valign="top"> |
519
|
|
|
<th scope="row" class="titledesc"> |
520
|
|
|
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> |
521
|
|
|
<?php echo $this->get_tooltip_html( $data ); ?> |
522
|
|
|
</th> |
523
|
|
|
<td class="forminp"> |
524
|
|
|
<fieldset> |
525
|
|
|
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> |
526
|
|
|
<span class="colorpickpreview" style="background:<?php echo esc_attr( $this->get_option( $key ) ); ?>;"></span> |
527
|
|
|
<input class="colorpick <?php echo esc_attr( $data['class'] ); ?>" type="text" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="<?php echo esc_attr( $this->get_option( $key ) ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?> /> |
528
|
|
|
<div id="colorPickerDiv_<?php echo esc_attr( $field_key ); ?>" class="colorpickdiv" style="z-index: 100; background: #eee; border: 1px solid #ccc; position: absolute; display: none;"></div> |
529
|
|
|
<?php echo $this->get_description_html( $data ); ?> |
530
|
|
|
</fieldset> |
531
|
|
|
</td> |
532
|
|
|
</tr> |
533
|
|
|
<?php |
534
|
|
|
|
535
|
|
|
return ob_get_clean(); |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
/** |
539
|
|
|
* Generate Textarea HTML. |
540
|
|
|
* |
541
|
|
|
* @param mixed $key |
542
|
|
|
* @param mixed $data |
543
|
|
|
* @since 1.0.0 |
544
|
|
|
* @return string |
545
|
|
|
*/ |
546
|
|
View Code Duplication |
public function generate_textarea_html( $key, $data ) { |
|
|
|
|
547
|
|
|
$field_key = $this->get_field_key( $key ); |
548
|
|
|
$defaults = array( |
549
|
|
|
'title' => '', |
550
|
|
|
'disabled' => false, |
551
|
|
|
'class' => '', |
552
|
|
|
'css' => '', |
553
|
|
|
'placeholder' => '', |
554
|
|
|
'type' => 'text', |
555
|
|
|
'desc_tip' => false, |
556
|
|
|
'description' => '', |
557
|
|
|
'custom_attributes' => array(), |
558
|
|
|
); |
559
|
|
|
|
560
|
|
|
$data = wp_parse_args( $data, $defaults ); |
561
|
|
|
|
562
|
|
|
ob_start(); |
563
|
|
|
?> |
564
|
|
|
<tr valign="top"> |
565
|
|
|
<th scope="row" class="titledesc"> |
566
|
|
|
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> |
567
|
|
|
<?php echo $this->get_tooltip_html( $data ); ?> |
568
|
|
|
</th> |
569
|
|
|
<td class="forminp"> |
570
|
|
|
<fieldset> |
571
|
|
|
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> |
572
|
|
|
<textarea rows="3" cols="20" class="input-text wide-input <?php echo esc_attr( $data['class'] ); ?>" type="<?php echo esc_attr( $data['type'] ); ?>" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" placeholder="<?php echo esc_attr( $data['placeholder'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?>><?php echo esc_textarea( $this->get_option( $key ) ); ?></textarea> |
573
|
|
|
<?php echo $this->get_description_html( $data ); ?> |
574
|
|
|
</fieldset> |
575
|
|
|
</td> |
576
|
|
|
</tr> |
577
|
|
|
<?php |
578
|
|
|
|
579
|
|
|
return ob_get_clean(); |
580
|
|
|
} |
581
|
|
|
|
582
|
|
|
/** |
583
|
|
|
* Generate Checkbox HTML. |
584
|
|
|
* |
585
|
|
|
* @param mixed $key |
586
|
|
|
* @param mixed $data |
587
|
|
|
* @since 1.0.0 |
588
|
|
|
* @return string |
589
|
|
|
*/ |
590
|
|
|
public function generate_checkbox_html( $key, $data ) { |
591
|
|
|
$field_key = $this->get_field_key( $key ); |
592
|
|
|
$defaults = array( |
593
|
|
|
'title' => '', |
594
|
|
|
'label' => '', |
595
|
|
|
'disabled' => false, |
596
|
|
|
'class' => '', |
597
|
|
|
'css' => '', |
598
|
|
|
'type' => 'text', |
599
|
|
|
'desc_tip' => false, |
600
|
|
|
'description' => '', |
601
|
|
|
'custom_attributes' => array(), |
602
|
|
|
); |
603
|
|
|
|
604
|
|
|
$data = wp_parse_args( $data, $defaults ); |
605
|
|
|
|
606
|
|
|
if ( ! $data['label'] ) { |
607
|
|
|
$data['label'] = $data['title']; |
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
ob_start(); |
611
|
|
|
?> |
612
|
|
|
<tr valign="top"> |
613
|
|
|
<th scope="row" class="titledesc"> |
614
|
|
|
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> |
615
|
|
|
<?php echo $this->get_tooltip_html( $data ); ?> |
616
|
|
|
</th> |
617
|
|
|
<td class="forminp"> |
618
|
|
|
<fieldset> |
619
|
|
|
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> |
620
|
|
|
<label for="<?php echo esc_attr( $field_key ); ?>"> |
621
|
|
|
<input <?php disabled( $data['disabled'], true ); ?> class="<?php echo esc_attr( $data['class'] ); ?>" type="checkbox" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" value="1" <?php checked( $this->get_option( $key ), 'yes' ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?> /> <?php echo wp_kses_post( $data['label'] ); ?></label><br/> |
622
|
|
|
<?php echo $this->get_description_html( $data ); ?> |
623
|
|
|
</fieldset> |
624
|
|
|
</td> |
625
|
|
|
</tr> |
626
|
|
|
<?php |
627
|
|
|
|
628
|
|
|
return ob_get_clean(); |
629
|
|
|
} |
630
|
|
|
|
631
|
|
|
/** |
632
|
|
|
* Generate Select HTML. |
633
|
|
|
* |
634
|
|
|
* @param mixed $key |
635
|
|
|
* @param mixed $data |
636
|
|
|
* @since 1.0.0 |
637
|
|
|
* @return string |
638
|
|
|
*/ |
639
|
|
|
public function generate_select_html( $key, $data ) { |
640
|
|
|
$field_key = $this->get_field_key( $key ); |
641
|
|
|
$defaults = array( |
642
|
|
|
'title' => '', |
643
|
|
|
'disabled' => false, |
644
|
|
|
'class' => '', |
645
|
|
|
'css' => '', |
646
|
|
|
'placeholder' => '', |
647
|
|
|
'type' => 'text', |
648
|
|
|
'desc_tip' => false, |
649
|
|
|
'description' => '', |
650
|
|
|
'custom_attributes' => array(), |
651
|
|
|
'options' => array(), |
652
|
|
|
); |
653
|
|
|
|
654
|
|
|
$data = wp_parse_args( $data, $defaults ); |
655
|
|
|
|
656
|
|
|
ob_start(); |
657
|
|
|
?> |
658
|
|
|
<tr valign="top"> |
659
|
|
|
<th scope="row" class="titledesc"> |
660
|
|
|
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> |
661
|
|
|
<?php echo $this->get_tooltip_html( $data ); ?> |
662
|
|
|
</th> |
663
|
|
|
<td class="forminp"> |
664
|
|
|
<fieldset> |
665
|
|
|
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> |
666
|
|
|
<select class="select <?php echo esc_attr( $data['class'] ); ?>" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?>> |
667
|
|
|
<?php foreach ( (array) $data['options'] as $option_key => $option_value ) : ?> |
668
|
|
|
<option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $option_key, esc_attr( $this->get_option( $key ) ) ); ?>><?php echo esc_attr( $option_value ); ?></option> |
669
|
|
|
<?php endforeach; ?> |
670
|
|
|
</select> |
671
|
|
|
<?php echo $this->get_description_html( $data ); ?> |
672
|
|
|
</fieldset> |
673
|
|
|
</td> |
674
|
|
|
</tr> |
675
|
|
|
<?php |
676
|
|
|
|
677
|
|
|
return ob_get_clean(); |
678
|
|
|
} |
679
|
|
|
|
680
|
|
|
/** |
681
|
|
|
* Generate Multiselect HTML. |
682
|
|
|
* |
683
|
|
|
* @param mixed $key |
684
|
|
|
* @param mixed $data |
685
|
|
|
* @since 1.0.0 |
686
|
|
|
* @return string |
687
|
|
|
*/ |
688
|
|
|
public function generate_multiselect_html( $key, $data ) { |
689
|
|
|
$field_key = $this->get_field_key( $key ); |
690
|
|
|
$defaults = array( |
691
|
|
|
'title' => '', |
692
|
|
|
'disabled' => false, |
693
|
|
|
'class' => '', |
694
|
|
|
'css' => '', |
695
|
|
|
'placeholder' => '', |
696
|
|
|
'type' => 'text', |
697
|
|
|
'desc_tip' => false, |
698
|
|
|
'description' => '', |
699
|
|
|
'custom_attributes' => array(), |
700
|
|
|
'select_buttons' => false, |
701
|
|
|
'options' => array(), |
702
|
|
|
); |
703
|
|
|
|
704
|
|
|
$data = wp_parse_args( $data, $defaults ); |
705
|
|
|
$value = (array) $this->get_option( $key, array() ); |
706
|
|
|
|
707
|
|
|
ob_start(); |
708
|
|
|
?> |
709
|
|
|
<tr valign="top"> |
710
|
|
|
<th scope="row" class="titledesc"> |
711
|
|
|
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label> |
712
|
|
|
<?php echo $this->get_tooltip_html( $data ); ?> |
713
|
|
|
</th> |
714
|
|
|
<td class="forminp"> |
715
|
|
|
<fieldset> |
716
|
|
|
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend> |
717
|
|
|
<select multiple="multiple" class="multiselect <?php echo esc_attr( $data['class'] ); ?>" name="<?php echo esc_attr( $field_key ); ?>[]" id="<?php echo esc_attr( $field_key ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?>> |
718
|
|
|
<?php foreach ( (array) $data['options'] as $option_key => $option_value ) : ?> |
719
|
|
|
<option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( in_array( $option_key, $value ), true ); ?>><?php echo esc_attr( $option_value ); ?></option> |
720
|
|
|
<?php endforeach; ?> |
721
|
|
|
</select> |
722
|
|
|
<?php echo $this->get_description_html( $data ); ?> |
723
|
|
|
<?php if ( $data['select_buttons'] ) : ?> |
724
|
|
|
<br/><a class="select_all button" href="#"><?php _e( 'Select all', 'woocommerce' ); ?></a> <a class="select_none button" href="#"><?php _e( 'Select none', 'woocommerce' ); ?></a> |
725
|
|
|
<?php endif; ?> |
726
|
|
|
</fieldset> |
727
|
|
|
</td> |
728
|
|
|
</tr> |
729
|
|
|
<?php |
730
|
|
|
|
731
|
|
|
return ob_get_clean(); |
732
|
|
|
} |
733
|
|
|
|
734
|
|
|
/** |
735
|
|
|
* Generate Title HTML. |
736
|
|
|
* |
737
|
|
|
* @param mixed $key |
738
|
|
|
* @param mixed $data |
739
|
|
|
* @since 1.0.0 |
740
|
|
|
* @return string |
741
|
|
|
*/ |
742
|
|
|
public function generate_title_html( $key, $data ) { |
743
|
|
|
$field_key = $this->get_field_key( $key ); |
744
|
|
|
$defaults = array( |
745
|
|
|
'title' => '', |
746
|
|
|
'class' => '', |
747
|
|
|
); |
748
|
|
|
|
749
|
|
|
$data = wp_parse_args( $data, $defaults ); |
750
|
|
|
|
751
|
|
|
ob_start(); |
752
|
|
|
?> |
753
|
|
|
</table> |
754
|
|
|
<h3 class="wc-settings-sub-title <?php echo esc_attr( $data['class'] ); ?>" id="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></h3> |
755
|
|
|
<?php if ( ! empty( $data['description'] ) ) : ?> |
756
|
|
|
<p><?php echo wp_kses_post( $data['description'] ); ?></p> |
757
|
|
|
<?php endif; ?> |
758
|
|
|
<table class="form-table"> |
759
|
|
|
<?php |
760
|
|
|
|
761
|
|
|
return ob_get_clean(); |
762
|
|
|
} |
763
|
|
|
|
764
|
|
|
/** |
765
|
|
|
* Validate Text Field. |
766
|
|
|
* |
767
|
|
|
* Make sure the data is escaped correctly, etc. |
768
|
|
|
* |
769
|
|
|
* @param string $key Field key |
770
|
|
|
* @param string|null $value Posted Value |
771
|
|
|
* @return string |
772
|
|
|
*/ |
773
|
|
|
public function validate_text_field( $key, $value ) { |
|
|
|
|
774
|
|
|
$value = is_null( $value ) ? '' : $value; |
775
|
|
|
return wp_kses_post( trim( stripslashes( $value ) ) ); |
776
|
|
|
} |
777
|
|
|
|
778
|
|
|
/** |
779
|
|
|
* Validate Price Field. |
780
|
|
|
* |
781
|
|
|
* Make sure the data is escaped correctly, etc. |
782
|
|
|
* |
783
|
|
|
* @param string $key |
784
|
|
|
* @param string|null $value Posted Value |
785
|
|
|
* @return string |
786
|
|
|
*/ |
787
|
|
|
public function validate_price_field( $key, $value ) { |
|
|
|
|
788
|
|
|
$value = is_null( $value ) ? '' : $value; |
789
|
|
|
return $value === '' ? '' : wc_format_decimal( trim( stripslashes( $value ) ) ); |
790
|
|
|
} |
791
|
|
|
|
792
|
|
|
/** |
793
|
|
|
* Validate Decimal Field. |
794
|
|
|
* |
795
|
|
|
* Make sure the data is escaped correctly, etc. |
796
|
|
|
* |
797
|
|
|
* @param string $key |
798
|
|
|
* @param string|null $value Posted Value |
799
|
|
|
* @return string |
800
|
|
|
*/ |
801
|
|
|
public function validate_decimal_field( $key, $value ) { |
|
|
|
|
802
|
|
|
$value = is_null( $value ) ? '' : $value; |
803
|
|
|
return $value === '' ? '' : wc_format_decimal( trim( stripslashes( $value ) ) ); |
804
|
|
|
} |
805
|
|
|
|
806
|
|
|
/** |
807
|
|
|
* Validate Password Field. |
808
|
|
|
* |
809
|
|
|
* Make sure the data is escaped correctly, etc. |
810
|
|
|
* |
811
|
|
|
* @param string $key |
812
|
|
|
* @param string|null $value Posted Value |
813
|
|
|
* @return string |
814
|
|
|
*/ |
815
|
|
|
public function validate_password_field( $key, $value ) { |
|
|
|
|
816
|
|
|
$value = is_null( $value ) ? '' : $value; |
817
|
|
|
return wp_kses_post( trim( stripslashes( $value ) ) ); |
818
|
|
|
} |
819
|
|
|
|
820
|
|
|
/** |
821
|
|
|
* Validate Textarea Field. |
822
|
|
|
* |
823
|
|
|
* @param string $key |
824
|
|
|
* @param string|null $value Posted Value |
825
|
|
|
* @return string |
826
|
|
|
*/ |
827
|
|
|
public function validate_textarea_field( $key, $value ) { |
|
|
|
|
828
|
|
|
$value = is_null( $value ) ? '' : $value; |
829
|
|
|
return wp_kses( trim( stripslashes( $value ) ), |
830
|
|
|
array_merge( |
831
|
|
|
array( |
832
|
|
|
'iframe' => array( 'src' => true, 'style' => true, 'id' => true, 'class' => true ) |
833
|
|
|
), |
834
|
|
|
wp_kses_allowed_html( 'post' ) |
835
|
|
|
) |
836
|
|
|
); |
837
|
|
|
} |
838
|
|
|
|
839
|
|
|
/** |
840
|
|
|
* Validate Checkbox Field. |
841
|
|
|
* |
842
|
|
|
* If not set, return "no", otherwise return "yes". |
843
|
|
|
* |
844
|
|
|
* @param string $key |
845
|
|
|
* @param string|null $value Posted Value |
846
|
|
|
* @return string |
847
|
|
|
*/ |
848
|
|
|
public function validate_checkbox_field( $key, $value ) { |
|
|
|
|
849
|
|
|
return ! is_null( $value ) ? 'yes' : 'no'; |
850
|
|
|
} |
851
|
|
|
|
852
|
|
|
/** |
853
|
|
|
* Validate Select Field. |
854
|
|
|
* |
855
|
|
|
* @param string $key |
856
|
|
|
* @param string $value Posted Value |
857
|
|
|
* @return string |
858
|
|
|
*/ |
859
|
|
|
public function validate_select_field( $key, $value ) { |
|
|
|
|
860
|
|
|
$value = is_null( $value ) ? '' : $value; |
861
|
|
|
return wc_clean( stripslashes( $value ) ); |
862
|
|
|
} |
863
|
|
|
|
864
|
|
|
/** |
865
|
|
|
* Validate Multiselect Field. |
866
|
|
|
* |
867
|
|
|
* @param string $key |
868
|
|
|
* @param string $value Posted Value |
869
|
|
|
* @return string |
870
|
|
|
*/ |
871
|
|
|
public function validate_multiselect_field( $key, $value ) { |
|
|
|
|
872
|
|
|
return is_array( $value ) ? array_map( 'wc_clean', array_map( 'stripslashes', $value ) ) : ''; |
873
|
|
|
} |
874
|
|
|
|
875
|
|
|
/** |
876
|
|
|
* Validate the data on the "Settings" form. |
877
|
|
|
* @deprecated 2.6.0 No longer used |
878
|
|
|
*/ |
879
|
|
|
public function validate_settings_fields( $form_fields = array() ) { |
|
|
|
|
880
|
|
|
_deprecated_function( 'validate_settings_fields', '2.6' ); |
881
|
|
|
} |
882
|
|
|
|
883
|
|
|
/** |
884
|
|
|
* Format settings if needed. |
885
|
|
|
* @deprecated 2.6.0 Unused |
886
|
|
|
* @param array $value |
887
|
|
|
* @return array |
888
|
|
|
*/ |
889
|
|
|
public function format_settings( $value ) { |
890
|
|
|
_deprecated_function( 'format_settings', '2.6' ); |
891
|
|
|
return $value; |
892
|
|
|
} |
893
|
|
|
} |
894
|
|
|
|
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.