PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.3.0
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.3.0
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 / classes / Media / CustomFolders.php

CustomFolders.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.3.0, at classes/Media/CustomFolders.php

361 lines 8.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Imagify\Media;
3
4 use Imagify\Traits\MediaRowTrait;
5 use Imagify\Deprecated\Traits\Media\CustomFoldersDeprecatedTrait;
6 use WP_Error;
7
8 /**
9 * Media class for the custom folders.
10 *
11 * @since 1.9
12 * @author Grégory Viguier
13 */
14 class CustomFolders extends AbstractMedia {
15 use MediaRowTrait;
16 use CustomFoldersDeprecatedTrait;
17
18 /**
19 * Context (where the media "comes from").
20 *
21 * @var string
22 * @since 1.9
23 * @access protected
24 * @author Grégory Viguier
25 */
26 protected $context = 'custom-folders';
27
28 /**
29 * The attachment SQL DB class.
30 *
31 * @var string
32 * @since 1.9
33 * @access protected
34 * @author Grégory Viguier
35 */
36 protected $db_class_name = 'Imagify_Files_DB';
37
38 /**
39 * Tell if the media/context is network-wide.
40 *
41 * @var bool
42 * @since 1.9
43 * @access protected
44 * @author Grégory Viguier
45 */
46 protected $is_network_wide = true;
47
48 /**
49 * The constructor.
50 *
51 * @since 1.9
52 * @access public
53 * @author Grégory Viguier
54 *
55 * @param int|array|object $id The file ID. It can also be an array or object representing the file data.
56 */
57 public function __construct( $id ) {
58 if ( ! static::constructor_accepts( $id ) ) {
59 $this->invalidate_row();
60 parent::__construct( 0 );
61 return;
62 }
63
64 if ( is_numeric( $id ) ) {
65 $this->id = (int) $id;
66 $this->get_row();
67 } else {
68 $prim_key = $this->get_row_db_instance()->get_primary_key();
69 $this->row = (array) $id;
70 $this->id = $this->row[ $prim_key ];
71 }
72
73 parent::__construct( $this->id );
74 }
75
76 /**
77 * Tell if the given entry can be accepted in the constructor.
78 *
79 * @since 1.9
80 * @access public
81 * @author Grégory Viguier
82 *
83 * @param mixed $id Whatever.
84 * @return bool
85 */
86 public static function constructor_accepts( $id ) {
87 return $id && ( is_numeric( $id ) || is_array( $id ) || is_object( $id ) );
88 }
89
90
91 /** ----------------------------------------------------------------------------------------- */
92 /** ORIGINAL FILE =========================================================================== */
93 /** ----------------------------------------------------------------------------------------- */
94
95 /**
96 * Get the original media's path.
97 *
98 * @since 1.9
99 * @access public
100 * @author Grégory Viguier
101 *
102 * @return string|bool The file path. False on failure.
103 */
104 public function get_raw_original_path() {
105 if ( ! $this->is_valid() ) {
106 return false;
107 }
108
109 if ( $this->get_cdn() ) {
110 return $this->get_cdn()->get_file_path( 'original' );
111 }
112
113 $row = $this->get_row();
114
115 if ( ! $row || empty( $row['path'] ) ) {
116 return false;
117 }
118
119 return \Imagify_Files_Scan::remove_placeholder( $row['path'] );
120 }
121
122
123 /** ----------------------------------------------------------------------------------------- */
124 /** FULL SIZE FILE ========================================================================== */
125 /** ----------------------------------------------------------------------------------------- */
126
127 /**
128 * Get the URL of the media’s full size file.
129 *
130 * @since 1.9.8
131 * @access public
132 * @author Grégory Viguier
133 *
134 * @return string|bool The file URL. False on failure.
135 */
136 public function get_fullsize_url() {
137 if ( ! $this->is_valid() ) {
138 return false;
139 }
140
141 if ( $this->get_cdn() ) {
142 return $this->get_cdn()->get_file_url();
143 }
144
145 $row = $this->get_row();
146
147 if ( ! $row || empty( $row['path'] ) ) {
148 return false;
149 }
150
151 return \Imagify_Files_Scan::remove_placeholder( $row['path'], 'url' );
152 }
153
154 /**
155 * Get the path to the media’s full size file, even if the file doesn't exist.
156 *
157 * @since 1.9.8
158 * @access public
159 * @author Grégory Viguier
160 *
161 * @return string|bool The file path. False on failure.
162 */
163 public function get_raw_fullsize_path() {
164 if ( ! $this->is_valid() ) {
165 return false;
166 }
167
168 if ( $this->get_cdn() ) {
169 return $this->get_cdn()->get_file_path();
170 }
171
172 $row = $this->get_row();
173
174 if ( ! $row || empty( $row['path'] ) ) {
175 return false;
176 }
177
178 return \Imagify_Files_Scan::remove_placeholder( $row['path'] );
179 }
180
181
182 /** ----------------------------------------------------------------------------------------- */
183 /** BACKUP FILE ============================================================================= */
184 /** ----------------------------------------------------------------------------------------- */
185
186 /**
187 * Get the backup URL, even if the file doesn't exist.
188 *
189 * @since 1.9
190 * @access public
191 * @author Grégory Viguier
192 *
193 * @return string|bool The file URL. False on failure.
194 */
195 public function get_backup_url() {
196 if ( ! $this->is_valid() ) {
197 return false;
198 }
199
200 return site_url( $this->filesystem->make_path_relative( $this->get_raw_backup_path() ) );
201 }
202
203 /**
204 * Get the backup file path, even if the file doesn't exist.
205 *
206 * @since 1.9
207 * @access public
208 * @author Grégory Viguier
209 *
210 * @return string|bool The file path. False on failure.
211 */
212 public function get_raw_backup_path() {
213 if ( ! $this->is_valid() ) {
214 return false;
215 }
216
217 return \Imagify_Custom_Folders::get_file_backup_path( $this->get_raw_original_path() );
218 }
219
220
221 /** ----------------------------------------------------------------------------------------- */
222 /** THUMBNAILS ============================================================================== */
223 /** ----------------------------------------------------------------------------------------- */
224
225 /**
226 * Create the media thumbnails.
227 * And since this context does not support thumbnails...
228 *
229 * @since 1.9
230 * @access public
231 * @author Grégory Viguier
232 *
233 * @return bool|WP_Error True on success. A WP_Error instance on failure.
234 */
235 public function generate_thumbnails() {
236 if ( ! $this->is_valid() ) {
237 return new WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) );
238 }
239
240 return true;
241 }
242
243
244 /** ----------------------------------------------------------------------------------------- */
245 /** MEDIA DATA ============================================================================== */
246 /** ----------------------------------------------------------------------------------------- */
247
248 /**
249 * Tell if the current media has the required data (the data containing the file paths and thumbnails).
250 *
251 * @since 1.9
252 * @access public
253 * @author Grégory Viguier
254 *
255 * @return bool
256 */
257 public function has_required_media_data() {
258 return $this->is_valid();
259 }
260
261 /**
262 * Get the list of the files of this media, including the full size file.
263 *
264 * @since 1.9
265 * @access public
266 * @author Grégory Viguier
267 *
268 * @return array {
269 * An array with the size names as keys ('full' is used for the full size file), and arrays of data as values:
270 *
271 * @type string $size The size name.
272 * @type string $path Absolute path to the file.
273 * @type int $width The file width.
274 * @type int $height The file height.
275 * @type string $mime-type The file mime type.
276 * @type bool $disabled True if the size is disabled in the plugin’s settings.
277 * }
278 */
279 public function get_media_files() {
280 if ( ! $this->is_valid() ) {
281 return [];
282 }
283
284 $fullsize_path = $this->get_raw_fullsize_path();
285
286 if ( ! $fullsize_path ) {
287 return [];
288 }
289
290 $dimensions = $this->get_dimensions();
291 $sizes = [
292 'full' => [
293 'size' => 'full',
294 'path' => $fullsize_path,
295 'width' => $dimensions['width'],
296 'height' => $dimensions['height'],
297 'mime-type' => $this->get_mime_type(),
298 'disabled' => false,
299 ],
300 ];
301
302 return $this->filter_media_files( $sizes );
303 }
304
305 /**
306 * If the media is an image, get its width and height.
307 *
308 * @since 1.9
309 * @access public
310 * @author Grégory Viguier
311 *
312 * @return array
313 */
314 public function get_dimensions() {
315 if ( ! $this->is_image() ) {
316 return [
317 'width' => 0,
318 'height' => 0,
319 ];
320 }
321
322 $row = $this->get_row();
323
324 return [
325 'width' => ! empty( $row['width'] ) ? $row['width'] : 0,
326 'height' => ! empty( $row['height'] ) ? $row['height'] : 0,
327 ];
328 }
329
330 /**
331 * Update the media data dimensions.
332 *
333 * @since 1.9
334 * @access public
335 * @author Grégory Viguier
336 *
337 * @param array $dimensions {
338 * An array containing width and height.
339 *
340 * @type int $width The image width.
341 * @type int $height The image height.
342 * }
343 */
344 protected function update_media_data_dimensions( $dimensions ) {
345 $row = $this->get_row();
346
347 if ( ! is_array( $row ) ) {
348 $row = [];
349 }
350
351 if ( isset( $row['width'], $row['height'] ) && $row['width'] === $dimensions['width'] && $row['height'] === $dimensions['height'] ) {
352 return;
353 }
354
355 $row['width'] = $dimensions['width'];
356 $row['height'] = $dimensions['height'];
357
358 $this->update_row( $row );
359 }
360 }
361