diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php
index 37cd32d5aa..412251d2f2 100644
--- a/src/wp-includes/default-filters.php
+++ b/src/wp-includes/default-filters.php
@@ -669,4 +669,7 @@ add_action( 'save_post_wp_template_part', 'wp_set_unique_slug_on_create_template
add_action( 'wp_footer', 'the_block_template_skip_link' );
add_action( 'setup_theme', 'wp_enable_block_templates' );
+// Navigation areas.
+add_action( 'setup_theme', '_register_default_navigation_areas' );
+
unset( $filter, $action );
diff --git a/src/wp-includes/navigation-areas.php b/src/wp-includes/navigation-areas.php
new file mode 100644
index 0000000000..fd4536199b
--- /dev/null
+++ b/src/wp-includes/navigation-areas.php
@@ -0,0 +1,52 @@
+ '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;
+}
diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php
index ba0f7a08de..517daf513c 100644
--- a/src/wp-includes/rest-api.php
+++ b/src/wp-includes/rest-api.php
@@ -349,6 +349,10 @@ function create_initial_rest_routes() {
// Menu Locations.
$controller = new WP_REST_Menu_Locations_Controller();
$controller->register_routes();
+
+ // Block Navigation Areas
+ $controller = new WP_REST_Block_Navigation_Areas_Controller();
+ $controller->register_routes();
}
/**
diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-navigation-areas-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-navigation-areas-controller.php
new file mode 100644
index 0000000000..ef515f5970
--- /dev/null
+++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-navigation-areas-controller.php
@@ -0,0 +1,310 @@
+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[\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' ) ),
+ );
+ }
+
+}
diff --git a/src/wp-settings.php b/src/wp-settings.php
index 1293c82208..0a55449bfc 100644
--- a/src/wp-settings.php
+++ b/src/wp-settings.php
@@ -244,8 +244,9 @@ require ABSPATH . WPINC . '/class-wp-http-requests-hooks.php';
require ABSPATH . WPINC . '/widgets.php';
require ABSPATH . WPINC . '/class-wp-widget.php';
require ABSPATH . WPINC . '/class-wp-widget-factory.php';
-require ABSPATH . WPINC . '/nav-menu.php';
require ABSPATH . WPINC . '/nav-menu-template.php';
+require ABSPATH . WPINC . '/nav-menu.php';
+require ABSPATH . WPINC . '/navigation-areas.php';
require ABSPATH . WPINC . '/admin-bar.php';
require ABSPATH . WPINC . '/class-wp-application-passwords.php';
require ABSPATH . WPINC . '/rest-api.php';
@@ -275,6 +276,7 @@ require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-settings-controller
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-themes-controller.php';
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-plugins-controller.php';
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-block-directory-controller.php';
+require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-block-navigation-areas-controller.php';
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php';
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-application-passwords-controller.php';
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-site-health-controller.php';
diff --git a/tests/phpunit/tests/rest-api/rest-schema-setup.php b/tests/phpunit/tests/rest-api/rest-schema-setup.php
index 1ed1d89611..500f6acdd0 100644
--- a/tests/phpunit/tests/rest-api/rest-schema-setup.php
+++ b/tests/phpunit/tests/rest-api/rest-schema-setup.php
@@ -171,6 +171,8 @@ class WP_Test_REST_Schema_Initialization extends WP_Test_REST_TestCase {
'/wp/v2/navigation/(?P[\d]+)/autosaves/(?P[\d]+)',
'/wp/v2/navigation/(?P[\d]+)/revisions',
'/wp/v2/navigation/(?P[\d]+)/revisions/(?P[\d]+)',
+ '/wp/v2/block-navigation-areas',
+ '/wp/v2/block-navigation-areas/(?P[\w-]+)',
'/wp-site-health/v1',
'/wp-site-health/v1/tests/background-updates',
'/wp-site-health/v1/tests/loopback-requests',
diff --git a/tests/phpunit/tests/rest-api/wpRestBlockNavigationAreasController.php b/tests/phpunit/tests/rest-api/wpRestBlockNavigationAreasController.php
new file mode 100644
index 0000000000..b35fc75ad8
--- /dev/null
+++ b/tests/phpunit/tests/rest-api/wpRestBlockNavigationAreasController.php
@@ -0,0 +1,136 @@
+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[\\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();
+ }
+}
+
diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js
index eb5633ba25..3e4b8f80ad 100644
--- a/tests/qunit/fixtures/wp-api-generated.js
+++ b/tests/qunit/fixtures/wp-api-generated.js
@@ -10367,6 +10367,93 @@ mockedApiResponse.Schema = {
}
}
]
+ },
+ "/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[\\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
+ }
+ }
+ }
+ ]
}
},
"site_logo": 0,