litespeed-cache
Last commit date
assets
2 months ago
cli
2 months ago
data
2 months ago
lang
2 months ago
lib
2 months ago
src
2 months ago
thirdparty
2 months ago
tpl
2 months ago
LICENSE
2 months ago
autoload.php
2 months ago
changelog.txt
2 months ago
composer.json
2 months ago
composer.lock
2 months ago
guest.vary.php
2 months ago
litespeed-cache.php
2 months ago
package-lock.json
2 months ago
package.json
2 months ago
phpcs.ruleset.xml
2 months ago
phpcs.xml.dist
2 months ago
qc-ping.txt
2 months ago
readme.txt
2 months ago
security.md
2 months ago
typos.toml
2 months ago
litespeed-cache.php
216 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Plugin Name: LiteSpeed Cache |
| 4 | * Plugin URI: https://www.litespeedtech.com/products/cache-plugins/wordpress-acceleration |
| 5 | * Description: High-performance page caching and site optimization from LiteSpeed |
| 6 | * Version: 7.8.1 |
| 7 | * Author: LiteSpeed Technologies |
| 8 | * Author URI: https://www.litespeedtech.com |
| 9 | * License: GPLv3 |
| 10 | * License URI: https://www.gnu.org/licenses/gpl-3.0.html |
| 11 | * Text Domain: litespeed-cache |
| 12 | * Domain Path: /lang |
| 13 | * |
| 14 | * @package LiteSpeed |
| 15 | * |
| 16 | * Copyright (C) 2015-2026 LiteSpeed Technologies, Inc. |
| 17 | * |
| 18 | * This program is free software: you can redistribute it and/or modify |
| 19 | * it under the terms of the GNU General Public License as published by |
| 20 | * the Free Software Foundation, either version 3 of the License, or |
| 21 | * (at your option) any later version. |
| 22 | * |
| 23 | * This program is distributed in the hope that it will be useful, |
| 24 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 26 | * GNU General Public License for more details. |
| 27 | * |
| 28 | * You should have received a copy of the GNU General Public License |
| 29 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 30 | */ |
| 31 | |
| 32 | defined( 'WPINC' ) || exit(); |
| 33 | |
| 34 | if ( defined( 'LSCWP_V' ) ) { |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | ! defined( 'LSCWP_V' ) && define( 'LSCWP_V', '7.8.1' ); |
| 39 | |
| 40 | ! defined( 'LSCWP_CONTENT_DIR' ) && define( 'LSCWP_CONTENT_DIR', WP_CONTENT_DIR ); |
| 41 | ! defined( 'LSCWP_DIR' ) && define( 'LSCWP_DIR', __DIR__ . '/' ); // Full absolute path '/var/www/html/***/wp-content/plugins/litespeed-cache/' or MU |
| 42 | ! defined( 'LSCWP_BASENAME' ) && define( 'LSCWP_BASENAME', 'litespeed-cache/litespeed-cache.php' ); // LSCWP_BASENAME='litespeed-cache/litespeed-cache.php' |
| 43 | |
| 44 | /** |
| 45 | * This needs to be before activation because admin-rules.class.php need const `LSCWP_CONTENT_FOLDER` |
| 46 | * This also needs to be before cfg.cls init because default cdn_included_dir needs `LSCWP_CONTENT_FOLDER` |
| 47 | * |
| 48 | * @since 5.2 Auto correct protocol for CONTENT URL |
| 49 | */ |
| 50 | $wp_content_url = WP_CONTENT_URL; |
| 51 | $site_url = site_url( '/' ); |
| 52 | if ( 'http:' === substr( $wp_content_url, 0, 5 ) && 'https' === substr( $site_url, 0, 5 ) ) { |
| 53 | $wp_content_url = str_replace( 'http://', 'https://', $wp_content_url ); |
| 54 | } |
| 55 | ! defined( 'LSCWP_CONTENT_FOLDER' ) && define( 'LSCWP_CONTENT_FOLDER', str_replace( $site_url, '', $wp_content_url ) ); // `wp-content` |
| 56 | unset( $site_url ); |
| 57 | ! defined( 'LSWCP_PLUGIN_URL' ) && define( 'LSWCP_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); // Full URL path '//example.com/wp-content/plugins/litespeed-cache/' |
| 58 | |
| 59 | /** |
| 60 | * Static cache files consts |
| 61 | * |
| 62 | * @since 3.0 |
| 63 | */ |
| 64 | ! defined( 'LITESPEED_DATA_FOLDER' ) && define( 'LITESPEED_DATA_FOLDER', 'litespeed' ); |
| 65 | ! defined( 'LITESPEED_STATIC_URL' ) && define( 'LITESPEED_STATIC_URL', $wp_content_url . '/' . LITESPEED_DATA_FOLDER ); // Full static cache folder URL '//example.com/wp-content/litespeed' |
| 66 | unset( $wp_content_url ); |
| 67 | ! defined( 'LITESPEED_STATIC_DIR' ) && define( 'LITESPEED_STATIC_DIR', LSCWP_CONTENT_DIR . '/' . LITESPEED_DATA_FOLDER ); // Full static cache folder path '/var/www/html/***/wp-content/litespeed' |
| 68 | |
| 69 | ! defined( 'LITESPEED_TIME_OFFSET' ) && define( 'LITESPEED_TIME_OFFSET', get_option( 'gmt_offset' ) * 60 * 60 ); |
| 70 | |
| 71 | // Placeholder for lazyload img |
| 72 | ! defined( 'LITESPEED_PLACEHOLDER' ) && define( 'LITESPEED_PLACEHOLDER', 'data:image/gif;base64,R0lGODdhAQABAPAAAMPDwwAAACwAAAAAAQABAAACAkQBADs=' ); |
| 73 | |
| 74 | // Auto register LiteSpeed classes |
| 75 | require_once LSCWP_DIR . 'autoload.php'; |
| 76 | |
| 77 | // Define CLI |
| 78 | if ( ( defined( 'WP_CLI' ) && constant('WP_CLI') ) || 'cli' === PHP_SAPI ) { |
| 79 | ! defined( 'LITESPEED_CLI' ) && define( 'LITESPEED_CLI', true ); |
| 80 | |
| 81 | // Register CLI cmd |
| 82 | if ( method_exists( 'WP_CLI', 'add_command' ) ) { |
| 83 | WP_CLI::add_command( 'litespeed-option', 'LiteSpeed\CLI\Option' ); |
| 84 | WP_CLI::add_command( 'litespeed-purge', 'LiteSpeed\CLI\Purge' ); |
| 85 | WP_CLI::add_command( 'litespeed-online', 'LiteSpeed\CLI\Online' ); |
| 86 | WP_CLI::add_command( 'litespeed-image', 'LiteSpeed\CLI\Image' ); |
| 87 | WP_CLI::add_command( 'litespeed-debug', 'LiteSpeed\CLI\Debug' ); |
| 88 | WP_CLI::add_command( 'litespeed-presets', 'LiteSpeed\CLI\Presets' ); |
| 89 | WP_CLI::add_command( 'litespeed-crawler', 'LiteSpeed\CLI\Crawler' ); |
| 90 | WP_CLI::add_command( 'litespeed-database', 'LiteSpeed\CLI\Database' ); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // Server type |
| 95 | if ( ! defined( 'LITESPEED_SERVER_TYPE' ) ) { |
| 96 | $http_x_lscache = isset( $_SERVER['HTTP_X_LSCACHE'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_LSCACHE'] ) ) : ''; |
| 97 | $lsws_edition = isset( $_SERVER['LSWS_EDITION'] ) ? sanitize_text_field( wp_unslash( $_SERVER['LSWS_EDITION'] ) ) : ''; |
| 98 | $server_software = isset( $_SERVER['SERVER_SOFTWARE'] ) ? sanitize_text_field( wp_unslash( $_SERVER['SERVER_SOFTWARE'] ) ) : ''; |
| 99 | |
| 100 | if ( $http_x_lscache ) { |
| 101 | define( 'LITESPEED_SERVER_TYPE', 'LITESPEED_SERVER_ADC' ); |
| 102 | } elseif ( 0 === strpos( $lsws_edition, 'Openlitespeed' ) ) { |
| 103 | define( 'LITESPEED_SERVER_TYPE', 'LITESPEED_SERVER_OLS' ); |
| 104 | } elseif ( 'LiteSpeed' === $server_software ) { |
| 105 | define( 'LITESPEED_SERVER_TYPE', 'LITESPEED_SERVER_ENT' ); |
| 106 | } else { |
| 107 | define( 'LITESPEED_SERVER_TYPE', 'NONE' ); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // Checks if caching is allowed via server variable |
| 112 | if ( ! empty( $_SERVER['X-LSCACHE'] ) || 'LITESPEED_SERVER_ADC' === LITESPEED_SERVER_TYPE || defined( 'LITESPEED_CLI' ) ) { |
| 113 | ! defined( 'LITESPEED_ALLOWED' ) && define( 'LITESPEED_ALLOWED', true ); |
| 114 | } |
| 115 | |
| 116 | // ESI const definition |
| 117 | if ( ! defined( 'LSWCP_ESI_SUPPORT' ) ) { |
| 118 | define( 'LSWCP_ESI_SUPPORT', LITESPEED_SERVER_TYPE !== 'LITESPEED_SERVER_OLS' ); |
| 119 | } |
| 120 | |
| 121 | if ( ! defined( 'LSWCP_TAG_PREFIX' ) ) { |
| 122 | define( 'LSWCP_TAG_PREFIX', substr( md5( LSCWP_DIR ), -3 ) ); |
| 123 | } |
| 124 | |
| 125 | if ( ! function_exists( 'litespeed_exception_handler' ) ) { |
| 126 | /** |
| 127 | * Handle exception |
| 128 | * |
| 129 | * @param int $errno Error number. |
| 130 | * @param string $errstr Error string. |
| 131 | * @param string $errfile Error file. |
| 132 | * @param int $errline Error line. |
| 133 | * @throws \ErrorException When an error is encountered. |
| 134 | */ |
| 135 | function litespeed_exception_handler( $errno, $errstr, $errfile, $errline ) { |
| 136 | throw new \ErrorException( |
| 137 | esc_html( $errstr ), |
| 138 | 0, |
| 139 | absint( $errno ), |
| 140 | esc_html( $errfile ), |
| 141 | absint( $errline ) |
| 142 | ); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | if ( ! function_exists( 'litespeed_define_nonce_func' ) ) { |
| 147 | /** |
| 148 | * Overwrite the WP nonce funcs outside of LiteSpeed namespace |
| 149 | * |
| 150 | * @since 3.0 |
| 151 | */ |
| 152 | function litespeed_define_nonce_func() { |
| 153 | /** |
| 154 | * If the nonce is in none_actions filter, convert it to ESI |
| 155 | * |
| 156 | * @param mixed $action Action name or -1. |
| 157 | * @return string |
| 158 | */ |
| 159 | function wp_create_nonce( $action = -1 ) { |
| 160 | if ( ! defined( 'LITESPEED_DISABLE_ALL' ) || ! LITESPEED_DISABLE_ALL ) { |
| 161 | $control = \LiteSpeed\ESI::cls()->is_nonce_action( $action ); |
| 162 | if ( null !== $control ) { |
| 163 | $params = array( |
| 164 | 'action' => $action, |
| 165 | ); |
| 166 | return \LiteSpeed\ESI::cls()->sub_esi_block( 'nonce', 'wp_create_nonce ' . $action, $params, $control, true, true, true ); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | return wp_create_nonce_litespeed_esi( $action ); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Ori WP wp_create_nonce |
| 175 | * |
| 176 | * @param mixed $action Action name or -1. |
| 177 | * @return string |
| 178 | */ |
| 179 | function wp_create_nonce_litespeed_esi( $action = -1 ) { |
| 180 | $uid = get_current_user_id(); |
| 181 | if ( ! $uid ) { |
| 182 | /** This filter is documented in wp-includes/pluggable.php */ |
| 183 | $uid = apply_filters( 'nonce_user_logged_out', $uid, $action ); |
| 184 | } |
| 185 | |
| 186 | $token = wp_get_session_token(); |
| 187 | $i = wp_nonce_tick(); |
| 188 | |
| 189 | return substr( wp_hash( $i . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 ); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | if ( ! function_exists( 'run_litespeed_cache' ) ) { |
| 195 | /** |
| 196 | * Begins execution of the plugin. |
| 197 | * |
| 198 | * @since 1.0.0 |
| 199 | */ |
| 200 | function run_litespeed_cache() { |
| 201 | // Check minimum PHP requirements, which is 7.2 at the moment. |
| 202 | if ( version_compare( PHP_VERSION, '7.2.0', '<' ) ) { |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | // Check minimum WP requirements, which is 5.3 at the moment. |
| 207 | if ( version_compare( $GLOBALS['wp_version'], '5.3', '<' ) ) { |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | \LiteSpeed\Core::cls(); |
| 212 | } |
| 213 | |
| 214 | run_litespeed_cache(); |
| 215 | } |
| 216 |