<!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>[52133] trunk: REST API: Add /wp/v2/block-navigation-areas endpoint</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/52133">52133</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/52133","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>noisysocks</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-11-11 03:50:22 +0000 (Thu, 11 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: Add /wp/v2/block-navigation-areas endpoint
Copies WP_REST_Block_Navigation_Areas_Controller from the Gutenberg plugin. This
provides the /wp/v2/block-navigation-areas endpoint used by the Navigation
block.
Props antonvlasenko, TimothyBlynJacobs.
Fixes <a href="https://core.trac.wordpress.org/ticket/54393">#54393</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesdefaultfiltersphp">trunk/src/wp-includes/default-filters.php</a></li>
<li><a href="#trunksrcwpincludesrestapiphp">trunk/src/wp-includes/rest-api.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="#trunktestsqunitfixtureswpapigeneratedjs">trunk/tests/qunit/fixtures/wp-api-generated.js</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesnavigationareasphp">trunk/src/wp-includes/navigation-areas.php</a></li>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestblocknavigationareascontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-navigation-areas-controller.php</a></li>
<li><a href="#trunktestsphpunittestsrestapiwpRestBlockNavigationAreasControllerphp">trunk/tests/phpunit/tests/rest-api/wpRestBlockNavigationAreasController.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesdefaultfiltersphp"></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/default-filters.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/default-filters.php 2021-11-11 02:47:10 UTC (rev 52132)
+++ trunk/src/wp-includes/default-filters.php 2021-11-11 03:50:22 UTC (rev 52133)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -669,4 +669,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'wp_footer', 'the_block_template_skip_link' );
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'setup_theme', 'wp_enable_block_templates' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+// Navigation areas.
+add_action( 'setup_theme', '_register_default_navigation_areas' );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> unset( $filter, $action );
</span></span></pre></div>
<a id="trunksrcwpincludesnavigationareasphp"></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/navigation-areas.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/navigation-areas.php (rev 0)
+++ trunk/src/wp-includes/navigation-areas.php 2021-11-11 03:50:22 UTC (rev 52133)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,52 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Block navigation areas functions.
+ *
+ * @package WordPress
+ */
+
+/**
+ * Registers the navigation areas supported by the current theme. The expected
+ * shape of the argument is:
+ * array(
+ * 'primary' => 'Primary',
+ * 'secondary' => 'Secondary',
+ * 'tertiary' => 'Tertiary',
+ * )
+ *
+ * @since 5.9.0
+ *
+ * @param array $new_areas Supported navigation areas.
+ */
+function register_navigation_areas( $new_areas ) {
+ global $navigation_areas;
+ $navigation_areas = $new_areas;
+}
+
+/**
+ * Register the default navigation areas.
+ *
+ * @since 5.9.0
+ * @access private
+ */
+function _register_default_navigation_areas() {
+ register_navigation_areas(
+ array(
+ 'primary' => _x( 'Primary', 'navigation area' ),
+ 'secondary' => _x( 'Secondary', 'navigation area' ),
+ 'tertiary' => _x( 'Tertiary', 'navigation area' ),
+ )
+ );
+}
+
+/**
+ * Returns the available navigation areas.
+ *
+ * @since 5.9.0
+ *
+ * @return array Registered navigation areas.
+ */
+function get_navigation_areas() {
+ global $navigation_areas;
+ return $navigation_areas;
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/src/wp-includes/navigation-areas.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="trunksrcwpincludesrestapiendpointsclasswprestblocknavigationareascontrollerphp"></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-block-navigation-areas-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-block-navigation-areas-controller.php (rev 0)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-navigation-areas-controller.php 2021-11-11 03:50:22 UTC (rev 52133)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,310 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * REST API: WP_REST_Block_Navigation_Areas_Controller class
+ *
+ * @subpackage REST_API
+ * @package WordPress
+ */
+
+/**
+ * Core class used to access block navigation areas via the REST API.
+ *
+ * @since 5.9.0
+ *
+ * @see WP_REST_Controller
+ */
+class WP_REST_Block_Navigation_Areas_Controller extends WP_REST_Controller {
+
+ /**
+ * Constructor.
+ *
+ * @since 5.9.0
+ */
+ public function __construct() {
+ $this->namespace = 'wp/v2';
+ $this->rest_base = 'block-navigation-areas';
+ }
+
+ /**
+ * 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<area>[\w-]+)',
+ array(
+ 'args' => array(
+ 'area' => array(
+ 'description' => __( 'An alphanumeric identifier for the navigation area.' ),
+ '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' ) ),
+ ),
+ ),
+ array(
+ 'methods' => WP_REST_Server::EDITABLE,
+ 'callback' => array( $this, 'update_item' ),
+ 'permission_callback' => array( $this, 'update_item_permissions_check' ),
+ 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
+ ),
+ 'allow_batch' => array( 'v1' => true ),
+ 'schema' => array( $this, 'get_public_item_schema' ),
+ )
+ );
+ }
+
+ /**
+ * Checks whether a given request has permission to read navigation areas.
+ *
+ * @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 navigation areas.' ),
+ array( 'status' => rest_authorization_required_code() )
+ );
+ }
+
+ return true;
+ }
+
+ /**
+ * Retrieves all navigation areas, 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_navigation_areas() as $name => $description ) {
+ $area = $this->get_navigation_area_object( $name );
+ $area = $this->prepare_item_for_response( $area, $request );
+ $data[] = $this->prepare_response_for_collection( $area );
+ }
+ return rest_ensure_response( $data );
+ }
+
+ /**
+ * Checks if a given request has access to read a navigation area.
+ *
+ * @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 navigation areas.' ),
+ array( 'status' => rest_authorization_required_code() )
+ );
+ }
+ if ( ! array_key_exists( $request['area'], get_navigation_areas() ) ) {
+ return new WP_Error( 'rest_navigation_area_invalid', __( 'Invalid navigation area.' ), array( 'status' => 404 ) );
+ }
+
+ return true;
+ }
+
+ /**
+ * Checks if a request has access to update the specified term.
+ *
+ * @since 5.9.0
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return bool|WP_Error True if the request has access to update the item, false or WP_Error object otherwise.
+ */
+ public function update_item_permissions_check( $request ) {
+ return $this->get_item_permissions_check( $request );
+ }
+
+ /**
+ * Retrieves a specific navigation area.
+ *
+ * @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 ) {
+ $name = $request['area'];
+ $area = $this->get_navigation_area_object( $name );
+ $data = $this->prepare_item_for_response( $area, $request );
+
+ return rest_ensure_response( $data );
+ }
+
+ /**
+ * Updates a specific navigation area.
+ *
+ * @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 update_item( $request ) {
+ $name = $request['area'];
+
+ $mapping = get_option( 'fse_navigation_areas', array() );
+ $mapping[ $name ] = $request['navigation'];
+ update_option( 'fse_navigation_areas', $mapping );
+
+ $area = $this->get_navigation_area_object( $name );
+ $data = $this->prepare_item_for_response( $area, $request );
+ return rest_ensure_response( $data );
+ }
+
+ /**
+ * Converts navigation area name to a convenient object that this endpoint can reason about.
+ *
+ * @since 5.9.0
+ *
+ * @param string $name Navigation area name.
+ * @return stdClass An object representation of the navigation area.
+ */
+ protected function get_navigation_area_object( $name ) {
+ $available_areas = get_navigation_areas();
+ $mapping = get_option( 'fse_navigation_areas', array() );
+ $area = new stdClass();
+ $area->name = $name;
+ $area->navigation = ! empty( $mapping[ $name ] ) ? $mapping[ $name ] : null;
+ $area->description = $available_areas[ $name ];
+ return $area;
+ }
+
+ /**
+ * Prepares a navigation area object for serialization.
+ *
+ * @since 5.9.0
+ *
+ * @param stdClass $area Post status data.
+ * @param WP_REST_Request $request Full details about the request.
+ * @return WP_REST_Response Post status data.
+ */
+ public function prepare_item_for_response( $area, $request ) {
+ $areas = get_navigation_areas();
+ $navigation = ( isset( $areas[ $area->name ] ) ) ? $area->navigation : 0;
+
+ $fields = $this->get_fields_for_response( $request );
+ $data = array();
+
+ if ( rest_is_field_included( 'name', $fields ) ) {
+ $data['name'] = $area->name;
+ }
+
+ if ( rest_is_field_included( 'description', $fields ) ) {
+ $data['description'] = $area->description;
+ }
+
+ if ( rest_is_field_included( 'navigation', $fields ) ) {
+ $data['navigation'] = (int) $navigation;
+ }
+
+ $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 );
+
+ /**
+ * Filters a navigation area returned from the REST API.
+ *
+ * @since 5.9.0
+ *
+ * Allows modification of the navigation area data right before it is
+ * returned.
+ *
+ * @param WP_REST_Response $response The response object.
+ * @param object $area The original status object.
+ * @param WP_REST_Request $request Request used to generate the response.
+ */
+ return apply_filters( 'rest_prepare_navigation_area', $response, $area, $request );
+ }
+
+ /**
+ * Retrieves the navigation area'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->schema;
+ }
+
+ $this->schema = array(
+ '$schema' => 'http://json-schema.org/draft-04/schema#',
+ 'title' => 'navigation-area',
+ 'type' => 'object',
+ 'properties' => array(
+ 'name' => array(
+ 'description' => __( 'The name of the navigation area.' ),
+ 'type' => 'string',
+ 'context' => array( 'embed', 'view', 'edit' ),
+ 'readonly' => true,
+ ),
+ 'description' => array(
+ 'description' => __( 'The description of the navigation area.' ),
+ 'type' => 'string',
+ 'context' => array( 'embed', 'view', 'edit' ),
+ 'readonly' => true,
+ ),
+ 'navigation' => array(
+ 'description' => __( 'The ID of the assigned navigation.' ),
+ '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' ) ),
+ );
+ }
+
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-navigation-areas-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="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-11 02:47:10 UTC (rev 52132)
+++ trunk/src/wp-includes/rest-api.php 2021-11-11 03:50:22 UTC (rev 52133)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -349,6 +349,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // Menu Locations.
</span><span class="cx" style="display: block; padding: 0 10px"> $controller = new WP_REST_Menu_Locations_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">+
+ // Block Navigation Areas
+ $controller = new WP_REST_Block_Navigation_Areas_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="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-11 02:47:10 UTC (rev 52132)
+++ trunk/src/wp-settings.php 2021-11-11 03:50:22 UTC (rev 52133)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -244,8 +244,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/widgets.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/class-wp-widget.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/class-wp-widget-factory.php';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+require ABSPATH . WPINC . '/nav-menu-template.php';
</ins><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/nav-menu.php';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-require ABSPATH . WPINC . '/nav-menu-template.php';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+require ABSPATH . WPINC . '/navigation-areas.php';
</ins><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/admin-bar.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/class-wp-application-passwords.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api.php';
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -275,6 +276,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-themes-controller.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-plugins-controller.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-block-directory-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-block-navigation-areas-controller.php';
</ins><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-application-passwords-controller.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-site-health-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-11 02:47:10 UTC (rev 52132)
+++ trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php 2021-11-11 03:50:22 UTC (rev 52133)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -171,6 +171,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/navigation/(?P<parent>[\d]+)/autosaves/(?P<id>[\d]+)',
</span><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/navigation/(?P<parent>[\d]+)/revisions',
</span><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/navigation/(?P<parent>[\d]+)/revisions/(?P<id>[\d]+)',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ '/wp/v2/block-navigation-areas',
+ '/wp/v2/block-navigation-areas/(?P<area>[\w-]+)',
</ins><span class="cx" style="display: block; padding: 0 10px"> '/wp-site-health/v1',
</span><span class="cx" style="display: block; padding: 0 10px"> '/wp-site-health/v1/tests/background-updates',
</span><span class="cx" style="display: block; padding: 0 10px"> '/wp-site-health/v1/tests/loopback-requests',
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapiwpRestBlockNavigationAreasControllerphp"></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/wpRestBlockNavigationAreasController.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/wpRestBlockNavigationAreasController.php (rev 0)
+++ trunk/tests/phpunit/tests/rest-api/wpRestBlockNavigationAreasController.php 2021-11-11 03:50:22 UTC (rev 52133)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,136 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Unit tests covering WP_REST_Block_Navigation_Areas_Controller functionality.
+ *
+ * @package WordPress
+ * @subpackage REST_API
+ * @group restapi
+ * @since 5.9.0
+ */
+class Tests_REST_WpRestBlockNavigationAreasController extends WP_Test_REST_Controller_Testcase {
+
+ const OPTION = 'fse_navigation_areas';
+
+ /**
+ * @var int Administrator user ID
+ */
+ protected static $admin_id;
+
+ /**
+ * @var array Contains mapping
+ */
+ protected static $old_mapping;
+
+ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+ self::$admin_id = $factory->user->create(
+ array(
+ 'role' => 'administrator',
+ )
+ );
+
+ static::$old_mapping = get_option( static::OPTION, array() );
+ }
+
+ public static function wpTearDownAfterClass() {
+ self::delete_user( self::$admin_id );
+ update_option( static::OPTION, static::$old_mapping );
+ }
+
+ public function test_get_items() {
+ wp_set_current_user( static::$admin_id );
+ $request = new WP_REST_Request( 'GET', '/wp/v2/block-navigation-areas' );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertSame( 200, $response->get_status() );
+ $data = $response->get_data();
+ $this->assertIsArray( $data );
+ $navigation_areas = get_navigation_areas();
+ $expected_data = array();
+ foreach ( $navigation_areas as $name => $navigation_area ) {
+ $expected_data[] = array(
+ 'name' => $name,
+ 'description' => $navigation_area,
+ 'navigation' => 0,
+ );
+ }
+ $this->assertSameSets( $expected_data, $data );
+ }
+
+ public function test_register_routes() {
+ wp_set_current_user( static::$admin_id );
+ $routes = rest_get_server()->get_routes();
+ $this->assertArrayHasKey( '/wp/v2/block-navigation-areas', $routes );
+ $this->assertArrayHasKey( '/wp/v2/block-navigation-areas/(?P<area>[\\w-]+)', $routes );
+ }
+
+ public function test_context_param() {
+ wp_set_current_user( static::$admin_id );
+ $request = new WP_REST_Request( Requests::OPTIONS, '/wp/v2/block-navigation-areas' );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertSame( 200, $response->get_status() );
+ $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'] );
+ }
+
+ public function test_get_item() {
+ wp_set_current_user( static::$admin_id );
+ $navigation_area = array_rand( get_navigation_areas(), 1 );
+
+ $this->assertIsString( $navigation_area );
+ $this->assertNotEmpty( $navigation_area );
+
+ $route = sprintf( '/wp/v2/block-navigation-areas/%s', urlencode( $navigation_area ) );
+ $request = new WP_REST_Request( 'GET', $route );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertSame( 200, $response->get_status() );
+ $data = $response->get_data();
+ $this->assertIsArray( $data );
+ $this->assertArrayHasKey( 'name', $data );
+ $this->assertSame( $navigation_area, $data['name'] );
+ }
+
+ public function test_create_item() {
+ // We cannot create new navigation areas using the current block navigation areas API,
+ // so the test should be marked as passed.
+ $this->markTestSkipped();
+ }
+
+ public function test_update_item() {
+ wp_set_current_user( static::$admin_id );
+ $navigation_area = array_rand( get_navigation_areas(), 1 );
+ $route = sprintf( '/wp/v2/block-navigation-areas/%s', urlencode( $navigation_area ) );
+ $request = new WP_REST_Request( Requests::POST, $route );
+
+ $updated_navigation_area = array(
+ 'name' => $navigation_area,
+ 'description' => 'Test Description',
+ );
+
+ $request->set_param( 'navigation', $updated_navigation_area );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertSame( 200, $response->get_status() );
+ $new_mapping = get_option( static::OPTION, array() );
+ $this->assertSame( $new_mapping[ $navigation_area ], $updated_navigation_area );
+ }
+
+ public function test_delete_item() {
+ // We cannot delete navigation areas using the current block navigation areas API,
+ // so the test should be marked as passed.
+ $this->markTestSkipped();
+ }
+
+ public function test_prepare_item() {
+ // The current block navigation areas API doesn't implement any custom prepare_item logic
+ // so there is nothing to test.
+ $this->markTestSkipped();
+ }
+
+ public function test_get_item_schema() {
+ // The current block navigation areas API doesn't implement any custom item schema
+ // so there is nothing to test.
+ $this->markTestSkipped();
+ }
+}
+
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/rest-api/wpRestBlockNavigationAreasController.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-11 02:47:10 UTC (rev 52132)
+++ trunk/tests/qunit/fixtures/wp-api-generated.js 2021-11-11 03:50:22 UTC (rev 52133)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -10367,6 +10367,93 @@
</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/block-navigation-areas": {
+ "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/block-navigation-areas"
+ }
+ ]
+ }
+ },
+ "/wp/v2/block-navigation-areas/(?P<area>[\\w-]+)": {
+ "namespace": "wp/v2",
+ "methods": [
+ "GET",
+ "POST",
+ "PUT",
+ "PATCH"
+ ],
+ "endpoints": [
+ {
+ "methods": [
+ "GET"
+ ],
+ "allow_batch": {
+ "v1": true
+ },
+ "args": {
+ "area": {
+ "description": "An alphanumeric identifier for the navigation area.",
+ "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
+ }
+ }
+ },
+ {
+ "methods": [
+ "POST",
+ "PUT",
+ "PATCH"
+ ],
+ "allow_batch": {
+ "v1": true
+ },
+ "args": {
+ "area": {
+ "description": "An alphanumeric identifier for the navigation area.",
+ "type": "string",
+ "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": 0,
</span></span></pre>
</div>
</div>
</body>
</html>