Skip to content

Commit ea04c43

Browse files
committed
perf(Misc): cache file infos and use attachment filesize meta
1 parent b51c2cc commit ea04c43

1 file changed

Lines changed: 106 additions & 30 deletions

File tree

‎inc/Helpers/Misc.php‎

Lines changed: 106 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,25 @@
55
/**
66
* Get file infos
77
*
8-
* @param int $file_id
8+
* @param int $file_id Attachment ID.
99
*
10-
* @return array $file_infos
10+
* @return array{
11+
* file_name: string,
12+
* details: string,
13+
* details_accessible: string,
14+
* href: string,
15+
* caption: string,
16+
* icon: string
17+
* }
1118
*/
1219
function get_file_infos( int $file_id ): array {
13-
$file_href = wp_get_attachment_url( $file_id );
20+
$cache_group = 'beapi_file_infos';
21+
$cached = wp_cache_get( $file_id, $cache_group );
22+
23+
if ( false !== $cached && is_array( $cached ) ) {
24+
return $cached;
25+
}
26+
1427
$file_infos = [
1528
'file_name' => '',
1629
'details' => '',
@@ -20,41 +33,86 @@ function get_file_infos( int $file_id ): array {
2033
'icon' => get_file_icon( '' ),
2134
];
2235

36+
$file_href = wp_get_attachment_url( $file_id );
37+
2338
if ( empty( $file_href ) ) {
39+
wp_cache_set( $file_id, $file_infos, $cache_group );
40+
2441
return $file_infos;
2542
}
2643

2744
$file_path = get_attached_file( $file_id );
2845

2946
if ( empty( $file_path ) ) {
47+
wp_cache_set( $file_id, $file_infos, $cache_group );
48+
3049
return $file_infos;
3150
}
3251

3352
$file_ext = pathinfo( $file_path, PATHINFO_EXTENSION );
3453

3554
if ( empty( $file_ext ) ) {
55+
wp_cache_set( $file_id, $file_infos, $cache_group );
56+
3657
return $file_infos;
3758
}
3859

39-
$file_size = (string) size_format( wp_filesize( $file_path ) );
40-
$file_name = (string) ( get_the_title( $file_id ) ?? '' );
60+
$file_bytes = get_attachment_filesize( $file_id, $file_path );
61+
$file_size = $file_bytes > 0 ? (string) size_format( $file_bytes ) : '';
62+
$accessible_file_size = $file_bytes > 0 ? get_accessible_file_size_label( $file_bytes ) : '';
63+
$file_name = (string) ( get_the_title( $file_id ) ?? '' );
4164

42-
return [
65+
$file_infos = [
4366
'file_name' => $file_name,
4467
'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 ) ),
68+
'details_accessible' => get_file_detail( $file_name, $file_ext, $accessible_file_size ),
4669
'href' => $file_href,
4770
'caption' => (string) wp_get_attachment_caption( $file_id ),
4871
'icon' => get_file_icon( $file_ext ),
4972
];
73+
74+
wp_cache_set( $file_id, $file_infos, $cache_group );
75+
76+
return $file_infos;
77+
}
78+
79+
/**
80+
* Resolve attachment filesize in bytes from metadata, with wp_filesize() fallback and meta backfill.
81+
*
82+
* @param int $file_id Attachment ID.
83+
* @param string $file_path Absolute path to the attached file.
84+
*
85+
* @return int Filesize in bytes, or 0 when unavailable.
86+
*/
87+
function get_attachment_filesize( int $file_id, string $file_path ): int {
88+
$metadata = wp_get_attachment_metadata( $file_id );
89+
90+
if ( is_array( $metadata ) && ! empty( $metadata['filesize'] ) ) {
91+
return (int) $metadata['filesize'];
92+
}
93+
94+
$filesize = wp_filesize( $file_path );
95+
96+
if ( $filesize <= 0 ) {
97+
return 0;
98+
}
99+
100+
if ( ! is_array( $metadata ) ) {
101+
$metadata = [];
102+
}
103+
104+
$metadata['filesize'] = $filesize;
105+
wp_update_attachment_metadata( $file_id, $metadata );
106+
107+
return $filesize;
50108
}
51109

52110
/**
53111
* Get file details
54112
*
55-
* @param string $file_name
56-
* @param string $file_ext
57-
* @param string $file_size
113+
* @param string $file_name File display name.
114+
* @param string $file_ext File extension.
115+
* @param string $file_size Formatted file size label.
58116
*
59117
* @return string $file_detail
60118
*/
@@ -77,43 +135,61 @@ function get_file_detail( string $file_name, string $file_ext, string $file_size
77135
}
78136

79137
/**
80-
* Get accessible file size label
138+
* Get accessible file size label from raw bytes (screen-reader friendly, pluralized units).
139+
*
140+
* Mirrors size_format() unit thresholds without parsing its localized output.
81141
*
82-
* @param string $file_size
142+
* @param int $bytes File size in bytes.
83143
*
84144
* @return string
85145
*/
86-
function get_accessible_file_size_label( string $file_size ): string {
87-
// Extract value and unit (e.g. "7ko" or "1\u{00A0}000 KB" from i18n thousands separators).
88-
// UTF-8 mode: allow NBSP/NNBSP inside the value; a non-possessive +? so the last \s* is the gap before the unit, not the thousands separator.
89-
if ( 1 !== preg_match( '/^([\d\.,\p{Zs}]+?)\s*+([a-zA-Z]+)$/u', $file_size, $matches ) ) {
90-
return $file_size;
146+
function get_accessible_file_size_label( int $bytes ): string {
147+
if ( $bytes <= 0 ) {
148+
return '';
149+
}
150+
151+
$quant = [
152+
'tb' => TB_IN_BYTES,
153+
'gb' => GB_IN_BYTES,
154+
'mb' => MB_IN_BYTES,
155+
'kb' => KB_IN_BYTES,
156+
'b' => 1,
157+
];
158+
159+
$unit = 'b';
160+
$mag = 1;
161+
162+
foreach ( $quant as $unit_key => $unit_mag ) {
163+
if ( (float) $bytes >= $unit_mag ) {
164+
$unit = $unit_key;
165+
$mag = $unit_mag;
166+
break;
167+
}
91168
}
92169

93-
$value = $matches[1] ?? '';
94-
$unit = strtolower( $matches[2] ?? '' );
95-
// Strip group separators (ASCII space, NBSP, NNBSP) for _n() plural; (int) leaves decimals as floor (e.g. 1.5 -> 1).
96-
$int_value = (int) str_replace( [ ' ', "\u{00A0}", "\u{202F}" ], '', $value );
170+
$value = $bytes / $mag;
171+
$int_value = (int) round( $value );
172+
$formatted = number_format_i18n( $value, 0 );
97173

98174
/* translators: file size units (byte, kilobyte, megabyte, etc.) */
99175
$unit_label = match ( $unit ) {
100-
'b', 'o' => _n( 'byte', 'bytes', $int_value, 'beapi-frontend-framework' ),
101-
'kb', 'ko' => _n( 'kilobyte', 'kilobytes', $int_value, 'beapi-frontend-framework' ),
102-
'mb', 'mo' => _n( 'megabyte', 'megabytes', $int_value, 'beapi-frontend-framework' ),
103-
'gb', 'go' => _n( 'gigabyte', 'gigabytes', $int_value, 'beapi-frontend-framework' ),
104-
'tb', 'to' => _n( 'terabyte', 'terabytes', $int_value, 'beapi-frontend-framework' ),
105-
default => null,
176+
'b' => _n( 'byte', 'bytes', $int_value, 'beapi-frontend-framework' ),
177+
'kb' => _n( 'kilobyte', 'kilobytes', $int_value, 'beapi-frontend-framework' ),
178+
'mb' => _n( 'megabyte', 'megabytes', $int_value, 'beapi-frontend-framework' ),
179+
'gb' => _n( 'gigabyte', 'gigabytes', $int_value, 'beapi-frontend-framework' ),
180+
'tb' => _n( 'terabyte', 'terabytes', $int_value, 'beapi-frontend-framework' ),
181+
default => null,
106182
};
107183

108184
if ( null === $unit_label ) {
109-
return $file_size;
185+
return '';
110186
}
111187

112-
return $value . ' ' . $unit_label;
188+
return $formatted . ' ' . $unit_label;
113189
}
114190

115191
/**
116-
* @param string $file_ext
192+
* @param string $file_ext File extension.
117193
*
118194
* @return string
119195
*/

0 commit comments

Comments
 (0)