Adds Misc helper for file information and formatting#474
Open
firestar300 wants to merge 6 commits intomasterfrom
Open
Adds Misc helper for file information and formatting#474firestar300 wants to merge 6 commits intomasterfrom
firestar300 wants to merge 6 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>
Contributor
|
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 |
Rahe
reviewed
Feb 25, 2026
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: