-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathMisc.php
More file actions
137 lines (116 loc) · 2.99 KB
/
Misc.php
File metadata and controls
137 lines (116 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?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 = 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;
}