PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.23.1
Independent Analytics – WordPress Analytics Plugin v1.23.1
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / IAWP / Utils / Option.php
independent-analytics / IAWP / Utils Last commit date
Array_To_CSV.php 3 years ago Currency.php 3 years ago Number_Formatter.php 3 years ago Option.php 3 years ago Request.php 3 years ago Salt.php 3 years ago Security.php 3 years ago Singleton.php 3 years ago String_Util.php 3 years ago Timezone.php 3 years ago URL.php 3 years ago WP_Async_Request.php 3 years ago WordPress_Site_Date_Format_Pattern.php 3 years ago
Option.php
176 lines
1 <?php
2
3 namespace IAWP_SCOPED\IAWP\Utils;
4
5 class Option
6 {
7 const STRING = 'string';
8 const BOOLEAN = 'boolean';
9 const BOOL = 'boolean';
10 const INT = 'integer';
11 const INTEGER = 'integer';
12 const FLOAT = 'float';
13 const DOUBLE = 'float';
14 const ARRAY = 'array';
15 private static $supported_types = ['string', 'boolean', 'integer', 'float', 'array'];
16 /**
17 * @var array
18 */
19 private static $registered_options = [];
20 /**
21 * @var string
22 */
23 protected $option_name;
24 /**
25 * @var string
26 */
27 private $type;
28 /**
29 * @var mixed|null
30 */
31 private $default_value;
32 /**
33 * @var boolean
34 */
35 private $allow_empty_string = \false;
36 public function __construct(string $option_name, string $type, $default_value = null, array $options = [])
37 {
38 $this->option_name = $option_name;
39 $this->type = $type;
40 $this->default_value = $default_value;
41 if (\array_key_exists('allow_empty_string', $options)) {
42 $this->allow_empty_string = \filter_var($options['allow_empty_string'], \FILTER_VALIDATE_BOOLEAN);
43 }
44 // Throw an error if the type is unsupported
45 if (!\in_array($type, self::$supported_types)) {
46 throw new \Exception("{$type} is not a supported type");
47 }
48 // Throw an error if the default_value is set, but does not match the options types
49 if (!\is_null($default_value) && $type !== $this->get_type($default_value)) {
50 throw new \Exception("Default value for {$option_name} is not of type {$type}");
51 }
52 // Throw an error if option name is already registered
53 foreach (self::$registered_options as $option) {
54 if ($option->option_name === $option_name) {
55 throw new \Exception("Option {$option_name} is already registered");
56 }
57 }
58 self::$registered_options[] = $this;
59 }
60 /**
61 * @param $value
62 *
63 * @return string
64 */
65 private function get_type($value) : string
66 {
67 $type = \gettype($value);
68 if ($type === 'double') {
69 return 'float';
70 }
71 return $type;
72 }
73 /**
74 * @param mixed $value
75 *
76 * @return mixed
77 */
78 private function convert_to_wordpress($value)
79 {
80 switch ($this->type) {
81 case 'boolean':
82 return $value === \true ? 1 : 0;
83 }
84 return $value;
85 }
86 /**
87 * @param mixed $value
88 *
89 * @return mixed
90 */
91 private function convert_from_wordpress($value)
92 {
93 switch ($this->type) {
94 case 'integer':
95 /**
96 * If the stored value is not parsable as an integer, use the default value instead
97 */
98 $filtered_value = \filter_var($value, \FILTER_VALIDATE_INT);
99 return $filtered_value !== \false ? $filtered_value : $this->default_value;
100 case 'float':
101 /**
102 * If the stored value is not parsable as an float, use the default value instead
103 */
104 $filtered_value = \filter_var($value, \FILTER_VALIDATE_FLOAT);
105 return $filtered_value !== \false ? $filtered_value : $this->default_value;
106 case 'boolean':
107 return \filter_var($value, \FILTER_VALIDATE_BOOLEAN);
108 case 'array':
109 /**
110 * If an array was saved with update_option, then get_option would return a parsed
111 * array. If get_option returns a string, that means the value wasn't a parsable
112 * array and the default value should be used.
113 */
114 return $this->get_type($value) === 'string' ? $this->default_value : $value;
115 case 'string':
116 if ($value === '' && !$this->allow_empty_string) {
117 return $this->default_value;
118 }
119 return $value;
120 }
121 return $value;
122 }
123 public function set($new_value = null)
124 {
125 if (\is_null($new_value)) {
126 $this->delete();
127 return;
128 }
129 $new_value_type = $this->get_type($new_value);
130 if ($this->type !== $new_value_type) {
131 throw new \Exception("{$this->option_name} expects type {$this->type} but {$new_value_type} was provided");
132 }
133 if ($new_value === '' && !$this->allow_empty_string) {
134 $this->delete();
135 return;
136 }
137 \update_option($this->option_name, $this->convert_to_wordpress($new_value));
138 }
139 public function get()
140 {
141 $value = \get_option($this->option_name, new \Exception());
142 if ($value instanceof \Exception) {
143 return $this->default_value;
144 }
145 return $this->convert_from_wordpress($value);
146 }
147 public function delete()
148 {
149 \delete_option($this->option_name);
150 }
151 public static function find($option_name)
152 {
153 foreach (Option::$registered_options as $option) {
154 if ($option->option_name === $option_name) {
155 return $option;
156 }
157 }
158 throw new \Exception("Option {$option_name} is not registered");
159 }
160 public static function set_option($option_name, $value = null)
161 {
162 $option = self::find($option_name);
163 $option->set($value);
164 }
165 public static function get_option($option_name)
166 {
167 $option = self::find($option_name);
168 return $option->get();
169 }
170 public static function delete_option($option_name)
171 {
172 $option = self::find($option_name);
173 return $option->delete();
174 }
175 }
176