Completed
Pull Request — master (#9826)
by Mike
22:02
created

WC_Settings_API::validate_settings_fields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 1

Duplication

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