public function add_cdn_url( $url, $original_url = '' ) { if ( ! empty( $original_url ) ) { if ( $this->cdn->is_excluded( $original_url ) ) { return $url; } } return $this->cdn->rewrite_url( $url ); } /** * Replace CDN URL with site URL on the provided asset URL. * * @since 3.5.3 * * @param string $url URL of the asset. * @param array $zones Array of corresponding zones for the asset. * * @return string */ public function maybe_replace_url( $url, array $zones = [ 'all' ] ) { if ( ! $this->is_allowed() ) { return $url; } $url_parts = get_rocket_parse_url( $url ); if ( empty( $url_parts['host'] ) ) { return $url; } $site_url_parts = get_rocket_parse_url( site_url() ); if ( empty( $site_url_parts['host'] ) ) { return $url; } if ( $url_parts['host'] === $site_url_parts['host'] ) { return $url; } $cdn_urls = $this->cdn->get_cdn_urls( $zones ); if ( empty( $cdn_urls ) ) { return $url; } $cdn_urls = array_map( 'rocket_add_url_protocol', $cdn_urls ); $site_url = $site_url_parts['scheme'] . '://' . $site_url_parts['host']; foreach ( $cdn_urls as $cdn_url ) { if ( false === strpos( $url, $cdn_url ) ) { continue; } return str_replace( $cdn_url, $site_url, $url ); } return $url; } /** * Add a preconnect tag for the CDN. * * @since 3.8.3 * * @param array $urls The initial array of wp_resource_hint urls. * @param string $relation_type The relation type for the hint: eg., 'preconnect', 'prerender', etc. * * @return array The filtered urls. */ public function add_preconnect_cdn( array $urls, string $relation_type ): array { if ( 'preconnect' !== $relation_type || rocket_bypass() || ! $this->is_allowed() || ! $this->is_cdn_enabled() ) { return $urls; } $cdn_urls = $this->cdn->get_cdn_urls( [ 'all', 'images', 'css_and_js', 'css', 'js' ] ); if ( empty( $cdn_urls ) ) { return $urls; } foreach ( $cdn_urls as $url ) { $url_parts = get_rocket_parse_url( $url ); if ( empty( $url_parts['scheme'] ) ) { if ( preg_match( '/^(?![\/])(?=[^\.]+\/).+/i', $url ) ) { continue; } $url = '//' . $url; $url_parts = get_rocket_parse_url( $url ); } $domain = empty( $url_parts['scheme'] ) ? '//' . $url_parts['host'] : $url_parts['scheme'] . '://' . $url_parts['host']; // Note: As of 22 Feb, 2021 we cannot add more than one instance of a domain url // on the wp_resource_hint() hook -- wp_resource_hint() will // only actually print the first one. // Ideally, we want both because CSS resources will use the crossorigin version, // But JS resources will not. // Jonathan has submitted a ticket to change this behavior: // @see https://core.trac.wordpress.org/ticket/52465 // Until then, we order these to prefer/print the non-crossorigin version. $urls[] = [ 'href' => $domain ]; $urls[] = [ 'href' => $domain, 'crossorigin' => 'anonymous', ]; } return $urls; } /** * Checks if CDN can be applied * * @since 3.4 * * @return boolean */ private function is_allowed() { if ( rocket_get_constant( 'DONOTROCKETOPTIMIZE' ) ) { return false; } if ( ! $this->is_cdn_enabled() ) { return false; } if ( is_rocket_post_excluded_option( 'cdn' ) ) { return false; } return true; } /** * Checks if the CDN option is enabled * * @since 3.5.5 * * @return bool */ private function is_cdn_enabled() { return (bool) $this->options->get( 'cdn', 0 ); } }