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.5 1.3.8.6 1.3.8.7 1.3.8.8 1.3.8.9 1.3.9.0 1.3.9.1 1.3.9.2 1.3.9.3 1.3.9.4 1.3.9.5 1.3.9.6 1.3.9.7 1.3.9.8 1.3.9.9 1.4 1.4.0.1 1.4.0.2 1.4.0.3 1.4.0.4
wp-asset-clean-up / classes / MiscArray.php
wp-asset-clean-up / classes Last commit date
Admin 2 weeks ago OptimiseAssets 2 weeks ago ThirdParty 1 year ago AdminBar.php 2 weeks ago AssetsManager.php 2 weeks ago BulkChanges.php 2 weeks ago CleanUp.php 2 months ago Debug.php 2 weeks ago DebugException.php 2 weeks ago FileSystem.php 1 year ago HardcodedAssets.php 2 weeks ago LoadExceptions.php 2 weeks ago Main.php 2 weeks ago MainFront.php 2 weeks ago Maintenance.php 2 weeks ago Menu.php 2 weeks ago MetaBoxes.php 2 weeks ago Misc.php 2 weeks ago MiscArray.php 2 weeks ago ObjectCache.php 2 months ago OwnAssets.php 2 weeks ago PluginTracking.php 2 weeks ago Preloads.php 2 weeks ago Regex.php 2 weeks ago Settings.php 2 weeks ago Tips.php 1 year ago Update.php 2 weeks ago
MiscArray.php
306 lines
1 <?php
2 /** @noinspection MultipleReturnStatementsInspection */
3
4 namespace WpAssetCleanUp;
5
6 /**
7 * Class MiscArray
8 * contains various common functions that are used by the plugin
9 * @package WpAssetCleanUp
10 */
11 class MiscArray
12 {
13 /**
14 * Check if a scalar value is considered non-empty.
15 *
16 * Valid values include:
17 * - 0
18 * - '0'
19 * - false
20 * - true
21 * - any non-empty string
22 *
23 * Invalid values:
24 * - null
25 * - ''
26 *
27 * @param mixed $value
28 *
29 * @return bool
30 */
31 public static function isNonEmptyValue($value)
32 {
33 return $value !== null && $value !== '';
34 }
35
36 /**
37 * @param $value
38 *
39 * @return bool
40 */
41 public static function hasNonEmptyValue($value)
42 {
43 if ( ! is_array($value) ) {
44 return self::isNonEmptyValue($value);
45 }
46
47 foreach ($value as $childValue) {
48 if (self::hasNonEmptyValue($childValue)) {
49 return true;
50 }
51 }
52
53 return false;
54 }
55
56 /**
57 * Check if a value is non-empty.
58 *
59 * If $path is provided, the nested value is checked.
60 *
61 * Examples:
62 * self::isNonEmpty($settings);
63 * self::isNonEmpty($settings, 'dom_get_type');
64 * self::isNonEmpty($settings, 'images.attr.data');
65 * self::isNonEmpty($settings, '[images][attr][data]');
66 * self::isNonEmpty($settings, 'images.attr.data', true); // must be array
67 *
68 * If $mustBeArray is true, the resolved value must be an array.
69 *
70 * @param mixed $value
71 * @param string|array $path
72 * @param bool $mustBeArray
73 *
74 * @return bool
75 */
76 public static function isNonEmpty($value, $path = null, $mustBeArray = false)
77 {
78 if ($path !== null) {
79 $value = self::getNestedValue($value, $path);
80 }
81
82 if ($mustBeArray && ! is_array($value)) {
83 return false;
84 }
85
86 return self::hasNonEmptyValue($value);
87 }
88
89 /**
90 * Alias of isNonEmpty().
91 *
92 * @param mixed $value
93 * @param string|array $path
94 * @param bool $mustBeArray
95 *
96 * @return bool
97 */
98 public static function isValid($value, $path = null, $mustBeArray = false)
99 {
100 return self::isNonEmpty($value, $path, $mustBeArray);
101 }
102
103 /**
104 * Opposite of isNonEmpty().
105 *
106 * @param mixed $value
107 * @param string|array $path
108 * @param bool $mustBeArray
109 *
110 * @return bool
111 */
112 public static function isEmpty($value, $path = null, $mustBeArray = false)
113 {
114 return ! self::isNonEmpty($value, $path, $mustBeArray);
115 }
116
117 /**
118 * Get an array value by key/path.
119 *
120 * Supported formats:
121 * - 'resource_loading'
122 * - '[resource_loading]'
123 * - '[resource_loading][images][attr]'
124 * - 'images.attr.data'
125 * - array('images', 'attr', 'data')
126 *
127 * @param array $array
128 * @param string|array $key
129 * @param mixed|null $default
130 *
131 * @return mixed|null
132 */
133 public static function getValue($array, $key, $default = null)
134 {
135 if ( ! is_array($array) ) {
136 return $default;
137 }
138
139 if ( is_array($key) ) {
140 return self::getNestedValue($array, $key, $default);
141 }
142
143 if ( ! is_string($key) ) {
144 return $default;
145 }
146
147 $key = trim($key);
148
149 if ($key === '') {
150 return $default;
151 }
152
153 // e.g. [resource_loading] => resource_loading
154 if (preg_match('#^\[([^\]]+)\]$#', $key, $matches)) {
155 $key = $matches[1];
156 }
157
158 // Simple top-level key.
159 if (strpos($key, '[') === false && strpos($key, '.') === false) {
160 if ( ! array_key_exists($key, $array) ) {
161 return $default;
162 }
163
164 return ($array[$key] === '') ? $default : $array[$key];
165 }
166
167 // Nested path.
168 return self::getNestedValue($array, $key, $default);
169 }
170
171 /**
172 * Get a nested array value by path.
173 *
174 * Supported path formats:
175 * - [images][attr][data]
176 * - images.attr.data
177 * - array('images', 'attr', 'data')
178 *
179 * @param array $array
180 * @param string|array $path
181 * @param mixed|null $default
182 *
183 * @return mixed|null
184 */
185 public static function getNestedValue($array, $path, $default = null)
186 {
187 if ( ! is_array($array) ) {
188 return $default;
189 }
190
191 if ( is_array($path) ) {
192 $keys = $path;
193 } else {
194 $path = trim($path);
195
196 if ($path === '') {
197 return $default;
198 }
199
200 if ( strpos($path, '[') === 0 ) {
201 preg_match_all('#\[([^\]]+)\]#', $path, $matches);
202 $keys = isset($matches[1]) ? $matches[1] : array();
203 } else {
204 $keys = array_filter(explode('.', $path), 'strlen');
205 }
206 }
207
208 if ( empty($keys) ) {
209 return $default;
210 }
211
212 foreach ($keys as $key) {
213 if ( ! is_array($array) || ! array_key_exists($key, $array) ) {
214 return $default;
215 }
216
217 $array = $array[$key];
218 }
219
220 return ($array === '') ? $default : $array;
221 }
222
223 /**
224 * @param $array
225 * @param $prefix
226 *
227 * @return bool
228 */
229 public static function hasKeyStartingWith($array, $prefix)
230 {
231 foreach ($array as $key => $value) {
232 if (strpos($key, $prefix) === 0) {
233 return true;
234 }
235 }
236
237 return false;
238 }
239
240 /**
241 * @param $list
242 * @param string $for
243 *
244 * @return array
245 */
246 public static function filterList($list, $for = 'empty_values')
247 {
248 if ( ! empty($list) && $for === 'empty_values' ) {
249 $list = self::unsetRecursive($list);
250 }
251
252 return $list;
253 }
254
255 /**
256 * Source: https://stackoverflow.com/questions/7696548/php-how-to-remove-empty-entries-of-an-array-recursively
257 *
258 * @param $array
259 *
260 * @return array
261 */
262 public static function unsetRecursive($array)
263 {
264 $array = (array)$array; // in case it's object, convert it to array
265
266 foreach ($array as $key => $value) {
267 if (is_array($value) || is_object($value)) {
268 $array[$key] = self::unsetRecursive($value);
269 }
270
271 // Values such as '0' are not considered empty values
272 if (is_string($value) && trim($value) === '0') {
273 continue;
274 }
275
276 // Clear it if it's empty
277 if (empty($array[$key])) {
278 unset($array[$key]);
279 }
280 }
281
282 return $array;
283 }
284
285 /**
286 * @param $transientName
287 * @param $value
288 * @param $expiration
289 *
290 * @return void
291 */
292 public static function addToTransient($transientName, $value, $expiration = 0)
293 {
294 $data = get_transient($transientName);
295
296 if ( ! is_array($data) ) {
297 $data = array();
298 }
299
300 $data[] = $value;
301
302 set_transient($transientName, $data, $expiration);
303 }
304
305 }
306