pinkcrab/ajax 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

pinkcrab/ajax

Composer 安装命令:

composer require pinkcrab/ajax

包简介

Ajax creation library for the Perique Framework.

README 文档

README

logo

Ajax

A simple but powerful Ajax library for the PinkCrab Perique framework. Allows for the creation of object based Ajax calls that handle all basic Nonce validation, WP Actions and makes use of the HTTP PSR Interfaces.

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

WP6.6 [PHP8.0-8.4] Tests WP6.7 [PHP8.0-8.4] Tests WP6.8 [PHP8.0-8.4] Tests WP6.9 [PHP8.0-8.4] Tests

codecov Scrutinizer Code Quality Maintainability

Why?

Writing Ajax scripts for WordPress can get messy really quickly, with the need to define up to 2 actions with a shared callback. The Perique Ajax Module makes use of the registration and dependency injection aspects of the framework. This allows for the injection of services into your callback, allowing for clean and testable code.

Perique Ajax Documentation

Setup

Requires the PinkCrab Perique Framework v2 and Composer

Install the Module using composer

$ composer require pinkcrab/ajax

Include the custom Ajax Module

// file:plugin.php

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

Usage

Create your Ajax Models

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 );
    }
}

This would have an ajax call with my_ajax_action action assigned.

**Add all your Ajax Models to registration.php **

// file:registration.php

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

License

MIT License

http://www.opensource.org/licenses/mit-license.html

Pre-Release

  • For Perique 1.4.*, use version 1.1.0
  • For Perique 1.3.*, use version 1.0.4
  • For Perique 1.0.* - 1.2.*, use version 1.0.3

Change Log

  • 2.1.1 - Updated dev dependencies.
  • 2.1.0 - Bump support for Perique 2.1.0, update deps.
  • 2.0.0 - Bump support for Perique 2.0.0, remove Ajax::bootstrap() and replace with the Ajax Module.
  • 1.1.0 - Bump support for Perique 1.4.0
  • 1.0.4 - Update dev deps to wp6.1 and PinkCrab/HTTP 1.*, Drop Support for PHP 7.1
  • 1.0.3 - Update dev deps, update GH Pipeline and improve conditional on checking if doing ajax.
  • 1.0.2 - Added in Ajax_Bootstrap class with ::use() method, for simpler inclusion with Perique. Docs improved as part of Perique.info site
  • 1.0.1 - Update yoast/phpunit-polyfills requirement from ^0.2.0 to ^0.2.0 || ^1.0.0 by @dependabot in #13
  • 1.0.0 - Supports Perique 1.0.0 and includes checks to ensure only added when wp_ajax called
  • 0.1.0 Extracted from the Registerables module. Now makes use of a custom Registration_Middleware service for dispatching all Ajax calls.

pinkcrab/ajax 适用场景与选型建议

pinkcrab/ajax 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.03k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2021 年 06 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 pinkcrab/ajax 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 pinkcrab/ajax 我们能提供哪些服务?
定制开发 / 二次开发

基于 pinkcrab/ajax 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.03k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-06-12