<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[52079] trunk: REST API: Introduce Menu management endpoints.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/52079">52079</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/52079","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>TimothyBlynJacobs</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-11-09 18:58:59 +0000 (Tue, 09 Nov 2021)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/40878">#40878</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesnavmenuphp">trunk/src/wp-includes/nav-menu.php</a></li>
<li><a href="#trunksrcwpincludespostphp">trunk/src/wp-includes/post.php</a></li>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestpostscontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php</a></li>
<li><a href="#trunksrcwpincludesrestapiphp">trunk/src/wp-includes/rest-api.php</a></li>
<li><a href="#trunksrcwpincludestaxonomyphp">trunk/src/wp-includes/taxonomy.php</a></li>
<li><a href="#trunksrcwpsettingsphp">trunk/src/wp-settings.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestschemasetupphp">trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php</a></li>
<li><a href="#trunktestsphpunittestsrestapiresttaxonomiescontrollerphp">trunk/tests/phpunit/tests/rest-api/rest-taxonomies-controller.php</a></li>
<li><a href="#trunktestsqunitfixtureswpapigeneratedjs">trunk/tests/qunit/fixtures/wp-api-generated.js</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestmenuitemscontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php</a></li>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestmenulocationscontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-menu-locations-controller.php</a></li>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestmenuscontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-menus-controller.php</a></li>
<li><a href="#trunktestsphpunittestsrestapiwpRestMenuItemsControllerphp">trunk/tests/phpunit/tests/rest-api/wpRestMenuItemsController.php</a></li>
<li><a href="#trunktestsphpunittestsrestapiwpRestMenuLocationsControllerphp">trunk/tests/phpunit/tests/rest-api/wpRestMenuLocationsController.php</a></li>
<li><a href="#trunktestsphpunittestsrestapiwpRestMenusControllerphp">trunk/tests/phpunit/tests/rest-api/wpRestMenusController.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesnavmenuphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/nav-menu.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/nav-menu.php 2021-11-09 18:49:12 UTC (rev 52078)
+++ trunk/src/wp-includes/nav-menu.php 2021-11-09 18:58:59 UTC (rev 52079)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -406,10 +406,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Save the properties of a menu item or create a new one.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * 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()`.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * 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.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 3.0.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.9.0 Added the menu-item-content parameter.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param int $menu_id The ID of the menu. Required. If "0", makes the menu item a draft orphan.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param int $menu_item_db_id The ID of the menu item. If "0", creates a new menu item.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -448,6 +449,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'menu-item-attr-title' => '',
</span><span class="cx" style="display: block; padding: 0 10px"> 'menu-item-target' => '',
</span><span class="cx" style="display: block; padding: 0 10px"> 'menu-item-classes' => '',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'menu-item-content' => '',
</ins><span class="cx" style="display: block; padding: 0 10px"> 'menu-item-xfn' => '',
</span><span class="cx" style="display: block; padding: 0 10px"> 'menu-item-status' => '',
</span><span class="cx" style="display: block; padding: 0 10px"> 'menu-item-post-date' => '',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -526,7 +528,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! $update ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $post['ID'] = 0;
</span><span class="cx" style="display: block; padding: 0 10px"> $post['post_status'] = 'publish' === $args['menu-item-status'] ? 'publish' : 'draft';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $menu_item_db_id = wp_insert_post( $post );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $menu_item_db_id = wp_insert_post( $post, true );
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $menu_item_db_id;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -548,7 +550,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // Associate the menu item with the menu term.
</span><span class="cx" style="display: block; padding: 0 10px"> // Only set the menu term if it isn't set to avoid unnecessary wp_get_object_terms().
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $menu_id && ( ! $update || ! is_object_in_term( $menu_item_db_id, 'nav_menu', (int) $menu->term_id ) ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- wp_set_object_terms( $menu_item_db_id, array( $menu->term_id ), 'nav_menu' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $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;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( 'custom' === $args['menu-item-type'] ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -569,6 +574,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> update_post_meta( $menu_item_db_id, '_menu_item_classes', $args['menu-item-classes'] );
</span><span class="cx" style="display: block; padding: 0 10px"> update_post_meta( $menu_item_db_id, '_menu_item_xfn', $args['menu-item-xfn'] );
</span><span class="cx" style="display: block; padding: 0 10px"> update_post_meta( $menu_item_db_id, '_menu_item_url', esc_url_raw( $args['menu-item-url'] ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ update_post_meta( $menu_item_db_id, '_menu_item_content', $args['menu-item-content'] );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( 0 == $menu_id ) {
</span><span class="cx" style="display: block; padding: 0 10px"> update_post_meta( $menu_item_db_id, '_menu_item_orphaned', (string) time() );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -580,7 +586,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $update ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $post['ID'] = $menu_item_db_id;
</span><span class="cx" style="display: block; padding: 0 10px"> $post['post_status'] = ( 'draft' === $args['menu-item-status'] ) ? 'draft' : 'publish';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- wp_update_post( $post );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ $update_post = wp_update_post( $post, true );
+ if ( is_wp_error( $update_post ) ) {
+ return $update_post;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -903,6 +913,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $menu_item->title = ( '' === $menu_item->post_title ) ? $original_title : $menu_item->post_title;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ } 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;
</ins><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><span class="cx" style="display: block; padding: 0 10px"> $menu_item->type_label = __( 'Custom Link' );
</span><span class="cx" style="display: block; padding: 0 10px"> $menu_item->title = $menu_item->post_title;
</span></span></pre></div>
<a id="trunksrcwpincludespostphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/post.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/post.php 2021-11-09 18:49:12 UTC (rev 52078)
+++ trunk/src/wp-includes/post.php 2021-11-09 18:58:59 UTC (rev 52079)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -126,16 +126,39 @@
</span><span class="cx" style="display: block; padding: 0 10px"> register_post_type(
</span><span class="cx" style="display: block; padding: 0 10px"> 'nav_menu_item',
</span><span class="cx" style="display: block; padding: 0 10px"> array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'labels' => array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'labels' => array(
</ins><span class="cx" style="display: block; padding: 0 10px"> 'name' => __( 'Navigation Menu Items' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'singular_name' => __( 'Navigation Menu Item' ),
</span><span class="cx" style="display: block; padding: 0 10px"> ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- '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,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ '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',
</ins><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiendpointsclasswprestmenuitemscontrollerphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php (rev 0)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php 2021-11-09 18:58:59 UTC (rev 52079)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,1073 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * REST API: WP_REST_Menu_Items_Controller class
+ *
+ * @package WordPress
+ * @subpackage REST_API
+ * @since 5.9.0
+ */
+
+/**
+ * Core class to access nav items via the REST API.
+ *
+ * @since 5.9.0
+ *
+ * @see WP_REST_Posts_Controller
+ */
+class WP_REST_Menu_Items_Controller extends WP_REST_Posts_Controller {
+
+ /**
+ * Get 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 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 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 menu items.' ),
+ array( 'status' => rest_authorization_required_code() )
+ );
+ }
+
+ /**
+ * Creates a single post.
+ *
+ * @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 ( ! empty( $request['id'] ) ) {
+ return new WP_Error( 'rest_post_exists', __( 'Cannot create existing post.' ), array( 'status' => 400 ) );
+ }
+
+ $prepared_nav_item = $this->prepare_item_for_database( $request );
+
+ if ( is_wp_error( $prepared_nav_item ) ) {
+ return $prepared_nav_item;
+ }
+ $prepared_nav_item = (array) $prepared_nav_item;
+
+ $nav_menu_item_id = wp_update_nav_menu_item( $prepared_nav_item['menu-id'], $prepared_nav_item['menu-item-db-id'], wp_slash( $prepared_nav_item ) );
+ if ( is_wp_error( $nav_menu_item_id ) ) {
+ if ( 'db_insert_error' === $nav_menu_item_id->get_error_code() ) {
+ $nav_menu_item_id->add_data( array( 'status' => 500 ) );
+ } else {
+ $nav_menu_item_id->add_data( array( 'status' => 400 ) );
+ }
+
+ return $nav_menu_item_id;
+ }
+
+ $nav_menu_item = $this->get_nav_menu_item( $nav_menu_item_id );
+ if ( is_wp_error( $nav_menu_item ) ) {
+ $nav_menu_item->add_data( array( 'status' => 404 ) );
+
+ return $nav_menu_item;
+ }
+
+ /**
+ * Fires after a single menu item is created or updated via the REST API.
+ *
+ * @since 5.9.0
+ *
+ * @param object $nav_menu_item Inserted or updated menu item object.
+ * @param WP_REST_Request $request Request object.
+ * @param bool $creating True when creating a menu item, false when updating.
+ */
+ do_action( 'rest_insert_nav_menu_item', $nav_menu_item, $request, true );
+
+ $schema = $this->get_item_schema();
+
+ if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
+ $meta_update = $this->meta->update_value( $request['meta'], $nav_menu_item_id );
+
+ if ( is_wp_error( $meta_update ) ) {
+ return $meta_update;
+ }
+ }
+
+ $nav_menu_item = $this->get_nav_menu_item( $nav_menu_item_id );
+ $fields_update = $this->update_additional_fields_for_object( $nav_menu_item, $request );
+
+ if ( is_wp_error( $fields_update ) ) {
+ return $fields_update;
+ }
+
+ $request->set_param( 'context', 'edit' );
+
+ /**
+ * Fires after a single menu item is completely created or updated via the REST API.
+ *
+ * @since 5.9.0
+ *
+ * @param object $nav_menu_item Inserted or updated menu item object.
+ * @param WP_REST_Request $request Request object.
+ * @param bool $creating True when creating a menu item, false when updating.
+ */
+ do_action( 'rest_after_insert_nav_menu_item', $nav_menu_item, $request, true );
+
+ $response = $this->prepare_item_for_response( get_post( $nav_menu_item_id ), $request );
+ $response = rest_ensure_response( $response );
+
+ $response->set_status( 201 );
+ $response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $nav_menu_item_id ) ) );
+
+ return $response;
+ }
+
+ /**
+ * Updates a single nav menu item.
+ *
+ * @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 ) {
+ $valid_check = $this->get_nav_menu_item( $request['id'] );
+ if ( is_wp_error( $valid_check ) ) {
+ return $valid_check;
+ }
+
+ $prepared_nav_item = $this->prepare_item_for_database( $request );
+
+ if ( is_wp_error( $prepared_nav_item ) ) {
+ return $prepared_nav_item;
+ }
+
+ $prepared_nav_item = (array) $prepared_nav_item;
+
+ $nav_menu_item_id = wp_update_nav_menu_item( $prepared_nav_item['menu-id'], $prepared_nav_item['menu-item-db-id'], wp_slash( $prepared_nav_item ) );
+
+ if ( is_wp_error( $nav_menu_item_id ) ) {
+ if ( 'db_update_error' === $nav_menu_item_id->get_error_code() ) {
+ $nav_menu_item_id->add_data( array( 'status' => 500 ) );
+ } else {
+ $nav_menu_item_id->add_data( array( 'status' => 400 ) );
+ }
+
+ return $nav_menu_item_id;
+ }
+
+ $nav_menu_item = $this->get_nav_menu_item( $nav_menu_item_id );
+ if ( is_wp_error( $nav_menu_item ) ) {
+ $nav_menu_item->add_data( array( 'status' => 404 ) );
+
+ return $nav_menu_item;
+ }
+
+ /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php */
+ do_action( 'rest_insert_nav_menu_item', $nav_menu_item, $request, false );
+
+ $schema = $this->get_item_schema();
+
+ if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
+ $meta_update = $this->meta->update_value( $request['meta'], $nav_menu_item->ID );
+
+ if ( is_wp_error( $meta_update ) ) {
+ return $meta_update;
+ }
+ }
+
+ $nav_menu_item = $this->get_nav_menu_item( $nav_menu_item_id );
+ $fields_update = $this->update_additional_fields_for_object( $nav_menu_item, $request );
+
+ if ( is_wp_error( $fields_update ) ) {
+ return $fields_update;
+ }
+
+ $request->set_param( 'context', 'edit' );
+
+ /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php */
+ do_action( 'rest_after_insert_nav_menu_item', $nav_menu_item, $request, false );
+
+ $response = $this->prepare_item_for_response( get_post( $nav_menu_item_id ), $request );
+
+ return rest_ensure_response( $response );
+ }
+
+ /**
+ * Deletes a single menu item.
+ *
+ * @since 5.9.0
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return WP_REST_Response|WP_Error True on success, or WP_Error object on failure.
+ */
+ public function delete_item( $request ) {
+ $menu_item = $this->get_nav_menu_item( $request['id'] );
+ if ( is_wp_error( $menu_item ) ) {
+ return $menu_item;
+ }
+
+ // We don't support trashing for menu items.
+ if ( ! $request['force'] ) {
+ /* translators: %s: force=true */
+ return new WP_Error( 'rest_trash_not_supported', sprintf( __( "Menu items do not support trashing. Set '%s' to delete." ), 'force=true' ), array( 'status' => 501 ) );
+ }
+
+ $previous = $this->prepare_item_for_response( get_post( $request['id'] ), $request );
+
+ $result = wp_delete_post( $request['id'], true );
+
+ if ( ! $result ) {
+ return new WP_Error( 'rest_cannot_delete', __( 'The post cannot be deleted.' ), array( 'status' => 500 ) );
+ }
+
+ $response = new WP_REST_Response();
+ $response->set_data(
+ array(
+ 'deleted' => true,
+ 'previous' => $previous->get_data(),
+ )
+ );
+
+ /**
+ * Fires immediately after a single menu item is deleted via the REST API.
+ *
+ * @since 5.9.0
+ *
+ * @param object $nav_menu_item Inserted or updated menu item object.
+ * @param WP_REST_Response $response The response data.
+ * @param WP_REST_Request $request Request object.
+ */
+ do_action( 'rest_delete_nav_menu_item', $menu_item, $response, $request );
+
+ return $response;
+ }
+
+ /**
+ * Prepares a single post for create or update.
+ *
+ * @since 5.9.0
+ *
+ * @param WP_REST_Request $request Request object.
+ *
+ * @return object|WP_Error
+ */
+ protected function prepare_item_for_database( $request ) {
+ $menu_item_db_id = $request['id'];
+ $menu_item_obj = $this->get_nav_menu_item( $menu_item_db_id );
+ // Need to persist the menu item data. See https://core.trac.wordpress.org/ticket/28138
+ if ( ! is_wp_error( $menu_item_obj ) ) {
+ // Correct the menu position if this was the first item. See https://core.trac.wordpress.org/ticket/28140
+ $position = ( 0 === $menu_item_obj->menu_order ) ? 1 : $menu_item_obj->menu_order;
+
+ $prepared_nav_item = array(
+ 'menu-item-db-id' => $menu_item_db_id,
+ 'menu-item-object-id' => $menu_item_obj->object_id,
+ 'menu-item-object' => $menu_item_obj->object,
+ 'menu-item-parent-id' => $menu_item_obj->menu_item_parent,
+ 'menu-item-position' => $position,
+ 'menu-item-type' => $menu_item_obj->type,
+ 'menu-item-title' => $menu_item_obj->title,
+ 'menu-item-url' => $menu_item_obj->url,
+ 'menu-item-description' => $menu_item_obj->description,
+ 'menu-item-content' => $menu_item_obj->menu_item_content,
+ 'menu-item-attr-title' => $menu_item_obj->attr_title,
+ 'menu-item-target' => $menu_item_obj->target,
+ 'menu-item-classes' => $menu_item_obj->classes,
+ // Stored in the database as a string.
+ 'menu-item-xfn' => explode( ' ', $menu_item_obj->xfn ),
+ 'menu-item-status' => $menu_item_obj->post_status,
+ 'menu-id' => $this->get_menu_id( $menu_item_db_id ),
+ );
+ } else {
+ $prepared_nav_item = array(
+ 'menu-id' => 0,
+ 'menu-item-db-id' => 0,
+ 'menu-item-object-id' => 0,
+ 'menu-item-object' => '',
+ 'menu-item-parent-id' => 0,
+ 'menu-item-position' => 1,
+ 'menu-item-type' => 'custom',
+ 'menu-item-title' => '',
+ 'menu-item-url' => '',
+ 'menu-item-description' => '',
+ 'menu-item-content' => '',
+ 'menu-item-attr-title' => '',
+ 'menu-item-target' => '',
+ 'menu-item-classes' => array(),
+ 'menu-item-xfn' => array(),
+ 'menu-item-status' => 'publish',
+ );
+ }
+
+ $mapping = array(
+ 'menu-item-db-id' => 'id',
+ 'menu-item-object-id' => 'object_id',
+ 'menu-item-object' => 'object',
+ 'menu-item-parent-id' => 'parent',
+ 'menu-item-position' => 'menu_order',
+ 'menu-item-type' => 'type',
+ 'menu-item-url' => 'url',
+ 'menu-item-description' => 'description',
+ 'menu-item-attr-title' => 'attr_title',
+ 'menu-item-target' => 'target',
+ 'menu-item-classes' => 'classes',
+ 'menu-item-xfn' => 'xfn',
+ 'menu-item-status' => 'status',
+ );
+
+ $schema = $this->get_item_schema();
+
+ foreach ( $mapping as $original => $api_request ) {
+ if ( isset( $request[ $api_request ] ) ) {
+ $prepared_nav_item[ $original ] = $request[ $api_request ];
+ }
+ }
+
+ $taxonomy = get_taxonomy( 'nav_menu' );
+ $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
+ // If menus submitted, cast to int.
+ if ( ! empty( $request[ $base ] ) ) {
+ $prepared_nav_item['menu-id'] = absint( $request[ $base ] );
+ }
+
+ // Nav menu title.
+ if ( ! empty( $schema['properties']['title'] ) && isset( $request['title'] ) ) {
+ if ( is_string( $request['title'] ) ) {
+ $prepared_nav_item['menu-item-title'] = $request['title'];
+ } elseif ( ! empty( $request['title']['raw'] ) ) {
+ $prepared_nav_item['menu-item-title'] = $request['title']['raw'];
+ }
+ }
+
+ // Nav menu content.
+ if ( ! empty( $schema['properties']['content'] ) && isset( $request['content'] ) ) {
+ if ( is_string( $request['content'] ) ) {
+ $prepared_nav_item['menu-item-content'] = $request['content'];
+ } elseif ( isset( $request['content']['raw'] ) ) {
+ $prepared_nav_item['menu-item-content'] = $request['content']['raw'];
+ }
+ }
+
+ $error = new WP_Error();
+
+ // Check if object id exists before saving.
+ if ( ! $prepared_nav_item['menu-item-object'] ) {
+ // If taxonomy, check if term exists.
+ if ( 'taxonomy' === $prepared_nav_item['menu-item-type'] ) {
+ $original = get_term( absint( $prepared_nav_item['menu-item-object-id'] ) );
+ if ( empty( $original ) || is_wp_error( $original ) ) {
+ $error->add( 'rest_term_invalid_id', __( 'Invalid term ID.' ), array( 'status' => 400 ) );
+ } else {
+ $prepared_nav_item['menu-item-object'] = get_term_field( 'taxonomy', $original );
+ }
+ // If post, check if post object exists.
+ } elseif ( 'post_type' === $prepared_nav_item['menu-item-type'] ) {
+ $original = get_post( absint( $prepared_nav_item['menu-item-object-id'] ) );
+ if ( empty( $original ) ) {
+ $error->add( 'rest_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 400 ) );
+ } else {
+ $prepared_nav_item['menu-item-object'] = get_post_type( $original );
+ }
+ }
+ }
+
+ // If post type archive, check if post type exists.
+ if ( 'post_type_archive' === $prepared_nav_item['menu-item-type'] ) {
+ $post_type = $prepared_nav_item['menu-item-object'] ? $prepared_nav_item['menu-item-object'] : false;
+ $original = get_post_type_object( $post_type );
+ if ( ! $original ) {
+ $error->add( 'rest_post_invalid_type', __( 'Invalid post type.' ), array( 'status' => 400 ) );
+ }
+ }
+
+ // Check if menu item is type custom, then title and url are required.
+ if ( 'custom' === $prepared_nav_item['menu-item-type'] ) {
+ if ( '' === $prepared_nav_item['menu-item-title'] ) {
+ $error->add( 'rest_title_required', __( 'The title is required when using a custom menu item type.' ), array( 'status' => 400 ) );
+ }
+ if ( empty( $prepared_nav_item['menu-item-url'] ) ) {
+ $error->add( 'rest_url_required', __( 'The url is required when using a custom menu item type.' ), array( 'status' => 400 ) );
+ }
+ }
+
+ // If menu item is type block, then content is required.
+ if ( 'block' === $prepared_nav_item['menu-item-type'] && empty( $prepared_nav_item['menu-item-content'] ) ) {
+ $error->add( 'rest_content_required', __( 'The content is required when using a block menu item type.' ), array( 'status' => 400 ) );
+ }
+
+ if ( $error->has_errors() ) {
+ return $error;
+ }
+
+ // The xfn and classes properties are arrays, but passed to wp_update_nav_menu_item as a string.
+ foreach ( array( 'menu-item-xfn', 'menu-item-classes' ) as $key ) {
+ $prepared_nav_item[ $key ] = implode( ' ', $prepared_nav_item[ $key ] );
+ }
+
+ // Only draft / publish are valid post status for menu items.
+ if ( 'publish' !== $prepared_nav_item['menu-item-status'] ) {
+ $prepared_nav_item['menu-item-status'] = 'draft';
+ }
+
+ $prepared_nav_item = (object) $prepared_nav_item;
+
+ /**
+ * Filters a menu item before it is inserted via the REST API.
+ *
+ * @since 5.9.0
+ *
+ * @param object $prepared_nav_item An object representing a single menu item prepared
+ * for inserting or updating the database.
+ * @param WP_REST_Request $request Request object.
+ */
+ return apply_filters( 'rest_pre_insert_nav_menu_item', $prepared_nav_item, $request );
+ }
+
+ /**
+ * Prepares a single post output for response.
+ *
+ * @since 5.9.0
+ *
+ * @param WP_Post $item Post object.
+ * @param WP_REST_Request $request Request object.
+ * @return WP_REST_Response Response object.
+ */
+ public function prepare_item_for_response( $item, $request ) {
+ // Base fields for every post.
+ $fields = $this->get_fields_for_response( $request );
+ $menu_item = $this->get_nav_menu_item( $item->ID );
+ $data = array();
+
+ if ( rest_is_field_included( 'id', $fields ) ) {
+ $data['id'] = $menu_item->ID;
+ }
+
+ if ( rest_is_field_included( 'title', $fields ) ) {
+ $data['title'] = array();
+ }
+
+ if ( rest_is_field_included( 'title.raw', $fields ) ) {
+ $data['title']['raw'] = $menu_item->title;
+ }
+
+ if ( rest_is_field_included( 'title.rendered', $fields ) ) {
+ add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
+
+ /** This filter is documented in wp-includes/post-template.php */
+ $title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID );
+
+ $data['title']['rendered'] = $title;
+
+ remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
+ }
+
+ if ( rest_is_field_included( 'status', $fields ) ) {
+ $data['status'] = $menu_item->post_status;
+ }
+
+ if ( rest_is_field_included( 'url', $fields ) ) {
+ $data['url'] = $menu_item->url;
+ }
+
+ if ( rest_is_field_included( 'attr_title', $fields ) ) {
+ // Same as post_excerpt.
+ $data['attr_title'] = $menu_item->attr_title;
+ }
+
+ if ( rest_is_field_included( 'description', $fields ) ) {
+ // Same as post_content.
+ $data['description'] = $menu_item->description;
+ }
+
+ if ( rest_is_field_included( 'type', $fields ) ) {
+ $data['type'] = $menu_item->type;
+ }
+
+ if ( rest_is_field_included( 'type_label', $fields ) ) {
+ $data['type_label'] = $menu_item->type_label;
+ }
+
+ if ( rest_is_field_included( 'object', $fields ) ) {
+ $data['object'] = $menu_item->object;
+ }
+
+ if ( rest_is_field_included( 'object_id', $fields ) ) {
+ // It is stored as a string, but should be exposed as an integer.
+ $data['object_id'] = absint( $menu_item->object_id );
+ }
+
+ if ( rest_is_field_included( 'content', $fields ) ) {
+ $data['content'] = array();
+ }
+
+ if ( rest_is_field_included( 'content.raw', $fields ) ) {
+ $data['content']['raw'] = $menu_item->menu_item_content;
+ }
+
+ if ( rest_is_field_included( 'content.rendered', $fields ) ) {
+ /** This filter is documented in wp-includes/post-template.php */
+ $data['content']['rendered'] = apply_filters( 'the_content', $menu_item->menu_item_content );
+ }
+
+ if ( rest_is_field_included( 'content.block_version', $fields ) ) {
+ $data['content']['block_version'] = block_version( $menu_item->menu_item_content );
+ }
+
+ if ( rest_is_field_included( 'parent', $fields ) ) {
+ // Same as post_parent, exposed as an integer.
+ $data['parent'] = (int) $menu_item->menu_item_parent;
+ }
+
+ if ( rest_is_field_included( 'menu_order', $fields ) ) {
+ // Same as post_parent, exposed as an integer.
+ $data['menu_order'] = (int) $menu_item->menu_order;
+ }
+
+ if ( rest_is_field_included( 'target', $fields ) ) {
+ $data['target'] = $menu_item->target;
+ }
+
+ if ( rest_is_field_included( 'classes', $fields ) ) {
+ $data['classes'] = (array) $menu_item->classes;
+ }
+
+ if ( rest_is_field_included( 'xfn', $fields ) ) {
+ $data['xfn'] = array_map( 'sanitize_html_class', explode( ' ', $menu_item->xfn ) );
+ }
+
+ if ( rest_is_field_included( 'invalid', $fields ) ) {
+ $data['invalid'] = (bool) $menu_item->_invalid;
+ }
+
+ if ( rest_is_field_included( 'meta', $fields ) ) {
+ $data['meta'] = $this->meta->get_value( $menu_item->ID, $request );
+ }
+
+ $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );
+
+ foreach ( $taxonomies as $taxonomy ) {
+ $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
+
+ if ( rest_is_field_included( $base, $fields ) ) {
+ $terms = get_the_terms( $item, $taxonomy->name );
+ if ( ! is_array( $terms ) ) {
+ continue;
+ }
+ $term_ids = $terms ? array_values( wp_list_pluck( $terms, 'term_id' ) ) : array();
+ if ( 'nav_menu' === $taxonomy->name ) {
+ $data[ $base ] = $term_ids ? array_shift( $term_ids ) : 0;
+ } else {
+ $data[ $base ] = $term_ids;
+ }
+ }
+ }
+
+ $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
+ $data = $this->add_additional_fields_to_object( $data, $request );
+ $data = $this->filter_response_by_context( $data, $context );
+
+ // Wrap the data in a response object.
+ $response = rest_ensure_response( $data );
+
+ $links = $this->prepare_links( $item );
+ $response->add_links( $links );
+
+ if ( ! empty( $links['self']['href'] ) ) {
+ $actions = $this->get_available_actions( $item, $request );
+
+ $self = $links['self']['href'];
+
+ foreach ( $actions as $rel ) {
+ $response->add_link( $rel, $self );
+ }
+ }
+
+ /**
+ * Filters the menu item data for a REST API response.
+ *
+ * @since 5.9.0
+ *
+ * @param WP_REST_Response $response The response object.
+ * @param object $menu_item Menu item setup by {@see wp_setup_nav_menu_item()}.
+ * @param WP_REST_Request $request Request object.
+ */
+ return apply_filters( 'rest_prepare_nav_menu_item', $response, $menu_item, $request );
+ }
+
+ /**
+ * Prepares links for the request.
+ *
+ * @since 5.9.0
+ *
+ * @param WP_Post $post Post object.
+ * @return array Links for the given post.
+ */
+ protected function prepare_links( $post ) {
+ $links = parent::prepare_links( $post );
+ $menu_item = $this->get_nav_menu_item( $post->ID );
+
+ if ( empty( $menu_item->object_id ) ) {
+ return $links;
+ }
+
+ $path = '';
+ $type = '';
+ $key = $menu_item->type;
+ if ( 'post_type' === $menu_item->type ) {
+ $path = rest_get_route_for_post( $menu_item->object_id );
+ $type = get_post_type( $menu_item->object_id );
+ } elseif ( 'taxonomy' === $menu_item->type ) {
+ $path = rest_get_route_for_term( $menu_item->object_id );
+ $type = get_term_field( 'taxonomy', $menu_item->object_id );
+ }
+
+ if ( $path && $type ) {
+ $links['https://api.w.org/menu-item-object'][] = array(
+ 'href' => rest_url( $path ),
+ $key => $type,
+ 'embeddable' => true,
+ );
+ }
+
+ return $links;
+ }
+
+ /**
+ * Retrieve Link Description Objects that should be added to the Schema for the posts collection.
+ *
+ * @since 5.9.0
+ *
+ * @return array
+ */
+ protected function get_schema_links() {
+ $links = parent::get_schema_links();
+ $href = rest_url( "{$this->namespace}/{$this->rest_base}/{id}" );
+ $links[] = array(
+ 'rel' => 'https://api.w.org/menu-item-object',
+ 'title' => __( 'Get linked object.' ),
+ 'href' => $href,
+ 'targetSchema' => array(
+ 'type' => 'object',
+ 'properties' => array(
+ 'object' => array(
+ 'type' => 'integer',
+ ),
+ ),
+ ),
+ );
+
+ return $links;
+ }
+
+ /**
+ * Retrieves the term's schema, conforming to JSON Schema.
+ *
+ * @since 5.9.0
+ *
+ * @return array Item schema data.
+ */
+ public function get_item_schema() {
+ $schema = array(
+ '$schema' => 'http://json-schema.org/draft-04/schema#',
+ 'title' => $this->post_type,
+ 'type' => 'object',
+ );
+
+ $schema['properties']['title'] = array(
+ 'description' => __( 'The title for the object.' ),
+ 'type' => array( 'string', 'object' ),
+ 'context' => array( 'view', 'edit', 'embed' ),
+ 'properties' => array(
+ 'raw' => array(
+ 'description' => __( 'Title for the object, as it exists in the database.' ),
+ 'type' => 'string',
+ 'context' => array( 'edit' ),
+ ),
+ 'rendered' => array(
+ 'description' => __( 'HTML title for the object, transformed for display.' ),
+ 'type' => 'string',
+ 'context' => array( 'view', 'edit', 'embed' ),
+ 'readonly' => true,
+ ),
+ ),
+ );
+
+ $schema['properties']['id'] = array(
+ 'description' => __( 'Unique identifier for the object.' ),
+ 'type' => 'integer',
+ 'default' => 0,
+ 'minimum' => 0,
+ 'context' => array( 'view', 'edit', 'embed' ),
+ 'readonly' => true,
+ );
+
+ $schema['properties']['type_label'] = array(
+ 'description' => __( 'Name of type.' ),
+ 'type' => 'string',
+ 'context' => array( 'view', 'edit', 'embed' ),
+ 'readonly' => true,
+ );
+
+ $schema['properties']['type'] = array(
+ 'description' => __( 'The family of objects originally represented, such as "post_type" or "taxonomy".' ),
+ 'type' => 'string',
+ 'enum' => array( 'taxonomy', 'post_type', 'post_type_archive', 'custom', 'block' ),
+ 'context' => array( 'view', 'edit', 'embed' ),
+ 'default' => 'custom',
+ );
+
+ $schema['properties']['status'] = array(
+ 'description' => __( 'A named status for the object.' ),
+ 'type' => 'string',
+ 'enum' => array_keys( get_post_stati( array( 'internal' => false ) ) ),
+ 'default' => 'publish',
+ 'context' => array( 'view', 'edit', 'embed' ),
+ );
+
+ $schema['properties']['parent'] = array(
+ 'description' => __( 'The ID for the parent of the object.' ),
+ 'type' => 'integer',
+ 'minimum' => 0,
+ 'default' => 0,
+ 'context' => array( 'view', 'edit', 'embed' ),
+ );
+
+ $schema['properties']['attr_title'] = array(
+ 'description' => __( 'Text for the title attribute of the link element for this menu item.' ),
+ 'type' => 'string',
+ 'context' => array( 'view', 'edit', 'embed' ),
+ 'arg_options' => array(
+ 'sanitize_callback' => 'sanitize_text_field',
+ ),
+ );
+
+ $schema['properties']['classes'] = array(
+ 'description' => __( 'Class names for the link element of this menu item.' ),
+ 'type' => 'array',
+ 'items' => array(
+ 'type' => 'string',
+ ),
+ 'context' => array( 'view', 'edit', 'embed' ),
+ 'arg_options' => array(
+ 'sanitize_callback' => function ( $value ) {
+ return array_map( 'sanitize_html_class', wp_parse_list( $value ) );
+ },
+ ),
+ );
+
+ $schema['properties']['description'] = array(
+ 'description' => __( 'The description of this menu item.' ),
+ 'type' => 'string',
+ 'context' => array( 'view', 'edit', 'embed' ),
+ 'arg_options' => array(
+ 'sanitize_callback' => 'sanitize_text_field',
+ ),
+ );
+
+ $schema['properties']['menu_order'] = array(
+ 'description' => __( 'The DB ID of the nav_menu_item that is this item\'s menu parent, if any, otherwise 0.' ),
+ 'context' => array( 'view', 'edit', 'embed' ),
+ 'type' => 'integer',
+ 'minimum' => 1,
+ 'default' => 1,
+ );
+
+ $schema['properties']['object'] = array(
+ 'description' => __( 'The type of object originally represented, such as "category," "post", or "attachment."' ),
+ 'context' => array( 'view', 'edit', 'embed' ),
+ 'type' => 'string',
+ 'arg_options' => array(
+ 'sanitize_callback' => 'sanitize_key',
+ ),
+ );
+
+ $schema['properties']['object_id'] = array(
+ 'description' => __( 'The database ID of the original object this menu item represents, for example the ID for posts or the term_id for categories.' ),
+ 'context' => array( 'view', 'edit', 'embed' ),
+ 'type' => 'integer',
+ 'minimum' => 0,
+ 'default' => 0,
+ );
+
+ $schema['properties']['content'] = array(
+ 'description' => __( 'HTML content to display for this block menu item.' ),
+ 'context' => array( 'view', 'edit', 'embed' ),
+ 'type' => array( 'string', 'object' ),
+ 'properties' => array(
+ 'raw' => array(
+ 'description' => __( 'HTML content, as it exists in the database.' ),
+ 'type' => 'string',
+ 'context' => array( 'edit' ),
+ ),
+ 'rendered' => array(
+ 'description' => __( 'HTML content, transformed for display.' ),
+ 'type' => 'string',
+ 'context' => array( 'view', 'edit' ),
+ 'readonly' => true,
+ ),
+ 'block_version' => array(
+ 'description' => __( 'Version of the block format used in the HTML content.' ),
+ 'type' => 'integer',
+ 'context' => array( 'edit' ),
+ 'readonly' => true,
+ ),
+ ),
+ );
+
+ $schema['properties']['target'] = array(
+ 'description' => __( 'The target attribute of the link element for this menu item.' ),
+ 'type' => 'string',
+ 'context' => array( 'view', 'edit', 'embed' ),
+ 'enum' => array(
+ '_blank',
+ '',
+ ),
+ );
+
+ $schema['properties']['type_label'] = array(
+ 'description' => __( 'The singular label used to describe this type of menu item.' ),
+ 'context' => array( 'view', 'edit', 'embed' ),
+ 'type' => 'string',
+ 'readonly' => true,
+ );
+
+ $schema['properties']['url'] = array(
+ 'description' => __( 'The URL to which this menu item points.' ),
+ 'type' => 'string',
+ 'format' => 'uri',
+ 'context' => array( 'view', 'edit', 'embed' ),
+ 'arg_options' => array(
+ 'validate_callback' => static function ( $url ) {
+ if ( '' === $url ) {
+ return true;
+ }
+
+ if ( esc_url_raw( $url ) ) {
+ return true;
+ }
+
+ return new WP_Error(
+ 'rest_invalid_url',
+ __( 'Invalid URL.' )
+ );
+ },
+ ),
+ );
+
+ $schema['properties']['xfn'] = array(
+ 'description' => __( 'The XFN relationship expressed in the link of this menu item.' ),
+ 'type' => 'array',
+ 'items' => array(
+ 'type' => 'string',
+ ),
+ 'context' => array( 'view', 'edit', 'embed' ),
+ 'arg_options' => array(
+ 'sanitize_callback' => function ( $value ) {
+ return array_map( 'sanitize_html_class', wp_parse_list( $value ) );
+ },
+ ),
+ );
+
+ $schema['properties']['invalid'] = array(
+ 'description' => __( 'Whether the menu item represents an object that no longer exists.' ),
+ 'context' => array( 'view', 'edit', 'embed' ),
+ 'type' => 'boolean',
+ 'readonly' => true,
+ );
+
+ $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );
+
+ foreach ( $taxonomies as $taxonomy ) {
+ $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
+ $schema['properties'][ $base ] = array(
+ /* translators: %s: taxonomy name */
+ 'description' => sprintf( __( 'The terms assigned to the object in the %s taxonomy.' ), $taxonomy->name ),
+ 'type' => 'array',
+ 'items' => array(
+ 'type' => 'integer',
+ ),
+ 'context' => array( 'view', 'edit' ),
+ );
+
+ if ( 'nav_menu' === $taxonomy->name ) {
+ $schema['properties'][ $base ]['type'] = 'integer';
+ unset( $schema['properties'][ $base ]['items'] );
+ }
+ }
+
+ $schema['properties']['meta'] = $this->meta->get_field_schema();
+
+ $schema_links = $this->get_schema_links();
+
+ if ( $schema_links ) {
+ $schema['links'] = $schema_links;
+ }
+
+ return $this->add_additional_fields_schema( $schema );
+ }
+
+ /**
+ * Retrieves the query params for the posts collection.
+ *
+ * @since 5.9.0
+ *
+ * @return array Collection parameters.
+ */
+ public function get_collection_params() {
+ $query_params = parent::get_collection_params();
+
+ $query_params['menu_order'] = array(
+ 'description' => __( 'Limit result set to posts with a specific menu_order value.' ),
+ 'type' => 'integer',
+ );
+
+ $query_params['order'] = array(
+ 'description' => __( 'Order sort attribute ascending or descending.' ),
+ 'type' => 'string',
+ 'default' => 'asc',
+ 'enum' => array( 'asc', 'desc' ),
+ );
+
+ $query_params['orderby'] = array(
+ 'description' => __( 'Sort collection by object attribute.' ),
+ 'type' => 'string',
+ 'default' => 'menu_order',
+ 'enum' => array(
+ 'author',
+ 'date',
+ 'id',
+ 'include',
+ 'modified',
+ 'parent',
+ 'relevance',
+ 'slug',
+ 'include_slugs',
+ 'title',
+ 'menu_order',
+ ),
+ );
+ // Change default to 100 items.
+ $query_params['per_page']['default'] = 100;
+
+ return $query_params;
+ }
+
+ /**
+ * Determines the allowed query_vars for a get_items() response and prepares
+ * them for WP_Query.
+ *
+ * @since 5.9.0
+ *
+ * @param array $prepared_args Optional. Prepared WP_Query arguments. Default empty array.
+ * @param WP_REST_Request $request Optional. Full details about the request.
+ * @return array Items query arguments.
+ */
+ protected function prepare_items_query( $prepared_args = array(), $request = null ) {
+ $query_args = parent::prepare_items_query( $prepared_args, $request );
+
+ // Map to proper WP_Query orderby param.
+ if ( isset( $query_args['orderby'], $request['orderby'] ) ) {
+ $orderby_mappings = array(
+ 'id' => 'ID',
+ 'include' => 'post__in',
+ 'slug' => 'post_name',
+ 'include_slugs' => 'post_name__in',
+ 'menu_order' => 'menu_order',
+ );
+
+ if ( isset( $orderby_mappings[ $request['orderby'] ] ) ) {
+ $query_args['orderby'] = $orderby_mappings[ $request['orderby'] ];
+ }
+ }
+
+ return $query_args;
+ }
+
+ /**
+ * Gets the id of the menu that the given menu item belongs to.
+ *
+ * @since 5.9.0
+ *
+ * @param int $menu_item_id Menu item id.
+ * @return int
+ */
+ protected function get_menu_id( $menu_item_id ) {
+ $menu_ids = wp_get_post_terms( $menu_item_id, 'nav_menu', array( 'fields' => 'ids' ) );
+ $menu_id = 0;
+ if ( $menu_ids && ! is_wp_error( $menu_ids ) ) {
+ $menu_id = array_shift( $menu_ids );
+ }
+
+ return $menu_id;
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunksrcwpincludesrestapiendpointsclasswprestmenulocationscontrollerphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-menu-locations-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-menu-locations-controller.php (rev 0)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-menu-locations-controller.php 2021-11-09 18:58:59 UTC (rev 52079)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,301 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?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;
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-menu-locations-controller.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunksrcwpincludesrestapiendpointsclasswprestmenuscontrollerphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-menus-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-menus-controller.php (rev 0)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-menus-controller.php 2021-11-09 18:58:59 UTC (rev 52079)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,568 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?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;
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-menus-controller.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunksrcwpincludesrestapiendpointsclasswprestpostscontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php 2021-11-09 18:49:12 UTC (rev 52078)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php 2021-11-09 18:58:59 UTC (rev 52079)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1551,7 +1551,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> continue;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- foreach ( $request[ $base ] as $term_id ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ foreach ( (array) $request[ $base ] as $term_id ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> // Invalid terms will be rejected later.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! get_term( $term_id, $taxonomy->name ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> continue;
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api.php 2021-11-09 18:49:12 UTC (rev 52078)
+++ trunk/src/wp-includes/rest-api.php 2021-11-09 18:58:59 UTC (rev 52079)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -345,6 +345,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // URL Details.
</span><span class="cx" style="display: block; padding: 0 10px"> $controller = new WP_REST_URL_Details_Controller();
</span><span class="cx" style="display: block; padding: 0 10px"> $controller->register_routes();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ // Menu Locations.
+ $controller = new WP_REST_Menu_Locations_Controller();
+ $controller->register_routes();
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="trunksrcwpincludestaxonomyphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/taxonomy.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/taxonomy.php 2021-11-09 18:49:12 UTC (rev 52078)
+++ trunk/src/wp-includes/taxonomy.php 2021-11-09 18:58:59 UTC (rev 52079)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -108,17 +108,26 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'nav_menu',
</span><span class="cx" style="display: block; padding: 0 10px"> 'nav_menu_item',
</span><span class="cx" style="display: block; padding: 0 10px"> array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'public' => false,
- 'hierarchical' => false,
- 'labels' => array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'public' => false,
+ 'hierarchical' => false,
+ 'labels' => array(
</ins><span class="cx" style="display: block; padding: 0 10px"> 'name' => __( 'Navigation Menus' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'singular_name' => __( 'Navigation Menu' ),
</span><span class="cx" style="display: block; padding: 0 10px"> ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'query_var' => false,
- 'rewrite' => false,
- 'show_ui' => false,
- '_builtin' => true,
- 'show_in_nav_menus' => false,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ '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',
</ins><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunksrcwpsettingsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-settings.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-settings.php 2021-11-09 18:49:12 UTC (rev 52078)
+++ trunk/src/wp-settings.php 2021-11-09 18:58:59 UTC (rev 52079)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -262,6 +262,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-autosaves-controller.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-taxonomies-controller.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-terms-controller.php';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+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';
</ins><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-users-controller.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-comments-controller.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-search-controller.php';
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestschemasetupphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php 2021-11-09 18:49:12 UTC (rev 52078)
+++ trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php 2021-11-09 18:58:59 UTC (rev 52079)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -89,6 +89,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/posts/(?P<parent>[\\d]+)/revisions/(?P<id>[\\d]+)',
</span><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/posts/(?P<id>[\\d]+)/autosaves',
</span><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/posts/(?P<parent>[\\d]+)/autosaves/(?P<id>[\\d]+)',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ '/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]+)',
</ins><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/pages',
</span><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/pages/(?P<id>[\\d]+)',
</span><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/pages/(?P<parent>[\\d]+)/revisions',
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapiresttaxonomiescontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/rest-taxonomies-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-taxonomies-controller.php 2021-11-09 18:49:12 UTC (rev 52078)
+++ trunk/tests/phpunit/tests/rest-api/rest-taxonomies-controller.php 2021-11-09 18:58:59 UTC (rev 52079)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -68,7 +68,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $request->set_param( 'context', 'edit' );
</span><span class="cx" style="display: block; padding: 0 10px"> $response = rest_get_server()->dispatch( $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $data = $response->get_data();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $taxonomies = $this->get_public_taxonomies( get_taxonomies( '', 'objects' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $taxonomies = get_taxonomies( '', 'objects' );
+ unset( $taxonomies['nav_menu'] ); // Menus are not editable by contributors.
+ $taxonomies = $this->get_public_taxonomies( $taxonomies );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( count( $taxonomies ), count( $data ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 'Categories', $data['category']['name'] );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 'category', $data['category']['slug'] );
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapiwpRestMenuItemsControllerphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/rest-api/wpRestMenuItemsController.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/wpRestMenuItemsController.php (rev 0)
+++ trunk/tests/phpunit/tests/rest-api/wpRestMenuItemsController.php 2021-11-09 18:58:59 UTC (rev 52079)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,1152 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * WP_REST_Menu_Items_Controller tests
+ *
+ * @package WordPress
+ * @subpackage REST_API
+ * @since 5.9.0
+ */
+
+/**
+ * Tests for REST API for Menu items.
+ *
+ * @group restapi
+ *
+ * @coversDefaultClass WP_REST_Menu_Items_Controller
+ */
+class Tests_REST_WpRestMenuItemsController extends WP_Test_REST_Post_Type_Controller_Testcase {
+ /**
+ * @var int
+ */
+ protected $menu_id;
+ /**
+ * @var int
+ */
+ protected $tag_id;
+ /**
+ * @var int
+ */
+ protected $menu_item_id;
+
+ /**
+ * @var int
+ */
+ protected static $admin_id;
+
+ /**
+ * @var int
+ */
+ protected static $subscriber_id;
+
+ /**
+ *
+ */
+ const POST_TYPE = 'nav_menu_item';
+
+ /**
+ * 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::$subscriber_id = $factory->user->create(
+ array(
+ 'role' => 'subscriber',
+ )
+ );
+ }
+
+ /**
+ *
+ */
+ public static function wpTearDownAfterClass() {
+ self::delete_user( self::$admin_id );
+ self::delete_user( self::$subscriber_id );
+ }
+
+ /**
+ *
+ */
+ public function set_up() {
+ parent::set_up();
+
+ $this->tag_id = self::factory()->tag->create();
+
+ $this->menu_id = wp_create_nav_menu( rand_str() );
+
+ $this->menu_item_id = wp_update_nav_menu_item(
+ $this->menu_id,
+ 0,
+ array(
+ 'menu-item-type' => 'taxonomy',
+ 'menu-item-object' => 'post_tag',
+ 'menu-item-object-id' => $this->tag_id,
+ 'menu-item-status' => 'publish',
+ )
+ );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::register_routes
+ */
+ public function test_register_routes() {
+ $routes = rest_get_server()->get_routes();
+
+ $this->assertArrayHasKey( '/wp/v2/menu-items', $routes );
+ $this->assertCount( 2, $routes['/wp/v2/menu-items'] );
+ $this->assertArrayHasKey( '/wp/v2/menu-items/(?P<id>[\d]+)', $routes );
+ $this->assertCount( 3, $routes['/wp/v2/menu-items/(?P<id>[\d]+)'] );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::get_context_param
+ */
+ public function test_context_param() {
+ // Collection.
+ $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/menu-items' );
+ $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'] );
+ $this->assertSame( array( 'v1' => true ), $data['endpoints'][0]['allow_batch'] );
+ // Single.
+ $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/menu-items/' . $this->menu_item_id );
+ $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'] );
+ $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/menu-items' );
+ $response = rest_get_server()->dispatch( $request );
+ $data = $response->get_data();
+ $properties = $data['endpoints'][0]['args'];
+ $this->assertArrayHasKey( 'before', $properties );
+ $this->assertArrayHasKey( 'context', $properties );
+ $this->assertArrayHasKey( 'exclude', $properties );
+ $this->assertArrayHasKey( 'include', $properties );
+ $this->assertArrayHasKey( 'menu_order', $properties );
+ $this->assertArrayHasKey( 'menus', $properties );
+ $this->assertArrayHasKey( 'menus_exclude', $properties );
+ $this->assertArrayHasKey( 'offset', $properties );
+ $this->assertArrayHasKey( 'order', $properties );
+ $this->assertArrayHasKey( 'orderby', $properties );
+ $this->assertArrayHasKey( 'page', $properties );
+ $this->assertArrayHasKey( 'per_page', $properties );
+ $this->assertArrayHasKey( 'search', $properties );
+ $this->assertArrayHasKey( 'slug', $properties );
+ $this->assertArrayHasKey( 'status', $properties );
+ }
+
+ /**
+ * @ticket 40878
+ */
+ public function test_registered_get_item_params() {
+ $request = new WP_REST_Request( 'OPTIONS', sprintf( '/wp/v2/menu-items/%d', $this->menu_item_id ) );
+ $response = rest_get_server()->dispatch( $request );
+ $data = $response->get_data();
+ $keys = array_keys( $data['endpoints'][0]['args'] );
+ sort( $keys );
+ $this->assertSame( array( 'context', 'id' ), $keys );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::get_items
+ */
+ public function test_get_items() {
+ wp_set_current_user( self::$admin_id );
+ $request = new WP_REST_Request( 'GET', '/wp/v2/menu-items' );
+ $response = rest_get_server()->dispatch( $request );
+
+ $this->check_get_menu_items_response( $response );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::get_item
+ */
+ public function test_get_item() {
+ wp_set_current_user( self::$admin_id );
+ $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/menu-items/%d', $this->menu_item_id ) );
+ $response = rest_get_server()->dispatch( $request );
+
+ $this->check_get_menu_item_response( $response, 'view' );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::get_item
+ */
+ public function test_get_item_edit() {
+ wp_set_current_user( self::$admin_id );
+ $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/menu-items/%d', $this->menu_item_id ) );
+ $request->set_param( 'context', 'edit' );
+ $response = rest_get_server()->dispatch( $request );
+
+ $this->check_get_menu_item_response( $response, 'edit' );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::get_item
+ * @covers ::prepare_links
+ */
+ public function test_get_item_term_links() {
+ wp_set_current_user( self::$admin_id );
+
+ $menu_item_id = wp_update_nav_menu_item(
+ $this->menu_id,
+ 0,
+ array(
+ 'menu-item-type' => 'taxonomy',
+ 'menu-item-object' => 'post_tag',
+ 'menu-item-object-id' => $this->tag_id,
+ 'menu-item-status' => 'publish',
+ 'menu-item-title' => 'Food',
+ )
+ );
+ $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/menu-items/%d', $menu_item_id ) );
+ $request->set_param( 'context', 'edit' );
+ $response = rest_get_server()->dispatch( $request );
+
+ $this->check_get_menu_item_response( $response, 'edit' );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::get_item
+ * @covers ::prepare_links
+ */
+ public function test_get_item_term_posts() {
+ wp_set_current_user( self::$admin_id );
+
+ $post_id = self::factory()->post->create();
+
+ $menu_item_id = wp_update_nav_menu_item(
+ $this->menu_id,
+ 0,
+ array(
+ 'menu-item-type' => 'post_type',
+ 'menu-item-object' => 'post',
+ 'menu-item-object-id' => $post_id,
+ 'menu-item-status' => 'publish',
+ 'menu-item-title' => 'Food',
+ )
+ );
+ $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/menu-items/%d', $menu_item_id ) );
+ $request->set_param( 'context', 'edit' );
+ $response = rest_get_server()->dispatch( $request );
+
+ $this->check_get_menu_item_response( $response, 'edit' );
+ }
+
+ /**
+ * Test that title.raw contains the verbatim title and that title.rendered
+ * has been passed through the_title which escapes & characters.
+ *
+ * @see https://github.com/WordPress/gutenberg/pull/24673
+ *
+ * @ticket 40878
+ * @covers ::get_item
+ */
+ public function test_get_item_escapes_title() {
+ wp_set_current_user( self::$admin_id );
+
+ $menu_item_id = wp_update_nav_menu_item(
+ $this->menu_id,
+ 0,
+ array(
+ 'menu-item-type' => 'taxonomy',
+ 'menu-item-object' => 'post_tag',
+ 'menu-item-object-id' => $this->tag_id,
+ 'menu-item-status' => 'publish',
+ 'menu-item-title' => '<strong>Foo</strong> & bar',
+ )
+ );
+
+ $request = new WP_REST_Request(
+ 'GET',
+ "/wp/v2/menu-items/$menu_item_id"
+ );
+ $request->set_query_params(
+ array(
+ 'context' => 'edit',
+ )
+ );
+
+ $response = rest_get_server()->dispatch( $request );
+
+ $data = $response->get_data();
+ $title = $data['title'];
+
+ if ( ! is_multisite() ) {
+ // Check that title.raw is the unescaped title and that
+ // title.rendered has been run through the_title.
+ $this->assertSame( '<strong>Foo</strong> & bar', $title['rendered'] );
+ $this->assertSame( '<strong>Foo</strong> & bar', $title['raw'] );
+ } else {
+ // In a multisite, administrators do not have unfiltered_html and
+ // post_title is ran through wp_kses before being saved in the
+ // database. Running the title through the_title does nothing in
+ // this case.
+ $this->assertSame( '<strong>Foo</strong> & bar', $title['rendered'] );
+ $this->assertSame( '<strong>Foo</strong> & bar', $title['raw'] );
+ }
+
+ wp_delete_post( $menu_item_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/menu-items' );
+ $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+ $params = $this->set_menu_item_data();
+ $request->set_body_params( $params );
+ $response = rest_get_server()->dispatch( $request );
+
+ $this->check_create_menu_item_response( $response );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::create_item
+ */
+ public function test_create_item_invalid_invalid() {
+ wp_set_current_user( self::$admin_id );
+
+ $request = new WP_REST_Request( 'POST', '/wp/v2/menu-items' );
+ $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+ $params = $this->set_menu_item_data(
+ array(
+ 'menus' => array( 123, 456 ),
+ )
+ );
+ $request->set_body_params( $params );
+ $response = rest_get_server()->dispatch( $request );
+
+ $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::create_item
+ */
+ public function test_create_item_invalid_term() {
+ wp_set_current_user( self::$admin_id );
+
+ $request = new WP_REST_Request( 'POST', '/wp/v2/menu-items' );
+ $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+ $params = $this->set_menu_item_data(
+ array(
+ 'type' => 'taxonomy',
+ 'title' => 'Tags',
+ )
+ );
+ $request->set_body_params( $params );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( 'rest_term_invalid_id', $response, 400 );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::create_item
+ */
+ public function test_create_item_change_position() {
+ wp_set_current_user( self::$admin_id );
+ $new_menu_id = wp_create_nav_menu( rand_str() );
+ $expected = array();
+ $actual = array();
+ for ( $i = 1; $i < 5; $i ++ ) {
+ $request = new WP_REST_Request( 'POST', '/wp/v2/menu-items' );
+ $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+ $params = $this->set_menu_item_data(
+ array(
+ 'menu_order' => $i,
+ 'menus' => $new_menu_id,
+ )
+ );
+ $request->set_body_params( $params );
+ $response = rest_get_server()->dispatch( $request );
+ $this->check_create_menu_item_response( $response );
+ $data = $response->get_data();
+
+ $expected[] = $i;
+ $actual[] = $data['menu_order'];
+ }
+ $this->assertSame( $actual, $expected );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::create_item
+ */
+ public function test_menu_order_must_be_set() {
+ wp_set_current_user( self::$admin_id );
+ $new_menu_id = wp_create_nav_menu( rand_str() );
+
+ $request = new WP_REST_Request( 'POST', '/wp/v2/menu-items' );
+ $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+ $params = $this->set_menu_item_data(
+ array(
+ 'menu_order' => 0,
+ 'menus' => $new_menu_id,
+ )
+ );
+ $request->set_body_params( $params );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
+
+ $request = new WP_REST_Request( 'POST', '/wp/v2/menu-items' );
+ $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+ $params = $this->set_menu_item_data(
+ array(
+ 'menu_order' => 1,
+ 'menus' => $new_menu_id,
+ )
+ );
+ $request->set_body_params( $params );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertSame( 201, $response->get_status() );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::create_item
+ */
+ public function test_create_item_invalid_position_2() {
+ wp_set_current_user( self::$admin_id );
+ $new_menu_id = wp_create_nav_menu( rand_str() );
+ $request = new WP_REST_Request( 'POST', '/wp/v2/menu-items' );
+ $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+ $params = $this->set_menu_item_data(
+ array(
+ 'menu_order' => 'ddddd',
+ 'menus' => $new_menu_id,
+ )
+ );
+ $request->set_body_params( $params );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::create_item
+ */
+ public function test_create_item_invalid_position_3() {
+ wp_set_current_user( self::$admin_id );
+ $new_menu_id = wp_create_nav_menu( rand_str() );
+ $request = new WP_REST_Request( 'POST', '/wp/v2/menu-items' );
+ $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+ $params = $this->set_menu_item_data(
+ array(
+ 'menu_order' => -9,
+ 'menus' => $new_menu_id,
+ )
+ );
+ $request->set_body_params( $params );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::create_item
+ */
+ public function test_create_item_invalid_parent() {
+ wp_set_current_user( self::$admin_id );
+ wp_create_nav_menu( rand_str() );
+ $request = new WP_REST_Request( 'POST', '/wp/v2/menu-items' );
+ $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+ $params = $this->set_menu_item_data(
+ array(
+ 'parent' => -9,
+ )
+ );
+ $request->set_body_params( $params );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::create_item
+ */
+ public function test_create_item_invalid_menu() {
+ wp_set_current_user( self::$admin_id );
+ $request = new WP_REST_Request( 'POST', '/wp/v2/menu-items' );
+ $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+ $params = $this->set_menu_item_data(
+ array(
+ 'menus' => -9,
+ )
+ );
+ $request->set_body_params( $params );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( 'invalid_menu_id', $response, 400 );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::create_item
+ */
+ public function test_create_item_invalid_post() {
+ wp_set_current_user( self::$admin_id );
+
+ $request = new WP_REST_Request( 'POST', '/wp/v2/menu-items' );
+ $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+ $params = $this->set_menu_item_data(
+ array(
+ 'type' => 'post_type',
+ 'title' => 'Post',
+ )
+ );
+ $request->set_body_params( $params );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( 'rest_post_invalid_id', $response, 400 );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::create_item
+ */
+ public function test_create_item_invalid_post_type() {
+ wp_set_current_user( self::$admin_id );
+
+ $request = new WP_REST_Request( 'POST', '/wp/v2/menu-items' );
+ $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+ $params = $this->set_menu_item_data(
+ array(
+ 'type' => 'post_type_archive',
+ 'menu-item-object' => 'invalid_post_type',
+ )
+ );
+ $request->set_body_params( $params );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( 'rest_post_invalid_type', $response, 400 );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::create_item
+ */
+ public function test_create_item_invalid_custom_link() {
+ wp_set_current_user( self::$admin_id );
+
+ $request = new WP_REST_Request( 'POST', '/wp/v2/menu-items' );
+ $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+ $params = $this->set_menu_item_data(
+ array(
+ 'type' => 'custom',
+ 'title' => '',
+ )
+ );
+ $request->set_body_params( $params );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( 'rest_title_required', $response, 400 );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::create_item
+ */
+ public function test_create_item_missing_custom_link_url() {
+ wp_set_current_user( self::$admin_id );
+
+ $request = new WP_REST_Request( 'POST', '/wp/v2/menu-items' );
+ $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+ $params = $this->set_menu_item_data(
+ array(
+ 'type' => 'custom',
+ 'url' => '',
+ )
+ );
+ $request->set_body_params( $params );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( 'rest_url_required', $response, 400 );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::create_item
+ */
+ public function test_create_item_invalid_custom_link_url() {
+ wp_set_current_user( self::$admin_id );
+
+ $request = new WP_REST_Request( 'POST', '/wp/v2/menu-items' );
+ $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+ $params = $this->set_menu_item_data(
+ array(
+ 'type' => 'custom',
+ 'url' => '"^<>{}`',
+ )
+ );
+ $request->set_body_params( $params );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
+ $this->assertArrayHasKey( 'url', $response->get_data()['data']['details'] );
+ $this->assertSame( 'rest_invalid_url', $response->get_data()['data']['details']['url']['code'] );
+ }
+
+ /**
+ * Tests that a block menu item can be created.
+ *
+ * @ticket 40878
+ * @covers ::create_item
+ */
+ public function test_create_item_block() {
+ wp_set_current_user( self::$admin_id );
+ $request = new WP_REST_Request( 'POST', '/wp/v2/menu-items' );
+ $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+ $params = $this->set_menu_item_data(
+ array(
+ 'type' => 'block',
+ 'content' => '<!-- wp:paragraph --><p>Block content</p><!-- /wp:paragraph -->',
+ )
+ );
+ $request->set_body_params( $params );
+ $response = rest_get_server()->dispatch( $request );
+
+ $this->check_create_menu_item_response( $response );
+ }
+
+ /**
+ * Tests that a block menu item can be created.
+ *
+ * @ticket 40878
+ * @covers ::create_item
+ */
+ public function test_create_item_invalid_block_content() {
+ wp_set_current_user( self::$admin_id );
+ $request = new WP_REST_Request( 'POST', '/wp/v2/menu-items' );
+ $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+ $params = $this->set_menu_item_data(
+ array(
+ 'type' => 'block',
+ )
+ );
+ $request->set_body_params( $params );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( 'rest_content_required', $response, 400 );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::update_item
+ */
+ public function test_update_item() {
+ wp_set_current_user( self::$admin_id );
+
+ $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/menu-items/%d', $this->menu_item_id ) );
+ $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+ $params = $this->set_menu_item_data(
+ array(
+ 'xfn' => array( 'test1', 'test2', 'test3' ),
+ )
+ );
+ $request->set_body_params( $params );
+ $response = rest_get_server()->dispatch( $request );
+ $this->check_update_menu_item_response( $response );
+ $new_data = $response->get_data();
+ $this->assertSame( $this->menu_item_id, $new_data['id'] );
+ $this->assertSame( $params['title'], $new_data['title']['raw'] );
+ $this->assertSame( $params['description'], $new_data['description'] );
+ $this->assertSame( $params['type_label'], $new_data['type_label'] );
+ $this->assertSame( $params['xfn'], $new_data['xfn'] );
+ $post = get_post( $this->menu_item_id );
+ $menu_item = wp_setup_nav_menu_item( $post );
+ $this->assertSame( $params['title'], $menu_item->title );
+ $this->assertSame( $params['description'], $menu_item->description );
+ $this->assertSame( $params['xfn'], explode( ' ', $menu_item->xfn ) );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::update_item
+ */
+ public function test_update_item_preserves_type() {
+ wp_set_current_user( self::$admin_id );
+
+ $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/menu-items/%d', $this->menu_item_id ) );
+ $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+ $params = array(
+ 'status' => 'draft',
+ 'type' => 'block',
+ 'title' => 'TEST',
+ 'content' => '<!-- wp:paragraph --><p>Block content</p><!-- /wp:paragraph -->',
+ );
+ $request->set_body_params( $params );
+ $response = rest_get_server()->dispatch( $request );
+ $this->check_update_menu_item_response( $response );
+ $new_data = $response->get_data();
+ $this->assertSame( 'block', $new_data['type'] );
+
+ $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/menu-items/%d', $this->menu_item_id ) );
+ $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+ $params = array(
+ 'status' => 'draft',
+ 'title' => 'TEST2',
+ 'content' => '<!-- wp:paragraph --><p>Block content</p><!-- /wp:paragraph -->',
+ );
+ $request->set_body_params( $params );
+ $response = rest_get_server()->dispatch( $request );
+ $this->check_update_menu_item_response( $response );
+ $new_data = $response->get_data();
+
+ // The type shouldn't change just because it was missing from request args.
+ $this->assertSame( 'block', $new_data['type'] );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::update_item
+ */
+ public function test_update_item_clean_xfn() {
+ wp_set_current_user( self::$admin_id );
+
+ $bad_data = array( 'test1":|":', 'test2+|+', 'test3±', 'test4😀' );
+ $good_data = array( 'test1', 'test2', 'test3', 'test4' );
+
+ $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/menu-items/%d', $this->menu_item_id ) );
+ $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+ $params = $this->set_menu_item_data(
+ array(
+ 'xfn' => $bad_data,
+ )
+ );
+ $request->set_body_params( $params );
+ $response = rest_get_server()->dispatch( $request );
+ $this->check_update_menu_item_response( $response );
+ $new_data = $response->get_data();
+ $this->assertSame( $this->menu_item_id, $new_data['id'] );
+ $this->assertSame( $params['title'], $new_data['title']['raw'] );
+ $this->assertSame( $params['description'], $new_data['description'] );
+ $this->assertSame( $params['type_label'], $new_data['type_label'] );
+ $this->assertSame( $good_data, $new_data['xfn'] );
+ $post = get_post( $this->menu_item_id );
+ $menu_item = wp_setup_nav_menu_item( $post );
+ $this->assertSame( $params['title'], $menu_item->title );
+ $this->assertSame( $params['description'], $menu_item->description );
+ $this->assertSame( $good_data, explode( ' ', $menu_item->xfn ) );
+ }
+
+
+ /**
+ * @ticket 40878
+ * @covers ::update_item
+ */
+ public function test_update_item_invalid() {
+ wp_set_current_user( self::$admin_id );
+ $post_id = self::factory()->post->create();
+
+ $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/menu-items/%d', $post_id ) );
+ $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+ $params = $this->set_menu_item_data();
+ $request->set_body_params( $params );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::delete_item
+ */
+ public function test_delete_item() {
+ wp_set_current_user( self::$admin_id );
+ $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/menu-items/%d', $this->menu_item_id ) );
+ $request->set_param( 'force', true );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertSame( 200, $response->get_status() );
+ $this->assertNull( get_post( $this->menu_item_id ) );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::delete_item
+ */
+ public function test_delete_item_no_force() {
+ wp_set_current_user( self::$admin_id );
+ $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/menu-items/%d', $this->menu_item_id ) );
+ $request->set_param( 'force', false );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertSame( 501, $response->get_status() );
+ $this->assertNotNull( get_post( $this->menu_item_id ) );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::delete_item
+ */
+ public function test_delete_item_invalid() {
+ wp_set_current_user( self::$admin_id );
+ $request = new WP_REST_Request( 'DELETE', '/wp/v2/menu-items/9999' );
+ $request->set_param( 'force', true );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::prepare_item_for_response
+ */
+ public function test_prepare_item() {
+ wp_set_current_user( self::$admin_id );
+ $request = new WP_REST_Request( 'GET', '/wp/v2/menu-items/' . $this->menu_item_id );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertSame( 200, $response->get_status() );
+ $this->check_get_menu_item_response( $response );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::get_item_schema
+ */
+ public function test_get_item_schema() {
+ $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/menu-items' );
+ $response = rest_get_server()->dispatch( $request );
+ $data = $response->get_data();
+ $properties = $data['schema']['properties'];
+ $this->assertSame( 19, count( $properties ) );
+ $this->assertArrayHasKey( 'type_label', $properties );
+ $this->assertArrayHasKey( 'attr_title', $properties );
+ $this->assertArrayHasKey( 'classes', $properties );
+ $this->assertArrayHasKey( 'description', $properties );
+ $this->assertArrayHasKey( 'content', $properties );
+ $this->assertArrayHasKey( 'id', $properties );
+ $this->assertArrayHasKey( 'url', $properties );
+ $this->assertArrayHasKey( 'meta', $properties );
+ $this->assertArrayHasKey( 'menu_order', $properties );
+ $this->assertArrayHasKey( 'object', $properties );
+ $this->assertArrayHasKey( 'object_id', $properties );
+ $this->assertArrayHasKey( 'target', $properties );
+ $this->assertArrayHasKey( 'parent', $properties );
+ $this->assertArrayHasKey( 'status', $properties );
+ $this->assertArrayHasKey( 'title', $properties );
+ $this->assertArrayHasKey( 'type', $properties );
+ $this->assertArrayHasKey( 'xfn', $properties );
+ $this->assertArrayHasKey( 'invalid', $properties );
+ }
+
+ /**
+ * @ticket 40878
+ * @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/menu-items' );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( 'rest_cannot_view', $response, 401 );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::get_item_permissions_check
+ */
+ public function test_get_item_no_permission() {
+ wp_set_current_user( 0 );
+ $request = new WP_REST_Request( 'GET', '/wp/v2/menu-items/' . $this->menu_item_id );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( 'rest_cannot_view', $response, 401 );
+ }
+
+ /**
+ * @ticket 40878
+ * @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/menu-items' );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( 'rest_cannot_view', $response, 403 );
+ }
+
+ /**
+ * @ticket 40878
+ * @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/menu-items/' . $this->menu_item_id );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( 'rest_cannot_view', $response, 403 );
+ }
+
+ /**
+ * @param WP_REST_Response $response Response Class.
+ * @param string $context Defaults to View.
+ */
+ protected function check_get_menu_items_response( $response, $context = 'view' ) {
+ $this->assertNotWPError( $response );
+ $response = rest_ensure_response( $response );
+ $this->assertSame( 200, $response->get_status() );
+
+ $headers = $response->get_headers();
+ $this->assertArrayHasKey( 'X-WP-Total', $headers );
+ $this->assertArrayHasKey( 'X-WP-TotalPages', $headers );
+
+ $all_data = $response->get_data();
+ foreach ( $all_data as $data ) {
+ $post = get_post( $data['id'] );
+ // Base fields for every post.
+ $menu_item = wp_setup_nav_menu_item( $post );
+ /**
+ * As the links for the post are "response_links" format in the data array we have to pull them out and parse them.
+ */
+ $links = $data['_links'];
+ foreach ( $links as &$links_array ) {
+ foreach ( $links_array as &$link ) {
+ $attributes = array_diff_key(
+ $link,
+ array(
+ 'href' => 1,
+ 'name' => 1,
+ )
+ );
+ $link = array_diff_key( $link, $attributes );
+ $link['attributes'] = $attributes;
+ }
+ }
+
+ $this->check_menu_item_data( $menu_item, $data, $context, $links );
+ }
+ }
+
+ /**
+ * @param WP_Post $post WP_Post object.
+ * @param array $data Data compare.
+ * @param string $context Context of REST Request.
+ * @param array $links Array links.
+ */
+ protected function check_menu_item_data( $post, $data, $context, $links ) {
+ $post_type_obj = get_post_type_object( self::POST_TYPE );
+
+ // Standard fields.
+ $this->assertSame( $post->ID, $data['id'] );
+ $this->assertSame( wpautop( $post->post_content ), $data['description'] );
+
+ // Check filtered values.
+ if ( post_type_supports( self::POST_TYPE, 'title' ) ) {
+ add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
+ $this->assertSame( $post->title, $data['title']['rendered'] );
+ remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
+ if ( 'edit' === $context ) {
+ $this->assertSame( $post->title, $data['title']['raw'] );
+ } else {
+ $this->assertFalse( isset( $data['title']['raw'] ) );
+ }
+ } else {
+ $this->assertFalse( isset( $data['title'] ) );
+ }
+
+ // Check content.
+ if ( 'block' === $data['type'] ) {
+ $menu_item_content = get_post_meta( $post->ID, '_menu_item_content', true );
+ $this->assertSame( apply_filters( 'the_content', $menu_item_content ), $data['content']['rendered'] );
+ if ( 'edit' === $context ) {
+ $this->assertSame( $menu_item_content, $data['content']['raw'] );
+ } else {
+ $this->assertFalse( isset( $data['title']['raw'] ) );
+ }
+ $this->assertSame( 1, $data['content']['block_version'] );
+ } else {
+ $this->assertEmpty( $data['content']['rendered'] );
+ }
+
+ // post_parent.
+ $this->assertArrayHasKey( 'parent', $data );
+ if ( $post->post_parent ) {
+ if ( is_int( $data['parent'] ) ) {
+ $this->assertSame( $post->post_parent, $data['parent'] );
+ } else {
+ $this->assertSame( $post->post_parent, $data['parent']['id'] );
+ $menu_item = wp_setup_nav_menu_item( get_post( $data['parent']['id'] ) );
+ $this->check_get_menu_item_response( $data['parent'], $menu_item, 'view-parent' );
+ }
+ } else {
+ $this->assertEmpty( $data['parent'] );
+ }
+
+ $this->assertFalse( $data['invalid'] );
+
+ // page attributes.
+ $this->assertSame( $post->menu_order, $data['menu_order'] );
+
+ $taxonomies = wp_list_filter( get_object_taxonomies( self::POST_TYPE, 'objects' ), array( 'show_in_rest' => true ) );
+ foreach ( $taxonomies as $taxonomy ) {
+ $this->assertTrue( isset( $data[ $taxonomy->rest_base ] ) );
+ $terms = wp_get_object_terms( $post->ID, $taxonomy->name, array( 'fields' => 'ids' ) );
+ sort( $terms );
+ if ( 'nav_menu' === $taxonomy->name ) {
+ $term_id = $terms ? array_shift( $terms ) : 0;
+ $this->assertSame( $term_id, $data[ $taxonomy->rest_base ] );
+ } else {
+ sort( $data[ $taxonomy->rest_base ] );
+ $this->assertSame( $terms, $data[ $taxonomy->rest_base ] );
+ }
+ }
+
+ // test links.
+ if ( $links ) {
+ $links = test_rest_expand_compact_links( $links );
+ $this->assertSame( $links['self'][0]['href'], rest_url( 'wp/v2/' . $post_type_obj->rest_base . '/' . $data['id'] ) );
+ $this->assertSame( $links['collection'][0]['href'], rest_url( 'wp/v2/' . $post_type_obj->rest_base ) );
+ $this->assertSame( $links['about'][0]['href'], rest_url( 'wp/v2/types/' . self::POST_TYPE ) );
+
+ $num = 0;
+ foreach ( $taxonomies as $taxonomy ) {
+ $this->assertSame( $taxonomy->name, $links['https://api.w.org/term'][ $num ]['attributes']['taxonomy'] );
+ $this->assertSame( add_query_arg( 'post', $data['id'], rest_url( 'wp/v2/' . $taxonomy->rest_base ) ), $links['https://api.w.org/term'][ $num ]['href'] );
+ $num ++;
+ }
+
+ if ( 'post_type' === $data['type'] ) {
+ $this->assertArrayHasKey( 'https://api.w.org/menu-item-object', $links );
+ $this->assertArrayHasKey( $data['type'], $links['https://api.w.org/menu-item-object'][0]['attributes'] );
+ $this->assertSame( $links['https://api.w.org/menu-item-object'][0]['href'], rest_url( rest_get_route_for_post( $data['object_id'] ) ) );
+ }
+
+ if ( 'taxonomy' === $data['type'] ) {
+ $this->assertArrayHasKey( 'https://api.w.org/menu-item-object', $links );
+ $this->assertArrayHasKey( $data['type'], $links['https://api.w.org/menu-item-object'][0]['attributes'] );
+ $this->assertSame( $links['https://api.w.org/menu-item-object'][0]['href'], rest_url( rest_get_route_for_term( $data['object_id'] ) ) );
+ }
+ }
+ }
+
+ /**
+ * @param WP_REST_Response $response Response Class.
+ * @param string $context Defaults to View.
+ */
+ protected function check_get_menu_item_response( $response, $context = 'view' ) {
+ $this->assertNotWPError( $response );
+ $response = rest_ensure_response( $response );
+ $this->assertSame( 200, $response->get_status() );
+
+ $data = $response->get_data();
+ $post = get_post( $data['id'] );
+ $menu_item = wp_setup_nav_menu_item( $post );
+ $this->check_menu_item_data( $menu_item, $data, $context, $response->get_links() );
+ }
+
+ /**
+ * @param WP_REST_Response $response Response Class.
+ */
+ protected function check_create_menu_item_response( $response ) {
+ $this->assertNotWPError( $response );
+ $response = rest_ensure_response( $response );
+
+ $this->assertSame( 201, $response->get_status() );
+ $headers = $response->get_headers();
+ $this->assertArrayHasKey( 'Location', $headers );
+
+ $data = $response->get_data();
+ $post = get_post( $data['id'] );
+ $menu_item = wp_setup_nav_menu_item( $post );
+ $this->check_menu_item_data( $menu_item, $data, 'edit', $response->get_links() );
+ }
+
+ /**
+ * @param WP_REST_Response $response Response Class.
+ */
+ protected function check_update_menu_item_response( $response ) {
+ $this->assertNotWPError( $response );
+ $response = rest_ensure_response( $response );
+
+ $this->assertSame( 200, $response->get_status() );
+ $headers = $response->get_headers();
+ $this->assertArrayNotHasKey( 'Location', $headers );
+
+ $data = $response->get_data();
+ $post = get_post( $data['id'] );
+ $menu_item = wp_setup_nav_menu_item( $post );
+ $this->check_menu_item_data( $menu_item, $data, 'edit', $response->get_links() );
+ }
+
+ /**
+ * @param array $args Override params.
+ *
+ * @return mixed
+ */
+ protected function set_menu_item_data( $args = array() ) {
+ $defaults = array(
+ 'object_id' => 0,
+ 'parent' => 0,
+ 'menu_order' => 1,
+ 'menus' => $this->menu_id,
+ 'type' => 'custom',
+ 'title' => 'Custom Link Title',
+ 'url' => '#',
+ 'description' => '',
+ 'attr-title' => '',
+ 'target' => '',
+ 'type_label' => 'Custom Link',
+ 'classes' => '',
+ 'xfn' => '',
+ 'status' => 'draft',
+ );
+
+ return wp_parse_args( $args, $defaults );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::create_item
+ */
+ public function test_create_item_properly_handles_slashed_data() {
+ wp_set_current_user( self::$admin_id );
+
+ $request = new WP_REST_Request( 'POST', '/wp/v2/menu-items' );
+ $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+ $parameters = $this->set_menu_item_data(
+ array(
+ 'title' => 'Some \\\'title',
+ )
+ );
+ $request->set_body_params( $parameters );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertNotWPError( $response->as_error() );
+ $data = $response->get_data();
+ $post = get_post( $data['id'] );
+ $this->assertSame( $parameters['title'], $post->post_title );
+ }
+
+ /**
+ * @ticket 40878
+ * @covers ::update_item
+ */
+ public function test_update_item_properly_handles_slashed_data() {
+ wp_set_current_user( self::$admin_id );
+
+ $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/menu-items/%d', $this->menu_item_id ) );
+ $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+ $title = 'Some \\\'title';
+ $params = $this->set_menu_item_data(
+ array(
+ 'title' => $title,
+ )
+ );
+ $request->set_body_params( $params );
+ $response = rest_get_server()->dispatch( $request );
+ $new_data = $response->get_data();
+ $this->assertSame( $params['title'], $new_data['title']['raw'] );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/rest-api/wpRestMenuItemsController.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestsrestapiwpRestMenuLocationsControllerphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/rest-api/wpRestMenuLocationsController.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/wpRestMenuLocationsController.php (rev 0)
+++ trunk/tests/phpunit/tests/rest-api/wpRestMenuLocationsController.php 2021-11-09 18:58:59 UTC (rev 52079)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,207 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?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() );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/rest-api/wpRestMenuLocationsController.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestsrestapiwpRestMenusControllerphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/rest-api/wpRestMenusController.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/wpRestMenusController.php (rev 0)
+++ trunk/tests/phpunit/tests/rest-api/wpRestMenusController.php 2021-11-09 18:58:59 UTC (rev 52079)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,642 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?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'] );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/rest-api/wpRestMenusController.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsqunitfixtureswpapigeneratedjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/qunit/fixtures/wp-api-generated.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/qunit/fixtures/wp-api-generated.js 2021-11-09 18:49:12 UTC (rev 52078)
+++ trunk/tests/qunit/fixtures/wp-api-generated.js 2021-11-09 18:58:59 UTC (rev 52079)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3415,6 +3415,908 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> ]
</span><span class="cx" style="display: block; padding: 0 10px"> },
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ "/wp/v2/menu-items": {
+ "namespace": "wp/v2",
+ "methods": [
+ "GET",
+ "POST"
+ ],
+ "endpoints": [
+ {
+ "methods": [
+ "GET"
+ ],
+ "allow_batch": {
+ "v1": true
+ },
+ "args": {
+ "context": {
+ "description": "Scope under which the request is made; determines fields present in response.",
+ "type": "string",
+ "enum": [
+ "view",
+ "embed",
+ "edit"
+ ],
+ "default": "view",
+ "required": false
+ },
+ "page": {
+ "description": "Current page of the collection.",
+ "type": "integer",
+ "default": 1,
+ "minimum": 1,
+ "required": false
+ },
+ "per_page": {
+ "description": "Maximum number of items to be returned in result set.",
+ "type": "integer",
+ "default": 100,
+ "minimum": 1,
+ "maximum": 100,
+ "required": false
+ },
+ "search": {
+ "description": "Limit results to those matching a string.",
+ "type": "string",
+ "required": false
+ },
+ "after": {
+ "description": "Limit response to posts published after a given ISO8601 compliant date.",
+ "type": "string",
+ "format": "date-time",
+ "required": false
+ },
+ "modified_after": {
+ "description": "Limit response to posts modified after a given ISO8601 compliant date.",
+ "type": "string",
+ "format": "date-time",
+ "required": false
+ },
+ "before": {
+ "description": "Limit response to posts published before a given ISO8601 compliant date.",
+ "type": "string",
+ "format": "date-time",
+ "required": false
+ },
+ "modified_before": {
+ "description": "Limit response to posts modified before a given ISO8601 compliant date.",
+ "type": "string",
+ "format": "date-time",
+ "required": false
+ },
+ "exclude": {
+ "description": "Ensure result set excludes specific IDs.",
+ "type": "array",
+ "items": {
+ "type": "integer"
+ },
+ "default": [],
+ "required": false
+ },
+ "include": {
+ "description": "Limit result set to specific IDs.",
+ "type": "array",
+ "items": {
+ "type": "integer"
+ },
+ "default": [],
+ "required": false
+ },
+ "offset": {
+ "description": "Offset the result set by a specific number of items.",
+ "type": "integer",
+ "required": false
+ },
+ "order": {
+ "description": "Order sort attribute ascending or descending.",
+ "type": "string",
+ "default": "asc",
+ "enum": [
+ "asc",
+ "desc"
+ ],
+ "required": false
+ },
+ "orderby": {
+ "description": "Sort collection by object attribute.",
+ "type": "string",
+ "default": "menu_order",
+ "enum": [
+ "author",
+ "date",
+ "id",
+ "include",
+ "modified",
+ "parent",
+ "relevance",
+ "slug",
+ "include_slugs",
+ "title",
+ "menu_order"
+ ],
+ "required": false
+ },
+ "slug": {
+ "description": "Limit result set to posts with one or more specific slugs.",
+ "type": "array",
+ "items": {
+ "type": "string"
+ },
+ "required": false
+ },
+ "status": {
+ "default": "publish",
+ "description": "Limit result set to posts assigned one or more statuses.",
+ "type": "array",
+ "items": {
+ "enum": [
+ "publish",
+ "future",
+ "draft",
+ "pending",
+ "private",
+ "trash",
+ "auto-draft",
+ "inherit",
+ "request-pending",
+ "request-confirmed",
+ "request-failed",
+ "request-completed",
+ "any"
+ ],
+ "type": "string"
+ },
+ "required": false
+ },
+ "tax_relation": {
+ "description": "Limit result set based on relationship between multiple taxonomies.",
+ "type": "string",
+ "enum": [
+ "AND",
+ "OR"
+ ],
+ "required": false
+ },
+ "menus": {
+ "description": "Limit result set to items with specific terms assigned in the menus taxonomy.",
+ "type": [
+ "object",
+ "array"
+ ],
+ "oneOf": [
+ {
+ "title": "Term ID List",
+ "description": "Match terms with the listed IDs.",
+ "type": "array",
+ "items": {
+ "type": "integer"
+ }
+ },
+ {
+ "title": "Term ID Taxonomy Query",
+ "description": "Perform an advanced term query.",
+ "type": "object",
+ "properties": {
+ "terms": {
+ "description": "Term IDs.",
+ "type": "array",
+ "items": {
+ "type": "integer"
+ },
+ "default": []
+ },
+ "operator": {
+ "description": "Whether items must be assigned all or any of the specified terms.",
+ "type": "string",
+ "enum": [
+ "AND",
+ "OR"
+ ],
+ "default": "OR"
+ }
+ },
+ "additionalProperties": false
+ }
+ ],
+ "required": false
+ },
+ "menus_exclude": {
+ "description": "Limit result set to items except those with specific terms assigned in the menus taxonomy.",
+ "type": [
+ "object",
+ "array"
+ ],
+ "oneOf": [
+ {
+ "title": "Term ID List",
+ "description": "Match terms with the listed IDs.",
+ "type": "array",
+ "items": {
+ "type": "integer"
+ }
+ },
+ {
+ "title": "Term ID Taxonomy Query",
+ "description": "Perform an advanced term query.",
+ "type": "object",
+ "properties": {
+ "terms": {
+ "description": "Term IDs.",
+ "type": "array",
+ "items": {
+ "type": "integer"
+ },
+ "default": []
+ }
+ },
+ "additionalProperties": false
+ }
+ ],
+ "required": false
+ },
+ "menu_order": {
+ "description": "Limit result set to posts with a specific menu_order value.",
+ "type": "integer",
+ "required": false
+ }
+ }
+ },
+ {
+ "methods": [
+ "POST"
+ ],
+ "allow_batch": {
+ "v1": true
+ },
+ "args": {
+ "title": {
+ "description": "The title for the object.",
+ "type": [
+ "string",
+ "object"
+ ],
+ "properties": {
+ "raw": {
+ "description": "Title for the object, as it exists in the database.",
+ "type": "string",
+ "context": [
+ "edit"
+ ]
+ },
+ "rendered": {
+ "description": "HTML title for the object, transformed for display.",
+ "type": "string",
+ "context": [
+ "view",
+ "edit",
+ "embed"
+ ],
+ "readonly": true
+ }
+ },
+ "required": false
+ },
+ "type": {
+ "default": "custom",
+ "description": "The family of objects originally represented, such as \"post_type\" or \"taxonomy\".",
+ "type": "string",
+ "enum": [
+ "taxonomy",
+ "post_type",
+ "post_type_archive",
+ "custom",
+ "block"
+ ],
+ "required": false
+ },
+ "status": {
+ "default": "publish",
+ "description": "A named status for the object.",
+ "type": "string",
+ "enum": [
+ "publish",
+ "future",
+ "draft",
+ "pending",
+ "private"
+ ],
+ "required": false
+ },
+ "parent": {
+ "default": 0,
+ "description": "The ID for the parent of the object.",
+ "type": "integer",
+ "minimum": 0,
+ "required": false
+ },
+ "attr_title": {
+ "description": "Text for the title attribute of the link element for this menu item.",
+ "type": "string",
+ "required": false
+ },
+ "classes": {
+ "description": "Class names for the link element of this menu item.",
+ "type": "array",
+ "items": {
+ "type": "string"
+ },
+ "required": false
+ },
+ "description": {
+ "description": "The description of this menu item.",
+ "type": "string",
+ "required": false
+ },
+ "menu_order": {
+ "default": 1,
+ "description": "The DB ID of the nav_menu_item that is this item's menu parent, if any, otherwise 0.",
+ "type": "integer",
+ "minimum": 1,
+ "required": false
+ },
+ "object": {
+ "description": "The type of object originally represented, such as \"category,\" \"post\", or \"attachment.\"",
+ "type": "string",
+ "required": false
+ },
+ "object_id": {
+ "default": 0,
+ "description": "The database ID of the original object this menu item represents, for example the ID for posts or the term_id for categories.",
+ "type": "integer",
+ "minimum": 0,
+ "required": false
+ },
+ "content": {
+ "description": "HTML content to display for this block menu item.",
+ "type": [
+ "string",
+ "object"
+ ],
+ "properties": {
+ "raw": {
+ "description": "HTML content, as it exists in the database.",
+ "type": "string",
+ "context": [
+ "edit"
+ ]
+ },
+ "rendered": {
+ "description": "HTML content, transformed for display.",
+ "type": "string",
+ "context": [
+ "view",
+ "edit"
+ ],
+ "readonly": true
+ },
+ "block_version": {
+ "description": "Version of the block format used in the HTML content.",
+ "type": "integer",
+ "context": [
+ "edit"
+ ],
+ "readonly": true
+ }
+ },
+ "required": false
+ },
+ "target": {
+ "description": "The target attribute of the link element for this menu item.",
+ "type": "string",
+ "enum": [
+ "_blank",
+ ""
+ ],
+ "required": false
+ },
+ "url": {
+ "description": "The URL to which this menu item points.",
+ "type": "string",
+ "format": "uri",
+ "required": false
+ },
+ "xfn": {
+ "description": "The XFN relationship expressed in the link of this menu item.",
+ "type": "array",
+ "items": {
+ "type": "string"
+ },
+ "required": false
+ },
+ "menus": {
+ "description": "The terms assigned to the object in the nav_menu taxonomy.",
+ "type": "integer",
+ "required": false
+ },
+ "meta": {
+ "description": "Meta fields.",
+ "type": "object",
+ "properties": [],
+ "required": false
+ }
+ }
+ }
+ ],
+ "_links": {
+ "self": [
+ {
+ "href": "http://example.org/index.php?rest_route=/wp/v2/menu-items"
+ }
+ ]
+ }
+ },
+ "/wp/v2/menu-items/(?P<id>[\\d]+)": {
+ "namespace": "wp/v2",
+ "methods": [
+ "GET",
+ "POST",
+ "PUT",
+ "PATCH",
+ "DELETE"
+ ],
+ "endpoints": [
+ {
+ "methods": [
+ "GET"
+ ],
+ "allow_batch": {
+ "v1": true
+ },
+ "args": {
+ "id": {
+ "description": "Unique identifier for the post.",
+ "type": "integer",
+ "required": false
+ },
+ "context": {
+ "description": "Scope under which the request is made; determines fields present in response.",
+ "type": "string",
+ "enum": [
+ "view",
+ "embed",
+ "edit"
+ ],
+ "default": "view",
+ "required": false
+ }
+ }
+ },
+ {
+ "methods": [
+ "POST",
+ "PUT",
+ "PATCH"
+ ],
+ "allow_batch": {
+ "v1": true
+ },
+ "args": {
+ "id": {
+ "description": "Unique identifier for the post.",
+ "type": "integer",
+ "required": false
+ },
+ "title": {
+ "description": "The title for the object.",
+ "type": [
+ "string",
+ "object"
+ ],
+ "properties": {
+ "raw": {
+ "description": "Title for the object, as it exists in the database.",
+ "type": "string",
+ "context": [
+ "edit"
+ ]
+ },
+ "rendered": {
+ "description": "HTML title for the object, transformed for display.",
+ "type": "string",
+ "context": [
+ "view",
+ "edit",
+ "embed"
+ ],
+ "readonly": true
+ }
+ },
+ "required": false
+ },
+ "type": {
+ "description": "The family of objects originally represented, such as \"post_type\" or \"taxonomy\".",
+ "type": "string",
+ "enum": [
+ "taxonomy",
+ "post_type",
+ "post_type_archive",
+ "custom",
+ "block"
+ ],
+ "required": false
+ },
+ "status": {
+ "description": "A named status for the object.",
+ "type": "string",
+ "enum": [
+ "publish",
+ "future",
+ "draft",
+ "pending",
+ "private"
+ ],
+ "required": false
+ },
+ "parent": {
+ "description": "The ID for the parent of the object.",
+ "type": "integer",
+ "minimum": 0,
+ "required": false
+ },
+ "attr_title": {
+ "description": "Text for the title attribute of the link element for this menu item.",
+ "type": "string",
+ "required": false
+ },
+ "classes": {
+ "description": "Class names for the link element of this menu item.",
+ "type": "array",
+ "items": {
+ "type": "string"
+ },
+ "required": false
+ },
+ "description": {
+ "description": "The description of this menu item.",
+ "type": "string",
+ "required": false
+ },
+ "menu_order": {
+ "description": "The DB ID of the nav_menu_item that is this item's menu parent, if any, otherwise 0.",
+ "type": "integer",
+ "minimum": 1,
+ "required": false
+ },
+ "object": {
+ "description": "The type of object originally represented, such as \"category,\" \"post\", or \"attachment.\"",
+ "type": "string",
+ "required": false
+ },
+ "object_id": {
+ "description": "The database ID of the original object this menu item represents, for example the ID for posts or the term_id for categories.",
+ "type": "integer",
+ "minimum": 0,
+ "required": false
+ },
+ "content": {
+ "description": "HTML content to display for this block menu item.",
+ "type": [
+ "string",
+ "object"
+ ],
+ "properties": {
+ "raw": {
+ "description": "HTML content, as it exists in the database.",
+ "type": "string",
+ "context": [
+ "edit"
+ ]
+ },
+ "rendered": {
+ "description": "HTML content, transformed for display.",
+ "type": "string",
+ "context": [
+ "view",
+ "edit"
+ ],
+ "readonly": true
+ },
+ "block_version": {
+ "description": "Version of the block format used in the HTML content.",
+ "type": "integer",
+ "context": [
+ "edit"
+ ],
+ "readonly": true
+ }
+ },
+ "required": false
+ },
+ "target": {
+ "description": "The target attribute of the link element for this menu item.",
+ "type": "string",
+ "enum": [
+ "_blank",
+ ""
+ ],
+ "required": false
+ },
+ "url": {
+ "description": "The URL to which this menu item points.",
+ "type": "string",
+ "format": "uri",
+ "required": false
+ },
+ "xfn": {
+ "description": "The XFN relationship expressed in the link of this menu item.",
+ "type": "array",
+ "items": {
+ "type": "string"
+ },
+ "required": false
+ },
+ "menus": {
+ "description": "The terms assigned to the object in the nav_menu taxonomy.",
+ "type": "integer",
+ "required": false
+ },
+ "meta": {
+ "description": "Meta fields.",
+ "type": "object",
+ "properties": [],
+ "required": false
+ }
+ }
+ },
+ {
+ "methods": [
+ "DELETE"
+ ],
+ "allow_batch": {
+ "v1": true
+ },
+ "args": {
+ "id": {
+ "description": "Unique identifier for the post.",
+ "type": "integer",
+ "required": false
+ },
+ "force": {
+ "type": "boolean",
+ "default": false,
+ "description": "Whether to bypass Trash and force deletion.",
+ "required": false
+ }
+ }
+ }
+ ]
+ },
+ "/wp/v2/menu-items/(?P<id>[\\d]+)/autosaves": {
+ "namespace": "wp/v2",
+ "methods": [
+ "GET",
+ "POST"
+ ],
+ "endpoints": [
+ {
+ "methods": [
+ "GET"
+ ],
+ "args": {
+ "parent": {
+ "description": "The ID for the parent of the autosave.",
+ "type": "integer",
+ "required": false
+ },
+ "context": {
+ "description": "Scope under which the request is made; determines fields present in response.",
+ "type": "string",
+ "enum": [
+ "view",
+ "embed",
+ "edit"
+ ],
+ "default": "view",
+ "required": false
+ }
+ }
+ },
+ {
+ "methods": [
+ "POST"
+ ],
+ "args": {
+ "parent": {
+ "description": "The ID for the parent of the object.",
+ "type": "integer",
+ "minimum": 0,
+ "required": false
+ },
+ "title": {
+ "description": "The title for the object.",
+ "type": [
+ "string",
+ "object"
+ ],
+ "properties": {
+ "raw": {
+ "description": "Title for the object, as it exists in the database.",
+ "type": "string",
+ "context": [
+ "edit"
+ ]
+ },
+ "rendered": {
+ "description": "HTML title for the object, transformed for display.",
+ "type": "string",
+ "context": [
+ "view",
+ "edit",
+ "embed"
+ ],
+ "readonly": true
+ }
+ },
+ "required": false
+ },
+ "type": {
+ "description": "The family of objects originally represented, such as \"post_type\" or \"taxonomy\".",
+ "type": "string",
+ "enum": [
+ "taxonomy",
+ "post_type",
+ "post_type_archive",
+ "custom",
+ "block"
+ ],
+ "required": false
+ },
+ "status": {
+ "description": "A named status for the object.",
+ "type": "string",
+ "enum": [
+ "publish",
+ "future",
+ "draft",
+ "pending",
+ "private"
+ ],
+ "required": false
+ },
+ "attr_title": {
+ "description": "Text for the title attribute of the link element for this menu item.",
+ "type": "string",
+ "required": false
+ },
+ "classes": {
+ "description": "Class names for the link element of this menu item.",
+ "type": "array",
+ "items": {
+ "type": "string"
+ },
+ "required": false
+ },
+ "description": {
+ "description": "The description of this menu item.",
+ "type": "string",
+ "required": false
+ },
+ "menu_order": {
+ "description": "The DB ID of the nav_menu_item that is this item's menu parent, if any, otherwise 0.",
+ "type": "integer",
+ "minimum": 1,
+ "required": false
+ },
+ "object": {
+ "description": "The type of object originally represented, such as \"category,\" \"post\", or \"attachment.\"",
+ "type": "string",
+ "required": false
+ },
+ "object_id": {
+ "description": "The database ID of the original object this menu item represents, for example the ID for posts or the term_id for categories.",
+ "type": "integer",
+ "minimum": 0,
+ "required": false
+ },
+ "content": {
+ "description": "HTML content to display for this block menu item.",
+ "type": [
+ "string",
+ "object"
+ ],
+ "properties": {
+ "raw": {
+ "description": "HTML content, as it exists in the database.",
+ "type": "string",
+ "context": [
+ "edit"
+ ]
+ },
+ "rendered": {
+ "description": "HTML content, transformed for display.",
+ "type": "string",
+ "context": [
+ "view",
+ "edit"
+ ],
+ "readonly": true
+ },
+ "block_version": {
+ "description": "Version of the block format used in the HTML content.",
+ "type": "integer",
+ "context": [
+ "edit"
+ ],
+ "readonly": true
+ }
+ },
+ "required": false
+ },
+ "target": {
+ "description": "The target attribute of the link element for this menu item.",
+ "type": "string",
+ "enum": [
+ "_blank",
+ ""
+ ],
+ "required": false
+ },
+ "url": {
+ "description": "The URL to which this menu item points.",
+ "type": "string",
+ "format": "uri",
+ "required": false
+ },
+ "xfn": {
+ "description": "The XFN relationship expressed in the link of this menu item.",
+ "type": "array",
+ "items": {
+ "type": "string"
+ },
+ "required": false
+ },
+ "menus": {
+ "description": "The terms assigned to the object in the nav_menu taxonomy.",
+ "type": "integer",
+ "required": false
+ },
+ "meta": {
+ "description": "Meta fields.",
+ "type": "object",
+ "properties": [],
+ "required": false
+ }
+ }
+ }
+ ]
+ },
+ "/wp/v2/menu-items/(?P<parent>[\\d]+)/autosaves/(?P<id>[\\d]+)": {
+ "namespace": "wp/v2",
+ "methods": [
+ "GET"
+ ],
+ "endpoints": [
+ {
+ "methods": [
+ "GET"
+ ],
+ "args": {
+ "parent": {
+ "description": "The ID for the parent of the autosave.",
+ "type": "integer",
+ "required": false
+ },
+ "id": {
+ "description": "The ID for the autosave.",
+ "type": "integer",
+ "required": false
+ },
+ "context": {
+ "description": "Scope under which the request is made; determines fields present in response.",
+ "type": "string",
+ "enum": [
+ "view",
+ "embed",
+ "edit"
+ ],
+ "default": "view",
+ "required": false
+ }
+ }
+ }
+ ]
+ },
</ins><span class="cx" style="display: block; padding: 0 10px"> "/wp/v2/blocks": {
</span><span class="cx" style="display: block; padding: 0 10px"> "namespace": "wp/v2",
</span><span class="cx" style="display: block; padding: 0 10px"> "methods": [
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -6630,6 +7532,286 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> ]
</span><span class="cx" style="display: block; padding: 0 10px"> },
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ "/wp/v2/menus": {
+ "namespace": "wp/v2",
+ "methods": [
+ "GET",
+ "POST"
+ ],
+ "endpoints": [
+ {
+ "methods": [
+ "GET"
+ ],
+ "allow_batch": {
+ "v1": true
+ },
+ "args": {
+ "context": {
+ "description": "Scope under which the request is made; determines fields present in response.",
+ "type": "string",
+ "enum": [
+ "view",
+ "embed",
+ "edit"
+ ],
+ "default": "view",
+ "required": false
+ },
+ "page": {
+ "description": "Current page of the collection.",
+ "type": "integer",
+ "default": 1,
+ "minimum": 1,
+ "required": false
+ },
+ "per_page": {
+ "description": "Maximum number of items to be returned in result set.",
+ "type": "integer",
+ "default": 10,
+ "minimum": 1,
+ "maximum": 100,
+ "required": false
+ },
+ "search": {
+ "description": "Limit results to those matching a string.",
+ "type": "string",
+ "required": false
+ },
+ "exclude": {
+ "description": "Ensure result set excludes specific IDs.",
+ "type": "array",
+ "items": {
+ "type": "integer"
+ },
+ "default": [],
+ "required": false
+ },
+ "include": {
+ "description": "Limit result set to specific IDs.",
+ "type": "array",
+ "items": {
+ "type": "integer"
+ },
+ "default": [],
+ "required": false
+ },
+ "offset": {
+ "description": "Offset the result set by a specific number of items.",
+ "type": "integer",
+ "required": false
+ },
+ "order": {
+ "description": "Order sort attribute ascending or descending.",
+ "type": "string",
+ "default": "asc",
+ "enum": [
+ "asc",
+ "desc"
+ ],
+ "required": false
+ },
+ "orderby": {
+ "description": "Sort collection by term attribute.",
+ "type": "string",
+ "default": "name",
+ "enum": [
+ "id",
+ "include",
+ "name",
+ "slug",
+ "include_slugs",
+ "term_group",
+ "description",
+ "count"
+ ],
+ "required": false
+ },
+ "hide_empty": {
+ "description": "Whether to hide terms not assigned to any posts.",
+ "type": "boolean",
+ "default": false,
+ "required": false
+ },
+ "post": {
+ "description": "Limit result set to terms assigned to a specific post.",
+ "type": "integer",
+ "default": null,
+ "required": false
+ },
+ "slug": {
+ "description": "Limit result set to terms with one or more specific slugs.",
+ "type": "array",
+ "items": {
+ "type": "string"
+ },
+ "required": false
+ }
+ }
+ },
+ {
+ "methods": [
+ "POST"
+ ],
+ "allow_batch": {
+ "v1": true
+ },
+ "args": {
+ "description": {
+ "description": "HTML description of the term.",
+ "type": "string",
+ "required": false
+ },
+ "name": {
+ "description": "HTML title for the term.",
+ "type": "string",
+ "required": true
+ },
+ "slug": {
+ "description": "An alphanumeric identifier for the term unique to its type.",
+ "type": "string",
+ "required": false
+ },
+ "meta": {
+ "description": "Meta fields.",
+ "type": "object",
+ "properties": [],
+ "required": false
+ },
+ "locations": {
+ "description": "The locations assigned to the menu.",
+ "type": "array",
+ "items": {
+ "type": "string"
+ },
+ "required": false
+ },
+ "auto_add": {
+ "description": "Whether to automatically add top level pages to this menu.",
+ "type": "boolean",
+ "required": false
+ }
+ }
+ }
+ ],
+ "_links": {
+ "self": [
+ {
+ "href": "http://example.org/index.php?rest_route=/wp/v2/menus"
+ }
+ ]
+ }
+ },
+ "/wp/v2/menus/(?P<id>[\\d]+)": {
+ "namespace": "wp/v2",
+ "methods": [
+ "GET",
+ "POST",
+ "PUT",
+ "PATCH",
+ "DELETE"
+ ],
+ "endpoints": [
+ {
+ "methods": [
+ "GET"
+ ],
+ "allow_batch": {
+ "v1": true
+ },
+ "args": {
+ "id": {
+ "description": "Unique identifier for the term.",
+ "type": "integer",
+ "required": false
+ },
+ "context": {
+ "description": "Scope under which the request is made; determines fields present in response.",
+ "type": "string",
+ "enum": [
+ "view",
+ "embed",
+ "edit"
+ ],
+ "default": "view",
+ "required": false
+ }
+ }
+ },
+ {
+ "methods": [
+ "POST",
+ "PUT",
+ "PATCH"
+ ],
+ "allow_batch": {
+ "v1": true
+ },
+ "args": {
+ "id": {
+ "description": "Unique identifier for the term.",
+ "type": "integer",
+ "required": false
+ },
+ "description": {
+ "description": "HTML description of the term.",
+ "type": "string",
+ "required": false
+ },
+ "name": {
+ "description": "HTML title for the term.",
+ "type": "string",
+ "required": false
+ },
+ "slug": {
+ "description": "An alphanumeric identifier for the term unique to its type.",
+ "type": "string",
+ "required": false
+ },
+ "meta": {
+ "description": "Meta fields.",
+ "type": "object",
+ "properties": [],
+ "required": false
+ },
+ "locations": {
+ "description": "The locations assigned to the menu.",
+ "type": "array",
+ "items": {
+ "type": "string"
+ },
+ "required": false
+ },
+ "auto_add": {
+ "description": "Whether to automatically add top level pages to this menu.",
+ "type": "boolean",
+ "required": false
+ }
+ }
+ },
+ {
+ "methods": [
+ "DELETE"
+ ],
+ "allow_batch": {
+ "v1": true
+ },
+ "args": {
+ "id": {
+ "description": "Unique identifier for the term.",
+ "type": "integer",
+ "required": false
+ },
+ "force": {
+ "type": "boolean",
+ "default": false,
+ "description": "Required to be true, as terms do not support trashing.",
+ "required": false
+ }
+ }
+ }
+ ]
+ },
</ins><span class="cx" style="display: block; padding: 0 10px"> "/wp/v2/users": {
</span><span class="cx" style="display: block; padding: 0 10px"> "namespace": "wp/v2",
</span><span class="cx" style="display: block; padding: 0 10px"> "methods": [
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -6766,6 +7948,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> "post": "post",
</span><span class="cx" style="display: block; padding: 0 10px"> "page": "page",
</span><span class="cx" style="display: block; padding: 0 10px"> "attachment": "attachment",
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ "nav_menu_item": "nav_menu_item",
</ins><span class="cx" style="display: block; padding: 0 10px"> "wp_block": "wp_block",
</span><span class="cx" style="display: block; padding: 0 10px"> "wp_template": "wp_template",
</span><span class="cx" style="display: block; padding: 0 10px"> "wp_template_part": "wp_template_part",
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -9120,6 +10303,70 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> ]
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ },
+ "/wp/v2/menu-locations": {
+ "namespace": "wp/v2",
+ "methods": [
+ "GET"
+ ],
+ "endpoints": [
+ {
+ "methods": [
+ "GET"
+ ],
+ "args": {
+ "context": {
+ "description": "Scope under which the request is made; determines fields present in response.",
+ "type": "string",
+ "enum": [
+ "view",
+ "embed",
+ "edit"
+ ],
+ "default": "view",
+ "required": false
+ }
+ }
+ }
+ ],
+ "_links": {
+ "self": [
+ {
+ "href": "http://example.org/index.php?rest_route=/wp/v2/menu-locations"
+ }
+ ]
+ }
+ },
+ "/wp/v2/menu-locations/(?P<location>[\\w-]+)": {
+ "namespace": "wp/v2",
+ "methods": [
+ "GET"
+ ],
+ "endpoints": [
+ {
+ "methods": [
+ "GET"
+ ],
+ "args": {
+ "location": {
+ "description": "An alphanumeric identifier for the menu location.",
+ "type": "string",
+ "required": false
+ },
+ "context": {
+ "description": "Scope under which the request is made; determines fields present in response.",
+ "type": "string",
+ "enum": [
+ "view",
+ "embed",
+ "edit"
+ ],
+ "default": "view",
+ "required": false
+ }
+ }
+ }
+ ]
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> },
</span><span class="cx" style="display: block; padding: 0 10px"> "site_logo": false
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -9995,6 +11242,36 @@
</span><span class="cx" style="display: block; padding: 0 10px"> ]
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> },
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ "nav_menu_item": {
+ "description": "",
+ "hierarchical": false,
+ "name": "Navigation Menu Items",
+ "slug": "nav_menu_item",
+ "taxonomies": [
+ "nav_menu"
+ ],
+ "rest_base": "menu-items",
+ "rest_namespace": "wp/v2",
+ "_links": {
+ "collection": [
+ {
+ "href": "http://example.org/index.php?rest_route=/wp/v2/types"
+ }
+ ],
+ "wp:items": [
+ {
+ "href": "http://example.org/index.php?rest_route=/wp/v2/menu-items"
+ }
+ ],
+ "curies": [
+ {
+ "name": "wp",
+ "href": "https://api.w.org/{rel}",
+ "templated": true
+ }
+ ]
+ }
+ },
</ins><span class="cx" style="display: block; padding: 0 10px"> "wp_block": {
</span><span class="cx" style="display: block; padding: 0 10px"> "description": "",
</span><span class="cx" style="display: block; padding: 0 10px"> "hierarchical": false,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -10277,6 +11554,36 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> ]
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ },
+ "nav_menu": {
+ "name": "Navigation Menus",
+ "slug": "nav_menu",
+ "description": "",
+ "types": [
+ "nav_menu_item"
+ ],
+ "hierarchical": false,
+ "rest_base": "menus",
+ "rest_namespace": "wp/v2",
+ "_links": {
+ "collection": [
+ {
+ "href": "http://example.org/index.php?rest_route=/wp/v2/taxonomies"
+ }
+ ],
+ "wp:items": [
+ {
+ "href": "http://example.org/index.php?rest_route=/wp/v2/menus"
+ }
+ ],
+ "curies": [
+ {
+ "name": "wp",
+ "href": "https://api.w.org/{rel}",
+ "templated": true
+ }
+ ]
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> };
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre>
</div>
</div>
</body>
</html>