carloswph / wp-endpoint-gen
WordPress自定义端点助手和类生成器。
Requires
- nette/php-generator: ^3.5
- nette/robot-loader: ^3.3
This package is auto-updated.
Last update: 2024-09-29 06:20:15 UTC
README
WordPress自定义端点助手和类生成器。WordPress引擎添加新自定义端点到WP API并不难理解。然而,根据插件或扩展的复杂程度,操作数十个新端点和相应的回调以及权限回调可能会很麻烦。因此,这个库不仅通过创建面向对象的添加新端点方法来帮助您,而且还帮助您管理它们的命名空间、参数和回调。此外,我们认为,如果我们将每个新端点的回调和权限集中在特定的控制器类中,这将更加简单和有序 - 当然,根据您如何自定义新端点自动生成它们。好吧,现在没有其他要添加的 - 让我们来查看以下代码示例中的工作方式。
安装
使用方法
第一步是在您的代码中创建WPH\Endpoints\Config类的新实例。不要忘记使用Composer的自动加载功能使其可实例化。Config类可以在Generator类中实例化,或者在其之前实例化,每次您想更改配置详细信息默认值时。
配置什么?
默认情况下,我们考虑所有自定义端点都位于wph
命名空间内(出于明显的原因)。生成的类将位于命名空间或Psr "EndpointGroup"下,并将"v1"作为当前版本。保存生成的类的基路径将位于wp-content
目录内名为"endpoints"的新文件夹中。聪明,不是吗?不?别担心,通过实例化Config类,您能够自定义所有这些变量。
use WPH\Endpoints\Config; require __DIR__ . '/vendor/autoload.php'; $config = new Config(); // Config class instance. $config->setPsr('Foo'); // Sets generated classes' namespaces as Foo $config->setPath( WP_CONTENT_DIR . '/foo'); // Creates and sets the generated classes' path $config->setVersion('v1.2'); // Sets a new version for all endpoints $config->setNamespace('foo-api'); // Sets a new namespace for the WP API endpoints (not the classes' namespace)
我喜欢您的默认设置 - 不需要更改它
又聪明了一次!在这种情况下,只需忽略Config类的设置,进入本教程的下一部分。您仍然需要使用Config类,但如果您不需要更改任何内容,您只需在Generator类中注入一个新的实例即可。下一个示例将阐明。
use WPH\Endpoints\Generator; require __DIR__ . '/vendor/autoload.php'; // If you instantiated the Config class and set up new values for the variables, just use the generator including three arguments: the endpoint name, an array of all http methods to be allowed on it and, finally, the object of the Config class: $gen = new Generator('wph-endpoint', ['GET', 'PUT'], $config); // That's it. The new endpoints have been created inside your WP API. $gen->generate(); // Now the magic: check the configured path or, if you just used ours, look inside the `wp-content/endpoints` directory. $gen->autoload(); // Once callbacks and permission callbacks have been set up as external classes, we need to autoload them.
所有自定义端点都会在其自己的类中找到相应的回调和权限回调,这些类位于您指定的路径中。这不仅仅是用面向对象的方式思考WP端点 - 这意味着您不需要编写数十个重复的函数和回调,也不需要在代码中添加庞大的匿名函数或类似的。
但是你们忘记了端点参数...
Uhhh...实际上我们没有忘记。顺便说一句,您可以通过两种不同的方法向端点添加参数。
- 如果所有端点方法都应接受相同的参数,请在实例化Generator类时(正在执行)将它们作为数组添加。
- 但是,如果您需要仅添加GET方法的参数,例如,您可以使用链式方法addArgs()。
让我们从第二种方法开始,它已经可用。您需要做的就是使用链式方法addArgs(),带两个参数:http方法(例如,GET)和所需参数的数组。如下所示
$gen = new Generator('wph-endpoint', ['GET', 'PUT'], $config); $gen->addArgs('PUT', ['id' => ['description' => 'Id of something']]); $gen->addArgs('GET', ['name' => ['description' => 'Name of someone']]);
生成的类看起来像什么?
现在你可能想知道我们之前提到的那些生成的控制器是什么样的。当然,它们基本上是骨架,你可以在这个骨架中创建自己的进程和条件,但它们从第一刻起就已经是功能性的,返回WP_Rest_Response类的简单实例。让我们考虑一个名为'boxes'的端点,它处理GET、POST和HEAD调用。在这种情况下,生成的控制器类将如下所示
namespace Foo\Routes; /** * Controller class for callbacks and permissions. * Route --> Foo\Routes\Boxes * @since 1.0.0 */ class Boxes { /** * Handles GET requests to the endpoint. * @return \WP_Rest_Response */ public function getBoxes(\WP_Rest_Request $request) { return new \WP_Rest_Response(); } /** * Handles POST requests to the endpoint. * @return \WP_Rest_Response */ public function postBoxes(\WP_Rest_Request $request) { return new \WP_Rest_Response(); } /** * Handles HEAD requests to the endpoint. * @return \WP_Rest_Response */ public function headBoxes(\WP_Rest_Request $request) { return new \WP_Rest_Response(); } /** * Authenticate or limitate requests to the endpoint. * @return bool */ public function permissions(\WP_Rest_Request $request) { // Your conditions. return true; } }
所有类都已经注解,并为每个端点创建的HTTP方法提供了一个回调函数,以及一个permissions()函数,该函数默认返回true
。如所示,回调函数最初返回WP_Rest_Response类的简单实例。