Passed
Push — master ( 6176aa...f7c939 )
by Mike
03:08
created

SettingsTrait::validate_setting_textarea_field()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 2
dl 0
loc 14
rs 9.9
c 0
b 0
f 0
1
<?php
2
/**
3
 * Settings trait.
4
 *
5
 * @package WooCommerce/RestApi
6
 */
7
8
namespace WooCommerce\RestApi\Controllers\Version4\Utilities;
9
10
/**
11
 * SettingsTrait.
12
 */
13
trait SettingsTrait {
14
	/**
15
	 * Validate a text value for a text based setting.
16
	 *
17
	 * @since 3.0.0
18
	 * @param string $value Value.
19
	 * @param array  $setting Setting.
20
	 * @return string
21
	 */
22
	public function validate_setting_text_field( $value, $setting ) {
0 ignored issues
show
Unused Code introduced by
The parameter $setting is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

22
	public function validate_setting_text_field( $value, /** @scrutinizer ignore-unused */ $setting ) {

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

Loading history...
23
		$value = is_null( $value ) ? '' : $value;
0 ignored issues
show
introduced by
The condition is_null($value) is always false.
Loading history...
24
		return wp_kses_post( trim( stripslashes( $value ) ) );
25
	}
26
27
	/**
28
	 * Validate select based settings.
29
	 *
30
	 * @since 3.0.0
31
	 * @param string $value Value.
32
	 * @param array  $setting Setting.
33
	 * @return string|\WP_Error
34
	 */
35
	public function validate_setting_select_field( $value, $setting ) {
36
		if ( array_key_exists( $value, $setting['options'] ) ) {
37
			return $value;
38
		} else {
39
			return new \WP_Error( 'rest_setting_value_invalid', __( 'An invalid setting value was passed.', 'woocommerce' ), array( 'status' => 400 ) );
40
		}
41
	}
42
43
	/**
44
	 * Validate multiselect based settings.
45
	 *
46
	 * @since 3.0.0
47
	 * @param array $values Values.
48
	 * @param array $setting Setting.
49
	 * @return array|\WP_Error
50
	 */
51
	public function validate_setting_multiselect_field( $values, $setting ) {
52
		if ( empty( $values ) ) {
53
			return array();
54
		}
55
56
		if ( ! is_array( $values ) ) {
0 ignored issues
show
introduced by
The condition is_array($values) is always true.
Loading history...
57
			return new \WP_Error( 'rest_setting_value_invalid', __( 'An invalid setting value was passed.', 'woocommerce' ), array( 'status' => 400 ) );
58
		}
59
60
		$final_values = array();
61
		foreach ( $values as $value ) {
62
			if ( array_key_exists( $value, $setting['options'] ) ) {
63
				$final_values[] = $value;
64
			}
65
		}
66
67
		return $final_values;
68
	}
69
70
	/**
71
	 * Validate image_width based settings.
72
	 *
73
	 * @since 3.0.0
74
	 * @param array $values Values.
75
	 * @param array $setting Setting.
76
	 * @return string|\WP_Error
77
	 */
78
	public function validate_setting_image_width_field( $values, $setting ) {
79
		if ( ! is_array( $values ) ) {
0 ignored issues
show
introduced by
The condition is_array($values) is always true.
Loading history...
80
			return new \WP_Error( 'rest_setting_value_invalid', __( 'An invalid setting value was passed.', 'woocommerce' ), array( 'status' => 400 ) );
81
		}
82
83
		$current = $setting['value'];
84
		if ( isset( $values['width'] ) ) {
85
			$current['width'] = intval( $values['width'] );
86
		}
87
		if ( isset( $values['height'] ) ) {
88
			$current['height'] = intval( $values['height'] );
89
		}
90
		if ( isset( $values['crop'] ) ) {
91
			$current['crop'] = (bool) $values['crop'];
92
		}
93
		return $current;
94
	}
95
96
	/**
97
	 * Validate radio based settings.
98
	 *
99
	 * @since 3.0.0
100
	 * @param string $value Value.
101
	 * @param array  $setting Setting.
102
	 * @return string|\WP_Error
103
	 */
104
	public function validate_setting_radio_field( $value, $setting ) {
105
		return $this->validate_setting_select_field( $value, $setting );
106
	}
107
108
	/**
109
	 * Validate checkbox based settings.
110
	 *
111
	 * @since 3.0.0
112
	 * @param string $value Value.
113
	 * @param array  $setting Setting.
114
	 * @return string|\WP_Error
115
	 */
116
	public function validate_setting_checkbox_field( $value, $setting ) {
117
		if ( in_array( $value, array( 'yes', 'no' ), true ) ) {
118
			return $value;
119
		} elseif ( empty( $value ) ) {
120
			$value = isset( $setting['default'] ) ? $setting['default'] : 'no';
121
			return $value;
122
		} else {
123
			return new \WP_Error( 'rest_setting_value_invalid', __( 'An invalid setting value was passed.', 'woocommerce' ), array( 'status' => 400 ) );
124
		}
125
	}
126
127
	/**
128
	 * Validate textarea based settings.
129
	 *
130
	 * @since 3.0.0
131
	 * @param string $value Value.
132
	 * @param array  $setting Setting.
133
	 * @return string
134
	 */
135
	public function validate_setting_textarea_field( $value, $setting ) {
0 ignored issues
show
Unused Code introduced by
The parameter $setting is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

135
	public function validate_setting_textarea_field( $value, /** @scrutinizer ignore-unused */ $setting ) {

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

Loading history...
136
		$value = is_null( $value ) ? '' : $value;
0 ignored issues
show
introduced by
The condition is_null($value) is always false.
Loading history...
137
		return wp_kses(
138
			trim( stripslashes( $value ) ),
139
			array_merge(
140
				array(
141
					'iframe' => array(
142
						'src'   => true,
143
						'style' => true,
144
						'id'    => true,
145
						'class' => true,
146
					),
147
				),
148
				wp_kses_allowed_html( 'post' )
149
			)
150
		);
151
	}
152
}
153