PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.29.0
Independent Analytics – WordPress Analytics Plugin v1.29.0
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 Device.php 2 years ago Number_Formatter.php 3 years ago Option.php 3 years ago Request.php 2 years ago Salt.php 3 years ago Security.php 3 years ago Singleton.php 2 years ago String_Util.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 public const STRING = 'string';
8 public const BOOLEAN = 'boolean';
9 public const BOOL = 'boolean';
10 public const INT = 'integer';
11 public const INTEGER = 'integer';
12 public const FLOAT = 'float';
13 public const DOUBLE = 'float';
14 public const ARRAY = 'array';
15 /**
16 * @var string
17 */
18 protected $option_name;
19 private static $supported_types = ['string', 'boolean', 'integer', 'float', 'array'];
20 /**
21 * @var array
22 */
23 private static $registered_options = [];
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 public function set($new_value = null)
61 {
62 if (\is_null($new_value)) {
63 $this->delete();
64 return;
65 }
66 $new_value_type = $this->get_type($new_value);
67 if ($this->type !== $new_value_type) {
68 throw new \Exception("{$this->option_name} expects type {$this->type} but {$new_value_type} was provided");
69 }
70 if ($new_value === '' && !$this->allow_empty_string) {
71 $this->delete();
72 return;
73 }
74 \update_option($this->option_name, $this->convert_to_wordpress($new_value));
75 }
76 public function get()
77 {
78 $value = \get_option($this->option_name, new \Exception());
79 if ($value instanceof \Exception) {
80 return $this->default_value;
81 }
82 return $this->convert_from_wordpress($value);
83 }
84 public function delete()
85 {
86 \delete_option($this->option_name);
87 }
88 public static function find($option_name)
89 {
90 foreach (Option::$registered_options as $option) {
91 if ($option->option_name === $option_name) {
92 return $option;
93 }
94 }
95 throw new \Exception("Option {$option_name} is not registered");
96 }
97 public static function set_option($option_name, $value = null)
98 {
99 $option = self::find($option_name);
100 $option->set($value);
101 }
102 public static function get_option($option_name)
103 {
104 $option = self::find($option_name);
105 return $option->get();
106 }
107 public static function delete_option($option_name)
108 {
109 $option = self::find($option_name);
110 return $option->delete();
111 }
112 /**
113 * @param $value
114 *
115 * @return string
116 */
117 private function get_type($value) : string
118 {
119 $type = \gettype($value);
120 if ($type === 'double') {
121 return 'float';
122 }
123 return $type;
124 }
125 /**
126 * @param mixed $value
127 *
128 * @return mixed
129 */
130 private function convert_to_wordpress($value)
131 {
132 switch ($this->type) {
133 case 'boolean':
134 return $value === \true ? 1 : 0;
135 }
136 return $value;
137 }
138 /**
139 * @param mixed $value
140 *
141 * @return mixed
142 */
143 private function convert_from_wordpress($value)
144 {
145 switch ($this->type) {
146 case 'integer':
147 /**
148 * If the stored value is not parsable as an integer, use the default value instead
149 */
150 $filtered_value = \filter_var($value, \FILTER_VALIDATE_INT);
151 return $filtered_value !== \false ? $filtered_value : $this->default_value;
152 case 'float':
153 /**
154 * If the stored value is not parsable as an float, use the default value instead
155 */
156 $filtered_value = \filter_var($value, \FILTER_VALIDATE_FLOAT);
157 return $filtered_value !== \false ? $filtered_value : $this->default_value;
158 case 'boolean':
159 return \filter_var($value, \FILTER_VALIDATE_BOOLEAN);
160 case 'array':
161 /**
162 * If an array was saved with update_option, then get_option would return a parsed
163 * array. If get_option returns a string, that means the value wasn't a parsable
164 * array and the default value should be used.
165 */
166 return $this->get_type($value) === 'string' ? $this->default_value : $value;
167 case 'string':
168 if ($value === '' && !$this->allow_empty_string) {
169 return $this->default_value;
170 }
171 return $value;
172 }
173 return $value;
174 }
175 }
176