Skip to content

Commit

Permalink
Removed Early Access detection setting, because it's considered "obso…
Browse files Browse the repository at this point in the history
…lete and unsupported" by Google: google/fonts#5518
  • Loading branch information
Dan0sz committed Mar 27, 2024
1 parent 5f7d853 commit 4d04e1f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 48 deletions.
61 changes: 27 additions & 34 deletions src/Admin/Settings/Detection.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public function __construct() {
* Description
*/
public function do_description() { ?>
<p>
<p>
<?php echo __(
'These settings affect the detection mechanism and in which areas it searches (i.e. how deep it digs) to find Google Fonts. If you want to remove (instead of replace) the Google Fonts your WordPress configuration currently uses, set <strong>Google Fonts Processing</strong> to Remove.',
'host-webfonts-local'
); ?>
</p>
</p>
<?php
}

Expand All @@ -57,20 +57,20 @@ public function do_description() { ?>
*/
public function google_fonts_processing() {
?>
<tr>
<th scope="row"><?php echo __( 'Google Fonts Processing', 'host-webfonts-local' ); ?></th>
<td>
<p class="description">
<tr>
<th scope="row"><?php echo __( 'Google Fonts Processing', 'host-webfonts-local' ); ?></th>
<td>
<p class="description">
<?php echo sprintf(
__(
'By default, OMGF replaces Google Fonts stylesheets (e.g. <code>https://fonts.googleapis.com/css?family=Open+Sans</code>) with locally hosted copies. This behavior can be tweaked further using the <strong>Advanced Processing (Pro)</strong> option. To remove/unload Google Fonts, go to <em>Local Fonts</em> > <a href="%s"><em>Optimize Local Fonts</em></a> and click <strong>Unload all</strong> next to the stylesheet handle you\'d like to remove.',
'host-webfonts-local'
),
admin_url( 'options-general.php?page=optimize-webfonts&tab=omgf-optimize-settings#omgf-manage-optimized-fonts' )
); ?>
</p>
</td>
</tr>
</p>
</td>
</tr>
<?php
}

Expand All @@ -79,26 +79,26 @@ public function google_fonts_processing() {
*/
public function promo_advanced_processing() {
?>
<tr>
<th scope="row"><?php echo __( 'Advanced Processing (Pro)', 'host-webfonts-local' ); ?></th>
<td>
<fieldset id="" class="scheme-list">
<tr>
<th scope="row"><?php echo __( 'Advanced Processing (Pro)', 'host-webfonts-local' ); ?></th>
<td>
<fieldset id="" class="scheme-list">
<?php foreach ( $this->advanced_processing_pro_options() as $name => $data ) : ?>
<?php
$checked = ! empty( OMGF::get_option( $name ) );
$disabled = ! defined( 'OMGF_PRO_ACTIVE' ) ? 'disabled' : '';
?>
<label for="<?php echo esc_attr( $name ); ?>">
<input type="hidden" name="omgf_settings[<?php echo esc_attr( $name ); ?>]" value="0"/>
<input type="checkbox" name="omgf_settings[<?php echo esc_attr( $name ); ?>]"
id="<?php echo esc_attr( $name ); ?>" <?php echo esc_attr(
<label for="<?php echo esc_attr( $name ); ?>">
<input type="hidden" name="omgf_settings[<?php echo esc_attr( $name ); ?>]" value="0"/>
<input type="checkbox" name="omgf_settings[<?php echo esc_attr( $name ); ?>]"
id="<?php echo esc_attr( $name ); ?>" <?php echo esc_attr(
$checked ? 'checked="checked"' : ''
); ?> <?php echo esc_attr( $disabled ); ?> value="on"/><?php echo wp_kses( $data[ 'label' ], $this->allowed_html ); ?>
&nbsp;
</label>
&nbsp;
</label>
<?php endforeach; ?>
</fieldset>
<p class="description">
</fieldset>
<p class="description">
<?php echo apply_filters(
'omgf_detection_settings_advanced_processing_description',
sprintf(
Expand All @@ -110,17 +110,17 @@ public function promo_advanced_processing() {
$this->promo
)
); ?>
</p>
<ul>
</p>
<ul>
<?php foreach ( $this->advanced_processing_pro_options() as $name => $data ) : ?>
<li><strong><?php echo wp_kses( $data[ 'label' ], $this->allowed_html ); ?></strong>: <?php echo wp_kses(
<li><strong><?php echo wp_kses( $data[ 'label' ], $this->allowed_html ); ?></strong>: <?php echo wp_kses(
$data[ 'description' ],
$this->allowed_html
); ?></li>
<?php endforeach; ?>
</ul>
</td>
</tr>
</ul>
</td>
</tr>
<?php
}

Expand Down Expand Up @@ -160,13 +160,6 @@ private function advanced_processing_pro_options() {
'host-webfonts-local'
),
],
'process_early_access' => [
'label' => __( 'Process Early Access', 'host-webfonts-local' ),
'description' => __(
'Process Google Fonts loaded from <code>fonts.googleapis.com/earlyaccess</code> or <code>fonts.gstatic.com/ea</code>.',
'host-webfonts-local'
),
],
];
}
}
19 changes: 18 additions & 1 deletion src/Frontend/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class Process {

/**
* Populates ?edit= parameter. To make sure OMGF doesn't run while editing posts.
*
* @var string[]
*/
private $edit_actions = [
Expand All @@ -76,13 +77,15 @@ class Process {
/**
* Break out early, e.g. if we want to parse other resources and don't need to
* set up all the hooks and filters.
*
* @since v5.4.0
* @var bool $break
*/
private $break = false;

/**
* OMGF_Frontend_Functions constructor.
*
* @var $break bool
*/
public function __construct( $break = false ) {
Expand All @@ -100,6 +103,7 @@ public function __construct( $break = false ) {

/**
* Actions and hooks.
*
* @return void
*/
private function init() {
Expand Down Expand Up @@ -213,6 +217,7 @@ function ( $variant ) use ( $preloads_stylesheet, $font_id ) {

/**
* Start output buffer.
*
* @action template_redirect
* @return bool|string valid HTML.
*/
Expand Down Expand Up @@ -246,6 +251,7 @@ public function maybe_buffer_output() {

/**
* Honor PageSpeed=off parameter as used by mod_pagespeed, in use by some pagebuilders,
*
* @see https://www.modpagespeed.com/doc/experiment#ModPagespeed
*/
if ( array_key_exists( 'PageSpeed', $_GET ) && 'off' === $_GET[ 'PageSpeed' ] ) {
Expand All @@ -269,6 +275,7 @@ public function maybe_buffer_output() {

/**
* Returns the buffer for filtering, so page cache doesn't break.
*
* @since v5.0.0 Tested with:
* - Asset Cleanup Pro
* - Works
Expand Down Expand Up @@ -304,6 +311,7 @@ public function return_buffer( $html ) {

/**
* We're downloading the fonts, so preconnecting to Google is a waste of time. Literally.
*
* @since v5.0.5 Use a regular expression to match all resource hints.
*
* @param string $html Valid HTML.
Expand Down Expand Up @@ -403,6 +411,7 @@ function ( $link ) {
/**
* Use string position of $search to make sure only that instance of the string is replaced.
* This is to prevent duplicate replaces.
*
* @since v5.3.7
*/
foreach ( $search_replace[ 'search' ] as $key => $search ) {
Expand Down Expand Up @@ -477,6 +486,7 @@ public function build_fonts_set( $links, $handle = 'omgf-stylesheet' ) {
* If no valid id attribute was found then this means that this stylesheet wasn't enqueued
* using proper WordPress conventions. We generate our own using the length of the href attribute
* to serve as a UID. This prevents clashes with other non-properly enqueued stylesheets on other pages.
*
* @since v5.1.4
*/
if ( ! $id ) {
Expand All @@ -485,6 +495,7 @@ public function build_fonts_set( $links, $handle = 'omgf-stylesheet' ) {

/**
* Compatibility fix for Divi Builder
*
* @since v5.1.3 Because Divi Builder uses the same handle for Google Fonts on each page,
* even when these contain Google Fonts, let's append a (kind of) unique
* identifier to the string, to make sure we can make a difference between
Expand All @@ -498,6 +509,7 @@ public function build_fonts_set( $links, $handle = 'omgf-stylesheet' ) {
} elseif ( OMGF::get_option( Settings::OMGF_ADV_SETTING_COMPATIBILITY ) && $id === 'google-fonts-1' ) {
/**
* Compatibility fix for Elementor
*
* @since v5.1.4 Because Elementor uses the same (annoyingly generic) handle for Google Fonts
* stylesheets on each page, even when these contain different Google Fonts than
* other pages, let's append a (kind of) unique identifier to the string, to make
Expand All @@ -507,6 +519,7 @@ public function build_fonts_set( $links, $handle = 'omgf-stylesheet' ) {
} elseif ( str_contains( $id, 'sp-wpcp-google-fonts' ) ) {
/**
* Compatibility fix for Category Slider Pro for WooCommerce by ShapedPlugin
*
* @since v5.3.7 This plugin finds it necessary to provide each Google Fonts stylesheet with a
* unique identifier on each pageload, to make sure its never cached. The worst idea ever.
* On top of that, it throws OMGF off the rails entirely, eventually crashing the site.
Expand All @@ -515,12 +528,14 @@ public function build_fonts_set( $links, $handle = 'omgf-stylesheet' ) {
} elseif ( str_contains( $id, 'sp-lc-google-fonts' ) ) {
/**
* Compatibility fix for Logo Carousel Pro by ShapedPlugin
*
* @since v5.3.8 Same reason as above.
*/
$google_fonts[ $key ][ 'id' ] = 'sp-lc-google-fonts';
} elseif ( apply_filters( 'omgf_frontend_process_convert_pro_compatibility', str_contains( $id, 'cp-google-fonts' ) ) ) {
/**
* Compatibility fix for Convert Pro by Brainstorm Force
*
* @since v5.5.4 Same reason as above, although it kind of makes sense in this case (since Convert Pro allows
* to create pop-ups and people tend to get creative. I just hope the ID isn't random.)
* @filter omgf_frontend_process_convert_pro_compatibility Allows people to disable this feature, in case the different
Expand All @@ -543,6 +558,7 @@ public function build_fonts_set( $links, $handle = 'omgf-stylesheet' ) {

/**
* Strip "-css" from the end of the stylesheet id, which WordPress adds to properly enqueued stylesheets.
*
* @since v5.0.1 This eases the migration from v4.6.0.
*
* @param mixed $handle
Expand Down Expand Up @@ -626,7 +642,7 @@ public function build_search_replace( $google_fonts ) {
/**
* If required parameters aren't set, this request is most likely invalid. Let's just remove it.
*/
if ( ! isset( $query[ 'family' ] ) ) {
if ( apply_filters( 'omgf_frontend_process_invalid_request', ! isset( $query[ 'family' ] ), $href ) ) {
$search[ $key ] = $stack[ 'link' ];
$replace[ $key ] = '';

Expand Down Expand Up @@ -676,6 +692,7 @@ public function add_success_message( $html ) {

/**
* Because all great themes come packed with extra Cumulative Layout Shifting.
*
* @since v5.4.3 Added compatibility for Highlight Pro; a Mesmerize based theme and Mesmerize,
* the non-premium theme.
*
Expand Down
22 changes: 9 additions & 13 deletions src/Optimize.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ private function convert_to_fonts_object( $stylesheet ) {

preg_match_all( '/font-family:\s\'(.*?)\';/', $stylesheet, $font_families );

if ( ! isset( $font_families[ 1 ] ) || empty( $font_families[ 1 ] ) ) {
if ( empty( $font_families[ 1 ] ) ) {
return [];
}

Expand Down Expand Up @@ -363,7 +363,7 @@ private function parse_variants( $stylesheet, $font_family ) {

$font_object = [];

foreach ( $font_faces[ 0 ] as $key => $font_face ) {
foreach ( $font_faces[ 0 ] as $font_face ) {
/**
* @since v5.3.3 Exact match for font-family attribute, to prevent similar font names from falling thru, e.g. Roboto and Roboto Slab.
*/
Expand All @@ -377,37 +377,33 @@ private function parse_variants( $stylesheet, $font_family ) {
preg_match( '/\/\*\s([a-z\-0-9\[\]]+?)\s\*\//', $font_face, $subset );
preg_match( '/unicode-range:\s(.*?);/', $font_face, $range );

$subset[ 1 ] = trim( $subset[ 1 ], '[]' );
$subset = ! empty( $subset[ 1 ] ) ? trim( $subset[ 1 ], '[]' ) : '';

/**
* @since v5.3.0 No need to keep this if this variant belongs to a subset we don't need.
*/
if ( ! empty( $subset ) &&
isset( $subset[ 1 ] ) &&
! in_array( $subset[ 1 ], OMGF::get_option( settings::OMGF_ADV_SETTING_SUBSETS ) ) &&
! is_numeric( $subset[ 1 ] ) ) {
if ( ! empty( $subset ) && ! in_array( $subset, OMGF::get_option( settings::OMGF_ADV_SETTING_SUBSETS ) ) && ! is_numeric( $subset ) ) {
continue;
}

/**
* If $subset is empty, assume it's a logographic (Chinese, Japanese, etc.) character set.
* TODO: [OMGF-87] the Used Subsets option doesn't work here. Can we make it work?
*/
if ( is_numeric( $subset[ 1 ] ) ) {
$subset[ 1 ] = 'logogram-' . $subset[ 1 ];
if ( is_numeric( $subset ) ) {
$subset = 'logogram-' . $subset;
}

$key =
$subset[ 1 ] . '-' . $font_weight[ 1 ] . ( $font_style[ 1 ] === 'normal' ? '' : '-' . $font_style[ 1 ] );
$key = $subset . '-' . $font_weight[ 1 ] . ( $font_style[ 1 ] === 'normal' ? '' : '-' . $font_style[ 1 ] );
$font_object[ $key ] = new \stdClass();
$font_object[ $key ]->id = $font_weight[ 1 ] . ( $font_style[ 1 ] === 'normal' ? '' : $font_style[ 1 ] );
$font_object[ $key ]->fontFamily = $font_family;
$font_object[ $key ]->fontStyle = $font_style[ 1 ];
$font_object[ $key ]->fontWeight = $font_weight[ 1 ];
$font_object[ $key ]->woff2 = $font_src[ 1 ];

if ( ! empty( $subset ) && isset( $subset[ 1 ] ) ) {
$font_object[ $key ]->subset = $subset[ 1 ];
if ( ! empty( $subset ) ) {
$font_object[ $key ]->subset = $subset;
}

if ( ! empty( $range ) && isset( $range[ 1 ] ) ) {
Expand Down

0 comments on commit 4d04e1f

Please sign in to comment.