PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / trunk
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score vtrunk
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
← All changes | includes/classes/CDN.php +23 -6 2.2trunk View file →
@@ -47,8 +47,16 @@
47 47 if ( ! $settings['enable_cdn'] ) {
48 48 return;
49 49 }
50 50
51 + add_action( 'plugins_loaded', [ $this, 'cdn_setup' ] );
52 +
53 + }
54 +
55 + /**
56 + * Setup CDN
57 + */
58 + public function cdn_setup() {
51 59 /**
52 60 * Filters CDN integration
53 61 *
54 62 * @hook powered_cache_cdn_disable
@@ -82,9 +90,9 @@
82 90 *
83 91 * @since 2.2
84 92 */
85 93 public function start_buffer() {
86 - ob_start( 'self::end_buffering' );
94 + ob_start( [ '\PoweredCache\CDN', 'end_buffering' ] );
87 95 }
88 96
89 97 /**
90 98 * Replace origin URLs with CDN.
@@ -119,8 +127,13 @@
119 127 if ( ! isset( $_SERVER['REQUEST_METHOD'] ) || 'GET' !== $_SERVER['REQUEST_METHOD'] ) {
120 128 return true;
121 129 }
122 130
131 + // Skip CDN integration for Block Editor requests, particularly when using the media endpoint for in-editor image resizing/manipulation.
132 + if ( isset( $_SERVER['HTTP_X_WP_NONCE'] ) && wp_is_json_request() ) {
133 + return true;
134 + }
135 +
123 136 // check conditional tags
124 137 if ( is_admin() || is_trackback() || is_robots() || is_preview() ) {
125 138 return true;
126 139 }
@@ -141,11 +154,11 @@
141 154 if ( ! is_string( $contents ) || empty( self::get_file_extensions() ) ) {
142 155 return $contents;
143 156 }
144 157
145 - $included_file_extensions_regex = quotemeta( implode( '|', self::get_file_extensions() ) );
158 + $included_file_extensions_regex = implode( '|', array_map( 'preg_quote', self::get_file_extensions() ) );
146 159 $urls_regex = '#(?:(?:[\"\'\s=>,;]|url\()\K|^)[^\"\'\s(=>,;]+(' . $included_file_extensions_regex . ')(\?[^\/?\\\"\'\s)>,]+)?(?:(?=\/?[?\\\"\'\s)>,&])|$)#i';
147 - $rewritten_contents = preg_replace_callback( $urls_regex, 'self::rewrite_url', $contents );
160 + $rewritten_contents = preg_replace_callback( $urls_regex, [ '\PoweredCache\CDN', 'rewrite_url' ], $contents );
148 161
149 162 return $rewritten_contents;
150 163 }
151 164
@@ -158,9 +171,9 @@
158 171 * @since 2.2
159 172 */
160 173 private static function rewrite_url( $matches ) {
161 174 $file_url = $matches[0];
162 - $site_hostname = ( ! empty( $_SERVER['HTTP_HOST'] ) ) ? $_SERVER['HTTP_HOST'] : wp_parse_url( home_url(), PHP_URL_HOST );
175 + $site_hostname = ( ! empty( $_SERVER['HTTP_HOST'] ) ) ? $_SERVER['HTTP_HOST'] : wp_parse_url( home_url(), PHP_URL_HOST ); // phpcs:ignore
163 176
164 177 /**
165 178 * Filters site hostname(s).
166 179 *
@@ -267,10 +280,15 @@
267 280 $zone = 'js';
268 281 }
269 282
270 283 $cdn_hostname = self::get_best_possible_cdn_host( $zone );
271 - $cdn_url = '//' . $cdn_hostname;
272 284
285 + if ( empty( $cdn_hostname ) ) {
286 + return $optimized_url;
287 + }
288 +
289 + $cdn_url = '//' . $cdn_hostname;
290 +
273 291 $optimized_url = str_replace( home_url(), $cdn_url, $optimized_url );
274 292
275 293 return $optimized_url;
276 294 }
@@ -382,5 +400,4 @@
382 400 return $file_extensions;
383 401 }
384 402
385 403 }
386 -