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
|
|
|
* Filters out bad values from the settings array/filter so we |
46
|
|
|
* only return known values via the API. |
47
|
|
|
* |
48
|
|
|
* @since 2.7.0 |
49
|
|
|
* @param array $setting |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
public function filter_setting( $setting ) { |
53
|
|
|
$setting = array_intersect_key( |
54
|
|
|
$setting, |
55
|
|
|
array_flip( array_filter( array_keys( $setting ), array( $this, 'allowed_setting_keys' ) ) ) |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
if ( empty( $setting['options'] ) ) { |
59
|
|
|
unset( $setting['options'] ); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $setting; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Callback for allowed keys for each setting response. |
67
|
|
|
* |
68
|
|
|
* @since 2.7.0 |
69
|
|
|
* @param string $key Key to check |
70
|
|
|
* @return boolean |
71
|
|
|
*/ |
72
|
|
|
public function allowed_setting_keys( $key ) { |
73
|
|
|
return in_array( $key, array( |
74
|
|
|
'id', 'label', 'description', 'default', 'tip', |
75
|
|
|
'placeholder', 'type', 'options', 'value', |
76
|
|
|
) ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Boolean for if a setting type is a valid supported setting type. |
81
|
|
|
* |
82
|
|
|
* @since 2.7.0 |
83
|
|
|
* @param string $type |
84
|
|
|
* @return boolean |
85
|
|
|
*/ |
86
|
|
|
public function is_setting_type_valid( $type ) { |
87
|
|
|
return in_array( $type, array( |
88
|
|
|
'text', 'email', 'number', 'color', 'password', |
89
|
|
|
'textarea', 'select', 'multiselect', 'radio', 'checkbox', |
90
|
|
|
'multi_select_countries', 'image_width', |
91
|
|
|
) ); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.