Skip to content

Commit

Permalink
Add a schema for plugins in a REST API context. Fixes #19.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbillion committed Aug 24, 2024
1 parent 1efbfdb commit cbaccf4
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 4 deletions.
70 changes: 70 additions & 0 deletions packages/wp-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/wp-types/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down Expand Up @@ -295,6 +301,8 @@
"Pages",
"Pattern_Directory_Pattern",
"Pattern_Directory_Patterns",
"Plugin",
"Plugins",
"Post",
"Posts",
"Rendered_Block",
Expand Down
19 changes: 19 additions & 0 deletions schemas/rest-api/collections/plugins.json
Original file line number Diff line number Diff line change
@@ -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": "#"
}
}
]
}
144 changes: 144 additions & 0 deletions schemas/rest-api/plugin.json
Original file line number Diff line number Diff line change
@@ -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": "#"
}
}
}
]
}
15 changes: 15 additions & 0 deletions tests/output/plugins.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace WPJsonSchemas;

$edit_data = get_rest_response( 'GET', '/wp/v2/plugins', [
'context' => 'edit',
] );
$view_data = get_rest_response( 'GET', '/wp/v2/plugins', [
'context' => 'view',
] );

save_rest_array( [
$edit_data,
$view_data,
], 'plugins' );

0 comments on commit cbaccf4

Please sign in to comment.