From 593207c0f6395eb01b98098a66a584f857add3fc Mon Sep 17 00:00:00 2001 From: mricoul Date: Wed, 25 Feb 2026 15:56:55 +0100 Subject: [PATCH 1/3] feat(Misc): adds Misc helper for file information and formatting 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. --- functions.php | 2 + inc/Helpers/Misc.php | 137 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 inc/Helpers/Misc.php diff --git a/functions.php b/functions.php index 71a2876b..f903ee54 100644 --- a/functions.php +++ b/functions.php @@ -10,6 +10,8 @@ function () { \BEA\Theme\Framework\Framework::get_container()->boot_services(); } ); + +require_once __DIR__ . '/inc/Helpers/Misc.php'; require_once __DIR__ . '/inc/Helpers/Svg.php'; require_once __DIR__ . '/inc/Helpers/Formatting/Escape.php'; require_once __DIR__ . '/inc/Helpers/Formatting/Image.php'; diff --git a/inc/Helpers/Misc.php b/inc/Helpers/Misc.php new file mode 100644 index 00000000..3ba5a6f3 --- /dev/null +++ b/inc/Helpers/Misc.php @@ -0,0 +1,137 @@ + '', + '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 [ + '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' => 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] ?? '' ); + + switch ( $unit ) { + case 'b': + case 'o': + $unit_label = _n( 'byte', 'bytes', $int_value, 'beapi-frontend-framework' ); + break; + case 'kb': + case 'ko': + $unit_label = _n( 'kilobyte', 'kilobytes', $int_value, 'beapi-frontend-framework' ); + break; + case 'mb': + case 'mo': + $unit_label = _n( 'megabyte', 'megabytes', $int_value, 'beapi-frontend-framework' ); + break; + case 'gb': + case 'go': + $unit_label = _n( 'gigabyte', 'gigabytes', $int_value, 'beapi-frontend-framework' ); + break; + case 'tb': + case 'to': + $unit_label = _n( 'terabyte', 'terabytes', $int_value, 'beapi-frontend-framework' ); + break; + default: + return $file_size; + } + + return $value . ' ' . $unit_label; +} From 162ed8dde8ee7e150ec2743e5c520e6d39ca53cf Mon Sep 17 00:00:00 2001 From: mricoul Date: Wed, 25 Feb 2026 15:58:18 +0100 Subject: [PATCH 2/3] fix(Misc): phpcs issue --- inc/Helpers/Misc.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/Helpers/Misc.php b/inc/Helpers/Misc.php index 3ba5a6f3..116ab290 100644 --- a/inc/Helpers/Misc.php +++ b/inc/Helpers/Misc.php @@ -58,7 +58,7 @@ function get_file_infos( int $file_id ): array { * @return string $file_detail */ function get_file_detail( string $file_name, string $file_ext, string $file_size ): string { - $details = []; + $details = []; if ( ! empty( $file_name ) ) { $details[] = $file_name; From 8e551c2223ed3208ee8c0e6041562ed9359b571e Mon Sep 17 00:00:00 2001 From: Milan Ricoul Date: Wed, 25 Feb 2026 16:02:11 +0100 Subject: [PATCH 3/3] Apply suggestion from @sourcery-ai[bot] Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com> --- inc/Helpers/Misc.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/Helpers/Misc.php b/inc/Helpers/Misc.php index 116ab290..3d71cc2a 100644 --- a/inc/Helpers/Misc.php +++ b/inc/Helpers/Misc.php @@ -44,7 +44,7 @@ function get_file_infos( int $file_id ): array { '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 ), + 'caption' => (string) wp_get_attachment_caption( $file_id ), ]; }