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

WC_Settings_API::set_defaults()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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