项目作者: WBerredo

项目描述 :
Use wordpress nonce functions in a object oriented environment.
高级语言: PHP
项目地址: git://github.com/WBerredo/nonce.git
创建时间: 2017-02-25T04:55:53Z
项目社区:https://github.com/WBerredo/nonce

开源协议:MIT License

下载


Latest Stable Version
Latest Unstable Version
License

nonce

Use wordpress nonce functions in a object oriented environment.

Installation

Add this package as requirement at your composer.json file and
then run ‘composer update’

  1. "wberredo/nonce": "1.0.*"

Or directly run

  1. composer require wberredo/nonce

Setup

If you want to change some configs before you start to generate
nonces, you will use Nonce_Config class.

  1. // set lifetime for 4 hours
  2. Nonce_Config::set_nonce_lifetime( 4 * HOUR_IN_SECONDS );
  3. // set message showed when showAys is called
  4. Nonce_Config::set_error_message( "Are you sure" );

Usage

To create a nonce you have to use the Nonce_Generator class and
to verify a nonce already created you will need the Nonce_Verifier
class.

Nonce_Generator

To generate a nonce

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

To generate a URL nonce

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

To retrieve a nonce field.

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

To Display ‘Are you sure you want to do this?’ message
(or the new message set with Nonce_Config#setErrorMessage)
to confirm the action being taken.

  1. Nonce_Generator::show_ays( 'action' );

Nonce_Verifier

To verify a nonce

  1. if ( Nonce_Verifier::verify( $nonce, $defaultAction ) ) {
  2. // if is valid
  3. } else {
  4. // if is not valid
  5. }

To verify a URL nonce

  1. if ( Nonce_Verifier::verify_url( $complete_url, $defaultAction ) ) {
  2. // if is valid
  3. } else {
  4. // if is not valid
  5. }

To tests either if the current request carries a valid nonce,
or if the current request was referred from an administration screen

  1. if ( Nonce_Verifier::verify_admin_referer( $defaultAction ) ) {
  2. // if is valid
  3. } else {
  4. // if is not valid
  5. }

To verify the AJAX request, to prevent any processing of
requests which are passed in by third-party sites or systems.

  1. if ( Nonce_Verifier::verify_ajax_referer( $defaultAction ) ) {
  2. // if is valid
  3. } else {
  4. // if is not valid
  5. }

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Tests

  1. Install PHPUnit. WordPress uses PHPUnit, the standard for unit
    testing PHP projects. Installation instructions can be found in
    the PHPUnit manual
    or on the PHPUnit Github repository.

  2. Check out the test repository. The WordPress tests live in
    the core development repository,
    at https://develop.svn.wordpress.org/trunk/:

    1. svn co https://develop.svn.wordpress.org/trunk/ wordpress-develop
    2. cd wordpress-develop
  3. Create an empty MySQL database. The test suite will delete all
    data from all tables for whichever MySQL database it is configured.
    Use a separate database.

  4. Set up a config file. Copy wp-tests-config-sample.php
    to wp-tests-config.php, and enter your database credentials.
    Use a separate database.

  5. Change the path of Wordpress project in the bootstrap.php file of the plugin

    1. /**
    2. * The path to the WordPress tests checkout.
    3. */
    4. define( 'WP_TESTS_DIR', '/home/berredo/Documents/repository/wordpress/wordpress-develop/tests/phpunit/' );
  6. Go to plugin’s folder

    1. cd vendor/wberredo/nonce
  7. Run phpunit to test

    1. phpunit

Thanks to

License

MIT