Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mehulkaklotar/wp-nonce
WordPress Nonce Implementation Object Oriented Way
https://github.com/mehulkaklotar/wp-nonce
oop wordpress wordpress-nonce wordpress-security
Last synced: about 1 month ago
JSON representation
WordPress Nonce Implementation Object Oriented Way
- Host: GitHub
- URL: https://github.com/mehulkaklotar/wp-nonce
- Owner: mehulkaklotar
- License: gpl-2.0
- Created: 2017-04-04T04:45:25.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-04-18T10:30:46.000Z (almost 8 years ago)
- Last Synced: 2024-10-30T20:48:46.841Z (3 months ago)
- Topics: oop, wordpress, wordpress-nonce, wordpress-security
- Language: PHP
- Size: 44.9 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.org/mehulkaklotar/wp-nonce.svg?branch=master)](https://travis-ci.org/mehulkaklotar/wp-nonce)
WPNonce
===================WordPress Nonce In An Object Oriented Way.
## Basic Usage
1. Create a demo `composer.json` file in your plugin.
2. Run `composer install`
3. It will load plugin dependency in a `vendor/` folder### demo `composer.json`
```
{
"repositories": [
{
"type": "vcs",
"url" : "https://github.com/mehulkaklotar/wp-nonce"
}
],
"require": {
"mehulkaklotar/wp-nonce" : "1.0.*"
}
}
```Here I have created a demo plugin to use this system. [WP Nonce Client](https://github.com/mehulkaklotar/wp-nonce-client)
### The Settings:
WP Nonce need an action to find the current action which is secured by a nonce. The first parameter of the configuration defines this name. Usually forms or URLs passes the nonce. The second parameter is for request key. In this case, we would expect the nonce to be in `$_REQUEST['request_name']`.
```
$setting = new NonceSetting(
'action',
'request_name'
);
```### To Create a Nonce
To create a simple Nonce, use `NonceCreate`:
```
$nonce_create = new NonceCreate( $setting );
$nonce = $nonce_create->create();
```To add a nonce to an URL, you can use
```
$nonce_create = new NonceCreateURL( $setting );
$url = $nonce_create->create_url( 'http://example.com/' );
```
Return URL will be:
`http://example.com/?request_name=$nonce`To add a form field:
```
$create = new NonceCreateField( $setting );
$field = $create->create_field();
```
Return field will be:
``Replicate `wp_nonce_field()` functionality by adding two parameters: `(bool) $referer` and `(bool) $echo`. Both are set to `false` by default.
Set `$referer` to `true`, field will be appended with the URL of the current page.
Set `$echo` to `true`, it will echo the field, before `create_url()`.### To Verify a Nonce
To verify a nonce, you can use `NonceVerify`:
```
$nonce_verify = new NonceVerify( $setting );
$is_valid = $nonce_verify->verify( $nonce );
```