Image
1 day ago
Select
1 day ago
Arrays.php
1 day ago
AvailableTranslations.php
1 day ago
Creatable.php
1 day ago
Dashicon.php
1 day ago
Date.php
1 day ago
Html.php
1 day ago
Icon.php
1 day ago
Image.php
1 day ago
LocalFile.php
1 day ago
Mbstring.php
1 day ago
MediaIdResolver.php
1 day ago
Menu.php
1 day ago
Network.php
1 day ago
Post.php
1 day ago
Strings.php
1 day ago
Taxonomy.php
1 day ago
User.php
1 day ago
UserRoles.php
1 day ago
WpDateFormat.php
1 day ago
LocalFile.php
65 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Helper; |
| 6 | |
| 7 | class LocalFile extends Creatable |
| 8 | { |
| 9 | public function url_to_path(string $url): string |
| 10 | { |
| 11 | // Normalize the scheme (http vs https) to WP_CONTENT_URL's so a local URL isn't mistaken for an external one. |
| 12 | $url = set_url_scheme($url, wp_parse_url(WP_CONTENT_URL, PHP_URL_SCHEME) ?: null); |
| 13 | |
| 14 | return (string)str_replace(WP_CONTENT_URL, WP_CONTENT_DIR, $url); |
| 15 | } |
| 16 | |
| 17 | public function path_to_url(string $path): string |
| 18 | { |
| 19 | return (string)str_replace(WP_CONTENT_DIR, WP_CONTENT_URL, $path); |
| 20 | } |
| 21 | |
| 22 | public function get_path(string $url): ?string |
| 23 | { |
| 24 | $path = $this->url_to_path($url); |
| 25 | |
| 26 | if (! file_exists($path)) { |
| 27 | return null; |
| 28 | } |
| 29 | |
| 30 | return $path; |
| 31 | } |
| 32 | |
| 33 | public function get_size(string $url): ?int |
| 34 | { |
| 35 | $path = $this->get_path($url); |
| 36 | |
| 37 | if ($path === null) { |
| 38 | return null; |
| 39 | } |
| 40 | |
| 41 | $size = filesize($path); |
| 42 | |
| 43 | return $size === false |
| 44 | ? null |
| 45 | : $size; |
| 46 | } |
| 47 | |
| 48 | public function get_file_name(int $attachment_id): ?string |
| 49 | { |
| 50 | $file = get_post_meta($attachment_id, '_wp_attached_file', true); |
| 51 | |
| 52 | if (! $file) { |
| 53 | return null; |
| 54 | } |
| 55 | |
| 56 | return basename((string)$file); |
| 57 | } |
| 58 | |
| 59 | public function get_file_extension(int $attachment_id): string |
| 60 | { |
| 61 | return (string)pathinfo($this->get_file_name($attachment_id) ?? '', PATHINFO_EXTENSION); |
| 62 | } |
| 63 | |
| 64 | } |
| 65 |