DataInconsistency
2 weeks ago
License
2 months ago
Notices
2 months ago
pQuery
2 months ago
APIPermissionHelper.php
1 year ago
CdnAssetUrl.php
3 years ago
ConflictResolver.php
1 month ago
Cookies.php
2 months ago
DBCollationChecker.php
2 months ago
DOM.php
2 years ago
DateConverter.php
3 years ago
FreeDomains.php
3 years ago
Headers.php
1 year ago
Helpers.php
1 month ago
Installation.php
1 year ago
LegacyDatabase.php
1 year ago
Request.php
2 months ago
SecondLevelDomainNames.php
3 years ago
Security.php
2 months ago
ThirdPartyOutput.php
1 month ago
Url.php
2 months ago
index.php
3 years ago
Helpers.php
150 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Util; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | class Helpers { |
| 9 | const DIVIDER = '***MailPoet***'; |
| 10 | const LINK_TAG = 'link'; |
| 11 | |
| 12 | public static function isJson($string) { |
| 13 | if (!is_string($string)) return false; |
| 14 | json_decode($string); |
| 15 | return json_last_error() == JSON_ERROR_NONE; |
| 16 | } |
| 17 | |
| 18 | public static function replaceLinkTags( |
| 19 | string $source, |
| 20 | string $link, |
| 21 | array $attributes = [], |
| 22 | string $linkTag = self::LINK_TAG |
| 23 | ) { |
| 24 | if (empty($link)) return $source; |
| 25 | |
| 26 | $attributes = array_map(function($key) use ($attributes) { |
| 27 | return sprintf('%s="%s"', $key, $attributes[$key]); |
| 28 | }, array_keys($attributes)); |
| 29 | $source = str_replace( |
| 30 | '[' . $linkTag . ']', |
| 31 | sprintf( |
| 32 | '<a %s href="%s">', |
| 33 | join(' ', $attributes), |
| 34 | $link |
| 35 | ), |
| 36 | $source |
| 37 | ); |
| 38 | $source = str_replace('[/' . $linkTag . ']', '</a>', $source); |
| 39 | return preg_replace('/\s+/', ' ', $source); |
| 40 | } |
| 41 | |
| 42 | public static function getMaxPostSize($bytes = false) { |
| 43 | $maxPostSize = ini_get('post_max_size'); |
| 44 | if (!$bytes) return $maxPostSize; |
| 45 | if ($maxPostSize === false) { |
| 46 | return 0; |
| 47 | } |
| 48 | switch (substr($maxPostSize, -1)) { |
| 49 | case 'M': |
| 50 | case 'm': |
| 51 | return (int)$maxPostSize * 1048576; |
| 52 | case 'K': |
| 53 | case 'k': |
| 54 | return (int)$maxPostSize * 1024; |
| 55 | case 'G': |
| 56 | case 'g': |
| 57 | return (int)$maxPostSize * 1073741824; |
| 58 | default: |
| 59 | return $maxPostSize; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | public static function flattenArray($array) { |
| 64 | if (!$array) return; |
| 65 | $flattenedArray = []; |
| 66 | array_walk_recursive($array, function ($a) use (&$flattenedArray) { |
| 67 | $flattenedArray[] = $a; |
| 68 | }); |
| 69 | return $flattenedArray; |
| 70 | } |
| 71 | |
| 72 | public static function underscoreToCamelCase($str, $capitaliseFirstChar = false) { |
| 73 | if ($capitaliseFirstChar) { |
| 74 | $str[0] = strtoupper($str[0]); |
| 75 | } |
| 76 | return preg_replace_callback('/_([a-z])/', function ($c) { |
| 77 | return strtoupper($c[1]); |
| 78 | }, $str); |
| 79 | } |
| 80 | |
| 81 | public static function camelCaseToUnderscore($str) { |
| 82 | $str[0] = strtolower($str[0]); |
| 83 | return preg_replace_callback('/([A-Z])/', function ($c) { |
| 84 | return "_" . strtolower($c[1]); |
| 85 | }, $str); |
| 86 | } |
| 87 | |
| 88 | public static function camelCaseToKebabCase($str) { |
| 89 | $str[0] = strtolower($str[0]); |
| 90 | return preg_replace_callback('/([A-Z])/', function ($c) { |
| 91 | return "-" . strtolower($c[1]); |
| 92 | }, $str); |
| 93 | } |
| 94 | |
| 95 | public static function joinObject($object = []) { |
| 96 | return implode(self::DIVIDER, $object); |
| 97 | } |
| 98 | |
| 99 | public static function splitObject($object = []) { |
| 100 | return explode(self::DIVIDER, $object); |
| 101 | } |
| 102 | |
| 103 | public static function getIP() { |
| 104 | return (isset($_SERVER['REMOTE_ADDR']) && is_string($_SERVER['REMOTE_ADDR'])) |
| 105 | ? sanitize_text_field(wp_unslash($_SERVER['REMOTE_ADDR'])) |
| 106 | : null; |
| 107 | } |
| 108 | |
| 109 | public static function recursiveTrim($value) { |
| 110 | if (is_array($value)) |
| 111 | return array_map([__CLASS__, 'recursiveTrim'], $value); |
| 112 | if (is_string($value)) |
| 113 | return trim($value); |
| 114 | return $value; |
| 115 | } |
| 116 | |
| 117 | public static function escapeSearch(string $search): string { |
| 118 | return str_replace(['\\', '%', '_'], ['\\\\', '\\%', '\\_'], trim($search)); // escape for 'LIKE' |
| 119 | } |
| 120 | |
| 121 | public static function extractEmailDomain(string $email = ''): string { |
| 122 | $arrayOfItems = explode('@', trim($email)); |
| 123 | return strtolower(array_pop($arrayOfItems)); |
| 124 | } |
| 125 | |
| 126 | public static function mySqlGoneAwayExceptionHandler(\Throwable $err): string { |
| 127 | $errorMessage = $err->getMessage() ? $err->getMessage() : ''; |
| 128 | $mySqlGoneAwayCheck = strpos(strtolower($errorMessage), 'mysql server has gone away') !== false; |
| 129 | |
| 130 | if ($mySqlGoneAwayCheck) { |
| 131 | $customErrorMessage = sprintf( |
| 132 | // translators: the %1$s is the link, the %2$s is the error message. |
| 133 | __('Please see %1$s for more information. %2$s.', 'mailpoet'), |
| 134 | 'https://kb.mailpoet.com/article/307-how-to-fix-general-error-2006-mysql-server-has-gone-away', |
| 135 | $errorMessage |
| 136 | ); |
| 137 | // logging to the php log |
| 138 | if (function_exists('error_log')) { |
| 139 | // phpcs:disable QITStandard.PHP.DebugCode.DebugFunctionFound |
| 140 | error_log($customErrorMessage); // phpcs:ignore Squiz.PHP.DiscouragedFunctions |
| 141 | // phpcs:enable QITStandard.PHP.DebugCode.DebugFunctionFound |
| 142 | } |
| 143 | |
| 144 | return $customErrorMessage; |
| 145 | } |
| 146 | |
| 147 | return ''; |
| 148 | } |
| 149 | } |
| 150 |