PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / trunk
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO vtrunk
2.6.0 2.5.0 2.4.0 2.3.0 2.2.0 2.1.1 2.1.0 2.0.2 2.0.1 2.0.0 1.32.0 1.31.0 1.30.0 1.29.0 1.28.0 1.27.0 1.26.0 1.25.0 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.10.0 1.11.0 All 47 releases
thinkrank / includes / api / class-role-manager-endpoint.php

class-role-manager-endpoint.php in ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO trunk, at includes/api/class-role-manager-endpoint.php

116 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace ThinkRank\API;
6
7 use ThinkRank\Core\Capability_Manager;
8 use WP_REST_Request;
9 use WP_REST_Response;
10
11 // Prevent direct access
12 if (!defined('ABSPATH')) {
13 exit;
14 }
15
16 /**
17 * Role Manager Endpoint
18 *
19 * REST API for the role × capability matrix: read the roles, capabilities and
20 * current assignments, and save updated assignments.
21 *
22 * @since 1.12.0
23 */
24 class Role_Manager_Endpoint {
25
26 /**
27 * REST namespace.
28 *
29 * @var string
30 */
31 private string $namespace = 'thinkrank/v1';
32
33 /**
34 * Permission callback — only those who can manage roles.
35 *
36 * @return bool
37 */
38 public function check_permissions(): bool {
39 return Capability_Manager::current_user_can(Capability_Manager::MANAGE_ROLES);
40 }
41
42 /**
43 * Register routes.
44 *
45 * @return void
46 */
47 public function register_routes(): void {
48 register_rest_route($this->namespace, '/role-manager', [
49 [
50 'methods' => 'GET',
51 'callback' => [$this, 'get_data'],
52 'permission_callback' => [$this, 'check_permissions'],
53 ],
54 [
55 'methods' => 'POST',
56 'callback' => [$this, 'save_matrix'],
57 'permission_callback' => [$this, 'check_permissions'],
58 'args' => [
59 'matrix' => ['type' => 'object', 'required' => true],
60 ],
61 ],
62 ]);
63 }
64
65 /**
66 * Return roles, capabilities, and the current matrix.
67 *
68 * @return WP_REST_Response
69 */
70 public function get_data(): WP_REST_Response {
71 $roles = [];
72 foreach (Capability_Manager::editable_roles() as $slug => $name) {
73 $roles[] = [
74 'slug' => $slug,
75 'name' => $name,
76 // False when the role lacks the WordPress capability ThinkRank's
77 // endpoints require on top of the section gate — the grant would
78 // save and then do nothing (#576).
79 'can_use' => Capability_Manager::role_meets_baseline($slug),
80 ];
81 }
82
83 $capabilities = [];
84 foreach (Capability_Manager::capabilities() as $key => $label) {
85 $capabilities[] = ['key' => $key, 'label' => $label];
86 }
87
88 return new WP_REST_Response([
89 'success' => true,
90 'data' => [
91 'roles' => $roles,
92 'capabilities' => $capabilities,
93 'matrix' => Capability_Manager::get_matrix(),
94 'base_cap' => Capability_Manager::ACCESS,
95 'baseline_cap' => Capability_Manager::BASELINE_CAPABILITY,
96 ],
97 ], 200);
98 }
99
100 /**
101 * Save the matrix.
102 *
103 * @param WP_REST_Request $request Request.
104 * @return WP_REST_Response
105 */
106 public function save_matrix(WP_REST_Request $request): WP_REST_Response {
107 $matrix = (array) $request->get_param('matrix');
108 Capability_Manager::save_matrix($matrix);
109
110 return new WP_REST_Response([
111 'success' => true,
112 'data' => ['matrix' => Capability_Manager::get_matrix()],
113 ], 200);
114 }
115 }
116