Uh oh!
There was an error while loading. Please reload this page.
Adds Misc helper for file information and formatting - #474
Conversation
Reviewer's GuideIntroduces a new Misc helper providing utilities to retrieve structured attachment metadata and generate accessible, human-readable file detail strings, and wires it into the theme bootstrap. Sequence diagram for retrieving accessible file informationsequenceDiagram
actor Caller
participant Misc as MiscHelper
participant WP_Attach as WordPress_Attachments
participant WP_File as WordPress_FileSystem
participant WP_Post as WordPress_Post
Caller->>Misc: get_file_infos(file_id)
Misc->>WP_Attach: wp_get_attachment_url(file_id)
WP_Attach-->>Misc: file_href
alt file_href empty
Misc-->>Caller: default_file_infos_array
else file_href present
Misc->>WP_Attach: get_attached_file(file_id)
WP_Attach-->>Misc: file_path
alt file_path empty
Misc-->>Caller: default_file_infos_array
else file_path present
Misc->>WP_Post: get_post_mime_type(file_id)
WP_Post-->>Misc: mime_type
Misc->>Misc: get_mime_type(file_id)
alt mime_type empty
Misc-->>Caller: default_file_infos_array
else mime_type present
Misc->>WP_File: wp_filesize(file_path)
WP_File-->>Misc: bytes
Misc->>WP_File: size_format(bytes)
WP_File-->>Misc: file_size
Misc->>WP_Post: get_the_title(file_id)
WP_Post-->>Misc: file_name
Misc->>Misc: get_file_detail(file_name, file_ext, file_size)
Misc-->>Misc: details
Misc->>Misc: get_accessible_file_size_label(file_size)
Misc-->>Misc: accessible_size
Misc->>Misc: get_file_detail(file_name, file_ext, accessible_size)
Misc-->>Misc: details_accessible
Misc->>WP_Post: wp_get_attachment_caption(file_id)
WP_Post-->>Misc: caption
Misc-->>Caller: file_infos_array
end
end
end
Class diagram for new Misc helper functionsclassDiagram
class MiscHelper {
<<namespace>>
+array get_file_infos(int file_id)
+string get_file_detail(string file_name, string file_ext, string file_size)
+string get_mime_type(int file_id)
+string get_accessible_file_size_label(string file_size)
}
class WordPress_Attachments {
+string wp_get_attachment_url(int file_id)
+string get_attached_file(int file_id)
}
class WordPress_Post {
+string get_post_mime_type(int file_id)
+string get_the_title(int file_id)
+string wp_get_attachment_caption(int file_id)
}
class WordPress_FileSystem {
+int wp_filesize(string file_path)
+string size_format(int bytes)
}
class WordPress_I18n {
+string _n(string singular, string plural, int count, string textdomain)
}
MiscHelper --> WordPress_Attach : uses
MiscHelper --> WordPress_Post : uses
MiscHelper --> WordPress_FileSystem : uses
MiscHelper --> WordPress_I18n : uses
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 2 issues, and left some high level feedback:
- In
get_file_infos, the documented/initialized keys (path,size,ext) are no longer present in the returned array, which may break callers relying on that structure; either restore those keys or update usages and the docblock to reflect the new shape. get_mime_typeis documented as returning a string but lacks a return type declaration; adding: stringwould make the contract explicit and consistent with the other helpers.get_accessible_file_size_labelassumes the regex matches and will produce notices if$matches[1]/[2]are missing; consider returning$file_sizeimmediately whenpreg_matchfails to avoid undefined index issues.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments- In `get_file_infos`, the documented/initialized keys (`path`, `size`, `ext`) are no longer present in the returned array, which may break callers relying on that structure; either restore those keys or update usages and the docblock to reflect the new shape.
-`get_mime_type` is documented as returning a string but lacks a return type declaration; adding `: string` would make the contract explicit and consistent with the other helpers.
-`get_accessible_file_size_label` assumes the regex matches and will produce notices if `$matches[1]`/`[2]` are missing; consider returning `$file_size` immediately when `preg_match` fails to avoid undefined index issues.
## Individual Comments### Comment 1
<locationpath="inc/Helpers/Misc.php"line_range="14-23" />
<code_context>
+*/
+function get_file_infos( int $file_id ): array {
+ $file_href = wp_get_attachment_url( $file_id );
+ $file_infos = [+'href' => '',+'file_name' => '',+'path' => '',+'size' => '',+'ext' => '',+'caption' => '',+];
++ if ( empty( $file_href ) ) {
+return $file_infos;+ }
++ $file_path = get_attached_file( $file_id );
++ if ( empty( $file_path ) ) {
+return $file_infos;+ }
++ $file_ext = get_mime_type( $file_id );
++ if ( empty( $file_ext ) ) {
+return $file_infos;+ }
++ $file_size = (string) size_format( wp_filesize( $file_path ) );
+ $file_name = (string) ( get_the_title( $file_id ) ?? '' );
++ return [
</code_context>
<issue_to_address>
**issue (bug_risk):** Returned array shape from get_file_infos is inconsistent between early-return and success paths.
Early returns use the initialized `$file_infos` (with `path`, `size`, `ext`, etc.), but the final return omits some of these keys and adds others (`details`, `details_accessible`). This forces callers to handle multiple shapes and can break existing code expecting `path/size/ext` to always be present. Consider always returning a single, consistent structure (e.g., populate and return `$file_infos`).
</issue_to_address>
### Comment 2
<locationpath="inc/Helpers/Misc.php"line_range="47" />
<code_context>
+'details' => get_file_detail( $file_name, $file_ext, $file_size ),+'details_accessible' => get_file_detail( $file_name, $file_ext, get_accessible_file_size_label( $file_size ) ),+'href' => $file_href,+'caption' => wp_get_attachment_caption( $file_id ),+];
+}
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Caption value might benefit from being normalized to a string.
Because `wp_get_attachment_caption()` may return `false` when no caption exists, consider normalizing it to a string (e.g., cast or map `false` to an empty string) so `get_file_infos()` consistently returns a string for `caption`.
```suggestion 'caption' => (string) wp_get_attachment_caption( $file_id ),```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Note Note pour moi : regarder ce qui a été optimisé sur https://github.com/BeAPI/cnis/blob/refonte-2025/web/app/themes/cnis-2025/inc/Helpers/Misc.php afin de reporter ici si besoin |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
cedric07
commented
Apr 3, 2026
Done, j'ai enlevé une fonction inutilisée bb88906 et ajouté le support des icônes en fonction du type de fichier. |
cedric07
commented
Apr 3, 2026
Il faudra potentiellement regarder les retours du bot sur le fichier initial |
cedric07
commented
Apr 8, 2026
Fixed pour le retour initial sur le tableau par défaut |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Introduces a new helper to provide utility functions for handling file-related data. This helper simplifies retrieving comprehensive attachment details and includes a dedicated function (`get_accessible_file_size_label`) to improve accessibility by transforming file size strings into pluralized, human-readable labels.
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Provides context for translators regarding the `%s` placeholder in pluralized file size unit strings. This ensures more accurate and contextually appropriate translations.
Replaces the `switch` statement with a more modern `match` expression in `get_accessible_file_size_label`. This improves code readability and conciseness while maintaining the existing functionality for parsing and presenting file size units.
Ensures the accurate file extension is obtained by switching from `get_mime_type` to `pathinfo`. The previous method could return a full MIME type, leading to incorrect extension values.
c949b46 to
484a70aCompareCompare extensions case-insensitively via strtolower() and include avif in the image list.
Handle size_format() output that uses NBSP/NNBSP and other Zs chars from number_format_i18n() (e.g. fr_FR). Use UTF-8 regex with a lazy value group, early return on no match, and strip group separators for _n() pluralization.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 7a062ea. Configure here.
| $value = $matches[1] ?? ''; | ||
| $unit = strtolower( $matches[2] ?? '' ); | ||
| // Strip group separators (ASCII space, NBSP, NNBSP) for _n() plural; (int) leaves decimals as floor (e.g. 1.5 -> 1). | ||
| $int_value = (int) str_replace( [ ' ', "\u{00A0}", "\u{202F}" ], '', $value ); |
There was a problem hiding this comment.
Thousands separator stripping misses commas and dots
High Severity
The str_replace on this line strips only space-based group separators (ASCII space, NBSP, NNBSP) before casting to (int), but WordPress's number_format_i18n uses commas in English and dots in German as thousands separators. For a file around ~1 MB, size_format returns "1,023 KB" in English. Since the comma isn't stripped, (int) "1,023" evaluates to 1, causing _n to return the singular form — producing "1,023 kilobyte" instead of "1,023 kilobytes". This affects the current get_file_infos caller for any file between 1,000 and 1,023 of any unit.
Reviewed by Cursor Bugbot for commit 7a062ea. Configure here.
herewithme
commented
Apr 24, 2026
Idéalement, faudrait stocker l'info dans une méta, et éviter tout ces processus couteux ^^ |
francoistibo
commented
May 18, 2026
vue en réunion de synchro le 18.05, @herewithme checke de son côté |
herewithme
commented
Jul 16, 2026
Il faut intégrer un cache objet en tout début de Pour la taille, inutile de la recalculer : depuis WP 6.0 le core la stocke en bytes dans la metadata d'attachment ( |


Introduces a new helper to provide utility functions for handling file-related data.
This helper simplifies retrieving comprehensive attachment details and includes a dedicated function (
get_accessible_file_size_label) to improve accessibility by transforming file size strings into pluralized, human-readable labels.Summary by Sourcery
Introduce a miscellaneous helper module for file metadata retrieval and accessible file size formatting.
New Features:
Enhancements:
Note
Low Risk
Low risk: additive helper functions and new SVG assets, with minimal impact outside places that opt into using the new utilities.
Overview
Adds a new
inc/Helpers/Misc.phphelper exposingget_file_infos()to return structured attachment metadata (name, URL, caption) plus derived display strings and an icon key.Includes formatting helpers for file details and an accessibility-focused
get_accessible_file_size_label()that convertssize_format()output into localized, pluralized unit labels for screen readers. Wires the helper intofunctions.phpand adds newfile/file-imagesprite SVGs.Reviewed by Cursor Bugbot for commit 7a062ea. Bugbot is set up for automated code reviews on this repo. Configure here.