JFIF $ d d 7 * Z qn )ZP 0 !1 A "#a$2Qr D8 a Ri [f \m I ykI w0 c u F c Rı?lO7 к _f˓ [C $殷WF< _W ԣ s Kc ëI zyQy /_LK ℂ ;C",p F A : /] =H ~ , l s / 9 ć: [=/#f ; ) x { ٛE Q )~ = 𘙲r*2~ a _V = ' ku m F D } KYYC )( { * g& f `툪 r y ` = ^ c J. I ]( * `wq 1dđ #̩ ͑ 0 ; H ] u 搂 @:~ ו K L N s h } O I R* 8 :2 ! lD JV o ( 3 = M ( z Ȱ+i *N A r6K n S l ) ! J J Ӂ* % ݉?|D } d5: eP0 R; {$X' x F @ .ÊB {,WJ uQ ɲRI ;9 Q E琯 6 2fT .DU J ;*c P A\IL Nj! J ۱+ O\ ͔ ] ޒ S J ȧ c%A N ol ՎprU L Zԛe r E2= X D Xg VQeӓ k y P7 U* o m Q I s ,K `) 6\G3t?pgjr m ۛجw luGt f h 9uyP 0D;Uڽ"OXl i f $ ) &|ML 0 Z rm1 [ H XP l P R0 'G=i 2N +0 e 2 ]] 9 VT P O 7 h(F * 癈 ' = Q VZD F,d ߬ ~ T X G[ `le 69C R(!S2! P < 0 x < !1AQ "Raq 02Br #S CTb ? Ζ "]mH5WR 7 k.ۛ ! } Q~ + yԏ z |@ T20 S~ K e k *zF f^2X *( @ 8 r ? C I uI|֓>^Ex LgNU Y+{.R Ѫ τV YT D I 6 2 '8Y2 7'\T P.6 d &˦ @V q i |8 -O Ε ] ʔ U= T L 8= ;6c | !qfF3 a ů& ~$ l} 'NW U s$U k^S V : U#6w + +s &r+n ڐ { @ 29gL u " T Ù M=6 (^ "7 r}=6Yݾ lC u hquymp Ǧ G jhs ǜNl ɻ} o 7 #S6 a w 4 ! OS r D 5 7 % | ?x> L | / nD 6?/8 w # [ ) L 7 +6〼T A T g ! %5 Mm Z /c -{1_Je "| ^$ 'O & ޱմ < H vNqϡQ$ < 6 &Nݛ ֨tǻN (9 n ru~'όN sMG& ,uH Kw *Ī 5@ u6 BԒ i YN * f 0 _ GhKW u lT <nj- 0{ M L~ | /a v\ E = ?1 \ɟuq Ђ Y O a K ޡP q $ H8o Q˼EV ~ k 0 h' L Pp M -M" m^ !6l nմ 쮝C U T . ! :vMǨb h ډ Vc r @~p # !1QAa q ? AL0L x LU 4 d ; x " 16ad0 7.\ߋ 0 = P l v/ " c A Ay A|2pO8"d MV #1] = \ " ˡ ] k v I9 d ۓ & q ) ǐq E ꞅ v "P )q X &Q З non]я u ג./ ; gZ$ M QР 2M ]h ni 0 P $ێ 3CɏGjώy : m c # S_ l@ '_ \m|C 2`K _ 1 cIvGc $? PCQ S+D ? K aײ k Fr b 8 ύ NEدg ĝ U 3 6 #Ik+ v { v i ( ވ ? a e) X xy/*@Q Ǽ Ծ ) $ IWO l c G; !i k{ gOŀ L ('x T rb $ w) R $ & N1E td U3Uȉ1p M"N* (DNy d96.(jQ) X 5 c QɎMy W ? Q *!R > 6=7)Xj 5`J ] e 8%t ! + ' ! 1Q 5 !1 AQaq ё #2 "0BRb ? G t^ ## .l lQ T $ v , ,m 㵜5u bV =sY+ @d { N ! dn O < .- B; _w J t 6;Q Jd. Q c%p{ 1 ,sND dF H I0 Гo X ш e黅X ۢ F :)[ F G X ƹ / w _c MeD ,ʡc c . W D tA $ j@ : ) - #u c1< @ۗ9F ) KJ- hpP] _ x [ q B lb p ʖ w q " LFGd ƶ * s +ډ_Zc " ? % t[I P 6 J ]# =ɺ V v v CGs Gh1 > )6 |e y ? L ӣ m,4 G W Ui ` ] uJVo VDG< SB 6ϏQ @ TiU l y O U 0 kfV ~ ~}S Z @ *WU Ui ## ; s /[ = ! 7}" W N ] ' (L ! ~ y5 g9T̅J kbM' +s :S +B) v @ M j e C f j E 0 Y\Qn z G 1 д~ W o{T 9?` Rm yhsy3 !HAD ] m c1 ~ 2 L S u7x T ;j$ `} 4-> L# vzŏ I L S ֭ T { r jGK C ; b pU= - ` Bs K. S F w4 M q] Z dH S 0 ) tLg
<?php
/**
* The core plugin config class.
*
* This maintains all the options and settings for this plugin.
*
* @since 1.0.0
* @package LiteSpeed
*/
namespace LiteSpeed;
defined('WPINC') || exit();
/**
* Class Conf
*
* Maintains all LiteSpeed plugin configuration, including CRUD for single-site
* and multisite options, upgrade flows, and side effects like purging/cron.
*/
class Conf extends Base {
const TYPE_SET = 'set';
/** Settings feeding the installation's shared object-cache and Guest files. */
private const SHARED_OPTIONS = [
self::O_OBJECT,
self::O_OBJECT_KIND,
self::O_OBJECT_HOST,
self::O_OBJECT_PORT,
self::O_OBJECT_USER,
self::O_OBJECT_PSWD,
self::O_OBJECT_DB_ID,
self::O_OBJECT_PERSISTENT,
self::O_OBJECT_ADMIN,
self::O_OBJECT_GLOBAL_GROUPS,
self::O_OBJECT_NON_PERSISTENT_GROUPS,
self::O_GUEST,
self::O_CACHE_LOGIN_COOKIE,
self::O_UTIL_NO_HTTPS_VARY,
];
/**
* Set while a network-wide activation runs, since WordPress records the plugin as network-active only after the hook.
*
* @var bool
*/
public static $_network_activating = false;
/**
* IDs that were updated during a save cycle.
*
* @var array<string|int,mixed>
*/
private $_updated_ids = [];
/**
* Request-local forced values, retained across configuration reloads.
*
* @var array
*/
private $_forced_options = [];
/**
* Whether current blog is the network primary site.
*
* @var bool
*/
private $_is_primary = false;
/**
* Specify init logic to avoid infinite loop when calling conf.cls instance
*
* @since 3.0
* @access public
* @return void
*/
public function init() {
// 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
$this->_conf_db_init();
/**
* Detect if has quic.cloud set
*
* @since 2.9.7
*/
if ( $this->conf( self::O_CDN_QUIC ) ) {
if ( ! defined( 'LITESPEED_ALLOWED' ) ) {
define( 'LITESPEED_ALLOWED', true );
}
}
add_action( 'litespeed_conf_append', [ $this, 'option_append' ], 10, 2 );
add_action( 'litespeed_conf_force', [ $this, 'force_option' ], 10, 2 );
$this->define_cache();
}
/**
* Init conf related data
*
* @since 3.0
* @access private
* @return void
*/
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()`
*/
$this->load_options();
// Check if debug is on
// Init debug as early as possible
if ( $this->conf( Base::O_DEBUG ) ) {
$this->cls( 'Debug2' )->init();
}
$ver = $this->conf( self::_VER );
$upgrade_completed = false;
/**
* Version is less than v3.0, or, is a new installation
*/
$ver_check_tag = 'new';
if ( $ver ) {
if ( ! defined( 'LSCWP_CUR_V' ) ) {
define( 'LSCWP_CUR_V', $ver );
}
/**
* Upgrade conf
*/
if ( Core::VER !== $ver ) {
// Plugin version will be set inside
// Site plugin upgrade & version change will do in load_site_conf
if ( did_action( 'wp_loaded' ) ) {
$ver_check_tag = Data::cls()->conf_upgrade( $ver );
$upgrade_completed = 'upgrade' === $ver_check_tag;
} else {
// Third-party vary hooks must exist before upgrading shared rewrite rules.
add_action( 'wp_loaded', function () {
$this->_conf_db_init();
}, 3 );
}
}
}
/**
* Sync latest new options
*/
if ( ! $ver || ( Core::VER !== $ver && $upgrade_completed ) ) {
// Load default values
$this->load_default_vals();
if ( ! $ver ) {
// New install
$this->set_conf( self::$_default_options );
$ver_check_tag .= ' activate' . ( defined( 'LSCWP_REF' ) ? '_' . constant( 'LSCWP_REF' ) : '' );
}
// 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 );
if ( $ver_check_tag ) {
Cloud::version_check( $ver_check_tag );
}
}
/**
* Network sites only
*
* Override conf if is network subsites and chose `Use Primary Config`
*/
$this->_try_load_site_options();
$this->load_shared_options();
// Check if debug is on
// Init debug as early as possible
if ( $this->conf( Base::O_DEBUG ) ) {
$this->cls( 'Debug2' )->init();
}
// Mark as conf loaded
if ( ! defined( 'LITESPEED_CONF_LOADED' ) ) {
define( 'LITESPEED_CONF_LOADED', true );
}
if ( ! $ver || ( Core::VER !== $ver && $upgrade_completed ) ) {
// Only trigger once in upgrade progress, don't run always
if ( ! did_action( 'wp_loaded' ) ) {
// New installations must finish WordPress initialization before detecting integrations and writing files.
add_action( 'wp_loaded', [ $this, 'update_confs' ], 3, 0 );
} else {
$this->update_confs();
}
}
}
/**
* Load all latest options from DB
*
* @since 3.0
* @access public
*
* @param int|null $blog_id Blog ID to load from. Null for current.
* @param bool $dry_run Return options instead of setting them.
* @return array<string,mixed>|void
*/
public function load_options( $blog_id = null, $dry_run = false ) {
$options = [];
foreach ( self::$_default_options as $k => $v ) {
if ( null !== $blog_id ) {
$options[ $k ] = self::get_blog_option( $blog_id, $k, $v );
} else {
$options[ $k ] = self::get_option( $k, $v );
}
// Correct value type.
$options[ $k ] = $this->type_casting( $options[ $k ], $k );
}
if ( $dry_run ) {
return $options;
}
// Bypass site special settings
if ( null !== $blog_id ) {
// 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 );
} else {
$this->set_conf( $options );
foreach ( $this->_forced_options as $key => $value ) {
$this->set_conf( $key, $value );
}
}
// Append const options
if ( defined( 'LITESPEED_CONF' ) && LITESPEED_CONF ) {
foreach ( self::$_default_options as $k => $v ) {
$const = Base::conf_const( $k );
if ( defined( $const ) ) {
$this->set_const_conf( $k, $this->type_casting( constant( $const ), $k ) );
}
}
}
}
/**
* Whether shared runtime settings follow the primary network, which applies only while the plugin is (being) network-activated.
*
* A per-site or MU-plugin install keeps the v7.9.1 per-site behaviour. Not cached because it changes inside the activation request.
*
* @since 7.9.2
* @return bool
*/
public static function shares_network_settings() {
if ( ! is_multisite() ) {
return false;
}
if ( self::$_network_activating ) {
return true;
}
if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
require_once ABSPATH . '/wp-admin/includes/plugin.php';
}
return is_plugin_active_for_network( Core::PLUGIN_FILE );
}
/**
* Check authority to change settings that feed shared runtime files and cookies.
*
* @param string $id Option ID.
* @param bool $network Whether the caller is saving network settings.
* @return bool
*/
public function can_update_shared( $id, $network = false ) {
if ( ! self::shares_network_settings() || ! in_array( $id, self::SHARED_OPTIONS, true ) ) {
return true;
}
$owner = get_main_network_id();
if ( $network ) {
$owns_settings = get_current_network_id() === $owner;
} else {
// Existing network settings remain authoritative; their site-level copies must not pretend to save.
$owns_settings = get_current_blog_id() === get_main_site_id( $owner ) && null === get_network_option( $owner, self::name( $id ), null );
}
return $owns_settings && ( defined( 'LITESPEED_CLI' ) || current_user_can( 'manage_network_options' ) );
}
/**
* Refresh the installation-primary settings without overriding site-specific hashes.
*
* @return void
*/
public function load_shared_options() {
if ( ! self::shares_network_settings() ) {
return;
}
$owner = get_main_network_id();
$primary = get_main_site_id( $owner );
$switched = get_current_blog_id() !== $primary;
if ( $switched ) {
switch_to_blog( $primary );
}
try {
$values = [];
foreach ( self::SHARED_OPTIONS as $id ) {
$value = \get_option( self::name( $id ), self::$_default_options[ $id ] );
$value = get_network_option( $owner, self::name( $id ), $value );
if ( is_array( self::$_default_options[ $id ] ) ) {
$value = self::_maybe_decode( $value );
}
$values[ $id ] = $this->type_casting( $value, $id );
}
} finally {
if ( $switched ) {
restore_current_blog();
}
}
foreach ( $values as $id => $value ) {
// Retain Root's existing primary-override precedence; a different per-site force cannot change shared files.
$this->set_primary_conf( $id, $value );
$this->set_network_conf( $id, $value );
}
}
/**
* Read a network setting from the primary network, for installation-wide files written while network settings are shared.
*
* @since 7.9.2
* @param string $id Network option ID.
* @return mixed
*/
public function owner_network_conf( $id ) {
$value = get_network_option( get_main_network_id(), self::name( $id ), self::$_default_site_options[ $id ] );
return $this->type_casting( $value, $id, true );
}
/**
* Read the primary blog's HASH on multisite, adding it only when absent.
*
* @param int $blog_id Owning blog ID.
* @return string Empty when the stored value is empty or invalid.
*/
public static function ensure_blog_hash( $blog_id ) {
$switched = get_current_blog_id() !== (int) $blog_id;
if ( $switched ) {
switch_to_blog( $blog_id );
}
try {
$hash = self::get_option( self::HASH, false );
if ( false === $hash ) {
self::add_option( self::HASH, Str::rrand( 32 ) );
$hash = self::get_option( self::HASH, '' );
}
} finally {
if ( $switched ) {
restore_current_blog();
}
}
return is_string( $hash ) ? $hash : '';
}
/**
* For multisite installations, the single site options need to be updated with the network wide options.
*
* @since 1.0.13
* @access private
* @return void
*/
private function _try_load_site_options() {
if ( ! $this->_if_need_site_options() ) {
return;
}
$this->_conf_site_db_init();
$this->_is_primary = BLOG_ID_CURRENT_SITE === get_current_blog_id();
// 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 ) ) {
continue;
}
// $this->_options[ $k ] = $this->_network_options[ $k ];
// Special handler to `Enable Cache` option if the value is set to OFF
if ( self::O_CACHE === $k ) {
if ( $this->_is_primary ) {
if ( $this->conf( $k ) !== $this->network_conf( $k ) ) {
if ( self::VAL_ON2 !== $this->conf( $k ) ) {
continue;
}
}
} elseif ( $this->network_conf( self::NETWORK_O_USE_PRIMARY ) ) {
if ( $this->has_primary_conf( $k ) && self::VAL_ON2 !== $this->primary_conf( $k ) ) {
// This case will use primary_options override always
continue;
}
} elseif ( self::VAL_ON2 !== $this->conf( $k ) ) {
continue;
}
}
// 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
*
* @since 3.0
* @access private
* @return bool
*/
private function _if_need_site_options() {
if ( ! is_multisite() ) {
return false;
}
// If is not activated on network, it will not have site options
if ( ! self::shares_network_settings() ) {
if ( self::VAL_ON2 === (int) $this->conf( self::O_CACHE ) ) {
// Default to cache on
$this->set_conf( self::_CACHE, true );
}
return false;
}
return true;
}
/**
* Init site conf and upgrade if necessary
*
* @since 3.0
* @access private
* @return void
*/
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 || Core::VER !== $ver ) {
if ( ! is_admin() && ! defined( 'LITESPEED_CLI' ) ) {
$this->set_network_conf( $this->load_default_site_vals() );
return;
}
}
/**
* Upgrade conf
*/
$upgrade_completed = false;
if ( $ver && Core::VER !== $ver ) {
// Site plugin version will change inside
if ( did_action( 'wp_loaded' ) ) {
$upgrade_completed = Data::cls()->conf_site_upgrade( $ver );
} else {
add_action( 'wp_loaded', function () {
$this->_try_load_site_options();
$this->load_shared_options();
}, 3 );
}
}
/**
* Is a new installation
*/
if ( ! $ver || ( Core::VER !== $ver && $upgrade_completed ) ) {
// Load default values
$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.
*
* @since 1.0.2
* @access public
* @return null|void
*/
public function load_site_options() {
if ( ! is_multisite() ) {
return null;
}
// Load all 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
*
* @since 3.0
* @access public
*
* @param string $name Option name.
* @param mixed $default_val Default value.
* @return void
*/
public function option_append( $name, $default_val ) {
self::$_default_options[ $name ] = $default_val;
$this->set_conf( $name, self::get_option( $name, $default_val ) );
$this->set_conf( $name, $this->type_casting( $this->conf( $name ), $name ) );
}
/**
* Force an option to a certain value
*
* @since 2.6
* @access public
*
* @param string $k Option key.
* @param mixed $v Option value.
* @return void
*/
public function force_option( $k, $v ) {
if ( ! $this->has_conf( $k ) ) {
return;
}
$v = $this->type_casting( $v, $k );
$this->_forced_options[ $k ] = $v;
if ( $this->conf( $k ) === $v ) {
return;
}
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
Debug2::debug( '[Conf] ** ' . $k . ' forced from ' . var_export( $this->conf( $k ), true ) . ' to ' . var_export( $v, true ) );
$this->set_conf( $k, $v );
}
/**
* Define `_CACHE` const in options ( for both single and network )
*
* @since 3.0
* @access public
* @return void
*/
public function define_cache() {
// Init global const cache on setting
$this->set_conf( self::_CACHE, false );
if ( self::VAL_ON === (int) $this->conf( self::O_CACHE ) || $this->conf( self::O_CDN_QUIC ) ) {
$this->set_conf( self::_CACHE, true );
}
// Check network
if ( ! $this->_if_need_site_options() ) {
// Set cache on
$this->_define_cache_on();
return;
}
// If use network setting
if ( self::VAL_ON2 === (int) $this->conf( self::O_CACHE ) && $this->network_conf( self::O_CACHE ) ) {
$this->set_conf( self::_CACHE, true );
}
$this->_define_cache_on();
}
/**
* Define `LITESPEED_ON`
*
* @since 2.1
* @access private
* @return void
*/
private function _define_cache_on() {
if ( ! $this->conf( self::_CACHE ) ) {
return;
}
if ( defined( 'LITESPEED_ALLOWED' ) && ! defined( 'LITESPEED_ON' ) ) {
define( 'LITESPEED_ON', true );
}
}
/**
* Save option
*
* @since 3.0
* @access public
*
* @param array<string,mixed> $the_matrix Option-value map.
* @return void
*/
public function update_confs( $the_matrix = [] ) {
if ( $the_matrix ) {
foreach ( $the_matrix as $id => $val ) {
$this->update( $id, $val );
}
}
if ( $this->_updated_ids ) {
foreach ( $this->_updated_ids as $id ) {
// 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
$tag = $this->_conf_purge_tag( $id );
if ( $tag ) {
Purge::add( $tag );
}
// Update cron
if ( $this->_conf_cron( $id ) ) {
$this->cls( 'Task' )->try_clean( $id );
}
// Reset crawler bypassed list when any of the options WebP replace, guest mode, or cache mobile got changed
if ( self::O_IMG_OPTM_WEBP === $id || self::O_GUEST === $id || self::O_CACHE_MOBILE === $id ) {
$this->cls( 'Crawler' )->clear_disabled_list();
}
}
// CCSS queue keys depend on these options, old entries can not be used anymore and would still spend quota
if ( array_intersect( [ self::O_OPTM_CCSS_PER_URL, self::O_OPTM_CCSS_SEP_POSTTYPE, self::O_OPTM_CCSS_SEP_URI ], $this->_updated_ids ) ) {
$this->cls( 'CSS' )->clear_q( 'ccss', true );
}
}
do_action( 'litespeed_update_confs', $the_matrix );
// Update related tables
$this->cls( 'Data' )->correct_tb_existence();
// Update related files
$this->cls( 'Activation' )->update_files();
/**
* CDN related actions - Cloudflare
*/
$this->cls( 'CDN\Cloudflare' )->try_refresh_zone();
// If Server IP changed, must test echo
if ( in_array( self::O_SERVER_IP, $this->_updated_ids, true ) ) {
$this->cls( 'Cloud' )->init_qc_cli();
}
// CDN related actions - QUIC.cloud
$this->cls( 'CDN\Quic' )->try_sync_conf();
}
/**
* Save option
*
* Note: this is direct save, won't trigger corresponding file update or data sync. To save settings normally, always use `Conf->update_confs()`
*
* @since 3.0
* @access public
*
* @param string $id Option ID.
* @param mixed $val Option value.
* @return void
*/
public function update( $id, $val ) {
if ( ! $this->can_update_shared( $id ) ) {
if ( $this->type_casting( $val, $id ) === $this->conf( $id ) ) {
return;
}
Admin_Display::error( __( 'Shared settings must be changed by a network administrator in the primary site or network settings.', 'litespeed-cache' ) );
return;
}
// Bypassed this bcos $this->_options could be changed by force_option()
// if ( $this->_options[ $id ] === $val ) {
// return;
// }
if ( in_array( $id, [ self::HASH, self::_VER ], true ) ) {
return;
}
if ( self::O_SERVER_IP === $id ) {
if ( $val && ! Utility::valid_ipv4( $val ) ) {
$msg = sprintf( __( 'Saving option failed. IPv4 only for %s.', 'litespeed-cache' ), Lang::title( Base::O_SERVER_IP ) );
Admin_Display::error( $msg );
return;
}
}
if ( ! array_key_exists( $id, self::$_default_options ) ) {
if ( defined( 'LSCWP_LOG' ) ) {
Debug2::debug( '[Conf] Invalid option ID ' . $id );
}
return;
}
if ( $val && $this->_conf_pswd( $id ) && ! preg_match( '/[^\*]/', (string) $val ) ) {
return;
}
// Special handler for CDN Original URLs
if ( self::O_CDN_ORI === $id && ! $val ) {
$site_url = site_url( '/' );
$parsed = wp_parse_url( $site_url );
if ( !empty( $parsed['scheme'] ) ) {
$site_url = str_replace( $parsed['scheme'] . ':', '', $site_url );
}
$val = $site_url;
}
// Validate type
$val = $this->type_casting( $val, $id );
if ( self::O_CDN_MAPPING === $id ) {
if ( is_multisite() && ! defined( 'LITESPEED_CLI' ) && $val !== $this->type_casting( $this->conf( $id ), $id ) && ! current_user_can( 'unfiltered_html' ) && ! current_user_can( 'manage_network_options' ) ) {
Admin_Display::error( __( 'Changing CDN mappings requires permission to manage network options or unfiltered HTML.', 'litespeed-cache' ) );
return;
}
foreach ( $val as $row ) {
if ( ! is_array( $row ) || ( ! empty( $row[ self::CDN_MAPPING_URL ] ) && ! CDN::valid_mapping_url( $row[ self::CDN_MAPPING_URL ] ) ) ) {
Admin_Display::error( __( 'Invalid CDN URL. CDN mappings were not saved.', 'litespeed-cache' ) );
return;
}
}
}
if ( self::O_CRAWLER_ROLES === $id ) {
foreach ( $val as $k => $user_id ) {
if ( user_can( $user_id, 'edit_posts' ) ) {
Admin_Display::error( __( 'Crawler role simulation cannot use a user who can edit posts.', 'litespeed-cache' ) );
unset( $val[ $k ] );
}
}
$val = array_values( $val );
}
// Save data
if ( ! self::update_option( $id, $val ) ) {
Admin_Display::error( __( 'Could not save the setting. The previous value remains in use.', 'litespeed-cache' ) );
return;
}
// Handle purge if setting changed
if ( $this->conf( $id ) !== $val ) {
$this->_updated_ids[] = $id;
// Check if need to fire a purge or not (Here has to stay inside `update()` bcos need comparing old value)
if ( $this->_conf_purge( $id ) ) {
$old = (array) $this->conf( $id );
$new = (array) $val;
$diff = array_merge( array_diff( $new, $old ), array_diff( $old, $new ) );
// If has difference
foreach ( $diff as $v ) {
$v = ltrim( (string) $v, '^' );
$v = rtrim( (string) $v, '$' );
$this->cls( 'Purge' )->purge_url( $v );
}
}
}
// Update in-memory data
$this->set_conf( $id, $val );
}
/**
* Save network option
*
* @since 3.0
* @access public
*
* @param string $id Option ID.
* @param mixed $val Option value.
* @return void
*/
public function network_update( $id, $val ) {
if ( ! $this->can_update_shared( $id, true ) ) {
if ( $this->type_casting( $val, $id ) === $this->network_conf( $id ) ) {
return;
}
Admin_Display::error( __( 'Shared settings must be changed by an administrator of the primary network.', 'litespeed-cache' ) );
return;
}
if ( ! array_key_exists( $id, self::$_default_site_options ) ) {
if ( defined( 'LSCWP_LOG' ) ) {
Debug2::debug( '[Conf] Invalid network option ID ' . $id );
}
return;
}
if ( $val && $this->_conf_pswd( $id ) && ! preg_match( '/[^\*]/', (string) $val ) ) {
return;
}
// Validate type
if ( is_bool( self::$_default_site_options[ $id ] ) ) {
$max = $this->_conf_multi_switch( $id );
if ( $max && $val > 1 ) {
$val %= ( $max + 1 );
} else {
$val = (bool) $val;
}
} elseif ( is_array( self::$_default_site_options[ $id ] ) ) {
// from textarea input
if ( ! is_array( $val ) ) {
$val = Utility::sanitize_lines( $val, $this->_conf_filter( $id ) );
}
} elseif ( ! is_string( self::$_default_site_options[ $id ] ) ) {
$val = (int) $val;
} else {
// Check if the string has a limit set
$val = $this->_conf_string_val( $id, $val );
}
// Save data
if ( ! self::update_site_option( $id, $val ) ) {
Admin_Display::error( __( 'Could not save the network setting. The previous value remains in use.', 'litespeed-cache' ) );
return;
}
// Handle purge if setting changed
if ( $this->network_conf( $id ) !== $val ) {
// Check if need to do a purge all or not
if ( $this->_conf_purge_all( $id ) ) {
Purge::purge_all( '[Conf] Network conf changed [id] ' . $id );
}
// Update in-memory data
$this->set_network_conf( $id, $val );
}
// No need to update cron here, Cron will register in each init
if ( $this->has_conf( $id ) ) {
$this->set_conf( $id, $val );
}
}
/**
* Check if one user role is in exclude optimization group settings
*
* @since 1.6
* @access public
*
* @param string|null $role The user role.
* @return string|false The set value if already set, otherwise false.
*/
public function in_optm_exc_roles( $role = null ) {
// Get user role
if ( null === $role ) {
$role = Router::get_role();
}
if ( ! $role ) {
return false;
}
$roles = explode( ',', $role );
$found = array_intersect( $roles, $this->conf( self::O_OPTM_EXC_ROLES ) );
return $found ? implode( ',', $found ) : false;
}
/**
* Set one config value directly
*
* @since 2.9
* @access private
* @return void
*/
private function _set_conf() {
/**
* NOTE: For URL Query String setting,
* 1. If append lines to an array setting e.g. `cache-force_uri`, use `set[cache-force_uri][]=the_url`.
* 2. If replace the array setting with one line, use `set[cache-force_uri]=the_url`.
* 3. If replace the array setting with multi lines value, use 2 then 1.
*/
// phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput
$raw = !empty( $_GET[ self::TYPE_SET ] ) ? wp_unslash( $_GET[ self::TYPE_SET ] ) : false;
if ( !$raw || ! is_array( $raw ) ) {
return;
}
// Sanitize the incoming matrix.
$the_matrix = [];
foreach ( $raw as $id => $v ) {
if ( ! $this->has_conf( $id ) ) {
continue;
}
// Append new item to array type settings
if ( is_array( $v ) && is_array( $this->conf( $id ) ) ) {
$v = array_merge( $this->conf( $id ), $v );
Debug2::debug( '[Conf] Appended to setting [' . $id . ']' );
} else {
Debug2::debug( '[Conf] Set setting [' . $id . ']' );
}
$the_matrix[ $id ] = $v;
}
if ( !$the_matrix ) {
return;
}
$this->update_confs( $the_matrix );
$msg = __( 'Changed setting successfully.', 'litespeed-cache' );
Admin_Display::success( $msg );
// Redirect if changed frontend URL
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$redirect = ! empty( $_GET['redirect'] ) ? sanitize_text_field( wp_unslash( $_GET['redirect'] ) ) : '';
if ( $redirect ) {
wp_safe_redirect( $redirect );
exit;
}
}
/**
* Handle all request actions from main cls
*
* @since 2.9
* @access public
* @return void
*/
public function handler() {
$type = Router::verify_type();
switch ( $type ) {
case self::TYPE_SET:
$this->_set_conf();
break;
default:
break;
}
Admin::redirect();
}
}