tenjuu99 / wp-resta
WordPress REST Api development framework using DI
Requires
- php: >=8.2
- phpstan/phpdoc-parser: ^2.3
Requires (Dev)
- brain/monkey: ^2.7
- guzzlehttp/guzzle: ^7.10
- mockery/mockery: ^1.6
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^11.1
- szepeviktor/phpstan-wordpress: ^1.3
Suggests
None
Provides
None
Conflicts
None
Replaces
None
- dev-main
- v0.13.1
- v0.13.0
- v0.12.0
- v0.11.3
- v0.11.2
- v0.11.1
- v0.11.0
- v0.10.2
- v0.10.1
- v0.10.0
- v0.9.2
- v0.9.1
- v0.9.0
- v0.8.6
- v0.8.5
- v0.8.4
- v0.8.3
- v0.8.2
- v0.8.1
- v0.8.0
- dev-fix/120-docs-follow-examples
- dev-feature/120-example-as-one-subject
- dev-fix/120-separate-e2e-fixtures
- dev-fix/120-doc-mismatches
- dev-release/0.13.1-bump
- dev-feature/158-schema-rest-route
- dev-fix/159-container-default-values
- dev-release/0.13.0-bump
- dev-feature/102-return-docblock-schema
- dev-fix/145-remove-authorize
- dev-feature/139-authorization-attributes
- dev-fix/133-remove-validate-hook
- dev-feature/design-dependencies
- dev-feature/agents-icebox
- dev-feature/rename-agents-md
- dev-feature/core-validation-comments
- dev-feature/spec-wordpress-types
- dev-feature/request-handler-not-final
- dev-feature/wp-request-accessor
- dev-fix/134-same-name-argument-type
- dev-feature/remove-adr
- dev-feature/99-callback-definition
- dev-feature/105-static-route-body
- dev-feature/108-remove-codegen
- dev-fix/107-ci-e2e
- dev-fix/109-ci-phpstan
- dev-fix/119-readme-entry
- dev-fix/120-doc-example-mismatch
- dev-fix/122-agent-reading-order
- dev-feature/design-declaration-criteria
- dev-feature/design-developer-experience
- dev-feature/design-docs-adr
- dev-release/0.12.0-bump
- dev-fix/101-schema-inference-fallback
- dev-fix/85-envelope-errors
- dev-feature/78-cache
- dev-feature/59-deprecate-properties
- dev-feature/79-openapi-ts
- dev-feature/92-operation
- dev-fix/94-sample-hook
- dev-feature/79-request-handler
- dev-feature/79-parameter-definitions
- dev-fix/61-authorization-model
- dev-fix/60-remove-state-machine
- dev-fix/81-string-param-default
- dev-fix/83-callback-interface-di
- dev-docs/agent-md-and-spec
- dev-fix/66-pattern-to-core-args
- dev-feat/59-response-helper
- dev-fix/56-regex-delimiter-anchor
- dev-fix/55-http-exception-400
- dev-fix/57-route-state-carryover
- dev-fix/58-parameter-order-scope
- dev-fix/62-readme-url-params
- dev-fix/64-endpoints-inline-required
- dev-fix/63-openapi-paths-merge
- dev-hotfix/remove-set-accessible
- dev-fix/glob-false-foreach
- dev-release/0.11
- dev-release/0.10
- dev-release/0.9
- dev-release/0.8
- dev-hotfix/regex-mismatch-silent-drop
- dev-hotfix/interface-binding-assert
- dev-feature/43/proxy-wp
- dev-feature/collect_actions_from_state
- dev-feature/change-license
- dev-feature/remove_flush_rewrite_rule
- dev-feature/refactor-unnecessary-loop
- dev-feature/dispatcher
- dev-feature/codegen
- dev-feature/schema
- dev-feature/envelope
- dev-feature/+31
- dev-feature/refactoring_20160215
- dev-feature/+24
- dev-copilot/sub-pr-28
- dev-feature/coverage
- dev-feature/remove_unnecessary
- dev-feature/+1
- dev-feature/+20
- dev-feature/+22
- dev-fix/config_path_error
This package is auto-updated.
Last update: 2026-11-20 17:09:27 UTC
README
Wp\Resta
WordPress 上で REST API 開発をするためのプラグインです。
用途別のエンドポイントを、決まった場所に、決まった形で書けるようにします。
- 規約: Route クラス 1 つがエンドポイント 1 つ。決まったディレクトリに置きます
- 契約: Route クラスの宣言(定数、callback の引数と戻り値の型、アトリビュート)から OpenAPI を導出します。フロントエンドはこの仕様に依存できます
- テスト: Route クラスは WordPress 無しでテストできます
誰のどの問題を解くかは docs/design.md にあります。
アイデアやインターフェースは BEAR.Sunday から影響を受けています。
How to install
前提: WordPress 管理画面からパーマリンク設定を「投稿名」などにしておいてください。
自作テーマで利用する
$ cd /path/to/theme
$ composer require tenjuu99/wp-resta
functions.php で初期化
以下は、サンプルのディレクトリにあるAPIを読みこむ設定です。
<?php require_once __DIR__ . '/vendor/autoload.php'; (new Wp\Resta\Resta)->init([ 'routeDirectory' => [ [__DIR__ . '/vendor/tenjuu99/wp-resta/src/REST/Example/Routes', 'Wp\\Resta\\REST\\Example\\Routes\\', 'example'] ], 'schemaDirectory' => [ [__DIR__ . '/vendor/tenjuu99/wp-resta/src/REST/Example/Schemas', 'Wp\\Resta\\REST\\Example\\Schemas\\'], ], 'hooks' => [ \Wp\Resta\Hooks\SwaggerHook::class, // Swagger UI を有効化 ], ]);
SwaggerHook は OpenAPI のスキーマを /wp-json/resta/v1/schema で返し、管理画面の Swagger UI はこれを読みます。スキーマは誰でも取得できます。
場所と、取得できる相手を変えるときは、dependencies に SwaggerOptions を書きます。
'dependencies' => [ \Wp\Resta\Hooks\SwaggerOptions::class => new \Wp\Resta\Hooks\SwaggerOptions( namespace: 'myapp/v1', // /wp-json/myapp/v1/schema で返す route: '/schema', capability: 'edit_pages', // この capability を持つ人にだけ返す。省略すると誰でも ), ],
capabilityを指定すると、管理画面のメニューも同じ capability を持つ人にだけ見えます。省略したときのメニューはedit_pagesです- 既定の
resta/v1と/schemaは、今後のバージョンで変わることがあります。URL に依存するときは、namespaceとrouteを明示してください - プラグインとして利用する場合、設定ファイルはオートローダーより先に読まれます。
fn () => new \Wp\Resta\Hooks\SwaggerOptions(...)のようにクロージャで書いてください
プラグインで利用する場合
WordPress のプラグインとしても利用できます。
次の例は composer/installers を利用して WordPress プラグインを wp-content/plugins/ 以下に配置しています。
$ composer config "extra.installer-paths.wp-content/plugins/{\$name}/" "['type:wordpress-plugin']"
$ composer require composer/installers tenjuu99/wp-resta
無事 wp-content/plugins 以下に展開できたら、管理画面からプラグインを有効化してください。
Example
インストールしたら、管理画面に REST API doc というメニューが追加されます。
このページでは、 Swagger UI を使ってAPI定義をドキュメント化しています。
このサンプル実装は src/REST/Example/Routes/ 以下にあります。
How to develop
自分のルーティング定義を追加するためには、 functions.php での初期化時のコードにルーティング用ディレクトリの設定を記述してください。
routeDirectory に渡す配列は、 ['ディレクトリ名', 'php namespace', 'api namespace'] となっています。
schemaDirectory は ['ディレクトリ名', 'php namespace'] です。
(new Wp\Resta\Resta)->init([
'routeDirectory' => [
['wp-content/themes/mytheme/vendor/tenjuu99/wp-resta/src/REST/Example/Routes', 'Wp\\Resta\\REST\\Example\\Routes\\', 'example'],
+ ['src/Routes', 'MyREST\\Routes\\', 'myroute']
],
'schemaDirectory' => [
['wp-content/themes/mytheme/vendor/tenjuu99/wp-resta/src/REST/Example/Schemas', 'Wp\\Resta\\REST\\Example\\Schemas\\'],
+ ['src/Schemas', 'MyREST\\Schemas\\']
],
]);
必要に応じて composer.json にも autoload 設定を追加してください。
"autoload": {
+ "psr-4": {
+ "MyREST\\": "src/"
+ }
}
src/Routes/HelloWorld.php を作ります。
<?php namespace MyREST\Routes; use Wp\Resta\REST\AbstractRoute; class HelloWorld extends AbstractRoute { public function callback(): string { return 'Hello, world!'; } }
次のURLが生成されます。
$ curl http://example.com/wp-json/myroute/helloworld
"Hello, world!" と値が返ってきていることを確かめてください。
ROUTE 定数での URL の変更、パラメータの宣言、HTTP メソッドごとの処理は docs/routes.md にあります。
文書の案内
やりたいことから、読む文書と手本を引けます。手本は src/REST/Example/Routes/ にある、実際に動くコードです。記事と下書きを題材にした、1 つの小さな API になっています。テストは tests/Unit/REST/Example/ にあります。
手本になるのは、Route クラスの宣言(URL、パラメータ、アトリビュート、HTTP メソッドごとの処理)と、テストの書きかたです。データの取りかた(WP_Query をどこに書くか、何に詰め替えて返すか)は一例で、決まった書きかたはありません。
| やりたいこと | 読むもの | 手本 |
|---|---|---|
| URL とパラメータを宣言する | docs/routes.md | Hello.php、Posts.php、Post.php |
| クエリの組み立てを分けて、テストで固定する | LatestPosts.php、src/REST/Example/Services/LatestPostsQuery.php |
|
| 同じ URL で GET と POST を分ける | docs/routes.md | Drafts.php |
| ステータスやヘッダを指定する、エラーを返す | docs/routes.md | |
| ログイン状態や権限で制限する | docs/authorization.md | Drafts.php、Draft.php |
| キャッシュのヘッダを付ける | docs/cache.md | Posts.php |
レスポンスを data / meta で包む |
docs/envelope.md | Post.php、Posts.php |
| レスポンスの形(スキーマ)を宣言する | docs/SCHEMA.md | Post.php、Posts.php |
| Service クラスを注入する | docs/di.md | LatestPosts.php |
| Route クラスをテストする | docs/testing.md | tests/Unit/REST/Example/ |
| フロントエンドの TypeScript の型を生成する | docs/frontend.md | |
| イベントを購読する、WordPress のフックを登録する | docs/extending.md | src/REST/Example/Hooks/ |
register_rest_route() との対応を知る |
docs/wordpress.md | |
| 何が保証されているかを確かめる | docs/spec.md | |
| 動かない | docs/troubleshooting.md |
AI エージェントに書かせるとき
wp-resta を使うプロジェクトの AGENTS.md や CLAUDE.md に、次を足してください。パスは wp-resta を置いた場所に合わせます。
wp-resta で API を書くときは、vendor/tenjuu99/wp-resta/README.md の「文書の案内」から読む。 Route クラスの宣言とテストの書きかたは vendor/tenjuu99/wp-resta/src/REST/Example/Routes/ をまねる。データの取りかたは一例なので、このプロジェクトのやりかたに合わせる。 何が保証されているかは vendor/tenjuu99/wp-resta/docs/spec.md で確かめる。
wp-resta 自体を開発する
- AI エージェント向けの指示: AGENTS.md
- 設計の考え方: docs/design.md
- テストの実行と Docker での動作確認: docs/testing.md