Completed
Push — development ( 5ed9d2...646735 )
by
unknown
04:14
created

Router::get_all_field_values()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 12
nop 2
dl 0
loc 18
rs 9.3554
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 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
194
		return current_user_can( 'manage_options' );
195
	}
196
197
	/**
198
	 * Wrapper method used for retrieving data from Data_Manager
199
	 *
200
	 * @param  string $container_type
201
	 * @param  string $object_id
202
	 * @return array
203
	 */
204
	protected function get_all_field_values( $container_type, $object_id = null ) {
205
		$object_id = ( $object_id !== '' ) ? $object_id : null;
206
207
		$containers = $this->container_repository->get_containers( $container_type );
208
		$fields = array();
209
		foreach ( $containers as $container ) {
210
			$fields = array_merge( $fields, $container->get_fields() );
211
		}
212
213
		$values = array();
214
		foreach ( $fields as $field ) {
215
			if ( ! $field->get_visible_in_rest_api() ) {
216
				continue;
217
			}
218
			$values[ $field->get_base_name() ] = Helper::get_value( $object_id, $container_type, '', $field->get_base_name() );
219
		}
220
		return $values;
221
	}
222
223
	/**
224
	 * Get Carbon Fields post meta values
225
	 *
226
	 * @param  array $data
227
	 * @return array
228
	 */
229
	public function get_post_meta( $data ) {
230
		$carbon_data = $this->get_all_field_values( 'post_meta', $data['id'] );
231
		return array( 'carbon_fields' => $carbon_data );
232
	}
233
234
	/**
235
	 * Get Carbon Fields user meta values
236
	 *
237
	 * @param  array $data
238
	 * @return array
239
	 */
240
	public function get_user_meta( $data ) {
241
		$carbon_data = $this->get_all_field_values( 'user_meta', $data['id'] );
242
		return array( 'carbon_fields' => $carbon_data );
243
	}
244
245
	/**
246
	 * Get Carbon Fields term meta values
247
	 *
248
	 * @param  array $data
249
	 * @return array
250
	 */
251
	public function get_term_meta( $data ) {
252
		$carbon_data = $this->get_all_field_values( 'term_meta', $data['id'] );
253
		return array( 'carbon_fields' => $carbon_data );
254
	}
255
256
	/**
257
	 * Get Carbon Fields comment meta values
258
	 *
259
	 * @param  array $data
260
	 * @return array
261
	 */
262
	public function get_comment_meta( $data ) {
263
		$carbon_data = $this->get_all_field_values( 'comment_meta', $data['id'] );
264
		return array( 'carbon_fields' => $carbon_data );
265
	}
266
267
	/**
268
	 * Retrieve Carbon theme options
269
	 *
270
	 * @return array
271
	 */
272
	protected function get_options() {
273
		$carbon_data = $this->get_all_field_values( 'theme_options' );
274
		return array( 'carbon_fields' => $carbon_data );
275
	}
276
277
	/**
278
	 * Set Carbon theme options
279
	 *
280
	 * @param WP_REST_Request $request Full data about the request.
281
	 * @return WP_Error|WP_REST_Response
282
	 */
283
	protected function set_options( $request ) {
284
		$options = $request->get_params();
285
286
		if ( empty( $options ) ) {
287
			return new \WP_REST_Response( __( 'No option names provided', 'carbon-fields' ) );
288
		}
289
290
		foreach ( $options as $key => $value ) {
291
			try {
292
				Helper::set_value( null, 'Theme_Options', '', $key, $value );
293
			} catch ( \Exception $e ) {
294
				return new \WP_REST_Response( wp_strip_all_tags( $e->getMessage() ) );
295
			}
296
		}
297
298
		return new \WP_REST_Response( __( 'Theme Options updated.', 'carbon-fields' ), 200 );
299
	}
300
}
301