Skip to content

Commit

Permalink
Remove version from font path and update test
Browse files Browse the repository at this point in the history
  • Loading branch information
Khadreal committed Nov 15, 2024
1 parent fa21618 commit a86be50
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 61 deletions.
32 changes: 2 additions & 30 deletions inc/Engine/Media/Fonts/Controller/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ class Filesystem extends AbstractFileSystem {
*/
private $path;

/**
* Version of the fonts.
*
* @var int
*/
private $version;

/**
* Instantiate the class
*
Expand Down Expand Up @@ -155,7 +148,7 @@ private function download_font( string $url ) {
* @return string Path for the font file.
*/
private function get_fonts_full_path( string $font_provider_path, string $hash ): string {
return $this->path . $font_provider_path . $this->get_version() . '/' . $this->hash_to_path( $hash );
return $this->path . $font_provider_path . $this->hash_to_path( $hash );
}


Expand All @@ -172,7 +165,7 @@ private function get_fonts_relative_path( string $font_provider_path, string $ha
$wp_content_dir = rocket_get_constant( 'WP_CONTENT_DIR' );
$relative_path = str_replace( $wp_content_dir, '', $full_path );

return $relative_path . $this->get_version() . '/' . $this->hash_to_path( $hash );
return $relative_path . $this->hash_to_path( $hash );
}

/**
Expand Down Expand Up @@ -203,27 +196,6 @@ public function delete_font_css( string $url ): bool {
return $this->delete_file( $dir );
}


/**
* Set the version of the fonts
*
* @param int $version The version of the font.
*
* @return void
*/
public function set_version( int $version ): void {
$this->version = $version;
}

/**
* Get the font version
*
* @return int
*/
public function get_version(): int {
return $this->version;
}

/**
* Converts hash to path with filtered number of levels
*
Expand Down
9 changes: 3 additions & 6 deletions inc/Engine/Media/Fonts/Controller/Fonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,16 @@ public function __construct(
/**
* Start the process of downloading font locally
*
* @param string $font_url URL of the font to be saved locally.
* @param string $provider Provider of the font.
* @param integer $version Version.
* @param string $font_url URL of the font to be saved locally.
* @param string $provider Provider of the font.
*
* @return void
*/
public function process( string $font_url, string $provider, int $version ): void {
public function process( string $font_url, string $provider ): void {
if ( ! $this->context->is_allowed() ) {
return;
}

$this->filesystem->set_version( $version );

$this->filesystem->write_font_css( $font_url, $provider );
}
}
22 changes: 9 additions & 13 deletions inc/Engine/Media/Fonts/Frontend/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ public function rewrite_fonts( string $html ): string {
}

foreach ( $v1_fonts as $font ) {
$this->download_font( $font['url'], 1 );
$html = $this->replace_font( $font, $html, 1 );
$this->download_font( $font['url'] );
$html = $this->replace_font( $font, $html );
}

foreach ( $v2_fonts as $font ) {
$this->download_font( $font['url'], 2 );
$html = $this->replace_font( $font, $html, 2 );
$this->download_font( $font['url'] );
$html = $this->replace_font( $font, $html );
}

return $html;
Expand All @@ -84,14 +84,13 @@ public function rewrite_fonts( string $html ): string {
*
* @param array $font Font data.
* @param string $html HTML content.
* @param int $version Provided font version.
* @param string $font_provider Font provider.
*
* @return string
*/
private function replace_font( array $font, string $html, int $version, string $font_provider = 'google-font' ): string {
private function replace_font( array $font, string $html, string $font_provider = 'google-font' ): string {
$hash = md5( $font['url'] );
$local = $this->get_optimized_markup( $hash, $font['url'], $version, $font_provider );
$local = $this->get_optimized_markup( $hash, $font['url'], $font_provider );

return str_replace( $font[0], $local, $html );
}
Expand All @@ -103,15 +102,13 @@ private function replace_font( array $font, string $html, int $version, string $
*
* @param string $hash Font Url has.
* @param string $original_url Fonts Url.
* @param int $version Fonts version.
* @param string $font_provider Fonts provider.
*
* @return string
*/
protected function get_optimized_markup(
string $hash,
string $original_url,
int $version,
string $font_provider
): string {
$levels = 3;
Expand All @@ -122,7 +119,7 @@ protected function get_optimized_markup(
$path_array[] = $remain;

$path = implode( '/', $path_array );
$font_provider_path = sprintf( '%s/%d/', $font_provider, $version );
$font_provider_path = sprintf( '%s/', $font_provider );

$url = $this->base_url . $font_provider_path . $path . '.css';

Expand Down Expand Up @@ -154,10 +151,9 @@ public function disable_google_fonts_preload( $disable ): bool {
* Download font
*
* @param string $font_url Font url to be downloaded.
* @param int $font_version The version of the font.
* @param string $provider The font provider.
*/
private function download_font( string $font_url, int $font_version, string $provider = 'google-font' ): void {
$this->font->process( $font_url, $provider, $font_version );
private function download_font( string $font_url, string $provider = 'google-font' ): void {
$this->font->process( $font_url, $provider );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Google Font V1 Template</title>
<link rel="stylesheet" href="http://example.org/wp-content/cache/fonts/1/google-font/1/e/b/c/173c0fc97eef86a6e51ada56c5a9a.css" data-wpr-hosted-gf-parameters="family=Roboto"/>
<link rel="stylesheet" href="http://example.org/wp-content/cache/fonts/1/google-font/1/5/9/5/cb6ccb56826a802ed411cef875f0e.css" data-wpr-hosted-gf-parameters="family=Open+Sans"/>
<link rel="stylesheet" href="http://example.org/wp-content/cache/fonts/1/google-font/e/b/c/173c0fc97eef86a6e51ada56c5a9a.css" data-wpr-hosted-gf-parameters="family=Roboto"/>
<link rel="stylesheet" href="http://example.org/wp-content/cache/fonts/1/google-font/5/9/5/cb6ccb56826a802ed411cef875f0e.css" data-wpr-hosted-gf-parameters="family=Open+Sans"/>
<style>
.roboto-font {
font-family: 'Roboto', sans-serif;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Google Font V1 and V2 Template</title>
<link rel="stylesheet" href="http://example.org/wp-content/cache/fonts/1/google-font/1/6/2/4/f2c2b9858423d0688793189f6e6cb.css" data-wpr-hosted-gf-parameters="family=Roboto|Open+Sans"/> <!-- V1 Fonts -->
<link rel="stylesheet" href="http://example.org/wp-content/cache/fonts/1/google-font/2/8/d/4/e42f26da0305c49cd3264956d8329.css" data-wpr-hosted-gf-parameters="family=Lato:wght@400;700&family=Montserrat:wght@400;700&display=swap"/> <!-- V2 Fonts -->
<link rel="stylesheet" href="http://example.org/wp-content/cache/fonts/1/google-font/6/2/4/f2c2b9858423d0688793189f6e6cb.css" data-wpr-hosted-gf-parameters="family=Roboto|Open+Sans"/> <!-- V1 Fonts -->
<link rel="stylesheet" href="http://example.org/wp-content/cache/fonts/1/google-font/8/d/4/e42f26da0305c49cd3264956d8329.css" data-wpr-hosted-gf-parameters="family=Lato:wght@400;700&family=Montserrat:wght@400;700&display=swap"/> <!-- V2 Fonts -->
<style>
.v1-font-roboto {
font-family: 'Roboto', sans-serif;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Google Font V2 Template</title>
<link rel="stylesheet" href="http://example.org/wp-content/cache/fonts/1/google-font/2/a/b/2/8ffb4f83584e9add1be594dd90dfd.css" data-wpr-hosted-gf-parameters="family=Roboto:wght@400;700&display=swap"/>
<link rel="stylesheet" href="http://example.org/wp-content/cache/fonts/1/google-font/2/b/4/d/9ffca0114d3acb6ab0b068feaf933.css" data-wpr-hosted-gf-parameters="family=Lato:wght@400;700&display=swap"/>
<link rel="stylesheet" href="http://example.org/wp-content/cache/fonts/1/google-font/a/b/2/8ffb4f83584e9add1be594dd90dfd.css" data-wpr-hosted-gf-parameters="family=Roboto:wght@400;700&display=swap"/>
<link rel="stylesheet" href="http://example.org/wp-content/cache/fonts/1/google-font/b/4/d/9ffca0114d3acb6ab0b068feaf933.css" data-wpr-hosted-gf-parameters="family=Lato:wght@400;700&display=swap"/>
<style>
.roboto-font {
font-family: 'Roboto', sans-serif;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,44 @@
'testShouldReturnOriginalWhenNotAllowed' => [
'config' => [
'is_allowed' => false,
'download_font' => 0
'download_font' => 0,
'base_url' => 'http://example.org/wp-content/cache'
],
'original' => '<html><body></body></html>',
'expected' => '<html><body></body></html>',
],
'testShouldReturnOriginalWhenNoGoogleFonts' => [
'config' => [
'is_allowed' => true,
'download_font' => 0
'download_font' => 0,
'base_url' => 'http://example.org/wp-content/cache'
],
'original' => '<html><body></body></html>',
'expected' => '<html><body></body></html>',
],
'testShouldRewriteV1Font' => [
'config' => [
'is_allowed' => true,
'download_font' => 1
'download_font' => 1,
'base_url' => 'http://example.org/wp-content/cache'
],
'original' => file_get_contents( __DIR__ . '/HTML/input_v1.php' ),
'expected' => file_get_contents( __DIR__ . '/HTML/expected_v1.php' ),
],
'testShouldRewriteV2' => [
'config' => [
'is_allowed' => true,
'download_font' => 1
'download_font' => 1,
'base_url' => 'http://example.org/wp-content/cache'
],
'original' => file_get_contents( __DIR__ . '/HTML/input_v2.php' ),
'expected' => file_get_contents( __DIR__ . '/HTML/expected_v2.php' ),
],
'testShouldRewriteV1AndV2' => [
'config' => [
'is_allowed' => true,
'download_font' => 1
'download_font' => 1,
'base_url' => 'http://example.org/wp-content/cache'
],
'original' => file_get_contents( __DIR__ . '/HTML/input_v1_v2.php' ),
'expected' => file_get_contents( __DIR__ . '/HTML/expected_v1_v2.php' ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/**
* @group HostFontsLocally
*/
class TestRewriteFonts extends FilesystemTestCase {
class Test_RewriteFonts extends FilesystemTestCase {
protected $path_to_test_data = '/inc/Engine/Media/Fonts/Frontend/Controller/rewriteFonts.php';
private $context;
private $controller;
Expand All @@ -39,6 +39,11 @@ public function testShouldDoExpected( $config, $original, $expected ) {
->once()
->andReturn( $config['is_allowed'] );

Functions\expect( 'rocket_get_constant' )
->with( 'WP_ROCKET_CACHE_URL' )
->once()
->andReturn( $config['base_url'] );

$this->font->shouldReceive('process')
->atLeast()
->times( $config['download_font'] )
Expand Down

0 comments on commit a86be50

Please sign in to comment.