| 1 |
<?php |
| 2 |
/** |
| 3 |
* Guideline Scopes REST API Controller. |
| 4 |
* |
| 5 |
* Read-only controller exposing the `wp_guideline_scopes()` registry at |
| 6 |
* `/wp/v2/knowledge/guideline-scopes`. This is a registry endpoint beside the |
| 7 |
* data routes (the same species as `/wp/v2/statuses`): it has no write paths |
| 8 |
* and carries no data semantics. The Settings → Guidelines page preloads it and |
| 9 |
* reads/writes the scope rows through the standard `/wp/v2/knowledge` collection. |
| 10 |
* |
| 11 |
* @package gutenberg |
| 12 |
*/ |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* REST controller for the guideline scopes registry. |
| 20 |
*/ |
| 21 |
class Gutenberg_Guideline_Scopes_REST_Controller extends WP_REST_Controller { |
| 22 |
|
| 23 |
/** |
| 24 |
* Constructor. |
| 25 |
*/ |
| 26 |
public function __construct() { |
| 27 |
$this->namespace = 'wp/v2'; |
| 28 |
$this->rest_base = 'knowledge/guideline-scopes'; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Registers the routes for the controller. |
| 33 |
*/ |
| 34 |
public function register_routes() { |
| 35 |
register_rest_route( |
| 36 |
$this->namespace, |
| 37 |
'/' . $this->rest_base, |
| 38 |
array( |
| 39 |
array( |
| 40 |
'methods' => WP_REST_Server::READABLE, |
| 41 |
'callback' => array( $this, 'get_items' ), |
| 42 |
'permission_callback' => array( $this, 'get_items_permissions_check' ), |
| 43 |
), |
| 44 |
'schema' => array( $this, 'get_public_item_schema' ), |
| 45 |
) |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Checks whether the current user can read guideline scopes. |
| 51 |
* |
| 52 |
* Gated on the knowledge read capability, matching the data routes. |
| 53 |
* |
| 54 |
* @param WP_REST_Request $request Full details about the request. |
| 55 |
* @return true|WP_Error True if the request has read access, WP_Error otherwise. |
| 56 |
*/ |
| 57 |
public function get_items_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 58 |
if ( ! current_user_can( 'read_knowledge_items' ) ) { |
| 59 |
return new WP_Error( |
| 60 |
'rest_cannot_read', |
| 61 |
__( 'Sorry, you are not allowed to view guideline scopes.', 'gutenberg' ), |
| 62 |
array( 'status' => rest_authorization_required_code() ) |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
return true; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Retrieves all registered guideline scopes. |
| 71 |
* |
| 72 |
* Labels are resolved at request time (in the request locale). |
| 73 |
* |
| 74 |
* @param WP_REST_Request $request Full details about the request. |
| 75 |
* @return WP_REST_Response Response object. |
| 76 |
*/ |
| 77 |
public function get_items( $request ) { |
| 78 |
$data = array(); |
| 79 |
|
| 80 |
foreach ( wp_guideline_scopes() as $slug => $scope ) { |
| 81 |
$item = $this->prepare_item_for_response( array_merge( array( 'slug' => $slug ), $scope ), $request ); |
| 82 |
$data[] = $this->prepare_response_for_collection( $item ); |
| 83 |
} |
| 84 |
|
| 85 |
return rest_ensure_response( $data ); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Prepares a single scope for response. |
| 90 |
* |
| 91 |
* @param array $item Scope data with a `slug` key. |
| 92 |
* @param WP_REST_Request $request Request object. |
| 93 |
* @return WP_REST_Response Response object. |
| 94 |
*/ |
| 95 |
public function prepare_item_for_response( $item, $request ) { |
| 96 |
$fields = $this->get_fields_for_response( $request ); |
| 97 |
$data = array(); |
| 98 |
|
| 99 |
if ( rest_is_field_included( 'slug', $fields ) ) { |
| 100 |
$data['slug'] = $item['slug']; |
| 101 |
} |
| 102 |
if ( rest_is_field_included( 'title', $fields ) ) { |
| 103 |
$data['title'] = $item['title'] ?? ''; |
| 104 |
} |
| 105 |
if ( rest_is_field_included( 'description', $fields ) ) { |
| 106 |
$data['description'] = $item['description'] ?? ''; |
| 107 |
} |
| 108 |
if ( rest_is_field_included( 'order', $fields ) ) { |
| 109 |
$data['order'] = (int) ( $item['order'] ?? 0 ); |
| 110 |
} |
| 111 |
|
| 112 |
$context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
| 113 |
$data = $this->add_additional_fields_to_object( $data, $request ); |
| 114 |
$data = $this->filter_response_by_context( $data, $context ); |
| 115 |
|
| 116 |
return rest_ensure_response( $data ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Retrieves the scope schema, conforming to JSON Schema. |
| 121 |
* |
| 122 |
* @return array Item schema data. |
| 123 |
*/ |
| 124 |
public function get_item_schema() { |
| 125 |
if ( $this->schema ) { |
| 126 |
return $this->add_additional_fields_schema( $this->schema ); |
| 127 |
} |
| 128 |
|
| 129 |
$this->schema = array( |
| 130 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 131 |
'title' => 'guideline-scope', |
| 132 |
'type' => 'object', |
| 133 |
'properties' => array( |
| 134 |
'slug' => array( |
| 135 |
'description' => __( 'An alphanumeric identifier for the scope.', 'gutenberg' ), |
| 136 |
'type' => 'string', |
| 137 |
'context' => array( 'view', 'edit', 'embed' ), |
| 138 |
'readonly' => true, |
| 139 |
), |
| 140 |
'title' => array( |
| 141 |
'description' => __( 'The title for the scope.', 'gutenberg' ), |
| 142 |
'type' => 'string', |
| 143 |
'context' => array( 'view', 'edit', 'embed' ), |
| 144 |
'readonly' => true, |
| 145 |
), |
| 146 |
'description' => array( |
| 147 |
'description' => __( 'A human-readable description of the scope.', 'gutenberg' ), |
| 148 |
'type' => 'string', |
| 149 |
'context' => array( 'view', 'edit', 'embed' ), |
| 150 |
'readonly' => true, |
| 151 |
), |
| 152 |
'order' => array( |
| 153 |
'description' => __( 'The sort order of the scope on the Settings page.', 'gutenberg' ), |
| 154 |
'type' => 'integer', |
| 155 |
'context' => array( 'view', 'edit', 'embed' ), |
| 156 |
'readonly' => true, |
| 157 |
), |
| 158 |
), |
| 159 |
); |
| 160 |
|
| 161 |
return $this->add_additional_fields_schema( $this->schema ); |
| 162 |
} |
| 163 |
} |
| 164 |
|