support_extension=array(); $this->support_extension[]='jpg'; $this->support_extension[]='jpeg'; $this->support_extension[]='png'; $this->support_extension[]='webp'; add_action('wp_ajax_compressx_get_custom_tree_dir_ex', array($this, 'get_custom_tree_dir_ex')); add_action('wp_ajax_compressx_add_include_folders', array($this, 'add_include_folders')); add_action('wp_ajax_compressx_remove_include_folders', array($this, 'remove_include_folders')); // add_action('wp_ajax_compressx_start_scan_custom_images', array($this, 'start_scan_custom_images')); add_action('wp_ajax_compressx_init_custom_bulk_optimization_task', array($this, 'init_custom_bulk_optimization_task')); add_action('wp_ajax_compressx_run_custom_optimize', array($this, 'run_custom_optimize')); add_action('wp_ajax_compressx_get_custom_opt_progress', array($this, 'get_custom_opt_progress')); add_action('wp_ajax_compressx_get_dir_info', array($this, 'get_dir_info')); // add_action('cx_output_custom_bulk',array($this, 'output_custom_bulk')); } public function output_custom_bulk() { CompressX_Custom_Image_Meta::check_custom_table(); $includes=CompressX_Options::get_option('compressx_custom_includes',array()); $stats=$this->get_custom_stats($includes); $found=$stats['files']; $saved=$stats['saved']; $processed=$stats['processed_files']; ?>
output_custom_folders(); ?>
| % |
get_folder_stats($include,$stats,$convert_to_webp,$convert_to_avif); } if($stats['total']>0) { $stats['saved'] = ( $stats['processed'] / $stats['total'] ) * 100; $stats['saved'] = round( $stats['saved'], 1 ); } return $stats; } public function get_folder_stats($path,&$stats,$convert_to_webp,$convert_to_avif) { try { $count=0; $rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); foreach ($rii as $file) { if ($file->isDir()) { continue; } if($this->is_support_extension($file->getExtension())) { $filename=$path.'/'.$file->getFilename(); $filename=$this->transfer_path($filename); $type=pathinfo($filename, PATHINFO_EXTENSION); if($convert_to_webp&&$type!="webp") { if(CompressX_Custom_Image_Meta::is_convert_webp($filename)) { $stats['processed_files']++; $stats['processed']+=CompressX_Custom_Image_Meta::get_convert_webp_size($filename); } $count++; $stats['files']++; $stats['total']+=$file->getSize(); } else if($convert_to_avif&&$type!="avif") { if(CompressX_Custom_Image_Meta::is_convert_avif($filename)) { $stats['processed_files']++; $stats['processed']+=CompressX_Custom_Image_Meta::get_convert_avif_size($filename); } $count++; $stats['files']++; $stats['total']+=$file->getSize(); } } } return $count; } catch (Exception $exception) { return 0; } } public function is_support_extension($extension) { if(in_array($extension,$this->support_extension)) { return true; } else { return false; } } public function output_custom_folders() { $includes=CompressX_Options::get_option('compressx_custom_includes',array()); $abs_path = trailingslashit(str_replace('\\', '/', realpath(ABSPATH))); $convert_to_avif=CompressX_Options::get_option('compressx_output_format_avif',1); if($convert_to_avif&&CompressX_Image_Opt_Method::is_support_avif()) { $convert_to_avif=true; } else { $convert_to_avif=false; } ?>
wp-content:
  • get_children_count($include,$convert_to_avif).')')?>
ajax_check_security('compressx-can-bulk-custom-convert'); try{ $node_array = array(); if ($_POST['tree_node']['node']['id'] == '#') { $path = ABSPATH; if (!empty($_POST['tree_node']['path'])) { $path = sanitize_text_field($_POST['tree_node']['path']); } } else { $path = sanitize_text_field($_POST['tree_node']['node']['id']); } $path = trailingslashit(str_replace('\\', '/', realpath($path))); if ($dh = opendir($path)) { while (substr($path, -1) == '/') { $path = rtrim($path, '/'); } $skip_paths = array(".", ".."); while (($value = readdir($dh)) !== false) { trailingslashit(str_replace('\\', '/', $value)); if (!in_array($value, $skip_paths)) { if (is_dir($path . '/' . $value)) { if(!$this->is_media_folder($path . '/' . $value)) { $node['children'] = $this->has_children($path . '/' . $value); $node['id'] = $path . '/' . $value; $node['icon'] = 'dashicons dashicons-category cx-icon-color-techblue'; $node['text'] =''.$value.''; $node['text'] .=''; $node_array[] = $node; } } else{ continue; } } } } $ret['nodes'] = $node_array; echo wp_json_encode($ret); } 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); echo wp_json_encode(array('result'=>'failed','error'=>$message)); } die(); } public function has_children($path) { if ($dh = opendir($path)) { while (substr($path, -1) == '/') { $path = rtrim($path, '/'); } $skip_paths = array(".", ".."); while (($value = readdir($dh)) !== false) { trailingslashit(str_replace('\\', '/', $value)); if (!in_array($value, $skip_paths)) { if (is_dir($path . '/' . $value)) { return true; } } } } return false; } public function is_media_folder($path) { $upload_dir = wp_upload_dir(); $base_dir = $upload_dir['basedir']; $base_dir = trailingslashit(str_replace('\\', '/', realpath($base_dir))); $path=str_replace( $base_dir, '', $path ); $path_arr = explode( '/', $path ); if ( count( $path_arr ) >= 1) { if(is_numeric( $path_arr[0] ) && $path_arr[0] > 1950 && $path_arr[0] < 2050) { return true; } } $path=$this->transfer_path($path); $upload_root=$this->transfer_path($upload_dir['basedir']); $compressx1=$this->transfer_path(WP_CONTENT_DIR.'/compressx'); $compressx2=$this->transfer_path(WP_CONTENT_DIR.'/compressx-nextgen'); if($path==$compressx1) { return true; } else if($path==$compressx2) { return true; } else if($path==$upload_root) { return true; } return false; } public function get_children_count($path,$convert_to_avif) { try { $rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); $files = 0; /** @var SplFileInfo $file */ foreach ($rii as $file) { if ($file->isDir()) { continue; } if($this->is_support_extension($file->getExtension())) { $filename=$path.'/'.$file->getFilename(); $type=pathinfo($filename, PATHINFO_EXTENSION); if($type=='webp'&&$convert_to_avif) { $files++; } else { $files++; } } } return $files; } catch (Exception $e) { return 0; } } public function add_include_folders() { global $compressx; $compressx->ajax_check_security('compressx-can-bulk-custom-convert'); $includes=CompressX_Options::get_option('compressx_custom_includes',array()); $new_include = sanitize_text_field($_POST['id']); $new_includes[$new_include]=$new_include; $includes=array_merge($includes,$new_includes); CompressX_Options::update_option('compressx_custom_includes',$includes); $stats=array(); $stats['files']=0; $stats['saved']=0; $stats['processed_files']=0; $stats['total']=0; $stats['processed']=0; $convert_to_webp=CompressX_Image_Opt_Method::get_convert_to_webp(); $convert_to_avif=CompressX_Image_Opt_Method::get_convert_to_avif(); $abs_path = trailingslashit(str_replace('\\', '/', realpath(ABSPATH))); $html='