mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
REST API: Introduce Menu management endpoints.
This commit introduces the `/wp/v2/menus`, `/wp/v2/menu-items` and `/wp/v2/menu-locations` REST API endpoints. These endpoints are fully available to users with the `edit_theme_options` capability, but can be read by any user who can edit a REST API available post type. The `nav_menu` taxonomy and `nav_menu_item` post type now map their capabilities to the `edit_theme_options` primitive capability. This allows developers to provide more fine-grained access control. However, if a developer is currently dynamically removing the `edit_theme_options` capability using `map_meta_cap`, they should use the `user_has_cap` filter instead. The `wp_update_nav_menu_item()` function has been adjusted to return an error if saving the menu item post or assigning the menu item to a menu generate an error. Lastly, a new menu item type is introduced, `block`, that can be used to store a Block as a menu item. Props andraganescu, antonvlasenko, dingo_d, dlh, isabel_brison, kadamwhite, Mamaduka, NateWr, noisysocks, peterwilsoncc, ryelle, schlessera, soean, Spacedmonkey, talldanwp, TimothyBlynJacobs, tobifjellner, westonruter, wpscholar, zieladam. Fixes #40878. git-svn-id: https://develop.svn.wordpress.org/trunk@52079 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
de9a1e6f94
commit
aae0000cef
@ -406,10 +406,11 @@ function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) {
|
||||
/**
|
||||
* Save the properties of a menu item or create a new one.
|
||||
*
|
||||
* The menu-item-title, menu-item-description, and menu-item-attr-title are expected
|
||||
* to be pre-slashed since they are passed directly into `wp_insert_post()`.
|
||||
* The menu-item-title, menu-item-description, menu-item-attr-title, and menu-item-content are expected
|
||||
* to be pre-slashed since they are passed directly to APIs that expect slashed data.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @since 5.9.0 Added the menu-item-content parameter.
|
||||
*
|
||||
* @param int $menu_id The ID of the menu. Required. If "0", makes the menu item a draft orphan.
|
||||
* @param int $menu_item_db_id The ID of the menu item. If "0", creates a new menu item.
|
||||
@ -448,6 +449,7 @@ function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item
|
||||
'menu-item-attr-title' => '',
|
||||
'menu-item-target' => '',
|
||||
'menu-item-classes' => '',
|
||||
'menu-item-content' => '',
|
||||
'menu-item-xfn' => '',
|
||||
'menu-item-status' => '',
|
||||
'menu-item-post-date' => '',
|
||||
@ -526,7 +528,7 @@ function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item
|
||||
if ( ! $update ) {
|
||||
$post['ID'] = 0;
|
||||
$post['post_status'] = 'publish' === $args['menu-item-status'] ? 'publish' : 'draft';
|
||||
$menu_item_db_id = wp_insert_post( $post );
|
||||
$menu_item_db_id = wp_insert_post( $post, true );
|
||||
if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) ) {
|
||||
return $menu_item_db_id;
|
||||
}
|
||||
@ -548,7 +550,10 @@ function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item
|
||||
// Associate the menu item with the menu term.
|
||||
// Only set the menu term if it isn't set to avoid unnecessary wp_get_object_terms().
|
||||
if ( $menu_id && ( ! $update || ! is_object_in_term( $menu_item_db_id, 'nav_menu', (int) $menu->term_id ) ) ) {
|
||||
wp_set_object_terms( $menu_item_db_id, array( $menu->term_id ), 'nav_menu' );
|
||||
$update_terms = wp_set_object_terms( $menu_item_db_id, array( $menu->term_id ), 'nav_menu' );
|
||||
if ( is_wp_error( $update_terms ) ) {
|
||||
return $update_terms;
|
||||
}
|
||||
}
|
||||
|
||||
if ( 'custom' === $args['menu-item-type'] ) {
|
||||
@ -569,6 +574,7 @@ function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item
|
||||
update_post_meta( $menu_item_db_id, '_menu_item_classes', $args['menu-item-classes'] );
|
||||
update_post_meta( $menu_item_db_id, '_menu_item_xfn', $args['menu-item-xfn'] );
|
||||
update_post_meta( $menu_item_db_id, '_menu_item_url', esc_url_raw( $args['menu-item-url'] ) );
|
||||
update_post_meta( $menu_item_db_id, '_menu_item_content', $args['menu-item-content'] );
|
||||
|
||||
if ( 0 == $menu_id ) {
|
||||
update_post_meta( $menu_item_db_id, '_menu_item_orphaned', (string) time() );
|
||||
@ -580,7 +586,11 @@ function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item
|
||||
if ( $update ) {
|
||||
$post['ID'] = $menu_item_db_id;
|
||||
$post['post_status'] = ( 'draft' === $args['menu-item-status'] ) ? 'draft' : 'publish';
|
||||
wp_update_post( $post );
|
||||
|
||||
$update_post = wp_update_post( $post, true );
|
||||
if ( is_wp_error( $update_post ) ) {
|
||||
return $update_post;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -903,6 +913,10 @@ function wp_setup_nav_menu_item( $menu_item ) {
|
||||
|
||||
$menu_item->title = ( '' === $menu_item->post_title ) ? $original_title : $menu_item->post_title;
|
||||
|
||||
} elseif ( 'block' === $menu_item->type ) {
|
||||
$menu_item->type_label = __( 'Block' );
|
||||
$menu_item->title = $menu_item->post_title;
|
||||
$menu_item->menu_item_content = ! isset( $menu_item->menu_item_content ) ? get_post_meta( $menu_item->ID, '_menu_item_content', true ) : $menu_item->menu_item_content;
|
||||
} else {
|
||||
$menu_item->type_label = __( 'Custom Link' );
|
||||
$menu_item->title = $menu_item->post_title;
|
||||
|
||||
@ -126,16 +126,39 @@ function create_initial_post_types() {
|
||||
register_post_type(
|
||||
'nav_menu_item',
|
||||
array(
|
||||
'labels' => array(
|
||||
'labels' => array(
|
||||
'name' => __( 'Navigation Menu Items' ),
|
||||
'singular_name' => __( 'Navigation Menu Item' ),
|
||||
),
|
||||
'public' => false,
|
||||
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
|
||||
'hierarchical' => false,
|
||||
'rewrite' => false,
|
||||
'delete_with_user' => false,
|
||||
'query_var' => false,
|
||||
'public' => false,
|
||||
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
|
||||
'hierarchical' => false,
|
||||
'rewrite' => false,
|
||||
'delete_with_user' => false,
|
||||
'query_var' => false,
|
||||
'map_meta_cap' => true,
|
||||
'capability_type' => array( 'edit_theme_options', 'edit_theme_options' ),
|
||||
'capabilities' => array(
|
||||
// Meta Capabilities.
|
||||
'edit_post' => 'edit_post',
|
||||
'read_post' => 'read_post',
|
||||
'delete_post' => 'delete_post',
|
||||
// Primitive Capabilities.
|
||||
'edit_posts' => 'edit_theme_options',
|
||||
'edit_others_posts' => 'edit_theme_options',
|
||||
'delete_posts' => 'edit_theme_options',
|
||||
'publish_posts' => 'edit_theme_options',
|
||||
'read_private_posts' => 'edit_theme_options',
|
||||
'read' => 'read',
|
||||
'delete_private_posts' => 'edit_theme_options',
|
||||
'delete_published_posts' => 'edit_theme_options',
|
||||
'delete_others_posts' => 'edit_theme_options',
|
||||
'edit_private_posts' => 'edit_theme_options',
|
||||
'edit_published_posts' => 'edit_theme_options',
|
||||
),
|
||||
'show_in_rest' => true,
|
||||
'rest_base' => 'menu-items',
|
||||
'rest_controller_class' => 'WP_REST_Menu_Items_Controller',
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
@ -345,6 +345,10 @@ function create_initial_rest_routes() {
|
||||
// URL Details.
|
||||
$controller = new WP_REST_URL_Details_Controller();
|
||||
$controller->register_routes();
|
||||
|
||||
// Menu Locations.
|
||||
$controller = new WP_REST_Menu_Locations_Controller();
|
||||
$controller->register_routes();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,301 @@
|
||||
<?php
|
||||
/**
|
||||
* REST API: WP_REST_Menu_Locations_Controller class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage REST_API
|
||||
* @since 5.9.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Core class used to access menu locations via the REST API.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @see WP_REST_Controller
|
||||
*/
|
||||
class WP_REST_Menu_Locations_Controller extends WP_REST_Controller {
|
||||
|
||||
/**
|
||||
* Menu Locations Constructor.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->namespace = 'wp/v2';
|
||||
$this->rest_base = 'menu-locations';
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the routes for the objects of the controller.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @see register_rest_route()
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route(
|
||||
$this->namespace,
|
||||
'/' . $this->rest_base,
|
||||
array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
)
|
||||
);
|
||||
|
||||
register_rest_route(
|
||||
$this->namespace,
|
||||
'/' . $this->rest_base . '/(?P<location>[\w-]+)',
|
||||
array(
|
||||
'args' => array(
|
||||
'location' => array(
|
||||
'description' => __( 'An alphanumeric identifier for the menu location.' ),
|
||||
'type' => 'string',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_item' ),
|
||||
'permission_callback' => array( $this, 'get_item_permissions_check' ),
|
||||
'args' => array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a given request has permission to read menu locations.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|bool True if the request has read access, WP_Error object otherwise.
|
||||
*/
|
||||
public function get_items_permissions_check( $request ) {
|
||||
if ( ! current_user_can( 'edit_theme_options' ) ) {
|
||||
return new WP_Error(
|
||||
'rest_cannot_view',
|
||||
__( 'Sorry, you are not allowed to view menu locations.' ),
|
||||
array( 'status' => rest_authorization_required_code() )
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all menu locations, depending on user context.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
$data = array();
|
||||
|
||||
foreach ( get_registered_nav_menus() as $name => $description ) {
|
||||
$location = new stdClass();
|
||||
$location->name = $name;
|
||||
$location->description = $description;
|
||||
|
||||
$location = $this->prepare_item_for_response( $location, $request );
|
||||
$data[ $name ] = $this->prepare_response_for_collection( $location );
|
||||
}
|
||||
|
||||
return rest_ensure_response( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a given request has access to read a menu location.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|bool True if the request has read access for the item, WP_Error object otherwise.
|
||||
*/
|
||||
public function get_item_permissions_check( $request ) {
|
||||
if ( ! current_user_can( 'edit_theme_options' ) ) {
|
||||
return new WP_Error(
|
||||
'rest_cannot_view',
|
||||
__( 'Sorry, you are not allowed to view menu locations.' ),
|
||||
array( 'status' => rest_authorization_required_code() )
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a specific menu location.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
|
||||
*/
|
||||
public function get_item( $request ) {
|
||||
$registered_menus = get_registered_nav_menus();
|
||||
if ( ! array_key_exists( $request['location'], $registered_menus ) ) {
|
||||
return new WP_Error( 'rest_menu_location_invalid', __( 'Invalid menu location.' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$location = new stdClass();
|
||||
$location->name = $request['location'];
|
||||
$location->description = $registered_menus[ $location->name ];
|
||||
|
||||
$data = $this->prepare_item_for_response( $location, $request );
|
||||
|
||||
return rest_ensure_response( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares a menu location object for serialization.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @param stdClass $item Post status data.
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_REST_Response Menu location data.
|
||||
*/
|
||||
public function prepare_item_for_response( $item, $request ) {
|
||||
// Restores the more descriptive, specific name for use within this method.
|
||||
$location = $item;
|
||||
$locations = get_nav_menu_locations();
|
||||
$menu = isset( $locations[ $location->name ] ) ? $locations[ $location->name ] : 0;
|
||||
|
||||
$fields = $this->get_fields_for_response( $request );
|
||||
$data = array();
|
||||
|
||||
if ( rest_is_field_included( 'name', $fields ) ) {
|
||||
$data['name'] = $location->name;
|
||||
}
|
||||
|
||||
if ( rest_is_field_included( 'description', $fields ) ) {
|
||||
$data['description'] = $location->description;
|
||||
}
|
||||
|
||||
if ( rest_is_field_included( 'menu', $fields ) ) {
|
||||
$data['menu'] = (int) $menu;
|
||||
}
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
$response = rest_ensure_response( $data );
|
||||
|
||||
$response->add_links( $this->prepare_links( $location ) );
|
||||
|
||||
/**
|
||||
* Filters menu location data returned from the REST API.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param object $location The original location object.
|
||||
* @param WP_REST_Request $request Request used to generate the response.
|
||||
*/
|
||||
return apply_filters( 'rest_prepare_menu_location', $response, $location, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the menu location's schema, conforming to JSON Schema.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @return array Item schema data.
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
if ( $this->schema ) {
|
||||
return $this->add_additional_fields_schema( $this->schema );
|
||||
}
|
||||
|
||||
$this->schema = array(
|
||||
'$schema' => 'http://json-schema.org/draft-04/schema#',
|
||||
'title' => 'menu-location',
|
||||
'type' => 'object',
|
||||
'properties' => array(
|
||||
'name' => array(
|
||||
'description' => __( 'The name of the menu location.' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'embed', 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'description' => array(
|
||||
'description' => __( 'The description of the menu location.' ),
|
||||
'type' => 'string',
|
||||
'context' => array( 'embed', 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
'menu' => array(
|
||||
'description' => __( 'The ID of the assigned menu.' ),
|
||||
'type' => 'integer',
|
||||
'context' => array( 'embed', 'view', 'edit' ),
|
||||
'readonly' => true,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_additional_fields_schema( $this->schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the query params for collections.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @return array Collection parameters.
|
||||
*/
|
||||
public function get_collection_params() {
|
||||
return array(
|
||||
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares links for the request.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @param stdClass $location Menu location.
|
||||
* @return array Links for the given menu location.
|
||||
*/
|
||||
protected function prepare_links( $location ) {
|
||||
$base = sprintf( '%s/%s', $this->namespace, $this->rest_base );
|
||||
|
||||
// Entity meta.
|
||||
$links = array(
|
||||
'self' => array(
|
||||
'href' => rest_url( trailingslashit( $base ) . $location->name ),
|
||||
),
|
||||
'collection' => array(
|
||||
'href' => rest_url( $base ),
|
||||
),
|
||||
);
|
||||
|
||||
$locations = get_nav_menu_locations();
|
||||
$menu = isset( $locations[ $location->name ] ) ? $locations[ $location->name ] : 0;
|
||||
if ( $menu ) {
|
||||
$path = rest_get_route_for_term( $menu );
|
||||
if ( $path ) {
|
||||
$url = rest_url( $path );
|
||||
|
||||
$links['https://api.w.org/menu'][] = array(
|
||||
'href' => $url,
|
||||
'embeddable' => true,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $links;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,568 @@
|
||||
<?php
|
||||
/**
|
||||
* REST API: WP_REST_Menus_Controller class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage REST_API
|
||||
* @since 5.9.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Core class used to managed menu terms associated via the REST API.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @see WP_REST_Controller
|
||||
*/
|
||||
class WP_REST_Menus_Controller extends WP_REST_Terms_Controller {
|
||||
|
||||
/**
|
||||
* Checks if a request has access to read menus.
|
||||
*
|
||||
* @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, otherwise false or WP_Error object.
|
||||
*/
|
||||
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 request has access to read or edit the specified menu.
|
||||
*
|
||||
* @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, otherwise false or WP_Error object.
|
||||
*/
|
||||
public function get_item_permissions_check( $request ) {
|
||||
$has_permission = parent::get_item_permissions_check( $request );
|
||||
|
||||
if ( true !== $has_permission ) {
|
||||
return $has_permission;
|
||||
}
|
||||
|
||||
return $this->check_has_read_only_access( $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the term, if the ID is valid.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @param int $id Supplied ID.
|
||||
* @return WP_Term|WP_Error Term object if ID is valid, WP_Error otherwise.
|
||||
*/
|
||||
protected function get_term( $id ) {
|
||||
$term = parent::get_term( $id );
|
||||
|
||||
if ( is_wp_error( $term ) ) {
|
||||
return $term;
|
||||
}
|
||||
|
||||
$nav_term = wp_get_nav_menu_object( $term );
|
||||
$nav_term->auto_add = $this->get_menu_auto_add( $nav_term->term_id );
|
||||
|
||||
return $nav_term;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 bool|WP_Error Whether the current user has permission.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
return new WP_Error(
|
||||
'rest_cannot_view',
|
||||
__( 'Sorry, you are not allowed to view menus.' ),
|
||||
array( 'status' => rest_authorization_required_code() )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares a single term output for response.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @param WP_Term $term Term object.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return WP_REST_Response Response object.
|
||||
*/
|
||||
public function prepare_item_for_response( $term, $request ) {
|
||||
$nav_menu = wp_get_nav_menu_object( $term );
|
||||
$response = parent::prepare_item_for_response( $nav_menu, $request );
|
||||
|
||||
$fields = $this->get_fields_for_response( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
if ( rest_is_field_included( 'locations', $fields ) ) {
|
||||
$data['locations'] = $this->get_menu_locations( $nav_menu->term_id );
|
||||
}
|
||||
|
||||
if ( rest_is_field_included( 'auto_add', $fields ) ) {
|
||||
$data['auto_add'] = $this->get_menu_auto_add( $nav_menu->term_id );
|
||||
}
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
|
||||
$response = rest_ensure_response( $data );
|
||||
$response->add_links( $this->prepare_links( $term ) );
|
||||
|
||||
/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
|
||||
return apply_filters( "rest_prepare_{$this->taxonomy}", $response, $term, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares links for the request.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @param WP_Term $term Term object.
|
||||
* @return array Links for the given term.
|
||||
*/
|
||||
protected function prepare_links( $term ) {
|
||||
$links = parent::prepare_links( $term );
|
||||
|
||||
$locations = $this->get_menu_locations( $term->term_id );
|
||||
foreach ( $locations as $location ) {
|
||||
$url = rest_url( sprintf( 'wp/v2/menu-locations/%s', $location ) );
|
||||
|
||||
$links['https://api.w.org/menu-location'][] = array(
|
||||
'href' => $url,
|
||||
'embeddable' => true,
|
||||
);
|
||||
}
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares a single term for create or update.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return object Prepared term data.
|
||||
*/
|
||||
public function prepare_item_for_database( $request ) {
|
||||
$prepared_term = parent::prepare_item_for_database( $request );
|
||||
|
||||
$schema = $this->get_item_schema();
|
||||
|
||||
if ( isset( $request['name'] ) && ! empty( $schema['properties']['name'] ) ) {
|
||||
$prepared_term->{'menu-name'} = $request['name'];
|
||||
}
|
||||
|
||||
return $prepared_term;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a single term in a taxonomy.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
|
||||
*/
|
||||
public function create_item( $request ) {
|
||||
if ( isset( $request['parent'] ) ) {
|
||||
if ( ! is_taxonomy_hierarchical( $this->taxonomy ) ) {
|
||||
return new WP_Error( 'rest_taxonomy_not_hierarchical', __( 'Cannot set parent term, taxonomy is not hierarchical.' ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
$parent = wp_get_nav_menu_object( (int) $request['parent'] );
|
||||
|
||||
if ( ! $parent ) {
|
||||
return new WP_Error( 'rest_term_invalid', __( 'Parent term does not exist.' ), array( 'status' => 400 ) );
|
||||
}
|
||||
}
|
||||
|
||||
$prepared_term = $this->prepare_item_for_database( $request );
|
||||
|
||||
$term = wp_update_nav_menu_object( 0, wp_slash( (array) $prepared_term ) );
|
||||
|
||||
if ( is_wp_error( $term ) ) {
|
||||
/*
|
||||
* If we're going to inform the client that the term already exists,
|
||||
* give them the identifier for future use.
|
||||
*/
|
||||
|
||||
if ( in_array( 'menu_exists', $term->get_error_codes(), true ) ) {
|
||||
$existing_term = get_term_by( 'name', $prepared_term->{'menu-name'}, $this->taxonomy );
|
||||
$term->add_data( $existing_term->term_id, 'menu_exists' );
|
||||
$term->add_data(
|
||||
array(
|
||||
'status' => 400,
|
||||
'term_id' => $existing_term->term_id,
|
||||
)
|
||||
);
|
||||
} else {
|
||||
$term->add_data( array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
return $term;
|
||||
}
|
||||
|
||||
$term = $this->get_term( $term );
|
||||
|
||||
/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
|
||||
do_action( "rest_insert_{$this->taxonomy}", $term, $request, true );
|
||||
|
||||
$schema = $this->get_item_schema();
|
||||
if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
|
||||
$meta_update = $this->meta->update_value( $request['meta'], $term->term_id );
|
||||
|
||||
if ( is_wp_error( $meta_update ) ) {
|
||||
return $meta_update;
|
||||
}
|
||||
}
|
||||
|
||||
$locations_update = $this->handle_locations( $term->term_id, $request );
|
||||
|
||||
if ( is_wp_error( $locations_update ) ) {
|
||||
return $locations_update;
|
||||
}
|
||||
|
||||
$this->handle_auto_add( $term->term_id, $request );
|
||||
|
||||
$fields_update = $this->update_additional_fields_for_object( $term, $request );
|
||||
|
||||
if ( is_wp_error( $fields_update ) ) {
|
||||
return $fields_update;
|
||||
}
|
||||
|
||||
$request->set_param( 'context', 'view' );
|
||||
|
||||
/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
|
||||
do_action( "rest_after_insert_{$this->taxonomy}", $term, $request, true );
|
||||
|
||||
$response = $this->prepare_item_for_response( $term, $request );
|
||||
$response = rest_ensure_response( $response );
|
||||
|
||||
$response->set_status( 201 );
|
||||
$response->header( 'Location', rest_url( $this->namespace . '/' . $this->rest_base . '/' . $term->term_id ) );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a single term from a taxonomy.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
|
||||
*/
|
||||
public function update_item( $request ) {
|
||||
$term = $this->get_term( $request['id'] );
|
||||
if ( is_wp_error( $term ) ) {
|
||||
return $term;
|
||||
}
|
||||
|
||||
if ( isset( $request['parent'] ) ) {
|
||||
if ( ! is_taxonomy_hierarchical( $this->taxonomy ) ) {
|
||||
return new WP_Error( 'rest_taxonomy_not_hierarchical', __( 'Cannot set parent term, taxonomy is not hierarchical.' ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
$parent = get_term( (int) $request['parent'], $this->taxonomy );
|
||||
|
||||
if ( ! $parent ) {
|
||||
return new WP_Error( 'rest_term_invalid', __( 'Parent term does not exist.' ), array( 'status' => 400 ) );
|
||||
}
|
||||
}
|
||||
|
||||
$prepared_term = $this->prepare_item_for_database( $request );
|
||||
|
||||
// Only update the term if we have something to update.
|
||||
if ( ! empty( $prepared_term ) ) {
|
||||
if ( ! isset( $prepared_term->{'menu-name'} ) ) {
|
||||
// wp_update_nav_menu_object() requires that the menu-name is always passed.
|
||||
$prepared_term->{'menu-name'} = $term->name;
|
||||
}
|
||||
|
||||
$update = wp_update_nav_menu_object( $term->term_id, wp_slash( (array) $prepared_term ) );
|
||||
|
||||
if ( is_wp_error( $update ) ) {
|
||||
return $update;
|
||||
}
|
||||
}
|
||||
|
||||
$term = get_term( $term->term_id, $this->taxonomy );
|
||||
|
||||
/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
|
||||
do_action( "rest_insert_{$this->taxonomy}", $term, $request, false );
|
||||
|
||||
$schema = $this->get_item_schema();
|
||||
if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
|
||||
$meta_update = $this->meta->update_value( $request['meta'], $term->term_id );
|
||||
|
||||
if ( is_wp_error( $meta_update ) ) {
|
||||
return $meta_update;
|
||||
}
|
||||
}
|
||||
|
||||
$locations_update = $this->handle_locations( $term->term_id, $request );
|
||||
|
||||
if ( is_wp_error( $locations_update ) ) {
|
||||
return $locations_update;
|
||||
}
|
||||
|
||||
$this->handle_auto_add( $term->term_id, $request );
|
||||
|
||||
$fields_update = $this->update_additional_fields_for_object( $term, $request );
|
||||
|
||||
if ( is_wp_error( $fields_update ) ) {
|
||||
return $fields_update;
|
||||
}
|
||||
|
||||
$request->set_param( 'context', 'view' );
|
||||
|
||||
/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
|
||||
do_action( "rest_after_insert_{$this->taxonomy}", $term, $request, false );
|
||||
|
||||
$response = $this->prepare_item_for_response( $term, $request );
|
||||
|
||||
return rest_ensure_response( $response );
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a single term from a taxonomy.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
|
||||
*/
|
||||
public function delete_item( $request ) {
|
||||
$term = $this->get_term( $request['id'] );
|
||||
if ( is_wp_error( $term ) ) {
|
||||
return $term;
|
||||
}
|
||||
|
||||
// We don't support trashing for terms.
|
||||
if ( ! $request['force'] ) {
|
||||
/* translators: %s: force=true */
|
||||
return new WP_Error( 'rest_trash_not_supported', sprintf( __( "Menus do not support trashing. Set '%s' to delete." ), 'force=true' ), array( 'status' => 501 ) );
|
||||
}
|
||||
|
||||
$request->set_param( 'context', 'view' );
|
||||
|
||||
$previous = $this->prepare_item_for_response( $term, $request );
|
||||
|
||||
$result = wp_delete_nav_menu( $term );
|
||||
|
||||
if ( ! $result || is_wp_error( $result ) ) {
|
||||
return new WP_Error( 'rest_cannot_delete', __( 'The menu cannot be deleted.' ), array( 'status' => 500 ) );
|
||||
}
|
||||
|
||||
$response = new WP_REST_Response();
|
||||
$response->set_data(
|
||||
array(
|
||||
'deleted' => true,
|
||||
'previous' => $previous->get_data(),
|
||||
)
|
||||
);
|
||||
|
||||
/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
|
||||
do_action( "rest_delete_{$this->taxonomy}", $term, $response, $request );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of a menu's auto_add setting.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @param int $menu_id The menu id to query.
|
||||
* @return bool The value of auto_add.
|
||||
*/
|
||||
protected function get_menu_auto_add( $menu_id ) {
|
||||
$nav_menu_option = (array) get_option( 'nav_menu_options', array( 'auto_add' => array() ) );
|
||||
|
||||
return in_array( $menu_id, $nav_menu_option['auto_add'], true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the menu's auto add from a REST request.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @param int $menu_id The menu id to update.
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return bool True if the auto add setting was successfully updated.
|
||||
*/
|
||||
protected function handle_auto_add( $menu_id, $request ) {
|
||||
if ( ! isset( $request['auto_add'] ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$nav_menu_option = (array) get_option( 'nav_menu_options', array( 'auto_add' => array() ) );
|
||||
|
||||
if ( ! isset( $nav_menu_option['auto_add'] ) ) {
|
||||
$nav_menu_option['auto_add'] = array();
|
||||
}
|
||||
|
||||
$auto_add = $request['auto_add'];
|
||||
|
||||
$i = array_search( $menu_id, $nav_menu_option['auto_add'], true );
|
||||
|
||||
if ( $auto_add && false === $i ) {
|
||||
$nav_menu_option['auto_add'][] = $menu_id;
|
||||
} elseif ( ! $auto_add && false !== $i ) {
|
||||
array_splice( $nav_menu_option['auto_add'], $i, 1 );
|
||||
}
|
||||
|
||||
$update = update_option( 'nav_menu_options', $nav_menu_option );
|
||||
|
||||
/** This action is documented in wp-includes/nav-menu.php */
|
||||
do_action( 'wp_update_nav_menu', $menu_id );
|
||||
|
||||
return $update;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the names of the locations assigned to the menu.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @param int $menu_id The menu id.
|
||||
* @return string[] The locations assigned to the menu.
|
||||
*/
|
||||
protected function get_menu_locations( $menu_id ) {
|
||||
$locations = get_nav_menu_locations();
|
||||
$menu_locations = array();
|
||||
|
||||
foreach ( $locations as $location => $assigned_menu_id ) {
|
||||
if ( $menu_id === $assigned_menu_id ) {
|
||||
$menu_locations[] = $location;
|
||||
}
|
||||
}
|
||||
|
||||
return $menu_locations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the menu's locations from a REST request.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @param int $menu_id The menu id to update.
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return true|WP_Error True on success, a WP_Error on an error updating any of the locations.
|
||||
*/
|
||||
protected function handle_locations( $menu_id, $request ) {
|
||||
if ( ! isset( $request['locations'] ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$menu_locations = get_registered_nav_menus();
|
||||
$menu_locations = array_keys( $menu_locations );
|
||||
$new_locations = array();
|
||||
foreach ( $request['locations'] as $location ) {
|
||||
if ( ! in_array( $location, $menu_locations, true ) ) {
|
||||
return new WP_Error(
|
||||
'rest_invalid_menu_location',
|
||||
__( 'Invalid menu location.' ),
|
||||
array(
|
||||
'status' => 400,
|
||||
'location' => $location,
|
||||
)
|
||||
);
|
||||
}
|
||||
$new_locations[ $location ] = $menu_id;
|
||||
}
|
||||
$assigned_menu = get_nav_menu_locations();
|
||||
foreach ( $assigned_menu as $location => $term_id ) {
|
||||
if ( $term_id === $menu_id ) {
|
||||
unset( $assigned_menu[ $location ] );
|
||||
}
|
||||
}
|
||||
$new_assignments = array_merge( $assigned_menu, $new_locations );
|
||||
set_theme_mod( 'nav_menu_locations', $new_assignments );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the term's schema, conforming to JSON Schema.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @return array Item schema data.
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$schema = parent::get_item_schema();
|
||||
unset( $schema['properties']['count'], $schema['properties']['link'], $schema['properties']['taxonomy'] );
|
||||
|
||||
$schema['properties']['locations'] = array(
|
||||
'description' => __( 'The locations assigned to the menu.' ),
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'string',
|
||||
),
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'arg_options' => array(
|
||||
'validate_callback' => function ( $locations, $request, $param ) {
|
||||
$valid = rest_validate_request_arg( $locations, $request, $param );
|
||||
|
||||
if ( true !== $valid ) {
|
||||
return $valid;
|
||||
}
|
||||
|
||||
$locations = rest_sanitize_request_arg( $locations, $request, $param );
|
||||
|
||||
foreach ( $locations as $location ) {
|
||||
if ( ! array_key_exists( $location, get_registered_nav_menus() ) ) {
|
||||
return new WP_Error(
|
||||
'rest_invalid_menu_location',
|
||||
__( 'Invalid menu location.' ),
|
||||
array(
|
||||
'location' => $location,
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
$schema['properties']['auto_add'] = array(
|
||||
'description' => __( 'Whether to automatically add top level pages to this menu.' ),
|
||||
'context' => array( 'view', 'edit' ),
|
||||
'type' => 'boolean',
|
||||
);
|
||||
|
||||
return $schema;
|
||||
}
|
||||
}
|
||||
@ -1551,7 +1551,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ( $request[ $base ] as $term_id ) {
|
||||
foreach ( (array) $request[ $base ] as $term_id ) {
|
||||
// Invalid terms will be rejected later.
|
||||
if ( ! get_term( $term_id, $taxonomy->name ) ) {
|
||||
continue;
|
||||
|
||||
@ -108,17 +108,26 @@ function create_initial_taxonomies() {
|
||||
'nav_menu',
|
||||
'nav_menu_item',
|
||||
array(
|
||||
'public' => false,
|
||||
'hierarchical' => false,
|
||||
'labels' => array(
|
||||
'public' => false,
|
||||
'hierarchical' => false,
|
||||
'labels' => array(
|
||||
'name' => __( 'Navigation Menus' ),
|
||||
'singular_name' => __( 'Navigation Menu' ),
|
||||
),
|
||||
'query_var' => false,
|
||||
'rewrite' => false,
|
||||
'show_ui' => false,
|
||||
'_builtin' => true,
|
||||
'show_in_nav_menus' => false,
|
||||
'query_var' => false,
|
||||
'rewrite' => false,
|
||||
'show_ui' => false,
|
||||
'_builtin' => true,
|
||||
'show_in_nav_menus' => false,
|
||||
'capabilities' => array(
|
||||
'manage_terms' => 'edit_theme_options',
|
||||
'edit_terms' => 'edit_theme_options',
|
||||
'delete_terms' => 'edit_theme_options',
|
||||
'assign_terms' => 'edit_theme_options',
|
||||
),
|
||||
'show_in_rest' => true,
|
||||
'rest_base' => 'menus',
|
||||
'rest_controller_class' => 'WP_REST_Menus_Controller',
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
@ -262,6 +262,9 @@ require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-revisions-controlle
|
||||
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-autosaves-controller.php';
|
||||
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-taxonomies-controller.php';
|
||||
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-terms-controller.php';
|
||||
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-menu-items-controller.php';
|
||||
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-menus-controller.php';
|
||||
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-menu-locations-controller.php';
|
||||
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-users-controller.php';
|
||||
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-comments-controller.php';
|
||||
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-search-controller.php';
|
||||
|
||||
@ -89,6 +89,14 @@ class WP_Test_REST_Schema_Initialization extends WP_Test_REST_TestCase {
|
||||
'/wp/v2/posts/(?P<parent>[\\d]+)/revisions/(?P<id>[\\d]+)',
|
||||
'/wp/v2/posts/(?P<id>[\\d]+)/autosaves',
|
||||
'/wp/v2/posts/(?P<parent>[\\d]+)/autosaves/(?P<id>[\\d]+)',
|
||||
'/wp/v2/menu-items',
|
||||
'/wp/v2/menu-items/(?P<id>[\d]+)',
|
||||
'/wp/v2/menu-items/(?P<id>[\d]+)/autosaves',
|
||||
'/wp/v2/menu-items/(?P<parent>[\d]+)/autosaves/(?P<id>[\d]+)',
|
||||
'/wp/v2/menu-locations',
|
||||
'/wp/v2/menu-locations/(?P<location>[\w-]+)',
|
||||
'/wp/v2/menus',
|
||||
'/wp/v2/menus/(?P<id>[\d]+)',
|
||||
'/wp/v2/pages',
|
||||
'/wp/v2/pages/(?P<id>[\\d]+)',
|
||||
'/wp/v2/pages/(?P<parent>[\\d]+)/revisions',
|
||||
|
||||
@ -68,7 +68,9 @@ class WP_Test_REST_Taxonomies_Controller extends WP_Test_REST_Controller_Testcas
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$taxonomies = $this->get_public_taxonomies( get_taxonomies( '', 'objects' ) );
|
||||
$taxonomies = get_taxonomies( '', 'objects' );
|
||||
unset( $taxonomies['nav_menu'] ); // Menus are not editable by contributors.
|
||||
$taxonomies = $this->get_public_taxonomies( $taxonomies );
|
||||
$this->assertSame( count( $taxonomies ), count( $data ) );
|
||||
$this->assertSame( 'Categories', $data['category']['name'] );
|
||||
$this->assertSame( 'category', $data['category']['slug'] );
|
||||
|
||||
1152
tests/phpunit/tests/rest-api/wpRestMenuItemsController.php
Normal file
1152
tests/phpunit/tests/rest-api/wpRestMenuItemsController.php
Normal file
File diff suppressed because it is too large
Load Diff
207
tests/phpunit/tests/rest-api/wpRestMenuLocationsController.php
Normal file
207
tests/phpunit/tests/rest-api/wpRestMenuLocationsController.php
Normal file
@ -0,0 +1,207 @@
|
||||
<?php
|
||||
/**
|
||||
* WP_REST_Menu_Locations_Controller tests.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage REST_API
|
||||
* @since 5.9.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Tests for REST API for Menu locations.
|
||||
*
|
||||
* @group restapi
|
||||
*
|
||||
* @coversDefaultClass WP_REST_Menu_Locations_Controller
|
||||
*/
|
||||
class Tests_REST_WpRestMenuLocationsController extends WP_Test_REST_Controller_Testcase {
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected static $admin_id;
|
||||
|
||||
/**
|
||||
* Create fake data before our tests run.
|
||||
*
|
||||
* @param WP_UnitTest_Factory $factory Helper that lets us create fake data.
|
||||
*/
|
||||
public static function wpSetUpBeforeClass( $factory ) {
|
||||
self::$admin_id = $factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up.
|
||||
*/
|
||||
public function set_up() {
|
||||
parent::set_up();
|
||||
|
||||
// Unregister all nav menu locations.
|
||||
foreach ( array_keys( get_registered_nav_menus() ) as $location ) {
|
||||
unregister_nav_menu( $location );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register nav menu locations.
|
||||
*
|
||||
* @param array $locations Location slugs.
|
||||
*/
|
||||
public function register_nav_menu_locations( $locations ) {
|
||||
foreach ( $locations as $location ) {
|
||||
register_nav_menu( $location, ucfirst( $location ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::register_routes
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = rest_get_server()->get_routes();
|
||||
$this->assertArrayHasKey( '/wp/v2/menu-locations', $routes );
|
||||
$this->assertCount( 1, $routes['/wp/v2/menu-locations'] );
|
||||
$this->assertArrayHasKey( '/wp/v2/menu-locations/(?P<location>[\w-]+)', $routes );
|
||||
$this->assertCount( 1, $routes['/wp/v2/menu-locations/(?P<location>[\w-]+)'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::get_context_param
|
||||
*/
|
||||
public function test_context_param() {
|
||||
// Collection.
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/menu-locations' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
|
||||
$this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
|
||||
$menu = 'primary';
|
||||
$this->register_nav_menu_locations( array( $menu ) );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/menu-locations/' . $menu );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
|
||||
$this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::get_items
|
||||
*/
|
||||
public function test_get_items() {
|
||||
$menus = array( 'primary', 'secondary' );
|
||||
$this->register_nav_menu_locations( array( 'primary', 'secondary' ) );
|
||||
wp_set_current_user( self::$admin_id );
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/menu-locations' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$data = array_values( $data );
|
||||
$this->assertCount( 2, $data );
|
||||
$names = wp_list_pluck( $data, 'name' );
|
||||
$descriptions = wp_list_pluck( $data, 'description' );
|
||||
$this->assertSame( $menus, $names );
|
||||
$menu_descriptions = array_map( 'ucfirst', $names );
|
||||
$this->assertSame( $menu_descriptions, $descriptions );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::get_item
|
||||
*/
|
||||
public function test_get_item() {
|
||||
$menu = 'primary';
|
||||
$this->register_nav_menu_locations( array( $menu ) );
|
||||
|
||||
wp_set_current_user( self::$admin_id );
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/menu-locations/' . $menu );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertSame( $menu, $data['name'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::get_item
|
||||
*/
|
||||
public function test_get_item_invalid() {
|
||||
$menu = 'primary';
|
||||
$this->register_nav_menu_locations( array( $menu ) );
|
||||
|
||||
wp_set_current_user( self::$admin_id );
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/menu-locations/invalid' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
|
||||
$this->assertErrorResponse( 'rest_menu_location_invalid', $response, 404 );
|
||||
}
|
||||
|
||||
/**
|
||||
* The test_create_item() method does not exist for menu locations.
|
||||
*/
|
||||
public function test_create_item() {}
|
||||
|
||||
/**
|
||||
* The test_update_item() method does not exist for menu locations.
|
||||
*/
|
||||
public function test_update_item() {}
|
||||
|
||||
/**
|
||||
* The test_delete_item() method does not exist for menu locations.
|
||||
*/
|
||||
public function test_delete_item() {}
|
||||
|
||||
/**
|
||||
* The test_prepare_item() method does not exist for menu locations.
|
||||
*/
|
||||
public function test_prepare_item() {}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::get_item_schema
|
||||
*/
|
||||
public function test_get_item_schema() {
|
||||
wp_set_current_user( self::$admin_id );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/menu-locations' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
$this->assertSame( 3, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'name', $properties );
|
||||
$this->assertArrayHasKey( 'description', $properties );
|
||||
$this->assertArrayHasKey( 'menu', $properties );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::get_items
|
||||
* @covers ::get_items_permissions_check
|
||||
*/
|
||||
public function test_get_items_menu_location_context_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/menu-locations' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
|
||||
$this->assertErrorResponse( 'rest_cannot_view', $response, rest_authorization_required_code() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::get_item
|
||||
* @covers ::get_item_permissions_check
|
||||
*/
|
||||
public function test_get_item_menu_location_context_without_permission() {
|
||||
$menu = 'primary';
|
||||
$this->register_nav_menu_locations( array( $menu ) );
|
||||
|
||||
wp_set_current_user( 0 );
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/menu-locations/' . $menu );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
|
||||
$this->assertErrorResponse( 'rest_cannot_view', $response, rest_authorization_required_code() );
|
||||
}
|
||||
}
|
||||
642
tests/phpunit/tests/rest-api/wpRestMenusController.php
Normal file
642
tests/phpunit/tests/rest-api/wpRestMenusController.php
Normal file
@ -0,0 +1,642 @@
|
||||
<?php
|
||||
/**
|
||||
* WP_REST_Menus_Controller tests
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage REST_API
|
||||
* @since 5.9.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Tests for REST API for Menus.
|
||||
*
|
||||
* @group restapi
|
||||
*
|
||||
* @coversDefaultClass WP_REST_Menus_Controller
|
||||
*/
|
||||
class Tests_REST_WpRestMenusController extends WP_Test_REST_Controller_Testcase {
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $menu_id;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected static $admin_id;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected static $editor_id;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected static $subscriber_id;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
const TAXONOMY = 'nav_menu';
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected static $per_page = 50;
|
||||
|
||||
/**
|
||||
* Create fake data before our tests run.
|
||||
*
|
||||
* @param WP_UnitTest_Factory $factory Helper that lets us create fake data.
|
||||
*/
|
||||
public static function wpSetUpBeforeClass( $factory ) {
|
||||
self::$admin_id = $factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
self::$editor_id = $factory->user->create(
|
||||
array(
|
||||
'role' => 'editor',
|
||||
)
|
||||
);
|
||||
self::$subscriber_id = $factory->user->create(
|
||||
array(
|
||||
'role' => 'subscriber',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function set_up() {
|
||||
parent::set_up();
|
||||
// Unregister all nav menu locations.
|
||||
foreach ( array_keys( get_registered_nav_menus() ) as $location ) {
|
||||
unregister_nav_menu( $location );
|
||||
}
|
||||
|
||||
$orig_args = array(
|
||||
'name' => 'Original Name',
|
||||
'description' => 'Original Description',
|
||||
'slug' => 'original-slug',
|
||||
'taxonomy' => 'nav_menu',
|
||||
);
|
||||
|
||||
$this->menu_id = $this->factory->term->create( $orig_args );
|
||||
|
||||
register_meta(
|
||||
'term',
|
||||
'test_single_menu',
|
||||
array(
|
||||
'object_subtype' => self::TAXONOMY,
|
||||
'show_in_rest' => true,
|
||||
'single' => true,
|
||||
'type' => 'string',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register nav menu locations.
|
||||
*
|
||||
* @param array $locations Location slugs.
|
||||
*/
|
||||
public function register_nav_menu_locations( $locations ) {
|
||||
foreach ( $locations as $location ) {
|
||||
register_nav_menu( $location, ucfirst( $location ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::register_routes
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = rest_get_server()->get_routes();
|
||||
$this->assertArrayHasKey( '/wp/v2/menus', $routes );
|
||||
$this->assertArrayHasKey( '/wp/v2/menus/(?P<id>[\d]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::get_context_param
|
||||
*/
|
||||
public function test_context_param() {
|
||||
// Collection.
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/menus' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
|
||||
$this->assertSameSets( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
|
||||
$this->assertSame( array( 'v1' => true ), $data['endpoints'][0]['allow_batch'] );
|
||||
// Single.
|
||||
$tag1 = $this->factory->tag->create( array( 'name' => 'Season 5' ) );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/menus/' . $tag1 );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
|
||||
$this->assertSameSets( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
|
||||
$this->assertSame( array( 'v1' => true ), $data['endpoints'][0]['allow_batch'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::get_collection_params
|
||||
*/
|
||||
public function test_registered_query_params() {
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/menus' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$keys = array_keys( $data['endpoints'][0]['args'] );
|
||||
sort( $keys );
|
||||
$this->assertSame(
|
||||
array(
|
||||
'context',
|
||||
'exclude',
|
||||
'hide_empty',
|
||||
'include',
|
||||
'offset',
|
||||
'order',
|
||||
'orderby',
|
||||
'page',
|
||||
'per_page',
|
||||
'post',
|
||||
'search',
|
||||
'slug',
|
||||
),
|
||||
$keys
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::get_items
|
||||
*/
|
||||
public function test_get_items() {
|
||||
wp_set_current_user( self::$admin_id );
|
||||
wp_update_nav_menu_object(
|
||||
0,
|
||||
array(
|
||||
'description' => 'Test get',
|
||||
'menu-name' => 'test Name get',
|
||||
)
|
||||
);
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/menus' );
|
||||
$request->set_param( 'per_page', self::$per_page );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->check_get_taxonomy_terms_response( $response );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::get_item
|
||||
*/
|
||||
public function test_get_item() {
|
||||
wp_set_current_user( self::$admin_id );
|
||||
$nav_menu_id = wp_update_nav_menu_object(
|
||||
0,
|
||||
array(
|
||||
'description' => 'Test menu',
|
||||
'menu-name' => 'test Name',
|
||||
)
|
||||
);
|
||||
|
||||
$this->register_nav_menu_locations( array( 'primary' ) );
|
||||
set_theme_mod( 'nav_menu_locations', array( 'primary' => $nav_menu_id ) );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/menus/' . $nav_menu_id );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->check_get_taxonomy_term_response( $response, $nav_menu_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::create_item
|
||||
*/
|
||||
public function test_create_item() {
|
||||
wp_set_current_user( self::$admin_id );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wp/v2/menus' );
|
||||
$request->set_param( 'name', 'My Awesome menus' );
|
||||
$request->set_param( 'description', 'This menu is so awesome.' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->assertSame( 201, $response->get_status() );
|
||||
$headers = $response->get_headers();
|
||||
$data = $response->get_data();
|
||||
$this->assertStringContainsString( '/wp/v2/menus/' . $data['id'], $headers['Location'] );
|
||||
$this->assertSame( 'My Awesome menus', $data['name'] );
|
||||
$this->assertSame( 'This menu is so awesome.', $data['description'] );
|
||||
$this->assertSame( 'my-awesome-menus', $data['slug'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::create_item
|
||||
*/
|
||||
public function test_create_item_same_name() {
|
||||
wp_set_current_user( self::$admin_id );
|
||||
|
||||
wp_update_nav_menu_object(
|
||||
0,
|
||||
array(
|
||||
'description' => 'This menu is so Original',
|
||||
'menu-name' => 'Original',
|
||||
)
|
||||
);
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wp/v2/menus' );
|
||||
$request->set_param( 'name', 'Original' );
|
||||
$request->set_param( 'description', 'This menu is so Original' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
|
||||
$this->assertErrorResponse( 'menu_exists', $response, 400 );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::update_item
|
||||
* @covers ::handle_auto_add
|
||||
*/
|
||||
public function test_update_item() {
|
||||
wp_set_current_user( self::$admin_id );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wp/v2/menus/' . $this->menu_id );
|
||||
$request->set_param( 'name', 'New Name' );
|
||||
$request->set_param( 'description', 'New Description' );
|
||||
$request->set_param( 'auto_add', true );
|
||||
$request->set_param(
|
||||
'meta',
|
||||
array(
|
||||
'test_single_menu' => 'just meta',
|
||||
)
|
||||
);
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->assertSame( 200, $response->get_status() );
|
||||
$data = $response->get_data();
|
||||
$this->assertSame( 'New Name', $data['name'] );
|
||||
$this->assertSame( 'New Description', $data['description'] );
|
||||
$this->assertSame( true, $data['auto_add'] );
|
||||
$this->assertSame( 'new-name', $data['slug'] );
|
||||
$this->assertSame( 'just meta', $data['meta']['test_single_menu'] );
|
||||
$this->assertFalse( isset( $data['meta']['test_cat_meta'] ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::delete_item
|
||||
*/
|
||||
public function test_delete_item() {
|
||||
wp_set_current_user( self::$admin_id );
|
||||
|
||||
$nav_menu_id = wp_update_nav_menu_object(
|
||||
0,
|
||||
array(
|
||||
'description' => 'Deleted Menu',
|
||||
'menu-name' => 'Deleted Menu',
|
||||
)
|
||||
);
|
||||
|
||||
$term = get_term_by( 'id', $nav_menu_id, self::TAXONOMY );
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wp/v2/menus/' . $term->term_id );
|
||||
$request->set_param( 'force', true );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->assertSame( 200, $response->get_status() );
|
||||
$data = $response->get_data();
|
||||
$this->assertTrue( $data['deleted'] );
|
||||
$this->assertSame( 'Deleted Menu', $data['previous']['name'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::prepare_item_for_response
|
||||
* @covers ::get_item
|
||||
*/
|
||||
public function test_prepare_item() {
|
||||
$nav_menu_id = wp_update_nav_menu_object(
|
||||
0,
|
||||
array(
|
||||
'description' => 'Foo Menu',
|
||||
'menu-name' => 'Foo Menu',
|
||||
)
|
||||
);
|
||||
|
||||
$term = get_term_by( 'id', $nav_menu_id, self::TAXONOMY );
|
||||
wp_set_current_user( self::$admin_id );
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/menus/' . $term->term_id );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->check_taxonomy_term( $term, $data, $response->get_links() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::get_item_schema
|
||||
*/
|
||||
public function test_get_item_schema() {
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/menus' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
$this->assertSame( 7, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'description', $properties );
|
||||
$this->assertArrayHasKey( 'meta', $properties );
|
||||
$this->assertArrayHasKey( 'name', $properties );
|
||||
$this->assertArrayHasKey( 'slug', $properties );
|
||||
$this->assertArrayHasKey( 'locations', $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::create_item
|
||||
*/
|
||||
public function test_create_item_with_location_permission_correct() {
|
||||
$this->register_nav_menu_locations( array( 'primary', 'secondary' ) );
|
||||
wp_set_current_user( self::$admin_id );
|
||||
$request = new WP_REST_Request( 'POST', '/wp/v2/menus' );
|
||||
$request->set_param( 'name', 'My Awesome Term' );
|
||||
$request->set_param( 'slug', 'so-awesome' );
|
||||
$request->set_param( 'locations', 'primary' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->assertSame( 201, $response->get_status() );
|
||||
$data = $response->get_data();
|
||||
$term_id = $data['id'];
|
||||
$locations = get_nav_menu_locations();
|
||||
$this->assertSame( $locations['primary'], $term_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::create_item
|
||||
*/
|
||||
public function test_create_item_with_invalid_location() {
|
||||
wp_set_current_user( self::$admin_id );
|
||||
$request = new WP_REST_Request( 'POST', '/wp/v2/menus' );
|
||||
$request->set_param( 'name', 'My Awesome Term' );
|
||||
$request->set_param( 'slug', 'so-awesome' );
|
||||
$request->set_param( 'locations', 'bar' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->assertSame( 400, $response->get_status() );
|
||||
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
|
||||
$this->assertArrayHasKey( 'locations', $response->get_data()['data']['details'] );
|
||||
$this->assertSame( 'rest_invalid_menu_location', $response->get_data()['data']['details']['locations']['code'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::update_item
|
||||
*/
|
||||
public function test_update_item_with_no_location() {
|
||||
$this->register_nav_menu_locations( array( 'primary', 'secondary' ) );
|
||||
wp_set_current_user( self::$admin_id );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wp/v2/menus/' . $this->menu_id );
|
||||
$request->set_param( 'name', 'New Name' );
|
||||
$request->set_param( 'description', 'New Description' );
|
||||
$request->set_param( 'slug', 'new-slug' );
|
||||
$request->set_param( 'locations', 'bar' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->assertSame( 400, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::update_item
|
||||
*/
|
||||
public function test_update_item_with_location_permission_correct() {
|
||||
$this->register_nav_menu_locations( array( 'primary', 'secondary' ) );
|
||||
wp_set_current_user( self::$admin_id );
|
||||
$request = new WP_REST_Request( 'POST', '/wp/v2/menus/' . $this->menu_id );
|
||||
$request->set_param( 'name', 'New Name' );
|
||||
$request->set_param( 'description', 'New Description' );
|
||||
$request->set_param( 'slug', 'new-slug' );
|
||||
$request->set_param( 'locations', 'primary' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->assertSame( 200, $response->get_status() );
|
||||
$locations = get_nav_menu_locations();
|
||||
$this->assertSame( $locations['primary'], $this->menu_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::update_item
|
||||
*/
|
||||
public function test_update_item_with_location_permission_incorrect() {
|
||||
$this->register_nav_menu_locations( array( 'primary', 'secondary' ) );
|
||||
wp_set_current_user( self::$subscriber_id );
|
||||
$request = new WP_REST_Request( 'POST', '/wp/v2/menus/' . $this->menu_id );
|
||||
$request->set_param( 'name', 'New Name' );
|
||||
$request->set_param( 'description', 'New Description' );
|
||||
$request->set_param( 'slug', 'new-slug' );
|
||||
$request->set_param( 'locations', 'primary' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->assertSame( rest_authorization_required_code(), $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::prepare_links
|
||||
*/
|
||||
public function test_get_item_links() {
|
||||
wp_set_current_user( self::$admin_id );
|
||||
|
||||
$nav_menu_id = wp_update_nav_menu_object(
|
||||
0,
|
||||
array(
|
||||
'description' => 'Foo Menu',
|
||||
'menu-name' => 'Foo Menu',
|
||||
)
|
||||
);
|
||||
|
||||
register_nav_menu( 'foo', 'Bar' );
|
||||
|
||||
set_theme_mod( 'nav_menu_locations', array( 'foo' => $nav_menu_id ) );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/menus/%d', $nav_menu_id ) );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
|
||||
$links = $response->get_links();
|
||||
$this->assertArrayHasKey( 'https://api.w.org/menu-location', $links );
|
||||
|
||||
$location_url = rest_url( '/wp/v2/menu-locations/foo' );
|
||||
$this->assertSame( $location_url, $links['https://api.w.org/menu-location'][0]['href'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::update_item
|
||||
* @covers ::handle_locations
|
||||
*/
|
||||
public function test_change_menu_location() {
|
||||
$this->register_nav_menu_locations( array( 'primary', 'secondary' ) );
|
||||
$secondary_id = self::factory()->term->create(
|
||||
array(
|
||||
'name' => 'Secondary Name',
|
||||
'description' => 'Secondary Description',
|
||||
'slug' => 'secondary-slug',
|
||||
'taxonomy' => 'nav_menu',
|
||||
)
|
||||
);
|
||||
|
||||
$locations = get_nav_menu_locations();
|
||||
$locations['primary'] = $this->menu_id;
|
||||
$locations['secondary'] = $secondary_id;
|
||||
set_theme_mod( 'nav_menu_locations', $locations );
|
||||
|
||||
wp_set_current_user( self::$admin_id );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wp/v2/menus/' . $this->menu_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'locations' => array( 'secondary' ),
|
||||
)
|
||||
);
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
|
||||
$this->assertSame( 200, $response->get_status() );
|
||||
|
||||
$locations = get_nav_menu_locations();
|
||||
$this->assertArrayNotHasKey( 'primary', $locations );
|
||||
$this->assertArrayHasKey( 'secondary', $locations );
|
||||
$this->assertSame( $this->menu_id, $locations['secondary'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::get_items
|
||||
* @covers ::get_items_permissions_check
|
||||
*/
|
||||
public function test_get_items_no_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/menus' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->assertErrorResponse( 'rest_cannot_view', $response, 401 );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::get_items
|
||||
* @covers ::get_items_permissions_check
|
||||
*/
|
||||
public function test_get_item_no_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/menus/' . $this->menu_id );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->assertErrorResponse( 'rest_cannot_view', $response, 401 );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::get_items
|
||||
* @covers ::get_items_permissions_check
|
||||
*/
|
||||
public function test_get_items_wrong_permission() {
|
||||
wp_set_current_user( self::$subscriber_id );
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/menus' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->assertErrorResponse( 'rest_cannot_view', $response, 403 );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
* @covers ::get_item
|
||||
* @covers ::get_item_permissions_check
|
||||
*/
|
||||
public function test_get_item_wrong_permission() {
|
||||
wp_set_current_user( self::$subscriber_id );
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/menus/' . $this->menu_id );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->assertErrorResponse( 'rest_cannot_view', $response, 403 );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40878
|
||||
*/
|
||||
public function test_it_allows_batch_requests_when_updating_menus() {
|
||||
$rest_server = rest_get_server();
|
||||
// This call is needed to initialize route_options.
|
||||
$rest_server->get_routes();
|
||||
$route_options = $rest_server->get_route_options( '/wp/v2/menus/(?P<id>[\d]+)' );
|
||||
|
||||
$this->assertArrayHasKey( 'allow_batch', $route_options );
|
||||
$this->assertSame( array( 'v1' => true ), $route_options['allow_batch'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Response $response Response Class.
|
||||
*/
|
||||
protected function check_get_taxonomy_terms_response( $response ) {
|
||||
$this->assertSame( 200, $response->get_status() );
|
||||
$data = $response->get_data();
|
||||
$args = array(
|
||||
'hide_empty' => false,
|
||||
);
|
||||
$tags = get_terms( self::TAXONOMY, $args );
|
||||
$this->assertSame( count( $tags ), count( $data ) );
|
||||
$this->assertSame( $tags[0]->term_id, $data[0]['id'] );
|
||||
$this->assertSame( $tags[0]->name, $data[0]['name'] );
|
||||
$this->assertSame( $tags[0]->slug, $data[0]['slug'] );
|
||||
$this->assertSame( $tags[0]->description, $data[0]['description'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Response $response Response Class.
|
||||
* @param int $id Term ID.
|
||||
*/
|
||||
protected function check_get_taxonomy_term_response( $response, $id ) {
|
||||
$this->assertSame( 200, $response->get_status() );
|
||||
|
||||
$data = $response->get_data();
|
||||
$menu = get_term( $id, self::TAXONOMY );
|
||||
$this->check_taxonomy_term( $menu, $data, $response->get_links() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_Term $term WP_Term object.
|
||||
* @param array $data Data from REST API.
|
||||
* @param array $links Array of links.
|
||||
*/
|
||||
protected function check_taxonomy_term( $term, $data, $links ) {
|
||||
$this->assertSame( $term->term_id, $data['id'] );
|
||||
$this->assertSame( $term->name, $data['name'] );
|
||||
$this->assertSame( $term->slug, $data['slug'] );
|
||||
$this->assertSame( $term->description, $data['description'] );
|
||||
$this->assertFalse( isset( $data['parent'] ) );
|
||||
|
||||
$locations = get_nav_menu_locations();
|
||||
if ( ! empty( $locations ) ) {
|
||||
$menu_locations = array();
|
||||
foreach ( $locations as $location => $menu_id ) {
|
||||
if ( $menu_id === $term->term_id ) {
|
||||
$menu_locations[] = $location;
|
||||
}
|
||||
}
|
||||
|
||||
$this->assertSame( $menu_locations, $data['locations'] );
|
||||
}
|
||||
|
||||
$relations = array(
|
||||
'self',
|
||||
'collection',
|
||||
'about',
|
||||
'https://api.w.org/post_type',
|
||||
);
|
||||
|
||||
if ( ! empty( $data['parent'] ) ) {
|
||||
$relations[] = 'up';
|
||||
}
|
||||
|
||||
if ( ! empty( $data['locations'] ) ) {
|
||||
$relations[] = 'https://api.w.org/menu-location';
|
||||
}
|
||||
|
||||
$this->assertSameSets( $relations, array_keys( $links ) );
|
||||
$this->assertStringContainsString( 'wp/v2/taxonomies/' . $term->taxonomy, $links['about'][0]['href'] );
|
||||
$this->assertSame( add_query_arg( 'menus', $term->term_id, rest_url( 'wp/v2/menu-items' ) ), $links['https://api.w.org/post_type'][0]['href'] );
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user