ajax_check_security(); $force=isset($_POST['force'])?sanitize_key($_POST['force']):'0'; if($force=='1') { $force=true; } else { $force=false; } $start_row=isset($_POST['offset'])?sanitize_key($_POST['offset']):0; $ret=CompressX_Image_Scanner::scan_unoptimized_images_v2($force,$start_row); echo wp_json_encode($ret); die(); } public function init_bulk_optimization_task() { global $compressx; $compressx->ajax_check_security(); $force=isset($_POST['force'])?sanitize_key($_POST['force']):'0'; if($force=='1') { $force=true; } else { $force=false; } $task=new CompressX_ImgOptim_Task(); $ret=$task->init_task($force); echo wp_json_encode($ret); die(); } public function run_optimize() { global $compressx; $compressx->ajax_check_security(); set_time_limit(120); $task=new CompressX_ImgOptim_Task(); $ret=$task->get_task_status(); if($ret['result']=='success'&&$ret['status']=='completed') { $this->flush($ret); $task->do_optimize_image(); //echo wp_json_encode($ret); } else { echo wp_json_encode($ret); } die(); } private function flush($ret) { $json=wp_json_encode($ret); if(!headers_sent()) { header('Content-Length: '.strlen($json)); header('Connection: close'); header('Content-Encoding: none'); } if (session_id()) session_write_close(); echo wp_json_encode($ret); if(function_exists('fastcgi_finish_request')) { fastcgi_finish_request(); } else { ob_flush(); flush(); } } public function get_opt_progress() { global $compressx; $compressx->ajax_check_security(); $task=new CompressX_ImgOptim_Task(); $result=$task->get_task_progress_ex(); // $file_name=$task->get_log_file(); // if(empty($file_name)) // { // $log=CompressX_Log_Ex::get_instance(); // $log->OpenLogFile(); // } // else // { // $log=CompressX_Log_Ex::get_instance(); // $log->OpenLogFile($file_name); // } echo wp_json_encode($result); die(); } public function get_task_log() { global $compressx; $compressx->ajax_check_security(); $task=new CompressX_ImgOptim_Task(); $offset=isset($_POST['offset'])?sanitize_key($_POST['offset']):0; $offset=intval($offset); $file_name=$task->get_log_file(); if(empty($file_name)) { $log=CompressX_Log_Ex::get_instance(); $log->OpenLogFile(); } else { $log=CompressX_Log_Ex::get_instance(); $log->OpenLogFile($file_name); } $ret=$log->get_log_content($offset); echo wp_json_encode($ret); die(); } public function download_task_log() { global $compressx; $compressx->ajax_check_security('compressx-can-use-logs'); try { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce is verified in ajax_check_security above $task=new CompressX_ImgOptim_Task(); $file_name=$task->get_log_file(); if(!empty($file_name)) { $log=CompressX_Log_Ex::get_instance(); $log->OpenLogFile($file_name); $path=$log->log_file; } else { die(); } if (!file_exists($path)) { die(); } if (session_id()) { session_write_close(); } $size = filesize($path); if (!headers_sent()) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . $file_name . '"'); header('Content-Transfer-Encoding: binary'); header('Connection: Keep-Alive'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . $size); } ob_end_clean(); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_readfile -- Sending file to browser for download readfile($path); } catch (Exception $error) { $message = 'An exception has occurred. class: ' . get_class($error) . ';msg: ' . $error->getMessage() . ';code: ' . $error->getCode() . ';line: ' . $error->getLine() . ';in_file: ' . $error->getFile() . ';'; //error_log($message); } exit; } public function display() { ?>
output_header(); $this->output_review(); $this->output_warning(); $this->output_bulk(); $this->output_progress(); $this->output_comparison(); $this->output_logs(); $this->output_footer(); ?>

().

().

().

get_recent_logs(); $url=admin_url('admin.php?page=info-compressx'); ?>

get_log_list(); $formatted_logs = array(); $count = 0; foreach ($log_list as $log) { if ($count >= 10) break; $size_formatted = 'N/A'; if (isset($log['size']) && is_numeric($log['size'])) { if ($log['size'] >= 1024) { $size_formatted = round($log['size'] / 1024, 2) . ' KB'; } else { $size_formatted = $log['size'] . ' B'; } } $formatted_logs[] = array( 'date' => isset($log['date']) ? $log['date'] : gmdate('M-d-y H:i'), 'filename' => isset($log['name']) ? $log['name'] : (isset($log['file_name']) ? $log['file_name'] : 'unknown.txt'), 'size' => $size_formatted, 'detail_url' => '#', 'download_url' => '#', 'delete_url' => '#' ); $count++; } return $formatted_logs; } /* old private function get_log_list() { $log_list = array(); $log = new CompressX_Log(); $dir = $log->GetSaveLogFolder(); $files = array(); $handler = opendir($dir); $regex = '#^compressx.*_log.txt#'; if ($handler !== false) { while (($filename = readdir($handler)) !== false) { if ($filename != "." && $filename != "..") { if (is_dir($dir . $filename)) { continue; } else { if (preg_match($regex, $filename)) { $files[$filename] = $dir . $filename; } } } } if ($handler) { @closedir($handler); } } foreach ($files as $file) { $handle = @fopen($file, "r"); if ($handle) { $log_file = array(); $log_file['file_name'] = basename($file); if (preg_match('/compressx-(.*?)_/', basename($file), $matches)) { $id = $matches[0]; $id = substr($id, 0, strlen($id) - 1); $log_file['id'] = $id; } $log_file['path'] = $file; $log_file['size'] = filesize($file); $log_file['name'] = basename($file); $log_file['time'] = filemtime($file); $offset = get_option('gmt_offset'); $localtime = $log_file['time'] + $offset * 60 * 60; $log_file['date'] = gmdate('M-d-y H:i', $localtime); $line = fgets($handle); if ($line !== false) { $pos = strpos($line, 'Log created: '); if ($pos !== false) { $log_file['time'] = substr($line, $pos + strlen('Log created: ')); } } fclose($handle); $log_list[basename($file)] = $log_file; } } uasort($log_list, function ($a, $b) { if ($a['time'] > $b['time']) { return -1; } else if ($a['time'] === $b['time']) { return 0; } else { return 1; } }); return $log_list; } */ private function get_log_list() { $log_list = array(); $log = new CompressX_Log(); $dir = trailingslashit( $log->GetSaveLogFolder() ); // Init WP_Filesystem. global $wp_filesystem; if ( empty( $wp_filesystem ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; WP_Filesystem(); } if ( empty( $wp_filesystem ) ) { return $log_list; } if ( ! $wp_filesystem->is_dir( $dir ) ) { return $log_list; } $regex = '#^compressx.*_log\.txt$#'; // List directory files. $dir_list = $wp_filesystem->dirlist( $dir, false, false ); if ( empty( $dir_list ) || ! is_array( $dir_list ) ) { return $log_list; } foreach ( $dir_list as $filename => $info ) { if ( empty( $filename ) ) { continue; } // Skip folders. if ( isset( $info['type'] ) && 'd' === $info['type'] ) { continue; } // Match log files. if ( ! preg_match( $regex, $filename ) ) { continue; } $file = $dir . $filename; $log_file = array(); $log_file['file_name'] = $filename; $log_file['path'] = $file; $log_file['name'] = $filename; // Size. $size = $wp_filesystem->size( $file ); $log_file['size'] = ( false === $size ) ? 0 : (int) $size; // Time. $mtime = $wp_filesystem->mtime( $file ); $mtime = ( false === $mtime ) ? 0 : (int) $mtime; $log_file['time'] = $mtime; // ID extraction (keep your original logic). if ( preg_match( '/compressx-(.*?)_/', $filename, $matches ) ) { $id = $matches[0]; $id = substr( $id, 0, strlen( $id ) - 1 ); $log_file['id'] = $id; } // Local time format. $offset = (float) get_option( 'gmt_offset' ); $localtime = $mtime + (int) ( $offset * 3600 ); $log_file['date'] = gmdate( 'M-d-y H:i', $localtime ); // Read first line from log file (simulate fgets). $content = $wp_filesystem->get_contents( $file ); if ( ! empty( $content ) ) { $lines = preg_split( "/\r\n|\n|\r/", $content ); $line = isset( $lines[0] ) ? $lines[0] : ''; if ( ! empty( $line ) ) { $pos = strpos( $line, 'Log created: ' ); if ( false !== $pos ) { $log_file['time'] = substr( $line, $pos + strlen( 'Log created: ' ) ); } } } $log_list[ $filename ] = $log_file; } uasort( $log_list, function ( $a, $b ) { if ( $a['time'] > $b['time'] ) { return -1; } elseif ( $a['time'] === $b['time'] ) { return 0; } else { return 1; } } ); return $log_list; } public function get_latest_image_data() { global $compressx; $compressx->ajax_check_security('compressx-can-use-image-optimization'); $current_image_id=CompressX_Options::get_option('compressx_latest_bulk_image',null);// $meta=CompressX_Image_Meta_V2::get_image_meta($current_image_id); if(empty($current_image_id)) { $default_image_data=true; } else if(empty($meta)) { $default_image_data=true; } else if(wp_get_attachment_url( $current_image_id )===false) { $default_image_data=true; } else { if($this->webp_converted_exist($current_image_id)||$this->avif_converted_exist($current_image_id)) { $default_image_data=false; } else { $default_image_data=true; } } $ret['compressx_latest_bulk_image']=$current_image_id; $ret['default_image_data']=$default_image_data; if($default_image_data) { $og_test_file='/includes/assets/compressx_test.png'; $og_test_webp_file='/includes/assets/compressx_test.png.webp'; $test_file='compressx_test_comparison.png'; $test_webp_file='compressx_test_comparison.webp'; $upload_dir = wp_upload_dir(); $path = $upload_dir['basedir'] . '/' .$test_file; if (!file_exists($path)) { $og_path = COMPRESSX_DIR . $og_test_file; copy($og_path, $path); } $path2 = $upload_dir['basedir'] . '/' . $test_webp_file; if (!file_exists($path2)) { $og_path = COMPRESSX_DIR . $og_test_webp_file; copy($og_path, $path2); } $url=$upload_dir['baseurl'].'/'. $test_file; $url2=$upload_dir['baseurl'].'/'. $test_webp_file; $original_size=filesize($path); $webp_size=filesize($path2); $savings = round((($original_size - $webp_size) / $original_size) * 100); $current_image['original_path']=$path; $current_image['original_url']=$url; $current_image['webp_path']=$path2; $current_image['webp_url']=$url2; $current_image['avif_path']=''; $current_image['avif_url']=''; $current_image['original_size']=size_format($original_size,2); $current_image['webp_size']=size_format($webp_size,2); $current_image['webp_savings'] ="~{$savings}% smaller"; $current_image['avif_size']=""; $current_image['avif_savings']=""; $current_image['webp_disabled'] = false; $current_image['avif_disabled'] = true; } else { $og_url=wp_get_attachment_url($current_image_id); $current_image['original_url']=$og_url.'?original'; $current_image['original_path'] = get_attached_file($current_image_id); $meta=CompressX_Image_Meta_V2::get_image_meta($current_image_id); $current_image['original_size']=size_format($meta['og_file_size']); if($meta['webp_converted']) { $current_image['webp_disabled'] = false; $current_image['webp_size'] = size_format($meta['webp_converted_size'],2); $savings = round((( $meta['og_file_size'] - $meta['webp_converted_size']) / $meta['og_file_size']) * 100); $output_path=CompressX_Image_Method::get_output_path($current_image['original_path']); if(isset($meta['mime_type'])&&$meta['mime_type']=="image/webp") { $current_image['webp_path']=$output_path; $current_image['webp_url']=$this->get_nextgen_url($og_url).'?original'; } else { $current_image['webp_path']=$output_path.'.webp'; $current_image['webp_url']=$this->get_nextgen_url($og_url).'.webp?original'; } $current_image['webp_savings'] ="~{$savings}% smaller"; } else { $current_image['webp_disabled'] = true; $current_image['webp_path']=''; $current_image['webp_url']=''; $current_image['webp_size'] =0; $current_image['webp_savings'] =0; } if($meta['avif_converted']) { $current_image['avif_disabled'] = false; $output_path=CompressX_Image_Method::get_output_path($current_image['original_path']); if(isset($meta['mime_type'])&&$meta['mime_type']=="image/avif") { $current_image['avif_path']=$output_path; $current_image['avif_url']=$this->get_nextgen_url($og_url).'?original'; } else { $current_image['avif_path']=$output_path.'.avif'; $current_image['avif_url']=$this->get_nextgen_url($og_url).'.avif?original'; } $current_image['avif_size'] = size_format($meta['avif_converted_size'],2); $savings = round((( $meta['og_file_size'] - $meta['avif_converted_size']) / $meta['og_file_size']) * 100); $current_image['avif_savings'] ="~{$savings}% smaller"; } else { $current_image['avif_disabled'] = true; $current_image['avif_path']=''; $current_image['avif_url']=''; $current_image['avif_size'] = 0; $current_image['avif_savings']=0; } } $ret['result']='success'; $ret['current_image']=$current_image; echo wp_json_encode($ret); die(); } public function webp_converted_exist($image_id) { $original_path = get_attached_file($image_id); $meta=CompressX_Image_Meta_V2::get_image_meta($image_id); if($meta['webp_converted']) { $output_path=CompressX_Image_Method::get_output_path($original_path); if(isset($meta['mime_type'])&&$meta['mime_type']=="image/webp") { $webp_path=$output_path; } else { $webp_path=$output_path.'.webp'; } if(file_exists($webp_path)) { return true; } else { return false; } } else { return false; } } public function avif_converted_exist($image_id) { $original_path = get_attached_file($image_id); $meta=CompressX_Image_Meta_V2::get_image_meta($image_id); if($meta['avif_converted']) { $output_path=CompressX_Image_Method::get_output_path($original_path); if(isset($meta['mime_type'])&&$meta['mime_type']=="image/avif") { $avif_path=$output_path; } else { $avif_path=$output_path.'.avif'; } if(file_exists($avif_path)) { return true; } else { return false; } } else { return false; } } public function get_nextgen_url( $orig_url ) { $upload_dir = wp_get_upload_dir(); return str_replace( $upload_dir['baseurl'], content_url( 'compressx-nextgen/uploads' ), $orig_url ); } }