| 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 -- 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 |
* 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 |
} |
| 37 |
|
| 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 |
} |
| 51 |
|
| 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 |
} |
| 67 |
|
| 68 |
return ( substr( $haystack, - $length ) === $needle ); |
| 69 |
} |
| 70 |
|
| 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; |
| 83 |
|
| 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 |
} |
| 97 |
|
| 98 |
if ( self::ends_with( $url, '/' ) ) { |
| 99 |
if ( ! $showHttp ) { |
| 100 |
$url = substr( $url, 0, strlen( $url ) - 1 ); |
| 101 |
} |
| 102 |
} else { |
| 103 |
$url = $url . '/'; |
| 104 |
} |
| 105 |
|
| 106 |
return $url; |
| 107 |
} |
| 108 |
|
| 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 |
} |
| 121 |
|
| 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 |
} |
| 134 |
|
| 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 |
} |
| 177 |
|
| 178 |
return $sorted; |
| 179 |
} |
| 180 |
|
| 181 |
return $sorted; |
| 182 |
} |
| 183 |
|
| 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 |
} |
| 204 |
|
| 205 |
return $output; |
| 206 |
} |
| 207 |
|
| 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 |
} |
| 220 |
|
| 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 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Method end_session() |
| 236 |
* |
| 237 |
* End a session. |
| 238 |
* |
| 239 |
* @return void |
| 240 |
*/ |
| 241 |
public static function end_session() { |
| 242 |
session_write_close(); |
| 243 |
if ( 0 < ob_get_length() ) { |
| 244 |
ob_end_flush(); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Method get_timestamp() |
| 250 |
* |
| 251 |
* Get time stamp in gmt_offset. |
| 252 |
* |
| 253 |
* @param mixed $timestamp Time stamp to convert. |
| 254 |
* |
| 255 |
* @return string Time stamp in general mountain time offset. |
| 256 |
*/ |
| 257 |
public static function get_timestamp( $timestamp ) { |
| 258 |
$gmtOffset = get_option( 'gmt_offset' ); |
| 259 |
|
| 260 |
return ( $gmtOffset ? ( $gmtOffset * HOUR_IN_SECONDS ) + $timestamp : $timestamp ); |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Method date() |
| 265 |
* |
| 266 |
* Show date in given format. |
| 267 |
* |
| 268 |
* @param mixed $format Format to display date in. |
| 269 |
* |
| 270 |
* @return string Date. |
| 271 |
*/ |
| 272 |
public static function date( $format ) { |
| 273 |
// phpcs:ignore -- use local date function. |
| 274 |
return date( $format, self::get_timestamp( time() ) ); |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Method format_timestamp() |
| 279 |
* |
| 280 |
* Format the given timestamp. |
| 281 |
* |
| 282 |
* @param mixed $timestamp Timestamp to format. |
| 283 |
* |
| 284 |
* @return string Formatted timestamp. |
| 285 |
*/ |
| 286 |
public static function format_timestamp( $timestamp ) { |
| 287 |
return date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $timestamp ); |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Method human_filesize() |
| 292 |
* |
| 293 |
* Convert to human readable file size format, |
| 294 |
* (B|kB|MB|GB|TB|PB|EB|ZB|YB). |
| 295 |
* |
| 296 |
* @param mixed $bytes File in bytes. |
| 297 |
* @param integer $decimals Number of decimals to output. |
| 298 |
* |
| 299 |
* @return string Human readable file size. |
| 300 |
*/ |
| 301 |
public static function human_filesize( $bytes, $decimals = 2 ) { |
| 302 |
$size = array( 'B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' ); |
| 303 |
$factor = floor( ( strlen( $bytes ) - 1 ) / 3 ); |
| 304 |
|
| 305 |
return sprintf( "%.{$decimals}f", $bytes / pow( 1024, $factor ) ) . @$size[ $factor ]; |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Method map_site() |
| 310 |
* |
| 311 |
* Map Site. |
| 312 |
* |
| 313 |
* @param mixed $website Website to map. |
| 314 |
* @param mixed $keys Keys to map. |
| 315 |
* @param bool $object_output Output format array|object. |
| 316 |
* |
| 317 |
* @return object $outputSite Mapped site. |
| 318 |
*/ |
| 319 |
public static function map_site( &$website, $keys, $object_output = true ) { |
| 320 |
$outputSite = array(); |
| 321 |
foreach ( $keys as $key ) { |
| 322 |
$outputSite[ $key ] = $website->$key; |
| 323 |
} |
| 324 |
|
| 325 |
if ( $object_output ) { |
| 326 |
return (object) $outputSite; |
| 327 |
} else { |
| 328 |
return $outputSite; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Method array_merge() |
| 334 |
* |
| 335 |
* Merge two given arrays into one. |
| 336 |
* |
| 337 |
* @param mixed $arr1 First array. |
| 338 |
* @param mixed $arr2 Second array. |
| 339 |
* |
| 340 |
* @return array Merged Array. |
| 341 |
*/ |
| 342 |
public static function array_merge( $arr1, $arr2 ) { |
| 343 |
if ( ! is_array( $arr1 ) && ! is_array( $arr2 ) ) { |
| 344 |
return array(); |
| 345 |
} |
| 346 |
if ( ! is_array( $arr1 ) ) { |
| 347 |
return $arr2; |
| 348 |
} |
| 349 |
if ( ! is_array( $arr2 ) ) { |
| 350 |
return $arr1; |
| 351 |
} |
| 352 |
|
| 353 |
$output = array(); |
| 354 |
foreach ( $arr1 as $el ) { |
| 355 |
$output[] = $el; |
| 356 |
} |
| 357 |
foreach ( $arr2 as $el ) { |
| 358 |
$output[] = $el; |
| 359 |
} |
| 360 |
|
| 361 |
return $output; |
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* Method update_option() |
| 366 |
* |
| 367 |
* Update option. |
| 368 |
* |
| 369 |
* @param mixed $option_name Option name. |
| 370 |
* @param mixed $option_value Option value. |
| 371 |
* |
| 372 |
* @return (boolean) False if value was not updated and true if value was updated. |
| 373 |
*/ |
| 374 |
public static function update_option( $option_name, $option_value ) { |
| 375 |
$success = add_option( $option_name, $option_value, '', 'no' ); |
| 376 |
|
| 377 |
if ( ! $success ) { |
| 378 |
$success = update_option( $option_name, $option_value ); |
| 379 |
} |
| 380 |
|
| 381 |
return $success; |
| 382 |
} |
| 383 |
|
| 384 |
/** |
| 385 |
* Method remove_preslash_spaces() |
| 386 |
* |
| 387 |
* Remove spaces before slashes. |
| 388 |
* |
| 389 |
* @param string $text String to strip. |
| 390 |
* |
| 391 |
* @return string $text Cleaned string. |
| 392 |
*/ |
| 393 |
public static function remove_preslash_spaces( $text ) { |
| 394 |
while ( stristr( $text, ' /' ) ) { |
| 395 |
$text = str_replace( ' /', '/', $text ); |
| 396 |
} |
| 397 |
|
| 398 |
return $text; |
| 399 |
} |
| 400 |
|
| 401 |
/** |
| 402 |
* Method remove_http_prefix() |
| 403 |
* |
| 404 |
* Remove http prefixes from given url. |
| 405 |
* |
| 406 |
* @param mixed $pUrl Given URL. |
| 407 |
* @param bool $pTrimSlashes Whether or not to trim slashes. Default is false. |
| 408 |
* |
| 409 |
* @return string Trimmed URL. |
| 410 |
*/ |
| 411 |
public static function remove_http_prefix( $pUrl, $pTrimSlashes = false ) { |
| 412 |
return str_replace( array( 'http:' . ( $pTrimSlashes ? '//' : '' ), 'https:' . ( $pTrimSlashes ? '//' : '' ) ), array( '', '' ), $pUrl ); |
| 413 |
} |
| 414 |
|
| 415 |
/** |
| 416 |
* Method remove_http_www_prefix() |
| 417 |
* |
| 418 |
* Remove 'www.' from given URL. |
| 419 |
* |
| 420 |
* @param mixed $pUrl Given URL. |
| 421 |
* |
| 422 |
* @return string Cleaned URL. |
| 423 |
*/ |
| 424 |
public static function remove_http_www_prefix( $pUrl ) { |
| 425 |
$pUrl = self::remove_http_prefix( $pUrl, true ); |
| 426 |
return str_replace( 'www', '', $pUrl ); |
| 427 |
} |
| 428 |
|
| 429 |
/** |
| 430 |
* Method sanitize_file_name() |
| 431 |
* |
| 432 |
* Sanitize file names. |
| 433 |
* |
| 434 |
* @param mixed $filename File name to sanitize. |
| 435 |
* |
| 436 |
* @return string Sanitized filename. |
| 437 |
*/ |
| 438 |
public static function sanitize_file_name( $filename ) { |
| 439 |
$filename = str_replace( array( '|', '/', '\\', ' ', ':' ), array( '-', '-', '-', '-', '-' ), $filename ); |
| 440 |
|
| 441 |
return sanitize_file_name( $filename ); |
| 442 |
} |
| 443 |
|
| 444 |
/** |
| 445 |
* Method normalize_filename() |
| 446 |
* |
| 447 |
* Normalize filename. |
| 448 |
* |
| 449 |
* @param mixed $s Filename to normalize. |
| 450 |
* |
| 451 |
* @return string $s Normalised filename. |
| 452 |
*/ |
| 453 |
public static function normalize_filename( $s ) { |
| 454 |
$s = preg_replace( '@\x{00c4}@u', 'A', $s ); |
| 455 |
$s = preg_replace( '@\x{00d6}@u', 'O', $s ); |
| 456 |
$s = preg_replace( '@\x{00dc}@u', 'U', $s ); |
| 457 |
$s = preg_replace( '@\x{00cb}@u', 'E', $s ); |
| 458 |
$s = preg_replace( '@\x{00e4}@u', 'a', $s ); |
| 459 |
$s = preg_replace( '@\x{00f6}@u', 'o', $s ); |
| 460 |
$s = preg_replace( '@\x{00fc}@u', 'u', $s ); |
| 461 |
$s = preg_replace( '@\x{00eb}@u', 'e', $s ); |
| 462 |
$s = preg_replace( '@\x{00f1}@u', 'n', $s ); |
| 463 |
$s = preg_replace( '@\x{00ff}@u', 'y', $s ); |
| 464 |
return $s; |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
/** |
| 469 |
* Method esc_content() |
| 470 |
* |
| 471 |
* Escape content, |
| 472 |
* allowed content (a,href,title,br,em,strong,p,hr,ul,ol,li,h1,h2 ... ). |
| 473 |
* |
| 474 |
* @param mixed $content Content to escape. |
| 475 |
* @param string $type Type of content. Default = note. |
| 476 |
* @param mixed $more_allowed input allowed tags - options. |
| 477 |
* |
| 478 |
* @return string Filtered content containing only the allowed HTML. |
| 479 |
*/ |
| 480 |
public static function esc_content( $content, $type = 'note', $more_allowed = array() ) { |
| 481 |
if ( 'note' === $type ) { |
| 482 |
|
| 483 |
$allowed_html = array( |
| 484 |
'a' => array( |
| 485 |
'href' => array(), |
| 486 |
'title' => array(), |
| 487 |
), |
| 488 |
'br' => array(), |
| 489 |
'em' => array(), |
| 490 |
'strong' => array(), |
| 491 |
'p' => array(), |
| 492 |
'hr' => array(), |
| 493 |
'ul' => array(), |
| 494 |
'ol' => array(), |
| 495 |
'li' => array(), |
| 496 |
'h1' => array(), |
| 497 |
'h2' => array(), |
| 498 |
); |
| 499 |
|
| 500 |
$content = wp_kses( $content, $allowed_html ); |
| 501 |
|
| 502 |
} elseif ( 'mixed' == $type ) { |
| 503 |
|
| 504 |
$allowed_html = array( |
| 505 |
'a' => array( |
| 506 |
'href' => array(), |
| 507 |
'title' => array(), |
| 508 |
'class' => array(), |
| 509 |
'onclick' => array(), |
| 510 |
), |
| 511 |
'br' => array(), |
| 512 |
'em' => array(), |
| 513 |
'strong' => array(), |
| 514 |
'p' => array(), |
| 515 |
'hr' => array(), |
| 516 |
'ul' => array( |
| 517 |
'style' => array(), |
| 518 |
), |
| 519 |
'ol' => array(), |
| 520 |
'li' => array(), |
| 521 |
'h1' => array(), |
| 522 |
'h2' => array(), |
| 523 |
'head' => array(), |
| 524 |
'html' => array( |
| 525 |
'lang' => array(), |
| 526 |
), |
| 527 |
'meta' => array( |
| 528 |
'name' => array(), |
| 529 |
'http-equiv' => array(), |
| 530 |
'content' => array(), |
| 531 |
'charset' => array(), |
| 532 |
), |
| 533 |
'title' => array(), |
| 534 |
'body' => array( |
| 535 |
'style' => array(), |
| 536 |
), |
| 537 |
'span' => array( |
| 538 |
'id' => array(), |
| 539 |
'style' => array(), |
| 540 |
'class' => array(), |
| 541 |
), |
| 542 |
'form' => array( |
| 543 |
'id' => array(), |
| 544 |
'method' => array(), |
| 545 |
'action' => array(), |
| 546 |
'onsubmit' => array(), |
| 547 |
), |
| 548 |
'table' => array( |
| 549 |
'class' => array(), |
| 550 |
), |
| 551 |
'thead' => array( |
| 552 |
'class' => array(), |
| 553 |
), |
| 554 |
'tbody' => array( |
| 555 |
'class' => array(), |
| 556 |
), |
| 557 |
'tr' => array( |
| 558 |
'id' => array(), |
| 559 |
), |
| 560 |
'td' => array( |
| 561 |
'class' => array(), |
| 562 |
), |
| 563 |
'div' => array( |
| 564 |
'id' => array(), |
| 565 |
'style' => array(), |
| 566 |
'class' => array(), |
| 567 |
), |
| 568 |
'input' => array( |
| 569 |
'type' => array(), |
| 570 |
'name' => array(), |
| 571 |
'class' => array(), |
| 572 |
'value' => array(), |
| 573 |
'onclick' => array(), |
| 574 |
), |
| 575 |
'button' => array( |
| 576 |
'type' => array(), |
| 577 |
'name' => array(), |
| 578 |
'value' => array(), |
| 579 |
'class' => array(), |
| 580 |
'title' => array(), |
| 581 |
'onclick' => array(), |
| 582 |
), |
| 583 |
); |
| 584 |
|
| 585 |
if ( is_array( $more_allowed ) && ! empty( $more_allowed ) ) { |
| 586 |
$allowed_html = array_merge( $allowed_html, $more_allowed ); |
| 587 |
} |
| 588 |
|
| 589 |
$content = wp_kses( $content, $allowed_html ); |
| 590 |
} else { |
| 591 |
$content = wp_kses_post( $content ); |
| 592 |
} |
| 593 |
|
| 594 |
return $content; |
| 595 |
} |
| 596 |
|
| 597 |
/** |
| 598 |
* Method esc_mixed_content() |
| 599 |
* |
| 600 |
* Escape mixed content, |
| 601 |
* allowed content (a,href,title,br,em,strong,p,hr,ul,ol,li,h1,h2 ... ). |
| 602 |
* |
| 603 |
* @param mixed $data data to escape. |
| 604 |
* @param string $depth Maximum depth to walk through $data. Must be greater than 0. |
| 605 |
* @param mixed $more_allowed input allowed tags - options. |
| 606 |
* |
| 607 |
* @throws \Exception Excetpion message. |
| 608 |
* |
| 609 |
* @return string Filtered content containing only the allowed HTML. |
| 610 |
*/ |
| 611 |
public static function esc_mixed_content( $data, $depth, $more_allowed = array() ) { |
| 612 |
if ( $depth < 0 ) { |
| 613 |
throw new \Exception( 'Reached depth limit' ); |
| 614 |
} |
| 615 |
|
| 616 |
if ( is_array( $data ) ) { |
| 617 |
$output = array(); |
| 618 |
foreach ( $data as $id => $el ) { |
| 619 |
// Don't forget to sanitize the ID! |
| 620 |
if ( is_string( $id ) ) { |
| 621 |
$clean_id = self::esc_content( $id, 'mixed', $more_allowed ); |
| 622 |
} else { |
| 623 |
$clean_id = $id; |
| 624 |
} |
| 625 |
|
| 626 |
// Check the element type, so that we're only recursing if we really have to. |
| 627 |
if ( is_array( $el ) || is_object( $el ) ) { |
| 628 |
$output[ $clean_id ] = self::esc_mixed_content( $el, $depth - 1 ); |
| 629 |
} elseif ( is_string( $el ) ) { |
| 630 |
$output[ $clean_id ] = self::esc_content( $el, 'mixed', $more_allowed ); |
| 631 |
} else { |
| 632 |
$output[ $clean_id ] = $el; |
| 633 |
} |
| 634 |
} |
| 635 |
} elseif ( is_object( $data ) ) { |
| 636 |
$output = new stdClass(); |
| 637 |
foreach ( $data as $id => $el ) { |
| 638 |
if ( is_string( $id ) ) { |
| 639 |
$clean_id = self::esc_content( $id, 'mixed', $more_allowed ); |
| 640 |
} else { |
| 641 |
$clean_id = $id; |
| 642 |
} |
| 643 |
|
| 644 |
if ( is_array( $el ) || is_object( $el ) ) { |
| 645 |
$output->$clean_id = self::esc_mixed_content( $el, $depth - 1, $more_allowed ); |
| 646 |
} elseif ( is_string( $el ) ) { |
| 647 |
$output->$clean_id = self::esc_content( $el, 'mixed', $more_allowed ); |
| 648 |
} else { |
| 649 |
$output->$clean_id = $el; |
| 650 |
} |
| 651 |
} |
| 652 |
} elseif ( is_string( $data ) ) { |
| 653 |
return self::esc_content( $data, 'mixed', $more_allowed ); |
| 654 |
} else { |
| 655 |
return $data; |
| 656 |
} |
| 657 |
|
| 658 |
return $output; |
| 659 |
} |
| 660 |
|
| 661 |
/** |
| 662 |
* Method show_mainwp_message() |
| 663 |
* |
| 664 |
* Check whenther or not to show the MainWP Message. |
| 665 |
* |
| 666 |
* @param mixed $type Type of message. |
| 667 |
* @param mixed $notice_id Notice ID. |
| 668 |
* |
| 669 |
* @return boolean true|false. |
| 670 |
*/ |
| 671 |
public static function show_mainwp_message( $type, $notice_id ) { |
| 672 |
$status = get_user_option( 'mainwp_notice_saved_status' ); |
| 673 |
if ( ! is_array( $status ) ) { |
| 674 |
$status = array(); |
| 675 |
} |
| 676 |
if ( isset( $status[ $notice_id ] ) ) { |
| 677 |
return false; |
| 678 |
} |
| 679 |
return true; |
| 680 |
} |
| 681 |
|
| 682 |
/** |
| 683 |
* Method array_sort() |
| 684 |
* |
| 685 |
* Sort given array by given flags. |
| 686 |
* |
| 687 |
* @param mixed $array Array to sort. |
| 688 |
* @param mixed $key Array key. |
| 689 |
* @param string $sort_flag Flags to sort by. Default = SORT_STRING. |
| 690 |
*/ |
| 691 |
public static function array_sort( &$array, $key, $sort_flag = SORT_STRING ) { |
| 692 |
$sorter = array(); |
| 693 |
$ret = array(); |
| 694 |
reset( $array ); |
| 695 |
foreach ( $array as $ii => $val ) { |
| 696 |
$sorter[ $ii ] = $val[ $key ]; |
| 697 |
} |
| 698 |
asort( $sorter, $sort_flag ); |
| 699 |
foreach ( $sorter as $ii => $val ) { |
| 700 |
$ret[ $ii ] = $array[ $ii ]; |
| 701 |
} |
| 702 |
$array = $ret; |
| 703 |
} |
| 704 |
|
| 705 |
/** |
| 706 |
* Method enabled_wp_seo() |
| 707 |
* |
| 708 |
* Check if Yoast SEO is enabled. |
| 709 |
* |
| 710 |
* @return boolean true|false. |
| 711 |
*/ |
| 712 |
public static function enabled_wp_seo() { |
| 713 |
if ( null === self::$enabled_wp_seo ) { |
| 714 |
self::$enabled_wp_seo = is_plugin_active( 'wordpress-seo-extension/wordpress-seo-extension.php' ); |
| 715 |
} |
| 716 |
return self::$enabled_wp_seo; |
| 717 |
} |
| 718 |
|
| 719 |
/** |
| 720 |
* Method value_to_string() |
| 721 |
* |
| 722 |
* Value to string. |
| 723 |
* |
| 724 |
* @param mixed $var Value to convert to string. |
| 725 |
* |
| 726 |
* @return string Value that has been converted into a string. |
| 727 |
*/ |
| 728 |
public static function value_to_string( $var ) { |
| 729 |
if ( is_array( $var ) || is_object( $var ) ) { |
| 730 |
//phpcs:ignore -- for debug only |
| 731 |
return print_r( $var, true ); // @codingStandardsIgnoreLine |
| 732 |
} elseif ( is_string( $var ) ) { |
| 733 |
return $var; |
| 734 |
} |
| 735 |
return ''; |
| 736 |
} |
| 737 |
|
| 738 |
/** |
| 739 |
* Method render_mainwp_nonce() |
| 740 |
* |
| 741 |
* Render MainWP nonce. |
| 742 |
*/ |
| 743 |
public static function render_mainwp_nonce() { |
| 744 |
wp_nonce_field( 'mainwp-admin-nonce' ); |
| 745 |
} |
| 746 |
|
| 747 |
|
| 748 |
/** |
| 749 |
* Get Health Site value. |
| 750 |
* |
| 751 |
* @param mixed $issue_counts Health site issues. |
| 752 |
* |
| 753 |
* @return array $array Health status value. |
| 754 |
*/ |
| 755 |
public static function get_site_health( $issue_counts ) { |
| 756 |
|
| 757 |
if ( empty( $issue_counts ) ) { |
| 758 |
$issue_counts = array( |
| 759 |
'good' => 0, |
| 760 |
'recommended' => 0, |
| 761 |
'critical' => 0, |
| 762 |
); |
| 763 |
} |
| 764 |
|
| 765 |
$totalTests = intval( $issue_counts['good'] ) + intval( $issue_counts['recommended'] ) + intval( $issue_counts['critical'] ) * 1.5; |
| 766 |
$failedTests = intval( $issue_counts['recommended'] ) * 0.5 + $issue_counts['critical'] * 1.5; |
| 767 |
|
| 768 |
if ( 0 == $totalTests ) { |
| 769 |
$val = 100; |
| 770 |
} else { |
| 771 |
$val = 100 - ceil( ( $failedTests / $totalTests ) * 100 ); |
| 772 |
} |
| 773 |
|
| 774 |
if ( 0 > $val ) { |
| 775 |
$val = 0; |
| 776 |
} |
| 777 |
|
| 778 |
if ( 100 < $val ) { |
| 779 |
$val = 100; |
| 780 |
} |
| 781 |
|
| 782 |
return array( |
| 783 |
'val' => $val, |
| 784 |
'critical' => $issue_counts['critical'], |
| 785 |
); |
| 786 |
} |
| 787 |
|
| 788 |
|
| 789 |
/** |
| 790 |
* Get HTTP code. |
| 791 |
* |
| 792 |
* @param int $code HTTP code. |
| 793 |
* |
| 794 |
* @return array $http_codes HTTP code. |
| 795 |
*/ |
| 796 |
public static function get_http_codes( $code = false ) { |
| 797 |
|
| 798 |
$http_codes = array( |
| 799 |
100 => 'Continue', |
| 800 |
101 => 'Switching Protocols', |
| 801 |
200 => 'OK', |
| 802 |
201 => 'Created', |
| 803 |
202 => 'Accepted', |
| 804 |
203 => 'Non-Authoritative Information', |
| 805 |
204 => 'No Content', |
| 806 |
205 => 'Reset Content', |
| 807 |
206 => 'Partial Content', |
| 808 |
300 => 'Multiple Choices', |
| 809 |
301 => 'Moved Permanently', |
| 810 |
302 => 'Found', |
| 811 |
303 => 'See Other', |
| 812 |
304 => 'Not Modified', |
| 813 |
305 => 'Use Proxy', |
| 814 |
306 => '(Unused)', |
| 815 |
307 => 'Temporary Redirect', |
| 816 |
400 => 'Bad Request', |
| 817 |
401 => 'Unauthorized', |
| 818 |
402 => 'Payment Required', |
| 819 |
403 => 'Forbidden', |
| 820 |
404 => 'Not Found', |
| 821 |
405 => 'Method Not Allowed', |
| 822 |
406 => 'Not Acceptable', |
| 823 |
407 => 'Proxy Authentication Required', |
| 824 |
408 => 'Request Timeout', |
| 825 |
409 => 'Conflict', |
| 826 |
410 => 'Gone', |
| 827 |
411 => 'Length Required', |
| 828 |
412 => 'Precondition Failed', |
| 829 |
413 => 'Request Entity Too Large', |
| 830 |
414 => 'Request-URI Too Long', |
| 831 |
415 => 'Unsupported Media Type', |
| 832 |
416 => 'Requested Range Not Satisfiable', |
| 833 |
417 => 'Expectation Failed', |
| 834 |
500 => 'Internal Server Error', |
| 835 |
501 => 'Not Implemented', |
| 836 |
502 => 'Bad Gateway', |
| 837 |
503 => 'Service Unavailable', |
| 838 |
504 => 'Gateway Timeout', |
| 839 |
505 => 'HTTP Version Not Supported', |
| 840 |
); |
| 841 |
|
| 842 |
if ( empty( $code ) ) { |
| 843 |
return $http_codes; |
| 844 |
} |
| 845 |
|
| 846 |
return isset( $http_codes[ $code ] ) ? $http_codes[ $code ] : ''; |
| 847 |
} |
| 848 |
|
| 849 |
/** |
| 850 |
* Method valid_input_emails(). |
| 851 |
* |
| 852 |
* @param string $emails Input emails string. |
| 853 |
* |
| 854 |
* @return string $valid_emails Valid emails string. |
| 855 |
*/ |
| 856 |
public static function valid_input_emails( $emails ) { |
| 857 |
|
| 858 |
if ( is_string( $emails ) ) { |
| 859 |
$emails = array_filter( explode( ',', $emails ) ); |
| 860 |
} |
| 861 |
|
| 862 |
$valid_emails = array(); |
| 863 |
if ( is_array( $emails ) ) { |
| 864 |
foreach ( $emails as $email ) { |
| 865 |
$email = esc_html( trim( $email ) ); |
| 866 |
if ( ! empty( $email ) && ! in_array( $email, $valid_emails, true ) ) { |
| 867 |
$valid_emails[] = $email; |
| 868 |
} |
| 869 |
} |
| 870 |
} |
| 871 |
$valid_emails = implode( ',', $valid_emails ); |
| 872 |
return $valid_emails; |
| 873 |
} |
| 874 |
} |
| 875 |
|