x-wp / ajax-router

通过提供 NestJS 启发的控制器设计,简化了 WordPress AJAX 的使用。

v1.0.0-alpha.2 2024-04-16 08:12 UTC

This package is auto-updated.

Last update: 2024-09-16 09:00:12 UTC


README

WordPress AJAX 路由器

Packagist Version Packagist PHP Version semantic-release: angular

PSR-7 / PSR-15 兼容的 WordPress AJAX 路由器。

NestJSExpress 启发 - 该库提供了一种简单的方式来处理 WordPress 中的 AJAX 请求。

Inner Platform Effect

快速入门

安装库

composer require x-wp/ajax-server

定义控制器

<?php

use Sunrise\Http\Router\Exception\PageNotFoundException;
use XWP\Decorator\Core\Controller;
use XWP\Decorator\Core\Use_Guards;
use XWP\Decorator\HTTP\Argument\Body;
use XWP\Decorator\HTTP\Get;
use XWP\Decorator\HTTP\Param;
use XWP\Decorator\HTTP\Post;
use XWP\Guard\Capability_Guard;
use XWP\Guard\Nonce_Guard;
use XWP\Response\JSON;

#[Use_Guards( new Nonce_Guard() )]
#[Controller( 'my-endpoint' )]
class My_Controller {
	#[Get( 'get-post/{id}', JSON::class )]
	public function get_post( #[Param( 'id' )]
    int $id, ): \WP_Post {
		return \get_post( $id );
	}

	#[Use_Guards( new Capability_Guard( 'manage_options' ) )]
	#[Post( 'modify-post/{id}', JSON::class )]
	public function modify_post( #[Param( 'id' )] int $id, #[Body] array $data ): int {
		$post = \get_post( $id );

		if ( ! $post ) {
			throw new PageNotFoundException( 'Invalid post', 404 );
		}

		return wp_update_post(
            array_merge(
                array( 'ID' => $id ),
                $data,
            ),
		);
	}
}

注册控制器

xwp_register_routes( My_Controller::class );

这将公开以下端点

  • GET /wp-ajax/my-endpoint/get-post/{id}
  • POST /wp-ajax/my-endpoint/modify-post/{id}

Ajax 基础 URL 默认为 /wp-ajax,可以在永久链接设置中更改。

获取端点 URL

在 PHP 中

xwp_ajax_url( 'some', 'endpoint' ); // https://example.com/wp-ajax/some/endpoint
xwp_ajax_url( 'some/endpoint' );    // https://example.com/wp-ajax/some/endpoint

在 JavaScript 中

库添加了一个全局对象 xwp,它附加到 window 对象。在后台和前端都有效。您可以通过使用 xwp_ajax_vars 过滤器或调用 xwp_disable_ajax_js_vars 来禁用此功能。

declare type XWP = {
  /**
   * URL for the wp-ajax endpoint
   */
  wpAjaxUrl: string;

  /**
   * URL for the admin-ajax endpoint
   */
  adminAjaxUrl: string;

  /**
   * Get URL for the wp-ajax endpoint
   * 
   * @param parts - parts of the URL
   */
  forWpAjax: (...parts: string[]): string;

  /**
   * Get URL for the admin-ajax endpoint
   * 
   * @param action - action name
   * @param nonce  - nonce value (optional)
   * @param data   - data to be sent
   */
  forAdminAjax: (action:string , nonce?: string, data: Record<string, any>|FormData): string;
}
const baseUrl = window.xwp.wpAjaxUrl;                       // https://example.com/wp-ajax
const endUrl  = window.xwp.adminAjaxUrl;                    // https://example.com/wp-admin/admin-ajax.php
const ajaxEp  = window.xwp.forWpAjax( 'some', 'endpoint' ); // https://example.com/wp-ajax/some/endpoint
const adminEp = window.xwp.forAdminAjax( 'some_action' );   // https://example.com/wp-admin/admin-ajax.php?action=some_action

常见问题解答

1. 你是重新发明轮子吗?

是的,我们是。我们基本上从 NestJS 和 Express 中拿来了轮子,并将其放在了 WordPress 车上。

2. 为什么?

阅读这里

3. 我可以在我的项目中使用这个吗?

是的,你可以。但请记住,这还是一个正在进行中的项目,我们还在探索做事的最佳方式。API 可能会发生变化。

4. 我可以贡献力量吗?

当然 💪💪💪。
如果你有一个想法,建议或错误报告,请随时提出问题。

5. 它是如何工作的?

阅读这里