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 #7258: Add self hosted font to metatag generator #7269

Merged
merged 3 commits into from
Feb 10, 2025
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
63 changes: 28 additions & 35 deletions inc/Engine/Support/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,52 +93,45 @@ public function add_meta_generator( $html ): string {
* @return string
*/
private function get_meta_tag( array $features = [] ): string {
if ( $this->options->get( 'do_caching_mobile_files', 0 ) ) {
$options = $this->options;

// Feature mapping for meta tags.
$features_to_check = [
'wpr_preload_links' => 'preload_links',
'wpr_host_fonts_locally' => 'host_fonts_locally',
];

foreach ( $features_to_check as $meta_name => $option_name ) {
if ( $options->get( $option_name, false ) ) {
$features[] = $meta_name;
}
}

// Mobile/Desktop caching.
if ( $options->get( 'do_caching_mobile_files', false ) ) {
$features[] = $this->mobile_detect->isMobile() ? 'wpr_mobile' : 'wpr_desktop';
}

// CDN & DNS prefetch check.
$dns_prefetch = rocket_get_dns_prefetch_domains();

if (
(
! $this->options->get( 'cdn', 0 )
&&
! empty( $dns_prefetch )
)
||
(
$this->options->get( 'cdn', 0 )
&&
count( $dns_prefetch ) > 1
)
) {
if ( $dns_prefetch && ( ! $options->get( 'cdn', false ) || count( $dns_prefetch ) > 1 ) ) {
$features[] = 'wpr_dns_prefetch';
}

if ( (bool) $this->options->get( 'preload_links', 0 ) ) {
$features[] = 'wpr_preload_links';
}

if ( empty( $features ) ) {
if ( ! $features ) {
return '';
}

$version = '';

/**
* Filters the display of WP Rocket version in the content attribute of the meta generator tag.
*
* @since 3.17.2
*
* @param bool $display True to display, false otherwise.
*/
if ( wpm_apply_filters_typed( 'boolean', 'rocket_display_meta_generator_content_version', true ) ) {
$version = ' ' . rocket_get_constant( 'WP_ROCKET_VERSION', '' );
}

$meta = '<meta name="generator" content="WP Rocket' . $version . '" data-wpr-features="' . implode( ' ', $features ) . '" />';
// Check if WP Rocket version should be included.
$version = wpm_apply_filters_typed( 'boolean', 'rocket_display_meta_generator_content_version', true )
? ' ' . rocket_get_constant( 'WP_ROCKET_VERSION', '' )
: '';

return $meta;
return sprintf(
'<meta name="generator" content="WP Rocket%s" data-wpr-features="%s" />',
$version,
implode( ' ', $features )
);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion tests/Fixtures/inc/Engine/Support/Meta/addMetaGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
'cdn' => 0,
'do_caching_mobile_files' => 0,
'preload_links' => 0,
'host_fonts_locally' => 0,
],
'html' => '<html><head></head><body></body></html>',
'expected' => '<html><head></head><body></body></html>',
Expand All @@ -53,8 +54,9 @@
'do_caching_mobile_files' => 1,
'preload_links' => 1,
'is_mobile' => true,
'host_fonts_locally' => 1
],
'html' => '<html><head></head><body></body></html><!-- wpr_remove_unused_css -->',
'expected' => '<html><head><meta name="generator" content="WP Rocket 3.17" data-wpr-features="wpr_remove_unused_css wpr_mobile wpr_preload_links" /></head><body></body></html>',
'expected' => '<html><head><meta name="generator" content="WP Rocket 3.17" data-wpr-features="wpr_remove_unused_css wpr_preload_links wpr_host_fonts_locally wpr_mobile" /></head><body></body></html>',
],
];
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function set_up() {
add_filter( 'pre_get_rocket_option_host_fonts_locally', [ $this, 'host_fonts_locally' ] );
add_filter( 'rocket_host_fonts_locally_inline_css', [ $this, 'locally_inline_css' ] );
add_filter('rocket_exclude_locally_host_fonts', [ $this, 'exclude_locally_host_fonts' ] );
add_filter('rocket_disable_meta_generator', '__return_true');
$this->setup_http();

}
Expand All @@ -32,6 +33,7 @@ public function tear_down() {
remove_filter('pre_get_rocket_option_host_fonts_locally', [$this, 'host_fonts_locally']);
remove_filter('rocket_host_fonts_locally_inline_css', [$this, 'locally_inline_css']);
remove_filter('rocket_exclude_locally_host_fonts', [ $this, 'exclude_locally_host_fonts' ] );
remove_filter('rocket_disable_meta_generator', '__return_true');

$this->restoreWpHook('rocket_buffer');
$this->tear_down_http();
Expand Down
6 changes: 6 additions & 0 deletions tests/Unit/inc/Engine/Support/Meta/addMetaGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ public function testShouldReturnExpected( $config, $html, $expected ) {
->andReturn( $config['preload_links'] );
}

if ( isset( $config['host_fonts_locally'] ) ) {
$this->options->shouldReceive( 'get' )
->with( 'host_fonts_locally', 0 )
->andReturn( $config['host_fonts_locally'] );
}

$this->mobile_detect->shouldReceive( 'isMobile' )
->andReturn( $config['is_mobile'] ?? false );

Expand Down
Loading