tenweb-speed-optimizer
/
vendor
/
10web-utils
/
tenweb-image-optimizer
/
src
/
TenWebIO
/
CompressService.php
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
CompressService.php
244 lines
| 1 | <?php |
| 2 | |
| 3 | namespace TenWebIO; |
| 4 | |
| 5 | use TenWebQueue\DTO\QueueConfigDTO; |
| 6 | use TenWebQueue\DTO\QueueDataDTO; |
| 7 | use TenWebQueue\QueueOrchestrator; |
| 8 | use TenWebQueue\Exceptions\QueueException; |
| 9 | use \TenWebQueue\QueueContext; |
| 10 | |
| 11 | class CompressService |
| 12 | { |
| 13 | const QUEUE_NAME = 'tenweb_image_optimizer'; |
| 14 | const QUEUE_RESTART_FILE_NAME = 'tenweb_image_optimizer_restart'; |
| 15 | /** |
| 16 | * @var CompressDataService |
| 17 | */ |
| 18 | private $compress_data; |
| 19 | |
| 20 | /** |
| 21 | * @var Config |
| 22 | */ |
| 23 | private $config; |
| 24 | |
| 25 | /** |
| 26 | * @var int |
| 27 | */ |
| 28 | private $restart = 0; |
| 29 | |
| 30 | /** |
| 31 | * @var string |
| 32 | */ |
| 33 | private $restart_route; |
| 34 | |
| 35 | |
| 36 | /** |
| 37 | * @var array |
| 38 | */ |
| 39 | private $restart_body; |
| 40 | |
| 41 | /** |
| 42 | * @var array |
| 43 | */ |
| 44 | private $images = array(); |
| 45 | |
| 46 | /** |
| 47 | * @param int $restart |
| 48 | */ |
| 49 | public function __construct($restart = 0) |
| 50 | { |
| 51 | $this->compress_data = new CompressDataService(); |
| 52 | $this->config = new Config(); |
| 53 | $this->restart = $restart; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * |
| 58 | * @param bool $force |
| 59 | * |
| 60 | * @return bool |
| 61 | * @throws QueueException |
| 62 | */ |
| 63 | public function compressBulk($force = false) |
| 64 | { |
| 65 | if ($this->ifRunningCompressExist() && !$this->restart) { |
| 66 | Utils::finishQueue('bulk', false, false); |
| 67 | |
| 68 | return false; |
| 69 | } |
| 70 | ini_set('max_execution_time', 5000); |
| 71 | if (!$this->restart) { |
| 72 | $attachments = $this->compress_data->getImagesReadyForOptimize($force, true); |
| 73 | if ($attachments instanceof Attachments) { |
| 74 | $this->images = $attachments->getData(); |
| 75 | } |
| 76 | if (empty($this->images)) { |
| 77 | Utils::finishQueue('bulk', false, false); |
| 78 | |
| 79 | return false; |
| 80 | } |
| 81 | } |
| 82 | $this->restart_route = add_query_arg(array('rest_route' => '/tenwebio/v2/compress'), get_home_url() . "/"); |
| 83 | $this->restart_body['force'] = $force; |
| 84 | $this->compress('bulk'); |
| 85 | |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * |
| 91 | * @param $image_urls |
| 92 | * @param $page_id |
| 93 | * @param int $only_convert_webp |
| 94 | * @param string $origin |
| 95 | * |
| 96 | * @return bool |
| 97 | * @throws QueueException |
| 98 | */ |
| 99 | public function compressCustom($image_urls, $page_id, $only_convert_webp = 0, $origin = 'io') |
| 100 | { |
| 101 | if (!$this->restart) { |
| 102 | $attachments = new Attachments(); |
| 103 | $this->images = $attachments->getCustomAttachmentsData($image_urls); |
| 104 | if (empty($this->images)) { |
| 105 | Utils::finishQueue('custom_' . $page_id, false, false); |
| 106 | |
| 107 | return false; |
| 108 | } |
| 109 | } |
| 110 | $this->restart_route = add_query_arg(array('rest_route' => '/tenwebio/v2/compress-custom'), get_home_url() . "/"); |
| 111 | $this->restart_body['page_id'] = $page_id; |
| 112 | $this->restart_body['only_convert_webp'] = $only_convert_webp; |
| 113 | $this->restart_body['origin'] = $origin; |
| 114 | update_site_option(TENWEBIO_PREFIX . '_custom_compress_only_convert_webp_' . $page_id, $only_convert_webp); |
| 115 | update_site_option(TENWEBIO_PREFIX . '_custom_compress_origin_' . $page_id, $origin); |
| 116 | $this->compress('custom_' . $page_id); |
| 117 | |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @param $post_id |
| 123 | * |
| 124 | * @return bool |
| 125 | * @throws QueueException |
| 126 | */ |
| 127 | public function compressOne($post_id) |
| 128 | { |
| 129 | if (!$this->restart) { |
| 130 | $this->images = $this->compress_data->getImageReadyForOptimize($post_id)->getData(); |
| 131 | if (empty($this->images)) { |
| 132 | Utils::finishQueue('single_' . $post_id, false, false); |
| 133 | |
| 134 | return false; |
| 135 | } |
| 136 | } |
| 137 | $this->restart_route = add_query_arg(array('rest_route' => '/tenwebio/v2/compress-one'), get_home_url() . "/"); |
| 138 | $this->compress('single_' . $post_id); |
| 139 | |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @param $type |
| 145 | * |
| 146 | * @return void |
| 147 | * @throws QueueException |
| 148 | */ |
| 149 | public function compress($type) |
| 150 | { |
| 151 | Logs::setLog("compress:" . $type . ":restart", $this->restart); |
| 152 | $queue = $this->createQueue($type); |
| 153 | |
| 154 | if (!$this->restart) { |
| 155 | Utils::deleteQueueTransients($type, false); |
| 156 | update_site_option(TENWEBIO_PREFIX . '_compress_images_count_' . $type, count($this->images)); |
| 157 | $queue->enqueue($this->images); |
| 158 | } |
| 159 | $queue->dequeue(); |
| 160 | } |
| 161 | |
| 162 | /*** |
| 163 | * @param $type |
| 164 | * |
| 165 | * @return QueueContext |
| 166 | * @throws QueueException |
| 167 | */ |
| 168 | public function createQueue($type) |
| 169 | { |
| 170 | $queue_dir = Utils::getQueueDir($type); |
| 171 | $this->restart_body['restart'] = 1; |
| 172 | $this->restart_body['tenwebio_nonce'] = wp_create_nonce('tenwebio_rest'); |
| 173 | |
| 174 | return QueueOrchestrator::initQueue(new QueueConfigDTO([ |
| 175 | 'connection_type' => 'file', |
| 176 | 'file_connection_path' => $queue_dir, |
| 177 | 'restart_file_path' => $queue_dir . '/' . self::QUEUE_RESTART_FILE_NAME . '_' . $type . '.txt', |
| 178 | 'restart_route' => $this->restart_route, |
| 179 | 'restart_body' => $this->restart_body, |
| 180 | 'items_count_for_restart' => $this->config->getImagesLimitForRestart(), |
| 181 | 'force' => (!$this->restart), |
| 182 | 'queue_data' => new QueueDataDTO([ |
| 183 | 'name' => self::QUEUE_NAME . '_' . $type, |
| 184 | 'multiple_items' => true |
| 185 | ]) |
| 186 | ]), \TenWebIO\Queue\QueueProducer::class, \TenWebIO\Queue\QueueConsumer::class); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * @param $data |
| 191 | * @param bool $is_finished |
| 192 | * |
| 193 | * @return void |
| 194 | */ |
| 195 | public function storeDataInService($data, $is_finished = 0) |
| 196 | { |
| 197 | $api_instance = new Api(Api::API_COMPRESS_LOG_STORE_ACTION); |
| 198 | $api_instance->apiRequest('POST', array('data' => $data, 'is_finished' => $is_finished)); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Counter can be number, or other directory name like elementor |
| 203 | * |
| 204 | * @param string $counter |
| 205 | * |
| 206 | * @param integer $webP_paths |
| 207 | * |
| 208 | * @return array |
| 209 | */ |
| 210 | public function getAlreadyConvertedImagesPaths($counter = '1', $webP_paths = 1) |
| 211 | { |
| 212 | $data = get_option(TENWEBIO_PREFIX . '_converted_images_' . $counter); |
| 213 | if (empty($data)) { |
| 214 | return array(); |
| 215 | } |
| 216 | $data = json_decode($data); |
| 217 | $paths = array(); |
| 218 | foreach ($data as $row) { |
| 219 | $row = json_decode($row, true); |
| 220 | $paths[] = $webP_paths ? $row['path'] : rtrim($row['path'], '.webp'); |
| 221 | } |
| 222 | |
| 223 | return array_unique($paths); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * @param $type |
| 228 | * |
| 229 | * @return bool |
| 230 | */ |
| 231 | public function ifRunningCompressExist($type = 'bulk') |
| 232 | { |
| 233 | $running_compress = get_site_option(TENWEBIO_PREFIX . '_running_compress_' . $type); |
| 234 | Logs::setLog("compress:" . $type . ":running:check", $running_compress); |
| 235 | if ($running_compress) { |
| 236 | return true; |
| 237 | } |
| 238 | update_site_option(TENWEBIO_PREFIX . '_running_compress_' . $type, 1); |
| 239 | |
| 240 | return false; |
| 241 | } |
| 242 | |
| 243 | } |
| 244 |