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', ] );