Skip to content

Conversation

@Arukuen
Copy link

@Arukuen Arukuen commented Dec 16, 2025

fixes #4

Summary by CodeRabbit

  • Bug Fixes
    • Improved robustness of media manager metadata display with enhanced system validation checks and better handling of missing data.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Dec 16, 2025

Walkthrough

This refactoring introduces a reusable injectCimoMetadata function to render media optimization metadata into admin containers. It replaces inline augmentation logic, strengthens initial readiness checks for wp.media, and centralizes metadata injection to eliminate duplication across different rendering paths.

Changes

Cohort / File(s) Change Summary
Metadata Injection Refactoring
src/admin/js/media-manager/sidebar-info.js
Extracts metadata injection into a new top-level function injectCimoMetadata({ model, container }) that handles rendering, duplicate guards, and cached fallback data. Updates Details.template and TwoColumn detail views to call this function. Strengthens wp.media readiness guard to verify presence of wp.media.view.Attachment.Details.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Areas requiring attention:
    • Verify the strengthened guard checks properly validate the entire wp.media chain before execution
    • Confirm duplicate-prevention logic correctly prevents metadata re-injection in both rendering paths
    • Validate that the cached metadata fallback appropriately handles missing or undefined data
    • Test that the fix resolves the reported issue where stats don't display after uploading images via Media > Add Media

Poem

🐰 Metadata once scattered and shy,
Now gathered in function on high!
No duplication, no hidden states—
Stats flow cleanly through the gates,
A sidebar bright where uploads relate! ✨

Pre-merge checks

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change—adding Cimo metadata to admin media in grid view, which directly addresses the linked issue about missing stats in the Media Manager.
Linked Issues check ✅ Passed The PR introduces the injectCimoMetadata function and integrates it into both Details.template and TwoColumn views to display Cimo metadata in the admin media interface, addressing the core requirement from issue #4.
Out of Scope Changes check ✅ Passed All changes are focused on injecting Cimo metadata into the media manager sidebar and grid views; no unrelated modifications were introduced outside the stated objectives.

Note

Free review on us!

CodeRabbit is offering free reviews until Wed Dec 17 2025 to showcase some of the refinements we've made.

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5ae075e and 3fc73d9.

📒 Files selected for processing (1)
  • src/admin/js/media-manager/sidebar-info.js (1 hunks)
🔇 Additional comments (4)
src/admin/js/media-manager/sidebar-info.js (4)

65-88: LGTM - Solid guard checks and fallback logic.

The duplicate prevention and fallback to cached metadata appropriately addresses the issue of newly uploaded images not having Cimo data immediately available on the model.


179-196: Verify view.model exists in template context.

In WordPress Backbone media views, the template method typically receives serialized model data (from toJSON()), not the view object itself. If view is just the model attributes object, then view.model on line 190 would be undefined, causing injectCimoMetadata to exit early without injecting metadata.

Compare with the TwoColumn approach (line 212) which correctly accesses this.model within render().

Please verify this works correctly in the editor media modal. If view.model is indeed undefined, consider restructuring to match the TwoColumn pattern:

 wp.media.view.Attachment.Details =
   wp.media.view.Attachment.Details.extend( {
-    template( view ) {
-      const html = wp.media.template( 'attachment-details' )( view )
-      const dom = document.createElement( 'div' )
-      dom.innerHTML = html
-
-      const container = dom.querySelector( '.attachment-info' )
-
-      injectCimoMetadata( {
-        model: view.model,
-        container,
-      } )
-
-      return dom.innerHTML
-    },
+    render() {
+      wp.media.view.Attachment.Details.__super__.render.apply( this, arguments )
+
+      const container = this.el.querySelector( '.attachment-info' )
+
+      injectCimoMetadata( {
+        model: this.model,
+        container,
+      } )
+
+      return this
+    },
   } )

169-177: LGTM - Thorough guard check.

The expanded guard correctly verifies the entire chain of wp.media.view.Attachment.Details before proceeding.


198-219: LGTM - TwoColumn extension follows correct Backbone pattern.

Properly stores the original constructor, extends it, calls parent render via prototype, and accesses this.model appropriately.

Comment on lines +108 to +121
const optimizationSavings = customMetadata.compressionSavings
? ( 100 - ( customMetadata.compressionSavings * 100 ) ).toFixed( 2 )
: null

const kbSaved = formatFilesize(
customMetadata.originalFilesize - customMetadata.convertedFilesize,
1,
true
)

const optimizationSavingsClass =
optimizationSavings > 0
? 'cimo-optimization-savings-up'
: 'cimo-optimization-savings-down'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Edge cases: handle compressionSavings === 0 and undefined filesizes.

Two potential edge cases:

  1. If compressionSavings is exactly 0, it's falsy and results in null, causing "Saved null%" to render.
  2. If originalFilesize or convertedFilesize is undefined, the subtraction yields NaN, causing formatFilesize to return NaN undefined.

Consider defensive handling:

-const optimizationSavings = customMetadata.compressionSavings
-  ? ( 100 - ( customMetadata.compressionSavings * 100 ) ).toFixed( 2 )
-  : null
+const optimizationSavings = typeof customMetadata.compressionSavings === 'number'
+  ? ( 100 - ( customMetadata.compressionSavings * 100 ) ).toFixed( 2 )
+  : null

-const kbSaved = formatFilesize(
-  customMetadata.originalFilesize - customMetadata.convertedFilesize,
+const originalSize = parseInt( customMetadata.originalFilesize ) || 0
+const convertedSize = parseInt( customMetadata.convertedFilesize ) || 0
+const kbSaved = formatFilesize(
+  originalSize - convertedSize,
   1,
   true
 )
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const optimizationSavings = customMetadata.compressionSavings
? ( 100 - ( customMetadata.compressionSavings * 100 ) ).toFixed( 2 )
: null
const kbSaved = formatFilesize(
customMetadata.originalFilesize - customMetadata.convertedFilesize,
1,
true
)
const optimizationSavingsClass =
optimizationSavings > 0
? 'cimo-optimization-savings-up'
: 'cimo-optimization-savings-down'
const optimizationSavings = typeof customMetadata.compressionSavings === 'number'
? ( 100 - ( customMetadata.compressionSavings * 100 ) ).toFixed( 2 )
: null
const originalSize = parseInt( customMetadata.originalFilesize ) || 0
const convertedSize = parseInt( customMetadata.convertedFilesize ) || 0
const kbSaved = formatFilesize(
originalSize - convertedSize,
1,
true
)
const optimizationSavingsClass =
optimizationSavings > 0
? 'cimo-optimization-savings-up'
: 'cimo-optimization-savings-down'

Copy link

@bfintal bfintal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deleted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Uploading an image via Media > Add Media File doesn't show stats

3 participants