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

Version Description

Fixed 400-error when re-downloading fonts. Added compatibility for Enfold-theme.

Download this release

Release Info

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

Code changes from version 1.9.11 to 2.0.8

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 and leverage browser cache by easily saving Google Fonts to your server and removing the external Google Fonts.
7
- * Version: 1.9.11
8
  * Author: Daan van den Bergh
9
  * Author URI: https://daan.dev
10
  * License: GPL2v2 or later
@@ -22,44 +22,56 @@ global $wpdb;
22
  /**
23
  * Define constants.
24
  */
25
- define('CAOS_WEBFONTS_DB_VERSION', '1.8.3');
26
- define('CAOS_WEBFONTS_STATIC_VERSION', '1.9.11');
27
- define('CAOS_WEBFONTS_SITE_URL', 'https://daan.dev');
28
- define('CAOS_WEBFONTS_DB_TABLENAME', $wpdb->prefix . 'caos_webfonts');
29
- define('CAOS_WEBFONTS_DB_CHARSET', $wpdb->get_charset_collate());
30
- define('CAOS_WEBFONTS_HELPER_URL', 'https://google-webfonts-helper.herokuapp.com/api/fonts/');
31
- define('CAOS_WEBFONTS_FILENAME', 'fonts.css');
32
- define('CAOS_WEBFONTS_CACHE_DIR', esc_attr(get_option('caos_webfonts_cache_dir')) ?: '/cache/omgf-webfonts');
33
- define('CAOS_WEBFONTS_CDN_URL', esc_attr(get_option('caos_webfonts_cdn_url')));
34
- define('CAOS_WEBFONTS_REMOVE_VERSION', esc_attr(get_option('caos_webfonts_remove_version')));
35
- define('CAOS_WEBFONTS_CURRENT_BLOG_ID', get_current_blog_id());
36
- define('CAOS_WEBFONTS_UPLOAD_DIR', WP_CONTENT_DIR . CAOS_WEBFONTS_CACHE_DIR);
37
- define('CAOS_WEBFONTS_UPLOAD_URL', hwlGetUploadUrl());
38
- define('CAOS_WEBFONTS_DISPLAY_OPTION', esc_attr(get_option('caos_webfonts_display_option')) ?: 'auto');
39
- define('CAOS_WEBFONTS_REMOVE_GFONTS', esc_attr(get_option('caos_webfonts_remove_gfonts')));
40
- define('CAOS_WEBFONTS_PRELOAD', esc_attr(get_option('caos_webfonts_preload')));
 
 
41
 
42
  /**
43
  * Register settings
44
  */
45
  function hwlRegisterSettings()
46
  {
47
- register_setting('caos-webfonts-basic-settings',
 
48
  'caos_webfonts_cache_dir'
49
  );
50
- register_setting('caos-webfonts-basic-settings',
 
51
  'caos_webfonts_cdn_url'
52
  );
53
- register_setting('caos-webfonts-basic-settings',
 
 
 
 
 
54
  'caos_webfonts_remove_version'
55
  );
56
- register_setting('caos-webfonts-basic-settings',
 
57
  'caos_webfonts_display_option'
58
  );
59
- register_setting('caos-webfonts-basic-settings',
 
60
  'caos_webfonts_remove_gfonts'
61
  );
62
- register_setting('caos-webfonts-basic-settings',
 
63
  'caos_webfonts_preload'
64
  );
65
  }
@@ -76,10 +88,12 @@ function hwlCreateMenu()
76
  'optimize-webfonts',
77
  'hwlSettingsPage'
78
  );
79
- add_action('admin_init',
 
80
  'hwlRegisterSettings'
81
  );
82
  }
 
83
  add_action('admin_menu', 'hwlCreateMenu');
84
 
85
  /**
@@ -99,10 +113,10 @@ function hwlGetContentDirName()
99
  */
100
  function hwlGetUploadUrl()
101
  {
102
- if (CAOS_WEBFONTS_CDN_URL) {
103
- $uploadUrl = '//' . CAOS_WEBFONTS_CDN_URL . '/' . hwlGetContentDirName() . CAOS_WEBFONTS_CACHE_DIR;
104
  } else {
105
- $uploadUrl = get_site_url(CAOS_WEBFONTS_CURRENT_BLOG_ID, hwlGetContentDirName() . CAOS_WEBFONTS_CACHE_DIR);
106
  }
107
 
108
  return $uploadUrl;
@@ -114,7 +128,7 @@ function hwlGetUploadUrl()
114
  function hwlCreateWebfontsTable()
115
  {
116
  global $wpdb;
117
- $sql = "CREATE TABLE IF NOT EXISTS " . CAOS_WEBFONTS_DB_TABLENAME . " (
118
  font_id varchar(191) NOT NULL,
119
  font_family varchar(191) NOT NULL,
120
  font_weight mediumint(5) NOT NULL,
@@ -125,7 +139,7 @@ function hwlCreateWebfontsTable()
125
  url_woff2 varchar(191) NULL,
126
  url_eot varchar(191) NULL,
127
  UNIQUE KEY (font_id)
128
- ) " . CAOS_WEBFONTS_DB_CHARSET . ";";
129
  $wpdb->query($sql);
130
 
131
  add_option('caos_webfonts_db_version', '1.6.1');
@@ -137,13 +151,13 @@ function hwlCreateWebfontsTable()
137
  function hwlCreateSubsetsTable()
138
  {
139
  global $wpdb;
140
- $sql = "CREATE TABLE IF NOT EXISTS " . CAOS_WEBFONTS_DB_TABLENAME . '_subsets' . " (
141
  subset_font varchar(32) NOT NULL,
142
  subset_family varchar(191) NOT NULL,
143
  available_subsets varchar(191) NOT NULL,
144
  selected_subsets varchar(191) NOT NULL,
145
  UNIQUE KEY (subset_font)
146
- ) " . CAOS_WEBFONTS_DB_CHARSET . ";";
147
  $wpdb->query($sql);
148
 
149
  update_option('caos_webfonts_db_version', '1.7.0');
@@ -156,7 +170,7 @@ function hwlAddLocalColumn()
156
  {
157
  global $wpdb;
158
 
159
- $sql = "ALTER TABLE " . CAOS_WEBFONTS_DB_TABLENAME . " " .
160
  "ADD COLUMN local varchar(128) AFTER font_style;";
161
  $wpdb->query($sql);
162
 
@@ -175,10 +189,11 @@ function hwlRunDbUpdates()
175
  if (version_compare($currentVersion, '1.7.0') < 0) {
176
  hwlCreateSubsetsTable();
177
  }
178
- if (version_compare($currentVersion, CAOS_WEBFONTS_DB_VERSION) < 0) {
179
  hwlAddLocalColumn();
180
  }
181
  }
 
182
  add_action('plugins_loaded', 'hwlRunDbUpdates');
183
 
184
  /**
@@ -194,6 +209,7 @@ function hwlSettingsLink($links)
194
 
195
  return $links;
196
  }
 
197
  $caosLink = plugin_basename(__FILE__);
198
 
199
  add_filter("plugin_action_links_$caosLink", 'hwlSettingsLink');
@@ -211,21 +227,16 @@ function hwlSettingsPage()
211
  <h1><?php _e('OMGF | Optimize My Google Fonts', 'host-webfonts-local'); ?></h1>
212
  <p>
213
  <?php _e('Developed by: ', 'host-webfonts-local'); ?>
214
- <a title="Buy me a beer!" href="<?php echo CAOS_WEBFONTS_SITE_URL; ?>/donate/">
215
- Daan van den Bergh</a>.
216
  </p>
217
 
218
  <div id="hwl-admin-notices"></div>
219
 
220
- <?php require_once(dirname(__FILE__) . '/includes/templates/settings-welcome.php'); ?>
221
 
222
  <form id="hwl-options-form" class="settings-column left" name="hwl-options-form" method="post">
223
  <div class="">
224
- <?php
225
-
226
- require_once(dirname(__FILE__) . '/includes/templates/settings-generate-stylesheet.php');
227
-
228
- ?>
229
  </div>
230
  </form>
231
 
@@ -234,7 +245,7 @@ function hwlSettingsPage()
234
  settings_fields('caos-webfonts-basic-settings');
235
  do_settings_sections('caos-webfonts-basic-settings');
236
 
237
- require_once(dirname(__FILE__) . '/includes/templates/settings-basic-settings.php');
238
 
239
  do_action('hwl_after_settings_form_settings');
240
 
@@ -253,7 +264,7 @@ function hwlGetTotalFonts()
253
  global $wpdb;
254
 
255
  try {
256
- return $wpdb->get_results("SELECT * FROM " . CAOS_WEBFONTS_DB_TABLENAME);
257
  } catch (\Exception $e) {
258
  return $e;
259
  }
@@ -267,7 +278,7 @@ function hwlGetDownloadedFonts()
267
  global $wpdb;
268
 
269
  try {
270
- return $wpdb->get_results("SELECT * FROM " . CAOS_WEBFONTS_DB_TABLENAME . " WHERE downloaded = 1");
271
  } catch (\Exception $e) {
272
  return $e;
273
  }
@@ -292,7 +303,7 @@ function hwlGetSubsets()
292
  global $wpdb;
293
 
294
  try {
295
- return $wpdb->get_results("SELECT * FROM " . CAOS_WEBFONTS_DB_TABLENAME . "_subsets");
296
  } catch (\Exception $e) {
297
  return $e;
298
  }
@@ -303,22 +314,22 @@ function hwlGetFontsByFamily($family)
303
  global $wpdb;
304
 
305
  try {
306
- return $wpdb->get_results("SELECT * FROM " . CAOS_WEBFONTS_DB_TABLENAME . " WHERE font_family = '$family'");
307
  } catch (\Exception $e) {
308
  return $e;
309
  }
310
  }
311
 
312
  /**
313
- * @return \Exception|false|int
314
  */
315
  function hwlCleanQueue()
316
  {
317
  global $wpdb;
318
 
319
  try {
320
- $wpdb->query("TRUNCATE TABLE " . CAOS_WEBFONTS_DB_TABLENAME);
321
- $wpdb->query("TRUNCATE TABLE " . CAOS_WEBFONTS_DB_TABLENAME . "_subsets");
322
  } catch (\Exception $e) {
323
  return $e;
324
  }
@@ -335,6 +346,7 @@ function hwlAjaxGetDownloadStatus()
335
  )
336
  );
337
  }
 
338
  add_action('wp_ajax_hwlAjaxGetDownloadStatus', 'hwlAjaxGetDownloadStatus');
339
 
340
  /**
@@ -344,6 +356,7 @@ function hwlAjaxCleanQueue()
344
  {
345
  return wp_die(hwlCleanQueue());
346
  }
 
347
  add_action('wp_ajax_hwlAjaxCleanQueue', 'hwlAjaxCleanQueue');
348
 
349
  /**
@@ -353,8 +366,9 @@ add_action('wp_ajax_hwlAjaxCleanQueue', 'hwlAjaxCleanQueue');
353
  */
354
  function hwlAjaxEmptyDir()
355
  {
356
- return array_map('unlink', array_filter((array) glob(CAOS_WEBFONTS_UPLOAD_DIR . '/*')));
357
  }
 
358
  add_action('wp_ajax_hwlAjaxEmptyDir', 'hwlAjaxEmptyDir');
359
 
360
  /**
@@ -367,7 +381,7 @@ function hwlAjaxSearchFontSubsets()
367
 
368
  foreach ($searchQueries as $searchQuery) {
369
  $request = curl_init();
370
- curl_setopt($request, CURLOPT_URL, CAOS_WEBFONTS_HELPER_URL . $searchQuery);
371
  curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
372
  $result = curl_exec($request);
373
  curl_close($request);
@@ -384,6 +398,7 @@ function hwlAjaxSearchFontSubsets()
384
  wp_die($e);
385
  }
386
  }
 
387
  add_action('wp_ajax_hwlAjaxSearchFontSubsets', 'hwlAjaxSearchFontSubsets');
388
 
389
  /**
@@ -396,7 +411,7 @@ function hwlAjaxSearchGoogleFonts()
396
  $searchQuery = sanitize_text_field($_POST['search_query']);
397
  $subsets = implode($_POST['search_subsets'], ',');
398
 
399
- curl_setopt($request, CURLOPT_URL, CAOS_WEBFONTS_HELPER_URL . $searchQuery . '?subsets=' . $subsets);
400
  curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
401
 
402
  $result = curl_exec($request);
@@ -407,6 +422,7 @@ function hwlAjaxSearchGoogleFonts()
407
  wp_die($e);
408
  }
409
  }
 
410
  add_action('wp_ajax_hwlAjaxSearchGoogleFonts', 'hwlAjaxSearchGoogleFonts');
411
 
412
  /**
@@ -414,11 +430,12 @@ add_action('wp_ajax_hwlAjaxSearchGoogleFonts', 'hwlAjaxSearchGoogleFonts');
414
  */
415
  function hwlCreateCacheDir()
416
  {
417
- $uploadDir = CAOS_WEBFONTS_UPLOAD_DIR;
418
  if (!is_dir($uploadDir)) {
419
  wp_mkdir_p($uploadDir);
420
  }
421
  }
 
422
  register_activation_hook(__FILE__, 'hwlCreateCacheDir');
423
 
424
  /**
@@ -442,8 +459,9 @@ function hwlFontDisplayOptions()
442
  */
443
  function hwlAjaxGenerateStyles()
444
  {
445
- require_once(plugin_dir_path(__FILE__) . 'includes/ajax/generate-stylesheet.php');
446
  }
 
447
  add_action('wp_ajax_hwlAjaxGenerateStyles', 'hwlAjaxGenerateStyles');
448
 
449
  /**
@@ -451,8 +469,9 @@ add_action('wp_ajax_hwlAjaxGenerateStyles', 'hwlAjaxGenerateStyles');
451
  */
452
  function hwlAjaxDownloadFonts()
453
  {
454
- require_once(plugin_dir_path(__FILE__) . 'includes/ajax/download-fonts.php');
455
  }
 
456
  add_action('wp_ajax_hwlAjaxDownloadFonts', 'hwlAjaxDownloadFonts');
457
 
458
  /**
@@ -460,8 +479,13 @@ add_action('wp_ajax_hwlAjaxDownloadFonts', 'hwlAjaxDownloadFonts');
460
  */
461
  function hwlEnqueueStylesheet()
462
  {
463
- wp_enqueue_style('omgf-fonts', CAOS_WEBFONTS_UPLOAD_URL . '/' . CAOS_WEBFONTS_FILENAME, array(), (CAOS_WEBFONTS_REMOVE_VERSION) ? null : CAOS_WEBFONTS_STATIC_VERSION);
 
 
 
 
464
  }
 
465
  add_action('wp_enqueue_scripts', 'hwlEnqueueStylesheet');
466
 
467
  /**
@@ -470,10 +494,11 @@ add_action('wp_enqueue_scripts', 'hwlEnqueueStylesheet');
470
  function hwlEnqueueAdminJs($hook)
471
  {
472
  if ($hook == 'settings_page_optimize-webfonts') {
473
- wp_enqueue_script('hwl-admin-js', plugin_dir_url(__FILE__) . 'js/hwl-admin.js', array('jquery'), CAOS_WEBFONTS_STATIC_VERSION, true);
474
- wp_enqueue_style('hwl-admin-css', plugin_dir_url(__FILE__) . 'css/hwl-admin.css', array(), CAOS_WEBFONTS_STATIC_VERSION);
475
  }
476
  }
 
477
  add_action('admin_enqueue_scripts', 'hwlEnqueueAdminJs');
478
 
479
  /**
@@ -485,6 +510,7 @@ function hwlDequeueJsCss()
485
  wp_dequeue_style('hwl-admin-css');
486
  wp_dequeue_style('omgf-fonts');
487
  }
 
488
  register_deactivation_hook(__FILE__, 'hwlDequeueJsCss');
489
 
490
  /**
@@ -495,7 +521,7 @@ function hwlAddLinkPreload()
495
  global $wp_styles;
496
 
497
  $handle = 'omgf-fonts';
498
- $style = $wp_styles->registered[$handle];
499
 
500
  /** Do not add 'preload' if Minification plugins are enabled. */
501
  if ($style) {
@@ -506,10 +532,11 @@ function hwlAddLinkPreload()
506
 
507
  function hwlIsPreloadEnabled()
508
  {
509
- if (CAOS_WEBFONTS_PRELOAD == 'on') {
510
  add_action('wp_head', 'hwlAddLinkPreload', 1);
511
  }
512
  }
 
513
  add_action('init', 'hwlIsPreloadEnabled');
514
 
515
  /**
@@ -521,16 +548,18 @@ function hwlRemoveGoogleFonts()
521
  {
522
  global $wp_styles;
523
 
524
- $registered = $wp_styles->registered;
525
-
526
- $fonts = array_filter($registered, function ($contents) {
527
  return strpos($contents->src, 'fonts.googleapis.com') !== false
528
  || strpos($contents->src, 'fonts.gstatic.com') !== false;
529
- });
530
-
531
- $dependencies = array_filter($registered, function ($contents) use ($fonts) {
 
532
  return !empty(array_intersect(array_keys($fonts), $contents->deps));
533
- });
 
534
 
535
  foreach ($fonts as $font) {
536
  wp_deregister_style($font->handle);
@@ -545,8 +574,14 @@ function hwlRemoveGoogleFonts()
545
 
546
  function hwlIsRemoveGoogleFontsEnabled()
547
  {
548
- if (CAOS_WEBFONTS_REMOVE_GFONTS == 'on' && !is_admin()) {
549
  add_action('wp_print_styles', 'hwlRemoveGoogleFonts', PHP_INT_MAX);
 
 
 
 
 
550
  }
551
  }
 
552
  add_action('init', 'hwlIsRemoveGoogleFontsEnabled');
4
  * Plugin Name: OMGF
5
  * Plugin URI: https://daan.dev/wordpress-plugins/host-google-fonts-locally
6
  * Description: Minimize DNS requests and leverage browser cache by easily saving Google Fonts to your server and removing the external Google Fonts.
7
+ * Version: 2.0.8
8
  * Author: Daan van den Bergh
9
  * Author URI: https://daan.dev
10
  * License: GPL2v2 or later
22
  /**
23
  * Define constants.
24
  */
25
+ define('OMGF_DB_VERSION', '1.8.3');
26
+ define('OMGF_STATIC_VERSION', '2.0.2');
27
+ define('OMGF_WEB_FONT_LOADER_VERSION', '1.6.26');
28
+ define('OMGF_SITE_URL', 'https://daan.dev');
29
+ define('OMGF_DB_TABLENAME', $wpdb->prefix . 'caos_webfonts');
30
+ define('OMGF_DB_CHARSET', $wpdb->get_charset_collate());
31
+ define('OMGF_HELPER_URL', 'https://google-webfonts-helper.herokuapp.com/api/fonts/');
32
+ define('OMGF_FILENAME', 'fonts.css');
33
+ define('OMGF_CACHE_DIR', esc_attr(get_option('caos_webfonts_cache_dir')) ?: '/cache/omgf-webfonts');
34
+ define('OMGF_CDN_URL', esc_attr(get_option('caos_webfonts_cdn_url')));
35
+ define('OMGF_WEB_FONT_LOADER', esc_attr(get_option('omgf_web_font_loader')));
36
+ define('OMGF_REMOVE_VERSION', esc_attr(get_option('caos_webfonts_remove_version')));
37
+ define('OMGF_CURRENT_BLOG_ID', get_current_blog_id());
38
+ define('OMGF_UPLOAD_DIR', WP_CONTENT_DIR . OMGF_CACHE_DIR);
39
+ define('OMGF_UPLOAD_URL', hwlGetUploadUrl());
40
+ define('OMGF_DISPLAY_OPTION', esc_attr(get_option('caos_webfonts_display_option')) ?: 'auto');
41
+ define('OMGF_REMOVE_GFONTS', esc_attr(get_option('caos_webfonts_remove_gfonts')));
42
+ define('OMGF_PRELOAD', esc_attr(get_option('caos_webfonts_preload')));
43
 
44
  /**
45
  * Register settings
46
  */
47
  function hwlRegisterSettings()
48
  {
49
+ register_setting(
50
+ 'caos-webfonts-basic-settings',
51
  'caos_webfonts_cache_dir'
52
  );
53
+ register_setting(
54
+ 'caos-webfonts-basic-settings',
55
  'caos_webfonts_cdn_url'
56
  );
57
+ register_setting(
58
+ 'caos-webfonts-basic-settings',
59
+ 'omgf_web_font_loader'
60
+ );
61
+ register_setting(
62
+ 'caos-webfonts-basic-settings',
63
  'caos_webfonts_remove_version'
64
  );
65
+ register_setting(
66
+ 'caos-webfonts-basic-settings',
67
  'caos_webfonts_display_option'
68
  );
69
+ register_setting(
70
+ 'caos-webfonts-basic-settings',
71
  'caos_webfonts_remove_gfonts'
72
  );
73
+ register_setting(
74
+ 'caos-webfonts-basic-settings',
75
  'caos_webfonts_preload'
76
  );
77
  }
88
  'optimize-webfonts',
89
  'hwlSettingsPage'
90
  );
91
+ add_action(
92
+ 'admin_init',
93
  'hwlRegisterSettings'
94
  );
95
  }
96
+
97
  add_action('admin_menu', 'hwlCreateMenu');
98
 
99
  /**
113
  */
114
  function hwlGetUploadUrl()
115
  {
116
+ if (OMGF_CDN_URL) {
117
+ $uploadUrl = '//' . OMGF_CDN_URL . '/' . hwlGetContentDirName() . OMGF_CACHE_DIR;
118
  } else {
119
+ $uploadUrl = get_site_url(OMGF_CURRENT_BLOG_ID, hwlGetContentDirName() . OMGF_CACHE_DIR);
120
  }
121
 
122
  return $uploadUrl;
128
  function hwlCreateWebfontsTable()
129
  {
130
  global $wpdb;
131
+ $sql = "CREATE TABLE IF NOT EXISTS " . OMGF_DB_TABLENAME . " (
132
  font_id varchar(191) NOT NULL,
133
  font_family varchar(191) NOT NULL,
134
  font_weight mediumint(5) NOT NULL,
139
  url_woff2 varchar(191) NULL,
140
  url_eot varchar(191) NULL,
141
  UNIQUE KEY (font_id)
142
+ ) " . OMGF_DB_CHARSET . ";";
143
  $wpdb->query($sql);
144
 
145
  add_option('caos_webfonts_db_version', '1.6.1');
151
  function hwlCreateSubsetsTable()
152
  {
153
  global $wpdb;
154
+ $sql = "CREATE TABLE IF NOT EXISTS " . OMGF_DB_TABLENAME . '_subsets' . " (
155
  subset_font varchar(32) NOT NULL,
156
  subset_family varchar(191) NOT NULL,
157
  available_subsets varchar(191) NOT NULL,
158
  selected_subsets varchar(191) NOT NULL,
159
  UNIQUE KEY (subset_font)
160
+ ) " . OMGF_DB_CHARSET . ";";
161
  $wpdb->query($sql);
162
 
163
  update_option('caos_webfonts_db_version', '1.7.0');
170
  {
171
  global $wpdb;
172
 
173
+ $sql = "ALTER TABLE " . OMGF_DB_TABLENAME . " " .
174
  "ADD COLUMN local varchar(128) AFTER font_style;";
175
  $wpdb->query($sql);
176
 
189
  if (version_compare($currentVersion, '1.7.0') < 0) {
190
  hwlCreateSubsetsTable();
191
  }
192
+ if (version_compare($currentVersion, OMGF_DB_VERSION) < 0) {
193
  hwlAddLocalColumn();
194
  }
195
  }
196
+
197
  add_action('plugins_loaded', 'hwlRunDbUpdates');
198
 
199
  /**
209
 
210
  return $links;
211
  }
212
+
213
  $caosLink = plugin_basename(__FILE__);
214
 
215
  add_filter("plugin_action_links_$caosLink", 'hwlSettingsLink');
227
  <h1><?php _e('OMGF | Optimize My Google Fonts', 'host-webfonts-local'); ?></h1>
228
  <p>
229
  <?php _e('Developed by: ', 'host-webfonts-local'); ?>
230
+ <a title="Buy me a beer!" href="<?php echo OMGF_SITE_URL; ?>/donate/">Daan van den Bergh</a>.
 
231
  </p>
232
 
233
  <div id="hwl-admin-notices"></div>
234
 
235
+ <?php require_once(dirname(__FILE__) . '/includes/templates/settings-welcome.phtml'); ?>
236
 
237
  <form id="hwl-options-form" class="settings-column left" name="hwl-options-form" method="post">
238
  <div class="">
239
+ <?php require_once(dirname(__FILE__) . '/includes/templates/settings-generate-stylesheet.phtml'); ?>
 
 
 
 
240
  </div>
241
  </form>
242
 
245
  settings_fields('caos-webfonts-basic-settings');
246
  do_settings_sections('caos-webfonts-basic-settings');
247
 
248
+ require_once(dirname(__FILE__) . '/includes/templates/settings-basic-settings.phtml');
249
 
250
  do_action('hwl_after_settings_form_settings');
251
 
264
  global $wpdb;
265
 
266
  try {
267
+ return $wpdb->get_results("SELECT * FROM " . OMGF_DB_TABLENAME);
268
  } catch (\Exception $e) {
269
  return $e;
270
  }
278
  global $wpdb;
279
 
280
  try {
281
+ return $wpdb->get_results("SELECT * FROM " . OMGF_DB_TABLENAME . " WHERE downloaded = 1");
282
  } catch (\Exception $e) {
283
  return $e;
284
  }
303
  global $wpdb;
304
 
305
  try {
306
+ return $wpdb->get_results("SELECT * FROM " . OMGF_DB_TABLENAME . "_subsets");
307
  } catch (\Exception $e) {
308
  return $e;
309
  }
314
  global $wpdb;
315
 
316
  try {
317
+ return $wpdb->get_results("SELECT * FROM " . OMGF_DB_TABLENAME . " WHERE font_family = '$family'");
318
  } catch (\Exception $e) {
319
  return $e;
320
  }
321
  }
322
 
323
  /**
324
+ * @return \Exception|void
325
  */
326
  function hwlCleanQueue()
327
  {
328
  global $wpdb;
329
 
330
  try {
331
+ $wpdb->query("TRUNCATE TABLE " . OMGF_DB_TABLENAME);
332
+ $wpdb->query("TRUNCATE TABLE " . OMGF_DB_TABLENAME . "_subsets");
333
  } catch (\Exception $e) {
334
  return $e;
335
  }
346
  )
347
  );
348
  }
349
+
350
  add_action('wp_ajax_hwlAjaxGetDownloadStatus', 'hwlAjaxGetDownloadStatus');
351
 
352
  /**
356
  {
357
  return wp_die(hwlCleanQueue());
358
  }
359
+
360
  add_action('wp_ajax_hwlAjaxCleanQueue', 'hwlAjaxCleanQueue');
361
 
362
  /**
366
  */
367
  function hwlAjaxEmptyDir()
368
  {
369
+ return array_map('unlink', array_filter((array) glob(OMGF_UPLOAD_DIR . '/*')));
370
  }
371
+
372
  add_action('wp_ajax_hwlAjaxEmptyDir', 'hwlAjaxEmptyDir');
373
 
374
  /**
381
 
382
  foreach ($searchQueries as $searchQuery) {
383
  $request = curl_init();
384
+ curl_setopt($request, CURLOPT_URL, OMGF_HELPER_URL . $searchQuery);
385
  curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
386
  $result = curl_exec($request);
387
  curl_close($request);
398
  wp_die($e);
399
  }
400
  }
401
+
402
  add_action('wp_ajax_hwlAjaxSearchFontSubsets', 'hwlAjaxSearchFontSubsets');
403
 
404
  /**
411
  $searchQuery = sanitize_text_field($_POST['search_query']);
412
  $subsets = implode($_POST['search_subsets'], ',');
413
 
414
+ curl_setopt($request, CURLOPT_URL, OMGF_HELPER_URL . $searchQuery . '?subsets=' . $subsets);
415
  curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
416
 
417
  $result = curl_exec($request);
422
  wp_die($e);
423
  }
424
  }
425
+
426
  add_action('wp_ajax_hwlAjaxSearchGoogleFonts', 'hwlAjaxSearchGoogleFonts');
427
 
428
  /**
430
  */
431
  function hwlCreateCacheDir()
432
  {
433
+ $uploadDir = OMGF_UPLOAD_DIR;
434
  if (!is_dir($uploadDir)) {
435
  wp_mkdir_p($uploadDir);
436
  }
437
  }
438
+
439
  register_activation_hook(__FILE__, 'hwlCreateCacheDir');
440
 
441
  /**
459
  */
460
  function hwlAjaxGenerateStyles()
461
  {
462
+ require_once(dirname(__FILE__) . '/includes/ajax/class-generate-styles.php');
463
  }
464
+
465
  add_action('wp_ajax_hwlAjaxGenerateStyles', 'hwlAjaxGenerateStyles');
466
 
467
  /**
469
  */
470
  function hwlAjaxDownloadFonts()
471
  {
472
+ require_once(dirname(__FILE__) . '/includes/ajax/class-download-fonts.php');
473
  }
474
+
475
  add_action('wp_ajax_hwlAjaxDownloadFonts', 'hwlAjaxDownloadFonts');
476
 
477
  /**
479
  */
480
  function hwlEnqueueStylesheet()
481
  {
482
+ if (OMGF_WEB_FONT_LOADER) {
483
+ require_once(dirname(__FILE__) . '/includes/templates/script-web-font-loader.phtml');
484
+ } else {
485
+ wp_enqueue_style('omgf-fonts', OMGF_UPLOAD_URL . '/' . OMGF_FILENAME, array(), (OMGF_REMOVE_VERSION ? null : OMGF_STATIC_VERSION));
486
+ }
487
  }
488
+
489
  add_action('wp_enqueue_scripts', 'hwlEnqueueStylesheet');
490
 
491
  /**
494
  function hwlEnqueueAdminJs($hook)
495
  {
496
  if ($hook == 'settings_page_optimize-webfonts') {
497
+ wp_enqueue_script('hwl-admin-js', plugin_dir_url(__FILE__) . 'js/hwl-admin.js', array('jquery'), OMGF_STATIC_VERSION, true);
498
+ wp_enqueue_style('hwl-admin-css', plugin_dir_url(__FILE__) . 'css/hwl-admin.css', array(), OMGF_STATIC_VERSION);
499
  }
500
  }
501
+
502
  add_action('admin_enqueue_scripts', 'hwlEnqueueAdminJs');
503
 
504
  /**
510
  wp_dequeue_style('hwl-admin-css');
511
  wp_dequeue_style('omgf-fonts');
512
  }
513
+
514
  register_deactivation_hook(__FILE__, 'hwlDequeueJsCss');
515
 
516
  /**
521
  global $wp_styles;
522
 
523
  $handle = 'omgf-fonts';
524
+ $style = $wp_styles->registered[$handle];
525
 
526
  /** Do not add 'preload' if Minification plugins are enabled. */
527
  if ($style) {
532
 
533
  function hwlIsPreloadEnabled()
534
  {
535
+ if (OMGF_PRELOAD == 'on') {
536
  add_action('wp_head', 'hwlAddLinkPreload', 1);
537
  }
538
  }
539
+
540
  add_action('init', 'hwlIsPreloadEnabled');
541
 
542
  /**
548
  {
549
  global $wp_styles;
550
 
551
+ $registered = $wp_styles->registered;
552
+ $fonts = array_filter(
553
+ $registered, function ($contents) {
554
  return strpos($contents->src, 'fonts.googleapis.com') !== false
555
  || strpos($contents->src, 'fonts.gstatic.com') !== false;
556
+ }
557
+ );
558
+ $dependencies = array_filter(
559
+ $registered, function ($contents) use ($fonts) {
560
  return !empty(array_intersect(array_keys($fonts), $contents->deps));
561
+ }
562
+ );
563
 
564
  foreach ($fonts as $font) {
565
  wp_deregister_style($font->handle);
574
 
575
  function hwlIsRemoveGoogleFontsEnabled()
576
  {
577
+ if (OMGF_REMOVE_GFONTS == 'on' && !is_admin()) {
578
  add_action('wp_print_styles', 'hwlRemoveGoogleFonts', PHP_INT_MAX);
579
+
580
+ // Enfold compatibility.
581
+ add_filter('avf_output_google_webfonts_script', function() {
582
+ return false;
583
+ });
584
  }
585
  }
586
+
587
  add_action('init', 'hwlIsRemoveGoogleFontsEnabled');
includes/ajax/class-download-fonts.php ADDED
@@ -0,0 +1,245 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @package : OMGF
4
+ * @author : Daan van den Bergh
5
+ * @copyright: (c) 2019 Daan van den Bergh
6
+ * @url : https://daan.dev
7
+ */
8
+
9
+ // Exit if accessed directly
10
+ if (!defined('ABSPATH')) {
11
+ exit;
12
+ }
13
+
14
+ require_once(dirname(dirname(__FILE__)) . '/class-ajax.php');
15
+
16
+ class OMGF_AJAX_Download_Fonts extends OMGF_AJAX
17
+ {
18
+ /** @var $fonts */
19
+ private $fonts;
20
+
21
+ /** @var $subsets */
22
+ private $subsets;
23
+
24
+ /**
25
+ * OMGF_Download_Fonts constructor.
26
+ */
27
+ public function __construct()
28
+ {
29
+ $this->init();
30
+ }
31
+
32
+ /**
33
+ * Initialize the download process.
34
+ */
35
+ private function init()
36
+ {
37
+ $this->create_dir_recursive();
38
+
39
+ /**
40
+ * To match the current queue of fonts. We need to truncate the table first.
41
+ */
42
+ try {
43
+ hwlCleanQueue();
44
+ } catch (\Exception $e) {
45
+ $this->throw_error($e->getCode(), $e->getMessage());
46
+ }
47
+
48
+ /**
49
+ * Get the POST data.
50
+ */
51
+ $this->fonts = $_POST['fonts'][0]['caos_webfonts_array'];
52
+ $this->subsets = $_POST['subsets'];
53
+
54
+ if (!$this->fonts || !$this->subsets) {
55
+ $this->throw_error('400', 'No fonts or subsets selected.');
56
+ }
57
+
58
+ $this->save_subsets_to_db();
59
+ $this->save_fonts_to_db();
60
+
61
+ $this->download_fonts();
62
+ }
63
+
64
+ /**
65
+ * If cache directory doesn't exist, we should create it.
66
+ */
67
+ private function create_dir_recursive()
68
+ {
69
+ $uploadDir = OMGF_UPLOAD_DIR;
70
+ if (!file_exists($uploadDir)) {
71
+ wp_mkdir_p($uploadDir);
72
+ }
73
+ }
74
+
75
+ /**
76
+ * Save used subsets to database for each font.
77
+ */
78
+ private function save_subsets_to_db()
79
+ {
80
+ global $wpdb;
81
+
82
+ foreach ($this->subsets as $id => $subset) {
83
+ $availableSubsets = implode($subset['available'], ',');
84
+ $selectedSubsets = implode($subset['selected'], ',');
85
+
86
+ $wpdb->insert(
87
+ OMGF_DB_TABLENAME . '_subsets',
88
+ array(
89
+ 'subset_font' => $id,
90
+ 'subset_family' => $subset['family'],
91
+ 'available_subsets' => $availableSubsets,
92
+ 'selected_subsets' => $selectedSubsets,
93
+ )
94
+ );
95
+ }
96
+ }
97
+
98
+ /**
99
+ * Save used fonts to database.
100
+ */
101
+ private function save_fonts_to_db()
102
+ {
103
+ global $wpdb;
104
+
105
+ foreach ($this->fonts as $id => $font) {
106
+ $wpdb->insert(
107
+ OMGF_DB_TABLENAME,
108
+ array(
109
+ 'font_id' => sanitize_text_field($id),
110
+ 'font_family' => sanitize_text_field($font['font-family']),
111
+ 'font_weight' => sanitize_text_field($font['font-weight']),
112
+ 'font_style' => sanitize_text_field($font['font-style']),
113
+ 'local' => sanitize_text_field($font['local']),
114
+ 'downloaded' => 0,
115
+ 'url_ttf' => esc_url_raw($font['url']['ttf']),
116
+ 'url_woff' => esc_url_raw($font['url']['woff']),
117
+ 'url_woff2' => esc_url_raw($font['url']['woff2']),
118
+ 'url_eot' => esc_url_raw($font['url']['eot'])
119
+ )
120
+ );
121
+ }
122
+ }
123
+
124
+ /**
125
+ *
126
+ */
127
+ private function download_fonts()
128
+ {
129
+ global $wpdb;
130
+
131
+ $selectedFonts = hwlGetTotalFonts();
132
+
133
+ foreach ($selectedFonts as $id => $font) {
134
+ // If font is marked as downloaded. Skip it.
135
+ if ($font->downloaded) {
136
+ continue;
137
+ }
138
+
139
+ $urls['url_ttf'] = $font->url_ttf;
140
+ $urls['url_woff'] = $font->url_woff;
141
+ $urls['url_woff2'] = $font->url_woff2;
142
+ $urls['url_eot'] = $font->url_eot;
143
+
144
+ foreach ($urls as $type => $url) {
145
+ $remoteFile = esc_url_raw($url);
146
+
147
+ /**
148
+ * We've already downloaded this one before.
149
+ */
150
+ if (strpos($remoteFile, get_site_url()) !== false) {
151
+ continue;
152
+ }
153
+
154
+ $filename = basename($remoteFile);
155
+ $localFile = OMGF_UPLOAD_DIR . '/' . $filename;
156
+
157
+ try {
158
+ $this->download_file_curl($localFile, $remoteFile);
159
+ } catch (Exception $e) {
160
+ $this->throw_error($e->getCode(), "File ($remoteFile) could not be downloaded: " . $e->getMessage());
161
+ }
162
+
163
+ clearstatcache();
164
+
165
+ if (file_exists($localFile) && !filesize($localFile) > 0) {
166
+ $this->throw_error('400', "File ($localFile) exists, but is 0 bytes in size. Is <code>allow_url_fopen</code> enabled on your server?");
167
+ }
168
+
169
+ /**
170
+ * If file is written, change the external URL to the local URL in the POST data.
171
+ * If it fails, we can still fall back to the external URL and nothing breaks.
172
+ */
173
+ $localFileUrl = OMGF_UPLOAD_URL . '/' . $filename;
174
+ $wpdb->update(
175
+ OMGF_DB_TABLENAME,
176
+ array(
177
+ $type => $localFileUrl
178
+ ),
179
+ array(
180
+ 'font_id' => $font->font_id
181
+ )
182
+ );
183
+ }
184
+
185
+ /**
186
+ * After all files are downloaded, set the 'downloaded'-field to 1.
187
+ */
188
+ $wpdb->update(
189
+ OMGF_DB_TABLENAME,
190
+ array(
191
+ 'downloaded' => 1
192
+ ),
193
+ array(
194
+ 'font_id' => $font->font_id
195
+ )
196
+ );
197
+ }
198
+ }
199
+
200
+ /**
201
+ * Download $remoteFile using cUrl and write to $localFile
202
+ *
203
+ * @param $localFile
204
+ * @param $remoteFile
205
+ */
206
+ private function download_file_curl($localFile, $remoteFile)
207
+ {
208
+ $file = fopen($localFile, 'w+');
209
+ $curl = curl_init();
210
+
211
+ curl_setopt_array(
212
+ $curl,
213
+ array(
214
+ CURLOPT_RETURNTRANSFER => true,
215
+ CURLOPT_URL => $remoteFile,
216
+ CURLOPT_FILE => $file,
217
+ CURLOPT_HEADER => false,
218
+ CURLOPT_FOLLOWLOCATION => true
219
+ )
220
+ );
221
+
222
+ curl_exec($curl);
223
+ curl_close($curl);
224
+ fclose($file);
225
+
226
+ if (file_exists($localFile)) {
227
+ return;
228
+ }
229
+
230
+ $this->download_file_fallback($localFile, $remoteFile);
231
+ }
232
+
233
+ /**
234
+ * Fallback download method if cUrl failed.
235
+ *
236
+ * @param $localFile
237
+ * @param $remoteFile
238
+ */
239
+ private function download_file_fallback($localFile, $remoteFile)
240
+ {
241
+ file_put_contents($localFile, file_get_contents($remoteFile));
242
+ }
243
+ }
244
+
245
+ new OMGF_AJAX_Download_Fonts();
includes/ajax/class-generate-styles.php ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @package : OMGF
4
+ * @author : Daan van den Bergh
5
+ * @copyright: (c) 2019 Daan van den Bergh
6
+ * @url : https://daan.dev
7
+ */
8
+
9
+ // Exit if accessed directly
10
+ if (!defined('ABSPATH')) {
11
+ exit;
12
+ }
13
+
14
+ require_once(dirname(dirname(__FILE__)) . '/class-ajax.php');
15
+
16
+ class OMGF_AJAX_Generate_Styles extends OMGF_AJAX
17
+ {
18
+ /** @var array $fonts */
19
+ private $fonts = [];
20
+
21
+ /**
22
+ * OMGF_AJAX_Generate_Styles constructor.
23
+ */
24
+ public function __construct()
25
+ {
26
+ $this->init();
27
+ }
28
+
29
+ /**
30
+ * Generate the Stylesheet
31
+ */
32
+ private function init()
33
+ {
34
+ header("Content-type: text/css");
35
+
36
+ $this->insert_promo();
37
+
38
+ $selectedFonts = hwlGetTotalFonts();
39
+
40
+ $this->process_fonts($selectedFonts);
41
+
42
+ $fonts = implode("\n", $this->fonts);
43
+ $file = OMGF_UPLOAD_DIR . '/' . OMGF_FILENAME;
44
+
45
+ /**
46
+ * If the file can be created and uploaded. Let's try to write it.
47
+ */
48
+ try {
49
+ $stylesheet = fopen($file, 'w') or $this->throw_error(400, "Cannot create file {$file}");
50
+ fwrite($stylesheet, $fonts);
51
+ fclose($stylesheet);
52
+ wp_die(__('Stylesheet was successfully generated and added to your theme\'s header.'));
53
+ } catch (Exception $e) {
54
+ $this->throw_error($e->getCode(), __("Stylesheet could not be generated: $e"));
55
+ }
56
+ }
57
+
58
+ /**
59
+ * Insert promo material :)
60
+ *
61
+ * The alignment is crooked, so it'll look nice in the stylesheet.
62
+ */
63
+ private function insert_promo()
64
+ {
65
+ $this->fonts[] = "/**
66
+ * This file is automagically generated by OMGF
67
+ *
68
+ * @author: Daan van den Bergh
69
+ * @copyright: (c) 2019 Daan van den Bergh
70
+ * @url: " . OMGF_SITE_URL . "
71
+ */";
72
+ }
73
+
74
+ /**
75
+ * Prepare fonts for generation.
76
+ */
77
+ private function process_fonts($fonts)
78
+ {
79
+ $fontDisplay = OMGF_DISPLAY_OPTION;
80
+
81
+ foreach ($fonts as $font) {
82
+ $fontFamily = sanitize_text_field($font->font_family);
83
+ $fontStyle = sanitize_text_field($font->font_style);
84
+ $fontWeight = sanitize_text_field($font->font_weight);
85
+ $fontUrlEot = esc_url_raw($font->url_eot);
86
+ $fontUrlWoffTwo = esc_url_raw($font->url_woff2);
87
+ $fontUrlWoff = esc_url_raw($font->url_woff);
88
+ $fontUrlTtf = esc_url_raw($font->url_ttf);
89
+ $locals = explode(',', sanitize_text_field($font->local));
90
+ $fontLocal = isset($locals[0]) ? $locals[0] : $fontFamily . " " . ucfirst($fontStyle);
91
+ $fontLocalDash = isset($locals[1]) ? $locals[1] : $fontFamily . "-" . ucfirst($fontStyle);
92
+
93
+ /**
94
+ * The alignment is crooked, so it'll look nice in the stylesheet.
95
+ */
96
+ $this->fonts[] = "@font-face {
97
+ font-family: '$fontFamily';
98
+ font-display: $fontDisplay;
99
+ font-style: $fontStyle;
100
+ font-weight: $fontWeight;
101
+ src: url('$fontUrlEot'); /* IE9 Compatible */
102
+ src: local('$fontLocal'), local('$fontLocalDash'),
103
+ url('$fontUrlWoffTwo') format('woff2'), /* Super Modern Browsers */
104
+ url('$fontUrlWoff') format('woff'), /* Modern Browsers */
105
+ url('$fontUrlTtf') format('truetype'); /* Safari, Android, iOS */
106
+ }";
107
+ }
108
+ }
109
+ }
110
+
111
+ new OMGF_AJAX_Generate_Styles();
includes/ajax/download-fonts.php DELETED
@@ -1,154 +0,0 @@
1
- <?php
2
- /**
3
- * @package: OMGF
4
- * @author: Daan van den Bergh
5
- * @copyright: (c) 2019 Daan van den Bergh
6
- * @url: https://daan.dev
7
- */
8
-
9
- // Exit if accessed directly
10
- if (!defined( 'ABSPATH')) exit;
11
-
12
- /**
13
- * Check if user has the needed permissions.
14
- */
15
- if (!current_user_can('manage_options'))
16
- {
17
- wp_die(__("You're not cool enough to access this page."));
18
- }
19
-
20
- /**
21
- * If cache directory doesn't exist, we should create it.
22
- */
23
- $uploadDir = CAOS_WEBFONTS_UPLOAD_DIR;
24
- if (!file_exists($uploadDir)) {
25
- wp_mkdir_p($uploadDir);
26
- }
27
-
28
- global $wpdb;
29
-
30
- /**
31
- * To match the current queue of fonts. We need to truncate the table first.
32
- */
33
- try {
34
- hwlCleanQueue();
35
- } catch (\Exception $e) {
36
- wp_die(__($e));
37
- }
38
-
39
- /**
40
- * Get the POST data.
41
- */
42
- $selectedFonts = $_POST['fonts'][0]['caos_webfonts_array'];
43
- $subsets = $_POST['subsets'];
44
-
45
- if (!$selectedFonts || !$subsets)
46
- {
47
- wp_die(__('No fonts or subsets selected.', CAOS_ANALYTICS_TRANSLATE_DOMAIN));
48
- }
49
-
50
- /**
51
- * Save used subsets to database for each font.
52
- */
53
- foreach ($subsets as $id => $subset)
54
- {
55
- $availableSubsets = implode($subset['available'], ',');
56
- $selectedSubsets = implode($subset['selected'], ',');
57
-
58
- $wpdb->insert(
59
- CAOS_WEBFONTS_DB_TABLENAME . '_subsets',
60
- array(
61
- 'subset_font' => $id,
62
- 'subset_family' => $subset['family'],
63
- 'available_subsets' => $availableSubsets,
64
- 'selected_subsets' => $selectedSubsets,
65
- )
66
- );
67
- }
68
-
69
-
70
- /**
71
- * Save used fonts to database.
72
- */
73
- foreach ($selectedFonts as $id => $font)
74
- {
75
- $wpdb->insert(
76
- CAOS_WEBFONTS_DB_TABLENAME,
77
- array(
78
- 'font_id' => sanitize_text_field($id),
79
- 'font_family' => sanitize_text_field($font['font-family']),
80
- 'font_weight' => sanitize_text_field($font['font-weight']),
81
- 'font_style' => sanitize_text_field($font['font-style']),
82
- 'local' => sanitize_text_field($font['local']),
83
- 'downloaded' => 0,
84
- 'url_ttf' => esc_url_raw($font['url']['ttf']),
85
- 'url_woff' => esc_url_raw($font['url']['woff']),
86
- 'url_woff2' => esc_url_raw($font['url']['woff2']),
87
- 'url_eot' => esc_url_raw($font['url']['eot'])
88
- )
89
- );
90
- }
91
-
92
- /**
93
- * Loaded fonts from database
94
- */
95
- $selectedFonts = hwlGetTotalFonts();
96
-
97
- /**
98
- * Download the fonts.
99
- */
100
- foreach ($selectedFonts as $id => $font) {
101
- // If font is marked as downloaded. Skip it.
102
- if ($font->downloaded) {
103
- continue;
104
- }
105
-
106
- $urls['url_ttf'] = $font->url_ttf;
107
- $urls['url_woff'] = $font->url_woff;
108
- $urls['url_woff2'] = $font->url_woff2;
109
- $urls['url_eot'] = $font->url_eot;
110
-
111
- foreach ($urls as $type => $url) {
112
- $remoteFile = esc_url_raw($url);
113
- $filename = basename($remoteFile);
114
- $localFile = CAOS_WEBFONTS_UPLOAD_DIR . '/' . $filename;
115
-
116
- try {
117
- $fileWritten = file_put_contents($localFile, file_get_contents($remoteFile));
118
- } catch (\Exception $e) {
119
- wp_die(__("File ($remoteFile) could not be downloaded: $e"));
120
- }
121
-
122
- /**
123
- * If file is written, change the external URL to the local URL in the POST data.
124
- * If it fails, we can still fall back to the external URL and nothing breaks.
125
- */
126
- if($fileWritten) {
127
- $localFileUrl = CAOS_WEBFONTS_UPLOAD_URL . '/' . $filename;
128
- $wpdb->update(
129
- CAOS_WEBFONTS_DB_TABLENAME,
130
- array(
131
- $type => $localFileUrl
132
- ),
133
- array(
134
- 'font_id' => $font->font_id
135
- )
136
- );
137
- }
138
- }
139
-
140
- /**
141
- * After all files are downloaded, set the 'downloaded'-field to 1.
142
- */
143
- $wpdb->update(
144
- CAOS_WEBFONTS_DB_TABLENAME,
145
- array(
146
- 'downloaded' => 1
147
- ),
148
- array(
149
- 'font_id' => $font->font_id
150
- )
151
- );
152
- }
153
-
154
- wp_die(__('Fonts saved. You can now generate the stylesheet.', 'host-webfonts-local'));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
includes/ajax/generate-stylesheet.php DELETED
@@ -1,84 +0,0 @@
1
- <?php
2
- /**
3
- * @package: OMGF
4
- * @author: Daan van den Bergh
5
- * @copyright: (c) 2019 Daan van den Bergh
6
- * @url: https://daan.dev
7
- */
8
-
9
- // Exit if accessed directly
10
- if (!defined( 'ABSPATH')) exit;
11
-
12
- /**
13
- * set the content type header
14
- */
15
- header("Content-type: text/css");
16
-
17
- /**
18
- * Check if user has the needed permissions.
19
- */
20
- if (!current_user_can('manage_options'))
21
- {
22
- wp_die(__("You're not cool enough to access this page."));
23
- }
24
-
25
- /**
26
- * Insert promotional material :)
27
- */
28
- $fonts[] = "
29
- /** This file is automagically generated by OMGF
30
- *
31
- * @author: Daan van den Bergh
32
- * @copyright: (c) 2019 Daan van den Bergh
33
- * @url: " . CAOS_WEBFONTS_SITE_URL . "
34
- */";
35
- $fontDisplay = CAOS_WEBFONTS_DISPLAY_OPTION;
36
-
37
- /**
38
- * Reload the fonts.
39
- */
40
- $selectedFonts = hwlGetTotalFonts();
41
-
42
- /**
43
- * Let's generate the stylesheet.
44
- */
45
- foreach ($selectedFonts as $font) {
46
- $fontFamily = sanitize_text_field($font->font_family);
47
- $fontStyle = sanitize_text_field($font->font_style);
48
- $fontWeight = sanitize_text_field($font->font_weight);
49
- $fontUrlEot = esc_url_raw($font->url_eot);
50
- $fontUrlWoffTwo = esc_url_raw($font->url_woff2);
51
- $fontUrlWoff = esc_url_raw($font->url_woff);
52
- $fontUrlTtf = esc_url_raw($font->url_ttf);
53
- $locals = explode(',', sanitize_text_field($font->local));
54
- $fontLocal = isset($locals[0]) ? $locals[0] : $fontFamily . " " . ucfirst($fontStyle);
55
- $fontLocalDash = isset($locals[1]) ? $locals[1] : $fontFamily . "-" . ucfirst($fontStyle);
56
-
57
- $fonts[] =
58
- "@font-face {
59
- font-family: '$fontFamily';
60
- font-display: $fontDisplay;
61
- font-style: $fontStyle;
62
- font-weight: $fontWeight;
63
- src: url('$fontUrlEot'); /* IE9 Compatible */
64
- src: local('$fontLocal'), local('$fontLocalDash'),
65
- url('$fontUrlWoffTwo') format('woff2'), /* Super Modern Browsers */
66
- url('$fontUrlWoff') format('woff'), /* Modern Browsers */
67
- url('$fontUrlTtf') format('truetype'); /* Safari, Android, iOS */
68
- }";
69
- }
70
-
71
- $fonts = implode("\n", $fonts);
72
- $file = CAOS_WEBFONTS_UPLOAD_DIR . '/' . CAOS_WEBFONTS_FILENAME;
73
-
74
- /**
75
- * If the file can be created and uploaded. Let's try to write it.
76
- */
77
- try {
78
- $stylesheet = fopen($file, 'w') or die ("Cannot create file {$file}");
79
- fwrite ($stylesheet, $fonts);
80
- fclose ($stylesheet);
81
- wp_die(__('Stylesheet was successfully generated and added to your theme\'s header.'));
82
- } catch (Exception $e) {
83
- wp_die(__("Stylesheet could not be generated: $e"));
84
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
includes/class-ajax.php ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @package : OMGF
4
+ * @author : Daan van den Bergh
5
+ * @copyright: (c) 2019 Daan van den Bergh
6
+ * @url : https://daan.dev
7
+ */
8
+
9
+ class OMGF_AJAX
10
+ {
11
+ /**
12
+ * @param $code
13
+ * @param $message
14
+ */
15
+ protected function throw_error($code, $message)
16
+ {
17
+ wp_send_json_error(__($message, 'host-webfonts-local'), (int) $code);
18
+ }
19
+ }
20
+
21
+ new OMGF_AJAX();
includes/libraries/webfont.js ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * Copyright 2016 Small Batch, Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5
+ * use this file except in compliance with the License. You may obtain a copy of
6
+ * the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ * License for the specific language governing permissions and limitations under
14
+ * the License.
15
+ */
16
+ /* Web Font Loader v1.6.26 - (c) Adobe Systems, Google. License: Apache 2.0 */(function(){function aa(a,b,c){return a.call.apply(a.bind,arguments)}function ba(a,b,c){if(!a)throw Error();if(2<arguments.length){var d=Array.prototype.slice.call(arguments,2);return function(){var c=Array.prototype.slice.call(arguments);Array.prototype.unshift.apply(c,d);return a.apply(b,c)}}return function(){return a.apply(b,arguments)}}function p(a,b,c){p=Function.prototype.bind&&-1!=Function.prototype.bind.toString().indexOf("native code")?aa:ba;return p.apply(null,arguments)}var q=Date.now||function(){return+new Date};function ca(a,b){this.a=a;this.m=b||a;this.c=this.m.document}var da=!!window.FontFace;function t(a,b,c,d){b=a.c.createElement(b);if(c)for(var e in c)c.hasOwnProperty(e)&&("style"==e?b.style.cssText=c[e]:b.setAttribute(e,c[e]));d&&b.appendChild(a.c.createTextNode(d));return b}function u(a,b,c){a=a.c.getElementsByTagName(b)[0];a||(a=document.documentElement);a.insertBefore(c,a.lastChild)}function v(a){a.parentNode&&a.parentNode.removeChild(a)}
17
+ function w(a,b,c){b=b||[];c=c||[];for(var d=a.className.split(/\s+/),e=0;e<b.length;e+=1){for(var f=!1,g=0;g<d.length;g+=1)if(b[e]===d[g]){f=!0;break}f||d.push(b[e])}b=[];for(e=0;e<d.length;e+=1){f=!1;for(g=0;g<c.length;g+=1)if(d[e]===c[g]){f=!0;break}f||b.push(d[e])}a.className=b.join(" ").replace(/\s+/g," ").replace(/^\s+|\s+$/,"")}function y(a,b){for(var c=a.className.split(/\s+/),d=0,e=c.length;d<e;d++)if(c[d]==b)return!0;return!1}
18
+ function z(a){if("string"===typeof a.f)return a.f;var b=a.m.location.protocol;"about:"==b&&(b=a.a.location.protocol);return"https:"==b?"https:":"http:"}function ea(a){return a.m.location.hostname||a.a.location.hostname}
19
+ function A(a,b,c){function d(){k&&e&&f&&(k(g),k=null)}b=t(a,"link",{rel:"stylesheet",href:b,media:"all"});var e=!1,f=!0,g=null,k=c||null;da?(b.onload=function(){e=!0;d()},b.onerror=function(){e=!0;g=Error("Stylesheet failed to load");d()}):setTimeout(function(){e=!0;d()},0);u(a,"head",b)}
20
+ function B(a,b,c,d){var e=a.c.getElementsByTagName("head")[0];if(e){var f=t(a,"script",{src:b}),g=!1;f.onload=f.onreadystatechange=function(){g||this.readyState&&"loaded"!=this.readyState&&"complete"!=this.readyState||(g=!0,c&&c(null),f.onload=f.onreadystatechange=null,"HEAD"==f.parentNode.tagName&&e.removeChild(f))};e.appendChild(f);setTimeout(function(){g||(g=!0,c&&c(Error("Script load timeout")))},d||5E3);return f}return null};function C(){this.a=0;this.c=null}function D(a){a.a++;return function(){a.a--;E(a)}}function F(a,b){a.c=b;E(a)}function E(a){0==a.a&&a.c&&(a.c(),a.c=null)};function G(a){this.a=a||"-"}G.prototype.c=function(a){for(var b=[],c=0;c<arguments.length;c++)b.push(arguments[c].replace(/[\W_]+/g,"").toLowerCase());return b.join(this.a)};function H(a,b){this.c=a;this.f=4;this.a="n";var c=(b||"n4").match(/^([nio])([1-9])$/i);c&&(this.a=c[1],this.f=parseInt(c[2],10))}function fa(a){return I(a)+" "+(a.f+"00")+" 300px "+J(a.c)}function J(a){var b=[];a=a.split(/,\s*/);for(var c=0;c<a.length;c++){var d=a[c].replace(/['"]/g,"");-1!=d.indexOf(" ")||/^\d/.test(d)?b.push("'"+d+"'"):b.push(d)}return b.join(",")}function K(a){return a.a+a.f}function I(a){var b="normal";"o"===a.a?b="oblique":"i"===a.a&&(b="italic");return b}
21
+ function ga(a){var b=4,c="n",d=null;a&&((d=a.match(/(normal|oblique|italic)/i))&&d[1]&&(c=d[1].substr(0,1).toLowerCase()),(d=a.match(/([1-9]00|normal|bold)/i))&&d[1]&&(/bold/i.test(d[1])?b=7:/[1-9]00/.test(d[1])&&(b=parseInt(d[1].substr(0,1),10))));return c+b};function ha(a,b){this.c=a;this.f=a.m.document.documentElement;this.h=b;this.a=new G("-");this.j=!1!==b.events;this.g=!1!==b.classes}function ia(a){a.g&&w(a.f,[a.a.c("wf","loading")]);L(a,"loading")}function M(a){if(a.g){var b=y(a.f,a.a.c("wf","active")),c=[],d=[a.a.c("wf","loading")];b||c.push(a.a.c("wf","inactive"));w(a.f,c,d)}L(a,"inactive")}function L(a,b,c){if(a.j&&a.h[b])if(c)a.h[b](c.c,K(c));else a.h[b]()};function ja(){this.c={}}function ka(a,b,c){var d=[],e;for(e in b)if(b.hasOwnProperty(e)){var f=a.c[e];f&&d.push(f(b[e],c))}return d};function N(a,b){this.c=a;this.f=b;this.a=t(this.c,"span",{"aria-hidden":"true"},this.f)}function O(a){u(a.c,"body",a.a)}function P(a){return"display:block;position:absolute;top:-9999px;left:-9999px;font-size:300px;width:auto;height:auto;line-height:normal;margin:0;padding:0;font-variant:normal;white-space:nowrap;font-family:"+J(a.c)+";"+("font-style:"+I(a)+";font-weight:"+(a.f+"00")+";")};function Q(a,b,c,d,e,f){this.g=a;this.j=b;this.a=d;this.c=c;this.f=e||3E3;this.h=f||void 0}Q.prototype.start=function(){var a=this.c.m.document,b=this,c=q(),d=new Promise(function(d,e){function k(){q()-c>=b.f?e():a.fonts.load(fa(b.a),b.h).then(function(a){1<=a.length?d():setTimeout(k,25)},function(){e()})}k()}),e=new Promise(function(a,d){setTimeout(d,b.f)});Promise.race([e,d]).then(function(){b.g(b.a)},function(){b.j(b.a)})};function R(a,b,c,d,e,f,g){this.v=a;this.B=b;this.c=c;this.a=d;this.s=g||"BESbswy";this.f={};this.w=e||3E3;this.u=f||null;this.o=this.j=this.h=this.g=null;this.g=new N(this.c,this.s);this.h=new N(this.c,this.s);this.j=new N(this.c,this.s);this.o=new N(this.c,this.s);a=new H(this.a.c+",serif",K(this.a));a=P(a);this.g.a.style.cssText=a;a=new H(this.a.c+",sans-serif",K(this.a));a=P(a);this.h.a.style.cssText=a;a=new H("serif",K(this.a));a=P(a);this.j.a.style.cssText=a;a=new H("sans-serif",K(this.a));a=
22
+ P(a);this.o.a.style.cssText=a;O(this.g);O(this.h);O(this.j);O(this.o)}var S={D:"serif",C:"sans-serif"},T=null;function U(){if(null===T){var a=/AppleWebKit\/([0-9]+)(?:\.([0-9]+))/.exec(window.navigator.userAgent);T=!!a&&(536>parseInt(a[1],10)||536===parseInt(a[1],10)&&11>=parseInt(a[2],10))}return T}R.prototype.start=function(){this.f.serif=this.j.a.offsetWidth;this.f["sans-serif"]=this.o.a.offsetWidth;this.A=q();la(this)};
23
+ function ma(a,b,c){for(var d in S)if(S.hasOwnProperty(d)&&b===a.f[S[d]]&&c===a.f[S[d]])return!0;return!1}function la(a){var b=a.g.a.offsetWidth,c=a.h.a.offsetWidth,d;(d=b===a.f.serif&&c===a.f["sans-serif"])||(d=U()&&ma(a,b,c));d?q()-a.A>=a.w?U()&&ma(a,b,c)&&(null===a.u||a.u.hasOwnProperty(a.a.c))?V(a,a.v):V(a,a.B):na(a):V(a,a.v)}function na(a){setTimeout(p(function(){la(this)},a),50)}function V(a,b){setTimeout(p(function(){v(this.g.a);v(this.h.a);v(this.j.a);v(this.o.a);b(this.a)},a),0)};function W(a,b,c){this.c=a;this.a=b;this.f=0;this.o=this.j=!1;this.s=c}var X=null;W.prototype.g=function(a){var b=this.a;b.g&&w(b.f,[b.a.c("wf",a.c,K(a).toString(),"active")],[b.a.c("wf",a.c,K(a).toString(),"loading"),b.a.c("wf",a.c,K(a).toString(),"inactive")]);L(b,"fontactive",a);this.o=!0;oa(this)};
24
+ W.prototype.h=function(a){var b=this.a;if(b.g){var c=y(b.f,b.a.c("wf",a.c,K(a).toString(),"active")),d=[],e=[b.a.c("wf",a.c,K(a).toString(),"loading")];c||d.push(b.a.c("wf",a.c,K(a).toString(),"inactive"));w(b.f,d,e)}L(b,"fontinactive",a);oa(this)};function oa(a){0==--a.f&&a.j&&(a.o?(a=a.a,a.g&&w(a.f,[a.a.c("wf","active")],[a.a.c("wf","loading"),a.a.c("wf","inactive")]),L(a,"active")):M(a.a))};function pa(a){this.j=a;this.a=new ja;this.h=0;this.f=this.g=!0}pa.prototype.load=function(a){this.c=new ca(this.j,a.context||this.j);this.g=!1!==a.events;this.f=!1!==a.classes;qa(this,new ha(this.c,a),a)};
25
+ function ra(a,b,c,d,e){var f=0==--a.h;(a.f||a.g)&&setTimeout(function(){var a=e||null,k=d||null||{};if(0===c.length&&f)M(b.a);else{b.f+=c.length;f&&(b.j=f);var h,m=[];for(h=0;h<c.length;h++){var l=c[h],n=k[l.c],r=b.a,x=l;r.g&&w(r.f,[r.a.c("wf",x.c,K(x).toString(),"loading")]);L(r,"fontloading",x);r=null;null===X&&(X=window.FontFace?(x=/Gecko.*Firefox\/(\d+)/.exec(window.navigator.userAgent))?42<parseInt(x[1],10):!0:!1);X?r=new Q(p(b.g,b),p(b.h,b),b.c,l,b.s,n):r=new R(p(b.g,b),p(b.h,b),b.c,l,b.s,a,
26
+ n);m.push(r)}for(h=0;h<m.length;h++)m[h].start()}},0)}function qa(a,b,c){var d=[],e=c.timeout;ia(b);var d=ka(a.a,c,a.c),f=new W(a.c,b,e);a.h=d.length;b=0;for(c=d.length;b<c;b++)d[b].load(function(b,d,c){ra(a,f,b,d,c)})};function sa(a,b){this.c=a;this.a=b}function ta(a,b,c){var d=z(a.c);a=(a.a.api||"fast.fonts.net/jsapi").replace(/^.*http(s?):(\/\/)?/,"");return d+"//"+a+"/"+b+".js"+(c?"?v="+c:"")}
27
+ sa.prototype.load=function(a){function b(){if(f["__mti_fntLst"+d]){var c=f["__mti_fntLst"+d](),e=[],h;if(c)for(var m=0;m<c.length;m++){var l=c[m].fontfamily;void 0!=c[m].fontStyle&&void 0!=c[m].fontWeight?(h=c[m].fontStyle+c[m].fontWeight,e.push(new H(l,h))):e.push(new H(l))}a(e)}else setTimeout(function(){b()},50)}var c=this,d=c.a.projectId,e=c.a.version;if(d){var f=c.c.m;B(this.c,ta(c,d,e),function(e){e?a([]):(f["__MonotypeConfiguration__"+d]=function(){return c.a},b())}).id="__MonotypeAPIScript__"+
28
+ d}else a([])};function ua(a,b){this.c=a;this.a=b}ua.prototype.load=function(a){var b,c,d=this.a.urls||[],e=this.a.families||[],f=this.a.testStrings||{},g=new C;b=0;for(c=d.length;b<c;b++)A(this.c,d[b],D(g));var k=[];b=0;for(c=e.length;b<c;b++)if(d=e[b].split(":"),d[1])for(var h=d[1].split(","),m=0;m<h.length;m+=1)k.push(new H(d[0],h[m]));else k.push(new H(d[0]));F(g,function(){a(k,f)})};function va(a,b,c){a?this.c=a:this.c=b+wa;this.a=[];this.f=[];this.g=c||""}var wa="//fonts.googleapis.com/css";function xa(a,b){for(var c=b.length,d=0;d<c;d++){var e=b[d].split(":");3==e.length&&a.f.push(e.pop());var f="";2==e.length&&""!=e[1]&&(f=":");a.a.push(e.join(f))}}
29
+ function ya(a){if(0==a.a.length)throw Error("No fonts to load!");if(-1!=a.c.indexOf("kit="))return a.c;for(var b=a.a.length,c=[],d=0;d<b;d++)c.push(a.a[d].replace(/ /g,"+"));b=a.c+"?family="+c.join("%7C");0<a.f.length&&(b+="&subset="+a.f.join(","));0<a.g.length&&(b+="&text="+encodeURIComponent(a.g));return b};function za(a){this.f=a;this.a=[];this.c={}}
30
+ var Aa={latin:"BESbswy","latin-ext":"\u00e7\u00f6\u00fc\u011f\u015f",cyrillic:"\u0439\u044f\u0416",greek:"\u03b1\u03b2\u03a3",khmer:"\u1780\u1781\u1782",Hanuman:"\u1780\u1781\u1782"},Ba={thin:"1",extralight:"2","extra-light":"2",ultralight:"2","ultra-light":"2",light:"3",regular:"4",book:"4",medium:"5","semi-bold":"6",semibold:"6","demi-bold":"6",demibold:"6",bold:"7","extra-bold":"8",extrabold:"8","ultra-bold":"8",ultrabold:"8",black:"9",heavy:"9",l:"3",r:"4",b:"7"},Ca={i:"i",italic:"i",n:"n",normal:"n"},
31
+ Da=/^(thin|(?:(?:extra|ultra)-?)?light|regular|book|medium|(?:(?:semi|demi|extra|ultra)-?)?bold|black|heavy|l|r|b|[1-9]00)?(n|i|normal|italic)?$/;
32
+ function Ea(a){for(var b=a.f.length,c=0;c<b;c++){var d=a.f[c].split(":"),e=d[0].replace(/\+/g," "),f=["n4"];if(2<=d.length){var g;var k=d[1];g=[];if(k)for(var k=k.split(","),h=k.length,m=0;m<h;m++){var l;l=k[m];if(l.match(/^[\w-]+$/)){var n=Da.exec(l.toLowerCase());if(null==n)l="";else{l=n[2];l=null==l||""==l?"n":Ca[l];n=n[1];if(null==n||""==n)n="4";else var r=Ba[n],n=r?r:isNaN(n)?"4":n.substr(0,1);l=[l,n].join("")}}else l="";l&&g.push(l)}0<g.length&&(f=g);3==d.length&&(d=d[2],g=[],d=d?d.split(","):
33
+ g,0<d.length&&(d=Aa[d[0]])&&(a.c[e]=d))}a.c[e]||(d=Aa[e])&&(a.c[e]=d);for(d=0;d<f.length;d+=1)a.a.push(new H(e,f[d]))}};function Fa(a,b){this.c=a;this.a=b}var Ga={Arimo:!0,Cousine:!0,Tinos:!0};Fa.prototype.load=function(a){var b=new C,c=this.c,d=new va(this.a.api,z(c),this.a.text),e=this.a.families;xa(d,e);var f=new za(e);Ea(f);A(c,ya(d),D(b));F(b,function(){a(f.a,f.c,Ga)})};function Ha(a,b){this.c=a;this.a=b}Ha.prototype.load=function(a){var b=this.a.id,c=this.c.m;b?B(this.c,(this.a.api||"https://use.typekit.net")+"/"+b+".js",function(b){if(b)a([]);else if(c.Typekit&&c.Typekit.config&&c.Typekit.config.fn){b=c.Typekit.config.fn;for(var e=[],f=0;f<b.length;f+=2)for(var g=b[f],k=b[f+1],h=0;h<k.length;h++)e.push(new H(g,k[h]));try{c.Typekit.load({events:!1,classes:!1,async:!0})}catch(m){}a(e)}},2E3):a([])};function Ia(a,b){this.c=a;this.f=b;this.a=[]}Ia.prototype.load=function(a){var b=this.f.id,c=this.c.m,d=this;b?(c.__webfontfontdeckmodule__||(c.__webfontfontdeckmodule__={}),c.__webfontfontdeckmodule__[b]=function(b,c){for(var g=0,k=c.fonts.length;g<k;++g){var h=c.fonts[g];d.a.push(new H(h.name,ga("font-weight:"+h.weight+";font-style:"+h.style)))}a(d.a)},B(this.c,z(this.c)+(this.f.api||"//f.fontdeck.com/s/css/js/")+ea(this.c)+"/"+b+".js",function(b){b&&a([])})):a([])};var Y=new pa(window);Y.a.c.custom=function(a,b){return new ua(b,a)};Y.a.c.fontdeck=function(a,b){return new Ia(b,a)};Y.a.c.monotype=function(a,b){return new sa(b,a)};Y.a.c.typekit=function(a,b){return new Ha(b,a)};Y.a.c.google=function(a,b){return new Fa(b,a)};var Z={load:p(Y.load,Y)};"function"===typeof define&&define.amd?define(function(){return Z}):"undefined"!==typeof module&&module.exports?module.exports=Z:(window.WebFont=Z,window.WebFontConfig&&Y.load(window.WebFontConfig));}());
includes/templates/script-web-font-loader.phtml ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @package : OMGF
4
+ * @author : Daan van den Bergh
5
+ * @copyright: (c) 2019 Daan van den Bergh
6
+ * @url : https://daan.dev
7
+ */
8
+
9
+ // Exit if accessed directly
10
+ if (!defined('ABSPATH')) {
11
+ exit;
12
+ }
13
+
14
+ $fonts = hwlGetDownloadedFonts();
15
+ $families = array_unique(
16
+ array_map(
17
+ function ($i) {
18
+ return $i->font_family;
19
+ },
20
+ $fonts
21
+ )
22
+ );
23
+ $omgfStaticVersion = '?ver=' . OMGF_STATIC_VERSION;
24
+ $omgfSrcUrl = OMGF_UPLOAD_URL . '/' . OMGF_FILENAME . (OMGF_REMOVE_VERSION ? null : $omgfStaticVersion);
25
+ $wflVersion = '?ver=' . OMGF_WEB_FONT_LOADER_VERSION;
26
+ $wflSrcUrl = plugin_dir_url(dirname(__FILE__)) . 'libraries/webfont.js' . (OMGF_REMOVE_VERSION ? null : $wflVersion);
27
+ ?>
28
+ <?php if ($fonts && $families): ?>
29
+ <script type="text/javascript">
30
+ /**
31
+ * Using Typekit's Web Font Loader to load OMGF's fonts asynchronously.
32
+ *
33
+ * @type {{custom: {urls: string[], families: string[]}}}
34
+ */
35
+ WebFontConfig = {
36
+ custom: {
37
+ families: [
38
+ <?php foreach ($families as $family): ?>
39
+ <?php
40
+ $fvds = [];
41
+ foreach ($fonts as $font) {
42
+ if ($font->font_family != $family) {
43
+ continue;
44
+ }
45
+ $fvds[] = mb_substr($font->font_style, 0, 1) . mb_substr($font->font_weight, 0, 1);
46
+ }
47
+ ?>
48
+ '<?= $family; ?>:<?= implode(',', $fvds); ?>',
49
+ <?php endforeach; ?>
50
+ ],
51
+ urls: [
52
+ '<?= $omgfSrcUrl; ?>'
53
+ ]
54
+ }
55
+ };
56
+
57
+ (function (d) {
58
+ var wf = d.createElement(
59
+ 'script'),
60
+ s = d.scripts[0];
61
+ wf.src = '<?= $wflSrcUrl; ?>';
62
+ wf.async = true;
63
+ wf.text = 'text/javascript';
64
+ s.parentNode.insertBefore(
65
+ wf,
66
+ s
67
+ );
68
+ })(document);
69
+ </script>
70
+ <?php endif; ?>
includes/templates/{settings-basic-settings.php → settings-basic-settings.phtml} RENAMED
@@ -21,26 +21,39 @@ if (!defined( 'ABSPATH')) exit;
21
  <tr valign="top">
22
  <th scope="row">
23
  <label for="caos_webfonts_cache_dir">
24
- <?php _e('Save webfonts to...', 'host-webfonts-local'); ?>
25
  </label>
26
  </th>
27
  <td>
28
- <input id="caos_webfonts_cache_dir" class="caos_webfonts_cache_dir" type="text" name="caos_webfonts_cache_dir" placeholder="<?= __('e.g. /cache/omgf-webfonts', 'host-webfonts-local'); ?>" value="<?= CAOS_WEBFONTS_CACHE_DIR; ?>" />
29
  <p class="description">
30
- <?php _e("Changes the path where webfonts are cached inside WordPress' content directory (usually <code>wp-content</code>). If you're using Multisite, give each site a unique cache directory path. Defaults to <code>/cache/caos-webfonts</code>.*", 'host-webfonts-local'); ?>
31
  </p>
32
  </td>
33
  </tr>
34
  <tr valign="top">
35
  <th scope="row">
36
  <label for="caos_webfonts_cdn_url">
37
- <?php _e('Serve fonts from CDN', 'host-webfonts-local'); ?>
38
  </label>
39
  </th>
40
  <td>
41
- <input id="caos_webfonts_cdn_url" class="caos_webfonts_cdn_url" type="text" name="caos_webfonts_cdn_url" placeholder="<?= __('e.g. cdn.mydomain.com', 'host-webfonts-local'); ?>" value="<?= CAOS_WEBFONTS_CDN_URL; ?>" />
42
  <p class="description">
43
- <?php _e("Are you using a CDN? Then enter the URL here.*", 'host-webfonts-local'); ?>
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  </p>
45
  </td>
46
  </tr>
@@ -51,9 +64,9 @@ if (!defined( 'ABSPATH')) exit;
51
  </label>
52
  </th>
53
  <td>
54
- <input id="caos_webfonts_remove_version" class="caos_webfonts_remove_version" type="checkbox" name="caos_webfonts_remove_version" <?= CAOS_WEBFONTS_REMOVE_VERSION ? "checked = 'checked'" : ""; ?> />
55
  <p class="description">
56
- <?php _e('This removes the <code>?ver=x.x.x</code> parameter from the Stylesheet\'s (<code>fonts.css</code>) request.', 'host-webfonts-local'); ?>
57
  </p>
58
  </td>
59
  </tr>
@@ -67,7 +80,7 @@ if (!defined( 'ABSPATH')) exit;
67
  <?php $fontDisplay = hwlFontDisplayOptions(); ?>
68
  <select id="caos_webfonts_display_option" name="caos_webfonts_display_option">
69
  <?php foreach ($fontDisplay as $label => $value): ?>
70
- <option value="<?= $value; ?>" <?= $value == CAOS_WEBFONTS_DISPLAY_OPTION ? 'selected' : ''; ?>><?php _e($label, 'host-webfonts-local'); ?></option>
71
  <?php endforeach; ?>
72
  </select>
73
  <br/>
@@ -84,9 +97,9 @@ if (!defined( 'ABSPATH')) exit;
84
  </label>
85
  </th>
86
  <td>
87
- <input class="caos_webfonts_remove_gfonts" id="caos_webfonts_remove_gfonts" type="checkbox" name="caos_webfonts_remove_gfonts" <?= CAOS_WEBFONTS_REMOVE_GFONTS == 'on' ? 'checked = "checked"' : ''; ?> />
88
  <p class="description">
89
- <?= sprintf(__('Enabling this option will attempt to remove any externally hosted Google Fonts-stylesheets from your WordPress-blog. If it doesn\'t work for you, click %shere%s for a more comprehensive guide.', 'host-webfonts-local'), '<a target="_blank" href="' . CAOS_WEBFONTS_SITE_URL . '/how-to/remove-google-fonts-wordpress">', '</a>'); ?>
90
  </p>
91
  </td>
92
  </tr>
@@ -97,7 +110,7 @@ if (!defined( 'ABSPATH')) exit;
97
  </label>
98
  </th>
99
  <td>
100
- <input class="caos_webfonts_preload" id="caos_webfonts_preload" type="checkbox" name="caos_webfonts_preload" <?= CAOS_WEBFONTS_PRELOAD == 'on' ? 'checked = "checked"' : ''; ?> />
101
  <p class="description">
102
  <?php _e('Leave this disabled if you\'re using a CSS minification plugin, such as Autoptimize or W3 Total Cache.', 'host-webfonts-local'); ?> <a target="_blank" href="https://developers.google.com/web/fundamentals/performance/resource-prioritization#preload"><?php _e('Read more', 'host-webfonts-local'); ?></a>
103
  </p>
21
  <tr valign="top">
22
  <th scope="row">
23
  <label for="caos_webfonts_cache_dir">
24
+ <?php _e('Save webfonts to...', 'host-webfonts-local'); ?> *
25
  </label>
26
  </th>
27
  <td>
28
+ <input id="caos_webfonts_cache_dir" class="caos_webfonts_cache_dir" type="text" name="caos_webfonts_cache_dir" placeholder="<?= __('e.g. /cache/omgf-webfonts', 'host-webfonts-local'); ?>" value="<?= OMGF_CACHE_DIR; ?>" />
29
  <p class="description">
30
+ <?php _e("Changes the path where webfonts are cached inside WordPress' content directory (usually <code>wp-content</code>). If you're using Multisite, give each site a unique cache directory path. Defaults to <code>/cache/caos-webfonts</code>.", 'host-webfonts-local'); ?>
31
  </p>
32
  </td>
33
  </tr>
34
  <tr valign="top">
35
  <th scope="row">
36
  <label for="caos_webfonts_cdn_url">
37
+ <?php _e('Serve fonts from CDN', 'host-webfonts-local'); ?> *
38
  </label>
39
  </th>
40
  <td>
41
+ <input id="caos_webfonts_cdn_url" class="caos_webfonts_cdn_url" type="text" name="caos_webfonts_cdn_url" placeholder="<?= __('e.g. cdn.mydomain.com', 'host-webfonts-local'); ?>" value="<?= OMGF_CDN_URL; ?>" />
42
  <p class="description">
43
+ <?php _e("Are you using a CDN? Then enter the URL here.", 'host-webfonts-local'); ?>
44
+ </p>
45
+ </td>
46
+ </tr>
47
+ <tr valign="top">
48
+ <th scope="row">
49
+ <label for="omgf_web_font_loader">
50
+ <?php _e('Use Web Font Loader?', 'host-webfonts-local'); ?>
51
+ </label>
52
+ </th>
53
+ <td>
54
+ <input id="omgf_web_font_loader" class="omgf_web_font_loader" type="checkbox" name="omgf_web_font_loader" <?= OMGF_WEB_FONT_LOADER ? "checked = 'checked'" : ""; ?> />
55
+ <p class="description">
56
+ <?php _e('Use Typekit\'s Web Font Loader to load fonts asynchronously. <strong>Caution:</strong> while this might raise your Pagespeed Score, it could temporarily cause fonts to be displayed unstyled.', 'host-webfonts-local'); ?>
57
  </p>
58
  </td>
59
  </tr>
64
  </label>
65
  </th>
66
  <td>
67
+ <input id="caos_webfonts_remove_version" class="caos_webfonts_remove_version" type="checkbox" name="caos_webfonts_remove_version" <?= OMGF_REMOVE_VERSION ? "checked = 'checked'" : ""; ?> />
68
  <p class="description">
69
+ <?php _e('This removes the <code>?ver=x.x.x</code> parameter from the Stylesheet\'s (<code>fonts.css</code>) request. ', 'host-webfonts-local'); ?>
70
  </p>
71
  </td>
72
  </tr>
80
  <?php $fontDisplay = hwlFontDisplayOptions(); ?>
81
  <select id="caos_webfonts_display_option" name="caos_webfonts_display_option">
82
  <?php foreach ($fontDisplay as $label => $value): ?>
83
+ <option value="<?= $value; ?>" <?= $value == OMGF_DISPLAY_OPTION ? 'selected' : ''; ?>><?php _e($label, 'host-webfonts-local'); ?></option>
84
  <?php endforeach; ?>
85
  </select>
86
  <br/>
97
  </label>
98
  </th>
99
  <td>
100
+ <input class="caos_webfonts_remove_gfonts" id="caos_webfonts_remove_gfonts" type="checkbox" name="caos_webfonts_remove_gfonts" <?= OMGF_REMOVE_GFONTS == 'on' ? 'checked = "checked"' : ''; ?> />
101
  <p class="description">
102
+ <?= sprintf(__('Enabling this option will attempt to remove any externally hosted Google Fonts-stylesheets from your WordPress-blog. If it doesn\'t work for you, click %shere%s for a more comprehensive guide.', 'host-webfonts-local'), '<a target="_blank" href="' . OMGF_SITE_URL . '/how-to/remove-google-fonts-wordpress">', '</a>'); ?>
103
  </p>
104
  </td>
105
  </tr>
110
  </label>
111
  </th>
112
  <td>
113
+ <input class="caos_webfonts_preload" id="caos_webfonts_preload" type="checkbox" name="caos_webfonts_preload" <?= OMGF_PRELOAD == 'on' ? 'checked = "checked"' : ''; ?> />
114
  <p class="description">
115
  <?php _e('Leave this disabled if you\'re using a CSS minification plugin, such as Autoptimize or W3 Total Cache.', 'host-webfonts-local'); ?> <a target="_blank" href="https://developers.google.com/web/fundamentals/performance/resource-prioritization#preload"><?php _e('Read more', 'host-webfonts-local'); ?></a>
116
  </p>
includes/templates/{settings-generate-stylesheet.php → settings-generate-stylesheet.phtml} RENAMED
File without changes
includes/templates/{settings-welcome.php → settings-welcome.phtml} RENAMED
@@ -35,14 +35,14 @@ if (!defined('ABSPATH')) {
35
  <li class="welcome-icon dashicons-before dashicons-smiley"><?php _e('Done!', 'host-webfonts-local'); ?></li>
36
  </ul>
37
  <p>
38
- <?= sprintf(__('%sClick here%s for a more comprehensive guide.', 'host-webfonts-local'), '<a target="_blank" href="' . CAOS_WEBFONTS_SITE_URL . '/wordpress/host-google-fonts-locally/">', '</a>'); ?>
39
  </p>
40
  </div>
41
  <div class="welcome-panel-column" style="width: 32%;">
42
  <h3><?php _e('Get a Perfect Score on Pagespeed & Pingdom!', 'host-webfonts-local'); ?></h3>
43
  <p><?= sprintf(__('%sLeverage your browser cache%s and
44
  %slower pageload times%s by hosting analytics.js locally with
45
- %sCAOS%s.', 'host-webfonts-local'), '<strong>', '</strong>', '<strong>', '</strong>', '<a href="' . CAOS_WEBFONTS_SITE_URL . '/wordpress-plugins/optimize-analytics-wordpress/" target="_blank">', '</a>'); ?>
46
  </p>
47
  <p>
48
  <a target="_blank" href="https://wordpress.org/plugins/host-analyticsjs-local"><?php _e('Download now', 'host-webfonts-local'); ?></a>
@@ -50,7 +50,7 @@ if (!defined('ABSPATH')) {
50
  <h3><?php _e('Want to Host other Files Locally?', 'host-webfonts-local'); ?></h3>
51
  <p><?php _e('Unleash your site\'s true potential by locally hosting as many files as possible.', 'host-webfonts-local'); ?></p>
52
  <p>
53
- <a target="_blank" href="<?= CAOS_WEBFONTS_SITE_URL; ?>/how-to/host-js-locally-crontab/"><?php _e('Read more', 'host-webfonts-local'); ?></a>
54
  </p>
55
  </div>
56
  <div class="welcome-panel-column welcome-panel-last" style="width: 34%;">
@@ -61,17 +61,17 @@ if (!defined('ABSPATH')) {
61
  <?php _e('Thank you for using OMGF.', 'host-webfonts-local'); ?>
62
  </p>
63
  <p>
64
- <?= sprintf(__('I am convinced that knowledge should be free. That\'s why I will never charge you for the plugins I create and I will help you to succeed in your projects through the %stutorials%s on my blog.', 'host-webfonts-local'), '<a href="' . CAOS_WEBFONTS_SITE_URL . '/how-to/" target="_blank">', '</a>'); ?>
65
  </p>
66
  <p>
67
- <?= sprintf(__('However, my time is just as valuable as yours. Consider supporting me by either %sdonating%s or leaving a %s5-star review%s on Wordpress.org.', 'host-webfonts-local'), '<a href="' . CAOS_WEBFONTS_SITE_URL . '/donate" target="_blank">', '</a>', '<a target="_blank" href="https://wordpress.org/support/plugin/host-analyticsjs-local/reviews/?rate=5#new-post">', '</a>'); ?>
68
  </p>
69
  <p>
70
 
71
- <?= sprintf(__('If you\'re running into any issues, please make sure you\'ve read %sthe manual%s thoroughly. Visit the %sFAQ%s and %sSupport Forum%s to see if your question has already been answered. If not, ask a question on the Support Forum.', 'host-webfonts-local'), '<a href="' . CAOS_WEBFONTS_SITE_URL . '/wordpress/host-google-fonts-locally/" target="_blank">', '</a>', '<a href="https://wordpress.org/plugins/host-webfonts-local/#description" target="_blank">', '</a>', '<a href="https://wordpress.org/support/plugin/host-webfonts-local">', '</a>'); ?>
72
  </p>
73
  <p>
74
- <a target="_blank" class="button button-primary button-hero" href="<?= CAOS_WEBFONTS_SITE_URL; ?>/donate/"><span class="dashicons-before dashicons-heart"> <?php _e('Donate', 'host-webfonts-local'); ?></span></a>
75
  <a target="_blank" class="button button-secondary button-hero" href="https://wordpress.org/support/plugin/host-webfonts-local/reviews/?rate=5#new-post"><span class="dashicons-before dashicons-star-filled"> <?php _e('Review', 'host-webfonts-local'); ?></span></a>
76
  <a target="_blank" class="button button-secondary button-hero" href="https://twitter.com/Dan0sz"><span class="dashicons-before dashicons-twitter"> <?php _e('Follow', 'host-webfonts-local'); ?></span></a>
77
  </p>
35
  <li class="welcome-icon dashicons-before dashicons-smiley"><?php _e('Done!', 'host-webfonts-local'); ?></li>
36
  </ul>
37
  <p>
38
+ <?= sprintf(__('%sClick here%s for a more comprehensive guide.', 'host-webfonts-local'), '<a target="_blank" href="' . OMGF_SITE_URL . '/wordpress/host-google-fonts-locally/">', '</a>'); ?>
39
  </p>
40
  </div>
41
  <div class="welcome-panel-column" style="width: 32%;">
42
  <h3><?php _e('Get a Perfect Score on Pagespeed & Pingdom!', 'host-webfonts-local'); ?></h3>
43
  <p><?= sprintf(__('%sLeverage your browser cache%s and
44
  %slower pageload times%s by hosting analytics.js locally with
45
+ %sCAOS%s.', 'host-webfonts-local'), '<strong>', '</strong>', '<strong>', '</strong>', '<a href="' . OMGF_SITE_URL . '/wordpress-plugins/optimize-analytics-wordpress/" target="_blank">', '</a>'); ?>
46
  </p>
47
  <p>
48
  <a target="_blank" href="https://wordpress.org/plugins/host-analyticsjs-local"><?php _e('Download now', 'host-webfonts-local'); ?></a>
50
  <h3><?php _e('Want to Host other Files Locally?', 'host-webfonts-local'); ?></h3>
51
  <p><?php _e('Unleash your site\'s true potential by locally hosting as many files as possible.', 'host-webfonts-local'); ?></p>
52
  <p>
53
+ <a target="_blank" href="<?= OMGF_SITE_URL; ?>/how-to/host-js-locally-crontab/"><?php _e('Read more', 'host-webfonts-local'); ?></a>
54
  </p>
55
  </div>
56
  <div class="welcome-panel-column welcome-panel-last" style="width: 34%;">
61
  <?php _e('Thank you for using OMGF.', 'host-webfonts-local'); ?>
62
  </p>
63
  <p>
64
+ <?= sprintf(__('I am convinced that knowledge should be free. That\'s why I will never charge you for the plugins I create and I will help you to succeed in your projects through the %stutorials%s on my blog.', 'host-webfonts-local'), '<a href="' . OMGF_SITE_URL . '/how-to/" target="_blank">', '</a>'); ?>
65
  </p>
66
  <p>
67
+ <?= sprintf(__('However, my time is just as valuable as yours. Consider supporting me by either %sdonating%s or leaving a %s5-star review%s on Wordpress.org.', 'host-webfonts-local'), '<a href="' . OMGF_SITE_URL . '/donate" target="_blank">', '</a>', '<a target="_blank" href="https://wordpress.org/support/plugin/host-analyticsjs-local/reviews/?rate=5#new-post">', '</a>'); ?>
68
  </p>
69
  <p>
70
 
71
+ <?= sprintf(__('If you\'re running into any issues, please make sure you\'ve read %sthe manual%s thoroughly. Visit the %sFAQ%s and %sSupport Forum%s to see if your question has already been answered. If not, ask a question on the Support Forum.', 'host-webfonts-local'), '<a href="' . OMGF_SITE_URL . '/wordpress/host-google-fonts-locally/" target="_blank">', '</a>', '<a href="https://wordpress.org/plugins/host-webfonts-local/#description" target="_blank">', '</a>', '<a href="https://wordpress.org/support/plugin/host-webfonts-local">', '</a>'); ?>
72
  </p>
73
  <p>
74
+ <a target="_blank" class="button button-primary button-hero" href="<?= OMGF_SITE_URL; ?>/donate/"><span class="dashicons-before dashicons-heart"> <?php _e('Donate', 'host-webfonts-local'); ?></span></a>
75
  <a target="_blank" class="button button-secondary button-hero" href="https://wordpress.org/support/plugin/host-webfonts-local/reviews/?rate=5#new-post"><span class="dashicons-before dashicons-star-filled"> <?php _e('Review', 'host-webfonts-local'); ?></span></a>
76
  <a target="_blank" class="button button-secondary button-hero" href="https://twitter.com/Dan0sz"><span class="dashicons-before dashicons-twitter"> <?php _e('Follow', 'host-webfonts-local'); ?></span></a>
77
  </p>
js/hwl-admin.js CHANGED
@@ -24,7 +24,7 @@ function hwlSearchFontSubsets(queriedFonts)
24
  {
25
  let searchField = jQuery('#search-field');
26
  let searchButton = jQuery('#search-btn');
27
-
28
  jQuery.ajax({
29
  type: 'POST',
30
  url: ajaxurl,
@@ -53,18 +53,18 @@ function hwlRenderAvailableSubsets(response)
53
  {
54
  let data = response['responseJSON'];
55
  dataLength = data.length;
56
-
57
  for (let ii = 0; ii < dataLength; ii++) {
58
  subsets = data[ii]['subsets']
59
  family = data[ii]['family'];
60
  id = data[ii]['id'];
61
  length = subsets.length;
62
  renderedSubsets = [];
63
-
64
  for (let iii = 0; iii < length; iii++) {
65
  renderedSubsets[iii] = `<td><label><input name="${id}" value="${subsets[iii]}" type="checkbox" onclick="hwlGenerateSearchQuery('${id}')" />${subsets[iii]}</label></td>`;
66
  }
67
-
68
  jQuery('#hwl-subsets').append('<tr valign="top" id="' + id + '"><td><input type="text" class="hwl-subset-font-family" value="' + family + '" readonly/></td>' + renderedSubsets + '</tr>');
69
  jQuery('#hwl-results').append("<tbody id='" + 'hwl-section-' + id + "'></tbody>");
70
  }
@@ -79,11 +79,11 @@ function hwlGenerateSearchQuery(id)
79
  {
80
  let subsets = [];
81
  checked = jQuery("input[name='" + id + "']:checked");
82
-
83
  jQuery.each(checked, function() {
84
  subsets.push(jQuery(this).val());
85
  });
86
-
87
  subsets.join()
88
  hwlSearchGoogleFonts(id, subsets);
89
  }
@@ -184,21 +184,21 @@ function hwlGatherSelectedSubsets()
184
  id = this.id;
185
  checkboxes = jQuery("input[name='" + id + "']");
186
  checked = jQuery("input[name='" + id + "']:checked");
187
-
188
  selectedSubsets = [];
189
  jQuery.each(checked, function() {
190
  selectedSubsets.push(jQuery(this).val());
191
  });
192
  selectedSubsets.join()
193
-
194
  availableSubsets = [];
195
  jQuery.each(checkboxes, function() {
196
  availableSubsets.push(jQuery(this).val());
197
  });
198
  availableSubsets.join()
199
-
200
  family = jQuery(this).find('.hwl-subset-font-family').val();
201
-
202
  subsets[id] = {};
203
  subsets[id]['family'] = {};
204
  subsets[id]['family'] = family;
@@ -207,12 +207,12 @@ function hwlGatherSelectedSubsets()
207
  subsets[id]['available'] = {};
208
  subsets[id]['available'] = availableSubsets;
209
  })
210
-
211
  return subsets;
212
  }
213
 
214
  /**
215
- * Triggered when 'Save Webfonts' is clicked.
216
  */
217
  function hwlDownloadFonts()
218
  {
@@ -234,13 +234,26 @@ function hwlDownloadFonts()
234
  },
235
  success: function() {
236
  clearTimeout(downloadStatus);
237
-
238
  hwlUpdateInputValue(downloadButton, 'Done!', '0 41px 1px');
239
  hwlUpdateStatusBar(100);
240
-
241
  setTimeout(function() {
242
  hwlUpdateInputValue(downloadButton, 'Download Fonts');
243
  }, 2500);
 
 
 
 
 
 
 
 
 
 
 
 
 
244
  }
245
  })
246
  }
@@ -261,7 +274,7 @@ function hwlGetDownloadStatus()
261
  downloaded = response.downloaded;
262
  total = response.total;
263
  progress = (100 / total) * downloaded;
264
-
265
  hwlUpdateStatusBar(progress);
266
  }
267
  });
24
  {
25
  let searchField = jQuery('#search-field');
26
  let searchButton = jQuery('#search-btn');
27
+
28
  jQuery.ajax({
29
  type: 'POST',
30
  url: ajaxurl,
53
  {
54
  let data = response['responseJSON'];
55
  dataLength = data.length;
56
+
57
  for (let ii = 0; ii < dataLength; ii++) {
58
  subsets = data[ii]['subsets']
59
  family = data[ii]['family'];
60
  id = data[ii]['id'];
61
  length = subsets.length;
62
  renderedSubsets = [];
63
+
64
  for (let iii = 0; iii < length; iii++) {
65
  renderedSubsets[iii] = `<td><label><input name="${id}" value="${subsets[iii]}" type="checkbox" onclick="hwlGenerateSearchQuery('${id}')" />${subsets[iii]}</label></td>`;
66
  }
67
+
68
  jQuery('#hwl-subsets').append('<tr valign="top" id="' + id + '"><td><input type="text" class="hwl-subset-font-family" value="' + family + '" readonly/></td>' + renderedSubsets + '</tr>');
69
  jQuery('#hwl-results').append("<tbody id='" + 'hwl-section-' + id + "'></tbody>");
70
  }
79
  {
80
  let subsets = [];
81
  checked = jQuery("input[name='" + id + "']:checked");
82
+
83
  jQuery.each(checked, function() {
84
  subsets.push(jQuery(this).val());
85
  });
86
+
87
  subsets.join()
88
  hwlSearchGoogleFonts(id, subsets);
89
  }
184
  id = this.id;
185
  checkboxes = jQuery("input[name='" + id + "']");
186
  checked = jQuery("input[name='" + id + "']:checked");
187
+
188
  selectedSubsets = [];
189
  jQuery.each(checked, function() {
190
  selectedSubsets.push(jQuery(this).val());
191
  });
192
  selectedSubsets.join()
193
+
194
  availableSubsets = [];
195
  jQuery.each(checkboxes, function() {
196
  availableSubsets.push(jQuery(this).val());
197
  });
198
  availableSubsets.join()
199
+
200
  family = jQuery(this).find('.hwl-subset-font-family').val();
201
+
202
  subsets[id] = {};
203
  subsets[id]['family'] = {};
204
  subsets[id]['family'] = family;
207
  subsets[id]['available'] = {};
208
  subsets[id]['available'] = availableSubsets;
209
  })
210
+
211
  return subsets;
212
  }
213
 
214
  /**
215
+ * Triggered when 'Download Fonts' is clicked.
216
  */
217
  function hwlDownloadFonts()
218
  {
234
  },
235
  success: function() {
236
  clearTimeout(downloadStatus);
237
+
238
  hwlUpdateInputValue(downloadButton, 'Done!', '0 41px 1px');
239
  hwlUpdateStatusBar(100);
240
+
241
  setTimeout(function() {
242
  hwlUpdateInputValue(downloadButton, 'Download Fonts');
243
  }, 2500);
244
+ },
245
+ error: function(message) {
246
+ clearTimeout(downloadStatus);
247
+
248
+ errorText = message.responseJSON.data;
249
+ errorCode = message.status;
250
+
251
+ var errorMessage = '<div id="setting-error-settings_updated" class="error settings-error notice is-dismissible"><p><strong>Error: ' + errorCode + '</strong> - ' + errorText + '</p></div>';
252
+
253
+ jQuery('html, body').animate({scrollTop: 0}, 800);
254
+ jQuery(errorMessage).insertAfter('.wrap h1');
255
+
256
+ hwlUpdateInputValue(downloadButton, 'Download Fonts');
257
  }
258
  })
259
  }
274
  downloaded = response.downloaded;
275
  total = response.total;
276
  progress = (100 / total) * downloaded;
277
+
278
  hwlUpdateStatusBar(progress);
279
  }
280
  });
readme.txt CHANGED
@@ -3,8 +3,8 @@ Contributors: DaanvandenBergh
3
  Donate link: https://daan.dev/donate/
4
  Tags: google, fonts, host, save, local, locally, webfonts, update, minimize, external, requests, leverage, browser, cache
5
  Requires at least: 4.6
6
- Tested up to: 5.2
7
- Stable tag: 1.9.11
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -24,6 +24,8 @@ This will *decrease your pageload times*, *leverage browser cache*, *minimize DN
24
  - Easily find and download your fonts in multiple subsets,
25
  - Generate a stylesheet, which is automatically added to your header using WordPress' wp_head()-function,
26
  - Change the caching path (where the fonts and stylesheet are saved) for increased compatibility with Multisite environments and Caching- and Security-plugins, such as WP Super Cache, Autoptimize and WordFence,
 
 
27
  - Control font performance by adding font-display property,
28
  - Auto-generates the local source for webfonts,
29
  - Automatically remove any fonts loaded from fonts.gstatic.com or fonts.googleapis.com,
@@ -59,9 +61,17 @@ This could be for several reasons:
59
 
60
  Yes, you can. Enter the url of your CDN and re-download and re-generate the stylesheet. Then the fonts will be saved to and served from your CDN.
61
 
 
 
 
 
62
  = I'm getting a 'Load resources from a consistent URL' after installing and configuring this plugin. What's going on? =
63
 
64
- This must be, because you're still loading the externally hosted Google Fonts, besides the fonts you downloaded using OMGF. Try checking the option 'Remove Google Fonts' and see if that helps. If it doesn't consider using a child theme to 'dequeue' any external requests. If you don't know how to do that, consider using a plugin such as Autoptimize to optimize your CSS and remove the fonts.
 
 
 
 
65
 
66
  = Does this plugin edit template files? =
67
 
@@ -90,6 +100,10 @@ Yes, it does. When using subdomains, however, you might run into CORS related is
90
 
91
  Repeat this for every site you want to use with OMGF. A new stylesheet, using the corresponding site's Home-URL and cache directory for each font, has been generated. Bypassing any Cross-Origin Resource Sharing (CORS) issues you might run into.
92
 
 
 
 
 
93
  = Can I buy you a beer? =
94
 
95
  Yes, please! [Click here to buy me a beer](https://daan.dev/donate/ "Let's do shots!")!
@@ -100,6 +114,30 @@ N/A
100
 
101
  == Changelog ==
102
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  = 1.9.11 =
104
  Fixed bug where a few strings couldn't be translated. Improved responsiveness of settings-screen. Minor re-factor for better structure.
105
 
3
  Donate link: https://daan.dev/donate/
4
  Tags: google, fonts, host, save, local, locally, webfonts, update, minimize, external, requests, leverage, browser, cache
5
  Requires at least: 4.6
6
+ Tested up to: 5.3
7
+ Stable tag: 2.0.8
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
24
  - Easily find and download your fonts in multiple subsets,
25
  - Generate a stylesheet, which is automatically added to your header using WordPress' wp_head()-function,
26
  - Change the caching path (where the fonts and stylesheet are saved) for increased compatibility with Multisite environments and Caching- and Security-plugins, such as WP Super Cache, Autoptimize and WordFence,
27
+ - Serve your fonts from your CDN,
28
+ - Enable Typekit's [Web Font Loader](https://github.com/typekit/webfontloader) to load your fonts asynchronously and further increase your Pagespeed Insights score (!),
29
  - Control font performance by adding font-display property,
30
  - Auto-generates the local source for webfonts,
31
  - Automatically remove any fonts loaded from fonts.gstatic.com or fonts.googleapis.com,
61
 
62
  Yes, you can. Enter the url of your CDN and re-download and re-generate the stylesheet. Then the fonts will be saved to and served from your CDN.
63
 
64
+ = How can I make sure the fonts load asynchronously AKA non-render blocking?
65
+
66
+ Enable Typekit's Web Font Loader in the settings and OMGF will take care of it for you!
67
+
68
  = I'm getting a 'Load resources from a consistent URL' after installing and configuring this plugin. What's going on? =
69
 
70
+ This must be, because you're still loading the externally hosted Google Fonts, besides the fonts you downloaded using OMGF. Try checking the option 'Remove Google Fonts' and see if that helps. If it doesn't consider using a child theme to 'dequeue' any external requests. If you don't know how to do that, please [contact](https://daan.dev/contact/) me.
71
+
72
+ = I have 'Remove Google Fonts' enabled, but the fonts from fonts.gstatic.com|fonts.googleapis.com are still loaded. What's going on? =
73
+
74
+ The option in OMGF removes any fonts that are loaded in the conventional way. However, if it doesn't work for you and you're using a popular theme, I'd love to help and make OMGF compatible. So don't hesitate to [contact](https://daan.dev/contact/) me.
75
 
76
  = Does this plugin edit template files? =
77
 
100
 
101
  Repeat this for every site you want to use with OMGF. A new stylesheet, using the corresponding site's Home-URL and cache directory for each font, has been generated. Bypassing any Cross-Origin Resource Sharing (CORS) issues you might run into.
102
 
103
+ = Is this plugin compatible with WPML? =
104
+
105
+ No, not yet. But I will definitely try to make it compatible in the future!
106
+
107
  = Can I buy you a beer? =
108
 
109
  Yes, please! [Click here to buy me a beer](https://daan.dev/donate/ "Let's do shots!")!
114
 
115
  == Changelog ==
116
 
117
+ = 2.0.8 =
118
+ Fixed 400-error when re-downloading fonts. Added compatibility for Enfold-theme.
119
+
120
+ = 2.0.6 =
121
+ Fixed bug with include paths.
122
+
123
+ = 2.0.5 =
124
+ OMGF now retries downloading the fonts using fopen, if the cURL attempt failed. Code improvements.
125
+
126
+ = 2.0.4 =
127
+ Further improvements for downloading of analytics.js.
128
+
129
+ = 2.0.3 =
130
+ Tested with WP 5.3 and replaced `fopen()` with cUrl to make OMGF compatible with servers that have `allow_url_fopen` disabled.
131
+
132
+ = 2.0.2 =
133
+ Added error handling to DownloadFonts-script.
134
+
135
+ = 2.0.1 =
136
+ Using Typekit's Web Font Loader only uses 1 SQL query in the frontend now, regardless of how many fonts you use.
137
+
138
+ = 2.0.0 =
139
+ Added Typekit's Web Font Loader to allow loading fonts asynchronously.
140
+
141
  = 1.9.11 =
142
  Fixed bug where a few strings couldn't be translated. Improved responsiveness of settings-screen. Minor re-factor for better structure.
143