PluginProbe
RabbitLoader / 3.2.0
RabbitLoader v3.2.0
4.0.2 4.0.1 4.0 3.2.0 3.1.1 3.1.0 3.0.5 3.0.3 3.0.4 2.17.1 2.17.2 2.17.3 2.17.4 2.17.5 2.17.6 2.17.7 2.18.0 2.18.1 2.18.2 2.18.3 2.18.4 2.18.5 2.18.6 2.18.7 2.18.8 All 129 releases
rabbit-loader / inc / pub_cdn.php

pub_cdn.php in RabbitLoader 3.2.0, at inc/pub_cdn.php

86 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class RabbitLoader_21_CDN
4 {
5
6 public $cdn_prefix = '';
7 public $site_host = '';
8 private $replaced_scripts = [];
9 private $excluded_handlers = ['amelia'];
10 private static $excluded_srs = [RabbitLoader_21_Public::skip_reason_ep, RabbitLoader_21_Public::skip_reason_us];
11
12 public static function init()
13 {
14 if (!function_exists('is_admin') || is_admin() || RL21UtilWP::is_login_page()) {
15 return;
16 }
17 $sr = RabbitLoader_21_Public::getSkipReason();
18 if (in_array($sr, self::$excluded_srs)) {
19 //excluded pages or RL origin pages
20 return;
21 }
22
23 $cdn_prefix = get_option('rabbitloader_cdn_prefix');
24 if (empty($cdn_prefix)) {
25 return;
26 }
27
28 $cdn = new RabbitLoader_21_CDN();
29 $cdn->cdn_prefix = $cdn_prefix;
30 $cdn->site_host = parse_url(get_site_url())['host'];
31
32 add_filter('script_loader_tag', [$cdn, 'rollback'], 99, 3);
33 add_filter('script_loader_src', [$cdn, 'replaceAssetHost'], 10, 2);
34 add_filter('style_loader_src', [$cdn, 'replaceAssetHost'], 10, 2);
35 add_filter('wp_get_attachment_url', [$cdn, 'replaceAssetHost'], 10, 2);
36 add_filter('wp_calculate_image_srcset', function ($sources) use ($cdn) {
37 foreach ($sources as &$source) {
38 if (!empty($source['url'])) {
39 $source['url'] = $cdn->replaceAssetHost($source['url'], '');
40 }
41 }
42 return $sources;
43 }, 10, 1);
44 }
45
46 public function replaceAssetHost($original_src, $handle_or_attachment_id)
47 {
48 if (empty($this->cdn_prefix) || stripos($original_src, ';')) {
49 return $original_src;
50 }
51 foreach ($this->excluded_handlers as $h) {
52 if (stripos($handle_or_attachment_id, $h) !== false) {
53 return $original_src;
54 }
55 }
56 $parsed_url = parse_url($original_src);
57 $sameHost = !empty($parsed_url['host']) && (strcasecmp($parsed_url['host'], $this->site_host) == 0);
58 $noHost = empty($parsed_url['host']);
59 if ($noHost || $sameHost) {
60 $parsed_url['host'] = $this->cdn_prefix;
61 $parsed_url['scheme'] = "https";
62 $new_src = sprintf(
63 '%s://%s%s%s%s',
64 $parsed_url['scheme'],
65 $parsed_url['host'],
66 $parsed_url['path'],
67 !empty($parsed_url['query']) ? '?' . $parsed_url['query'] : '',
68 !empty($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : ''
69 );
70 $this->replaced_scripts[$new_src] = $original_src;
71 return $new_src;
72 }
73 return $original_src;
74 }
75
76 public function rollback($tag, $handle, $new_src)
77 {
78 if (!empty($new_src) && stripos($tag, "module") !== false && !empty($this->replaced_scripts[$new_src])) {
79 //rollback
80 $count = 0;
81 return str_replace($new_src, $this->replaced_scripts[$new_src], $tag, $count);
82 }
83 return $tag;
84 }
85 }
86