Local Google Fonts - Version 0.6

Version Description

Download this release

Release Info

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

Code changes from version 0.5 to 0.6

README.md CHANGED
@@ -4,7 +4,7 @@ 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.5
8
  Requires PHP: 5.6+
9
  License: GPLv2 or later
10
  Author: EverPress
@@ -60,6 +60,13 @@ The plugin currently only checks fonts embedded via [`wp_enqueue_style`](https:/
60
 
61
  ## Changelog
62
 
 
 
 
 
 
 
 
63
  ### 0.5
64
 
65
  - added option to flush everything
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.6
8
  Requires PHP: 5.6+
9
  License: GPLv2 or later
10
  Author: EverPress
60
 
61
  ## Changelog
62
 
63
+ ### 0.6
64
+
65
+ - loading all variants if none explicit are requested
66
+ - cache API requests
67
+ - respect font-display
68
+ - removes DNS prefetch and preconnect to Google server from page header
69
+
70
  ### 0.5
71
 
72
  - added option to flush everything
includes/class-local-google-fonts-admin.php CHANGED
@@ -65,7 +65,7 @@ class LGF_Admin {
65
  }
66
 
67
  if ( isset( $_POST['flush'] ) ) {
68
- $class->remove_set();
69
  }
70
 
71
  }
@@ -75,8 +75,8 @@ class LGF_Admin {
75
  // a bit sanitation as URLs are often registered with esc_url
76
  $src = str_replace( array( '#038;', '&' ), '&', $src );
77
 
78
- $params = wp_parse_url( $src );
79
- wp_parse_str( $params['query'], $args );
80
  $args = wp_parse_args(
81
  $args,
82
  array(
@@ -102,21 +102,32 @@ class LGF_Admin {
102
  }
103
 
104
  foreach ( $families as $family => $variants ) {
105
- $url = 'https://google-webfonts-helper.herokuapp.com/api/fonts/';
106
- $the_url = add_query_arg(
107
  array(
108
  // doesn't seem to have an effect so we filter it later
109
- 'variants' => implode( ',', $variants ),
110
- 'subsets' => $args['subset'],
111
  ),
112
  $url . $family
113
  );
114
- $response = wp_remote_get( $the_url );
115
- $code = wp_remote_retrieve_response_code( $response );
116
 
117
- if ( 200 == $code ) {
118
- $body = wp_remote_retrieve_body( $response );
119
- $info = json_decode( $body );
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  foreach ( $info->variants as $i => $variant ) {
121
  // special case for italic 400
122
  if ( 'italic' == $variant->id && in_array( '400italic', $variants ) ) {
@@ -125,10 +136,11 @@ class LGF_Admin {
125
  unset( $info->variants[ $i ] );
126
  }
127
  }
128
- $info->variants = array_values( $info->variants );
129
- $fontinfo[] = $info;
130
-
131
  }
 
 
 
 
132
  }
133
 
134
  return $fontinfo;
65
  }
66
 
67
  if ( isset( $_POST['flush'] ) ) {
68
+ $class->clear();
69
  }
70
 
71
  }
75
  // a bit sanitation as URLs are often registered with esc_url
76
  $src = str_replace( array( '#038;', '&' ), '&', $src );
77
 
78
+ $query = wp_parse_url( $src, PHP_URL_QUERY );
79
+ wp_parse_str( $query, $args );
80
  $args = wp_parse_args(
81
  $args,
82
  array(
102
  }
103
 
104
  foreach ( $families as $family => $variants ) {
105
+ $url = 'https://google-webfonts-helper.herokuapp.com/api/fonts/';
106
+ $the_url = add_query_arg(
107
  array(
108
  // doesn't seem to have an effect so we filter it later
109
+ // 'variants' => implode( ',', $variants ),
110
+ 'subsets' => $args['subset'],
111
  ),
112
  $url . $family
113
  );
 
 
114
 
115
+ $transient_key = 'lcg_' . md5( $the_url );
116
+ if ( false === ( $info = get_transient( $transient_key ) ) ) {
117
+ $response = wp_remote_get( $the_url );
118
+ $code = wp_remote_retrieve_response_code( $response );
119
+ if ( 200 === $code ) {
120
+ $body = wp_remote_retrieve_body( $response );
121
+ $info = json_decode( $body );
122
+ set_transient( $transient_key, $info, HOUR_IN_SECONDS );
123
+ } else {
124
+ continue;
125
+ }
126
+ }
127
+
128
+ // if only regular is present we actually need all of them
129
+ if ( count( $variants ) > 1 ) {
130
+
131
  foreach ( $info->variants as $i => $variant ) {
132
  // special case for italic 400
133
  if ( 'italic' == $variant->id && in_array( '400italic', $variants ) ) {
136
  unset( $info->variants[ $i ] );
137
  }
138
  }
 
 
 
139
  }
140
+
141
+ $info->variants = array_values( $info->variants );
142
+ $fontinfo[] = $info;
143
+
144
  }
145
 
146
  return $fontinfo;
includes/class-local-google-fonts.php CHANGED
@@ -17,7 +17,7 @@ class LGF {
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
 
@@ -29,6 +29,20 @@ class LGF {
29
  return self::$instance;
30
  }
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
 
34
  public function process_url( $src, $handle ) {
@@ -41,13 +55,23 @@ class LGF {
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
 
50
- $fontDisplay = 'fallback';
 
 
 
 
 
 
 
 
51
 
52
  $folder = WP_CONTENT_DIR . '/uploads/fonts';
53
  $folder_url = WP_CONTENT_URL . '/uploads/fonts';
@@ -75,19 +99,20 @@ class LGF {
75
  $WP_Filesystem->delete( $tmp_file );
76
 
77
  }
78
-
79
  $style .= "@font-face {\n";
80
  $style .= "\tfont-family: " . $v->fontFamily . ";\n";
81
  $style .= "\tfont-style: " . $v->fontStyle . ";\n";
82
  $style .= "\tfont-weight: " . $v->fontWeight . ";\n";
83
- $style .= "\tfont-display: " . $fontDisplay . ";\n";
 
 
84
  $style .= "\tsrc: url('" . $file . ".eot');\n";
85
  $style .= "\tsrc: local(''),\n";
86
  $style .= "\t url('" . $file . ".eot?#iefix') format('embedded-opentype'),\n";
87
  $style .= "\t url('" . $file . ".woff2') format('woff2'),\n";
88
  $style .= "\t url('" . $file . ".woff') format('woff'),\n";
89
  $style .= "\t url('" . $file . ".ttf') format('truetype'),\n";
90
- $style .= "\t url('" . $file . '.svg#' . $v->id . "') format('svg');\n";
91
  $style .= "}\n\n";
92
 
93
  }
@@ -95,7 +120,7 @@ class LGF {
95
 
96
  $WP_Filesystem->put_contents( $new_dir, $style );
97
 
98
- return $new_src !== $src;
99
 
100
  }
101
 
@@ -107,26 +132,21 @@ class LGF {
107
  $folder = WP_CONTENT_DIR . '/uploads/fonts';
108
  $folder_url = WP_CONTENT_URL . '/uploads/fonts';
109
 
110
- $new_dir = $folder . '/' . $id . '/font.css';
111
-
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 {
129
- $src = add_query_arg( 'v', filemtime( $new_dir ), $folder_url . '/' . $id . '/font.css' );
130
  }
131
 
132
  return $src;
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( 'wp_resource_hints', array( $this, 'remove_dns_prefetch' ), 10, 2 );
21
 
22
  }
23
 
29
  return self::$instance;
30
  }
31
 
32
+ public static function remove_dns_prefetch( $urls, $relation_type ) {
33
+
34
+ if ( 'dns-prefetch' === $relation_type ) {
35
+ $urls = array_diff( $urls, array( 'fonts.googleapis.com' ) );
36
+ } elseif ( 'preconnect' === $relation_type ) {
37
+ foreach ( $urls as $key => $url ) {
38
+ if ( false !== strpos( $url['href'], '//fonts.gstatic.com' ) ) {
39
+ unset( $urls[ $key ] );
40
+ }
41
+ }
42
+ }
43
+
44
+ return $urls;
45
+ }
46
 
47
 
48
  public function process_url( $src, $handle ) {
55
 
56
  $WP_Filesystem = $this->wp_filesystem();
57
 
58
+ $plugin_data = get_plugin_data( LGF_PLUGIN_FILE );
 
 
59
 
60
+ $style = "/*\n";
61
+ $style .= ' * ' . sprintf( 'Font file created by %s %s', $plugin_data['Name'], $plugin_data['Version'] ) . "\n";
62
+ $style .= ' * Created: ' . date( 'r' ) . "\n";
63
+ $style .= ' * Handle: ' . esc_attr( $handle ) . "\n";
64
+ $style .= "*/\n\n";
65
 
66
+ $query = wp_parse_url( $src, PHP_URL_QUERY );
67
+ wp_parse_str( $query, $args );
68
+ $args = wp_parse_args(
69
+ $args,
70
+ array(
71
+ 'subset' => null,
72
+ 'display' => 'fallback',
73
+ )
74
+ );
75
 
76
  $folder = WP_CONTENT_DIR . '/uploads/fonts';
77
  $folder_url = WP_CONTENT_URL . '/uploads/fonts';
99
  $WP_Filesystem->delete( $tmp_file );
100
 
101
  }
 
102
  $style .= "@font-face {\n";
103
  $style .= "\tfont-family: " . $v->fontFamily . ";\n";
104
  $style .= "\tfont-style: " . $v->fontStyle . ";\n";
105
  $style .= "\tfont-weight: " . $v->fontWeight . ";\n";
106
+ if ( $args['display'] && $args['display'] !== 'auto' ) {
107
+ $style .= "\tfont-display: " . $args['display'] . ";\n";
108
+ }
109
  $style .= "\tsrc: url('" . $file . ".eot');\n";
110
  $style .= "\tsrc: local(''),\n";
111
  $style .= "\t url('" . $file . ".eot?#iefix') format('embedded-opentype'),\n";
112
  $style .= "\t url('" . $file . ".woff2') format('woff2'),\n";
113
  $style .= "\t url('" . $file . ".woff') format('woff'),\n";
114
  $style .= "\t url('" . $file . ".ttf') format('truetype'),\n";
115
+ $style .= "\t url('" . $file . '.svg' . strrchr( $v->svg, '#' ) . "') format('svg');\n";
116
  $style .= "}\n\n";
117
 
118
  }
120
 
121
  $WP_Filesystem->put_contents( $new_dir, $style );
122
 
123
+ return $new_src;
124
 
125
  }
126
 
132
  $folder = WP_CONTENT_DIR . '/uploads/fonts';
133
  $folder_url = WP_CONTENT_URL . '/uploads/fonts';
134
 
135
+ $stylesheet = $folder . '/' . $id . '/font.css';
136
+ $stylesheet_url = $folder_url . '/' . $id . '/font.css';
137
+ $buffer = get_option( 'local_google_fonts_buffer', array() );
138
 
139
+ if ( file_exists( $stylesheet ) ) {
140
+ $src = add_query_arg( 'v', filemtime( $stylesheet ), $stylesheet_url );
141
+ } else {
142
 
 
 
 
143
  $buffer[ $handle ] = array(
144
+ 'id' => $id,
145
+ 'handle' => $handle,
146
+ 'src' => $src,
 
147
  );
148
 
149
  update_option( 'local_google_fonts_buffer', $buffer );
 
 
 
150
  }
151
 
152
  return $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.5
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.6
6
  Author: EverPress
7
  Author URI: https://everpress.co
8
  Text Domain: local-google-fonts