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

191 lines 8.6 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, 4 );
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 $imagify = new \Imagify_Attachment($attachment_id);
54
55 if($force || !get_imagify_option( 'auto_optimize' )) return false;
56
57 if ( is_callable( array( $imagify, 'is_extension_supported' ) ) ) {
58 if ( ! $imagify->is_extension_supported() ) {
59 return false;
60 }
61 } elseif ( function_exists( 'imagify_is_attachment_mime_type_supported' ) ) {
62 // Use `imagify_is_attachment_mime_type_supported( $attachment_id )`.
63 if ( ! imagify_is_attachment_mime_type_supported( $attachment_id ) ) {
64 return false;
65 }
66 } elseif(!wp_attachment_is_image($attachment_id)){
67 return false;
68 }
69
70 return true;
71 }
72
73 /**
74 * Try to restore images before compression
75 *
76 * @param $file
77 * @param $attachment_id
78 * @return mixed
79 */
80 public function fix_missing_file( $attachment_id ) {
81 /**
82 * If hook is triggered by ShortPixel
83 */
84 if ( !$this->hook_from_imagify($attachment_id) ) return;
85
86 /**
87 * If mode is stateless then we change it to cdn in order images not being deleted before optimization
88 * Remember that we changed mode via global var
89 */
90 if ( ud_get_stateless_media()->get( 'sm.mode' ) == 'stateless' ) {
91 ud_get_stateless_media()->set( 'sm.mode', 'cdn' );
92 global $wp_stateless_imagify_mode;
93 $wp_stateless_imagify_mode = 'stateless';
94 }
95
96 $upload_basedir = wp_upload_dir();
97 $upload_basedir = trailingslashit( $upload_basedir[ 'basedir' ] );
98 $meta_data = wp_get_attachment_metadata( $attachment_id );
99 $file = $upload_basedir . $meta_data['file'];
100
101 /**
102 * Try to get all missing files from GCS
103 */
104 if ( !file_exists( $file ) ) {
105 ud_get_stateless_media()->get_client()->get_media( apply_filters( 'wp_stateless_file_name', $meta_data['file']), true, $file );
106 }
107
108 if ( !empty( $meta_data['sizes'] ) && is_array( $meta_data['sizes'] ) ) {
109 foreach( $meta_data['sizes'] as $image ) {
110 if ( !empty( $image['gs_name'] ) && !file_exists( $file = $upload_basedir . $image['gs_name'] ) ) {
111 ud_get_stateless_media()->get_client()->get_media( apply_filters( 'wp_stateless_file_name', $image['gs_name']), true, $file );
112 }
113 }
114 }
115
116 }
117
118 /**
119 * Determine where we hook from
120 * We need to do this only for something specific in shortpixel plugin
121 *
122 * @return bool
123 */
124 private function hook_from_imagify($attachment_id) {
125 $imagify = new \Imagify_Attachment($attachment_id);
126 if(method_exists($imagify, 'is_running')){
127 return $imagify->is_running($attachment_id);
128 }
129 elseif(get_transient( 'imagify-async-in-progress-' . $attachment_id ) !== false){
130 return true;
131 }
132
133 return false;
134 }
135
136
137 /**
138 * If image size not exist then upload it to GS.
139 *
140 * $args = array(
141 * 'thumbnail' => $thumbnail,
142 * 'p_img_large' => $p_img_large,
143 * )
144 */
145 public function after_imagify_optimize_attachment($id){
146 /**
147 * Restore stateless mode if needed
148 */
149 global $wp_stateless_imagify_mode;
150 if ( $wp_stateless_imagify_mode == 'stateless' ) {
151 ud_get_stateless_media()->set( 'sm.mode', 'stateless' );
152 }
153
154 $metadata = wp_get_attachment_metadata( $id );
155 ud_get_stateless_media()->add_media( $metadata, $id, true );
156
157 // Sync backup file with GCS
158 if( current_filter() == 'after_imagify_optimize_attachment' && ud_get_stateless_media()->get( 'sm.mode' ) !== 'stateless' ) {
159 $file_path = get_attached_file( $id );
160 $backup_path = get_imagify_attachment_backup_path( $file_path );
161 if(file_exists($backup_path)){
162 $upload_dir = wp_upload_dir();
163 $overwrite = apply_filters( 'imagify_backup_overwrite_backup', false, $file_path, $backup_path );
164 $name = str_replace(trailingslashit( $upload_dir[ 'basedir' ] ), '', $backup_path);
165 $name = apply_filters( 'wp_stateless_file_name', $name);
166 do_action( 'sm:sync::syncFile', $name, $backup_path, $overwrite);
167 }
168 }
169 }
170
171 /**
172 * Restore backup file from GCS if not exist.
173 */
174 public function get_image_from_gcs($id){
175 $file_path = get_attached_file( $id );
176 $backup_path = get_imagify_attachment_backup_path( $file_path );
177 if(!file_exists($backup_path)){
178 $upload_dir = wp_upload_dir();
179 $name = str_replace(trailingslashit( $upload_dir[ 'basedir' ] ), '', $backup_path);
180 $name = apply_filters( 'wp_stateless_file_name', $name);
181 do_action( 'sm:sync::syncFile', $name, $backup_path, true);
182 }
183 }
184
185
186 }
187
188 }
189
190 }
191