Completed
Push — master ( 9252c3...f95ffe )
by Mike
08:03
created

WC_Settings_API::validate_multiselect_field()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 2
eloc 2
nc 2
nop 2
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
	 * Get the form fields after they are initialized.
47
	 * @return array of options
48
	 */
49
	public function get_form_fields() {
50
		return apply_filters( 'woocommerce_settings_api_form_fields_' . $this->id, array_map( array( $this, 'set_defaults' ), $this->form_fields ) );
51
	}
52
53
	/**
54
	 * Set default required properties for each field.
55
	 * @param array
56
	 */
57
	private function set_defaults( $field ) {
58
		if ( ! isset( $field['default'] ) ) {
59
			$field['default'] = '';
60
		}
61
		return $field;
62
	}
63
64
	/**
65
	 * Output the admin options table.
66
	 */
67
	public function admin_options() {
68
		echo '<table class="form-table">' . $this->generate_settings_html( $this->get_form_fields(), false ) . '</table>';
69
	}
70
71
	/**
72
	 * Return the name of the option in the WP DB.
73
	 * @since 2.6.0
74
	 * @return string
75
	 */
76
	public function get_option_key() {
77
		return $this->plugin_id . $this->id . '_settings';
78
	}
79
80
	/**
81
	 * Get a fields type. Defaults to "text" if not set.
82
	 * @param  array $field
83
	 * @return string
84
	 */
85
	public function get_field_type( $field ) {
86
		return empty( $field['type'] ) ? 'text' : $field['type'];
87
	}
88
89
	/**
90
	 * Get a fields default value. Defaults to "" if not set.
91
	 * @param  array $field
92
	 * @return string
93
	 */
94
	public function get_field_default( $field ) {
95
		return empty( $field['default'] ) ? '' : $field['default'];
96
	}
97
98
	/**
99
	 * Get a field's posted and validated value.
100
	 * @return string
101
	 */
102
	public function get_field_value( $key, $field ) {
103
		$type      = $this->get_field_type( $field );
104
		$field_key = $this->get_field_key( $key );
105
		$value     = isset( $_POST[ $field_key ] ) ? $_POST[ $field_key ] : null;
106
107
		// Look for a validate_FIELDID_field method for special handling
108 View Code Duplication
		if ( is_callable( array( $this, 'validate_' . $key . '_field' ) ) ) {
1 ignored issue
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...
109
			return $this->{'validate_' . $key . '_field'}( $key, $value );
110
		}
111
112
		// Look for a validate_FIELDTYPE_field method
113 View Code Duplication
		if ( is_callable( array( $this, 'validate_' . $type . '_field' ) ) ) {
1 ignored issue
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...
114
			return $this->{'validate_' . $type . '_field'}( $key, $value );
115
		}
116
117
		// Fallback to text
118
		return $this->validate_text_field( $key, $value );
119
	}
120
121
	/**
122
	 * Processes and saves options.
123
	 * If there is an error thrown, will continue to save and validate fields, but will leave the erroring field out.
124
	 * @return bool was anything saved?
125
	 */
126
	public function process_admin_options() {
127
		$this->init_settings();
128
129 View Code Duplication
		foreach ( $this->get_form_fields() as $key => $field ) {
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...
130
			if ( 'title' !== $this->get_field_type( $field ) ) {
131
				try {
132
					$this->settings[ $key ] = $this->get_field_value( $key, $field );
133
				} catch ( Exception $e ) {
134
					$this->add_error( $e->getMessage() );
135
				}
136
			}
137
		}
138
139
		return update_option( $this->get_option_key(), apply_filters( 'woocommerce_settings_api_sanitized_fields_' . $this->id, $this->settings ) );
140
	}
141
142
	/**
143
	 * Add an error message for display in admin on save.
144
	 * @param string $error
145
	 */
146
	public function add_error( $error ) {
147
		$this->errors[] = $error;
148
	}
149
150
	/**
151
	 * Display admin error messages.
152
	 */
153
	public function display_errors() {
154
		if ( count( $this->errors ) > 0 ) {
155
			echo '<div id="woocommerce_errors" class="error notice is-dismissible">';
156
			foreach ( $this->errors as $error ) {
157
				echo '<p>' . wp_kses_post( $error ) . '</p>';
158
			}
159
			echo '</div>';
160
		}
161
	}
162
163
	/**
164
	 * Initialise Settings.
165
	 *
166
	 * Store all settings in a single database entry
167
	 * and make sure the $settings array is either the default
168
	 * or the settings stored in the database.
169
	 *
170
	 * @since 1.0.0
171
	 * @uses get_option(), add_option()
172
	 */
173 View Code Duplication
	public function init_settings() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
174
		$this->settings = get_option( $this->get_option_key(), null );
175
176
		// If there are no settings defined, use defaults.
177
		if ( ! is_array( $this->settings ) ) {
178
			$form_fields    = $this->get_form_fields();
179
			$this->settings = array_merge( array_fill_keys( array_keys( $form_fields ), '' ), wp_list_pluck( $form_fields, 'default' ) );
180
		}
181
	}
182
183
	/**
184
	 * get_option function.
185
	 *
186
	 * Gets an option from the settings API, using defaults if necessary to prevent undefined notices.
187
	 *
188
	 * @param  string $key
189
	 * @param  mixed  $empty_value
190
	 * @return mixed  The value specified for the option or a default value for the option.
191
	 */
192 View Code Duplication
	public function get_option( $key, $empty_value = null ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
193
		if ( empty( $this->settings ) ) {
194
			$this->init_settings();
195
		}
196
197
		// Get option default if unset.
198
		if ( ! isset( $this->settings[ $key ] ) ) {
199
			$form_fields            = $this->get_form_fields();
200
			$this->settings[ $key ] = isset( $form_fields[ $key ] ) ? $this->get_field_default( $form_fields[ $key ] ) : '';
201
		}
202
203
		if ( ! is_null( $empty_value ) && '' === $this->settings[ $key ] ) {
204
			$this->settings[ $key ] = $empty_value;
205
		}
206
207
		return $this->settings[ $key ];
208
	}
209
210
	/**
211
	 * Prefix key for settings.
212
	 *
213
	 * @param  mixed $key
214
	 * @return string
215
	 */
216
	public function get_field_key( $key ) {
217
		return $this->plugin_id . $this->id . '_' . $key;
218
	}
219
220
	/**
221
	 * Generate Settings HTML.
222
	 *
223
	 * Generate the HTML for the fields on the "settings" screen.
224
	 *
225
	 * @param  array $form_fields (default: array())
226
	 * @since  1.0.0
227
	 * @uses   method_exists()
228
	 * @return string the html for the settings
229
	 */
230
	public function generate_settings_html( $form_fields = array(), $echo = true ) {
231
		if ( empty( $form_fields ) ) {
232
			$form_fields = $this->get_form_fields();
233
		}
234
235
		$html = '';
236
		foreach ( $form_fields as $k => $v ) {
237
			$type = $this->get_field_type( $v );
238
239
			if ( method_exists( $this, 'generate_' . $type . '_html' ) ) {
240
				$html .= $this->{'generate_' . $type . '_html'}( $k, $v );
241
			} else {
242
				$html .= $this->generate_text_html( $k, $v );
243
			}
244
		}
245
246
		if ( $echo ) {
247
			echo $html;
248
		} else {
249
			return $html;
250
		}
251
	}
252
253
	/**
254
	 * Get HTML for tooltips.
255
	 *
256
	 * @param  array $data
257
	 * @return string
258
	 */
259
	public function get_tooltip_html( $data ) {
260
		if ( $data['desc_tip'] === true ) {
261
			$tip = $data['description'];
262
		} elseif ( ! empty( $data['desc_tip'] ) ) {
263
			$tip = $data['desc_tip'];
264
		} else {
265
			$tip = '';
266
		}
267
268
		return $tip ? wc_help_tip( $tip, true ) : '';
269
	}
270
271
	/**
272
	 * Get HTML for descriptions.
273
	 *
274
	 * @param  array $data
275
	 * @return string
276
	 */
277
	public function get_description_html( $data ) {
278 View Code Duplication
		if ( $data['desc_tip'] === true ) {
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...
279
			$description = '';
280
		} elseif ( ! empty( $data['desc_tip'] ) ) {
281
			$description = $data['description'];
282
		} elseif ( ! empty( $data['description'] ) ) {
283
			$description = $data['description'];
284
		} else {
285
			$description = '';
286
		}
287
288
		return $description ? '<p class="description">' . wp_kses_post( $description ) . '</p>' . "\n" : '';
289
	}
290
291
	/**
292
	 * Get custom attributes.
293
	 *
294
	 * @param  array $data
295
	 * @return string
296
	 */
297
	public function get_custom_attribute_html( $data ) {
298
		$custom_attributes = array();
299
300 View Code Duplication
		if ( ! empty( $data['custom_attributes'] ) && is_array( $data['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...
301
			foreach ( $data['custom_attributes'] as $attribute => $attribute_value ) {
302
				$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
303
			}
304
		}
305
306
		return implode( ' ', $custom_attributes );
307
	}
308
309
	/**
310
	 * Generate Text Input HTML.
311
	 *
312
	 * @param  mixed $key
313
	 * @param  mixed $data
314
	 * @since  1.0.0
315
	 * @return string
316
	 */
317 View Code Duplication
	public function generate_text_html( $key, $data ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
318
		$field_key = $this->get_field_key( $key );
319
		$defaults  = array(
320
			'title'             => '',
321
			'disabled'          => false,
322
			'class'             => '',
323
			'css'               => '',
324
			'placeholder'       => '',
325
			'type'              => 'text',
326
			'desc_tip'          => false,
327
			'description'       => '',
328
			'custom_attributes' => array(),
329
		);
330
331
		$data = wp_parse_args( $data, $defaults );
332
333
		ob_start();
334
		?>
335
		<tr valign="top">
336
			<th scope="row" class="titledesc">
337
				<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
338
				<?php echo $this->get_tooltip_html( $data ); ?>
339
			</th>
340
			<td class="forminp">
341
				<fieldset>
342
					<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
343
					<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 ); ?> />
344
					<?php echo $this->get_description_html( $data ); ?>
345
				</fieldset>
346
			</td>
347
		</tr>
348
		<?php
349
350
		return ob_get_clean();
351
	}
352
353
	/**
354
	 * Generate Price Input HTML.
355
	 *
356
	 * @param  mixed $key
357
	 * @param  mixed $data
358
	 * @since  1.0.0
359
	 * @return string
360
	 */
361 View Code Duplication
	public function generate_price_html( $key, $data ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
362
		$field_key = $this->get_field_key( $key );
363
		$defaults  = array(
364
			'title'             => '',
365
			'disabled'          => false,
366
			'class'             => '',
367
			'css'               => '',
368
			'placeholder'       => '',
369
			'type'              => 'text',
370
			'desc_tip'          => false,
371
			'description'       => '',
372
			'custom_attributes' => array(),
373
		);
374
375
		$data = wp_parse_args( $data, $defaults );
376
377
		ob_start();
378
		?>
379
		<tr valign="top">
380
			<th scope="row" class="titledesc">
381
				<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
382
				<?php echo $this->get_tooltip_html( $data ); ?>
383
			</th>
384
			<td class="forminp">
385
				<fieldset>
386
					<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
387
					<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 ); ?> />
388
					<?php echo $this->get_description_html( $data ); ?>
389
				</fieldset>
390
			</td>
391
		</tr>
392
		<?php
393
394
		return ob_get_clean();
395
	}
396
397
	/**
398
	 * Generate Decimal Input HTML.
399
	 *
400
	 * @param  mixed $key
401
	 * @param  mixed $data
402
	 * @since  1.0.0
403
	 * @return string
404
	 */
405 View Code Duplication
	public function generate_decimal_html( $key, $data ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
406
		$field_key = $this->get_field_key( $key );
407
		$defaults  = array(
408
			'title'             => '',
409
			'disabled'          => false,
410
			'class'             => '',
411
			'css'               => '',
412
			'placeholder'       => '',
413
			'type'              => 'text',
414
			'desc_tip'          => false,
415
			'description'       => '',
416
			'custom_attributes' => array(),
417
		);
418
419
		$data = wp_parse_args( $data, $defaults );
420
421
		ob_start();
422
		?>
423
		<tr valign="top">
424
			<th scope="row" class="titledesc">
425
				<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
426
				<?php echo $this->get_tooltip_html( $data ); ?>
427
			</th>
428
			<td class="forminp">
429
				<fieldset>
430
					<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
431
					<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 ); ?> />
432
					<?php echo $this->get_description_html( $data ); ?>
433
				</fieldset>
434
			</td>
435
		</tr>
436
		<?php
437
438
		return ob_get_clean();
439
	}
440
441
	/**
442
	 * Generate Password Input HTML.
443
	 *
444
	 * @param  mixed $key
445
	 * @param  mixed $data
446
	 * @since  1.0.0
447
	 * @return string
448
	 */
449
	public function generate_password_html( $key, $data ) {
450
		$data['type'] = 'password';
451
		return $this->generate_text_html( $key, $data );
452
	}
453
454
	/**
455
	 * Generate Color Picker Input HTML.
456
	 *
457
	 * @param  mixed $key
458
	 * @param  mixed $data
459
	 * @since  1.0.0
460
	 * @return string
461
	 */
462
	public function generate_color_html( $key, $data ) {
463
		$field_key = $this->get_field_key( $key );
464
		$defaults  = array(
465
			'title'             => '',
466
			'disabled'          => false,
467
			'class'             => '',
468
			'css'               => '',
469
			'placeholder'       => '',
470
			'desc_tip'          => false,
471
			'description'       => '',
472
			'custom_attributes' => array(),
473
		);
474
475
		$data = wp_parse_args( $data, $defaults );
476
477
		ob_start();
478
		?>
479
		<tr valign="top">
480
			<th scope="row" class="titledesc">
481
				<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
482
				<?php echo $this->get_tooltip_html( $data ); ?>
483
			</th>
484
			<td class="forminp">
485
				<fieldset>
486
					<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
487
					<span class="colorpickpreview" style="background:<?php echo esc_attr( $this->get_option( $key ) ); ?>;"></span>
488
					<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 ); ?> />
489
					<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>
490
					<?php echo $this->get_description_html( $data ); ?>
491
				</fieldset>
492
			</td>
493
		</tr>
494
		<?php
495
496
		return ob_get_clean();
497
	}
498
499
	/**
500
	 * Generate Textarea HTML.
501
	 *
502
	 * @param  mixed $key
503
	 * @param  mixed $data
504
	 * @since  1.0.0
505
	 * @return string
506
	 */
507 View Code Duplication
	public function generate_textarea_html( $key, $data ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
508
		$field_key = $this->get_field_key( $key );
509
		$defaults  = array(
510
			'title'             => '',
511
			'disabled'          => false,
512
			'class'             => '',
513
			'css'               => '',
514
			'placeholder'       => '',
515
			'type'              => 'text',
516
			'desc_tip'          => false,
517
			'description'       => '',
518
			'custom_attributes' => array(),
519
		);
520
521
		$data = wp_parse_args( $data, $defaults );
522
523
		ob_start();
524
		?>
525
		<tr valign="top">
526
			<th scope="row" class="titledesc">
527
				<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
528
				<?php echo $this->get_tooltip_html( $data ); ?>
529
			</th>
530
			<td class="forminp">
531
				<fieldset>
532
					<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
533
					<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>
534
					<?php echo $this->get_description_html( $data ); ?>
535
				</fieldset>
536
			</td>
537
		</tr>
538
		<?php
539
540
		return ob_get_clean();
541
	}
542
543
	/**
544
	 * Generate Checkbox HTML.
545
	 *
546
	 * @param  mixed $key
547
	 * @param  mixed $data
548
	 * @since  1.0.0
549
	 * @return string
550
	 */
551
	public function generate_checkbox_html( $key, $data ) {
552
		$field_key = $this->get_field_key( $key );
553
		$defaults  = array(
554
			'title'             => '',
555
			'label'             => '',
556
			'disabled'          => false,
557
			'class'             => '',
558
			'css'               => '',
559
			'type'              => 'text',
560
			'desc_tip'          => false,
561
			'description'       => '',
562
			'custom_attributes' => array(),
563
		);
564
565
		$data = wp_parse_args( $data, $defaults );
566
567
		if ( ! $data['label'] ) {
568
			$data['label'] = $data['title'];
569
		}
570
571
		ob_start();
572
		?>
573
		<tr valign="top">
574
			<th scope="row" class="titledesc">
575
				<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
576
				<?php echo $this->get_tooltip_html( $data ); ?>
577
			</th>
578
			<td class="forminp">
579
				<fieldset>
580
					<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
581
					<label for="<?php echo esc_attr( $field_key ); ?>">
582
					<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/>
583
					<?php echo $this->get_description_html( $data ); ?>
584
				</fieldset>
585
			</td>
586
		</tr>
587
		<?php
588
589
		return ob_get_clean();
590
	}
591
592
	/**
593
	 * Generate Select HTML.
594
	 *
595
	 * @param  mixed $key
596
	 * @param  mixed $data
597
	 * @since  1.0.0
598
	 * @return string
599
	 */
600
	public function generate_select_html( $key, $data ) {
601
		$field_key = $this->get_field_key( $key );
602
		$defaults  = array(
603
			'title'             => '',
604
			'disabled'          => false,
605
			'class'             => '',
606
			'css'               => '',
607
			'placeholder'       => '',
608
			'type'              => 'text',
609
			'desc_tip'          => false,
610
			'description'       => '',
611
			'custom_attributes' => array(),
612
			'options'           => array(),
613
		);
614
615
		$data = wp_parse_args( $data, $defaults );
616
617
		ob_start();
618
		?>
619
		<tr valign="top">
620
			<th scope="row" class="titledesc">
621
				<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
622
				<?php echo $this->get_tooltip_html( $data ); ?>
623
			</th>
624
			<td class="forminp">
625
				<fieldset>
626
					<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
627
					<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 ); ?>>
628
						<?php foreach ( (array) $data['options'] as $option_key => $option_value ) : ?>
629
							<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>
630
						<?php endforeach; ?>
631
					</select>
632
					<?php echo $this->get_description_html( $data ); ?>
633
				</fieldset>
634
			</td>
635
		</tr>
636
		<?php
637
638
		return ob_get_clean();
639
	}
640
641
	/**
642
	 * Generate Multiselect HTML.
643
	 *
644
	 * @param  mixed $key
645
	 * @param  mixed $data
646
	 * @since  1.0.0
647
	 * @return string
648
	 */
649
	public function generate_multiselect_html( $key, $data ) {
650
		$field_key = $this->get_field_key( $key );
651
		$defaults  = array(
652
			'title'             => '',
653
			'disabled'          => false,
654
			'class'             => '',
655
			'css'               => '',
656
			'placeholder'       => '',
657
			'type'              => 'text',
658
			'desc_tip'          => false,
659
			'description'       => '',
660
			'custom_attributes' => array(),
661
			'select_buttons'    => false,
662
			'options'           => array(),
663
		);
664
665
		$data  = wp_parse_args( $data, $defaults );
666
		$value = (array) $this->get_option( $key, array() );
667
668
		ob_start();
669
		?>
670
		<tr valign="top">
671
			<th scope="row" class="titledesc">
672
				<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
673
				<?php echo $this->get_tooltip_html( $data ); ?>
674
			</th>
675
			<td class="forminp">
676
				<fieldset>
677
					<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
678
					<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 ); ?>>
679
						<?php foreach ( (array) $data['options'] as $option_key => $option_value ) : ?>
680
							<option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( in_array( $option_key, $value ), true ); ?>><?php echo esc_attr( $option_value ); ?></option>
681
						<?php endforeach; ?>
682
					</select>
683
					<?php echo $this->get_description_html( $data ); ?>
684
					<?php if ( $data['select_buttons'] ) : ?>
685
						<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>
686
					<?php endif; ?>
687
				</fieldset>
688
			</td>
689
		</tr>
690
		<?php
691
692
		return ob_get_clean();
693
	}
694
695
	/**
696
	 * Generate Title HTML.
697
	 *
698
	 * @param  mixed $key
699
	 * @param  mixed $data
700
	 * @since  1.0.0
701
	 * @return string
702
	 */
703
	public function generate_title_html( $key, $data ) {
704
		$field_key = $this->get_field_key( $key );
705
		$defaults  = array(
706
			'title' => '',
707
			'class' => '',
708
		);
709
710
		$data = wp_parse_args( $data, $defaults );
711
712
		ob_start();
713
		?>
714
			</table>
715
			<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>
716
			<?php if ( ! empty( $data['description'] ) ) : ?>
717
				<p><?php echo wp_kses_post( $data['description'] ); ?></p>
718
			<?php endif; ?>
719
			<table class="form-table">
720
		<?php
721
722
		return ob_get_clean();
723
	}
724
725
	/**
726
	 * Validate Text Field.
727
	 *
728
	 * Make sure the data is escaped correctly, etc.
729
	 *
730
	 * @param  string $key Field key
731
	 * @param  string|null $value Posted Value
732
	 * @return string
733
	 */
734
	public function validate_text_field( $key, $value ) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
735
		$value = is_null( $value ) ? '' : $value;
736
		return wp_kses_post( trim( stripslashes( $value ) ) );
737
	}
738
739
	/**
740
	 * Validate Price Field.
741
	 *
742
	 * Make sure the data is escaped correctly, etc.
743
	 *
744
	 * @param  string $key
745
	 * @param  string|null $value Posted Value
746
	 * @return string
747
	 */
748
	public function validate_price_field( $key, $value ) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
749
		$value = is_null( $value ) ? '' : $value;
750
		return $value === '' ? '' : wc_format_decimal( trim( stripslashes( $value ) ) );
751
	}
752
753
	/**
754
	 * Validate Decimal Field.
755
	 *
756
	 * Make sure the data is escaped correctly, etc.
757
	 *
758
	 * @param  string $key
759
	 * @param  string|null $value Posted Value
760
	 * @return string
761
	 */
762
	public function validate_decimal_field( $key, $value ) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
763
		$value = is_null( $value ) ? '' : $value;
764
		return $value === '' ? '' : wc_format_decimal( trim( stripslashes( $value ) ) );
765
	}
766
767
	/**
768
	 * Validate Password Field.
769
	 *
770
	 * Make sure the data is escaped correctly, etc.
771
	 *
772
	 * @param  string $key
773
	 * @param  string|null $value Posted Value
774
	 * @return string
775
	 */
776
	public function validate_password_field( $key, $value ) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
777
		$value = is_null( $value ) ? '' : $value;
778
		return wp_kses_post( trim( stripslashes( $value ) ) );
779
	}
780
781
	/**
782
	 * Validate Textarea Field.
783
	 *
784
	 * @param  string $key
785
	 * @param  string|null $value Posted Value
786
	 * @return string
787
	 */
788
	public function validate_textarea_field( $key, $value ) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
789
		$value = is_null( $value ) ? '' : $value;
790
		return wp_kses( trim( stripslashes( $value ) ),
791
			array_merge(
792
				array(
793
					'iframe' => array( 'src' => true, 'style' => true, 'id' => true, 'class' => true )
794
				),
795
				wp_kses_allowed_html( 'post' )
796
			)
797
		);
798
	}
799
800
	/**
801
	 * Validate Checkbox Field.
802
	 *
803
	 * If not set, return "no", otherwise return "yes".
804
	 *
805
	 * @param  string $key
806
	 * @param  string|null $value Posted Value
807
	 * @return string
808
	 */
809
	public function validate_checkbox_field( $key, $value ) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
810
		return ! is_null( $value ) ? 'yes' : 'no';
811
	}
812
813
	/**
814
	 * Validate Select Field.
815
	 *
816
	 * @param  string $key
817
	 * @param  string $value Posted Value
818
	 * @return string
819
	 */
820
	public function validate_select_field( $key, $value ) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
821
		$value = is_null( $value ) ? '' : $value;
822
		return wc_clean( stripslashes( $value ) );
823
	}
824
825
	/**
826
	 * Validate Multiselect Field.
827
	 *
828
	 * @param  string $key
829
	 * @param  string $value Posted Value
830
	 * @return string
831
	 */
832
	public function validate_multiselect_field( $key, $value ) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
833
		return is_array( $value ) ? array_map( 'wc_clean', array_map( 'stripslashes', $value ) ) : '';
834
	}
835
836
	/**
837
	 * Validate the data on the "Settings" form.
838
	 * @deprecated 2.6.0 No longer used
839
	 */
840
	public function validate_settings_fields( $form_fields = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form_fields is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
841
		_deprecated_function( 'validate_settings_fields', '2.6' );
842
	}
843
844
	/**
845
	 * Format settings if needed.
846
	 * @deprecated 2.6.0 Unused
847
	 * @param  array $value
848
	 * @return array
849
	 */
850
	public function format_settings( $value ) {
851
		_deprecated_function( 'format_settings', '2.6' );
852
		return $value;
853
	}
854
}
855