lse { $ic = new Image_Conversion(); $path = $image->get_file_path( Image::SIZE_FULL ); $backups_enabled = Settings::get( Settings::BACKUP_ORIGINAL_IMAGES_OPTION_NAME ); $conversion_enabled = $ic->is_enabled(); $needs_conversion = $conversion_enabled && strtolower( File_Utils::get_extension( $path ) ) !== $ic->get_current_file_extension(); $global_context['can_be_restored'] = ( $needs_conversion && $conversion_enabled ) || $backups_enabled; } switch ( $meta->get_status() ) { case Image_Status::OPTIMIZATION_IN_PROGRESS: Module::load_template( $context, 'loading', array_merge( $global_context, [ 'action' => 'optimize', ] ) ); break; case Image_Status::REOPTIMIZING_IN_PROGRESS: Module::load_template( $context, 'loading', array_merge( $global_context, [ 'action' => 'reoptimize', ] ) ); break; case Image_Status::RESTORING_IN_PROGRESS: Module::load_template( $context, 'loading', array_merge( $global_context, [ 'action' => 'restore', ] ) ); break; case Image_Status::OPTIMIZED: $stats = Optimization_Stats::get_image_stats( $image_id ); $saved = [ 'relative' => max( 100 - round( $stats['current_image_size'] / $stats['initial_image_size'] * 100 ), 0 ), 'absolute' => $stats['initial_image_size'] - $stats['current_image_size'], ]; $is_losseless_and_webp = Image_Conversion_Option::WEBP === Settings::get( Settings::CONVERT_TO_FORMAT_OPTION_NAME ) && 'lossless' === Settings::get( Settings::COMPRESSION_LEVEL_OPTION_NAME ); Module::load_template( $context, 'optimized', array_merge( $global_context, [ 'sizes_optimized_count' => $stats['optimized_image_count'], 'saved' => $saved, 'is_losseless_and_webp' => $is_losseless_and_webp, ] ) ); break; case Image_Status::OPTIMIZATION_FAILED: $error_type = $meta->get_error_type() ?? Image_Optimization_Error_Type::GENERIC; $error_message = Optimization_Error_Message::get_optimization_error_message( $error_type ); $images_left = $module->connect_instance->images_left(); Module::load_template( $context, 'error', array_merge( $global_context, [ 'message' => $error_message, 'optimization_error_type' => $error_type, 'images_left' => $images_left, 'allow_retry' => true, 'action' => 'optimize', ] ) ); break; case Image_Status::REOPTIMIZING_FAILED: $error_type = $meta->get_error_type() ?? Image_Optimization_Error_Type::GENERIC; $error_message = Optimization_Error_Message::get_reoptimization_error_message( $error_type ); $images_left = $module->connect_instance->images_left(); Module::load_template( $context, 'error', array_merge( $global_context, [ 'message' => $error_message, 'optimization_error_type' => $error_type, 'images_left' => $images_left, 'allow_retry' => true, 'action' => 'reoptimize', ] ) ); break; case Image_Status::RESTORING_FAILED: Module::load_template( $context, 'error', array_merge( $global_context, [ 'message' => esc_html__( 'Image restoring error', 'image-optimization' ), 'allow_retry' => true, 'action' => 'restore', ] ) ); break; default: Module::load_template( $context, 'not-optimized', $global_context ); } } catch ( Invalid_Image_Exception | Image_Validation_Error $iie ) { Module::load_template( $context, 'error', array_merge( $global_context, [ 'action' => 'error', 'message' => $iie->getMessage(), 'allow_retry' => false, ] ) ); } catch ( Auth_Error $ae ) { Module::load_template( $context, 'error', array_merge( $global_context, [ 'action' => 'error', 'message' => esc_html__( 'N/A', 'image-optimization' ), 'allow_retry' => false, ] ) ); } catch ( Throwable $t ) { Module::load_template( $context, 'error', array_merge( $global_context, [ 'action' => 'error', 'message' => esc_html__( 'Internal server error', 'image-optimization' ), 'allow_retry' => false, ] ) ); } } public function __construct() { add_filter( 'manage_upload_columns', [ $this, 'add_optimization_column' ] ); add_action( 'manage_media_custom_column', [ $this, 'render_optimization_column' ], 10, 2 ); add_action( 'add_meta_boxes_attachment', [ $this, 'add_optimization_meta_box' ] ); add_filter( 'attachment_fields_to_edit', [ $this, 'add_media_modal_details' ], 10, 2 ); } }