Assets.php
15 hours ago
Cache.php
3 weeks ago
Date.php
15 hours ago
Debug.php
15 hours ago
FeedReader.php
15 hours ago
HTML.php
15 hours ago
Helper.php
3 weeks ago
JSON.php
3 weeks ago
Nonce.php
3 weeks ago
Notice.php
15 hours ago
Number.php
3 weeks ago
NumberConverter.php
15 hours ago
Param.php
3 weeks ago
Sanitizing.php
15 hours ago
Strip.php
3 weeks ago
Templates.php
3 weeks ago
User.php
3 weeks ago
Validating.php
3 weeks ago
WooCommerce.php
3 weeks ago
WordPress.php
15 hours ago
Nonce.php
38 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPParsidate\Helper; |
| 4 | |
| 5 | class Nonce { |
| 6 | /** |
| 7 | * Create nonce |
| 8 | * |
| 9 | * @param string $action Scalar value to add context to the nonce. |
| 10 | * |
| 11 | * @return string The token. |
| 12 | */ |
| 13 | public static function create( string $action = WP_PARSI_KEY ): string { |
| 14 | return wp_create_nonce( $action ); |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Verifies that a correct security nonce was used with time limit. |
| 19 | * |
| 20 | * A nonce is valid for between 12 and 24 hours (by default). |
| 21 | * |
| 22 | * @param string|null $nonce Nonce value that was used for verification, usually via a form field. |
| 23 | * @param string|int $action Should give context to what is taking place and be the same when nonce was created. |
| 24 | * |
| 25 | * @return int|false 1 if the nonce is valid and generated between 0-12 hours ago, |
| 26 | * 2 if the nonce is valid and generated between 12-24 hours ago. |
| 27 | * False if the nonce is invalid. |
| 28 | */ |
| 29 | public static function verify( ?string $nonce = null, $action = WP_PARSI_KEY ) { |
| 30 | $nonce = is_null( $nonce ) && isset( $_POST['nonce'] ) ? Sanitizing::text( wp_unslash( Param::post( 'nonce' ) ) ) : $nonce; |
| 31 | if ( is_null( $nonce ) ) { |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | return wp_verify_nonce( $nonce, $action ); |
| 36 | } |
| 37 | } |
| 38 |