PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 1.7
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v1.7
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / inc / functions / files.php

files.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 1.7, at inc/functions/files.php

234 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined( 'ABSPATH' ) || die( 'Cheatin\' uh?' );
3
4 /**
5 * Get WP Direct filesystem object. Also define chmod constants if not done yet.
6 *
7 * @since 1.6.5
8 * @author Grégory Viguier
9 *
10 * @return object A `$wp_filesystem` object.
11 */
12 function imagify_get_filesystem() {
13 static $filesystem;
14
15 if ( $filesystem ) {
16 return $filesystem;
17 }
18
19 require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' );
20 require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php' );
21
22 $filesystem = new WP_Filesystem_Direct( new StdClass() );
23
24 // Set the permission constants if not already set.
25 if ( ! defined( 'FS_CHMOD_DIR' ) ) {
26 define( 'FS_CHMOD_DIR', ( fileperms( ABSPATH ) & 0777 | 0755 ) );
27 }
28 if ( ! defined( 'FS_CHMOD_FILE' ) ) {
29 define( 'FS_CHMOD_FILE', ( fileperms( ABSPATH . 'index.php' ) & 0777 | 0644 ) );
30 }
31
32 return $filesystem;
33 }
34
35
36 /**
37 * Set the default file permissions using FS_CHMOD_FILE from WP.
38 *
39 * @since 1.2
40 * @since 1.6.5 Use WP Filesystem.
41 *
42 * @param string $file_path The path to file.
43 * @return bool
44 */
45 function imagify_chmod_file( $file_path ) {
46 return imagify_get_filesystem()->chmod( $file_path, FS_CHMOD_FILE );
47 }
48
49 /**
50 * Get a file mime type.
51 *
52 * @since 1.6.9
53 * @since 1.7 Doesn't use exif_imagetype() nor getimagesize() anymore.
54 * @author Grégory Viguier
55 *
56 * @param string $file_path A file path (prefered) or a filename.
57 * @return string|bool A mime type. False on failure: the last test is limited to mime types supported by Imagify.
58 */
59 function imagify_get_mime_type_from_file( $file_path ) {
60 if ( ! $file_path ) {
61 return false;
62 }
63
64 $file_type = wp_check_filetype( $file_path, imagify_get_mime_types() );
65
66 return $file_type['type'];
67 }
68
69 /**
70 * Get a file modification date, formated as "mysql". Fallback to current date.
71 *
72 * @since 1.7
73 * @author Grégory Viguier
74 *
75 * @param string $file_path The file path.
76 * @return string The date.
77 */
78 function imagify_get_file_date( $file_path ) {
79 static $offset;
80
81 if ( ! $file_path ) {
82 return current_time( 'mysql' );
83 }
84
85 $date = imagify_get_filesystem()->mtime( $file_path );
86
87 if ( ! $date ) {
88 return current_time( 'mysql' );
89 }
90
91 if ( ! isset( $offset ) ) {
92 $offset = get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
93 }
94
95 return gmdate( 'Y-m-d H:i:s', $date + $offset );
96 }
97
98
99 /**
100 * Get a clean value of ABSPATH that can be used in string replacements.
101 *
102 * @since 1.6.8
103 * @author Grégory Viguier
104 *
105 * @return string The path to WordPress' root folder.
106 */
107 function imagify_get_abspath() {
108 static $abspath;
109
110 if ( isset( $abspath ) ) {
111 return $abspath;
112 }
113
114 $abspath = wp_normalize_path( ABSPATH );
115
116 // Make sure ABSPATH is not messed up: it could be defined as a relative path for example (yeah, I know, but we've seen it).
117 $test_file = wp_normalize_path( IMAGIFY_FILE );
118 $pos = strpos( $test_file, $abspath );
119
120 if ( 0 < $pos ) {
121 // ABSPATH has a wrong value.
122 $abspath = substr( $test_file, 0, $pos ) . $abspath;
123
124 } elseif ( false === $pos && class_exists( 'ReflectionClass' ) ) {
125 // Imagify is symlinked (dude, you look for trouble).
126 $reflector = new ReflectionClass( 'Requests' );
127 $test_file = $reflector->getFileName();
128 $pos = strpos( $test_file, $abspath );
129
130 if ( 0 < $pos ) {
131 // ABSPATH has a wrong value.
132 $abspath = substr( $test_file, 0, $pos ) . $abspath;
133 }
134 }
135
136 $abspath = '/' . trim( $abspath, '/' ) . '/';
137
138 return $abspath;
139 }
140
141
142 /**
143 * Make an absolute path relative to WordPress' root folder.
144 * Also works for files from registered symlinked plugins.
145 *
146 * @since 1.6.10
147 * @since 1.7 The parameter $base is added.
148 * @author Grégory Viguier
149 *
150 * @param string $file_path An absolute path.
151 * @param string $base A base path to use instead of ABSPATH.
152 * @return string|bool A relative path. Can return the absolute path or false in case of a failure.
153 */
154 function imagify_make_file_path_relative( $file_path, $base = '' ) {
155 static $abspath;
156 global $wp_plugin_paths;
157
158 if ( ! $file_path ) {
159 return false;
160 }
161
162 if ( ! isset( $abspath ) ) {
163 $abspath = wp_normalize_path( ABSPATH );
164 }
165
166 $file_path = wp_normalize_path( $file_path );
167 $base = $base ? trailingslashit( wp_normalize_path( $base ) ) : $abspath;
168 $pos = strpos( $file_path, $base );
169
170 if ( false === $pos && $wp_plugin_paths && is_array( $wp_plugin_paths ) ) {
171 // The file is probably part of a symlinked plugin.
172 arsort( $wp_plugin_paths );
173
174 foreach ( $wp_plugin_paths as $dir => $realdir ) {
175 if ( strpos( $file_path, $realdir ) === 0 ) {
176 $file_path = wp_normalize_path( $dir . substr( $file_path, strlen( $realdir ) ) );
177 }
178 }
179
180 $pos = strpos( $file_path, $base );
181 }
182
183 if ( false === $pos ) {
184 // We're in trouble.
185 return $file_path;
186 }
187
188 return substr_replace( $file_path, '', 0, $pos + strlen( $base ) );
189 }
190
191 /**
192 * Tell if a file is symlinked.
193 *
194 * @since 1.7
195 * @author Grégory Viguier
196 *
197 * @param string $file_path An absolute path.
198 * @return bool
199 */
200 function imagify_file_is_symlinked( $file_path ) {
201 static $abspath;
202 static $plugin_paths = array();
203 global $wp_plugin_paths;
204
205 if ( ! isset( $abspath ) ) {
206 $abspath = strtolower( wp_normalize_path( trailingslashit( imagify_get_abspath() ) ) );
207 }
208
209 $lower_file_path = strtolower( wp_normalize_path( trailingslashit( realpath( $file_path ) ) ) );
210
211 if ( strpos( $lower_file_path, $abspath ) !== 0 ) {
212 return true;
213 }
214
215 if ( $wp_plugin_paths && is_array( $wp_plugin_paths ) ) {
216 if ( ! $plugin_paths ) {
217 foreach ( $wp_plugin_paths as $dir => $realdir ) {
218 $dir = strtolower( wp_normalize_path( trailingslashit( $dir ) ) );
219 $plugin_paths[ $dir ] = strtolower( wp_normalize_path( trailingslashit( $realdir ) ) );
220 }
221 }
222
223 $lower_file_path = strtolower( wp_normalize_path( trailingslashit( $file_path ) ) );
224
225 foreach ( $plugin_paths as $dir => $realdir ) {
226 if ( strpos( $lower_file_path, $dir ) === 0 ) {
227 return true;
228 }
229 }
230 }
231
232 return false;
233 }
234