equal
deleted
inserted
replaced
118 * Checks if a given request has access to read a menu location. |
118 * Checks if a given request has access to read a menu location. |
119 * |
119 * |
120 * @since 5.9.0 |
120 * @since 5.9.0 |
121 * |
121 * |
122 * @param WP_REST_Request $request Full details about the request. |
122 * @param WP_REST_Request $request Full details about the request. |
123 * @return WP_Error|bool True if the request has read access for the item, WP_Error object otherwise. |
123 * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise. |
124 */ |
124 */ |
125 public function get_item_permissions_check( $request ) { |
125 public function get_item_permissions_check( $request ) { |
126 if ( ! current_user_can( 'edit_theme_options' ) ) { |
126 if ( ! current_user_can( 'edit_theme_options' ) ) { |
127 return new WP_Error( |
127 return new WP_Error( |
128 'rest_cannot_view', |
128 'rest_cannot_view', |
166 * @param WP_REST_Request $request Full details about the request. |
166 * @param WP_REST_Request $request Full details about the request. |
167 * @return WP_REST_Response Menu location data. |
167 * @return WP_REST_Response Menu location data. |
168 */ |
168 */ |
169 public function prepare_item_for_response( $item, $request ) { |
169 public function prepare_item_for_response( $item, $request ) { |
170 // Restores the more descriptive, specific name for use within this method. |
170 // Restores the more descriptive, specific name for use within this method. |
171 $location = $item; |
171 $location = $item; |
|
172 |
172 $locations = get_nav_menu_locations(); |
173 $locations = get_nav_menu_locations(); |
173 $menu = isset( $locations[ $location->name ] ) ? $locations[ $location->name ] : 0; |
174 $menu = isset( $locations[ $location->name ] ) ? $locations[ $location->name ] : 0; |
174 |
175 |
175 $fields = $this->get_fields_for_response( $request ); |
176 $fields = $this->get_fields_for_response( $request ); |
176 $data = array(); |
177 $data = array(); |
191 $data = $this->add_additional_fields_to_object( $data, $request ); |
192 $data = $this->add_additional_fields_to_object( $data, $request ); |
192 $data = $this->filter_response_by_context( $data, $context ); |
193 $data = $this->filter_response_by_context( $data, $context ); |
193 |
194 |
194 $response = rest_ensure_response( $data ); |
195 $response = rest_ensure_response( $data ); |
195 |
196 |
196 $response->add_links( $this->prepare_links( $location ) ); |
197 if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) { |
|
198 $response->add_links( $this->prepare_links( $location ) ); |
|
199 } |
197 |
200 |
198 /** |
201 /** |
199 * Filters menu location data returned from the REST API. |
202 * Filters menu location data returned from the REST API. |
200 * |
203 * |
201 * @since 5.9.0 |
204 * @since 5.9.0 |
206 */ |
209 */ |
207 return apply_filters( 'rest_prepare_menu_location', $response, $location, $request ); |
210 return apply_filters( 'rest_prepare_menu_location', $response, $location, $request ); |
208 } |
211 } |
209 |
212 |
210 /** |
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 /** |
211 * Retrieves the menu location's schema, conforming to JSON Schema. |
252 * Retrieves the menu location's schema, conforming to JSON Schema. |
212 * |
253 * |
213 * @since 5.9.0 |
254 * @since 5.9.0 |
214 * |
255 * |
215 * @return array Item schema data. |
256 * @return array Item schema data. |
258 public function get_collection_params() { |
299 public function get_collection_params() { |
259 return array( |
300 return array( |
260 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
301 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
261 ); |
302 ); |
262 } |
303 } |
263 |
|
264 /** |
|
265 * Prepares links for the request. |
|
266 * |
|
267 * @since 5.9.0 |
|
268 * |
|
269 * @param stdClass $location Menu location. |
|
270 * @return array Links for the given menu location. |
|
271 */ |
|
272 protected function prepare_links( $location ) { |
|
273 $base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); |
|
274 |
|
275 // Entity meta. |
|
276 $links = array( |
|
277 'self' => array( |
|
278 'href' => rest_url( trailingslashit( $base ) . $location->name ), |
|
279 ), |
|
280 'collection' => array( |
|
281 'href' => rest_url( $base ), |
|
282 ), |
|
283 ); |
|
284 |
|
285 $locations = get_nav_menu_locations(); |
|
286 $menu = isset( $locations[ $location->name ] ) ? $locations[ $location->name ] : 0; |
|
287 if ( $menu ) { |
|
288 $path = rest_get_route_for_term( $menu ); |
|
289 if ( $path ) { |
|
290 $url = rest_url( $path ); |
|
291 |
|
292 $links['https://api.w.org/menu'][] = array( |
|
293 'href' => $url, |
|
294 'embeddable' => true, |
|
295 ); |
|
296 } |
|
297 } |
|
298 |
|
299 return $links; |
|
300 } |
|
301 } |
304 } |