Completed
Pull Request — master (#9826)
by Mike
10:01
created

WC_Settings_API::validate_settings_fields()   C

Complexity

Conditions 7
Paths 18

Size

Total Lines 32
Code Lines 15

Duplication

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

Syntax error, unexpected ')', expecting ';'
Loading history...
843
	}
844
845
	/**
846
	 * Validate Select Field.
847
	 *
848
	 * @param  string $key
849
	 * @return string
850
	 */
851
	public function validate_select_field( $key ) {
852
		$field = $this->get_field_key( $key );
853
		return isset( $_POST[ $field ] ) ? wc_clean( stripslashes( $_POST[ $field ] ) ) : $this->get_option( $key );
854
	}
855
856
	/**
857
	 * Validate Multiselect Field.
858
	 *
859
	 * @param  string $key
860
	 * @return string
861
	 */
862
	public function validate_multiselect_field( $key ) {
863
		$field = $this->get_field_key( $key );
864
		return isset( $_POST[ $field ] ) ? array_map( 'wc_clean', array_map( 'stripslashes', (array) $_POST[ $field ] ) ) : '';
865
	}
866
}
867