1 | <?php |
||
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 ) { |
||
82 | |||
83 | /** |
||
84 | * Boot up functionality |
||
85 | */ |
||
86 | public function boot() { |
||
89 | |||
90 | /** |
||
91 | * Set routes |
||
92 | */ |
||
93 | public function set_routes( $routes ) { |
||
96 | |||
97 | /** |
||
98 | * Return routes |
||
99 | * |
||
100 | * @return array |
||
101 | */ |
||
102 | public function get_routes() { |
||
105 | |||
106 | /** |
||
107 | * Set version |
||
108 | */ |
||
109 | public function set_version( $version ) { |
||
112 | |||
113 | /** |
||
114 | * Return version |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | public function get_version() { |
||
121 | |||
122 | /** |
||
123 | * Set vendor |
||
124 | */ |
||
125 | public function set_vendor( $vendor ) { |
||
128 | |||
129 | /** |
||
130 | * Return vendor |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | public function get_vendor() { |
||
137 | |||
138 | /** |
||
139 | * Allow access to an endpoint |
||
140 | * |
||
141 | * @return bool |
||
142 | */ |
||
143 | public function allow_access() { |
||
146 | |||
147 | /** |
||
148 | * Register custom routes |
||
149 | * |
||
150 | * @see register_route() |
||
151 | */ |
||
152 | public function register_routes() { |
||
157 | |||
158 | /** |
||
159 | * Register a custom REST route |
||
160 | * |
||
161 | * @param array $route |
||
162 | */ |
||
163 | protected function register_route( $route ) { |
||
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 ) { |
||
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 ) { |
||
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 ) { |
||
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 ) { |
||
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 ) { |
||
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 ) { |
||
272 | |||
273 | /** |
||
274 | * Retrieve Carbon theme options |
||
275 | * |
||
276 | * @return array |
||
277 | */ |
||
278 | protected function get_options() { |
||
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 ) { |
||
306 | } |
||
307 |