* The localization class.
defined('WPINC') || exit();
class Localization extends Base {
add_filter('litespeed_buffer_finalize', array( $this, 'finalize' ), 23); // After page optm
public function serve_static( $uri ) {
$url = base64_decode($uri);
if (!$this->conf(self::O_OPTM_LOCALIZE)) {
if (substr($url, -3) !== '.js') {
// exit( 'Not supported ' . $uri );
$domains = $this->conf(self::O_OPTM_LOCALIZE_DOMAINS);
foreach ($domains as $v) {
if (!$v || strpos($v, '#') === 0) {
// Try to parse space split value
$type = strtolower($v[0]);
if (strpos($domain, 'https://') !== 0) {
// if ( strpos( $url, $domain ) !== 0 ) {
header('Content-Type: application/javascript');
$this->_maybe_mk_cache_folder('localres');
$file = $this->_realpath($url);
self::debug('localize [url] ' . $url);
$response = wp_safe_remote_get($url, array(
if (is_wp_error($response)) {
$error_message = $response->get_error_message();
file_exists($file) && unlink($file);
self::debug('failed to get: ' . $error_message);
$url = $this->_rewrite($url);
* Get the final URL of local avatar
private function _rewrite( $url ) {
return LITESPEED_STATIC_URL . '/localres/' . $this->_filepath($url);
* Generate realpath of the cache file
private function _realpath( $url ) {
return LITESPEED_STATIC_DIR . '/localres/' . $this->_filepath($url);
private function _filepath( $url ) {
$filename = md5($url) . '.js';
$filename = get_current_blog_id() . '/' . $filename;
public function finalize( $content ) {
if (!$this->conf(self::O_OPTM_LOCALIZE)) {
$domains = $this->conf(self::O_OPTM_LOCALIZE_DOMAINS);
foreach ($domains as $v) {
if (!$v || strpos($v, '#') === 0) {
// Try to parse space split value
$type = strtolower($v[0]);
if (strpos($domain, 'https://') !== 0) {
$content = str_replace($domain, LITESPEED_STATIC_URL . '/localres/' . base64_encode($domain), $content);