PluginProbe
WP-Stateless – Google Cloud Storage / 2.2.6
WP-Stateless – Google Cloud Storage v2.2.6
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 2.2.6, at lib/classes/compatibility/imagify.php

195 lines 8.8 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';
22
23 public function module_init($sm){
24 // Skip sync on upload when attachment is image, sync will be handled after image is optimized.
25 add_filter( 'wp_stateless_skip_add_media', array( $this, 'skip_add_media' ), 10, 5 );
26 add_filter( 'before_imagify_optimize_attachment', array($this, 'fix_missing_file'), 10);
27 add_action( 'after_imagify_optimize_attachment', array($this, 'after_imagify_optimize_attachment'), 10 );
28
29 add_filter( 'before_imagify_restore_attachment', array($this, 'get_image_from_gcs'), 10);
30 add_action( 'after_imagify_restore_attachment', array($this, 'after_imagify_optimize_attachment'), 10 );
31 // Sync from sync tab
32 add_action( 'sm:synced::image', array( $this, 'get_image_from_gcs') );
33
34 }
35
36 /**
37 * Whether to skip the sync on image upload before the image is optimized.
38 * The sync is skipped if the image is compatible with Smush.
39 *
40 * The image will be synced after it's get optimized using the 'wp_smush_image_optimised' action.
41 *
42 *
43 * @param bool $return This should return true if want to skip the sync.
44 * @param int $metadata Metadata for the attachment.
45 * @param string $attachment_id Attachment ID.
46 * @param bool $force Whether to force the sync even the file already exist in GCS.
47 * @param bool $args Whether to only sync the full size image.
48 *
49 * @return bool $return True to skip the sync and false to do the sync.
50 *
51 */
52 public function skip_add_media($return, $metadata, $attachment_id, $force = false, $args = array()) {
53 global $doing_manual_sync;
54 $args = wp_parse_args($args, array(
55 'no_thumb' => false,
56 ));
57
58 if($force || $doing_manual_sync || !get_imagify_option( 'auto_optimize' ) || $args['no_thumb'] == true ) return false;
59
60 $imagify = new \Imagify_Attachment($attachment_id);
61 if ( is_callable( array( $imagify, 'is_extension_supported' ) ) ) {
62 if ( ! $imagify->is_extension_supported() ) {
63 return false;
64 }
65 } elseif ( function_exists( 'imagify_is_attachment_mime_type_supported' ) ) {
66 // Use `imagify_is_attachment_mime_type_supported( $attachment_id )`.
67 if ( ! imagify_is_attachment_mime_type_supported( $attachment_id ) ) {
68 return false;
69 }
70 } elseif(!wp_attachment_is_image($attachment_id)){
71 return false;
72 }
73
74 return true;
75 }
76
77 /**
78 * Try to restore images before compression
79 *
80 * @param $file
81 * @param $attachment_id
82 * @return mixed
83 */
84 public function fix_missing_file( $attachment_id ) {
85 /**
86 * If hook is triggered by ShortPixel
87 */
88 if ( !$this->hook_from_imagify($attachment_id) ) return;
89
90 /**
91 * If mode is stateless then we change it to cdn in order images not being deleted before optimization
92 * Remember that we changed mode via global var
93 */
94 if ( ud_get_stateless_media()->get( 'sm.mode' ) == 'stateless' ) {
95 ud_get_stateless_media()->set( 'sm.mode', 'cdn' );
96 global $wp_stateless_imagify_mode;
97 $wp_stateless_imagify_mode = 'stateless';
98 }
99
100 $upload_basedir = wp_upload_dir();
101 $upload_basedir = trailingslashit( $upload_basedir[ 'basedir' ] );
102 $meta_data = wp_get_attachment_metadata( $attachment_id );
103 $file = $upload_basedir . $meta_data['file'];
104
105 /**
106 * Try to get all missing files from GCS
107 */
108 if ( !file_exists( $file ) ) {
109 ud_get_stateless_media()->get_client()->get_media( apply_filters( 'wp_stateless_file_name', $meta_data['file']), true, $file );
110 }
111
112 if ( !empty( $meta_data['sizes'] ) && is_array( $meta_data['sizes'] ) ) {
113 foreach( $meta_data['sizes'] as $image ) {
114 if ( !empty( $image['gs_name'] ) && !file_exists( $file = $upload_basedir . $image['gs_name'] ) ) {
115 ud_get_stateless_media()->get_client()->get_media( apply_filters( 'wp_stateless_file_name', $image['gs_name']), true, $file );
116 }
117 }
118 }
119
120 }
121
122 /**
123 * Determine where we hook from
124 * We need to do this only for something specific in shortpixel plugin
125 *
126 * @return bool
127 */
128 private function hook_from_imagify($attachment_id) {
129 $imagify = new \Imagify_Attachment($attachment_id);
130 if(method_exists($imagify, 'is_running')){
131 return $imagify->is_running($attachment_id);
132 }
133 elseif(get_transient( 'imagify-async-in-progress-' . $attachment_id ) !== false){
134 return true;
135 }
136
137 return false;
138 }
139
140
141 /**
142 * If image size not exist then upload it to GS.
143 *
144 * $args = array(
145 * 'thumbnail' => $thumbnail,
146 * 'p_img_large' => $p_img_large,
147 * )
148 */
149 public function after_imagify_optimize_attachment($id){
150 /**
151 * Restore stateless mode if needed
152 */
153 global $wp_stateless_imagify_mode;
154 if ( $wp_stateless_imagify_mode == 'stateless' ) {
155 ud_get_stateless_media()->set( 'sm.mode', 'stateless' );
156 }
157
158 $metadata = wp_get_attachment_metadata( $id );
159 ud_get_stateless_media()->add_media( $metadata, $id, true );
160
161 // Sync backup file with GCS
162 if( current_filter() == 'after_imagify_optimize_attachment' && ud_get_stateless_media()->get( 'sm.mode' ) !== 'stateless' ) {
163 $file_path = get_attached_file( $id );
164 $backup_path = get_imagify_attachment_backup_path( $file_path );
165 if(file_exists($backup_path)){
166 $upload_dir = wp_upload_dir();
167 $overwrite = apply_filters( 'imagify_backup_overwrite_backup', false, $file_path, $backup_path );
168 $name = str_replace(trailingslashit( $upload_dir[ 'basedir' ] ), '', $backup_path);
169 $name = apply_filters( 'wp_stateless_file_name', $name);
170 do_action( 'sm:sync::syncFile', $name, $backup_path, $overwrite);
171 }
172 }
173 }
174
175 /**
176 * Restore backup file from GCS if not exist.
177 */
178 public function get_image_from_gcs($id){
179 $file_path = get_attached_file( $id );
180 $backup_path = get_imagify_attachment_backup_path( $file_path );
181 if(!file_exists($backup_path)){
182 $upload_dir = wp_upload_dir();
183 $name = str_replace(trailingslashit( $upload_dir[ 'basedir' ] ), '', $backup_path);
184 $name = apply_filters( 'wp_stateless_file_name', $name);
185 do_action( 'sm:sync::syncFile', $name, $backup_path, true);
186 }
187 }
188
189
190 }
191
192 }
193
194 }
195