-
Notifications
You must be signed in to change notification settings - Fork 0
Feat: bulk optimization #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7ee34f4
c8f4854
d4a360a
b5b583c
a7762b0
69a2fbd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -159,6 +159,9 @@ public function save_metadata( $request ) { | |
| } | ||
| } | ||
|
|
||
| // Add our metadata key to note that this was optimized during upload. | ||
| $sanitized_metadata['optimized_during_upload'] = true; | ||
|
|
||
|
Comment on lines
+162
to
+164
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Data structure inconsistency: mixing non-numeric key into a numerically-indexed array.
Consider storing this flag separately or restructuring to avoid mixing numeric and associative keys: Proposed fix- // Add our metadata key to note that this was optimized during upload.
- $sanitized_metadata['optimized_during_upload'] = true;
-
// Save to a transient queue for metadata waiting for attachment creation
$transient_key = 'cimo_metadata_queue';
$queue = get_transient( $transient_key );
if ( ! is_array( $queue ) ) {
$queue = [];
}
// Append new metadata to the queue
- $queue = array_merge( $queue, $sanitized_metadata );
+ // Mark each entry as optimized during upload
+ foreach ( $sanitized_metadata as &$entry ) {
+ $entry['optimized_during_upload'] = true;
+ }
+ $queue = array_merge( $queue, $sanitized_metadata );🤖 Prompt for AI Agents |
||
| // Save to a transient queue for metadata waiting for attachment creation | ||
| $transient_key = 'cimo_metadata_queue'; | ||
| $queue = get_transient( $transient_key ); | ||
|
|
@@ -285,6 +288,11 @@ public function prepare_attachment_for_js( $response, $attachment, $meta ) { | |
| * @return array The updated metadata. | ||
| */ | ||
| public function preserve_cimo_metadata( $metadata, $attachment_id ) { | ||
| // If there's a metadata for cimo incoming, then normal operation. | ||
| if ( isset( $metadata['cimo'] ) ) { | ||
| return $metadata; | ||
| } | ||
|
|
||
| // Get the existing Cimo metadata from the attachment metadata. | ||
| $existing_metadata = wp_get_attachment_metadata( $attachment_id ); | ||
| if ( $existing_metadata && isset( $existing_metadata['cimo'] ) ) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -84,6 +84,25 @@ private static function compute_stats( $existing_stats = null ) { | |
| } | ||
|
|
||
| $cimo_data = $metadata['cimo']; | ||
|
|
||
| /** | ||
| * This data comes from bulk optimizing media in the Media Library: | ||
| */ | ||
|
|
||
| if ( isset( $cimo_data['bulk_optimization'] ) ) { | ||
| foreach ( $cimo_data['bulk_optimization'] as $size => $data ) { | ||
| $updated_stats['media_optimized_num']++; | ||
| $original_size_b = isset( $data['originalFilesize'] ) ? (int) $data['originalFilesize'] : 0; | ||
| $converted_size_b = isset( $data['convertedFilesize'] ) ? (int) $data['convertedFilesize'] : 0; | ||
| $updated_stats['total_original_size'] += $original_size_b / 1024; | ||
| $updated_stats['total_optimized_size'] += $converted_size_b / 1024; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * This data comes from individually optimized images from the user uploading media: | ||
| */ | ||
|
|
||
| $updated_stats['media_optimized_num']++; | ||
|
Comment on lines
+87
to
106
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Double-counting issue: When Proposed fix: only count individually optimized images when bulk_optimization is absent if ( isset( $cimo_data['bulk_optimization'] ) ) {
foreach ( $cimo_data['bulk_optimization'] as $size => $data ) {
$updated_stats['media_optimized_num']++;
$original_size_b = isset( $data['originalFilesize'] ) ? (int) $data['originalFilesize'] : 0;
$converted_size_b = isset( $data['convertedFilesize'] ) ? (int) $data['convertedFilesize'] : 0;
$updated_stats['total_original_size'] += $original_size_b / 1024;
$updated_stats['total_optimized_size'] += $converted_size_b / 1024;
}
- }
-
- /**
- * This data comes from individually optimized images from the user uploading media:
- */
-
- $updated_stats['media_optimized_num']++;
-
- // Extract file sizes (bytes) and add to KB totals
- $original_size_b = isset( $cimo_data['originalFilesize'] ) ? (int) $cimo_data['originalFilesize'] : 0;
- $converted_size_b = isset( $cimo_data['convertedFilesize'] ) ? (int) $cimo_data['convertedFilesize'] : 0;
-
- $updated_stats['total_original_size'] += $original_size_b / 1024;
- $updated_stats['total_optimized_size'] += $converted_size_b / 1024;
+ } else {
+ /**
+ * This data comes from individually optimized images from the user uploading media:
+ */
+ $updated_stats['media_optimized_num']++;
+
+ // Extract file sizes (bytes) and add to KB totals
+ $original_size_b = isset( $cimo_data['originalFilesize'] ) ? (int) $cimo_data['originalFilesize'] : 0;
+ $converted_size_b = isset( $cimo_data['convertedFilesize'] ) ? (int) $cimo_data['convertedFilesize'] : 0;
+
+ $updated_stats['total_original_size'] += $original_size_b / 1024;
+ $updated_stats['total_optimized_size'] += $converted_size_b / 1024;
+ }🧰 Tools🪛 PHPMD (2.15.0)[warning] 93-93: Avoid unused local variables such as '$size'. (undefined) (UnusedLocalVariable) 🤖 Prompt for AI Agents |
||
|
|
||
| // Extract file sizes (bytes) and add to KB totals | ||
|
|
@@ -153,5 +172,35 @@ private static function format_bytes( $bytes, $decimals = 2 ) { | |
|
|
||
| return $value . ' ' . $sizes[ $i ]; | ||
| } | ||
|
|
||
| /** | ||
| * Update stats for when an attachment has been bulk optimized. | ||
| * | ||
| * @param int $attachment_id The attachment ID. | ||
| * @param int $original_size The original file size in bytes. | ||
| * @param int $optimized_size The optimized file size in bytes. | ||
| */ | ||
| public static function update_stats_bulk_optimized( $attachment_id, $original_size, $optimized_size ) { | ||
| $stats = self::get_stats(); | ||
| $stats['media_optimized_num']++; | ||
| $stats['total_original_size'] += $original_size / 1024; | ||
| $stats['total_optimized_size'] += $optimized_size / 1024; | ||
| update_option( self::OPTION_KEY, $stats, false ); | ||
| } | ||
|
|
||
| /** | ||
| * Update stats for when an attachment has been restored from bulk optimization. | ||
| * | ||
| * @param int $attachment_id The attachment ID. | ||
| * @param int $optimized_size The optimized file size in bytes. | ||
| * @param int $restored_size The restored file size in bytes. | ||
| */ | ||
| public static function update_stats_bulk_restored( $attachment_id, $optimized_size, $restored_size ) { | ||
| $stats = self::get_stats(); | ||
| $stats['media_optimized_num']--; | ||
| $stats['total_original_size'] -= $optimized_size / 1024; | ||
| $stats['total_optimized_size'] -= $restored_size / 1024; | ||
| update_option( self::OPTION_KEY, $stats, false ); | ||
| } | ||
|
Comment on lines
+198
to
+204
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verify the restore stats logic: parameter usage appears inverted. The subtraction logic seems backwards:
Also, Suggested fix (verify intended behavior)- public static function update_stats_bulk_restored( $attachment_id, $optimized_size, $restored_size ) {
+ public static function update_stats_bulk_restored( $_attachment_id, $optimized_size, $restored_size ) {
$stats = self::get_stats();
$stats['media_optimized_num']--;
- $stats['total_original_size'] -= $optimized_size / 1024;
- $stats['total_optimized_size'] -= $restored_size / 1024;
+ $stats['total_original_size'] -= $restored_size / 1024;
+ $stats['total_optimized_size'] -= $optimized_size / 1024;
update_option( self::OPTION_KEY, $stats, false );
}🧰 Tools🪛 PHPMD (2.15.0)[warning] 198-198: Avoid unused parameters such as '$attachment_id'. (undefined) (UnusedFormalParameter) 🤖 Prompt for AI Agents |
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Short-circuit the meta box when there are no usable optimization stats.
sidebar-info.jsnow returns early whencompressionSavingsis missing andbulk_optimizationis absent/empty. This path still renders the full box for that state, which leaves blank/zeroed stats, and the bulk percentage calculation still divides by$original_filesizeunconditionally.Suggested guard
📝 Committable suggestion
🧰 Tools
🪛 PHPMD (2.15.0)
[warning] 102-102: Avoid unused local variables such as '$bulk_optimization_sizes'. (undefined)
(UnusedLocalVariable)
[warning] 108-108: Avoid unused local variables such as '$size_key'. (undefined)
(UnusedLocalVariable)
🤖 Prompt for AI Agents