format
3 months ago
helpers
5 months ago
models
5 months ago
webp
3 months ago
class.backup.php
5 months ago
class.custom-folders.php
5 months ago
class.folder-image.php
5 months ago
class.folder.php
5 months ago
class.folders-list-table.php
5 months ago
class.gallery-nextgen.php
5 months ago
class.image-nextgen.php
5 months ago
class.image-statistic-folders.php
5 months ago
class.image-statistic-nextgen.php
5 months ago
class.wpcli-optimize.php
5 months ago
index.php
5 months ago
class.image-statistic-folders.php
351 lines
| 1 | <?php |
| 2 | |
| 3 | // Exit if accessed directly |
| 4 | if ( ! defined( 'ABSPATH' ) ) { |
| 5 | exit; |
| 6 | } |
| 7 | |
| 8 | /** |
| 9 | * Класс для работы со статистическими данными по оптимизации изображений |
| 10 | * |
| 11 | * @version 1.0 |
| 12 | */ |
| 13 | class WRIO_Image_Statistic_Folders extends WRIO_Image_Statistic { |
| 14 | |
| 15 | /** |
| 16 | * The single instance of the class. |
| 17 | * |
| 18 | * @since 1.3.0 |
| 19 | * @access protected |
| 20 | * @var static |
| 21 | */ |
| 22 | protected static $_instance; |
| 23 | |
| 24 | /** |
| 25 | * Со� |
| 26 | ранение статистики |
| 27 | */ |
| 28 | public function save() { |
| 29 | WRIO_Plugin::app()->updateOption( 'folders_original_size', $this->statistic['original_size'] ); |
| 30 | WRIO_Plugin::app()->updateOption( 'folders_optimized_size', $this->statistic['optimized_size'] ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Загрузка статистики и расчёт некоторы� |
| 35 | параметров |
| 36 | * |
| 37 | * @return array |
| 38 | */ |
| 39 | public function load() { |
| 40 | $original_size = WRIO_Plugin::app()->getOption( 'folders_original_size', 0 ); |
| 41 | $optimized_size = WRIO_Plugin::app()->getOption( 'folders_optimized_size', 0 ); |
| 42 | $total_images = $this->getTotalCount(); |
| 43 | $error_count = RIO_Process_Queue::count_by_type_status( 'cf_image', 'error' ); |
| 44 | $skipped_count = RIO_Process_Queue::count_by_type_status( 'cf_image', 'skip' ); |
| 45 | $optimized_count = $this->getOptimizedCount(); |
| 46 | |
| 47 | if ( ! $total_images ) { |
| 48 | $total_images = 0; |
| 49 | if ( $original_size || $optimized_size ) { |
| 50 | // если нет картинок, то и размеров не должно быть |
| 51 | $original_size = $this->statistic['original_size'] = 0; |
| 52 | $optimized_size = $this->statistic['optimized_size'] = 0; |
| 53 | $this->save(); |
| 54 | } |
| 55 | } |
| 56 | if ( ! $error_count ) { |
| 57 | $error_count = 0; |
| 58 | } |
| 59 | if ( ! $skipped_count ) { |
| 60 | $skipped_count = 0; |
| 61 | } |
| 62 | if ( ! $optimized_count ) { |
| 63 | $optimized_count = 0; |
| 64 | } |
| 65 | // unoptimized count: all - optimized - error - skip |
| 66 | $unoptimized_count = $total_images - $optimized_count - $error_count - $skipped_count; |
| 67 | if ( $optimized_size and $original_size ) { |
| 68 | $percent_diff = round( ( $original_size - $optimized_size ) * 100 / $original_size, 1 ); |
| 69 | $percent_diff_line = round( $optimized_size * 100 / $original_size, 0 ); |
| 70 | } else { |
| 71 | $percent_diff = 0; |
| 72 | $percent_diff_line = 100; |
| 73 | } |
| 74 | if ( $total_images ) { |
| 75 | $optimized_images_percent = round( $optimized_count * 100 / $total_images ); |
| 76 | } else { |
| 77 | $optimized_images_percent = 0; |
| 78 | } |
| 79 | |
| 80 | return [ |
| 81 | 'original' => $total_images, |
| 82 | 'optimized' => $optimized_count, |
| 83 | 'optimized_percent' => $optimized_images_percent, |
| 84 | 'percent_line' => $percent_diff_line, |
| 85 | 'unoptimized' => $unoptimized_count, |
| 86 | 'optimized_size' => $optimized_size, |
| 87 | 'original_size' => $original_size, |
| 88 | 'save_size_percent' => $percent_diff, |
| 89 | 'error' => $error_count, |
| 90 | ]; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Общее кол-во изображений |
| 95 | */ |
| 96 | public function getTotalCount() { |
| 97 | $cf = WRIO_Custom_Folders::get_instance(); |
| 98 | $current_folder = apply_filters( 'wriop_cf_current_folder', false ); |
| 99 | if ( $current_folder ) { |
| 100 | // если нужна конкретная папка |
| 101 | $folder = $cf->getFolder( $current_folder ); |
| 102 | if ( ! $folder ) { |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | return $folder->get( 'files_count' ); |
| 107 | } |
| 108 | $folders = $cf->getFolders(); |
| 109 | $total_images = 0; |
| 110 | if ( ! $folders ) { |
| 111 | return $total_images; |
| 112 | } |
| 113 | foreach ( $folders as $folder ) { |
| 114 | $total_images += $folder->get( 'files_count' ); |
| 115 | } |
| 116 | |
| 117 | return $total_images; |
| 118 | } |
| 119 | |
| 120 | public function getOptimizedCount() { |
| 121 | $current_folder = apply_filters( 'wriop_cf_current_folder', false ); |
| 122 | if ( $current_folder ) { |
| 123 | global $wpdb; |
| 124 | $db_table = RIO_Process_Queue::table_name(); |
| 125 | $sql_optimized = $wpdb->prepare( "SELECT COUNT(*) FROM {$db_table} WHERE item_type = 'cf_image' AND item_hash_alternative = %s AND result_status = 'success';", $current_folder ); |
| 126 | $optimized_count = $wpdb->get_var( $sql_optimized ); |
| 127 | } else { |
| 128 | $optimized_count = RIO_Process_Queue::count_by_type_status( 'cf_image', 'success' ); |
| 129 | } |
| 130 | |
| 131 | return $optimized_count; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Кол-во неоптимизированны� |
| 136 | изображений |
| 137 | */ |
| 138 | public function getUnoptimizedCount() { |
| 139 | global $wpdb; |
| 140 | |
| 141 | $db_table = RIO_Process_Queue::table_name(); |
| 142 | $current_folder = apply_filters( 'wriop_cf_current_folder', false ); |
| 143 | |
| 144 | if ( $current_folder ) { |
| 145 | $sql_unoptimized = $wpdb->prepare( |
| 146 | " |
| 147 | SELECT COUNT(*) |
| 148 | FROM {$db_table} |
| 149 | WHERE item_type = 'cf_image' |
| 150 | AND item_hash_alternative = %s |
| 151 | AND result_status IN (%s,%s);", |
| 152 | $current_folder, |
| 153 | RIO_Process_Queue::STATUS_UNOPTIMIZED, |
| 154 | RIO_Process_Queue::STATUS_PROCESSING |
| 155 | ); |
| 156 | } else { |
| 157 | $sql_unoptimized = $wpdb->prepare( |
| 158 | " |
| 159 | SELECT COUNT(*) |
| 160 | FROM {$db_table} WHERE |
| 161 | item_type = 'cf_image' AND result_status IN (%s,%s);", |
| 162 | RIO_Process_Queue::STATUS_UNOPTIMIZED, |
| 163 | RIO_Process_Queue::STATUS_PROCESSING |
| 164 | ); |
| 165 | } |
| 166 | |
| 167 | $unoptimized = $wpdb->get_var( $sql_unoptimized ); |
| 168 | |
| 169 | return $unoptimized; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Возвращает неоптимизированные изображения |
| 174 | * |
| 175 | * @param int $limit ограничение выборки |
| 176 | * |
| 177 | * @return RIO_Process_Queue[]|array |
| 178 | */ |
| 179 | public function getUnoptimized( $limit = 10 ) { |
| 180 | // переделать |
| 181 | global $wpdb; |
| 182 | |
| 183 | $unoptimized_items = []; |
| 184 | $db_table = RIO_Process_Queue::table_name(); |
| 185 | $current_folder = apply_filters( 'wriop_cf_current_folder', false ); |
| 186 | |
| 187 | if ( $current_folder ) { |
| 188 | $sql_unoptimized = $wpdb->prepare( "SELECT * FROM {$db_table} WHERE item_type = 'cf_image' AND item_hash_alternative = %s AND result_status = 'unoptimized' LIMIT %d", $current_folder, $limit ); |
| 189 | } else { |
| 190 | $sql_unoptimized = "SELECT * FROM {$db_table} WHERE item_type = 'cf_image' AND result_status = 'unoptimized' LIMIT " . intval( $limit ); |
| 191 | } |
| 192 | |
| 193 | $result = $wpdb->get_results( $sql_unoptimized ); |
| 194 | |
| 195 | if ( ! empty( $result ) ) { |
| 196 | foreach ( $result as $key => $data ) { |
| 197 | $unoptimized_items[ $key ] = new RIO_Process_Queue( $data ); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | return $unoptimized_items; |
| 202 | } |
| 203 | |
| 204 | public function getDeferredUnoptimized( $limit = 10 ) { |
| 205 | global $wpdb; |
| 206 | |
| 207 | $db_table = RIO_Process_Queue::table_name(); |
| 208 | $current_folder = apply_filters( 'wriop_cf_current_folder', false ); |
| 209 | if ( $current_folder ) { |
| 210 | $sql_unoptimized = $wpdb->prepare( "SELECT * FROM {$db_table} WHERE item_type = 'cf_image' AND item_hash_alternative = %s AND result_status = 'processing' LIMIT %d", $current_folder, $limit ); |
| 211 | } else { |
| 212 | $sql_unoptimized = "SELECT * FROM {$db_table} WHERE item_type = 'cf_image' and result_status = 'processing' LIMIT " . intval( $limit ); |
| 213 | } |
| 214 | $unoptimized = $wpdb->get_results( $sql_unoptimized ); |
| 215 | |
| 216 | return $unoptimized; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Returns the result of the last optimized images. |
| 221 | * |
| 222 | * @param int $limit Limit. |
| 223 | * |
| 224 | * @return array<int, array{ |
| 225 | * id: int|string, |
| 226 | * file_name: string, |
| 227 | * url: string, |
| 228 | * thumbnail_url: string, |
| 229 | * original_size: string, |
| 230 | * optimized_size: string, |
| 231 | * webp_size?: string, |
| 232 | * original_saving: string, |
| 233 | * thumbnails_count: int, |
| 234 | * type: string, |
| 235 | * total_saving: string, |
| 236 | * error_msg?: string |
| 237 | * }> |
| 238 | */ |
| 239 | public function get_last_optimized_images( $limit = 100 ) { |
| 240 | global $wpdb; |
| 241 | |
| 242 | $items = []; |
| 243 | $db_table = RIO_Process_Queue::table_name(); |
| 244 | |
| 245 | $sql = $wpdb->prepare( |
| 246 | "SELECT * |
| 247 | FROM {$db_table} as t1 |
| 248 | WHERE t1.item_type = 'cf_image' |
| 249 | AND t1.result_status |
| 250 | IN (%s, %s) |
| 251 | ORDER BY id DESC |
| 252 | LIMIT %d;", |
| 253 | RIO_Process_Queue::STATUS_SUCCESS, |
| 254 | RIO_Process_Queue::STATUS_ERROR, |
| 255 | $limit |
| 256 | ); |
| 257 | |
| 258 | $optimized_images = $wpdb->get_results( $sql, ARRAY_A ); |
| 259 | |
| 260 | foreach ( $optimized_images as $row ) { |
| 261 | $items[] = $this->format_for_log( new RIO_Process_Queue( $row ) ); |
| 262 | } |
| 263 | |
| 264 | return $items; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Get the last optimized image record for a specific model. |
| 269 | * |
| 270 | * @param RIO_Process_Queue $model Queue model instance. |
| 271 | * |
| 272 | * @return array<int, array<string, mixed>> |
| 273 | * @since 1.1 |
| 274 | */ |
| 275 | public function get_last_optimized_image( $model ) { |
| 276 | $items = []; |
| 277 | $items[] = $this->format_for_log( $model ); |
| 278 | |
| 279 | return $items; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * @since 1.0.4 |
| 284 | * |
| 285 | * @param int|RIO_Process_Queue $queue_model |
| 286 | */ |
| 287 | protected function format_for_log( $queue_model ) { |
| 288 | if ( $queue_model instanceof RIO_Process_Queue ) { |
| 289 | $cf_image = new WRIO_Folder_Image( $queue_model->id, $queue_model ); |
| 290 | } else { |
| 291 | // todo: Temporarily fix |
| 292 | $cf_image = new WRIO_Folder_Image( $queue_model ); |
| 293 | } |
| 294 | |
| 295 | $optimization_data = $cf_image->getOptimizationData(); |
| 296 | |
| 297 | $main_file = $cf_image->get( 'path' ); |
| 298 | $main_saving = $total_saving = 0; |
| 299 | |
| 300 | if ( $optimization_data->original_size ) { |
| 301 | $total_saving = ( $optimization_data->original_size - $optimization_data->final_size ) * 100 / $optimization_data->original_size; |
| 302 | $main_saving = $total_saving; |
| 303 | } |
| 304 | |
| 305 | $image_url = $cf_image->get( 'url' ); |
| 306 | $thumbnail_url = $image_url; |
| 307 | |
| 308 | $formated_data = [ |
| 309 | 'id' => $optimization_data->id, |
| 310 | 'url' => $image_url, |
| 311 | 'original_url' => $image_url, |
| 312 | 'thumbnail_url' => $thumbnail_url, |
| 313 | 'file_name' => wp_basename( $main_file ), |
| 314 | 'original_size' => size_format( $optimization_data->original_size, 2 ), |
| 315 | 'optimized_size' => size_format( $optimization_data->final_size, 2 ), |
| 316 | 'original_saving' => round( $main_saving ) . '%', |
| 317 | 'thumbnails_count' => 0, |
| 318 | 'type' => 'success', |
| 319 | 'total_saving' => round( $total_saving ) . '%', |
| 320 | ]; |
| 321 | |
| 322 | /** |
| 323 | * @var WRIO_CF_Image_Extra_Data $extra_data |
| 324 | */ |
| 325 | $extra_data = $optimization_data->get_extra_data(); |
| 326 | |
| 327 | if ( ! empty( $extra_data ) ) { |
| 328 | $webp_size = $extra_data->get_webp_main_size(); |
| 329 | if ( $webp_size ) { |
| 330 | $webp_size = size_format( $webp_size, 2 ); |
| 331 | } else { |
| 332 | $webp_size = '-'; |
| 333 | } |
| 334 | |
| 335 | $formated_data['webp_size'] = $webp_size; |
| 336 | |
| 337 | $error = $extra_data->get_error(); |
| 338 | |
| 339 | if ( $optimization_data->result_status === RIO_Process_Queue::STATUS_ERROR || ! empty( $error ) ) { |
| 340 | $formated_data['type'] = 'error'; |
| 341 | |
| 342 | $error_message = $extra_data->get_error_msg(); |
| 343 | |
| 344 | $formated_data['error_msg'] = ! empty( $error_message ) ? $error_message : esc_html__( 'Unknown error', 'robin-image-optimizer' ); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | return $formated_data; |
| 349 | } |
| 350 | } |
| 351 |