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

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