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
localization.cls.php
197 lines
| 1 | <?php |
| 2 | // phpcs:ignoreFile |
| 3 | |
| 4 | /** |
| 5 | * The localization class. |
| 6 | * |
| 7 | * @since 3.3 |
| 8 | */ |
| 9 | |
| 10 | namespace LiteSpeed; |
| 11 | |
| 12 | defined('WPINC') || exit(); |
| 13 | |
| 14 | class Localization extends Base { |
| 15 | |
| 16 | const LOG_TAG = '🛍️'; |
| 17 | |
| 18 | /** |
| 19 | * Init optimizer |
| 20 | * |
| 21 | * @since 3.0 |
| 22 | * @access protected |
| 23 | */ |
| 24 | public function init() { |
| 25 | add_filter('litespeed_buffer_finalize', array( $this, 'finalize' ), 23); // After page optm |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Localize Resources |
| 30 | * |
| 31 | * @since 3.3 |
| 32 | */ |
| 33 | public function serve_static( $uri ) { |
| 34 | $url = base64_decode($uri); |
| 35 | |
| 36 | if (!$this->conf(self::O_OPTM_LOCALIZE)) { |
| 37 | // wp_redirect( $url ); |
| 38 | exit('Not supported'); |
| 39 | } |
| 40 | |
| 41 | if (substr($url, -3) !== '.js') { |
| 42 | // wp_redirect( $url ); |
| 43 | // exit( 'Not supported ' . $uri ); |
| 44 | } |
| 45 | |
| 46 | $match = false; |
| 47 | $domains = $this->conf(self::O_OPTM_LOCALIZE_DOMAINS); |
| 48 | foreach ($domains as $v) { |
| 49 | if (!$v || strpos($v, '#') === 0) { |
| 50 | continue; |
| 51 | } |
| 52 | |
| 53 | $type = 'js'; |
| 54 | $domain = $v; |
| 55 | // Try to parse space split value |
| 56 | if (strpos($v, ' ')) { |
| 57 | $v = explode(' ', $v); |
| 58 | if (!empty($v[1])) { |
| 59 | $type = strtolower($v[0]); |
| 60 | $domain = $v[1]; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if (strpos($domain, 'https://') !== 0) { |
| 65 | continue; |
| 66 | } |
| 67 | |
| 68 | if ($type != 'js') { |
| 69 | continue; |
| 70 | } |
| 71 | |
| 72 | // if ( strpos( $url, $domain ) !== 0 ) { |
| 73 | if ($url != $domain) { |
| 74 | continue; |
| 75 | } |
| 76 | |
| 77 | $match = true; |
| 78 | break; |
| 79 | } |
| 80 | |
| 81 | if (!$match) { |
| 82 | // wp_redirect( $url ); |
| 83 | exit('Not supported2'); |
| 84 | } |
| 85 | |
| 86 | header('Content-Type: application/javascript'); |
| 87 | |
| 88 | // Generate |
| 89 | $this->_maybe_mk_cache_folder('localres'); |
| 90 | |
| 91 | $file = $this->_realpath($url); |
| 92 | |
| 93 | self::debug('localize [url] ' . $url); |
| 94 | $response = wp_safe_remote_get($url, array( |
| 95 | 'timeout' => 180, |
| 96 | 'stream' => true, |
| 97 | 'filename' => $file, |
| 98 | )); |
| 99 | |
| 100 | // Parse response data |
| 101 | if (is_wp_error($response)) { |
| 102 | $error_message = $response->get_error_message(); |
| 103 | file_exists($file) && unlink($file); |
| 104 | self::debug('failed to get: ' . $error_message); |
| 105 | wp_redirect($url); |
| 106 | exit(); |
| 107 | } |
| 108 | |
| 109 | $url = $this->_rewrite($url); |
| 110 | |
| 111 | wp_redirect($url); |
| 112 | exit(); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Get the final URL of local avatar |
| 117 | * |
| 118 | * @since 4.5 |
| 119 | */ |
| 120 | private function _rewrite( $url ) { |
| 121 | return LITESPEED_STATIC_URL . '/localres/' . $this->_filepath($url); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Generate realpath of the cache file |
| 126 | * |
| 127 | * @since 4.5 |
| 128 | * @access private |
| 129 | */ |
| 130 | private function _realpath( $url ) { |
| 131 | return LITESPEED_STATIC_DIR . '/localres/' . $this->_filepath($url); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Get filepath |
| 136 | * |
| 137 | * @since 4.5 |
| 138 | */ |
| 139 | private function _filepath( $url ) { |
| 140 | $filename = md5($url) . '.js'; |
| 141 | if (is_multisite()) { |
| 142 | $filename = get_current_blog_id() . '/' . $filename; |
| 143 | } |
| 144 | return $filename; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Localize JS/Fonts |
| 149 | * |
| 150 | * @since 3.3 |
| 151 | * @access public |
| 152 | */ |
| 153 | public function finalize( $content ) { |
| 154 | if (is_admin()) { |
| 155 | return $content; |
| 156 | } |
| 157 | |
| 158 | if (!$this->conf(self::O_OPTM_LOCALIZE)) { |
| 159 | return $content; |
| 160 | } |
| 161 | |
| 162 | $domains = $this->conf(self::O_OPTM_LOCALIZE_DOMAINS); |
| 163 | if (!$domains) { |
| 164 | return $content; |
| 165 | } |
| 166 | |
| 167 | foreach ($domains as $v) { |
| 168 | if (!$v || strpos($v, '#') === 0) { |
| 169 | continue; |
| 170 | } |
| 171 | |
| 172 | $type = 'js'; |
| 173 | $domain = $v; |
| 174 | // Try to parse space split value |
| 175 | if (strpos($v, ' ')) { |
| 176 | $v = explode(' ', $v); |
| 177 | if (!empty($v[1])) { |
| 178 | $type = strtolower($v[0]); |
| 179 | $domain = $v[1]; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | if (strpos($domain, 'https://') !== 0) { |
| 184 | continue; |
| 185 | } |
| 186 | |
| 187 | if ($type != 'js') { |
| 188 | continue; |
| 189 | } |
| 190 | |
| 191 | $content = str_replace($domain, LITESPEED_STATIC_URL . '/localres/' . base64_encode($domain), $content); |
| 192 | } |
| 193 | |
| 194 | return $content; |
| 195 | } |
| 196 | } |
| 197 |