-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-litespeed-cache.php
427 lines (367 loc) · 14.7 KB
/
class-litespeed-cache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<?php
namespace SLCA\LiteSpeedCache;
use wpCloud\StatelessMedia\Compatibility;
use wpCloud\StatelessMedia\Utility;
use wpCloud\StatelessMedia\Helper;
/**
* Class LiteSpeedCache
*/
class LiteSpeedCache extends Compatibility {
protected $id = 'lscache_wp';
protected $title = 'LiteSpeed Cache';
protected $constant = 'WP_STATELESS_COMPATIBILITY_LITESPEED_CACHE';
protected $description = 'Ensures compatibility with LiteSpeed Cache plugins.';
protected $plugin_file = 'litespeed-cache/litespeed-cache.php';
protected $sm_mode_not_supported = ['stateless'];
/**
* @param $sm
*/
public function module_init($sm) {
// Sync image.
add_action('litespeed_img_pull_ori', array($this, 'sync_image'), 10, 2);
add_action('litespeed_img_pull_webp', array($this, 'sync_webp'), 10, 2);
// Deleting images from GCS.
add_action('litespeed_media_del', array($this, 'litespeed_media_del'), 10, 2);
// moving images in GCS.
add_action('litespeed_media_rename', array($this, 'litespeed_media_rename'), 10, 3);
// Adding file md5 for original image to cloud meta.
add_filter('wp_stateless_generate_cloud_meta', array($this, 'cloud_meta_add_file_md5'), 10, 5);
// override is_internal_file check.
add_filter('litespeed_media_check_ori', array($this, 'litespeed_media_check_img'), 10, 2);
add_filter('litespeed_media_check_webp', array($this, 'litespeed_media_check_img'), 10, 2);
// litespeed_media_info
add_filter('litespeed_media_info', array($this, 'litespeed_media_info'), 10, 3);
// Manual sync
add_action('sm:synced::image', array($this, 'manual_sync_backup_file'), 10, 2);
add_action('sm:pre::synced::image', array($this, 'update_md5_and_manual_sync'), 10, 1);
}
/**
* Get the md5_file hash from cloud meta.
*
* @param int $attachment_id
* @return array
*/
private function _get_md5_meta($attachment_id) {
// If WP-Stateless version >= 4.0.0
if ( function_exists('ud_stateless_db') ) {
$cloud_meta = [
'fileMd5' => apply_filters('wp_stateless_get_file_meta_value', [], $attachment_id, 'fileMd5', []),
];
} else {
$cloud_meta = get_post_meta($attachment_id, 'sm_cloud', true);
}
return $cloud_meta;
}
/**
* Update the md5_file hash in cloud meta.
*
* @param int $attachment_id
* @param array $cloud_meta
*/
private function _update_md5_meta($attachment_id, $cloud_meta) {
// If WP-Stateless version >= 4.0.0, postmeta can be limited to 'fileMd5' only
// and we should not override it
$test_meta = $cloud_meta;
unset($test_meta['fileMd5']);
if ( empty($test_meta) ) {
$test_meta = get_post_meta($attachment_id, 'sm_cloud', true);
$test_meta['fileMd5'] = $cloud_meta['fileMd5'];
$cloud_meta = $test_meta;
}
update_post_meta($attachment_id, 'sm_cloud', $cloud_meta);
// If WP-Stateless version >= 4.0.0
do_action('wp_stateless_set_file_meta', $attachment_id, 'fileMd5', $cloud_meta['fileMd5']);
}
/**
* Sync the image when Lite Speed plugin pull the optimized image.
* We need to overwrite the existing image.
* @param stdClass Object $row_img
* stdClass Object
* (
* [id] => 28
* [post_id] => 494
* [optm_status] => notified
* [src] => 2019/10/22645b39-asdf.jpg
* [srcpath_md5] => ad206986974729e1c8edc9321ed9ba9b
* [src_md5] => 9d396b4f7a261a5fac1234b292a7d585
* [root_id] => 0
* [src_filesize] => 1
* [target_filesize] => 0
* [target_saved] => 827956
* [webp_filesize] => 0
* [webp_saved] => 830743
* [server_info] => {
* "server":"https:\/\/us1.wp.api.litespeedtech.com",
* "id":"SEU98",
* "ori_md5":"3a7bb6b684d34552d75291ed4c32d399",
* "ori":"https:\/\/us1.wp.api.litespeedtech.com\/dl\/20191011\/c91821\/47721644.jpg",
* "webp_md5":"61d80e1d2799af383c820492a1208846",
* "webp":"https:\/\/us1.wp.api.litespeedtech.com\/dl\/20191011\/c91821\/47721644.jpg.webp"
* }
* )
*
* @param String $local_file
* /var/www/wp-content/uploads/2019/10/22645b39-asdf.jpg
*
*/
public function sync_image($row_img, $local_file) {
$rm_ori_bkup = apply_filters('litespeed_conf', 'img_optm-rm_bkup');
$gs_name = apply_filters('wp_stateless_file_name', $row_img->src);
$cloud_meta = $this->_get_md5_meta($row_img->post_id);
if (empty($cloud_meta)) $cloud_meta = array();
if (!$rm_ori_bkup) {
$extension = pathinfo($gs_name, PATHINFO_EXTENSION);
$bk_file = substr($gs_name, 0, -strlen($extension)) . 'bk.' . $extension;
$cloud_meta['fileMd5'][$bk_file] = $cloud_meta['fileMd5'][$gs_name];
do_action('sm:sync::copyFile', $gs_name, $bk_file);
}
$cloud_meta['fileMd5'][$gs_name] = md5_file($local_file);
$this->_update_md5_meta($row_img->post_id, $cloud_meta);
do_action('sm:sync::syncFile', $gs_name, $local_file, 2);
}
/**
* Upload webp image after LS pulled the images.
* @todo put md5_file hash creating here. $row_img might have attachment_id
* @param $row_img
* @param $local_file
*/
public function sync_webp($row_img, $local_file) {
$optm_webp = apply_filters('litespeed_conf', 'img_optm-webp');
if ($optm_webp) {
$gs_name = apply_filters('wp_stateless_file_name', $row_img->src . '.webp');
$cloud_meta = $this->_get_md5_meta($row_img->post_id);
$cloud_meta['fileMd5'][$gs_name] = md5_file($local_file);
$this->_update_md5_meta($row_img->post_id, $cloud_meta);
add_filter('upload_mimes', array($this, 'add_webp_mime'), 10, 2);
do_action('sm:sync::syncFile', $gs_name, $local_file, 2, array('use_root' => true));
}
}
/**
* Bypassing the is_internal_file check on LiteSpeed Cache.
* That check fails because we are replacing URL with GCS URL.
* So we need to override it with filter.
*
* @todo maybe we can add some validation.
*
* @param $return
* @param $url
* @return bool
*/
public function litespeed_media_check_img($return, $url) {
$image_host = ud_get_stateless_media()->get_gs_host();
if (strpos($url, $image_host) === 0) {
return true;
}
return $return;
}
/**
* Return information about a file from relative path.
*
* @param $info
* @param $short_file_path
* @param $post_id
* @return array( 'url', 'md5', 'size' )
*/
public function litespeed_media_info($info, $short_file_path, $post_id) {
if (!$post_id) return $info;
try {
$metadata = wp_get_attachment_metadata($post_id);
$cloud_meta = $this->_get_md5_meta($post_id);
if (!empty($metadata['gs_link'])) {
$short_file_path = apply_filters('wp_stateless_file_name', $short_file_path);
$url = ud_get_stateless_media()->get_gs_host() . '/' . $short_file_path;
$md5 = !empty($cloud_meta['fileMd5'][$short_file_path]) ? $cloud_meta['fileMd5'][$short_file_path] : null;
if ($metadata['file'] == $short_file_path) {
$url = $metadata['gs_link'];
} else {
foreach ($metadata['sizes'] as $size => $meta) {
if ($meta['file'] == basename($short_file_path)) {
$url = $meta['gs_link'];
break;
}
}
}
if ($md5) {
$info = array('url' => $url, 'md5' => $md5, 'size' => 1,);
}
}
} catch (\Throwable $e) {
Helper::log( $e->getMessage() );
}
return $info;
}
/**
* Deletes a file in GCS and remove the hash from cloud meta.
* @param $short_file_path
* @param $post_id
*/
public function litespeed_media_del($short_file_path, $post_id) {
$short_file_path = apply_filters('wp_stateless_file_name', $short_file_path);
do_action('sm:sync::deleteFile', $short_file_path);
}
/**
* Hooks into the rename function of the LS cache.
* And move the file in GCS.
* Also update the md5_file hash on cloud meta.
*
* @param $short_file_path
* @param $short_file_path_new
* @param $post_id
*/
public function litespeed_media_rename($short_file_path, $short_file_path_new, $post_id) {
$short_file_path = apply_filters('wp_stateless_file_name', $short_file_path);
$short_file_path_new = apply_filters('wp_stateless_file_name', $short_file_path_new);
// copy file to the new location and delete the old one.
do_action('sm:sync::moveFile', $short_file_path, $short_file_path_new);
$this->update_hash($post_id, $short_file_path_new, $short_file_path);
}
/**
* add_webp_mime
* @param $t
* @param $user
* @return mixed
*/
public function add_webp_mime($t, $user) {
$t['webp'] = 'image/webp';
return $t;
}
/**
* Move file hash from one key to another.
*
* @param $attachment_id
* @param $gs_name_new key to store md5_file.
* @param $gs_name_old whether to get md5 from another entry.
* @param bool $delete whether only remove the key.
* @return bool
*/
public function update_hash($attachment_id, $gs_name_new, $gs_name_old, $delete = false) {
try {
$cloud_meta = $this->_get_md5_meta($attachment_id);
if (!$delete) {
if ($gs_name_old && !empty($cloud_meta['fileMd5'][$gs_name_old])) {
$cloud_meta['fileMd5'][$gs_name_new] = $cloud_meta['fileMd5'][$gs_name_old];
} else {
$url = ud_get_stateless_media()->get_gs_host() . '/' . $gs_name_new;
$cloud_meta['fileMd5'][$gs_name_new] = md5_file($url);
}
}
if (isset($cloud_meta['fileMd5'][$gs_name_old])) unset($cloud_meta['fileMd5'][$gs_name_old]);
$this->_update_md5_meta($attachment_id, $cloud_meta);
return true;
} catch (\Throwable $e) {
Helper::log( $e->getMessage() );
return false;
}
return false;
}
/**
* Adds file hash to cloud meta, so that we can use it later.
*
* @param $cloud_meta
* @param $media
* @param $image_size
* @param $img
* @param $bucketLink
* @return array $cloud_meta with fileMd5
*/
public function cloud_meta_add_file_md5($cloud_meta, $media, $image_size, $img, $bucketLink) {
if ($file_hash = md5_file($img['path'])) {
$gs_name = !empty($media['name']) ? $media['name'] : $img['gs_name'];
// Storing file hash
$cloud_meta['fileMd5'][$gs_name] = $file_hash;
// If WP-Stateless version >= 4.0.0
if ( function_exists('ud_stateless_db') ) {
$metadata = $media['metadata'] ?? [];
$post_id = null;
if ( isset($metadata['object-id']) ) {
$post_id = $metadata['object-id'];
} else if ( isset($metadata['child-of']) ) {
$post_id = $metadata['child-of'];
}
$meta = apply_filters('wp_stateless_get_file_meta_value', [], $post_id, 'fileMd5', []);
$meta[$gs_name] = $file_hash;
do_action('wp_stateless_set_file_meta', $post_id, 'fileMd5', $meta);
}
}
return $cloud_meta;
}
/**
* On manual sync/regenerate we need to sync backup/webp files.
*
* @param $attachment_id
* @param $metadata
*/
public function manual_sync_backup_file($attachment_id, $metadata) {
$cloud_meta = $this->_get_md5_meta($attachment_id);
if (!empty($cloud_meta['fileMd5'])) {
$upload_dir = wp_upload_dir();
$fileMd5 = $cloud_meta['fileMd5'];
$root_dir = ud_get_stateless_media()->get('sm.root_dir');
$root_dir = apply_filters("wp_stateless_handle_root_dir", $root_dir);
$root_dir = trim($root_dir, '/ '); // Remove any forward slash and empty space.
foreach ($fileMd5 as $gs_name => $value) {
$_gs_name = str_replace($root_dir, '', $gs_name);
$local_file = $upload_dir['basedir'] . '/' . trim($_gs_name, '/');
do_action('sm:sync::syncFile', $gs_name, $local_file, true);
}
}
}
/**
* On manual sync/regenerate wp regenerate all image sizes.
* So to keep the file md5 accurate we need to update it after image is regenerated.
*
* Manual sync if it's the first time syncing and image optimized before enabling Stateless
*
* @param $attachment_id
*/
public function update_md5_and_manual_sync($attachment_id) {
$cloud_meta = $this->_get_md5_meta($attachment_id);
$metadata = wp_get_attachment_metadata($attachment_id);
$image_sizes = Utility::get_path_and_url($metadata, $attachment_id);
if (!empty($cloud_meta['fileMd5'])) {
foreach ($image_sizes as $img) {
$cloud_meta['fileMd5'][$img['gs_name']] = md5_file($img['path']);
}
}
if (empty($cloud_meta['fileMd5']) || count($cloud_meta['fileMd5']) <= 1) {
if (empty($cloud_meta)) {
$cloud_meta = array();
}
// In case image optimized before enabling Stateless.
// We only need to copy from local to GCS, otherwise file_md5 meta should be available.
foreach ($image_sizes as $img) {
$file_path = $img['path'];
$rm_ori_bkup = apply_filters('litespeed_conf', 'img_optm-rm_bkup');
$optm_webp = apply_filters('litespeed_conf', 'img_optm-webp');
if (!$rm_ori_bkup) {
$extension = pathinfo($file_path, PATHINFO_EXTENSION);
$bk_file = substr($file_path, 0, -strlen($extension)) . 'bk.' . $extension;
$bk_file_optm = substr($file_path, 0, -strlen($extension)) . 'bk.optm.' . $extension;
if (file_exists($bk_file)) {
$gs_name = apply_filters('wp_stateless_file_name', $bk_file);
$cloud_meta['fileMd5'][$gs_name] = md5_file($bk_file);
do_action('sm:sync::syncFile', $gs_name, $bk_file);
} elseif (file_exists($bk_file_optm)) {
$gs_name = apply_filters('wp_stateless_file_name', $bk_file_optm);
$cloud_meta['fileMd5'][$gs_name] = md5_file($bk_file_optm);
do_action('sm:sync::syncFile', $gs_name, $bk_file_optm);
}
}
if ($optm_webp) {
$gs_name_webp = $file_path . '.webp';
$gs_name_webp_optm = $file_path . '.optm.webp';
if (file_exists($gs_name_webp)) {
$gs_name = apply_filters('wp_stateless_file_name', $gs_name_webp);
$cloud_meta['fileMd5'][$gs_name] = md5_file($gs_name_webp);
do_action('sm:sync::syncFile', $gs_name, $gs_name_webp);
} elseif (file_exists($gs_name_webp_optm)) {
$gs_name = apply_filters('wp_stateless_file_name', $gs_name_webp_optm);
$cloud_meta['fileMd5'][$gs_name] = md5_file($gs_name_webp_optm);
do_action('sm:sync::syncFile', $gs_name, $gs_name_webp_optm);
}
}
}
}
$this->_update_md5_meta($attachment_id, $cloud_meta);
}
}