| 1 |
<?php |
| 2 |
|
| 3 |
namespace Leadin\utils; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class containing utility functions for the sanitizing and getting query parameters. |
| 7 |
*/ |
| 8 |
class QueryParameters { |
| 9 |
|
| 10 |
/** |
| 11 |
* Return a text sanitized, unslashed query parameter by its key, validated with a nonce. |
| 12 |
* |
| 13 |
* @param String $key Key for the query parameter to return. |
| 14 |
* @param String $nonce_action Name of the nonce action to verify the given nonce against. |
| 15 |
* @param String $nonce_arg Query parmeter the nonce is in. |
| 16 |
*/ |
| 17 |
public static function get_param( $key, $nonce_action, $nonce_arg = '_wpnonce' ) { |
| 18 |
if ( |
| 19 |
isset( $_GET[ $key ] ) && |
| 20 |
isset( $_GET[ $nonce_arg ] ) && |
| 21 |
wp_verify_nonce( sanitize_text_field( wp_unslash( ( $_GET[ $nonce_arg ] ) ) ), $nonce_action ) |
| 22 |
) { |
| 23 |
return sanitize_text_field( wp_unslash( ( $_GET[ $key ] ) ) ); |
| 24 |
} |
| 25 |
|
| 26 |
return null; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Return an array sanitized, unslashed query parameter by its key, validated with a nonce. |
| 31 |
* |
| 32 |
* @param String $key Key for the query parameter to return. |
| 33 |
* @param String $nonce_action Name of the nonce action to verify the given nonce against. |
| 34 |
* @param String $nonce_arg Query parmeter the nonce is in. |
| 35 |
*/ |
| 36 |
public static function get_param_array( $key, $nonce_action, $nonce_arg = '_wpnonce' ) { |
| 37 |
if ( |
| 38 |
isset( $_GET[ $key ] ) && |
| 39 |
isset( $_GET[ $nonce_arg ] ) && |
| 40 |
wp_verify_nonce( sanitize_text_field( wp_unslash( ( $_GET[ $nonce_arg ] ) ) ), $nonce_action ) |
| 41 |
) { |
| 42 |
return array_map( 'sanitize_text_field', wp_unslash( $_GET[ $key ] ) ); |
| 43 |
} |
| 44 |
|
| 45 |
return array(); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Return an associative array of query param keys and values given an array of keys |
| 50 |
* that are sanitized and validated against a nonce |
| 51 |
* |
| 52 |
* @param String $keys Array of keys to fetch query parameter values for. |
| 53 |
* @param String $nonce_action Name of the nonce action to verify the given nonce against. |
| 54 |
* @param String $nonce_arg Query parmeter the nonce is in. |
| 55 |
*/ |
| 56 |
public static function get_parameters( $keys, $nonce_action, $nonce_arg = '_wpnonce' ) { |
| 57 |
$query_params = array_reduce( |
| 58 |
$keys, |
| 59 |
function( $result, $key ) use ( $nonce_arg, $nonce_action ) { |
| 60 |
$query_param = QueryParameters::get_param( $key, $nonce_action, $nonce_arg ); |
| 61 |
|
| 62 |
$result[ $key ] = $query_param; |
| 63 |
return $result; |
| 64 |
}, |
| 65 |
array() |
| 66 |
); |
| 67 |
|
| 68 |
return $query_params; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
|