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 / wp-smush.php

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

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