Block.php
2 years ago
BlockFinder.php
5 years ago
DynamicData.php
4 years ago
HasOneRelationship.php
5 years ago
Integration.php
5 years ago
Utility.php
4 years ago
Utility.php
227 lines
| 1 | <?php |
| 2 | |
| 3 | namespace PrestoPlayer\Support; |
| 4 | |
| 5 | class Utility |
| 6 | { |
| 7 | public static function sanitizeCSS($css) |
| 8 | { |
| 9 | return preg_match('#</?\w+#', $css) ? "" : $css; |
| 10 | } |
| 11 | |
| 12 | public static function insertAfterString($str, $search, $insert) |
| 13 | { |
| 14 | $index = strpos($str, $search); |
| 15 | if ($index === false) { |
| 16 | return $str; |
| 17 | } |
| 18 | return substr_replace($str, $search . $insert, $index, strlen($search)); |
| 19 | } |
| 20 | |
| 21 | public static function snakeToCamel($input) |
| 22 | { |
| 23 | return lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $input)))); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Convert a duration to human readable format. |
| 28 | * |
| 29 | * @since 5.1.0 |
| 30 | * |
| 31 | * @param string $duration Duration will be in string format (HH:ii:ss) OR (ii:ss), |
| 32 | * with a possible prepended negative sign (-). |
| 33 | * @return string|false A human readable duration string, false on failure. |
| 34 | */ |
| 35 | public static function human_readable_duration($duration = '') |
| 36 | { |
| 37 | if ((empty($duration) || !is_string($duration))) { |
| 38 | return __('0 seconds', 'presto-player'); |
| 39 | } |
| 40 | |
| 41 | $duration = trim($duration); |
| 42 | |
| 43 | // Remove prepended negative sign. |
| 44 | if ('-' === substr($duration, 0, 1)) { |
| 45 | $duration = substr($duration, 1); |
| 46 | } |
| 47 | |
| 48 | // Extract duration parts. |
| 49 | $duration_parts = array_reverse(explode(':', $duration)); |
| 50 | $duration_count = count($duration_parts); |
| 51 | |
| 52 | $hour = null; |
| 53 | $minute = null; |
| 54 | $second = null; |
| 55 | |
| 56 | if (3 === $duration_count) { |
| 57 | // Validate HH:ii:ss duration format. |
| 58 | if (!((bool) preg_match('/^([0-9]+):([0-5]?[0-9]):([0-5]?[0-9])$/', $duration))) { |
| 59 | return false; |
| 60 | } |
| 61 | // Three parts: hours, minutes & seconds. |
| 62 | list($second, $minute, $hour) = $duration_parts; |
| 63 | } elseif (2 === $duration_count) { |
| 64 | // Validate ii:ss duration format. |
| 65 | if (!((bool) preg_match('/^([0-5]?[0-9]):([0-5]?[0-9])$/', $duration))) { |
| 66 | return false; |
| 67 | } |
| 68 | // Two parts: minutes & seconds. |
| 69 | list($second, $minute) = $duration_parts; |
| 70 | } else { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | $human_readable_duration = array(); |
| 75 | |
| 76 | // Add the hour part to the string. |
| 77 | if (is_numeric($hour) && $hour > 0) { |
| 78 | /* translators: %s: Time duration in hour or hours. */ |
| 79 | $human_readable_duration[] = sprintf(_n('%s hour', '%s hours', $hour), (int) $hour); |
| 80 | } |
| 81 | |
| 82 | // Add the minute part to the string. |
| 83 | if (is_numeric($minute) && $minute > 0) { |
| 84 | /* translators: %s: Time duration in minute or minutes. */ |
| 85 | $human_readable_duration[] = sprintf(_n('%s minute', '%s minutes', $minute), (int) $minute); |
| 86 | } |
| 87 | |
| 88 | // Add the second part to the string. |
| 89 | if (is_numeric($second) && $second > 0) { |
| 90 | /* translators: %s: Time duration in second or seconds. */ |
| 91 | $human_readable_duration[] = sprintf(_n('%s second', '%s seconds', $second), (int) $second); |
| 92 | } |
| 93 | |
| 94 | return implode(', ', $human_readable_duration); |
| 95 | } |
| 96 | |
| 97 | public static function getIPAddress($ip_address = '') |
| 98 | { |
| 99 | $ip = $ip_address ? $ip_address : (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : ''); |
| 100 | |
| 101 | if (filter_var($ip, FILTER_VALIDATE_IP)) { |
| 102 | return $ip; |
| 103 | } else { |
| 104 | return ''; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Insert an array into another array before/after a certain key |
| 110 | * |
| 111 | * @param array $array The initial array |
| 112 | * @param array $pairs The array to insert |
| 113 | * @param string $key The certain key |
| 114 | * @param string $position Wether to insert the array before or after the key |
| 115 | * @return array |
| 116 | */ |
| 117 | public static function arrayInsert($array, $pairs, $key, $position = 'after') |
| 118 | { |
| 119 | $key_pos = array_search($key, array_keys($array)); |
| 120 | |
| 121 | if ('after' == $position) |
| 122 | $key_pos++; |
| 123 | |
| 124 | if (false !== $key_pos) { |
| 125 | $result = array_slice($array, 0, $key_pos); |
| 126 | $result = array_merge($result, $pairs); |
| 127 | $result = array_merge($result, array_slice($array, $key_pos)); |
| 128 | } else { |
| 129 | $result = array_merge($array, $pairs); |
| 130 | } |
| 131 | |
| 132 | return $result; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * Inserts a new key/value before the key in the array. |
| 137 | * |
| 138 | * @param $key The key to insert before. |
| 139 | * @param $array An array to insert in to. |
| 140 | * @param $new_key The key to insert. |
| 141 | * @param $new_value An value to insert. |
| 142 | * |
| 143 | * @return The new array if the key exists, FALSE otherwise. |
| 144 | * |
| 145 | */ |
| 146 | public static function arrayInsertBefore($key, array &$array, $new_key, $new_value) |
| 147 | { |
| 148 | if (array_key_exists($key, $array)) { |
| 149 | $new = array(); |
| 150 | foreach ($array as $k => $value) { |
| 151 | if ($k === $key) { |
| 152 | $new[$new_key] = $new_value; |
| 153 | } |
| 154 | $new[$k] = $value; |
| 155 | } |
| 156 | return $new; |
| 157 | } |
| 158 | return FALSE; |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * Inserts a new key/value after the key in the array. |
| 163 | * |
| 164 | * @param $key The key to insert after. |
| 165 | * @param $array An array to insert in to. |
| 166 | * @param $new_key The key to insert. |
| 167 | * @param $new_value An value to insert. |
| 168 | * |
| 169 | * @return The new array if the key exists, FALSE otherwise. |
| 170 | */ |
| 171 | public static function arrayInsertAfter($key, array &$array, $new_key, $new_value) |
| 172 | { |
| 173 | if (array_key_exists($key, $array)) { |
| 174 | $new = array(); |
| 175 | foreach ($array as $k => $value) { |
| 176 | $new[$k] = $value; |
| 177 | if ($k === $key) { |
| 178 | $new[$new_key] = $new_value; |
| 179 | } |
| 180 | } |
| 181 | return $new; |
| 182 | } |
| 183 | return FALSE; |
| 184 | } |
| 185 | |
| 186 | public static function hex2rgba($color, $opacity = false) |
| 187 | { |
| 188 | |
| 189 | $defaultColor = 'rgb(0,0,0)'; |
| 190 | |
| 191 | // Return default color if no color provided |
| 192 | if (empty($color)) { |
| 193 | return $defaultColor; |
| 194 | } |
| 195 | |
| 196 | // Ignore "#" if provided |
| 197 | if ($color[0] == '#') { |
| 198 | $color = substr($color, 1); |
| 199 | } |
| 200 | |
| 201 | // Check if color has 6 or 3 characters, get values |
| 202 | if (strlen($color) == 6) { |
| 203 | $hex = array($color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5]); |
| 204 | } elseif (strlen($color) == 3) { |
| 205 | $hex = array($color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2]); |
| 206 | } else { |
| 207 | return $default; |
| 208 | } |
| 209 | |
| 210 | // Convert hex values to rgb values |
| 211 | $rgb = array_map('hexdec', $hex); |
| 212 | |
| 213 | // Check if opacity is set(rgba or rgb) |
| 214 | if ($opacity) { |
| 215 | if (abs($opacity) > 1) { |
| 216 | $opacity = 1.0; |
| 217 | } |
| 218 | $output = 'rgba(' . implode(",", $rgb) . ',' . $opacity . ')'; |
| 219 | } else { |
| 220 | $output = 'rgb(' . implode(",", $rgb) . ')'; |
| 221 | } |
| 222 | |
| 223 | // Return rgb(a) color string |
| 224 | return $output; |
| 225 | } |
| 226 | } |
| 227 |