pinkcrab/ajax

Perique框架的Ajax创建库。


README

logo

Ajax

为PinkCrab Perique框架提供的一个简单但强大的Ajax库。允许创建基于对象的Ajax调用,处理所有基本的Nonce验证、WP操作,并使用HTTP PSR接口。

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

WordPress 5.9 Test Suite [PHP7.2-8.1] WordPress 6.0 Test Suite [PHP7.2-8.1] WordPress 6.1 Test Suite [PHP7.2-8.1] WP6.2 [PHP7.4-8.2] Tests

codecov Scrutinizer Code Quality Maintainability

为什么选择它?

编写WordPress的Ajax脚本可能会非常混乱,需要定义多达2个共享回调的操作。Perique Ajax模块利用框架的注册和依赖注入方面。这允许将服务注入到回调中,从而实现干净和可测试的代码。

Perique Ajax文档

设置

需要PinkCrab Perique框架v2和Composer

使用Composer安装模块

$ composer require pinkcrab/ajax

包含自定义Ajax模块

// file:plugin.php

// Boot the app as normal, including the module.
$app = ( new App_Factory )      
    ->default_setup()
    ->module( \PinkCrab\Ajax\Module\Ajax::class )
    ->boot();

用法

创建您的Ajax模型

use PinkCrab\Ajax\Ajax;
use PinkCrab\Ajax\Ajax_Helper;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use PinkCrab\Ajax\Dispatcher\Response_Factory;

class My_Ajax extends Ajax {

    /**
     * Define the action to call.
     * @var string
     */
    protected $action = 'my_ajax_action';

    /**
     * The ajax calls nonce handle.
     * @var string
     */
    protected $nonce_handle = 'my_ajax_nonce';

    /** 
     * Some service which handles the logic of the call.
     * @var Some_Service 
     */
    protected $my_service;

    /**
     * Constructs the object
     * My_Service will be injected when this is created by the DI Container
     */
    public function __construct( Some_Service $my_service ) {
        $this->my_service = $my_service;
    }

    /**
     * The callback
     *
     * @param \Psr\Http\Message\ServerRequestInterface $request
     * @param \PinkCrab\Ajax\Dispatcher\Response_Factory $response_factory
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function callback(
        ServerRequestInterface $request,
        Response_Factory $response_factory
    ): ResponseInterface {
        
        // Extract the args from the request, you can also do this manually
        $args = Ajax_Helper::extract_server_request_args( $request );

        // Do something with the request args, ideally in a service class
        $data_to_return = array_key_exists('foo', $args)
            ? $this->my_service->do_something($args['foo'])
            : 'Foo not found!';
        
        // Return with a valid PSR Response. 
        return $response_factory->success( $data_to_return );
    }
}

这将具有分配了my_ajax_action动作的Ajax调用。

**将所有Ajax模型添加到registration.php **

// file:registration.php

return [
    ....
    My_Ajax_Call::class,
    ....
];

许可协议

MIT许可协议

https://open-source.org.cn/licenses/mit-license.html

预发布版本

  • 对于Perique 1.4.*,使用版本1.1.0
  • 对于Perique 1.3.*,使用版本1.0.4
  • 对于Perique 1.0.* - 1.2.*,使用版本1.0.3

变更日志

  • 2.0.0 - 升级对Perique 2.0.0的支持,删除Ajax::bootstrap()并用Ajax模块替换。
  • 1.1.0 - 升级对Perique 1.4.0的支持
  • 1.0.4 - 更新开发依赖到wp6.1和PinkCrab/HTTP 1.*,停止支持PHP 7.1
  • 1.0.3 - 更新开发依赖,更新GH Pipeline并改进在检查是否进行Ajax时的条件。
  • 1.0.2 - 添加Ajax_Bootstrap类和::use()方法,简化与Perique的集成。作为Perique.info网站的一部分改进了文档。
  • 1.0.1 - 更新yoast/phpunit-polyfills要求从^0.2.0到^0.2.0 || ^1.0.0,由@dependabot在#13中更新
  • 1.0.0 - 支持Perique 1.0.0,并包含检查以确保仅在wp_ajax调用时添加
  • 0.1.0 从Registerables模块提取出来。现在使用自定义Registration_Middleware服务来调度所有Ajax调用。