PluginProbe ʕ •ᴥ•ʔ
Advanced Access Manager – Access Governance for WordPress / trunk
Advanced Access Manager – Access Governance for WordPress vtrunk
6.8.4 6.8.5 6.9.0 6.9.1 6.9.10 6.9.11 6.9.12 6.9.13 6.9.14 6.9.15 6.9.16 6.9.17 6.9.18 6.9.19 6.9.2 6.9.20 6.9.21 6.9.22 6.9.23 6.9.24 6.9.25 6.9.26 6.9.27 6.9.28 6.9.29 6.9.3 6.9.30 6.9.31 6.9.32 6.9.33 6.9.34 6.9.35 6.9.36 6.9.37 6.9.38 6.9.39 6.9.4 6.9.41 6.9.42 6.9.43 6.9.44 6.9.45 6.9.46 6.9.47 6.9.48 6.9.49 6.9.5 6.9.51 6.9.6 6.9.7 6.9.8 6.9.9 7.0.0 7.0.0-alpha.6 7.0.0-alpha.7 7.0.0-beta.1 7.0.0-rc1 7.0.0-rc2 7.0.0-rc3 7.0.1 7.0.10 7.0.11 7.0.2 7.0.3 7.0.4 7.0.5 7.0.6 7.0.7 7.0.8 7.0.9 7.1.0 7.1.1 trunk 3.0 4.0 4.0.1 4.1 4.2 4.3 4.4 4.4.1 4.5 4.6 4.6.1 4.6.2 4.7 4.7.1 4.7.2 4.7.5 4.7.6 4.8 4.8.1 4.9 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.5.1 4.9.5.2 5.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1 5.1.1 5.10 5.11 5.2 5.2.1 5.2.5 5.2.6 5.2.7 5.3 5.3.1 5.3.2 5.3.3 5.3.4 5.3.5 5.4 5.4.1 5.4.2 5.4.3 5.4.3.1 5.4.3.2 5.5 5.5.1 5.5.2 5.6 5.6.1 5.6.1.1 5.7 5.7.1 5.7.2 5.7.3 5.8 5.8.1 5.8.2 5.8.3 5.9 5.9.1 5.9.1.1 5.9.2 5.9.2.1 5.9.3 5.9.4 5.9.5 5.9.6 5.9.6.1 5.9.6.2 5.9.6.3 5.9.7 5.9.7.1 5.9.7.2 5.9.7.3 5.9.8 5.9.8.1 5.9.9 5.9.9.1 6.0.0 6.0.1 6.0.2 6.0.3 6.0.4 6.0.5 6.1.0 6.1.1 6.2.0 6.2.1 6.2.2 6.3.0 6.3.1 6.3.2 6.3.3 6.4.0 6.4.1 6.4.2 6.4.3 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.6.0 6.6.1 6.6.2 6.6.3 6.6.4 6.7.0 6.7.1 6.7.2 6.7.3 6.7.4 6.7.5 6.7.6 6.7.7 6.7.8 6.7.9 6.8.0 6.8.1 6.8.2 6.8.3
advanced-access-manager / application / Restful / Content.php
advanced-access-manager / application / Restful Last commit date
AccessDeniedRedirect.php 1 year ago AdminToolbar.php 1 year ago ApiRoute.php 1 year ago BackendMenu.php 1 year ago BackwardCompatibility.php 1 year ago Capability.php 1 year ago Configs.php 5 months ago Content.php 1 year ago Identity.php 1 year ago Jwt.php 5 months ago LoginRedirect.php 1 year ago LogoutRedirect.php 1 year ago Metabox.php 1 year ago Mu.php 1 year ago NotFoundRedirect.php 1 year ago Policies.php 1 year ago Roles.php 1 year ago SecureLogin.php 1 year ago SecurityAudit.php 1 year ago ServiceTrait.php 1 year ago Settings.php 1 year ago Urls.php 1 year ago Users.php 1 year ago Widgets.php 1 year ago
Content.php
667 lines
1 <?php
2
3 /**
4 * ======================================================================
5 * LICENSE: This file is subject to the terms and conditions defined in *
6 * file 'license.txt', which is part of this source code package. *
7 * ======================================================================
8 */
9
10 /**
11 * RESTful API for the Posts & Terms (aka Content) service
12 *
13 * @package AAM
14 *
15 * @version 7.0.0
16 */
17 class AAM_Restful_Content
18 {
19
20 use AAM_Restful_ServiceTrait;
21
22 /**
23 * Necessary permissions to access endpoint
24 *
25 * @version 7.0.0
26 */
27 const PERMISSIONS = [
28 'aam_manager',
29 'aam_manage_content'
30 ];
31
32 /**
33 * Constructor
34 *
35 * @return void
36 *
37 * @access protected
38 * @version 7.0.0
39 */
40 protected function __construct()
41 {
42 add_action('rest_api_init', function() {
43 // Get list of all registered post types
44 $this->_register_route('/post_types', [
45 'methods' => WP_REST_Server::READABLE,
46 'callback' => [ $this, 'get_post_types' ],
47 'args' => [
48 'return_all' => [
49 'description' => 'Include all post types or only public',
50 'type' => 'boolean',
51 'default' => null
52 ]
53 ]
54 ], self::PERMISSIONS);
55
56 // Get list of all registered taxonomies
57 $this->_register_route('/taxonomies', array(
58 'methods' => WP_REST_Server::READABLE,
59 'callback' => array($this, 'get_taxonomies')
60 ), self::PERMISSIONS);
61
62 // Get list of terms for given taxonomy
63 $this->_register_route('/terms', array(
64 'methods' => WP_REST_Server::READABLE,
65 'callback' => array($this, 'get_terms'),
66 'args' => array(
67 'taxonomy' => array(
68 'description' => 'Scope for specific taxonomy',
69 'type' => 'string',
70 'required' => true
71 ),
72 'post_type' => array(
73 'description' => 'Scope for specific post type',
74 'type' => 'string'
75 ),
76 'search' => array(
77 'description' => 'Search string',
78 'type' => 'string'
79 ),
80 'offset' => array(
81 'description' => 'Pagination offset',
82 'type' => 'number',
83 'default' => 0
84 ),
85 'per_page' => array(
86 'description' => 'Pagination limit per page',
87 'type' => 'number',
88 'default' => 10
89 )
90 )
91 ), self::PERMISSIONS);
92
93 // Get list of posts for given post type
94 $this->_register_route('/posts', array(
95 'methods' => WP_REST_Server::READABLE,
96 'callback' => array($this, 'get_posts'),
97 'args' => array(
98 'post_type' => array(
99 'description' => 'Unique post type identifier',
100 'type' => 'string',
101 'required' => true
102 ),
103 'search' => array(
104 'description' => 'Search string',
105 'type' => 'string'
106 ),
107 'offset' => array(
108 'description' => 'Pagination offset',
109 'type' => 'number',
110 'default' => 0
111 ),
112 'per_page' => array(
113 'description' => 'Pagination limit per page',
114 'type' => 'number',
115 'default' => 10
116 )
117 )
118 ), self::PERMISSIONS);
119
120 // Get post permissions
121 $this->_register_route('/post/(?P<id>[\d]+)', array(
122 'methods' => WP_REST_Server::READABLE,
123 'callback' => array($this, 'get_post'),
124 'args' => array(
125 'id' => array(
126 'description' => 'Unique post identifier',
127 'type' => 'number',
128 'required' => true
129 )
130 )
131 ), self::PERMISSIONS);
132
133 // Update post permissions
134 $this->_register_route('/post/(?P<id>[\d]+)', array(
135 'methods' => WP_REST_Server::EDITABLE,
136 'callback' => array($this, 'update_post_permissions'),
137 'args' => array(
138 'id' => array(
139 'description' => 'Unique post identifier',
140 'type' => 'number',
141 'required' => true
142 ),
143 'permissions' => array(
144 'description' => 'Collection of permissions',
145 'type' => 'array',
146 'required' => true,
147 'items' => array(
148 'type' => 'object',
149 'properties' => array(
150 'permission' => array(
151 'type' => 'string',
152 'required' => true
153 ),
154 'effect' => array(
155 'type' => 'string',
156 'required' => true,
157 'default' => 'deny',
158 'enum' => [ 'allow', 'deny' ]
159 )
160 )
161 )
162 )
163 )
164 ), self::PERMISSIONS);
165
166 // Update post permission
167 $this->_register_route('/post/(?P<id>[\d]+)/(?P<permission>[\w]+)', [
168 'methods' => WP_REST_Server::EDITABLE,
169 'callback' => [ $this, 'set_post_permission' ],
170 'args' => [
171 'id' => [
172 'description' => 'Unique post identifier',
173 'type' => 'number',
174 'required' => true
175 ],
176 'permission' => [
177 'description' => 'Permission',
178 'type' => 'string',
179 'required' => true
180 ],
181 'effect' => [
182 'type' => 'string',
183 'required' => true,
184 'default' => 'deny',
185 'enum' => [ 'allow', 'deny' ]
186 ]
187 ]
188 ], self::PERMISSIONS);
189
190 // Reset post permissions
191 $this->_register_route('/post/(?P<id>[\d]+)', array(
192 'methods' => WP_REST_Server::DELETABLE,
193 'callback' => array($this, 'reset_post_permissions'),
194 'args' => array(
195 'id' => array(
196 'description' => 'Unique post identifier',
197 'type' => 'number',
198 'required' => true
199 )
200 )
201 ), self::PERMISSIONS);
202 });
203 }
204
205 /**
206 * Get the list of all registered post type
207 *
208 * @param WP_REST_Request $request
209 *
210 * @return WP_REST_Response
211 * @access public
212 *
213 * @version 7.0.0
214 */
215 public function get_post_types(WP_REST_Request $request)
216 {
217 try {
218 $access_level = $this->_determine_access_level($request);
219 $raw_list = AAM::api()->content->get_post_types(
220 $access_level,
221 $request->get_param('return_all')
222 );
223
224 $result = [
225 'list' => [],
226 'summary' => [
227 'total_count' => count(get_post_types()),
228 'filtered_count' => 0
229 ]
230 ];
231
232 foreach($raw_list as $post_type) {
233 array_push(
234 $result['list'], $this->_prepare_post_type_output(
235 $access_level,
236 $post_type
237 )
238 );
239
240 $result['summary']['filtered_count']++;
241 }
242 } catch (Exception $e) {
243 $result = $this->_prepare_error_response($e);
244 }
245
246 return rest_ensure_response($result);
247 }
248
249 /**
250 * Get list of taxonomies
251 *
252 * @param WP_REST_Request $request
253 *
254 * @return WP_REST_Response
255 * @access public
256 *
257 * @version 7.0.0
258 */
259 public function get_taxonomies(WP_REST_Request $request)
260 {
261 try {
262 $access_level = $this->_determine_access_level($request);
263 $raw_list = AAM::api()->content->get_taxonomies(
264 $request->get_param('return_all')
265 );
266
267 $result = [
268 'list' => [],
269 'summary' => [
270 'total_count' => count(get_taxonomies()),
271 'filtered_count' => 0
272 ]
273 ];
274
275 foreach($raw_list as $taxonomy) {
276 array_push(
277 $result['list'], $this->_prepare_taxonomy_output(
278 $access_level, $taxonomy
279 )
280 );
281
282 $result['summary']['filtered_count']++;
283 }
284 } catch (Exception $e) {
285 $result = $this->_prepare_error_response($e);
286 }
287
288 return rest_ensure_response($result);
289 }
290
291 /**
292 * Get list of posts
293 *
294 * @param WP_REST_Request $request
295 *
296 * @return WP_REST_Response
297 * @access public
298 *
299 * @version 7.0.0
300 */
301 public function get_posts(WP_REST_Request $request)
302 {
303 try {
304 $args = [
305 'numberposts' => $request->get_param('per_page'),
306 'post_type' => $request->get_param('post_type'),
307 's' => $request->get_param('search'),
308 'offset' => $request->get_param('offset')
309 ];
310
311 // Getting the list of posts
312 // The minimum required attribute is post_type. If it is not defined,
313 // throw an error
314 if (empty($args['post_type']) || !is_string($args['post_type'])) {
315 throw new InvalidArgumentException(
316 'The post_type has to be a valid string'
317 );
318 }
319
320 if (empty($args['numberposts'])) {
321 $args['numberposts'] = 10;
322 }
323
324 $result = [
325 'list' => [],
326 'summary' => [
327 'total_count' => AAM::api()->content->get_post_count(
328 $args['post_type']
329 ),
330 'filtered_count' => AAM::api()->content->get_post_count(
331 $args['post_type'], $args['s']
332 )
333 ]
334 ];
335
336 $access_level = $this->_determine_access_level($request);
337
338 foreach(AAM::api()->content->get_posts($args) as $item) {
339 array_push($result['list'], $this->_prepare_post_output(
340 $item, $access_level
341 ));
342 }
343 } catch (Exception $e) {
344 $result = $this->_prepare_error_response($e);
345 }
346
347 return rest_ensure_response($result);
348 }
349
350 /**
351 * Get post permissions
352 *
353 * @param WP_REST_Request $request
354 *
355 * @return WP_REST_Response
356 * @access public
357 *
358 * @version 7.0.0
359 */
360 public function get_post(WP_REST_Request $request)
361 {
362 try {
363 $result = $this->_prepare_post_output(
364 get_post($request->get_param('id')),
365 $this->_determine_access_level($request)
366 );
367 } catch (Exception $e) {
368 $result = $this->_prepare_error_response($e);
369 }
370
371 return rest_ensure_response($result);
372 }
373
374 /**
375 * Get list of terms
376 *
377 * @param WP_REST_Request $request
378 *
379 * @return WP_REST_Response
380 * @access public
381 *
382 * @version 7.0.0
383 */
384 public function get_terms(WP_REST_Request $request)
385 {
386 try {
387 $access_level = $this->_determine_access_level($request);
388 $args = [
389 'number' => $request->get_param('per_page'),
390 'taxonomy' => $request->get_param('taxonomy'),
391 'hide_empty' => false,
392 'search' => $request->get_param('search'),
393 'offset' => $request->get_param('offset')
394 ];
395
396 // Getting the list of terms
397 // The minimum required attribute is taxonomy. If it is not defined,
398 // throw an error
399 if (empty($args['taxonomy']) || !is_string($args['taxonomy'])) {
400 throw new InvalidArgumentException(
401 'The taxonomy has to be a valid string'
402 );
403 }
404
405 if (empty($args['number'])) {
406 $args['number'] = 10;
407 }
408
409 $result = [
410 'list' => [],
411 'summary' => [
412 'total_count' => AAM::api()->content->get_term_count(
413 $args['taxonomy']
414 ),
415 'filtered_count' => AAM::api()->content->get_term_count(
416 $args['taxonomy'], $args
417 )
418 ]
419 ];
420
421 $raw_list = AAM::api()->content->get_terms(
422 $args, $request->get_param('post_type')
423 );
424
425 foreach($raw_list as $item) {
426 array_push($result['list'], $this->_prepare_term_output(
427 $access_level, $item
428 ));
429 }
430 } catch (Exception $e) {
431 $result = $this->_prepare_error_response($e);
432 }
433
434 return rest_ensure_response($result);
435 }
436
437 /**
438 * Update post permissions
439 *
440 * @param WP_REST_Request $request
441 *
442 * @return WP_REST_Response
443 * @access public
444 *
445 * @version 7.0.0
446 */
447 public function update_post_permissions(WP_REST_Request $request)
448 {
449 try {
450 $post = get_post($request->get_param('id'));
451 $access_level = $this->_determine_access_level($request);
452 $resource = $access_level->get_resource(
453 AAM_Framework_Type_Resource::POST
454 );
455
456 // Normalize array of permissions
457 $normalized = [];
458
459 foreach($request->get_param('permissions') as $item) {
460 $normalized[$item['permission']] = array_filter($item, function($k) {
461 return $k !== 'permission';
462 }, ARRAY_FILTER_USE_KEY);
463 }
464
465 $resource->set_permissions($normalized, $post);
466
467 $result = $this->_prepare_post_output($post, $access_level);
468 } catch (Exception $e) {
469 $result = $this->_prepare_error_response($e);
470 }
471
472 return rest_ensure_response($result);
473 }
474
475 /**
476 * Update single post permission
477 *
478 * @param WP_REST_Request $request
479 *
480 * @return WP_REST_Response
481 * @access public
482 *
483 * @version 7.0.0
484 */
485 public function set_post_permission(WP_REST_Request $request)
486 {
487 try {
488 $post = get_post($request->get_param('id'));
489 $access_level = $this->_determine_access_level($request);
490 $resource = $access_level->get_resource(
491 AAM_Framework_Type_Resource::POST
492 );
493
494 $resource->set_permission(
495 $post,
496 $request->get_param('permission'),
497 $request->get_json_params()
498 );
499
500 $result = $this->_prepare_post_output($post, $access_level);
501 } catch (Exception $e) {
502 $result = $this->_prepare_error_response($e);
503 }
504
505 return rest_ensure_response($result);
506 }
507
508 /**
509 * Delete post permissions
510 *
511 * @param WP_REST_Request $request
512 *
513 * @return WP_REST_Response
514 * @access public
515 *
516 * @version 7.0.0
517 */
518 public function reset_post_permissions(WP_REST_Request $request)
519 {
520 try {
521 $post = get_post($request->get_param('id'));
522 $resource = $this->_determine_access_level($request)->get_resource(
523 AAM_Framework_Type_Resource::POST
524
525 );
526
527 $result = [
528 'success' => $resource->reset($post)
529 ];
530 } catch (Exception $e) {
531 $result = $this->_prepare_error_response($e);
532 }
533
534 return rest_ensure_response($result);
535 }
536
537 /**
538 * Prepare post output model
539 *
540 * @param WP_Post $post
541 * @param AAM_Framework_AccessLevel_Interface $access_level
542 *
543 * @return array
544 * @access private
545 *
546 * @version 7.0.0
547 */
548 private function _prepare_post_output($post, $access_level)
549 {
550 // Get post type to add additional information about post
551 $post_type = get_post_type_object($post->post_type);
552 $resource = $access_level->get_resource(
553 AAM_Framework_Type_Resource::POST
554 );
555
556 $result = [
557 'id' => $post->ID,
558 'icon' => $post_type->menu_icon,
559 'is_hierarchical' => $post_type->hierarchical
560 ];
561
562 if ($post->post_type === 'nav_menu_item') {
563 $result['title'] = wp_setup_nav_menu_item($post)->title;
564 } else {
565 $result['title'] = $post->post_title;
566 }
567
568 $result['permissions'] = $resource->get_permissions($post);
569 $result['is_customized'] = $resource->is_customized($post);
570
571 return $result;
572 }
573
574 /**
575 * Prepare post type item for output
576 *
577 * @param AAM_Framework_AccessLevel_Interface $access_level
578 * @param WP_Post_Type $post_type
579 *
580 * @return array
581 * @access private
582 *
583 * @version 7.0.0
584 */
585 private function _prepare_post_type_output($access_level, $post_type)
586 {
587 $resource = $access_level->get_resource(
588 AAM_Framework_Type_Resource::POST_TYPE
589 );
590
591 return [
592 'slug' => $post_type->name,
593 'title' => $post_type->label,
594 'icon' => $post_type->menu_icon,
595 'is_hierarchical' => $post_type->hierarchical,
596 'permissions' => $resource->get_permissions($post_type),
597 'is_customized' => $resource->is_customized($post_type)
598 ];
599 }
600
601 /**
602 * Prepare taxonomy item for output
603 *
604 * @param AAM_Framework_AccessLevel_Interface $access_level
605 * @param \WP_Taxonomy $taxonomy
606 *
607 * @return array
608 * @access private
609 *
610 * @version 7.0.0
611 */
612 private function _prepare_taxonomy_output($access_level, $taxonomy)
613 {
614 $resource = $access_level->get_resource(
615 AAM_Framework_Type_Resource::TAXONOMY
616 );
617
618 return [
619 'slug' => $taxonomy->name,
620 'title' => $taxonomy->label,
621 'is_hierarchical' => $taxonomy->hierarchical,
622 'permissions' => $resource->get_permissions($taxonomy),
623 'is_customized' => $resource->is_customized($taxonomy),
624 'post_types' => array_values($taxonomy->object_type)
625 ];
626 }
627
628 /**
629 * Prepare term item for output
630 *
631 * @param AAM_Framework_AccessLevel_Interface $access_level
632 * @param WP_Term $term
633 *
634 * @return array
635 * @access private
636 *
637 * @version 7.0.5
638 */
639 private function _prepare_term_output($access_level, $term)
640 {
641 $resource = $access_level->get_resource(AAM_Framework_Type_Resource::TERM);
642 $result = [
643 'id' => $term->term_id,
644 'title' => $term->name,
645 'slug' => $term->slug,
646 'taxonomy' => $term->taxonomy,
647 'is_hierarchical' => get_taxonomy($term->taxonomy)->hierarchical,
648 'permissions' => $resource->get_permissions($term),
649 'is_customized' => $resource->is_customized($term)
650 ];
651
652 if (!empty($term->post_type)) {
653 $is_default = $term->taxonomy === 'category'
654 && intval(get_option('default_category')) === $term->term_id;
655
656 $result['is_default'] = apply_filters(
657 'aam_is_default_term_filter', $is_default, $term, $access_level
658 );
659
660 // Also adding post type scope to the output
661 $result['post_type'] = $term->post_type;
662 }
663
664 return $result;
665 }
666
667 }