PluginProbe ʕ •ᴥ•ʔ
LiteSpeed Cache / 7.8.1
LiteSpeed Cache v7.8.1
trunk 1.0.15 1.9.1.1 2.9.9.2 3.6.4 4.6 5.7.0.1 6.5.4 7.0.0.1 7.0.1 7.1 7.2 7.3 7.3.0.1 7.4 7.5 7.5.0.1 7.6 7.6.1 7.6.2 7.7 7.8 7.8.0.1 7.8.1
litespeed-cache / cli / option.cls.php
litespeed-cache / cli Last commit date
crawler.cls.php 2 months ago database.cls.php 2 months ago debug.cls.php 2 months ago image.cls.php 2 months ago online.cls.php 2 months ago option.cls.php 2 months ago presets.cls.php 2 months ago purge.cls.php 2 months ago
option.cls.php
420 lines
1 <?php
2 /**
3 * LiteSpeed Cache option Interface CLI.
4 *
5 * @package LiteSpeed\CLI
6 */
7
8 namespace LiteSpeed\CLI;
9
10 defined( 'WPINC' ) || exit();
11
12 use LiteSpeed\Base;
13 use LiteSpeed\Admin_Settings;
14 use LiteSpeed\Utility;
15 use WP_CLI;
16 use WP_Filesystem;
17
18 /**
19 * LiteSpeed Cache option Interface
20 */
21 class Option extends Base {
22
23 /**
24 * Set an individual LiteSpeed Cache option.
25 *
26 * ## OPTIONS
27 *
28 * <key>
29 * : The option key to update.
30 *
31 * <newvalue>
32 * : The new value to set the option to.
33 *
34 * ## EXAMPLES
35 *
36 * # Set to not cache the login page
37 * $ wp litespeed-option set cache-priv false
38 * $ wp litespeed-option set 'cdn-mapping[url][0]' https://cdn.EXAMPLE.com
39 * $ wp litespeed-option set media-lqip_exc $'line1\nline2'
40 *
41 * @param array $args Positional arguments (key, newvalue).
42 * @param array $assoc_args Associative arguments.
43 */
44 public function set( $args, $assoc_args ) {
45 // Note: If the value is multiple dimensions like cdn-mapping, need to specially handle it both here and in `const.default.json`
46 // 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.
47 // E.g. to delete cdn-mapping[0], need to run below:
48 // `set cdn-mapping[url][0] ''`
49 // `set cdn-mapping[inc_img][0] ''`
50 // `set cdn-mapping[inc_css][0] ''`
51 // `set cdn-mapping[inc_js][0] ''`
52 // `set cdn-mapping[filetype][0] ''`
53
54 $key = $args[0];
55 $val = $args[1];
56
57 // For CDN mapping, allow:
58 // `set 'cdn-mapping[url][0]' https://the1st_cdn_url`
59 // `set 'cdn-mapping[inc_img][0]' true`
60 // `set 'cdn-mapping[inc_img][0]' 1`
61 //
62 // For Crawler cookies:
63 // `set 'crawler-cookies[name][0]' my_currency`
64 // `set 'crawler-cookies[vals][0]' "USD\nTWD"`
65 //
66 // For multi lines setting:
67 // `set media-lqip_exc $'img1.jpg\nimg2.jpg'`
68
69 // Build raw data
70 $raw_data = array(
71 Admin_Settings::ENROLL => array( $key ),
72 );
73
74 // Contains child set
75 if ( false !== strpos( $key, '[' ) ) {
76 parse_str( $key . '=' . $val, $key2 );
77 $raw_data = array_merge( $raw_data, $key2 );
78 } else {
79 $raw_data[ $key ] = $val;
80 }
81
82 $this->cls( 'Admin_Settings' )->save( $raw_data );
83 WP_CLI::line( "$key:" );
84 $this->get( $args, $assoc_args );
85 }
86
87 /**
88 * Get all plugin options.
89 *
90 * ## OPTIONS
91 *
92 * [--format=<format>]
93 * : Output format (e.g., json).
94 *
95 * ## EXAMPLES
96 *
97 * # Get all options
98 * $ wp litespeed-option all
99 * $ wp litespeed-option all --json
100 * $ wp litespeed-option all --format=json
101 *
102 * @param array $args Positional arguments.
103 * @param array $assoc_args Associative arguments.
104 */
105 public function all( $args, $assoc_args ) {
106 $options = $this->get_options();
107
108 if ( ! empty( $assoc_args['format'] ) ) {
109 WP_CLI::print_value( $options, $assoc_args );
110 return;
111 }
112
113 $option_out = array();
114
115 $buf = WP_CLI::colorize( '%CThe list of options:%n' );
116 WP_CLI::line( $buf );
117
118 foreach ( $options as $k => $v ) {
119 if ( self::O_CDN_MAPPING === $k || self::O_CRAWLER_COOKIES === $k ) {
120 foreach ( $v as $k2 => $v2 ) {
121 // $k2 is numeric
122 if ( is_array( $v2 ) ) {
123 foreach ( $v2 as $k3 => $v3 ) {
124 // $k3 is 'url/inc_img/name/vals'
125 if ( is_array( $v3 ) ) {
126 $option_out[] = array(
127 'key' => '',
128 'value' => '',
129 );
130 foreach ( $v3 as $k4 => $v4 ) {
131 $option_out[] = array(
132 'key' => 0 === $k4 ? "{$k}[$k3][$k2]" : '',
133 'value' => $v4,
134 );
135 }
136 $option_out[] = array(
137 'key' => '',
138 'value' => '',
139 );
140 } else {
141 $option_out[] = array(
142 'key' => "{$k}[$k3][$k2]",
143 'value' => $v3,
144 );
145 }
146 }
147 }
148 }
149 continue;
150 } elseif ( is_array( $v ) && $v ) {
151 $option_out[] = array(
152 'key' => '',
153 'value' => '',
154 );
155 foreach ( $v as $k2 => $v2 ) {
156 $option_out[] = array(
157 'key' => 0 === $k2 ? $k : '',
158 'value' => $v2,
159 );
160 }
161 $option_out[] = array(
162 'key' => '',
163 'value' => '',
164 );
165 continue;
166 }
167
168 if ( array_key_exists( $k, self::$_default_options ) && is_bool( self::$_default_options[ $k ] ) && ! $v ) {
169 $v = 0;
170 }
171
172 if ( '' === $v || array() === $v ) {
173 $v = "''";
174 }
175
176 $option_out[] = array(
177 'key' => $k,
178 'value' => $v,
179 );
180 }
181
182 WP_CLI\Utils\format_items( 'table', $option_out, array( 'key', 'value' ) );
183 }
184
185 /**
186 * Get a specific plugin option.
187 *
188 * ## OPTIONS
189 *
190 * <id>
191 * : The option ID to retrieve (e.g., cache-priv, cdn-mapping[url][0]).
192 *
193 * ## EXAMPLES
194 *
195 * # Get one option
196 * $ wp litespeed-option get cache-priv
197 * $ wp litespeed-option get 'cdn-mapping[url][0]'
198 *
199 * @param array $args Positional arguments (id).
200 * @param array $assoc_args Associative arguments.
201 */
202 public function get( $args, $assoc_args ) {
203 $id = $args[0];
204
205 $child = false;
206 if ( false !== strpos( $id, '[' ) ) {
207 parse_str( $id, $id2 );
208 Utility::compatibility();
209 $id = array_key_first( $id2 );
210
211 $child = array_key_first( $id2[ $id ] ); // is `url`
212 if ( ! $child ) {
213 WP_CLI::error( 'Wrong child key' );
214 return;
215 }
216 $numeric = array_key_first( $id2[ $id ][ $child ] ); // `0`
217 if ( null === $numeric ) {
218 WP_CLI::error( 'Wrong 2nd level numeric key' );
219 return;
220 }
221 }
222
223 if ( ! isset( self::$_default_options[ $id ] ) ) {
224 WP_CLI::error( 'ID not exist [id] ' . $id );
225 return;
226 }
227
228 $v = $this->conf( $id );
229 $default_v = self::$_default_options[ $id ];
230
231 // For CDN_mapping and crawler_cookies
232 // Examples of option name:
233 // cdn-mapping[url][0]
234 // crawler-cookies[name][1]
235 if ( self::O_CDN_MAPPING === $id ) {
236 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 ) ) {
237 WP_CLI::error( 'Wrong child key' );
238 return;
239 }
240 }
241 if ( self::O_CRAWLER_COOKIES === $id ) {
242 if ( ! in_array( $child, array( self::CRWL_COOKIE_NAME, self::CRWL_COOKIE_VALS ), true ) ) {
243 WP_CLI::error( 'Wrong child key' );
244 return;
245 }
246 }
247
248 if ( self::O_CDN_MAPPING === $id || self::O_CRAWLER_COOKIES === $id ) {
249 if ( ! empty( $v[ $numeric ][ $child ] ) ) {
250 $v = $v[ $numeric ][ $child ];
251 } elseif ( self::O_CDN_MAPPING === $id ) {
252 if ( in_array( $child, array( self::CDN_MAPPING_INC_IMG, self::CDN_MAPPING_INC_CSS, self::CDN_MAPPING_INC_JS ), true ) ) {
253 $v = 0;
254 } else {
255 $v = "''";
256 }
257 } else {
258 $v = "''";
259 }
260 }
261
262 if ( is_array( $v ) ) {
263 $v = implode( PHP_EOL, $v );
264 }
265
266 if ( ! $v && self::O_CDN_MAPPING !== $id && self::O_CRAWLER_COOKIES !== $id ) {
267 // empty array for CDN/crawler has been handled
268 if ( is_bool( $default_v ) ) {
269 $v = 0;
270 } elseif ( ! is_array( $default_v ) ) {
271 $v = "''";
272 }
273 }
274
275 WP_CLI::line( $v );
276 }
277
278 /**
279 * Export plugin options to a file.
280 *
281 * ## OPTIONS
282 *
283 * [--filename=<path>]
284 * : The default path used is CURRENTDIR/lscache_wp_options_DATE-TIME.txt.
285 * To select a different file, use this option.
286 *
287 * ## EXAMPLES
288 *
289 * # Export options to a file.
290 * $ wp litespeed-option export
291 *
292 * @param array $args Positional arguments.
293 * @param array $assoc_args Associative arguments.
294 */
295 public function export( $args, $assoc_args ) {
296 if ( isset( $assoc_args['filename'] ) ) {
297 $file = $assoc_args['filename'];
298 } else {
299 $file = getcwd() . '/litespeed_options_' . gmdate( 'd_m_Y-His' ) . '.data';
300 }
301
302 global $wp_filesystem;
303 if ( ! $wp_filesystem ) {
304 require_once ABSPATH . '/wp-admin/includes/file.php';
305 WP_Filesystem();
306 }
307
308 if ( ! $wp_filesystem->is_writable( dirname( $file ) ) ) {
309 WP_CLI::error( 'Directory not writable.' );
310 return;
311 }
312
313 $data = $this->cls( 'Import' )->export( true );
314
315 if ( false === $wp_filesystem->put_contents( $file, $data ) ) {
316 WP_CLI::error( 'Failed to create file.' );
317 return;
318 }
319
320 WP_CLI::success( 'Created file ' . $file );
321 }
322
323 /**
324 * Import plugin options from a file.
325 *
326 * The file must be formatted as such:
327 * option_key=option_value
328 * One per line.
329 * A semicolon at the beginning of the line indicates a comment and will be skipped.
330 *
331 * ## OPTIONS
332 *
333 * <file>
334 * : The file to import options from.
335 *
336 * ## EXAMPLES
337 *
338 * # Import options from CURRENTDIR/options.txt
339 * $ wp litespeed-option import options.txt
340 *
341 * @param array $args Positional arguments (file).
342 * @param array $assoc_args Associative arguments.
343 */
344 public function import( $args, $assoc_args ) {
345 $file = $args[0];
346
347 global $wp_filesystem;
348 if ( ! $wp_filesystem ) {
349 require_once ABSPATH . '/wp-admin/includes/file.php';
350 WP_Filesystem();
351 }
352
353 if ( ! $wp_filesystem->exists( $file ) || ! $wp_filesystem->is_readable( $file ) ) {
354 WP_CLI::error( 'File does not exist or is not readable.' );
355 return;
356 }
357
358 $res = $this->cls( 'Import' )->import( $file );
359
360 if ( ! $res ) {
361 WP_CLI::error( 'Failed to parse serialized data from file.' );
362 return;
363 }
364
365 WP_CLI::success( 'Options imported. [File] ' . $file );
366 }
367
368 /**
369 * Import plugin options from a remote file.
370 *
371 * The file must be formatted as such:
372 * option_key=option_value
373 * One per line.
374 * A semicolon at the beginning of the line indicates a comment and will be skipped.
375 *
376 * ## OPTIONS
377 *
378 * <url>
379 * : The URL to import options from.
380 *
381 * ## EXAMPLES
382 *
383 * # Import options from https://domain.com/options.txt
384 * $ wp litespeed-option import_remote https://domain.com/options.txt
385 *
386 * @param array $args Positional arguments (url).
387 */
388 public function import_remote( $args ) {
389 $file = $args[0];
390
391 $tmp_file = download_url( $file );
392
393 if ( is_wp_error( $tmp_file ) ) {
394 WP_CLI::error( 'Failed to download file.' );
395 return;
396 }
397
398 $res = $this->cls( 'Import' )->import( $tmp_file );
399
400 if ( ! $res ) {
401 WP_CLI::error( 'Failed to parse serialized data from file.' );
402 return;
403 }
404
405 WP_CLI::success( 'Options imported. [File] ' . $file );
406 }
407
408 /**
409 * Reset all options to default.
410 *
411 * ## EXAMPLES
412 *
413 * # Reset all options
414 * $ wp litespeed-option reset
415 */
416 public function reset() {
417 $this->cls( 'Import' )->reset();
418 }
419 }
420