conditionals.php
1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/**
* Determines if the provided value should be regarded as 'true'.
*
* @since 0.5.1
*
* @param mixed $var Value to be tested.
*
* @return bool
*/
function fp_is_truthy( $var ) {
if ( is_bool( $var ) ) {
return $var;
}
/**
* Provides an opportunity to modify strings that will be
* deemed to evaluate to true.
*
* @param array $truthy_strings
*/
$truthy_strings = (array) apply_filters( 'fakerpress_is_truthy_strings', [
'1',
'enable',
'enabled',
'on',
'y',
'yes',
'true',
] );
// Makes sure we are dealing with lowercase for testing
if ( is_string( $var ) ) {
$var = strtolower( $var );
}
// If $var is a string, it is only true if it is contained in the above array
if ( in_array( $var, $truthy_strings, true ) ) {
return true;
}
// All other strings will be treated as false
if ( is_string( $var ) ) {
return false;
}
// For other types (ints, floats etc) cast to bool
return (bool) $var;
}
/**
* Determines if the provided value is a regular expressions.
*
* @since 0.5.1
*
* @param mixed $variable Value to be tested.
*
* @return bool
*/
function fp_is_regex( $variable ) {
// @codingStandardsIgnoreStart
$test = @preg_match( $variable, '' );
// @codingStandardsIgnoreEnd
return $test !== false;
}
/**
* Determines if the provided value is an IP address.
*
* @since 0.5.1
*
* @param mixed $variable Value to be tested.
*
* @return bool
*/
function fp_is_ip_address( $variable ) {
return false !== WP_Http::is_ip_address( $variable );
}