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