Local Google Fonts - Version 0.13

Version Description

Download this release

Release Info

Developer everpress
Plugin Icon 128x128 Local Google Fonts
Version 0.13
Comparing to
See all releases

Code changes from version 0.12 to 0.13

README.md CHANGED
@@ -4,7 +4,7 @@ Contributors: everpress, xaverb
4
  Tags: googlefonts, google, fonts, gdpr, lgf, font, speed
5
  Requires at least: 4.6
6
  Tested up to: 6.0
7
- Stable tag: 0.12
8
  Requires PHP: 5.6+
9
  License: GPLv2 or later
10
  Author: EverPress
4
  Tags: googlefonts, google, fonts, gdpr, lgf, font, speed
5
  Requires at least: 4.6
6
  Tested up to: 6.0
7
+ Stable tag: 0.13
8
  Requires PHP: 5.6+
9
  License: GPLv2 or later
10
  Author: EverPress
assets/admin.css CHANGED
@@ -5,6 +5,10 @@
5
  border-radius: 4px;
6
  padding: 0.3em 0.2em;
7
  }
 
 
 
 
8
  summary {
9
  cursor: pointer;
10
  }
5
  border-radius: 4px;
6
  padding: 0.3em 0.2em;
7
  }
8
+ .font-alternative {
9
+ color: #aaa;
10
+ cursor: help;
11
+ }
12
  summary {
13
  cursor: pointer;
14
  }
includes/class-local-google-fonts-admin.php CHANGED
@@ -159,7 +159,11 @@ class LGF_Admin {
159
 
160
  <?php $fontinfo = $this->get_font_info( $data['src'], $data['handle'] ); ?>
161
 
162
- <?php if ( $fontinfo ) : ?>
 
 
 
 
163
 
164
  <table class="wp-list-table widefat fixed striped table-view-list ">
165
  <thead>
@@ -175,6 +179,9 @@ class LGF_Admin {
175
  <?php $filename = $font->id . '-' . $font->version . '-' . $font->defSubset; ?>
176
  <tr>
177
  <td><strong><?php echo esc_html( $font->family ); ?></strong><br>
 
 
 
178
  </td>
179
  <td>
180
  <p class="code">
@@ -182,9 +189,7 @@ class LGF_Admin {
182
  <span class="variant"><?php printf( '%s %s', $variant->fontStyle, $variant->fontWeight ); ?></span>
183
  <?php endforeach ?>
184
  </p>
185
- <?php
186
- $active_subsets = isset( $data['subsets'] ) ? $data['subsets'][ $font->id ] : array_keys( array_filter( (array) $font->subsetMap ) );
187
- ?>
188
  <p><strong><?php esc_html_e( 'Subsets', 'local-google-fonts' ); ?></strong><br>
189
  <?php foreach ( $font->subsetMap as $subset => $is_active ) : ?>
190
  <label title="<?php printf( esc_attr__( 'Load %s subset with this font', 'local-google-fonts' ), $subset ); ?>" class="subset"><input type="checkbox" name="subsets[<?php echo esc_attr( $data['handle'] ); ?>][<?php echo esc_attr( $font->id ); ?>][]" value="<?php echo esc_attr( $subset ); ?>" <?php checked( in_array( $subset, $active_subsets ) ); ?>> <?php echo esc_html( $subset ); ?> </label>
@@ -237,10 +242,6 @@ class LGF_Admin {
237
  <button class="host-locally button button-primary" name="hostlocal" value="<?php echo esc_attr( $data['handle'] ); ?>"><?php esc_html_e( 'Host locally', 'local-google-fonts' ); ?></button>
238
  <?php endif; ?>
239
  </p>
240
- <?php else : ?>
241
- <div class="notice inline error">
242
- <p><strong><?php esc_html_e( 'This source contains more than 30 fonts and is most likely used as helper for your theme. Skipped.', 'local-google-fonts' ); ?></strong></p>
243
- </div>
244
  <?php endif; ?>
245
 
246
  <?php endforeach ?>
@@ -309,13 +310,14 @@ class LGF_Admin {
309
 
310
  // do not load them as its most likely some helper font thing
311
  if ( count( $families ) > 30 ) {
312
- return false;
313
  }
314
 
315
  $buffer = get_option( 'local_google_fonts_buffer', array() );
316
 
317
  foreach ( $families as $family => $variants ) {
318
  $url = 'https://google-webfonts-helper.herokuapp.com/api/fonts/';
 
319
  $subsets = isset( $buffer[ $handle ]['subsets'][ $family ] ) ? implode( ',', array_filter( $buffer[ $handle ]['subsets'][ $family ] ) ) : $args['subset'];
320
  $the_url = add_query_arg(
321
  array(
@@ -323,13 +325,17 @@ class LGF_Admin {
323
  // 'variants' => implode( ',', $variants ),
324
  'subsets' => $subsets,
325
  ),
326
- $url . $family
327
  );
328
 
329
- $transient_key = 'lcg_' . md5( $the_url );
330
  if ( false === ( $info = get_transient( $transient_key ) ) ) {
331
  $response = wp_remote_get( $the_url );
332
- $code = wp_remote_retrieve_response_code( $response );
 
 
 
 
333
  if ( 200 === $code ) {
334
  $body = wp_remote_retrieve_body( $response );
335
  $info = json_decode( $body );
@@ -363,7 +369,7 @@ class LGF_Admin {
363
  '%s-%s%s',
364
  $san_family,
365
  $this->weightClass[ $variant->fontWeight ],
366
- ( $variant->fontStyle === 'italic' ? 'Italic' : '' ),
367
  );
368
 
369
  // there's no RegularItalic
@@ -372,9 +378,10 @@ class LGF_Admin {
372
  }
373
  }
374
 
375
- $filename = $id . '/' . $info->id . '-' . $info->version . '-' . $info->defSubset;
376
- $info->total = count( $info->variants ) * 5;
377
- $info->loaded = 0;
 
378
 
379
  foreach ( $info->variants as $i => $variant ) {
380
  $file = $filename . '-' . $variant->id;
@@ -390,7 +397,10 @@ class LGF_Admin {
390
 
391
  $info->variants = array_values( $info->variants );
392
  $fontinfo[] = $info;
 
393
 
 
 
394
  }
395
 
396
  return $fontinfo;
@@ -432,6 +442,20 @@ class LGF_Admin {
432
 
433
  }
434
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
435
  public function admin_footer_text( $default ) {
436
  return sprintf( esc_html__( 'If you like %1$s please leave a %2$s&#9733;&#9733;&#9733;&#9733;&#9733;%3$s rating. Thanks in advance!', 'local-google-fonts' ), '<strong>Local Google Fonts</strong>', '<a href="https://wordpress.org/support/view/plugin-reviews/local-google-fonts?filter=5#new-post" target="_blank" rel="noopener noreferrer">', '</a>' );
437
  }
159
 
160
  <?php $fontinfo = $this->get_font_info( $data['src'], $data['handle'] ); ?>
161
 
162
+ <?php if ( is_wp_error( $fontinfo ) ) : ?>
163
+ <div class="notice inline error">
164
+ <p><strong><?php echo esc_html( $fontinfo->get_error_message() ); ?></strong></p>
165
+ </div>
166
+ <?php else : ?>
167
 
168
  <table class="wp-list-table widefat fixed striped table-view-list ">
169
  <thead>
179
  <?php $filename = $font->id . '-' . $font->version . '-' . $font->defSubset; ?>
180
  <tr>
181
  <td><strong><?php echo esc_html( $font->family ); ?></strong><br>
182
+ <?php if ( $font->id != $font->original ) : ?>
183
+ <span class="font-alternative" title="<?php esc_attr_e( 'This is the best alternative for a font no longer supported.', 'local-google-fonts' ); ?>"><?php esc_html_e( 'alternative', 'local-google-fonts' ); ?></span>
184
+ <?php endif; ?>
185
  </td>
186
  <td>
187
  <p class="code">
189
  <span class="variant"><?php printf( '%s %s', $variant->fontStyle, $variant->fontWeight ); ?></span>
190
  <?php endforeach ?>
191
  </p>
192
+ <?php $active_subsets = isset( $data['subsets'] ) ? $data['subsets'][ $font->id ] : array_keys( array_filter( (array) $font->subsetMap ) ); ?>
 
 
193
  <p><strong><?php esc_html_e( 'Subsets', 'local-google-fonts' ); ?></strong><br>
194
  <?php foreach ( $font->subsetMap as $subset => $is_active ) : ?>
195
  <label title="<?php printf( esc_attr__( 'Load %s subset with this font', 'local-google-fonts' ), $subset ); ?>" class="subset"><input type="checkbox" name="subsets[<?php echo esc_attr( $data['handle'] ); ?>][<?php echo esc_attr( $font->id ); ?>][]" value="<?php echo esc_attr( $subset ); ?>" <?php checked( in_array( $subset, $active_subsets ) ); ?>> <?php echo esc_html( $subset ); ?> </label>
242
  <button class="host-locally button button-primary" name="hostlocal" value="<?php echo esc_attr( $data['handle'] ); ?>"><?php esc_html_e( 'Host locally', 'local-google-fonts' ); ?></button>
243
  <?php endif; ?>
244
  </p>
 
 
 
 
245
  <?php endif; ?>
246
 
247
  <?php endforeach ?>
310
 
311
  // do not load them as its most likely some helper font thing
312
  if ( count( $families ) > 30 ) {
313
+ return new \WP_Error( 'to_many_families', esc_html__( 'This source contains more than 30 fonts and is most likely used as helper for your theme. Skipped.', 'local-google-fonts' ) );
314
  }
315
 
316
  $buffer = get_option( 'local_google_fonts_buffer', array() );
317
 
318
  foreach ( $families as $family => $variants ) {
319
  $url = 'https://google-webfonts-helper.herokuapp.com/api/fonts/';
320
+ $alias = $this->font_family_alias( $family );
321
  $subsets = isset( $buffer[ $handle ]['subsets'][ $family ] ) ? implode( ',', array_filter( $buffer[ $handle ]['subsets'][ $family ] ) ) : $args['subset'];
322
  $the_url = add_query_arg(
323
  array(
325
  // 'variants' => implode( ',', $variants ),
326
  'subsets' => $subsets,
327
  ),
328
+ $url . $alias
329
  );
330
 
331
+ $transient_key = 'lcg_s' . md5( $the_url );
332
  if ( false === ( $info = get_transient( $transient_key ) ) ) {
333
  $response = wp_remote_get( $the_url );
334
+ // break early if there's an error here.
335
+ if ( is_wp_error( $response ) ) {
336
+ return $response;
337
+ }
338
+ $code = wp_remote_retrieve_response_code( $response );
339
  if ( 200 === $code ) {
340
  $body = wp_remote_retrieve_body( $response );
341
  $info = json_decode( $body );
369
  '%s-%s%s',
370
  $san_family,
371
  $this->weightClass[ $variant->fontWeight ],
372
+ ( $variant->fontStyle === 'italic' ? 'Italic' : '' )
373
  );
374
 
375
  // there's no RegularItalic
378
  }
379
  }
380
 
381
+ $filename = $id . '/' . $info->id . '-' . $info->version . '-' . $info->defSubset;
382
+ $info->total = count( $info->variants ) * 5;
383
+ $info->original = $family;
384
+ $info->loaded = 0;
385
 
386
  foreach ( $info->variants as $i => $variant ) {
387
  $file = $filename . '-' . $variant->id;
397
 
398
  $info->variants = array_values( $info->variants );
399
  $fontinfo[] = $info;
400
+ }
401
 
402
+ if ( empty( $fontinfo ) ) {
403
+ return new \WP_Error( 'no_fontinfo', esc_html__( 'This font is not supported. Skipped.', 'local-google-fonts' ) );
404
  }
405
 
406
  return $fontinfo;
442
 
443
  }
444
 
445
+ private function font_family_alias( $name ) {
446
+
447
+ $alias = array(
448
+ 'droid-sans' => 'noto-sans',
449
+ );
450
+
451
+ if ( isset( $alias[ $name ] ) ) {
452
+ return $alias[ $name ];
453
+ }
454
+
455
+ return $name;
456
+
457
+ }
458
+
459
  public function admin_footer_text( $default ) {
460
  return sprintf( esc_html__( 'If you like %1$s please leave a %2$s&#9733;&#9733;&#9733;&#9733;&#9733;%3$s rating. Thanks in advance!', 'local-google-fonts' ), '<strong>Local Google Fonts</strong>', '<a href="https://wordpress.org/support/view/plugin-reviews/local-google-fonts?filter=5#new-post" target="_blank" rel="noopener noreferrer">', '</a>' );
461
  }
includes/class-local-google-fonts.php CHANGED
@@ -15,7 +15,7 @@ class LGF {
15
 
16
  add_filter( 'style_loader_src', array( $this, 'switch_stylesheet_src' ), 10, 2 );
17
  add_filter( 'switch_theme', array( $this, 'clear' ) );
18
- add_filter( 'wp_resource_hints', array( $this, 'remove_dns_prefetch' ), 10, 2 );
19
 
20
  add_filter( 'local_google_fonts_replace_in_content', array( $this, 'replace_in_content' ) );
21
  add_filter( 'local_google_fonts_replace_url', array( $this, 'google_to_local_url' ), 10, 2 );
@@ -89,7 +89,7 @@ class LGF {
89
  $class = LGF_Admin::get_instance();
90
  $fontinfo = $class->get_font_info( $src, $handle );
91
 
92
- if ( ! $fontinfo ) {
93
  return $src;
94
  }
95
 
15
 
16
  add_filter( 'style_loader_src', array( $this, 'switch_stylesheet_src' ), 10, 2 );
17
  add_filter( 'switch_theme', array( $this, 'clear' ) );
18
+ add_filter( 'wp_resource_hints', array( $this, 'remove_dns_prefetch' ), PHP_INT_MAX, 2 );
19
 
20
  add_filter( 'local_google_fonts_replace_in_content', array( $this, 'replace_in_content' ) );
21
  add_filter( 'local_google_fonts_replace_url', array( $this, 'google_to_local_url' ), 10, 2 );
89
  $class = LGF_Admin::get_instance();
90
  $fontinfo = $class->get_font_info( $src, $handle );
91
 
92
+ if ( is_wp_error( $fontinfo ) ) {
93
  return $src;
94
  }
95
 
local-google-fonts.php CHANGED
@@ -2,7 +2,7 @@
2
  /*
3
  Plugin Name: Local Google Fonts
4
  Description: Host your used Google fonts on your server and make your site GDPR compliant.
5
- Version: 0.12
6
  Author: EverPress
7
  Author URI: https://everpress.co
8
  License: GPLv2 or later
2
  /*
3
  Plugin Name: Local Google Fonts
4
  Description: Host your used Google fonts on your server and make your site GDPR compliant.
5
+ Version: 0.13
6
  Author: EverPress
7
  Author URI: https://everpress.co
8
  License: GPLv2 or later