diff --git a/inc/customizer.php b/inc/customizer.php index 54f4f38..546d61b 100644 --- a/inc/customizer.php +++ b/inc/customizer.php @@ -84,7 +84,7 @@ function zeitfresser_customize_register( $wp_customize ) { 'type' => 'checkbox', 'section' => 'ztfr_performance_tools', 'label' => 'Auto Delete Original Pictures on Upload', - 'description' => 'Automatically deletes original images after optimization. ⚠️ This action cannot be undone.', + 'description' => 'Automatically deletes original images after optimization.', ) ); } @@ -236,3 +236,29 @@ function zeitfresser_customize_controls_dependency_js() { + + 'attachment', - 'post_status'=>'inherit', - 'post_mime_type'=>'image', - 'posts_per_page'=>1, - 'fields'=>'ids', - 'meta_query'=>[ - 'relation'=>'AND', - ['key'=>'_zeitfresser_original_file','compare'=>'EXISTS'], - ['key'=>'_zeitfresser_original_deleted','compare'=>'NOT EXISTS'] + 'post_type' => 'attachment', + 'post_status' => 'inherit', + 'post_mime_type' => 'image', + 'posts_per_page' => 1, + 'fields' => 'ids', + 'meta_query' => [ + 'relation' => 'AND', + [ + 'key' => '_zeitfresser_original_file', + 'compare' => 'EXISTS', + ], + [ + 'key' => '_zeitfresser_original_deleted', + 'compare' => 'NOT EXISTS', + ], + [ + 'key' => '_zeitfresser_media_optimized_version', + 'value' => ZEITFRESSER_IMAGE_OPTIMIZATION_VERSION, + 'compare' => '=', + ], ], - 'no_found_rows'=>false + 'no_found_rows' => false, ]); + + return (int) $query->found_posts; +} + +function zeitfresser_get_unoptimized_originals_count() { + $query = new WP_Query([ + 'post_type' => 'attachment', + 'post_status' => 'inherit', + 'post_mime_type' => 'image', + 'posts_per_page' => 1, + 'fields' => 'ids', + 'meta_query' => [ + 'relation' => 'AND', + [ + 'key' => '_zeitfresser_original_file', + 'compare' => 'EXISTS', + ], + [ + 'key' => '_zeitfresser_original_deleted', + 'compare' => 'NOT EXISTS', + ], + [ + 'relation' => 'OR', + [ + 'key' => '_zeitfresser_media_optimized_version', + 'compare' => 'NOT EXISTS', + ], + [ + 'key' => '_zeitfresser_media_optimized_version', + 'value' => ZEITFRESSER_IMAGE_OPTIMIZATION_VERSION, + 'compare' => '!=', + ], + ], + ], + 'no_found_rows' => false, + ]); + return (int) $query->found_posts; } @@ -374,11 +422,27 @@ function zeitfresser_ajax_optimize_images() { $results = zeitfresser_process_legacy_images_batch( 25 ); + $pending = zeitfresser_get_pending_legacy_images_count(); + $total = zeitfresser_get_total_images_count(); + + $cleanup_total = zeitfresser_get_total_originals_count(); + $cleanup_remaining = zeitfresser_get_remaining_originals_count(); + $cleanup_unoptimized = zeitfresser_get_unoptimized_originals_count(); + $cleanup_deleted = $cleanup_total - ( $cleanup_remaining + $cleanup_unoptimized ); + $cleanup_progress = $cleanup_total > 0 + ? round( ( $cleanup_deleted / $cleanup_total ) * 100 ) + : 0; + wp_send_json_success([ - 'processed' => $results['processed'], - 'updated' => $results['updated'], - 'pending' => zeitfresser_get_pending_legacy_images_count(), - 'total' => zeitfresser_get_total_images_count(), + 'processed' => $results['processed'], + 'updated' => $results['updated'], + 'pending' => $pending, + 'total' => $total, + 'cleanup_total' => $cleanup_total, + 'cleanup_remaining' => $cleanup_remaining, + 'cleanup_unoptimized' => $cleanup_unoptimized, + 'cleanup_deleted' => $cleanup_deleted, + 'cleanup_progress' => $cleanup_progress, ]); } add_action( 'wp_ajax_zeitfresser_optimize_images', 'zeitfresser_ajax_optimize_images' ); @@ -394,16 +458,18 @@ function zeitfresser_ajax_delete_originals() { check_ajax_referer( 'zeitfresser_performance_tools', 'nonce' ); - $deleted = zeitfresser_delete_originals_batch( 10 ); - - $total = zeitfresser_get_total_originals_count(); - $remaining = zeitfresser_get_remaining_originals_count(); + $deleted = zeitfresser_delete_originals_batch( 10 ); + $total = zeitfresser_get_total_originals_count(); + $remaining = zeitfresser_get_remaining_originals_count(); + $unoptimized = zeitfresser_get_unoptimized_originals_count(); + $deleted_total = $total - ( $remaining + $unoptimized ); wp_send_json_success([ 'deleted' => $deleted, 'total' => $total, 'remaining' => $remaining, - 'deleted_total' => $total - $remaining, + 'unoptimized' => $unoptimized, + 'deleted_total' => $deleted_total, ]); } add_action( 'wp_ajax_zeitfresser_delete_originals', 'zeitfresser_ajax_delete_originals' ); @@ -423,10 +489,24 @@ function zeitfresser_render_performance_tools_page() { $progress = $total > 0 ? round(($optimized / $total) * 100) : 0; // 🔥 NEW: Cleanup counters - $cleanup_total = zeitfresser_get_total_originals_count(); - $cleanup_remaining = zeitfresser_get_remaining_originals_count(); - $cleanup_deleted = $cleanup_total - $cleanup_remaining; - $cleanup_progress = $cleanup_total > 0 ? round(($cleanup_deleted / $cleanup_total) * 100) : 0; + $cleanup_total = zeitfresser_get_total_originals_count(); + $cleanup_remaining = zeitfresser_get_remaining_originals_count(); + $cleanup_unoptimized = zeitfresser_get_unoptimized_originals_count(); + $cleanup_deleted = $cleanup_total - ( $cleanup_remaining + $cleanup_unoptimized ); + $cleanup_progress = $cleanup_total > 0 ? round(($cleanup_deleted / $cleanup_total) * 100) : 0; + + $cleanup_nothing_to_do = ( + 0 === $cleanup_remaining && + ( + $cleanup_unoptimized > 0 || + $cleanup_deleted >= $cleanup_total + ) + ); + + $cleanup_button_disabled = $cleanup_nothing_to_do; + $cleanup_button_label = $cleanup_nothing_to_do + ? '🧹 Nothing to clean yet' + : '🧹 Delete Originals'; ?>
Total Originals:
Deleted:
-Remaining:
+Ready for Cleanup:
+Not Optimized Yet:
Cleanup Progress: %