wp-cli / restful
在命令行中释放WP REST API的潜力。
Requires
Requires (Dev)
- behat/behat: ~2.5
This package is auto-updated.
Last update: 2024-09-02 14:14:29 UTC
README
在命令行中释放WP REST API的潜力。
警告:该项目处于非常初级的阶段。将其视为一个实验,并理解可能会在没有警告的情况下进行破坏性更改。天也可能会塌下来。使用RESTful WP-CLI需要WP-CLI的最新夜间版本,您可以使用wp cli update --nightly
进行安装。
最初的开发是由Kickstarter项目支持的。该项目将与WordPress核心中WP REST API的演变同步发展。
使用
RESTful WP-CLI将WP REST API端点作为WP-CLI命令提供。
随着WordPress越来越成为一个嵌入到网络中的应用程序框架,RESTful WP-CLI允许WP-CLI用户通过WordPress如何理解自己的高级、自我表达的方式来与特定的WordPress安装进行交互。例如,在一个电子商务网站上,您不需要知道数据存储为wp post list --post_type=edd_product
,RESTful WP-CLI在wp rest product list
中公开了正确建模的数据。
以下是RESTful WP-CLI工作概述的概述,分为两部分。
1. 从运行WordPress 4.7或更高版本的任何WordPress站点自动发现WP REST API端点
使用--path=<path>
、--ssh=<host>
或--http=<domain>
针对特定的WordPress安装
# The `--path=<path>` global parameter tells WP-CLI to interact with a WordPress install at a given path.
# Because this is a stock WordPress install, you see the posts, pages, and other resources you'd expect to see.
$ wp --path=/srv/www/wordpress-develop.dev/src rest
usage: wp rest attachment <command>
or: wp rest category <command>
or: wp rest comment <command>
or: wp rest page <command>
or: wp rest page-revision <command>
or: wp rest post <command>
or: wp rest post-revision <command>
or: wp rest status <command>
or: wp rest tag <command>
or: wp rest taxonomy <command>
or: wp rest type <command>
or: wp rest user <command>
# The `--http=<domain>` global parameter tells WP-CLI to auto-discover endpoints over HTTP.
# Because Wired has some custom post types, they're automatically registered as WP-CLI commands.
$ wp --http=www.wired.com rest
usage: wp rest attachment <command>
or: wp rest category <command>
or: wp rest comment <command>
or: wp rest liveblog <command>
or: wp rest liveblog-revision <command>
or: wp rest page <command>
or: wp rest page-revision <command>
or: wp rest podcast <command>
or: wp rest post <command>
or: wp rest post-revision <command>
or: wp rest series <command>
or: wp rest slack-channel <command>
or: wp rest status <command>
or: wp rest tag <command>
or: wp rest taxonomy <command>
or: wp rest type <command>
or: wp rest user <command>
or: wp rest video <command>
# The `--ssh=<host>` global parameter proxies command execution to a remote WordPress install.
# Because runcommand has a completely custom data model, you can only interact with commands, excerpts, and sparks.
$ wp --ssh=runcommand.io rest
usage: wp rest command <command>
or: wp rest excerpt <command>
or: wp rest spark <command>
2. 在wp rest
命名空间中注册它所理解的资源端点的WP-CLI命令。
除了标准的列表、获取、创建、更新和删除命令之外,RESTful WP-CLI还注册了用于高级操作的命令,如edit
、generate
和diff
。
# In this example, `@wpdev` is a WP-CLI alias to `--path=/srv/www/wordpress-develop.dev/src`.
$ wp @wpdev rest user
usage: wp rest user create --username=<username> [--name=<name>] [--first_name=<first_name>] [--last_name=<last_name>] --email=<email> [--url=<url>] [--description=<description>] [--nickname=<nickname>] [--slug=<slug>] [--roles=<roles>] --password=<password> [--capabilities=<capabilities>] [--porcelain]
or: wp rest user delete <id> [--force=<force>] [--reassign=<reassign>] [--porcelain]
or: wp rest user diff <alias> [<resource>] [--fields=<fields>]
or: wp rest user edit <id>
or: wp rest user generate [--count=<count>] [--format=<format>] --username=<username> [--name=<name>] [--first_name=<first_name>] [--last_name=<last_name>] --email=<email> [--url=<url>] [--description=<description>] [--nickname=<nickname>] [--slug=<slug>] [--roles=<roles>] --password=<password> [--capabilities=<capabilities>] [--porcelain]
or: wp rest user get <id> [--context=<context>] [--fields=<fields>] [--field=<field>] [--format=<format>]
or: wp rest user list [--context=<context>] [--page=<page>] [--per_page=<per_page>] [--search=<search>] [--exclude=<exclude>] [--include=<include>] [--offset=<offset>] [--order=<order>] [--orderby=<orderby>] [--slug=<slug>] [--roles=<roles>] [--fields=<fields>] [--field=<field>] [--format=<format>]
or: wp rest user update <id> [--username=<username>] [--name=<name>] [--first_name=<first_name>] [--last_name=<last_name>] [--email=<email>] [--url=<url>] [--description=<description>] [--nickname=<nickname>] [--slug=<slug>] [--roles=<roles>] [--password=<password>] [--capabilities=<capabilities>] [--porcelain]
# Use `wp rest * edit` to open an existing item in the editor.
$ wp rest category edit 1 --user=daniel
---
description:
name: Uncategorized
slug: uncategorized
parent: 0
# Use `wp rest * generate` to generate dummy content.
$ wp @wpdev rest post generate --count=50 --title="Test Post" --user=daniel
Generating items 100% [==============================================] 0:01 / 0:02
# Use `wp rest * diff` to diff a resource or collection of resources between environments.
$ wp @dev-rest rest command diff @prod-rest find-unused-themes --fields=title
(-) http://runcommand.dev/api/ (+) https://runcommand.io/api/
command:
+ title: find-unused-themes
如果WP-CLI直接针对WordPress安装运行,您可以使用--debug
标志来跟踪查询数和总执行时间。这可以用来衡量和剖析API请求。
$ wp rest category list --debug
Debug (rest): REST command executed 3 queries in 0.000311 seconds. Use --debug=rest to see all queries. (1.118s)
+---------------+
| name |
+---------------+
| Test Category |
| Uncategorized |
+---------------+
RESTful WP-CLI还有很多不能做的事情。请查看问题记录,如果找不到与您主题相关的问题,请创建一个新的问题。
安装
安装此软件包需要WP-CLI 1.3.0-alpha或更高版本。使用wp cli update --nightly
更新到最新夜间版本。
完成后,您可以使用wp package install wp-cli/restful
安装此软件包。
贡献
我们感谢您主动为该项目做出贡献。
贡献不仅仅限于代码。我们鼓励您以最适合您能力的方式做出贡献,例如撰写教程、在当地聚会中演示、帮助其他用户回答支持问题或修订我们的文档。
有关更详细的介绍,请查看WP-CLI的贡献指南。此软件包遵循那些政策和指南。
报告错误
认为您发现了错误?我们非常希望您帮助我们修复它。
在创建新问题之前,您应该先搜索现有问题,看看是否已有解决方案,或者是否在更新的版本中已经修复。
搜索后,如果发现没有关于您问题的开放或已修复问题,请创建新问题。尽可能提供详细信息,如果可能,请提供清晰的复现步骤。如需更多指导,请查看我们的错误报告文档。
创建 pull 请求
想贡献一个新功能?请首先打开新问题讨论该功能是否适合项目。
一旦您决定投入时间完成 pull 请求,请遵循我们的 pull 请求创建指南,确保这是一个愉快的体验。有关在本地工作于该包的具体设置,请参阅 "设置"。
支持
GitHub 的问题不是用于一般支持问题,但您可以尝试其他途径:https://wp-cli.org/#support
此 README.md 是使用 wp scaffold package-readme
(文档)从项目代码库动态生成的。要建议更改,请提交针对代码库相应部分的 pull 请求。