wberredo/nonce

在面向对象的环境中使用 wordpress nonce 函数。

安装: 41

依赖: 0

建议者: 0

安全: 0

星级: 4

关注者: 1

分支: 5

开放问题: 0

类型:wordpress-plugin

2.0.0 2017-03-31 15:05 UTC

This package is not auto-updated.

Last update: 2024-09-28 20:41:18 UTC


README

Latest Stable Version Latest Unstable Version License

nonce

在面向对象的环境中使用 wordpress nonce 函数。

安装

将此包作为需求添加到您的 composer.json 文件中,然后运行 'composer update'

"wberredo/nonce": "1.0.*"

或者直接运行

composer require wberredo/nonce

设置

如果您想在开始生成 nonce 之前更改一些配置,您将使用 Nonce_Config 类。

// set lifetime for 4 hours
Nonce_Config::set_nonce_lifetime( 4 * HOUR_IN_SECONDS );

// set message showed when showAys is called
Nonce_Config::set_error_message( "Are you sure" );

用法

要创建一个 nonce,您必须使用 Nonce_Generator 类,要验证已创建的 nonce,您需要使用 Nonce_Verifier 类。

Nonce_Generator

生成 nonce

$nonce_gen = new Nonce_Generator( "default-action" );
$nonce = $nonce_gen->generate_nonce();

生成 URL nonce

// you can also set parameters with set functions
$nonce_gen = new Nonce_Generator();
$complete_url = $nonce_gen
                    ->set_url( "http://github.com/WBerredo" )
                    ->set_action( "default_action" )
                    ->generate_nonce_url();

检索 nonce 字段。

$nonce_gen = new Nonce_Generator();
$nonceField = $nonce_gen
                    ->set_action( "default_action" )
                    ->generate_nonce_field( "nonce", "referer", "do_not_echo" );
                    
// to print the nonce field you have to set the last param as true
$nonce_gen
    ->generate_nonce_field( "nonce", "referer", "echo" );

显示“您确定要这样做吗?”消息(或通过 Nonce_Config#setErrorMessage 设置的新消息)以确认所采取的操作。

Nonce_Generator::show_ays( 'action' );

Nonce_Verifier

验证 nonce

if ( Nonce_Verifier::verify( $nonce, $defaultAction ) ) {
// if is valid
} else {
// if is not valid
}

验证 URL nonce

if ( Nonce_Verifier::verify_url( $complete_url, $defaultAction ) ) { 
// if is valid
} else {
// if is not valid
}

测试当前请求是否包含有效的 nonce,或当前请求是否来自管理屏幕。

if ( Nonce_Verifier::verify_admin_referer( $defaultAction ) ) {
// if is valid
} else {
// if is not valid
}

验证 AJAX 请求,以防止任何由第三方网站或系统传递的请求的处理。

if ( Nonce_Verifier::verify_ajax_referer( $defaultAction ) ) {
// if is valid
} else {
// if is not valid
}

贡献

  1. 创建分支
  2. 创建您的功能分支:git checkout -b my-new-feature
  3. 提交您的更改:git commit -am '添加一些功能'
  4. 推送到分支:git push origin my-new-feature
  5. 提交拉取请求 :D

测试

  1. 安装 PHPUnit。 WordPress 使用 PHPUnit,这是 PHP 项目的标准单元测试。安装说明可以在 PHPUnit 手册 或在 PHPUnit Github 存储库 上找到。

  2. 查看测试存储库。 WordPress 测试位于核心开发存储库中,在 https://develop.svn.wordpress.org/trunk/

svn co https://develop.svn.wordpress.org/trunk/ wordpress-develop
cd wordpress-develop
  1. 创建一个空的 MySQL 数据库。 测试套件将从配置的任何 MySQL 数据库的所有表中删除所有数据。请使用单独的数据库。

  2. 设置配置文件。 将 wp-tests-config-sample.php 复制到 wp-tests-config.php,并输入您的数据库凭据。请使用单独的数据库。

  3. 在插件的 bootstrap.php 文件中更改 Wordpress 项目的路径

/**
* The path to the WordPress tests checkout.
*/
define( 'WP_TESTS_DIR', '/home/berredo/Documents/repository/wordpress/wordpress-develop/tests/phpunit/' );
  1. 转到插件的文件夹
cd vendor/wberredo/nonce
  1. 运行 phpunit 进行测试
phpunit 

感谢

许可证

MIT