tenweb-speed-optimizer
/
vendor
/
10web-utils
/
tenweb-image-optimizer
/
src
/
TenWebIO
Last commit date
Exceptions
4 years ago
Queue
2 years ago
Views
2 years ago
Api.php
2 years ago
Attachments.php
2 years ago
Backup.php
2 years ago
CLI.php
3 years ago
Compress.php
2 years ago
CompressDataService.php
3 years ago
CompressService.php
3 years ago
Config.php
2 years ago
Init.php
2 years ago
LastCompress.php
3 years ago
Logs.php
2 years ago
PreInit.php
3 years ago
Rest.php
2 years ago
Settings.php
3 years ago
Utils.php
1 year ago
Utils.php
261 lines
| 1 | <?php |
| 2 | namespace TenWebIO; |
| 3 | |
| 4 | class Utils |
| 5 | { |
| 6 | /** |
| 7 | * @return array |
| 8 | */ |
| 9 | public static function getAttachmentData($url) |
| 10 | { |
| 11 | $no_schema_url = str_replace(array('https://', 'http://', 'www.'), '', $url); |
| 12 | $no_schema_site_url = str_replace(array('https://', 'http://', 'www.'), '', site_url()); |
| 13 | |
| 14 | $absolute_path = rtrim(self::strReplaceFirstOccurrence($no_schema_site_url, rtrim(ABSPATH, '/'), $no_schema_url), '/'); |
| 15 | $absolute_url = rtrim(self::strReplaceFirstOccurrence(rtrim(ABSPATH, '/'), site_url(), $url), '/'); |
| 16 | |
| 17 | $base_name = rtrim(pathinfo($url, PATHINFO_BASENAME), '/'); |
| 18 | $destination = rtrim(pathinfo($absolute_path, PATHINFO_DIRNAME), '/'); |
| 19 | $extension = pathinfo($url, PATHINFO_EXTENSION); |
| 20 | |
| 21 | return [ |
| 22 | 'base_name' => $base_name, |
| 23 | 'destination' => $destination, |
| 24 | 'extension' => $extension, |
| 25 | 'absolute_url' => $absolute_url, |
| 26 | 'absolute_path' => $absolute_path, |
| 27 | ]; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @param $dir |
| 32 | * |
| 33 | * @return array |
| 34 | */ |
| 35 | public static function getFilesFromDir($dir) |
| 36 | { |
| 37 | $result = array(); |
| 38 | foreach (scandir($dir) as $value) { |
| 39 | if (!in_array($value, array(".", "..", ".original"))) { |
| 40 | if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) { |
| 41 | $result = array_merge($result, self::getFilesFromDir($dir . DIRECTORY_SEPARATOR . $value)); |
| 42 | } else { |
| 43 | $result[] = $dir . DIRECTORY_SEPARATOR . $value; |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | return $result; |
| 49 | } |
| 50 | |
| 51 | public static function removeDir($dir) |
| 52 | { |
| 53 | if (is_dir($dir)) { |
| 54 | foreach (scandir($dir) as $value) { |
| 55 | if (!in_array($value, array(".", "..", ".original"))) { |
| 56 | if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) |
| 57 | rrmdir($dir . DIRECTORY_SEPARATOR . $value); |
| 58 | else |
| 59 | unlink($dir . DIRECTORY_SEPARATOR . $value); |
| 60 | } |
| 61 | } |
| 62 | rmdir($dir); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @param $type |
| 68 | * @param bool $delete_all |
| 69 | * |
| 70 | * @return void |
| 71 | */ |
| 72 | public static function deleteQueueTransients($type, $delete_all = true) |
| 73 | { |
| 74 | $page_id = Utils::getCustomQueueIdByType($type); |
| 75 | delete_site_option(TENWEBIO_PREFIX . '_compress_images_count_' . $type); |
| 76 | delete_site_option(TENWEBIO_PREFIX . '_compress_images_counter_' . $type); |
| 77 | if ($delete_all) { |
| 78 | delete_site_option(TENWEBIO_PREFIX . '_custom_compress_only_convert_webp_' . $page_id); |
| 79 | delete_site_option(TENWEBIO_PREFIX . '_custom_compress_origin_' . $page_id); |
| 80 | delete_site_option(TENWEBIO_PREFIX . '_running_compress_' . $type); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @return void |
| 86 | */ |
| 87 | public static function clearSpeedCache() |
| 88 | { |
| 89 | if (class_exists('\OptimizerAdmin')) { |
| 90 | \OptimizerAdmin::clear_cache(false, true); |
| 91 | wp_remote_get(get_site_url(), array('method' => 'GET', 'sslverify' => false, 'timeout' => 0.1)); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @param $type |
| 97 | * @param bool $clear_booster_cache |
| 98 | * @param bool $retry_queue_chunks |
| 99 | * |
| 100 | * @return void |
| 101 | */ |
| 102 | public static function finishQueue($type, $clear_booster_cache = true, $retry_queue_chunks = true) |
| 103 | { |
| 104 | $queue_dir = Utils::getQueueDir($type); |
| 105 | if (is_dir($queue_dir)) { |
| 106 | Utils::removeDir($queue_dir); |
| 107 | } |
| 108 | Utils::deleteQueueTransients($type); |
| 109 | Settings::purgeCompressSettings(); |
| 110 | if ($clear_booster_cache) { |
| 111 | Utils::clearSpeedCache(); |
| 112 | } |
| 113 | /*$config = new Config(); |
| 114 | if ($type === 'bulk' && $retry_queue_chunks) { |
| 115 | self::compressBulkRequest(); |
| 116 | }*/ |
| 117 | } |
| 118 | |
| 119 | public static function compressBulkRequest() |
| 120 | { |
| 121 | $route = add_query_arg(array('rest_route' => '/tenwebio/v2/compress', 'chunk' => rand(1, 5000)), get_home_url() . "/"); |
| 122 | wp_remote_post($route, array('method' => 'POST', 'sslverify' => false, 'timeout' => 0.1, 'body' => array( |
| 123 | 'tenwebio_nonce' => wp_create_nonce('tenwebio_rest') |
| 124 | ))); |
| 125 | } |
| 126 | |
| 127 | public static function compressOneRequest($id) |
| 128 | { |
| 129 | $route = add_query_arg(array('rest_route' => '/tenwebio/v2/compress-one', 'single' => $id), get_home_url() . "/"); |
| 130 | wp_remote_post($route, array('method' => 'POST', 'sslverify' => false, 'timeout' => 0.1, 'body' => array( |
| 131 | "id" => $id, |
| 132 | 'tenwebio_nonce' => wp_create_nonce('tenwebio_rest') |
| 133 | ))); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @param $queue_name |
| 138 | * |
| 139 | * @return array|string|string[] |
| 140 | */ |
| 141 | public static function getQueueTypeByName($queue_name) |
| 142 | { |
| 143 | return str_replace(CompressService::QUEUE_NAME . '_', '', $queue_name); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * @param $queue_type |
| 148 | * |
| 149 | * @return int |
| 150 | */ |
| 151 | public static function getCustomQueueIdByType($queue_type) |
| 152 | { |
| 153 | return str_replace('custom_', '', $queue_type); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * @param $queue_type |
| 158 | * |
| 159 | * @return string |
| 160 | */ |
| 161 | public static function getQueueDir($queue_type) |
| 162 | { |
| 163 | $upload_dir = wp_get_upload_dir(); |
| 164 | $queue_dir = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . $queue_type; |
| 165 | if (!is_dir($queue_dir)) { |
| 166 | mkdir($queue_dir); |
| 167 | } |
| 168 | |
| 169 | return $queue_dir; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * @param $url |
| 174 | * |
| 175 | * @return void |
| 176 | */ |
| 177 | public static function storeWebPLog($url) |
| 178 | { |
| 179 | $data = get_option(TENWEBIO_PREFIX . '_webp_converted', array()); |
| 180 | $attachment_data = self::getAttachmentData($url); |
| 181 | $data[] = $attachment_data['absolute_path']; |
| 182 | update_option(TENWEBIO_PREFIX . '_webp_converted', $data, false); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * @return int |
| 187 | */ |
| 188 | public static function deleteWebPImages() |
| 189 | { |
| 190 | $data = get_option(TENWEBIO_PREFIX . '_webp_converted', array()); |
| 191 | $deleted_count = 0; |
| 192 | if (!empty($data)) { |
| 193 | foreach ($data as $image) { |
| 194 | $extension = pathinfo($image, PATHINFO_EXTENSION); |
| 195 | if (file_exists($image) && strtolower($extension) === 'webp') { |
| 196 | unlink($image); |
| 197 | $deleted_count++; |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | delete_option(TENWEBIO_PREFIX . '_webp_converted'); |
| 202 | |
| 203 | return $deleted_count; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * @param $search |
| 208 | * @param $replacement |
| 209 | * @param $src |
| 210 | * |
| 211 | * @return array|mixed|string|string[] |
| 212 | */ |
| 213 | public static function strReplaceFirstOccurrence($search, $replacement, $src) |
| 214 | { |
| 215 | return (false !== ($pos = strpos($src, $search))) ? substr_replace($src, $replacement, $pos, strlen($search)) : $src; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * @param $url |
| 220 | * @param $destination |
| 221 | * |
| 222 | * @return bool |
| 223 | */ |
| 224 | public static function downloadFile($url, $destination) |
| 225 | { |
| 226 | $response = wp_remote_get($url); |
| 227 | if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200 || empty($response['body'])) { |
| 228 | Logs::setLog("download:url:" . $url . ":error", json_encode($response), 'error'); |
| 229 | |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | return file_put_contents($destination, $response['body']); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * @param $data |
| 238 | * |
| 239 | * @return \Generator |
| 240 | */ |
| 241 | public static function getGenerator($data) |
| 242 | { |
| 243 | foreach ($data as $item) { |
| 244 | yield $item; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | |
| 249 | /** |
| 250 | * @return bool |
| 251 | */ |
| 252 | public static function ifWorkspaceOrDomainEmpty() |
| 253 | { |
| 254 | $workspace_id = get_site_option(TENWEBIO_MANAGER_PREFIX . '_workspace_id', 0); |
| 255 | $domain_id = get_option(TENWEBIO_MANAGER_PREFIX . '_domain_id', 0); |
| 256 | $access_token = get_site_option(TENWEBIO_MANAGER_PREFIX . '_access_token'); |
| 257 | |
| 258 | return empty($workspace_id) || empty($domain_id) || empty($access_token); |
| 259 | } |
| 260 | } |
| 261 |