add wp-rocket
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Rocket\Engine\Optimization\GoogleFonts\Admin;
|
||||
|
||||
use WP_Rocket\Abstract_Render;
|
||||
use WP_Rocket\Admin\Options_Data;
|
||||
use WP_Rocket\Engine\Admin\Beacon\Beacon;
|
||||
|
||||
class Settings extends Abstract_Render {
|
||||
/**
|
||||
* WP Rocket options instance
|
||||
*
|
||||
* @var Options_Data
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* Beacon instance
|
||||
*
|
||||
* @var Beacon
|
||||
*/
|
||||
private $beacon;
|
||||
|
||||
/**
|
||||
* Instantiate the class
|
||||
*
|
||||
* @param Options_Data $options WP Rocket options instance.
|
||||
* @param Beacon $beacon Beacon instance.
|
||||
* @param string $template_path Path to template files.
|
||||
*/
|
||||
public function __construct( Options_Data $options, Beacon $beacon, $template_path ) {
|
||||
parent::__construct( $template_path );
|
||||
|
||||
$this->options = $options;
|
||||
$this->beacon = $beacon;
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays the Google Fonts Optimization section in the tools tab
|
||||
*
|
||||
* @since 3.7
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function display_google_fonts_enabler() {
|
||||
if ( ! current_user_can( 'rocket_manage_options' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! apply_filters( 'pre_get_rocket_option_minify_google_fonts', true ) ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
|
||||
return;
|
||||
}
|
||||
|
||||
if ( $this->options->get( 'minify_google_fonts', 0 ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$data = [
|
||||
'beacon' => $this->beacon->get_suggest( 'google_fonts' ),
|
||||
];
|
||||
|
||||
echo $this->generate( 'settings/enable-google-fonts', $data ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback method for the AJAX request to enable Google Fonts Optimization
|
||||
*
|
||||
* @since 3.7
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function enable_google_fonts() {
|
||||
check_ajax_referer( 'rocket-ajax', 'nonce', true );
|
||||
|
||||
if ( ! current_user_can( 'rocket_manage_options' ) ) {
|
||||
wp_send_json_error();
|
||||
return;
|
||||
}
|
||||
|
||||
$this->options->set( 'minify_google_fonts', 1 );
|
||||
update_option( rocket_get_constant( 'WP_ROCKET_SLUG', 'wp_rocket_settings' ), $this->options->get_options() );
|
||||
|
||||
wp_send_json_success();
|
||||
}
|
||||
}
|
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Rocket\Engine\Optimization\GoogleFonts\Admin;
|
||||
|
||||
use WP_Rocket\Event_Management\Subscriber_Interface;
|
||||
|
||||
class Subscriber implements Subscriber_Interface {
|
||||
/**
|
||||
* Google Fonts Settings instance
|
||||
*
|
||||
* @var Settings
|
||||
*/
|
||||
private $settings;
|
||||
|
||||
/**
|
||||
* Instantiate the class
|
||||
*
|
||||
* @param Settings $settings Google Fonts Settings instance.
|
||||
*/
|
||||
public function __construct( Settings $settings ) {
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of events that this subscriber wants to listen to.
|
||||
*
|
||||
* @since 3.7
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_subscribed_events() {
|
||||
return [
|
||||
'rocket_settings_tools_content' => 'display_google_fonts_enabler',
|
||||
'wp_ajax_rocket_enable_google_fonts' => 'enable_google_fonts',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays the Google Fonts Optimization section in the tools tab
|
||||
*
|
||||
* @since 3.7
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function display_google_fonts_enabler() {
|
||||
$this->settings->display_google_fonts_enabler();
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback method for the AJAX request to enable Google Fonts Optimization
|
||||
*
|
||||
* @since 3.7
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function enable_google_fonts() {
|
||||
$this->settings->enable_google_fonts();
|
||||
}
|
||||
}
|
@@ -0,0 +1,234 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Rocket\Engine\Optimization\GoogleFonts;
|
||||
|
||||
use WP_Rocket\Logger\Logger;
|
||||
use WP_Rocket\Engine\Optimization\AbstractOptimization;
|
||||
|
||||
/**
|
||||
* Combine Google Fonts
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*/
|
||||
class Combine extends AbstractOptimization {
|
||||
/**
|
||||
* Found fonts
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $fonts = '';
|
||||
|
||||
/**
|
||||
* Found subsets
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $subsets = '';
|
||||
|
||||
/**
|
||||
* Combines multiple Google Fonts links into one
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $html HTML content.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function optimize( $html ) {
|
||||
Logger::info( 'GOOGLE FONTS COMBINE PROCESS STARTED.', [ 'GF combine process' ] );
|
||||
|
||||
$html_nocomments = $this->hide_comments( $html );
|
||||
$fonts = $this->find( '<link(?:\s+(?:(?!href\s*=\s*)[^>])+)?(?:\s+href\s*=\s*([\'"])(?<url>(?:https?:)?\/\/fonts\.googleapis\.com\/css(?:(?!\1).)+)\1)(?:\s+[^>]*)?>', $html_nocomments );
|
||||
|
||||
if ( ! $fonts ) {
|
||||
Logger::debug( 'No Google Fonts found.', [ 'GF combine process' ] );
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
$num_fonts = count( $fonts );
|
||||
|
||||
Logger::debug(
|
||||
"Found {$num_fonts} Google Fonts.",
|
||||
[
|
||||
'GF combine process',
|
||||
'tags' => $fonts,
|
||||
]
|
||||
);
|
||||
|
||||
if ( 1 === $num_fonts ) {
|
||||
return str_replace( $fonts[0][0], $this->get_font_with_display( $fonts[0] ), $html );
|
||||
}
|
||||
|
||||
$this->parse( $fonts );
|
||||
|
||||
if ( empty( $this->fonts ) ) {
|
||||
Logger::debug( 'No Google Fonts left to combine.', [ 'GF combine process' ] );
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
$html = preg_replace( '@<\/title>@i', '$0' . $this->get_combine_tag(), $html, 1 );
|
||||
|
||||
foreach ( $fonts as $font ) {
|
||||
$html = str_replace( $font[0], '', $html );
|
||||
}
|
||||
|
||||
Logger::info(
|
||||
'Google Fonts successfully combined.',
|
||||
[
|
||||
'GF combine process',
|
||||
'url' => $this->fonts . $this->subsets,
|
||||
]
|
||||
);
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds links to Google fonts
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $pattern Pattern to search for.
|
||||
* @param string $html HTML content.
|
||||
*
|
||||
* @return bool|array
|
||||
*/
|
||||
protected function find( $pattern, $html ) {
|
||||
$result = preg_match_all( '/' . $pattern . '/Umsi', $html, $matches, PREG_SET_ORDER );
|
||||
|
||||
if ( empty( $result ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $matches;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses found matches to extract fonts and subsets.
|
||||
*
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param array $matches Found matches for the pattern.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function parse( array $matches ) {
|
||||
$fonts_array = [];
|
||||
$subsets_array = [];
|
||||
foreach ( $matches as $match ) {
|
||||
$url = html_entity_decode( $match[2] );
|
||||
$query = wp_parse_url( $url, PHP_URL_QUERY );
|
||||
if ( empty( $query ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$font = wp_parse_args( $query );
|
||||
if ( isset( $font['family'] ) ) {
|
||||
$font_family = $font['family'];
|
||||
$font_family = rtrim( $font_family, '%7C' );
|
||||
$font_family = rtrim( $font_family, '|' );
|
||||
// Add font to the collection.
|
||||
$fonts_array[] = rawurlencode( htmlentities( $font_family ) );
|
||||
}
|
||||
|
||||
// Add subset to collection.
|
||||
if ( isset( $font['subset'] ) ) {
|
||||
$subsets_array[] = rawurlencode( htmlentities( $font['subset'] ) );
|
||||
}
|
||||
}
|
||||
|
||||
// Concatenate fonts tag.
|
||||
$this->subsets = ! empty( $subsets_array ) ? '&subset=' . implode( ',', array_filter( array_unique( $subsets_array ) ) ) : '';
|
||||
$this->fonts = ! empty( $fonts_array ) ? implode( '%7C', array_filter( array_unique( $fonts_array ) ) ) : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the combined Google fonts link tag
|
||||
*
|
||||
* @since 3.3.5 Add support for the display parameter
|
||||
* @since 3.1
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_combine_tag() {
|
||||
$display = $this->get_font_display_value();
|
||||
|
||||
return sprintf(
|
||||
'<link rel="stylesheet" href="%s" />', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet
|
||||
esc_url( "https://fonts.googleapis.com/css?family={$this->fonts}{$this->subsets}&display={$display}" )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns font with display value.
|
||||
*
|
||||
* @since 3.5.1
|
||||
* @author Soponar Cristina
|
||||
*
|
||||
* @param array $font Array containing font tag and matches.
|
||||
*
|
||||
* @return string Google Font tag with display param.
|
||||
*/
|
||||
protected function get_font_with_display( array $font ) {
|
||||
$font_url = html_entity_decode( $font['url'] );
|
||||
$query = wp_parse_url( $font_url, PHP_URL_QUERY );
|
||||
|
||||
if ( empty( $query ) ) {
|
||||
return $font[0];
|
||||
}
|
||||
|
||||
$display = $this->get_font_display_value();
|
||||
$parsed_font = wp_parse_args( $query );
|
||||
|
||||
$font_url = ! empty( $parsed_font['display'] )
|
||||
? str_replace( "&display={$parsed_font['display']}", "&display={$display}", $font_url )
|
||||
: "{$font_url}&display={$display}";
|
||||
|
||||
return str_replace( $font['url'], esc_url( $font_url ), $font[0] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the font display value.
|
||||
*
|
||||
* @since 3.5.1
|
||||
*
|
||||
* @return string font display value.
|
||||
*/
|
||||
protected function get_font_display_value() {
|
||||
$allowed_values = [
|
||||
'auto' => 1,
|
||||
'block' => 1,
|
||||
'swap' => 1,
|
||||
'fallback' => 1,
|
||||
'optional' => 1,
|
||||
];
|
||||
|
||||
/**
|
||||
* Filters the combined Google Fonts display parameter value
|
||||
*
|
||||
* @since 3.3.5
|
||||
* @author Remy Perona
|
||||
*
|
||||
* @param string $display Display value. Can be either auto, block, swap, fallback or optional.
|
||||
*/
|
||||
$display = apply_filters( 'rocket_combined_google_fonts_display', 'swap' );
|
||||
if ( ! is_string( $display ) ) {
|
||||
return 'swap';
|
||||
}
|
||||
|
||||
return isset( $allowed_values[ $display ] ) ? $display : 'swap';
|
||||
}
|
||||
}
|
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
namespace WP_Rocket\Engine\Optimization\GoogleFonts;
|
||||
|
||||
use WP_Rocket\Admin\Options_Data;
|
||||
use WP_Rocket\Event_Management\Subscriber_Interface;
|
||||
|
||||
/**
|
||||
* Combine Google Fonts subscriber
|
||||
*
|
||||
* @since 3.1
|
||||
*/
|
||||
class Subscriber implements Subscriber_Interface {
|
||||
/**
|
||||
* Plugin options.
|
||||
*
|
||||
* @var Options_Data
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* Combine instance.
|
||||
*
|
||||
* @var Combine
|
||||
*/
|
||||
private $combine;
|
||||
|
||||
/**
|
||||
* Instantiate the subscirber
|
||||
*
|
||||
* @param Combine $combine Combine instance.
|
||||
* @param Options_Data $options Options_Data instance.
|
||||
*/
|
||||
public function __construct( Combine $combine, Options_Data $options ) {
|
||||
$this->combine = $combine;
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of events that this subscriber wants to listen to.
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_subscribed_events() {
|
||||
return [
|
||||
'wp_resource_hints' => [ 'preconnect', 10, 2 ],
|
||||
'rocket_buffer' => [ 'process', 18 ],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds google fonts URL to preconnect
|
||||
*
|
||||
* @since 3.5.3
|
||||
*
|
||||
* @param array $urls URLs to print for resource hints.
|
||||
* @param string $relation_type The relation type the URLs are printed for, e.g. 'preconnect' or 'prerender'.
|
||||
* @return array
|
||||
*/
|
||||
public function preconnect( array $urls, $relation_type ) {
|
||||
if ( ! $this->is_allowed() ) {
|
||||
return $urls;
|
||||
}
|
||||
|
||||
if ( 'preconnect' !== $relation_type ) {
|
||||
return $urls;
|
||||
}
|
||||
|
||||
$urls[] = [
|
||||
'href' => 'https://fonts.gstatic.com',
|
||||
1 => 'crossorigin',
|
||||
];
|
||||
|
||||
return $urls;
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes the HTML to combine found Google fonts
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @param string $html HTML content.
|
||||
* @return string
|
||||
*/
|
||||
public function process( $html ) {
|
||||
if ( ! $this->is_allowed() ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
return $this->combine->optimize( $html );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if files can combine found Google fonts.
|
||||
*
|
||||
* @since 3.1
|
||||
*/
|
||||
protected function is_allowed() {
|
||||
if ( rocket_bypass() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool) $this->options->get( 'minify_google_fonts', 0 );
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user