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