Nessuna descrizione

Helpers.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace MailPoet\Util;
  3. if (!defined('ABSPATH')) exit;
  4. class Helpers {
  5. const DIVIDER = '***MailPoet***';
  6. const LINK_TAG = 'link';
  7. public static function isJson($string) {
  8. if (!is_string($string)) return false;
  9. json_decode($string);
  10. return json_last_error() == JSON_ERROR_NONE;
  11. }
  12. public static function replaceLinkTags($source, $link = false, $attributes = [], $linkTag = false) {
  13. if (!$link) return $source;
  14. $linkTag = ($linkTag) ? $linkTag : self::LINK_TAG;
  15. $attributes = array_map(function($key) use ($attributes) {
  16. return sprintf('%s="%s"', $key, $attributes[$key]);
  17. }, array_keys($attributes));
  18. $source = str_replace(
  19. '[' . $linkTag . ']',
  20. sprintf(
  21. '<a %s href="%s">',
  22. join(' ', $attributes),
  23. $link
  24. ),
  25. $source
  26. );
  27. $source = str_replace('[/' . $linkTag . ']', '</a>', $source);
  28. return preg_replace('/\s+/', ' ', $source);
  29. }
  30. public static function getMaxPostSize($bytes = false) {
  31. $maxPostSize = ini_get('post_max_size');
  32. if (!$bytes) return $maxPostSize;
  33. if ($maxPostSize === false) {
  34. return 0;
  35. }
  36. switch (substr($maxPostSize, -1)) {
  37. case 'M':
  38. case 'm':
  39. return (int)$maxPostSize * 1048576;
  40. case 'K':
  41. case 'k':
  42. return (int)$maxPostSize * 1024;
  43. case 'G':
  44. case 'g':
  45. return (int)$maxPostSize * 1073741824;
  46. default:
  47. return $maxPostSize;
  48. }
  49. }
  50. public static function flattenArray($array) {
  51. if (!$array) return;
  52. $flattenedArray = [];
  53. array_walk_recursive($array, function ($a) use (&$flattenedArray) {
  54. $flattenedArray[] = $a;
  55. });
  56. return $flattenedArray;
  57. }
  58. public static function underscoreToCamelCase($str, $capitaliseFirstChar = false) {
  59. if ($capitaliseFirstChar) {
  60. $str[0] = strtoupper($str[0]);
  61. }
  62. return preg_replace_callback('/_([a-z])/', function ($c) {
  63. return strtoupper($c[1]);
  64. }, $str);
  65. }
  66. public static function camelCaseToUnderscore($str) {
  67. $str[0] = strtolower($str[0]);
  68. return preg_replace_callback('/([A-Z])/', function ($c) {
  69. return "_" . strtolower($c[1]);
  70. }, $str);
  71. }
  72. public static function joinObject($object = []) {
  73. return implode(self::DIVIDER, $object);
  74. }
  75. public static function splitObject($object = []) {
  76. return explode(self::DIVIDER, $object);
  77. }
  78. public static function getIP() {
  79. return (isset($_SERVER['REMOTE_ADDR']))
  80. ? $_SERVER['REMOTE_ADDR']
  81. : null;
  82. }
  83. public static function recursiveTrim($value) {
  84. if (is_array($value))
  85. return array_map([__CLASS__, 'recursiveTrim'], $value);
  86. if (is_string($value))
  87. return trim($value);
  88. return $value;
  89. }
  90. public static function escapeSearch(string $search): string {
  91. return str_replace(['\\', '%', '_'], ['\\\\', '\\%', '\\_'], trim($search)); // escape for 'LIKE'
  92. }
  93. }