Completed
Pull Request — master (#11947)
by
unknown
08:12
created

WC_Settings_API::validate_password_field()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
790
		$value = is_null( $value ) ? '' : $value;
791
		return wp_kses_post( trim( stripslashes( $value ) ) );
792
	}
793
794
	/**
795
	 * Validate Price Field.
796
	 *
797
	 * Make sure the data is escaped correctly, etc.
798
	 *
799
	 * @param  string $key
800
	 * @param  string|null $value Posted Value
801
	 * @return string
802
	 */
803
	public function validate_price_field( $key, $value ) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
804
		$value = is_null( $value ) ? '' : $value;
805
		return ( '' === $value ) ? '' : wc_format_decimal( trim( stripslashes( $value ) ) );
806
	}
807
808
	/**
809
	 * Validate Decimal Field.
810
	 *
811
	 * Make sure the data is escaped correctly, etc.
812
	 *
813
	 * @param  string $key
814
	 * @param  string|null $value Posted Value
815
	 * @return string
816
	 */
817
	public function validate_decimal_field( $key, $value ) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
818
		$value = is_null( $value ) ? '' : $value;
819
		return ( '' === $value ) ? '' : wc_format_decimal( trim( stripslashes( $value ) ) );
820
	}
821
822
	/**
823
	 * Validate Password Field. No input sanitization is used to avoid corrupting passwords.
824
	 *
825
	 * @param  string $key
826
	 * @param  string|null $value Posted Value
827
	 * @return string
828
	 */
829
	public function validate_password_field( $key, $value ) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
830
		$value = is_null( $value ) ? '' : $value;
831
		return trim( stripslashes( $value ) );
832
	}
833
834
	/**
835
	 * Validate Textarea Field.
836
	 *
837
	 * @param  string $key
838
	 * @param  string|null $value Posted Value
839
	 * @return string
840
	 */
841 View Code Duplication
	public function validate_textarea_field( $key, $value ) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
842
		$value = is_null( $value ) ? '' : $value;
843
		return wp_kses( trim( stripslashes( $value ) ),
844
			array_merge(
845
				array(
846
					'iframe' => array( 'src' => true, 'style' => true, 'id' => true, 'class' => true ),
847
				),
848
				wp_kses_allowed_html( 'post' )
849
			)
850
		);
851
	}
852
853
	/**
854
	 * Validate Checkbox Field.
855
	 *
856
	 * If not set, return "no", otherwise return "yes".
857
	 *
858
	 * @param  string $key
859
	 * @param  string|null $value Posted Value
860
	 * @return string
861
	 */
862
	public function validate_checkbox_field( $key, $value ) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
863
		return ! is_null( $value ) ? 'yes' : 'no';
864
	}
865
866
	/**
867
	 * Validate Select Field.
868
	 *
869
	 * @param  string $key
870
	 * @param  string $value Posted Value
871
	 * @return string
872
	 */
873
	public function validate_select_field( $key, $value ) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
874
		$value = is_null( $value ) ? '' : $value;
875
		return wc_clean( stripslashes( $value ) );
876
	}
877
878
	/**
879
	 * Validate Multiselect Field.
880
	 *
881
	 * @param  string $key
882
	 * @param  string $value Posted Value
883
	 * @return string
884
	 */
885
	public function validate_multiselect_field( $key, $value ) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
886
		return is_array( $value ) ? array_map( 'wc_clean', array_map( 'stripslashes', $value ) ) : '';
887
	}
888
889
	/**
890
	 * Validate the data on the "Settings" form.
891
	 * @deprecated 2.6.0 No longer used
892
	 */
893
	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...
894
		_deprecated_function( 'validate_settings_fields', '2.6' );
895
	}
896
897
	/**
898
	 * Format settings if needed.
899
	 * @deprecated 2.6.0 Unused
900
	 * @param  array $value
901
	 * @return array
902
	 */
903
	public function format_settings( $value ) {
904
		_deprecated_function( 'format_settings', '2.6' );
905
		return $value;
906
	}
907
}
908