unprefix/nonce

WordPress Nonce 的 OOP 封装

dev-dev 2017-11-07 17:38 UTC

This package is not auto-updated.

Last update: 2024-09-15 04:20:56 UTC


README

Build Status codecov

Unprefix Nonce

一个用 OOP 方法封装 nonce 逻辑的 WordPress 扩展包。

要求

安装

使用 composer

composer require unprefix/nonce

许可证

该包是开源的,并按照 GPL-2 许可证发布。有关更多信息,请参阅 LICENSE。

问题

您可以通过 github issues 提交问题。

文档

创建基本 Nonce

use Unprefix\Nonce\Nonce;

// This create a new nonce instance.
$nonce = new Nonce('action_name');

// To retrieve the nonce just do this.
$nonce->nonce();

// To retrieve the nonce action.
$nonce->action();

使用助手创建基本 Nonce

use Unprefix\Nonce\Nonce;

// This will generate the nonce and return the nonce string, all at once.
$nonce = Nonce::create('nonce_action');

创建 URL Nonce

NonceUrl 构造函数接受三个参数,一个 Nonce 实例,一个 nonce 名称 和一个添加 nonce 的 URL

use Unprefix\Nonce\NonceUrl;

$nonceUrl = new NonceUrl(
    new Nonce('nonce_action'),
    'nonce_name',
    'http://www.mycustomurl.com'
);

// Retrieve the url.
$nonceUrl->url();

// To retrieve the name.
$nonceUrl->name();

使用助手创建 nonce URL

使用助手函数时,不需要传递 Nonce 实例,只需将动作名称作为第一个参数传递。

use Unprefix\Nonce\NonceUrl;

// Retrieve the nonce url string at once.
$nonceUrl = NonceUrl::create('action_name', 'nonce_name', 'http://www.mycustomurl.com');

创建 nonce 字段

NonceUrl 类似,创建 NonceField 时,您必须传递一个 Nonce 实例以及 名称引用者 参数。

引用者参数是可选的,如果您不想包含引用者输入字段,可以忽略它。

use Unprefix\Nonce\NonceField;

$nonceField = new NonceField(
    new Nonce('nonce_action'),
    'nonce_name',
    true
);

// Show the nonce field.
$nonceField->tmpl(
    $nonceField->data()
);

NonceField 类实现了来自 unprefix/unprefix-templateloader 的 Unprefix\TemplateInterface,用于打印字段的标记。

该文件在 src/ 目录上两级目录的 views 目录中搜索。

您可以通过挂钩到 tmploader_template_file_path 过滤器来过滤模板路径,如 Loader.php 中所示。

同样,您也可以使用助手函数一次性打印字段。

use Unprefix\Nonce\NonceField;

NonceField::field('nonce_action', 'nonce_name', true);

验证 Nonce

要验证 nonce,可以使用 NonceVerification 类。

该类提供了一个方法,可用于验证 nonce、管理引用者和 AJAX 引用者。

use Unprefix\Nonce\NonceValidation;

// Create the instance
$nonceVerify = new NonceVerification(
    new Nonce('nonce_action'),
    'name_action',
    'POST', // But can be GET, REQUEST
    false
);

$nonceVerify->verify();