From b2e43c48b7f01b47542a709aefbfe6ab460323d1 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Thu, 22 Aug 2024 17:28:11 +0100 Subject: [PATCH 1/3] Add a schema for global styles. --- packages/wp-types/index.ts | 39 +++++++++++++++++ packages/wp-types/readme.md | 2 +- readme.md | 2 +- schema.json | 4 ++ schemas/rest-api/global-style.json | 69 ++++++++++++++++++++++++++++++ tests/bin/test.sh | 3 ++ tests/mu-plugins/mu-plugin.php | 8 +++- tests/output/post.php | 23 ++++++++++ 8 files changed, 146 insertions(+), 4 deletions(-) create mode 100644 schemas/rest-api/global-style.json diff --git a/packages/wp-types/index.ts b/packages/wp-types/index.ts index 6eabbee..6e4cbf6 100644 --- a/packages/wp-types/index.ts +++ b/packages/wp-types/index.ts @@ -246,6 +246,7 @@ export interface WP { Pages: WP_REST_API_Pages; Navigation_Menu: WP_REST_API_Navigation_Menu; Navigation_Menus: WP_REST_API_Navigation_Menus; + Global_Style: WP_REST_API_Global_Style; Attachment: WP_REST_API_Attachment; Attachments: WP_REST_API_Attachments; Block: WP_REST_API_Block; @@ -2377,6 +2378,44 @@ interface WP_REST_API_Partial_Post_Excerpt { protected: boolean; }; } +/** + * A global style item in a REST API context. + */ +export interface WP_REST_API_Global_Style { + /** + * ID of global styles config. + */ + 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 media attachment object in a REST API context. */ diff --git a/packages/wp-types/readme.md b/packages/wp-types/readme.md index 19d30dc..6e47933 100644 --- a/packages/wp-types/readme.md +++ b/packages/wp-types/readme.md @@ -58,7 +58,7 @@ 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` /wp/v2/global-styles/{parent}/revisions | Todo /wp/v2/global-styles/{parent}/revisions/{id} | Todo /wp/v2/global-styles/themes/{stylesheet}/variations | Todo diff --git a/readme.md b/readme.md index fba4f85..9e30c3f 100644 --- a/readme.md +++ b/readme.md @@ -62,7 +62,7 @@ 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` /wp/v2/global-styles/{parent}/revisions | Todo /wp/v2/global-styles/{parent}/revisions/{id} | Todo /wp/v2/global-styles/themes/{stylesheet}/variations | Todo diff --git a/schema.json b/schema.json index 27f4f82..bbbe6db 100644 --- a/schema.json +++ b/schema.json @@ -116,6 +116,9 @@ "Navigation_Menus": { "$ref": "schemas/rest-api/collections/navigation.json" }, + "Global_Style": { + "$ref": "schemas/rest-api/global-style.json" + }, "Attachment": { "$ref": "schemas/rest-api/attachment.json" }, @@ -255,6 +258,7 @@ "Pages", "Navigation_Menu", "Navigation_Menus", + "Global_Style", "Attachment", "Attachments", "Block_Directory_Item", diff --git a/schemas/rest-api/global-style.json b/schemas/rest-api/global-style.json new file mode 100644 index 0000000..bb1a113 --- /dev/null +++ b/schemas/rest-api/global-style.json @@ -0,0 +1,69 @@ +{ + "$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.json", + "title": "WP_REST_API_Global_Style", + "description": "A global style item in a REST API context.", + "type": "object", + "required": [ + "id", + "styles", + "settings", + "title", + "_links" + ], + "properties": { + "id": { + "description": "ID of global styles config.", + "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": "#" + } + } + ] +} diff --git a/tests/bin/test.sh b/tests/bin/test.sh index 261541b..685267f 100755 --- a/tests/bin/test.sh +++ b/tests/bin/test.sh @@ -70,3 +70,6 @@ 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.json diff --git a/tests/mu-plugins/mu-plugin.php b/tests/mu-plugins/mu-plugin.php index a5d9f5b..37a4941 100644 --- a/tests/mu-plugins/mu-plugin.php +++ b/tests/mu-plugins/mu-plugin.php @@ -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 ); diff --git a/tests/output/post.php b/tests/output/post.php index 35a2281..00eb814 100644 --- a/tests/output/post.php +++ b/tests/output/post.php @@ -58,6 +58,13 @@ 'post_status' => 'publish', ] ); +$global_style = wp_insert_post( [ + 'post_type' => 'wp_global_styles', + 'post_title' => 'Style Title', + 'post_content' => '{"styles": {"blocks": {"core/image": {"filter": {"duotone": "var(--wp--preset--duotone--duotone-2)"}}},"elements": {"button": {"border": {"radius": "100px"}}}},"settings": {"color": {"gradients": {"theme": [{"slug": "gradient-1","gradient": "linear-gradient(to bottom, #f6decd 0%, #dbab88 100%)","name": "Vertical linen to beige"},{"slug": "gradient-2","gradient": "linear-gradient(to bottom, #A4A4A4 0%, #dbab88 100%)","name": "Vertical taupe to beige"}]}}},"isGlobalStylesUserThemeJSON": true,"version": 3}', + 'post_status' => 'publish', +] ); + $posts = get_posts( [ 'posts_per_page' => -1, 'post_status' => 'any', @@ -88,3 +95,19 @@ $empty_response, ], $type ); } + +$view_data = get_rest_response( 'GET', "/wp/v2/global-styles/{$global_style}", [ + 'context' => 'view', +] ); +$edit_data = get_rest_response( 'GET', "/wp/v2/global-styles/{$global_style}", [ + 'context' => 'edit', +] ); + +save_rest_array( + [ + $view_data, + $edit_data, + ], + 'global-style', + true, +); From 08cd22ab69b8fd3f5cf9521e97c0ed29f07da1a7 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Thu, 22 Aug 2024 19:33:32 +0100 Subject: [PATCH 2/3] Add schema for theme global styles config. --- packages/wp-types/index.ts | 26 ++++++++++-- packages/wp-types/readme.md | 4 +- readme.md | 4 +- schema.json | 10 +++-- schemas/rest-api/global-style-config.json | 41 +++++++++++++++++++ ...style.json => global-style-variation.json} | 23 +++++++++-- tests/bin/test.sh | 3 +- tests/output/post.php | 41 ++++++++++++++++++- 8 files changed, 136 insertions(+), 16 deletions(-) create mode 100644 schemas/rest-api/global-style-config.json rename schemas/rest-api/{global-style.json => global-style-variation.json} (73%) diff --git a/packages/wp-types/index.ts b/packages/wp-types/index.ts index 6e4cbf6..10f0644 100644 --- a/packages/wp-types/index.ts +++ b/packages/wp-types/index.ts @@ -246,7 +246,8 @@ export interface WP { Pages: WP_REST_API_Pages; Navigation_Menu: WP_REST_API_Navigation_Menu; Navigation_Menus: WP_REST_API_Navigation_Menus; - Global_Style: WP_REST_API_Global_Style; + Global_Style_Variation: WP_REST_API_Global_Style_Variation; + Global_Style_Config: WP_REST_API_Global_Style_Config; Attachment: WP_REST_API_Attachment; Attachments: WP_REST_API_Attachments; Block: WP_REST_API_Block; @@ -2379,9 +2380,9 @@ interface WP_REST_API_Partial_Post_Excerpt { }; } /** - * A global style item in a REST API context. + * A global style variation item in a REST API context. */ -export interface WP_REST_API_Global_Style { +export interface WP_REST_API_Global_Style_Variation { /** * ID of global styles config. */ @@ -2416,6 +2417,25 @@ export interface WP_REST_API_Global_Style { _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. */ diff --git a/packages/wp-types/readme.md b/packages/wp-types/readme.md index 6e47933..6354e06 100644 --- a/packages/wp-types/readme.md +++ b/packages/wp-types/readme.md @@ -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} | `WP_REST_API_Global_Style` +/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} | `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 diff --git a/readme.md b/readme.md index 9e30c3f..4790683 100644 --- a/readme.md +++ b/readme.md @@ -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} | `WP_REST_API_Global_Style` +/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} | `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 diff --git a/schema.json b/schema.json index bbbe6db..df2fb79 100644 --- a/schema.json +++ b/schema.json @@ -116,8 +116,11 @@ "Navigation_Menus": { "$ref": "schemas/rest-api/collections/navigation.json" }, - "Global_Style": { - "$ref": "schemas/rest-api/global-style.json" + "Global_Style_Variation": { + "$ref": "schemas/rest-api/global-style-variation.json" + }, + "Global_Style_Config": { + "$ref": "schemas/rest-api/global-style-config.json" }, "Attachment": { "$ref": "schemas/rest-api/attachment.json" @@ -258,7 +261,8 @@ "Pages", "Navigation_Menu", "Navigation_Menus", - "Global_Style", + "Global_Style_Variation", + "Global_Style_Config", "Attachment", "Attachments", "Block_Directory_Item", diff --git a/schemas/rest-api/global-style-config.json b/schemas/rest-api/global-style-config.json new file mode 100644 index 0000000..fdbc9e8 --- /dev/null +++ b/schemas/rest-api/global-style-config.json @@ -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": "#" + } + } + ] +} diff --git a/schemas/rest-api/global-style.json b/schemas/rest-api/global-style-variation.json similarity index 73% rename from schemas/rest-api/global-style.json rename to schemas/rest-api/global-style-variation.json index bb1a113..cbb9f54 100644 --- a/schemas/rest-api/global-style.json +++ b/schemas/rest-api/global-style-variation.json @@ -1,8 +1,8 @@ { "$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.json", - "title": "WP_REST_API_Global_Style", - "description": "A global style item in a REST API context.", + "$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 style variation item in a REST API context.", "type": "object", "required": [ "id", @@ -64,6 +64,23 @@ "targetSchema": { "$ref": "#" } + }, + { + "rel": "collection", + "href": "/wp/v2/global-styles/themes/{theme}/variations", + "hrefSchema": { + "properties": { + "id": { + "type": "string" + } + } + }, + "targetSchema": { + "type": "array", + "items": { + "$ref": "#" + } + } } ] } diff --git a/tests/bin/test.sh b/tests/bin/test.sh index 685267f..cc9a9cb 100755 --- a/tests/bin/test.sh +++ b/tests/bin/test.sh @@ -72,4 +72,5 @@ do done # Validation for REST API entities that don't have a collection: -validate_schema schemas/rest-api/global-style.json +validate_schema schemas/rest-api/global-style-variation.json +validate_schema schemas/rest-api/global-style-config.json diff --git a/tests/output/post.php b/tests/output/post.php index 00eb814..92c4aff 100644 --- a/tests/output/post.php +++ b/tests/output/post.php @@ -2,6 +2,8 @@ namespace WPJsonSchemas; +$theme = wp_get_theme()->get_stylesheet(); + $parent_post = wp_insert_post( [ 'post_type' => 'post', 'post_title' => 'Post Title', @@ -60,7 +62,7 @@ $global_style = wp_insert_post( [ 'post_type' => 'wp_global_styles', - 'post_title' => 'Style Title', + 'post_title' => 'Global Style Variation Title', 'post_content' => '{"styles": {"blocks": {"core/image": {"filter": {"duotone": "var(--wp--preset--duotone--duotone-2)"}}},"elements": {"button": {"border": {"radius": "100px"}}}},"settings": {"color": {"gradients": {"theme": [{"slug": "gradient-1","gradient": "linear-gradient(to bottom, #f6decd 0%, #dbab88 100%)","name": "Vertical linen to beige"},{"slug": "gradient-2","gradient": "linear-gradient(to bottom, #A4A4A4 0%, #dbab88 100%)","name": "Vertical taupe to beige"}]}}},"isGlobalStylesUserThemeJSON": true,"version": 3}', 'post_status' => 'publish', ] ); @@ -75,6 +77,7 @@ save_object_array( $posts, 'post' ); +// Generate REST API responses for all post types foreach ( [ 'posts', 'pages', 'blocks', 'navigation' ] as $type ) { $view_data = get_rest_response( 'GET', "/wp/v2/{$type}", [ 'context' => 'view', @@ -96,6 +99,7 @@ ], $type ); } +// Generate REST API responses for a single global style variation $view_data = get_rest_response( 'GET', "/wp/v2/global-styles/{$global_style}", [ 'context' => 'view', ] ); @@ -108,6 +112,39 @@ $view_data, $edit_data, ], - 'global-style', + 'global-style-variation', + true, +); + +// Generate REST API responses for the global style variations collection +$view_data = get_rest_response( 'GET', "/wp/v2/global-styles/themes/{$theme}/variations", [ + 'context' => 'view', +] ); +$edit_data = get_rest_response( 'GET', "/wp/v2/global-styles/themes/{$theme}/variations", [ + 'context' => 'edit', +] ); + +save_rest_array( + [ + $view_data, + $edit_data, + ], + 'global-style-variations', +); + +// Generate REST API responses for the theme global style config +$view_data = get_rest_response( 'GET', "/wp/v2/global-styles/themes/{$theme}", [ + 'context' => 'view', +] ); +$edit_data = get_rest_response( 'GET', "/wp/v2/global-styles/themes/{$theme}", [ + 'context' => 'edit', +] ); + +save_rest_array( + [ + $view_data, + $edit_data, + ], + 'global-style-config', true, ); From 2ad37a27dfc13c4a744dd38b385a89aaea33ab19 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Fri, 23 Aug 2024 14:00:39 +0100 Subject: [PATCH 3/3] Add schema for the global styles variations collection. --- packages/wp-types/index.ts | 31 +++++++++- packages/wp-types/readme.md | 2 +- readme.md | 2 +- schema.json | 4 ++ .../collections/global-style-variations.json | 49 +++++++++++++++ schemas/rest-api/global-style-variation.json | 9 +-- tests/output/post.php | 61 ++++++++++++------- 7 files changed, 126 insertions(+), 32 deletions(-) create mode 100644 schemas/rest-api/collections/global-style-variations.json diff --git a/packages/wp-types/index.ts b/packages/wp-types/index.ts index 10f0644..7c8e965 100644 --- a/packages/wp-types/index.ts +++ b/packages/wp-types/index.ts @@ -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. */ @@ -247,6 +273,7 @@ export interface WP { 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; @@ -2380,11 +2407,11 @@ interface WP_REST_API_Partial_Post_Excerpt { }; } /** - * A global style variation item in a REST API context. + * A global styles variation item in a REST API context. */ export interface WP_REST_API_Global_Style_Variation { /** - * ID of global styles config. + * ID of global styles variation. */ id: number; /** diff --git a/packages/wp-types/readme.md b/packages/wp-types/readme.md index 6354e06..b3f9615 100644 --- a/packages/wp-types/readme.md +++ b/packages/wp-types/readme.md @@ -61,7 +61,7 @@ Route | Schema /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}/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` diff --git a/readme.md b/readme.md index 4790683..4b71cc0 100644 --- a/readme.md +++ b/readme.md @@ -65,7 +65,7 @@ Route | Schema /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}/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` diff --git a/schema.json b/schema.json index df2fb79..ee255f1 100644 --- a/schema.json +++ b/schema.json @@ -119,6 +119,9 @@ "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" }, @@ -262,6 +265,7 @@ "Navigation_Menu", "Navigation_Menus", "Global_Style_Variation", + "Global_Style_Variations", "Global_Style_Config", "Attachment", "Attachments", diff --git a/schemas/rest-api/collections/global-style-variations.json b/schemas/rest-api/collections/global-style-variations.json new file mode 100644 index 0000000..a750803 --- /dev/null +++ b/schemas/rest-api/collections/global-style-variations.json @@ -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": "#" + } + } + ] +} diff --git a/schemas/rest-api/global-style-variation.json b/schemas/rest-api/global-style-variation.json index cbb9f54..104b848 100644 --- a/schemas/rest-api/global-style-variation.json +++ b/schemas/rest-api/global-style-variation.json @@ -2,7 +2,7 @@ "$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 style variation item in a REST API context.", + "description": "A global styles variation item in a REST API context.", "type": "object", "required": [ "id", @@ -13,7 +13,7 @@ ], "properties": { "id": { - "description": "ID of global styles config.", + "description": "ID of global styles variation.", "type": "integer" }, "styles": { @@ -76,10 +76,7 @@ } }, "targetSchema": { - "type": "array", - "items": { - "$ref": "#" - } + "$ref": "collections/global-style-variations.json" } } ] diff --git a/tests/output/post.php b/tests/output/post.php index 92c4aff..ad59f7c 100644 --- a/tests/output/post.php +++ b/tests/output/post.php @@ -2,7 +2,9 @@ namespace WPJsonSchemas; -$theme = wp_get_theme()->get_stylesheet(); +$theme = wp_get_theme(); +$theme_name = $theme->get_stylesheet(); +$theme_dir = $theme->get_stylesheet_directory(); $parent_post = wp_insert_post( [ 'post_type' => 'post', @@ -60,12 +62,26 @@ 'post_status' => 'publish', ] ); -$global_style = wp_insert_post( [ - 'post_type' => 'wp_global_styles', - 'post_title' => 'Global Style Variation Title', - 'post_content' => '{"styles": {"blocks": {"core/image": {"filter": {"duotone": "var(--wp--preset--duotone--duotone-2)"}}},"elements": {"button": {"border": {"radius": "100px"}}}},"settings": {"color": {"gradients": {"theme": [{"slug": "gradient-1","gradient": "linear-gradient(to bottom, #f6decd 0%, #dbab88 100%)","name": "Vertical linen to beige"},{"slug": "gradient-2","gradient": "linear-gradient(to bottom, #A4A4A4 0%, #dbab88 100%)","name": "Vertical taupe to beige"}]}}},"isGlobalStylesUserThemeJSON": true,"version": 3}', - 'post_status' => 'publish', -] ); +$global_style_ids = []; + +foreach ( glob( $theme_dir . '/styles/*.json' ) as $variation ) { + $json = file_get_contents( $variation ); + $data = json_decode( $json, true ); + + $data['isGlobalStylesUserThemeJSON'] = true; + $data['version'] = \WP_Theme_JSON::LATEST_SCHEMA; + + $global_style_ids[] = wp_insert_post( [ + 'post_type' => 'wp_global_styles', + 'post_title' => ' Style Variation: ' . basename( $variation ), + 'post_content' => addslashes( json_encode( $data, JSON_UNESCAPED_SLASHES ) ), + 'post_status' => 'publish', + ] ); +} + +if ( empty( $global_style_ids ) ) { + throw new \Exception( 'No global style variations found' ); +} $posts = get_posts( [ 'posts_per_page' => -1, @@ -99,28 +115,29 @@ ], $type ); } -// Generate REST API responses for a single global style variation -$view_data = get_rest_response( 'GET', "/wp/v2/global-styles/{$global_style}", [ - 'context' => 'view', -] ); -$edit_data = get_rest_response( 'GET', "/wp/v2/global-styles/{$global_style}", [ - 'context' => 'edit', -] ); +$global_style_data = []; + +foreach ( $global_style_ids as $id ) { + // Generate REST API responses for each of the single global style variations + $global_style_data[] = get_rest_response( 'GET', "/wp/v2/global-styles/{$id}", [ + 'context' => 'view', + ] ); + $global_style_data[] = get_rest_response( 'GET', "/wp/v2/global-styles/{$id}", [ + 'context' => 'edit', + ] ); +} save_rest_array( - [ - $view_data, - $edit_data, - ], + $global_style_data, 'global-style-variation', true, ); // Generate REST API responses for the global style variations collection -$view_data = get_rest_response( 'GET', "/wp/v2/global-styles/themes/{$theme}/variations", [ +$view_data = get_rest_response( 'GET', "/wp/v2/global-styles/themes/{$theme_name}/variations", [ 'context' => 'view', ] ); -$edit_data = get_rest_response( 'GET', "/wp/v2/global-styles/themes/{$theme}/variations", [ +$edit_data = get_rest_response( 'GET', "/wp/v2/global-styles/themes/{$theme_name}/variations", [ 'context' => 'edit', ] ); @@ -133,10 +150,10 @@ ); // Generate REST API responses for the theme global style config -$view_data = get_rest_response( 'GET', "/wp/v2/global-styles/themes/{$theme}", [ +$view_data = get_rest_response( 'GET', "/wp/v2/global-styles/themes/{$theme_name}", [ 'context' => 'view', ] ); -$edit_data = get_rest_response( 'GET', "/wp/v2/global-styles/themes/{$theme}", [ +$edit_data = get_rest_response( 'GET', "/wp/v2/global-styles/themes/{$theme_name}", [ 'context' => 'edit', ] );