* This is the main class for the LiteSpeed Cache plugin, responsible for initializing
* the plugin's core functionality, registering hooks, and handling cache-related operations.
* Note: Core doesn't allow $this->cls( 'Core' )
defined( 'WPINC' ) || exit();
class Core extends Root {
const NAME = 'LiteSpeed Cache';
const PLUGIN_NAME = 'litespeed-cache';
const PLUGIN_FILE = 'litespeed-cache/litespeed-cache.php';
const ACTION_DISMISS = 'dismiss';
const ACTION_PURGE_BY = 'PURGE_BY';
const ACTION_PURGE_EMPTYCACHE = 'PURGE_EMPTYCACHE';
const ACTION_QS_PURGE = 'PURGE';
const ACTION_QS_PURGE_SINGLE = 'PURGESINGLE'; // This will be same as `ACTION_QS_PURGE` (purge single URL only)
const ACTION_QS_SHOW_HEADERS = 'SHOWHEADERS';
const ACTION_QS_PURGE_ALL = 'purge_all';
const ACTION_QS_PURGE_EMPTYCACHE = 'empty_all';
const ACTION_QS_NOCACHE = 'NOCACHE';
const HEADER_DEBUG = 'X-LiteSpeed-Debug';
* Whether to show debug headers.
protected static $debug_show_header = false;
private $footer_comment = '';
* Define the core functionality of the plugin.
* Set the plugin name and the plugin version that can be used throughout the plugin.
* Load the dependencies, define the locale, and set the hooks for the admin area and
* the public-facing side of the site.
public function __construct() {
! defined( 'LSCWP_TS_0' ) && define( 'LSCWP_TS_0', microtime( true ) );
$this->cls( 'Conf' )->init();
$this->cls( 'API' )->init();
if ( defined( 'LITESPEED_ON' ) ) {
// Load third party detection if lscache enabled.
include_once LSCWP_DIR . 'thirdparty/entry.inc.php';
if ( $this->conf( Base::O_DEBUG_DISABLE_ALL ) || Debug2::is_tmp_disable() ) {
! defined( 'LITESPEED_DISABLE_ALL' ) && define( 'LITESPEED_DISABLE_ALL', true );
* Register plugin activate/deactivate/uninstall hooks
* NOTE: this can't be moved under after_setup_theme, otherwise activation will be bypassed
* @since 2.7.1 Disabled admin&CLI check to make frontend able to enable cache too
$plugin_file = LSCWP_DIR . 'litespeed-cache.php';
register_activation_hook( $plugin_file, [ __NAMESPACE__ . '\Activation', 'register_activation' ] );
register_deactivation_hook( $plugin_file, [ __NAMESPACE__ . '\Activation', 'register_deactivation' ] );
register_uninstall_hook( $plugin_file, __NAMESPACE__ . '\Activation::uninstall_litespeed_cache' );
if ( defined( 'LITESPEED_ON' ) ) {
// Register purge_all actions
$purge_all_events = $this->conf( Base::O_PURGE_HOOK_ALL );
if ( $this->conf( Base::O_PURGE_ON_UPGRADE ) ) {
$purge_all_events[] = 'automatic_updates_complete';
$purge_all_events[] = 'upgrader_process_complete';
$purge_all_events[] = 'admin_action_do-plugin-upgrade';
foreach ( $purge_all_events as $event ) {
// Don't allow hook to update_option because purge_all will cause infinite loop of update_option
if ( in_array( $event, [ 'update_option' ], true ) ) {
add_action( $event, __NAMESPACE__ . '\Purge::purge_all' );
// Add headers to site health check for full page cache
add_filter( 'site_status_page_cache_supported_cache_headers', function ( $cache_headers ) {
$is_cache_hit = function ( $header_value ) {
return false !== strpos( strtolower( $header_value ), 'hit' );
$cache_headers['x-litespeed-cache'] = $is_cache_hit;
$cache_headers['x-lsadc-cache'] = $is_cache_hit;
$cache_headers['x-qc-cache'] = $is_cache_hit;
add_action( 'after_setup_theme', [ $this, 'init' ] );
// Check if there is a purge request in queue
if ( ! defined( 'LITESPEED_CLI' ) ) {
$purge_queue = Purge::get_option( Purge::DB_QUEUE );
if ( $purge_queue && '-1' !== $purge_queue ) {
$this->http_header( $purge_queue );
Debug2::debug( '[Core] Purge Queue found&sent: ' . $purge_queue );
if ( '-1' !== $purge_queue ) {
Purge::update_option( Purge::DB_QUEUE, '-1' ); // Use -1 to bypass purge while still enable db update as WP's update_option will check value===false to bypass update
$purge_queue = Purge::get_option( Purge::DB_QUEUE2 );
if ( $purge_queue && '-1' !== $purge_queue ) {
$this->http_header( $purge_queue );
Debug2::debug( '[Core] Purge2 Queue found&sent: ' . $purge_queue );
if ( '-1' !== $purge_queue ) {
Purge::update_option( Purge::DB_QUEUE2, '-1' );
* Note: ESI nonce won't be available until hook after_setup_theme ESI init due to Guest Mode concern
if ( $this->cls( 'Router' )->esi_enabled() && ! function_exists( 'wp_create_nonce' ) ) {
Debug2::debug( '[ESI] Overwrite wp_create_nonce()' );
litespeed_define_nonce_func();
* The plugin initializer.
* This function checks if the cache is enabled and ready to use, then determines what actions need to be set up based on the type of user and page accessed. Output is buffered if the cache is enabled.
* NOTE: WP user doesn't init yet
* 3rd party preload hooks will be fired here too (e.g. Divi disable all in edit mode)
* @since 2.6 Added filter to all config values in Conf
do_action( 'litespeed_init' );
add_action( 'wp_ajax_async_litespeed', 'LiteSpeed\Task::async_litespeed_handler' );
add_action( 'wp_ajax_nopriv_async_litespeed', 'LiteSpeed\Task::async_litespeed_handler' );
// In `after_setup_theme`, before `init` hook
$this->cls( 'Activation' )->auto_update();
if ( is_admin() && ! wp_doing_ajax() ) {
if ( defined( 'LITESPEED_DISABLE_ALL' ) && LITESPEED_DISABLE_ALL ) {
Debug2::debug( '[Core] Bypassed due to debug disable all setting' );
do_action( 'litespeed_initing' );
ob_start( [ $this, 'send_headers_force' ] );
add_action( 'shutdown', [ $this, 'send_headers' ], 0 );
add_action( 'wp_footer', [ $this, 'footer_hook' ] );
* Check if is non-optimization simulator
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ! empty( $_GET[ Router::ACTION ] ) && 'before_optm' === $_GET[ Router::ACTION ] && ! apply_filters( 'litespeed_qs_forbidden', false ) ) {
Debug2::debug( '[Core] ⛑️ bypass_optm due to QS CTRL' );
! defined( 'LITESPEED_NO_OPTM' ) && define( 'LITESPEED_NO_OPTM', true );
$this->cls( 'Control' )->init();
$this->cls( 'Purge' )->init();
$this->cls( 'Tag' )->init();
// Load hooks that may be related to users
add_action( 'init', [ $this, 'after_user_init' ], 5 );
add_action( 'wp_loaded', [ $this, 'load_thirdparty' ], 2 );
* Run hooks after user init
public function after_user_init() {
$this->cls( 'Router' )->is_role_simulation();
// Detect if is Guest mode or not
$this->cls( 'Vary' )->after_user_init();
// Register attachment delete hook
$this->cls( 'Media' )->after_user_init();
* Preload ESI functionality for ESI request URI recovery
* @since 4.0 ESI init needs to be after Guest mode detection to bypass ESI if is under Guest mode
$this->cls( 'ESI' )->init();
if ( ! is_admin() && ! defined( 'LITESPEED_GUEST_OPTM' ) ) {
$result = $this->cls( 'Conf' )->in_optm_exc_roles();
Debug2::debug( '[Core] ⛑️ bypass_optm: hit Role Excludes setting: ' . $result );
! defined( 'LITESPEED_NO_OPTM' ) && define( 'LITESPEED_NO_OPTM', true );
$this->cls( 'Tool' )->heartbeat();
if ( ! defined( 'LITESPEED_NO_OPTM' ) || ! LITESPEED_NO_OPTM ) {
// Check missing static files
$this->cls( 'Router' )->serve_static();
$this->cls( 'Media' )->init();
$this->cls( 'Placeholder' )->init();
$this->cls( 'Router' )->can_optm() && $this->cls( 'Optimize' )->init();
$this->cls( 'Localization' )->init();
// Hook CDN for attachments
$this->cls( 'CDN' )->init();
$this->cls( 'Task' )->init();
// Load litespeed actions
$action = Router::get_action();
$this->proceed_action( $action );
$this->cls( 'GUI' )->init();
* @param string $action The action to proceed.
public function proceed_action( $action ) {
case self::ACTION_QS_SHOW_HEADERS:
self::$debug_show_header = true;
case self::ACTION_QS_PURGE:
case self::ACTION_QS_PURGE_SINGLE:
Purge::set_purge_single();
case self::ACTION_QS_PURGE_ALL:
case self::ACTION_PURGE_EMPTYCACHE:
case self::ACTION_QS_PURGE_EMPTYCACHE:
define( 'LSWCP_EMPTYCACHE', true ); // Clear all sites caches
$msg = __( 'Notified LiteSpeed Web Server to purge everything.', 'litespeed-cache' );
case self::ACTION_PURGE_BY:
$this->cls( 'Purge' )->purge_list();
$msg = __( 'Notified LiteSpeed Web Server to purge the list.', 'litespeed-cache' );
case self::ACTION_DISMISS:
$msg = $this->cls( 'Router' )->handler( $action );
if ( $msg && ! Router::is_ajax() ) {
Admin_Display::add_notice( Admin_Display::NOTICE_GREEN, $msg );
if ( Router::is_ajax() ) {
* Callback used to call the detect third party action.
* The detect action is used by third party plugin integration classes to determine if they should add the rest of their hooks.
public function load_thirdparty() {
do_action( 'litespeed_load_thirdparty' );
public function footer_hook() {
Debug2::debug( '[Core] Footer hook called' );
if ( ! defined( 'LITESPEED_FOOTER_CALLED' ) ) {
define( 'LITESPEED_FOOTER_CALLED', true );
* Trigger comment info display hook
* @param string|null $buffer The buffer to check.
private function check_is_html( $buffer = null ) {
if ( ! defined( 'LITESPEED_FOOTER_CALLED' ) ) {
Debug2::debug2( '[Core] CHK html bypass: miss footer const' );
Debug2::debug2( '[Core] CHK html bypass: doing ajax' );
Debug2::debug2( '[Core] CHK html bypass: doing cron' );
if ( empty( $_SERVER['REQUEST_METHOD'] ) || 'GET' !== $_SERVER['REQUEST_METHOD'] ) {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
Debug2::debug2( '[Core] CHK html bypass: not get method ' . wp_unslash( $_SERVER['REQUEST_METHOD'] ) );
if ( null === $buffer ) {
$buffer = ob_get_contents();
// Double check to make sure it is an HTML file
if ( strlen( $buffer ) > 300 ) {
$buffer = substr( $buffer, 0, 300 );
if ( false !== strstr( $buffer, '<!--' ) ) {
$buffer = preg_replace( '/<!--.*?-->/s', '', $buffer );
$buffer = trim( $buffer );
$buffer = File::remove_zero_space( $buffer );
$is_html = 0 === stripos( $buffer, '<html' ) || 0 === stripos( $buffer, '<!DOCTYPE' );
Debug2::debug( '[Core] Footer check failed: ' . ob_get_level() . '-' . substr( $buffer, 0, 100 ) );
Debug2::debug( '[Core] Footer check passed' );
if ( ! defined( 'LITESPEED_IS_HTML' ) ) {
define( 'LITESPEED_IS_HTML', true );
* For compatibility with plugins that have 'Bad' logic that forced all buffer output even if it is NOT their buffer.
* Usually this is called after send_headers() if following original WP process
* @param string $buffer The buffer to process.
* @return string The processed buffer.
public function send_headers_force( $buffer ) {
$this->check_is_html( $buffer );
// Hook to modify buffer before
$buffer = apply_filters( 'litespeed_buffer_before', $buffer );
* Media: Image lazyload && WebP
* GUI: Clean wrapper mainly for ESI block NOTE: this needs to be before optimizer to avoid wrapper being removed
if ( ! defined( 'LITESPEED_NO_OPTM' ) || ! LITESPEED_NO_OPTM ) {
Debug2::debug( '[Core] run hook litespeed_buffer_finalize' );
$buffer = apply_filters( 'litespeed_buffer_finalize', $buffer );
* Replace ESI preserved list
* @since 3.3 Replace this in the end to avoid `Inline JS Defer` or other Page Optm features encoded ESI tags wrongly, which caused LSWS can't recognize ESI
$buffer = $this->cls( 'ESI' )->finalize( $buffer );
$this->send_headers( true );
// Log ESI nonce buffer empty issue
if ( defined( 'LSCACHE_IS_ESI' ) && 0 === strlen( $buffer ) && ! empty( $_SERVER['REQUEST_URI'] ) ) {
// Log ref for debug purpose
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log( 'ESI buffer empty ' . wp_unslash( $_SERVER['REQUEST_URI'] ) );
$running_info_showing = defined( 'LITESPEED_IS_HTML' ) || defined( 'LSCACHE_IS_ESI' );
if ( defined( 'LSCACHE_ESI_SILENCE' ) ) {
$running_info_showing = false;
Debug2::debug( '[Core] ESI silence' );
* Silence comment for JSON request
if ( REST::cls()->is_rest() || Router::is_ajax() ) {
$running_info_showing = false;
Debug2::debug( '[Core] Silence Comment due to REST/AJAX' );
$running_info_showing = apply_filters( 'litespeed_comment', $running_info_showing );
if ( $running_info_showing && $this->footer_comment ) {
$buffer .= $this->footer_comment;