* The core plugin config class.
* This maintains all the options and settings for this plugin.
* @since 1.5 Moved into /inc
* @subpackage LiteSpeed/inc
* @author LiteSpeed Technologies <info@litespeedtech.com>
defined('WPINC') || exit();
private $_updated_ids = array();
private $_is_primary = false;
* Specify init logic to avoid infinite loop when calling conf.cls instance
// Check if conf exists or not. If not, create them in DB (won't change version if is converting v2.9- data)
// Conf may be stale, upgrade later
* Detect if has quic.cloud set
if ($this->conf(self::O_CDN_QUIC)) {
!defined('LITESPEED_ALLOWED') && define('LITESPEED_ALLOWED', true);
add_action('litespeed_conf_append', array($this, 'option_append'), 10, 2);
add_action('litespeed_conf_force', array($this, 'force_option'), 10, 2);
private function _conf_db_init()
* Try to load options first, network sites can override this later
* NOTE: Load before run `conf_upgrade()` to avoid infinite loop when getting conf in `conf_upgrade()`
$ver = $this->conf(self::_VER);
* Don't upgrade or run new installations other than from backend visit at the 2nd time (delay the update)
* In this case, just use default conf
$has_delay_conf_tag = self::get_option('__activation');
if (!$ver || $ver != Core::VER) {
if ((!is_admin() && !defined('LITESPEED_CLI')) || (!$has_delay_conf_tag || $has_delay_conf_tag == -1)) {
// Reuse __activation to control the delay conf update
if (!$has_delay_conf_tag || $has_delay_conf_tag == -1) {
self::update_option('__activation', Core::VER);
$this->set_conf($this->load_default_vals());
$this->_try_load_site_options();
// Disable new installation auto upgrade to avoid overwritten to customized data.ini
defined('LITESPEED_BYPASS_AUTO_V') || define('LITESPEED_BYPASS_AUTO_V', true);
* Version is less than v3.0, or, is a new installation
// Try upgrade first (network will upgrade inside too)
Data::cls()->try_upgrade_conf_3_0();
defined('LSCWP_CUR_V') || define('LSCWP_CUR_V', $ver);
// Plugin version will be set inside
// Site plugin upgrade & version change will do in load_site_conf
Data::cls()->conf_upgrade($ver);
* Sync latest new options
if (!$ver || $ver != Core::VER) {
$this->load_default_vals();
$this->set_conf(self::$_default_options);
// Init new default/missing options
foreach (self::$_default_options as $k => $v) {
// If the option existed, bypass updating
// Bcos we may ask clients to deactivate for debug temporarily, we need to keep the current cfg in deactivation, hence we need to only try adding default cfg when activating.
self::add_option($k, $v);
// Force correct version in case a rare unexpected case that `_ver` exists but empty
self::update_option(Base::_VER, Core::VER);
* Override conf if is network subsites and chose `Use Primary Config`
$this->_try_load_site_options();
defined('LITESPEED_CONF_LOADED') || define('LITESPEED_CONF_LOADED', true);
* Activation delayed file update
* Pros: This is to avoid file correction script changed in new versions
* Cons: Conf upgrade won't get file correction if there is new values that are used in file
if ($has_delay_conf_tag && $has_delay_conf_tag != -1) {
// Check new version @since 2.9.3
Cloud::version_check('activate' . (defined('LSCWP_REF') ? '_' . LSCWP_REF : ''));
$this->update_confs(); // Files only get corrected in activation or saving settings actions.
if ($has_delay_conf_tag != -1) {
self::update_option('__activation', -1);
* Load all latest options from DB
public function load_options($blog_id = null, $dry_run = false)
foreach (self::$_default_options as $k => $v) {
if (!is_null($blog_id)) {
$options[$k] = self::get_blog_option($blog_id, $k, $v);
$options[$k] = self::get_option($k, $v);
$options[$k] = $this->type_casting($options[$k], $k);
// Bypass site special settings
// This is to load the primary settings ONLY
// These options are the ones that can be overwritten by primary
$options = array_diff_key($options, array_flip(self::$SINGLE_SITE_OPTIONS));
$this->set_primary_conf($options);
$this->set_conf($options);
if (defined('LITESPEED_CONF') && LITESPEED_CONF) {
foreach (self::$_default_options as $k => $v) {
$const = Base::conf_const($k);
$this->set_const_conf($k, $this->type_casting(constant($const), $k));
* For multisite installations, the single site options need to be updated with the network wide options.
private function _try_load_site_options()
if (!$this->_if_need_site_options()) {
$this->_conf_site_db_init();
$this->_is_primary = get_current_blog_id() == BLOG_ID_CURRENT_SITE;
// If network set to use primary setting
if ($this->network_conf(self::NETWORK_O_USE_PRIMARY) && !$this->_is_primary) {
// subsites or network admin
// Get the primary site settings
// If it's just upgraded, 2nd blog is being visited before primary blog, can just load default config (won't hurt as this could only happen shortly)
$this->load_options(BLOG_ID_CURRENT_SITE);
// Overwrite single blog options with site options
foreach (self::$_default_options as $k => $v) {
if (!$this->has_network_conf($k)) {
// $this->_options[ $k ] = $this->_network_options[ $k ];
// Special handler to `Enable Cache` option if the value is set to OFF
if ($k == self::O_CACHE) {
if ($this->_is_primary) {
if ($this->conf($k) != $this->network_conf($k)) {
if ($this->conf($k) != self::VAL_ON2) {
if ($this->network_conf(self::NETWORK_O_USE_PRIMARY)) {
if ($this->has_primary_conf($k) && $this->primary_conf($k) != self::VAL_ON2) {
// This case will use primary_options override always
if ($this->conf($k) != self::VAL_ON2) {
// primary_options will store primary settings + network settings, OR, store the network settings for subsites
$this->set_primary_conf($k, $this->network_conf($k));
// var_dump($this->_options);
* Check if needs to load site_options for network sites
private function _if_need_site_options()
// Check if needs to use site_options or not
// todo: check if site settings are separate bcos it will affect .htaccess
* In case this is called outside the admin page
* @see https://codex.wordpress.org/Function_Reference/is_plugin_active_for_network
if (!function_exists('is_plugin_active_for_network')) {
require_once ABSPATH . '/wp-admin/includes/plugin.php';
// If is not activated on network, it will not have site options
if (!is_plugin_active_for_network(Core::PLUGIN_FILE)) {
if ((int) $this->conf(self::O_CACHE) == self::VAL_ON2) {
$this->set_conf(self::_CACHE, true);
* Init site conf and upgrade if necessary
private function _conf_site_db_init()
$this->load_site_options();
$ver = $this->network_conf(self::_VER);
* Don't upgrade or run new installations other than from backend visit
* In this case, just use default conf
if (!$ver || $ver != Core::VER) {
if (!is_admin() && !defined('LITESPEED_CLI')) {
$this->set_network_conf($this->load_default_site_vals());
if ($ver && $ver != Core::VER) {
// Site plugin version will change inside
Data::cls()->conf_site_upgrade($ver);
if (!$ver || $ver != Core::VER) {
$this->load_default_site_vals();
// Init new default/missing options
foreach (self::$_default_site_options as $k => $v) {
// If the option existed, bypass updating
self::add_site_option($k, $v);
* Get the plugin's site wide options.
* If the site wide options are not set yet, set it to default.
public function load_site_options()
foreach (self::$_default_site_options as $k => $v) {
$val = self::get_site_option($k, $v);
$val = $this->type_casting($val, $k, true);
$this->set_network_conf($k, $val);
* Append a 3rd party option to default options
* This will not be affected by network use primary site setting.
* NOTE: If it is a multi switch option, need to call `_conf_multi_switch()` first
public function option_append($name, $default)
self::$_default_options[$name] = $default;
$this->set_conf($name, self::get_option($name, $default));
$this->set_conf($name, $this->type_casting($this->conf($name), $name));
* Force an option to a certain value
public function force_option($k, $v)
if (!$this->has_conf($k)) {
$v = $this->type_casting($v, $k);
if ($this->conf($k) === $v) {
Debug2::debug("[Conf] ** $k forced from " . var_export($this->conf($k), true) . ' to ' . var_export($v, true));
* Define `_CACHE` const in options ( for both single and network )
public function define_cache()
// Init global const cache on setting
$this->set_conf(self::_CACHE, false);
if ((int) $this->conf(self::O_CACHE) == self::VAL_ON || $this->conf(self::O_CDN_QUIC)) {
$this->set_conf(self::_CACHE, true);
if (!$this->_if_need_site_options()) {
$this->_define_cache_on();
// If use network setting
if ((int) $this->conf(self::O_CACHE) == self::VAL_ON2 && $this->network_conf(self::O_CACHE)) {
$this->set_conf(self::_CACHE, true);
$this->_define_cache_on();
private function _define_cache_on()
if (!$this->conf(self::_CACHE)) {
defined('LITESPEED_ALLOWED') && !defined('LITESPEED_ON') && define('LITESPEED_ON', true);
* @deprecated 4.0 Use $this->conf() instead
public static function val($id, $ori = false)
error_log('Called deprecated function \LiteSpeed\Conf::val(). Please use API call instead.');
return self::cls()->conf($id, $ori);
public function update_confs($the_matrix = false)
foreach ($the_matrix as $id => $val) {
$this->update($id, $val);
if ($this->_updated_ids) {
foreach ($this->_updated_ids as $id) {
// Special handler for QUIC.cloud domain key to clear all existing nodes
if ($id == self::O_API_KEY) {
$this->cls('Cloud')->clear_cloud();
// Special handler for crawler: reset sitemap when drop_domain setting changed
if ($id == self::O_CRAWLER_DROP_DOMAIN) {
$this->cls('Crawler_Map')->empty_map();
// Check if need to do a purge all or not
if ($this->_conf_purge_all($id)) {
Purge::purge_all('conf changed [id] ' . $id);
// Check if need to purge a tag
if ($tag = $this->_conf_purge_tag($id)) {