Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a schema for global styles #75

Merged
merged 3 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions packages/wp-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,32 @@ 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 global styles variations in a REST API context.
*/
export type WP_REST_API_Global_Style_Variations = {
/**
* Version number of the global styles variation.
*/
version: number;
/**
* Global styles.
*/
styles?: {
[k: string]: unknown;
};
/**
* Global settings.
*/
settings?: {
[k: string]: unknown;
};
/**
* Title of the global styles variation.
*/
title: string;
[k: string]: unknown;
}[];
/**
* A collection of media attachment objects in a REST API context.
*/
Expand Down Expand Up @@ -246,6 +272,9 @@ export interface WP {
Pages: WP_REST_API_Pages;
Navigation_Menu: WP_REST_API_Navigation_Menu;
Navigation_Menus: WP_REST_API_Navigation_Menus;
Global_Style_Variation: WP_REST_API_Global_Style_Variation;
Global_Style_Variations: WP_REST_API_Global_Style_Variations;
Global_Style_Config: WP_REST_API_Global_Style_Config;
Attachment: WP_REST_API_Attachment;
Attachments: WP_REST_API_Attachments;
Block: WP_REST_API_Block;
Expand Down Expand Up @@ -2377,6 +2406,63 @@ interface WP_REST_API_Partial_Post_Excerpt {
protected: boolean;
};
}
/**
* A global styles variation item in a REST API context.
*/
export interface WP_REST_API_Global_Style_Variation {
/**
* ID of global styles variation.
*/
id: number;
/**
* Global styles.
*/
styles: {
[k: string]: unknown;
};
/**
* Global settings.
*/
settings: {
[k: string]: unknown;
};
/**
* Title of the global styles variation.
*/
title:
| string
| {
/**
* Title for the global styles variation, as it exists in the database.
*/
raw?: string;
/**
* HTML title for the post, transformed for display.
*/
rendered?: string;
};
_links: WP_REST_API_Object_Links;
[k: string]: unknown;
}
/**
* A theme's global style config in a REST API context.
*/
export interface WP_REST_API_Global_Style_Config {
/**
* Global styles.
*/
styles: {
[k: string]: unknown;
};
/**
* Global settings.
*/
settings: {
[k: string]: unknown;
};
_links: WP_REST_API_Object_Links;
[k: string]: unknown;
}
/**
* A media attachment object in a REST API context.
*/
Expand Down
6 changes: 3 additions & 3 deletions packages/wp-types/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ Route | Schema
/wp/v2/font-families/{id}/ | `WP_REST_API_Font_Family`
/wp/v2/font-families/{parent}/font-faces | `WP_REST_API_Font_Faces`
/wp/v2/font-families/{parent}/font-faces/{id} | `WP_REST_API_Font_Face`
/wp/v2/global-styles/{id} | Todo
/wp/v2/global-styles/{id} | `WP_REST_API_Global_Style_Variation`
/wp/v2/global-styles/{parent}/revisions | Todo
/wp/v2/global-styles/{parent}/revisions/{id} | Todo
/wp/v2/global-styles/themes/{stylesheet}/variations | Todo
/wp/v2/global-styles/themes/{stylesheet} | Todo
/wp/v2/global-styles/themes/{stylesheet}/variations | `WP_REST_API_Global_Style_Variations`
/wp/v2/global-styles/themes/{stylesheet} | `WP_REST_API_Global_Style_Config`
/wp/v2/media | `WP_REST_API_Attachments`
/wp/v2/media/{id} | `WP_REST_API_Attachment`
/wp/v2/media/{id}/edit | Todo
Expand Down
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ Route | Schema
/wp/v2/font-families/{id}/ | `WP_REST_API_Font_Family`
/wp/v2/font-families/{parent}/font-faces | `WP_REST_API_Font_Faces`
/wp/v2/font-families/{parent}/font-faces/{id} | `WP_REST_API_Font_Face`
/wp/v2/global-styles/{id} | Todo
/wp/v2/global-styles/{id} | `WP_REST_API_Global_Style_Variation`
/wp/v2/global-styles/{parent}/revisions | Todo
/wp/v2/global-styles/{parent}/revisions/{id} | Todo
/wp/v2/global-styles/themes/{stylesheet}/variations | Todo
/wp/v2/global-styles/themes/{stylesheet} | Todo
/wp/v2/global-styles/themes/{stylesheet}/variations | `WP_REST_API_Global_Style_Variations`
/wp/v2/global-styles/themes/{stylesheet} | `WP_REST_API_Global_Style_Config`
/wp/v2/media | `WP_REST_API_Attachments`
/wp/v2/media/{id} | `WP_REST_API_Attachment`
/wp/v2/media/{id}/edit | Todo
Expand Down
12 changes: 12 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@
"Navigation_Menus": {
"$ref": "schemas/rest-api/collections/navigation.json"
},
"Global_Style_Variation": {
"$ref": "schemas/rest-api/global-style-variation.json"
},
"Global_Style_Variations": {
"$ref": "schemas/rest-api/collections/global-style-variations.json"
},
"Global_Style_Config": {
"$ref": "schemas/rest-api/global-style-config.json"
},
"Attachment": {
"$ref": "schemas/rest-api/attachment.json"
},
Expand Down Expand Up @@ -255,6 +264,9 @@
"Pages",
"Navigation_Menu",
"Navigation_Menus",
"Global_Style_Variation",
"Global_Style_Variations",
"Global_Style_Config",
"Attachment",
"Attachments",
"Block_Directory_Item",
Expand Down
49 changes: 49 additions & 0 deletions schemas/rest-api/collections/global-style-variations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"$schema": "https://json-schema.org/draft/2019-09/hyper-schema",
"$id": "https://raw.githubusercontent.com/johnbillion/wp-json-schemas/trunk/schemas/rest-api/collections/global-style-variations.json",
"title": "WP_REST_API_Global_Style_Variations",
"description": "A collection of global styles variations in a REST API context.",
"type": "array",
"items": {
"$comment": "These items are not the same shape as a single global styles variation object.",
"type": "object",
"required": [
"version",
"title"
],
"properties": {
"version": {
"description": "Version number of the global styles variation.",
"type": "integer"
},
"styles": {
"description": "Global styles.",
"type": "object"
},
"settings": {
"description": "Global settings.",
"type": "object"
},
"title": {
"description": "Title of the global styles variation.",
"type": "string"
}
}
},
"links": [
{
"rel": "self",
"href": "/wp/v2/global-styles/themes/{theme}/variations",
"hrefSchema": {
"properties": {
"id": {
"type": "string"
}
}
},
"targetSchema": {
"$ref": "#"
}
}
]
}
41 changes: 41 additions & 0 deletions schemas/rest-api/global-style-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"$schema": "https://json-schema.org/draft/2019-09/hyper-schema",
"$id": "https://raw.githubusercontent.com/johnbillion/wp-json-schemas/trunk/schemas/rest-api/global-style-config.json",
"title": "WP_REST_API_Global_Style_Config",
"description": "A theme's global style config in a REST API context.",
"type": "object",
"required": [
"styles",
"settings",
"_links"
],
"properties": {
"styles": {
"description": "Global styles.",
"type": "object"
},
"settings": {
"description": "Global settings.",
"type": "object"
},
"_links": {
"$ref": "properties/object-links.json"
}
},
"links": [
{
"rel": "self",
"href": "/wp/v2/global-styles/themes/{theme}",
"hrefSchema": {
"properties": {
"id": {
"type": "string"
}
}
},
"targetSchema": {
"$ref": "#"
}
}
]
}
83 changes: 83 additions & 0 deletions schemas/rest-api/global-style-variation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"$schema": "https://json-schema.org/draft/2019-09/hyper-schema",
"$id": "https://raw.githubusercontent.com/johnbillion/wp-json-schemas/trunk/schemas/rest-api/global-style-variation.json",
"title": "WP_REST_API_Global_Style_Variation",
"description": "A global styles variation item in a REST API context.",
"type": "object",
"required": [
"id",
"styles",
"settings",
"title",
"_links"
],
"properties": {
"id": {
"description": "ID of global styles variation.",
"type": "integer"
},
"styles": {
"description": "Global styles.",
"type": "object"
},
"settings": {
"description": "Global settings.",
"type": "object"
},
"title": {
"description": "Title of the global styles variation.",
"oneOf": [
{
"type": "string"
},
{
"type": "object",
"properties": {
"raw": {
"description": "Title for the global styles variation, as it exists in the database.",
"type": "string"
},
"rendered": {
"description": "HTML title for the post, transformed for display.",
"type": "string"
}
},
"additionalProperties": false
}
]
},
"_links": {
"$ref": "properties/object-links.json"
}
},
"links": [
{
"rel": "self",
"href": "/wp/v2/global-styles/{id}",
"hrefSchema": {
"properties": {
"id": {
"$ref": "#/properties/id"
}
}
},
"targetSchema": {
"$ref": "#"
}
},
{
"rel": "collection",
"href": "/wp/v2/global-styles/themes/{theme}/variations",
"hrefSchema": {
"properties": {
"id": {
"type": "string"
}
}
},
"targetSchema": {
"$ref": "collections/global-style-variations.json"
}
}
]
}
4 changes: 4 additions & 0 deletions tests/bin/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,7 @@ for file in schemas/rest-api/collections/*.json
do
validate_schema "$file"
done

# Validation for REST API entities that don't have a collection:
validate_schema schemas/rest-api/global-style-variation.json
validate_schema schemas/rest-api/global-style-config.json
8 changes: 6 additions & 2 deletions tests/mu-plugins/mu-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,16 @@ function save_object_array( array $data, string $dir ) : void {
* @param WP_REST_Response[] $data Array of responses to a REST API request.
* @param string $dir The directory to save the files.
*/
function save_rest_array( array $data, string $dir ) : void {
function save_rest_array( array $data, string $dir, bool $single = false ) : void {
if ( empty( $data ) ) {
throw new \Exception( "No REST API data to save for {$dir}." );
}

$dir = dirname( ABSPATH ) . '/data/rest-api/collections/' . $dir;
if ( $single ) {
$dir = dirname( ABSPATH ) . '/data/rest-api/' . $dir;
} else {
$dir = dirname( ABSPATH ) . '/data/rest-api/collections/' . $dir;
}

if ( ! file_exists( $dir ) ) {
mkdir( $dir, 0777, true );
Expand Down
Loading