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
wp-stateless / lib / classes / compatibility / imagify.php

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

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