|
1
|
|
|
<?php |
|
2
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
3
|
|
|
exit; |
|
4
|
|
|
} |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Base class for settings endpoints so common code can be shared. Handles permissions |
|
8
|
|
|
* and helper functions used by both settings and groups endpoints. |
|
9
|
|
|
* |
|
10
|
|
|
* @author WooThemes |
|
11
|
|
|
* @category API |
|
12
|
|
|
* @package WooCommerce/Abstracts |
|
13
|
|
|
* @extends WP_REST_Controller |
|
14
|
|
|
* @version 2.7.0 |
|
15
|
|
|
*/ |
|
16
|
|
|
class WC_REST_Settings_API_Controller extends WC_REST_Controller { |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Route base. |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $rest_base = 'settings'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Makes sure the current user has access to the settings APIs. |
|
27
|
|
|
* |
|
28
|
|
|
* @since 2.7.0 |
|
29
|
|
|
* @param WP_REST_Request $request Full details about the request. |
|
30
|
|
|
* @return WP_Error|boolean |
|
31
|
|
|
*/ |
|
32
|
|
View Code Duplication |
public function permissions_check( $request ) { |
|
|
|
|
|
|
33
|
|
|
if ( ! current_user_can( 'manage_options' ) ) { |
|
34
|
|
|
return new WP_Error( |
|
35
|
|
|
'woocommerce_rest_cannot_view', |
|
36
|
|
|
__( 'Sorry, you cannot access settings.', 'woocommerce' ), |
|
37
|
|
|
array( 'status' => rest_authorization_required_code() ) |
|
38
|
|
|
); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return true; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Cleans a value before setting it. |
|
46
|
|
|
* |
|
47
|
|
|
* @since 2.7.0 |
|
48
|
|
|
* |
|
49
|
|
|
* @param array $setting WC Setting Array |
|
50
|
|
|
* @param mixed $raw_value Raw value from PUT request |
|
51
|
|
|
* @return mixed Sanitized value |
|
52
|
|
|
*/ |
|
53
|
|
|
public function sanitize_setting_value( $setting, $raw_value ) { |
|
54
|
|
|
switch ( $setting['type'] ) { |
|
55
|
|
|
case 'checkbox' : |
|
56
|
|
|
$default = ( ! empty( $setting['default'] ) ? $setting['default'] : 'no' ); |
|
57
|
|
|
$value = ( in_array( $raw_value, array( 'yes', 'no' ) ) ? $raw_value : $default ); |
|
58
|
|
|
break; |
|
59
|
|
|
case 'email' : |
|
60
|
|
|
$value = sanitize_email( $raw_value ); |
|
61
|
|
|
$default = ( ! empty( $setting['default'] ) ? $setting['default'] : '' ); |
|
62
|
|
|
$value = ( ! empty( $value ) ? $value : $default ); |
|
63
|
|
|
break; |
|
64
|
|
|
case 'textarea' : |
|
65
|
|
|
$value = wp_kses( trim( $raw_value ), |
|
66
|
|
|
array_merge( |
|
67
|
|
|
array( |
|
68
|
|
|
'iframe' => array( 'src' => true, 'style' => true, 'id' => true, 'class' => true ) |
|
69
|
|
|
), |
|
70
|
|
|
wp_kses_allowed_html( 'post' ) |
|
71
|
|
|
) |
|
72
|
|
|
); |
|
73
|
|
|
break; |
|
74
|
|
|
case 'multiselect' : |
|
75
|
|
|
case 'multi_select_countries' : |
|
76
|
|
|
$value = array_filter( array_map( 'wc_clean', (array) $raw_value ) ); |
|
77
|
|
|
break; |
|
78
|
|
View Code Duplication |
case 'image_width' : |
|
|
|
|
|
|
79
|
|
|
$value = array(); |
|
80
|
|
|
if ( isset( $raw_value['width'] ) ) { |
|
81
|
|
|
$value['width'] = wc_clean( $raw_value['width'] ); |
|
82
|
|
|
$value['height'] = wc_clean( $raw_value['height'] ); |
|
83
|
|
|
$value['crop'] = isset( $raw_value['crop'] ) ? 1 : 0; |
|
84
|
|
|
} else { |
|
85
|
|
|
$value['width'] = $setting['default']['width']; |
|
86
|
|
|
$value['height'] = $setting['default']['height']; |
|
87
|
|
|
$value['crop'] = $setting['default']['crop']; |
|
88
|
|
|
} |
|
89
|
|
|
break; |
|
90
|
|
|
case 'select': |
|
91
|
|
|
$options = array_keys( $setting['options'] ); |
|
92
|
|
|
$default = ( empty( $setting['default'] ) ? $options[0] : $setting['default'] ); |
|
93
|
|
|
$value = in_array( $raw_value, $options ) ? $raw_value : $default; |
|
94
|
|
|
break; |
|
95
|
|
|
default : |
|
96
|
|
|
$value = wc_clean( $raw_value ); |
|
97
|
|
|
break; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// A couple fields changed in the REST API -- we can just pass these too so old filters still work |
|
101
|
|
|
$setting['desc'] = ( ! empty( $setting['description'] ) ? $setting['description'] : '' ); |
|
102
|
|
|
$setting['title'] = ( ! empty( $setting['label'] ) ? $setting['label'] : '' ); |
|
103
|
|
|
|
|
104
|
|
|
$value = apply_filters( 'woocommerce_admin_settings_sanitize_option', $value, $setting, $raw_value ); |
|
105
|
|
|
$value = apply_filters( "woocommerce_admin_settings_sanitize_option_" . $setting['id'], $value, $setting, $raw_value ); |
|
106
|
|
|
do_action( 'woocommerce_update_option', $setting ); |
|
107
|
|
|
|
|
108
|
|
|
return $value; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Get a value from WP's settings API. |
|
113
|
|
|
* |
|
114
|
|
|
* @since 2.7.0 |
|
115
|
|
|
* @param string $setting |
|
116
|
|
|
* @param string $default |
|
117
|
|
|
* @return mixed |
|
118
|
|
|
*/ |
|
119
|
|
View Code Duplication |
public function get_value( $setting, $default = '' ) { |
|
|
|
|
|
|
120
|
|
|
if ( strstr( $setting, '[' ) ) { // Array value. |
|
121
|
|
|
parse_str( $setting, $setting_array ); |
|
122
|
|
|
$setting = current( array_keys( $setting ) ); |
|
123
|
|
|
$values = get_option( $setting, '' ); |
|
124
|
|
|
$key = key( $setting_array[ $setting ] ); |
|
125
|
|
|
$value = isset( $values[ $key ] ) ? $values[ $key ] : null; |
|
126
|
|
|
} else { // Single value. |
|
127
|
|
|
$value = get_option( $setting, null ); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
if ( is_array( $value ) ) { |
|
131
|
|
|
$value = array_map( 'stripslashes', $value ); |
|
132
|
|
|
} elseif ( ! is_null( $value ) ) { |
|
133
|
|
|
$value = stripslashes( $value ); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $value === null ? $default : $value; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Filters out bad values from the settings array/filter so we |
|
141
|
|
|
* only return known values via the API. |
|
142
|
|
|
* |
|
143
|
|
|
* @since 2.7.0 |
|
144
|
|
|
* @param array $setting |
|
145
|
|
|
* @return array |
|
146
|
|
|
*/ |
|
147
|
|
|
public function filter_setting( $setting ) { |
|
148
|
|
|
$setting = array_intersect_key( |
|
149
|
|
|
$setting, |
|
150
|
|
|
array_flip( array_filter( array_keys( $setting ), array( $this, 'allowed_setting_keys' ) ) ) |
|
151
|
|
|
); |
|
152
|
|
|
|
|
153
|
|
|
if ( empty( $setting['options'] ) ) { |
|
154
|
|
|
unset( $setting['options'] ); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return $setting; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Callback for allowed keys for each setting response. |
|
162
|
|
|
* |
|
163
|
|
|
* @since 2.7.0 |
|
164
|
|
|
* @param string $key Key to check |
|
165
|
|
|
* @return boolean |
|
166
|
|
|
*/ |
|
167
|
|
|
public function allowed_setting_keys( $key ) { |
|
168
|
|
|
return in_array( $key, array( |
|
169
|
|
|
'id', 'label', 'description', 'default', 'tip', |
|
170
|
|
|
'placeholder', 'type', 'options', 'value', |
|
171
|
|
|
) ); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Boolean for if a setting type is a valid supported setting type. |
|
176
|
|
|
* |
|
177
|
|
|
* @since 2.7.0 |
|
178
|
|
|
* @param string $type |
|
179
|
|
|
* @return boolean |
|
180
|
|
|
*/ |
|
181
|
|
|
public function is_setting_type_valid( $type ) { |
|
182
|
|
|
return in_array( $type, array( |
|
183
|
|
|
'text', 'email', 'number', 'color', 'password', |
|
184
|
|
|
'textarea', 'select', 'multiselect', 'radio', 'checkbox', |
|
185
|
|
|
'multi_select_countries', 'image_width', |
|
186
|
|
|
) ); |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.