PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 4.4.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v4.4.1
6.2.2 6.2.1 6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 All 155 releases
← All changes | class/class-mainwp-utility.php +1031 -1598 5.2.2 → 4.4.1 View file →
@@ -13,1700 +13,1133 @@
13 13 * Class MainWP_Utility
14 14 *
15 15 * @package MainWP\Dashboard
16 16 */
17 -class MainWP_Utility { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
17 +class MainWP_Utility {
18 18
19 - /**
20 - * Yoast SEO is enabled return true else return null.
21 - *
22 - * @static
23 - * @var boolean $enabled_wp_seo If Yoast SEO is enabled return true else return null.
24 - */
25 - public static $enabled_wp_seo = null;
19 + /**
20 + * Yoast SEO is enabled return true else return null.
21 + *
22 + * @static
23 + * @var boolean $enabled_wp_seo If Yoast SEO is enabled return true else return null.
24 + */
25 + public static $enabled_wp_seo = null;
26 26
27 - /**
28 - * Private static variable.
29 - *
30 - * @static
31 - *
32 - * @var mixed Default null
33 - */
34 - public static $last_deactivated_alerts = null;
27 + /**
28 + * Method get_class_name()
29 + *
30 + * Get Class Name.
31 + *
32 + * @return object __CLASS__
33 + */
34 + public static function get_class_name() {
35 + return __CLASS__;
36 + }
35 37
36 - /**
37 - * Private static variable to hold the single instance of the class.
38 - *
39 - * @static
40 - *
41 - * @var mixed Default null
42 - */
43 - private static $instance = null;
38 + /**
39 + * Method starts_with()
40 + *
41 + * Start of Stack Trace.
42 + *
43 + * @param mixed $haystack The full stack.
44 + * @param mixed $needle The function that is throwing the error.
45 + *
46 + * @return mixed Needle in the Haystack.
47 + */
48 + public static function starts_with( $haystack, $needle ) {
49 + return ! strncmp( $haystack, $needle, strlen( $needle ) );
50 + }
44 51
45 - /**
46 - * Store the disabled php functions.
47 - *
48 - * @static
49 - * @var string $disabled_functions disabled php functions.
50 - */
51 - public static $disabled_functions = null;
52 + /**
53 + * Method ends_with()
54 + *
55 + * End of Stack Trace.
56 + *
57 + * @param mixed $haystack Haystack parameter.
58 + * @param mixed $needle Needle parameter.
59 + *
60 + * @return boolean
61 + */
62 + public static function ends_with( $haystack, $needle ) {
63 + $length = strlen( $needle );
64 + if ( 0 === $length ) {
65 + return true;
66 + }
52 67
53 - /**
54 - * Method get_class_name()
55 - *
56 - * Get Class Name.
57 - *
58 - * @return object __CLASS__
59 - */
60 - public static function get_class_name() {
61 - return __CLASS__;
62 - }
68 + return ( substr( $haystack, - $length ) === $needle );
69 + }
63 70
64 - /**
65 - * Method instance()
66 - *
67 - * Create public static instance.
68 - *
69 - * @static
70 - * @return MainWP_Utility
71 - */
72 - public static function instance() {
73 - if ( null === static::$instance ) {
74 - static::$instance = new self();
75 - }
71 + /**
72 + * Method get_nice_url()
73 + *
74 + * Grab url.
75 + *
76 + * @param string $pUrl Website URL.
77 + * @param bool $showHttp Show HTTP.
78 + *
79 + * @return string $url.
80 + */
81 + public static function get_nice_url( $pUrl, $showHttp = false ) {
82 + $url = $pUrl;
76 83
77 - return static::$instance;
78 - }
84 + if ( self::starts_with( $url, 'http://' ) ) {
85 + if ( ! $showHttp ) {
86 + $url = substr( $url, 7 );
87 + }
88 + } elseif ( self::starts_with( $pUrl, 'https://' ) ) {
89 + if ( ! $showHttp ) {
90 + $url = substr( $url, 8 );
91 + }
92 + } else {
93 + if ( $showHttp ) {
94 + $url = 'http://' . $url;
95 + }
96 + }
79 97
80 - /**
81 - * Method starts_with()
82 - *
83 - * Start of Stack Trace.
84 - *
85 - * @param mixed $haystack The full stack.
86 - * @param mixed $needle The function that is throwing the error.
87 - *
88 - * @return mixed Needle in the Haystack.
89 - */
90 - public static function starts_with( $haystack, $needle ) {
91 - return ! strncmp( $haystack, $needle, strlen( $needle ) );
92 - }
98 + if ( self::ends_with( $url, '/' ) ) {
99 + if ( ! $showHttp ) {
100 + $url = substr( $url, 0, strlen( $url ) - 1 );
101 + }
102 + } else {
103 + $url = $url . '/';
104 + }
93 105
94 - /**
95 - * Method ends_with()
96 - *
97 - * End of Stack Trace.
98 - *
99 - * @param mixed $haystack Haystack parameter.
100 - * @param mixed $needle Needle parameter.
101 - *
102 - * @return boolean
103 - */
104 - public static function ends_with( $haystack, $needle ) {
105 - $length = strlen( $needle );
106 - if ( 0 === $length ) {
107 - return true;
108 - }
106 + return $url;
107 + }
109 108
110 - return substr( $haystack, - $length ) === $needle;
111 - }
109 + /**
110 + * Method is_domain_valid()
111 + *
112 + * Check $url against FILTER_VALIDATE_URL.
113 + *
114 + * @param mixed $url Domain to check.
115 + *
116 + * @return boolean True|False.
117 + */
118 + public static function is_domain_valid( $url ) {
119 + return filter_var( $url, FILTER_VALIDATE_URL );
120 + }
112 121
113 - /**
114 - * Method get_nice_url()
115 - *
116 - * Grab url.
117 - *
118 - * @param string $pUrl Website URL.
119 - * @param bool $showHttp Show HTTP.
120 - *
121 - * @return string $url.
122 - */
123 - public static function get_nice_url( $pUrl, $showHttp = false ) {
124 - $url = $pUrl;
122 + /**
123 + * Method ctype_digit()
124 + *
125 + * Returns TRUE if every character in the string text is a decimal digit, FALSE otherwise.
126 + *
127 + * @param mixed $str String to check.
128 + *
129 + * @return boolean Returns TRUE if every character in the string text is a decimal digit, FALSE otherwise.
130 + */
131 + public static function ctype_digit( $str ) {
132 + return ( is_string( $str ) || is_int( $str ) || is_float( $str ) ) && preg_match( '/^\d+\z/', $str );
133 + }
125 134
126 - if ( static::starts_with( $url, 'http://' ) ) {
127 - if ( ! $showHttp ) {
128 - $url = substr( $url, 7 );
129 - }
130 - } elseif ( static::starts_with( $pUrl, 'https://' ) ) {
131 - if ( ! $showHttp ) {
132 - $url = substr( $url, 8 );
133 - }
134 - } elseif ( $showHttp ) {
135 - $url = 'http://' . $url;
136 - }
135 + /**
136 + * Method sortmulti()
137 + *
138 + * Sort the given array, Acending, Decending or by Natural Order.
139 + *
140 + * @param mixed $array Array to sort.
141 + * @param mixed $index Index of array.
142 + * @param mixed $order Acending or Decending order.
143 + * @param bool $natsort Sort an array using a "natural order" algorithm. Default: false.
144 + * @param bool $case_sensitive If case sensitive return true else return false. Default: false.
145 + *
146 + * @return array $sorted Return the sorted array.
147 + */
148 + public static function sortmulti( $array, $index, $order, $natsort = false, $case_sensitive = false ) {
149 + $sorted = array();
150 + if ( is_array( $array ) && 0 < count( $array ) ) {
151 + foreach ( array_keys( $array ) as $key ) {
152 + $temp[ $key ] = $array[ $key ][ $index ];
153 + }
154 + if ( ! $natsort ) {
155 + if ( 'asc' === $order ) {
156 + asort( $temp );
157 + } else {
158 + arsort( $temp );
159 + }
160 + } else {
161 + if ( true === $case_sensitive ) {
162 + natsort( $temp );
163 + } else {
164 + natcasesort( $temp );
165 + }
166 + if ( 'asc' !== $order ) {
167 + $temp = array_reverse( $temp, true );
168 + }
169 + }
170 + foreach ( array_keys( $temp ) as $key ) {
171 + if ( is_numeric( $key ) ) {
172 + $sorted[] = $array[ $key ];
173 + } else {
174 + $sorted[ $key ] = $array[ $key ];
175 + }
176 + }
137 177
138 - if ( static::ends_with( $url, '/' ) ) {
139 - if ( ! $showHttp ) {
140 - $url = substr( $url, 0, strlen( $url ) - 1 );
141 - }
142 - } else {
143 - $url = $url . '/';
144 - }
178 + return $sorted;
179 + }
145 180
146 - return $url;
147 - }
181 + return $sorted;
182 + }
148 183
149 - /**
150 - * Method is_domain_valid()
151 - *
152 - * Check $url against FILTER_VALIDATE_URL.
153 - *
154 - * @param mixed $url Domain to check.
155 - *
156 - * @return boolean True|False.
157 - */
158 - public static function is_domain_valid( $url ) {
159 - return filter_var( $url, FILTER_VALIDATE_URL );
160 - }
184 + /**
185 + * Method get_sub_array_having()
186 + *
187 + * Get sub array.
188 + *
189 + * @param mixed $array Array to traverse.
190 + * @param mixed $index Index of array.
191 + * @param mixed $value Array values.
192 + *
193 + * void array $output Sub array.
194 + */
195 + public static function get_sub_array_having( $array, $index, $value ) {
196 + $output = array();
197 + if ( is_array( $array ) && 0 < count( $array ) ) {
198 + foreach ( $array as $arrvalue ) {
199 + if ( $arrvalue[ $index ] == $value ) {
200 + $output[] = $arrvalue;
201 + }
202 + }
203 + }
161 204
162 - /**
163 - * Method ctype_digit()
164 - *
165 - * Returns TRUE if every character in the string text is a decimal digit, FALSE otherwise.
166 - *
167 - * @param mixed $str String to check.
168 - *
169 - * @return boolean Returns TRUE if every character in the string text is a decimal digit, FALSE otherwise.
170 - */
171 - public static function ctype_digit( $str ) {
172 - return ( is_string( $str ) || is_int( $str ) || is_float( $str ) ) && preg_match( '/^\d+\z/', $str );
173 - }
205 + return $output;
206 + }
174 207
175 - /**
176 - * Method sortmulti()
177 - *
178 - * Sort the given array, Acending, Decending or by Natural Order.
179 - *
180 - * @param mixed $arr Array to sort.
181 - * @param mixed $index Index of array.
182 - * @param mixed $order Acending or Decending order.
183 - * @param bool $natsort Sort an array using a "natural order" algorithm. Default: false.
184 - * @param bool $case_sensitive If case sensitive return true else return false. Default: false.
185 - *
186 - * @return array $sorted Return the sorted array.
187 - */
188 - public static function sortmulti( $arr, $index, $order, $natsort = false, $case_sensitive = false ) { // phpcs:ignore -- NOSONAR - complex.
189 - $sorted = array();
190 - if ( is_array( $arr ) && ! empty( $arr ) ) {
191 - foreach ( array_keys( $arr ) as $key ) {
192 - $temp[ $key ] = $arr[ $key ][ $index ];
193 - }
194 - if ( ! $natsort ) {
195 - if ( 'asc' === $order ) {
196 - asort( $temp );
197 - } else {
198 - arsort( $temp );
199 - }
200 - } else {
201 - if ( true === $case_sensitive ) {
202 - natsort( $temp );
203 - } else {
204 - natcasesort( $temp );
205 - }
206 - if ( 'asc' !== $order ) {
207 - $temp = array_reverse( $temp, true );
208 - }
209 - }
210 - foreach ( array_keys( $temp ) as $key ) {
211 - if ( is_numeric( $key ) ) {
212 - $sorted[] = $arr[ $key ];
213 - } else {
214 - $sorted[ $key ] = $arr[ $key ];
215 - }
216 - }
208 + /**
209 + * Method trim_slashes()
210 + *
211 + * Trim stashes from element.
212 + *
213 + * @param mixed $elem Element to trim.
214 + *
215 + * @return string Return string with no slashes.
216 + */
217 + public static function trim_slashes( $elem ) {
218 + return trim( $elem, '/' );
219 + }
217 220
218 - return $sorted;
219 - }
221 + /**
222 + * Method sanitize()
223 + *
224 + * Sanitize given string.
225 + *
226 + * @param mixed $str String to sanitize.
227 + *
228 + * @return string Sanitized string.
229 + */
230 + public static function sanitize( $str ) {
231 + return preg_replace( '/[\\\\\/\:"\*\?\<\>\|]+/', '', $str );
232 + }
220 233
221 - return $sorted;
222 - }
234 + /**
235 + * Method end_session()
236 + *
237 + * End a session.
238 + *
239 + * @return void
240 + */
241 + public static function end_session() {
223 242
224 - /**
225 - * Method get_sub_array_having()
226 - *
227 - * Get sub array.
228 - *
229 - * @param mixed $arr Array to traverse.
230 - * @param mixed $index Index of array.
231 - * @param mixed $value Array values.
232 - *
233 - * void array $output Sub array.
234 - */
235 - public static function get_sub_array_having( $arr, $index, $value ) {
236 - $output = array();
237 - if ( is_array( $arr ) && ! empty( $arr ) ) {
238 - foreach ( $arr as $arrvalue ) {
239 - $existed = isset( $arrvalue[ $index ] ) ? $arrvalue[ $index ] : null;
240 - if ( $existed === $value ) {
241 - $output[] = $arrvalue;
242 - }
243 - }
244 - }
243 + if ( defined( 'WP_CLI' ) && WP_CLI ) {
244 + return;
245 + }
245 246
246 - return $output;
247 - }
247 + if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
248 + return;
249 + }
248 250
249 - /**
250 - * Method get_sub_array_with_limit()
251 - *
252 - * Get sub array.
253 - *
254 - * @param mixed $arr Array to traverse.
255 - * @param mixed $start start index of array.
256 - * @param mixed $count count values.
257 - *
258 - * void array $output Sub array.
259 - */
260 - public static function get_sub_array_with_limit( $arr, $start, $count ) {
261 - $output = array();
262 - if ( is_array( $arr ) && ! empty( $arr ) ) {
263 - if ( $start > count( $arr ) ) {
264 - return array();
265 - }
266 - $i = 0;
267 - foreach ( $arr as $value ) {
268 - if ( $i >= $start && $i < $start + $count ) {
269 - $output[] = $value;
270 - }
271 - ++$i;
272 - if ( $i > $start + $count ) {
273 - break;
274 - }
275 - }
276 - }
277 - return $output;
278 - }
251 + session_write_close();
252 + if ( 0 < ob_get_length() ) {
253 + ob_end_flush();
254 + }
255 + }
279 256
257 + /**
258 + * Method get_timestamp()
259 + *
260 + * Get time stamp in gmt_offset.
261 + *
262 + * @param mixed $timestamp Time stamp to convert.
263 + *
264 + * @return string Time stamp in general mountain time offset.
265 + */
266 + public static function get_timestamp( $timestamp = false ) {
267 + if ( false === $timestamp ) {
268 + $timestamp = time();
269 + }
270 + $gmtOffset = get_option( 'gmt_offset' );
280 271
281 - /**
282 - * Method trim_slashes()
283 - *
284 - * Trim stashes from element.
285 - *
286 - * @param mixed $elem Element to trim.
287 - *
288 - * @return string Return string with no slashes.
289 - */
290 - public static function trim_slashes( $elem ) {
291 - return trim( $elem, '/' );
292 - }
272 + return ( $gmtOffset ? ( $gmtOffset * HOUR_IN_SECONDS ) + $timestamp : $timestamp );
273 + }
293 274
294 - /**
295 - * Method sanitize()
296 - *
297 - * Sanitize given string.
298 - *
299 - * @param mixed $str String to sanitize.
300 - *
301 - * @return string Sanitized string.
302 - */
303 - public static function sanitize( $str ) {
304 - return preg_replace( '/[\\\\\/\:"\*\?\<\>\|]+/', '', $str );
305 - }
275 + /**
276 + * Method date()
277 + *
278 + * Show date in given format.
279 + *
280 + * @param mixed $format Format to display date in.
281 + *
282 + * @return string Date.
283 + */
284 + public static function date( $format ) {
285 + // phpcs:ignore -- use local date function.
286 + return date( $format, self::get_timestamp() );
287 + }
306 288
307 - /**
308 - * Method sanitize_alphanumeric()
309 - *
310 - * Sanitize given string.
311 - *
312 - * @param mixed $str String to sanitize.
313 - *
314 - * @return string Sanitized string.
315 - */
316 - public static function sanitize_attr_slug( $str ) {
317 - $str = strtolower( $str );
318 - $str = str_replace( array( '=', '?', '/' ), '-', $str );
319 - $str = preg_replace( '/[^A-Za-z0-9^\-]/', '', $str );
320 - return $str;
321 - }
289 + /**
290 + * Method format_timestamp()
291 + *
292 + * Format the given timestamp.
293 + *
294 + * @param mixed $timestamp Timestamp to format.
295 + *
296 + * @return string Formatted timestamp.
297 + */
298 + public static function format_timestamp( $timestamp ) {
299 + return date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $timestamp );
300 + }
322 301
323 - /**
324 - * Method end_session()
325 - *
326 - * End a session.
327 - *
328 - * @return void
329 - */
330 - public static function end_session() {
302 + /**
303 + * Method human_filesize()
304 + *
305 + * Convert to human readable file size format,
306 + * (B|kB|MB|GB|TB|PB|EB|ZB|YB).
307 + *
308 + * @param mixed $bytes File in bytes.
309 + * @param integer $decimals Number of decimals to output.
310 + *
311 + * @return string Human readable file size.
312 + */
313 + public static function human_filesize( $bytes, $decimals = 2 ) {
314 + $size = array( 'B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' );
315 + $factor = floor( ( strlen( $bytes ) - 1 ) / 3 );
331 316
332 - if ( defined( 'WP_CLI' ) && WP_CLI ) {
333 - return;
334 - }
317 + return sprintf( "%.{$decimals}f", $bytes / pow( 1024, $factor ) ) . @$size[ $factor ];
318 + }
335 319
336 - if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
337 - return;
338 - }
320 + /**
321 + * Method map_fields()
322 + *
323 + * Map Site.
324 + *
325 + * @param mixed $data data to map.
326 + * @param mixed $keys Keys to map.
327 + * @param bool $object_output Output format array|object.
328 + *
329 + * @return mixed Mapped data.
330 + */
331 + public static function map_fields( &$data, $keys, $object_output = true ) {
332 + return self::map_site( $data, $keys, $object_output );
333 + }
339 334
340 - session_write_close();
341 - if ( 0 < ob_get_length() ) {
342 - ob_end_flush();
343 - }
344 - }
335 + /**
336 + * Method map_site()
337 + *
338 + * Map Site.
339 + *
340 + * @param mixed $website Website to map.
341 + * @param mixed $keys Keys to map.
342 + * @param bool $object_output Output format array|object.
343 + *
344 + * @return mixed $outputSite Mapped site.
345 + */
346 + public static function map_site( &$website, $keys, $object_output = true ) {
347 + $outputSite = array();
348 + if ( ! empty( $website ) ) {
349 + if ( is_object( $website ) ) {
350 + foreach ( $keys as $key ) {
351 + $outputSite[ $key ] = $website->$key;
352 + }
353 + } elseif ( is_array( $website ) ) {
354 + foreach ( $keys as $key ) {
355 + $outputSite[ $key ] = $website[ $key ];
356 + }
357 + }
358 + }
345 359
346 - /**
347 - * Method get_timestamp()
348 - *
349 - * Get time stamp in gmt_offset.
350 - *
351 - * @param mixed $timestamp Time stamp to convert.
352 - *
353 - * @return string Time stamp in general mountain time offset.
354 - */
355 - public static function get_timestamp( $timestamp = false ) {
356 - if ( false === $timestamp ) {
357 - $timestamp = time();
358 - }
359 - $gmtOffset = get_option( 'gmt_offset' );
360 + if ( $object_output ) {
361 + return (object) $outputSite;
362 + } else {
363 + return $outputSite;
364 + }
365 + }
360 366
361 - return $gmtOffset ? ( $gmtOffset * HOUR_IN_SECONDS ) + $timestamp : $timestamp;
362 - }
367 + /**
368 + * Method array_merge()
369 + *
370 + * Merge two given arrays into one.
371 + *
372 + * @param mixed $arr1 First array.
373 + * @param mixed $arr2 Second array.
374 + *
375 + * @return array Merged Array.
376 + */
377 + public static function array_merge( $arr1, $arr2 ) {
378 + if ( ! is_array( $arr1 ) && ! is_array( $arr2 ) ) {
379 + return array();
380 + }
381 + if ( ! is_array( $arr1 ) ) {
382 + return $arr2;
383 + }
384 + if ( ! is_array( $arr2 ) ) {
385 + return $arr1;
386 + }
363 387
364 - /**
365 - * Method date()
366 - *
367 - * Show date in given format.
368 - *
369 - * @param mixed $format Format to display date in.
370 - *
371 - * @return string Date.
372 - */
373 - public static function date( $format ) {
374 - // phpcs:ignore -- use local date function.
375 - return date( $format, static::get_timestamp() );
376 - }
388 + $output = array();
389 + foreach ( $arr1 as $el ) {
390 + $output[] = $el;
391 + }
392 + foreach ( $arr2 as $el ) {
393 + $output[] = $el;
394 + }
377 395
378 - /**
379 - * Method format_timestamp()
380 - *
381 - * Format the given timestamp.
382 - *
383 - * @param mixed $timestamp Timestamp to format.
384 - *
385 - * @return string Formatted timestamp.
386 - */
387 - public static function format_timestamp( $timestamp ) {
388 - return date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $timestamp );
389 - }
396 + return $output;
397 + }
390 398
391 - /**
392 - * Method format_timestamp()
393 - *
394 - * Format the given timestamp.
395 - *
396 - * @param mixed $timestamp Timestamp to format.
397 - *
398 - * @return string Formatted timestamp.
399 - */
400 - public static function format_date( $timestamp ) {
401 - return date_i18n( get_option( 'date_format' ), $timestamp );
402 - }
399 + /**
400 + * Method update_option()
401 + *
402 + * Update option.
403 + *
404 + * @param mixed $option_name Option name.
405 + * @param mixed $option_value Option value.
406 + *
407 + * @return (boolean) False if value was not updated and true if value was updated.
408 + */
409 + public static function update_option( $option_name, $option_value ) {
410 + $success = add_option( $option_name, $option_value, '', 'no' );
403 411
404 - /**
405 - * Format duration time to show.
406 - *
407 - * @param float $time timestamp.
408 - * @return mixed result.
409 - */
410 - public static function format_duration_time( $time ) {
412 + if ( ! $success ) {
413 + $success = update_option( $option_name, $option_value );
414 + }
411 415
412 - $original_sec = absint( $time );
413 - $dura_sec = $original_sec;
414 - $days = floor( $dura_sec / 86400 );
415 - $dura_sec -= $days * 86400;
416 - $dura_hour_sec = $dura_sec;
417 - $dura_hours = floor( $dura_sec / 3600 );
416 + return $success;
417 + }
418 418
419 - if ( $days > 0 ) {
420 - $formatted_dura = ( $days * 24 + $dura_hours ) . gmdate( 'i\m s\s', $dura_hour_sec );
421 - } else {
422 - $formatted_dura = gmdate( 'H\h i\m s\s', $original_sec );
423 - }
424 - return '<bdi>' . esc_html( $formatted_dura ) . '</bdi>';
425 - }
419 + /**
420 + * Method update_user_option()
421 + *
422 + * Update option.
423 + *
424 + * @param mixed $option_name Option name.
425 + * @param mixed $option_value Option value.
426 + *
427 + * @return (boolean) False if value was not updated and true if value was updated.
428 + */
429 + public static function update_user_option( $option_name, $option_value ) {
430 + $user = wp_get_current_user();
431 + if ( $user ) {
432 + return update_user_option( $user->ID, $option_name, $option_value );
433 + }
434 + return false;
435 + }
426 436
437 + /**
438 + * Method remove_preslash_spaces()
439 + *
440 + * Remove spaces before slashes.
441 + *
442 + * @param string $text String to strip.
443 + *
444 + * @return string $text Cleaned string.
445 + */
446 + public static function remove_preslash_spaces( $text ) {
447 + while ( stristr( $text, ' /' ) ) {
448 + $text = str_replace( ' /', '/', $text );
449 + }
427 450
428 - /**
429 - * Method human_filesize()
430 - *
431 - * Convert to human readable file size format,
432 - * (B|kB|MB|GB|TB|PB|EB|ZB|YB).
433 - *
434 - * @param mixed $bytes File in bytes.
435 - * @param integer $decimals Number of decimals to output.
436 - *
437 - * @return string Human readable file size.
438 - */
439 - public static function human_filesize( $bytes, $decimals = 2 ) {
440 - $size = array( 'B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' );
441 - $factor = floor( ( strlen( $bytes ) - 1 ) / 3 );
451 + return $text;
452 + }
442 453
443 - return sprintf( "%.{$decimals}f", $bytes / pow( 1024, $factor ) ) . @$size[ $factor ];
444 - }
454 + /**
455 + * Method remove_http_prefix()
456 + *
457 + * Remove http prefixes from given url.
458 + *
459 + * @param mixed $pUrl Given URL.
460 + * @param bool $pTrimSlashes Whether or not to trim slashes. Default is false.
461 + *
462 + * @return string Trimmed URL.
463 + */
464 + public static function remove_http_prefix( $pUrl, $pTrimSlashes = false ) {
465 + return str_replace( array( 'http:' . ( $pTrimSlashes ? '//' : '' ), 'https:' . ( $pTrimSlashes ? '//' : '' ) ), array( '', '' ), $pUrl );
466 + }
445 467
446 - /**
447 - * Method map_fields()
448 - *
449 - * Map Site.
450 - *
451 - * @param mixed $data data to map.
452 - * @param mixed $keys Keys to map.
453 - * @param bool $object_output Output format array|object.
454 - *
455 - * @return mixed Mapped data.
456 - */
457 - public static function map_fields( &$data, $keys, $object_output = true ) {
458 - return static::map_site( $data, $keys, $object_output );
459 - }
468 + /**
469 + * Method remove_http_www_prefix()
470 + *
471 + * Remove 'www.' from given URL.
472 + *
473 + * @param mixed $pUrl Given URL.
474 + *
475 + * @return string Cleaned URL.
476 + */
477 + public static function remove_http_www_prefix( $pUrl ) {
478 + $pUrl = self::remove_http_prefix( $pUrl, true );
479 + return str_replace( 'www.', '', $pUrl );
480 + }
460 481
461 - /**
462 - * Method map_site()
463 - *
464 - * Map Site.
465 - *
466 - * @param mixed $website Website to map.
467 - * @param mixed $keys Keys to map.
468 - * @param bool $object_output Output format array|object.
469 - *
470 - * @return mixed $outputSite Mapped site.
471 - */
472 - public static function map_site( &$website, $keys, $object_output = true ) { // phpcs:ignore -- NOSONAR - complex.
473 - if ( $object_output ) {
474 - $outputSite = new \stdClass();
475 - if ( ! empty( $website ) ) {
476 - if ( is_object( $website ) ) {
477 - foreach ( $keys as $key ) {
478 - if ( property_exists( $website, $key ) ) {
479 - $outputSite->{$key} = $website->$key;
480 - } else {
481 - $outputSite->{$key} = '';
482 - }
483 - }
484 - } elseif ( is_array( $website ) ) {
485 - foreach ( $keys as $key ) {
486 - if ( isset( $website[ $key ] ) ) {
487 - $outputSite->{$key} = $website[ $key ];
488 - } else {
489 - $outputSite->{$key} = '';
490 - }
491 - }
492 - }
493 - }
494 - } else {
495 - $outputSite = array();
496 - if ( ! empty( $website ) ) {
497 - if ( is_object( $website ) ) {
498 - foreach ( $keys as $key ) {
499 - if ( property_exists( $website, $key ) ) {
500 - $outputSite[ $key ] = $website->$key;
501 - } else {
502 - $outputSite[ $key ] = '';
503 - }
504 - }
505 - } elseif ( is_array( $website ) ) {
506 - foreach ( $keys as $key ) {
507 - if ( isset( $website[ $key ] ) ) {
508 - $outputSite[ $key ] = $website[ $key ];
509 - } else {
510 - $outputSite[ $key ] = '';
511 - }
512 - }
513 - }
514 - }
515 - }
516 - return $outputSite;
517 - }
482 + /**
483 + * Method sanitize_file_name()
484 + *
485 + * Sanitize file names.
486 + *
487 + * @param mixed $filename File name to sanitize.
488 + *
489 + * @return string Sanitized filename.
490 + */
491 + public static function sanitize_file_name( $filename ) {
492 + $filename = str_replace( array( '|', '/', '\\', ' ', ':' ), array( '-', '-', '-', '-', '-' ), $filename );
518 493
519 - /**
520 - * Method array_merge()
521 - *
522 - * Merge two given arrays into one.
523 - *
524 - * @param mixed $arr1 First array.
525 - * @param mixed $arr2 Second array.
526 - *
527 - * @return array Merged Array.
528 - */
529 - public static function array_merge( $arr1, $arr2 ) {
530 - if ( ! is_array( $arr1 ) && ! is_array( $arr2 ) ) {
531 - return array();
532 - }
533 - if ( ! is_array( $arr1 ) ) {
534 - return $arr2;
535 - }
536 - if ( ! is_array( $arr2 ) ) {
537 - return $arr1;
538 - }
494 + return sanitize_file_name( $filename );
495 + }
539 496
540 - $output = array();
541 - foreach ( $arr1 as $el ) {
542 - $output[] = $el;
543 - }
544 - foreach ( $arr2 as $el ) {
545 - $output[] = $el;
546 - }
497 + /**
498 + * Method normalize_filename()
499 + *
500 + * Normalize filename.
501 + *
502 + * @param mixed $s Filename to normalize.
503 + *
504 + * @return string $s Normalised filename.
505 + */
506 + public static function normalize_filename( $s ) {
507 + $s = preg_replace( '@\x{00c4}@u', 'A', $s );
508 + $s = preg_replace( '@\x{00d6}@u', 'O', $s );
509 + $s = preg_replace( '@\x{00dc}@u', 'U', $s );
510 + $s = preg_replace( '@\x{00cb}@u', 'E', $s );
511 + $s = preg_replace( '@\x{00e4}@u', 'a', $s );
512 + $s = preg_replace( '@\x{00f6}@u', 'o', $s );
513 + $s = preg_replace( '@\x{00fc}@u', 'u', $s );
514 + $s = preg_replace( '@\x{00eb}@u', 'e', $s );
515 + $s = preg_replace( '@\x{00f1}@u', 'n', $s );
516 + $s = preg_replace( '@\x{00ff}@u', 'y', $s );
517 + return $s;
518 + }
547 519
548 - return $output;
549 - }
550 520
551 - /**
552 - * Method update_option()
553 - *
554 - * Update option.
555 - *
556 - * @param mixed $option_name Option name.
557 - * @param mixed $option_value Option value.
558 - *
559 - * @return (boolean) False if value was not updated and true if value was updated.
560 - */
561 - public static function update_option( $option_name, $option_value ) {
562 - $success = add_option( $option_name, $option_value, '', 'no' );
521 + /**
522 + * Method esc_content()
523 + *
524 + * Escape content,
525 + * allowed content (a,href,title,br,em,strong,p,hr,ul,ol,li,h1,h2 ... ).
526 + *
527 + * @param mixed $content Content to escape.
528 + * @param string $type Type of content. Default = note.
529 + * @param mixed $more_allowed input allowed tags - options.
530 + *
531 + * @return string Filtered content containing only the allowed HTML.
532 + */
533 + public static function esc_content( $content, $type = 'note', $more_allowed = array() ) {
534 + if ( ! is_string( $content ) ) {
535 + return $content;
536 + }
563 537
564 - if ( ! $success ) {
565 - $success = update_option( $option_name, $option_value );
566 - }
538 + if ( 'note' === $type ) {
567 539
568 - return $success;
569 - }
540 + $allowed_html = array(
541 + 'a' => array(
542 + 'href' => array(),
543 + 'title' => array(),
544 + ),
545 + 'br' => array(),
546 + 'em' => array(),
547 + 'strong' => array(),
548 + 'p' => array(),
549 + 'hr' => array(),
550 + 'ul' => array(),
551 + 'ol' => array(),
552 + 'li' => array(),
553 + 'h1' => array(),
554 + 'h2' => array(),
555 + );
570 556
571 - /**
572 - * Method update_user_option()
573 - *
574 - * Update option.
575 - *
576 - * @param mixed $option_name Option name.
577 - * @param mixed $option_value Option value.
578 - *
579 - * @return (boolean) False if value was not updated and true if value was updated.
580 - */
581 - public static function update_user_option( $option_name, $option_value ) {
582 - $user = wp_get_current_user();
583 - if ( $user ) {
584 - return update_user_option( $user->ID, $option_name, $option_value );
585 - }
586 - return false;
587 - }
557 + if ( is_array( $more_allowed ) && ! empty( $more_allowed ) ) {
558 + $allowed_html = array_merge( $allowed_html, $more_allowed );
559 + }
588 560
589 - /**
590 - * Method remove_preslash_spaces()
591 - *
592 - * Remove spaces before slashes.
593 - *
594 - * @param string $text String to strip.
595 - *
596 - * @return string $text Cleaned string.
597 - */
598 - public static function remove_preslash_spaces( $text ) {
599 - while ( stristr( $text, ' /' ) ) {
600 - $text = str_replace( ' /', '/', $text );
601 - }
561 + $content = wp_kses( $content, $allowed_html );
602 562
603 - return $text;
604 - }
563 + } elseif ( 'mixed' == $type ) {
605 564
606 - /**
607 - * Method remove_http_prefix()
608 - *
609 - * Remove http prefixes from given url.
610 - *
611 - * @param mixed $pUrl Given URL.
612 - * @param bool $pTrimSlashes Whether or not to trim slashes. Default is false.
613 - *
614 - * @return string Trimmed URL.
615 - */
616 - public static function remove_http_prefix( $pUrl, $pTrimSlashes = false ) {
617 - return str_replace( array( 'http:' . ( $pTrimSlashes ? '//' : '' ), 'https:' . ( $pTrimSlashes ? '//' : '' ) ), array( '', '' ), $pUrl );
618 - }
565 + $allowed_html = array(
566 + 'a' => array(
567 + 'href' => array(),
568 + 'title' => array(),
569 + 'class' => array(),
570 + 'onclick' => array(),
571 + ),
572 + 'img' => array(
573 + 'src' => array(),
574 + 'title' => array(),
575 + 'class' => array(),
576 + 'onclick' => array(),
577 + 'alt' => array(),
578 + 'width' => array(),
579 + 'height' => array(),
580 + 'sizes' => array(),
581 + 'srcset' => array(),
582 + 'usemap' => array(),
583 + ),
584 + 'br' => array(),
585 + 'em' => array(),
586 + 'strong' => array(),
587 + 'p' => array(),
588 + 'hr' => array(),
589 + 'ul' => array(
590 + 'style' => array(),
591 + ),
592 + 'ol' => array(),
593 + 'li' => array(),
594 + 'h1' => array(),
595 + 'h2' => array(),
596 + 'head' => array(),
597 + 'html' => array(
598 + 'lang' => array(),
599 + ),
600 + 'meta' => array(
601 + 'name' => array(),
602 + 'http-equiv' => array(),
603 + 'content' => array(),
604 + 'charset' => array(),
605 + ),
606 + 'title' => array(),
607 + 'body' => array(
608 + 'style' => array(),
609 + ),
610 + 'span' => array(
611 + 'id' => array(),
612 + 'style' => array(),
613 + 'class' => array(),
614 + ),
615 + 'form' => array(
616 + 'id' => array(),
617 + 'method' => array(),
618 + 'action' => array(),
619 + 'onsubmit' => array(),
620 + ),
621 + 'table' => array(
622 + 'class' => array(),
623 + ),
624 + 'thead' => array(
625 + 'class' => array(),
626 + ),
627 + 'tbody' => array(
628 + 'class' => array(),
629 + ),
630 + 'tr' => array(
631 + 'id' => array(),
632 + ),
633 + 'td' => array(
634 + 'class' => array(),
635 + ),
636 + 'div' => array(
637 + 'id' => array(),
638 + 'style' => array(),
639 + 'class' => array(),
640 + ),
641 + 'input' => array(
642 + 'type' => array(),
643 + 'name' => array(),
644 + 'class' => array(),
645 + 'value' => array(),
646 + 'onclick' => array(),
647 + ),
648 + 'button' => array(
649 + 'type' => array(),
650 + 'name' => array(),
651 + 'value' => array(),
652 + 'class' => array(),
653 + 'title' => array(),
654 + 'onclick' => array(),
655 + ),
656 + );
619 657
620 - /**
621 - * Method remove_http_www_prefix()
622 - *
623 - * Remove 'www.' from given URL.
624 - *
625 - * @param mixed $pUrl Given URL.
626 - *
627 - * @return string Cleaned URL.
628 - */
629 - public static function remove_http_www_prefix( $pUrl ) {
630 - $pUrl = static::remove_http_prefix( $pUrl, true );
631 - if ( static::starts_with( strtolower( $pUrl ), 'www.' ) ) {
632 - $pUrl = substr( $pUrl, 4 );
633 - }
634 - return $pUrl;
635 - }
658 + if ( is_array( $more_allowed ) && ! empty( $more_allowed ) ) {
659 + $allowed_html = array_merge( $allowed_html, $more_allowed );
660 + }
636 661
637 - /**
638 - * Method sanitize_file_name()
639 - *
640 - * Sanitize file names.
641 - *
642 - * @param mixed $filename File name to sanitize.
643 - *
644 - * @return string Sanitized filename.
645 - */
646 - public static function sanitize_file_name( $filename ) {
647 - $filename = str_replace( array( '|', '/', '\\', ' ', ':' ), array( '-', '-', '-', '-', '-' ), $filename );
648 - return sanitize_file_name( $filename );
649 - }
662 + $content = wp_kses( $content, $allowed_html );
663 + } else {
664 + $content = wp_kses_post( $content );
665 + }
650 666
667 + return $content;
668 + }
651 669
670 + /**
671 + * Method esc_mixed_content()
672 + *
673 + * Escape mixed content,
674 + * allowed content (a,href,title,br,em,strong,p,hr,ul,ol,li,h1,h2 ... ).
675 + *
676 + * @param mixed $data data to escape.
677 + * @param string $depth Maximum depth to walk through $data. Must be greater than 0.
678 + * @param mixed $more_allowed input allowed tags - options.
679 + *
680 + * @throws \Exception Excetpion message.
681 + *
682 + * @return string Filtered content containing only the allowed HTML.
683 + */
684 + public static function esc_mixed_content( $data, $depth, $more_allowed = array() ) {
685 + if ( $depth < 0 ) {
686 + throw new \Exception( 'Reached depth limit' );
687 + }
652 688
653 - /**
654 - * Method esc_content()
655 - *
656 - * Escape content,
657 - * allowed content (a,href,title,br,em,strong,p,hr,ul,ol,li,h1,h2 ... ).
658 - *
659 - * @param mixed $content Content to escape.
660 - * @param string $type Type of content. Default = note.
661 - * @param mixed $more_allowed input allowed tags - options.
662 - *
663 - * @return string Filtered content containing only the allowed HTML.
664 - */
665 - public static function esc_content( $content, $type = 'note', $more_allowed = array() ) {
666 - if ( ! is_string( $content ) ) {
667 - return $content;
668 - }
689 + if ( is_array( $data ) ) {
690 + $output = array();
691 + foreach ( $data as $id => $el ) {
692 + // Don't forget to sanitize the ID!
693 + if ( is_string( $id ) ) {
694 + $clean_id = self::esc_content( $id, 'mixed', $more_allowed );
695 + } else {
696 + $clean_id = $id;
697 + }
669 698
670 - if ( 'note' === $type ) {
699 + // Check the element type, so that we're only recursing if we really have to.
700 + if ( is_array( $el ) || is_object( $el ) ) {
701 + $output[ $clean_id ] = self::esc_mixed_content( $el, $depth - 1 );
702 + } elseif ( is_string( $el ) ) {
703 + $output[ $clean_id ] = self::esc_content( $el, 'mixed', $more_allowed );
704 + } else {
705 + $output[ $clean_id ] = $el;
706 + }
707 + }
708 + } elseif ( is_object( $data ) ) {
709 + $output = new stdClass();
710 + foreach ( $data as $id => $el ) {
711 + if ( is_string( $id ) ) {
712 + $clean_id = self::esc_content( $id, 'mixed', $more_allowed );
713 + } else {
714 + $clean_id = $id;
715 + }
671 716
672 - $allowed_html = array(
673 - 'a' => array(
674 - 'href' => array(),
675 - 'title' => array(),
676 - ),
677 - 'br' => array(),
678 - 'em' => array(),
679 - 'strong' => array(),
680 - 'p' => array(),
681 - 'hr' => array(),
682 - 'ul' => array(),
683 - 'ol' => array(),
684 - 'li' => array(),
685 - 'h1' => array(),
686 - 'h2' => array(),
687 - );
717 + if ( is_array( $el ) || is_object( $el ) ) {
718 + $output->$clean_id = self::esc_mixed_content( $el, $depth - 1, $more_allowed );
719 + } elseif ( is_string( $el ) ) {
720 + $output->$clean_id = self::esc_content( $el, 'mixed', $more_allowed );
721 + } else {
722 + $output->$clean_id = $el;
723 + }
724 + }
725 + } elseif ( is_string( $data ) ) {
726 + return self::esc_content( $data, 'mixed', $more_allowed );
727 + } else {
728 + return $data;
729 + }
688 730
689 - if ( is_array( $more_allowed ) && ! empty( $more_allowed ) ) {
690 - $allowed_html = array_merge( $allowed_html, $more_allowed );
691 - }
731 + return $output;
732 + }
692 733
693 - $content = wp_kses( $content, $allowed_html );
734 + /**
735 + * Method show_mainwp_message()
736 + *
737 + * Check whenther or not to show the MainWP Message.
738 + *
739 + * @param mixed $type Type of message.
740 + * @param mixed $notice_id Notice ID.
741 + *
742 + * @return boolean true|false.
743 + */
744 + public static function show_mainwp_message( $type, $notice_id ) {
745 + $status = get_user_option( 'mainwp_notice_saved_status' );
746 + if ( ! is_array( $status ) ) {
747 + $status = array();
748 + }
749 + if ( isset( $status[ $notice_id ] ) ) {
750 + return false;
751 + }
752 + return true;
753 + }
694 754
695 - } elseif ( 'mixed' === $type ) {
755 + /**
756 + * Method get_hide_notice_status()
757 + *
758 + * Check whenther or not to show the MainWP Message.
759 + *
760 + * @param mixed $notice_id Notice ID.
761 + *
762 + * @return mixed true|false|time.
763 + */
764 + public static function get_hide_notice_status( $notice_id ) {
765 + $notices = get_user_option( 'mainwp_notice_saved_status' );
766 + if ( ! is_array( $notices ) ) {
767 + $notices = array();
768 + }
769 + if ( isset( $notices[ $notice_id ] ) ) {
770 + return $notices[ $notice_id ];
771 + }
772 + return false;
773 + }
696 774
697 - $allowed_html = array(
698 - 'a' => array(
699 - 'href' => array(),
700 - 'title' => array(),
701 - 'class' => array(),
702 - 'onclick' => array(),
703 - ),
704 - 'img' => array(
705 - 'src' => array(),
706 - 'title' => array(),
707 - 'class' => array(),
708 - 'onclick' => array(),
709 - 'alt' => array(),
710 - 'width' => array(),
711 - 'height' => array(),
712 - 'sizes' => array(),
713 - 'srcset' => array(),
714 - 'usemap' => array(),
715 - ),
716 - 'br' => array(),
717 - 'em' => array(),
718 - 'strong' => array(),
719 - 'p' => array(),
720 - 'hr' => array(),
721 - 'ul' => array(
722 - 'style' => array(),
723 - ),
724 - 'ol' => array(),
725 - 'li' => array(),
726 - 'h1' => array(),
727 - 'h2' => array(),
728 - 'head' => array(),
729 - 'html' => array(
730 - 'lang' => array(),
731 - ),
732 - 'meta' => array(
733 - 'name' => array(),
734 - 'http-equiv' => array(),
735 - 'content' => array(),
736 - 'charset' => array(),
737 - ),
738 - 'title' => array(),
739 - 'body' => array(
740 - 'style' => array(),
741 - ),
742 - 'span' => array(
743 - 'id' => array(),
744 - 'style' => array(),
745 - 'class' => array(),
746 - ),
747 - 'form' => array(
748 - 'id' => array(),
749 - 'method' => array(),
750 - 'action' => array(),
751 - 'onsubmit' => array(),
752 - ),
753 - 'table' => array(
754 - 'class' => array(),
755 - ),
756 - 'thead' => array(
757 - 'class' => array(),
758 - ),
759 - 'tbody' => array(
760 - 'class' => array(),
761 - ),
762 - 'tr' => array(
763 - 'id' => array(),
764 - ),
765 - 'td' => array(
766 - 'class' => array(),
767 - ),
768 - 'div' => array(
769 - 'id' => array(),
770 - 'style' => array(),
771 - 'class' => array(),
772 - ),
773 - 'input' => array(
774 - 'type' => array(),
775 - 'name' => array(),
776 - 'class' => array(),
777 - 'value' => array(),
778 - 'onclick' => array(),
779 - ),
780 - 'button' => array(
781 - 'type' => array(),
782 - 'name' => array(),
783 - 'value' => array(),
784 - 'class' => array(),
785 - 'title' => array(),
786 - 'onclick' => array(),
787 - ),
788 - );
775 + /**
776 + * Method get_flash_message()
777 + *
778 + * Get saved flash Message.
779 + *
780 + * @param mixed $message_id Notice ID.
781 + * @param bool $delete True to delete the message after get it.
782 + *
783 + * @return boolean true|false.
784 + */
785 + public static function get_flash_message( $message_id, $delete = true ) {
786 + $flash_messages = get_user_option( 'mainwp_flash_messages' );
787 + if ( ! is_array( $flash_messages ) ) {
788 + $flash_messages = array();
789 + }
790 + if ( ! isset( $flash_messages[ $message_id ] ) ) {
791 + return false;
792 + }
793 + $content = $flash_messages[ $message_id ];
794 + if ( $delete ) {
795 + unset( $flash_messages[ $message_id ] );
796 + self::update_user_option( 'mainwp_flash_messages', $flash_messages );
797 + }
798 + return $content;
799 + }
789 800
790 - if ( is_array( $more_allowed ) && ! empty( $more_allowed ) ) {
791 - $allowed_html = array_merge( $allowed_html, $more_allowed );
792 - }
801 + /**
802 + * Method update_flash_message()
803 + *
804 + * Check whenther or not to show the MainWP Message.
805 + *
806 + * @param mixed $message_id Notice ID.
807 + * @param mixed $content Content of message.
808 + *
809 + * @return boolean true|false.
810 + */
811 + public static function update_flash_message( $message_id, $content ) {
812 + $flash_messages = get_user_option( 'mainwp_flash_messages' );
813 + if ( ! is_array( $flash_messages ) ) {
814 + $flash_messages = array();
815 + }
816 + $current = isset( $flash_messages[ $message_id ] ) ? $flash_messages[ $message_id ] : '';
817 + if ( empty( $current ) ) {
818 + $current = $content;
819 + } else {
820 + $current .= '|' . $content;
821 + }
822 + $flash_messages[ $message_id ] = $current;
823 + return self::update_user_option( 'mainwp_flash_messages', $flash_messages );
824 + }
793 825
794 - $content = wp_kses( $content, $allowed_html );
795 - } else {
796 - $content = wp_kses_post( $content );
797 - }
826 + /**
827 + * Method array_sort()
828 + *
829 + * Sort given array by given flags.
830 + *
831 + * @param mixed $array Array to sort.
832 + * @param mixed $key Array key.
833 + * @param string $sort_flag Flags to sort by. Default = SORT_STRING.
834 + */
835 + public static function array_sort( &$array, $key, $sort_flag = SORT_STRING ) {
836 + $sorter = array();
837 + $ret = array();
838 + reset( $array );
839 + foreach ( $array as $ii => $val ) {
840 + $sorter[ $ii ] = $val[ $key ];
841 + }
842 + asort( $sorter, $sort_flag );
843 + foreach ( $sorter as $ii => $val ) {
844 + $ret[ $ii ] = $array[ $ii ];
845 + }
846 + $array = $ret;
847 + }
798 848
799 - return $content;
800 - }
849 + /**
850 + * Method enabled_wp_seo()
851 + *
852 + * Check if Yoast SEO is enabled.
853 + *
854 + * @return boolean true|false.
855 + */
856 + public static function enabled_wp_seo() {
857 + if ( null === self::$enabled_wp_seo ) {
858 + self::$enabled_wp_seo = is_plugin_active( 'wordpress-seo-extension/wordpress-seo-extension.php' );
859 + }
860 + return self::$enabled_wp_seo;
861 + }
801 862
802 - /**
803 - * Method esc_mixed_content()
804 - *
805 - * Escape mixed content,
806 - * allowed content (a,href,title,br,em,strong,p,hr,ul,ol,li,h1,h2 ... ).
807 - *
808 - * @param mixed $data data to escape.
809 - * @param string $depth Maximum depth to walk through $data. Must be greater than 0.
810 - * @param mixed $more_allowed input allowed tags - options.
811 - *
812 - * @throws \MainWP_Exception Excetpion message.
813 - *
814 - * @return string Filtered content containing only the allowed HTML.
815 - */
816 - public static function esc_mixed_content( $data, $depth, $more_allowed = array() ) { // phpcs:ignore -- NOSONAR - complex.
817 - if ( $depth < 0 ) {
818 - throw new MainWP_Exception( 'Reached depth limit' );
819 - }
863 + /**
864 + * Method value_to_string()
865 + *
866 + * Value to string.
867 + *
868 + * @param mixed $var Value to convert to string.
869 + *
870 + * @return string Value that has been converted into a string.
871 + */
872 + public static function value_to_string( $var ) {
873 + if ( is_array( $var ) || is_object( $var ) ) {
874 + //phpcs:ignore -- for debug only
875 + return print_r( $var, true ); // @codingStandardsIgnoreLine
876 + } elseif ( is_string( $var ) ) {
877 + return $var;
878 + }
879 + return '';
880 + }
820 881
821 - if ( is_array( $data ) ) {
822 - $output = array();
823 - foreach ( $data as $id => $el ) {
824 - // Don't forget to sanitize the ID!
825 - if ( is_string( $id ) ) {
826 - $clean_id = static::esc_content( $id, 'mixed', $more_allowed );
827 - } else {
828 - $clean_id = $id;
829 - }
882 + /**
883 + * Method render_mainwp_nonce()
884 + *
885 + * Render MainWP nonce.
886 + */
887 + public static function render_mainwp_nonce() {
888 + wp_nonce_field( 'mainwp-admin-nonce' );
889 + }
830 890
831 - // Check the element type, so that we're only recursing if we really have to.
832 - if ( is_array( $el ) || is_object( $el ) ) {
833 - $output[ $clean_id ] = static::esc_mixed_content( $el, $depth - 1 );
834 - } elseif ( is_string( $el ) ) {
835 - $output[ $clean_id ] = static::esc_content( $el, 'mixed', $more_allowed );
836 - } else {
837 - $output[ $clean_id ] = $el;
838 - }
839 - }
840 - } elseif ( is_object( $data ) ) {
841 - $output = new stdClass();
842 - foreach ( $data as $id => $el ) {
843 - if ( is_string( $id ) ) {
844 - $clean_id = static::esc_content( $id, 'mixed', $more_allowed );
845 - } else {
846 - $clean_id = $id;
847 - }
848 891
849 - if ( is_array( $el ) || is_object( $el ) ) {
850 - $output->$clean_id = static::esc_mixed_content( $el, $depth - 1, $more_allowed );
851 - } elseif ( is_string( $el ) ) {
852 - $output->$clean_id = static::esc_content( $el, 'mixed', $more_allowed );
853 - } else {
854 - $output->$clean_id = $el;
855 - }
856 - }
857 - } elseif ( is_string( $data ) ) {
858 - return static::esc_content( $data, 'mixed', $more_allowed );
859 - } else {
860 - return $data;
861 - }
892 + /**
893 + * Get Health Site value.
894 + *
895 + * @param mixed $issue_counts Health site issues.
896 + *
897 + * @return array $array Health status value.
898 + */
899 + public static function get_site_health( $issue_counts ) {
862 900
863 - return $output;
864 - }
901 + if ( empty( $issue_counts ) ) {
902 + $issue_counts = array(
903 + 'good' => 0,
904 + 'recommended' => 0,
905 + 'critical' => 0,
906 + );
907 + }
865 908
866 - /**
867 - * Method parse_html_error_message()
868 - *
869 - * @param string $error_msg Error message.
870 - *
871 - * @return mixed array|string.
872 - */
873 - public static function parse_html_error_message( $error_msg ) {
874 - // pasing error message that included link html.
875 - preg_match( '/([^\<]*)(<a[^\>]*>)([^\<]*)(<[^\>]*>)(.*)/', $error_msg, $output_array );
876 - if ( is_array( $output_array ) && 6 === count( $output_array ) ) {
877 - preg_match( '/<a href="([^\"]*)"(.*)/', $output_array[2], $link_array );
878 - $link = '';
879 - if ( is_array( $link_array ) && 3 === count( $link_array ) ) {
880 - $link = $link_array[1];
881 - }
882 - if ( ! empty( $link ) ) {
883 - return array(
884 - 'el_before' => esc_html( $output_array[1] ),
885 - 'el_link' => esc_html( $link ),
886 - 'el_text' => esc_html( $output_array[3] ),
887 - 'el_after' => esc_html( $output_array[5] ),
888 - );
889 - }
890 - }
891 - return $error_msg;
892 - }
909 + $totalTests = intval( $issue_counts['good'] ) + intval( $issue_counts['recommended'] ) + intval( $issue_counts['critical'] ) * 1.5;
910 + $failedTests = intval( $issue_counts['recommended'] ) * 0.5 + $issue_counts['critical'] * 1.5;
893 911
894 - /**
895 - * Method show_mainwp_message()
896 - *
897 - * Check whenther or not to show the MainWP Message.
898 - *
899 - * @param mixed $type Type of message.
900 - * @param mixed $notice_id Notice ID.
901 - *
902 - * @return boolean true|false.
903 - */
904 - public static function show_mainwp_message( $type, $notice_id ) {
905 - unset( $type );
906 - $status = get_user_option( 'mainwp_notice_saved_status' );
907 - if ( ! is_array( $status ) ) {
908 - $status = array();
909 - }
910 - if ( isset( $status[ $notice_id ] ) ) {
911 - return false;
912 - }
913 - return true;
914 - }
912 + if ( 0 == $totalTests ) {
913 + $val = 100;
914 + } else {
915 + $val = 100 - ceil( ( $failedTests / $totalTests ) * 100 );
916 + }
915 917
916 - /**
917 - * Method get_hide_notice_status()
918 - *
919 - * Check whenther or not to show the MainWP Message.
920 - *
921 - * @param mixed $notice_id Notice ID.
922 - *
923 - * @return mixed true|false|time.
924 - */
925 - public static function get_hide_notice_status( $notice_id ) {
926 - $notices = get_user_option( 'mainwp_notice_saved_status' );
927 - if ( ! is_array( $notices ) ) {
928 - $notices = array();
929 - }
930 - if ( isset( $notices[ $notice_id ] ) ) {
931 - return $notices[ $notice_id ];
932 - }
933 - return false;
934 - }
918 + if ( 0 > $val ) {
919 + $val = 0;
920 + }
935 921
936 - /**
937 - * Method get_flash_message()
938 - *
939 - * Get saved flash Message.
940 - *
941 - * @param mixed $message_id Notice ID.
942 - * @param bool $delete True to delete the message after get it.
943 - *
944 - * @return boolean true|false.
945 - */
946 - public static function get_flash_message( $message_id, $delete = true ) {
947 - $flash_messages = get_user_option( 'mainwp_flash_messages' );
948 - if ( ! is_array( $flash_messages ) ) {
949 - $flash_messages = array();
950 - }
951 - if ( ! isset( $flash_messages[ $message_id ] ) ) {
952 - return false;
953 - }
954 - $content = $flash_messages[ $message_id ];
955 - if ( $delete ) {
956 - unset( $flash_messages[ $message_id ] );
957 - static::update_user_option( 'mainwp_flash_messages', $flash_messages );
958 - }
959 - return $content;
960 - }
922 + if ( 100 < $val ) {
923 + $val = 100;
924 + }
961 925
962 - /**
963 - * Method update_flash_message()
964 - *
965 - * Check whenther or not to show the MainWP Message.
966 - *
967 - * @param mixed $message_id Notice ID.
968 - * @param mixed $content Content of message.
969 - *
970 - * @return boolean true|false.
971 - */
972 - public static function update_flash_message( $message_id, $content ) {
973 - $flash_messages = get_user_option( 'mainwp_flash_messages' );
974 - if ( ! is_array( $flash_messages ) ) {
975 - $flash_messages = array();
976 - }
977 - $current = isset( $flash_messages[ $message_id ] ) ? $flash_messages[ $message_id ] : '';
978 - if ( empty( $current ) ) {
979 - $current = $content;
980 - } else {
981 - $current .= '|' . $content;
982 - }
983 - $flash_messages[ $message_id ] = $current;
984 - return static::update_user_option( 'mainwp_flash_messages', $flash_messages );
985 - }
926 + return array(
927 + 'val' => $val,
928 + 'critical' => $issue_counts['critical'],
929 + );
930 + }
986 931
987 - /**
988 - * Method array_sort()
989 - *
990 - * Sort given array by given flags.
991 - *
992 - * @param mixed $arr Array to sort.
993 - * @param mixed $key Array key.
994 - * @param string $sort_flag Flags to sort by. Default = SORT_STRING.
995 - */
996 - public static function array_sort( &$arr, $key, $sort_flag = SORT_STRING ) {
997 - $sorter = array();
998 - $ret = array();
999 - reset( $arr );
1000 - foreach ( $arr as $ii => $val ) {
1001 - $sorter[ $ii ] = $val[ $key ];
1002 - }
1003 - asort( $sorter, $sort_flag );
1004 - foreach ( $sorter as $ii => $val ) {
1005 - $ret[ $ii ] = $arr[ $ii ];
1006 - }
1007 - $arr = $ret;
1008 - }
1009 932
1010 - /**
1011 - * Method array_sort_existed_keys()
1012 - *
1013 - * Sort given array by given flags.
1014 - *
1015 - * @param mixed $arr Array to sort.
1016 - * @param mixed $key Array key.
1017 - * @param string $sort_flag Flags to sort by. Default = SORT_STRING.
1018 - */
1019 - public static function array_sort_existed_keys( &$arr, $key, $sort_flag = SORT_STRING ) {
1020 - $sorter = array();
1021 - $ret = array();
1022 - reset( $arr );
933 + /**
934 + * Get HTTP code.
935 + *
936 + * @param int $code HTTP code.
937 + *
938 + * @return array $http_codes HTTP code.
939 + */
940 + public static function get_http_codes( $code = false ) {
1023 941
1024 - // get items with $key to sort.
1025 - foreach ( $arr as $ii => $val ) {
1026 - if ( isset( $val[ $key ] ) ) {
1027 - $sorter[ $ii ] = $val[ $key ];
1028 - }
1029 - }
1030 - asort( $sorter, $sort_flag );
942 + $http_codes = array(
943 + 100 => 'Continue',
944 + 101 => 'Switching Protocols',
945 + 200 => 'OK',
946 + 201 => 'Created',
947 + 202 => 'Accepted',
948 + 203 => 'Non-Authoritative Information',
949 + 204 => 'No Content',
950 + 205 => 'Reset Content',
951 + 206 => 'Partial Content',
952 + 300 => 'Multiple Choices',
953 + 301 => 'Moved Permanently',
954 + 302 => 'Found',
955 + 303 => 'See Other',
956 + 304 => 'Not Modified',
957 + 305 => 'Use Proxy',
958 + 306 => '(Unused)',
959 + 307 => 'Temporary Redirect',
960 + 400 => 'Bad Request',
961 + 401 => 'Unauthorized',
962 + 402 => 'Payment Required',
963 + 403 => 'Forbidden',
964 + 404 => 'Not Found',
965 + 405 => 'Method Not Allowed',
966 + 406 => 'Not Acceptable',
967 + 407 => 'Proxy Authentication Required',
968 + 408 => 'Request Timeout',
969 + 409 => 'Conflict',
970 + 410 => 'Gone',
971 + 411 => 'Length Required',
972 + 412 => 'Precondition Failed',
973 + 413 => 'Request Entity Too Large',
974 + 414 => 'Request-URI Too Long',
975 + 415 => 'Unsupported Media Type',
976 + 416 => 'Requested Range Not Satisfiable',
977 + 417 => 'Expectation Failed',
978 + 500 => 'Internal Server Error',
979 + 501 => 'Not Implemented',
980 + 502 => 'Bad Gateway',
981 + 503 => 'Service Unavailable',
982 + 504 => 'Gateway Timeout',
983 + 505 => 'HTTP Version Not Supported',
984 + );
1031 985
1032 - foreach ( $sorter as $ii => $val ) {
1033 - $ret[ $ii ] = $arr[ $ii ];
1034 - }
986 + if ( false === $code ) {
987 + return $http_codes;
988 + }
1035 989
1036 - // asign other items (without $keys).
1037 - foreach ( $arr as $ii => $val ) {
1038 - if ( ! isset( $val[ $key ] ) ) {
1039 - $ret[ $ii ] = $val;
1040 - }
1041 - }
990 + return isset( $http_codes[ $code ] ) ? $http_codes[ $code ] : '';
991 + }
1042 992
1043 - $arr = $ret;
1044 - }
993 + /**
994 + * Method valid_input_emails().
995 + *
996 + * @param string $emails Input emails string.
997 + *
998 + * @return string $valid_emails Valid emails string.
999 + */
1000 + public static function valid_input_emails( $emails ) {
1045 1001
1046 - /**
1047 - * Method numeric_filter()
1048 - *
1049 - * Filter given numeric.
1050 - *
1051 - * @param int $int_num Int number.
1052 - * @return array $arr_ints Array filtered.
1053 - */
1054 - public static function numeric_filter( $int_num ) {
1055 - return ( (string) (int) $int_num === (string) $int_num && 0 < $int_num ) ? $int_num : false;
1056 - }
1002 + if ( is_string( $emails ) ) {
1003 + $emails = array_filter( explode( ',', $emails ) );
1004 + }
1057 1005
1058 - /**
1059 - * Method array_numeric_filter()
1060 - *
1061 - * Filter given numeric array.
1062 - *
1063 - * @param array $arr_ints Array to filter.
1064 - * @return array $arr_ints Array filtered.
1065 - */
1066 - public static function array_numeric_filter( $arr_ints ) {
1067 - $arr_ints = array_filter(
1068 - $arr_ints,
1069 - function ( $e ) {
1070 - return ( (string) (int) $e === (string) $e && 0 < $e ) ? true : false;
1071 - }
1072 - );
1073 - return $arr_ints;
1074 - }
1006 + $valid_emails = array();
1007 + if ( is_array( $emails ) ) {
1008 + foreach ( $emails as $email ) {
1009 + $email = esc_html( trim( $email ) );
1010 + if ( ! empty( $email ) && ! in_array( $email, $valid_emails, true ) ) {
1011 + $valid_emails[] = $email;
1012 + }
1013 + }
1014 + }
1015 + $valid_emails = implode( ',', $valid_emails );
1016 + return $valid_emails;
1017 + }
1075 1018
1076 - /**
1077 - * Method enabled_wp_seo()
1078 - *
1079 - * Check if Yoast SEO is enabled.
1080 - *
1081 - * @return boolean true|false.
1082 - */
1083 - public static function enabled_wp_seo() {
1084 - if ( null === static::$enabled_wp_seo ) {
1085 - static::$enabled_wp_seo = is_plugin_active( 'wordpress-seo-extension/wordpress-seo-extension.php' );
1086 - }
1087 - return static::$enabled_wp_seo;
1088 - }
1019 + /**
1020 + * Method check_image_file_name()
1021 + *
1022 + * Check if the file image.
1023 + *
1024 + * @param string $filename Contains image (file) name.
1025 + *
1026 + * @return true|false valid name or not.
1027 + */
1028 + public static function check_image_file_name( $filename ) {
1029 + if ( validate_file( $filename ) ) {
1030 + return false;
1031 + }
1089 1032
1090 - /**
1091 - * Method value_to_string()
1092 - *
1093 - * Value to string.
1094 - *
1095 - * @param mixed $var_value Value to convert to string.
1096 - *
1097 - * @return string Value that has been converted into a string.
1098 - */
1099 - public static function value_to_string( $var_value ) {
1100 - if ( is_array( $var_value ) || is_object( $var_value ) ) {
1101 - //phpcs:ignore -- for debug only
1102 - return print_r( $var_value, true );
1103 - } elseif ( is_string( $var_value ) ) {
1104 - return $var_value;
1105 - }
1106 - return '';
1107 - }
1033 + $allowed_files = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'bmp', 'tif', 'tiff', 'ico' );
1034 + $file_ext = array_values( array_slice( explode( '.', $filename ), -1 ) )[0];
1035 + $file_ext = strtolower( $file_ext );
1036 + if ( ! in_array( $file_ext, $allowed_files ) ) {
1037 + return false;
1038 + }
1108 1039
1109 - /**
1110 - * Get Health Site value.
1111 - *
1112 - * @param mixed $issue_counts Health site issues.
1113 - *
1114 - * @return array Health status value.
1115 - */
1116 - public static function get_site_health( $issue_counts ) {
1040 + return true;
1041 + }
1117 1042
1118 - if ( empty( $issue_counts ) ) {
1119 - $issue_counts = array(
1120 - 'good' => 0,
1121 - 'recommended' => 0,
1122 - 'critical' => 0,
1123 - );
1124 - }
1043 + /**
1044 + * Method check_abandoned()
1045 + *
1046 + * Get site's icon.
1047 + *
1048 + * @param mixed $siteId site's id.
1049 + * @param string $which to check plugin/theme.
1050 + *
1051 + * @return array result error or success
1052 + * @throws \Exception Error message.
1053 + */
1054 + public static function check_abandoned( $siteId = null, $which = '' ) {
1055 + if ( self::ctype_digit( $siteId ) ) {
1056 + $website = MainWP_DB::instance()->get_website_by_id( $siteId );
1057 + if ( MainWP_System_Utility::can_edit_website( $website ) ) {
1058 + $error = '';
1059 + try {
1060 + $information = MainWP_Connect::fetch_url_authed( $website, 'check_abandoned', array( 'which' => $which ) );
1061 + if ( is_array( $information ) && isset( $information['sync'] ) && ! empty( $information['sync'] ) ) {
1062 + MainWP_Sync::sync_information_array( $website, $information['sync'] );
1063 + unset( $information['sync'] );
1064 + }
1065 + } catch ( MainWP_Exception $e ) {
1066 + $error = $e->getMessage();
1067 + }
1125 1068
1126 - $totalTests = intval( $issue_counts['good'] ) + intval( $issue_counts['recommended'] ) + intval( $issue_counts['critical'] ) * 1.5;
1127 - $failedTests = intval( $issue_counts['recommended'] ) * 0.5 + $issue_counts['critical'] * 1.5;
1069 + if ( '' != $error ) {
1070 + return array( 'error' => $error );
1071 + } elseif ( isset( $information['success'] ) && ! empty( $information['success'] ) ) {
1072 + return array( 'result' => 'success' );
1073 + } else {
1074 + return array( 'undefined_error' => true );
1075 + }
1076 + }
1077 + }
1078 + return array( 'result' => 'NOSITE' );
1079 + }
1128 1080
1129 - if ( empty( $totalTests ) ) {
1130 - $val = 100;
1131 - } else {
1132 - $val = 100 - ceil( ( $failedTests / $totalTests ) * 100 );
1133 - }
1081 + /**
1082 + * Get directory or slug of plugin.
1083 + *
1084 + * @param string $slug Plugin slug.
1085 + *
1086 + * @return string $value directory or slug of plugin.
1087 + */
1088 + public static function get_dir_slug( $slug ) {
1089 + $value = '';
1090 + if ( false === strpos( $slug, '/' ) ) {
1091 + if ( false !== strpos( $slug, '.' ) ) {
1092 + $value = substr( $slug, 0, strpos( $slug, '.' ) );
1093 + }
1094 + } else {
1095 + $value = dirname( $slug );
1096 + }
1097 + if ( empty( $value ) ) {
1098 + return $slug;
1099 + }
1100 + return $value;
1101 + }
1134 1102
1135 - if ( 0 > $val ) {
1136 - $val = 0;
1137 - }
1103 + /**
1104 + * Metho get_siteview_mode().
1105 + *
1106 + * Get site view mode.
1107 + *
1108 + * @return string $viewmode Site view mode.
1109 + */
1110 + public static function get_siteview_mode() {
1111 + $viewmode = get_user_option( 'mainwp_sitesviewmode' );
1112 + if ( 'grid' !== $viewmode && 'table' !== $viewmode ) {
1113 + $viewmode = 'grid';
1114 + }
1115 + return $viewmode;
1116 + }
1138 1117
1139 - if ( 100 < $val ) {
1140 - $val = 100;
1141 - }
1142 1118
1143 - return array(
1144 - 'val' => $val,
1145 - 'critical' => $issue_counts['critical'],
1146 - );
1147 - }
1119 + /**
1120 + * Metho delete_file().
1121 + *
1122 + * Delete file.
1123 + *
1124 + * @param string $file_path File path.
1125 + *
1126 + * @return bool true|false.
1127 + */
1128 + public static function delete_file( $file_path ) {
1148 1129
1130 + global $wp_filesystem;
1149 1131
1150 - /**
1151 - * Get HTTP code.
1152 - *
1153 - * @param int $code HTTP code.
1154 - *
1155 - * @return array $http_codes HTTP code.
1156 - */
1157 - public static function get_http_codes( $code = false ) {
1132 + if ( ! empty( $file_path ) ) {
1133 + if ( $wp_filesystem ) {
1134 + if ( $wp_filesystem->exists( $file_path ) ) {
1135 + $wp_filesystem->delete( $file_path );
1136 + }
1137 + } elseif ( file_exists( $file_path ) ) {
1138 + unlink( $file_path );
1139 + }
1140 + return true;
1141 + }
1158 1142
1159 - $http_codes = array(
1160 - 100 => 'Continue',
1161 - 101 => 'Switching Protocols',
1162 - 200 => 'OK',
1163 - 201 => 'Created',
1164 - 202 => 'Accepted',
1165 - 203 => 'Non-Authoritative Information',
1166 - 204 => 'No Content',
1167 - 205 => 'Reset Content',
1168 - 206 => 'Partial Content',
1169 - 300 => 'Multiple Choices',
1170 - 301 => 'Moved Permanently',
1171 - 302 => 'Found',
1172 - 303 => 'See Other',
1173 - 304 => 'Not Modified',
1174 - 305 => 'Use Proxy',
1175 - 306 => '(Unused)',
1176 - 307 => 'Temporary Redirect',
1177 - 400 => 'Bad Request',
1178 - 401 => 'Unauthorized',
1179 - 402 => 'Payment Required',
1180 - 403 => 'Forbidden',
1181 - 404 => 'Not Found',
1182 - 405 => 'Method Not Allowed',
1183 - 406 => 'Not Acceptable',
1184 - 407 => 'Proxy Authentication Required',
1185 - 408 => 'Request Timeout',
1186 - 409 => 'Conflict',
1187 - 410 => 'Gone',
1188 - 411 => 'Length Required',
1189 - 412 => 'Precondition Failed',
1190 - 413 => 'Request Entity Too Large',
1191 - 414 => 'Request-URI Too Long',
1192 - 415 => 'Unsupported Media Type',
1193 - 416 => 'Requested Range Not Satisfiable',
1194 - 417 => 'Expectation Failed',
1195 - 500 => 'Internal Server Error',
1196 - 501 => 'Not Implemented',
1197 - 502 => 'Bad Gateway',
1198 - 503 => 'Service Unavailable',
1199 - 504 => 'Gateway Timeout',
1200 - 505 => 'HTTP Version Not Supported',
1201 - );
1202 -
1203 - if ( false === $code ) {
1204 - return $http_codes;
1205 - }
1206 -
1207 - return isset( $http_codes[ $code ] ) ? $http_codes[ $code ] : '';
1208 - }
1209 -
1210 - /**
1211 - * Method valid_input_emails().
1212 - *
1213 - * @param string $emails Input emails string.
1214 - *
1215 - * @return string $valid_emails Valid emails string.
1216 - */
1217 - public static function valid_input_emails( $emails ) {
1218 -
1219 - if ( is_string( $emails ) ) {
1220 - $emails = array_filter( explode( ',', $emails ) );
1221 - }
1222 -
1223 - $valid_emails = array();
1224 - if ( is_array( $emails ) ) {
1225 - foreach ( $emails as $email ) {
1226 - $email = esc_html( trim( $email ) );
1227 - if ( ! empty( $email ) && ! in_array( $email, $valid_emails, true ) ) {
1228 - $valid_emails[] = $email;
1229 - }
1230 - }
1231 - }
1232 - $valid_emails = implode( ',', $valid_emails );
1233 - return $valid_emails;
1234 - }
1235 -
1236 - /**
1237 - * Method check_image_file_name()
1238 - *
1239 - * Check if the file image.
1240 - *
1241 - * @param string $filename Contains image (file) name.
1242 - *
1243 - * @return true|false valid name or not.
1244 - */
1245 - public static function check_image_file_name( $filename ) {
1246 - if ( validate_file( $filename ) ) {
1247 - return false;
1248 - }
1249 -
1250 - $allowed_files = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'bmp', 'tif', 'tiff', 'ico', 'webp', 'heic' );
1251 - $file_ext = array_values( array_slice( explode( '.', $filename ), -1 ) )[0];
1252 - $file_ext = strtolower( $file_ext );
1253 - if ( ! in_array( $file_ext, $allowed_files ) ) {
1254 - return false;
1255 - }
1256 -
1257 - return true;
1258 - }
1259 -
1260 - /**
1261 - * Method check_abandoned()
1262 - *
1263 - * Get site's icon.
1264 - *
1265 - * @param mixed $siteId site's id.
1266 - * @param string $which to check plugin/theme.
1267 - *
1268 - * @return array result error or success
1269 - * @throws \MainWP_Exception Error message.
1270 - */
1271 - public static function check_abandoned( $siteId = null, $which = '' ) { // phpcs:ignore -- NOSONAR - complex.
1272 - if ( static::ctype_digit( $siteId ) ) {
1273 - $website = MainWP_DB::instance()->get_website_by_id( $siteId );
1274 - if ( MainWP_System_Utility::can_edit_website( $website ) ) {
1275 - $error = '';
1276 - try {
1277 - $information = MainWP_Connect::fetch_url_authed( $website, 'check_abandoned', array( 'which' => $which ) );
1278 - if ( is_array( $information ) && isset( $information['sync'] ) && ! empty( $information['sync'] ) ) {
1279 - MainWP_Sync::sync_information_array( $website, $information['sync'] );
1280 - unset( $information['sync'] );
1281 - }
1282 - } catch ( MainWP_Exception $e ) {
1283 - $error = $e->getMessage();
1284 - }
1285 -
1286 - if ( '' !== $error ) {
1287 - return array( 'error' => $error );
1288 - } elseif ( isset( $information['success'] ) && ! empty( $information['success'] ) ) {
1289 - return array( 'result' => 'success' );
1290 - } else {
1291 - return array( 'undefined_error' => true );
1292 - }
1293 - }
1294 - }
1295 - return array( 'result' => 'NOSITE' );
1296 - }
1297 -
1298 - /**
1299 - * Get directory or slug of plugin.
1300 - *
1301 - * @param string $slug Plugin slug.
1302 - *
1303 - * @return string $value directory or slug of plugin.
1304 - */
1305 - public static function get_dir_slug( $slug ) {
1306 - $value = '';
1307 - if ( false === strpos( $slug, '/' ) ) {
1308 - if ( false !== strpos( $slug, '.' ) ) {
1309 - $value = substr( $slug, 0, strpos( $slug, '.' ) );
1310 - }
1311 - } else {
1312 - $value = dirname( $slug );
1313 - }
1314 - if ( empty( $value ) ) {
1315 - return $slug;
1316 - }
1317 - return $value;
1318 - }
1319 -
1320 - /**
1321 - * Metho get_siteview_mode().
1322 - *
1323 - * Get site view mode.
1324 - *
1325 - * @return string $viewmode Site view mode.
1326 - */
1327 - public static function get_siteview_mode() {
1328 - $viewmode = get_user_option( 'mainwp_sitesviewmode' );
1329 - if ( 'grid' !== $viewmode && 'table' !== $viewmode ) {
1330 - $viewmode = 'grid';
1331 - }
1332 - return $viewmode;
1333 - }
1334 -
1335 -
1336 - /**
1337 - * Metho delete_file().
1338 - *
1339 - * Delete file.
1340 - *
1341 - * @param string $file_path File path.
1342 - *
1343 - * @return bool true|false.
1344 - */
1345 - public static function delete_file( $file_path ) {
1346 -
1347 - global $wp_filesystem;
1348 -
1349 - if ( ! empty( $file_path ) ) {
1350 - if ( $wp_filesystem ) {
1351 - if ( $wp_filesystem->exists( $file_path ) ) {
1352 - $wp_filesystem->delete( $file_path );
1353 - }
1354 - } elseif ( file_exists( $file_path ) ) {
1355 - wp_delete_file( $file_path );
1356 - }
1357 - return true;
1358 - }
1359 -
1360 - return false;
1361 - }
1362 -
1363 - /**
1364 - * Method get_disable_functions()
1365 - *
1366 - * Get disable functions.
1367 - *
1368 - * @return string
1369 - */
1370 - public function get_disable_functions() {
1371 - if ( null === static::$disabled_functions ) {
1372 - static::$disabled_functions = ini_get( 'disable_functions' );
1373 - }
1374 - return static::$disabled_functions;
1375 - }
1376 -
1377 - /**
1378 - * Method is_disable_functions()
1379 - *
1380 - * Check if it is disabled functions.
1381 - *
1382 - * @param string $func Function name to check.
1383 - *
1384 - * @return string
1385 - */
1386 - public function is_disabled_functions( $func ) {
1387 - $dis_funcs = $this->get_disable_functions();
1388 -
1389 - if ( ! empty( $dis_funcs ) && ( false !== stristr( $dis_funcs, $func ) ) ) {
1390 - return true;
1391 - }
1392 - return false;
1393 - }
1394 -
1395 - /**
1396 - * Method hook_verify_ping_nonce()
1397 - *
1398 - * Verify nonce without session and user id.
1399 - *
1400 - * @param bool $input_value Boolean value, it should always be FALSE.
1401 - * @param string $nonce Nonce to verify.
1402 - * @param mixed $siteid Site ID.
1403 - *
1404 - * @return mixed If verified return 1 or 2, if not return false.
1405 - */
1406 - public static function hook_verify_ping_nonce( $input_value, $nonce = '', $siteid = false ) {
1407 - unset( $input_value );
1408 - $action = 'pingnonce';
1409 - return static::verify_site_nonce( $nonce, $action, $siteid );
1410 - }
1411 -
1412 - /**
1413 - * Method create_site_nonce()
1414 - *
1415 - * Create action nonce for site.
1416 - *
1417 - * @param mixed $action Action to perform.
1418 - * @param mixed $siteid Site ID.
1419 - *
1420 - * @return string Custom nonce.
1421 - */
1422 - public static function create_site_nonce( $action = - 1, $siteid = false ) {
1423 - if ( empty( $action ) || empty( $siteid || ! is_numeric( $siteid ) ) ) {
1424 - return false;
1425 - }
1426 - return substr( wp_hash( 'site|' . $siteid . '|' . $action, 'nonce' ), - 12, 10 );
1427 - }
1428 -
1429 - /**
1430 - * Method verify_site_nonce()
1431 - *
1432 - * Verify nonce without session and user id.
1433 - *
1434 - * @param string $nonce Nonce to verify.
1435 - * @param mixed $action Action to perform.
1436 - * @param mixed $siteid Site ID.
1437 - *
1438 - * @return mixed If verified return 1 or 2, if not return false.
1439 - */
1440 - public static function verify_site_nonce( $nonce, $action = - 1, $siteid = 0 ) {
1441 - $nonce = (string) $nonce;
1442 - if ( empty( $nonce ) || empty( $siteid || ! is_numeric( $siteid ) ) ) {
1443 - return false;
1444 - }
1445 -
1446 - $expected = substr( wp_hash( 'site|' . $siteid . '|' . $action, 'nonce' ), - 12, 10 );
1447 - if ( hash_equals( $expected, $nonce ) ) {
1448 - return 1;
1449 - }
1450 - return false;
1451 - }
1452 -
1453 -
1454 - /**
1455 - * Find for multi keywords.
1456 - *
1457 - * @param string $name_str string find on.
1458 - * @param array $words Array string input.
1459 - * @return bool True|False.
1460 - */
1461 - public static function multi_find_keywords( $name_str, $words = array() ) {
1462 - if ( ! is_array( $words ) ) {
1463 - return false;
1464 - }
1465 - foreach ( $words as $word ) {
1466 - if ( stristr( $name_str, $word ) ) {
1467 - return true;
1468 -
1469 - }
1470 - }
1471 - return false;
1472 - }
1473 -
1474 - /**
1475 - * Merge values from right array to left array.
1476 - *
1477 - * @param array $left_array left array.
1478 - * @param array $right_array right array.
1479 - *
1480 - * @return array $result result array.
1481 - */
1482 - public static function right_array_merge( $left_array, $right_array ) {
1483 - if ( ! is_array( $left_array ) || ! is_array( $right_array ) ) {
1484 - return array();
1485 - }
1486 - $result = array_intersect_key( $right_array, $left_array );
1487 - return array_merge( $left_array, $result );
1488 - }
1489 -
1490 -
1491 - /**
1492 - * Method get_set_deactivated_licenses_alerted().
1493 - *
1494 - * @param string $slug Extension slug.
1495 - * @param bool $time_value Time value.
1496 - * @param string $act get/set value.
1497 - *
1498 - * @return array $result result array.
1499 - */
1500 - public function get_set_deactivated_licenses_alerted( $slug, $time_value = false, $act = 'get' ) {
1501 - if ( null === $this->last_deactivated_alerts ) {
1502 - $this->last_deactivated_alerts = get_option( 'mainwp_cron_licenses_deactivated_alerted', array() );
1503 - if ( ! is_array( $this->last_deactivated_alerts ) ) {
1504 - $this->last_deactivated_alerts = array();
1505 - }
1506 - }
1507 - if ( 'get' === $act ) {
1508 - return isset( $this->last_deactivated_alerts[ $slug ] ) ? $this->last_deactivated_alerts[ $slug ] : 0;
1509 - } elseif ( 'set' === $act ) {
1510 - $this->last_deactivated_alerts[ $slug ] = intval( $time_value );
1511 - get_option( 'mainwp_cron_licenses_deactivated_alerted', $this->last_deactivated_alerts );
1512 - }
1513 - }
1514 -
1515 - /**
1516 - * Method get_remote_favicon().
1517 - *
1518 - * @param string $url Url.
1519 - * @param string $favi favicon file name.
1520 - * @param int $item_id item id.
1521 - * @param string $file_prefix favicon file prefix name.
1522 - *
1523 - * @return mixed result.
1524 - */
1525 - public static function get_remote_favicon( $url, $favi = '', $item_id = false, $file_prefix = '' ) { // phpcs:ignore -- NOSONAR - complex.
1526 -
1527 - if ( empty( $favi ) ) {
1528 - $favi = 'favicon.ico';
1529 - }
1530 -
1531 - if ( '/' !== substr( $url, - 1 ) ) {
1532 - $url .= '/';
1533 - }
1534 -
1535 - $favi_url = $url . $favi;
1536 -
1537 - $content = MainWP_Connect::get_file_content( $favi_url );
1538 -
1539 - if ( empty( $content ) && 'favicon.ico' === $favi ) {
1540 - $favi_url = $url . 'favicon.png';
1541 - $content = MainWP_Connect::get_file_content( $favi_url ); // try other file.
1542 - }
1543 -
1544 - if ( ! empty( $content ) ) {
1545 -
1546 - MainWP_System_Utility::get_wp_file_system();
1547 -
1548 - global $wp_filesystem;
1549 -
1550 - $dirs = MainWP_System_Utility::get_mainwp_dir( 'icons', true );
1551 - $iconsDir = $dirs[0];
1552 - if ( $favi ) {
1553 -
1554 - $tmp = explode( '.', $favi );
1555 - if ( 2 !== count( $tmp ) ) {
1556 - return false;
1557 - }
1558 -
1559 - $favi_ext = $tmp[1];
1560 -
1561 - if ( empty( $item_id ) ) {
1562 - $item_id = time() . '-' . wp_rand( 100, 999 );
1563 - }
1564 - if ( ! empty( $file_prefix ) ) {
1565 - $filename = $file_prefix . $item_id . '.' . $favi_ext;
1566 - } else {
1567 - $filename = 'favi-' . $item_id . '.' . $favi_ext;
1568 - }
1569 -
1570 - $size = $wp_filesystem->put_contents( $iconsDir . $filename, $content ); // phpcs:ignore --
1571 - if ( $size ) {
1572 - MainWP_Logger::instance()->debug( 'Icon Cost Product size :: ' . $size );
1573 - return array(
1574 - 'result' => 'success',
1575 - 'file' => $filename,
1576 - 'dir' => $iconsDir,
1577 - );
1578 - } else {
1579 - return array( 'error' => 'Save icon file failed.' );
1580 - }
1581 - }
1582 - return false;
1583 - } else {
1584 - return array( 'error' => esc_html__( 'Download icon file failed', 'mainwp' ) );
1585 - }
1586 - }
1587 -
1588 - /**
1589 - * Method get_saved_favicon_url()
1590 - *
1591 - * @param string $favi Favicon file name.
1592 - *
1593 - * @return mixed $faviurl Favicon URL.
1594 - */
1595 - public static function get_saved_favicon_url( $favi ) {
1596 - $faviurl = '';
1597 - if ( ! empty( $favi ) ) {
1598 - $dirs = MainWP_System_Utility::get_icons_dir();
1599 - if ( file_exists( $dirs[0] . $favi ) ) {
1600 - $faviurl = $dirs[1] . $favi;
1601 - } else {
1602 - $faviurl = '';
1603 - }
1604 - }
1605 - return $faviurl;
1606 - }
1607 -
1608 - /**
1609 - * Method delete_saved_favicon()
1610 - *
1611 - * @param string $favi Favicon file name.
1612 - *
1613 - * @return bool Success result.
1614 - */
1615 - public static function delete_saved_favicon( $favi ) {
1616 - if ( ! empty( $favi ) ) {
1617 - $hasWPFileSystem = MainWP_System_Utility::get_wp_file_system();
1618 - global $wp_filesystem;
1619 - $dirs = MainWP_System_Utility::get_icons_dir();
1620 - if ( $hasWPFileSystem && $wp_filesystem->exists( $dirs[0] . $favi ) ) {
1621 - $wp_filesystem->delete( $dirs[0] . $favi );
1622 - return true;
1623 - }
1624 - }
1625 - return false;
1626 - }
1627 -
1628 - /**
1629 - * Delete icon file.
1630 - *
1631 - * @param string $sub_dir Sub dir file icon.
1632 - * @param string $cost_icon file icon.
1633 - */
1634 - public function delete_uploaded_icon_file( $sub_dir, $cost_icon ) {
1635 - $valid_file = 0 === validate_file( $cost_icon ) ? true : false;
1636 - if ( $valid_file ) {
1637 - $dirs = MainWP_System_Utility::get_mainwp_dir( $sub_dir, true );
1638 - $f = $dirs[0] . $cost_icon;
1639 - if ( file_exists( $f ) ) {
1640 - wp_delete_file( $f );
1641 - }
1642 - }
1643 - }
1644 -
1645 - /**
1646 - * Method get_table_orders().
1647 - *
1648 - * @param array $data table data.
1649 - */
1650 - public function get_table_orders( $data ) {
1651 -
1652 - $values = array(
1653 - 'orderby' => null,
1654 - 'order' => null,
1655 - );
1656 -
1657 - if ( isset( $data['order'] ) ) {
1658 - $columns = isset( $data['columns'] ) ? wp_unslash( $data['columns'] ) : array();
1659 - $ord_col = isset( $data['order'][0]['column'] ) ? sanitize_text_field( wp_unslash( $data['order'][0]['column'] ) ) : '';
1660 - if ( isset( $columns[ $ord_col ] ) ) {
1661 - $values = array(
1662 - 'orderby' => isset( $columns[ $ord_col ]['data'] ) ? sanitize_text_field( wp_unslash( $columns[ $ord_col ]['data'] ) ) : '',
1663 - 'order' => isset( $data['order'][0]['dir'] ) ? sanitize_text_field( wp_unslash( $data['order'][0]['dir'] ) ) : '',
1664 - );
1665 - }
1666 - }
1667 -
1668 - return $values;
1669 - }
1670 -
1671 - /**
1672 - * Method valid_file_check().
1673 - *
1674 - * @param string $path file path.
1675 - * @param bool $readable readable.
1676 - *
1677 - * @return bool is valid.
1678 - */
1679 - public static function valid_file_check( $path, $readable = true ) {
1680 - $valid = is_string( $path ) && ! stristr( $path, '..' );
1681 - if ( $valid && $readable ) {
1682 - $valid = is_readable( $path );
1683 - }
1684 - return $valid;
1685 - }
1686 -
1687 -
1688 - /**
1689 - * Handle sanitize POST data.
1690 - *
1691 - * @param array $data input data.
1692 - *
1693 - * @return array
1694 - */
1695 - public function sanitize_data( $data ) {
1696 - if ( ! is_array( $data ) ) {
1697 - return array();
1698 - }
1699 -
1700 - // Sanitize all record values.
1701 - return array_map(
1702 - function ( $value ) {
1703 - if ( ! is_array( $value ) ) {
1704 - return wp_strip_all_tags( $value );
1705 - }
1706 -
1707 - return $value;
1708 - },
1709 - $data
1710 - );
1711 - }
1143 + return false;
1144 + }
1712 1145 }