|
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
|
|
|
'association_data' => array( |
|
50
|
|
|
'path' => '/association', |
|
51
|
|
|
'callback' => 'get_association_data', |
|
52
|
|
|
'permission_callback' => 'allow_access', |
|
53
|
|
|
'methods' => 'GET', |
|
54
|
|
|
), |
|
55
|
|
|
'attachment_data' => array( |
|
56
|
|
|
'path' => '/attachment', |
|
57
|
|
|
'callback' => 'get_attachment_data', |
|
58
|
|
|
'permission_callback' => 'allow_access', |
|
59
|
|
|
'methods' => 'GET', |
|
60
|
|
|
), |
|
61
|
|
|
); |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Version of the API |
|
65
|
|
|
* |
|
66
|
|
|
* @see set_version() |
|
67
|
|
|
* @see get_version() |
|
68
|
|
|
* @var string |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $version = '1'; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Vendor slug for the API |
|
74
|
|
|
* |
|
75
|
|
|
* @see set_vendor() |
|
76
|
|
|
* @see get_vendor() |
|
77
|
|
|
* @var string |
|
78
|
|
|
*/ |
|
79
|
|
|
protected $vendor = 'carbon-fields'; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* ContainerRepository instance |
|
83
|
|
|
* |
|
84
|
|
|
* @var ContainerRepository |
|
85
|
|
|
*/ |
|
86
|
|
|
protected $container_repository; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param ContainerRepository $container_repository |
|
90
|
|
|
*/ |
|
91
|
|
|
public function __construct( ContainerRepository $container_repository ) { |
|
92
|
|
|
$this->container_repository = $container_repository; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Boot up functionality |
|
97
|
|
|
*/ |
|
98
|
|
|
public function boot() { |
|
99
|
|
|
add_action( 'rest_api_init', array( $this, 'register_routes' ), 15 ); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Set routes |
|
104
|
|
|
*/ |
|
105
|
|
|
public function set_routes( $routes ) { |
|
106
|
|
|
$this->routes = $routes; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Return routes |
|
111
|
|
|
* |
|
112
|
|
|
* @return array |
|
113
|
|
|
*/ |
|
114
|
|
|
public function get_routes() { |
|
115
|
|
|
return $this->routes; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Set version |
|
120
|
|
|
*/ |
|
121
|
|
|
public function set_version( $version ) { |
|
122
|
|
|
$this->version = $version; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Return version |
|
127
|
|
|
* |
|
128
|
|
|
* @return string |
|
129
|
|
|
*/ |
|
130
|
|
|
public function get_version() { |
|
131
|
|
|
return $this->version; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Set vendor |
|
136
|
|
|
*/ |
|
137
|
|
|
public function set_vendor( $vendor ) { |
|
138
|
|
|
$this->vendor = $vendor; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Return vendor |
|
143
|
|
|
* |
|
144
|
|
|
* @return string |
|
145
|
|
|
*/ |
|
146
|
|
|
public function get_vendor() { |
|
147
|
|
|
return $this->vendor; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Allow access to an endpoint |
|
152
|
|
|
* |
|
153
|
|
|
* @return bool |
|
154
|
|
|
*/ |
|
155
|
|
|
public function allow_access() { |
|
156
|
|
|
return true; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Register custom routes |
|
161
|
|
|
* |
|
162
|
|
|
* @see register_route() |
|
163
|
|
|
*/ |
|
164
|
|
|
public function register_routes() { |
|
165
|
|
|
foreach ( $this->routes as $route ) { |
|
166
|
|
|
$this->register_route( $route ); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Register a custom REST route |
|
172
|
|
|
* |
|
173
|
|
|
* @param array $route |
|
174
|
|
|
*/ |
|
175
|
|
|
protected function register_route( $route ) { |
|
176
|
|
|
register_rest_route( $this->get_vendor() . '/v' . $this->get_version(), $route['path'], array( |
|
177
|
|
|
'methods' => $route['methods'], |
|
178
|
|
|
'permission_callback' => array( $this, $route['permission_callback'] ), |
|
179
|
|
|
'callback' => array( $this, $route['callback'] ), |
|
180
|
|
|
) ); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Proxy method for handling get/set for theme options |
|
185
|
|
|
* |
|
186
|
|
|
* @param WP_REST_Request $request |
|
187
|
|
|
* @return array|WP_REST_Response |
|
188
|
|
|
*/ |
|
189
|
|
|
public function options_accessor( $request ) { |
|
190
|
|
|
$request_type = $request->get_method(); |
|
191
|
|
|
|
|
192
|
|
|
if ( $request_type === 'POST' ) { |
|
193
|
|
|
return $this->set_options( $request ); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
return $this->get_options(); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Proxy method for handling theme options permissions |
|
201
|
|
|
* |
|
202
|
|
|
* @param WP_REST_Request $request |
|
203
|
|
|
* @return bool |
|
204
|
|
|
*/ |
|
205
|
|
|
public function options_permission( $request ) { |
|
|
|
|
|
|
206
|
|
|
return current_user_can( 'manage_options' ); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Wrapper method used for retrieving data from Data_Manager |
|
211
|
|
|
* |
|
212
|
|
|
* @param string $container_type |
|
213
|
|
|
* @param string $object_id |
|
214
|
|
|
* @return array |
|
215
|
|
|
*/ |
|
216
|
|
|
protected function get_all_field_values( $container_type, $object_id = null ) { |
|
217
|
|
|
$object_id = ( $object_id !== '' ) ? $object_id : null; |
|
218
|
|
|
|
|
219
|
|
|
$containers = $this->container_repository->get_containers( $container_type ); |
|
220
|
|
|
$fields = array(); |
|
221
|
|
|
foreach ( $containers as $container ) { |
|
222
|
|
|
$fields = array_merge( $fields, $container->get_fields() ); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
$values = array(); |
|
226
|
|
|
foreach ( $fields as $field ) { |
|
227
|
|
|
if ( ! $field->get_visible_in_rest_api() ) { |
|
228
|
|
|
continue; |
|
229
|
|
|
} |
|
230
|
|
|
$values[ $field->get_base_name() ] = Helper::get_value( $object_id, $container_type, '', $field->get_base_name() ); |
|
231
|
|
|
} |
|
232
|
|
|
return $values; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Get Carbon Fields post meta values |
|
237
|
|
|
* |
|
238
|
|
|
* @param array $data |
|
239
|
|
|
* @return array |
|
240
|
|
|
*/ |
|
241
|
|
|
public function get_post_meta( $data ) { |
|
242
|
|
|
$carbon_data = $this->get_all_field_values( 'post_meta', $data['id'] ); |
|
243
|
|
|
return array( 'carbon_fields' => $carbon_data ); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Get Carbon Fields user meta values |
|
248
|
|
|
* |
|
249
|
|
|
* @param array $data |
|
250
|
|
|
* @return array |
|
251
|
|
|
*/ |
|
252
|
|
|
public function get_user_meta( $data ) { |
|
253
|
|
|
$carbon_data = $this->get_all_field_values( 'user_meta', $data['id'] ); |
|
254
|
|
|
return array( 'carbon_fields' => $carbon_data ); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Get Carbon Fields term meta values |
|
259
|
|
|
* |
|
260
|
|
|
* @param array $data |
|
261
|
|
|
* @return array |
|
262
|
|
|
*/ |
|
263
|
|
|
public function get_term_meta( $data ) { |
|
264
|
|
|
$carbon_data = $this->get_all_field_values( 'term_meta', $data['id'] ); |
|
265
|
|
|
return array( 'carbon_fields' => $carbon_data ); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* Get Carbon Fields comment meta values |
|
270
|
|
|
* |
|
271
|
|
|
* @param array $data |
|
272
|
|
|
* @return array |
|
273
|
|
|
*/ |
|
274
|
|
|
public function get_comment_meta( $data ) { |
|
275
|
|
|
$carbon_data = $this->get_all_field_values( 'comment_meta', $data['id'] ); |
|
276
|
|
|
return array( 'carbon_fields' => $carbon_data ); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Get Carbon Fields association options data. |
|
281
|
|
|
* |
|
282
|
|
|
* @return array |
|
283
|
|
|
*/ |
|
284
|
|
|
public function get_association_data() { |
|
285
|
|
|
$container_id = $_GET['container_id']; |
|
|
|
|
|
|
286
|
|
|
$field_id = $_GET['field_id']; |
|
|
|
|
|
|
287
|
|
|
$options = isset( $_GET['options'] ) ? $_GET['options'] : []; |
|
|
|
|
|
|
288
|
|
|
$return_value = array(); |
|
289
|
|
|
|
|
290
|
|
|
$field = Helper::get_field( null, $container_id, $field_id ); |
|
291
|
|
|
|
|
292
|
|
|
foreach ( $options as $entry ) { |
|
293
|
|
|
$item = array( |
|
294
|
|
|
'type' => $entry['type'], |
|
295
|
|
|
'subtype' => $entry['subtype'], |
|
296
|
|
|
'thumbnail' => $field->get_thumbnail_by_type( $entry['id'], $entry['type'], $entry['subtype'] ), |
|
|
|
|
|
|
297
|
|
|
'id' => intval( $entry['id'] ), |
|
298
|
|
|
'title' => $field->get_title_by_type( $entry['id'], $entry['type'], $entry['subtype'] ), |
|
|
|
|
|
|
299
|
|
|
'label' => $field->get_item_label( $entry['id'], $entry['type'], $entry['subtype'] ), |
|
|
|
|
|
|
300
|
|
|
'is_trashed' => ( $entry['type'] == 'post' && get_post_status( $entry['id'] ) === 'trash' ), |
|
301
|
|
|
); |
|
302
|
|
|
|
|
303
|
|
|
$return_value[] = $item; |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
return $return_value; |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
/** |
|
310
|
|
|
* Get attachment data by given ID or URL. |
|
311
|
|
|
* |
|
312
|
|
|
* @return array |
|
313
|
|
|
*/ |
|
314
|
|
|
public function get_attachment_data() { |
|
315
|
|
|
$type = sanitize_text_field( $_GET['type'] ); |
|
|
|
|
|
|
316
|
|
|
$value = sanitize_text_field( $_GET['value'] ); |
|
|
|
|
|
|
317
|
|
|
|
|
318
|
|
|
return Helper::get_attachment_metadata( $value, $type ); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* Retrieve Carbon theme options |
|
323
|
|
|
* |
|
324
|
|
|
* @return array |
|
325
|
|
|
*/ |
|
326
|
|
|
protected function get_options() { |
|
327
|
|
|
$carbon_data = $this->get_all_field_values( 'theme_options' ); |
|
328
|
|
|
return array( 'carbon_fields' => $carbon_data ); |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
/** |
|
332
|
|
|
* Set Carbon theme options |
|
333
|
|
|
* |
|
334
|
|
|
* @param WP_REST_Request $request Full data about the request. |
|
335
|
|
|
* @return WP_Error|WP_REST_Response |
|
336
|
|
|
*/ |
|
337
|
|
|
protected function set_options( $request ) { |
|
338
|
|
|
$options = $request->get_params(); |
|
339
|
|
|
|
|
340
|
|
|
if ( empty( $options ) ) { |
|
341
|
|
|
return new \WP_REST_Response( __( 'No option names provided', 'carbon-fields' ) ); |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
foreach ( $options as $key => $value ) { |
|
345
|
|
|
try { |
|
346
|
|
|
Helper::set_value( null, 'Theme_Options', '', $key, $value ); |
|
347
|
|
|
} catch ( \Exception $e ) { |
|
348
|
|
|
return new \WP_REST_Response( wp_strip_all_tags( $e->getMessage() ) ); |
|
349
|
|
|
} |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
return new \WP_REST_Response( __( 'Theme Options updated.', 'carbon-fields' ), 200 ); |
|
353
|
|
|
} |
|
354
|
|
|
} |
|
355
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.