PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 1.9.8
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v1.9.8
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 1.9.8, at classes/Media/CustomFolders.php

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