Adds Misc helper for file information and formatting#474
Adds Misc helper for file information and formatting#474firestar300 wants to merge 3 commits intomasterfrom
Conversation
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.
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
<location path="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
<location path="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.
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
|
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 |
| return $file_infos; | ||
| } | ||
|
|
||
| $file_ext = get_mime_type( $file_id ); |
There was a problem hiding this comment.
extension !== de mime_type, je ne suis pas sûr que ça fonctionne comme attendu pour tout type de fichier.
| $int_value = (int) $value; // Cast to int for _n() pluralization. | ||
| $unit = strtolower( $matches[2] ?? '' ); | ||
|
|
||
| switch ( $unit ) { |
There was a problem hiding this comment.
Dépendant de la traduction, est-ce que c'est fonctionnel toutes langues là ?
| switch ( $unit ) { | ||
| case 'b': | ||
| case 'o': | ||
| $unit_label = _n( 'byte', 'bytes', $int_value, 'beapi-frontend-framework' ); |
There was a problem hiding this comment.
Il faut ajouter le commentaire translators: lorsque l'export po/mo se fasse on ait les consignes de traduction.
| $int_value = (int) $value; // Cast to int for _n() pluralization. | ||
| $unit = strtolower( $matches[2] ?? '' ); | ||
|
|
||
| switch ( $unit ) { |
There was a problem hiding this comment.
Avecun match on est plus simple et morderne :
'b', 'o' => _n( 'byte', 'bytes', $int_value, 'beapi-frontend-framework' ),
'kb', 'ko' => _n( 'kilobyte', 'kilobytes', $int_value, 'beapi-frontend-framework' ),
'mb', 'mo' => _n( 'megabyte', 'megabytes', $int_value, 'beapi-frontend-framework' ),
'gb', 'go' => _n( 'gigabyte', 'gigabytes', $int_value, 'beapi-frontend-framework' ),
'tb', 'to' => _n( 'terabyte', 'terabytes', $int_value, 'beapi-frontend-framework' ),
default => null,
};
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: