Completed
Push — milestone/2.0 ( 8a1186...26a446 )
by
unknown
04:33
created

Router::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
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
			// TODO remove debug statement
222
			if ( false && ! $field->get_visible_in_rest_api() ) {
223
				continue;
224
			}
225
			$values[ $field->get_base_name() ] = Helper::get_value( $object_id, $container_type, $field->get_base_name() );
226
		}
227
		return $values;
228
	}
229
230
	/**
231
	 * Get Carbon Fields post meta values
232
	 * 
233
	 * @param  array $data
234
	 * @return array
235
	 */
236
	public function get_post_meta( $data ) {
237
		$carbon_data = $this->get_all_field_values( 'Post_Meta', $data['id'] );
238
		return array( 'carbon_fields' => $carbon_data );
239
	}
240
241
	/**
242
	 * Get Carbon Fields user meta values
243
	 * 
244
	 * @param  array $data
245
	 * @return array
246
	 */
247
	public function get_user_meta( $data ) {
248
		$carbon_data = $this->get_all_field_values( 'User_Meta', $data['id'] );
249
		return array( 'carbon_fields' => $carbon_data );
250
	}
251
252
	/**
253
	 * Get Carbon Fields term meta values
254
	 * 
255
	 * @param  array $data
256
	 * @return array
257
	 */
258
	public function get_term_meta( $data ) {
259
		$carbon_data = $this->get_all_field_values( 'Term_Meta', $data['id'] );
260
		return array( 'carbon_fields' => $carbon_data );
261
	}
262
263
	/**
264
	 * Get Carbon Fields comment meta values
265
	 * 
266
	 * @param  array $data
267
	 * @return array
268
	 */
269
	public function get_comment_meta( $data ) {
270
		$carbon_data = $this->get_all_field_values( 'Comment_Meta', $data['id'] );
271
		return array( 'carbon_fields' => $carbon_data );
272
	}
273
274
	/**
275
	 * Retrieve Carbon theme options
276
	 * 
277
	 * @return array
278
	 */
279
	protected function get_options() {
280
		$carbon_data = $this->get_all_field_values( 'Theme_Options' );
281
		return array( 'carbon_fields' => $carbon_data );
282
	}
283
284
	/**
285
	 * Set Carbon theme options
286
	 *
287
	 * @param WP_REST_Request $request Full data about the request.
288
	 * @return WP_Error|WP_REST_Response
289
	 */
290
	protected function set_options( $request ) {
291
		$options = $request->get_params();
292
		
293
		if ( empty( $options ) ) {
294
			return new \WP_REST_Response( __( 'No option names provided', 'crb' ) );
295
		}
296
		
297
		foreach ( $options as $key => $value ) {
298
			try {
299
				$success = Helper::set_value( null, 'Theme_Options', $key, $value );
300
				if ( ! $success ) {
301
					return new \WP_REST_Response( 'Failed to find or update field "' . $key . '".' );
302
				}
303
			} catch ( \Exception $e ) {
304
				return new \WP_REST_Response( wp_strip_all_tags( $e->getMessage() ) );
305
			}
306
		}
307
308
		return new \WP_REST_Response( __( 'Theme Options updated.', 'crb' ), 200 );
309
	}
310
}
311