farsil / wp-nonce
Wordpress非ces的对象实现
v1.0.0
2018-02-22 12:40 UTC
Requires
- php: >=7.0
This package is not auto-updated.
Last update: 2024-09-29 05:08:14 UTC
README
wp-nonce
是一个用PHP编写的Wordpress非ces的替代库。它旨在提供一个面向对象的接口,可以用来替代Wordpress的 wp_nonce_*
函数。
安装
将以下条目添加到您的 composer.json
文件中的 required
列表中
"farsil/wp-nonce": "v1.0.*"
或者,也可以运行
$ composer require farsil/wp-nonce
用法
非ces生成是通过 Nonce_Generator
类实现的,非ces验证是通过 Nonce_Validator
类实现的。方法名称与Wordpress核心函数的对应方法相似,行为也类似,但有一些差异。有关更多详细信息,请参阅文档。
示例
<?php
// We don't need to load themes
define( 'WP_USE_THEMES', false );
// This line assumes your Wordpress installation is in your PHP include path,
// change accordingly if it is not the case.
require_once 'wp-load.php';
// Let composer autoload wp-nonce classes.
require_once 'vendor/autoload.php';
// Generate a new nonce. Default lifetime is 1 day.
$gen = new Wordpress\Nonce_Generator( 'test-nonce' );
$token = $gen->generate();
// In order to properly validate the generated token, the constructor parameters
// need to match those used in Nonce_Generator.
$val = new Wordpress\Nonce_Validator( 'test-nonce' );
echo $val->verify( $token );
预期输出
1
测试
wp-nonce
包含一个全面的PHPUnit测试套件。由于 wp-nonce
包含一个 phpunit.xml.dist
文件,您只需要导航到项目根目录,然后运行
$ php /path/to/phpunit.phar --include-path=/path/to/wordpress