-
Notifications
You must be signed in to change notification settings - Fork 18
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
6
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.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
593207c
feat(Misc): adds Misc helper for file information and formatting
firestar300 162ed8d
fix(Misc): phpcs issue
firestar300 8e551c2
Apply suggestion from @sourcery-ai[bot]
firestar300 829a702
feat: adds translator notes for file size units
firestar300 3c22de9
refactor(Misc): file size unit labeling
firestar300 c82223e
fix(Misc): corrects file extension retrieval
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
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,126 @@ | ||
| <?php | ||
|
|
||
| namespace BEA\Theme\Framework\Helpers\Misc; | ||
|
|
||
| /** | ||
| * Get file infos | ||
| * | ||
| * @param int $file_id | ||
| * | ||
| * @return array $file_infos | ||
| */ | ||
| 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 = pathinfo( $file_path, PATHINFO_EXTENSION ); | ||
|
|
||
| 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 [ | ||
| 'file_name' => $file_name, | ||
| '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' => (string) wp_get_attachment_caption( $file_id ), | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Get file details | ||
| * | ||
| * @param string $file_name | ||
| * @param string $file_ext | ||
| * @param string $file_size | ||
| * | ||
| * @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 mime type | ||
| * | ||
| * @param int $file_id | ||
| * | ||
| * @return string | ||
| */ | ||
| function get_mime_type( int $file_id ) { | ||
| $mime_type = (string) get_post_mime_type( $file_id ); | ||
|
|
||
| if ( empty( $mime_type ) ) { | ||
| return ''; | ||
| } | ||
|
|
||
| $mime_type = explode( '/', $mime_type ); | ||
|
|
||
| return end( $mime_type ); | ||
| } | ||
|
|
||
| /** | ||
| * Get accessible file size label | ||
| * | ||
| * @param string $file_size | ||
| * | ||
| * @return string | ||
| */ | ||
| function get_accessible_file_size_label( string $file_size ): string { | ||
| // Extract value and unit from file size (e.g., "7ko" → "7" + "ko"). | ||
| preg_match( '/^([\d.,]+)\s*([a-zA-Z]+)$/', $file_size, $matches ); | ||
| $value = $matches[1] ?? ''; | ||
| $int_value = (int) $value; // Cast to int for _n() pluralization. | ||
| $unit = strtolower( $matches[2] ?? '' ); | ||
|
|
||
| /* translators: file size units (byte, kilobyte, megabyte, etc.) */ | ||
| $unit_label = match ( $unit ) { | ||
| '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, | ||
| }; | ||
|
|
||
| if ( null === $unit_label ) { | ||
| return $file_size; | ||
| } | ||
|
|
||
| return $value . ' ' . $unit_label; | ||
| } | ||
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.