Completed
Pull Request — master (#11831)
by
unknown
13:02
created

WC_Settings_API::set_post_data()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
eloc 2
nc 1
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 View Code Duplication
		foreach ( $this->get_form_fields() as $key => $field ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
182
		return update_option( $this->get_option_key(), apply_filters( 'woocommerce_settings_api_sanitized_fields_' . $this->id, $this->settings ) );
183
	}
184
185
	/**
186
	 * Add an error message for display in admin on save.
187
	 * @param string $error
188
	 */
189
	public function add_error( $error ) {
190
		$this->errors[] = $error;
191
	}
192
193
	/**
194
	 * Get admin error messages.
195
	 */
196
	public function get_errors() {
197
		return $this->errors;
198
	}
199
200
	/**
201
	 * Display admin error messages.
202
	 */
203
	public function display_errors() {
204
		if ( $this->get_errors() ) {
205
			echo '<div id="woocommerce_errors" class="error notice is-dismissible">';
206
			foreach ( $this->get_errors() as $error ) {
207
				echo '<p>' . wp_kses_post( $error ) . '</p>';
208
			}
209
			echo '</div>';
210
		}
211
	}
212
213
	/**
214
	 * Initialise Settings.
215
	 *
216
	 * Store all settings in a single database entry
217
	 * and make sure the $settings array is either the default
218
	 * or the settings stored in the database.
219
	 *
220
	 * @since 1.0.0
221
	 * @uses get_option(), add_option()
222
	 */
223 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...
224
		$this->settings = get_option( $this->get_option_key(), null );
225
226
		// If there are no settings defined, use defaults.
227
		if ( ! is_array( $this->settings ) ) {
228
			$form_fields    = $this->get_form_fields();
229
			$this->settings = array_merge( array_fill_keys( array_keys( $form_fields ), '' ), wp_list_pluck( $form_fields, 'default' ) );
230
		}
231
	}
232
233
	/**
234
	 * get_option function.
235
	 *
236
	 * Gets an option from the settings API, using defaults if necessary to prevent undefined notices.
237
	 *
238
	 * @param  string $key
239
	 * @param  mixed  $empty_value
240
	 * @return string The value specified for the option or a default value for the option.
241
	 */
242 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...
243
		if ( empty( $this->settings ) ) {
244
			$this->init_settings();
245
		}
246
247
		// Get option default if unset.
248
		if ( ! isset( $this->settings[ $key ] ) ) {
249
			$form_fields            = $this->get_form_fields();
250
			$this->settings[ $key ] = isset( $form_fields[ $key ] ) ? $this->get_field_default( $form_fields[ $key ] ) : '';
251
		}
252
253
		if ( ! is_null( $empty_value ) && '' === $this->settings[ $key ] ) {
254
			$this->settings[ $key ] = $empty_value;
255
		}
256
257
		return $this->settings[ $key ];
258
	}
259
260
	/**
261
	 * Prefix key for settings.
262
	 *
263
	 * @param  mixed $key
264
	 * @return string
265
	 */
266
	public function get_field_key( $key ) {
267
		return $this->plugin_id . $this->id . '_' . $key;
268
	}
269
270
	/**
271
	 * Generate Settings HTML.
272
	 *
273
	 * Generate the HTML for the fields on the "settings" screen.
274
	 *
275
	 * @param  array $form_fields (default: array())
276
	 * @since  1.0.0
277
	 * @uses   method_exists()
278
	 * @return string the html for the settings
279
	 */
280
	public function generate_settings_html( $form_fields = array(), $echo = true ) {
281
		if ( empty( $form_fields ) ) {
282
			$form_fields = $this->get_form_fields();
283
		}
284
285
		$html = '';
286
		foreach ( $form_fields as $k => $v ) {
287
			$type = $this->get_field_type( $v );
288
289
			if( has_filter( 'wc_settings_generate_' . $type . '_html' ) ){
290
				$html .= apply_filters( 'wc_settings_generate_' . $type . '_html', '', $k, $v, $this );
291
			} else if ( method_exists( $this, 'generate_' . $type . '_html' ) ) {
292
				$html .= $this->{'generate_' . $type . '_html'}( $k, $v );
293
			} else {
294
				$html .= $this->generate_text_html( $k, $v );
295
			}
296
		}
297
298
		if ( $echo ) {
299
			echo $html;
300
		} else {
301
			return $html;
302
		}
303
	}
304
305
	/**
306
	 * Get HTML for tooltips.
307
	 *
308
	 * @param  array $data
309
	 * @return string
310
	 */
311
	public function get_tooltip_html( $data ) {
312 View Code Duplication
		if ( $data['desc_tip'] === true ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
313
			$tip = $data['description'];
314
		} elseif ( ! empty( $data['desc_tip'] ) ) {
315
			$tip = $data['desc_tip'];
316
		} else {
317
			$tip = '';
318
		}
319
320
		return $tip ? wc_help_tip( $tip, true ) : '';
321
	}
322
323
	/**
324
	 * Get HTML for descriptions.
325
	 *
326
	 * @param  array $data
327
	 * @return string
328
	 */
329
	public function get_description_html( $data ) {
330 View Code Duplication
		if ( $data['desc_tip'] === true ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
331
			$description = '';
332
		} elseif ( ! empty( $data['desc_tip'] ) ) {
333
			$description = $data['description'];
334
		} elseif ( ! empty( $data['description'] ) ) {
335
			$description = $data['description'];
336
		} else {
337
			$description = '';
338
		}
339
340
		return $description ? '<p class="description">' . wp_kses_post( $description ) . '</p>' . "\n" : '';
341
	}
342
343
	/**
344
	 * Get custom attributes.
345
	 *
346
	 * @param  array $data
347
	 * @return string
348
	 */
349
	public function get_custom_attribute_html( $data ) {
350
		$custom_attributes = array();
351
352 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...
353
			foreach ( $data['custom_attributes'] as $attribute => $attribute_value ) {
354
				$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
355
			}
356
		}
357
358
		return implode( ' ', $custom_attributes );
359
	}
360
361
	/**
362
	 * Generate Text Input HTML.
363
	 *
364
	 * @param  mixed $key
365
	 * @param  mixed $data
366
	 * @since  1.0.0
367
	 * @return string
368
	 */
369 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...
370
		$field_key = $this->get_field_key( $key );
371
		$defaults  = array(
372
			'title'             => '',
373
			'disabled'          => false,
374
			'class'             => '',
375
			'css'               => '',
376
			'placeholder'       => '',
377
			'type'              => 'text',
378
			'desc_tip'          => false,
379
			'description'       => '',
380
			'custom_attributes' => array(),
381
		);
382
383
		$data = wp_parse_args( $data, $defaults );
384
385
		ob_start();
386
		?>
387
		<tr valign="top">
388
			<th scope="row" class="titledesc">
389
				<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
390
				<?php echo $this->get_tooltip_html( $data ); ?>
391
			</th>
392
			<td class="forminp">
393
				<fieldset>
394
					<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
395
					<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 ); ?> />
396
					<?php echo $this->get_description_html( $data ); ?>
397
				</fieldset>
398
			</td>
399
		</tr>
400
		<?php
401
402
		return ob_get_clean();
403
	}
404
405
	/**
406
	 * Generate Upload Input HTML.
407
	 *
408
	 * @param  mixed $key
409
	 * @param  mixed $data
410
	 * @since  2.7.0
411
	 * @return string
412
	 */
413
	public function generate_upload_html( $key, $data ) {
414
415
        wp_enqueue_media();
416
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
            'media_frame_title' => __( 'Select Media', 'woocommerce' ),
429
            'media_button_text' => __( 'Select', 'woocommerce' )
430
        );
431
432
        $data = wp_parse_args( $data, $defaults );
433
434
        ob_start();
435
        ?>
436
        <tr valign="top">
437
            <th scope="row" class="titledesc">
438
                <label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
439
                <?php echo $this->get_tooltip_html( $data ); ?>
440
            </th>
441
            <td class="forminp">
442
                <fieldset>
443
                    <legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
444
445
                    <div class="wc-settings-media-preview-wrapper">
446
                        <?php $attachment_id = $this->get_option( $key );
447
                        
448
                        $remove_button_class = $attachment_id > 0 ? '' : 'hidden';
449
450
                        if( $attachment_id > 0 ){
451
                            echo wp_get_attachment_image( $attachment_id, 'thumbnail', true ); 
452
                            echo '<p class="description">' . basename( get_attached_file( $attachment_id ) ) . '</p>';
453
                        } ?>
454
                    </div>
455
                    
456
                    <button class="wc-settings-media-upload button" data-media_frame_title="<?php echo esc_attr( $data['media_frame_title'] ); ?>" data-media_button_text="<?php echo esc_attr( $data['media_button_text'] ); ?>"><?php _e( "Upload", "wc-legal-docs" ); ?></button>
457
458
                    <input class="wc-settings-media-id input-text regular-input <?php echo esc_attr( $data['class'] ); ?>" type="hidden" name="<?php echo esc_attr( $field_key ); ?>" id="<?php echo esc_attr( $field_key ); ?>" value="<?php echo esc_attr( $this->get_option( $key ) ); ?>" <?php disabled( $data['disabled'], true ); ?> <?php echo $this->get_custom_attribute_html( $data ); ?> />
459
460
                    <button class="wc-settings-media-remove button <?php echo $remove_button_class;?>"><?php _e( "Remove", "wc-legal-docs" ); ?></button>
461
462
                    <?php echo $this->get_description_html( $data ); ?>
463
                </fieldset>
464
            </td>
465
        </tr>
466
        <?php
467
468
        return ob_get_clean();
469
	}
470
471
	/**
472
	 * Generate Price Input HTML.
473
	 *
474
	 * @param  mixed $key
475
	 * @param  mixed $data
476
	 * @since  1.0.0
477
	 * @return string
478
	 */
479 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...
480
		$field_key = $this->get_field_key( $key );
481
		$defaults  = array(
482
			'title'             => '',
483
			'disabled'          => false,
484
			'class'             => '',
485
			'css'               => '',
486
			'placeholder'       => '',
487
			'type'              => 'text',
488
			'desc_tip'          => false,
489
			'description'       => '',
490
			'custom_attributes' => array(),
491
		);
492
493
		$data = wp_parse_args( $data, $defaults );
494
495
		ob_start();
496
		?>
497
		<tr valign="top">
498
			<th scope="row" class="titledesc">
499
				<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
500
				<?php echo $this->get_tooltip_html( $data ); ?>
501
			</th>
502
			<td class="forminp">
503
				<fieldset>
504
					<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
505
					<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 ); ?> />
506
					<?php echo $this->get_description_html( $data ); ?>
507
				</fieldset>
508
			</td>
509
		</tr>
510
		<?php
511
512
		return ob_get_clean();
513
	}
514
515
	/**
516
	 * Generate Decimal Input HTML.
517
	 *
518
	 * @param  mixed $key
519
	 * @param  mixed $data
520
	 * @since  1.0.0
521
	 * @return string
522
	 */
523 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...
524
		$field_key = $this->get_field_key( $key );
525
		$defaults  = array(
526
			'title'             => '',
527
			'disabled'          => false,
528
			'class'             => '',
529
			'css'               => '',
530
			'placeholder'       => '',
531
			'type'              => 'text',
532
			'desc_tip'          => false,
533
			'description'       => '',
534
			'custom_attributes' => array(),
535
		);
536
537
		$data = wp_parse_args( $data, $defaults );
538
539
		ob_start();
540
		?>
541
		<tr valign="top">
542
			<th scope="row" class="titledesc">
543
				<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
544
				<?php echo $this->get_tooltip_html( $data ); ?>
545
			</th>
546
			<td class="forminp">
547
				<fieldset>
548
					<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
549
					<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 ); ?> />
550
					<?php echo $this->get_description_html( $data ); ?>
551
				</fieldset>
552
			</td>
553
		</tr>
554
		<?php
555
556
		return ob_get_clean();
557
	}
558
559
	/**
560
	 * Generate Password Input HTML.
561
	 *
562
	 * @param  mixed $key
563
	 * @param  mixed $data
564
	 * @since  1.0.0
565
	 * @return string
566
	 */
567
	public function generate_password_html( $key, $data ) {
568
		$data['type'] = 'password';
569
		return $this->generate_text_html( $key, $data );
570
	}
571
572
	/**
573
	 * Generate Color Picker Input HTML.
574
	 *
575
	 * @param  mixed $key
576
	 * @param  mixed $data
577
	 * @since  1.0.0
578
	 * @return string
579
	 */
580
	public function generate_color_html( $key, $data ) {
581
		$field_key = $this->get_field_key( $key );
582
		$defaults  = array(
583
			'title'             => '',
584
			'disabled'          => false,
585
			'class'             => '',
586
			'css'               => '',
587
			'placeholder'       => '',
588
			'desc_tip'          => false,
589
			'description'       => '',
590
			'custom_attributes' => array(),
591
		);
592
593
		$data = wp_parse_args( $data, $defaults );
594
595
		ob_start();
596
		?>
597
		<tr valign="top">
598
			<th scope="row" class="titledesc">
599
				<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
600
				<?php echo $this->get_tooltip_html( $data ); ?>
601
			</th>
602
			<td class="forminp">
603
				<fieldset>
604
					<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
605
					<span class="colorpickpreview" style="background:<?php echo esc_attr( $this->get_option( $key ) ); ?>;"></span>
606
					<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 ); ?> />
607
					<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>
608
					<?php echo $this->get_description_html( $data ); ?>
609
				</fieldset>
610
			</td>
611
		</tr>
612
		<?php
613
614
		return ob_get_clean();
615
	}
616
617
	/**
618
	 * Generate Textarea HTML.
619
	 *
620
	 * @param  mixed $key
621
	 * @param  mixed $data
622
	 * @since  1.0.0
623
	 * @return string
624
	 */
625 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...
626
		$field_key = $this->get_field_key( $key );
627
		$defaults  = array(
628
			'title'             => '',
629
			'disabled'          => false,
630
			'class'             => '',
631
			'css'               => '',
632
			'placeholder'       => '',
633
			'type'              => 'text',
634
			'desc_tip'          => false,
635
			'description'       => '',
636
			'custom_attributes' => array(),
637
		);
638
639
		$data = wp_parse_args( $data, $defaults );
640
641
		ob_start();
642
		?>
643
		<tr valign="top">
644
			<th scope="row" class="titledesc">
645
				<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
646
				<?php echo $this->get_tooltip_html( $data ); ?>
647
			</th>
648
			<td class="forminp">
649
				<fieldset>
650
					<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
651
					<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>
652
					<?php echo $this->get_description_html( $data ); ?>
653
				</fieldset>
654
			</td>
655
		</tr>
656
		<?php
657
658
		return ob_get_clean();
659
	}
660
661
	/**
662
	 * Generate Checkbox HTML.
663
	 *
664
	 * @param  mixed $key
665
	 * @param  mixed $data
666
	 * @since  1.0.0
667
	 * @return string
668
	 */
669
	public function generate_checkbox_html( $key, $data ) {
670
		$field_key = $this->get_field_key( $key );
671
		$defaults  = array(
672
			'title'             => '',
673
			'label'             => '',
674
			'disabled'          => false,
675
			'class'             => '',
676
			'css'               => '',
677
			'type'              => 'text',
678
			'desc_tip'          => false,
679
			'description'       => '',
680
			'custom_attributes' => array(),
681
		);
682
683
		$data = wp_parse_args( $data, $defaults );
684
685
		if ( ! $data['label'] ) {
686
			$data['label'] = $data['title'];
687
		}
688
689
		ob_start();
690
		?>
691
		<tr valign="top">
692
			<th scope="row" class="titledesc">
693
				<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
694
				<?php echo $this->get_tooltip_html( $data ); ?>
695
			</th>
696
			<td class="forminp">
697
				<fieldset>
698
					<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
699
					<label for="<?php echo esc_attr( $field_key ); ?>">
700
					<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/>
701
					<?php echo $this->get_description_html( $data ); ?>
702
				</fieldset>
703
			</td>
704
		</tr>
705
		<?php
706
707
		return ob_get_clean();
708
	}
709
710
	/**
711
	 * Generate Select HTML.
712
	 *
713
	 * @param  mixed $key
714
	 * @param  mixed $data
715
	 * @since  1.0.0
716
	 * @return string
717
	 */
718
	public function generate_select_html( $key, $data ) {
719
		$field_key = $this->get_field_key( $key );
720
		$defaults  = array(
721
			'title'             => '',
722
			'disabled'          => false,
723
			'class'             => '',
724
			'css'               => '',
725
			'placeholder'       => '',
726
			'type'              => 'text',
727
			'desc_tip'          => false,
728
			'description'       => '',
729
			'custom_attributes' => array(),
730
			'options'           => array(),
731
		);
732
733
		$data = wp_parse_args( $data, $defaults );
734
735
		ob_start();
736
		?>
737
		<tr valign="top">
738
			<th scope="row" class="titledesc">
739
				<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
740
				<?php echo $this->get_tooltip_html( $data ); ?>
741
			</th>
742
			<td class="forminp">
743
				<fieldset>
744
					<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
745
					<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 ); ?>>
746
						<?php foreach ( (array) $data['options'] as $option_key => $option_value ) : ?>
747
							<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>
748
						<?php endforeach; ?>
749
					</select>
750
					<?php echo $this->get_description_html( $data ); ?>
751
				</fieldset>
752
			</td>
753
		</tr>
754
		<?php
755
756
		return ob_get_clean();
757
	}
758
759
	/**
760
	 * Generate Multiselect HTML.
761
	 *
762
	 * @param  mixed $key
763
	 * @param  mixed $data
764
	 * @since  1.0.0
765
	 * @return string
766
	 */
767
	public function generate_multiselect_html( $key, $data ) {
768
		$field_key = $this->get_field_key( $key );
769
		$defaults  = array(
770
			'title'             => '',
771
			'disabled'          => false,
772
			'class'             => '',
773
			'css'               => '',
774
			'placeholder'       => '',
775
			'type'              => 'text',
776
			'desc_tip'          => false,
777
			'description'       => '',
778
			'custom_attributes' => array(),
779
			'select_buttons'    => false,
780
			'options'           => array(),
781
		);
782
783
		$data  = wp_parse_args( $data, $defaults );
784
		$value = (array) $this->get_option( $key, array() );
785
786
		ob_start();
787
		?>
788
		<tr valign="top">
789
			<th scope="row" class="titledesc">
790
				<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
791
				<?php echo $this->get_tooltip_html( $data ); ?>
792
			</th>
793
			<td class="forminp">
794
				<fieldset>
795
					<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
796
					<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 ); ?>>
797
						<?php foreach ( (array) $data['options'] as $option_key => $option_value ) : ?>
798
							<option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( in_array( $option_key, $value ), true ); ?>><?php echo esc_attr( $option_value ); ?></option>
799
						<?php endforeach; ?>
800
					</select>
801
					<?php echo $this->get_description_html( $data ); ?>
802
					<?php if ( $data['select_buttons'] ) : ?>
803
						<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>
804
					<?php endif; ?>
805
				</fieldset>
806
			</td>
807
		</tr>
808
		<?php
809
810
		return ob_get_clean();
811
	}
812
813
	/**
814
	 * Generate Title HTML.
815
	 *
816
	 * @param  mixed $key
817
	 * @param  mixed $data
818
	 * @since  1.0.0
819
	 * @return string
820
	 */
821
	public function generate_title_html( $key, $data ) {
822
		$field_key = $this->get_field_key( $key );
823
		$defaults  = array(
824
			'title' => '',
825
			'class' => '',
826
		);
827
828
		$data = wp_parse_args( $data, $defaults );
829
830
		ob_start();
831
		?>
832
			</table>
833
			<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>
834
			<?php if ( ! empty( $data['description'] ) ) : ?>
835
				<p><?php echo wp_kses_post( $data['description'] ); ?></p>
836
			<?php endif; ?>
837
			<table class="form-table">
838
		<?php
839
840
		return ob_get_clean();
841
	}
842
843
	/**
844
	 * Validate Text Field.
845
	 *
846
	 * Make sure the data is escaped correctly, etc.
847
	 *
848
	 * @param  string $key Field key
849
	 * @param  string|null $value Posted Value
850
	 * @return string
851
	 */
852
	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...
853
		$value = is_null( $value ) ? '' : $value;
854
		return wp_kses_post( trim( stripslashes( $value ) ) );
855
	}
856
857
	/**
858
	 * Validate Price Field.
859
	 *
860
	 * Make sure the data is escaped correctly, etc.
861
	 *
862
	 * @param  string $key
863
	 * @param  string|null $value Posted Value
864
	 * @return string
865
	 */
866
	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...
867
		$value = is_null( $value ) ? '' : $value;
868
		return $value === '' ? '' : wc_format_decimal( trim( stripslashes( $value ) ) );
869
	}
870
871
	/**
872
	 * Validate Decimal Field.
873
	 *
874
	 * Make sure the data is escaped correctly, etc.
875
	 *
876
	 * @param  string $key
877
	 * @param  string|null $value Posted Value
878
	 * @return string
879
	 */
880
	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...
881
		$value = is_null( $value ) ? '' : $value;
882
		return $value === '' ? '' : wc_format_decimal( trim( stripslashes( $value ) ) );
883
	}
884
885
	/**
886
	 * Validate Password Field. No input sanitization is used to avoid corrupting passwords.
887
	 *
888
	 * @param  string $key
889
	 * @param  string|null $value Posted Value
890
	 * @return string
891
	 */
892
	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...
893
		$value = is_null( $value ) ? '' : $value;
894
		return trim( stripslashes( $value ) );
895
	}
896
897
	/**
898
	 * Validate Textarea Field.
899
	 *
900
	 * @param  string $key
901
	 * @param  string|null $value Posted Value
902
	 * @return string
903
	 */
904
	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...
905
		$value = is_null( $value ) ? '' : $value;
906
		return wp_kses( trim( stripslashes( $value ) ),
907
			array_merge(
908
				array(
909
					'iframe' => array( 'src' => true, 'style' => true, 'id' => true, 'class' => true ),
910
				),
911
				wp_kses_allowed_html( 'post' )
912
			)
913
		);
914
	}
915
916
	/**
917
	 * Validate Checkbox Field.
918
	 *
919
	 * If not set, return "no", otherwise return "yes".
920
	 *
921
	 * @param  string $key
922
	 * @param  string|null $value Posted Value
923
	 * @return string
924
	 */
925
	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...
926
		return ! is_null( $value ) ? 'yes' : 'no';
927
	}
928
929
	/**
930
	 * Validate Select Field.
931
	 *
932
	 * @param  string $key
933
	 * @param  string $value Posted Value
934
	 * @return string
935
	 */
936
	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...
937
		$value = is_null( $value ) ? '' : $value;
938
		return wc_clean( stripslashes( $value ) );
939
	}
940
941
	/**
942
	 * Validate Multiselect Field.
943
	 *
944
	 * @param  string $key
945
	 * @param  string $value Posted Value
946
	 * @return string
947
	 */
948
	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...
949
		return is_array( $value ) ? array_map( 'wc_clean', array_map( 'stripslashes', $value ) ) : '';
950
	}
951
952
	/**
953
	 * Validate the data on the "Settings" form.
954
	 * @deprecated 2.6.0 No longer used
955
	 */
956
	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...
957
		_deprecated_function( 'validate_settings_fields', '2.6' );
958
	}
959
960
	/**
961
	 * Format settings if needed.
962
	 * @deprecated 2.6.0 Unused
963
	 * @param  array $value
964
	 * @return array
965
	 */
966
	public function format_settings( $value ) {
967
		_deprecated_function( 'format_settings', '2.6' );
968
		return $value;
969
	}
970
}
971