PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.0.3
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.0.3
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 5.2.2 5.3 All 153 releases
mainwp / class / class-mainwp-utility.php

class-mainwp-utility.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.0.3, at class/class-mainwp-utility.php

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