vitalis/wpnonce

An OOP implementation of WordPress Nonces

dev-master 2020-01-13 18:38 UTC

This package is not auto-updated.

Last update: 2025-06-11 06:48:35 UTC


README

This library is an object oriented implementation of WordPress nonces. It implements the following WordPress functions:

  • wp_create_nonce()
  • wp_verify_nonce()
  • wp_nonce_ays()
  • wp_nonce_field()
  • wp_nonce_url()

For an explanation of what WordPress nonces are please see WordPress codex.
PLEASE NOTE: This is a development version that, for generating the nonces, uses fake algorithms and stubbed values instead of real algorithms and values.

Installation

This library is distributed as a Composer package.
To install the development version:

composer.phar install

To install the production version (skipping the development packages):

composer.phar install --no-dev

Required PHP version

This library is developed with PHP 7.1.x.

Coding standards

Code style: PSR-1 and PSR-2.
Autoloader: PSR-4 autoloader generated by Composer.

How to use this library

The preferred method for using this library is through the provided Factory. Here you can find some examples.

Examples:

wp_create_nonce() functionality

Getting a nonce for the action 'my_action':

use Agavitalis\WPNonce\Factories\WPNonceFactory;
$nonce_string = (new WPNonceFactory('my_action'))->getRaw()->get();

wp_nonce_field() functionality

Getting hidden fields with nonce, for action 'my_action', field name 'my_nonce', with the referer and without echo:

use Agavitalis\WPNonce\Factories\WPNonceFactory;
$nonce_field_string = (new WPNonceFactory('my_action'))->getField('my_nonce', true, false)->get();

wp_nonce_url() functionality

Getting a nonce added to the url http://example.com/insertdata , for action 'my_action', with name 'my_nonce':

use Agavitalis\WPNonce\Factories\WPNonceFactory;
$nonce_url_string = (new WPNonceFactory('my_action'))->getUrl('http://example.com/insertdata', 'my_nonce')->get();

wp_verify_nonce() functionality

If you receive the string 'abcd123456', the action is 'my_action' and you want to verify this nonce against your action you'll use:

use Agavitalis\WPNonce\Factories\WPNonceFactory;
$nonce_ok = (new WPNonceFactory('my_action'))->getRaw()->verify('abcd123456');

wp_nonce_ays() functionality

To display the "Are you sure" message to confirm the action being taken, for the action 'my_action':

use Agavitalis\WPNonce\Factories\WPNonceFactory;
(new WPNonceFactory('my_action'))->getRaw()->ays();

Tests

This library comes with unit tests for PHPUnit version 6.1.x. The tests use the PSR-4 autoloader generated by Composer.
To set up the development environment follow the instructions of the 'Installation' section above.

License

This library is licensed under the terms of the GPLv2+ license. See the provided LICENSE.md file.