output_header();
$this->output_settings();
$this->output_save_section();
$this->output_footer();
?>
output_thumbnail_sizes(); ?>
output_exclude_folders(); ?>
output_custom_folders(); ?>
output_cache_control_settings(); ?>
output_scan_page_settings(); ?>
output_scan_max_count_settings(); ?>
output_delete_images(); ?>
$_wp_additional_image_sizes[$size_key]['width'],
'height' => $_wp_additional_image_sizes[$size_key]['height'],
'crop' => $_wp_additional_image_sizes[$size_key]['crop'],
);
if (isset($options['skip_size'][$size_key]) && $options['skip_size'][$size_key]) {
$image_sizes[$size_key]['skip'] = true;
} else {
$image_sizes[$size_key]['skip'] = false;
}
}
}
if (! isset($image_sizes['medium_large']) || empty($image_sizes['medium_large'])) {
$width = intval(get_option('medium_large_size_w'));
$height = intval(get_option('medium_large_size_h'));
$image_sizes['medium_large'] = array(
'width' => $width,
'height' => $height,
);
if (isset($options['skip_size']['medium_large']) && $options['skip_size']['medium_large']) {
$image_sizes['medium_large']['skip'] = true;
} else {
$image_sizes['medium_large']['skip'] = false;
}
}
?>
output_tooltip('', esc_html__('Choose thumbnail sizes you want to process. Some themes may generate new thumbnail sizes sometimes, then you\'ll need to reprocess the thumbnails sizes using the Bulk Processing function.', 'compressx'), 'large'); ?>
output_tooltip('', esc_html__('Select folders in the Uploads folder (media library) and exclude them from the processing.', 'compressx'), 'large'); ?>
get_custom_stats($custom_folders);
$found = $stats['files'];
$saved = $stats['saved'];
$processed = $stats['processed_files'];
?>
output_tooltip('', esc_html__('Select custom folders in the /wp-content folder and process images inside them.', 'compressx'), 'large'); ?>
'compressx-v2-h-4 compressx-v2-w-4',
'medium' => 'compressx-v2-h-5 compressx-v2-w-5',
'large' => 'compressx-v2-h-6 compressx-v2-w-6'
);
$button_class = isset($size_classes[$button_size]) ? $size_classes[$button_size] : $size_classes['medium'];
?>
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;
}
private function get_folder_stats($path, &$stats, $convert_to_webp, $convert_to_avif)
{
try {
if (!class_exists('CompressX_Custom_Image_Meta')) {
return 0;
}
$support_extension = array('jpg', 'jpeg', 'png', 'webp');
$count = 0;
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
foreach ($rii as $file) {
if ($file->isDir()) {
continue;
}
$extension = strtolower($file->getExtension());
if (in_array($extension, $support_extension)) {
$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;
}
}
private function transfer_path($path)
{
$path = str_replace('\\', '/', $path);
$values = explode('/', $path);
return implode(DIRECTORY_SEPARATOR, $values);
}
public function get_custom_tree_dir()
{
global $compressx;
$compressx->ajax_check_security('compressx-can-use-exclude');
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']);
}
/*
$node_array[] = array(
'text' => basename($path),
'children' => true,
'id' => $path,
'icon' => 'dashicons dashicons-category cx-icon-color-yellow',
'state' => array(
'opened' => true
)
);*/
} else {
$path = sanitize_text_field($_POST['tree_node']['node']['id']);
}
$upload_dir = wp_upload_dir();
$uploads_path = $upload_dir['basedir'];
$uploads_path = str_replace('\\', '/', $uploads_path);
$uploads_path = $uploads_path . '/';
if ($path == $uploads_path) {
$init = true;
$key = gmdate('Y', time());
} else {
$init = false;
$key = gmdate('Y', time());
}
$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)) {
$node['children'] = $this->has_children($path . '/' . $value);
$node['id'] = $path . '/' . $value;
if ($init) {
if ($value == $key) {
$node['state']['opened'] = true;
}
}
//
$node['icon'] = 'dashicons dashicons-open-folder cx-icon-color-techblue compressx-text';
$node['children_count'] = $this->get_children_count($path . '/' . $value);
$node['text'] = $value . ',(' . $node['children_count'] . ') images';
$node['text'] .= ' ';
$node_array[] = $node;
/*
if($this->is_media_folder($path . '/' . $value))
{
$node['children'] = $this->has_children($path . '/' . $value);
$node['id'] = $path . '/' . $value;
if($init)
{
if($value==$key)
{
$node['state']['opened']=true;
}
}
//
$node['icon'] = 'dashicons dashicons-open-folder cx-icon-color-techblue compressx-text';
$node['children_count']=$this->get_children_count($path . '/' . $value);
$node['text'] = $value.',('. $node['children_count'].') images';
$node['text'] .=' ';
$node_array[] = $node;
}
else
{
continue;
}*/
} else {
//$node['text'] = $value;
//$node['children'] = true;
//$node['id'] = $path . '/' . $value;
//$node['icon'] = 'dashicons dashicons-media-default cx-icon-color-yellow';
//$node_array[] = $node;
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;
}
}
return false;
}
public function get_children_count($path)
{
try {
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
$files = 0;
/** @var SplFileInfo $file */
foreach ($rii as $file) {
if ($file->isDir()) {
continue;
}
$files++;
}
return $files;
} catch (Exception $e) {
return 0;
}
}
public function add_exclude_folders()
{
global $compressx;
$compressx->ajax_check_security('compressx-can-use-exclude');
$json_excludes = sanitize_text_field($_POST['excludes']);
$json_excludes = stripslashes($json_excludes);
$new_excludes = json_decode($json_excludes, true);
CompressX_Options::update_option('compressx_media_excludes', $new_excludes);
$abs_path = trailingslashit(str_replace('\\', '/', realpath(ABSPATH)));
$html = '';
foreach ($new_excludes as $exclude) {
$path = str_replace($abs_path, '', $exclude);
$html .= ' ' .
'' . $path . ',(' . $this->get_children_count($exclude) . ') images' . ' ' .
' ';
}
$html .= '';
$ret['result'] = 'success';
$ret['html'] = $html;
echo wp_json_encode($ret);
die();
}
public function add_exclude_folder()
{
global $compressx;
$compressx->ajax_check_security('compressx-can-use-exclude');
$exclude = sanitize_text_field($_POST['id']);
$abs_path = trailingslashit(str_replace('\\', '/', realpath(ABSPATH)));
$path = str_replace($abs_path, '', $exclude);
$html = ' ' .
'' . $path . ',(' . $this->get_children_count($exclude) . ') images' . ' ' .
' ';
$ret['result'] = 'success';
$ret['html'] = $html;
echo wp_json_encode($ret);
die();
}
public function remove_exclude_folders()
{
global $compressx;
$compressx->ajax_check_security('compressx-can-use-exclude');
$excludes = CompressX_Options::get_option('compressx_media_excludes', array());
$id = sanitize_text_field($_POST['id']);
unset($excludes[$id]);
CompressX_Options::update_option('compressx_media_excludes', $excludes);
$ret['result'] = 'success';
echo wp_json_encode($ret);
die();
}
public function save_settings()
{
global $compressx;
$compressx->ajax_check_security();
if (isset($_POST['settings']) && !empty($_POST['settings']))
{
$json_settings = sanitize_text_field($_POST['settings']);
$json_settings = stripslashes($json_settings);
$settings = json_decode($json_settings, true);
if (is_null($settings))
{
$ret['result'] = 'failed';
$ret['error'] = 'json decode failed';
echo wp_json_encode($ret);
die();
}
}
else
{
die();
}
$options = CompressX_Options::get_option('compressx_general_settings', array());
if (isset($settings['size_settings']))
{
$intermediate_image_sizes = get_intermediate_image_sizes();
if (isset($settings['size_settings']['og'])) {
$options['skip_size']['og'] = !$settings['size_settings']['og'];
} else {
$options['skip_size']['og'] = true;
}
foreach ($intermediate_image_sizes as $size_key) {
if (isset($settings['size_settings'][$size_key])) {
$options['skip_size'][$size_key] = !$settings['size_settings'][$size_key];
} else {
$options['skip_size'][$size_key] = true;
}
}
}
// Handle list_view_setting (Media Library Display Options)
if (isset($settings['list_view_setting'])) {
$media_library_list_view = CompressX_Options::get_option('compressx_media_library_list_view', array());
$media_library_list_view['delete_thumbnails'] = isset($settings['list_view_setting']['delete_thumbnails']) && $settings['list_view_setting']['delete_thumbnails'] == '1';
$media_library_list_view['delete_thumbnails_saved'] = isset($settings['list_view_setting']['delete_thumbnails_saved']) && $settings['list_view_setting']['delete_thumbnails_saved'] == '1';
$media_library_list_view['dropdown_delete_thumbnails'] = isset($settings['list_view_setting']['dropdown_delete_thumbnails']) && $settings['list_view_setting']['dropdown_delete_thumbnails'] == '1';
$media_library_list_view['watermark_status'] = isset($settings['list_view_setting']['watermark_status']) && $settings['list_view_setting']['watermark_status'] == '1';
$media_library_list_view['dropdown_watermark'] = isset($settings['list_view_setting']['dropdown_watermark']) && $settings['list_view_setting']['dropdown_watermark'] == '1';
CompressX_Options::update_option('compressx_media_library_list_view', $media_library_list_view);
}
// Handle cache_control_setting
$reset_rewrite = false;
if (isset($settings['cache_control_setting']))
{
if (isset($settings['cache_control_setting']['disable_cache_control']))
{
$old_value = isset($options['disable_cache_control']) ? $options['disable_cache_control'] : false;
$new_value = $settings['cache_control_setting']['disable_cache_control'] == '1';
if ($old_value != $new_value)
{
$reset_rewrite = true;
$options['disable_cache_control'] = $new_value;
}
}
}
// Handle scan_page_setting
if (isset($settings['scan_page_setting']))
{
if (isset($settings['scan_page_setting']['scan_images_page']))
{
$scan_images_page = intval($settings['scan_page_setting']['scan_images_page']);
if ($scan_images_page <= 0)
{
$scan_images_page = 500;
}
if ($scan_images_page < 50)
{
$scan_images_page = 50;
}
if ($scan_images_page > 5000)
{
$scan_images_page = 5000;
}
$options['scan_images_page'] = $scan_images_page;
}
if (isset($settings['scan_page_setting']['scan_images_max_count']))
{
$scan_images_max_count = intval($settings['scan_page_setting']['scan_images_max_count']);
if ($scan_images_max_count <= 0)
{
$scan_images_max_count = 10000;
}
if ($scan_images_max_count < 500)
{
$scan_images_max_count = 500;
}
if ($scan_images_max_count > 100000)
{
$scan_images_max_count = 100000;
}
$options['scan_images_max_count'] = $scan_images_max_count;
}
}
CompressX_Options::update_option('compressx_general_settings', $options);
if ($reset_rewrite)
{
$image_load = isset($options['image_load']) ? $options['image_load'] : 'htaccess';
if ($image_load == 'htaccess')
{
include_once COMPRESSX_DIR . '/includes/class-compressx-webp-rewrite.php';
$rewrite = new CompressX_Webp_Rewrite();
$rewrite->create_rewrite_rules();
}
else if ($image_load == 'compat_htaccess')
{
include_once COMPRESSX_DIR . '/includes/class-compressx-webp-rewrite.php';
$rewrite = new CompressX_Webp_Rewrite();
$rewrite->create_rewrite_rules_ex();
}
}
if (isset($_POST['excludes']) && !empty($_POST['excludes']))
{
$json_settings = sanitize_text_field($_POST['excludes']);
$json_settings = stripslashes($json_settings);
$excludes = json_decode($json_settings, true);
if (is_null($excludes))
{
$ret['result'] = 'failed';
$ret['error'] = 'json decode failed';
echo wp_json_encode($ret);
die();
}
$old_excludes = CompressX_Options::get_option('compressx_media_excludes', array());
$excludes_changed = $this->is_excludes_changed($old_excludes, $excludes);
CompressX_Options::update_option('compressx_media_excludes', $excludes);
if ($excludes_changed)
{
CompressX_Image_Meta_V2::clear_image_meta_status();
}
}
$ret['result'] = 'success';
echo wp_json_encode($ret);
die();
}
private function is_excludes_changed($old_excludes, $new_excludes)
{
if (!is_array($old_excludes)) {
$old_excludes = array();
}
if (!is_array($new_excludes)) {
$new_excludes = array();
}
$old_excludes = $this->normalize_excludes_for_compare($old_excludes);
$new_excludes = $this->normalize_excludes_for_compare($new_excludes);
return md5(wp_json_encode($old_excludes)) !== md5(wp_json_encode($new_excludes));
}
private function normalize_excludes_for_compare($excludes)
{
if (!is_array($excludes)) {
return array();
}
ksort($excludes);
foreach ($excludes as $key => $value) {
if (is_array($value)) {
$excludes[$key] = $this->normalize_excludes_for_compare($value);
} else {
if ($value === '1' || $value === 1 || $value === true) {
$excludes[$key] = true;
} else if ($value === '0' || $value === 0 || $value === false) {
$excludes[$key] = false;
} else {
$excludes[$key] = $value;
}
}
}
return $excludes;
}
public function delete_files()
{
global $compressx;
$compressx->ajax_check_security('compressx-can-delete');
global $wpdb;
$where['meta_key'] = "compressx_image_meta_status";
$wpdb->delete($wpdb->postmeta, $where);
$where['meta_key'] = "compressx_image_meta_webp_converted";
$wpdb->delete($wpdb->postmeta, $where);
$where['meta_key'] = "compressx_image_meta_avif_converted";
$wpdb->delete($wpdb->postmeta, $where);
$where['meta_key'] = "compressx_image_meta_compressed";
$wpdb->delete($wpdb->postmeta, $where);
$where['meta_key'] = "compressx_image_meta_og_file_size";
$wpdb->delete($wpdb->postmeta, $where);
$where['meta_key'] = "compressx_image_meta_webp_converted_size";
$wpdb->delete($wpdb->postmeta, $where);
$where['meta_key'] = "compressx_image_meta_avif_converted_size";
$wpdb->delete($wpdb->postmeta, $where);
$where['meta_key'] = "compressx_image_meta_compressed_size";
$wpdb->delete($wpdb->postmeta, $where);
$where['meta_key'] = "compressx_image_meta";
$wpdb->delete($wpdb->postmeta, $where);
delete_transient('compressx_set_global_stats');
$table_name = $wpdb->prefix . "compressx_files_opt_meta";
$exists = $wpdb->get_var(
$wpdb->prepare("SHOW TABLES LIKE %s", $table_name)
);
if ($exists == $table_name)
{
$wpdb->get_results("TRUNCATE TABLE $table_name", ARRAY_A);
}
CompressX_Image_Meta_V2::delete_all_image_meta();
$this->_delete_files();
$ret['result'] = "success";
echo wp_json_encode($ret);
die();
}
public function _delete_files()
{
$path = WP_CONTENT_DIR . '/compressx-nextgen';
$this->deleteDir($path);
include_once COMPRESSX_DIR . '/includes/class-compressx-default-folder.php';
$dir = new CompressX_default_folder();
$dir->create_uploads_dir();
}
public function deleteDir( $dirPath )
{
if ( ! is_dir( $dirPath ) ) {
return false;
}
global $wp_filesystem;
if ( empty( $wp_filesystem ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
WP_Filesystem();
}
if ( empty( $wp_filesystem ) ) {
return false;
}
// Recursive delete.
return $wp_filesystem->rmdir( $dirPath, true );
}
private function get_custom_children_count($path)
{
try
{
$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;
}
$support_extension = array('jpg', 'jpeg', 'png', 'webp');
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
$files = 0;
foreach ($rii as $file) {
if ($file->isDir()) {
continue;
}
$extension = strtolower($file->getExtension());
if (in_array($extension, $support_extension)) {
$filename = $path . '/' . $file->getFilename();
$type = pathinfo($filename, PATHINFO_EXTENSION);
if ($type == 'webp' && $convert_to_avif) {
$files++;
} else if ($type != 'webp') {
$files++;
}
}
}
return $files;
} catch (Exception $e) {
return 0;
}
}
}