* Purge handlers for X-LiteSpeed-Purge.
* @since 2.2 Refactored. Changed access from public to private for most functions and class variables.
defined( 'WPINC' ) || exit();
class Purge extends Base {
* Public purge tags for X-LiteSpeed-Purge.
protected $_pub_purge = [];
* Public purge tags for X-LiteSpeed-Purge2.
protected $_pub_purge2 = [];
* Private purge tags for X-LiteSpeed-Purge (private section).
protected $_priv_purge = [];
* Whether to purge only current URL (QS helper).
protected $_purge_single = false;
const X_HEADER = 'X-LiteSpeed-Purge';
const X_HEADER2 = 'X-LiteSpeed-Purge2';
const DB_QUEUE = 'queue';
const DB_QUEUE2 = 'queue2';
const TYPE_PURGE_ALL = 'purge_all';
const TYPE_PURGE_ALL_LSCACHE = 'purge_all_lscache';
const TYPE_PURGE_ALL_CSSJS = 'purge_all_cssjs';
const TYPE_PURGE_ALL_LOCALRES = 'purge_all_localres';
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';
const TYPE_PURGE_FRONT = 'purge_front';
const TYPE_PURGE_UCSS = 'purge_ucss';
const TYPE_PURGE_FRONTPAGE = 'purge_frontpage';
const TYPE_PURGE_PAGES = 'purge_pages';
const TYPE_PURGE_ERROR = 'purge_error';
$purge_post_events = apply_filters(
'litespeed_purge_post_events',
'wp_update_comment_count',
foreach ( $purge_post_events as $event ) {
add_action( $event, [ $this, 'purge_post' ] );
// Purge post only when status is/was publish.
add_action( 'transition_post_status', [ $this, 'purge_publish' ], 10, 3 );
add_action( 'wp_update_comment_count', [ $this, 'purge_feeds' ] );
if ( $this->conf( self::O_OPTM_UCSS ) ) {
add_action( 'edit_post', __NAMESPACE__ . '\Purge::purge_ucss' );
* Only purge publish related status post.
* @param string $new_status New status.
* @param string $old_status Old status.
* @param \WP_Post $post Post object.
public function purge_publish( $new_status, $old_status, $post ) {
if ( 'publish' !== $new_status && 'publish' !== $old_status ) {
$this->purge_post( $post->ID );
* Handle all request actions from main cls.
* @since 7.6 Add VPI clear.
public function handler() {
$type = Router::verify_type();
case self::TYPE_PURGE_ALL:
case self::TYPE_PURGE_ALL_LSCACHE:
$this->_purge_all_lscache();
case self::TYPE_PURGE_ALL_CSSJS:
$this->_purge_all_cssjs();
case self::TYPE_PURGE_ALL_LOCALRES:
$this->_purge_all_localres();
case self::TYPE_PURGE_ALL_CCSS:
$this->_purge_all_ccss();
case self::TYPE_PURGE_ALL_UCSS:
$this->_purge_all_ucss();
case self::TYPE_PURGE_ALL_LQIP:
$this->_purge_all_lqip();
case self::TYPE_PURGE_ALL_VPI:
case self::TYPE_PURGE_ALL_AVATAR:
$this->_purge_all_avatar();
case self::TYPE_PURGE_ALL_OBJECT:
$this->_purge_all_object();
case self::TYPE_PURGE_ALL_OPCACHE:
$this->purge_all_opcache();
case self::TYPE_PURGE_FRONT:
case self::TYPE_PURGE_UCSS:
case self::TYPE_PURGE_FRONTPAGE:
$this->_purge_frontpage();
case self::TYPE_PURGE_PAGES:
case ( 0 === strpos( $type, self::TYPE_PURGE_ERROR ) ):
$this->_purge_error( substr( $type, strlen( self::TYPE_PURGE_ERROR ) ) );
* Shortcut to purge all lscache.
* @param string|false $reason Optional reason to log.
public static function purge_all( $reason = false ) {
self::cls()->_purge_all( $reason );
* Purge all caches (LSCache/CSS/JS/localres/object/opcache).
* @param string|false $reason Optional log string.
private function _purge_all( $reason = false ) {
$this->_purge_all_lscache( true );
$this->_purge_all_cssjs( true );
$this->_purge_all_localres( true );
$this->_purge_all_object( true );
$this->purge_all_opcache( true );
if ( $this->conf( self::O_CDN_CLOUDFLARE_CLEAR ) ) {
CDN\Cloudflare::purge_all( 'Purge All' );
$reason = is_string( $reason ) ? "( $reason )" : '';
self::debug( 'Purge all ' . $reason, 3 );
$msg = __( 'Purged all caches successfully.', 'litespeed-cache' );
if ( ! defined( 'LITESPEED_PURGE_SILENT' ) ) {
Admin_Display::success( $msg );
do_action( 'litespeed_purged_all' );
* Shortcut to purge lscache.
* @param string|false $reason Optional reason to log.
public static function purge_all_lscache( $reason = false ) {
self::debug( 'Purge lscache ' . $reason, 3 );
self::cls()->_purge_all_lscache();
* Alerts LiteSpeed Web Server to purge all pages.
* @param bool $silence If true, don't show admin notice.
private function _purge_all_lscache( $silence = false ) {
do_action( 'litespeed_purged_all_lscache' );
$msg = __( 'Notified LiteSpeed Web Server to purge all LSCache entries.', 'litespeed-cache' );
if ( ! defined( 'LITESPEED_PURGE_SILENT' ) ) {
Admin_Display::success( $msg );
* Delete all critical CSS.
* @param bool $silence If true, don't show admin notice.
private function _purge_all_ccss( $silence = false ) {
do_action( 'litespeed_purged_all_ccss' );
$this->cls( 'CSS' )->rm_cache_folder( 'ccss' );
$this->cls( 'Data' )->url_file_clean( 'ccss' );
$msg = __( 'Cleaned all Critical CSS files.', 'litespeed-cache' );
if ( ! defined( 'LITESPEED_PURGE_SILENT' ) ) {
Admin_Display::success( $msg );
* @param bool $silence If true, don't show admin notice.
private function _purge_all_ucss( $silence = false ) {
do_action( 'litespeed_purged_all_ucss' );
$this->cls( 'CSS' )->rm_cache_folder( 'ucss' );
$this->cls( 'Data' )->url_file_clean( 'ucss' );
$msg = __( 'Cleaned all Unique CSS files.', 'litespeed-cache' );
if ( ! defined( 'LITESPEED_PURGE_SILENT' ) ) {
Admin_Display::success( $msg );
* Purge one UCSS by URL or post ID.
* @param int|string $post_id_or_url Post ID or URL.
public static function purge_ucss( $post_id_or_url ) {
self::debug( 'Purge a single UCSS: ' . $post_id_or_url );
// If is post_id, generate URL.
if ( ! preg_match( '/\D/', (string) $post_id_or_url ) ) {
$post_id_or_url = get_permalink( (int) $post_id_or_url );
$post_id_or_url = (string) $post_id_or_url;
$permalink_structure = get_option( 'permalink_structure' );
if ( ! empty( $permalink_structure ) ) {
$post_id_or_url = trailingslashit( (string) $post_id_or_url );
$existing_url_files = Data::cls()->mark_as_expired( $post_id_or_url, true );
if ( $existing_url_files ) {
self::cls( 'UCSS' )->add_to_q( $existing_url_files );
* Delete all LQIP images.
* @param bool $silence If true, don't show admin notice.
private function _purge_all_lqip( $silence = false ) {
do_action( 'litespeed_purged_all_lqip' );
$this->cls( 'Placeholder' )->rm_cache_folder( 'lqip' );
$msg = __( 'Cleaned all LQIP files.', 'litespeed-cache' );
if ( ! defined( 'LITESPEED_PURGE_SILENT' ) ) {
Admin_Display::success( $msg );
* Delete all VPI data generated
* @param bool $silence If true, don't show admin notice.
private function _purge_all_vpi( $silence = false ) {
do_action( 'litespeed_purged_all_vpi' );
$wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
'DELETE FROM `' . $wpdb->postmeta . '` WHERE meta_key = %s',
$wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
'DELETE FROM `' . $wpdb->postmeta . '` WHERE meta_key = %s',
$this->cls( 'Placeholder' )->rm_cache_folder( 'vpi' );
$msg = __( 'Cleaned all VPI data.', 'litespeed-cache' );
!defined( 'LITESPEED_PURGE_SILENT' ) && Admin_Display::success( $msg );
* Delete all avatar images
* @param bool $silence If true, don't show admin notice.
private function _purge_all_avatar( $silence = false ) {
do_action( 'litespeed_purged_all_avatar' );
$this->cls( 'Data' )->table_truncate( 'avatar' );
$this->cls( 'Avatar' )->rm_cache_folder( 'avatar' );
$msg = __( 'Cleaned all Gravatar files.', 'litespeed-cache' );
if ( ! defined( 'LITESPEED_PURGE_SILENT' ) ) {
Admin_Display::success( $msg );
* Delete all localized resources.
* @param bool $silence If true, don't show admin notice.
private function _purge_all_localres( $silence = false ) {
do_action( 'litespeed_purged_all_localres' );
$this->_add( Tag::TYPE_LOCALRES );
$msg = __( 'Cleaned all localized resource entries.', 'litespeed-cache' );
if ( ! defined( 'LITESPEED_PURGE_SILENT' ) ) {
Admin_Display::success( $msg );
* Purge CSS/JS assets and related LSCache entries.
* @param bool $silence If true, don't show admin notice.
private function _purge_all_cssjs( $silence = false ) {
if ( wp_doing_cron() || defined( 'LITESPEED_DID_send_headers' ) ) {
self::debug( 'โ Bypassed cssjs delete as header sent (lscache purge after this point will fail) or doing cron' );
$this->_purge_all_lscache( $silence ); // Purge CSSJS must purge lscache too to avoid 404
do_action( 'litespeed_purged_all_cssjs' );
Optimize::update_option( Optimize::ITEM_TIMESTAMP_PURGE_CSS, time() );
$this->_add( Tag::TYPE_MIN );
$this->cls( 'CSS' )->rm_cache_folder( 'css' );
$this->cls( 'CSS' )->rm_cache_folder( 'js' );
$this->cls( 'Data' )->url_file_clean( 'css' );
$this->cls( 'Data' )->url_file_clean( 'js' );
// Clear UCSS queue as it used combined CSS to generate.
$this->clear_q( 'ucss', true );
$msg = __( 'Notified LiteSpeed Web Server to purge CSS/JS entries.', 'litespeed-cache' );
if ( ! defined( 'LITESPEED_PURGE_SILENT' ) ) {
Admin_Display::success( $msg );
* @since 7.3 Added test for opcode cache restriction.
* @param bool $silence If true, don't show admin notice.
* @return bool True on success.
public function purge_all_opcache( $silence = false ) {
if ( ! Router::opcache_enabled() ) {
self::debug( 'โ Failed to reset OPcache due to OPcache not enabled' );
$msg = __( 'OPcache is not enabled.', 'litespeed-cache' );
if ( ! defined( 'LITESPEED_PURGE_SILENT' ) ) {
Admin_Display::error( $msg );
if ( Router::opcache_restricted( __FILE__ ) ) {