1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TGM; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Generic utilities for TGMPA. |
7
|
|
|
* |
8
|
|
|
* All methods are static, poor-dev name-spacing class wrapper. |
9
|
|
|
* |
10
|
|
|
* Class was called TGM_Utils in 2.5.0 but renamed TGMPA_Utils in 2.5.1 as this was conflicting with Soliloquy. |
11
|
|
|
* |
12
|
|
|
* @since 2.5.0 |
13
|
|
|
* |
14
|
|
|
* @package TGM-Plugin-Activation |
15
|
|
|
* @author Juliette Reinders Folmer |
16
|
|
|
*/ |
17
|
|
|
class TGMPA_Utils { |
18
|
|
|
/** |
19
|
|
|
* Whether the PHP filter extension is enabled. |
20
|
|
|
* |
21
|
|
|
* @see http://php.net/book.filter |
22
|
|
|
* |
23
|
|
|
* @since 2.5.0 |
24
|
|
|
* |
25
|
|
|
* @static |
26
|
|
|
* |
27
|
|
|
* @var bool $has_filters True is the extension is enabled. |
28
|
|
|
*/ |
29
|
|
|
public static $has_filters; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Wrap an arbitrary string in <em> tags. Meant to be used in combination with array_map(). |
33
|
|
|
* |
34
|
|
|
* @since 2.5.0 |
35
|
|
|
* |
36
|
|
|
* @static |
37
|
|
|
* |
38
|
|
|
* @param string $string Text to be wrapped. |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
public static function wrap_in_em( $string ) { |
42
|
|
|
return '<em>' . wp_kses_post( $string ) . '</em>'; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Wrap an arbitrary string in <strong> tags. Meant to be used in combination with array_map(). |
47
|
|
|
* |
48
|
|
|
* @since 2.5.0 |
49
|
|
|
* |
50
|
|
|
* @static |
51
|
|
|
* |
52
|
|
|
* @param string $string Text to be wrapped. |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public static function wrap_in_strong( $string ) { |
56
|
|
|
return '<strong>' . wp_kses_post( $string ) . '</strong>'; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Helper function: Validate a value as boolean |
61
|
|
|
* |
62
|
|
|
* @since 2.5.0 |
63
|
|
|
* |
64
|
|
|
* @static |
65
|
|
|
* |
66
|
|
|
* @param mixed $value Arbitrary value. |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
public static function validate_bool( $value ) { |
70
|
|
|
if ( ! isset( self::$has_filters ) ) { |
71
|
|
|
self::$has_filters = extension_loaded( 'filter' ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ( self::$has_filters ) { |
75
|
|
|
return filter_var( $value, FILTER_VALIDATE_BOOLEAN ); |
76
|
|
|
} else { |
77
|
|
|
return self::emulate_filter_bool( $value ); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Helper function: Cast a value to bool |
83
|
|
|
* |
84
|
|
|
* @since 2.5.0 |
85
|
|
|
* |
86
|
|
|
* @static |
87
|
|
|
* |
88
|
|
|
* @param mixed $value Value to cast. |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
protected static function emulate_filter_bool( $value ) { |
92
|
|
|
// @codingStandardsIgnoreStart |
93
|
|
|
static $true = array( |
94
|
|
|
'1', |
95
|
|
|
'true', 'True', 'TRUE', |
96
|
|
|
'y', 'Y', |
97
|
|
|
'yes', 'Yes', 'YES', |
98
|
|
|
'on', 'On', 'ON', |
99
|
|
|
); |
100
|
|
|
static $false = array( |
101
|
|
|
'0', |
102
|
|
|
'false', 'False', 'FALSE', |
103
|
|
|
'n', 'N', |
104
|
|
|
'no', 'No', 'NO', |
105
|
|
|
'off', 'Off', 'OFF', |
106
|
|
|
); |
107
|
|
|
// @codingStandardsIgnoreEnd |
108
|
|
|
|
109
|
|
|
if ( is_bool( $value ) ) { |
110
|
|
|
return $value; |
111
|
|
|
} elseif ( is_int( $value ) && ( 0 === $value || 1 === $value ) ) { |
112
|
|
|
return (bool) $value; |
113
|
|
|
} elseif ( ( is_float( $value ) && ! is_nan( $value ) ) && ( (float) 0 === $value || (float) 1 === $value ) ) { |
114
|
|
|
return (bool) $value; |
115
|
|
|
} elseif ( is_string( $value ) ) { |
116
|
|
|
$value = trim( $value ); |
117
|
|
|
if ( in_array( $value, $true, true ) ) { |
118
|
|
|
return true; |
119
|
|
|
} elseif ( in_array( $value, $false, true ) ) { |
120
|
|
|
return false; |
121
|
|
|
} else { |
122
|
|
|
return false; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return false; |
127
|
|
|
} |
128
|
|
|
} // End of class TGMPA_Utils |
129
|
|
|
|