PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.2.2
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.2.2
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.2.2, at class/class-mainwp-utility.php

1,713 lines 50.5 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 { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
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 === static::$instance ) {
74 static::$instance = new self();
75 }
76
77 return static::$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 ( 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 }
137
138 if ( static::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 ) { // 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 }
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 ) && ! 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 }
245
246 return $output;
247 }
248
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 }
279
280
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 }
293
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 }
306
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 }
322
323 /**
324 * Method end_session()
325 *
326 * End a session.
327 *
328 * @return void
329 */
330 public static function end_session() {
331
332 if ( defined( 'WP_CLI' ) && WP_CLI ) {
333 return;
334 }
335
336 if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
337 return;
338 }
339
340 session_write_close();
341 if ( 0 < ob_get_length() ) {
342 ob_end_flush();
343 }
344 }
345
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
361 return $gmtOffset ? ( $gmtOffset * HOUR_IN_SECONDS ) + $timestamp : $timestamp;
362 }
363
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 }
377
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 }
390
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 }
403
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 ) {
411
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 );
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 }
426
427
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 );
442
443 return sprintf( "%.{$decimals}f", $bytes / pow( 1024, $factor ) ) . @$size[ $factor ];
444 }
445
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 }
460
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 }
518
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 }
539
540 $output = array();
541 foreach ( $arr1 as $el ) {
542 $output[] = $el;
543 }
544 foreach ( $arr2 as $el ) {
545 $output[] = $el;
546 }
547
548 return $output;
549 }
550
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' );
563
564 if ( ! $success ) {
565 $success = update_option( $option_name, $option_value );
566 }
567
568 return $success;
569 }
570
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 }
588
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 }
602
603 return $text;
604 }
605
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 }
619
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 }
636
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 }
650
651
652
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 }
669
670 if ( 'note' === $type ) {
671
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 );
688
689 if ( is_array( $more_allowed ) && ! empty( $more_allowed ) ) {
690 $allowed_html = array_merge( $allowed_html, $more_allowed );
691 }
692
693 $content = wp_kses( $content, $allowed_html );
694
695 } elseif ( 'mixed' === $type ) {
696
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 );
789
790 if ( is_array( $more_allowed ) && ! empty( $more_allowed ) ) {
791 $allowed_html = array_merge( $allowed_html, $more_allowed );
792 }
793
794 $content = wp_kses( $content, $allowed_html );
795 } else {
796 $content = wp_kses_post( $content );
797 }
798
799 return $content;
800 }
801
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 }
820
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 }
830
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
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 }
862
863 return $output;
864 }
865
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 }
893
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 }
915
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 }
935
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 }
961
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 }
986
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
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 );
1023
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 );
1031
1032 foreach ( $sorter as $ii => $val ) {
1033 $ret[ $ii ] = $arr[ $ii ];
1034 }
1035
1036 // asign other items (without $keys).
1037 foreach ( $arr as $ii => $val ) {
1038 if ( ! isset( $val[ $key ] ) ) {
1039 $ret[ $ii ] = $val;
1040 }
1041 }
1042
1043 $arr = $ret;
1044 }
1045
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 }
1057
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 }
1075
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 }
1089
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 }
1108
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 ) {
1117
1118 if ( empty( $issue_counts ) ) {
1119 $issue_counts = array(
1120 'good' => 0,
1121 'recommended' => 0,
1122 'critical' => 0,
1123 );
1124 }
1125
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;
1128
1129 if ( empty( $totalTests ) ) {
1130 $val = 100;
1131 } else {
1132 $val = 100 - ceil( ( $failedTests / $totalTests ) * 100 );
1133 }
1134
1135 if ( 0 > $val ) {
1136 $val = 0;
1137 }
1138
1139 if ( 100 < $val ) {
1140 $val = 100;
1141 }
1142
1143 return array(
1144 'val' => $val,
1145 'critical' => $issue_counts['critical'],
1146 );
1147 }
1148
1149
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 ) {
1158
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 }
1712 }
1713