2017 © Pedro Peláez
 

library wp-oop-nonces

Classes that serves the WordPress Nonces functionality in an object oriented way.

image

madaritech/wp-oop-nonces

Classes that serves the WordPress Nonces functionality in an object oriented way.

  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 4 Versions
  • 7 % Grown

The README.md

wp-oop-nonces

Package that serves the WordPress Nonces functionality (wp_nonce_*()) in an object orientated way., (*1)

Requirements

  • PHP 5.6+
  • Composer
  • WordPress 4.8.3+

Installation

Install with Composer:, (*2)

$ composer require madaritech/wp-oop-nonces

Run the tests

To run tests, executes commands below:, (*3)

$ cd vendor/madaritech/wp-oop-nonces
$ composer install
$ vendor/bin/phpunit

Usage

The code refers to the wordPress workflow for nonce creation and verification. For more info see Codex., (*4)

Nonce Generation

In WordPress the nonce generation is achieved with the wp_create_nonce() function specifying a string representing the action., (*5)

Similarly, to proceed with nonce generation use the Nonce_Generator class with the proper action:, (*6)

$generator = new Nonce_Generator( 'action_parameter' );

Then, to generate the nonce use the generate_nonce() method:, (*7)

$nonce = $generator->generate_nonce();

Nonce Url Generation

To add a nonce to a URL, WordPress uses wp_nonce_url() specifying the bare url and a string representing the action. Optionally is possible to specify a string for the name parameter, otherwise it defaults to '_wpnonce'., (*8)

Similarly, to generate a url with a nonce query parameter use the Nonce_Url_Generator class with the proper action value and optionally with the proper name parameter, otherwise it defaults to '_wp_nonce'., (*9)

$url_generator = new Nonce_Url_Generator( 'action_parameter' );

So, use the generate_nonce_url() method with the proper url to generate the url with the nonce., (*10)

$url = $url_generator->generate_nonce_url( 'http://www.madaritech.com' );

The same class can also generate a nonce directly:, (*11)

$nonce = $url_generator->generate_nonce();

Nonce Field Generation

To add a nonce to a form, WordPress uses wp_nonce_field() specifying a string representing the action. By default wp_nonce_field() generates two hidden fields, one whose value is the nonce and one whose value is the current url (the referrer), and it echoes the result., (*12)

Similarly, to generate form fields with nonce use the Nonce_Field_Generator class with the proper action value:, (*13)

$field_generator = new Nonce_Field_Generator( 'action_parameter' );

Optionally, the constructor accepts other parameters that affects the nonce field generation result (generate_nonce_field() method):, (*14)

  1. name: the name of the nonce field. Defaults to '_wpnonce'.
  2. referer: boolean value to add an hidden field with refer url value. Set it to false to not add the field. Defaults to true.
  3. echo: boolean value to print the field/s. Set it to false to not print the fields. Defaults to true.

So, use the generate_nonce_field() method to generate the field/s with the nonce., (*15)

$field_generated = $field_generator->generate_nonce_field()

The same class can also generate a nonce directly:, (*16)

$nonce = $field_generator->generate_nonce();

Nonce Validation

To verify a nonce WordPress uses wp_verify_nonce() specifying the nonce and the string representing the action., (*17)

Similarly, validating funtionality is provided through the Nonce_Validator class; the constructor accept an action parameter (the same used to generate the nonce we want to validate) to verify the nonce:, (*18)

$validator = new Nonce_Validator( 'action_parameter' );

Nonce Straight Validation

To validate a nonce use the validate_nonce() method with the nonce to verify as parameter:, (*19)

$is_valid = $validator->validate_nonce($nonce);

If the validation is successful the method returns true; false otherwise., (*20)

Nonce Request Validation

To validate a nonce received in a page through request (GET or POST) use the validate_request() method:, (*21)

$is_valid = $validator->validate_request();

If the validation is successful the method returns true; false otherwise., (*22)

The Versions

23/11 2017

dev-master

9999999-dev https://github.com/madaritech/wp-oop-nonces

Classes that serves the WordPress Nonces functionality in an object oriented way.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

wordpress oop nonces

23/11 2017

v1.0.2

1.0.2.0 https://github.com/madaritech/wp-oop-nonces

Classes that serves the WordPress Nonces functionality in an object oriented way.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

wordpress oop nonces

22/11 2017

v1.0.1

1.0.1.0 https://github.com/madaritech/wp-oop-nonces

Classes that serves the WordPress Nonces functionality in an object oriented way.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

wordpress oop nonces

22/11 2017

v1.0.0

1.0.0.0 https://github.com/madaritech/wp-oop-nonces

Classes that serves the WordPress Nonces functionality in an object oriented way.

  Sources   Download

MIT

The Requires

  • php 5.6.*

 

The Development Requires

wordpress oop nonces