PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / 1.1.2
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score v1.1.2
trunk 1.0 1.0.1 1.1 1.1.1 1.1.2 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1 2.1.1 2.1.2 2.2 2.2.1 All 69 releases
powered-cache / includes / class-powered-cache-cdn.php

class-powered-cache-cdn.php in Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score 1.1.2, at includes/class-powered-cache-cdn.php

194 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 class Powered_Cache_CDN {
8
9 /**
10 * Return an instance of the current class
11 *
12 * @since 1.0
13 * @return Powered_Cache_CDN
14 */
15 public static function factory() {
16 static $instance = false;
17
18 if ( ! $instance ) {
19 $instance = new self();
20 $instance->setup();
21 }
22
23 return $instance;
24 }
25
26 /**
27 * Setup hooks
28 *
29 * @since 1.0
30 */
31 public function setup() {
32
33 add_filter( 'wp_get_attachment_url', array( $this, 'cdn_url' ), 9999 );
34 add_filter( 'stylesheet_uri', array( $this, 'cdn_url' ), 9999 );
35 add_filter( 'smilies_src', array( $this, 'cdn_url' ), 9999 );
36 add_filter( 'bp_core_fetch_avatar_url', array( $this, 'cdn_url' ), 9999 );
37 add_filter( 'style_loader_src', array( $this, 'cdn_url' ), 9999 );
38 add_filter( 'script_loader_src', array( $this, 'cdn_url' ), 9999 );
39
40
41 add_filter( 'the_content', array( $this, 'cdn_images' ), 9999 );
42 add_filter( 'post_thumbnail_html', array( $this, 'cdn_images' ), 9999 );
43 add_filter( 'get_avatar', array( $this, 'cdn_images' ), 9999 );
44 add_filter( 'bp_core_fetch_avatar', array( $this, 'cdn_images' ), 9999 );
45 add_filter( 'widget_text', array( $this, 'cdn_images' ), 9999 );
46
47
48 do_action( 'powered_cache_cdn_setup' );
49 }
50
51
52 public function cdn_url( $url ) {
53 if ( is_admin() || is_preview() ) {
54 return $url;
55 }
56
57 return $this->maybe_cdn_replace( $url );
58 }
59
60
61
62 public function cdn_images( $content ) {
63 if ( is_admin() || is_preview() || empty( $content ) ) {
64 return $content;
65 }
66
67 if ( ! class_exists( 'DOMDocument' ) ) {
68 return $content;
69 }
70
71 $document = new DOMDocument();
72 @$document->loadHTML($content);
73 $images = $document->getElementsByTagName('img');
74
75 $img_url = array();
76 foreach ( $images as $img ) {
77 if ( $img->hasAttribute( 'src' ) ) {
78 $img_url[] = $img->getAttribute( 'src' );
79 }
80
81 if ( $img->hasAttribute( 'srcset' ) ) {
82 $imgset = explode( ',', $img->getAttribute( 'srcset' ) );
83 foreach ( $imgset as $src_item ) {
84 $imgsrc = explode( ' ', trim( $src_item ) );
85 // first item is url, second width
86 $img_url[] = $imgsrc[0];
87 }
88 }
89 }
90
91 $img_url = array_unique($img_url);
92 $cdn_url = array();
93
94 foreach ( $img_url as $replace_url ) {
95 $cdn_url[] = $this->maybe_cdn_replace( $replace_url );
96 }
97
98
99 return str_replace($img_url,$cdn_url,$content);
100 }
101
102
103 /**
104 * this method decide to url replacement.
105 * rejected files & external resources will be ignored
106 *
107 * @since 1.0
108 *
109 * @param string $url
110 * @return string cdn url
111 */
112 public static function maybe_cdn_replace( $url ) {
113 global $powered_cache_options;
114
115 $raw_url = $url;
116
117 // rejected file
118 if ( ! empty( $powered_cache_options['cdn_rejected_files'] ) ) {
119 $cdn_rejected_files = preg_split( '#(\r\n|\r|\n)#', $powered_cache_options['cdn_rejected_files'] );
120 $cdn_rejected_files = implode( '|', $cdn_rejected_files );
121
122 if ( preg_match( '#(' . $cdn_rejected_files . ')#', $url ) ) {
123 return $url;
124 }
125 }
126
127 // external resource
128 if ( false === strpos( $url, site_url() ) ) {
129 return $url;
130 }
131
132 // clean version string
133 if ( strpos( $url, '?ver=' ) ) {
134 $raw_url = remove_query_arg( 'ver', $url );
135 }
136
137 $raw_url = untrailingslashit( $raw_url );
138 $ext = explode( '.', $raw_url );
139 $ext = strtolower( end( $ext ) );
140
141 $zone = 'all';
142 $image_extensions = apply_filters( 'powered_cache_cdn_image_extensions', array( 'jpg', 'jpeg', 'gif', 'png', 'bmp', 'ico' ) );
143
144 if ( in_array( $ext, $image_extensions ) ) {
145 $zone = 'image';
146 } elseif ( 'css' == $ext ) {
147 $zone = 'css';
148 } elseif ( 'js' == $ext ) {
149 $zone = 'js';
150 }
151
152 $cdn_url = self::get_best_possible_cdn_host( $zone );
153
154 // no host found
155 if ( false === $cdn_url ) {
156 return $url;
157 }
158
159 return str_replace( site_url(), $cdn_url, $url );
160 }
161
162 /**
163 * try to catch best cdn address to given zone
164 * @param string $zone keys of Powered_Cache_Admin_Helper::cdn_zones
165 *
166 * @return mixed string | false
167 */
168 public static function get_best_possible_cdn_host( $zone = 'all' ) {
169 global $powered_cache_cdn_addresses;
170
171 if ( ! isset( $powered_cache_cdn_addresses ) ) {
172 $powered_cache_cdn_addresses = powered_cache_cdn_addresses();
173 }
174
175 if ( isset( $powered_cache_cdn_addresses[ $zone ] ) && is_array( $powered_cache_cdn_addresses[ $zone ] ) ) {
176 // if we have multiple host for the same resource get randomly
177 $random_key = array_rand( $powered_cache_cdn_addresses[ $zone ] );
178
179 return $powered_cache_cdn_addresses[ $zone ][ $random_key ];
180 }
181
182 // fallback to primary host
183 if ( isset( $powered_cache_cdn_addresses['all'] ) && is_array( $powered_cache_cdn_addresses['all'] ) ) {
184 $random_key = array_rand( $powered_cache_cdn_addresses['all'] );
185
186 return $powered_cache_cdn_addresses['all'][ $random_key ];
187 }
188
189 return false;
190 }
191
192 }
193
194