WordPress AJAX 包装器

1.0.1 2020-02-11 09:58 UTC

This package is auto-updated.

Last update: 2024-09-12 21:01:49 UTC


README

BracketSpace Micropackage Latest Stable Version PHP from Packagist Total Downloads License

Micropackage logo

🧬 关于Ajax

这个微包是一个PHP中WordPress AJAX响应的包装器。

💾 安装

composer require micropackage/ajax

🕹 使用

基本使用

use Micropackage\Ajax\Response;

function ajax_action_handler() {
	$response = new Response();

	// Handle nonce.
	$response->verify_nonce( $action = 'my_action', $query_arg = 'noncefield', $send_if_failed = true );

	// Do some checks and immediately send an error.
	if ( something_is_wrong() ) {
		$response->error( 'Error message' );
	}

	// This is never reached.
	$response->send( 'All good' );

}

错误收集

您可以在一个响应中收集多个错误。

use Micropackage\Ajax\Response;

function ajax_action_handler() {
	$response = new Response();

	// Do some checks.
	if ( something_is_wrong() ) {
		$response->add_error( 'Error message' );
	}

	// Do some checks.
	if ( something_else_is_wrong() ) {
		$response->add_error( 'Whoah!' );
	}

	// If no error added, the below message will be sent.
	$response->send( 'All good if no errors' );

}

发送数据

use Micropackage\Ajax\Response;

function ajax_action_handler() {
	$response = new Response();
	$response->send( $data_array );
}

📦 关于微包项目

微包 - 正如其名所示 - 是带有少量可重用代码的微包,特别是在WordPress开发中非常有用。

目标是拥有多个包,通过定义结构,可以将它们组合起来创建更大的东西。

微包由 BracketSpace 维护。

📖 变更日志

查看变更日志文件.

📃 许可证

GNU通用公共许可证(GPL)v3.0。更多信息请参阅LICENSE 文件。