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

Closes #7170 Fonts download data measurement #7173

Merged
merged 4 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions inc/Engine/Media/Fonts/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ public function exists( string $file ): bool {
}

/**
* Writes font css to path
* Writes CSS & fonts locally
*
* @param string $font_url The font url to save locally.
* @param string $provider The url of the page.
* @param string $css_url The CSS url to save locally.
* @param string $provider The font provider.
*
* @return bool
*/
public function write_font_css( string $font_url, string $provider ): bool {
public function write_font_css( string $css_url, string $provider ): bool {
$font_provider_path = $this->get_font_provider_path( $provider );
$css_filepath = $this->get_absolute_path( $font_provider_path, 'css/' . $this->hash_to_path( $this->hash_url( $font_url ) ) . '.css' );
$css_filepath = $this->get_absolute_path( $font_provider_path, 'css/' . $this->hash_to_path( $this->hash_url( $css_url ) ) . '.css' );
$fonts_basepath = $this->get_absolute_path( $font_provider_path, 'fonts' );

if ( ! rocket_mkdir_p( dirname( $css_filepath ) ) ) {
Expand All @@ -74,7 +74,7 @@ public function write_font_css( string $font_url, string $provider ): bool {

$start_time = microtime( true );

$css_content = $this->get_remote_content( html_entity_decode( $font_url ) );
$css_content = $this->get_remote_content( html_entity_decode( $css_url ) );

if ( ! $css_content ) {
return false;
Expand All @@ -84,6 +84,9 @@ public function write_font_css( string $font_url, string $provider ): bool {
$font_urls = $matches[1];
$local_css = $css_content;

$count_fonts = 0;
$download_average = 0;

foreach ( $font_urls as $font_url ) {
$font_path = wp_parse_url( $font_url, PHP_URL_PATH );

Expand All @@ -99,6 +102,8 @@ public function write_font_css( string $font_url, string $provider ): bool {
}

if ( ! $this->filesystem->exists( $local_path ) ) {
$download_start = microtime( true );

$font_content = $this->get_remote_content( $font_url );

if ( ! $font_content ) {
Expand All @@ -107,6 +112,15 @@ public function write_font_css( string $font_url, string $provider ): bool {
}

$this->write_file( $local_path, $font_content );

$download_end = microtime( true );
$download_time = $download_end - $download_start;

$download_average += $download_time;

++$count_fonts;

Logger::debug( "Font $font_url download duration -- $download_time", [ 'Host Fonts Locally' ] );
}

$local_url = content_url( $this->get_fonts_relative_path( $font_provider_path, $font_path ) );
Expand All @@ -118,6 +132,8 @@ public function write_font_css( string $font_url, string $provider ): bool {

// Add for test purpose.
Logger::debug( "Font download and optimization duration in seconds -- $duration", [ 'Host Fonts Locally' ] );
Logger::debug( "Number of fonts downloaded for $css_url -- $count_fonts", [ 'Host Fonts Locally' ] );
Logger::debug( 'Average download time per font -- ' . ( $count_fonts ? $download_average / $count_fonts : 0 ), [ 'Host Fonts Locally' ] );

return $this->write_file( $css_filepath, $local_css );
}
Expand Down
2 changes: 1 addition & 1 deletion inc/Engine/Media/Fonts/Frontend/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function rewrite_fonts( $html ): string {

// Log the total execution time and number of fonts processed, with breakdown.
$duration = $end_time - $start_time;
Logger::debug( "Total execution time for Host Google Fonts Feature in seconds -- $duration. Fonts processed: $total_fonts | Total v1: $total_v1 | Total v2: $total_v2", [ 'Host Fonts Locally' ] );
Logger::debug( "Total execution time for Host Google Fonts Feature in seconds -- $duration. CSS files processed: $total_fonts | Total v1: $total_v1 | Total v2: $total_v2", [ 'Host Fonts Locally' ] );

return $html;
}
Expand Down
Loading