PluginProbe
WP-Stateless – Google Cloud Storage / 3.0.3
WP-Stateless – Google Cloud Storage v3.0.3
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 / wp-smush.php

wp-smush.php in WP-Stateless – Google Cloud Storage 3.0.3, at lib/classes/compatibility/wp-smush.php

243 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: Smush Image Compression and Optimization
4 * Plugin URI: https://wordpress.org/plugins/wp-smushit/
5 *
6 * Compatibility Description: Ensures compatibility with WPSmush.
7 *
8 * @todo Compatibility for backup feature
9 */
10
11 namespace wpCloud\StatelessMedia {
12
13 if( !class_exists( 'wpCloud\StatelessMedia\WPSmush' ) ) {
14
15 class WPSmush extends ICompatibility {
16 protected $id = 'wp-smush';
17 protected $title = 'WP Smush';
18 protected $constant = 'WP_STATELESS_COMPATIBILITY_WPSMUSH';
19 protected $description = 'Ensures compatibility with WP Smush.';
20 protected $plugin_file = [ 'wp-smushit/wp-smush.php', 'wp-smush-pro/wp-smush.php', 'wp-smushit-pro/wp-smush-pro.php' ];
21
22 /**
23 * @param $sm
24 */
25 public function module_init( $sm ) {
26 add_action( 'wp_smush_image_optimised', array( $this, 'image_optimized' ), 10, 2 );
27 // Check if the file not exists for the given path then download
28 // Useful in Ephemeral mode
29 add_action( 'smush_file_exists', array( $this, 'maybe_download_file' ), 10, 3 );
30
31 // Skip sync when attachment is image, sync will be handled after image is optimized.
32 // add_filter( 'wp_stateless_skip_add_media', array( $this, 'skip_add_media' ), 10, 5 );
33
34 add_filter( 'delete_attachment', array( $this, 'remove_backup' ) );
35 add_filter( 'smush_backup_exists', array( $this, 'backup_exists_on_gcs' ), 10, 3 );
36 add_action( 'sm:synced::image', array( $this, 'sync_backup' ), 10, 2 );
37 }
38
39 /**
40 * Whether to skip the sync on image upload before the image is optimized.
41 * The sync is skipped if the image is compatible with Smush.
42 *
43 * The image will be synced after it's get optimized using the 'wp_smush_image_optimised' action.
44 *
45 *
46 * @param bool $return This should return true if want to skip the sync.
47 * @param int $metadata Metadata for the attachment.
48 * @param string $attachment_id Attachment ID.
49 * @param bool $force Whether to force the sync even the file already exist in GCS.
50 * @param bool $args Whether to only sync the full size image.
51 *
52 * @return bool $return True to skip the sync and false to do the sync.
53 *
54 */
55 public function skip_add_media( $return, $metadata, $attachment_id, $force = false, $args = array() ) {
56 global $doing_manual_sync;
57 $args = wp_parse_args( $args, array( 'no_thumb' => false, ) );
58
59 if( $force || $doing_manual_sync || $args[ 'no_thumb' ] == true ) return false;
60
61 if( class_exists( 'WP_Smush_Modules' ) ) {
62 $auto_smush = \WP_Smush::get_instance()->core()->mod->settings->get( 'auto' );
63 } else {
64 global $wpsmush_settings;
65 $auto_smush = $wpsmush_settings->settings[ 'auto' ];
66 }
67
68 if (!$auto_smush || !wp_attachment_is_image($attachment_id) ||
69 !apply_filters('wp_smush_image', true, $attachment_id) ||
70 !(
71 ((!empty($_POST['action']) && 'upload-attachment' == $_POST['action']) || isset($_POST['post_id'])) &&
72 // And, check if Async is enabled.
73 defined('WP_SMUSH_ASYNC') && WP_SMUSH_ASYNC
74 )
75 ) {
76 return false;
77 }
78 return true;
79 }
80
81 /**
82 * Sync image after it's been optimized.
83 *
84 * @param int $attachment_id attachment id
85 * @param array $stats compression stats
86 *
87 * @return null
88 */
89 public function image_optimized( $attachment_id, $stats ) {
90 // Sync the attachment to GCS
91 ud_get_stateless_media()->add_media( array(), $attachment_id, true );
92
93 // also sync the backup images
94 $this->sync_backup( $attachment_id );
95 }
96
97 /**
98 * If local file don't exists then download it from GCS
99 *
100 * @param string $file_path Full file path
101 * @param string $attachment_id
102 * @param array $size_details Array of width and height for the image
103 *
104 * @return null
105 */
106 function maybe_download_file( $file_path = '', $attachment_id = '', $size_details = array() ) {
107 if( empty( $file_path ) || empty( $attachment_id ) ) {
108 return;
109 }
110
111 //Download if file not exists
112 if( !file_exists( $file_path ) ) {
113 $client = ud_get_stateless_media()->get_client();
114 $metadata = wp_get_attachment_metadata( $attachment_id );
115 if( !empty( $metadata[ 'gs_name' ] ) ) {
116 $image_sizes = Utility::get_path_and_url( $metadata, $attachment_id );
117 foreach( $image_sizes as $size => $img ) {
118 $client->get_media( apply_filters( 'wp_stateless_file_name', $img[ 'gs_name' ] ), true, $img[ 'path' ] );
119 }
120
121 $gs_name = dirname( $metadata[ 'gs_name' ] ) . '/' . basename( $file_path );
122 // We need to remove backup from GCS if it's a restore action
123 // @todo revise this code
124 if( $this->hook_from_restore_image() ) {
125 $client->remove_media( apply_filters( 'wp_stateless_file_name', $gs_name ) );
126 }
127 }
128 }
129 }
130
131 /**
132 * Remove backup when attachment is removed
133 *
134 * @param $attachment_id
135 */
136 function remove_backup( $attachment_id ) {
137 $upload_dir = wp_get_upload_dir();
138 $metadata = wp_get_attachment_metadata( $attachment_id );
139 $backup_paths = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true );
140
141 if( !empty( $metadata[ 'gs_name' ] ) && !empty( $backup_paths ) && is_array( $backup_paths ) ) {
142 // Getting local dir path for backup image
143 $base_dir = $upload_dir[ 'basedir' ] . '/' . dirname( $metadata[ 'file' ] );
144 // Getting GCS dir name from meta data. In case Bucket Folder used.
145 $gs_dir = dirname( $metadata[ 'gs_name' ] );
146 foreach( $backup_paths as $key => $data ) {
147 $gs_name = $gs_dir . '/' . basename( $data[ 'file' ] );
148 // Path of backup image
149 $backup_path = $base_dir . '/' . basename( $data[ 'file' ] );
150 do_action( 'sm:sync::deleteFile', apply_filters( 'wp_stateless_file_name', $gs_name ), $backup_path );
151 delete_transient( 'sm-wp-smush-backup-exists-' . $attachment_id );
152 }
153 }
154
155 }
156
157 /**
158 * Checks if we've backup on gcs for the given attachment id and backup path
159 *
160 * @param string $attachment_id
161 * @param string $backup_path
162 *
163 * @return bool
164 */
165 function backup_exists_on_gcs( $exists, $attachment_id = '', $backup_path = '' ) {
166 if( !$exists && $attachment_id ) {
167 if( get_transient( 'sm-wp-smush-backup-exists-' . $attachment_id ) ) {
168 return true;
169 }
170
171 $metadata = wp_get_attachment_metadata( $attachment_id );
172 if( !empty( $metadata[ 'gs_name' ] ) ) {
173 $gs_name = dirname( $metadata[ 'gs_name' ] ) . '/' . basename( $backup_path );
174 if( ud_get_stateless_media()->get_client()->media_exists( apply_filters( 'wp_stateless_file_name', $gs_name ) ) ) {
175 set_transient( 'sm-wp-smush-backup-exists-' . $attachment_id, true, HOUR_IN_SECONDS );
176 return true;
177 }
178 }
179 }
180
181 return $exists;
182 }
183
184 /**
185 * Sync backup image to GCS
186 *
187 * @param $attachment_id
188 * @param array $metadata
189 */
190 public function sync_backup( $attachment_id, $metadata = array() ) {
191 $upload_dir = wp_get_upload_dir();
192 if( empty( $metadata ) || empty( $metadata[ 'gs_name' ] ) ) {
193 $metadata = wp_get_attachment_metadata( $attachment_id );
194 }
195
196 // Getting backup path from smush settings in db
197 $backup_paths = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true );
198
199 if( !empty( $metadata[ 'gs_name' ] ) && !empty( $backup_paths ) && is_array( $backup_paths ) ) {
200 // Getting local dir for backup image
201 $base_dir = $upload_dir[ 'basedir' ] . '/' . dirname( $metadata[ 'file' ] );
202 // Getting GCS dir name from meta data. In case Bucket Folder used.
203 $gs_dir = dirname( $metadata[ 'gs_name' ] );
204
205 foreach( $backup_paths as $key => $data ) {
206 $gs_name = $gs_dir . '/' . basename( $data[ 'file' ] );
207 // Path of backup image
208 $backup_path = $base_dir . '/' . basename( $data[ 'file' ] );
209 // Sync backup image with GCS
210 do_action( 'sm:sync::syncFile', apply_filters( 'wp_stateless_file_name', $gs_name ), $backup_path );
211 delete_transient( 'sm-wp-smush-backup-exists-' . $attachment_id );
212 }
213 }
214 }
215
216 /**
217 * Determine where we hook from
218 * Is this a hook from wp smush restore image or not.
219 *
220 * @return bool
221 */
222 private function hook_from_restore_image() {
223 $call_stack = debug_backtrace();
224 $class_name = class_exists( 'WpSmushBackup' ) ? 'WpSmushBackup' : 'WP_Smush_Backup';
225
226 if( !empty( $call_stack ) && is_array( $call_stack ) ) {
227 foreach( $call_stack as $step ) {
228
229 if( $step[ 'function' ] == 'restore_image' && $step[ 'class' ] == $class_name ) {
230 return true;
231 }
232 }
233 }
234
235 return false;
236 }
237
238 }
239
240 }
241
242 }
243