|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BEA\Theme\Framework\Helpers\Misc; |
| 4 | + |
| 5 | +/** |
| 6 | + * Get file infos |
| 7 | + * |
| 8 | + * @param int $file_id |
| 9 | + * |
| 10 | + * @return array $file_infos |
| 11 | + */ |
| 12 | +function get_file_infos( int $file_id ): array { |
| 13 | + $file_href = wp_get_attachment_url( $file_id ); |
| 14 | + $file_infos = [ |
| 15 | + 'href' => '', |
| 16 | + 'file_name' => '', |
| 17 | + 'path' => '', |
| 18 | + 'size' => '', |
| 19 | + 'ext' => '', |
| 20 | + 'caption' => '', |
| 21 | + ]; |
| 22 | + |
| 23 | + if ( empty( $file_href ) ) { |
| 24 | + return $file_infos; |
| 25 | + } |
| 26 | + |
| 27 | + $file_path = get_attached_file( $file_id ); |
| 28 | + |
| 29 | + if ( empty( $file_path ) ) { |
| 30 | + return $file_infos; |
| 31 | + } |
| 32 | + |
| 33 | + $file_ext = get_mime_type( $file_id ); |
| 34 | + |
| 35 | + if ( empty( $file_ext ) ) { |
| 36 | + return $file_infos; |
| 37 | + } |
| 38 | + |
| 39 | + $file_size = (string) size_format( wp_filesize( $file_path ) ); |
| 40 | + $file_name = (string) ( get_the_title( $file_id ) ?? '' ); |
| 41 | + |
| 42 | + return [ |
| 43 | + 'file_name' => $file_name, |
| 44 | + 'details' => get_file_detail( $file_name, $file_ext, $file_size ), |
| 45 | + 'details_accessible' => get_file_detail( $file_name, $file_ext, get_accessible_file_size_label( $file_size ) ), |
| 46 | + 'href' => $file_href, |
| 47 | + 'caption' => wp_get_attachment_caption( $file_id ), |
| 48 | + ]; |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Get file details |
| 53 | + * |
| 54 | + * @param string $file_name |
| 55 | + * @param string $file_ext |
| 56 | + * @param string $file_size |
| 57 | + * |
| 58 | + * @return string $file_detail |
| 59 | + */ |
| 60 | +function get_file_detail( string $file_name, string $file_ext, string $file_size ): string { |
| 61 | + $details = []; |
| 62 | + |
| 63 | + if ( ! empty( $file_name ) ) { |
| 64 | + $details[] = $file_name; |
| 65 | + } |
| 66 | + |
| 67 | + if ( ! empty( $file_ext ) ) { |
| 68 | + $details[] = strtoupper( $file_ext ); |
| 69 | + } |
| 70 | + |
| 71 | + if ( ! empty( $file_size ) ) { |
| 72 | + $details[] = $file_size; |
| 73 | + } |
| 74 | + |
| 75 | + return implode( ' – ', $details ); |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Get mime type |
| 80 | + * |
| 81 | + * @param int $file_id |
| 82 | + * |
| 83 | + * @return string |
| 84 | + */ |
| 85 | +function get_mime_type( int $file_id ) { |
| 86 | + $mime_type = (string) get_post_mime_type( $file_id ); |
| 87 | + |
| 88 | + if ( empty( $mime_type ) ) { |
| 89 | + return ''; |
| 90 | + } |
| 91 | + |
| 92 | + $mime_type = explode( '/', $mime_type ); |
| 93 | + |
| 94 | + return end( $mime_type ); |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Get accessible file size label |
| 99 | + * |
| 100 | + * @param string $file_size |
| 101 | + * |
| 102 | + * @return string |
| 103 | + */ |
| 104 | +function get_accessible_file_size_label( string $file_size ): string { |
| 105 | + // Extract value and unit from file size (e.g., "7ko" → "7" + "ko"). |
| 106 | + preg_match( '/^([\d.,]+)\s*([a-zA-Z]+)$/', $file_size, $matches ); |
| 107 | + $value = $matches[1] ?? ''; |
| 108 | + $int_value = (int) $value; // Cast to int for _n() pluralization. |
| 109 | + $unit = strtolower( $matches[2] ?? '' ); |
| 110 | + |
| 111 | + switch ( $unit ) { |
| 112 | + case 'b': |
| 113 | + case 'o': |
| 114 | + $unit_label = _n( 'byte', 'bytes', $int_value, 'beapi-frontend-framework' ); |
| 115 | + break; |
| 116 | + case 'kb': |
| 117 | + case 'ko': |
| 118 | + $unit_label = _n( 'kilobyte', 'kilobytes', $int_value, 'beapi-frontend-framework' ); |
| 119 | + break; |
| 120 | + case 'mb': |
| 121 | + case 'mo': |
| 122 | + $unit_label = _n( 'megabyte', 'megabytes', $int_value, 'beapi-frontend-framework' ); |
| 123 | + break; |
| 124 | + case 'gb': |
| 125 | + case 'go': |
| 126 | + $unit_label = _n( 'gigabyte', 'gigabytes', $int_value, 'beapi-frontend-framework' ); |
| 127 | + break; |
| 128 | + case 'tb': |
| 129 | + case 'to': |
| 130 | + $unit_label = _n( 'terabyte', 'terabytes', $int_value, 'beapi-frontend-framework' ); |
| 131 | + break; |
| 132 | + default: |
| 133 | + return $file_size; |
| 134 | + } |
| 135 | + |
| 136 | + return $value . ' ' . $unit_label; |
| 137 | +} |
0 commit comments