From cbaccf40b59590ba5411b0634b2306d6ecc55a6a Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Sat, 24 Aug 2024 23:10:24 +0100 Subject: [PATCH] Add a schema for plugins in a REST API context. Fixes #19. --- packages/wp-types/index.ts | 70 +++++++++++ packages/wp-types/readme.md | 4 +- readme.md | 4 +- schema.json | 8 ++ schemas/rest-api/collections/plugins.json | 19 +++ schemas/rest-api/plugin.json | 144 ++++++++++++++++++++++ tests/output/plugins.php | 15 +++ 7 files changed, 260 insertions(+), 4 deletions(-) create mode 100644 schemas/rest-api/collections/plugins.json create mode 100644 schemas/rest-api/plugin.json create mode 100644 tests/output/plugins.php diff --git a/packages/wp-types/index.ts b/packages/wp-types/index.ts index 3e95846..15e6ca0 100644 --- a/packages/wp-types/index.ts +++ b/packages/wp-types/index.ts @@ -170,6 +170,10 @@ export type WP_REST_API_Pages = WP_REST_API_Page[]; * A collection of patterns from the pattern directory in a REST API context. */ export type WP_REST_API_Pattern_Directory_Patterns = WP_REST_API_Pattern_Directory_Pattern[]; +/** + * A collection of plugins in a REST API context. + */ +export type WP_REST_API_Plugins = WP_REST_API_Plugin[]; /** * A post object in a REST API context. */ @@ -301,6 +305,8 @@ export interface WP { Pages: WP_REST_API_Pages; Pattern_Directory_Pattern: WP_REST_API_Pattern_Directory_Pattern; Pattern_Directory_Patterns: WP_REST_API_Pattern_Directory_Patterns; + Plugin: WP_REST_API_Plugin; + Plugins: WP_REST_API_Plugins; Post: WP_REST_API_Post; Posts: WP_REST_API_Posts; Rendered_Block: WP_REST_API_Rendered_Block; @@ -3408,6 +3414,70 @@ export interface WP_REST_API_Pattern_Directory_Pattern { block_types?: string[]; [k: string]: unknown; } +/** + * A plugin in a REST API context. + */ +export interface WP_REST_API_Plugin { + /** + * The plugin file. + */ + plugin: string; + /** + * The plugin activation status. + */ + status: "inactive" | "active" | "network-active"; + /** + * The plugin name. + */ + name: string; + /** + * The plugin's website address. + */ + plugin_uri: string | ""; + /** + * The plugin author. + */ + author: string; + /** + * Plugin author's website address. + */ + author_uri: string | ""; + /** + * The plugin description. + */ + description: { + /** + * The raw plugin description. + */ + raw: string; + /** + * The plugin description formatted for display. + */ + rendered: string; + }; + /** + * The plugin version number. + */ + version: string; + /** + * Whether the plugin can only be activated network-wide. + */ + network_only: boolean; + /** + * Minimum required version of WordPress. + */ + requires_wp: string; + /** + * Minimum required version of PHP. + */ + requires_php: string; + /** + * The plugin's text domain. + */ + textdomain: string; + _links: WP_REST_API_Object_Links; + [k: string]: unknown; +} /** * A rendered dynamic block in a REST API context. Only accessible with the 'edit' context. */ diff --git a/packages/wp-types/readme.md b/packages/wp-types/readme.md index c1a5ba7..7a5443f 100644 --- a/packages/wp-types/readme.md +++ b/packages/wp-types/readme.md @@ -88,8 +88,8 @@ Route | Schema /wp/v2/pages/{parent}/revisions | `WP_REST_API_Revisions` /wp/v2/pages/{parent}/revisions/{id} | `WP_REST_API_Revision` /wp/v2/pattern-directory/patterns | `WP_REST_API_Pattern_Directory_Patterns` -/wp/v2/plugins | Todo -/wp/v2/plugins/{plugin} | Todo +/wp/v2/plugins | `WP_REST_API_Plugins` +/wp/v2/plugins/{plugin} | `WP_REST_API_Plugin` /wp/v2/posts | `WP_REST_API_Posts` /wp/v2/posts/{id} | `WP_REST_API_Post` /wp/v2/posts/{id}/autosaves | Todo diff --git a/readme.md b/readme.md index 1c58ce9..b532e6d 100644 --- a/readme.md +++ b/readme.md @@ -92,8 +92,8 @@ Route | Schema /wp/v2/pages/{parent}/revisions | `WP_REST_API_Revisions` /wp/v2/pages/{parent}/revisions/{id} | `WP_REST_API_Revision` /wp/v2/pattern-directory/patterns | `WP_REST_API_Pattern_Directory_Patterns` -/wp/v2/plugins | Todo -/wp/v2/plugins/{plugin} | Todo +/wp/v2/plugins | `WP_REST_API_Plugins` +/wp/v2/plugins/{plugin} | `WP_REST_API_Plugin` /wp/v2/posts | `WP_REST_API_Posts` /wp/v2/posts/{id} | `WP_REST_API_Post` /wp/v2/posts/{id}/autosaves | Todo diff --git a/schema.json b/schema.json index 6361200..0b03fb6 100644 --- a/schema.json +++ b/schema.json @@ -191,6 +191,12 @@ "Pattern_Directory_Patterns": { "$ref": "schemas/rest-api/collections/pattern-directory-patterns.json" }, + "Plugin": { + "$ref": "schemas/rest-api/plugin.json" + }, + "Plugins": { + "$ref": "schemas/rest-api/collections/plugins.json" + }, "Post": { "$ref": "schemas/rest-api/post.json" }, @@ -295,6 +301,8 @@ "Pages", "Pattern_Directory_Pattern", "Pattern_Directory_Patterns", + "Plugin", + "Plugins", "Post", "Posts", "Rendered_Block", diff --git a/schemas/rest-api/collections/plugins.json b/schemas/rest-api/collections/plugins.json new file mode 100644 index 0000000..cb3bbe9 --- /dev/null +++ b/schemas/rest-api/collections/plugins.json @@ -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/plugins.json", + "title": "WP_REST_API_Plugins", + "description": "A collection of plugins in a REST API context.", + "type": "array", + "items": { + "$ref": "../plugin.json" + }, + "links": [ + { + "rel": "self", + "href": "/wp/v2/plugins", + "targetSchema": { + "$ref": "#" + } + } + ] +} diff --git a/schemas/rest-api/plugin.json b/schemas/rest-api/plugin.json new file mode 100644 index 0000000..e9b4138 --- /dev/null +++ b/schemas/rest-api/plugin.json @@ -0,0 +1,144 @@ +{ + "$schema": "https://json-schema.org/draft/2019-09/hyper-schema", + "$id": "https://raw.githubusercontent.com/johnbillion/wp-json-schemas/trunk/schemas/rest-api/plugin.json", + "title": "WP_REST_API_Plugin", + "description": "A plugin in a REST API context.", + "type": "object", + "required": [ + "plugin", + "status", + "name", + "plugin_uri", + "author", + "author_uri", + "description", + "version", + "network_only", + "requires_wp", + "requires_php", + "textdomain", + "_links" + ], + "properties": { + "plugin": { + "description": "The plugin file.", + "type": "string", + "pattern": "[^.\\/]+(?:\\/[^.\\/]+)?" + }, + "status": { + "description": "The plugin activation status.", + "type": "string", + "enum": [ + "inactive", + "active", + "network-active" + ] + }, + "name": { + "description": "The plugin name.", + "type": "string" + }, + "plugin_uri": { + "description": "The plugin's website address.", + "oneOf": [ + { + "type": "string", + "format": "uri" + }, + { + "type": "string", + "enum": [ + "" + ] + } + ] + }, + "author": { + "description": "The plugin author.", + "type": "string" + }, + "author_uri": { + "description": "Plugin author's website address.", + "oneOf": [ + { + "type": "string", + "format": "uri" + }, + { + "type": "string", + "enum": [ + "" + ] + } + ] + }, + "description": { + "description": "The plugin description.", + "type": "object", + "required": [ + "raw", + "rendered" + ], + "properties": { + "raw": { + "description": "The raw plugin description.", + "type": "string" + }, + "rendered": { + "description": "The plugin description formatted for display.", + "type": "string" + } + }, + "additionalProperties": false + }, + "version": { + "description": "The plugin version number.", + "type": "string" + }, + "network_only": { + "description": "Whether the plugin can only be activated network-wide.", + "type": "boolean" + }, + "requires_wp": { + "description": "Minimum required version of WordPress.", + "type": "string" + }, + "requires_php": { + "description": "Minimum required version of PHP.", + "type": "string" + }, + "textdomain": { + "description": "The plugin's text domain.", + "type": "string" + }, + "_links": { + "$ref": "properties/object-links.json" + } + }, + "links": [ + { + "rel": "self", + "href": "/wp/v2/plugins/{plugin}", + "hrefSchema": { + "properties": { + "plugin": { + "$ref": "#/properties/plugin" + } + } + }, + "targetSchema": { + "$ref": "#" + } + }, + { + "rel": "collection", + "href": "/wp/v2/plugins", + "targetSchema": { + "type": "array", + "items": { + "$ref": "#" + } + } + } + ] +} diff --git a/tests/output/plugins.php b/tests/output/plugins.php new file mode 100644 index 0000000..470765a --- /dev/null +++ b/tests/output/plugins.php @@ -0,0 +1,15 @@ + 'edit', +] ); +$view_data = get_rest_response( 'GET', '/wp/v2/plugins', [ + 'context' => 'view', +] ); + +save_rest_array( [ + $edit_data, + $view_data, +], 'plugins' );