PluginProbe
Metricool – Social media and site statistics / 2.1.1
Metricool – Social media and site statistics v2.1.1
2.1.1 2.1.0 2.0.2 2.0.1 2.0.0 1.27 trunk
← All changes | app/Support/Helpers/Storage.php +224 -0 2.0.1 → 2.1.1 View file →
@@ -1,0 +1,224 @@
1 +<?php
2 +
3 +declare(strict_types=1);
4 +
5 +namespace Metricool\Support\Helpers;
6 +
7 +use Metricool\Vendor\Adbar\Dot;
8 +
9 +/**
10 + * Wrapper for easy access to storage data. Create a new instance with an array
11 + * of data in the constructor. Now all data can be accessed using Dot notation.
12 + *
13 + * @usage $storage = new Storage(['key' => 'value']);
14 + * @usage $storage->get('key', 'default');
15 + */
16 +class Storage extends Dot
17 +{
18 + /**
19 + * Create a new Storage instance with dot notation functionality from the
20 + * parent {@see Dot} class. This internal class is extendable but the
21 + * construct should not be overridable, it is therefor made final.
22 + */
23 + public function __construct($items = [], $parse = false, $delimiter = ".")
24 + {
25 + parent::__construct($items, $parse, $delimiter);
26 + }
27 +
28 + /**
29 + * Returns the parameter keys.
30 + */
31 + public function keys(): array
32 + {
33 + return array_keys($this->items);
34 + }
35 +
36 + /**
37 + * Returns the sanitized string of the parameter value.
38 + * @uses sanitize_text_field()
39 + */
40 + public function getString(string $key, string $default = '', bool $trim = false): string
41 + {
42 + $value = sanitize_text_field($this->get($key, $default));
43 + return $trim ? trim($value) : $value;
44 + }
45 +
46 + /**
47 + * Returns the sanitized string of the parameter value.
48 + * @uses sanitize_textarea_field()
49 + */
50 + public function getTextarea(string $key, string $default = '', bool $trim = false): string
51 + {
52 + $value = sanitize_textarea_field($this->get($key, $default));
53 + return $trim ? trim($value) : $value;
54 + }
55 +
56 + /**
57 + * Strips out all characters that are not allowable in an email and returns
58 + * the value.
59 + * @uses sanitize_email()
60 + */
61 + public function getEmail(string $key, string $default = ''): string
62 + {
63 + return sanitize_email($this->get($key, $default));
64 + }
65 +
66 + /**
67 + * Returns the parameter value as a slug.
68 + * @uses sanitize_title
69 + */
70 + public function getTitle(string $key, string $default = ''): string
71 + {
72 + return sanitize_title($this->get($key, $default));
73 + }
74 +
75 + /**
76 + * Sanitizes content for allowed HTML tags for post content.
77 + * @uses wp_kses_post()
78 + */
79 + public function getPost(string $key, string $default = ''): string
80 + {
81 + return wp_kses_post($this->get($key, $default));
82 + }
83 +
84 + /**
85 + * Returns a sanitized URL.
86 + * @uses sanitize_url()
87 + */
88 + public function getUrl(string $key, string $default = ''): string
89 + {
90 + return sanitize_url($this->get($key, $default));
91 + }
92 +
93 + /**
94 + * Keys are used as internal identifiers. Lowercase alphanumeric characters,
95 + * dashes, and underscores are allowed.
96 + * @uses sanitize_key()
97 + */
98 + public function getKey(string $key, string $default = ''): string
99 + {
100 + return sanitize_key($this->get($key, $default));
101 + }
102 +
103 + /**
104 + * Returns the alphabetic characters of the parameter value.
105 + */
106 + public function getAlpha(string $key, string $default = ''): string
107 + {
108 + return preg_replace('/[^[:alpha:]]/', '', $this->get($key, $default));
109 + }
110 +
111 + /**
112 + * Returns the alphabetic characters of the parameter value. With spaces.
113 + */
114 + public function getAlphaSpace(string $key, string $default = ''): string
115 + {
116 + return preg_replace('/[^[:alpha:] ]/', '', $this->get($key, $default));
117 + }
118 +
119 + /**
120 + * Returns the alphabetic characters and digits of the parameter value.
121 + */
122 + public function getAlnum(string $key, string $default = ''): string
123 + {
124 + return preg_replace('/[^[:alnum:]]/', '', $this->get($key, $default));
125 + }
126 +
127 + /**
128 + * Returns the digits of the parameter value.
129 + *
130 + * @param string $default The default value runs through
131 + * FILTER_SANITIZE_NUMBER_INT as well
132 + */
133 + public function getDigits(string $key, string $default = ''): string
134 + {
135 + // we need to remove - and + because they're still allowed by the filter
136 + return str_replace(['-', '+'], '', $this->filter($key, $default, FILTER_SANITIZE_NUMBER_INT));
137 + }
138 +
139 + /**
140 + * Returns the parameter value typecast as integer.
141 + */
142 + public function getInt(string $key, int $default = 0): int
143 + {
144 + return (int) $this->get($key, $default);
145 + }
146 +
147 + /**
148 + * Returns the parameter value typecast as float.
149 + */
150 + public function getFloat(string $key, float $default = 0): float
151 + {
152 + return (float) $this->get($key, $default);
153 + }
154 +
155 + /**
156 + * Returns the parameter value filtered as a boolean. Uses flag:
157 + * FILTER_VALIDATE_BOOLEAN
158 + */
159 + public function getBoolean(string $key, bool $default = false): bool
160 + {
161 + return $this->filter($key, $default, FILTER_VALIDATE_BOOLEAN);
162 + }
163 +
164 + /**
165 + * Returns the parameter value validated as a 2 character country code. If
166 + * the preg_match for exactly two alphabetic characters fails, the default
167 + * value is returned.
168 + */
169 + public function getCountryCode(string $key, string $default = ''): string
170 + {
171 + $country = strtoupper(trim($this->get($key, $default)));
172 + if (preg_match('/^[a-z]{2}$/i', $country)) {
173 + return $country;
174 + }
175 +
176 + return $default;
177 + }
178 +
179 + /**
180 + * Returns a boolean if the value is considered not empty.
181 + * @param array<array-key>|int|string|null $keys
182 + */
183 + public function isNotEmpty($keys = null): bool
184 + {
185 + return $this->isEmpty($keys) === false;
186 + }
187 +
188 + /**
189 + * Returns a boolean if the value of one of the keys is considered empty.
190 + * @param array<int,array-key> $keys
191 + */
192 + public function isOneEmpty(array $keys = []): bool
193 + {
194 + foreach ($keys as $key) {
195 + if ($this->isEmpty($key)) {
196 + return true;
197 + }
198 + }
199 +
200 + return false;
201 + }
202 +
203 + /**
204 + * Filter key.
205 + * @param mixed $default
206 + * @param array|int $options
207 + * @return mixed
208 + * @see http://php.net/manual/en/function.filter-var.php
209 + */
210 + public function filter(string $key, $default = null, int $filter = FILTER_DEFAULT, $options = [])
211 + {
212 + $value = $this->get($key, $default);
213 + // Always turn $options into an array - this allows filter_var option shortcuts.
214 + if (!\is_array($options) && $options) {
215 + $options = ['flags' => $options];
216 + }
217 + // Add a convenience check for arrays.
218 + if (\is_array($value) && !isset($options['flags'])) {
219 + $options['flags'] = FILTER_REQUIRE_ARRAY;
220 + }
221 +
222 + return filter_var($value, $filter, $options);
223 + }
224 +}