PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.5.4
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.5.4
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
image-optimization / classes / file-system / file-system.php

file-system.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.5.4, at classes/file-system/file-system.php

147 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ImageOptimization\Classes\File_System;
4
5 use ImageOptimization\Classes\File_System\Exceptions\File_System_Operation_Error;
6
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit; // Exit if accessed directly.
9 }
10
11 require_once ABSPATH . 'wp-admin/includes/file.php';
12 WP_Filesystem();
13
14 /**
15 * A wrapper class under WP_Filesystem that throws clear exceptions in case if something went wrong.
16 */
17 class File_System {
18 /**
19 * Writes a string to a file.
20 *
21 * @param string $file Remote path to the file where to write the data.
22 * @param string $contents The data to write.
23 * @param int|false $mode The file permissions as octal number, usually 0644.
24 *
25 * @return true
26 *
27 * @throws File_System_Operation_Error
28 */
29 public static function put_contents( string $file, string $contents, $mode = false ): bool {
30 global $wp_filesystem;
31
32 $result = $wp_filesystem->put_contents( $file, $contents, $mode );
33
34 if ( ! $result ) {
35 throw new File_System_Operation_Error( 'Error while writing ' . $file );
36 }
37
38 return true;
39 }
40
41 /**
42 * Deletes a file or directory.
43 *
44 * @param string $file Path to the file or directory.
45 * @param bool $recursive If set to true, deletes files and folders recursively.
46 * @param string|false $type Type of resource. 'f' for file, 'd' for directory.
47 *
48 * @return true
49 *
50 * @throws File_System_Operation_Error
51 */
52 public static function delete( string $file, bool $recursive = false, $type = false ): bool {
53 global $wp_filesystem;
54
55 $result = $wp_filesystem->delete( $file, $recursive, $type );
56
57 if ( ! $result ) {
58 throw new File_System_Operation_Error( 'Error while deleting ' . $file );
59 }
60
61 return true;
62 }
63
64 /**
65 * @param string $source Path to the source file.
66 * @param string $destination Path to the destination file.
67 * @param bool $overwrite Whether to overwrite the destination file if it exists.
68 *
69 * @return true
70 *
71 * @throws File_System_Operation_Error
72 */
73 public static function move( string $source, string $destination, bool $overwrite = false ): bool {
74 global $wp_filesystem;
75
76 $result = $wp_filesystem->move( $source, $destination, $overwrite );
77
78 if ( ! $result ) {
79 throw new File_System_Operation_Error( "Error while moving {$source} to {$destination}" );
80 }
81
82 return true;
83 }
84
85 /**
86 * Copies a file.
87 *
88 * @param string $source Path to the source file.
89 * @param string $destination Path to the destination file.
90 * @param bool $overwrite Whether to overwrite the destination file if it exists.
91 * @param int|false $mode The permissions as octal number, usually 0644 for files, 0755 for dirs.
92 *
93 * @return bool
94 *
95 * @throws File_System_Operation_Error
96 */
97 public static function copy( string $source, string $destination, bool $overwrite = false, $mode = false ): bool {
98 global $wp_filesystem;
99
100 $result = $wp_filesystem->copy( $source, $destination, $overwrite, $mode );
101
102 if ( ! $result ) {
103 throw new File_System_Operation_Error( "Error while copying {$source} to {$destination}" );
104 }
105
106 return true;
107 }
108
109 /**
110 * Checks if a file or directory exists.
111 *
112 * @param string $path Path to file or directory.
113 *
114 * @return bool Whether $path exists or not.
115 */
116 public static function exists( string $path ): bool {
117 global $wp_filesystem;
118
119 return $wp_filesystem->exists( $path );
120 }
121
122 /**
123 * Gets the file size (in bytes).
124 *
125 * @param ?string $path Path to file.
126 *
127 * @return int Size of the file in bytes on success, false on failure.
128 *
129 * @throws File_System_Operation_Error
130 */
131 public static function size( ?string $path ): int {
132 global $wp_filesystem;
133
134 if ( is_null( $path ) ) {
135 throw new File_System_Operation_Error( 'Null file path provided' );
136 }
137
138 $size = $wp_filesystem->size( $path );
139
140 if ( ! $size ) {
141 throw new File_System_Operation_Error( "Unable to calculate file size for $path" );
142 }
143
144 return $size;
145 }
146 }
147