Repository navigation
Adds Misc helper for file information and formatting #474
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
Open
firestar300
wants to merge
13
commits into
master
Choose a base branch
from
feature/misc-helper
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+240
−0
Open
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5bb06bc
feat(Misc): adds Misc helper for file information and formatting
firestar300 4a3f51b
fix(Misc): phpcs issue
firestar300 a2189c3
Apply suggestion from @sourcery-ai[bot]
firestar300 ea5df86
feat: adds translator notes for file size units
firestar300 77e8125
refactor(Misc): file size unit labeling
firestar300 aa781fd
fix(Misc): corrects file extension retrieval
firestar300 fa6a384
remove unused function
cedric07 e0f22dd
add icons files
cedric07 e0f493b
Fix bugbot - Mismatched array keys between error and success paths
cedric07 7ec2a25
fix: resolve file-image icon for uppercase extensions and AVIF
firestar300 b51c2cc
fix(Misc): parse i18n file sizes with Unicode thousands separators
firestar300 ea04c43
perf(Misc): cache file infos and use attachment filesize meta
firestar300 1126aec
fix: File info cache never invalidates
firestar300 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| <?php | ||
|
|
||
| namespace BEA\Theme\Framework\Helpers\Misc; | ||
|
|
||
| /** | ||
| * Get file infos | ||
| * | ||
| * @param int $file_id Attachment ID. | ||
| * | ||
| * @return array{ | ||
| * file_name: string, | ||
| * details: string, | ||
| * details_accessible: string, | ||
| * href: string, | ||
| * caption: string, | ||
| * icon: string | ||
| * } | ||
| */ | ||
| function get_file_infos( int $file_id ): array { | ||
| $cache_group = 'beapi_file_infos'; | ||
| $cached = wp_cache_get( $file_id, $cache_group ); | ||
|
|
||
| if ( false !== $cached && is_array( $cached ) ) { | ||
| return $cached; | ||
| } | ||
|
|
||
| $file_infos = [ | ||
| 'file_name' => '', | ||
| 'details' => '', | ||
| 'details_accessible' => '', | ||
| 'href' => '', | ||
| 'caption' => '', | ||
| 'icon' => get_file_icon( '' ), | ||
| ]; | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| $file_href = wp_get_attachment_url( $file_id ); | ||
|
|
||
| if ( empty( $file_href ) ) { | ||
|
sourcery-ai[bot] marked this conversation as resolved.
Resolved
|
||
| wp_cache_set( $file_id, $file_infos, $cache_group ); | ||
|
|
||
| return $file_infos; | ||
| } | ||
|
|
||
| $file_path = get_attached_file( $file_id ); | ||
|
|
||
| if ( empty( $file_path ) ) { | ||
| wp_cache_set( $file_id, $file_infos, $cache_group ); | ||
|
|
||
| return $file_infos; | ||
| } | ||
|
|
||
| $file_ext = pathinfo( $file_path, PATHINFO_EXTENSION ); | ||
|
|
||
| if ( empty( $file_ext ) ) { | ||
| wp_cache_set( $file_id, $file_infos, $cache_group ); | ||
|
|
||
| return $file_infos; | ||
| } | ||
|
|
||
| $file_bytes = get_attachment_filesize( $file_id, $file_path ); | ||
| $file_size = $file_bytes > 0 ? (string) size_format( $file_bytes ) : ''; | ||
| $accessible_file_size = $file_bytes > 0 ? get_accessible_file_size_label( $file_bytes ) : ''; | ||
| $file_name = (string) ( get_the_title( $file_id ) ?? '' ); | ||
|
|
||
| $file_infos = [ | ||
| 'file_name' => $file_name, | ||
| 'details' => get_file_detail( $file_name, $file_ext, $file_size ), | ||
| 'details_accessible' => get_file_detail( $file_name, $file_ext, $accessible_file_size ), | ||
| 'href' => $file_href, | ||
| 'caption' => (string) wp_get_attachment_caption( $file_id ), | ||
| 'icon' => get_file_icon( $file_ext ), | ||
| ]; | ||
|
|
||
| wp_cache_set( $file_id, $file_infos, $cache_group ); | ||
|
|
||
| return $file_infos; | ||
| } | ||
|
|
||
| /** | ||
| * Resolve attachment filesize in bytes from metadata, with wp_filesize() fallback and meta backfill. | ||
| * | ||
| * @param int $file_id Attachment ID. | ||
| * @param string $file_path Absolute path to the attached file. | ||
| * | ||
| * @return int Filesize in bytes, or 0 when unavailable. | ||
| */ | ||
| function get_attachment_filesize( int $file_id, string $file_path ): int { | ||
| $metadata = wp_get_attachment_metadata( $file_id ); | ||
|
|
||
| if ( is_array( $metadata ) && ! empty( $metadata['filesize'] ) ) { | ||
| return (int) $metadata['filesize']; | ||
| } | ||
|
|
||
| $filesize = wp_filesize( $file_path ); | ||
|
|
||
| if ( $filesize <= 0 ) { | ||
| return 0; | ||
| } | ||
|
|
||
| if ( ! is_array( $metadata ) ) { | ||
| $metadata = []; | ||
| } | ||
|
|
||
| $metadata['filesize'] = $filesize; | ||
| wp_update_attachment_metadata( $file_id, $metadata ); | ||
|
|
||
| return $filesize; | ||
| } | ||
|
|
||
| /** | ||
| * Get file details | ||
| * | ||
| * @param string $file_name File display name. | ||
| * @param string $file_ext File extension. | ||
| * @param string $file_size Formatted file size label. | ||
| * | ||
| * @return string $file_detail | ||
| */ | ||
| function get_file_detail( string $file_name, string $file_ext, string $file_size ): string { | ||
| $details = []; | ||
|
|
||
| if ( ! empty( $file_name ) ) { | ||
| $details[] = $file_name; | ||
| } | ||
|
|
||
| if ( ! empty( $file_ext ) ) { | ||
| $details[] = strtoupper( $file_ext ); | ||
| } | ||
|
|
||
| if ( ! empty( $file_size ) ) { | ||
| $details[] = $file_size; | ||
| } | ||
|
|
||
| return implode( ' – ', $details ); | ||
| } | ||
|
|
||
| /** | ||
| * Get accessible file size label from raw bytes (screen-reader friendly, pluralized units). | ||
| * | ||
| * Mirrors size_format() unit thresholds without parsing its localized output. | ||
| * | ||
| * @param int $bytes File size in bytes. | ||
| * | ||
| * @return string | ||
| */ | ||
| function get_accessible_file_size_label( int $bytes ): string { | ||
|
Member
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. Amha, copier https://developer.wordpress.org/reference/functions/size_format/ et modifier pour notre trad. |
||
| if ( $bytes <= 0 ) { | ||
| return ''; | ||
| } | ||
|
|
||
| $quant = [ | ||
| 'tb' => TB_IN_BYTES, | ||
| 'gb' => GB_IN_BYTES, | ||
| 'mb' => MB_IN_BYTES, | ||
| 'kb' => KB_IN_BYTES, | ||
| 'b' => 1, | ||
| ]; | ||
|
|
||
| $unit = 'b'; | ||
| $mag = 1; | ||
|
|
||
| foreach ( $quant as $unit_key => $unit_mag ) { | ||
| if ( (float) $bytes >= $unit_mag ) { | ||
| $unit = $unit_key; | ||
| $mag = $unit_mag; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| $value = $bytes / $mag; | ||
| $int_value = (int) round( $value ); | ||
| $formatted = number_format_i18n( $value, 0 ); | ||
|
|
||
| /* translators: file size units (byte, kilobyte, megabyte, etc.) */ | ||
| $unit_label = match ( $unit ) { | ||
| 'b' => _n( 'byte', 'bytes', $int_value, 'beapi-frontend-framework' ), | ||
| 'kb' => _n( 'kilobyte', 'kilobytes', $int_value, 'beapi-frontend-framework' ), | ||
| 'mb' => _n( 'megabyte', 'megabytes', $int_value, 'beapi-frontend-framework' ), | ||
| 'gb' => _n( 'gigabyte', 'gigabytes', $int_value, 'beapi-frontend-framework' ), | ||
| 'tb' => _n( 'terabyte', 'terabytes', $int_value, 'beapi-frontend-framework' ), | ||
| default => null, | ||
| }; | ||
|
|
||
| if ( null === $unit_label ) { | ||
| return ''; | ||
| } | ||
|
|
||
| return $formatted . ' ' . $unit_label; | ||
| } | ||
|
|
||
| /** | ||
| * @param string $file_ext File extension. | ||
| * | ||
| * @return string | ||
| */ | ||
| function get_file_icon( string $file_ext ): string { | ||
| $file_icon = 'file'; | ||
|
|
||
| if ( in_array( strtolower( $file_ext ), [ 'jpg', 'jpeg', 'png', 'gif', 'webp', 'avif', 'svg', 'bmp', 'ico' ], true ) ) { | ||
| $file_icon = 'file-image'; | ||
| } | ||
|
|
||
| return $file_icon; | ||
| } | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.