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 / src / admin-display.cls.php
litespeed-cache / src Last commit date
cdn 2 months ago data_structure 2 months ago activation.cls.php 2 months ago admin-display.cls.php 2 months ago admin-settings.cls.php 2 months ago admin.cls.php 2 months ago api.cls.php 2 months ago avatar.cls.php 2 months ago base.cls.php 2 months ago cdn.cls.php 2 months ago cloud-auth-callback.trait.php 2 months ago cloud-auth-ip.trait.php 2 months ago cloud-auth.trait.php 2 months ago cloud-misc.trait.php 2 months ago cloud-node.trait.php 2 months ago cloud-request.trait.php 2 months ago cloud.cls.php 2 months ago conf.cls.php 2 months ago control.cls.php 2 months ago core.cls.php 2 months ago crawler-map.cls.php 2 months ago crawler.cls.php 2 months ago css.cls.php 2 months ago data.cls.php 2 months ago data.upgrade.func.php 2 months ago db-optm.cls.php 2 months ago debug2.cls.php 2 months ago doc.cls.php 2 months ago error.cls.php 2 months ago esi.cls.php 2 months ago file.cls.php 2 months ago guest.cls.php 2 months ago gui.cls.php 2 months ago health.cls.php 2 months ago htaccess.cls.php 2 months ago img-optm-manage.trait.php 2 months ago img-optm-pull.trait.php 2 months ago img-optm-send.trait.php 2 months ago img-optm.cls.php 2 months ago import.cls.php 2 months ago import.preset.cls.php 2 months ago lang.cls.php 2 months ago localization.cls.php 2 months ago media.cls.php 2 months ago metabox.cls.php 2 months ago object-cache-wp.cls.php 2 months ago object-cache.cls.php 2 months ago object.lib.php 2 months ago optimize.cls.php 2 months ago optimizer.cls.php 2 months ago placeholder.cls.php 2 months ago purge.cls.php 2 months ago report.cls.php 2 months ago rest.cls.php 2 months ago root.cls.php 2 months ago router.cls.php 2 months ago str.cls.php 2 months ago tag.cls.php 2 months ago task.cls.php 2 months ago tool.cls.php 2 months ago ucss.cls.php 2 months ago utility.cls.php 2 months ago vary.cls.php 2 months ago vpi.cls.php 2 months ago
admin-display.cls.php
1676 lines
1 <?php
2 /**
3 * The admin-panel specific functionality of the plugin.
4 *
5 * Provides admin page rendering, notices, enqueueing of assets,
6 * menu registrations, and various admin utilities.
7 *
8 * @since 1.0.0
9 * @package LiteSpeed
10 * @subpackage LiteSpeed/admin
11 * @author LiteSpeed Technologies <info@litespeedtech.com>
12 */
13
14 namespace LiteSpeed;
15
16 defined( 'WPINC' ) || exit();
17
18 /**
19 * Class Admin_Display
20 *
21 * Handles WP-Admin UI for LiteSpeed Cache.
22 */
23 class Admin_Display extends Base {
24
25 /**
26 * Log tag for Admin_Display.
27 *
28 * @var string
29 */
30 const LOG_TAG = '👮‍♀️';
31
32 /**
33 * Notice class (info/blue).
34 *
35 * @var string
36 */
37 const NOTICE_BLUE = 'notice notice-info';
38 /**
39 * Notice class (success/green).
40 *
41 * @var string
42 */
43 const NOTICE_GREEN = 'notice notice-success';
44 /**
45 * Notice class (error/red).
46 *
47 * @var string
48 */
49 const NOTICE_RED = 'notice notice-error';
50 /**
51 * Notice class (warning/yellow).
52 *
53 * @var string
54 */
55 const NOTICE_YELLOW = 'notice notice-warning';
56 /**
57 * Option key for one-time messages.
58 *
59 * @var string
60 */
61 const DB_MSG = 'messages';
62 /**
63 * Option key for pinned messages.
64 *
65 * @var string
66 */
67 const DB_MSG_PIN = 'msg_pin';
68
69 /**
70 * Purge by: category.
71 *
72 * @var string
73 */
74 const PURGEBY_CAT = '0';
75 /**
76 * Purge by: post ID.
77 *
78 * @var string
79 */
80 const PURGEBY_PID = '1';
81 /**
82 * Purge by: tag.
83 *
84 * @var string
85 */
86 const PURGEBY_TAG = '2';
87 /**
88 * Purge by: URL.
89 *
90 * @var string
91 */
92 const PURGEBY_URL = '3';
93
94 /**
95 * Purge selection field name.
96 *
97 * @var string
98 */
99 const PURGEBYOPT_SELECT = 'purgeby';
100 /**
101 * Purge list field name.
102 *
103 * @var string
104 */
105 const PURGEBYOPT_LIST = 'purgebylist';
106
107 /**
108 * Dismiss key for messages.
109 *
110 * @var string
111 */
112 const DB_DISMISS_MSG = 'dismiss';
113 /**
114 * Rule conflict flag (on).
115 *
116 * @var string
117 */
118 const RULECONFLICT_ON = 'ExpiresDefault_1';
119 /**
120 * Rule conflict dismissed flag.
121 *
122 * @var string
123 */
124 const RULECONFLICT_DISMISSED = 'ExpiresDefault_0';
125
126 /**
127 * Router type for QC hide banner.
128 *
129 * @var string
130 */
131 const TYPE_QC_HIDE_BANNER = 'qc_hide_banner';
132 /**
133 * Cookie name for QC hide banner.
134 *
135 * @var string
136 */
137 const COOKIE_QC_HIDE_BANNER = 'litespeed_qc_hide_banner';
138
139 /**
140 * Internal messages cache.
141 *
142 * @var array<string,string>
143 */
144 protected $messages = [];
145
146 /**
147 * Cached default settings.
148 *
149 * @var array<string,mixed>
150 */
151 protected $default_settings = [];
152
153 /**
154 * Whether current context is network admin.
155 *
156 * @var bool
157 */
158 protected $_is_network_admin = false;
159
160 /**
161 * Whether multisite is enabled.
162 *
163 * @var bool
164 */
165 protected $_is_multisite = false;
166
167 /**
168 * Incremental form submit button index.
169 *
170 * @var int
171 */
172 private $_btn_i = 0;
173
174 /**
175 * List of settings with filters and return type.
176 *
177 * @since 7.4
178 * @deprecated 7.7 Use general conf fitlers.
179 *
180 * @var array<string,array<string,mixed>>
181 */
182 protected static $settings_filters = [
183 // Crawler - Blocklist.
184 'crawler-blocklist' => [
185 'filter' => 'litespeed_crawler_disable_blocklist',
186 'type' => 'boolean',
187 ],
188 // Crawler - Settings.
189 self::O_CRAWLER_LOAD_LIMIT => [
190 'filter' => [ Base::ENV_CRAWLER_LOAD_LIMIT_ENFORCE, Base::ENV_CRAWLER_LOAD_LIMIT ],
191 'type' => 'input',
192 ],
193 // Cache - ESI.
194 self::O_ESI_NONCE => [
195 'filter' => 'litespeed_esi_nonces',
196 ],
197 // Page Optimization - CSS.
198 'optm-ucss_per_pagetype' => [
199 'filter' => 'litespeed_ucss_per_pagetype',
200 'type' => 'boolean',
201 ],
202 // Page Optimization - Media.
203 self::O_MEDIA_ADD_MISSING_SIZES => [
204 'filter' => 'litespeed_media_ignore_remote_missing_sizes',
205 'type' => 'boolean',
206 ],
207 // Page Optimization - Media Exclude.
208 self::O_MEDIA_LAZY_EXC => [
209 'filter' => 'litespeed_media_lazy_img_excludes',
210 ],
211 // Page Optimization - Tuning (JS).
212 self::O_OPTM_JS_DELAY_INC => [
213 'filter' => 'litespeed_optm_js_delay_inc',
214 ],
215 self::O_OPTM_JS_EXC => [
216 'filter' => 'litespeed_optimize_js_excludes',
217 ],
218 self::O_OPTM_JS_DEFER_EXC => [
219 'filter' => 'litespeed_optm_js_defer_exc',
220 ],
221 self::O_OPTM_GM_JS_EXC => [
222 'filter' => 'litespeed_optm_gm_js_exc',
223 ],
224 self::O_OPTM_EXC => [
225 'filter' => 'litespeed_optm_uri_exc',
226 ],
227 // Page Optimization - Tuning (CSS).
228 self::O_OPTM_CSS_EXC => [
229 'filter' => 'litespeed_optimize_css_excludes',
230 ],
231 self::O_OPTM_UCSS_EXC => [
232 'filter' => 'litespeed_ucss_exc',
233 ],
234 ];
235
236 /**
237 * Flat pages map: menu slug to template metadata.
238 *
239 * @var array<string,array{title:string,tpl:string,network?:bool}>
240 */
241 private $_pages = [];
242
243 /**
244 * Initialize the class and set its properties.
245 *
246 * @since 1.0.7
247 */
248 public function __construct() {
249 $this->_pages = [
250 // Site-level pages
251 'litespeed' => [ 'title' => __( 'Dashboard', 'litespeed-cache' ), 'tpl' => 'dash/entry.tpl.php' ],
252 'litespeed-optimax' => [ 'title' => __( 'OptimaX', 'litespeed-cache' ), 'tpl' => 'optimax/entry.tpl.php', 'scope' => 'site' ],
253 'litespeed-presets' => [ 'title' => __( 'Presets', 'litespeed-cache' ), 'tpl' => 'presets/entry.tpl.php', 'scope' => 'site' ],
254 'litespeed-general' => [ 'title' => __( 'General', 'litespeed-cache' ), 'tpl' => 'general/entry.tpl.php' ],
255 'litespeed-cache' => [ 'title' => __( 'Cache', 'litespeed-cache' ), 'tpl' => 'cache/entry.tpl.php' ],
256 'litespeed-cdn' => [ 'title' => __( 'CDN', 'litespeed-cache' ), 'tpl' => 'cdn/entry.tpl.php', 'scope' => 'site' ],
257 'litespeed-img_optm' => [ 'title' => __( 'Image Optimization', 'litespeed-cache'), 'tpl' => 'img_optm/entry.tpl.php' ],
258 'litespeed-page_optm' => [ 'title' => __( 'Page Optimization', 'litespeed-cache' ), 'tpl' => 'page_optm/entry.tpl.php', 'scope' => 'site' ],
259 'litespeed-db_optm' => [ 'title' => __( 'Database', 'litespeed-cache' ), 'tpl' => 'db_optm/entry.tpl.php' ],
260 'litespeed-crawler' => [ 'title' => __( 'Crawler', 'litespeed-cache' ), 'tpl' => 'crawler/entry.tpl.php', 'scope' => 'site' ],
261 'litespeed-toolbox' => [ 'title' => __( 'Toolbox', 'litespeed-cache' ), 'tpl' => 'toolbox/entry.tpl.php' ],
262 ];
263
264 // main css
265 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_style' ] );
266 // Main js
267 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
268
269 $this->_is_network_admin = is_network_admin();
270 $this->_is_multisite = is_multisite();
271
272 // Quick access menu
273 $manage = ( $this->_is_multisite && $this->_is_network_admin ) ? 'manage_network_options' : 'manage_options';
274
275 if ( current_user_can( $manage ) ) {
276 add_action( 'wp_before_admin_bar_render', [ GUI::cls(), 'backend_shortcut' ] );
277
278 // `admin_notices` is after `admin_enqueue_scripts`.
279 add_action( $this->_is_network_admin ? 'network_admin_notices' : 'admin_notices', [ $this, 'display_messages' ] );
280 }
281
282 /**
283 * In case this is called outside the admin page.
284 *
285 * @see https://codex.wordpress.org/Function_Reference/is_plugin_active_for_network
286 * @since 2.0
287 */
288 if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
289 require_once ABSPATH . 'wp-admin/includes/plugin.php';
290 }
291
292 // add menus (Also check for mu-plugins)
293 if ( $this->_is_network_admin && ( is_plugin_active_for_network( LSCWP_BASENAME ) || defined( 'LSCWP_MU_PLUGIN' ) ) ) {
294 add_action( 'network_admin_menu', [ $this, 'register_admin_menu' ] );
295 } else {
296 add_action( 'admin_menu', [ $this, 'register_admin_menu' ] );
297 }
298
299 $this->cls( 'Metabox' )->register_settings();
300 }
301
302 /**
303 * Echo a translated section title.
304 *
305 * @since 3.0
306 *
307 * @param string $id Language key.
308 * @return void
309 */
310 public function title( $id ) {
311 echo wp_kses_post( Lang::title( $id ) );
312 }
313
314 /**
315 * Bind per-page admin hooks for a given page hook.
316 *
317 * Adds footer text filter and preview banner when loading the page.
318 *
319 * @param string $hook Page hook suffix returned by add_*_page().
320 * @return void
321 */
322 private function bind_page( $hook ) {
323 add_action( "load-$hook", function () {
324 add_filter(
325 'admin_footer_text',
326 function ( $footer_text ) {
327 $this->cls( 'Cloud' )->maybe_preview_banner();
328 require_once LSCWP_DIR . 'tpl/inc/admin_footer.php';
329 return $footer_text;
330 },
331 1
332 );
333 // Add unified body class for settings page and top-level page
334 add_filter( 'admin_body_class', function ( $classes ) {
335 $screen = get_current_screen();
336 if ( $screen && in_array( $screen->id, [ 'settings_page_litespeed-cache-options', 'toplevel_page_litespeed' ], true ) ) {
337 $classes .= ' litespeed-cache_page_litespeed';
338 }
339 return $classes;
340 } );
341 } );
342 }
343
344 /**
345 * Render an admin page by slug using its mapped template file.
346 *
347 * @param string $slug The menu slug registered in $_pages.
348 * @return void
349 */
350 private function render_page( $slug ) {
351 $tpl = LSCWP_DIR . 'tpl/' . $this->_pages[ $slug ]['tpl'];
352 is_file( $tpl ) ? require $tpl : wp_die( 'Template not found' );
353 }
354
355 /**
356 * Register the admin menu display.
357 *
358 * @since 1.0.0
359 * @return void
360 */
361 public function register_admin_menu() {
362 $capability = $this->_is_network_admin ? 'manage_network_options' : 'manage_options';
363 $scope = $this->_is_network_admin ? 'network' : 'site';
364
365 add_menu_page(
366 'LiteSpeed Cache',
367 'LiteSpeed Cache',
368 $capability,
369 'litespeed'
370 );
371
372 foreach ( $this->_pages as $slug => $meta ) {
373 if ( 'litespeed-optimax' === $slug && !defined( 'LITESPEED_OX' ) ) {
374 continue;
375 }
376 if ( ! empty( $meta['scope'] ) && $meta['scope'] !== $scope ) {
377 continue;
378 }
379 $hook = add_submenu_page(
380 'litespeed',
381 $meta['title'],
382 $meta['title'],
383 $capability,
384 $slug,
385 function () use ( $slug ) {
386 $this->render_page( $slug );
387 }
388 );
389 $this->bind_page( $hook );
390 }
391
392 // sub menus under options.
393 $hook = add_options_page(
394 'LiteSpeed Cache',
395 'LiteSpeed Cache',
396 $capability,
397 'litespeed-cache-options',
398 function () {
399 $this->render_page( 'litespeed-cache' );
400 }
401 );
402 $this->bind_page( $hook );
403 }
404
405 /**
406 * Register the stylesheets for the admin area.
407 *
408 * @since 1.0.14
409 * @return void
410 */
411 public function enqueue_style() {
412 wp_enqueue_style( Core::PLUGIN_NAME, LSWCP_PLUGIN_URL . 'assets/css/litespeed.css', [], Core::VER, 'all' );
413 wp_enqueue_style( Core::PLUGIN_NAME . '-dark-mode', LSWCP_PLUGIN_URL . 'assets/css/litespeed-dark-mode.css', [], Core::VER, 'all' );
414 }
415
416 /**
417 * Register/enqueue the JavaScript for the admin area.
418 *
419 * @since 1.0.0
420 * @since 7.3 Added deactivation modal code.
421 * @return void
422 */
423 public function enqueue_scripts() {
424 wp_register_script( Core::PLUGIN_NAME, LSWCP_PLUGIN_URL . 'assets/js/litespeed-cache-admin.js', [], Core::VER, true );
425
426 $localize_data = [];
427 if ( GUI::has_whm_msg() ) {
428 $ajax_url_dismiss_whm = Utility::build_url( Core::ACTION_DISMISS, GUI::TYPE_DISMISS_WHM, true );
429 $localize_data['ajax_url_dismiss_whm'] = $ajax_url_dismiss_whm;
430 }
431
432 if ( GUI::has_msg_ruleconflict() ) {
433 $ajax_url = Utility::build_url( Core::ACTION_DISMISS, GUI::TYPE_DISMISS_EXPIRESDEFAULT, true );
434 $localize_data['ajax_url_dismiss_ruleconflict'] = $ajax_url;
435 }
436
437 // Injection to LiteSpeed pages
438 global $pagenow;
439 $page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
440
441 if ( 'admin.php' === $pagenow && $page && ( 0 === strpos( $page, 'litespeed-' ) || 'litespeed' === $page ) ) {
442 if ( in_array( $page, [ 'litespeed-crawler', 'litespeed-cdn' ], true ) ) {
443 // Babel JS type correction
444 add_filter( 'script_loader_tag', [ $this, 'babel_type' ], 10, 3 );
445
446 wp_enqueue_script( Core::PLUGIN_NAME . '-lib-react', LSWCP_PLUGIN_URL . 'assets/js/react.min.js', [], Core::VER, false );
447 wp_enqueue_script( Core::PLUGIN_NAME . '-lib-babel', LSWCP_PLUGIN_URL . 'assets/js/babel.min.js', [], Core::VER, false );
448 }
449
450 // Crawler Cookie Simulation
451 if ( 'litespeed-crawler' === $page ) {
452 wp_enqueue_script( Core::PLUGIN_NAME . '-crawler', LSWCP_PLUGIN_URL . 'assets/js/component.crawler.js', [], Core::VER, false );
453
454 $localize_data['lang'] = [];
455 $localize_data['lang']['cookie_name'] = __( 'Cookie Name', 'litespeed-cache' );
456 $localize_data['lang']['cookie_value'] = __( 'Cookie Values', 'litespeed-cache' );
457 $localize_data['lang']['one_per_line'] = Doc::one_per_line( true );
458 $localize_data['lang']['remove_cookie_simulation'] = __( 'Remove cookie simulation', 'litespeed-cache' );
459 $localize_data['lang']['add_cookie_simulation_row'] = __( 'Add new cookie to simulate', 'litespeed-cache' );
460 if ( empty( $localize_data['ids'] ) ) {
461 $localize_data['ids'] = [];
462 }
463 $localize_data['ids']['crawler_cookies'] = self::O_CRAWLER_COOKIES;
464 }
465
466 // CDN mapping
467 if ( 'litespeed-cdn' === $page ) {
468 $home_url = home_url( '/' );
469 $parsed = wp_parse_url( $home_url );
470 if ( ! empty( $parsed['scheme'] ) ) {
471 $home_url = str_replace( $parsed['scheme'] . ':', '', $home_url );
472 }
473 $cdn_url = 'https://cdn.' . substr( $home_url, 2 );
474
475 wp_enqueue_script( Core::PLUGIN_NAME . '-cdn', LSWCP_PLUGIN_URL . 'assets/js/component.cdn.js', [], Core::VER, false );
476 $localize_data['lang'] = [];
477 $localize_data['lang']['cdn_mapping_url'] = Lang::title( self::CDN_MAPPING_URL );
478 $localize_data['lang']['cdn_mapping_inc_img'] = Lang::title( self::CDN_MAPPING_INC_IMG );
479 $localize_data['lang']['cdn_mapping_inc_css'] = Lang::title( self::CDN_MAPPING_INC_CSS );
480 $localize_data['lang']['cdn_mapping_inc_js'] = Lang::title( self::CDN_MAPPING_INC_JS );
481 $localize_data['lang']['cdn_mapping_filetype'] = Lang::title( self::CDN_MAPPING_FILETYPE );
482 $localize_data['lang']['cdn_mapping_url_desc'] = sprintf( __( 'CDN URL to be used. For example, %s', 'litespeed-cache' ), '<code>' . esc_html( $cdn_url ) . '</code>' );
483 $localize_data['lang']['one_per_line'] = Doc::one_per_line( true );
484 $localize_data['lang']['cdn_mapping_remove'] = __( 'Remove CDN URL', 'litespeed-cache' );
485 $localize_data['lang']['add_cdn_mapping_row'] = __( 'Add new CDN URL', 'litespeed-cache' );
486 $localize_data['lang']['on'] = __( 'ON', 'litespeed-cache' );
487 $localize_data['lang']['off'] = __( 'OFF', 'litespeed-cache' );
488 if ( empty( $localize_data['ids'] ) ) {
489 $localize_data['ids'] = [];
490 }
491 $localize_data['ids']['cdn_mapping'] = self::O_CDN_MAPPING;
492 }
493 }
494
495 // Load iziModal JS and CSS
496 $show_deactivation_modal = ( is_multisite() && ! is_network_admin() ) ? false : true;
497 if ( $show_deactivation_modal && 'plugins.php' === $pagenow ) {
498 wp_enqueue_script( Core::PLUGIN_NAME . '-iziModal', LSWCP_PLUGIN_URL . 'assets/js/iziModal.min.js', [], Core::VER, true );
499 wp_enqueue_style( Core::PLUGIN_NAME . '-iziModal', LSWCP_PLUGIN_URL . 'assets/css/iziModal.min.css', [], Core::VER, 'all' );
500 add_action( 'admin_footer', [ $this, 'add_deactivation_html' ] );
501 }
502
503 if ( $localize_data ) {
504 wp_localize_script( Core::PLUGIN_NAME, 'litespeed_data', $localize_data );
505 }
506
507 wp_enqueue_script( Core::PLUGIN_NAME );
508 }
509
510 /**
511 * Add modal HTML on Plugins screen.
512 *
513 * @since 7.3
514 * @return void
515 */
516 public function add_deactivation_html() {
517 require LSCWP_DIR . 'tpl/inc/modal.deactivation.php';
518 }
519
520 /**
521 * Filter the script tag for specific handles to set Babel type.
522 *
523 * @since 3.6
524 *
525 * @param string $tag The script tag.
526 * @param string $handle Script handle.
527 * @param string $src Script source URL.
528 * @return string The filtered script tag.
529 */
530 public function babel_type( $tag, $handle, $src ) {
531 if ( Core::PLUGIN_NAME . '-crawler' !== $handle && Core::PLUGIN_NAME . '-cdn' !== $handle ) {
532 return $tag;
533 }
534
535 // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
536 return '<script src="' . Str::trim_quotes( $src ) . '" type="text/babel"></script>';
537 }
538
539 /**
540 * Callback that adds LiteSpeed Cache's action links.
541 *
542 * @since 1.0.0
543 *
544 * @param array<string> $links Previously added links from other plugins.
545 * @return array<string> Links with the LiteSpeed Cache one appended.
546 */
547 public function add_plugin_links( $links ) {
548 $links[] = '<a href="' . esc_url( admin_url( 'admin.php?page=litespeed-cache' ) ) . '">' . esc_html__( 'Settings', 'litespeed-cache' ) . '</a>';
549
550 return $links;
551 }
552
553 /**
554 * Build a single notice HTML string.
555 *
556 * @since 1.0.7
557 *
558 * @param string $color The color CSS class for the notice.
559 * @param string $str The notice message.
560 * @param bool $irremovable If true, the notice cannot be dismissed.
561 * @param string $additional_classes Additional classes to add to the wrapper.
562 * @return string The built notice HTML.
563 */
564 public static function build_notice( $color, $str, $irremovable = false, $additional_classes = '' ) {
565 $cls = $color;
566 if ( $irremovable ) {
567 $cls .= ' litespeed-irremovable';
568 } else {
569 $cls .= ' is-dismissible';
570 }
571 if ( $additional_classes ) {
572 $cls .= ' ' . $additional_classes;
573 }
574
575 // possible translation
576 $str = Lang::maybe_translate( $str );
577
578 return '<div class="litespeed_icon ' . esc_attr( $cls ) . '"><p>' . wp_kses_post( $str ) . '</p></div>';
579 }
580
581 /**
582 * Display info notice.
583 *
584 * @since 1.6.5
585 *
586 * @param string|array<string> $msg Message or list of messages.
587 * @param bool $do_echo Echo immediately instead of storing.
588 * @param bool $irremovable If true, cannot be dismissed.
589 * @param string $additional_classes Extra CSS classes.
590 * @return void
591 */
592 public static function info( $msg, $do_echo = false, $irremovable = false, $additional_classes = '' ) {
593 self::add_notice( self::NOTICE_BLUE, $msg, $do_echo, $irremovable, $additional_classes );
594 }
595
596 /**
597 * Display note (warning) notice.
598 *
599 * @since 1.6.5
600 *
601 * @param string|array<string> $msg Message or list of messages.
602 * @param bool $do_echo Echo immediately instead of storing.
603 * @param bool $irremovable If true, cannot be dismissed.
604 * @param string $additional_classes Extra CSS classes.
605 * @return void
606 */
607 public static function note( $msg, $do_echo = false, $irremovable = false, $additional_classes = '' ) {
608 self::add_notice( self::NOTICE_YELLOW, $msg, $do_echo, $irremovable, $additional_classes );
609 }
610
611 /**
612 * Display success notice.
613 *
614 * @since 1.6
615 *
616 * @param string|array<string> $msg Message or list of messages.
617 * @param bool $do_echo Echo immediately instead of storing.
618 * @param bool $irremovable If true, cannot be dismissed.
619 * @param string $additional_classes Extra CSS classes.
620 * @return void
621 */
622 public static function success( $msg, $do_echo = false, $irremovable = false, $additional_classes = '' ) {
623 self::add_notice( self::NOTICE_GREEN, $msg, $do_echo, $irremovable, $additional_classes );
624 }
625
626 /**
627 * Deprecated alias for success().
628 *
629 * @deprecated 4.7 Will drop in v7.5. Use success().
630 *
631 * @param string|array<string> $msg Message or list of messages.
632 * @param bool $do_echo Echo immediately instead of storing.
633 * @param bool $irremovable If true, cannot be dismissed.
634 * @param string $additional_classes Extra CSS classes.
635 * @return void
636 */
637 public static function succeed( $msg, $do_echo = false, $irremovable = false, $additional_classes = '' ) {
638 self::success( $msg, $do_echo, $irremovable, $additional_classes );
639 }
640
641 /**
642 * Display error notice.
643 *
644 * @since 1.6
645 *
646 * @param string|array<string> $msg Message or list of messages.
647 * @param bool $do_echo Echo immediately instead of storing.
648 * @param bool $irremovable If true, cannot be dismissed.
649 * @param string $additional_classes Extra CSS classes.
650 * @return void
651 */
652 public static function error( $msg, $do_echo = false, $irremovable = false, $additional_classes = '' ) {
653 self::add_notice( self::NOTICE_RED, $msg, $do_echo, $irremovable, $additional_classes );
654 }
655
656 /**
657 * Add unique (irremovable optional) messages.
658 *
659 * @since 4.7
660 *
661 * @param string $color_mode One of info|note|success|error.
662 * @param string|array<string> $msgs Message(s).
663 * @param bool $irremovable If true, cannot be dismissed.
664 * @return void
665 */
666 public static function add_unique_notice( $color_mode, $msgs, $irremovable = false ) {
667 if ( ! is_array( $msgs ) ) {
668 $msgs = [ $msgs ];
669 }
670
671 $color_map = [
672 'info' => self::NOTICE_BLUE,
673 'note' => self::NOTICE_YELLOW,
674 'success' => self::NOTICE_GREEN,
675 'error' => self::NOTICE_RED,
676 ];
677 if ( empty( $color_map[ $color_mode ] ) ) {
678 self::debug( 'Wrong admin display color mode!' );
679 return;
680 }
681 $color = $color_map[ $color_mode ];
682
683 // Go through to make sure unique.
684 $filtered_msgs = [];
685 foreach ( $msgs as $k => $str ) {
686 if ( is_numeric( $k ) ) {
687 $k = md5( $str );
688 } // Use key to make it overwritable to previous same msg.
689 $filtered_msgs[ $k ] = $str;
690 }
691
692 self::add_notice( $color, $filtered_msgs, false, $irremovable );
693 }
694
695 /**
696 * Add a notice to display on the admin page (store or echo).
697 *
698 * @since 1.0.7
699 *
700 * @param string $color Notice color CSS class.
701 * @param string|array<string> $msg Message(s).
702 * @param bool $do_echo Echo immediately instead of storing.
703 * @param bool $irremovable If true, cannot be dismissed.
704 * @param string $additional_classes Extra classes for wrapper.
705 * @return void
706 */
707 public static function add_notice( $color, $msg, $do_echo = false, $irremovable = false, $additional_classes = '' ) {
708 // Bypass adding for CLI or cron
709 if ( defined( 'LITESPEED_CLI' ) || wp_doing_cron() ) {
710 // WP CLI will show the info directly
711 if ( defined( 'WP_CLI' ) && constant('WP_CLI') ) {
712 if ( ! is_array( $msg ) ) {
713 $msg = [ $msg ];
714 }
715 foreach ( $msg as $v ) {
716 $v = wp_strip_all_tags( $v );
717 if ( self::NOTICE_RED === $color ) {
718 \WP_CLI::error( $v, false );
719 } else {
720 \WP_CLI::success( $v );
721 }
722 }
723 }
724 return;
725 }
726
727 if ( $do_echo ) {
728 echo self::build_notice( $color, $msg, $irremovable, $additional_classes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
729 return;
730 }
731
732 $msg_name = $irremovable ? self::DB_MSG_PIN : self::DB_MSG;
733
734 $messages = self::get_option( $msg_name, [] );
735 if ( ! is_array( $messages ) ) {
736 $messages = [];
737 }
738
739 if ( is_array( $msg ) ) {
740 foreach ( $msg as $k => $str ) {
741 $messages[ $k ] = self::build_notice( $color, $str, $irremovable, $additional_classes );
742 }
743 } else {
744 $messages[] = self::build_notice( $color, $msg, $irremovable, $additional_classes );
745 }
746 $messages = array_unique( $messages );
747 self::update_option( $msg_name, $messages );
748 }
749
750 /**
751 * Display notices and errors in dashboard.
752 *
753 * @since 1.1.0
754 * @return void
755 */
756 public function display_messages() {
757 if ( ! defined( 'LITESPEED_CONF_LOADED' ) ) {
758 $this->_in_upgrading();
759 }
760
761 if ( GUI::has_whm_msg() ) {
762 $this->show_display_installed();
763 }
764
765 Data::cls()->check_upgrading_msg();
766
767 // If is in dev version, always check latest update
768 Cloud::cls()->check_dev_version();
769
770 // One time msg
771 $messages = self::get_option( self::DB_MSG, [] );
772 $added_thickbox = false;
773 if ( is_array( $messages ) ) {
774 foreach ( $messages as $msg ) {
775 // Added for popup links
776 if ( strpos( $msg, 'TB_iframe' ) && ! $added_thickbox ) {
777 add_thickbox();
778 $added_thickbox = true;
779 }
780 echo wp_kses_post( $msg );
781 }
782 }
783 if ( -1 !== $messages ) {
784 self::update_option( self::DB_MSG, -1 );
785 }
786
787 // Pinned msg
788 $messages = self::get_option( self::DB_MSG_PIN, [] );
789 if ( is_array( $messages ) ) {
790 foreach ( $messages as $k => $msg ) {
791 // Added for popup links
792 if ( strpos( $msg, 'TB_iframe' ) && ! $added_thickbox ) {
793 add_thickbox();
794 $added_thickbox = true;
795 }
796
797 // Append close btn
798 if ( '</div>' === substr( $msg, -6 ) ) {
799 $link = Utility::build_url( Core::ACTION_DISMISS, GUI::TYPE_DISMISS_PIN, false, null, [ 'msgid' => $k ] );
800 $msg =
801 substr( $msg, 0, -6 ) .
802 '<p><a href="' .
803 esc_url( $link ) .
804 '" class="button litespeed-btn-primary litespeed-btn-mini">' .
805 esc_html__( 'Dismiss', 'litespeed-cache' ) .
806 '</a>' .
807 '</p></div>';
808 }
809 echo wp_kses_post( $msg );
810 }
811 }
812
813 if ( empty( $_GET['page'] ) || 0 !== strpos( sanitize_text_field( wp_unslash( $_GET['page'] ) ), 'litespeed' ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
814 global $pagenow;
815 if ( 'plugins.php' !== $pagenow ) {
816 return;
817 }
818 }
819
820 if ( ! $this->conf( self::O_NEWS ) ) {
821 return;
822 }
823
824 // Show promo from cloud
825 Cloud::cls()->show_promo();
826
827 /**
828 * Check promo msg first
829 *
830 * @since 2.9
831 */
832 GUI::cls()->show_promo();
833
834 // Show version news
835 Cloud::cls()->news();
836 }
837
838 /**
839 * Dismiss pinned msg.
840 *
841 * @since 3.5.2
842 * @return void
843 */
844 public static function dismiss_pin() {
845 if ( ! isset( $_GET['msgid'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
846 return;
847 }
848
849 $messages = self::get_option( self::DB_MSG_PIN, [] );
850 $msgid = sanitize_text_field( wp_unslash( $_GET['msgid'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
851
852 if ( ! is_array( $messages ) || empty( $messages[ $msgid ] ) ) {
853 return;
854 }
855
856 unset( $messages[ $msgid ] );
857 if ( ! $messages ) {
858 $messages = -1;
859 }
860 self::update_option( self::DB_MSG_PIN, $messages );
861 }
862
863 /**
864 * Dismiss pinned msg by msg content.
865 *
866 * @since 7.0
867 *
868 * @param string $content Message content.
869 * @param string $color Color CSS class.
870 * @param bool $irremovable Is irremovable.
871 * @return void
872 */
873 public static function dismiss_pin_by_content( $content, $color, $irremovable ) {
874 $content = self::build_notice( $color, $content, $irremovable );
875 $messages = self::get_option( self::DB_MSG_PIN, [] );
876 $hit = false;
877 if ( -1 !== $messages ) {
878 foreach ( $messages as $k => $v ) {
879 if ( $v === $content ) {
880 unset( $messages[ $k ] );
881 $hit = true;
882 self::debug( '�
883 pinned msg content hit. Removed' );
884 break;
885 }
886 }
887 }
888 if ( $hit ) {
889 if ( ! $messages ) {
890 $messages = -1;
891 }
892 self::update_option( self::DB_MSG_PIN, $messages );
893 } else {
894 self::debug( ' No pinned msg content hit' );
895 }
896 }
897
898 /**
899 * Hooked to the in_widget_form action.
900 * Appends LiteSpeed Cache settings to the widget edit settings screen.
901 * This will append the esi on/off selector and ttl text.
902 *
903 * @since 1.1.0
904 *
905 * @param \WP_Widget $widget The widget instance (passed by reference).
906 * @param mixed $return_val Return param (unused).
907 * @param array $instance The widget instance's settings.
908 * @return void
909 */
910 public function show_widget_edit( $widget, $return_val, $instance ) {
911 require LSCWP_DIR . 'tpl/esi_widget_edit.php';
912 }
913
914 /**
915 * Outputs a notice when the plugin is installed via WHM.
916 *
917 * @since 1.0.12
918 * @return void
919 */
920 public function show_display_installed() {
921 require_once LSCWP_DIR . 'tpl/inc/show_display_installed.php';
922 }
923
924 /**
925 * Display error cookie msg.
926 *
927 * @since 1.0.12
928 * @return void
929 */
930 public static function show_error_cookie() {
931 require_once LSCWP_DIR . 'tpl/inc/show_error_cookie.php';
932 }
933
934 /**
935 * Display warning if lscache is disabled.
936 *
937 * @since 2.1
938 * @return void
939 */
940 public function cache_disabled_warning() {
941 include LSCWP_DIR . 'tpl/inc/check_cache_disabled.php';
942 }
943
944 /**
945 * Display conf data upgrading banner.
946 *
947 * @since 2.1
948 * @access private
949 * @return void
950 */
951 private function _in_upgrading() {
952 include LSCWP_DIR . 'tpl/inc/in_upgrading.php';
953 }
954
955 /**
956 * Output LiteSpeed form open tag and hidden fields.
957 *
958 * @since 3.0
959 *
960 * @param string|false $action Router action.
961 * @param string|false $type Router type.
962 * @param bool $has_upload Whether form has file uploads.
963 * @return void
964 */
965 public function form_action( $action = false, $type = false, $has_upload = false ) {
966 if ( ! $action ) {
967 $action = Router::ACTION_SAVE_SETTINGS;
968 }
969
970 if ( ! defined( 'LITESPEED_CONF_LOADED' ) ) {
971 echo '<div class="litespeed-relative">';
972 } else {
973 $current = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
974 if ( $has_upload ) {
975 echo '<form method="post" action="' . esc_url( $current ) . '" class="litespeed-relative" enctype="multipart/form-data">';
976 } else {
977 echo '<form method="post" action="' . esc_url( $current ) . '" class="litespeed-relative">';
978 }
979 }
980
981 echo '<input type="hidden" name="' . esc_attr( Router::ACTION ) . '" value="' . esc_attr( $action ) . '" />';
982 if ( $type ) {
983 echo '<input type="hidden" name="' . esc_attr( Router::TYPE ) . '" value="' . esc_attr( $type ) . '" />';
984 }
985 wp_nonce_field( $action, Router::NONCE );
986 }
987
988 /**
989 * Output LiteSpeed form end (submit + closing tags).
990 *
991 * @since 3.0
992 *
993 * @return void
994 */
995 public function form_end() {
996 echo "<div class='litespeed-top20'></div>";
997
998 if ( ! defined( 'LITESPEED_CONF_LOADED' ) ) {
999 submit_button( __( 'Save Changes', 'litespeed-cache' ), 'secondary litespeed-duplicate-float', 'litespeed-submit', true, [ 'disabled' => 'disabled' ] );
1000
1001 echo '</div>';
1002 } else {
1003 submit_button(
1004 __( 'Save Changes', 'litespeed-cache' ),
1005 'primary litespeed-duplicate-float',
1006 'litespeed-submit',
1007 true,
1008 [
1009 'id' => 'litespeed-submit-' . $this->_btn_i++,
1010 ]
1011 );
1012
1013 echo '</form>';
1014 }
1015 }
1016
1017 /**
1018 * Register a setting for saving.
1019 *
1020 * @since 3.0
1021 *
1022 * @param string $id Setting ID.
1023 * @return void
1024 */
1025 public function enroll( $id ) {
1026 echo '<input type="hidden" name="' . esc_attr( Admin_Settings::ENROLL ) . '[]" value="' . esc_attr( $id ) . '" />';
1027 }
1028
1029 /**
1030 * Build a textarea input.
1031 *
1032 * @since 1.1.0
1033 *
1034 * @param string $id Setting ID.
1035 * @param int|false $cols Columns count.
1036 * @param string|nil $val Pre-set value.
1037 * @return void
1038 */
1039 public function build_textarea( $id, $cols = false, $val = null ) {
1040 if ( null === $val ) {
1041 $val = $this->conf( $id, true );
1042
1043 if ( is_array( $val ) ) {
1044 $val = implode( "\n", $val );
1045 }
1046 }
1047
1048 if ( ! $cols ) {
1049 $cols = 80;
1050 }
1051
1052 $rows = $this->get_textarea_rows( $val );
1053
1054 $this->enroll( $id );
1055
1056 echo "<textarea name='" . esc_attr( $id ) . "' rows='" . (int) $rows . "' cols='" . (int) $cols . "'>" . esc_textarea( $val ) . '</textarea>';
1057
1058 $this->_check_overwritten( $id );
1059 }
1060
1061 /**
1062 * Calculate textarea rows.
1063 *
1064 * @since 7.4
1065 *
1066 * @param string $val Text area value.
1067 * @return int Number of rows to use.
1068 */
1069 public function get_textarea_rows( $val ) {
1070 $rows = 5;
1071 $lines = substr_count( (string) $val, "\n" ) + 2;
1072 if ( $lines > $rows ) {
1073 $rows = $lines;
1074 }
1075 if ( $rows > 40 ) {
1076 $rows = 40;
1077 }
1078
1079 return $rows;
1080 }
1081
1082 /**
1083 * Build a text input field.
1084 *
1085 * @since 1.1.0
1086 *
1087 * @param string $id Setting ID.
1088 * @param string|null $cls CSS class.
1089 * @param string|null $val Value.
1090 * @param string $type Input type.
1091 * @param bool $disabled Whether disabled.
1092 * @return void
1093 */
1094 public function build_input( $id, $cls = null, $val = null, $type = 'text', $disabled = false ) {
1095 if ( null === $val ) {
1096 $val = $this->conf( $id, true );
1097
1098 // Mask passwords.
1099 if ( $this->_conf_pswd( $id ) && $val ) {
1100 $val = str_repeat( '*', strlen( $val ) );
1101 }
1102 }
1103
1104 $label_id = preg_replace( '/\W/', '', $id );
1105
1106 if ( 'text' === $type ) {
1107 $cls = "regular-text $cls";
1108 }
1109
1110 if ( $disabled ) {
1111 echo "<input type='" . esc_attr( $type ) . "' class='" . esc_attr( $cls ) . "' value='" . esc_attr( $val ) . "' id='input_" . esc_attr( $label_id ) . "' disabled /> ";
1112 } else {
1113 $this->enroll( $id );
1114 echo "<input type='" . esc_attr( $type ) . "' class='" . esc_attr( $cls ) . "' name='" . esc_attr( $id ) . "' value='" . esc_attr( $val ) . "' id='input_" . esc_attr( $label_id ) . "' /> ";
1115 }
1116
1117 $this->_check_overwritten( $id );
1118 }
1119
1120 /**
1121 * Build a checkbox HTML snippet.
1122 *
1123 * @since 1.1.0
1124 *
1125 * @param string $id Setting ID.
1126 * @param string $title Checkbox label (HTML allowed).
1127 * @param bool|null $checked Whether checked.
1128 * @param int|string $value Checkbox value.
1129 * @return void
1130 */
1131 public function build_checkbox( $id, $title, $checked = null, $value = 1 ) {
1132 if ( null === $checked && $this->conf( $id, true ) ) {
1133 $checked = true;
1134 }
1135
1136 $label_id = preg_replace( '/\W/', '', $id );
1137
1138 if ( 1 !== $value ) {
1139 $label_id .= '_' . $value;
1140 }
1141
1142 $this->enroll( $id );
1143
1144 echo "<div class='litespeed-tick'>
1145 <input type='checkbox' name='" . esc_attr( $id ) . "' id='input_checkbox_" . esc_attr( $label_id ) . "' value='" . esc_attr( $value ) . "' " . checked( (bool) $checked, true, false ) . " />
1146 <label for='input_checkbox_" . esc_attr( $label_id ) . "'>" . wp_kses_post( $title ) . '</label>
1147 </div>';
1148
1149 $this->_check_overwritten( $id );
1150 }
1151
1152 /**
1153 * Build a toggle checkbox snippet.
1154 *
1155 * @since 1.7
1156 *
1157 * @param string $id Setting ID.
1158 * @param bool|null $checked Whether enabled.
1159 * @param string|null $title_on Label when on.
1160 * @param string|null $title_off Label when off.
1161 * @return void
1162 */
1163 public function build_toggle( $id, $checked = null, $title_on = null, $title_off = null ) {
1164 if ( null === $checked && $this->conf( $id, true ) ) {
1165 $checked = true;
1166 }
1167 if ( null === $title_on ) {
1168 $title_on = __( 'ON', 'litespeed-cache' );
1169 $title_off = __( 'OFF', 'litespeed-cache' );
1170 }
1171 $cls = $checked ? 'primary' : 'default litespeed-toggleoff';
1172 echo "<div class='litespeed-toggle litespeed-toggle-btn litespeed-toggle-btn-" . esc_attr( $cls ) . "' data-litespeed-toggle-on='primary' data-litespeed-toggle-off='default' data-litespeed_toggle_id='" . esc_attr( $id ) . "' >
1173 <input name='" . esc_attr( $id ) . "' type='hidden' value='" . esc_attr( $checked ) . "' />
1174 <div class='litespeed-toggle-group'>
1175 <label class='litespeed-toggle-btn litespeed-toggle-btn-primary litespeed-toggle-on'>" . esc_html( $title_on ) . "</label>
1176 <label class='litespeed-toggle-btn litespeed-toggle-btn-default litespeed-toggle-active litespeed-toggle-off'>" . esc_html( $title_off ) . "</label>
1177 <span class='litespeed-toggle-handle litespeed-toggle-btn litespeed-toggle-btn-default'></span>
1178 </div>
1179 </div>";
1180 }
1181
1182 /**
1183 * Build a switch (radio) field.
1184 *
1185 * @since 1.1.0
1186 * @since 1.7 Removed $disable param.
1187 *
1188 * @param string $id Setting ID.
1189 * @param array<int,mixed>|false $title_list Labels for options (OFF/ON).
1190 * @return void
1191 */
1192 public function build_switch( $id, $title_list = false ) {
1193 $this->enroll( $id );
1194
1195 echo '<div class="litespeed-switch">';
1196
1197 if ( ! $title_list ) {
1198 $title_list = [ __( 'OFF', 'litespeed-cache' ), __( 'ON', 'litespeed-cache' ) ];
1199 }
1200
1201 foreach ( $title_list as $k => $v ) {
1202 $this->_build_radio( $id, $k, $v );
1203 }
1204
1205 echo '</div>';
1206
1207 $this->_check_overwritten( $id );
1208 }
1209
1210 /**
1211 * Build a radio input and echo it.
1212 *
1213 * @since 1.1.0
1214 * @access private
1215 *
1216 * @param string $id Setting ID.
1217 * @param int|string $val Value for the radio.
1218 * @param string $txt Label HTML.
1219 * @return void
1220 */
1221 private function _build_radio( $id, $val, $txt ) {
1222 $id_attr = 'input_radio_' . preg_replace( '/\W/', '', $id ) . '_' . $val;
1223
1224 $default = isset( self::$_default_options[ $id ] ) ? self::$_default_options[ $id ] : self::$_default_site_options[ $id ];
1225
1226 $is_checked = ! is_string( $default )
1227 ? ( (int) $this->conf( $id, true ) === (int) $val )
1228 : ( $this->conf( $id, true ) === $val );
1229
1230 echo "<input type='radio' autocomplete='off' name='" . esc_attr( $id ) . "' id='" . esc_attr( $id_attr ) . "' value='" . esc_attr( $val ) . "' " . checked( $is_checked, true, false ) . " /> <label for='" . esc_attr( $id_attr ) . "'>" . wp_kses_post( $txt ) . '</label>';
1231 }
1232
1233 /**
1234 * Show overwritten info if value comes from const/primary/filter/server.
1235 *
1236 * @since 3.0
1237 * @since 7.4 Show value from filters. Added type parameter.
1238 *
1239 * @param string $id Setting ID.
1240 * @return void
1241 */
1242 protected function _check_overwritten( $id ) {
1243 $const_val = $this->const_overwritten( $id );
1244 $primary_val = $this->primary_overwritten( $id );
1245 $deprecated_filter_val = $this->deprecated_filter_overwritten( $id );
1246 $filter_val = $this->filter_overwritten( $id );
1247 $server_val = $this->server_overwritten( $id );
1248
1249 if ( null === $const_val && null === $primary_val && null === $deprecated_filter_val && null === $filter_val && null === $server_val ) {
1250 return;
1251 }
1252
1253 // Get value to display.
1254 $val = null !== $const_val ? $const_val : $primary_val;
1255 // If we have deprecated_filter_val will set as new val.
1256 if ( null !== $deprecated_filter_val ) {
1257 $val = $deprecated_filter_val;
1258 }
1259 // If we have filter_val will set as new val.
1260 if ( null !== $filter_val ) {
1261 $val = $filter_val;
1262 }
1263 // If we have server_val will set as new val.
1264 if ( null !== $server_val ) {
1265 $val = $server_val;
1266 }
1267
1268 // Get type (used for display purpose).
1269 $type = ( isset( self::$settings_filters[ $id ] ) && isset( self::$settings_filters[ $id ]['type'] ) ) ? self::$settings_filters[ $id ]['type'] : 'textarea';
1270 if ( ( null !== $const_val || null !== $primary_val || null !== $filter_val ) && null === $deprecated_filter_val ) {
1271 $type = 'setting';
1272 }
1273
1274 // Get default setting: if settings exist, use default setting, otherwise use filter/server value.
1275 $default = '';
1276 if ( isset( self::$_default_options[ $id ] ) || isset( self::$_default_site_options[ $id ] ) ) {
1277 $default = isset( self::$_default_options[ $id ] ) ? self::$_default_options[ $id ] : self::$_default_site_options[ $id ];
1278 }
1279 if ( null !== $deprecated_filter_val || null !== $server_val ) {
1280 $default = null !== $deprecated_filter_val ? $deprecated_filter_val : $server_val;
1281 }
1282
1283 // Set value to display, will be a string.
1284 if ( is_bool( $default ) ) {
1285 $val = $val ? __( 'ON', 'litespeed-cache' ) : __( 'OFF', 'litespeed-cache' );
1286 } else {
1287 if ( is_array( $val ) ) {
1288 $val = implode( "\n", $val );
1289 }
1290 $val = esc_textarea( $val );
1291 }
1292
1293 // Show warning for all types except textarea.
1294 if ( 'textarea' !== $type ) {
1295 echo '<div class="litespeed-desc litespeed-warning litespeed-overwrite">⚠️ ';
1296
1297 if ( null !== $server_val ) {
1298 // Show $_SERVER value.
1299 printf( esc_html__( 'This value is overwritten by the %s variable.', 'litespeed-cache' ), '$_SERVER' );
1300 $val = '$_SERVER["' . $server_val[0] . '"] = ' . $server_val[1];
1301 } elseif ( null !== $deprecated_filter_val ) {
1302 // Show filter value.
1303 echo esc_html__( 'This value is overwritten by the filter.', 'litespeed-cache' );
1304 } elseif ( null !== $const_val ) {
1305 // Show CONSTANT value.
1306 printf( esc_html__( 'This value is overwritten by the PHP constant %s.', 'litespeed-cache' ), '<code>' . esc_html( Base::conf_const( $id ) ) . '</code>' );
1307 } elseif ( is_multisite() ) {
1308 // Show multisite overwrite.
1309 if ( get_current_blog_id() !== BLOG_ID_CURRENT_SITE && $this->conf( self::NETWORK_O_USE_PRIMARY ) ) {
1310 echo esc_html__( 'This value is overwritten by the primary site setting.', 'litespeed-cache' );
1311 } else {
1312 echo esc_html__( 'This value is overwritten by the Network setting.', 'litespeed-cache' );
1313 }
1314 } elseif ( null !== $filter_val ) {
1315 // Show filter value.
1316 echo esc_html__( 'This value is overwritten by the filter.', 'litespeed-cache' );
1317 }
1318
1319 echo ' ' . sprintf( esc_html__( 'Currently set to %s', 'litespeed-cache' ), '<code>' . esc_html( $val ) . '</code>' ) . '</div>';
1320 } elseif ( 'textarea' === $type && null !== $deprecated_filter_val ) {
1321 // Show warning for textarea.
1322 // Textarea sizes.
1323 $cols = 30;
1324 $rows = $this->get_textarea_rows( $val );
1325 $rows_current_val = $this->get_textarea_rows( implode( "\n", $this->conf( $id, true ) ) );
1326 // If filter rows is bigger than textarea size, equalize them.
1327 if ( $rows > $rows_current_val ) {
1328 $rows = $rows_current_val;
1329 }
1330 ?>
1331 <div class="litespeed-desc-wrapper">
1332 <div class="litespeed-desc"><?php echo esc_html__( 'Value from filter applied', 'litespeed-cache' ); ?>:</div>
1333 <textarea readonly rows="<?php echo (int) $rows; ?>" cols="<?php echo (int) $cols; ?>"><?php echo esc_textarea( $val ); ?></textarea>
1334 </div>
1335 <?php
1336 }
1337 }
1338
1339 /**
1340 * Display seconds label and readable span.
1341 *
1342 * @since 3.0
1343 * @return void
1344 */
1345 public function readable_seconds() {
1346 echo esc_html__( 'seconds', 'litespeed-cache' );
1347 echo ' <span data-litespeed-readable=""></span>';
1348 }
1349
1350 /**
1351 * Display default value for a setting.
1352 *
1353 * @since 1.1.1
1354 *
1355 * @param string $id Setting ID.
1356 * @return void
1357 */
1358 public function recommended( $id ) {
1359 if ( ! $this->default_settings ) {
1360 $this->default_settings = $this->load_default_vals();
1361 }
1362
1363 $val = $this->default_settings[ $id ];
1364
1365 if ( ! $val ) {
1366 return;
1367 }
1368
1369 if ( ! is_array( $val ) ) {
1370 printf(
1371 '%s: <code>%s</code>',
1372 esc_html__( 'Default value', 'litespeed-cache' ),
1373 esc_html( $val )
1374 );
1375 return;
1376 }
1377
1378 $rows = 5;
1379 $cols = 30;
1380 // Flexible rows/cols.
1381 $lines = count( $val ) + 1;
1382 $rows = min( max( $lines, $rows ), 40 );
1383 foreach ( $val as $v ) {
1384 $cols = max( strlen( $v ), $cols );
1385 }
1386 $cols = min( $cols, 150 );
1387
1388 $val = implode( "\n", $val );
1389 printf(
1390 '<div class="litespeed-desc">%s:</div><textarea readonly rows="%d" cols="%d">%s</textarea>',
1391 esc_html__( 'Default value', 'litespeed-cache' ),
1392 (int) $rows,
1393 (int) $cols,
1394 esc_textarea( $val )
1395 );
1396 }
1397
1398 /**
1399 * Validate rewrite rules regex syntax.
1400 *
1401 * @since 3.0
1402 *
1403 * @param string $id Setting ID.
1404 * @return void
1405 */
1406 protected function _validate_syntax( $id ) {
1407 $val = $this->conf( $id, true );
1408
1409 if ( ! $val ) {
1410 return;
1411 }
1412
1413 if ( ! is_array( $val ) ) {
1414 $val = [ $val ];
1415 }
1416
1417 foreach ( $val as $v ) {
1418 if ( ! Utility::syntax_checker( $v ) ) {
1419 echo '<br /><span class="litespeed-warning"> ❌ ' . esc_html__( 'Invalid rewrite rule', 'litespeed-cache' ) . ': <code>' . wp_kses_post( $v ) . '</code></span>';
1420 }
1421 }
1422 }
1423
1424 /**
1425 * Validate if the .htaccess path is valid.
1426 *
1427 * @since 3.0
1428 *
1429 * @param string $id Setting ID.
1430 * @return void
1431 */
1432 protected function _validate_htaccess_path( $id ) {
1433 $val = $this->conf( $id, true );
1434 if ( ! $val ) {
1435 return;
1436 }
1437
1438 if ( '/.htaccess' !== substr( $val, -10 ) ) {
1439 echo '<br /><span class="litespeed-warning"> ❌ ' . sprintf( esc_html__( 'Path must end with %s', 'litespeed-cache' ), '<code>/.htaccess</code>' ) . '</span>';
1440 }
1441 }
1442
1443 /**
1444 * Check TTL ranges and show tips.
1445 *
1446 * @since 3.0
1447 *
1448 * @param string $id Setting ID.
1449 * @param int|bool $min Minimum value (or false).
1450 * @param int|bool $max Maximum value (or false).
1451 * @param bool $allow_zero Whether zero is allowed.
1452 * @return void
1453 */
1454 protected function _validate_ttl( $id, $min = false, $max = false, $allow_zero = false ) {
1455 $val = $this->conf( $id, true );
1456
1457 $tip = [];
1458 if ( $min && $val < $min && ( ! $allow_zero || 0 !== $val ) ) {
1459 $tip[] = esc_html__( 'Minimum value', 'litespeed-cache' ) . ': <code>' . $min . '</code>.';
1460 }
1461 if ( $max && $val > $max ) {
1462 $tip[] = esc_html__( 'Maximum value', 'litespeed-cache' ) . ': <code>' . $max . '</code>.';
1463 }
1464
1465 echo '<br />';
1466
1467 if ( $tip ) {
1468 echo '<span class="litespeed-warning"> ❌ ' . wp_kses_post( implode( ' ', $tip ) ) . '</span>';
1469 }
1470
1471 $range = '';
1472
1473 if ( $allow_zero ) {
1474 $range .= esc_html__( 'Zero, or', 'litespeed-cache' ) . ' ';
1475 }
1476
1477 if ( $min && $max ) {
1478 $range .= $min . ' - ' . $max;
1479 } elseif ( $min ) {
1480 $range .= esc_html__( 'Larger than', 'litespeed-cache' ) . ' ' . $min;
1481 } elseif ( $max ) {
1482 $range .= esc_html__( 'Smaller than', 'litespeed-cache' ) . ' ' . $max;
1483 }
1484
1485 echo esc_html__( 'Value range', 'litespeed-cache' ) . ': <code>' . esc_html( $range ) . '</code>';
1486 }
1487
1488 /**
1489 * Validate IPs in a list.
1490 *
1491 * @since 3.0
1492 *
1493 * @param string $id Setting ID.
1494 * @return void
1495 */
1496 protected function _validate_ip( $id ) {
1497 $val = $this->conf( $id, true );
1498 if ( ! $val ) {
1499 return;
1500 }
1501
1502 if ( ! is_array( $val ) ) {
1503 $val = [ $val ];
1504 }
1505
1506 $tip = [];
1507 foreach ( $val as $v ) {
1508 if ( ! $v ) {
1509 continue;
1510 }
1511
1512 if ( ! \WP_Http::is_ip_address( $v ) ) {
1513 $tip[] = esc_html__( 'Invalid IP', 'litespeed-cache' ) . ': <code>' . esc_html( $v ) . '</code>.';
1514 }
1515 }
1516
1517 if ( $tip ) {
1518 echo '<br /><span class="litespeed-warning"> ❌ ' . wp_kses_post( implode( ' ', $tip ) ) . '</span>';
1519 }
1520 }
1521
1522 /**
1523 * Display API environment variable support.
1524 *
1525 * @since 1.8.3
1526 * @access protected
1527 *
1528 * @param string ...$args Server variable names.
1529 * @return void
1530 */
1531 protected function _api_env_var( ...$args ) {
1532 echo '<span class="litespeed-success"> ' .
1533 esc_html__( 'API', 'litespeed-cache' ) . ': ' .
1534 sprintf(
1535 /* translators: %s: list of server variables in <code> tags */
1536 esc_html__( 'Server variable(s) %s available to override this setting.', 'litespeed-cache' ),
1537 '<code>' . implode( '</code>, <code>', array_map( 'esc_html', $args ) ) . '</code>'
1538 ) .
1539 '</span>';
1540
1541 Doc::learn_more( 'https://docs.litespeedtech.com/lscache/lscwp/admin/#limiting-the-crawler' );
1542 }
1543
1544 /**
1545 * Display URI setting example.
1546 *
1547 * @since 2.6.1
1548 * @access protected
1549 * @return void
1550 */
1551 protected function _uri_usage_example() {
1552 echo esc_html__( 'The URLs will be compared to the REQUEST_URI server variable.', 'litespeed-cache' );
1553 /* translators: 1: example URL, 2: pattern example */
1554 echo ' ' . sprintf( esc_html__( 'For example, for %1$s, %2$s can be used here.', 'litespeed-cache' ), '<code>/mypath/mypage?aa=bb</code>', '<code>mypage?aa=</code>' );
1555 echo '<br /><i>';
1556 /* translators: %s: caret symbol */
1557 printf( esc_html__( 'To match the beginning, add %s to the beginning of the item.', 'litespeed-cache' ), '<code>^</code>' );
1558 /* translators: %s: dollar symbol */
1559 echo ' ' . sprintf( esc_html__( 'To do an exact match, add %s to the end of the URL.', 'litespeed-cache' ), '<code>$</code>' );
1560 echo ' ' . esc_html__( 'One per line.', 'litespeed-cache' );
1561 echo '</i>';
1562 }
1563
1564 /**
1565 * Return pluralized strings.
1566 *
1567 * @since 2.0
1568 *
1569 * @param int $num Number.
1570 * @param string $kind Kind of item (group|image).
1571 * @return string
1572 */
1573 public static function print_plural( $num, $kind = 'group' ) {
1574 if ( $num > 1 ) {
1575 switch ( $kind ) {
1576 case 'group':
1577 return sprintf( esc_html__( '%s groups', 'litespeed-cache' ), $num );
1578
1579 case 'image':
1580 return sprintf( esc_html__( '%s images', 'litespeed-cache' ), $num );
1581
1582 default:
1583 return $num;
1584 }
1585 }
1586
1587 switch ( $kind ) {
1588 case 'group':
1589 return sprintf( esc_html__( '%s group', 'litespeed-cache' ), $num );
1590
1591 case 'image':
1592 return sprintf( esc_html__( '%s image', 'litespeed-cache' ), $num );
1593
1594 default:
1595 return $num;
1596 }
1597 }
1598
1599 /**
1600 * Return guidance HTML.
1601 *
1602 * @since 2.0
1603 *
1604 * @param string $title Title HTML.
1605 * @param array<int,string> $steps Steps list (HTML allowed).
1606 * @param int|string $current_step Current step number or 'done'.
1607 * @return string HTML for guidance widget.
1608 */
1609 public static function guidance( $title, $steps, $current_step ) {
1610 if ( 'done' === $current_step ) {
1611 $current_step = count( $steps ) + 1;
1612 }
1613
1614 $percentage = ' (' . floor( ( ( $current_step - 1 ) * 100 ) / count( $steps ) ) . '%)';
1615
1616 $html = '<div class="litespeed-guide"><h2>' . $title . $percentage . '</h2><ol>';
1617 foreach ( $steps as $k => $v ) {
1618 $step = $k + 1;
1619 if ( $current_step > $step ) {
1620 $html .= '<li class="litespeed-guide-done">';
1621 } else {
1622 $html .= '<li>';
1623 }
1624 $html .= $v . '</li>';
1625 }
1626
1627 $html .= '</ol></div>';
1628
1629 return $html;
1630 }
1631
1632 /**
1633 * Check whether has QC hide banner cookie.
1634 *
1635 * @since 7.1
1636 *
1637 * @return bool
1638 */
1639 public static function has_qc_hide_banner() {
1640 return isset( $_COOKIE[ self::COOKIE_QC_HIDE_BANNER ] ) && ( time() - (int) $_COOKIE[ self::COOKIE_QC_HIDE_BANNER ] ) < 86400 * 90;
1641 }
1642
1643 /**
1644 * Set QC hide banner cookie.
1645 *
1646 * @since 7.1
1647 * @return void
1648 */
1649 public static function set_qc_hide_banner() {
1650 $expire = time() + 86400 * 365;
1651 self::debug( 'Set qc hide banner cookie' );
1652 setcookie( self::COOKIE_QC_HIDE_BANNER, time(), $expire, COOKIEPATH, COOKIE_DOMAIN );
1653 }
1654
1655 /**
1656 * Handle all request actions from main cls.
1657 *
1658 * @since 7.1
1659 * @return void
1660 */
1661 public function handler() {
1662 $type = Router::verify_type();
1663
1664 switch ( $type ) {
1665 case self::TYPE_QC_HIDE_BANNER:
1666 self::set_qc_hide_banner();
1667 break;
1668
1669 default:
1670 break;
1671 }
1672
1673 Admin::redirect();
1674 }
1675 }
1676