Completed
Pull Request — master (#9826)
by Mike
11:23
created

WC_Settings_API::get_field_default()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 2
eloc 2
nc 2
nop 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 an 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_key = $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_key ); ?>"><?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_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 ); ?> />
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_key = $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_key ); ?>"><?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_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 ); ?> />
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_key = $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_key ); ?>"><?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_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 ); ?> />
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_key = $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_key ); ?>"><?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_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 ); ?> />
487
					<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>
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_key = $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_key ); ?>"><?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_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>
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_key = $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_key ); ?>"><?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_key ); ?>">
580
					<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/>
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
	public function generate_select_html( $key, $data ) {
599
		$field_key = $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_key ); ?>"><?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_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 ); ?>>
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
	public function generate_multiselect_html( $key, $data ) {
648
		$field_key = $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
			'select_buttons'    => false,
660
			'options'           => array()
661
		);
662
663
		$data  = wp_parse_args( $data, $defaults );
664
		$value = (array) $this->get_option( $key, array() );
665
666
		ob_start();
667
		?>
668
		<tr valign="top">
669
			<th scope="row" class="titledesc">
670
				<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
671
				<?php echo $this->get_tooltip_html( $data ); ?>
672
			</th>
673
			<td class="forminp">
674
				<fieldset>
675
					<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
676
					<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 ); ?>>
677
						<?php foreach ( (array) $data['options'] as $option_key => $option_value ) : ?>
678
							<option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( in_array( $option_key, $value ), true ); ?>><?php echo esc_attr( $option_value ); ?></option>
679
						<?php endforeach; ?>
680
					</select>
681
					<?php echo $this->get_description_html( $data ); ?>
682
					<?php if ( $data['select_buttons'] ) : ?>
683
						<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>
684
					<?php endif; ?>
685
				</fieldset>
686
			</td>
687
		</tr>
688
		<?php
689
690
		return ob_get_clean();
691
	}
692
693
	/**
694
	 * Generate Title HTML.
695
	 *
696
	 * @param  mixed $key
697
	 * @param  mixed $data
698
	 * @since  1.0.0
699
	 * @return string
700
	 */
701
	public function generate_title_html( $key, $data ) {
702
		$field_key = $this->get_field_key( $key );
703
		$defaults  = array(
704
			'title' => '',
705
			'class' => ''
706
		);
707
708
		$data = wp_parse_args( $data, $defaults );
709
710
		ob_start();
711
		?>
712
			</table>
713
			<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>
714
			<?php if ( ! empty( $data['description'] ) ) : ?>
715
				<p><?php echo wp_kses_post( $data['description'] ); ?></p>
716
			<?php endif; ?>
717
			<table class="form-table">
718
		<?php
719
720
		return ob_get_clean();
721
	}
722
723
	/**
724
	 * Validate Text Field.
725
	 *
726
	 * Make sure the data is escaped correctly, etc.
727
	 *
728
	 * @param  string $key
729
	 * @return string
730
	 */
731
	public function validate_text_field( $key ) {
732
		$text      = $this->get_option( $key );
733
		$field_key = $this->get_field_key( $key );
734
735
		if ( isset( $_POST[ $field_key ] ) ) {
736
			$text = wp_kses_post( trim( stripslashes( $_POST[ $field_key ] ) ) );
737
		}
738
739
		return $text;
740
	}
741
742
	/**
743
	 * Validate Price Field.
744
	 *
745
	 * Make sure the data is escaped correctly, etc.
746
	 *
747
	 * @param  string $key
748
	 * @return string
749
	 */
750 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...
751
		$text      = $this->get_option( $key );
752
		$field_key = $this->get_field_key( $key );
753
754
		if ( isset( $_POST[ $field_key ] ) ) {
755
756
			if ( $_POST[ $field_key ] !== '' ) {
757
				$text = wc_format_decimal( trim( stripslashes( $_POST[ $field_key ] ) ) );
758
			} else {
759
				$text = '';
760
			}
761
		}
762
763
		return $text;
764
	}
765
766
	/**
767
	 * Validate Decimal Field.
768
	 *
769
	 * Make sure the data is escaped correctly, etc.
770
	 *
771
	 * @param  string $key
772
	 * @return string
773
	 */
774 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...
775
		$text      = $this->get_option( $key );
776
		$field_key = $this->get_field_key( $key );
777
778
		if ( isset( $_POST[ $field_key ] ) ) {
779
780
			if ( $_POST[ $field_key ] !== '' ) {
781
				$text = wc_format_decimal( trim( stripslashes( $_POST[ $field_key ] ) ) );
782
			} else {
783
				$text = '';
784
			}
785
		}
786
787
		return $text;
788
	}
789
790
	/**
791
	 * Validate Password Field.
792
	 *
793
	 * Make sure the data is escaped correctly, etc.
794
	 *
795
	 * @param  string $key
796
	 * @return string
797
	 */
798
	public function validate_password_field( $key ) {
799
		$field_key = $this->get_field_key( $key );
800
		$value     = wp_kses_post( trim( stripslashes( $_POST[ $field_key ] ) ) );
801
		return $value;
802
	}
803
804
	/**
805
	 * Validate Textarea Field.
806
	 *
807
	 * @param  string $key
808
	 * @return string
809
	 */
810
	public function validate_textarea_field( $key ) {
811
		$field_key = $this->get_field_key( $key );
812
813
		if ( isset( $_POST[ $field_key ] ) ) {
814
			$text = wp_kses( trim( stripslashes( $_POST[ $field_key ] ) ),
815
				array_merge(
816
					array(
817
						'iframe' => array( 'src' => true, 'style' => true, 'id' => true, 'class' => true )
818
					),
819
					wp_kses_allowed_html( 'post' )
820
				)
821
			);
822
		} else {
823
			$text = $this->get_option( $key );
824
		}
825
826
		return $text;
827
	}
828
829
	/**
830
	 * Validate Checkbox Field.
831
	 *
832
	 * If not set, return "no", otherwise return "yes".
833
	 *
834
	 * @param  string $key
835
	 * @return string
836
	 */
837
	public function validate_checkbox_field( $key ) {
838
		$field_key = $this->get_field_key( $key );
839
		return isset( $_POST[ $field_key ] ) && '1' === $_POST[ $field_key ] ? 'yes' : 'no';
840
	}
841
842
	/**
843
	 * Validate Select Field.
844
	 *
845
	 * @param  string $key
846
	 * @return string
847
	 */
848
	public function validate_select_field( $key ) {
849
		$field_key = $this->get_field_key( $key );
850
		return isset( $_POST[ $field_key ] ) ? wc_clean( stripslashes( $_POST[ $field_key ] ) ) : $this->get_option( $key );
851
	}
852
853
	/**
854
	 * Validate Multiselect Field.
855
	 *
856
	 * @param  string $key
857
	 * @return string
858
	 */
859
	public function validate_multiselect_field( $key ) {
860
		$field_key = $this->get_field_key( $key );
861
		return isset( $_POST[ $field_key ] ) ? array_map( 'wc_clean', array_map( 'stripslashes', (array) $_POST[ $field_key ] ) ) : '';
862
	}
863
864
	/**
865
	 * Validate the data on the "Settings" form.
866
	 * @deprecated 2.6.0 No longer used
867
	 */
868
	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...
869
870
	/**
871
	 * Format settings if needed.
872
	 * @deprecated 2.6.0 Unused
873
	 * @param  array $value
874
	 * @return array
875
	 */
876
	public function format_settings( $value ) {
877
		return $value;
878
	}
879
}
880