| 1 |
<?php |
| 2 |
/** |
| 3 |
* REST API: WP_REST_Block_Navigation_Areas_Controller class |
| 4 |
* |
| 5 |
* @subpackage REST_API |
| 6 |
* @package WordPress |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Core class used to access block navigation areas via the REST API. |
| 11 |
* |
| 12 |
* @see WP_REST_Controller |
| 13 |
*/ |
| 14 |
class WP_REST_Block_Navigation_Areas_Controller extends WP_REST_Controller { |
| 15 |
|
| 16 |
/** |
| 17 |
* Constructor. |
| 18 |
*/ |
| 19 |
public function __construct() { |
| 20 |
$this->namespace = 'wp/v2'; |
| 21 |
$this->rest_base = 'block-navigation-areas'; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Registers the routes for the objects of the controller. |
| 26 |
* |
| 27 |
* @see register_rest_route() |
| 28 |
*/ |
| 29 |
public function register_routes() { |
| 30 |
register_rest_route( |
| 31 |
$this->namespace, |
| 32 |
'/' . $this->rest_base, |
| 33 |
array( |
| 34 |
array( |
| 35 |
'methods' => WP_REST_Server::READABLE, |
| 36 |
'callback' => array( $this, 'get_items' ), |
| 37 |
'permission_callback' => array( $this, 'get_items_permissions_check' ), |
| 38 |
'args' => $this->get_collection_params(), |
| 39 |
), |
| 40 |
'schema' => array( $this, 'get_public_item_schema' ), |
| 41 |
'allow_batch' => array( 'v1' => true ), |
| 42 |
) |
| 43 |
); |
| 44 |
|
| 45 |
register_rest_route( |
| 46 |
$this->namespace, |
| 47 |
'/' . $this->rest_base . '/(?P<area>[\w-]+)', |
| 48 |
array( |
| 49 |
'args' => array( |
| 50 |
'area' => array( |
| 51 |
'description' => __( 'An alphanumeric identifier for the navigation area.', 'gutenberg' ), |
| 52 |
'type' => 'string', |
| 53 |
), |
| 54 |
), |
| 55 |
array( |
| 56 |
'methods' => WP_REST_Server::READABLE, |
| 57 |
'callback' => array( $this, 'get_item' ), |
| 58 |
'permission_callback' => array( $this, 'get_item_permissions_check' ), |
| 59 |
'args' => array( |
| 60 |
'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
| 61 |
), |
| 62 |
), |
| 63 |
array( |
| 64 |
'methods' => WP_REST_Server::EDITABLE, |
| 65 |
'callback' => array( $this, 'update_item' ), |
| 66 |
'permission_callback' => array( $this, 'update_item_permissions_check' ), |
| 67 |
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
| 68 |
), |
| 69 |
'schema' => array( $this, 'get_public_item_schema' ), |
| 70 |
) |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Checks whether a given request has permission to read navigation areas. |
| 76 |
* |
| 77 |
* @param WP_REST_Request $request Full details about the request. |
| 78 |
* |
| 79 |
* @return WP_Error|bool True if the request has read access, WP_Error object otherwise. |
| 80 |
*/ |
| 81 |
public function get_items_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 82 |
if ( ! current_user_can( 'edit_theme_options' ) ) { |
| 83 |
return new WP_Error( |
| 84 |
'rest_cannot_view', |
| 85 |
__( 'Sorry, you are not allowed to view navigation areas.', 'gutenberg' ), |
| 86 |
array( 'status' => rest_authorization_required_code() ) |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
return true; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Retrieves all navigation areas, depending on user context. |
| 95 |
* |
| 96 |
* @param WP_REST_Request $request Full details about the request. |
| 97 |
* |
| 98 |
* @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure. |
| 99 |
*/ |
| 100 |
public function get_items( $request ) { |
| 101 |
$data = array(); |
| 102 |
foreach ( gutenberg_get_navigation_areas() as $name => $description ) { |
| 103 |
$area = $this->get_navigation_area_object( $name ); |
| 104 |
$area = $this->prepare_item_for_response( $area, $request ); |
| 105 |
$data[ $name ] = $this->prepare_response_for_collection( $area ); |
| 106 |
} |
| 107 |
return rest_ensure_response( $data ); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Checks if a given request has access to read a navigation area. |
| 112 |
* |
| 113 |
* @param WP_REST_Request $request Full details about the request. |
| 114 |
* |
| 115 |
* @return WP_Error|bool True if the request has read access for the item, WP_Error object otherwise. |
| 116 |
*/ |
| 117 |
public function get_item_permissions_check( $request ) { |
| 118 |
if ( ! current_user_can( 'edit_theme_options' ) ) { |
| 119 |
return new WP_Error( |
| 120 |
'rest_cannot_view', |
| 121 |
__( 'Sorry, you are not allowed to view navigation areas.', 'gutenberg' ), |
| 122 |
array( 'status' => rest_authorization_required_code() ) |
| 123 |
); |
| 124 |
} |
| 125 |
if ( ! array_key_exists( $request['area'], gutenberg_get_navigation_areas() ) ) { |
| 126 |
return new WP_Error( 'rest_navigation_area_invalid', __( 'Invalid navigation area.', 'gutenberg' ), array( 'status' => 404 ) ); |
| 127 |
} |
| 128 |
|
| 129 |
return true; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Checks if a request has access to update the specified term. |
| 134 |
* |
| 135 |
* @param WP_REST_Request $request Full details about the request. |
| 136 |
* |
| 137 |
* @return bool|WP_Error True if the request has access to update the item, false or WP_Error object otherwise. |
| 138 |
*/ |
| 139 |
public function update_item_permissions_check( $request ) { |
| 140 |
return $this->get_item_permissions_check( $request ); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Retrieves a specific navigation area. |
| 145 |
* |
| 146 |
* @param WP_REST_Request $request Full details about the request. |
| 147 |
* |
| 148 |
* @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure. |
| 149 |
*/ |
| 150 |
public function get_item( $request ) { |
| 151 |
$name = $request['area']; |
| 152 |
$area = $this->get_navigation_area_object( $name ); |
| 153 |
$data = $this->prepare_item_for_response( $area, $request ); |
| 154 |
|
| 155 |
return rest_ensure_response( $data ); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Updates a specific navigation area. |
| 160 |
* |
| 161 |
* @param WP_REST_Request $request Full details about the request. |
| 162 |
* |
| 163 |
* @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure. |
| 164 |
*/ |
| 165 |
public function update_item( $request ) { |
| 166 |
$name = $request['area']; |
| 167 |
|
| 168 |
$mapping = gutenberg_get_navigation_areas_menus(); |
| 169 |
$mapping[ $name ] = $request['navigation']; |
| 170 |
update_option( 'wp_navigation_areas', $mapping ); |
| 171 |
|
| 172 |
$area = $this->get_navigation_area_object( $name ); |
| 173 |
$data = $this->prepare_item_for_response( $area, $request ); |
| 174 |
return rest_ensure_response( $data ); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Converts navigation area name to a convenient object that this endpoint can reason about. |
| 179 |
* |
| 180 |
* @param string $name Navigation area name. |
| 181 |
* @return stdClass An object representation of the navigation area. |
| 182 |
*/ |
| 183 |
private function get_navigation_area_object( $name ) { |
| 184 |
$available_areas = gutenberg_get_navigation_areas(); |
| 185 |
$mapping = gutenberg_get_navigation_areas_menus(); |
| 186 |
$area = new stdClass(); |
| 187 |
$area->name = $name; |
| 188 |
$area->navigation = ! empty( $mapping[ $name ] ) ? $mapping[ $name ] : null; |
| 189 |
$area->description = $available_areas[ $name ]; |
| 190 |
return $area; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Prepares a navigation area object for serialization. |
| 195 |
* |
| 196 |
* @param stdClass $area Post status data. |
| 197 |
* @param WP_REST_Request $request Full details about the request. |
| 198 |
* |
| 199 |
* @return WP_REST_Response Post status data. |
| 200 |
*/ |
| 201 |
public function prepare_item_for_response( $area, $request ) { |
| 202 |
$areas = gutenberg_get_navigation_areas(); |
| 203 |
$navigation = ( isset( $areas[ $area->name ] ) ) ? $area->navigation : 0; |
| 204 |
|
| 205 |
$fields = $this->get_fields_for_response( $request ); |
| 206 |
$data = array(); |
| 207 |
|
| 208 |
if ( rest_is_field_included( 'name', $fields ) ) { |
| 209 |
$data['name'] = $area->name; |
| 210 |
} |
| 211 |
|
| 212 |
if ( rest_is_field_included( 'description', $fields ) ) { |
| 213 |
$data['description'] = $area->description; |
| 214 |
} |
| 215 |
|
| 216 |
if ( rest_is_field_included( 'navigation', $fields ) ) { |
| 217 |
$data['navigation'] = (int) $navigation; |
| 218 |
} |
| 219 |
|
| 220 |
$context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
| 221 |
$data = $this->add_additional_fields_to_object( $data, $request ); |
| 222 |
$data = $this->filter_response_by_context( $data, $context ); |
| 223 |
|
| 224 |
$response = rest_ensure_response( $data ); |
| 225 |
|
| 226 |
/** |
| 227 |
* Filters a navigation area returned from the REST API. |
| 228 |
* |
| 229 |
* Allows modification of the navigation area data right before it is |
| 230 |
* returned. |
| 231 |
* |
| 232 |
* @param WP_REST_Response $response The response object. |
| 233 |
* @param object $area The original status object. |
| 234 |
* @param WP_REST_Request $request Request used to generate the response. |
| 235 |
*/ |
| 236 |
return apply_filters( 'rest_prepare_navigation_area', $response, $area, $request ); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Retrieves the navigation area's schema, conforming to JSON Schema. |
| 241 |
* |
| 242 |
* @return array Item schema data. |
| 243 |
*/ |
| 244 |
public function get_item_schema() { |
| 245 |
$schema = array( |
| 246 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 247 |
'title' => 'navigation-area', |
| 248 |
'type' => 'object', |
| 249 |
'properties' => array( |
| 250 |
'name' => array( |
| 251 |
'description' => __( 'The name of the navigation area.', 'gutenberg' ), |
| 252 |
'type' => 'string', |
| 253 |
'context' => array( 'embed', 'view', 'edit' ), |
| 254 |
'readonly' => true, |
| 255 |
), |
| 256 |
'description' => array( |
| 257 |
'description' => __( 'The description of the navigation area.', 'gutenberg' ), |
| 258 |
'type' => 'string', |
| 259 |
'context' => array( 'embed', 'view', 'edit' ), |
| 260 |
'readonly' => true, |
| 261 |
), |
| 262 |
'navigation' => array( |
| 263 |
'description' => __( 'The ID of the assigned navigation.', 'gutenberg' ), |
| 264 |
'type' => 'integer', |
| 265 |
'context' => array( 'embed', 'view', 'edit' ), |
| 266 |
'readonly' => true, |
| 267 |
), |
| 268 |
), |
| 269 |
); |
| 270 |
|
| 271 |
return $this->add_additional_fields_schema( $schema ); |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Retrieves the query params for collections. |
| 276 |
* |
| 277 |
* @return array Collection parameters. |
| 278 |
*/ |
| 279 |
public function get_collection_params() { |
| 280 |
return array( |
| 281 |
'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
| 282 |
); |
| 283 |
} |
| 284 |
|
| 285 |
} |
| 286 |
|