PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / trunk
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF vtrunk
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
← All changes | classes/Optimization/Process/WP.php +57 -15 1.10 → trunk View file →
@@ -1,11 +1,10 @@
1 1 <?php
2 2 namespace Imagify\Optimization\Process;
3 3
4 4 use Imagify\Optimization\File;
5 +use WP_Error;
5 6
6 -defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
7 -
8 7 /**
9 8 * Optimization class for the attachments in the WP library.
10 9 * This class constructor accepts:
11 10 * - A post ID (int).
@@ -18,8 +17,36 @@
18 17 */
19 18 class WP extends AbstractProcess {
20 19
21 20 /** ----------------------------------------------------------------------------------------- */
21 + /** RESIZING ================================================================================ */
22 + /** ----------------------------------------------------------------------------------------- */
23 +
24 + /**
25 + * Tell if a size should be resized.
26 + *
27 + * When the browser handled the upload it also produced the scaled version, using the
28 + * very threshold Imagify configured ({@see \Imagify\Context\WP::filter_big_image_size_threshold()}),
29 + * so resizing here would only shrink the untouched original that WordPress keeps
30 + * aside as `original_image`.
31 + *
32 + * @since 2.3.3
33 + *
34 + * @param string $size The size name.
35 + * @param File $file A File instance.
36 + * @return bool
37 + */
38 + protected function can_resize( $size, $file ) {
39 + $media = $this->get_media();
40 +
41 + if ( $media && \Imagify\Context\WP::is_client_side_scaled( $media->get_id() ) ) {
42 + return false;
43 + }
44 +
45 + return parent::can_resize( $size, $file );
46 + }
47 +
48 + /** ----------------------------------------------------------------------------------------- */
22 49 /** MISSING THUMBNAILS ====================================================================== */
23 50 /** ----------------------------------------------------------------------------------------- */
24 51
25 52 /**
@@ -45,29 +72,33 @@
45 72 */
46 73 public function get_missing_sizes() {
47 74 // The media must have been optimized once and have a backup.
48 75 if ( ! $this->is_valid() ) {
49 - return new \WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) );
76 + return new WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) );
50 77 }
51 78
52 79 $media = $this->get_media();
53 80
81 + if ( ! $media ) {
82 + return new WP_Error( 'no_media', __( 'No media found.', 'imagify' ) );
83 + }
84 +
54 85 if ( ! $media->is_supported() ) {
55 - return new \WP_Error( 'media_not_supported', __( 'This media is not supported.', 'imagify' ) );
86 + return new WP_Error( 'media_not_supported', __( 'This media is not supported.', 'imagify' ) );
56 87 }
57 88
58 89 $data = $this->get_data();
59 90
60 91 if ( ! $data->is_optimized() ) {
61 - return new \WP_Error( 'media_not_optimized', __( 'This media is not optimized yet.', 'imagify' ) );
92 + return new WP_Error( 'media_not_optimized', __( 'This media is not optimized yet.', 'imagify' ) );
62 93 }
63 94
64 95 if ( ! $media->has_backup() ) {
65 - return new \WP_Error( 'no_backup', __( 'This file has no backup file.', 'imagify' ) );
96 + return new WP_Error( 'no_backup', __( 'This file has no backup file.', 'imagify' ) );
66 97 }
67 98
68 99 if ( ! $media->is_image() ) {
69 - return new \WP_Error( 'media_not_an_image', __( 'This media is not an image.', 'imagify' ) );
100 + return new WP_Error( 'media_not_an_image', __( 'This media is not an image.', 'imagify' ) );
70 101 }
71 102
72 103 // Compare registered sizes and optimized sizes.
73 104 $context_sizes = $media->get_context_instance()->get_thumbnail_sizes();
@@ -129,19 +160,25 @@
129 160 * @return bool|WP_Error True if successfully launched. A \WP_Error instance on failure.
130 161 */
131 162 public function optimize_missing_thumbnails() {
132 163 if ( ! $this->is_valid() ) {
133 - return new \WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) );
164 + return new WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) );
134 165 }
135 166
136 - if ( ! $this->get_media()->is_supported() ) {
137 - return new \WP_Error( 'media_not_supported', __( 'This media is not supported.', 'imagify' ) );
167 + $media = $this->get_media();
168 +
169 + if ( ! $media ) {
170 + return new WP_Error( 'no_media', __( 'No media found.', 'imagify' ) );
138 171 }
139 172
173 + if ( ! $media->is_supported() ) {
174 + return new WP_Error( 'media_not_supported', __( 'This media is not supported.', 'imagify' ) );
175 + }
176 +
140 177 $missing_sizes = $this->get_missing_sizes();
141 178
142 179 if ( ! $missing_sizes ) {
143 - return new \WP_Error( 'no_sizes', __( 'No thumbnails seem to be missing.', 'imagify' ) );
180 + return new WP_Error( 'no_sizes', __( 'No thumbnails seem to be missing.', 'imagify' ) );
144 181 }
145 182
146 183 if ( is_wp_error( $missing_sizes ) ) {
147 184 return $missing_sizes;
@@ -147,9 +184,9 @@
147 184 return $missing_sizes;
148 185 }
149 186
150 187 if ( $this->is_locked() ) {
151 - return new \WP_Error( 'media_locked', __( 'This media is already being processed.', 'imagify' ) );
188 + return new WP_Error( 'media_locked', __( 'This media is already being processed.', 'imagify' ) );
152 189 }
153 190
154 191 $this->lock();
155 192
@@ -157,9 +194,9 @@
157 194 $sizes = $this->create_missing_thumbnails( $missing_sizes );
158 195
159 196 if ( ! $sizes ) {
160 197 $this->unlock();
161 - return new \WP_Error( 'thumbnail_creation_failed', __( 'The thumbnails failed to be created.', 'imagify' ) );
198 + return new WP_Error( 'thumbnail_creation_failed', __( 'The thumbnails failed to be created.', 'imagify' ) );
162 199 }
163 200
164 201 $optimization_level = $this->get_data()->get_optimization_level();
165 202
@@ -208,9 +245,14 @@
208 245 if ( ! $missing_sizes ) {
209 246 return [];
210 247 }
211 248
212 - $media = $this->get_media();
249 + $media = $this->get_media();
250 +
251 + if ( ! $media ) {
252 + return [];
253 + }
254 +
213 255 $media_id = $media->get_id();
214 256 $metadata = wp_get_attachment_metadata( $media_id );
215 257 $metadata['sizes'] = ! empty( $metadata['sizes'] ) && is_array( $metadata['sizes'] ) ? $metadata['sizes'] : [];
216 258
@@ -235,9 +277,9 @@
235 277 } else {
236 278 $result = true;
237 279 }
238 280
239 - if ( ! empty( $metadata['sizes'][ $size_name ] ) && ! is_wp_error( $result ) ) {
281 + if ( ! empty( $metadata['sizes'][ $size_name ] ) && ! is_wp_error( $result ) ) { // phpstan-ignore-line
240 282 // Not an error.
241 283 $without_errors[ $size_name ] = $metadata['sizes'][ $size_name ];
242 284 }
243 285 }