varunsridharan/wp-ajaxer

为 WP 主题/插件开发者提供的简单轻量级 Ajax 处理器。

1.8.2 2019-07-22 09:57 UTC

This package is auto-updated.

Last update: 2024-08-29 05:07:50 UTC


README

为 WP 主题/插件开发者提供的简单轻量级 Ajax 处理器

Latest Stable Version Total Downloads Latest Unstable Version License composer.lock available

安装

通过 Composer 安装此扩展是首选方式。

要安装 WP_Ajaxer 库,只需

$ composer require Varunsridharan/WP_Ajaxer

前面的命令只会安装必要的文件,如果您想 下载整个源代码,可以使用

$ composer require Varunsridharan/WP_Ajaxer --prefer-source

您也可以使用 Git 克隆完整的仓库

$ git clone https://github.com/varunsridharan/wp-ajaxer.git

或者 手动安装

下载 WP_Ajaxer.php:

$ wget https://raw.githubusercontent.com/varunsridharan/wp-ajaxer/master/class-ajaxer.php

示例用法

class BoilerPlate_Ajax extends Varunsridharan\WordPress\Ajaxer {
	/**
	 * Ajax Action Prefix
	 *
	 * @example for wordpress_show_popup wordpress is the prefix
	 *
	 * @var string
	 */
	protected $action_prefix = 'bp';

	/**
	 * Ajax Action Surfix
	 *
	 * @example for wordpress_show_popup_data data is the surfix
	 *
	 * @var string
	 */
	protected $action_surfix = '';

	/**
	 * Action Name
	 * provide value if all ajax requests runs in a single action key.
	 *
	 * @var string
	 */
	protected $action = 'bp-plugin';

	/**
	 * Array of ajax actions
	 *
	 * @example array('ajax_action_1' => true,'ajax_action_2' => false)
	 *          if value set to true then it runs for both loggedout / logged in users
	 *          if value set to false then it runs only for the logged in user
	 *
	 * @var array
	 */
	protected $actions = array(
		'action1'   => false,
		'action-12' => false,
	);

	/**
	 * Set to true if plugin's ajax runs in a single action
	 *
	 * @example Single Ajax Action
	 *          admin-ajax.php?action=plugin-slug&plugin-slug-action=ajax-action&param1=value1&param2=value=2
	 *          Multiple Ajax Actions
	 *          admin-ajax.php?action=plugin-slug-ajax-action1&param1=value1=param2=value2
	 *
	 * @var bool
	 */
	protected $is_single = true;

	/**
	 * Provide a action key if $this->is_single is set to true
	 *
	 * @var string
	 */
	protected $single_ajax_key = '';

	public function __construct( ) {
		parent::__construct();
	}

	public function action1() {
		///do some action here
	}
}

方法

  • is_get() -> 检查当前 Ajax 是否通过 GET 方法请求
  • is_post() -> 检查当前 Ajax 是否通过 POST 方法请求
  • get($key,$default) -> 从 $_GET 变量中检索给定键的值
  • post($key,$default) -> 从 $_POST 变量中检索给定键的值
  • request($key,$default) -> 从 $_REQUEST 变量中检索给定键的值
  • json_error($data,$status_code) -> 触发 wp_send_json_error
  • json_success($data,$status_code) -> 触发 wp_send_json_success
  • validate_post($key,$error) -> 检查给定的键是否存在于 $_POST 中,如果不存在,发送错误
  • validate_get($key,$error) -> 检查给定的键是否存在于 $_GET 中,如果不存在,发送错误

📝 更新日志

此项目的所有显著更改都将记录在此文件中。

格式基于 Keep a Changelog,并且此项目遵循 语义化版本控制

查看 CHANGELOG.md

🤝 贡献

如果您想帮忙,请查看 问题列表

📜 许可证 & 行为准则

📣 反馈

  • ⭐ 如果此项目对您有帮助! 😉
  • 如果您需要帮助/发现了一个错误,请创建一个 🔧 问题

💰 赞助商

在 2013 年爱上了开源,从那时起就没有回头路了!您可以在 这里 了解更多关于我的信息。如果您或您的公司使用了我的任何项目或喜欢我所做的事情,请考虑支持我。我打算长期坚持。

  • ☕ 咖啡如何?只需 $9.99 就可以给我买一杯咖啡
  • ☕️☕️ 每月给我买两杯咖啡如何?您可以从 $9.99 开始
  • 🔰 我们热爱提升开源项目。只需 一次性支付24.99美元? 支持一小时的开放源代码维护。
  • 🚀 你喜欢开源工具吗?我也是!为何不支持一小时的开放源代码开发,只需 一次性支付49.99美元?

连接 & 问候 👋

用 ❤️ 构建,由 Varun Sridharan 制作