1
|
|
|
<?php |
2
|
|
|
namespace Carbon_Fields\REST; |
3
|
|
|
|
4
|
|
|
use Carbon_Fields\Updater\Updater; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Register custom REST routes |
8
|
|
|
*/ |
9
|
|
|
class Routes { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Carbon Fields routes |
13
|
|
|
* |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $routes = array( |
17
|
|
|
'post_meta' => array( |
18
|
|
|
'path' => '/posts/(?P<id>\d+)', |
19
|
|
|
'callback' => 'get_post_meta', |
20
|
|
|
'permission_callback' => 'allow_access', |
21
|
|
|
'methods' => 'GET', |
22
|
|
|
), |
23
|
|
|
'term_meta' => array( |
24
|
|
|
'path' => '/terms/(?P<id>\d+)', |
25
|
|
|
'callback' => 'get_term_meta', |
26
|
|
|
'permission_callback' => 'allow_access', |
27
|
|
|
'methods' => 'GET', |
28
|
|
|
), |
29
|
|
|
'user_meta' => array( |
30
|
|
|
'path' => '/users/(?P<id>\d+)', |
31
|
|
|
'callback' => 'get_user_meta', |
32
|
|
|
'permission_callback' => 'allow_access', |
33
|
|
|
'methods' => 'GET', |
34
|
|
|
), |
35
|
|
|
'comment_meta' => array( |
36
|
|
|
'path' => '/comments/(?P<id>\d+)', |
37
|
|
|
'callback' => 'get_comment_meta', |
38
|
|
|
'permission_callback' => 'allow_access', |
39
|
|
|
'methods' => 'GET', |
40
|
|
|
), |
41
|
|
|
'theme_options' => array( |
42
|
|
|
'path' => '/options/', |
43
|
|
|
'callback' => 'options_accessor', |
44
|
|
|
'permission_callback' => 'options_permission', |
45
|
|
|
'methods' => array( 'GET', 'POST' ), |
46
|
|
|
), |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Version of the API |
51
|
|
|
* |
52
|
|
|
* @see set_version() |
53
|
|
|
* @see get_version() |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $version = '1'; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Plugin slug |
60
|
|
|
* |
61
|
|
|
* @see set_vendor() |
62
|
|
|
* @see get_vendor() |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
protected $vendor = 'carbon-fields'; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Instance of the Data_Manager class |
69
|
|
|
* |
70
|
|
|
* @var object |
71
|
|
|
*/ |
72
|
|
|
public $data_manager; |
73
|
|
|
|
74
|
|
|
public function __construct( $data_manager ) { |
75
|
|
|
$this->data_manager = $data_manager; |
76
|
|
|
|
77
|
|
|
add_action( 'rest_api_init', array( $this, 'register_routes' ), 15 ); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Creates the custom routes |
82
|
|
|
* |
83
|
|
|
* @see create() |
84
|
|
|
*/ |
85
|
|
|
public function register_routes() { |
86
|
|
|
foreach ( $this->routes as $route ) { |
87
|
|
|
$this->create( $route ); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Registers a custom REST route |
93
|
|
|
* |
94
|
|
|
* @param array $route |
95
|
|
|
*/ |
96
|
|
|
public function create( $route ) { |
97
|
|
|
register_rest_route( $this->get_vendor() . '/v' . $this->get_version(), $route['path'], [ |
98
|
|
|
'methods' => $route['methods'], |
99
|
|
|
'permission_callback' => array( $this, $route['permission_callback'] ), |
100
|
|
|
'callback' => array( $this, $route['callback'] ), |
101
|
|
|
] ); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Retrieves Carbon post meta |
106
|
|
|
* |
107
|
|
|
* @param array $data |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
public function get_post_meta( $data ) { |
111
|
|
|
$carbon_data = $this->get_data( 'Post_Meta', $data['id'] ); |
112
|
|
|
return array( 'carbon_fields' => $carbon_data ); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Retrieves Carbon user meta |
117
|
|
|
* |
118
|
|
|
* @param array $data |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
public function get_user_meta( $data ) { |
122
|
|
|
$carbon_data = $this->get_data( 'User_Meta', $data['id'] ); |
123
|
|
|
return array( 'carbon_fields' => $carbon_data ); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Retrieves Carbon term meta |
128
|
|
|
* |
129
|
|
|
* @param array $data |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
public function get_term_meta( $data ) { |
133
|
|
|
$carbon_data = $this->get_data( 'Term_Meta', $data['id'] ); |
134
|
|
|
return array( 'carbon_fields' => $carbon_data ); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Retrieves Carbon comment meta |
139
|
|
|
* |
140
|
|
|
* @param array $data |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
|
|
public function get_comment_meta( $data ) { |
144
|
|
|
$carbon_data = $this->get_data( 'Comment_Meta', $data['id'] ); |
145
|
|
|
return array( 'carbon_fields' => $carbon_data ); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Retrieves Carbon theme options |
150
|
|
|
* |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
|
|
public function get_options() { |
154
|
|
|
$carbon_data = $this->get_data( 'Theme_Options' ); |
155
|
|
|
return array( 'carbon_fields' => $carbon_data ); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Set Carbon theme options |
160
|
|
|
* |
161
|
|
|
* @param WP_REST_Request $request Full data about the request. |
162
|
|
|
* @return WP_Error|WP_REST_Response |
163
|
|
|
*/ |
164
|
|
|
public function set_options( $request ) { |
165
|
|
|
$options = $request->get_params(); |
166
|
|
|
|
167
|
|
|
if ( empty( $options ) ) { |
168
|
|
|
return new \WP_REST_Response( __( 'No option names provided', 'crb' ) ); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
Updater::$is_rest_request = true; |
172
|
|
|
|
173
|
|
|
foreach ( $options as $key => $value ) { |
174
|
|
|
try { |
175
|
|
|
Updater::update_field( 'theme_option', null, $key, $value ); |
176
|
|
|
} catch ( \Exception $e ) { |
177
|
|
|
return new \WP_REST_Response( wp_strip_all_tags( $e->getMessage() ) ); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return new \WP_REST_Response( __( 'Theme Options updated.', 'crb' ), 200 ); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Wrapper method used for retrieving data from the $data_manager |
186
|
|
|
* |
187
|
|
|
* @param string $container_type |
188
|
|
|
* @param string $id |
189
|
|
|
* @return array |
190
|
|
|
*/ |
191
|
|
|
public function get_data( $container_type, $id = '' ) { |
192
|
|
|
return $this->data_manager->get_data( $container_type, $id ); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Sets routes |
197
|
|
|
*/ |
198
|
|
|
public function set_routes( $routes ) { |
199
|
|
|
$this->routes = $routes; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Returns routes |
204
|
|
|
* |
205
|
|
|
* @return array |
206
|
|
|
*/ |
207
|
|
|
public function get_routes() { |
208
|
|
|
return $this->routes; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Sets version |
213
|
|
|
*/ |
214
|
|
|
public function set_version( $version ) { |
215
|
|
|
$this->version = $version; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Returns version |
220
|
|
|
* |
221
|
|
|
* @return string |
222
|
|
|
*/ |
223
|
|
|
public function get_version() { |
224
|
|
|
return $this->version; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Sets vendor |
229
|
|
|
*/ |
230
|
|
|
public function set_vendor( $vendor ) { |
231
|
|
|
$this->vendor = $vendor; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Returns vendor |
236
|
|
|
* |
237
|
|
|
* @return string |
238
|
|
|
*/ |
239
|
|
|
public function get_vendor() { |
240
|
|
|
return $this->vendor; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Proxy method for handling get/set for theme options |
245
|
|
|
* |
246
|
|
|
* @param WP_REST_Request $request |
247
|
|
|
* @return array|WP_REST_Response |
248
|
|
|
*/ |
249
|
|
|
public function options_accessor( $request ) { |
250
|
|
|
$request_type = $request->get_method(); |
251
|
|
|
|
252
|
|
|
if ( $request_type === 'GET' ) { |
253
|
|
|
return $this->get_options(); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
if ( $request_type === 'POST' ) { |
257
|
|
|
return $this->set_options( $request ); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Allow access to an endpoint |
263
|
|
|
* |
264
|
|
|
* @return bool true |
265
|
|
|
*/ |
266
|
|
|
public function allow_access() { |
267
|
|
|
return true; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Proxy method for handling theme options permissions |
272
|
|
|
* |
273
|
|
|
* @param WP_REST_Request $request |
274
|
|
|
* @return bool |
275
|
|
|
*/ |
276
|
|
|
public function options_permission( $request ) { |
277
|
|
|
$request_type = $request->get_method(); |
278
|
|
|
|
279
|
|
|
if ( $request_type === 'GET' ) { |
280
|
|
|
return true; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
if ( $request_type === 'POST' ) { |
284
|
|
|
return current_user_can( 'edit_theme_options' ); |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
} |