Skip to content

Commit

Permalink
Add schemas for navigation menus.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbillion committed Aug 20, 2024
1 parent f5b4be7 commit 20713d4
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 5 deletions.
10 changes: 10 additions & 0 deletions packages/wp-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ export type WP_REST_API_Page = WP_REST_API_Partial_Post_Common &
* A collection of page objects in a REST API context.
*/
export type WP_REST_API_Pages = WP_REST_API_Page[];
/**
* A navigation menu object in a REST API context.
*/
export type WP_REST_API_Navigation_Menu = WP_REST_API_Partial_Post_Common;
/**
* A collection of navigation menu objects in a REST API context.
*/
export type WP_REST_API_Navigation_Menus = WP_REST_API_Navigation_Menu[];
/**
* A collection of media attachment objects in a REST API context.
*/
Expand Down Expand Up @@ -236,6 +244,8 @@ export interface WP {
Posts: WP_REST_API_Posts;
Page: WP_REST_API_Page;
Pages: WP_REST_API_Pages;
Navigation_Menu: WP_REST_API_Navigation_Menu;
Navigation_Menus: WP_REST_API_Navigation_Menus;
Attachment: WP_REST_API_Attachment;
Attachments: WP_REST_API_Attachments;
Block: WP_REST_API_Block;
Expand Down
4 changes: 2 additions & 2 deletions packages/wp-types/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ Route | Schema
/wp/v2/menu-locations/{location} | `WP_REST_API_Menu_Location`
/wp/v2/menus | `WP_REST_API_Menus`
/wp/v2/menus/{id} | `WP_REST_API_Menu`
/wp/v2/navigation | Todo
/wp/v2/navigation/{id} | Todo
/wp/v2/navigation | `WP_REST_API_Navigation_Menus`
/wp/v2/navigation/{id} | `WP_REST_API_Navigation_Menu`
/wp/v2/navigation/{id}/autosaves | Todo
/wp/v2/navigation/{parent}/autosaves/{id} | Todo
/wp/v2/navigation/{parent}/revisions | Todo
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ Route | Schema
/wp/v2/menu-locations/{location} | `WP_REST_API_Menu_Location`
/wp/v2/menus | `WP_REST_API_Menus`
/wp/v2/menus/{id} | `WP_REST_API_Menu`
/wp/v2/navigation | Todo
/wp/v2/navigation/{id} | Todo
/wp/v2/navigation | `WP_REST_API_Navigation_Menus`
/wp/v2/navigation/{id} | `WP_REST_API_Navigation_Menu`
/wp/v2/navigation/{id}/autosaves | Todo
/wp/v2/navigation/{parent}/autosaves/{id} | Todo
/wp/v2/navigation/{parent}/revisions | Todo
Expand Down
8 changes: 8 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@
"Pages": {
"$ref": "schemas/rest-api/collections/pages.json"
},
"Navigation_Menu": {
"$ref": "schemas/rest-api/navigation-menu.json"
},
"Navigation_Menus": {
"$ref": "schemas/rest-api/collections/navigation.json"
},
"Attachment": {
"$ref": "schemas/rest-api/attachment.json"
},
Expand Down Expand Up @@ -247,6 +253,8 @@
"Posts",
"Page",
"Pages",
"Navigation_Menu",
"Navigation_Menus",
"Attachment",
"Attachments",
"Block_Directory_Item",
Expand Down
19 changes: 19 additions & 0 deletions schemas/rest-api/collections/navigation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"$schema": "https://json-schema.org/draft/2019-09/hyper-schema",
"$id": "https://raw.githubusercontent.com/johnbillion/wp-json-schemas/trunk/schemas/rest-api/collections/navigation.json",
"title": "WP_REST_API_Navigation_Menus",
"description": "A collection of navigation menu objects in a REST API context.",
"type": "array",
"items": {
"$ref": "../navigation-menu.json"
},
"links": [
{
"rel": "self",
"href": "/wp/v2/navigation",
"targetSchema": {
"$ref": "#"
}
}
]
}
55 changes: 55 additions & 0 deletions schemas/rest-api/navigation-menu.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"$schema": "https://json-schema.org/draft/2019-09/hyper-schema",
"$id": "https://raw.githubusercontent.com/johnbillion/wp-json-schemas/trunk/schemas/rest-api/navigation-menu.json",
"title": "WP_REST_API_Navigation_Menu",
"description": "A navigation menu object in a REST API context.",
"type": "object",
"allOf": [
{
"$ref": "partials/post/common.json"
}
],
"links": [
{
"rel": "self",
"href": "/wp/v2/navigation/{id}",
"hrefSchema": {
"properties": {
"id": {
"$ref": "partials/post/common.json#/properties/id"
}
}
},
"targetSchema": {
"$ref": "#"
}
},
{
"rel": "collection",
"href": "/wp/v2/navigation",
"targetSchema": {
"type": "array",
"items": {
"$ref": "#"
}
}
},
{
"rel": "version-history",
"href": "/wp/v2/navigation/{id}/revisions",
"hrefSchema": {
"properties": {
"id": {
"$ref": "partials/post/common.json#/properties/id"
}
}
},
"targetSchema": {
"type": "array",
"items": {
"$ref": "#"
}
}
}
]
}
9 changes: 8 additions & 1 deletion tests/output/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
'post_status' => 'publish',
] );

wp_insert_post( [
'post_type' => 'wp_navigation',
'post_title' => 'Navigation',
'post_content' => '<!-- wp:navigation-link {"label":"Title","type":"page","id":123,"url":"/title/","kind":"post-type"} /-->',
'post_status' => 'publish',
] );

$posts = get_posts( [
'posts_per_page' => -1,
'post_status' => 'any',
Expand All @@ -31,7 +38,7 @@

save_object_array( $posts, 'post' );

foreach ( [ 'posts', 'pages', 'blocks' ] as $type ) {
foreach ( [ 'posts', 'pages', 'blocks', 'navigation' ] as $type ) {
$view_data = get_rest_response( 'GET', "/wp/v2/{$type}", [
'context' => 'view',
'per_page' => 100,
Expand Down

0 comments on commit 20713d4

Please sign in to comment.