Skip to content

Commit

Permalink
Closes #2520 Rewrite both http & https versions of the URLs to the CD…
Browse files Browse the repository at this point in the history
…N URL (PR #2589)

* move and rename files to new architecture
* use home_url() instead of get_option( 'home' )
* add service provider for the CDN module
* improve tests code

Co-authored-by: Tonya Mork <[email protected]>
  • Loading branch information
remyperona and Tonya Mork authored May 5, 2020
1 parent 48d9cde commit 28e7b00
Show file tree
Hide file tree
Showing 49 changed files with 3,437 additions and 691 deletions.
49 changes: 34 additions & 15 deletions inc/classes/CDN/CDN.php → inc/Engine/CDN/CDN.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<?php
namespace WP_Rocket\CDN;
namespace WP_Rocket\Engine\CDN;

use WP_Rocket\Admin\Options_Data;

/**
* CDN class
*
* @since 3.4
* @author Remy Perona
*/
class CDN {
/**
Expand All @@ -17,6 +16,13 @@ class CDN {
*/
private $options;

/**
* Home URL host
*
* @var string
*/
private $home_host;

/**
* Constructor
*
Expand All @@ -30,7 +36,6 @@ public function __construct( Options_Data $options ) {
* Search & Replace URLs with the CDN URLs in the provided content
*
* @since 3.4
* @author Remy Perona
*
* @param string $html HTML content.
* @return string
Expand All @@ -50,7 +55,6 @@ function( $matches ) {
* Rewrites URLs in a srcset attribute using the CDN URL
*
* @since 3.4.0.4
* @author Remy Perona
*
* @param string $html HTML content.
* @return string
Expand All @@ -66,7 +70,7 @@ public function rewrite_srcset( $html ) {
$sources = array_unique( array_map( 'trim', $sources ) );
$cdn_srcset = $srcset['sources'];
foreach ( $sources as $source ) {
$url = \preg_split( '#\s+#', trim( $source ) );
$url = preg_split( '#\s+#', trim( $source ) );
$cdn_srcset = str_replace( $url[0], $this->rewrite_url( $url[0] ), $cdn_srcset );
}

Expand All @@ -81,13 +85,12 @@ public function rewrite_srcset( $html ) {
* Rewrites an URL with the CDN URL
*
* @since 3.4
* @author Remy Perona
*
* @param string $url Original URL.
* @return string
*/
public function rewrite_url( $url ) {
if ( ! $this->options->get( 'cdn' ) ) {
if ( ! $this->options->get( 'cdn', 0 ) ) {
return $url;
}

Expand All @@ -108,14 +111,16 @@ public function rewrite_url( $url ) {
return rocket_add_url_protocol( $cdn_url . '/' . ltrim( $url, '/' ) );
}

$home = get_option( 'home' );
$home_parts = wp_parse_url( $home );
$home_host = $this->get_home_host();

if ( ! isset( $parsed_url['scheme'] ) ) {
return str_replace( $home_parts['host'], rocket_remove_url_protocol( $cdn_url ), $url );
return str_replace( $home_host, rocket_remove_url_protocol( $cdn_url ), $url );
}

$home_url = $home_parts['scheme'] . '://' . $home_parts['host'];
$home_url = [
'http://' . $home_host,
'https://' . $home_host,
];

return str_replace( $home_url, rocket_add_url_protocol( $cdn_url ), $url );
}
Expand All @@ -124,7 +129,6 @@ public function rewrite_url( $url ) {
* Rewrites URLs to CDN URLs in CSS content
*
* @since 3.4
* @author Remy Perona
*
* @param string $content CSS content.
* @return string
Expand Down Expand Up @@ -156,10 +160,10 @@ public function rewrite_css_properties( $content ) {
* @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 string $zones List of zones. Default is 'all'.
* @param array $zones List of zones. Default is [ 'all' ].
* @return array
*/
public function get_cdn_urls( $zones = 'all' ) {
public function get_cdn_urls( $zones = [ 'all' ] ) {
$hosts = [];
$zones = (array) $zones;
$cdn_urls = $this->options->get( 'cdn_cnames', [] );
Expand Down Expand Up @@ -207,7 +211,7 @@ public function get_cdn_urls( $zones = 'all' ) {
* @return string
*/
private function get_base_url() {
return '//' . wp_parse_url( get_option( 'home' ), PHP_URL_HOST );
return '//' . $this->get_home_host();
}

/**
Expand Down Expand Up @@ -269,6 +273,21 @@ public function is_excluded( $url ) {
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
*
Expand Down
40 changes: 40 additions & 0 deletions inc/Engine/CDN/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
namespace WP_Rocket\Engine\CDN;

use League\Container\ServiceProvider\AbstractServiceProvider;

/**
* Service provider for WP Rocket CDN
*
* @since 3.5.5
*/
class ServiceProvider extends AbstractServiceProvider {
/**
* The provides array is a way to let the container
* know that a service is provided by this service
* provider. Every service that is registered via
* this service provider must have an alias added
* to this array or it will be ignored.
*
* @var array
*/
protected $provides = [
'cdn',
'cdn_subscriber',
];

/**
* Registers the services in the container
*
* @return void
*/
public function register() {
$options = $this->getContainer()->get( 'options' );

$this->getContainer()->share( 'cdn', 'WP_Rocket\Engine\CDN\CDN' )
->withArgument( $options );
$this->getContainer()->share( 'cdn_subscriber', 'WP_Rocket\Engine\CDN\Subscriber' )
->withArgument( $options )
->withArgument( $this->getContainer()->get( 'cdn' ) );
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
<?php
namespace WP_Rocket\Subscriber\CDN;
namespace WP_Rocket\Engine\CDN;

use WP_Rocket\Admin\Options_Data;
use WP_Rocket\CDN\CDN;
use WP_Rocket\Event_Management\Subscriber_Interface;

/**
* Subscriber for the CDN feature
*
* @since 3.4
* @author Remy Perona
*/
class CDNSubscriber implements Subscriber_Interface {
class Subscriber implements Subscriber_Interface {
/**
* WP Rocket Options instance
*
Expand Down Expand Up @@ -41,7 +39,6 @@ public function __construct( Options_Data $options, CDN $cdn ) {
* Return an array of events that this subscriber wants to listen to.
*
* @since 3.4
* @author Remy Perona
*
* @return array
*/
Expand All @@ -65,7 +62,6 @@ public static function get_subscribed_events() {
* Rewrites URLs to the CDN URLs if allowed
*
* @since 3.4
* @author Remy Perona
*
* @param string $html HTML content.
* @return string
Expand All @@ -82,7 +78,6 @@ public function rewrite( $html ) {
* Rewrites URLs in srcset attributes to the CDN URLs if allowed
*
* @since 3.4.0.4
* @author Remy Perona
*
* @param string $html HTML content.
* @return string
Expand All @@ -99,7 +94,6 @@ public function rewrite_srcset( $html ) {
* Rewrites URLs to the CDN URLs in CSS files
*
* @since 3.4
* @author Remy Perona
*
* @param string $content CSS content.
* @return string
Expand All @@ -118,7 +112,7 @@ public function rewrite_css_properties( $content ) {
return $content;
}

if ( ! $this->options->get( 'cdn' ) ) {
if ( ! $this->is_cdn_enabled() ) {
return $content;
}

Expand All @@ -129,7 +123,6 @@ public function rewrite_css_properties( $content ) {
* Gets the host value for each CDN URLs
*
* @since 3.4
* @author Remy Perona
*
* @param array $hosts Base hosts.
* @param array $zones Zones to get the CND URLs associated with.
Expand Down Expand Up @@ -160,7 +153,6 @@ public function get_cdn_hosts( array $hosts = [], array $zones = [ 'all' ] ) {
* Adds CDN URLs to the DNS prefetch links
*
* @since 3.4
* @author Remy Perona
*
* @param array $domains Domain names to DNS prefetch.
* @return array
Expand All @@ -183,7 +175,6 @@ public function add_dns_prefetch_cdn( $domains ) {
* Adds the CDN URL on the provided URL
*
* @since 3.4
* @author Remy Perona
*
* @param string $url URL to rewrite.
* @param string $original_url Original URL for this URL. Optional.
Expand Down Expand Up @@ -254,7 +245,6 @@ public function maybe_replace_url( $url, array $zones = [ 'all' ] ) {
* Checks if CDN can be applied
*
* @since 3.4
* @author Remy Perona
*
* @return boolean
*/
Expand All @@ -263,7 +253,7 @@ private function is_allowed() {
return false;
}

if ( ! $this->options->get( 'cdn' ) ) {
if ( ! $this->is_cdn_enabled() ) {
return false;
}

Expand All @@ -273,4 +263,15 @@ private function is_allowed() {

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 );
}
}
23 changes: 9 additions & 14 deletions inc/classes/ServiceProvider/class-common-subscribers.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ class Common_Subscribers extends AbstractServiceProvider {
'critical_css',
'critical_css_subscriber',
'cache_dir_size_check_subscriber',
'cdn',
'cdn_subscriber',
'capabilities_subscriber',
'webp_subscriber',
'expired_cache_purge',
Expand All @@ -46,36 +44,33 @@ class Common_Subscribers extends AbstractServiceProvider {
* @return void
*/
public function register() {
$options = $this->getContainer()->get( 'options' );

$this->getContainer()->share( 'heartbeat_subscriber', 'WP_Rocket\Subscriber\Heartbeat_Subscriber' )
->withArgument( $this->getContainer()->get( 'options' ) );
->withArgument( $options );
$this->getContainer()->share( 'db_optimization_subscriber', 'WP_Rocket\Subscriber\Admin\Database\Optimization_Subscriber' )
->withArgument( $this->getContainer()->get( 'db_optimization' ) )
->withArgument( $this->getContainer()->get( 'options' ) );
->withArgument( $options );
$this->getContainer()->add( 'critical_css_generation', 'WP_Rocket\Optimization\CSS\Critical_CSS_Generation' );
$this->getContainer()->add( 'critical_css', 'WP_Rocket\Optimization\CSS\Critical_CSS' )
->withArgument( $this->getContainer()->get( 'critical_css_generation' ) );
$this->getContainer()->share( 'critical_css_subscriber', 'WP_Rocket\Subscriber\Optimization\Critical_CSS_Subscriber' )
->withArgument( $this->getContainer()->get( 'critical_css' ) )
->withArgument( $this->getContainer()->get( 'options' ) );
->withArgument( $options );
$this->getContainer()->add( 'expired_cache_purge', 'WP_Rocket\Cache\Expired_Cache_Purge' )
->withArgument( WP_ROCKET_CACHE_PATH );
->withArgument( rocket_get_constant( 'WP_ROCKET_CACHE_PATH' ) );
$this->getContainer()->share( 'cache_dir_size_check_subscriber', 'WP_Rocket\Subscriber\Tools\Cache_Dir_Size_Check_Subscriber' );
$this->getContainer()->share( 'expired_cache_purge_subscriber', 'WP_Rocket\Subscriber\Cache\Expired_Cache_Purge_Subscriber' )
->withArgument( $this->getContainer()->get( 'options' ) )
->withArgument( $options )
->withArgument( $this->getContainer()->get( 'expired_cache_purge' ) );
$this->getContainer()->share( 'cdn', 'WP_Rocket\CDN\CDN' )
->withArgument( $this->getContainer()->get( 'options' ) );
$this->getContainer()->share( 'cdn_subscriber', 'WP_Rocket\Subscriber\CDN\CDNSubscriber' )
->withArgument( $this->getContainer()->get( 'options' ) )
->withArgument( $this->getContainer()->get( 'cdn' ) );
$this->getContainer()->share( 'capabilities_subscriber', 'WP_Rocket\Subscriber\Plugin\Capabilities_Subscriber' );
$this->getContainer()->share( 'webp_subscriber', 'WP_Rocket\Subscriber\Media\Webp_Subscriber' )
->withArgument( $this->getContainer()->get( 'options' ) )
->withArgument( $options )
->withArgument( $this->getContainer()->get( 'options_api' ) )
->withArgument( $this->getContainer()->get( 'cdn_subscriber' ) )
->withArgument( $this->getContainer()->get( 'beacon' ) );
$this->getContainer()->share( 'detect_missing_tags_subscriber', 'WP_Rocket\Subscriber\Tools\Detect_Missing_Tags_Subscriber' );
$this->getContainer()->share( 'purge_actions_subscriber', 'WP_Rocket\Subscriber\Cache\PurgeActionsSubscriber' )
->withArgument( $this->getContainer()->get( 'options' ) );
->withArgument( $options );
}
}
1 change: 1 addition & 0 deletions inc/classes/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ function() {

$this->container->addServiceProvider( 'WP_Rocket\Addon\ServiceProvider' );
$this->container->addServiceProvider( 'WP_Rocket\Engine\Preload\ServiceProvider' );
$this->container->addServiceProvider( 'WP_Rocket\Engine\CDN\ServiceProvider' );
$this->container->addServiceProvider( 'WP_Rocket\ServiceProvider\Common_Subscribers' );
$this->container->addServiceProvider( 'WP_Rocket\ThirdParty\ServiceProvider' );
$this->container->addServiceProvider( 'WP_Rocket\ServiceProvider\Hostings_Subscribers' );
Expand Down
2 changes: 1 addition & 1 deletion inc/classes/subscriber/Media/class-webp-subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\Admin\Beacon\Beacon;
use WP_Rocket\Event_Management\Subscriber_Interface;
use WP_Rocket\Subscriber\CDN\CDNSubscriber;
use WP_Rocket\Engine\CDN\Subscriber as CDNSubscriber;

/**
* Subscriber for the WebP support.
Expand Down
9 changes: 0 additions & 9 deletions tests/Fixtures/CDN/original.css

This file was deleted.

9 changes: 0 additions & 9 deletions tests/Fixtures/CDN/rewrite.css

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 28e7b00

Please sign in to comment.