Completed
Push — master ( 4bbd92...56e642 )
by Claudio
14:37
created

WC_Settings_API::init_form_fields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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