| @@ -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 | * |
| @@ -272,9 +285,9 @@ | ||
| 272 | 285 | if ( empty( $cdn_hostname ) ) { |
| 273 | 286 | return $optimized_url; |
| 274 | 287 | } |
| 275 | 288 | |
| 276 | - $cdn_url = '//' . $cdn_hostname; | |
| 289 | + $cdn_url = '//' . $cdn_hostname; | |
| 277 | 290 | |
| 278 | 291 | $optimized_url = str_replace( home_url(), $cdn_url, $optimized_url ); |
| 279 | 292 | |
| 280 | 293 | return $optimized_url; |
| @@ -387,5 +400,4 @@ | ||
| 387 | 400 | return $file_extensions; |
| 388 | 401 | } |
| 389 | 402 | |
| 390 | 403 | } |
| 391 | - | |