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
CLI.php
95 lines
| 1 | <?php |
| 2 | namespace TenWebIO; |
| 3 | |
| 4 | class CLI |
| 5 | { |
| 6 | public static function readyToOptimizeImages($args, $assoc_args) |
| 7 | { |
| 8 | $blog_id = $args[0]; |
| 9 | if (is_multisite()) { |
| 10 | switch_to_blog($blog_id); |
| 11 | } |
| 12 | $io_compress_data = new CompressDataService(); |
| 13 | $images = $io_compress_data->getImagesReadyForOptimize(false, true)->getData(); |
| 14 | if (empty($images)) { |
| 15 | \WP_CLI::error('No images for optimization.'); |
| 16 | } else { |
| 17 | \WP_CLI\Utils\format_items( |
| 18 | 'json', |
| 19 | $images, |
| 20 | ['ID', 'guid', 'size'] |
| 21 | ); |
| 22 | } |
| 23 | if (is_multisite()) { |
| 24 | restore_current_blog(); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | public static function storeOptimizedImagesLog($args, $assoc_args) |
| 29 | { |
| 30 | $blog_id = $args[0]; |
| 31 | $data = $args[1]; |
| 32 | $is_finished = $args[2]; |
| 33 | |
| 34 | if (is_multisite()) { |
| 35 | switch_to_blog($blog_id); |
| 36 | } |
| 37 | if ($data) { |
| 38 | $data = json_decode($data, true); |
| 39 | } else { |
| 40 | $data = array(); |
| 41 | } |
| 42 | $data = array_map(function ($value) { |
| 43 | if (!is_array($value)) { |
| 44 | return json_decode($value); |
| 45 | } |
| 46 | |
| 47 | return $value; |
| 48 | }, $data); |
| 49 | $io_compress = new CompressService(); |
| 50 | // store data in service |
| 51 | $io_compress->storeDataInService($data, $is_finished); |
| 52 | |
| 53 | \WP_CLI::success('ok'); |
| 54 | if (is_multisite()) { |
| 55 | restore_current_blog(); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | public static function storeLastOptimizationLog($args, $assoc_args) |
| 60 | { |
| 61 | $blog_id = $args[0]; |
| 62 | $image_orig_size = $args[1]; |
| 63 | $image_size = $args[2]; |
| 64 | $count = $args[3]; |
| 65 | |
| 66 | if (is_multisite()) { |
| 67 | switch_to_blog($blog_id); |
| 68 | } |
| 69 | |
| 70 | $last_compress = new LastCompress(true); |
| 71 | $last_compress->update($image_size, $image_orig_size, $count); |
| 72 | |
| 73 | \WP_CLI::success('ok'); |
| 74 | if (is_multisite()) { |
| 75 | restore_current_blog(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | public static function convertedImages($args, $assoc_args) |
| 80 | { |
| 81 | $counter = $args[0] ? $args[0] : '1'; |
| 82 | $glue = $args[1] ? $args[1] : ' '; |
| 83 | $webP_paths = isset($args[2]) ? (int)$args[2] : 1; |
| 84 | $io_compress = new CompressService(); |
| 85 | $images = $io_compress->getAlreadyConvertedImagesPaths($counter, $webP_paths); |
| 86 | if (empty($images)) { |
| 87 | \WP_CLI::error('Counter ' . $counter . '. No converted images.'); |
| 88 | } else { |
| 89 | \WP_CLI::line(implode($glue, $images)); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | |
| 95 |