Local Google Fonts - Version 0.4

Version Description

Download this release

Release Info

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

Code changes from version 0.3 to 0.4

README.md CHANGED
@@ -3,8 +3,8 @@
3
  Contributors: everpress
4
  Tags: googlefonts, google, fonts, gdpr, lgf, font, speed
5
  Requires at least: 4.2
6
- Tested up to: 5.9
7
- Stable tag: 0.3
8
  Requires PHP: 5.6+
9
  License: GPLv2 or later
10
  Author: EverPress
@@ -44,6 +44,16 @@ more on [wptavern.com](https://wptavern.com/german-court-fines-website-owner-for
44
 
45
  ## Changelog
46
 
 
 
 
 
 
 
 
 
 
 
47
  ### 0.3
48
 
49
  - add explanation info on settings page
3
  Contributors: everpress
4
  Tags: googlefonts, google, fonts, gdpr, lgf, font, speed
5
  Requires at least: 4.2
6
+ Tested up to: 6.0
7
+ Stable tag: 0.4
8
  Requires PHP: 5.6+
9
  License: GPLv2 or later
10
  Author: EverPress
44
 
45
  ## Changelog
46
 
47
+ ### 0.4
48
+
49
+ - you may have to reload fonts so please check the settings page
50
+ - improved handling of fonts
51
+ - only loading required font files
52
+ - supports now different types of variant declarations
53
+ - add variants to list only if available in the source
54
+ - added some css to improve settings page
55
+ - you may have to reload fonts so please check the settings page
56
+
57
  ### 0.3
58
 
59
  - add explanation info on settings page
assets/admin.css ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
1
+ .variant {
2
+ background: #fafafa;
3
+ border: 1 px solid #d6d6d6;
4
+ box-shadow: 0 2px 8px 0 rgb(0 0 0 / 5%), 0 2px 1px 0 rgb(0 0 0 / 5%);
5
+ border-radius: 4px;
6
+ padding: 0.3em 0.2em;
7
+ }
8
+ summary {
9
+ cursor: pointer;
10
+ }
includes/class-local-google-fonts-admin.php CHANGED
@@ -34,30 +34,45 @@ class LGF_Admin {
34
 
35
  public function script_styles() {
36
 
37
- $url = plugin_dir_url( LGF_PLUGIN_FILE ) . 'assets/admin.js';
38
- $path = plugin_dir_path( LGF_PLUGIN_FILE ) . 'assets/admin.js';
39
 
40
- wp_enqueue_script( 'local-google-fonts-admin', $url, array( 'jquery' ), filemtime( $path ), true );
 
41
 
42
  }
43
 
44
  public function local_google_fonts_validate() {
45
 
46
- if ( ! isset( $_POST['hostlocal'] ) ) {
47
- return;
48
- }
49
-
50
  $class = LGF::get_instance();
51
 
52
- foreach ( $_POST['hostlocal'] as $handle => $url ) {
53
- $class->process_url( $url, $handle );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  }
 
55
  }
56
 
57
  public function get_font_info( $src ) {
58
 
59
- $params = parse_url( $src );
60
- parse_str( $params['query'], $args );
 
 
 
61
  $args = wp_parse_args(
62
  $args,
63
  array(
@@ -76,16 +91,19 @@ class LGF_Admin {
76
  if ( ! isset( $families[ $fam ] ) ) {
77
  $families[ $fam ] = array( 'regular' );
78
  }
79
- $families[ $fam ] = array_unique( array_merge( $families[ $fam ], explode( ',', $parts[1] ) ) );
80
- sort( $families[ $fam ] );
 
 
81
  }
82
 
83
  foreach ( $families as $family => $variants ) {
84
  $url = 'https://google-webfonts-helper.herokuapp.com/api/fonts/';
85
  $the_url = add_query_arg(
86
  array(
 
87
  'variants' => implode( ',', $variants ),
88
- 'subset' => $args['subset'],
89
  ),
90
  $url . $family
91
  );
@@ -93,9 +111,18 @@ class LGF_Admin {
93
  $code = wp_remote_retrieve_response_code( $response );
94
 
95
  if ( 200 == $code ) {
96
- $body = wp_remote_retrieve_body( $response );
97
- $info = json_decode( $body );
98
- $fontinfo[] = $info;
 
 
 
 
 
 
 
 
 
99
 
100
  }
101
  }
@@ -104,6 +131,39 @@ class LGF_Admin {
104
 
105
  }
106
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  public function render_settings() {
108
 
109
  $buffer = get_option( 'local_google_fonts_buffer', array() );
@@ -128,9 +188,10 @@ class LGF_Admin {
128
  do_settings_sections( 'local_google_fonts_section' );
129
  ?>
130
 
131
- <?php foreach ( $buffer as $handle => $data ) : ?>
132
 
133
- <h2><?php esc_html_e( 'Handle', 'local-google-fonts' ); ?>: <code><?php esc_html_e( $handle ); ?></code></h2>
 
134
 
135
  <table class="wp-list-table widefat fixed striped table-view-list ">
136
  <thead>
@@ -145,24 +206,26 @@ class LGF_Admin {
145
 
146
  <?php foreach ( $fontinfo as $i => $set ) : ?>
147
  <tr>
148
- <td><strong><?php echo esc_html( $set->family ); ?></strong> <br>
149
-
150
  </td>
151
  <td>
152
  <p class="code">
153
  <?php foreach ( $set->variants as $variant ) : ?>
154
- <?php printf( '%s %s', $variant->fontStyle, $variant->fontWeight ); ?>,
155
  <?php endforeach ?>
156
  </p>
157
  <details>
158
  <summary><strong><?php printf( '%d files from Google Servers', count( $set->variants ) * 5 ); ?></strong></summary>
159
- <div style="max-height: 100px; overflow: scroll;font-size: small;white-space: nowrap; overflow: hidden; overflow-y: auto;" class="code">
160
  <?php foreach ( $set->variants as $variant ) : ?>
 
 
161
  <code><?php echo esc_url( $variant->woff2 ); ?></code><br>
162
  <code><?php echo esc_url( $variant->ttf ); ?></code><br>
163
  <code><?php echo esc_url( $variant->svg ); ?></code><br>
164
  <code><?php echo esc_url( $variant->eot ); ?></code><br>
165
- <code><?php echo esc_url( $variant->woff ); ?></code><br>
 
166
  <?php endforeach ?>
167
  </div>
168
  </details>
@@ -181,7 +244,10 @@ class LGF_Admin {
181
  </tbody>
182
  </table>
183
  <p>
184
- <button class="host-locally button button-primary" name="hostlocal[<?php echo esc_attr( $handle ); ?>]" value="<?php echo esc_attr( $data['src'] ); ?>"><?php esc_html_e( 'Host locally', 'local-google-fonts' ); ?></button>
 
 
 
185
  </p>
186
  <?php endforeach ?>
187
  </form>
34
 
35
  public function script_styles() {
36
 
37
+ $url = plugin_dir_url( LGF_PLUGIN_FILE ) . 'assets';
38
+ $path = plugin_dir_path( LGF_PLUGIN_FILE ) . 'assets';
39
 
40
+ wp_enqueue_script( 'local-google-fonts-admin', $url . '/admin.js', array( 'jquery' ), filemtime( $path . '/admin.js' ), true );
41
+ wp_enqueue_style( 'local-google-fonts-admin', $url . '/admin.css', array(), filemtime( $path . '/admin.css' ) );
42
 
43
  }
44
 
45
  public function local_google_fonts_validate() {
46
 
 
 
 
 
47
  $class = LGF::get_instance();
48
 
49
+ $buffer = get_option( 'local_google_fonts_buffer', array() );
50
+
51
+ if ( isset( $_POST['hostlocal'] ) ) {
52
+ $handle = $_POST['hostlocal'];
53
+ if ( isset( $buffer[ $handle ] ) ) {
54
+ $class->remove_set( $buffer[ $handle ]['id'] );
55
+ $class->process_url( $buffer[ $handle ]['src'], $handle );
56
+
57
+ }
58
+ }
59
+
60
+ if ( isset( $_POST['removelocal'] ) ) {
61
+ $handle = $_POST['removelocal'];
62
+ if ( isset( $buffer[ $handle ] ) ) {
63
+ $class->remove_set( $buffer[ $handle ]['id'] );
64
+ }
65
  }
66
+
67
  }
68
 
69
  public function get_font_info( $src ) {
70
 
71
+ // a bit sanitation as URLs are often registered with esc_url
72
+ $src = str_replace( array( '#038;', '&amp;' ), '&', $src );
73
+
74
+ $params = wp_parse_url( $src );
75
+ wp_parse_str( $params['query'], $args );
76
  $args = wp_parse_args(
77
  $args,
78
  array(
91
  if ( ! isset( $families[ $fam ] ) ) {
92
  $families[ $fam ] = array( 'regular' );
93
  }
94
+ if ( isset( $parts[1] ) ) {
95
+ $variants = $this->normalize_variants( $parts[1] );
96
+ $families[ $fam ] = array_unique( array_merge( $families[ $fam ], $variants ) );
97
+ }
98
  }
99
 
100
  foreach ( $families as $family => $variants ) {
101
  $url = 'https://google-webfonts-helper.herokuapp.com/api/fonts/';
102
  $the_url = add_query_arg(
103
  array(
104
+ // doesn't seem to have an effect so we filter it later
105
  'variants' => implode( ',', $variants ),
106
+ 'subsets' => $args['subset'],
107
  ),
108
  $url . $family
109
  );
111
  $code = wp_remote_retrieve_response_code( $response );
112
 
113
  if ( 200 == $code ) {
114
+ $body = wp_remote_retrieve_body( $response );
115
+ $info = json_decode( $body );
116
+ foreach ( $info->variants as $i => $variant ) {
117
+ // special case for italic 400
118
+ if ( 'italic' == $variant->id && in_array( '400italic', $variants ) ) {
119
+
120
+ } elseif ( ! in_array( $variant->id, $variants ) ) {
121
+ unset( $info->variants[ $i ] );
122
+ }
123
+ }
124
+ $info->variants = array_values( $info->variants );
125
+ $fontinfo[] = $info;
126
 
127
  }
128
  }
131
 
132
  }
133
 
134
+ private function normalize_variants( $variants ) {
135
+ // possibles
136
+ // Merriweather:400,700,400italic,700italic
137
+ // Open+Sans:wght@400;700
138
+ // Open+Sans:ital,wght@0,800;1,800
139
+ // Open+Sans:ital,wght@0,400;0,700;1,800
140
+ // Google+Sans:300,300i,400,400i,500,500i,700,700i|Roboto:300,300i,400,400i,500,500i,700,700i
141
+
142
+ if ( false !== strpos( $variants, '@' ) ) {
143
+ $variant_parts = explode( '@', $variants );
144
+ $styles = explode( ';', $variant_parts[1] );
145
+ $variants = array();
146
+ foreach ( $styles as $style ) {
147
+ // regular version
148
+ if ( 0 === strpos( $style, '0,' ) ) {
149
+ $variants[] = substr( $style, 2 );
150
+ // italic version
151
+ } elseif ( 0 === strpos( $style, '1,' ) ) {
152
+ $variants[] = substr( $style, 2 ) . 'italic';
153
+ } else {
154
+ $variants[] = $style;
155
+ }
156
+ }
157
+ } else {
158
+ // handle XXXi variants
159
+ $variants = preg_replace( '/(\d{3}+)i/', '$1italic', $variants );
160
+ $variants = explode( ',', $variants );
161
+ }
162
+
163
+ return $variants;
164
+
165
+ }
166
+
167
  public function render_settings() {
168
 
169
  $buffer = get_option( 'local_google_fonts_buffer', array() );
188
  do_settings_sections( 'local_google_fonts_section' );
189
  ?>
190
 
191
+ <?php foreach ( $buffer as $id => $data ) : ?>
192
 
193
+ <h2><?php esc_html_e( 'Handle', 'local-google-fonts' ); ?>: <code><?php esc_html_e( $data['handle'] ); ?></code></h2>
194
+ <p><?php esc_html_e( 'Original URL', 'local-google-fonts' ); ?>: <code><?php echo rawurldecode( $data['src'] ); ?></code> <a href="<?php echo esc_url( $data['src'] ); ?>" class="dashicons dashicons-external" target="_blank" title="<?php esc_attr_e( 'show original URL', 'local-google-fonts' ); ?>"></a></p>
195
 
196
  <table class="wp-list-table widefat fixed striped table-view-list ">
197
  <thead>
206
 
207
  <?php foreach ( $fontinfo as $i => $set ) : ?>
208
  <tr>
209
+ <td><strong><?php echo esc_html( $set->family ); ?></strong><br>
 
210
  </td>
211
  <td>
212
  <p class="code">
213
  <?php foreach ( $set->variants as $variant ) : ?>
214
+ <span class="variant"><?php printf( '%s %s', $variant->fontStyle, $variant->fontWeight ); ?></span>
215
  <?php endforeach ?>
216
  </p>
217
  <details>
218
  <summary><strong><?php printf( '%d files from Google Servers', count( $set->variants ) * 5 ); ?></strong></summary>
219
+ <div style="max-height: 200px; overflow: scroll;font-size: small;white-space: nowrap; overflow: hidden; overflow-y: auto;" class="code">
220
  <?php foreach ( $set->variants as $variant ) : ?>
221
+ <p>
222
+ <strong><?php printf( '%s %s', $variant->fontStyle, $variant->fontWeight ); ?></strong><br>
223
  <code><?php echo esc_url( $variant->woff2 ); ?></code><br>
224
  <code><?php echo esc_url( $variant->ttf ); ?></code><br>
225
  <code><?php echo esc_url( $variant->svg ); ?></code><br>
226
  <code><?php echo esc_url( $variant->eot ); ?></code><br>
227
+ <code><?php echo esc_url( $variant->woff ); ?></code>
228
+ </p>
229
  <?php endforeach ?>
230
  </div>
231
  </details>
244
  </tbody>
245
  </table>
246
  <p>
247
+ <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>
248
+ <?php if ( is_dir( $folder . '/' . $data['id'] ) ) : ?>
249
+ <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>
250
+ <?php endif; ?>
251
  </p>
252
  <?php endforeach ?>
253
  </form>
includes/class-local-google-fonts.php CHANGED
@@ -14,7 +14,10 @@ class LGF {
14
  register_deactivation_hook( LGF_PLUGIN_FILE, array( $this, 'deactivate' ) );
15
 
16
  add_filter( 'style_loader_src', array( $this, 'switch_stylesheet_src' ), 10, 2 );
17
- add_filter( 'switch_theme', array( $this, 'clear' ), 10, 2 );
 
 
 
18
 
19
  }
20
 
@@ -31,13 +34,16 @@ class LGF {
31
  public function process_url( $src, $handle ) {
32
 
33
  $id = md5( $src );
 
34
  if ( ! function_exists( 'download_url' ) ) {
35
  include ABSPATH . 'wp-admin/includes/file.php';
36
  }
37
 
38
  $WP_Filesystem = $this->wp_filesystem();
39
 
40
- $style = "/* Font file served by Local Google Fonts Plugin */\n";
 
 
41
 
42
  $urls[ $id ] = array();
43
 
@@ -55,6 +61,7 @@ class LGF {
55
  foreach ( $fontinfo as $font ) {
56
  $filename = $font->id . '-' . $font->version . '-' . $font->defSubset;
57
  foreach ( $font->variants as $v ) {
 
58
  $file = $filename . '-' . $v->id;
59
 
60
  foreach ( array( 'woff', 'svg', 'woff2', 'ttf', 'eot' ) as $ext ) {
@@ -105,13 +112,17 @@ class LGF {
105
  if ( ! file_exists( $new_dir ) ) {
106
 
107
  $buffer = get_option( 'local_google_fonts_buffer' );
 
108
  if ( empty( $buffer ) ) {
109
  $buffer = array();
110
  }
111
  $buffer[ $handle ] = array(
112
- 'id' => $id,
113
- 'src' => $src,
 
 
114
  );
 
115
  update_option( 'local_google_fonts_buffer', $buffer );
116
 
117
  } else {
@@ -125,9 +136,7 @@ class LGF {
125
  public function switch_stylesheet_src( $src, $handle ) {
126
 
127
  if ( false !== strpos( $src, '//fonts.googleapis.com/css' ) ) {
128
- if ( ! is_customize_preview() ) {
129
- $src = $this->google_to_local_url( $src, $handle );
130
- }
131
  }
132
  return $src;
133
  }
@@ -139,8 +148,21 @@ class LGF {
139
  $WP_Filesystem = $this->wp_filesystem();
140
  $WP_Filesystem->delete( $folder, true );
141
  }
 
 
 
 
 
142
  delete_option( 'local_google_fonts_buffer' );
 
143
 
 
 
 
 
 
 
 
144
  }
145
 
146
  private function wp_filesystem() {
14
  register_deactivation_hook( LGF_PLUGIN_FILE, array( $this, 'deactivate' ) );
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( 'deactivated_plugin', array( $this, 'clear_option' ) );
19
+ add_filter( 'activated_plugin', array( $this, 'clear_option' ) );
20
+ add_filter( 'upgrader_process_complete', array( $this, 'clear_option' ) );
21
 
22
  }
23
 
34
  public function process_url( $src, $handle ) {
35
 
36
  $id = md5( $src );
37
+
38
  if ( ! function_exists( 'download_url' ) ) {
39
  include ABSPATH . 'wp-admin/includes/file.php';
40
  }
41
 
42
  $WP_Filesystem = $this->wp_filesystem();
43
 
44
+ $style = "/* Font file served by Local Google Fonts Plugin */\n";
45
+ $style .= '/* Created: ' . date( 'r' ) . " */\n";
46
+ $style .= "\n";
47
 
48
  $urls[ $id ] = array();
49
 
61
  foreach ( $fontinfo as $font ) {
62
  $filename = $font->id . '-' . $font->version . '-' . $font->defSubset;
63
  foreach ( $font->variants as $v ) {
64
+
65
  $file = $filename . '-' . $v->id;
66
 
67
  foreach ( array( 'woff', 'svg', 'woff2', 'ttf', 'eot' ) as $ext ) {
112
  if ( ! file_exists( $new_dir ) ) {
113
 
114
  $buffer = get_option( 'local_google_fonts_buffer' );
115
+
116
  if ( empty( $buffer ) ) {
117
  $buffer = array();
118
  }
119
  $buffer[ $handle ] = array(
120
+ 'id' => $id,
121
+ 'handle' => $handle,
122
+ 'src' => $src,
123
+ 'timestamp' => time(),
124
  );
125
+
126
  update_option( 'local_google_fonts_buffer', $buffer );
127
 
128
  } else {
136
  public function switch_stylesheet_src( $src, $handle ) {
137
 
138
  if ( false !== strpos( $src, '//fonts.googleapis.com/css' ) ) {
139
+ $src = $this->google_to_local_url( $src, $handle );
 
 
140
  }
141
  return $src;
142
  }
148
  $WP_Filesystem = $this->wp_filesystem();
149
  $WP_Filesystem->delete( $folder, true );
150
  }
151
+ $this->clear_option();
152
+
153
+ }
154
+
155
+ public function clear_option() {
156
  delete_option( 'local_google_fonts_buffer' );
157
+ }
158
 
159
+ public function remove_set( $id ) {
160
+ $folder = WP_CONTENT_DIR . '/uploads/fonts/' . basename( $id );
161
+ if ( is_dir( $folder ) ) {
162
+ $WP_Filesystem = $this->wp_filesystem();
163
+ return $WP_Filesystem->delete( $folder, true );
164
+ }
165
+ return true;
166
  }
167
 
168
  private function wp_filesystem() {
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.3
6
  Author: EverPress
7
  Author URI: https://everpress.co
8
  Text Domain: local-google-fonts
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.4
6
  Author: EverPress
7
  Author URI: https://everpress.co
8
  Text Domain: local-google-fonts