* LiteSpeed Cache option Interface CLI.
defined( 'WPINC' ) || exit();
use LiteSpeed\Admin_Settings;
* LiteSpeed Cache option Interface
class Option extends Base {
* Set an individual LiteSpeed Cache option.
* : The option key to update.
* : The new value to set the option to.
* # Set to not cache the login page
* $ wp litespeed-option set cache-priv false
* $ wp litespeed-option set 'cdn-mapping[url][0]' https://cdn.EXAMPLE.com
* $ wp litespeed-option set media-lqip_exc $'line1\nline2'
* @param array $args Positional arguments (key, newvalue).
* @param array $assoc_args Associative arguments.
public function set( $args, $assoc_args ) {
// Note: If the value is multiple dimensions like cdn-mapping, need to specially handle it both here and in `const.default.json`
// For CDN/Crawler multi dimension settings, if all children are empty in one line, will delete that line. To delete one line, just set all to empty.
// E.g. to delete cdn-mapping[0], need to run below:
// `set cdn-mapping[url][0] ''`
// `set cdn-mapping[inc_img][0] ''`
// `set cdn-mapping[inc_css][0] ''`
// `set cdn-mapping[inc_js][0] ''`
// `set cdn-mapping[filetype][0] ''`
// For CDN mapping, allow:
// `set 'cdn-mapping[url][0]' https://the1st_cdn_url`
// `set 'cdn-mapping[inc_img][0]' true`
// `set 'cdn-mapping[inc_img][0]' 1`
// `set 'crawler-cookies[name][0]' my_currency`
// `set 'crawler-cookies[vals][0]' "USD\nTWD"`
// For multi lines setting:
// `set media-lqip_exc $'img1.jpg\nimg2.jpg'`
Admin_Settings::ENROLL => array( $key ),
if ( false !== strpos( $key, '[' ) ) {
parse_str( $key . '=' . $val, $key2 );
$raw_data = array_merge( $raw_data, $key2 );
$raw_data[ $key ] = $val;
$this->cls( 'Admin_Settings' )->save( $raw_data );
$this->get( $args, $assoc_args );
* Get all plugin options.
* : Output format (e.g., json).
* $ wp litespeed-option all
* $ wp litespeed-option all --json
* $ wp litespeed-option all --format=json
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
public function all( $args, $assoc_args ) {
$options = $this->get_options();
if ( ! empty( $assoc_args['format'] ) ) {
WP_CLI::print_value( $options, $assoc_args );
$buf = WP_CLI::colorize( '%CThe list of options:%n' );
foreach ( $options as $k => $v ) {
if ( self::O_CDN_MAPPING === $k || self::O_CRAWLER_COOKIES === $k ) {
foreach ( $v as $k2 => $v2 ) {
foreach ( $v2 as $k3 => $v3 ) {
// $k3 is 'url/inc_img/name/vals'
foreach ( $v3 as $k4 => $v4 ) {
'key' => 0 === $k4 ? "{$k}[$k3][$k2]" : '',
'key' => "{$k}[$k3][$k2]",
} elseif ( is_array( $v ) && $v ) {
foreach ( $v as $k2 => $v2 ) {
'key' => 0 === $k2 ? $k : '',
if ( array_key_exists( $k, self::$_default_options ) && is_bool( self::$_default_options[ $k ] ) && ! $v ) {
if ( '' === $v || array() === $v ) {
WP_CLI\Utils\format_items( 'table', $option_out, array( 'key', 'value' ) );
* Get a specific plugin option.
* : The option ID to retrieve (e.g., cache-priv, cdn-mapping[url][0]).
* $ wp litespeed-option get cache-priv
* $ wp litespeed-option get 'cdn-mapping[url][0]'
* @param array $args Positional arguments (id).
* @param array $assoc_args Associative arguments.
public function get( $args, $assoc_args ) {
if ( false !== strpos( $id, '[' ) ) {
Utility::compatibility();
$id = array_key_first( $id2 );
$child = array_key_first( $id2[ $id ] ); // is `url`
WP_CLI::error( 'Wrong child key' );
$numeric = array_key_first( $id2[ $id ][ $child ] ); // `0`
if ( null === $numeric ) {
WP_CLI::error( 'Wrong 2nd level numeric key' );
if ( ! isset( self::$_default_options[ $id ] ) ) {
WP_CLI::error( 'ID not exist [id] ' . $id );
$default_v = self::$_default_options[ $id ];
// For CDN_mapping and crawler_cookies
// Examples of option name:
// crawler-cookies[name][1]
if ( self::O_CDN_MAPPING === $id ) {
if ( ! in_array( $child, array( self::CDN_MAPPING_URL, self::CDN_MAPPING_INC_IMG, self::CDN_MAPPING_INC_CSS, self::CDN_MAPPING_INC_JS, self::CDN_MAPPING_FILETYPE ), true ) ) {
WP_CLI::error( 'Wrong child key' );
if ( self::O_CRAWLER_COOKIES === $id ) {
if ( ! in_array( $child, array( self::CRWL_COOKIE_NAME, self::CRWL_COOKIE_VALS ), true ) ) {
WP_CLI::error( 'Wrong child key' );
if ( self::O_CDN_MAPPING === $id || self::O_CRAWLER_COOKIES === $id ) {
if ( ! empty( $v[ $numeric ][ $child ] ) ) {
$v = $v[ $numeric ][ $child ];
} elseif ( self::O_CDN_MAPPING === $id ) {
if ( in_array( $child, array( self::CDN_MAPPING_INC_IMG, self::CDN_MAPPING_INC_CSS, self::CDN_MAPPING_INC_JS ), true ) ) {
$v = implode( PHP_EOL, $v );
if ( ! $v && self::O_CDN_MAPPING !== $id && self::O_CRAWLER_COOKIES !== $id ) {
// empty array for CDN/crawler has been handled
if ( is_bool( $default_v ) ) {
} elseif ( ! is_array( $default_v ) ) {
* Export plugin options to a file.
* : The default path used is CURRENTDIR/lscache_wp_options_DATE-TIME.txt.
* To select a different file, use this option.
* # Export options to a file.
* $ wp litespeed-option export
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
public function export( $args, $assoc_args ) {
if ( isset( $assoc_args['filename'] ) ) {
$file = $assoc_args['filename'];
$file = getcwd() . '/litespeed_options_' . gmdate( 'd_m_Y-His' ) . '.data';
if ( ! $wp_filesystem ) {
require_once ABSPATH . '/wp-admin/includes/file.php';
if ( ! $wp_filesystem->is_writable( dirname( $file ) ) ) {
WP_CLI::error( 'Directory not writable.' );
$data = $this->cls( 'Import' )->export( true );
if ( false === $wp_filesystem->put_contents( $file, $data ) ) {
WP_CLI::error( 'Failed to create file.' );
WP_CLI::success( 'Created file ' . $file );
* Import plugin options from a file.
* The file must be formatted as such:
* option_key=option_value
* A semicolon at the beginning of the line indicates a comment and will be skipped.
* : The file to import options from.
* # Import options from CURRENTDIR/options.txt
* $ wp litespeed-option import options.txt
* @param array $args Positional arguments (file).
* @param array $assoc_args Associative arguments.
public function import( $args, $assoc_args ) {
if ( ! $wp_filesystem ) {
require_once ABSPATH . '/wp-admin/includes/file.php';
if ( ! $wp_filesystem->exists( $file ) || ! $wp_filesystem->is_readable( $file ) ) {
WP_CLI::error( 'File does not exist or is not readable.' );
$res = $this->cls( 'Import' )->import( $file );
WP_CLI::error( 'Failed to parse serialized data from file.' );
WP_CLI::success( 'Options imported. [File] ' . $file );
* Import plugin options from a remote file.
* The file must be formatted as such:
* option_key=option_value
* A semicolon at the beginning of the line indicates a comment and will be skipped.
* : The URL to import options from.
* # Import options from https://domain.com/options.txt
* $ wp litespeed-option import_remote https://domain.com/options.txt
* @param array $args Positional arguments (url).
public function import_remote( $args ) {
$tmp_file = download_url( $file );
if ( is_wp_error( $tmp_file ) ) {
WP_CLI::error( 'Failed to download file.' );
$res = $this->cls( 'Import' )->import( $tmp_file );
WP_CLI::error( 'Failed to parse serialized data from file.' );
WP_CLI::success( 'Options imported. [File] ' . $file );
* Reset all options to default.
* $ wp litespeed-option reset
public function reset() {
$this->cls( 'Import' )->reset();