OMGF | GDPR/DSVGO Compliant, Faster Google Fonts. Easy. - Version 4.5.7

Version Description

| September 29th, 2021

Download this release

Release Info

Developer DaanvandenBergh
Plugin Icon 128x128 OMGF | GDPR/DSVGO Compliant, Faster Google Fonts. Easy.
Version 4.5.7
Comparing to
See all releases

Code changes from version 4.5.6 to 4.5.7

assets/js/omgf-admin.js CHANGED
@@ -282,5 +282,7 @@ jQuery(document).ready(function ($) {
282
  }
283
  };
284
 
 
 
285
  omgf_admin.init();
286
  });
282
  }
283
  };
284
 
285
+ omgf_show_loader = omgf_admin.show_loader;
286
+
287
  omgf_admin.init();
288
  });
host-webfonts-local.php CHANGED
@@ -4,7 +4,7 @@
4
  * Plugin Name: OMGF
5
  * Plugin URI: https://daan.dev/wordpress-plugins/host-google-fonts-locally
6
  * Description: Minimize DNS requests, leverage browser cache and speed up WordPress by saving Google Fonts to your server and removing external Google Fonts requests.
7
- * Version: 4.5.6
8
  * Author: Daan from FFW.Press
9
  * Author URI: https://ffw.press
10
  * License: GPL2v2 or later
@@ -19,7 +19,7 @@ defined('ABSPATH') || exit;
19
  define('OMGF_PLUGIN_DIR', plugin_dir_path(__FILE__));
20
  define('OMGF_PLUGIN_FILE', __FILE__);
21
  define('OMGF_PLUGIN_BASENAME', plugin_basename(OMGF_PLUGIN_FILE));
22
- define('OMGF_STATIC_VERSION', '4.5.1');
23
  define('OMGF_DB_VERSION', '4.5.1');
24
 
25
  /**
4
  * Plugin Name: OMGF
5
  * Plugin URI: https://daan.dev/wordpress-plugins/host-google-fonts-locally
6
  * Description: Minimize DNS requests, leverage browser cache and speed up WordPress by saving Google Fonts to your server and removing external Google Fonts requests.
7
+ * Version: 4.5.7
8
  * Author: Daan from FFW.Press
9
  * Author URI: https://ffw.press
10
  * License: GPL2v2 or later
19
  define('OMGF_PLUGIN_DIR', plugin_dir_path(__FILE__));
20
  define('OMGF_PLUGIN_FILE', __FILE__);
21
  define('OMGF_PLUGIN_BASENAME', plugin_basename(OMGF_PLUGIN_FILE));
22
+ define('OMGF_STATIC_VERSION', '4.5.7');
23
  define('OMGF_DB_VERSION', '4.5.1');
24
 
25
  /**
includes/admin/settings/class-advanced.php CHANGED
@@ -80,7 +80,7 @@ class OMGF_Admin_Settings_Advanced extends OMGF_Admin_Settings_Builder
80
  'omgf_pro_excluded_ids',
81
  __('e.g. 1,2,5,21,443'),
82
  defined('OMGF_PRO_EXCLUDED_IDS') ? OMGF_PRO_EXCLUDED_IDS : '',
83
- __('A comma separated list of post/page IDs where OMGF Pro shouldn\'t run.', $this->plugin_text_domain) . ' ' . $this->promo,
84
  true
85
  );
86
  }
80
  'omgf_pro_excluded_ids',
81
  __('e.g. 1,2,5,21,443'),
82
  defined('OMGF_PRO_EXCLUDED_IDS') ? OMGF_PRO_EXCLUDED_IDS : '',
83
+ __('A comma separated list of post/page IDs where OMGF Pro shouldn\'t run. Only works when Advanced Proccessing is enabled under Detection Settings.', $this->plugin_text_domain) . ' ' . $this->promo,
84
  true
85
  );
86
  }
includes/admin/settings/class-optimize.php CHANGED
@@ -213,7 +213,7 @@ class OMGF_Admin_Settings_Optimize extends OMGF_Admin_Settings_Builder
213
  <?php
214
  $aka = in_array($font->id, OMGF_API_Download::OMGF_RENAMED_GOOGLE_FONTS) ? array_search($font->id, OMGF_API_Download::OMGF_RENAMED_GOOGLE_FONTS) : '';
215
  ?>
216
- <tr class="font-family" data-id="<?= $font->id; ?>">
217
  <td colspan="5">
218
  <span class="family"><em><?= rawurldecode($font->family); ?><?= $aka ? ' (' . sprintf(__('formerly known as <strong>%s</strong>', $this->plugin_text_domain) . ')', ucfirst($aka)) : ''; ?></em></span> <span class="unload-mass-action">(<a href="#" class="unload-italics"><?= __('Unload italics', $this->plugin_text_domain); ?></a> <span class="dashicons dashicons-info tooltip"><span class="tooltip-text"><?= __('In most situations you can safely unload all Italic font styles. Modern browsers are capable of mimicking Italic font styles.', $this->plugin_text_domain); ?></span></span> | <a href="#" class="unload-all"><?= __('Unload all', $this->plugin_text_domain); ?></a> | <a href="#" class="load-all"><?= __('Load all', $this->plugin_text_domain); ?></a>)</span>
219
  </td>
@@ -237,10 +237,10 @@ class OMGF_Admin_Settings_Optimize extends OMGF_Admin_Settings_Builder
237
  <td><?= $variant->fontStyle; ?></td>
238
  <td><?= $variant->fontWeight; ?></td>
239
  <td class="preload-<?= $class; ?>">
240
- <input data-handle="<?= $handle; ?>" data-font-id="<?= $font->id; ?>" autocomplete="off" type="checkbox" class="preload" name="<?= OMGF_Admin_Settings::OMGF_OPTIMIZE_SETTING_PRELOAD_FONTS; ?>[<?= $handle; ?>][<?= $font->id; ?>][<?= $variant->id; ?>]" value="<?= $variant->id; ?>" <?= $preload ? 'checked="checked"' : ''; ?> <?= $unload ? 'disabled' : ''; ?> />
241
  </td>
242
  <td class="unload-<?= $class; ?>">
243
- <input data-handle="<?= $handle; ?>" data-font-id="<?= $font->id; ?>" autocomplete="off" type="checkbox" class="unload" name="<?= OMGF_Admin_Settings::OMGF_OPTIMIZE_SETTING_UNLOAD_FONTS; ?>[<?= $handle; ?>][<?= $font->id; ?>][<?= $variant->id; ?>]" value="<?= $variant->id; ?>" <?= $unload ? 'checked="checked"' : ''; ?> <?= $preload ? 'disabled' : ''; ?> />
244
  </td>
245
  </tr>
246
  <?php endforeach; ?>
213
  <?php
214
  $aka = in_array($font->id, OMGF_API_Download::OMGF_RENAMED_GOOGLE_FONTS) ? array_search($font->id, OMGF_API_Download::OMGF_RENAMED_GOOGLE_FONTS) : '';
215
  ?>
216
+ <tr class="font-family" data-id="<?= $handle . '-' . $font->id; ?>">
217
  <td colspan="5">
218
  <span class="family"><em><?= rawurldecode($font->family); ?><?= $aka ? ' (' . sprintf(__('formerly known as <strong>%s</strong>', $this->plugin_text_domain) . ')', ucfirst($aka)) : ''; ?></em></span> <span class="unload-mass-action">(<a href="#" class="unload-italics"><?= __('Unload italics', $this->plugin_text_domain); ?></a> <span class="dashicons dashicons-info tooltip"><span class="tooltip-text"><?= __('In most situations you can safely unload all Italic font styles. Modern browsers are capable of mimicking Italic font styles.', $this->plugin_text_domain); ?></span></span> | <a href="#" class="unload-all"><?= __('Unload all', $this->plugin_text_domain); ?></a> | <a href="#" class="load-all"><?= __('Load all', $this->plugin_text_domain); ?></a>)</span>
219
  </td>
237
  <td><?= $variant->fontStyle; ?></td>
238
  <td><?= $variant->fontWeight; ?></td>
239
  <td class="preload-<?= $class; ?>">
240
+ <input data-handle="<?= $handle; ?>" data-font-id="<?= $handle . '-' . $font->id; ?>" autocomplete="off" type="checkbox" class="preload" name="<?= OMGF_Admin_Settings::OMGF_OPTIMIZE_SETTING_PRELOAD_FONTS; ?>[<?= $handle; ?>][<?= $font->id; ?>][<?= $variant->id; ?>]" value="<?= $variant->id; ?>" <?= $preload ? 'checked="checked"' : ''; ?> <?= $unload ? 'disabled' : ''; ?> />
241
  </td>
242
  <td class="unload-<?= $class; ?>">
243
+ <input data-handle="<?= $handle; ?>" data-font-id="<?= $handle . '-' . $font->id; ?>" autocomplete="off" type="checkbox" class="unload" name="<?= OMGF_Admin_Settings::OMGF_OPTIMIZE_SETTING_UNLOAD_FONTS; ?>[<?= $handle; ?>][<?= $font->id; ?>][<?= $variant->id; ?>]" value="<?= $variant->id; ?>" <?= $unload ? 'checked="checked"' : ''; ?> <?= $preload ? 'disabled' : ''; ?> />
244
  </td>
245
  </tr>
246
  <?php endforeach; ?>
includes/api/class-download.php CHANGED
@@ -19,7 +19,7 @@ defined('ABSPATH') || exit;
19
  class OMGF_API_Download extends WP_REST_Controller
20
  {
21
  const OMGF_GOOGLE_FONTS_API_URL = 'https://google-webfonts-helper.herokuapp.com/api/fonts/';
22
- const OMGF_GOOGLE_FONTS_API_FALLBACK = 'https://n8n-google-fonts-helper.herokuapp.com/api/fonts/';
23
  const OMGF_USE_FALLBACK_API_TRANSIENT = 'omgf_use_fallback_api';
24
 
25
  /**
@@ -91,12 +91,7 @@ class OMGF_API_Download extends WP_REST_Controller
91
  */
92
  public function process($request)
93
  {
94
- if (strpos($request->get_route(), 'css2') !== false) {
95
- $this->convert_css2($request);
96
- }
97
-
98
- $params = $request->get_params();
99
- $this->handle = sanitize_title_with_dashes($params['handle']) ?? '';
100
  $original_handle = sanitize_title_with_dashes($request->get_param('original_handle'));
101
 
102
  if (!$this->handle || !$original_handle) {
@@ -104,8 +99,8 @@ class OMGF_API_Download extends WP_REST_Controller
104
  }
105
 
106
  $this->path = WP_CONTENT_DIR . OMGF_CACHE_PATH . '/' . $this->handle;
107
- $font_families = explode('|', $params['family']);
108
- $query['subsets'] = $params['subset'] ?? 'latin,latin-ext';
109
  $fonts = [];
110
 
111
  foreach ($font_families as $font_family) {
@@ -188,28 +183,21 @@ class OMGF_API_Download extends WP_REST_Controller
188
 
189
  file_put_contents($local_file, $stylesheet);
190
 
191
- $current_fonts = [$original_handle => $fonts];
192
- $optimized_fonts = OMGF::optimized_fonts();
193
-
194
- // At first run, simply override the optimized_fonts array.
195
- if (empty($optimized_fonts)) {
196
- $optimized_fonts = $current_fonts;
197
- // When a new font is detected, add it to the list.
198
- } elseif (!isset($optimized_fonts[$original_handle])) {
199
- $optimized_fonts = $optimized_fonts + $current_fonts;
200
- // Unload is probably used. Let's rewrite the variants still in use.
201
- } else {
202
- $optimized_fonts = $this->rewrite_variants($optimized_fonts, $current_fonts);
203
- }
204
 
205
  update_option(OMGF_Admin_Settings::OMGF_OPTIMIZE_SETTING_OPTIMIZED_FONTS, $optimized_fonts);
206
-
207
- // After generating it, serve it.
208
- header('Content-Type: text/css');
209
- header('Content-Length: ' . filesize($local_file));
210
- flush();
211
- readfile($local_file);
212
- exit();
213
  }
214
 
215
  /**
@@ -239,57 +227,6 @@ class OMGF_API_Download extends WP_REST_Controller
239
  );
240
  }
241
 
242
- /**
243
- * Converts requests to OMGF's Download/CSS2 API to a format readable by the regular API.
244
- *
245
- * @param $request WP_Rest_Request
246
- */
247
- private function convert_css2(&$request)
248
- {
249
- $query = $this->get_query_from_request();
250
- $params = explode('&', $query);
251
- $font_families = [];
252
- $fonts = [];
253
-
254
- foreach ($params as $param) {
255
- if (strpos($param, 'family') === false) {
256
- continue;
257
- }
258
-
259
- parse_str($param, $parts);
260
-
261
- $font_families[] = $parts;
262
- }
263
-
264
- foreach ($font_families as $font_family) {
265
- if (strpos($font_family, ':') !== false) {
266
- list($family, $weights) = explode(':', reset($font_family));
267
- } else {
268
- $family = $font_family;
269
- $weights = '';
270
- }
271
-
272
- /**
273
- * @return array [ '300', '400', '500', etc. ]
274
- */
275
- $weights = explode(';', substr($weights, strpos($weights, '@') + 1));
276
-
277
- $fonts[] = $family . ':' . implode(',', $weights);
278
- }
279
-
280
- $request->set_param('family', implode('|', $fonts));
281
- }
282
-
283
- /**
284
- * Since Google Fonts' variable fonts API uses the same name for each parameter ('family') we need to parse the url manually.
285
- *
286
- * @return mixed
287
- */
288
- private function get_query_from_request()
289
- {
290
- return parse_url($_SERVER['REQUEST_URI'])['query'];
291
- }
292
-
293
  /**
294
  * @param $font_family
295
  * @param $url
@@ -334,7 +271,7 @@ class OMGF_API_Download extends WP_REST_Controller
334
  );
335
 
336
  if (is_wp_error($response)) {
337
- OMGF_Admin_Notice::set_notice(sprintf(__('An error occurred while trying to fetch fonts: %s', $this->plugin_text_domain), $response->get_error_message()), $response->get_error_code(), true, 'error', 500);
338
  }
339
 
340
  $response_code = wp_remote_retrieve_response_code($response);
@@ -343,12 +280,12 @@ class OMGF_API_Download extends WP_REST_Controller
343
  $font_family = str_replace('-', ' ', $family);
344
  $error_body = wp_remote_retrieve_body($response);
345
  $error_message = wp_remote_retrieve_response_message($response);
346
- $message = sprintf(__('<strong>%s</strong> could not be found. The API returned the following error: %s.', $this->plugin_text_domain), ucwords($font_family), $error_message);
347
 
348
  OMGF_Admin_Notice::set_notice($message, 'omgf_api_error', false, 'error');
349
 
350
  if ($error_message == 'Service Unavailable') {
351
- $message = __('Google Fonts Helper API is currently unavailable. Try again later.', $this->plugin_text_domain);
352
 
353
  OMGF_Admin_Notice::set_notice($message, 'omgf_api_error', true, 'error', $response_code);
354
  }
@@ -498,23 +435,23 @@ class OMGF_API_Download extends WP_REST_Controller
498
  }
499
 
500
  /**
501
- * When unload is used, insert the cache key for the variants still in use.
502
  *
503
- * @param $stylesheets
504
- * @param $current_font
505
  *
506
  * @return mixed
507
  */
508
- private function rewrite_variants($stylesheets, $current_font)
509
  {
510
- foreach ($stylesheets as $stylesheet => &$fonts) {
511
  foreach ($fonts as $index => &$font) {
512
  if (empty((array) $font->variants)) {
513
  continue;
514
  }
515
 
516
  foreach ($font->variants as $variant_index => &$variant) {
517
- $replace_variant = $current_font[$stylesheet][$index]->variants[$variant_index] ?? (object) [];
518
 
519
  if (!empty((array) $replace_variant)) {
520
  $variant = $replace_variant;
@@ -523,6 +460,6 @@ class OMGF_API_Download extends WP_REST_Controller
523
  }
524
  }
525
 
526
- return $stylesheets;
527
  }
528
  }
19
  class OMGF_API_Download extends WP_REST_Controller
20
  {
21
  const OMGF_GOOGLE_FONTS_API_URL = 'https://google-webfonts-helper.herokuapp.com/api/fonts/';
22
+ const OMGF_GOOGLE_FONTS_API_FALLBACK = 'https://omgf-google-fonts-api.herokuapp.com/api/fonts/';
23
  const OMGF_USE_FALLBACK_API_TRANSIENT = 'omgf_use_fallback_api';
24
 
25
  /**
91
  */
92
  public function process($request)
93
  {
94
+ $this->handle = sanitize_title_with_dashes($request->get_param('handle'));
 
 
 
 
 
95
  $original_handle = sanitize_title_with_dashes($request->get_param('original_handle'));
96
 
97
  if (!$this->handle || !$original_handle) {
99
  }
100
 
101
  $this->path = WP_CONTENT_DIR . OMGF_CACHE_PATH . '/' . $this->handle;
102
+ $font_families = explode('|', $request->get_param('family'));
103
+ $query['subsets'] = $request->get_param('subset') ?? 'latin,latin-ext';
104
  $fonts = [];
105
 
106
  foreach ($font_families as $font_family) {
183
 
184
  file_put_contents($local_file, $stylesheet);
185
 
186
+ $current_stylesheet = [$original_handle => $fonts];
187
+ /**
188
+ * $current_stylesheet is added to temporary cache layer, if it isn't present in database.
189
+ *
190
+ * @since v4.5.7
191
+ */
192
+ $optimized_fonts = OMGF::optimized_fonts($current_stylesheet);
193
+ /**
194
+ * When unload is used, this takes care of rewriting the font style URLs in the database.
195
+ *
196
+ * @since v4.5.7
197
+ */
198
+ $optimized_fonts = $this->rewrite_variants($optimized_fonts, $current_stylesheet);
199
 
200
  update_option(OMGF_Admin_Settings::OMGF_OPTIMIZE_SETTING_OPTIMIZED_FONTS, $optimized_fonts);
 
 
 
 
 
 
 
201
  }
202
 
203
  /**
227
  );
228
  }
229
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230
  /**
231
  * @param $font_family
232
  * @param $url
271
  );
272
 
273
  if (is_wp_error($response)) {
274
+ OMGF_Admin_Notice::set_notice(sprintf(__('OMGF encountered an error while trying to fetch fonts: %s', $this->plugin_text_domain), $response->get_error_message()), $response->get_error_code(), true, 'error', 500);
275
  }
276
 
277
  $response_code = wp_remote_retrieve_response_code($response);
280
  $font_family = str_replace('-', ' ', $family);
281
  $error_body = wp_remote_retrieve_body($response);
282
  $error_message = wp_remote_retrieve_response_message($response);
283
+ $message = sprintf(__('OMGF could not find <strong>%s</strong>. The API returned the following error: %s.', $this->plugin_text_domain), ucwords($font_family), $error_message);
284
 
285
  OMGF_Admin_Notice::set_notice($message, 'omgf_api_error', false, 'error');
286
 
287
  if ($error_message == 'Service Unavailable') {
288
+ $message = __('OMGF\'s Google Fonts API is currently unavailable. Try again later.', $this->plugin_text_domain);
289
 
290
  OMGF_Admin_Notice::set_notice($message, 'omgf_api_error', true, 'error', $response_code);
291
  }
435
  }
436
 
437
  /**
438
+ * When unload is used, insert the cache key in the font URLs for the variants still in use.
439
  *
440
+ * @param array $all_stylesheets Contains all font styles, loaded and unloaded.
441
+ * @param array $current_stylesheet Contains just the loaded font styles of current stylesheet.
442
  *
443
  * @return mixed
444
  */
445
+ private function rewrite_variants($all_stylesheets, $current_stylesheet)
446
  {
447
+ foreach ($all_stylesheets as $stylesheet => &$fonts) {
448
  foreach ($fonts as $index => &$font) {
449
  if (empty((array) $font->variants)) {
450
  continue;
451
  }
452
 
453
  foreach ($font->variants as $variant_index => &$variant) {
454
+ $replace_variant = $current_stylesheet[$stylesheet][$index]->variants[$variant_index] ?? (object) [];
455
 
456
  if (!empty((array) $replace_variant)) {
457
  $variant = $replace_variant;
460
  }
461
  }
462
 
463
+ return $all_stylesheets;
464
  }
465
  }
includes/class-admin.php CHANGED
@@ -55,7 +55,8 @@ class OMGF_Admin
55
  $this->do_help();
56
  $this->maybe_do_after_update_notice();
57
 
58
- add_filter('pre_update_option_omgf_optimized_fonts', [$this, 'update_optimized_fonts'], 10, 2);
 
59
  add_filter('pre_update_option_omgf_cache_keys', [$this, 'clean_up_cache'], 10, 3);
60
  add_filter('pre_update_option', [$this, 'settings_changed'], 10, 3);
61
  }
55
  $this->do_help();
56
  $this->maybe_do_after_update_notice();
57
 
58
+ // This used to fix a bug, but now it breaks stuff. Leave it here for the time being.
59
+ // add_filter('pre_update_option_omgf_optimized_fonts', [$this, 'update_optimized_fonts'], 10, 2);
60
  add_filter('pre_update_option_omgf_cache_keys', [$this, 'clean_up_cache'], 10, 3);
61
  add_filter('pre_update_option', [$this, 'settings_changed'], 10, 3);
62
  }
includes/class-omgf.php CHANGED
@@ -84,12 +84,24 @@ class OMGF
84
  }
85
 
86
  /**
 
 
 
 
 
 
 
 
87
  * @return array
88
  */
89
- public static function optimized_fonts()
90
  {
91
- static $optimized_fonts = [];
 
92
 
 
 
 
93
  if (empty($optimized_fonts)) {
94
  $optimized_fonts = get_option(OMGF_Admin_Settings::OMGF_OPTIMIZE_SETTING_OPTIMIZED_FONTS, []) ?: [];
95
  }
@@ -103,6 +115,13 @@ class OMGF
103
  $optimized_fonts = unserialize($optimized_fonts);
104
  }
105
 
 
 
 
 
 
 
 
106
  return $optimized_fonts;
107
  }
108
 
84
  }
85
 
86
  /**
87
+ * Manage Optimized Fonts to be displayed in the Optimized Fonts table.
88
+ *
89
+ * Use a static variable to reduce database reads/writes.
90
+ *
91
+ * @since v4.5.7
92
+ *
93
+ * @param array $maybe_add If it doesn't exist, it's added to the cache layer.
94
+ *
95
  * @return array
96
  */
97
+ public static function optimized_fonts($maybe_add = [])
98
  {
99
+ /** @var array $optimized_fonts Cache layer */
100
+ static $optimized_fonts;
101
 
102
+ /**
103
+ * Get a fresh copy from the database if $optimized_fonts is empty|null|false (on 1st run)
104
+ */
105
  if (empty($optimized_fonts)) {
106
  $optimized_fonts = get_option(OMGF_Admin_Settings::OMGF_OPTIMIZE_SETTING_OPTIMIZED_FONTS, []) ?: [];
107
  }
115
  $optimized_fonts = unserialize($optimized_fonts);
116
  }
117
 
118
+ /**
119
+ * If $maybe_add doesn't exist in the cache layer yet, add it.
120
+ */
121
+ if (!isset($optimized_fonts[key($maybe_add)])) {
122
+ $optimized_fonts = $optimized_fonts + $maybe_add;
123
+ }
124
+
125
  return $optimized_fonts;
126
  }
127
 
includes/optimization-mode/class-manual.php CHANGED
@@ -39,7 +39,7 @@ class OMGF_OptimizationMode_Manual
39
  private function run()
40
  {
41
  $url = esc_url_raw(OMGF_MANUAL_OPTIMIZE_URL);
42
- $front_html = $this->remote_get($url);
43
  $error = false;
44
 
45
  if (is_wp_error($front_html) || wp_remote_retrieve_response_code($front_html) != 200) {
@@ -68,9 +68,13 @@ class OMGF_OptimizationMode_Manual
68
  }
69
 
70
  foreach ($api_request_urls as $url) {
71
- $download = $this->remote_get($url);
 
 
 
 
72
 
73
- if (is_wp_error($download) || wp_remote_retrieve_response_code($download) != 200) {
74
  $this->download_failed($download);
75
 
76
  $error = true;
@@ -82,6 +86,83 @@ class OMGF_OptimizationMode_Manual
82
  }
83
  }
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  /**
86
  * @return void
87
  */
@@ -127,7 +208,7 @@ class OMGF_OptimizationMode_Manual
127
  * @param mixed $url
128
  * @return array|WP_Error
129
  */
130
- private function remote_get($url)
131
  {
132
  return wp_remote_get(
133
  $this->no_cache_optimize_url($url),
39
  private function run()
40
  {
41
  $url = esc_url_raw(OMGF_MANUAL_OPTIMIZE_URL);
42
+ $front_html = $this->get_front_html($url);
43
  $error = false;
44
 
45
  if (is_wp_error($front_html) || wp_remote_retrieve_response_code($front_html) != 200) {
68
  }
69
 
70
  foreach ($api_request_urls as $url) {
71
+ if (strpos($url, 'css2') !== false) {
72
+ $url = $this->convert($url);
73
+ }
74
+
75
+ $download = $this->do_rest_request($url);
76
 
77
+ if (isset($download->status) && $download->status != 200) {
78
  $this->download_failed($download);
79
 
80
  $error = true;
86
  }
87
  }
88
 
89
+
90
+ /**
91
+ * Converts requests to Google's Variable Fonts (CSS2) API to the 'old' (CSS) API.
92
+ *
93
+ * @param string $css2_url
94
+ */
95
+ private function convert($css2_url)
96
+ {
97
+ $parsed_url = parse_url($css2_url);
98
+ $query = $parsed_url['query'];
99
+ parse_str($query, $params);
100
+ $raw_params = explode('&', $query);
101
+ $font_families = [];
102
+ $fonts = [];
103
+
104
+ foreach ($raw_params as $param) {
105
+ if (strpos($param, 'family') === false) {
106
+ continue;
107
+ }
108
+
109
+ parse_str($param, $parts);
110
+
111
+ $font_families[] = $parts['family'];
112
+ }
113
+
114
+ if (empty($font_families)) {
115
+ return $css2_url;
116
+ }
117
+
118
+ foreach ($font_families as $font_family) {
119
+ if (strpos($font_family, ':') !== false) {
120
+ list($family, $weights) = explode(':', $font_family);
121
+ } else {
122
+ $family = $font_family;
123
+ $weights = '';
124
+ }
125
+
126
+ /**
127
+ * @return array [ '300', '400', '500', etc. ]
128
+ */
129
+ $weights = explode(';', substr($weights, strpos($weights, '@') + 1));
130
+
131
+ foreach ($weights as &$weight) {
132
+ $properties = explode(',', $weight);
133
+ $weight = $properties[0] == '1' ? $properties[1] . 'italic' : $properties[1];
134
+ }
135
+
136
+ $fonts[] = $family . ':' . implode(',', $weights);
137
+ }
138
+
139
+ $params['family'] = implode('|', $fonts);
140
+
141
+ return $parsed_url['scheme'] . '://' . $parsed_url['host'] . $parsed_url['path'] . '?' . http_build_query($params);
142
+ }
143
+
144
+ /**
145
+ *
146
+ */
147
+ private function do_rest_request($url)
148
+ {
149
+ $parsed_url = parse_url($url);
150
+ $request = new WP_REST_Request('GET', str_replace('/wp-json', '', $parsed_url['path']));
151
+
152
+ parse_str($parsed_url['query'], $query_params);
153
+
154
+ if (isset($query_params['_wpnonce'])) {
155
+ unset($query_params['_wpnonce']);
156
+ }
157
+
158
+ $request->set_query_params($query_params);
159
+
160
+ // TODO: Find out proper WP way to add this param to request.
161
+ $_REQUEST['_wpnonce'] = wp_create_nonce('wp_rest');
162
+
163
+ return rest_do_request($request);
164
+ }
165
+
166
  /**
167
  * @return void
168
  */
208
  * @param mixed $url
209
  * @return array|WP_Error
210
  */
211
+ private function get_front_html($url)
212
  {
213
  return wp_remote_get(
214
  $this->no_cache_optimize_url($url),
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: DaanvandenBergh
3
  Tags: google, fonts, gdpr, cache, speed, preload, font-display, webfonts, subsets, remove, minimize, external, requests
4
  Requires at least: 4.6
5
  Tested up to: 5.8
6
- Stable tag: 4.5.6
7
  Requires PHP: 7.0
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
@@ -46,7 +46,7 @@ Everything in the free version, plus:
46
  - Specify a Fallback Font Stack for every Google Font, to reduce Cumulative Layout Shift,
47
  - Automatically remove/replace all Google Fonts throughout the entire document/page,
48
  - Also supports WebFont Loader (webfont.js), Early Access Google Fonts and requests in stylesheets using @import and @font-face statements.
49
- - Automatically generate different stylesheets for pages with another configuration of Google Fonts.
50
  - Combine all Google Fonts stylesheets (requested by your theme and/or plugins) into one file,
51
  - Deduplicate Google Fonts stylesheets,
52
  - Define file types to include in stylesheet (WOFF, WOFF2, EOT, TTF, SVG),
@@ -70,10 +70,6 @@ For a more comprehensive guide on configuring OMGF, check out the [user manual](
70
 
71
  == Frequently Asked Questions ==
72
 
73
- = Why do my fonts load slow the first time? =
74
-
75
- When OMGF runs in Automatic (Pro) mode, all requests to Google Fonts' API are rewritten and point to OMGF's on-premise download API. The API downloads the fonts and generates the stylesheet, which takes a while. When this is finished, the API will not be used anymore and the stylesheet and its fonts will be loaded directly, just like any other file.
76
-
77
  = I don't know what I'm doing! Can you help? =
78
 
79
  Of course :) But first calm down and read the [user manual](https://ffw.press/docs/omgf-pro/user-manual/). If you have any questions afterwards, visit the [Support Forum](https://wordpress.org/support/plugin/host-webfonts-local).
@@ -132,6 +128,17 @@ No, not yet. But I will definitely try to make it compatible in the future!
132
 
133
  == Changelog ==
134
 
 
 
 
 
 
 
 
 
 
 
 
135
  = 4.5.6 =
136
  * Fix: Added Fallback API URL for when Google Fonts Helper is down.
137
  * Enhancement: Added extra error handling in Manual Optimization Mode.
3
  Tags: google, fonts, gdpr, cache, speed, preload, font-display, webfonts, subsets, remove, minimize, external, requests
4
  Requires at least: 4.6
5
  Tested up to: 5.8
6
+ Stable tag: 4.5.7
7
  Requires PHP: 7.0
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
46
  - Specify a Fallback Font Stack for every Google Font, to reduce Cumulative Layout Shift,
47
  - Automatically remove/replace all Google Fonts throughout the entire document/page,
48
  - Also supports WebFont Loader (webfont.js), Early Access Google Fonts and requests in stylesheets using @import and @font-face statements.
49
+ - Automatically generate different stylesheets for pages with different Google Fonts configurations.
50
  - Combine all Google Fonts stylesheets (requested by your theme and/or plugins) into one file,
51
  - Deduplicate Google Fonts stylesheets,
52
  - Define file types to include in stylesheet (WOFF, WOFF2, EOT, TTF, SVG),
70
 
71
  == Frequently Asked Questions ==
72
 
 
 
 
 
73
  = I don't know what I'm doing! Can you help? =
74
 
75
  Of course :) But first calm down and read the [user manual](https://ffw.press/docs/omgf-pro/user-manual/). If you have any questions afterwards, visit the [Support Forum](https://wordpress.org/support/plugin/host-webfonts-local).
128
 
129
  == Changelog ==
130
 
131
+ = 4.5.7 | September 29th, 2021 =
132
+ * Enhancement: significantly reduced code running frontend.
133
+ * Fix: internal requests to OMGF's Download API are no longer treated as 'remote'.
134
+ * Fix: stylesheets are no longer skipped in some situations by the temp storage layer, before writing them to the database.
135
+ * Fix: using the mass actions (e.g. unload all, unload italics) no longer affect font families with the same name in a stylesheet with a different handle.
136
+ * Fix: Italic fonts are now properly detected by the API when CSS2 (variable fonts) API is used by themes and/or plugins.
137
+ * Fix: Added my own self-managed fallback API mirror to prevent more Google Fonts API downtime.
138
+ * Enhancement: reduced code in Download API by ~20%.
139
+ * Dev: add-ons for OMGF can now use the show_loader() method.
140
+ * Several UX and performance tweaks.
141
+
142
  = 4.5.6 =
143
  * Fix: Added Fallback API URL for when Google Fonts Helper is down.
144
  * Enhancement: Added extra error handling in Manual Optimization Mode.