From 29af4c78db99657d93fbc3c169332a7a277c1479 Mon Sep 17 00:00:00 2001 From: Tim LSC Date: Mon, 7 Apr 2025 22:55:48 +0300 Subject: [PATCH 1/2] Add Clear VPI VPI class add constants --- src/gui.cls.php | 19 +++++++++++++++++++ src/media.cls.php | 2 +- src/metabox.cls.php | 4 ++-- src/purge.cls.php | 26 ++++++++++++++++++++++++++ src/vpi.cls.php | 5 ++++- tpl/toolbox/purge.tpl.php | 9 +++++++++ 6 files changed, 61 insertions(+), 4 deletions(-) diff --git a/src/gui.cls.php b/src/gui.cls.php index 105e766c7..0681555a4 100644 --- a/src/gui.cls.php +++ b/src/gui.cls.php @@ -574,6 +574,15 @@ public function frontend_shortcut() )); } + if ($this->has_cache_folder('vpi')) { + $wp_admin_bar->add_menu(array( + 'parent' => 'litespeed-menu', + 'id' => 'litespeed-purge-vpi', + 'title' => __('Purge All', 'litespeed-cache') . ' - VPI', + 'href' => Utility::build_url(Router::ACTION_PURGE, Purge::TYPE_PURGE_ALL_VPI, false, '_ori'), + )); + } + if ($this->has_cache_folder('avatar')) { $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', @@ -734,6 +743,16 @@ public function backend_shortcut() )); } + if ($this->has_cache_folder('vpi')) { + $wp_admin_bar->add_menu(array( + 'parent' => 'litespeed-menu', + 'id' => 'litespeed-purge-placeholder', + 'title' => __('Purge All', 'litespeed-cache') . ' - ' . __('VPI data', 'litespeed-cache'), + 'href' => Utility::build_url(Router::ACTION_PURGE, Purge::TYPE_PURGE_ALL_VPI), + 'meta' => array('tabindex' => '0'), + )); + } + if ($this->has_cache_folder('avatar')) { $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', diff --git a/src/media.cls.php b/src/media.cls.php index ad043c103..c08832f4f 100644 --- a/src/media.cls.php +++ b/src/media.cls.php @@ -646,7 +646,7 @@ private function _parse_img_for_preload() { // Load VPI setting $is_mobile = $this->_separate_mobile(); - $vpi_files = $this->cls('Metabox')->setting($is_mobile ? 'litespeed_vpi_list_mobile' : 'litespeed_vpi_list'); + $vpi_files = $this->cls('Metabox')->setting($is_mobile ? $this->cls('Vpi')::POST_META_MOBILE : $this->cls('Vpi')::POST_META); if ($vpi_files) { $vpi_files = Utility::sanitize_lines($vpi_files, 'basename'); } diff --git a/src/metabox.cls.php b/src/metabox.cls.php index 266bb1db9..5b23de2f3 100644 --- a/src/metabox.cls.php +++ b/src/metabox.cls.php @@ -131,7 +131,7 @@ public function setting($conf, $post_id = false) */ public function save($post_id, $name, $val, $is_append = false) { - if (strpos($name, 'litespeed_vpi_list') !== false) { + if (strpos($name, $this->cls('Vpi')::POST_META) !== false) { $val = Utility::sanitize_lines($val, 'basename,drop_webp'); } @@ -158,7 +158,7 @@ public function save($post_id, $name, $val, $is_append = false) public function lazy_img_excludes($list) { $is_mobile = $this->_separate_mobile(); - $excludes = $this->setting($is_mobile ? 'litespeed_vpi_list_mobile' : 'litespeed_vpi_list'); + $excludes = $this->setting($is_mobile ? $this->cls('Vpi')::POST_META_POST : $this->cls('Vpi')::POST_META); if ($excludes !== null) { $excludes = Utility::sanitize_lines($excludes, 'basename'); if ($excludes) { diff --git a/src/purge.cls.php b/src/purge.cls.php index 74133d2ac..c74b55493 100644 --- a/src/purge.cls.php +++ b/src/purge.cls.php @@ -32,6 +32,7 @@ class Purge extends Base const TYPE_PURGE_ALL_CCSS = 'purge_all_ccss'; const TYPE_PURGE_ALL_UCSS = 'purge_all_ucss'; const TYPE_PURGE_ALL_LQIP = 'purge_all_lqip'; + const TYPE_PURGE_ALL_VPI = 'purge_all_vpi'; const TYPE_PURGE_ALL_AVATAR = 'purge_all_avatar'; const TYPE_PURGE_ALL_OBJECT = 'purge_all_object'; const TYPE_PURGE_ALL_OPCACHE = 'purge_all_opcache'; @@ -127,6 +128,10 @@ public function handler() $this->_purge_all_lqip(); break; + case self::TYPE_PURGE_ALL_VPI: + $this->_purge_all_vpi(); + break; + case self::TYPE_PURGE_ALL_AVATAR: $this->_purge_all_avatar(); break; @@ -318,6 +323,27 @@ private function _purge_all_lqip($silence = false) } } + /** + * Delete all VPI data generated + * + * @since 7.1 + * @access private + */ + private function _purge_all_vpi($silence = false) + { + global $wpdb; + do_action('litespeed_purged_all_vpi'); + + $wpdb->query("DELETE FROM `$wpdb->postmeta` WHERE meta_key = '" . $this->cls('Vpi')::POST_META . "'"); + $wpdb->query("DELETE FROM `$wpdb->postmeta` WHERE meta_key = '" . $this->cls('Vpi')::POST_META_MOBILE . "'"); + $this->cls('Placeholder')->rm_cache_folder('vpi'); + + if (!$silence) { + $msg = __('Cleaned all VPI data.', 'litespeed-cache'); + !defined('LITESPEED_PURGE_SILENT') && Admin_Display::success($msg); + } + } + /** * Delete all avatar images * diff --git a/src/vpi.cls.php b/src/vpi.cls.php index 4a7219dd3..2ef2415c6 100644 --- a/src/vpi.cls.php +++ b/src/vpi.cls.php @@ -17,6 +17,9 @@ class VPI extends Base const TYPE_GEN = 'gen'; const TYPE_CLEAR_Q = 'clear_q'; + const POST_META = 'litespeed_vpi_list'; + const POST_META_MOBILE = 'litespeed_vpi_list_mobile'; + protected $_summary; private $_queue; @@ -126,7 +129,7 @@ public function notify() // Save data if (!empty($v['data_vpi'])) { $post_id = $this->_queue[$queue_k]['post_id']; - $name = !empty($v['is_mobile']) ? 'litespeed_vpi_list_mobile' : 'litespeed_vpi_list'; + $name = !empty($v['is_mobile']) ? self::POST_META_MOBILE : self::POST_META; $urldecode = is_array($v['data_vpi']) ? array_map('urldecode', $v['data_vpi']) : urldecode($v['data_vpi']); self::debug('save data_vpi', $urldecode); $this->cls('Metabox')->save($post_id, $name, $urldecode); diff --git a/tpl/toolbox/purge.tpl.php b/tpl/toolbox/purge.tpl.php index 8b3dd04e4..eab62ed46 100644 --- a/tpl/toolbox/purge.tpl.php +++ b/tpl/toolbox/purge.tpl.php @@ -93,6 +93,15 @@ ); } +if ($this->has_cache_folder('vpi')) { + $_panels[] = array( + 'title' => __('Purge All', 'litespeed-cache') . ' - ' . __('VPI', 'litespeed-cache'), + 'desc' => __('This will delete all generated Viewport Images', 'litespeed-cache'), + 'icon' => 'purge-front', + 'append_url' => Purge::TYPE_PURGE_ALL_VPI, + ); +} + if ($this->has_cache_folder('avatar')) { $_panels[] = array( 'title' => __('Purge All', 'litespeed-cache') . ' - ' . __('Gravatar Cache', 'litespeed-cache'), From 8bc5dfb280999dbe5d3db34baecaeea186445aae Mon Sep 17 00:00:00 2001 From: Tim LSC Date: Mon, 21 Apr 2025 08:14:08 +0300 Subject: [PATCH 2/2] Reviews --- src/gui.cls.php | 4 ++-- src/media.cls.php | 2 +- tpl/toolbox/purge.tpl.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/gui.cls.php b/src/gui.cls.php index 0681555a4..5d983faa7 100644 --- a/src/gui.cls.php +++ b/src/gui.cls.php @@ -746,8 +746,8 @@ public function backend_shortcut() if ($this->has_cache_folder('vpi')) { $wp_admin_bar->add_menu(array( 'parent' => 'litespeed-menu', - 'id' => 'litespeed-purge-placeholder', - 'title' => __('Purge All', 'litespeed-cache') . ' - ' . __('VPI data', 'litespeed-cache'), + 'id' => 'litespeed-purge-vpi', + 'title' => __('Purge All', 'litespeed-cache') . ' - VPI', 'href' => Utility::build_url(Router::ACTION_PURGE, Purge::TYPE_PURGE_ALL_VPI), 'meta' => array('tabindex' => '0'), )); diff --git a/src/media.cls.php b/src/media.cls.php index c08832f4f..2fd8d6e5e 100644 --- a/src/media.cls.php +++ b/src/media.cls.php @@ -646,7 +646,7 @@ private function _parse_img_for_preload() { // Load VPI setting $is_mobile = $this->_separate_mobile(); - $vpi_files = $this->cls('Metabox')->setting($is_mobile ? $this->cls('Vpi')::POST_META_MOBILE : $this->cls('Vpi')::POST_META); + $vpi_files = $this->cls('Metabox')->setting($is_mobile ? VPI::POST_META_MOBILE : VPI::POST_META); if ($vpi_files) { $vpi_files = Utility::sanitize_lines($vpi_files, 'basename'); } diff --git a/tpl/toolbox/purge.tpl.php b/tpl/toolbox/purge.tpl.php index eab62ed46..c421a378b 100644 --- a/tpl/toolbox/purge.tpl.php +++ b/tpl/toolbox/purge.tpl.php @@ -95,7 +95,7 @@ if ($this->has_cache_folder('vpi')) { $_panels[] = array( - 'title' => __('Purge All', 'litespeed-cache') . ' - ' . __('VPI', 'litespeed-cache'), + 'title' => __('Purge All', 'litespeed-cache') . ' - VPI', 'desc' => __('This will delete all generated Viewport Images', 'litespeed-cache'), 'icon' => 'purge-front', 'append_url' => Purge::TYPE_PURGE_ALL_VPI,