| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles all HTTP requests in this plugin. |
| 4 |
* |
| 5 |
* @package Calotes\Helper |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Calotes\Helper; |
| 9 |
|
| 10 |
/** |
| 11 |
* Validates and sanitizes HTTP request data. |
| 12 |
*/ |
| 13 |
class HTTP { |
| 14 |
|
| 15 |
|
| 16 |
/** |
| 17 |
* Strips the protocol from a URL. |
| 18 |
* |
| 19 |
* @param string $url The URL to strip the protocol from. |
| 20 |
* |
| 21 |
* @return string The URL without the protocol. |
| 22 |
*/ |
| 23 |
public static function strips_protocol( $url ) { |
| 24 |
$parts = wp_parse_url( $url ); |
| 25 |
|
| 26 |
$host = $parts['host'] . ( isset( $parts['path'] ) ? $parts['path'] : null ); |
| 27 |
|
| 28 |
return rtrim( $host, '/' ); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Retrieves a value from the $_GET super global array based on the provided key. |
| 33 |
* |
| 34 |
* @param mixed $key The key to search for in the $_GET array. |
| 35 |
* @param mixed $default_name The default value to return if key is not found. |
| 36 |
* @param bool $strict Flag to determine if strict comparison should be used. |
| 37 |
* |
| 38 |
* @return mixed The retrieved value after applying sanitization if needed. |
| 39 |
*/ |
| 40 |
public static function get( $key, $default_name = null, bool $strict = false ) { |
| 41 |
$value = defender_get_data_from_request( $key, 'g' ) ?? $default_name; |
| 42 |
if ( true === $strict && empty( $value ) ) { |
| 43 |
$value = $default_name; |
| 44 |
} |
| 45 |
if ( is_array( $value ) ) { |
| 46 |
$value = defender_sanitize_data( $value ); |
| 47 |
} elseif ( is_string( $value ) ) { |
| 48 |
$value = sanitize_textarea_field( $value ); |
| 49 |
} |
| 50 |
|
| 51 |
return $value; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Retrieves a value from the $_POST super global array based on the provided key. |
| 56 |
* |
| 57 |
* @param mixed $key The key to search for in the $_POST array. |
| 58 |
* @param mixed $default_name The default value to return if key is not found. |
| 59 |
* |
| 60 |
* @return mixed The retrieved value after applying sanitization if needed. |
| 61 |
*/ |
| 62 |
public static function post( $key, $default_name = null ) { |
| 63 |
$value = defender_get_data_from_request( $key, 'p' ) ?? $default_name; |
| 64 |
if ( is_array( $value ) ) { |
| 65 |
$value = defender_sanitize_data( $value ); |
| 66 |
} elseif ( is_string( $value ) ) { |
| 67 |
$value = sanitize_textarea_field( $value ); |
| 68 |
} |
| 69 |
|
| 70 |
return $value; |
| 71 |
} |
| 72 |
} |
| 73 |
|