| 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 |
|