PluginProbe
WP-Stateless – Google Cloud Storage / 3.0
WP-Stateless – Google Cloud Storage v3.0
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
wp-stateless / lib / classes / compatibility / imagify.php

imagify.php in WP-Stateless – Google Cloud Storage 3.0, at lib/classes/compatibility/imagify.php

312 lines 12.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Imagify
4 * Plugin URI: https://wordpress.org/plugins/imagify/
5 *
6 * Compatibility Description: Enables support for these Imagify Image Optimizer features:
7 * auto-optimize images on upload, bulk optimizer, resize larger images, optimization levels (normal, aggressive, ultra).
8 *
9 * https://github.com/wpCloud/wp-stateless/issues/206
10 */
11
12 namespace wpCloud\StatelessMedia {
13
14 if( !class_exists( 'wpCloud\StatelessMedia\Imagify' ) ) {
15
16 class Imagify extends ICompatibility {
17 protected $id = 'imagify';
18 protected $title = 'Imagify Image Optimizer';
19 protected $constant = 'WP_STATELESS_COMPATIBILITY_IMAGIFY';
20 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' ];
23
24 public function module_init( $sm ) {
25 // Skip sync on upload when attachment is image, sync will be handled after image is optimized.
26 // Disabling for now because it's cause problem.
27 //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 );
30
31 //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
36 // if imagify implement this filter then enable it.
37 add_filter( 'imagify_has_backup', array( $this, 'imagify_has_backup' ), 10, 2 );
38
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 );
41 // Sync from sync tab
42 add_action( 'sm:synced::image', array( $this, 'get_image_from_gcs' ) );
43 }
44
45 /**
46 * Whether to skip the sync on image upload before the image is optimized.
47 * The sync is skipped if the image is compatible with Smush.
48 *
49 * The image will be synced after it's get optimized using the 'wp_smush_image_optimised' action.
50 *
51 *
52 * @param bool $return This should return true if want to skip the sync.
53 * @param int $metadata Metadata for the attachment.
54 * @param string $attachment_id Attachment ID.
55 * @param bool $force Whether to force the sync even the file already exist in GCS.
56 * @param array $args Whether to only sync the full size image.
57 *
58 * @return bool $return True to skip the sync and false to do the sync.
59 *
60 */
61 public function skip_add_media( $return, $metadata, $attachment_id, $force = false, $args = array() ) {
62 global $doing_manual_sync;
63 $args = wp_parse_args( $args, array( 'no_thumb' => false, ) );
64
65 if( $force || $doing_manual_sync || !get_imagify_option( 'auto_optimize' ) || $args[ 'no_thumb' ] == true ) return false;
66
67 $imagify = new \Imagify_Attachment( $attachment_id );
68 if( is_callable( array( $imagify, 'is_extension_supported' ) ) ) {
69 if( !$imagify->is_extension_supported() ) {
70 return false;
71 }
72 } elseif( function_exists( 'imagify_is_attachment_mime_type_supported' ) ) {
73 // Use `imagify_is_attachment_mime_type_supported( $attachment_id )`.
74 if( !imagify_is_attachment_mime_type_supported( $attachment_id ) ) {
75 return false;
76 }
77 } elseif( !wp_attachment_is_image( $attachment_id ) ) {
78 return false;
79 }
80
81 return true;
82 }
83
84 /**
85 * Added fix for Imagify version 1.9
86 * In Ephemeral mode remove files from server after optimization process `imagify_after_optimize_file`
87 * @param $return
88 * @param $metadata
89 * @param $attachment_id
90 * @param bool $force
91 * @param array $args
92 * @return bool
93 * @author palant@ud
94 */
95 public function skip_remove_media( $return, $metadata, $attachment_id, $force = false, $args = array() ) {
96 global $doing_manual_sync;
97
98 $args = wp_parse_args( $args, array( 'no_thumb' => false ) );
99
100 if( $force || $doing_manual_sync || !get_imagify_option( 'auto_optimize' ) || $args[ 'no_thumb' ] == true ) return false;
101
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() ) ) {
106 return false;
107 }
108 } elseif( function_exists( 'imagify_is_attachment_mime_type_supported' ) ) {
109 // Use `imagify_is_attachment_mime_type_supported( $attachment_id )`.
110 if( !imagify_is_attachment_mime_type_supported( $attachment_id ) ) {
111 return false;
112 }
113 } elseif( !wp_attachment_is_image( $attachment_id ) ) {
114 return false;
115 }
116
117 return true;
118 }
119
120 /**
121 * Try to restore images before compression
122 *
123 * @param $attachment_id
124 * @return mixed
125 */
126 public function fix_missing_file( $attachment_id ) {
127 /**
128 * If mode is ephemeral then we change it to cdn in order images not being deleted before optimization
129 * Remember that we changed mode via global var
130 */
131 if( ud_get_stateless_media()->get( 'sm.mode' ) == 'ephemeral' ) {
132 ud_get_stateless_media()->set( 'sm.mode', 'cdn' );
133 global $wp_stateless_imagify_mode;
134 $wp_stateless_imagify_mode = 'ephemeral';
135 }
136
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' ];
141
142 /**
143 * Try to get all missing files from GCS
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 );
147 }
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 );
154 }
155 }
156 }
157
158 }
159
160 /**
161 * If image size not exist then upload it to GS.
162 *
163 * $args = array(
164 * 'thumbnail' => $thumbnail,
165 * 'p_img_large' => $p_img_large,
166 * )
167 * @param $id
168 */
169 public function after_imagify_optimize_attachment( $id ) {
170 /**
171 * Restore ephemeral mode if needed
172 */
173 global $wp_stateless_imagify_mode;
174 if( $wp_stateless_imagify_mode == 'ephemeral' ) {
175 ud_get_stateless_media()->set( 'sm.mode', 'ephemeral' );
176 }
177
178 $metadata = wp_get_attachment_metadata( $id );
179 ud_get_stateless_media()->add_media( $metadata, $id, true );
180
181 // Sync backup file with GCS
182 if( current_filter() == 'after_imagify_optimize_attachment' ) {
183 /**
184 * If mode is ephemeral then we change it to cdn in order images not being deleted before optimization
185 * Remember that we changed mode via global var
186 * @todo remove if Imagify implement "imagify_has_backup" filter.
187 */
188 if( ud_get_stateless_media()->get( 'sm.mode' ) == 'ephemeral' ) {
189 ud_get_stateless_media()->set( 'sm.mode', 'cdn' );
190 global $wp_stateless_imagify_mode;
191 $wp_stateless_imagify_mode = 'ephemeral';
192 }
193
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 );
198 // 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 );
201 }
202 }
203 }
204
205 /**
206 * Restore backup file from GCS if not exist.
207 * @param $id
208 */
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 ) ) {
213 $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 );
217 }
218 }
219
220 /**
221 * Check if backup exists in GCS.
222 * @param $return
223 * @param $has_backup
224 * @return bool
225 */
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 );
230 }
231 return $return;
232 }
233
234 /**
235 * Synchronization after optmize process
236 * @param $file
237 * @param array $args
238 */
239 public function imagify_after_optimize_file( $file, $args = array() ) {
240
241 global $wp_stateless_imagify_mode;
242 if( $wp_stateless_imagify_mode == 'ephemeral' ) {
243 ud_get_stateless_media()->set( 'sm.mode', 'ephemeral' );
244 }
245
246 $name = apply_filters( 'wp_stateless_file_name', basename( $file ) );
247
248 if( file_exists( $file ) ) {
249 add_filter( 'upload_mimes', array( $this, 'add_webp_mime' ), 10, 2 );
250 /**
251 * Media already on GCS, so only replacing data on it. For webp format adding path and status to wp_sm_sync table
252 */
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 );
255 }
256 }
257
258 /**
259 * @param $return
260 * @param $process
261 * @param $file
262 * @param $thumb_size
263 * @param $optimization_level
264 * @param $webp
265 * @param $is_disabled
266 * @return mixed
267 */
268 public function imagify_before_optimize_size( $return, $process, $file, $thumb_size, $optimization_level, $webp, $is_disabled ) {
269
270 try {
271 $attachment_id = $this->getProperties( $this->getProperties( $this->getProperties( $process )[ 'data' ] )[ 'media' ] )[ 'id' ];
272
273 $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 ] );
276 // error_log("\n\ndo_action( 'sm:sync::syncFile', $name, $full_size_path, true, ['download' => true] );");
277 } catch( \Throwable $th ) {
278 //throw $th;
279 }
280 return $return;
281 }
282
283 /**
284 * Get properties from protected value
285 * @param $process
286 * @return array
287 */
288 public function getProperties( $process ) {
289 $properties = array();
290 try {
291 $rc = new \ReflectionClass( $process );
292 do {
293 $rp = array();
294 /* @var $p \ReflectionProperty */
295 foreach( $rc->getProperties() as $p ) {
296 $p->setAccessible( true );
297 $rp[ $p->getName() ] = $p->getValue( $process );
298 }
299 $properties = array_merge( $rp, $properties );
300 } while( $rc = $rc->getParentClass() );
301 } catch( \ReflectionException $e ) {
302
303 }
304 return $properties;
305 }
306
307 }
308
309 }
310
311 }
312