-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathMisc.php
More file actions
124 lines (102 loc) · 3.01 KB
/
Misc.php
File metadata and controls
124 lines (102 loc) · 3.01 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
<?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 = pathinfo( $file_path, PATHINFO_EXTENSION );
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' => (string) wp_get_attachment_caption( $file_id ),
'icon' => get_file_icon( $file_ext ),
];
}
/**
* 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 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] ?? '' );
/* translators: file size units (byte, kilobyte, megabyte, etc.) */
$unit_label = match ( $unit ) {
'b', 'o' => _n( 'byte', 'bytes', $int_value, 'beapi-frontend-framework' ),
'kb', 'ko' => _n( 'kilobyte', 'kilobytes', $int_value, 'beapi-frontend-framework' ),
'mb', 'mo' => _n( 'megabyte', 'megabytes', $int_value, 'beapi-frontend-framework' ),
'gb', 'go' => _n( 'gigabyte', 'gigabytes', $int_value, 'beapi-frontend-framework' ),
'tb', 'to' => _n( 'terabyte', 'terabytes', $int_value, 'beapi-frontend-framework' ),
default => null,
};
if ( null === $unit_label ) {
return $file_size;
}
return $value . ' ' . $unit_label;
}
/**
* @param string $file_ext
*
* @return string
*/
function get_file_icon( string $file_ext ): string {
$file_icon = 'file';
if ( in_array( $file_ext, [ 'jpg', 'jpeg', 'png', 'gif', 'webp', 'svg', 'bmp', 'ico' ], true ) ) {
$file_icon = 'file-image';
}
return $file_icon;
}