Completed
Pull Request — master (#9826)
by Mike
15:37
created

WC_Settings_API::generate_select_html()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 40
Code Lines 30

Duplication

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