PluginProbe ʕ •ᴥ•ʔ
LiteSpeed Cache / 7.6.1
LiteSpeed Cache v7.6.1
trunk 1.0.15 1.9.1.1 2.9.9.2 3.6.4 4.6 5.7.0.1 6.5.4 7.0.0.1 7.0.1 7.1 7.2 7.3 7.3.0.1 7.4 7.5 7.5.0.1 7.6 7.6.1 7.6.2 7.7 7.8 7.8.0.1 7.8.1
litespeed-cache / src / placeholder.cls.php
litespeed-cache / src Last commit date
cdn 7 months ago data_structure 7 months ago activation.cls.php 7 months ago admin-display.cls.php 7 months ago admin-settings.cls.php 7 months ago admin.cls.php 7 months ago api.cls.php 7 months ago avatar.cls.php 7 months ago base.cls.php 7 months ago cdn.cls.php 7 months ago cloud.cls.php 7 months ago conf.cls.php 7 months ago control.cls.php 7 months ago core.cls.php 7 months ago crawler-map.cls.php 7 months ago crawler.cls.php 7 months ago css.cls.php 7 months ago data.cls.php 7 months ago data.upgrade.func.php 7 months ago db-optm.cls.php 7 months ago debug2.cls.php 7 months ago doc.cls.php 7 months ago error.cls.php 7 months ago esi.cls.php 7 months ago file.cls.php 7 months ago gui.cls.php 7 months ago health.cls.php 7 months ago htaccess.cls.php 7 months ago img-optm.cls.php 7 months ago import.cls.php 7 months ago import.preset.cls.php 7 months ago lang.cls.php 7 months ago localization.cls.php 7 months ago media.cls.php 7 months ago metabox.cls.php 7 months ago object-cache-wp.cls.php 7 months ago object-cache.cls.php 7 months ago object.lib.php 7 months ago optimize.cls.php 7 months ago optimizer.cls.php 7 months ago placeholder.cls.php 7 months ago purge.cls.php 7 months ago report.cls.php 7 months ago rest.cls.php 7 months ago root.cls.php 7 months ago router.cls.php 7 months ago str.cls.php 7 months ago tag.cls.php 7 months ago task.cls.php 7 months ago tool.cls.php 7 months ago ucss.cls.php 7 months ago utility.cls.php 7 months ago vary.cls.php 7 months ago vpi.cls.php 7 months ago
placeholder.cls.php
541 lines
1 <?php
2 // phpcs:ignoreFile
3
4 /**
5 * The PlaceHolder class
6 *
7 * @since 3.0
8 * @package LiteSpeed
9 */
10
11 namespace LiteSpeed;
12
13 defined('WPINC') || exit();
14
15 class Placeholder extends Base {
16
17 const TYPE_GENERATE = 'generate';
18 const TYPE_CLEAR_Q = 'clear_q';
19
20 private $_conf_placeholder_resp;
21 private $_conf_placeholder_resp_svg;
22 private $_conf_lqip;
23 private $_conf_lqip_qual;
24 private $_conf_lqip_min_w;
25 private $_conf_lqip_min_h;
26 private $_conf_placeholder_resp_color;
27 private $_conf_placeholder_resp_async;
28 private $_conf_ph_default;
29 private $_placeholder_resp_dict = array();
30 private $_ph_queue = array();
31
32 protected $_summary;
33
34 /**
35 * Init
36 *
37 * @since 3.0
38 */
39 public function __construct() {
40 $this->_conf_placeholder_resp = defined('LITESPEED_GUEST_OPTM') || $this->conf(self::O_MEDIA_PLACEHOLDER_RESP);
41 $this->_conf_placeholder_resp_svg = $this->conf(self::O_MEDIA_PLACEHOLDER_RESP_SVG);
42 $this->_conf_lqip = !defined('LITESPEED_GUEST_OPTM') && $this->conf(self::O_MEDIA_LQIP);
43 $this->_conf_lqip_qual = $this->conf(self::O_MEDIA_LQIP_QUAL);
44 $this->_conf_lqip_min_w = $this->conf(self::O_MEDIA_LQIP_MIN_W);
45 $this->_conf_lqip_min_h = $this->conf(self::O_MEDIA_LQIP_MIN_H);
46 $this->_conf_placeholder_resp_async = $this->conf(self::O_MEDIA_PLACEHOLDER_RESP_ASYNC);
47 $this->_conf_placeholder_resp_color = $this->conf(self::O_MEDIA_PLACEHOLDER_RESP_COLOR);
48 $this->_conf_ph_default = $this->conf(self::O_MEDIA_LAZY_PLACEHOLDER) ?: LITESPEED_PLACEHOLDER;
49
50 $this->_summary = self::get_summary();
51 }
52
53 /**
54 * Init Placeholder
55 */
56 public function init() {
57 Debug2::debug2('[LQIP] init');
58
59 add_action('litespeed_after_admin_init', array( $this, 'after_admin_init' ));
60 }
61
62 /**
63 * Display column in Media
64 *
65 * @since 3.0
66 * @access public
67 */
68 public function after_admin_init() {
69 if ($this->_conf_lqip) {
70 add_filter('manage_media_columns', array( $this, 'media_row_title' ));
71 add_filter('manage_media_custom_column', array( $this, 'media_row_actions' ), 10, 2);
72 add_action('litespeed_media_row_lqip', array( $this, 'media_row_con' ));
73 }
74 }
75
76 /**
77 * Media Admin Menu -> LQIP col
78 *
79 * @since 3.0
80 * @access public
81 */
82 public function media_row_title( $posts_columns ) {
83 $posts_columns['lqip'] = __('LQIP', 'litespeed-cache');
84
85 return $posts_columns;
86 }
87
88 /**
89 * Media Admin Menu -> LQIP Column
90 *
91 * @since 3.0
92 * @access public
93 */
94 public function media_row_actions( $column_name, $post_id ) {
95 if ($column_name !== 'lqip') {
96 return;
97 }
98
99 do_action('litespeed_media_row_lqip', $post_id);
100 }
101
102 /**
103 * Display LQIP column
104 *
105 * @since 3.0
106 * @access public
107 */
108 public function media_row_con( $post_id ) {
109 $meta_value = wp_get_attachment_metadata($post_id);
110
111 if (empty($meta_value['file'])) {
112 return;
113 }
114
115 $total_files = 0;
116
117 // List all sizes
118 $all_sizes = array( $meta_value['file'] );
119 $size_path = pathinfo($meta_value['file'], PATHINFO_DIRNAME) . '/';
120 foreach ($meta_value['sizes'] as $v) {
121 $all_sizes[] = $size_path . $v['file'];
122 }
123
124 foreach ($all_sizes as $short_path) {
125 $lqip_folder = LITESPEED_STATIC_DIR . '/lqip/' . $short_path;
126
127 if (is_dir($lqip_folder)) {
128 Debug2::debug('[LQIP] Found folder: ' . $short_path);
129
130 // List all files
131 foreach (scandir($lqip_folder) as $v) {
132 if ($v == '.' || $v == '..') {
133 continue;
134 }
135
136 if ($total_files == 0) {
137 echo '<div class="litespeed-media-lqip"><img src="' .
138 Str::trim_quotes(File::read($lqip_folder . '/' . $v)) .
139 '" alt="' .
140 sprintf(__('LQIP image preview for size %s', 'litespeed-cache'), $v) .
141 '"></div>';
142 }
143
144 echo '<div class="litespeed-media-size"><a href="' . Str::trim_quotes(File::read($lqip_folder . '/' . $v)) . '" target="_blank">' . $v . '</a></div>';
145
146 ++$total_files;
147 }
148 }
149 }
150
151 if ($total_files == 0) {
152 echo '';
153 }
154 }
155
156 /**
157 * Replace image with placeholder
158 *
159 * @since 3.0
160 * @access public
161 */
162 public function replace( $html, $src, $size ) {
163 // Check if need to enable responsive placeholder or not
164 $this_placeholder = $this->_placeholder($src, $size) ?: $this->_conf_ph_default;
165
166 $additional_attr = '';
167 if ($this->_conf_lqip && $this_placeholder != $this->_conf_ph_default) {
168 Debug2::debug2('[LQIP] Use resp LQIP [size] ' . $size);
169 $additional_attr = ' data-placeholder-resp="' . Str::trim_quotes($size) . '"';
170 }
171
172 $snippet = defined('LITESPEED_GUEST_OPTM') || $this->conf(self::O_OPTM_NOSCRIPT_RM) ? '' : '<noscript>' . $html . '</noscript>';
173 $html = str_replace(array( ' src=', ' srcset=', ' sizes=' ), array( ' data-src=', ' data-srcset=', ' data-sizes=' ), $html);
174 $html = str_replace('<img ', '<img data-lazyloaded="1"' . $additional_attr . ' src="' . Str::trim_quotes($this_placeholder) . '" ', $html);
175 $snippet = $html . $snippet;
176
177 return $snippet;
178 }
179
180 /**
181 * Generate responsive placeholder
182 *
183 * @since 2.5.1
184 * @access private
185 */
186 private function _placeholder( $src, $size ) {
187 // Low Quality Image Placeholders
188 if (!$size) {
189 Debug2::debug2('[LQIP] no size ' . $src);
190 return false;
191 }
192
193 if (!$this->_conf_placeholder_resp) {
194 return false;
195 }
196
197 // If use local generator
198 if (!$this->_conf_lqip || !$this->_lqip_size_check($size)) {
199 return $this->_generate_placeholder_locally($size);
200 }
201
202 Debug2::debug2('[LQIP] Resp LQIP process [src] ' . $src . ' [size] ' . $size);
203
204 $arr_key = $size . ' ' . $src;
205
206 // Check if its already in dict or not
207 if (!empty($this->_placeholder_resp_dict[$arr_key])) {
208 Debug2::debug2('[LQIP] already in dict');
209
210 return $this->_placeholder_resp_dict[$arr_key];
211 }
212
213 // Need to generate the responsive placeholder
214 $placeholder_realpath = $this->_placeholder_realpath($src, $size); // todo: give offload API
215 if (file_exists($placeholder_realpath)) {
216 Debug2::debug2('[LQIP] file exists');
217 $this->_placeholder_resp_dict[$arr_key] = File::read($placeholder_realpath);
218
219 return $this->_placeholder_resp_dict[$arr_key];
220 }
221
222 // Add to cron queue
223
224 // Prevent repeated requests
225 if (in_array($arr_key, $this->_ph_queue)) {
226 Debug2::debug2('[LQIP] file bypass generating due to in queue');
227 return $this->_generate_placeholder_locally($size);
228 }
229
230 if ($hit = Utility::str_hit_array($src, $this->conf(self::O_MEDIA_LQIP_EXC))) {
231 Debug2::debug2('[LQIP] file bypass generating due to exclude setting [hit] ' . $hit);
232 return $this->_generate_placeholder_locally($size);
233 }
234
235 $this->_ph_queue[] = $arr_key;
236
237 // Send request to generate placeholder
238 if (!$this->_conf_placeholder_resp_async) {
239 // If requested recently, bypass
240 if ($this->_summary && !empty($this->_summary['curr_request']) && time() - $this->_summary['curr_request'] < 300) {
241 Debug2::debug2('[LQIP] file bypass generating due to interval limit');
242 return false;
243 }
244 // Generate immediately
245 $this->_placeholder_resp_dict[$arr_key] = $this->_generate_placeholder($arr_key);
246
247 return $this->_placeholder_resp_dict[$arr_key];
248 }
249
250 // Prepare default svg placeholder as tmp placeholder
251 $tmp_placeholder = $this->_generate_placeholder_locally($size);
252
253 // Store it to prepare for cron
254 $queue = $this->load_queue('lqip');
255 if (in_array($arr_key, $queue)) {
256 Debug2::debug2('[LQIP] already in queue');
257
258 return $tmp_placeholder;
259 }
260
261 if (count($queue) > 500) {
262 Debug2::debug2('[LQIP] queue is full');
263
264 return $tmp_placeholder;
265 }
266
267 $queue[] = $arr_key;
268 $this->save_queue('lqip', $queue);
269 Debug2::debug('[LQIP] Added placeholder queue');
270
271 return $tmp_placeholder;
272 }
273
274 /**
275 * Generate realpath of placeholder file
276 *
277 * @since 2.5.1
278 * @access private
279 */
280 private function _placeholder_realpath( $src, $size ) {
281 // Use LQIP Cloud generator, each image placeholder will be separately stored
282
283 // Compatibility with WebP and AVIF
284 $src = Utility::drop_webp($src);
285
286 $filepath_prefix = $this->_build_filepath_prefix('lqip');
287
288 // External images will use cache folder directly
289 $domain = parse_url($src, PHP_URL_HOST);
290 if ($domain && !Utility::internal($domain)) {
291 // todo: need to improve `util:internal()` to include `CDN::internal()`
292 $md5 = md5($src);
293
294 return LITESPEED_STATIC_DIR . $filepath_prefix . 'remote/' . substr($md5, 0, 1) . '/' . substr($md5, 1, 1) . '/' . $md5 . '.' . $size;
295 }
296
297 // Drop domain
298 $short_path = Utility::att_short_path($src);
299
300 return LITESPEED_STATIC_DIR . $filepath_prefix . $short_path . '/' . $size;
301 }
302
303 /**
304 * Cron placeholder generation
305 *
306 * @since 2.5.1
307 * @access public
308 */
309 public static function cron( $continue = false ) {
310 $_instance = self::cls();
311
312 $queue = $_instance->load_queue('lqip');
313
314 if (empty($queue)) {
315 return;
316 }
317
318 // For cron, need to check request interval too
319 if (!$continue) {
320 if (!empty($_instance->_summary['curr_request']) && time() - $_instance->_summary['curr_request'] < 300) {
321 Debug2::debug('[LQIP] Last request not done');
322 return;
323 }
324 }
325
326 foreach ($queue as $v) {
327 Debug2::debug('[LQIP] cron job [size] ' . $v);
328
329 $res = $_instance->_generate_placeholder($v, true);
330
331 // Exit queue if out of quota
332 if ($res === 'out_of_quota') {
333 return;
334 }
335
336 // only request first one
337 if (!$continue) {
338 return;
339 }
340 }
341 }
342
343 /**
344 * Generate placeholder locally
345 *
346 * @since 3.0
347 * @access private
348 */
349 private function _generate_placeholder_locally( $size ) {
350 Debug2::debug2('[LQIP] _generate_placeholder local [size] ' . $size);
351
352 $size = explode('x', $size);
353
354 $svg = str_replace(array( '{width}', '{height}', '{color}' ), array( $size[0], $size[1], $this->_conf_placeholder_resp_color ), $this->_conf_placeholder_resp_svg);
355
356 return 'data:image/svg+xml;base64,' . base64_encode($svg);
357 }
358
359 /**
360 * Send to LiteSpeed API to generate placeholder
361 *
362 * @since 2.5.1
363 * @access private
364 */
365 private function _generate_placeholder( $raw_size_and_src, $from_cron = false ) {
366 // Parse containing size and src info
367 $size_and_src = explode(' ', $raw_size_and_src, 2);
368 $size = $size_and_src[0];
369
370 if (empty($size_and_src[1])) {
371 $this->_popup_and_save($raw_size_and_src);
372 Debug2::debug('[LQIP] ❌ No src [raw] ' . $raw_size_and_src);
373 return $this->_generate_placeholder_locally($size);
374 }
375
376 $src = $size_and_src[1];
377
378 $file = $this->_placeholder_realpath($src, $size);
379
380 // Local generate SVG to serve ( Repeatedly doing this here to remove stored cron queue in case the setting _conf_lqip is changed )
381 if (!$this->_conf_lqip || !$this->_lqip_size_check($size)) {
382 $data = $this->_generate_placeholder_locally($size);
383 } else {
384 $err = false;
385 $allowance = Cloud::cls()->allowance(Cloud::SVC_LQIP, $err);
386 if (!$allowance) {
387 Debug2::debug('[LQIP] ❌ No credit: ' . $err);
388 $err && Admin_Display::error(Error::msg($err));
389
390 if ($from_cron) {
391 return 'out_of_quota';
392 }
393
394 return $this->_generate_placeholder_locally($size);
395 }
396
397 // Generate LQIP
398 list($width, $height) = explode('x', $size);
399 $req_data = array(
400 'width' => $width,
401 'height' => $height,
402 'url' => Utility::drop_webp($src),
403 'quality' => $this->_conf_lqip_qual,
404 );
405
406 // CHeck if the image is 404 first
407 if (File::is_404($req_data['url'])) {
408 $this->_popup_and_save($raw_size_and_src, true);
409 $this->_append_exc($src);
410 Debug2::debug('[LQIP] 404 before request [src] ' . $req_data['url']);
411 return $this->_generate_placeholder_locally($size);
412 }
413
414 // Update request status
415 $this->_summary['curr_request'] = time();
416 self::save_summary();
417
418 $json = Cloud::post(Cloud::SVC_LQIP, $req_data, 120);
419 if (!is_array($json)) {
420 return $this->_generate_placeholder_locally($size);
421 }
422
423 if (empty($json['lqip']) || strpos($json['lqip'], 'data:image/svg+xml') !== 0) {
424 // image error, pop up the current queue
425 $this->_popup_and_save($raw_size_and_src, true);
426 $this->_append_exc($src);
427 Debug2::debug('[LQIP] wrong response format', $json);
428
429 return $this->_generate_placeholder_locally($size);
430 }
431
432 $data = $json['lqip'];
433
434 Debug2::debug('[LQIP] _generate_placeholder LQIP');
435 }
436
437 // Write to file
438 File::save($file, $data, true);
439
440 // Save summary data
441 $this->_summary['last_spent'] = time() - $this->_summary['curr_request'];
442 $this->_summary['last_request'] = $this->_summary['curr_request'];
443 $this->_summary['curr_request'] = 0;
444 self::save_summary();
445 $this->_popup_and_save($raw_size_and_src);
446
447 Debug2::debug('[LQIP] saved LQIP ' . $file);
448
449 return $data;
450 }
451
452 /**
453 * Check if the size is valid to send LQIP request or not
454 *
455 * @since 3.0
456 */
457 private function _lqip_size_check( $size ) {
458 $size = explode('x', $size);
459 if ($size[0] >= $this->_conf_lqip_min_w || $size[1] >= $this->_conf_lqip_min_h) {
460 return true;
461 }
462
463 Debug2::debug2('[LQIP] Size too small');
464
465 return false;
466 }
467
468 /**
469 * Add to LQIP exclude list
470 *
471 * @since 3.4
472 */
473 private function _append_exc( $src ) {
474 $val = $this->conf(self::O_MEDIA_LQIP_EXC);
475 $val[] = $src;
476 $this->cls('Conf')->update(self::O_MEDIA_LQIP_EXC, $val);
477 Debug2::debug('[LQIP] Appended to LQIP Excludes [URL] ' . $src);
478 }
479
480 /**
481 * Pop up the current request and save
482 *
483 * @since 3.0
484 */
485 private function _popup_and_save( $raw_size_and_src, $append_to_exc = false ) {
486 $queue = $this->load_queue('lqip');
487 if (!empty($queue) && in_array($raw_size_and_src, $queue)) {
488 unset($queue[array_search($raw_size_and_src, $queue)]);
489 }
490
491 if ($append_to_exc) {
492 $size_and_src = explode(' ', $raw_size_and_src, 2);
493 $this_src = $size_and_src[1];
494
495 // Append to lqip exc setting first
496 $this->_append_exc($this_src);
497
498 // Check if other queues contain this src or not
499 if ($queue) {
500 foreach ($queue as $k => $raw_size_and_src) {
501 $size_and_src = explode(' ', $raw_size_and_src, 2);
502 if (empty($size_and_src[1])) {
503 continue;
504 }
505
506 if ($size_and_src[1] == $this_src) {
507 unset($queue[$k]);
508 }
509 }
510 }
511 }
512
513 $this->save_queue('lqip', $queue);
514 }
515
516 /**
517 * Handle all request actions from main cls
518 *
519 * @since 2.5.1
520 * @access public
521 */
522 public function handler() {
523 $type = Router::verify_type();
524
525 switch ($type) {
526 case self::TYPE_GENERATE:
527 self::cron(true);
528 break;
529
530 case self::TYPE_CLEAR_Q:
531 $this->clear_q('lqip');
532 break;
533
534 default:
535 break;
536 }
537
538 Admin::redirect();
539 }
540 }
541