| @@ -1,18 +1,39 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -namespace ImageOptimizer\Classes; | |
| 3 | +namespace ImageOptimization\Classes; | |
| 4 | 4 | |
| 5 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 6 | + exit; // Exit if accessed directly. | |
| 7 | +} | |
| 8 | + | |
| 5 | 9 | class File_Utils { |
| 6 | 10 | public static function get_extension( string $path ): string { |
| 7 | - return pathinfo( $path, PATHINFO_EXTENSION ); | |
| 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; | |
| 8 | 19 | } |
| 9 | 20 | |
| 10 | 21 | public static function get_basename( string $path ): string { |
| 11 | - return pathinfo( $path, PATHINFO_BASENAME ); | |
| 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; | |
| 12 | 30 | } |
| 13 | 31 | |
| 14 | 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 | + | |
| 15 | 36 | $path = pathinfo( $path ); |
| 16 | 37 | $basename = sprintf( '%s.%s', $path['filename'], $new_extension ); |
| 17 | 38 | |
| 18 | 39 | if ( $unique_filename ) { |
| @@ -18,23 +39,40 @@ | ||
| 18 | 39 | if ( $unique_filename ) { |
| 19 | 40 | $basename = wp_unique_filename( $path['dirname'], $basename ); |
| 20 | 41 | } |
| 21 | 42 | |
| 43 | + $locale->reset_to_original(); | |
| 44 | + | |
| 22 | 45 | return sprintf( '%s/%s', $path['dirname'], $basename ); |
| 23 | 46 | } |
| 24 | 47 | |
| 25 | 48 | public static function get_unique_path( string $path ): string { |
| 49 | + $locale = new Locale(); | |
| 50 | + $locale->set_utf_locale(); | |
| 51 | + | |
| 26 | 52 | $path = pathinfo( $path ); |
| 27 | 53 | $basename = sprintf( '%s.%s', $path['filename'], $path['extension'] ); |
| 28 | 54 | |
| 55 | + $locale->reset_to_original(); | |
| 56 | + | |
| 29 | 57 | return sprintf( '%s/%s', $path['dirname'], wp_unique_filename( $path['dirname'], $basename ) ); |
| 30 | 58 | } |
| 31 | 59 | |
| 32 | 60 | public static function get_relative_upload_path( string $path ): string { |
| 33 | - return _wp_relative_upload_path( $path ); | |
| 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; | |
| 34 | 69 | } |
| 35 | 70 | |
| 36 | 71 | public static function get_url_from_path( string $full_path ): string { |
| 72 | + $locale = new Locale(); | |
| 73 | + $locale->set_utf_locale(); | |
| 74 | + | |
| 37 | 75 | $upload_info = wp_upload_dir(); |
| 38 | 76 | $url_base = $upload_info['baseurl']; |
| 39 | 77 | |
| 40 | 78 | $parts = preg_split( |
| @@ -41,17 +79,19 @@ | ||
| 41 | 79 | '/\/wp-content\/uploads/', |
| 42 | 80 | $full_path |
| 43 | 81 | ); |
| 44 | 82 | |
| 83 | + $locale->reset_to_original(); | |
| 84 | + | |
| 45 | 85 | return $url_base . $parts[1]; |
| 46 | 86 | } |
| 47 | 87 | |
| 48 | 88 | public static function format_file_size( int $file_size_in_bytes, $decimals = 2 ): string { |
| 49 | 89 | $sizes = [ |
| 50 | - __( '%s Bytes', 'image-optimizer' ), | |
| 51 | - __( '%s Kb', 'image-optimizer' ), | |
| 52 | - __( '%s Mb', 'image-optimizer' ), | |
| 53 | - __( '%s Gb', 'image-optimizer' ), | |
| 90 | + __( '%s Bytes', 'image-optimization' ), | |
| 91 | + __( '%s Kb', 'image-optimization' ), | |
| 92 | + __( '%s Mb', 'image-optimization' ), | |
| 93 | + __( '%s Gb', 'image-optimization' ), | |
| 54 | 94 | ]; |
| 55 | 95 | |
| 56 | 96 | if ( ! $file_size_in_bytes ) { |
| 57 | 97 | return sprintf( $sizes[0], 0 ); |