Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite google fonts with SaaS visit #7162

Merged
1 change: 1 addition & 0 deletions .github/workflows/lint_phpcs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
- branch-*
- feature/*
- enhancement/*
- fix/*

jobs:
run:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/lint_phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
- branch-*
- feature/*
- enhancement/*
- fix/*

jobs:
run:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test_wprocket.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
- branch-*
- feature/*
- enhancement/*
- fix/*

jobs:
run:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
- branch-*
- feature/*
- enhancement/*
- fix/*

jobs:
run:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test_wprocket_latest_specific.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
- branch-*
- feature/*
- enhancement/*
- fix/*

jobs:
run:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test_wprocket_php8.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
- branch-*
- feature/*
- enhancement/*
- fix/*

jobs:
run:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use WP_Rocket\Engine\Common\Context\AbstractContext;

class Context extends AbstractContext {
class OptimizationContext extends AbstractContext {
/**
* Checks if the feature is allowed.
*
Expand All @@ -14,14 +14,12 @@ class Context extends AbstractContext {
* @return bool
*/
public function is_allowed( array $data = [] ): bool {
$is_allowed = $this->run_common_checks(
[
'do_not_optimize' => false,
'bypass' => false,
'option' => 'host_fonts_locally',
]
);
$checks = [
'option' => 'host_fonts_locally',
'do_not_optimize' => false,
'bypass' => false,
];

return $is_allowed;
return $this->run_common_checks( $checks );
}
}
23 changes: 23 additions & 0 deletions inc/Engine/Media/Fonts/Context/SaasContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Media\Fonts\Context;

use WP_Rocket\Engine\Common\Context\AbstractContext;

class SaasContext extends AbstractContext {
/**
* Checks if the feature is allowed.
*
* @param array $data Optional. Data to check against.
*
* @return bool
*/
public function is_allowed( array $data = [] ): bool {
$checks = [
'option' => 'host_fonts_locally',
];

return $this->run_common_checks( $checks );
}
}
66 changes: 49 additions & 17 deletions inc/Engine/Media/Fonts/Frontend/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

namespace WP_Rocket\Engine\Media\Fonts\Frontend;

use WP_Rocket\Engine\Media\Fonts\Context\Context;
use WP_Rocket\Engine\Media\Fonts\Context\OptimizationContext;
use WP_Rocket\Engine\Media\Fonts\Context\SaasContext;
use WP_Rocket\Engine\Media\Fonts\Filesystem;
use WP_Rocket\Engine\Optimization\RegexTrait;
use WP_Rocket\Logger\Logger;
Expand All @@ -12,11 +13,18 @@ class Controller {
use RegexTrait;

/**
* Context instance.
* Optimization Context instance.
*
* @var Context
* @var OptimizationContext
*/
private $context;
private $optimization_context;

/**
* SaaS Context instance.
*
* @var SaasContext
*/
private $saas_context;

/**
* Filesystem instance.
Expand Down Expand Up @@ -49,14 +57,16 @@ class Controller {
/**
* Constructor.
*
* @param Context $context Context instance.
* @param Filesystem $filesystem Filesystem instance.
* @param OptimizationContext $optimization_context Optimization Context instance.
* @param SaasContext $saas_context SaaS Context instance.
* @param Filesystem $filesystem Filesystem instance.
*/
public function __construct( Context $context, Filesystem $filesystem ) {
$this->context = $context;
$this->base_path = rocket_get_constant( 'WP_ROCKET_CACHE_ROOT_PATH', '' ) . 'fonts/' . get_current_blog_id() . '/';
$this->base_url = rocket_get_constant( 'WP_ROCKET_CACHE_ROOT_URL', '' ) . 'fonts/' . get_current_blog_id() . '/';
$this->filesystem = $filesystem;
public function __construct( OptimizationContext $optimization_context, SaasContext $saas_context, Filesystem $filesystem ) {
$this->optimization_context = $optimization_context;
$this->saas_context = $saas_context;
$this->base_path = rocket_get_constant( 'WP_ROCKET_CACHE_ROOT_PATH', '' ) . 'fonts/' . get_current_blog_id() . '/';
$this->base_url = rocket_get_constant( 'WP_ROCKET_CACHE_ROOT_URL', '' ) . 'fonts/' . get_current_blog_id() . '/';
$this->filesystem = $filesystem;
}

/**
Expand All @@ -65,11 +75,7 @@ public function __construct( Context $context, Filesystem $filesystem ) {
* @param string $html HTML content.
* @return string
*/
public function rewrite_fonts( $html ): string {
if ( ! $this->context->is_allowed() ) {
return $html;
}

private function rewrite_fonts( $html ): string {
// For test purposes.
$start_time = microtime( true );

Expand Down Expand Up @@ -110,6 +116,32 @@ public function rewrite_fonts( $html ): string {
return $html;
}

/**
* Rewrite fonts for normal optimizations.
*
* @param string $html page HTML.
* @return string
*/
public function rewrite_fonts_for_optimizations( $html ): string {
if ( ! $this->optimization_context->is_allowed() ) {
return $html;
}
return $this->rewrite_fonts( $html );
}

/**
* Rewrite fonts for SaaS visits optimizations.
*
* @param string $html page HTML.
* @return string
*/
public function rewrite_fonts_for_saas( $html ): string {
if ( ! $this->saas_context->is_allowed() ) {
return $html;
}
return $this->rewrite_fonts( $html );
}

/**
* Replaces the Google Fonts URL with the local one.
*
Expand Down Expand Up @@ -238,7 +270,7 @@ private function remove_preconnect_and_prefetch( string $html ) {
* @return bool
*/
public function disable_google_fonts_preload( $disable ): bool {
if ( ! $this->context->is_allowed() ) {
if ( ! $this->optimization_context->is_allowed() ) {
return $disable;
}

Expand Down
17 changes: 14 additions & 3 deletions inc/Engine/Media/Fonts/Frontend/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public function __construct( Controller $frontend_controller ) {
*/
public static function get_subscribed_events(): array {
return [
'rocket_buffer' => [ 'rewrite_fonts', 18 ],
'rocket_buffer' => [ 'rewrite_fonts_for_optimizations', 18 ],
'rocket_disable_google_fonts_preload' => 'disable_google_fonts_preload',
'rocket_performance_hints_buffer' => 'rewrite_fonts_for_saas',
];
}

Expand All @@ -42,8 +43,18 @@ public static function get_subscribed_events(): array {
* @param string $html HTML content.
* @return string
*/
public function rewrite_fonts( $html ): string {
return $this->frontend_controller->rewrite_fonts( $html );
public function rewrite_fonts_for_optimizations( $html ): string {
return $this->frontend_controller->rewrite_fonts_for_optimizations( $html );
}

/**
* Rewrites the Google Fonts paths to local ones for SaaS.
*
* @param string $html HTML content.
* @return string
*/
public function rewrite_fonts_for_saas( $html ): string {
return $this->frontend_controller->rewrite_fonts_for_saas( $html );
}

/**
Expand Down
15 changes: 10 additions & 5 deletions inc/Engine/Media/Fonts/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
namespace WP_Rocket\Engine\Media\Fonts;

use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
use WP_Rocket\Engine\Media\Fonts\Filesystem;
use WP_Rocket\Engine\Media\Fonts\Context\OptimizationContext;
use WP_Rocket\Engine\Media\Fonts\Context\SaasContext;
use WP_Rocket\Engine\Media\Fonts\Admin\Settings;
use WP_Rocket\Engine\Media\Fonts\Admin\Subscriber as AdminSubscriber;
use WP_Rocket\Engine\Media\Fonts\Context\Context;
use WP_Rocket\Engine\Media\Fonts\Frontend\Controller as FrontendController;
use WP_Rocket\Engine\Media\Fonts\Frontend\Subscriber as FrontendSubscriber;

Expand All @@ -28,7 +28,8 @@ class ServiceProvider extends AbstractServiceProvider {
'media_fonts_filesystem',
'media_fonts_settings',
'media_fonts_admin_subscriber',
'media_fonts_context',
'media_fonts_optimization_context',
'media_fonts_saas_context',
'media_fonts_frontend_controller',
'media_fonts_frontend_subscriber',
];
Expand Down Expand Up @@ -58,13 +59,17 @@ public function register(): void {
$this->getContainer()->addShared( 'media_fonts_admin_subscriber', AdminSubscriber::class )
->addArgument( 'media_fonts_settings' );

$this->getContainer()->add( 'media_fonts_context', Context::class )
$this->getContainer()->add( 'media_fonts_optimization_context', OptimizationContext::class )
->addArgument( $this->getContainer()->get( 'options' ) );

$this->getContainer()->add( 'media_fonts_saas_context', SaasContext::class )
->addArgument( $this->getContainer()->get( 'options' ) );

$this->getContainer()->add( 'media_fonts_frontend_controller', FrontendController::class )
->addArguments(
[
$this->getContainer()->get( 'media_fonts_context' ),
$this->getContainer()->get( 'media_fonts_optimization_context' ),
$this->getContainer()->get( 'media_fonts_saas_context' ),
$this->getContainer()->get( 'media_fonts_filesystem' ),
]
);
Expand Down
2 changes: 1 addition & 1 deletion inc/functions/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ function is_rocket_generate_caching_mobile_files() { // phpcs:ignore WordPress.N
* return Array An array of domain names to DNS prefetch
*/
function rocket_get_dns_prefetch_domains() {
$domains = (array) get_rocket_option( 'dns_prefetch' );
$domains = (array) get_rocket_option( 'dns_prefetch', [] );

/**
* Filter list of domains to prefetch DNS
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<?php

return [
'testShouldReturnFalseWhenBypass' => [
'testShouldReturntrueWhenBypass' => [
'config' => [
'bypass' => true,
'do_not_optimize' => false,
'option' => true,
],
'expected' => false,
'expected' => true,
],
'testShouldReturnFalseWhenDoNotOptimize' => [
'testShouldReturnTrueWhenDoNotOptimize' => [
'config' => [
'bypass' => false,
'do_not_optimize' => true,
'option' => true,
],
'expected' => false,
'expected' => true,
],
'testShouldReturnFalseWhenOptionDisabled' => [
'config' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
* Test class covering \WP_Rocket\Engine\Media\Fonts\Frontend\Subscriber::rewrite_fonts
* @group HostFontsLocally
*/
class Test_RewriteFonts extends FilesystemTestCase {
class Test_RewriteFontsForOptimizations extends FilesystemTestCase {
use HttpCallTrait;

protected $path_to_test_data = '/inc/Engine/Media/Fonts/Frontend/Subscriber/rewriteFonts.php';
protected $path_to_test_data = '/inc/Engine/Media/Fonts/Frontend/Subscriber/rewriteFontsForOptimizations.php';

protected $config;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Brain\Monkey\Functions;
use Mockery;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Engine\Media\Fonts\Context\Context;
use WP_Rocket\Engine\Media\Fonts\Context\OptimizationContext;
use WP_Rocket\Tests\Unit\TestCase;

/**
Expand All @@ -20,7 +20,7 @@ public function testShouldDoExpected( $config, $expected ) {
$this->donotrocketoptimize = $config['do_not_optimize'];

$options = Mockery::mock( Options_Data::class );
$context = new Context( $options );
$context = new OptimizationContext( $options );

Functions\when( 'rocket_bypass' )->justReturn( $config['bypass'] );

Expand Down
Loading
Loading