| 1 |
<?php |
| 2 |
|
| 3 |
namespace FakerPress; |
| 4 |
|
| 5 |
/** |
| 6 |
* Determines if the provided value should be regarded as 'true'. |
| 7 |
* |
| 8 |
* @since 0.5.1 |
| 9 |
* |
| 10 |
* @param mixed $var Value to be tested. |
| 11 |
* |
| 12 |
* @return bool |
| 13 |
*/ |
| 14 |
function is_truthy( $var ): bool { |
| 15 |
if ( is_bool( $var ) ) { |
| 16 |
return $var; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Provides an opportunity to modify strings that will be |
| 21 |
* deemed to evaluate to true. |
| 22 |
* |
| 23 |
* @param array $truthy_strings |
| 24 |
*/ |
| 25 |
$truthy_strings = (array) apply_filters( 'fakerpress_is_truthy_strings', [ |
| 26 |
'1', |
| 27 |
'enable', |
| 28 |
'enabled', |
| 29 |
'on', |
| 30 |
'y', |
| 31 |
'yes', |
| 32 |
'true', |
| 33 |
] ); |
| 34 |
// Makes sure we are dealing with lowercase for testing |
| 35 |
if ( is_string( $var ) ) { |
| 36 |
$var = strtolower( $var ); |
| 37 |
} |
| 38 |
|
| 39 |
// If $var is a string, it is only true if it is contained in the above array |
| 40 |
if ( in_array( $var, $truthy_strings, true ) ) { |
| 41 |
return true; |
| 42 |
} |
| 43 |
|
| 44 |
// All other strings will be treated as false |
| 45 |
if ( is_string( $var ) ) { |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
// For other types (ints, floats etc) cast to bool |
| 50 |
return (bool) $var; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Determines if the provided value is a regular expressions. |
| 55 |
* |
| 56 |
* @since 0.5.1 |
| 57 |
* |
| 58 |
* @param mixed $variable Value to be tested. |
| 59 |
* |
| 60 |
* @return bool |
| 61 |
*/ |
| 62 |
function is_regex( $variable ) { |
| 63 |
// @codingStandardsIgnoreStart |
| 64 |
$test = @preg_match( $variable, '' ); |
| 65 |
|
| 66 |
// @codingStandardsIgnoreEnd |
| 67 |
|
| 68 |
return $test !== false; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Determines if the provided value is an IP address. |
| 73 |
* |
| 74 |
* @since 0.5.1 |
| 75 |
* |
| 76 |
* @param mixed $variable Value to be tested. |
| 77 |
* |
| 78 |
* @return bool |
| 79 |
*/ |
| 80 |
function is_ip_address( $variable ) { |
| 81 |
return false !== WP_Http::is_ip_address( $variable ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Determines if we are doing an AJAX request. |
| 86 |
* |
| 87 |
* @since 0.6.0 |
| 88 |
* |
| 89 |
* @return bool |
| 90 |
*/ |
| 91 |
function is_ajax(): bool { |
| 92 |
if ( defined( 'DOING_AJAX' ) ) { |
| 93 |
return DOING_AJAX; |
| 94 |
} |
| 95 |
|
| 96 |
return false; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Check if it's a given request method. |
| 101 |
* |
| 102 |
* @since 0.6.0 |
| 103 |
* |
| 104 |
* @param string $type Which type of request we are checking against. |
| 105 |
* |
| 106 |
* @return bool |
| 107 |
*/ |
| 108 |
function is_request_method( string $type ): bool { |
| 109 |
return strtoupper( $type ) === get_request_method(); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Check if it's a POST request. |
| 114 |
* |
| 115 |
* @since 0.6.0 |
| 116 |
* |
| 117 |
* @return bool |
| 118 |
*/ |
| 119 |
function is_post_request(): bool { |
| 120 |
return is_request_method( 'POST' ); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Check if it's a GET request. |
| 125 |
* |
| 126 |
* @since 0.6.0 |
| 127 |
* |
| 128 |
* @return bool |
| 129 |
*/ |
| 130 |
function is_get_request(): bool { |
| 131 |
return is_request_method( 'GET' ); |
| 132 |
} |
| 133 |
|