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