[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/rest-api/endpoints/ -> class-wp-rest-menu-locations-controller.php (source)

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


Generated : Thu Nov 21 08:20:01 2024 Cross-referenced by PHPXref