PluginProbe
WP-Stateless – Google Cloud Storage / trunk
WP-Stateless – Google Cloud Storage vtrunk
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
← All changes | lib/classes/compatibility/imagify.php +93 -98 3.0trunk View file →
@@ -1,5 +1,6 @@
1 1 <?php
2 +
2 3 /**
3 4 * Plugin Name: Imagify
4 5 * Plugin URI: https://wordpress.org/plugins/imagify/
5 6 *
@@ -10,37 +11,39 @@
10 11 */
11 12
12 13 namespace wpCloud\StatelessMedia {
13 14
14 - if( !class_exists( 'wpCloud\StatelessMedia\Imagify' ) ) {
15 + if (!class_exists('wpCloud\StatelessMedia\Imagify')) {
15 16
16 - class Imagify extends ICompatibility {
17 + class Imagify extends Compatibility {
17 18 protected $id = 'imagify';
18 19 protected $title = 'Imagify Image Optimizer';
19 20 protected $constant = 'WP_STATELESS_COMPATIBILITY_IMAGIFY';
20 21 protected $description = 'Enables support for these Imagify Image Optimizer features: auto-optimize images on upload, bulk optimizer, resize larger images, optimization levels (normal, aggressive, ultra).';
21 - protected $plugin_file = [ 'imagify/imagify.php', 'imagify-plugin/imagify.php' ];
22 - protected $sm_mode_not_supported = [ 'stateless' ];
22 + protected $plugin_file = ['imagify/imagify.php', 'imagify-plugin/imagify.php'];
23 + protected $sm_mode_not_supported = ['stateless'];
24 + protected $enabled = false;
25 + protected $is_internal = true;
23 26
24 - public function module_init( $sm ) {
27 + public function module_init($sm) {
25 28 // Skip sync on upload when attachment is image, sync will be handled after image is optimized.
26 29 // Disabling for now because it's cause problem.
27 30 //add_filter( 'wp_stateless_skip_add_media', array( $this, 'skip_add_media' ), 10, 5 );
28 - add_filter( 'before_imagify_optimize_attachment', array( $this, 'fix_missing_file' ), 10 );
29 - add_action( 'after_imagify_optimize_attachment', array( $this, 'after_imagify_optimize_attachment' ), 10 );
31 + add_filter('before_imagify_optimize_attachment', array($this, 'fix_missing_file'), 10);
32 + add_action('after_imagify_optimize_attachment', array($this, 'after_imagify_optimize_attachment'), 10);
30 33
31 34 //hook for Imagify since version 1.9
32 - add_filter( 'wp_stateless_skip_remove_media', array( $this, 'skip_remove_media' ), 10, 5 );
33 - add_action( 'imagify_after_optimize_file', array( $this, 'imagify_after_optimize_file' ), 10, 2 );
34 - add_action( 'imagify_before_optimize_size', array( $this, 'imagify_before_optimize_size' ), 10, 7 );
35 + add_filter('wp_stateless_skip_remove_media', array($this, 'skip_remove_media'), 10, 5);
36 + add_action('imagify_after_optimize_file', array($this, 'imagify_after_optimize_file'), 10, 2);
37 + add_action('imagify_before_optimize_size', array($this, 'imagify_before_optimize_size'), 10, 7);
35 38
36 39 // if imagify implement this filter then enable it.
37 - add_filter( 'imagify_has_backup', array( $this, 'imagify_has_backup' ), 10, 2 );
40 + add_filter('imagify_has_backup', array($this, 'imagify_has_backup'), 10, 2);
38 41
39 - add_filter( 'before_imagify_restore_attachment', array( $this, 'get_image_from_gcs' ), 10 );
40 - add_action( 'after_imagify_restore_attachment', array( $this, 'after_imagify_optimize_attachment' ), 10 );
42 + add_filter('before_imagify_restore_attachment', array($this, 'get_image_from_gcs'), 10);
43 + add_action('after_imagify_restore_attachment', array($this, 'after_imagify_optimize_attachment'), 10);
41 44 // Sync from sync tab
42 - add_action( 'sm:synced::image', array( $this, 'get_image_from_gcs' ) );
45 + add_action('sm:synced::image', array($this, 'get_image_from_gcs'));
43 46 }
44 47
45 48 /**
46 49 * Whether to skip the sync on image upload before the image is optimized.
@@ -57,25 +60,24 @@
57 60 *
58 61 * @return bool $return True to skip the sync and false to do the sync.
59 62 *
60 63 */
61 - public function skip_add_media( $return, $metadata, $attachment_id, $force = false, $args = array() ) {
64 + public function skip_add_media($return, $metadata, $attachment_id, $force = false, $args = array()) {
62 65 global $doing_manual_sync;
63 - $args = wp_parse_args( $args, array( 'no_thumb' => false, ) );
64 66
65 - if( $force || $doing_manual_sync || !get_imagify_option( 'auto_optimize' ) || $args[ 'no_thumb' ] == true ) return false;
67 + if ($force || $doing_manual_sync || !get_imagify_option('auto_optimize')) return false;
66 68
67 - $imagify = new \Imagify_Attachment( $attachment_id );
68 - if( is_callable( array( $imagify, 'is_extension_supported' ) ) ) {
69 - if( !$imagify->is_extension_supported() ) {
69 + $imagify = new \Imagify_Attachment($attachment_id);
70 + if (is_callable(array($imagify, 'is_extension_supported'))) {
71 + if (!$imagify->is_extension_supported()) {
70 72 return false;
71 73 }
72 - } elseif( function_exists( 'imagify_is_attachment_mime_type_supported' ) ) {
74 + } elseif (function_exists('imagify_is_attachment_mime_type_supported')) {
73 75 // Use `imagify_is_attachment_mime_type_supported( $attachment_id )`.
74 - if( !imagify_is_attachment_mime_type_supported( $attachment_id ) ) {
76 + if (!imagify_is_attachment_mime_type_supported($attachment_id)) {
75 77 return false;
76 78 }
77 - } elseif( !wp_attachment_is_image( $attachment_id ) ) {
79 + } elseif (!wp_attachment_is_image($attachment_id)) {
78 80 return false;
79 81 }
80 82
81 83 return true;
@@ -91,27 +93,25 @@
91 93 * @param array $args
92 94 * @return bool
93 95 * @author palant@ud
94 96 */
95 - public function skip_remove_media( $return, $metadata, $attachment_id, $force = false, $args = array() ) {
97 + public function skip_remove_media($return, $metadata, $attachment_id, $force = false, $args = array()) {
96 98 global $doing_manual_sync;
97 99
98 - $args = wp_parse_args( $args, array( 'no_thumb' => false ) );
100 + if ($force || $doing_manual_sync || !get_imagify_option('auto_optimize')) return false;
99 101
100 - if( $force || $doing_manual_sync || !get_imagify_option( 'auto_optimize' ) || $args[ 'no_thumb' ] == true ) return false;
102 + $imagify = new \Imagify\Optimization\File(get_attached_file($attachment_id));
101 103
102 - $imagify = new \Imagify\Optimization\File( get_attached_file( $attachment_id ) );
103 -
104 - if( is_callable( array( $imagify, 'is_supported' ) ) ) {
105 - if( !$imagify->is_supported( imagify_get_mime_types() ) ) {
104 + if (is_callable(array($imagify, 'is_supported'))) {
105 + if (!$imagify->is_supported(imagify_get_mime_types())) {
106 106 return false;
107 107 }
108 - } elseif( function_exists( 'imagify_is_attachment_mime_type_supported' ) ) {
108 + } elseif (function_exists('imagify_is_attachment_mime_type_supported')) {
109 109 // Use `imagify_is_attachment_mime_type_supported( $attachment_id )`.
110 - if( !imagify_is_attachment_mime_type_supported( $attachment_id ) ) {
110 + if (!imagify_is_attachment_mime_type_supported($attachment_id)) {
111 111 return false;
112 112 }
113 - } elseif( !wp_attachment_is_image( $attachment_id ) ) {
113 + } elseif (!wp_attachment_is_image($attachment_id)) {
114 114 return false;
115 115 }
116 116
117 117 return true;
@@ -122,40 +122,39 @@
122 122 *
123 123 * @param $attachment_id
124 124 * @return mixed
125 125 */
126 - public function fix_missing_file( $attachment_id ) {
126 + public function fix_missing_file($attachment_id) {
127 127 /**
128 128 * If mode is ephemeral then we change it to cdn in order images not being deleted before optimization
129 129 * Remember that we changed mode via global var
130 130 */
131 - if( ud_get_stateless_media()->get( 'sm.mode' ) == 'ephemeral' ) {
132 - ud_get_stateless_media()->set( 'sm.mode', 'cdn' );
131 + if (ud_get_stateless_media()->get('sm.mode') == 'ephemeral') {
132 + ud_get_stateless_media()->set('sm.mode', 'cdn');
133 133 global $wp_stateless_imagify_mode;
134 134 $wp_stateless_imagify_mode = 'ephemeral';
135 135 }
136 136
137 137 $upload_basedir = wp_upload_dir();
138 - $upload_basedir = trailingslashit( $upload_basedir[ 'basedir' ] );
139 - $meta_data = wp_get_attachment_metadata( $attachment_id );
140 - $file = $upload_basedir . $meta_data[ 'file' ];
138 + $upload_basedir = trailingslashit($upload_basedir['basedir']);
139 + $meta_data = wp_get_attachment_metadata($attachment_id);
140 + $file = $upload_basedir . $meta_data['file'];
141 141
142 142 /**
143 143 * Try to get all missing files from GCS
144 144 */
145 - if( !file_exists( $file ) ) {
146 - ud_get_stateless_media()->get_client()->get_media( apply_filters( 'wp_stateless_file_name', $meta_data[ 'file' ] ), true, $file );
145 + if (!file_exists($file)) {
146 + ud_get_stateless_media()->get_client()->get_media(apply_filters('wp_stateless_file_name', $meta_data['file']), true, $file);
147 147 }
148 148
149 - if( !empty( $meta_data[ 'sizes' ] ) && is_array( $meta_data[ 'sizes' ] ) ) {
150 - $upload_basedir = trailingslashit( dirname( $file ) );
151 - foreach( $meta_data[ 'sizes' ] as $image ) {
152 - if( !empty( $image[ 'gs_name' ] ) && !file_exists( $file = $upload_basedir . $image[ 'file' ] ) ) {
153 - ud_get_stateless_media()->get_client()->get_media( apply_filters( 'wp_stateless_file_name', $image[ 'gs_name' ] ), true, $file );
149 + if (!empty($meta_data['sizes']) && is_array($meta_data['sizes'])) {
150 + $upload_basedir = trailingslashit(dirname($file));
151 + foreach ($meta_data['sizes'] as $image) {
152 + if (!empty($image['gs_name']) && !file_exists($file = $upload_basedir . $image['file'])) {
153 + ud_get_stateless_media()->get_client()->get_media(apply_filters('wp_stateless_file_name', $image['gs_name']), true, $file);
154 154 }
155 155 }
156 156 }
157 -
158 157 }
159 158
160 159 /**
161 160 * If image size not exist then upload it to GS.
@@ -165,40 +164,40 @@
165 164 * 'p_img_large' => $p_img_large,
166 165 * )
167 166 * @param $id
168 167 */
169 - public function after_imagify_optimize_attachment( $id ) {
168 + public function after_imagify_optimize_attachment($id) {
170 169 /**
171 170 * Restore ephemeral mode if needed
172 171 */
173 172 global $wp_stateless_imagify_mode;
174 - if( $wp_stateless_imagify_mode == 'ephemeral' ) {
175 - ud_get_stateless_media()->set( 'sm.mode', 'ephemeral' );
173 + if ($wp_stateless_imagify_mode == 'ephemeral') {
174 + ud_get_stateless_media()->set('sm.mode', 'ephemeral');
176 175 }
177 176
178 - $metadata = wp_get_attachment_metadata( $id );
179 - ud_get_stateless_media()->add_media( $metadata, $id, true );
177 + $metadata = wp_get_attachment_metadata($id);
178 + ud_get_stateless_media()->add_media($metadata, $id, true);
180 179
181 180 // Sync backup file with GCS
182 - if( current_filter() == 'after_imagify_optimize_attachment' ) {
181 + if (current_filter() == 'after_imagify_optimize_attachment') {
183 182 /**
184 183 * If mode is ephemeral then we change it to cdn in order images not being deleted before optimization
185 184 * Remember that we changed mode via global var
186 185 * @todo remove if Imagify implement "imagify_has_backup" filter.
187 186 */
188 - if( ud_get_stateless_media()->get( 'sm.mode' ) == 'ephemeral' ) {
189 - ud_get_stateless_media()->set( 'sm.mode', 'cdn' );
187 + if (ud_get_stateless_media()->get('sm.mode') == 'ephemeral') {
188 + ud_get_stateless_media()->set('sm.mode', 'cdn');
190 189 global $wp_stateless_imagify_mode;
191 190 $wp_stateless_imagify_mode = 'ephemeral';
192 191 }
193 192
194 - $file_path = get_attached_file( $id );
195 - $backup_path = get_imagify_attachment_backup_path( $file_path );
196 - if( file_exists( $backup_path ) ) {
197 - $overwrite = apply_filters( 'imagify_backup_overwrite_backup', false, $file_path, $backup_path );
193 + $file_path = get_attached_file($id);
194 + $backup_path = get_imagify_attachment_backup_path($file_path);
195 + if (file_exists($backup_path)) {
196 + $overwrite = apply_filters('imagify_backup_overwrite_backup', false, $file_path, $backup_path);
198 197 // wp_stateless_file_name filter will remove the basedir from the path and prepend with root dir.
199 - $name = apply_filters( 'wp_stateless_file_name', $backup_path );
200 - do_action( 'sm:sync::syncFile', $name, $backup_path, $overwrite );
198 + $name = apply_filters('wp_stateless_file_name', $backup_path);
199 + do_action('sm:sync::syncFile', $name, $backup_path, $overwrite);
201 200 }
202 201 }
203 202 }
204 203
@@ -205,16 +204,16 @@
205 204 /**
206 205 * Restore backup file from GCS if not exist.
207 206 * @param $id
208 207 */
209 - public function get_image_from_gcs( $id ) {
210 - $file_path = get_attached_file( $id );
211 - $backup_path = get_imagify_attachment_backup_path( $file_path );
212 - if( !file_exists( $backup_path ) ) {
208 + public function get_image_from_gcs($id) {
209 + $file_path = get_attached_file($id);
210 + $backup_path = get_imagify_attachment_backup_path($file_path);
211 + if (!file_exists($backup_path)) {
213 212 $upload_dir = wp_upload_dir();
214 - $name = str_replace( trailingslashit( $upload_dir[ 'basedir' ] ), '', $backup_path );
215 - $name = apply_filters( 'wp_stateless_file_name', $name );
216 - do_action( 'sm:sync::syncFile', $name, $backup_path, true );
213 + $name = str_replace(trailingslashit($upload_dir['basedir']), '', $backup_path);
214 + $name = apply_filters('wp_stateless_file_name', $name);
215 + do_action('sm:sync::syncFile', $name, $backup_path, true);
217 216 }
218 217 }
219 218
220 219 /**
@@ -222,12 +221,12 @@
222 221 * @param $return
223 222 * @param $has_backup
224 223 * @return bool
225 224 */
226 - public function imagify_has_backup( $return, $has_backup ) {
227 - if( !$return && $has_backup ) {
228 - $name = apply_filters( 'wp_stateless_file_name', $has_backup );
229 - $return = (bool) apply_filters( 'sm:sync::queue_is_exists', $name );
225 + public function imagify_has_backup($return, $has_backup) {
226 + if (!$return && $has_backup) {
227 + $name = apply_filters('wp_stateless_file_name', $has_backup);
228 + $return = (bool) apply_filters('sm:sync::queue_is_exists', $name);
230 229 }
231 230 return $return;
232 231 }
233 232
@@ -235,24 +234,24 @@
235 234 * Synchronization after optmize process
236 235 * @param $file
237 236 * @param array $args
238 237 */
239 - public function imagify_after_optimize_file( $file, $args = array() ) {
238 + public function imagify_after_optimize_file($file, $args = array()) {
240 239
241 240 global $wp_stateless_imagify_mode;
242 - if( $wp_stateless_imagify_mode == 'ephemeral' ) {
243 - ud_get_stateless_media()->set( 'sm.mode', 'ephemeral' );
241 + if ($wp_stateless_imagify_mode == 'ephemeral') {
242 + ud_get_stateless_media()->set('sm.mode', 'ephemeral');
244 243 }
245 244
246 - $name = apply_filters( 'wp_stateless_file_name', basename( $file ) );
245 + $name = apply_filters('wp_stateless_file_name', basename($file));
247 246
248 - if( file_exists( $file ) ) {
249 - add_filter( 'upload_mimes', array( $this, 'add_webp_mime' ), 10, 2 );
247 + if (file_exists($file)) {
248 + add_filter('upload_mimes', array($this, 'add_webp_mime'), 10, 2);
250 249 /**
251 - * Media already on GCS, so only replacing data on it. For webp format adding path and status to wp_sm_sync table
250 + * Media already on GCS, so only replacing data on it. For webp format adding path and status to wp_stateless_files table
252 251 */
253 - do_action( 'sm:sync::syncFile', $name, $file, true, array( 'use_root' => true, 'skip_db' => ( substr( $name, -4 ) == "webp" ? false : true ) ) );
254 - remove_filter( 'upload_mimes', array( $this, 'add_webp_mime' ), 10 );
252 + do_action('sm:sync::syncFile', $name, $file, true, array('use_root' => true, 'skip_db' => (substr($name, -4) == "webp" ? false : true)));
253 + remove_filter('upload_mimes', array($this, 'add_webp_mime'), 10);
255 254 }
256 255 }
257 256
258 257 /**
@@ -264,18 +263,18 @@
264 263 * @param $webp
265 264 * @param $is_disabled
266 265 * @return mixed
267 266 */
268 - public function imagify_before_optimize_size( $return, $process, $file, $thumb_size, $optimization_level, $webp, $is_disabled ) {
267 + public function imagify_before_optimize_size($return, $process, $file, $thumb_size, $optimization_level, $webp, $is_disabled) {
269 268
270 269 try {
271 - $attachment_id = $this->getProperties( $this->getProperties( $this->getProperties( $process )[ 'data' ] )[ 'media' ] )[ 'id' ];
270 + $attachment_id = $this->getProperties($this->getProperties($this->getProperties($process)['data'])['media'])['id'];
272 271
273 272 $full_size_path = $file->get_path();
274 - $name = apply_filters( 'wp_stateless_file_name', basename( $full_size_path ), true, $attachment_id );
275 - do_action( 'sm:sync::syncFile', $name, $full_size_path, true, [ 'download' => true ] );
273 + $name = apply_filters('wp_stateless_file_name', basename($full_size_path), true, $attachment_id);
274 + do_action('sm:sync::syncFile', $name, $full_size_path, true, ['download' => true]);
276 275 // error_log("\n\ndo_action( 'sm:sync::syncFile', $name, $full_size_path, true, ['download' => true] );");
277 - } catch( \Throwable $th ) {
276 + } catch (\Throwable $th) {
278 277 //throw $th;
279 278 }
280 279 return $return;
281 280 }
@@ -284,28 +283,24 @@
284 283 * Get properties from protected value
285 284 * @param $process
286 285 * @return array
287 286 */
288 - public function getProperties( $process ) {
287 + public function getProperties($process) {
289 288 $properties = array();
290 289 try {
291 - $rc = new \ReflectionClass( $process );
290 + $rc = new \ReflectionClass($process);
292 291 do {
293 292 $rp = array();
294 293 /* @var $p \ReflectionProperty */
295 - foreach( $rc->getProperties() as $p ) {
296 - $p->setAccessible( true );
297 - $rp[ $p->getName() ] = $p->getValue( $process );
294 + foreach ($rc->getProperties() as $p) {
295 + $p->setAccessible(true);
296 + $rp[$p->getName()] = $p->getValue($process);
298 297 }
299 - $properties = array_merge( $rp, $properties );
300 - } while( $rc = $rc->getParentClass() );
301 - } catch( \ReflectionException $e ) {
302 -
298 + $properties = array_merge($rp, $properties);
299 + } while ($rc = $rc->getParentClass());
300 + } catch (\ReflectionException $e) {
303 301 }
304 302 return $properties;
305 303 }
306 -
307 304 }
308 -
309 305 }
310 -
311 306 }