WP_REST_Menu_Items_Controller
- Since
- 5.9.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php:17
Core class to access nav items via the REST API.
Compatibility
- WordPress
- since 5.9.0
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0).
Hooks and filters fired · 8
Every hook that fires from inside WP_REST_Menu_Items_Controller, in the order it appears in the class, grouped by the method that fires it.
WP_REST_Menu_Items_Controller::create_item()
- do_action( rest_insert_nav_menu_item )action line 151
- do_action( rest_after_insert_nav_menu_item )action line 181
WP_REST_Menu_Items_Controller::update_item()
- do_action( rest_insert_nav_menu_item )action line 237
- do_action( rest_after_insert_nav_menu_item )action line 260
Methods · 15
- get_nav_menu_item()Gets the nav menu item, if the ID is valid.
- get_items_permissions_check()Checks if a given request has access to read menu items.
- get_item_permissions_check()Checks if a given request has access to read a menu item if they have access to edit them.
- check_has_read_only_access()Checks whether the current user has read permission for the endpoint.
- create_item()Creates a single nav menu item.
- update_item()Updates a single nav menu item.
- delete_item()Deletes a single nav menu item.
- prepare_item_for_database()Prepares a single nav menu item for create or update.
- prepare_item_for_response()Prepares a single nav menu item output for response.
- prepare_links()Prepares links for the request.
- get_schema_links()Retrieves Link Description Objects that should be added to the Schema for the nav menu items collection.
- get_item_schema()Retrieves the nav menu item's schema, conforming to JSON Schema.
- get_collection_params()Retrieves the query params for the nav menu items collection.
- prepare_items_query()Determines the allowed query_vars for a get_items() response and prepares them for WP_Query.
- get_menu_id()Gets the id of the menu that the given menu item belongs to.
Source code
class WP_REST_Menu_Items_Controller extends WP_REST_Posts_Controller { /** * Gets the nav menu item, if the ID is valid. * * @since 5.9.0 * * @param int $id Supplied ID. * @return object|WP_Error Post object if ID is valid, WP_Error otherwise. */ protected function get_nav_menu_item( $id ) { $post = $this->get_post( $id ); if ( is_wp_error( $post ) ) { return $post; } return wp_setup_nav_menu_item( $post ); } /** * Checks if a given request has access to read menu items. * * @since 5.9.0 * * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True if the request has read access, WP_Error object otherwise. */ public function get_items_permissions_check( $request ) { $has_permission = parent::get_items_permissions_check( $request ); if ( true !== $has_permission ) { return $has_permission; } return $this->check_has_read_only_access( $request ); } /** * Checks if a given request has access to read a menu item if they have access to edit them. * * @since 5.9.0 * * @param WP_REST_Request $request Full details about the request. * @return bool|WP_Error True if the request has read access for the item, WP_Error object or false otherwise. */ public function get_item_permissions_check( $request ) { $permission_check = parent::get_item_permissions_check( $request ); if ( true !== $permission_check ) { return $permission_check; } return $this->check_has_read_only_access( $request ); } /** * Checks whether the current user has read permission for the endpoint. * * This allows for any user that can `edit_theme_options` or edit any REST API available post type. * * @since 5.9.0 * * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise. */ protected function check_has_read_only_access( $request ) { if ( current_user_can( 'edit_theme_options' ) ) { return true; } if ( current_user_can( 'edit_posts' ) ) { return true; } foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) { if ( current_user_can( $post_type->cap->edit_posts ) ) { return true; } }Changelog
Introduced in 5.9.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 tag, from
src/wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.