Local Google Fonts - Version 0.10

Version Description

Download this release

Release Info

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

Code changes from version 0.9 to 0.10

README.md CHANGED
@@ -4,10 +4,10 @@ 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.9
8
  Requires PHP: 5.6+
9
- License: GPLv2 or later
10
- Author: EverPress
11
  Author URI: https://everpress.co
12
 
13
  ## Description
@@ -58,8 +58,19 @@ Fonts are discovered once they are used. Try to browse your front end first and
58
 
59
  The plugin currently only checks fonts embedded via [`wp_enqueue_style`](https://developer.wordpress.org/reference/functions/wp_enqueue_style/) if you use Google fonts via `@import` you have to change that first.
60
 
 
 
 
 
61
  ## Changelog
62
 
 
 
 
 
 
 
 
63
  ### 0.9
64
 
65
  - new: options to load fonts automatically
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.10
8
  Requires PHP: 5.6+
9
+ License: GPLv2 or later
10
+ Author: EverPress
11
  Author URI: https://everpress.co
12
 
13
  ## Description
58
 
59
  The plugin currently only checks fonts embedded via [`wp_enqueue_style`](https://developer.wordpress.org/reference/functions/wp_enqueue_style/) if you use Google fonts via `@import` you have to change that first.
60
 
61
+ ### The Divi theme still loads a Google font?
62
+
63
+ The Divi theme has a custom implementation of Google Fonts. To use LGF with Divi disable _Improve Google Fonts Loading_ on the Performance Settings page of Divi.
64
+
65
  ## Changelog
66
 
67
+ ### 0.10
68
+
69
+ - fixed: rending issue on some fonts (please reload fonts if you use one of these: "exo-2", "inter", "jost" or "montserrat")
70
+ - fixed: some styles are not recognized and therefore not loaded.
71
+ - improved: info about loaded font files on the admin screen
72
+ - added timestamp to loaded font files to prevent cached fonts get loaded.
73
+
74
  ### 0.9
75
 
76
  - new: options to load fonts automatically
includes/class-local-google-fonts-admin.php CHANGED
@@ -6,6 +6,18 @@ namespace EverPress;
6
  class LGF_Admin {
7
 
8
  private static $instance = null;
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  private function __construct() {
11
 
@@ -93,7 +105,7 @@ class LGF_Admin {
93
  <?php
94
  }
95
 
96
-
97
  public function render_settings() {
98
  // check user capabilities
99
  if ( ! current_user_can( 'manage_options' ) ) {
@@ -143,28 +155,40 @@ class LGF_Admin {
143
  <tbody>
144
  <?php $fontinfo = $this->get_font_info( $data['src'] ); ?>
145
 
146
- <?php foreach ( $fontinfo as $i => $set ) : ?>
 
 
147
  <tr>
148
- <td><strong><?php echo esc_html( $set->family ); ?></strong><br>
149
  </td>
150
  <td>
151
  <p class="code">
152
- <?php foreach ( $set->variants as $variant ) : ?>
153
  <span class="variant"><?php printf( '%s %s', $variant->fontStyle, $variant->fontWeight ); ?></span>
154
  <?php endforeach ?>
155
  </p>
156
  <details>
157
- <summary><strong><?php printf( '%d files from Google Servers', count( $set->variants ) * 5 ); ?></strong></summary>
158
- <div style="max-height: 200px; overflow: scroll;font-size: small;white-space: nowrap; overflow: hidden; overflow-y: auto;" class="code">
159
- <?php foreach ( $set->variants as $variant ) : ?>
160
- <p>
161
- <strong><?php printf( '%s %s', $variant->fontStyle, $variant->fontWeight ); ?></strong><br>
162
- <code><?php echo esc_url( $variant->woff2 ); ?></code><br>
163
- <code><?php echo esc_url( $variant->ttf ); ?></code><br>
164
- <code><?php echo esc_url( $variant->svg ); ?></code><br>
165
- <code><?php echo esc_url( $variant->eot ); ?></code><br>
166
- <code><?php echo esc_url( $variant->woff ); ?></code>
167
- </p>
 
 
 
 
 
 
 
 
 
 
168
  <?php endforeach ?>
169
  </div>
170
  </details>
@@ -183,16 +207,18 @@ class LGF_Admin {
183
  </tbody>
184
  </table>
185
  <p>
186
- <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>
187
  <?php if ( is_dir( $folder . '/' . $data['id'] ) ) : ?>
 
188
  <button class="host-locally button button-link-delete" name="removelocal" value="<?php echo esc_attr( $data['handle'] ); ?>"><?php esc_html_e( 'Remove hosted files', 'local-google-fonts' ); ?></button>
 
 
189
  <?php endif; ?>
190
  </p>
191
  <?php endforeach ?>
192
  <hr>
193
- <p class="textright">
194
- <button class="host-locally button button-link-delete" name="flush" value="1"><?php esc_html_e( 'Remove all stored data', 'local-google-fonts' ); ?></button>
195
- </p>
196
  </form>
197
  </div>
198
  <?php
@@ -201,6 +227,11 @@ class LGF_Admin {
201
 
202
  public function get_font_info( $src ) {
203
 
 
 
 
 
 
204
  // a bit sanitation as URLs are often registered with esc_url
205
  $src = str_replace( array( '#038;', '&amp;' ), '&', $src );
206
 
@@ -281,6 +312,42 @@ class LGF_Admin {
281
  }
282
  }
283
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
284
  $info->variants = array_values( $info->variants );
285
  $fontinfo[] = $info;
286
 
@@ -316,6 +383,8 @@ class LGF_Admin {
316
  } else {
317
  // handle XXXi variants
318
  $variants = preg_replace( '/(\d{3}+)i/', '$1italic', $variants );
 
 
319
  $variants = explode( ',', $variants );
320
  }
321
 
6
  class LGF_Admin {
7
 
8
  private static $instance = null;
9
+ private $weightClass = array(
10
+ 100 => 'Thin',
11
+ 200 => 'ExtraLight',
12
+ 300 => 'Light',
13
+ 400 => 'Regular',
14
+ 500 => 'Medium',
15
+ 600 => 'SemiBold',
16
+ 700 => 'Bold',
17
+ 800 => 'ExtraBold',
18
+ 900 => 'Black',
19
+ );
20
+
21
 
22
  private function __construct() {
23
 
105
  <?php
106
  }
107
 
108
+
109
  public function render_settings() {
110
  // check user capabilities
111
  if ( ! current_user_can( 'manage_options' ) ) {
155
  <tbody>
156
  <?php $fontinfo = $this->get_font_info( $data['src'] ); ?>
157
 
158
+ <?php foreach ( $fontinfo as $i => $font ) : ?>
159
+
160
+ <?php $filename = $font->id . '-' . $font->version . '-' . $font->defSubset; ?>
161
  <tr>
162
+ <td><strong><?php echo esc_html( $font->family ); ?></strong><br>
163
  </td>
164
  <td>
165
  <p class="code">
166
+ <?php foreach ( $font->variants as $variant ) : ?>
167
  <span class="variant"><?php printf( '%s %s', $variant->fontStyle, $variant->fontWeight ); ?></span>
168
  <?php endforeach ?>
169
  </p>
170
  <details>
171
+ <summary><strong><?php printf( esc_html__( '%1$d of %2$d files loaded.', 'local-google-fonts' ), $font->loaded, $font->total ); ?></strong></summary>
172
+ <div style="max-height: 280px; overflow: scroll;font-size: small;white-space: nowrap; overflow: hidden; overflow-y: auto;" class="code">
173
+ <?php foreach ( $font->variants as $variant ) : ?>
174
+ <div>
175
+ <h4><?php printf( '%s %s', $variant->fontStyle, $variant->fontWeight ); ?></h4>
176
+ <?php foreach ( array( 'woff', 'svg', 'woff2', 'ttf', 'eot' ) as $ext ) : ?>
177
+ <ul>
178
+ <li>
179
+ <?php $file = $data['id'] . '/' . $filename . '-' . $variant->id . '.' . $ext; ?>
180
+ <?php if ( file_exists( $folder . '/' . $file ) ) : ?>
181
+ <code><?php esc_html_e( 'Local', 'local-google-fonts' ); ?>: <a href="<?php echo esc_url( $folder_url . '/' . $file ); ?>" download><?php echo esc_html( basename( $file ) ); ?></a></code>
182
+ <strong title="<?php esc_attr_e( 'loaded, served from your server', 'local-google-fonts' ); ?>">✔</strong>
183
+ <?php else : ?>
184
+ <code><?php esc_html_e( 'Local', 'local-google-fonts' ); ?>: <?php echo esc_html( basename( $file ) ); ?></code>
185
+ <strong class="wp-ui-text-notification" title="<?php esc_attr_e( 'not loaded, served from Google servers', 'local-google-fonts' ); ?>">✕</strong>
186
+ <?php endif; ?>
187
+ </li>
188
+ <li><code><?php esc_html_e( 'Remote', 'local-google-fonts' ); ?>: <?php echo esc_url( $variant->{$ext} ); ?></code></li>
189
+ </ul>
190
+ <?php endforeach; ?>
191
+ </div>
192
  <?php endforeach ?>
193
  </div>
194
  </details>
207
  </tbody>
208
  </table>
209
  <p>
 
210
  <?php if ( is_dir( $folder . '/' . $data['id'] ) ) : ?>
211
+ <button class="host-locally button button-primary" name="hostlocal" value="<?php echo esc_attr( $data['handle'] ); ?>"><?php esc_html_e( 'Reload Fonts', 'local-google-fonts' ); ?></button>
212
  <button class="host-locally button button-link-delete" name="removelocal" value="<?php echo esc_attr( $data['handle'] ); ?>"><?php esc_html_e( 'Remove hosted files', 'local-google-fonts' ); ?></button>
213
+ <?php else : ?>
214
+ <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>
215
  <?php endif; ?>
216
  </p>
217
  <?php endforeach ?>
218
  <hr>
219
+ <p class="textright">
220
+ <button class="host-locally button button-link-delete" name="flush" value="1"><?php esc_html_e( 'Remove all stored data', 'local-google-fonts' ); ?></button>
221
+ </p>
222
  </form>
223
  </div>
224
  <?php
227
 
228
  public function get_font_info( $src ) {
229
 
230
+ $folder = WP_CONTENT_DIR . '/uploads/fonts';
231
+ $folder_url = WP_CONTENT_URL . '/uploads/fonts';
232
+
233
+ $id = md5( $src );
234
+
235
  // a bit sanitation as URLs are often registered with esc_url
236
  $src = str_replace( array( '#038;', '&amp;' ), '&', $src );
237
 
312
  }
313
  }
314
 
315
+ // fonts with render bug /https://github.com/everpress-co/local-google-fonts/issues/1)
316
+ if ( in_array( $family, array( 'montserrat', 'jost', 'inter', 'exo-2' ) ) ) {
317
+
318
+ foreach ( $info->variants as $i => $variant ) {
319
+
320
+ $san_family = str_replace( ' ', '', $info->family );
321
+
322
+ $font_name = sprintf(
323
+ '%s-%s%s',
324
+ $san_family,
325
+ $this->weightClass[ $variant->fontWeight ],
326
+ ( $variant->fontStyle === 'italic' ? 'Italic' : '' ),
327
+ );
328
+
329
+ // there's no RegularItalic
330
+ $font_name = str_replace( $san_family . '-RegularItalic', $san_family . '-Italic', $font_name );
331
+ $info->variants[ $i ]->woff2 = 'https://github.com/everpress-co/local-google-fonts-render-bug/raw/main/fonts/' . $family . '/' . $font_name . '.woff2';
332
+ }
333
+ }
334
+
335
+ $filename = $id . '/' . $info->id . '-' . $info->version . '-' . $info->defSubset;
336
+ $info->total = count( $info->variants ) * 5;
337
+ $info->loaded = 0;
338
+
339
+ foreach ( $info->variants as $i => $variant ) {
340
+ $file = $filename . '-' . $variant->id;
341
+
342
+ $info->variants[ $i ]->loaded = array();
343
+ foreach ( array( 'woff', 'svg', 'woff2', 'ttf', 'eot' ) as $ext ) {
344
+ if ( file_exists( $folder . '/' . $file . '.' . $ext ) ) {
345
+ $info->loaded++;
346
+ $info->variants[ $i ]->loaded[ $ext ] = $file . '.' . $ext;
347
+ }
348
+ }
349
+ }
350
+
351
  $info->variants = array_values( $info->variants );
352
  $fontinfo[] = $info;
353
 
383
  } else {
384
  // handle XXXi variants
385
  $variants = preg_replace( '/(\d{3}+)i/', '$1italic', $variants );
386
+ // handle XXXitalic is converted into XXXitalictalic
387
+ $variants = str_replace( 'italictalic', 'italic', $variants );
388
  $variants = explode( ',', $variants );
389
  }
390
 
includes/class-local-google-fonts.php CHANGED
@@ -60,6 +60,8 @@ class LGF {
60
  include ABSPATH . 'wp-admin/includes/plugin.php';
61
  }
62
 
 
 
63
  $plugin_data = get_plugin_data( LGF_PLUGIN_FILE );
64
 
65
  $style = "/*\n";
@@ -92,17 +94,21 @@ class LGF {
92
  foreach ( $font->variants as $v ) {
93
 
94
  $file = $filename . '-' . $v->id;
95
-
 
 
96
  foreach ( array( 'woff', 'svg', 'woff2', 'ttf', 'eot' ) as $ext ) {
97
 
98
- if ( ! is_dir( $folder . '/' . $id ) ) {
99
- wp_mkdir_p( $folder . '/' . $id );
 
 
 
 
 
 
 
100
  }
101
- $tmp_file = download_url( $v->{$ext} );
102
- $filepath = $folder . '/' . $id . '/' . $file . '.' . $ext;
103
- $WP_Filesystem->copy( $tmp_file, $filepath );
104
- $WP_Filesystem->delete( $tmp_file );
105
-
106
  }
107
  $style .= "@font-face {\n";
108
  $style .= "\tfont-family: " . $v->fontFamily . ";\n";
@@ -111,18 +117,29 @@ class LGF {
111
  if ( $args['display'] && $args['display'] !== 'auto' ) {
112
  $style .= "\tfont-display: " . $args['display'] . ";\n";
113
  }
114
- $style .= "\tsrc: url('" . $file . ".eot');\n";
 
115
  $style .= "\tsrc: local(''),\n";
116
- $style .= "\t url('" . $file . ".eot?#iefix') format('embedded-opentype'),\n";
117
- $style .= "\t url('" . $file . ".woff2') format('woff2'),\n";
118
- $style .= "\t url('" . $file . ".woff') format('woff'),\n";
119
- $style .= "\t url('" . $file . ".ttf') format('truetype'),\n";
120
- $style .= "\t url('" . $file . '.svg' . strrchr( $v->svg, '#' ) . "') format('svg');\n";
 
 
 
 
 
 
 
 
 
121
  $style .= "}\n\n";
122
 
123
  }
124
  }
125
 
 
126
  $WP_Filesystem->put_contents( $new_dir, $style );
127
 
128
  return $new_src;
60
  include ABSPATH . 'wp-admin/includes/plugin.php';
61
  }
62
 
63
+ $time = time();
64
+
65
  $plugin_data = get_plugin_data( LGF_PLUGIN_FILE );
66
 
67
  $style = "/*\n";
94
  foreach ( $font->variants as $v ) {
95
 
96
  $file = $filename . '-' . $v->id;
97
+ if ( ! is_dir( $folder . '/' . $id ) ) {
98
+ wp_mkdir_p( $folder . '/' . $id );
99
+ }
100
  foreach ( array( 'woff', 'svg', 'woff2', 'ttf', 'eot' ) as $ext ) {
101
 
102
+ if ( $v->{$ext} ) {
103
+ $tmp_file = download_url( $v->{$ext} );
104
+ if ( ! is_wp_error( $tmp_file ) ) {
105
+ $filepath = $folder . '/' . $id . '/' . $file . '.' . $ext;
106
+ $WP_Filesystem->copy( $tmp_file, $filepath );
107
+ $WP_Filesystem->delete( $tmp_file );
108
+ } else {
109
+ $v->{$ext} = null;
110
+ }
111
  }
 
 
 
 
 
112
  }
113
  $style .= "@font-face {\n";
114
  $style .= "\tfont-family: " . $v->fontFamily . ";\n";
117
  if ( $args['display'] && $args['display'] !== 'auto' ) {
118
  $style .= "\tfont-display: " . $args['display'] . ";\n";
119
  }
120
+
121
+ $style .= "\tsrc: url('" . $file . ".eot?v=$time');\n";
122
  $style .= "\tsrc: local(''),\n";
123
+ $style .= "\t\turl('" . $file . ".eot?v=$time?#iefix') format('embedded-opentype'),\n";
124
+
125
+ if ( $v->woff2 ) {
126
+ $style .= "\t\turl('" . $file . ".woff2?v=$time') format('woff2'),\n";
127
+ }
128
+ if ( $v->woff ) {
129
+ $style .= "\t\turl('" . $file . ".woff?v=$time') format('woff'),\n";
130
+ }
131
+ if ( $v->ttf ) {
132
+ $style .= "\t\turl('" . $file . ".ttf?v=$time') format('truetype'),\n";
133
+ }
134
+ if ( $v->svg ) {
135
+ $style .= "\t\turl('" . $file . ".svg?v=$time" . strrchr( $v->svg, '#' ) . "') format('svg');\n";
136
+ }
137
  $style .= "}\n\n";
138
 
139
  }
140
  }
141
 
142
+
143
  $WP_Filesystem->put_contents( $new_dir, $style );
144
 
145
  return $new_src;
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.9
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.10
6
  Author: EverPress
7
  Author URI: https://everpress.co
8
  License: GPLv2 or later