pinkcrab/wp-nonce

基于类的简单 WP Nonce 解决方案

0.1.0 2021-02-11 11:42 UTC

This package is auto-updated.

Last update: 2024-09-11 19:33:33 UTC


README

基于类的简单 WP Nonce 解决方案

alt text Open Source Love

codecov

了解更多详细信息,请访问我们的文档。 https://app.gitbook.com/@glynn-quelch/s/pinkcrab/

版本

发布 0.1.0

为什么?

允许以面向对象的方式使用 Nonces,同时允许对象的序列化和反序列化。

设置

$ composer require pinkcrab/wp-nonce
    $nonce = new Nonce('my_none_key');

    // To get the current nonce token
    $nonce->token();
    
    // To validate
    $nonce->validate($_POST['nonce']); // true/false

    // To add to url
    $url = $nonce->as_url('http://www.url.com', 'my_nonce'); // http://www.url.com?my_nonce={nonce_value}

    // Validate url.
    $nonce->admin_referer('my_nonce'); // true/false if set in url.

方法

创建实例

// Create with a custom key
$custom_nonce = new Nonce('custom_key');

一旦创建 Nonce,就可以在代码库中序列化和/或传递。

as_url( string $url, string $arg='_wpnonce' ): string

$nonce = new Nonce('url_key');

$custom_key_in_url = $nonce->as_url('http://test.com', 'url_nonce');
// http://test.com?url_nonce={nonce_token}

$default_key_in_url = $nonce->as_url('http://test.com');
// http://test.com?_wpnonce={nonce_token}

注意!这没有使用在管理 Nonce 中找到的 refer 值。

token(): string

$nonce = new Nonce('url_key');

// To get the current nonce value.
print $nonce->token(); // 31b31db189

$nonce_token = nonce->token(); // 31b31db189

nonce_field($name = '_wpnonce'): string

$nonce = new Nonce('as_input');

// Create a nonce field, with a custom id/name for input
print $nonce->nonce_field('my_nonce'); 
// <input type="hidden" id="my_nonce" name="my_nonce" value="{nonce_token}">

// Create a nonce field, with a custom id/name for input
print $nonce->nonce_field(); 
// <input type="hidden" id="_wpnonce_" name="_wpnonce_" value="{nonce_token}">

该 nonce 字段不会自动打印

validate($name = '_wpnonce'): string

$nonce = new Nonce('as_input');

// Create a nonce field, with a custom id/name for input
print $nonce->nonce_field('my_nonce'); 
// <input type="hidden" id="my_nonce" name="my_nonce" value="{nonce_token}">

// Create a nonce field, with a custom id/name for input
print $nonce->nonce_field(); 
// <input type="hidden" id="_wpnonce_" name="_wpnonce_" value="{nonce_token}">

该 nonce 字段不会自动打印

依赖项

  • --NONE--

许可证

MIT 许可证

https://open-source.org.cn/licenses/mit-license.html

变更日志

0.1.0 - 从 PC 框架 0.1.0 的一部分创建