ontent ) { if ( ! preg_match_all( '#url\(\s*(\'|")?\s*(?![\'"]?data)(?(?:https?:|)' . preg_quote( $this->get_base_url(), '#' ) . '\/[^"|\'|\)|\s]+)\s*#i', $content, $matches, PREG_SET_ORDER ) ) { return $content; } foreach ( $matches as $property ) { /** * Filters the URL of the CSS property * * @since 2.8 * * @param string $url URL of the CSS property. */ $cdn_url = $this->rewrite_url( apply_filters( 'rocket_cdn_css_properties_url', $property['url'] ) ); $replacement = str_replace( $property['url'], $cdn_url, $property[0] ); $content = str_replace( $property[0], $replacement, $content ); } return $content; } /** * Get all CDN URLs for one or more zones. * * @since 2.1 * @since 3.0 Don't check for WP Rocket CDN option activated to be able to use the function on Hosting with CDN auto-enabled. * * @param array $zones List of zones. Default is [ 'all' ]. * @return array */ public function get_cdn_urls( $zones = [ 'all' ] ) { $hosts = []; $zones = (array) $zones; $cdn_urls = $this->options->get( 'cdn_cnames', [] ); if ( $cdn_urls ) { $cdn_zones = $this->options->get( 'cdn_zone', [] ); foreach ( $cdn_urls as $k => $urls ) { if ( ! in_array( $cdn_zones[ $k ], $zones, true ) ) { continue; } $urls = explode( ',', $urls ); $urls = array_map( 'trim', $urls ); foreach ( $urls as $url ) { $hosts[] = $url; } } } /** * Filter all CDN URLs. * * @since 2.7 * @since 3.4 Added $zone parameter. * * @param array $hosts List of CDN URLs. * @param array $zones List of zones. Default is [ 'all' ]. */ $hosts = (array) apply_filters( 'rocket_cdn_cnames', $hosts, $zones ); $hosts = array_filter( $hosts ); $hosts = array_flip( array_flip( $hosts ) ); $hosts = array_values( $hosts ); return $hosts; } /** * Gets the base URL for the website * * @since 3.4 * * @return string */ private function get_base_url() { return '//' . $this->get_home_host(); } /** * Gets the allowed paths as a regex pattern for the CDN rewrite * * @since 3.4 * * @return string */ private function get_allowed_paths() { $wp_content_dirname = ltrim( trailingslashit( wp_parse_url( content_url(), PHP_URL_PATH ) ), '/' ); $wp_includes_dirname = ltrim( trailingslashit( wp_parse_url( includes_url(), PHP_URL_PATH ) ), '/' ); $upload_dirname = ''; $uploads_info = wp_upload_dir(); if ( ! empty( $uploads_info['baseurl'] ) ) { $upload_dirname = '|' . ltrim( trailingslashit( wp_parse_url( $uploads_info['baseurl'], PHP_URL_PATH ) ), '/' ); } return $wp_content_dirname . $upload_dirname . '|' . $wp_includes_dirname; } /** * Checks if the provided URL can be rewritten with the CDN URL * * @since 3.4 * * @param string $url URL to check. * @return boolean */ public function is_excluded( $url ) { $path = wp_parse_url( $url, PHP_URL_PATH ); $excluded_extensions = [ 'php', 'html', 'htm', 'cfm', ]; if ( in_array( pathinfo( $path, PATHINFO_EXTENSION ), $excluded_extensions, true ) ) { return true; } if ( ! $path ) { return true; } if ( '/' === $path ) { return true; } if ( preg_match( '#^(' . $this->get_excluded_files( '#' ) . ')$#', $path ) ) { return true; } return false; } /** * Gets the home URL host * * @since 3.5.5 * * @return string */ private function get_home_host() { if ( empty( $this->home_host ) ) { $this->home_host = wp_parse_url( home_url(), PHP_URL_HOST ); } return $this->home_host; } /** * Gets the CDN zones for the provided URL * * @since 3.4 * * @param string $url URL to check. * @return array */ private function get_zones_for_url( $url ) { $zones = [ 'all' ]; $ext = pathinfo( wp_parse_url( $url, PHP_URL_PATH ), PATHINFO_EXTENSION ); $image_types = [ 'jpg', 'jpeg', 'jpe', 'png', 'gif', 'webp', 'bmp', 'tiff', 'svg', ]; if ( 'css' === $ext || 'js' === $ext ) { $zones[] = 'css_and_js'; } if ( 'css' === $ext ) { $zones[] = 'css'; } if ( 'js' === $ext ) { $zones[] = 'js'; } if ( in_array( $ext, $image_types, true ) ) { $zones[] = 'images'; } return $zones; } /** * Get all files we don't allow to get in CDN. * * @since 2.5 * * @param string $delimiter RegEx delimiter. * @return string A pipe-separated list of excluded files. */ private function get_excluded_files( $delimiter ) { $files = $this->options->get( 'cdn_reject_files', [] ); /** * Filter the excluded files. * * @since 2.5 * * @param array $files List of excluded files. */ $files = (array) apply_filters( 'rocket_cdn_reject_files', $files ); $files = array_filter( $files ); if ( ! $files ) { return ''; } $files = array_flip( array_flip( $files ) ); $files = array_map( function ( $file ) use ( $delimiter ) { return str_replace( $delimiter, '\\' . $delimiter, $file ); }, $files ); return implode( '|', $files ); } /** * Get srcset attributes to rewrite to the CDN. * * @since 3.8.7 * * @return string A pipe-separated list of srcset attributes. */ private function get_srcset_attributes() { /** * Filter the srcset attributes. * * @since 3.8.7 * * @param array $srcset_attributes List of srcset attributes. */ $srcset_attributes = (array) apply_filters( 'rocket_cdn_srcset_attributes', [ 'data-lazy-', 'data-', ] ); return implode( '|', $srcset_attributes ); } /** * Removes inline scripts from the HTML * * @since 3.10.5 * * @param string $html HTML content. * * @return string */ private function remove_inline_scripts( $html ): string { if ( ! preg_match_all( '#]*)>(?[\s\S]*?)#msi', $html, $matches, PREG_SET_ORDER ) ) { return $html; } if ( empty( $matches ) ) { return $html; } foreach ( $matches as $inline_js ) { if ( empty( $inline_js['content'] ) ) { continue; } $html = str_replace( $inline_js[0], '', $html ); } return $html; } }