ContentManager
3 weeks ago
Frontend
3 days ago
KirkiComments
3 weeks ago
Media.php
3 weeks ago
Utils.php
3 weeks ago
Utils.php
154 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Media Utils |
| 4 | * |
| 5 | * @package kirki |
| 6 | */ |
| 7 | |
| 8 | namespace Kirki\API; |
| 9 | |
| 10 | if (!defined('ABSPATH')) { |
| 11 | exit; // Exit if accessed directly. |
| 12 | } |
| 13 | |
| 14 | // @todo: remove this file. it has no use |
| 15 | |
| 16 | /** |
| 17 | * Media Utils class |
| 18 | */ |
| 19 | class Utils |
| 20 | { |
| 21 | |
| 22 | |
| 23 | /** |
| 24 | * Convert to bytes |
| 25 | * |
| 26 | * @param int $value The value to converted to bytes. |
| 27 | * @param string $type The type from which the value will be converted. |
| 28 | * |
| 29 | * @return int The converted bytes equivalent value. |
| 30 | */ |
| 31 | public static function to_bytes(int $value, $type = 'MB') |
| 32 | { |
| 33 | $multipliers = array( |
| 34 | 'gb' => 1073741824, // 1024 * 1024 * 1024 |
| 35 | 'mb' => 1048576, // 1024 * 1024 |
| 36 | 'kb' => 1024, // 1024 |
| 37 | ); |
| 38 | |
| 39 | $type = strtolower($type); |
| 40 | |
| 41 | return $value * $multipliers[$type]; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Recursively search a file |
| 46 | * |
| 47 | * @param string $directory_path The path of file directory. |
| 48 | * @param array $icon_libraries The icon_libraries to search. |
| 49 | * @return string The generated directory. |
| 50 | */ |
| 51 | public static function search_file(string $directory_path, array $icon_libraries): array |
| 52 | { |
| 53 | $it = new \RecursiveDirectoryIterator($directory_path); |
| 54 | |
| 55 | foreach ($icon_libraries as $icon_name => $icon_data) { |
| 56 | $required_files = $icon_data['requiredFiles']; |
| 57 | $css_file = $icon_data['cssFile']; |
| 58 | $css_file_path = ''; |
| 59 | $files_found = array(); |
| 60 | |
| 61 | foreach (new \RecursiveIteratorIterator($it) as $file) { |
| 62 | $arr = explode('/', $file); |
| 63 | $filename = array_pop($arr); |
| 64 | |
| 65 | if (in_array(strtolower($filename), $required_files, true)) { |
| 66 | $files_found[] = strtolower($filename); |
| 67 | |
| 68 | if (strtolower($filename) === $css_file) { |
| 69 | $css_file_path = $file; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | if (count(array_unique($files_found)) === count($required_files)) { |
| 75 | return array( |
| 76 | 'icon_name' => $icon_name, |
| 77 | 'css_file_path' => $css_file_path, |
| 78 | ); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return array( |
| 83 | 'icon_name' => '', |
| 84 | 'css_file_path' => '', |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Parse css classes |
| 90 | * |
| 91 | * @param string $css The css string. |
| 92 | * @param array $library The css string. |
| 93 | * @return array The parsed css classes array. |
| 94 | */ |
| 95 | public static function parse_css_classes($css, $library) |
| 96 | { |
| 97 | $classes = array(); |
| 98 | $math_all_css_pattern = '/([^\{\}]+)\{([^\}]*)\}|([\/\*])/ims'; |
| 99 | preg_match_all($math_all_css_pattern, $css, $match_css); |
| 100 | |
| 101 | foreach ($match_css[0] as $key => $value) { |
| 102 | $library_prefix = $library['prefix']; |
| 103 | $library_postfix = 'before{content'; |
| 104 | $css_match_regex = '/^(.' . $library_prefix . '-)[^.]+[(:)]+(' . $library_postfix . ').*$/m'; |
| 105 | $selector = trim($match_css[0][$key]); |
| 106 | |
| 107 | if (preg_match($css_match_regex, $selector)) { |
| 108 | $trimmed_cls = ''; |
| 109 | |
| 110 | $last_pattern = '/[(:)]+(' . $library_postfix . ').*$/m'; |
| 111 | $first_pattern = '/^.*\./'; |
| 112 | $trimmed_cls = preg_replace($last_pattern, '', $selector); |
| 113 | $trimmed_cls = preg_replace($first_pattern, '', $trimmed_cls); |
| 114 | $trimmed_cls = $library['leadClass'] ? "{$library['leadClass']} {$trimmed_cls}" : $trimmed_cls; |
| 115 | |
| 116 | if ($trimmed_cls) { |
| 117 | $classes[] = $trimmed_cls; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return $classes; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Minify css |
| 127 | * |
| 128 | * @param string $content css content. |
| 129 | * |
| 130 | * @return string minify css. |
| 131 | */ |
| 132 | public static function minify_css($content) |
| 133 | { |
| 134 | $content = preg_replace('/\/\*(?:(?!\*\/)[\s\S])*\*\/|[\r\n\t]+/', '', $content); |
| 135 | $content = preg_replace('/ {2,}/', ' ', $content); |
| 136 | $content = preg_replace('/ ([{:}]) /', '$1', $content); |
| 137 | $content = preg_replace('/([;,]) /', '$1', $content); |
| 138 | $content = preg_replace('/ !/', '!', $content); |
| 139 | |
| 140 | return $content; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Extract zip file |
| 145 | * |
| 146 | * @param string $path zip file path. |
| 147 | * |
| 148 | * @return void |
| 149 | */ |
| 150 | public static function extract_zip_file($path) |
| 151 | { |
| 152 | } |
| 153 | } |
| 154 |