async-operation
1 year ago
client
4 months ago
exceptions
1 year ago
file-system
2 years ago
image
4 months ago
migration
1 year ago
rest
1 year ago
basic-enum.php
2 years ago
file-utils.php
2 years ago
locale.php
2 years ago
logger.php
4 months ago
module-base.php
2 years ago
route.php
2 years ago
utils.php
4 months ago
file-utils.php
106 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ImageOptimization\Classes; |
| 4 | |
| 5 | if ( ! defined( 'ABSPATH' ) ) { |
| 6 | exit; // Exit if accessed directly. |
| 7 | } |
| 8 | |
| 9 | class File_Utils { |
| 10 | public static function get_extension( string $path ): string { |
| 11 | $locale = new Locale(); |
| 12 | $locale->set_utf_locale(); |
| 13 | |
| 14 | $extension = pathinfo( $path, PATHINFO_EXTENSION ); |
| 15 | |
| 16 | $locale->reset_to_original(); |
| 17 | |
| 18 | return $extension; |
| 19 | } |
| 20 | |
| 21 | public static function get_basename( string $path ): string { |
| 22 | $locale = new Locale(); |
| 23 | $locale->set_utf_locale(); |
| 24 | |
| 25 | $basename = pathinfo( $path, PATHINFO_BASENAME ); |
| 26 | |
| 27 | $locale->reset_to_original(); |
| 28 | |
| 29 | return $basename; |
| 30 | } |
| 31 | |
| 32 | public static function replace_extension( string $path, string $new_extension, bool $unique_filename = false ): string { |
| 33 | $locale = new Locale(); |
| 34 | $locale->set_utf_locale(); |
| 35 | |
| 36 | $path = pathinfo( $path ); |
| 37 | $basename = sprintf( '%s.%s', $path['filename'], $new_extension ); |
| 38 | |
| 39 | if ( $unique_filename ) { |
| 40 | $basename = wp_unique_filename( $path['dirname'], $basename ); |
| 41 | } |
| 42 | |
| 43 | $locale->reset_to_original(); |
| 44 | |
| 45 | return sprintf( '%s/%s', $path['dirname'], $basename ); |
| 46 | } |
| 47 | |
| 48 | public static function get_unique_path( string $path ): string { |
| 49 | $locale = new Locale(); |
| 50 | $locale->set_utf_locale(); |
| 51 | |
| 52 | $path = pathinfo( $path ); |
| 53 | $basename = sprintf( '%s.%s', $path['filename'], $path['extension'] ); |
| 54 | |
| 55 | $locale->reset_to_original(); |
| 56 | |
| 57 | return sprintf( '%s/%s', $path['dirname'], wp_unique_filename( $path['dirname'], $basename ) ); |
| 58 | } |
| 59 | |
| 60 | public static function get_relative_upload_path( string $path ): string { |
| 61 | $locale = new Locale(); |
| 62 | $locale->set_utf_locale(); |
| 63 | |
| 64 | $path = _wp_relative_upload_path( $path ); |
| 65 | |
| 66 | $locale->reset_to_original(); |
| 67 | |
| 68 | return $path; |
| 69 | } |
| 70 | |
| 71 | public static function get_url_from_path( string $full_path ): string { |
| 72 | $locale = new Locale(); |
| 73 | $locale->set_utf_locale(); |
| 74 | |
| 75 | $upload_info = wp_upload_dir(); |
| 76 | $url_base = $upload_info['baseurl']; |
| 77 | |
| 78 | $parts = preg_split( |
| 79 | '/\/wp-content\/uploads/', |
| 80 | $full_path |
| 81 | ); |
| 82 | |
| 83 | $locale->reset_to_original(); |
| 84 | |
| 85 | return $url_base . $parts[1]; |
| 86 | } |
| 87 | |
| 88 | public static function format_file_size( int $file_size_in_bytes, $decimals = 2 ): string { |
| 89 | $sizes = [ |
| 90 | __( '%s Bytes', 'image-optimization' ), |
| 91 | __( '%s Kb', 'image-optimization' ), |
| 92 | __( '%s Mb', 'image-optimization' ), |
| 93 | __( '%s Gb', 'image-optimization' ), |
| 94 | ]; |
| 95 | |
| 96 | if ( ! $file_size_in_bytes ) { |
| 97 | return sprintf( $sizes[0], 0 ); |
| 98 | } |
| 99 | |
| 100 | $current_scale = floor( log( $file_size_in_bytes ) / log( 1024 ) ); |
| 101 | $formatted_value = number_format( $file_size_in_bytes / pow( 1024, $current_scale ), $decimals ); |
| 102 | |
| 103 | return sprintf( $sizes[ $current_scale ], $formatted_value ); |
| 104 | } |
| 105 | } |
| 106 |