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

Version Description

Replaced download counter for progress-bar. Refactored logic for AJAX-requests for better performance.

Download this release

Release Info

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

Code changes from version 1.1.0 to 1.5.7

.gitignore CHANGED
@@ -2,3 +2,4 @@
2
  .idea
3
  .git
4
  .svn
 
2
  .idea
3
  .git
4
  .svn
5
+ .svnignore
css/hwl-admin.css ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * @package: CAOS for Webfonts
3
+ * @author: Daan van den Bergh
4
+ * @copyright: (c) 2019 Daan van den Bergh
5
+ * @url: https://dev.daanvandenbergh.com
6
+ */
7
+
8
+ #hwl-results .spinner {
9
+ visibility: visible !important;
10
+ float: none;
11
+ }
12
+
13
+ #hwl-results {
14
+ position: relative;
15
+ }
16
+
17
+ tr.loading td {
18
+ position: absolute;
19
+ width: 100%;
20
+ height: 100%;
21
+ left: 0;
22
+ right: 0;
23
+ text-align: center;
24
+ background-color: rgba(255, 255, 255, 0.5)
25
+ }
26
+
27
+ .hwl-remove {
28
+ cursor: pointer;
29
+ }
30
+
31
+ .button-cancel {
32
+ color: #a00;
33
+ text-decoration: none;
34
+ border-color: transparent;
35
+ box-shadow: none;
36
+ background: 0 0;
37
+ border-radius: 3px;
38
+ white-space: nowrap;
39
+ padding: 6px 10px !important;
40
+ }
41
+
42
+ .button-cancel:hover {
43
+ cursor: pointer;
44
+ background: #d54e21;
45
+ color: #fff;
46
+ border-color: #d54e21;
47
+ }
48
+
49
+ .caos-status-total-bar {
50
+ height: 30px;
51
+ border-radius: 6px;
52
+ padding: 4px 4px 0;
53
+ background: rgba(0, 0, 0, 0.25);
54
+ box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.25), 0 1px rgba(255, 255, 255, 0.08);
55
+ }
56
+
57
+ #caos-status-progress-bar {
58
+ height: 26px;
59
+ color: #fff;
60
+ border-radius: 4px;
61
+ white-space: nowrap;
62
+ line-height: 25px;
63
+ text-align: center;
64
+ transition: width 1s;
65
+ -webkit-transition: width 1s;
66
+ background-color: #0daadb;
67
+ background-image: linear-gradient(to bottom, rgba(255, 255, 255, 0.3), rgba(255, 255, 255, 0.05));
68
+ box-shadow: 0 0 1px 1px rgba(0, 0, 0, 0.25), inset 0 1px rgba(255, 255, 255, 0.1);
69
+ }
host-webfonts-local.php CHANGED
@@ -2,325 +2,342 @@
2
  /**
3
  * Plugin Name: CAOS for Webfonts
4
  * Plugin URI: https://dev.daanvandenbergh.com/wordpress-plugins/host-google-fonts-locally
5
- * Description: Automagically save the fonts you want to use inside your content-folder, generate a stylesheet for them and enqueue it in your theme's header.
6
- * Version: 1.1.0
 
7
  * Author: Daan van den Bergh
8
  * Author URI: https://dev.daanvandenbergh.com
9
  * License: GPL2v2 or later
10
  */
11
 
12
  // Exit if accessed directly
13
- if (!defined('ABSPATH')) exit;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  /**
16
- * Extend WP default Allowed Filetypes
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  *
18
- * @param array $filetypes
19
- * @return array
20
  */
21
- function hwlSetAllowedFiletypes($filetypes = array()) {
 
 
 
 
22
 
23
- $filetypes['woff'] = "application/font-woff";
24
- $filetypes['woff2'] = "application/font-woff2";
25
- $filetypes['ttf'] = "application/-x-font-truetype";
26
- $filetypes['otf'] = "font/opentype";
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- return $filetypes;
 
 
 
29
  }
30
- add_filter('upload_mimes', 'hwlSetAllowedFiletypes');
31
 
32
  /**
33
- * Create the Admin menu-item
34
  */
35
- function hwlCreateMenu()
36
- {
37
- add_options_page(
38
- 'CAOS for Webfonts',
39
- 'Optimize Webfonts',
40
- 'manage_options',
41
- 'optimize-webfonts',
42
- 'hwlSettingsPage'
43
- );
44
  }
 
45
 
46
- add_action('admin_menu', 'hwlCreateMenu');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  /**
49
  * Render the settings page.
50
  */
51
- function hwlSettingsPage()
52
- {
53
- if (!current_user_can('manage_options'))
54
- {
55
- wp_die(__("You're not cool enough to access this page."));
56
- }
57
- ?>
58
  <div class="wrap">
59
  <h1><?php _e('CAOS for Webfonts', 'host-webfonts-local'); ?></h1>
60
  <p>
61
- <?php _e('Developed by: ', 'host-webfonts-local'); ?>
62
- <a title="Buy me a beer!" href="http://dev.daanvandenbergh.com/donate/">
63
  Daan van den Bergh</a>.
64
  </p>
 
65
  <div id="hwl-admin-notices"></div>
66
- <?php require_once('includes/welcome-panel.php'); ?>
67
- <form id="hwl-options-form" name="hwl-options-form">
68
- <?php
69
- settings_fields('host-webfonts-local-basic-settings'
70
- );
71
- do_settings_sections('host-webfonts-local-basic-settings'
72
- );
73
-
74
- /**
75
- * Render the upload-functions.
76
- */
77
- hwlMediaUploadInit();
78
-
79
- do_action('hwl_after_form_settings');
80
- ?>
 
 
 
 
 
 
 
 
 
 
81
  </form>
82
  </div>
83
- <?php
84
  }
85
 
86
  /**
87
- * Set custom upload-fields and render upload buttons.
88
  */
89
- function hwlMediaUploadInit() {
90
- wp_enqueue_media();
91
- ?>
92
- <table>
93
- <tbody>
94
- <tr valign="top">
95
- <th>filename</th>
96
- <th>font-family</th>
97
- <th>font-weight</th>
98
- <th>font-type</th>
99
- <th>source URL</th>
100
- </tr>
101
- </tbody>
102
- <tbody id="hwl_uploaded_fonts">
103
- </tbody>
104
- <tbody>
105
- <tr valign="bottom">
106
- <td>
107
- <input type="button" onclick="hwlFontUploader()" name="upload-btn"
108
- id="upload-btn" class="button-secondary" value="Select Fonts" />
109
- </td>
110
- <td>
111
- <input type="button" onclick="hwlGenerateStylesheet()" name="generate-btn"
112
- id="generate-btn" class="button-primary" value="Generate Stylesheet" />
113
- </td>
114
- </tr>
115
- </tbody>
116
- </table>
117
- <script type="text/javascript">
118
- var media_uploader = null;
119
-
120
- /**
121
- * Get the Media Uploader and prepare the uploaded fonts for generating the style sheet.
122
- */
123
- function hwlFontUploader()
124
- {
125
- media_uploader = wp.media({
126
- frame: "post",
127
- state: "insert",
128
- multiple: true
129
- }).open();
130
-
131
- hwlSetUploadDir();
132
-
133
- media_uploader.on(
134
- "close",
135
- function() {
136
- hwlResetUploadDir();
137
- }
138
- ).on(
139
- "insert",
140
- function() {
141
- hwlGenerateOutput();
142
- }
143
- );
144
- }
145
-
146
- /**
147
- * Call the media-upload script or logs an error to the console.
148
- */
149
- function hwlSetUploadDir()
150
- {
151
- jQuery.ajax({
152
- type: 'POST',
153
- url: ajaxurl,
154
- data: {
155
- action: 'hwlAjaxSetUploadDir'
156
- },
157
- error: function(response) {
158
- console.log(response);
159
- }
160
- });
161
- }
162
-
163
- /**
164
- * AJAX call to reset upload directory.
165
- */
166
- function hwlResetUploadDir()
167
- {
168
- jQuery.ajax({
169
- type: 'POST',
170
- url: ajaxurl,
171
- data: {
172
- action: 'hwlAjaxResetUploadDir'
173
- },
174
- error: function(response) {
175
- console.log(response);
176
- }
177
- });
178
- }
179
-
180
- /**
181
- * Generate the output after upload/insert
182
- */
183
- function hwlGenerateOutput()
184
- {
185
- var length = media_uploader.state().get("selection").length;
186
- var fonts = media_uploader.state().get("selection").models;
187
- for (var iii = 0; iii < length; iii++) {
188
- var font_url = fonts[iii].changed.url;
189
- var font_name = fonts[iii].changed.title;
190
- var font_type = fonts[iii].changed.subtype;
191
- var uploadedFont = `<tr valign="top">
192
- <td>
193
- <input type="text" name="hwl_uploaded_font][${font_name}]"
194
- id="hwl_uploaded_font][${font_name}]"
195
- value="${font_name}" readonly />
196
- </td>
197
- <td>
198
- <input type="text" name="hwl_uploaded_font][${font_name}][font_family]"
199
- id="hwl_uploaded_font][${font_name}][font_family]"
200
- value="" />
201
- </td>
202
- <td>
203
- <input type="text" name="hwl_uploaded_font][${font_name}][font_weight]"
204
- id="hwl_uploaded_font][${font_name}][font_weight]"
205
- value="" />
206
- </td>
207
- <td>
208
- <input type="text" name="hwl_uploaded_font][${font_name}][${font_type}]"
209
- id="hwl_uploaded_font][${font_name}][${font_type}]"
210
- value="${font_type}" readonly />
211
- </td>
212
- <td>
213
- <input type="text" name="hwl_uploaded_font][${font_name}][${font_type}][url]"
214
- id="hwl_uploaded_font][${font_name}][${font_type}][url]"
215
- value="${font_url}" readonly />
216
- </td>
217
- </tr>`;
218
- jQuery('#hwl_uploaded_fonts').append(uploadedFont);
219
- }
220
- }
221
-
222
- /**
223
- * Call the generate-stylesheet script and reset the upload dir to the default setting.
224
- */
225
- function hwlGenerateStylesheet() {
226
- var hwlData = hwlSerializeArray($('#hwl-options-form'));
227
-
228
- jQuery.ajax({
229
- type: 'POST',
230
- url: ajaxurl,
231
- data: {
232
- action: 'hwlAjaxGenerateStyles',
233
- uploaded_fonts: hwlData
234
- },
235
- success: function(response) {
236
- jQuery('#hwl-admin-notices').append(
237
- `<div class="updated settings-error notice is-dismissible">
238
- <p>${response}</p>
239
- </div>`
240
- );
241
- jQuery('#hwl_uploaded_fonts').each(function() {
242
- jQuery(this).fadeOut(700, function() {
243
- jQuery(this).remove();
244
- })
245
- });
246
- },
247
- error: function(response) {
248
- jQuery('#hwl-admin-notices').append(
249
- `<div class="notice notice-error is-dismissible">
250
- <p><?php _e( 'The stylesheet could not be created:', 'host-webfonts-local' ); ?> ${response}</p>"
251
- </div>`
252
- )
253
- }
254
- });
255
- }
256
-
257
- /**
258
- * Serialize form data to a multi-dimensional array.
259
- */
260
- function hwlSerializeArray(data) {
261
- var result = [];
262
- data.each(function() {
263
- var fields = {};
264
- $.each($(this).serializeArray(), function() {
265
- fields[this.name] = this.value;
266
- });
267
- result.push(fields);
268
- });
269
-
270
- return result;
271
- }
272
- </script>
273
- <?php
274
  }
275
 
276
  /**
277
- * Before each upload we temporarily set our custom upload-directory.
278
  */
279
- function hwlAjaxSetUploadDir() {
280
- try {
281
- update_option('upload_path',WP_CONTENT_DIR . '/local-fonts');
282
- update_option('upload_url_path',content_url() . '/local-fonts');
283
- update_option('uploads_use_yearmonth_folders', false);
284
- wp_die();
285
- } catch (\Exception $e) {
286
- wp_die($e);
287
- }
288
  }
289
- add_action('wp_ajax_hwlAjaxSetUploadDir', 'hwlAjaxSetUploadDir');
290
 
 
 
 
 
 
 
 
 
 
291
 
292
  /**
293
- * After we're done uploading we need to reset the upload-directory.
294
  */
295
- function hwlAjaxResetUploadDir() {
296
- try {
297
- update_option('upload_path',null);
298
- update_option('upload_url_path',null);
299
- update_option('uploads_use_yearmonth_folders', true);
300
- wp_die();
301
- } catch (\Exception $e) {
302
- wp_die($e);
303
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
304
  }
305
- add_action('wp_ajax_hwlAjaxResetUploadDir', 'hwlAjaxResetUploadDir');
306
 
307
  /**
308
- * The function for generating the stylesheet and resetting the upload dir to the default.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
309
  */
310
  function hwlAjaxGenerateStyles() {
311
- require_once('includes/generate-stylesheet.php');
312
  }
313
  add_action('wp_ajax_hwlAjaxGenerateStyles', 'hwlAjaxGenerateStyles');
314
 
 
 
 
 
 
 
 
 
315
  /**
316
  * Once the stylesheet is generated. We can enqueue it.
317
  */
318
- function hwlEnqueueStylesheet()
319
- {
320
- $stylesheet = WP_CONTENT_DIR . '/local-fonts/local-fonts.css';
321
- if (file_exists($stylesheet)) {
322
- wp_register_style('hwl-style', content_url() . '/local-fonts/local-fonts.css');
323
- wp_enqueue_style('hwl-style');
324
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
325
  }
326
- add_action('wp_enqueue_scripts', 'hwlEnqueueStylesheet' );
2
  /**
3
  * Plugin Name: CAOS for Webfonts
4
  * Plugin URI: https://dev.daanvandenbergh.com/wordpress-plugins/host-google-fonts-locally
5
+ * Description: Automagically save the fonts you want to use inside your content-folder, generate a
6
+ * stylesheet for them and enqueue it in your theme's header.
7
+ * Version: 1.5.7
8
  * Author: Daan van den Bergh
9
  * Author URI: https://dev.daanvandenbergh.com
10
  * License: GPL2v2 or later
11
  */
12
 
13
  // Exit if accessed directly
14
+ if (!defined('ABSPATH')) {
15
+ exit;
16
+ }
17
+
18
+ global $wpdb;
19
+
20
+ /**
21
+ * Define constants.
22
+ */
23
+ define('CAOS_WEBFONTS_DB_VERSION', '1.5.0');
24
+ define('CAOS_WEBFONTS_STATIC_VERSION', '1.5.7');
25
+ define('CAOS_WEBFONTS_DB_TABLENAME', $wpdb->prefix . 'caos_webfonts');
26
+ define('CAOS_WEBFONTS_DB_CHARSET', $wpdb->get_charset_collate());
27
+ define('CAOS_WEBFONTS_FILENAME', 'fonts.css');
28
+ define('CAOS_WEBFONTS_CACHE_DIR', esc_attr(get_option('caos_webfonts_cache_dir')) ?: '/cache/caos-webfonts');
29
+ define('CAOS_WEBFONTS_CURRENT_BLOG_ID', get_current_blog_id());
30
+ define('CAOS_WEBFONTS_UPLOAD_DIR', WP_CONTENT_DIR . CAOS_WEBFONTS_CACHE_DIR);
31
+ define('CAOS_WEBFONTS_UPLOAD_URL', get_site_url(CAOS_WEBFONTS_CURRENT_BLOG_ID, hwlGetContentDirName() . CAOS_WEBFONTS_CACHE_DIR));
32
+ define('CAOS_WEBFONTS_DISPLAY_OPTION', esc_attr(get_option('caos_webfonts_display_option')) ?: 'auto');
33
+
34
+ function hwlRegisterSettings() {
35
+ register_setting('caos-webfonts-basic-settings',
36
+ 'caos_webfonts_cache_dir'
37
+ );
38
+ register_setting('caos-webfonts-basic-settings',
39
+ 'caos_webfonts_display_option'
40
+ );
41
+ }
42
 
43
  /**
44
+ * Create the Admin menu-item
45
+ */
46
+ function hwlCreateMenu() {
47
+ add_options_page(
48
+ 'CAOS for Webfonts',
49
+ 'Optimize Webfonts',
50
+ 'manage_options',
51
+ 'optimize-webfonts',
52
+ 'hwlSettingsPage'
53
+ );
54
+ add_action('admin_init',
55
+ 'hwlRegisterSettings'
56
+ );
57
+ }
58
+ add_action('admin_menu', 'hwlCreateMenu');
59
+
60
+ /**
61
+ * Returns the configured name of WordPress' content directory.
62
  *
63
+ * @return mixed
 
64
  */
65
+ function hwlGetContentDirName() {
66
+ preg_match('/[^\/]+$/u', WP_CONTENT_DIR, $match);
67
+
68
+ return $match[0];
69
+ }
70
 
71
+ /**
72
+ * Create table to store downloaded fonts in.
73
+ */
74
+ function hwlCreateTable() {
75
+ $sql = "CREATE TABLE " . CAOS_WEBFONTS_DB_TABLENAME . "(
76
+ font_id varchar(191) NOT NULL,
77
+ font_family varchar(191) NOT NULL,
78
+ font_weight mediumint(5) NOT NULL,
79
+ font_style varchar(191) NOT NULL,
80
+ downloaded tinyint(1) DEFAULT 0,
81
+ url_ttf varchar(191) NULL,
82
+ url_woff varchar(191) NULL,
83
+ url_woff2 varchar(191) NULL,
84
+ url_eot varchar(191) NULL,
85
+ UNIQUE KEY (font_id)
86
+ ) " . CAOS_WEBFONTS_DB_CHARSET . ";";
87
 
88
+ require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
89
+ dbDelta($sql);
90
+
91
+ add_option('caos_webfonts_db_version', CAOS_WEBFONTS_DB_VERSION);
92
  }
 
93
 
94
  /**
95
+ * Check current version and execute required db updates.
96
  */
97
+ function hwlRunDbUpdates() {
98
+ $currentVersion = get_site_option('caos_webfonts_db_version');
99
+ if (version_compare($currentVersion, CAOS_WEBFONTS_DB_VERSION) < 0) {
100
+ hwlCreateTable();
101
+ }
102
+
103
+ // We can trigger update scripts in the future here.
 
 
104
  }
105
+ add_action('plugins_loaded', 'hwlRunDbUpdates');
106
 
107
+ /**
108
+ * @param $links
109
+ *
110
+ * @return mixed
111
+ */
112
+ function hwlSettingsLink($links) {
113
+ $adminUrl = admin_url() . 'options-general.php?page=optimize-webfonts';
114
+ $settingsLink = "<a href='$adminUrl'>" . __('Settings') . "</a>";
115
+ array_push($links, $settingsLink);
116
+
117
+ return $links;
118
+ }
119
+ $caosLink = plugin_basename(__FILE__);
120
+
121
+ add_filter("plugin_action_links_$caosLink", 'hwlSettingsLink');
122
 
123
  /**
124
  * Render the settings page.
125
  */
126
+ function hwlSettingsPage() {
127
+ if (!current_user_can('manage_options')) {
128
+ wp_die(__("You're not cool enough to access this page."));
129
+ }
130
+ ?>
 
 
131
  <div class="wrap">
132
  <h1><?php _e('CAOS for Webfonts', 'host-webfonts-local'); ?></h1>
133
  <p>
134
+ <?php _e('Developed by: ', 'host-webfonts-local'); ?>
135
+ <a title="Buy me a beer!" href="https://dev.daanvandenbergh.com/donate/">
136
  Daan van den Bergh</a>.
137
  </p>
138
+
139
  <div id="hwl-admin-notices"></div>
140
+
141
+ <?php require_once(plugin_dir_path(__FILE__) . 'includes/welcome-panel.php'); ?>
142
+
143
+ <form id="hwl-options-form" name="hwl-options-form" method="post" action="options.php" style="float: left; width: 60%;">
144
+ <div class="">
145
+ <h3><?php _e('Generate Stylesheet'); ?></h3>
146
+ <?php
147
+
148
+ include(plugin_dir_path(__FILE__) . 'includes/caos-webfonts-style-generation.php');
149
+
150
+ ?>
151
+ </div>
152
+ </form>
153
+
154
+ <form id="hwl-settings-form" name="hwl-settings-form" method="post" action="options.php" style="float: left; width: 39%;">
155
+ <?php
156
+ settings_fields('caos-webfonts-basic-settings');
157
+ do_settings_sections('caos-webfonts-basic-settings');
158
+
159
+ include(plugin_dir_path(__FILE__) . 'includes/caos-webfonts-basic-settings.php');
160
+
161
+ do_action('hwl_after_settings_form_settings');
162
+
163
+ submit_button();
164
+ ?>
165
  </form>
166
  </div>
167
+ <?php
168
  }
169
 
170
  /**
171
+ * @return array|\Exception
172
  */
173
+ function hwlGetTotalFonts() {
174
+ global $wpdb;
175
+
176
+ try {
177
+ return $wpdb->get_results("SELECT * FROM " . CAOS_WEBFONTS_DB_TABLENAME);
178
+ } catch (\Exception $e) {
179
+ return $e;
180
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181
  }
182
 
183
  /**
184
+ * @return array|\Exception
185
  */
186
+ function hwlGetDownloadedFonts() {
187
+ global $wpdb;
188
+
189
+ try {
190
+ return $wpdb->get_results("SELECT * FROM " . CAOS_WEBFONTS_DB_TABLENAME . " WHERE downloaded = 1");
191
+ } catch (\Exception $e) {
192
+ return $e;
193
+ }
 
194
  }
 
195
 
196
+ /**
197
+ * @return array
198
+ */
199
+ function hwlGetDownloadStatus() {
200
+ return array(
201
+ "downloaded" => count(hwlGetDownloadedFonts()),
202
+ "total" => count(hwlGetTotalFonts())
203
+ );
204
+ }
205
 
206
  /**
207
+ * @return \Exception|false|int
208
  */
209
+ function hwlCleanQueue() {
210
+ global $wpdb;
211
+
212
+ try {
213
+ return $wpdb->query("TRUNCATE TABLE " . CAOS_WEBFONTS_DB_TABLENAME);
214
+ } catch (\Exception $e) {
215
+ return $e;
216
+ }
217
+ }
218
+
219
+ /**
220
+ * AJAX-wrapper for hwlGetDownloadStatus()
221
+ */
222
+ function hwlAjaxGetDownloadStatus() {
223
+ return wp_die(
224
+ json_encode(
225
+ hwlGetDownloadStatus()
226
+ )
227
+ );
228
+ }
229
+ add_action('wp_ajax_hwlAjaxGetDownloadStatus', 'hwlAjaxGetDownloadStatus');
230
+
231
+ /**
232
+ * AJAX-wrapper for hwlCleanQueue()
233
+ */
234
+ function hwlAjaxCleanQueue() {
235
+ return wp_die(hwlCleanQueue());
236
+ }
237
+ add_action('wp_ajax_hwlAjaxCleanQueue', 'hwlAjaxCleanQueue');
238
+
239
+ /**
240
+ * AJAX-wrapper for hwlEmptyDir()
241
+ *
242
+ * @return array
243
+ */
244
+ function hwlAjaxEmptyDir() {
245
+ return array_map('unlink', array_filter((array) glob(CAOS_WEBFONTS_UPLOAD_DIR . '/*')));
246
+ }
247
+ add_action('wp_ajax_hwlAjaxEmptyDir', 'hwlAjaxEmptyDir');
248
+
249
+ /**
250
+ * Search Fonts in Google Webfonts Helper
251
+ */
252
+ function hwlAjaxSearchGoogleFonts() {
253
+ try {
254
+ $request = curl_init();
255
+ $searchQuery = sanitize_text_field($_POST['search_query']);
256
+
257
+ curl_setopt($request, CURLOPT_URL, 'https://google-webfonts-helper.herokuapp.com/api/fonts/' . $searchQuery);
258
+ curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
259
+
260
+ $result = curl_exec($request);
261
+
262
+ curl_close($request);
263
+ wp_die($result);
264
+ } catch (\Exception $e) {
265
+ wp_die($e);
266
+ }
267
  }
268
+ add_action('wp_ajax_hwlAjaxSearchGoogleFonts', 'hwlAjaxSearchGoogleFonts');
269
 
270
  /**
271
+ * Create cache dir upon plugin (re-)activation.
272
+ */
273
+ function hwlCreateCacheDir() {
274
+ $uploadDir = CAOS_WEBFONTS_UPLOAD_DIR;
275
+ if (!is_dir($uploadDir)) {
276
+ wp_mkdir_p($uploadDir);
277
+ }
278
+ }
279
+ register_activation_hook(__FILE__, 'hwlCreateCacheDir');
280
+
281
+ /**
282
+ * @return array
283
+ */
284
+ function hwlFontDisplayOptions() {
285
+ $fontDisplay = array(
286
+ 'Auto (default)' => 'auto',
287
+ 'Block' => 'block',
288
+ 'Swap' => 'swap',
289
+ 'Fallback' => 'fallback',
290
+ 'Optional' => 'optional'
291
+ );
292
+
293
+ return $fontDisplay;
294
+ }
295
+
296
+ /**
297
+ * The function for generating the stylesheet and saving it to the upload-dir.
298
  */
299
  function hwlAjaxGenerateStyles() {
300
+ require_once(plugin_dir_path(__FILE__) . 'includes/ajax/generate-stylesheet.php');
301
  }
302
  add_action('wp_ajax_hwlAjaxGenerateStyles', 'hwlAjaxGenerateStyles');
303
 
304
+ /**
305
+ * Saves the chosen webfonts to the database for further processing.
306
+ */
307
+ function hwlAjaxDownloadFonts() {
308
+ require_once(plugin_dir_path(__FILE__) . 'includes/ajax/download-fonts.php');
309
+ }
310
+ add_action('wp_ajax_hwlAjaxDownloadFonts', 'hwlAjaxDownloadFonts');
311
+
312
  /**
313
  * Once the stylesheet is generated. We can enqueue it.
314
  */
315
+ function hwlEnqueueStylesheet() {
316
+ $stylesheet = CAOS_WEBFONTS_UPLOAD_DIR . '/' . CAOS_WEBFONTS_FILENAME;
317
+ if (file_exists($stylesheet)) {
318
+ wp_register_style('hwl-style', CAOS_WEBFONTS_UPLOAD_URL . '/' . CAOS_WEBFONTS_FILENAME);
319
+ wp_enqueue_style('hwl-style');
320
+ }
321
+ }
322
+ add_action('wp_enqueue_scripts', 'hwlEnqueueStylesheet');
323
+
324
+ /**
325
+ * Stylesheet and Javascript needed in Admin
326
+ */
327
+ function hwlEnqueueAdminJs($hook) {
328
+ if ($hook == 'settings_page_optimize-webfonts') {
329
+ wp_enqueue_script('hwl-admin-js', plugin_dir_url(__FILE__) . 'js/hwl-admin.js', array('jquery'), CAOS_WEBFONTS_STATIC_VERSION, true);
330
+ wp_enqueue_style('hwl-admin.css', plugin_dir_url(__FILE__) . 'css/hwl-admin.css', array(), CAOS_WEBFONTS_STATIC_VERSION);
331
+ }
332
+ }
333
+ add_action('admin_enqueue_scripts', 'hwlEnqueueAdminJs');
334
+
335
+ /**
336
+ * When plugin is deactivated. Remove all CSS and JS.
337
+ */
338
+ function hwlDequeueJsCss() {
339
+ wp_dequeue_script('hwl-admin-js');
340
+ wp_dequeue_style('hwl-admin.css');
341
+ wp_dequeue_style('hwl-style');
342
  }
343
+ register_deactivation_hook(__FILE__, 'hwlDequeueJsCss');
includes/ajax/download-fonts.php ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @package: CAOS for Webfonts
4
+ * @author: Daan van den Bergh
5
+ * @copyright: (c) 2019 Daan van den Bergh
6
+ * @url: https://dev.daanvandenbergh.com
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['selected_fonts'][0]['caos_webfonts_array'];
43
+
44
+ if (!$selectedFonts)
45
+ {
46
+ wp_die(__('No fonts found.', 'host-webfonts-local'));
47
+ }
48
+
49
+ /**
50
+ * Save used fonts to database.
51
+ */
52
+ foreach ($selectedFonts as $id => $font)
53
+ {
54
+ $wpdb->insert(
55
+ CAOS_WEBFONTS_DB_TABLENAME,
56
+ array(
57
+ 'font_id' => sanitize_text_field($id),
58
+ 'font_family' => sanitize_text_field($font['font-family']),
59
+ 'font_weight' => sanitize_text_field($font['font-weight']),
60
+ 'font_style' => sanitize_text_field($font['font-style']),
61
+ 'downloaded' => 0,
62
+ 'url_ttf' => esc_url_raw($font['url']['ttf']),
63
+ 'url_woff' => esc_url_raw($font['url']['woff']),
64
+ 'url_woff2' => esc_url_raw($font['url']['woff2']),
65
+ 'url_eot' => esc_url_raw($font['url']['eot'])
66
+ )
67
+ );
68
+ }
69
+
70
+ /**
71
+ * Loaded fonts from database
72
+ */
73
+ $selectedFonts = hwlGetTotalFonts();
74
+
75
+ /**
76
+ * Download the fonts.
77
+ */
78
+ foreach ($selectedFonts as $id => $font) {
79
+ // If font is marked as downloaded. Skip it.
80
+ if ($font->downloaded) {
81
+ continue;
82
+ }
83
+
84
+ $urls['url_ttf'] = $font->url_ttf;
85
+ $urls['url_woff'] = $font->url_woff;
86
+ $urls['url_woff2'] = $font->url_woff2;
87
+ $urls['url_eot'] = $font->url_eot;
88
+
89
+ foreach ($urls as $type => $url) {
90
+ $remoteFile = esc_url_raw($url);
91
+ $filename = basename($remoteFile);
92
+ $localFile = CAOS_WEBFONTS_UPLOAD_DIR . '/' . $filename;
93
+
94
+ try {
95
+ $fileWritten = file_put_contents($localFile, file_get_contents($remoteFile));
96
+ } catch (\Exception $e) {
97
+ wp_die(__("File ($remoteFile) could not be downloaded: $e"));
98
+ }
99
+
100
+ /**
101
+ * If file is written, change the external URL to the local URL in the POST data.
102
+ * If it fails, we can still fall back to the external URL and nothing breaks.
103
+ */
104
+ if($fileWritten) {
105
+ $localFileUrl = CAOS_WEBFONTS_UPLOAD_URL . '/' . $filename;
106
+ $wpdb->update(
107
+ CAOS_WEBFONTS_DB_TABLENAME,
108
+ array(
109
+ $type => $localFileUrl
110
+ ),
111
+ array(
112
+ 'font_id' => $font->font_id
113
+ )
114
+ );
115
+ }
116
+ }
117
+
118
+ /**
119
+ * After all files are downloaded, set the 'downloaded'-field to 1.
120
+ */
121
+ $wpdb->update(
122
+ CAOS_WEBFONTS_DB_TABLENAME,
123
+ array(
124
+ 'downloaded' => 1
125
+ ),
126
+ array(
127
+ 'font_id' => $font->font_id
128
+ )
129
+ );
130
+ }
131
+
132
+ wp_die(__('Fonts saved. You can now generate the stylesheet.', 'host-webfonts-local'));
includes/ajax/generate-stylesheet.php ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @package: CAOS for Webfonts
4
+ * @author: Daan van den Bergh
5
+ * @copyright: (c) 2019 Daan van den Bergh
6
+ * @url: https://dev.daanvandenbergh.com
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 CAOS for Webfonts
30
+ *
31
+ * @author: Daan van den Bergh
32
+ * @copyright: (c) 2019 Daan van den Bergh
33
+ * @url: https://dev.daanvandenbergh.com
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
+
54
+ $fonts[] =
55
+ "@font-face {
56
+ font-family: '$fontFamily';
57
+ font-display: $fontDisplay;
58
+ font-style: $fontStyle;
59
+ font-weight: $fontWeight;
60
+ src: url('$fontUrlWoffTwo') format('woff2'), /* Super Modern Browsers */
61
+ url('$fontUrlWoff') format('woff'), /* Modern Browsers */
62
+ url('$fontUrlTtf') format('truetype'), /* Safari, Android, iOS */
63
+ url('$fontUrlEot'); /* IE9 Compatible */
64
+ }";
65
+ }
66
+
67
+ $fonts = implode("\n", $fonts);
68
+ $file = CAOS_WEBFONTS_UPLOAD_DIR . '/' . CAOS_WEBFONTS_FILENAME;
69
+
70
+ /**
71
+ * If the file can be created and uploaded. Let's try to write it.
72
+ */
73
+ try {
74
+ $stylesheet = fopen($file, 'w') or die ("Cannot create file {$file}");
75
+ fwrite ($stylesheet, $fonts);
76
+ fclose ($stylesheet);
77
+ wp_die(__('Stylesheet was successfully generated and added to your theme\'s header.'));
78
+ } catch (Exception $e) {
79
+ wp_die(__("Stylesheet could not be generated: $e"));
80
+ }
includes/caos-webfonts-basic-settings.php ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @package: CAOS for Webfonts
4
+ * @author: Daan van den Bergh
5
+ * @copyright: (c) 2019 Daan van den Bergh
6
+ * @url: https://dev.daanvandenbergh.com
7
+ */
8
+
9
+ // Exit if accessed directly
10
+ if (!defined( 'ABSPATH')) exit;
11
+ ?>
12
+ <div class="">
13
+ <h3><?php _e('Basic Settings'); ?></h3>
14
+ <table class="form-table">
15
+ <tr valign="top">
16
+ <th scope="row"><?php _e('Save webfonts to...', 'host-webfonts-local'); ?></th>
17
+ <td>
18
+ <input class="caos_webfonts_cache_dir" type="text" name="caos_webfonts_cache_dir" placeholder="e.g. /cache/caos-webfonts" value="<?php echo CAOS_WEBFONTS_CACHE_DIR; ?>" />
19
+ <p class="description">
20
+ <?php _e("Changes the path where webfonts are cached inside WordPress' content directory (usually <code>wp-content</code>). Defaults to <code>/cache/caos-webfonts</code>.", 'host-webfonts-local'); ?>
21
+ </p>
22
+ </td>
23
+ </tr>
24
+ <tr valign="top">
25
+ <th scope="row"><?php _e('Font-display option', 'host-webfonts-local'); ?></th>
26
+ <td>
27
+ <?php $fontDisplay = hwlFontDisplayOptions(); ?>
28
+ <label>
29
+ <select name="caos_webfonts_display_option">
30
+ <?php foreach ($fontDisplay as $label => $value): ?>
31
+ <option value="<?php echo $value; ?>" <?php echo $value == CAOS_WEBFONTS_DISPLAY_OPTION ? 'selected' : ''; ?>><?php _e($label, 'host-webfonts-local'); ?></option>
32
+ <?php endforeach; ?>
33
+ </select>
34
+ </label>
35
+ <br/>
36
+ <p class="description">
37
+ <?php _e('Select which font-display strategy to use. Defaults to \'Auto\'.', 'host-webfonts-local'); ?>
38
+ <a target="_blank" href="https://developers.google.com/web/updates/2016/02/font-display"><?php _e('Read more', 'host-webfonts-local'); ?></a>
39
+ </p>
40
+ </td>
41
+ </tr>
42
+ </table>
43
+ </div>
includes/caos-webfonts-style-generation.php ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @package: CAOS for Webfonts
4
+ * @author: Daan van den Bergh
5
+ * @copyright: (c) 2019 Daan van den Bergh
6
+ * @url: https://dev.daanvandenbergh.com
7
+ */
8
+
9
+ // Exit if accessed directly
10
+ if (!defined( 'ABSPATH')) exit;
11
+ ?>
12
+ <table>
13
+ <tbody>
14
+ <tr valign="top">
15
+ <td colspan="2">
16
+ <input type="text" name="search-field"
17
+ id="search-field" class="form-input-tip ui-autocomplete-input" placeholder="Search fonts..." />
18
+ </td>
19
+ </tr>
20
+ </tbody>
21
+ <tr valign="top">
22
+ <th>
23
+ font-family
24
+ </th>
25
+ <th>
26
+ font-style
27
+ </th>
28
+ <th>
29
+ font-weight
30
+ </th>
31
+ <th>
32
+
33
+ </th>
34
+ </tr>
35
+ <tbody id="hwl-results">
36
+ <?php
37
+ $savedFonts = hwlGetTotalFonts();
38
+ ?>
39
+ <?php if ($savedFonts): ?>
40
+ <?php foreach ($savedFonts as $font): ?>
41
+ <?php
42
+ $fontId = $font->font_id;
43
+ $arrayPath = "caos_webfonts_array][$fontId]";
44
+ ?>
45
+ <tr id="row-<?php echo $fontId; ?>" valign="top">
46
+ <td>
47
+ <input readonly type="text" value="<?php echo $font->font_family; ?>" name="<?php echo $arrayPath; ?>[font-family]" />
48
+ </td>
49
+ <td>
50
+ <input readonly type="text" value="<?php echo $font->font_style; ?>" name="<?php echo $arrayPath; ?>[font-style]" />
51
+ </td>
52
+ <td>
53
+ <input readonly type="text" value="<?php echo $font->font_weight; ?>" name="<?php echo $arrayPath; ?>[font-weight]" />
54
+ </td>
55
+ <td>
56
+ <input type="hidden" value="<?php echo $fontId; ?>" name="<?php echo $arrayPath; ?>[id]" />
57
+ <input type="hidden" value="<?php echo $font->url_ttf; ?>" name="<?php echo $arrayPath; ?>[url][ttf]" />
58
+ <input type="hidden" value="<?php echo $font->url_woff; ?>" name="<?php echo $arrayPath; ?>[url][woff]" />
59
+ <input type="hidden" value="<?php echo $font->url_woff2; ?>" name="<?php echo $arrayPath; ?>[url][woff2]" />
60
+ <input type="hidden" value="<?php echo $font->url_eot; ?>" name="<?php echo $arrayPath; ?>[url][eot]" />
61
+ <div class="hwl-remove">
62
+ <a onclick="hwlRemoveRow('row-<?php echo $fontId; ?>')"><small>remove</small></a>
63
+ </div>
64
+ </td>
65
+ </tr>
66
+ <?php endforeach; ?>
67
+ <?php endif; ?>
68
+ </tbody>
69
+ <tbody id="hwl-warning">
70
+ <tr class="loading" style="display: none;">
71
+ <td colspan="3" align="center">
72
+ <span class="spinner"></span>
73
+ </td>
74
+ </tr>
75
+ <tr class="error" style="display: none;">
76
+ <td colspan="3" align="center">No fonts available.</td>
77
+ </tr>
78
+ </tbody>
79
+ </table>
80
+
81
+ <table>
82
+ <tbody>
83
+ <tr valign="center" align="center">
84
+ <td>
85
+ <input type="button" onclick="hwlDownloadFonts()" name="save-btn"
86
+ id="save-btn" class="button-primary" value="Download Fonts" />
87
+ </td>
88
+ <td>
89
+ <input type="button" onclick="hwlGenerateStylesheet()" name="generate-btn"
90
+ id="generate-btn" class="button-secondary" value="Generate Stylesheet" />
91
+ </td>
92
+ <td>
93
+ <input type="button" onclick="hwlRegenerateStylesheet()" name="regenerate-btn"
94
+ id="regenerate-btn" class="button-secondary" value="Save & Regenerate" />
95
+ </td>
96
+ <td>
97
+ <a onclick="hwlCleanQueue()" name="clean-btn"
98
+ id="clean-btn" class="button-cancel">Clean Queue</a>
99
+ </td>
100
+ <td>
101
+ <a onclick="hwlEmptyDir()" name="empty-btn"
102
+ id="empty-btn" class="button-cancel">Empty Cache Directory</a>
103
+ </td>
104
+ </tr>
105
+ <tr valign="center">
106
+ <?php
107
+ $downloaded = hwlGetDownloadStatus()['downloaded'];
108
+ $total = hwlGetDownloadStatus()['total'];
109
+ $width = $downloaded && $total ? (100 / $total) * $downloaded : 0;
110
+ ?>
111
+ <td colspan="5">
112
+ <div class="caos-status-total-bar" style="">
113
+ <div id="caos-status-progress-bar" style="width: <?php echo $width; ?>%;">
114
+ <span class="caos-status-progress-percentage"><?php echo $width . '%'; ?></span>
115
+ </div>
116
+ </div>
117
+ </td>
118
+ </tr>
119
+ </tbody>
120
+ </table>
includes/generate-stylesheet.php DELETED
@@ -1,100 +0,0 @@
1
- <?php
2
- /**
3
- * @package: CAOS for Webfonts
4
- * @author: Daan van den Bergh
5
- * @copyright: (c) 2018 Daan van den Bergh
6
- * @url: https://dev.daanvandenbergh.com
7
- */
8
-
9
- // Exit if accessed directly
10
- if (!defined( 'ABSPATH')) exit;
11
-
12
- /** We need the Wordpress-object here. */
13
- global $wpdb;
14
-
15
- /**
16
- * set the content type header
17
- */
18
- header("Content-type: text/css");
19
-
20
- /**
21
- * Detect Font style so we can populate the CSS-file correctly.
22
- *
23
- * @param $fontname
24
- * @return string
25
- */
26
- function hwlDetectFontStyle($fontweight) {
27
- switch ($fontweight){
28
- case 300:
29
- return 'light';
30
- case 400:
31
- return 'normal';
32
- case 600:
33
- return 'semibold';
34
- case 700:
35
- return 'bold';
36
- case 900:
37
- return 'extrabold';
38
- default:
39
- return 'normal';
40
- }
41
- }
42
-
43
- /**
44
- * Return the fonts location if it matches the requested type in the CSS.
45
- *
46
- * @param $font
47
- * @param $requestedType
48
- *
49
- * @return string
50
- */
51
- function hwlReturnFontUrl($font, $requestedType) {
52
- foreach ($font as $fontType => $url) {
53
- if (strpos($fontType, $requestedType) !== false) {
54
- return implode($url);
55
- }
56
- }
57
-
58
- return null;
59
- }
60
-
61
- /**
62
- * Check if user has the needed permissions.
63
- */
64
- if (!current_user_can('manage_options'))
65
- {
66
- wp_die(__("You're not cool enough to access this page."));
67
- }
68
-
69
- /**
70
- * Let's generate the stylesheet.
71
- */
72
- $fonts[] = "/** This file is automagically generated by CAOS for Webfonts */\n";
73
- $uploadedFonts = $_POST['uploaded_fonts'][0]['hwl_uploaded_font'];
74
-
75
- foreach ($uploadedFonts as $font) {
76
- $fonts[] = "@font-face {
77
- font-family: '" . sanitize_text_field($font['font_family']) . "';
78
- font-style: " . hwlDetectFontStyle(sanitize_text_field( $font['font_weight'])) . ";
79
- font-weight: " . sanitize_text_field($font['font_weight']) . ";
80
- src: url('" . hwlReturnFontUrl($font, 'eot') . "'), /* IE9 Compatible */
81
- url('" . hwlReturnFontUrl($font, 'woff2') . "') format('woff2'), /* Super Modern Browsers */
82
- url('" . hwlReturnFontUrl($font, 'woff') . "') format('woff'), /* Modern Browsers */
83
- url('" . hwlReturnFontUrl($font, 'octet') . "') format('truetype'); /* Safari, Android, iOS */
84
- }";
85
- }
86
-
87
- $fonts = implode("\n", $fonts);
88
- $file = WP_CONTENT_DIR . '/local-fonts/local-fonts.css';
89
-
90
- /**
91
- * If the file can be created and uploaded. Let's try to write it.
92
- */
93
- try {
94
- $stylesheet = fopen($file, 'w') or die ("Cannot create file {$file}");
95
- fwrite ($stylesheet, $fonts);
96
- fclose ($stylesheet);
97
- wp_die(_e('Stylesheet was successfully generated and added to your theme\'s header.'));
98
- } catch (Exception $e) {
99
- wp_die($e);
100
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
includes/welcome-panel.php CHANGED
@@ -2,45 +2,54 @@
2
  /**
3
  * @package: CAOS for Webfonts
4
  * @author: Daan van den Bergh
5
- * @copyright: (c) 2018 Daan van den Bergh
6
  * @url: https://dev.daanvandenbergh.com
7
  */
 
 
 
8
  ?>
9
  <div id="welcome-panel" class="welcome-panel">
10
- <div class="welcome-panel-content">
11
- <h2>Thank you for using CAOS for Webfonts!</h2>
12
- <p class="about-description">
13
- CAOS for Webfonts automagically saves the fonts you want to use inside your content-folder, generates a stylesheet for them and enqueues it in your theme's header.
14
  This will decrease your pageload times, leverage your browser cache, minimize external requests and effectively bring you a perfect score on Pagespeed Insights and Pingdom.
15
- </p>
16
- <div class="welcome-panel-column-container">
17
- <div class="welcome-panel-column">
18
- <h3>Quickstart</h3>
19
- <ul>
20
- <li class="welcome-icon dashicons-before dashicons-editor-bold">Download fonts from <a href="https://fonts.google.com/" target="_blank">Google Fonts</a></li>
21
- <li class="welcome-icon dashicons-before dashicons-admin-tools">Convert them using <a href="https://www.fontsquirrel.com/tools/webfont-generator" target="_blank">FontSquirrel's Webfont Generator</a></li>
22
- <li class="welcome-icon dashicons-before dashicons-cloud">Upload/Add them using the 'Select Fonts' button</li>
23
- <li class="welcome-icon dashicons-before dashicons-art">Click 'Generate Stylesheet'</li>
24
- <li class="welcome-icon dashicons-before dashicons-media-text">Stylesheet is added to template and ready for use.</li>
25
- <li class="welcome-icon dashicons-before dashicons-smiley">Done!</li>
26
- </ul>
27
- <p><a target="_blank" href="https://dev.daanvandenbergh.com/wordpress-plugins/host-google-fonts-locally/">Click here</a> for a more comprehensive guide.</p>
28
- </div>
29
- <div class="welcome-panel-column">
30
- <h3>Get a Perfect Score on Pagespeed & Pingdom!</h3>
 
 
31
  <p><strong>Leverage your browser cache</strong> and <strong>lower pageload times</strong> by hosting analytics.js locally with <a href="https://dev.daanvandenbergh.com/wordpress-plugins/optimize-analytics-wordpress/" target="_blank">CAOS for Analytics</a>.</p>
32
  <p><a target="_blank" href="https://wordpress.org/plugins/host-analyticsjs-local">Download now</a></p>
33
- <h3>Need help?</h3>
34
- <p>First make sure you read the manual thoroughly.</p>
35
- <p>Still running into issues? Visit the <a href="https://wordpress.org/plugins/host-webfonts-local/#description">FAQ</a> and <a href="https://wordpress.org/plugins/host-webfonts-local">Support Forum</a> to see if your question has already been answered.</p>
36
  <p>If not, ask a question or <a href="https://dev.daanvandenbergh.com/contact" target="_blank">contact me</a>.</p>
37
- </div>
38
- <div class="welcome-panel-column welcome-panel-last">
39
- <h3>Did I make you happy?</h3>
40
- <p>I create simple, but useful solutions that easily optimize your website for Pagespeed Insights and Pingdom.</p>
41
- <p>I believe these solutions should be free, but if you appreciate my work, let's have a beer together!</p>
42
- <a target="_blank" class="button button-primary button-hero" href="https://dev.daanvandenbergh.com/donate/"><span class="dashicons-before dashicons-heart"> Sure. This round's on me!</span></a>
43
- </div>
44
- </div>
45
- </div>
 
 
 
 
46
  </div>
2
  /**
3
  * @package: CAOS for Webfonts
4
  * @author: Daan van den Bergh
5
+ * @copyright: (c) 2019 Daan van den Bergh
6
  * @url: https://dev.daanvandenbergh.com
7
  */
8
+
9
+ // Exit if accessed directly
10
+ if (!defined('ABSPATH')) exit;
11
  ?>
12
  <div id="welcome-panel" class="welcome-panel">
13
+ <div class="welcome-panel-content">
14
+ <h2>Thank you for using CAOS for Webfonts!</h2>
15
+ <p class="about-description">
16
+ CAOS for Webfonts automagically downloads and saves the fonts you want to use inside Wordpress' content-folder, generates a stylesheet for them and enqueues it in your theme's header.
17
  This will decrease your pageload times, leverage your browser cache, minimize external requests and effectively bring you a perfect score on Pagespeed Insights and Pingdom.
18
+ </p>
19
+ <div class="welcome-panel-column-container">
20
+ <div class="welcome-panel-column">
21
+ <h3>Quickstart</h3>
22
+ <ul>
23
+ <li class="welcome-icon dashicons-before dashicons-editor-bold">For all available fonts, check out <a href="https://fonts.google.com/" target="_blank">Google Fonts</a></li>
24
+ <li class="welcome-icon dashicons-before dashicons-cloud">Search for your fonts by entering the name in the searchbar and repeat this for as many fonts you like!</li>
25
+ <li class="welcome-icon dashicons-before dashicons-admin-tools">Modify the list by removing the fonts you don't need/want</li>
26
+ <li class="welcome-icon dashicons-before dashicons-update">Click 'Save Fonts'</li>
27
+ <li class="welcome-icon dashicons-before dashicons-art">Click 'Generate Stylesheet'</li>
28
+ <li class="welcome-icon dashicons-before dashicons-media-text">Fonts are being downloaded and stylesheet is generated</li>
29
+ <li class="welcome-icon dashicons-before dashicons-smiley">Done!</li>
30
+ <li class="welcome-icon dashicons-before dashicons-admin-settings">After changing settings use 'Save & Regenerate'</li>
31
+ </ul>
32
+ <p><a target="_blank" href="https://dev.daanvandenbergh.com/wordpress/host-google-fonts-locally/">Click here</a> for a more comprehensive guide.</p>
33
+ </div>
34
+ <div class="welcome-panel-column">
35
+ <h3>Get a Perfect Score on Pagespeed & Pingdom!</h3>
36
  <p><strong>Leverage your browser cache</strong> and <strong>lower pageload times</strong> by hosting analytics.js locally with <a href="https://dev.daanvandenbergh.com/wordpress-plugins/optimize-analytics-wordpress/" target="_blank">CAOS for Analytics</a>.</p>
37
  <p><a target="_blank" href="https://wordpress.org/plugins/host-analyticsjs-local">Download now</a></p>
38
+ <h3>Need help?</h3>
39
+ <p>First make sure you read the manual thoroughly.</p>
40
+ <p>Still running into issues? Visit the <a href="https://wordpress.org/plugins/host-webfonts-local/#description">FAQ</a> and <a href="https://wordpress.org/support/plugin/host-webfonts-local/">Support Forum</a> to see if your question has already been answered.</p>
41
  <p>If not, ask a question or <a href="https://dev.daanvandenbergh.com/contact" target="_blank">contact me</a>.</p>
42
+ </div>
43
+ <div class="welcome-panel-column welcome-panel-last">
44
+ <h3>Want to Host other Files Locally?</h3>
45
+ <p>Unleash your site's true potential by locally hosting as many files as possible.</p>
46
+ <p><a target="_blank" href="https://dev.daanvandenbergh.com/how-to/host-locally-crontab/">Read more</a></p>
47
+ <h3>Support CAOS!</h3>
48
+ <p>I create solutions that easily optimize your website for Pagespeed Insights and Pingdom.</p>
49
+ <p>I believe these solutions should be free, but if you appreciate my work, please consider donating!</p>
50
+ <a target="_blank" class="button button-primary button-hero" href="https://dev.daanvandenbergh.com/donate/"><span class="dashicons-before dashicons-heart"> Donate</span></a>
51
+ <a target="_blank" class="button button-primary button-hero" style="background-color: #00aced; box-shadow: 0 2px 0 #0084b4; border-color: #0084b4 #0084b4 #0084b4;" href="https://twitter.com/Dan0sz"><span class="dashicons-before dashicons-twitter"> Follow me!</span></a>
52
+ </div>
53
+ </div>
54
+ </div>
55
  </div>
js/hwl-admin.js ADDED
@@ -0,0 +1,288 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * @package: CAOS for Webfonts
3
+ * @author: Daan van den Bergh
4
+ * @copyright: (c) 2019 Daan van den Bergh
5
+ * @url: https://dev.daanvandenbergh.com
6
+ */
7
+ /**
8
+ * Timer which triggers search after waiting for user to finish typing.
9
+ */
10
+ var typingTimer
11
+ var doneTypingInterval = 300
12
+ var $input = jQuery('#search-field')
13
+ // on keyup, start the countdown
14
+ $input.on('keyup', function() {
15
+ clearTimeout(typingTimer)
16
+ typingTimer = setTimeout(doneTyping, doneTypingInterval)
17
+ })
18
+ // on keydown, clear the countdown
19
+ $input.on('keydown', function() {
20
+ clearTimeout(typingTimer)
21
+ })
22
+
23
+ /**
24
+ * When user is done typing, trigger search.
25
+ */
26
+ function doneTyping()
27
+ {
28
+ query = $input.val().replace(/\s/g, '-').toLowerCase()
29
+ console.log(query)
30
+ hwlSearchGoogleFonts(query)
31
+ }
32
+
33
+ /**
34
+ * Triggers the AJAX-request to Google Webfont Helper.
35
+ * @param $data
36
+ */
37
+ function hwlSearchGoogleFonts($data)
38
+ {
39
+ var loadingDiv = jQuery('#hwl-warning .loading')
40
+ var errorDiv = jQuery('#hwl-warning .error')
41
+ jQuery.ajax({
42
+ type: 'POST',
43
+ url: ajaxurl,
44
+ data: {
45
+ action: 'hwlAjaxSearchGoogleFonts',
46
+ search_query: $data
47
+ },
48
+ dataType: 'json',
49
+ beforeSend: function() {
50
+ loadingDiv.show()
51
+ },
52
+ error: function() {
53
+ errorDiv.show()
54
+ },
55
+ complete: function(response) {
56
+ loadingDiv.hide()
57
+ errorDiv.hide()
58
+ if(response['responseText'] !== 'Not found') {
59
+ hwlGenerateResults(response)
60
+ } else {
61
+ errorDiv.show()
62
+ }
63
+ }
64
+ })
65
+ }
66
+
67
+ /**
68
+ * Displays the search results
69
+ *
70
+ * @param results
71
+ */
72
+ function hwlGenerateResults(results)
73
+ {
74
+ var response = JSON.parse(results['responseText'])
75
+ var variants = response['variants']
76
+ var length = variants.length
77
+ var renderedFonts = []
78
+ for(var iii = 0; iii < length; iii++) {
79
+ var fontFamily = variants[iii].fontFamily.replace(/'/g, '')
80
+ var fontId = variants[iii].id
81
+ var font = fontFamily.replace(/\s+/g, '-').toLowerCase() + '-' + variants[iii].id
82
+ var fontWeight = variants[iii].fontWeight
83
+ var fontStyle = variants[iii].fontStyle
84
+ renderedFonts[iii] = `<tr id="row-${font}" valign="top">
85
+ <td>
86
+ <input readonly type="text" value="${fontFamily}" name="caos_webfonts_array][${font}][font-family]" />
87
+ </td>
88
+ <td>
89
+ <input readonly type="text" value="${fontStyle}" name="caos_webfonts_array][${font}][font-style]" />
90
+ </td>
91
+ <td>
92
+ <input readonly type="text" value="${fontWeight}" name="caos_webfonts_array][${font}][font-weight]" />
93
+ </td>
94
+ <td>
95
+ <input type="hidden" value="${fontId}" name="caos_webfonts_array][${font}][id]" />
96
+ <input type="hidden" value="${variants[iii].ttf}" name="caos_webfonts_array][${font}][url][ttf]" />
97
+ <input type="hidden" value="${variants[iii].woff}" name="caos_webfonts_array][${font}][url][woff]" />
98
+ <input type="hidden" value="${variants[iii].woff2}" name="caos_webfonts_array][${font}][url][woff2]" />
99
+ <input type="hidden" value="${variants[iii].eot}" name="caos_webfonts_array][${font}][url][eot]" />
100
+ <div class="hwl-remove">
101
+ <a onclick="hwlRemoveRow('row-${font}')"><small>remove</small></a>
102
+ </div>
103
+ </td>
104
+ </tr>`
105
+ }
106
+ jQuery('#hwl-results').append(renderedFonts)
107
+ }
108
+
109
+ /**
110
+ * Call the generate-stylesheet script.
111
+ */
112
+ function hwlGenerateStylesheet()
113
+ {
114
+ var hwlData = hwlSerializeArray(jQuery('#hwl-options-form'))
115
+ jQuery.ajax({
116
+ type: 'POST',
117
+ url: ajaxurl,
118
+ data: {
119
+ action: 'hwlAjaxGenerateStyles',
120
+ selected_fonts: hwlData
121
+ },
122
+ success: function(response) {
123
+ jQuery('#hwl-admin-notices').append(
124
+ `<div class="updated settings-success notice is-dismissible">
125
+ <p>${response}</p>
126
+ </div>`
127
+ )
128
+ hwlScrollTop()
129
+ },
130
+ error: function(response) {
131
+ jQuery('#hwl-admin-notices').append(
132
+ `<div class="notice notice-error is-dismissible">
133
+ <p>The stylesheet could not be created: ${response}</p>
134
+ </div>`
135
+ )
136
+ hwlScrollTop()
137
+ }
138
+ })
139
+ }
140
+
141
+ /**
142
+ * Triggered when 'Save Webfonts' is clicked.
143
+ */
144
+ function hwlDownloadFonts()
145
+ {
146
+ var downloadStatus = window.setInterval(hwlGetDownloadStatus, 1000);
147
+ var hwlData = hwlSerializeArray(jQuery('#hwl-options-form'));
148
+ jQuery.ajax({
149
+ type: 'POST',
150
+ url: ajaxurl,
151
+ data: {
152
+ action: 'hwlAjaxDownloadFonts',
153
+ selected_fonts: hwlData
154
+ },
155
+ success: function(response) {
156
+ jQuery('#hwl-admin-notices').append(
157
+ `<div class="notice notice-success is-dismissible">
158
+ <p>${response}</p>
159
+ </div>`
160
+ )
161
+ hwlScrollTop();
162
+ window.clearInterval(downloadStatus);
163
+ }
164
+ })
165
+ }
166
+
167
+ /**
168
+ * Gets a JSON object with the download progress information
169
+ */
170
+ function hwlGetDownloadStatus()
171
+ {
172
+ jQuery.ajax({
173
+ type: 'POST',
174
+ url: ajaxurl,
175
+ data: {
176
+ action: 'hwlAjaxGetDownloadStatus'
177
+ },
178
+ dataType: 'text json',
179
+ success: function(response) {
180
+ downloaded = response.downloaded;
181
+ total = response.total;
182
+ progress = (100 / total) * downloaded;
183
+ console.log("[" + progress + "] Downloaded " + downloaded + " of " + total + ".")
184
+ hwlUpdateStatusBar(progress);
185
+ }
186
+ })
187
+ }
188
+
189
+ /**
190
+ * Updated Status-bar with the set progress
191
+ *
192
+ * @param progress
193
+ */
194
+ function hwlUpdateStatusBar(progress)
195
+ {
196
+ progress = Math.round(progress) + '%';
197
+ jQuery('#caos-status-progress-bar').width(progress);
198
+ jQuery('.caos-status-progress-percentage').html(progress);
199
+ }
200
+
201
+ /**
202
+ * Remove all files within the configured cache dir.
203
+ */
204
+ function hwlEmptyDir()
205
+ {
206
+ jQuery.ajax({
207
+ type: 'POST',
208
+ url: ajaxurl,
209
+ data: {
210
+ action: 'hwlAjaxEmptyDir'
211
+ },
212
+ success: function() {
213
+ jQuery('#hwl-admin-notices').append(
214
+ `<div class="notice notice-success is-dismissible">
215
+ <p>Cache-dir emptied.</p>
216
+ </div>`
217
+ )
218
+ hwlCleanQueue()
219
+ hwlUpdateStatusBar(0)
220
+ hwlScrollTop()
221
+ }
222
+ })
223
+ }
224
+
225
+ /**
226
+ * Trigger the DB clean-up and clean list.
227
+ */
228
+ function hwlCleanQueue()
229
+ {
230
+ jQuery.ajax({
231
+ type: 'POST',
232
+ url: ajaxurl,
233
+ data: {
234
+ action: 'hwlAjaxCleanQueue'
235
+ },
236
+ success: function() {
237
+ jQuery('.caos-status-progress-percentage').html('0%')
238
+ jQuery('#hwl-results').empty()
239
+ }
240
+ })
241
+ }
242
+
243
+ /**
244
+ * After settings have changed, trigger this.
245
+ */
246
+ function hwlRegenerateStylesheet()
247
+ {
248
+ hwlDownloadFonts()
249
+ hwlGenerateStylesheet()
250
+ }
251
+
252
+ /**
253
+ * Scroll to top-effect.
254
+ */
255
+ function hwlScrollTop()
256
+ {
257
+ setTimeout(function () {
258
+ jQuery('html, body').animate({
259
+ scrollTop: 0
260
+ }, 1500)
261
+ }, 1500)
262
+ }
263
+
264
+ /**
265
+ * Serialize form data to a multi-dimensional array.
266
+ */
267
+ function hwlSerializeArray(data)
268
+ {
269
+ var result = []
270
+ data.each(function() {
271
+ var fields = {}
272
+ jQuery.each(jQuery(this).serializeArray(), function() {
273
+ fields[this.name] = this.value
274
+ })
275
+ result.push(fields)
276
+ })
277
+ return result
278
+ }
279
+
280
+ /**
281
+ * Remove selected row.
282
+ *
283
+ * @param rowId
284
+ */
285
+ function hwlRemoveRow(rowId)
286
+ {
287
+ jQuery('#' + rowId).remove()
288
+ }
readme.md ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # CAOS for Webfonts | Host Google Fonts Locally
2
+
3
+ With only 2 clicks of a button, CAOS for Webfonts automagically downloads your Google Fonts you want to WordPress' content-folder, generates a stylesheet for it and enqueues it in your theme's header.
4
+
5
+ ## Description
6
+
7
+ CAOS for Webfonts is written with performance in mind. Other plugins make repeated requests to your Blog's database to replace/remove requests to external Webfonts source (e.g. Google Fonts) on-the-fly. This might be user-friendly, but it's a performance killer, because locally hosting your Google Webfonts should be a set-and-forget feature. The source of your webfonts should be replaced once and kept that way.
8
+
9
+ That's why I kept CAOS for Webfonts small and useful. It uses the Google Fonts Helper API to automagically download the fonts you want to WordPress' contents folder and generate a stylesheet for it. The stylesheet is automatically included to your site's header and 100% compatible with plugins like Autoptimize or W3 Total Cache. After that, all you need to do is remove any mention of requests to external webfont sources (using e.g. a child theme) and you're done!
10
+
11
+ This will *decrease your pageload times*, *leverage browser cache*, *minimize DNS requests* and effectively bring you a perfect score on *Pagespeed Insights* and *Pingdom*, without taking toll on the performance of your webserver.
12
+
13
+ ## Installation
14
+
15
+ ### Using GIT
16
+
17
+ 1. From your terminal, `cd` to your plugins directory (usually `wp-content/plugins`)
18
+ 1. Run the following command: `git clone https://github.com/Dan0sz/host-webfonts-locally.git host-webfonts-local`
19
+
20
+ ### From the Wordpress Repository
21
+
22
+ 1. From your WordPress administrator area, go to *Plugins > Add New*
23
+ 1. Search for 'Daan van den Bergh'
24
+ 1. Click the 'Install' button next to *CAOS for Webfonts | Host Google Fonts Locally*
25
+ 1. Click 'Activate'
26
+
27
+ ## Frequently Asked Questions
28
+
29
+ **Can I buy you a beer?**
30
+
31
+ Yes, please! [Click here to buy me a beer](https://dev.daanvandenbergh.com/donate/ "Let's do shots!")!
32
+
33
+ Visit the [FAQ at Wordpress.org](https://wordpress.org/plugins/host-webfonts-local/#faq)
34
+
35
+ ## Support
36
+
37
+ For Support Queries, checkout the [Support Forum at Wordpress.org](https://wordpress.org/support/plugin/host-webfonts-local)
38
+
39
+ ## Changelog
40
+
41
+ Visit the [Changelog at Wordpress.org](https://wordpress.org/plugins/host-webfonts-local/#developers)
readme.txt CHANGED
@@ -1,20 +1,22 @@
1
- === CAOS for Webfonts - Host your Webfonts Locally! ===
2
  Contributors: DaanvandenBergh
3
  Donate link: https://dev.daanvandenbergh.com/donate/
4
  Tags: update, host, save, local, locally, google, fonts, webfonts, minimize, external, requests, leverage, browser, cache
5
  Requires at least: 4.5
6
- Tested up to: 4.9
7
- Stable tag: 1.1.0
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
11
- Automagically saves the fonts you want to use inside your content-folder, generates a stylesheet for them and enqueues it in your theme's header.
12
 
13
  == Description ==
14
 
15
- Another cool plugin created by [Daan van den Bergh](http://dev.daanvandenbergh.com "Click here to visit my Wordpress Development Blog") that automagically saves the fonts you want to use inside your content-folder, generates a stylesheet for them and enqueues it in your theme's header.
16
 
17
- This will *decrease your pageload times*, *leverage browser cache*, *minimize DNS requests* and effectively bring you a perfect score on *Pagespeed Insights* and *Pingdom*.
 
 
18
 
19
  == Installation ==
20
 
@@ -22,11 +24,43 @@ This will *decrease your pageload times*, *leverage browser cache*, *minimize DN
22
  1. Activate the plugin through the 'Plugins' screen in WordPress
23
  1. Use the Settings -> Optimize Webfonts screen to configure the plugin
24
 
 
 
25
  == Frequently Asked Questions ==
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  = Can I buy you a beer? =
28
 
29
- Yes, please! [Click here to buy me a beer](http://dev.daanvandenbergh.com/donate/ "Let's do shots!")!
30
 
31
  == Screenshots ==
32
 
@@ -34,8 +68,69 @@ N/A
34
 
35
  == Changelog ==
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  = 1.1.0 =
38
- Fixed bug where the plugin would sometimes permanently change your upload-director to /local-fonts.
39
 
40
  = 1.0.1 =
41
  Changed to Github.
1
+ === CAOS for Webfonts | Host Google Fonts Locally ===
2
  Contributors: DaanvandenBergh
3
  Donate link: https://dev.daanvandenbergh.com/donate/
4
  Tags: update, host, save, local, locally, google, fonts, webfonts, minimize, external, requests, leverage, browser, cache
5
  Requires at least: 4.5
6
+ Tested up to: 5.0
7
+ Stable tag: 1.5.7
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
11
+ With only 2 clicks of a button, CAOS for Webfonts automagically downloads your Google Fonts you want to WordPress' content-folder, generates a stylesheet for it and enqueues it in your theme's header.
12
 
13
  == Description ==
14
 
15
+ CAOS for Webfonts is written with performance in mind. Other plugins make repeated requests to your Blog's database to replace/remove requests to external Webfonts source (e.g. Google Fonts) on-the-fly. This might be user-friendly, but it's a performance killer, because locally hosting your Google Webfonts should be a set-and-forget feature. The source of your webfonts should be replaced once and kept that way.
16
 
17
+ That's why I kept CAOS for Webfonts small and useful. It uses the Google Fonts Helper API to automagically download the fonts you want to WordPress' contents folder and generate a stylesheet for it. The stylesheet is automatically included to your site's header and 100% compatible with plugins like Autoptimize or W3 Total Cache. After that, all you need to do is remove any mention of requests to external webfont sources (using e.g. a child theme or a plugin such as Autoptimize) and you're done!
18
+
19
+ This will *decrease your pageload times*, *leverage browser cache*, *minimize DNS requests* and effectively bring you a perfect score on *Pagespeed Insights* and *Pingdom*, without taking toll on the performance of your webserver.
20
 
21
  == Installation ==
22
 
24
  1. Activate the plugin through the 'Plugins' screen in WordPress
25
  1. Use the Settings -> Optimize Webfonts screen to configure the plugin
26
 
27
+ For a more comprehensive guide for configuration, click [here](https://dev.daanvandenbergh.com/wordpress/host-google-fonts-locally/).
28
+
29
  == Frequently Asked Questions ==
30
 
31
+ = I don't know what I'm doing! Can you help? =
32
+
33
+ Of course :) But first calm down and read this [comprehensive guide on how to configure CAOS for Webfonts](https://dev.daanvandenbergh.com/wordpress/host-google-fonts-locally/). If you have any questions afterwards, visit the [Support Forum](https://wordpress.org/support/plugin/host-webfonts-local).
34
+
35
+ = I have another file I want to host locally. Could you make a plugin? =
36
+
37
+ Maintaining two plugins besides my daily 9-to-5 job is a handful, so no. If you're looking for a way to host analytics.js locally; please install [CAOS for Analytics](https://wordpress.org/plugins/host-analyticsjs-local/). For anything else, please follow the steps in [this how-to](https://dev.daanvandenbergh.com/how-to/host-js-locally-crontab/).
38
+
39
+ = How come my font isn't available in CAOS for Webfonts? =
40
+
41
+ This could be for several reasons:
42
+ 1. Have you checked if your font is available on Google Fonts?
43
+ 1. Is your font listed as an open source font, or is it a premium font? For obvious reasons, CAOS for Webfonts only has access to open source fonts.
44
+
45
+ = Does this plugin edit template files? =
46
+
47
+ No, it does not. It creates a CSS Stylesheet which will be automatically added to your theme's header using a built-in WordPress queueing system.
48
+
49
+ = My fonts aren't being downloaded! What's going on? =
50
+
51
+ First check your database if the table {prefix}_caos_webfonts exists. If you're using any caching plugins, such as Autoptimize, W3TC or WP Super Cache, empty their caches. After that empty your browser's cache and reload the page. Try again.
52
+
53
+ = The stylesheet isn't loaded? What's going on? =
54
+
55
+ CAOS for Webfonts enqueues the stylesheet into WordPress' head. If the stylesheet isn't loaded, this probably means your theme isn't implementing the wp_head() function into it's header section.
56
+
57
+ = Does this plugin support Multi Site? =
58
+
59
+ Yes, it does!
60
+
61
  = Can I buy you a beer? =
62
 
63
+ Yes, please! [Click here to buy me a beer](https://dev.daanvandenbergh.com/donate/ "Let's do shots!")!
64
 
65
  == Screenshots ==
66
 
68
 
69
  == Changelog ==
70
 
71
+ = 1.5.7 =
72
+ Replaced download counter for progress-bar. Refactored logic for AJAX-requests for better
73
+ performance.
74
+
75
+ = 1.5.6 =
76
+ Optimized AJAX-requests for download-counter.
77
+
78
+ = 1.5.5 =
79
+ Added a clean-up button, which cleans the currently configured cache-dir.
80
+
81
+ = 1.5.3 =
82
+ Plugins admin-JS and -CSS is now updated by force after plugin update to prevent malfunction.
83
+
84
+ = 1.5.2 =
85
+ Added a download counter, clean queue-button and improved allround UX. Fixed a few minor bugs.
86
+
87
+ = 1.5.1 =
88
+ Fixed a bug where sometimes the fonts weren't downloaded yet when the stylesheet was generated.
89
+
90
+ = 1.5.0 =
91
+ CAOS for Webfonts now remembers which fonts you use, to make it easier to edit your stylesheet.
92
+
93
+ = 1.4.1 =
94
+ Added option to change font-display strategy.
95
+
96
+ = 1.4.0 =
97
+ Added option to change cache directory.
98
+
99
+ = 1.3.10 =
100
+ Fixed bug with detecting wp-content directory.
101
+
102
+ = 1.3.9 =
103
+ Fixed Multisite Bug.
104
+
105
+ = 1.3.8 =
106
+ Revert accidental commit
107
+
108
+ = 1.3.7 =
109
+ Tested with WP 5+
110
+
111
+ = 1.3.6 =
112
+ Changed order of loaded fonts to improve compatibility in Firefox. [Reported by @lofesa]
113
+
114
+ = 1.3.5 =
115
+ When plugin is deactivated, enqueued styles and scripts are removed. Fixed bug where fontnames containing multiple spaces did not return any results. Added console log when no results are returned.
116
+
117
+ = 1.3.2 =
118
+ Finally added 'Settings'-link to Plugins-page.
119
+
120
+ = 1.3.1 =
121
+ Further security measures to remove Path Traversal vulnerabilities.
122
+
123
+ = 1.2.9 =
124
+ Added security measures to prevent XSS.
125
+
126
+ = 1.2.8 =
127
+ Fixed bug where 'remove' would sometimes remove two rows.
128
+
129
+ = 1.2.5 =
130
+ Complete overhaul of the plugin. Fonts are now searched using the Google Fonts Helper API and
131
+
132
  = 1.1.0 =
133
+ Fixed bug where the plugin would sometimes permanently change your uploads-directory to /local-fonts.
134
 
135
  = 1.0.1 =
136
  Changed to Github.