Skip to content

Commit

Permalink
Add schema for the global styles variations collection.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbillion committed Aug 23, 2024
1 parent 08cd22a commit 2ad37a2
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 32 deletions.
31 changes: 29 additions & 2 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 @@ -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;
Expand Down Expand Up @@ -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;
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/wp-types/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
4 changes: 4 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down Expand Up @@ -262,6 +265,7 @@
"Navigation_Menu",
"Navigation_Menus",
"Global_Style_Variation",
"Global_Style_Variations",
"Global_Style_Config",
"Attachment",
"Attachments",
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": "#"
}
}
]
}
9 changes: 3 additions & 6 deletions schemas/rest-api/global-style-variation.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -13,7 +13,7 @@
],
"properties": {
"id": {
"description": "ID of global styles config.",
"description": "ID of global styles variation.",
"type": "integer"
},
"styles": {
Expand Down Expand Up @@ -76,10 +76,7 @@
}
},
"targetSchema": {
"type": "array",
"items": {
"$ref": "#"
}
"$ref": "collections/global-style-variations.json"
}
}
]
Expand Down
61 changes: 39 additions & 22 deletions tests/output/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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',
] );

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

Expand Down

0 comments on commit 2ad37a2

Please sign in to comment.