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 / WP.php

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

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