Completed
Push — master ( 77da7c...c757fd )
by Mike
09:37
created

WC_Settings_API::get_option()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 17
Code Lines 9

Duplication

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