cdn
7 months ago
data_structure
7 months ago
activation.cls.php
7 months ago
admin-display.cls.php
7 months ago
admin-settings.cls.php
7 months ago
admin.cls.php
7 months ago
api.cls.php
7 months ago
avatar.cls.php
7 months ago
base.cls.php
7 months ago
cdn.cls.php
7 months ago
cloud.cls.php
7 months ago
conf.cls.php
7 months ago
control.cls.php
7 months ago
core.cls.php
7 months ago
crawler-map.cls.php
7 months ago
crawler.cls.php
7 months ago
css.cls.php
7 months ago
data.cls.php
7 months ago
data.upgrade.func.php
7 months ago
db-optm.cls.php
7 months ago
debug2.cls.php
7 months ago
doc.cls.php
7 months ago
error.cls.php
7 months ago
esi.cls.php
7 months ago
file.cls.php
7 months ago
gui.cls.php
7 months ago
health.cls.php
7 months ago
htaccess.cls.php
7 months ago
img-optm.cls.php
7 months ago
import.cls.php
7 months ago
import.preset.cls.php
7 months ago
lang.cls.php
7 months ago
localization.cls.php
7 months ago
media.cls.php
7 months ago
metabox.cls.php
7 months ago
object-cache-wp.cls.php
7 months ago
object-cache.cls.php
7 months ago
object.lib.php
7 months ago
optimize.cls.php
7 months ago
optimizer.cls.php
7 months ago
placeholder.cls.php
7 months ago
purge.cls.php
7 months ago
report.cls.php
7 months ago
rest.cls.php
7 months ago
root.cls.php
7 months ago
router.cls.php
7 months ago
str.cls.php
7 months ago
tag.cls.php
7 months ago
task.cls.php
7 months ago
tool.cls.php
7 months ago
ucss.cls.php
7 months ago
utility.cls.php
7 months ago
vary.cls.php
7 months ago
vpi.cls.php
7 months ago
tool.cls.php
167 lines
| 1 | <?php |
| 2 | // phpcs:ignoreFile |
| 3 | /** |
| 4 | * The tools |
| 5 | * |
| 6 | * @since 3.0 |
| 7 | * @package LiteSpeed |
| 8 | */ |
| 9 | |
| 10 | namespace LiteSpeed; |
| 11 | |
| 12 | defined( 'WPINC' ) || exit(); |
| 13 | |
| 14 | /** |
| 15 | * Class Tool |
| 16 | * |
| 17 | * Provides utility functions for LiteSpeed Cache, including IP detection and heartbeat control. |
| 18 | * |
| 19 | * @since 3.0 |
| 20 | */ |
| 21 | class Tool extends Root { |
| 22 | |
| 23 | const LOG_TAG = '[Tool]'; |
| 24 | |
| 25 | /** |
| 26 | * Get public IP |
| 27 | * |
| 28 | * Retrieves the public IP address of the server. |
| 29 | * |
| 30 | * @since 3.0 |
| 31 | * @access public |
| 32 | * @return string The public IP address or an error message. |
| 33 | */ |
| 34 | public function check_ip() { |
| 35 | self::debug( '� |
| 36 | check_ip' ); |
| 37 | |
| 38 | $response = wp_safe_remote_get( 'https://cyberpanel.sh/?ip', array( |
| 39 | 'headers' => array( |
| 40 | 'User-Agent' => 'curl/8.7.1', |
| 41 | ), |
| 42 | ) ); |
| 43 | |
| 44 | if ( is_wp_error( $response ) ) { |
| 45 | return esc_html__( 'Failed to detect IP', 'litespeed-cache' ); |
| 46 | } |
| 47 | |
| 48 | $ip = trim( $response['body'] ); |
| 49 | |
| 50 | self::debug( 'result [ip] ' . $ip ); |
| 51 | |
| 52 | if ( Utility::valid_ipv4( $ip ) ) { |
| 53 | return $ip; |
| 54 | } |
| 55 | |
| 56 | return esc_html__( 'Failed to detect IP', 'litespeed-cache' ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Heartbeat Control |
| 61 | * |
| 62 | * Configures WordPress heartbeat settings for frontend, backend, and editor. |
| 63 | * |
| 64 | * @since 3.0 |
| 65 | * @access public |
| 66 | */ |
| 67 | public function heartbeat() { |
| 68 | add_action( 'wp_enqueue_scripts', array( $this, 'heartbeat_frontend' ) ); |
| 69 | add_action( 'admin_enqueue_scripts', array( $this, 'heartbeat_backend' ) ); |
| 70 | add_filter( 'heartbeat_settings', array( $this, 'heartbeat_settings' ) ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Heartbeat Control frontend control |
| 75 | * |
| 76 | * Manages heartbeat settings for the frontend. |
| 77 | * |
| 78 | * @since 3.0 |
| 79 | * @access public |
| 80 | */ |
| 81 | public function heartbeat_frontend() { |
| 82 | if ( ! $this->conf( Base::O_MISC_HEARTBEAT_FRONT ) ) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | if ( ! $this->conf( Base::O_MISC_HEARTBEAT_FRONT_TTL ) ) { |
| 87 | wp_deregister_script( 'heartbeat' ); |
| 88 | Debug2::debug( '[Tool] Deregistered frontend heartbeat' ); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Heartbeat Control backend control |
| 94 | * |
| 95 | * Manages heartbeat settings for the backend and editor. |
| 96 | * |
| 97 | * @since 3.0 |
| 98 | * @access public |
| 99 | */ |
| 100 | public function heartbeat_backend() { |
| 101 | if ( $this->is_editor() ) { |
| 102 | if ( ! $this->conf( Base::O_MISC_HEARTBEAT_EDITOR ) ) { |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | if ( ! $this->conf( Base::O_MISC_HEARTBEAT_EDITOR_TTL ) ) { |
| 107 | wp_deregister_script( 'heartbeat' ); |
| 108 | Debug2::debug( '[Tool] Deregistered editor heartbeat' ); |
| 109 | } |
| 110 | } else { |
| 111 | if ( ! $this->conf( Base::O_MISC_HEARTBEAT_BACK ) ) { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | if ( ! $this->conf( Base::O_MISC_HEARTBEAT_BACK_TTL ) ) { |
| 116 | wp_deregister_script( 'heartbeat' ); |
| 117 | Debug2::debug( '[Tool] Deregistered backend heartbeat' ); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Heartbeat Control settings |
| 124 | * |
| 125 | * Adjusts heartbeat interval settings based on configuration. |
| 126 | * |
| 127 | * @since 3.0 |
| 128 | * @access public |
| 129 | * @param array $settings Existing heartbeat settings. |
| 130 | * @return array Modified heartbeat settings. |
| 131 | */ |
| 132 | public function heartbeat_settings( $settings ) { |
| 133 | // Check editor first to make frontend editor valid too |
| 134 | if ( $this->is_editor() ) { |
| 135 | if ( $this->conf( Base::O_MISC_HEARTBEAT_EDITOR ) ) { |
| 136 | $settings['interval'] = $this->conf( Base::O_MISC_HEARTBEAT_EDITOR_TTL ); |
| 137 | Debug2::debug( '[Tool] Heartbeat interval set to ' . $this->conf( Base::O_MISC_HEARTBEAT_EDITOR_TTL ) ); |
| 138 | } |
| 139 | } elseif ( ! is_admin() ) { |
| 140 | if ( $this->conf( Base::O_MISC_HEARTBEAT_FRONT ) ) { |
| 141 | $settings['interval'] = $this->conf( Base::O_MISC_HEARTBEAT_FRONT_TTL ); |
| 142 | Debug2::debug( '[Tool] Heartbeat interval set to ' . $this->conf( Base::O_MISC_HEARTBEAT_FRONT_TTL ) ); |
| 143 | } |
| 144 | } elseif ( $this->conf( Base::O_MISC_HEARTBEAT_BACK ) ) { |
| 145 | $settings['interval'] = $this->conf( Base::O_MISC_HEARTBEAT_BACK_TTL ); |
| 146 | Debug2::debug( '[Tool] Heartbeat interval set to ' . $this->conf( Base::O_MISC_HEARTBEAT_BACK_TTL ) ); |
| 147 | } |
| 148 | return $settings; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Check if in editor |
| 153 | * |
| 154 | * Determines if the current request is within the WordPress editor. |
| 155 | * |
| 156 | * @since 3.0 |
| 157 | * @access public |
| 158 | * @return bool True if in editor, false otherwise. |
| 159 | */ |
| 160 | public function is_editor() { |
| 161 | $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; |
| 162 | $res = is_admin() && Utility::str_hit_array( $request_uri, array( 'post.php', 'post-new.php' ) ); |
| 163 | |
| 164 | return apply_filters( 'litespeed_is_editor', $res ); |
| 165 | } |
| 166 | } |
| 167 |