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 #7065: 3.18 - Filter changing output #7107

Merged
Merged
Show file tree
Hide file tree
Changes from 6 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
60 changes: 56 additions & 4 deletions inc/Engine/Media/Fonts/Frontend/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use WP_Rocket\Engine\Media\Fonts\Context\Context;
use WP_Rocket\Engine\Optimization\RegexTrait;
use WP_Rocket\Logger\Logger;
use WP_Filesystem_Direct;

class Controller {
use RegexTrait;
Expand All @@ -24,14 +25,32 @@ class Controller {
*/
private $base_url;


/**
* WordPress filesystem.
*
* @var WP_Filesystem_Direct
*/
private $filesystem;

/**
* Base path.
*
* @var string
*/
private $base_path;

/**
* Constructor.
*
* @param Context $context Context instance.
* @param Context $context Context instance.
* @param WP_Filesystem_Direct|null $filesystem WordPress filesystem.
*/
public function __construct( Context $context ) {
$this->context = $context;
$this->base_url = rocket_get_constant( 'WP_ROCKET_CACHE_ROOT_URL', '' ) . 'fonts/' . get_current_blog_id() . '/';
public function __construct( Context $context, ?WP_Filesystem_Direct $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 = ! empty( $filesystem ) ? $filesystem : rocket_direct_filesystem();
}

/**
Expand Down Expand Up @@ -104,6 +123,15 @@ protected function get_optimized_markup( string $hash, string $original_url ): s

$gf_parameters = wp_parse_url( $original_url, PHP_URL_QUERY );

$internal_styling = wpm_apply_filters_typed( 'boolean', 'rocket_internal_fonts_styling', false );
if ( $internal_styling ) {
$raw_path = $this->base_path . $path . '.css';
$internal_style = $this->set_font_internal_style( $gf_parameters, $raw_path );
if ( $internal_style ) {
return $internal_style;
}
}

return sprintf(
'<link rel="stylesheet" href="%1$s" data-wpr-hosted-gf-parameters="%2$s"/>', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet
$url,
Expand All @@ -125,4 +153,28 @@ public function disable_google_fonts_preload( $disable ): bool {

return true;
}

/**
* Sets the font internal style.
*
* @param string $gf_parameters Google Fonts parameters.
* @param string $css_path CSS file path.
*
* @return string|bool
*/
private function set_font_internal_style( string $gf_parameters, string $css_path ) {
if ( ! $css_path ) {
return false;
}

if ( ! $this->filesystem->exists( $css_path ) ) {
return false;
}

return sprintf(
'<style data-wpr-hosted-gf-parameters="%1$s">%2$s</style>',
$gf_parameters,
$this->filesystem->get_contents( $css_path )
);
}
}
4 changes: 4 additions & 0 deletions inc/Engine/Media/Fonts/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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;
use WP_Filesystem_Direct;

class ServiceProvider extends AbstractServiceProvider {
/**
Expand Down Expand Up @@ -46,6 +47,8 @@ public function provides( string $id ): bool {
*/
public function register(): void {
$this->getContainer()->add( 'media_fonts_settings', Settings::class );
$this->getContainer()->add( 'wp_direct_filesystem', WP_Filesystem_Direct::class )
->addArgument( [] );
$this->getContainer()->addShared( 'media_fonts_admin_subscriber', AdminSubscriber::class )
->addArgument( 'media_fonts_settings' );

Expand All @@ -55,6 +58,7 @@ public function register(): void {
->addArguments(
[
$this->getContainer()->get( 'media_fonts_context' ),
$this->getContainer()->get( 'wp_direct_filesystem' ),
]
);
$this->getContainer()->add( 'media_fonts_frontend_subscriber', FrontendSubscriber::class )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Template Name: Google Font V1 Template
*/ ?>

<!DOCTYPE html>
<html>
<head>
<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/e/b/c/173c0fc97eef86a6e51ada56c5a9a.css" data-wpr-hosted-gf-parameters="family=Roboto"/>
<link rel="stylesheet" href="http://example.org/wp-content/cache/fonts/1/5/9/5/cb6ccb56826a802ed411cef875f0e.css" data-wpr-hosted-gf-parameters="family=Open+Sans"/>
<style>
.roboto-font {
font-family: 'Roboto', sans-serif;
}
.open-sans-font {
font-family: 'Open Sans', sans-serif;
}
</style>
</head>
<body>
<div class="wrap">
<div id="primary" class="content-area">
<main id="main" class="site-main">
<h1 class="roboto-font">Hello World</h1>
<p class="open-sans-font">Welcome to the world</p>
</main><!-- #main -->
</div><!-- #primary -->
</div><!-- .wrap -->
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Template Name: Google Font V1 Template
*/ ?>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Google Font V1 Template</title>
<style data-wpr-hosted-gf-parameters="family=Roboto">body { font-family: "Roboto"; }</style>
<style data-wpr-hosted-gf-parameters="family=Open+Sans">body { font-family: "Open-San"; }</style>
<style>
.roboto-font {
font-family: 'Roboto', sans-serif;
}
.open-sans-font {
font-family: 'Open Sans', sans-serif;
}
</style>
</head>
<body>
<div class="wrap">
<div id="primary" class="content-area">
<main id="main" class="site-main">
<h1 class="roboto-font">Hello World</h1>
<p class="open-sans-font">Welcome to the world</p>
</main><!-- #main -->
</div><!-- #primary -->
</div><!-- .wrap -->
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Template Name: Google Font V1 and V2 Template
*/ ?>

<!DOCTYPE html>
<html>
<head>
<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/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/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;
}
.v1-font-open-sans {
font-family: 'Open Sans', sans-serif;
}
.v2-font-lato {
font-family: 'Lato', sans-serif;
}
.v2-font-montserrat {
font-family: 'Montserrat', sans-serif;
}
</style>
</head>
<body>
<div class="wrap">
<div id="primary" class="content-area">
<main id="main" class="site-main">
<h1 class="v1-font-roboto">Hello World</h1>
<p class="v1-font-open-sans">Welcome to the world</p>
<h2 class="v2-font-lato">This is a subtitle</h2>
<p class="v2-font-montserrat">Enjoy your stay</p>
</main><!-- #main -->
</div><!-- #primary -->
</div><!-- .wrap -->
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Template Name: Google Font V2 Template
*/ ?>

<!DOCTYPE html>
<html>
<head>
<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/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/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;
}
.lato-font {
font-family: 'Lato', sans-serif;
}
</style>
</head>
<body>
<div class="wrap">
<div id="primary" class="content-area">
<main id="main" class="site-main">
<h1 class="roboto-font">Hello World</h1>
<p class="lato-font">Welcome to the world</p>
</main><!-- #main -->
</div><!-- #primary -->
</div><!-- .wrap -->
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Template Name: Google Font V1 Template
*/ ?>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Google Font V1 Template</title>
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<style>
.roboto-font {
font-family: 'Roboto', sans-serif;
}
.open-sans-font {
font-family: 'Open Sans', sans-serif;
}
</style>
</head>
<body>
<div class="wrap">
<div id="primary" class="content-area">
<main id="main" class="site-main">
<h1 class="roboto-font">Hello World</h1>
<p class="open-sans-font">Welcome to the world</p>
</main><!-- #main -->
</div><!-- #primary -->
</div><!-- .wrap -->
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Template Name: Google Font V1 and V2 Template
*/ ?>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Google Font V1 and V2 Template</title>
<link href="https://fonts.googleapis.com/css?family=Roboto|Open+Sans" rel="stylesheet"> <!-- V1 Fonts -->
<link href="https://fonts.googleapis.com/css2?family=Lato:wght@400;700&family=Montserrat:wght@400;700&display=swap" rel="stylesheet"> <!-- V2 Fonts -->
<style>
.v1-font-roboto {
font-family: 'Roboto', sans-serif;
}
.v1-font-open-sans {
font-family: 'Open Sans', sans-serif;
}
.v2-font-lato {
font-family: 'Lato', sans-serif;
}
.v2-font-montserrat {
font-family: 'Montserrat', sans-serif;
}
</style>
</head>
<body>
<div class="wrap">
<div id="primary" class="content-area">
<main id="main" class="site-main">
<h1 class="v1-font-roboto">Hello World</h1>
<p class="v1-font-open-sans">Welcome to the world</p>
<h2 class="v2-font-lato">This is a subtitle</h2>
<p class="v2-font-montserrat">Enjoy your stay</p>
</main><!-- #main -->
</div><!-- #primary -->
</div><!-- .wrap -->
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Template Name: Google Font V2 Template
*/ ?>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Google Font V2 Template</title>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Lato:wght@400;700&display=swap" rel="stylesheet">
<style>
.roboto-font {
font-family: 'Roboto', sans-serif;
}
.lato-font {
font-family: 'Lato', sans-serif;
}
</style>
</head>
<body>
<div class="wrap">
<div id="primary" class="content-area">
<main id="main" class="site-main">
<h1 class="roboto-font">Hello World</h1>
<p class="lato-font">Welcome to the world</p>
</main><!-- #main -->
</div><!-- #primary -->
</div><!-- .wrap -->
</body>
</html>
Loading
Loading