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

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