Repository navigation
Expand file tree
/
Copy pathMisc.php
More file actions
204 lines (164 loc) · 4.95 KB
/
Copy pathMisc.php
File metadata and controls
204 lines (164 loc) · 4.95 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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( '' ),
];
$file_href = wp_get_attachment_url( $file_id );
if ( empty( $file_href ) ) {
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 {
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;
}