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

Version Description

OMGF - CORONA EDITION * Moved Welcome-panel to the side. * wp_remote_get() is now used instead of cURL. * Complete code overhaul to increase performance and UX. * Notices and errors are now more explanatory and dismissable. * Fixed several bugs. * OMGF now uses wp_options table, instead of own tables. * Old tables are removed and data is migrated. * Auto detect now works better than ever. * Search now works bug free. * WordPress' default admin fonts no longer show up as results.

Download this release

Release Info

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

Code changes from version 2.5.0 to 3.0.0

css/hwl-admin.min.css DELETED
@@ -1 +0,0 @@
1
- .settings-column{float:left}.settings-column.left{width:60%}.settings-column.right{width:39%}@media only screen and (max-width:940px){.settings-column{display:block;float:none}.settings-column.left,.settings-column.right{width:100%}}#search-field{line-height:38px;width:360px;font-size:20px;padding:3px 10px}#hwl-results .spinner{visibility:visible !important;float:none}#hwl-results{position:relative}tr.loading td{position:absolute;width:100%;height:100%;left:0;right:0;text-align:center;background-color:rgba(255,255,255,0.5)}.hwl-remove{cursor:pointer}.button-cancel{color:#a00;text-decoration:none;border-color:transparent;box-shadow:none;background:0;border-radius:3px;white-space:nowrap;padding:6px 10px !important}.button-cancel:hover{cursor:pointer;background:#d54e21;color:#fff;border-color:#d54e21}.caos-status-total-bar{height:30px;border-radius:6px;padding:4px 4px 0;background:rgba(0,0,0,0.25);box-shadow:inset 0 1px 2px rgba(0,0,0,0.25),0 1px rgba(255,255,255,0.08)}#caos-status-progress-bar{height:26px;color:#fff;border-radius:4px;white-space:nowrap;line-height:25px;text-align:center;transition:width 1s;-webkit-transition:width 1s;background-color:#0daadb;background-image:linear-gradient(to bottom,rgba(255,255,255,0.3),rgba(255,255,255,0.05));box-shadow:0 0 1px 1px rgba(0,0,0,0.25),inset 0 1px rgba(255,255,255,0.1)}
 
css/{hwl-admin.css → omgf-admin.css} RENAMED
@@ -9,7 +9,7 @@
9
  *
10
  * @package : OMGF
11
  * @author : Daan van den Bergh
12
- * @copyright: (c) 2019 Daan van den Bergh
13
  * @url : https://daan.dev
14
  * * * * * * * * * * * * * * * * * * * */
15
 
@@ -18,15 +18,22 @@
18
  */
19
 
20
  .settings-column {
21
- float: left;
22
  }
23
 
24
  .settings-column.left {
25
- width: 60%;
 
 
 
 
 
 
26
  }
27
 
28
  .settings-column.right {
29
- width: 39%
 
30
  }
31
 
32
  @media only screen and (max-width: 940px) {
@@ -36,43 +43,60 @@
36
  }
37
 
38
  .settings-column.left,
 
39
  .settings-column.right {
40
  width: 100%;
41
  }
42
  }
43
 
 
 
 
 
 
 
 
 
 
 
44
  /**
45
  * Generate Stylesheet-form
46
  */
47
 
48
- #search-field {
49
  line-height: 38px;
50
  width: 360px;
51
  font-size: 20px;
52
  padding: 3px 10px;
53
  }
54
 
55
- #hwl-results .spinner {
56
- visibility: visible !important;
57
- float: none;
58
- }
59
-
60
- #hwl-results {
61
- position: relative;
62
- }
63
-
64
- tr.loading td {
65
  position: absolute;
66
  width: 100%;
67
  height: 100%;
68
  left: 0;
69
  right: 0;
 
70
  text-align: center;
71
- background-color: rgba(255, 255, 255, 0.5)
72
  }
73
 
74
- .hwl-remove {
75
- cursor: pointer;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  }
77
 
78
  .button-cancel {
@@ -93,24 +117,7 @@ tr.loading td {
93
  border-color: #d54e21;
94
  }
95
 
96
- .caos-status-total-bar {
97
- height: 30px;
98
- border-radius: 6px;
99
- padding: 4px 4px 0;
100
- background: rgba(0, 0, 0, 0.25);
101
- box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.25), 0 1px rgba(255, 255, 255, 0.08);
102
- }
103
-
104
- #caos-status-progress-bar {
105
- height: 26px;
106
- color: #fff;
107
- border-radius: 4px;
108
- white-space: nowrap;
109
- line-height: 25px;
110
- text-align: center;
111
- transition: width 1s;
112
- -webkit-transition: width 1s;
113
- background-color: #0daadb;
114
- background-image: linear-gradient(to bottom, rgba(255, 255, 255, 0.3), rgba(255, 255, 255, 0.05));
115
- box-shadow: 0 0 1px 1px rgba(0, 0, 0, 0.25), inset 0 1px rgba(255, 255, 255, 0.1);
116
  }
9
  *
10
  * @package : OMGF
11
  * @author : Daan van den Bergh
12
+ * @copyright: (c) 2020 Daan van den Bergh
13
  * @url : https://daan.dev
14
  * * * * * * * * * * * * * * * * * * * */
15
 
18
  */
19
 
20
  .settings-column {
21
+ display: inline-block;
22
  }
23
 
24
  .settings-column.left {
25
+ float: left;
26
+ min-width: 655px;
27
+ width: 37%;
28
+ }
29
+
30
+ .settings-column.center {
31
+ width: 35%
32
  }
33
 
34
  .settings-column.right {
35
+ float: right;
36
+ width: 25%;
37
  }
38
 
39
  @media only screen and (max-width: 940px) {
43
  }
44
 
45
  .settings-column.left,
46
+ .settings-column.center,
47
  .settings-column.right {
48
  width: 100%;
49
  }
50
  }
51
 
52
+ @media only screen and (min-width: 941px) and (max-width: 1024px) {
53
+ .settings-column.center {
54
+ width: 66%;
55
+ }
56
+
57
+ .settings-column.right {
58
+ width: 33%;
59
+ }
60
+ }
61
+
62
  /**
63
  * Generate Stylesheet-form
64
  */
65
 
66
+ #omgf-search {
67
  line-height: 38px;
68
  width: 360px;
69
  font-size: 20px;
70
  padding: 3px 10px;
71
  }
72
 
73
+ .omgf-loading {
 
 
 
 
 
 
 
 
 
74
  position: absolute;
75
  width: 100%;
76
  height: 100%;
77
  left: 0;
78
  right: 0;
79
+ top: 0;
80
  text-align: center;
 
81
  }
82
 
83
+ .omgf-loading .spinner {
84
+ top: 50%;
85
+ left: 50%;
86
+ position: absolute;
87
+ -ms-transform: translate(0, -70%);
88
+ transform: translate(0, -70%);
89
+ }
90
+
91
+ .omgf-font-remove {
92
+ padding: 0 0 0 20px;
93
+ position: relative;
94
+ display: inline-block;
95
+ }
96
+
97
+ .omgf-font-remove:before {
98
+ position: absolute;
99
+ left: 0;
100
  }
101
 
102
  .button-cancel {
117
  border-color: #d54e21;
118
  }
119
 
120
+ #omgf-control-panel {
121
+ min-width: 654px;
122
+ width: 66%;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  }
css/omgf-admin.min.css ADDED
@@ -0,0 +1 @@
 
1
+ .settings-column{display:inline-block}.settings-column.left{float:left;min-width:655px;width:37%}.settings-column.center{width:35%}.settings-column.right{float:right;width:25%}@media only screen and (max-width:940px){.settings-column{display:block;float:none}.settings-column.left,.settings-column.center,.settings-column.right{width:100%}}@media only screen and (min-width:941px) and (max-width:1024px){.settings-column.center{width:66%}.settings-column.right{width:33%}}#omgf-search{line-height:38px;width:360px;font-size:20px;padding:3px 10px}.omgf-loading{position:absolute;width:100%;height:100%;left:0;right:0;top:0;text-align:center}.omgf-loading .spinner{top:50%;left:50%;position:absolute;-ms-transform:translate(0,-70%);transform:translate(0,-70%)}.omgf-font-remove{padding:0 0 0 20px;position:relative;display:inline-block}.omgf-font-remove:before{position:absolute;left:0}.button-cancel{color:#a00;text-decoration:none;border-color:transparent;box-shadow:none;background:0;border-radius:3px;white-space:nowrap;padding:6px 10px !important}.button-cancel:hover{cursor:pointer;background:#d54e21;color:#fff;border-color:#d54e21}#omgf-control-panel{min-width:654px;width:66%}
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: 2.5.0
8
  * Author: Daan van den Bergh
9
  * Author URI: https://daan.dev
10
  * License: GPL2v2 or later
@@ -19,8 +19,8 @@ defined('ABSPATH') || exit;
19
  */
20
  define('OMGF_PLUGIN_DIR', plugin_dir_path(__FILE__));
21
  define('OMGF_PLUGIN_FILE', __FILE__);
22
- define('OMGF_DB_VERSION', '2.5.0');
23
- define('OMGF_STATIC_VERSION', '2.5.0');
24
  define('OMGF_WEB_FONT_LOADER_VERSION', '1.6.26');
25
 
26
  /**
@@ -55,7 +55,9 @@ function omgf_autoload($class)
55
  $i++;
56
  }
57
 
58
- $filename .= 'class-' . strtolower($path[$i]) . '.php';
 
 
59
  }
60
 
61
  return include OMGF_PLUGIN_DIR . 'includes/' . $filename;
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: 3.0.0
8
  * Author: Daan van den Bergh
9
  * Author URI: https://daan.dev
10
  * License: GPL2v2 or later
19
  */
20
  define('OMGF_PLUGIN_DIR', plugin_dir_path(__FILE__));
21
  define('OMGF_PLUGIN_FILE', __FILE__);
22
+ define('OMGF_DB_VERSION', '3.0.0');
23
+ define('OMGF_STATIC_VERSION', '3.0.0');
24
  define('OMGF_WEB_FONT_LOADER_VERSION', '1.6.26');
25
 
26
  /**
55
  $i++;
56
  }
57
 
58
+ $pieces = preg_split('/(?=[A-Z])/', lcfirst($path[$i]));
59
+
60
+ $filename .= 'class-' . strtolower(implode($pieces, '-')) . '.php';
61
  }
62
 
63
  return include OMGF_PLUGIN_DIR . 'includes/' . $filename;
includes/admin/class-auto-detect.php ADDED
@@ -0,0 +1,180 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /* * * * * * * * * * * * * * * * * * * * *
3
+ *
4
+ * ██████╗ ███╗ ███╗ ██████╗ ███████╗
5
+ * ██╔═══██╗████╗ ████║██╔════╝ ██╔════╝
6
+ * ██║ ██║██╔████╔██║██║ ███╗█████╗
7
+ * ██║ ██║██║╚██╔╝██║██║ ██║██╔══╝
8
+ * ╚██████╔╝██║ ╚═╝ ██║╚██████╔╝██║
9
+ * ╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝
10
+ *
11
+ * @package : OMGF
12
+ * @author : Daan van den Bergh
13
+ * @copyright: (c) 2020 Daan van den Bergh
14
+ * @url : https://daan.dev
15
+ * * * * * * * * * * * * * * * * * * * */
16
+
17
+ defined('ABSPATH') || exit;
18
+
19
+ class OMGF_Admin_AutoDetect
20
+ {
21
+ /** @var array $detected_fonts */
22
+ private $detected_fonts = [];
23
+
24
+ /** @var OMGF_API $api */
25
+ private $api;
26
+
27
+ /**
28
+ * OMGF_Admin_AutoDetect constructor.
29
+ *
30
+ * @param $detected_fonts
31
+ */
32
+ public function __construct(
33
+ $detected_fonts
34
+ ) {
35
+ $this->detected_fonts = $detected_fonts;
36
+ $this->api = new OMGF_API();
37
+
38
+ $this->init();
39
+ }
40
+
41
+ /**
42
+ * Initialize Auto Detect.
43
+ */
44
+ private function init()
45
+ {
46
+ $font_properties = $this->extract_font_properties($this->detected_fonts);
47
+
48
+ $fonts = $this->build_subsets_array($font_properties);
49
+
50
+ // Fetch available font styles from API, except for default WordPress Admin fonts.
51
+ foreach ($fonts as $index => &$font) {
52
+ if (($index == 0 && $font['subset_font'] == 'open-sans') || ($index == 1 && $font['subset_font'] == 'noto-serif')) {
53
+ unset($fonts[$index]);
54
+
55
+ continue;
56
+ }
57
+
58
+ $font_styles[$font['subset_font']] = $this->api->get_font_styles($font['subset_font'], implode(',', $font['selected_subsets']));
59
+ }
60
+
61
+ update_option(OMGF_Admin_Settings::OMGF_SETTING_SUBSETS, $fonts);
62
+
63
+ // Match used styles with available styles.
64
+ foreach ($fonts as $subset) {
65
+ $used_styles[] = $this->process_used_styles($subset['used_styles'], $font_styles[$subset['subset_font']]);
66
+ }
67
+
68
+ if (isset($used_styles)) {
69
+ $detected_fonts = array_merge(...$used_styles);
70
+
71
+ update_option(OMGF_Admin_Settings::OMGF_SETTING_FONTS, $detected_fonts);
72
+ }
73
+
74
+ /** It only needs to run once. */
75
+ delete_option(OMGF_Admin_Settings::OMGF_SETTING_AUTO_DETECTION_ENABLED);
76
+ delete_option(OMGF_Admin_Settings::OMGF_SETTING_DETECTED_FONTS);
77
+
78
+ if (empty($fonts)) {
79
+ OMGF_Admin_Notice::set_notice(__('Auto-detection completed successfully, but no Google Fonts were found.', 'host-webfonts-local'), false, 'warning');
80
+
81
+ OMGF_Admin_Notice::set_notice(sprintf(__('Your theme (or plugin) might be using unconventional methods (or Web Font Loader) to load Google Fonts. For a custom integration to load your Google Fonts locally, <a href="%s" target="_blank">hire me</a> or <a href="%s" target="_blank">contact me</a> when in doubt.', 'host-webfonts-local'), 'https://woosh.dev/wordpress-services/omgf-expert-configuration/', OMGF_SITE_URL . '/contact'), false, 'info');
82
+ } else {
83
+ OMGF_Admin_Notice::set_notice(__('Auto-detection completed. Please check the results and proceed to download the fonts and generate the stylesheet.', 'host-webfonts-local'), false);
84
+ }
85
+ }
86
+
87
+ /**
88
+ * @param $fontSource
89
+ *
90
+ * @return array
91
+ */
92
+ private function extract_font_properties($fontSource)
93
+ {
94
+ $font_properties = array();
95
+
96
+ $i = 0;
97
+
98
+ foreach ($fontSource as $source) {
99
+ $parts = parse_url($source);
100
+
101
+ parse_str($parts['query'], $font_properties[]);
102
+
103
+ /**
104
+ * Some themes (like Twenty Sixteen) do chained requests using a pipe (|).
105
+ * This function explodes these requests and adds them to the query.
106
+ */
107
+ if (strpos($font_properties[$i]['family'], '|') !== false) {
108
+ $parts_parts = explode('|', $font_properties[$i]['family']);
109
+ $font_property_subset = isset($font_properties[$i]['subset']) ? $font_properties[$i]['subset'] : 'latin';
110
+
111
+ foreach ($parts_parts as $part) {
112
+ $font_properties[$i]['family'] = $part;
113
+ $font_properties[$i]['subset'] = $font_property_subset;
114
+ $i++;
115
+ }
116
+ }
117
+
118
+ $i++;
119
+ }
120
+
121
+ return $font_properties;
122
+ }
123
+
124
+ /**
125
+ * @param $font_properties
126
+ *
127
+ * @return array
128
+ */
129
+ private function build_subsets_array($font_properties)
130
+ {
131
+ foreach ($font_properties as $properties) {
132
+ $parts = explode(':', $properties['family']);
133
+ $subsets = isset($properties['subset']) ? explode(',', $properties['subset']) : null;
134
+
135
+ if (!empty($parts)) {
136
+ $font_family = $parts[0];
137
+ $styles = explode(',', $parts[1]);
138
+ }
139
+
140
+ $fonts[] = [
141
+ 'subset_family' => $font_family,
142
+ 'subset_font' => str_replace(' ', '-', strtolower($font_family)),
143
+ 'available_subsets' => $subsets ?? [ 'latin' ],
144
+ 'selected_subsets' => $subsets ?? [ 'latin' ],
145
+ 'used_styles' => $styles
146
+ ];
147
+ }
148
+
149
+ return $fonts;
150
+ }
151
+
152
+ /**
153
+ * @param $usedStyles
154
+ * @param $availableStyles
155
+ *
156
+ * @return array
157
+ */
158
+ private function process_used_styles($usedStyles, $availableStyles)
159
+ {
160
+ foreach ($usedStyles as &$style) {
161
+ $fontWeight = preg_replace('/[^0-9]/', '', $style);
162
+ $fontStyle = preg_replace('/[^a-zA-Z]/', '', $style);
163
+
164
+ if ($fontStyle == 'i') {
165
+ $fontStyle = 'italic';
166
+ }
167
+
168
+ $style = $fontWeight . $fontStyle;
169
+ }
170
+
171
+ return array_filter(
172
+ $availableStyles,
173
+ function ($style) use ($usedStyles) {
174
+ $fontStyle = $style['font_weight'] . ($style['font_style'] !== 'normal' ? $style['font_style'] : '');
175
+
176
+ return in_array($fontStyle, $usedStyles);
177
+ }
178
+ );
179
+ }
180
+ }
includes/admin/class-notice.php ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /* * * * * * * * * * * * * * * * * * * * *
3
+ *
4
+ * ██████╗ ███╗ ███╗ ██████╗ ███████╗
5
+ * ██╔═══██╗████╗ ████║██╔════╝ ██╔════╝
6
+ * ██║ ██║██╔████╔██║██║ ███╗█████╗
7
+ * ██║ ██║██║╚██╔╝██║██║ ██║██╔══╝
8
+ * ╚██████╔╝██║ ╚═╝ ██║╚██████╔╝██║
9
+ * ╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝
10
+ *
11
+ * @package : OMGF
12
+ * @author : Daan van den Bergh
13
+ * @copyright: (c) 2020 Daan van den Bergh
14
+ * @url : https://daan.dev
15
+ * * * * * * * * * * * * * * * * * * * */
16
+
17
+ defined('ABSPATH') || exit;
18
+
19
+ class OMGF_Admin_Notice
20
+ {
21
+ const OMGF_ADMIN_NOTICE_TRANSIENT = 'omgf_admin_notice';
22
+ const OMGF_ADMIN_NOTICE_EXPIRATION = 30;
23
+
24
+ /** @var array $notices */
25
+ public static $notices = [];
26
+
27
+ /**
28
+ * @param $message
29
+ * @param string $type (info|warning|error|success)
30
+ * @param string $screen_id
31
+ * @param bool $json
32
+ * @param int $code
33
+ */
34
+ public static function set_notice($message, $die = true, $type = 'success', $code = 200, $screen_id = 'all')
35
+ {
36
+ self::$notices = get_transient(self::OMGF_ADMIN_NOTICE_TRANSIENT);
37
+ self::$notices[$screen_id][$type] = $message;
38
+
39
+ set_transient(self::OMGF_ADMIN_NOTICE_TRANSIENT, self::$notices, self::OMGF_ADMIN_NOTICE_EXPIRATION);
40
+
41
+ if ($die) {
42
+ switch ($type) {
43
+ case 'error':
44
+ wp_send_json_error($message, $code);
45
+ break;
46
+ default:
47
+ wp_send_json_success($message, $code);
48
+ }
49
+ }
50
+ }
51
+
52
+ /**
53
+ * Prints notice (if any)
54
+ */
55
+ public static function print_notice()
56
+ {
57
+ $admin_notices = get_transient(self::OMGF_ADMIN_NOTICE_TRANSIENT);
58
+
59
+ if (is_array($admin_notices)) {
60
+ $current_screen = get_current_screen();
61
+
62
+ foreach ($admin_notices as $screen => $notice) {
63
+ if ($current_screen->id != $screen && $screen != 'all') {
64
+ continue;
65
+ }
66
+
67
+ foreach ($notice as $type => $message) {
68
+ ?>
69
+ <div id="message" class="notice notice-<?php echo $type; ?> is-dismissible">
70
+ <p><?php _e($message, 'host-webfonts-local'); ?></p>
71
+ </div>
72
+ <?php
73
+ }
74
+ }
75
+ }
76
+
77
+ delete_transient(self::OMGF_ADMIN_NOTICE_TRANSIENT);
78
+ }
79
+ }
includes/admin/class-settings.php CHANGED
@@ -10,7 +10,7 @@
10
  *
11
  * @package : OMGF
12
  * @author : Daan van den Bergh
13
- * @copyright: (c) 2019 Daan van den Bergh
14
  * @url : https://daan.dev
15
  * * * * * * * * * * * * * * * * * * * */
16
 
@@ -26,6 +26,8 @@ class OMGF_Admin_Settings extends OMGF_Admin
26
  'Optional' => 'optional'
27
  );
28
  const OMGF_SETTING_AUTO_DETECTION_ENABLED = 'omgf_auto_detection_enabled';
 
 
29
  const OMGF_SETTING_DETECTED_FONTS = 'omgf_detected_fonts';
30
  const OMGF_SETTING_CACHE_DIR = 'omgf_cache_dir';
31
  const OMGF_SETTING_CDN_URL = 'omgf_cdn_url';
@@ -87,17 +89,13 @@ class OMGF_Admin_Settings extends OMGF_Admin
87
  <div class="wrap">
88
  <h1><?php _e('OMGF | Optimize My Google Fonts', 'host-webfonts-local'); ?></h1>
89
 
90
- <div id="hwl-admin-notices"></div>
91
-
92
- <?php $this->get_template('welcome'); ?>
93
-
94
- <form id="hwl-options-form" class="settings-column left" name="hwl-options-form" method="post">
95
  <div class="">
96
  <?php $this->get_template('generate-stylesheet'); ?>
97
  </div>
98
  </form>
99
 
100
- <form id="hwl-settings-form" class="settings-column right" name="hwl-settings-form" method="post" action="options.php">
101
  <?php
102
  settings_fields('omgf-basic-settings');
103
  do_settings_sections('omgf-basic-settings');
@@ -109,6 +107,10 @@ class OMGF_Admin_Settings extends OMGF_Admin
109
  submit_button();
110
  ?>
111
  </form>
 
 
 
 
112
  </div>
113
  <?php
114
  }
10
  *
11
  * @package : OMGF
12
  * @author : Daan van den Bergh
13
+ * @copyright: (c) 2020 Daan van den Bergh
14
  * @url : https://daan.dev
15
  * * * * * * * * * * * * * * * * * * * */
16
 
26
  'Optional' => 'optional'
27
  );
28
  const OMGF_SETTING_AUTO_DETECTION_ENABLED = 'omgf_auto_detection_enabled';
29
+ const OMGF_SETTING_SUBSETS = 'omgf_subsets';
30
+ const OMGF_SETTING_FONTS = 'omgf_fonts';
31
  const OMGF_SETTING_DETECTED_FONTS = 'omgf_detected_fonts';
32
  const OMGF_SETTING_CACHE_DIR = 'omgf_cache_dir';
33
  const OMGF_SETTING_CDN_URL = 'omgf_cdn_url';
89
  <div class="wrap">
90
  <h1><?php _e('OMGF | Optimize My Google Fonts', 'host-webfonts-local'); ?></h1>
91
 
92
+ <form id="omgf-options-form" class="settings-column left" name="omgf-options-form" method="post">
 
 
 
 
93
  <div class="">
94
  <?php $this->get_template('generate-stylesheet'); ?>
95
  </div>
96
  </form>
97
 
98
+ <form id="omgf-settings-form" class="settings-column center" name="omgf-settings-form" method="post" action="options.php">
99
  <?php
100
  settings_fields('omgf-basic-settings');
101
  do_settings_sections('omgf-basic-settings');
107
  submit_button();
108
  ?>
109
  </form>
110
+
111
+ <div class="settings-column right">
112
+ <?php $this->get_template('welcome'); ?>
113
+ </div>
114
  </div>
115
  <?php
116
  }
includes/ajax/class-detect.php DELETED
@@ -1,109 +0,0 @@
1
- <?php
2
- /* * * * * * * * * * * * * * * * * * * * *
3
- *
4
- * ██████╗ ███╗ ███╗ ██████╗ ███████╗
5
- * ██╔═══██╗████╗ ████║██╔════╝ ██╔════╝
6
- * ██║ ██║██╔████╔██║██║ ███╗█████╗
7
- * ██║ ██║██║╚██╔╝██║██║ ██║██╔══╝
8
- * ╚██████╔╝██║ ╚═╝ ██║╚██████╔╝██║
9
- * ╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝
10
- *
11
- * @package : OMGF
12
- * @author : Daan van den Bergh
13
- * @copyright: (c) 2019 Daan van den Bergh
14
- * @url : https://daan.dev
15
- * * * * * * * * * * * * * * * * * * * */
16
-
17
- defined('ABSPATH') || exit;
18
-
19
- class OMGF_AJAX_Detect
20
- {
21
- public function __construct(
22
- $fonts
23
- ) {
24
- $this->save_detected_fonts($fonts);
25
- }
26
-
27
- /**
28
- *
29
- */
30
- public function save_detected_fonts($used_fonts)
31
- {
32
- $font_properties = $this->extract_font_properties($used_fonts);
33
-
34
- $fonts = $this->build_subsets_array($font_properties);
35
-
36
- /** It only needs to run once. */
37
- update_option(OMGF_Admin_Settings::OMGF_SETTING_AUTO_DETECTION_ENABLED, false);
38
-
39
- wp_send_json_success($fonts);
40
- }
41
-
42
- /**
43
- * @param $fontSource
44
- *
45
- * @return array
46
- */
47
- private function extract_font_properties($fontSource)
48
- {
49
- $font_properties = array();
50
-
51
- $i = 0;
52
-
53
- foreach ($fontSource as $source) {
54
- $parts = parse_url($source);
55
-
56
- parse_str($parts['query'], $font_properties[]);
57
-
58
- /**
59
- * Some themes (like Twenty Sixteen) do chained requests using a pipe (|).
60
- * This function explodes these requests and adds them to the query.
61
- */
62
- if (strpos($font_properties[$i]['family'], '|') !== false) {
63
- $parts_parts = explode('|', $font_properties[$i]['family']);
64
- $font_property_subset = isset($font_properties[$i]['subset']) ? $font_properties[$i]['subset'] : 'latin';
65
-
66
- foreach ($parts_parts as $part) {
67
- $font_properties[$i]['family'] = $part;
68
- $font_properties[$i]['subset'] = $font_property_subset;
69
- $i++;
70
- }
71
- }
72
-
73
- $i++;
74
- }
75
-
76
- return $font_properties;
77
- }
78
-
79
- /**
80
- * @param $font_properties
81
- *
82
- * @return array
83
- */
84
- private function build_subsets_array($font_properties)
85
- {
86
- $i = 0;
87
-
88
- foreach ($font_properties as $properties) {
89
- $parts = explode(':', $properties['family']);
90
- $subsets = isset($properties['subset']) ? explode(',', $properties['subset']) : null;
91
-
92
- if (!empty($parts)) {
93
- $font_family = $parts[0];
94
- $styles = explode(',', $parts[1]);
95
- }
96
-
97
- $fonts['subsets'][$i]['family'] = $font_family;
98
- $fonts['subsets'][$i]['id'] = str_replace(' ', '-', strtolower($font_family));
99
- $fonts['subsets'][$i]['subsets'] = $subsets;
100
- $fonts['subsets'][$i]['used_styles'] = $styles;
101
-
102
- $i++;
103
- }
104
-
105
- $fonts['auto-detect'] = true;
106
-
107
- return $fonts;
108
- }
109
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
includes/ajax/class-download.php CHANGED
@@ -10,7 +10,7 @@
10
  *
11
  * @package : OMGF
12
  * @author : Daan van den Bergh
13
- * @copyright: (c) 2019 Daan van den Bergh
14
  * @url : https://daan.dev
15
  * * * * * * * * * * * * * * * * * * * */
16
 
@@ -18,12 +18,6 @@ defined('ABSPATH') || exit;
18
 
19
  class OMGF_AJAX_Download extends OMGF_AJAX
20
  {
21
- /** @var $fonts */
22
- private $fonts;
23
-
24
- /** @var $subsets */
25
- private $subsets;
26
-
27
  /** @var QM_DB $wpdb */
28
  private $wpdb;
29
 
@@ -51,28 +45,6 @@ class OMGF_AJAX_Download extends OMGF_AJAX
51
  {
52
  $this->create_dir_recursive();
53
 
54
- /**
55
- * To match the current queue of fonts. We need to truncate the table first.
56
- */
57
- try {
58
- $this->db->clean_queue();
59
- } catch (\Exception $e) {
60
- $this->throw_error($e->getCode(), $e->getMessage());
61
- }
62
-
63
- /**
64
- * Get the POST data.
65
- */
66
- $this->fonts = $_POST['fonts'][0]['caos_webfonts_array'];
67
- $this->subsets = $_POST['subsets'];
68
-
69
- if (!$this->fonts || !$this->subsets) {
70
- $this->throw_error('400', 'No fonts or subsets selected.');
71
- }
72
-
73
- $this->save_subsets_to_db();
74
- $this->save_fonts_to_db();
75
-
76
  $this->download();
77
  }
78
 
@@ -87,57 +59,6 @@ class OMGF_AJAX_Download extends OMGF_AJAX
87
  }
88
  }
89
 
90
- /**
91
- * Save used subsets to database for each font.
92
- */
93
- private function save_subsets_to_db()
94
- {
95
- foreach ($this->subsets as $id => $subset) {
96
- $availableSubsets = implode($subset['available'], ',');
97
- $selectedSubsets = implode(isset($subset['selected']) ? $subset['selected'] : array(), ',');
98
-
99
- $this->wpdb->insert(
100
- OMGF_DB_TABLENAME . '_subsets',
101
- array(
102
- 'subset_font' => $id,
103
- 'subset_family' => $subset['family'],
104
- 'available_subsets' => $availableSubsets,
105
- 'selected_subsets' => $selectedSubsets,
106
- )
107
- );
108
- }
109
- }
110
-
111
- /**
112
- * Save used fonts to database.
113
- */
114
- private function save_fonts_to_db()
115
- {
116
- foreach ($this->fonts as $id => $font) {
117
- $urlTtf = $font['url']['ttf'] !== 'undefined' ? $font['url']['ttf'] : null;
118
- $urlWoff = $font['url']['woff'] !== 'undefined' ? $font['url']['woff'] : null;
119
- $urlWoffTwo = $font['url']['woff2'] !== 'undefined' ? $font['url']['woff2'] : null;
120
- $urlEot = $font['url']['eot'] !== 'undefined' ? $font['url']['eot'] : null;
121
-
122
- $this->wpdb->insert(
123
- OMGF_DB_TABLENAME,
124
- array(
125
- 'font_id' => sanitize_text_field($id),
126
- 'font_family' => sanitize_text_field($font['font-family']),
127
- 'font_weight' => sanitize_text_field($font['font-weight']),
128
- 'font_style' => sanitize_text_field($font['font-style']),
129
- 'local' => sanitize_text_field($font['local']),
130
- 'preload' => sanitize_text_field($font['preload'] ?? null),
131
- 'downloaded' => 0,
132
- 'url_ttf' => esc_url_raw($urlTtf),
133
- 'url_woff' => esc_url_raw($urlWoff),
134
- 'url_woff2' => esc_url_raw($urlWoffTwo),
135
- 'url_eot' => esc_url_raw($urlEot)
136
- )
137
- );
138
- }
139
- }
140
-
141
  /**
142
  * Download the fonts and write them to the database.
143
  */
@@ -145,16 +66,20 @@ class OMGF_AJAX_Download extends OMGF_AJAX
145
  {
146
  $selectedFonts = $this->db->get_total_fonts();
147
 
148
- foreach ($selectedFonts as $id => $font) {
 
 
 
 
149
  // If font is marked as downloaded. Skip it.
150
- if ($font->downloaded) {
151
  continue;
152
  }
153
 
154
- $urls['url_ttf'] = $font->url_ttf;
155
- $urls['url_woff'] = $font->url_woff;
156
- $urls['url_woff2'] = $font->url_woff2;
157
- $urls['url_eot'] = $font->url_eot;
158
 
159
  foreach ($urls as $type => $url) {
160
  if (!$url) {
@@ -173,97 +98,62 @@ class OMGF_AJAX_Download extends OMGF_AJAX
173
  /**
174
  * We rewrite the local filename for easier debugging in the waterfall.
175
  */
176
- $filename = sanitize_title_with_dashes($font->font_family) . '-' . $font->font_weight . '-' . $font->font_style . '-' . substr(basename($remoteFile), -10);
177
  $localFile = OMGF_UPLOAD_DIR . '/' . $filename;
178
 
179
  try {
180
- $this->download_file_curl($localFile, $remoteFile);
 
181
  } catch (Exception $e) {
182
- $this->throw_error($e->getCode(), "File ($remoteFile) could not be downloaded: " . $e->getMessage());
183
  }
184
 
185
  clearstatcache();
186
 
187
- if (file_exists($localFile) && !filesize($localFile) > 0) {
188
- $this->throw_error('400', "File ($localFile) exists, but is 0 bytes in size. Is <code>allow_url_fopen</code> enabled on your server?");
189
- }
190
-
191
- /**
192
- * If file is written, change the external URL to the local URL in the POST data.
193
- * If it fails, we can still fall back to the external URL and nothing breaks.
194
- */
195
- $localFileUrl = OMGF_UPLOAD_URL . '/' . $filename;
196
- $this->wpdb->update(
197
- OMGF_DB_TABLENAME,
198
- array(
199
- $type => $localFileUrl
200
- ),
201
- array(
202
- 'font_id' => $font->font_id
203
- )
204
- );
205
  }
206
-
207
- /**
208
- * After all files are downloaded, set the 'downloaded'-field to 1.
209
- */
210
- $this->wpdb->update(
211
- OMGF_DB_TABLENAME,
212
- array(
213
- 'downloaded' => 1
214
- ),
215
- array(
216
- 'font_id' => $font->font_id
217
- )
218
- );
219
  }
 
 
 
 
220
  }
221
 
222
  /**
223
- * Download $remoteFile using cUrl and write to $localFile
224
  *
225
  * @param $localFile
226
  * @param $remoteFile
227
  */
228
- private function download_file_curl($localFile, $remoteFile)
229
  {
230
- if (!function_exists('curl_exec')) {
231
- $this->throw_error(500, 'cURL is disabled on your server and required for OMGF to function properly. Contact your hosting provider for assistance to enable cURL on your server.');
232
- }
233
-
234
- $file = fopen($localFile, 'w+');
235
- $curl = curl_init();
236
 
237
- curl_setopt_array(
238
- $curl,
239
- array(
240
- CURLOPT_RETURNTRANSFER => true,
241
- CURLOPT_URL => $remoteFile,
242
- CURLOPT_FILE => $file,
243
- CURLOPT_HEADER => false,
244
- CURLOPT_FOLLOWLOCATION => true
245
- )
246
- );
247
 
248
- curl_exec($curl);
249
- curl_close($curl);
250
- fclose($file);
251
 
252
  if (file_exists($localFile)) {
253
  return;
254
  }
255
-
256
- $this->download_file_fallback($localFile, $remoteFile);
257
  }
258
 
259
  /**
260
- * Fallback download method if cUrl failed.
261
  *
262
- * @param $localFile
263
- * @param $remoteFile
264
  */
265
- private function download_file_fallback($localFile, $remoteFile)
266
  {
267
- file_put_contents($localFile, file_get_contents($remoteFile));
 
 
 
 
 
 
 
268
  }
269
  }
10
  *
11
  * @package : OMGF
12
  * @author : Daan van den Bergh
13
+ * @copyright: (c) 2020 Daan van den Bergh
14
  * @url : https://daan.dev
15
  * * * * * * * * * * * * * * * * * * * */
16
 
18
 
19
  class OMGF_AJAX_Download extends OMGF_AJAX
20
  {
 
 
 
 
 
 
21
  /** @var QM_DB $wpdb */
22
  private $wpdb;
23
 
45
  {
46
  $this->create_dir_recursive();
47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  $this->download();
49
  }
50
 
59
  }
60
  }
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  /**
63
  * Download the fonts and write them to the database.
64
  */
66
  {
67
  $selectedFonts = $this->db->get_total_fonts();
68
 
69
+ if (empty($selectedFonts)) {
70
+ OMGF_Admin_Notice::set_notice(__('Hmmm... Seems like there\'s nothing to do here. Have you tried using <strong>search</strong> or <strong>auto detect</strong>?', 'host-webfonts-local'), true, 'error');
71
+ }
72
+
73
+ foreach ($selectedFonts as $id => &$font) {
74
  // If font is marked as downloaded. Skip it.
75
+ if ($font['downloaded']) {
76
  continue;
77
  }
78
 
79
+ $urls['url_ttf'] = $font['url_ttf'];
80
+ $urls['url_woff'] = $font['url_woff'];
81
+ $urls['url_woff2'] = $font['url_woff2'];
82
+ $urls['url_eot'] = $font['url_eot'];
83
 
84
  foreach ($urls as $type => $url) {
85
  if (!$url) {
98
  /**
99
  * We rewrite the local filename for easier debugging in the waterfall.
100
  */
101
+ $filename = sanitize_title_with_dashes($font['font_family']) . '-' . $font['font_weight'] . '-' . $font['font_style'] . '-' . substr(basename($remoteFile), -10);
102
  $localFile = OMGF_UPLOAD_DIR . '/' . $filename;
103
 
104
  try {
105
+ $this->download_file($localFile, $remoteFile);
106
+ $font['downloaded'] = 1;
107
  } catch (Exception $e) {
108
+ OMGF_Admin_Notice::set_notice(__("File ($remoteFile) could not be downloaded: ", 'host-webfonts-local') . $e->getMessage(), 'error', $e->getCode());
109
  }
110
 
111
  clearstatcache();
112
 
113
+ $font[$type] = OMGF_UPLOAD_URL . '/' . $filename;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  }
116
+
117
+ update_option(OMGF_Admin_Settings::OMGF_SETTING_FONTS, $selectedFonts);
118
+
119
+ OMGF_Admin_Notice::set_notice(count($selectedFonts) . ' ' . __('fonts downloaded. You can now proceed to generate the stylesheet.', 'host-webfonts-local'));
120
  }
121
 
122
  /**
123
+ * Download $remoteFile and write to $localFile
124
  *
125
  * @param $localFile
126
  * @param $remoteFile
127
  */
128
+ private function download_file($localFile, $remoteFile)
129
  {
130
+ $file = wp_remote_get($remoteFile, $localFile);
 
 
 
 
 
131
 
132
+ if (is_wp_error($file)) {
133
+ OMGF_Admin_Notice::set_notice($file->get_error_message(), true, 'error', $file->get_error_code());
134
+ }
 
 
 
 
 
 
 
135
 
136
+ $this->filesystem()->put_contents($localFile, $file['body']);
 
 
137
 
138
  if (file_exists($localFile)) {
139
  return;
140
  }
 
 
141
  }
142
 
143
  /**
144
+ * Helper to return WordPress filesystem subclass.
145
  *
146
+ * @return WP_Filesystem_Base $wp_filesystem
 
147
  */
148
+ private function filesystem()
149
  {
150
+ global $wp_filesystem;
151
+
152
+ if ( is_null( $wp_filesystem ) ) {
153
+ require_once ABSPATH . '/wp-admin/includes/file.php';
154
+ WP_Filesystem();
155
+ }
156
+
157
+ return $wp_filesystem;
158
  }
159
  }
includes/ajax/class-generate.php CHANGED
@@ -10,7 +10,7 @@
10
  *
11
  * @package : OMGF
12
  * @author : Daan van den Bergh
13
- * @copyright: (c) 2019 Daan van den Bergh
14
  * @url : https://daan.dev
15
  * * * * * * * * * * * * * * * * * * * */
16
 
@@ -42,6 +42,10 @@ class OMGF_AJAX_Generate extends OMGF_AJAX
42
 
43
  $selectedFonts = $this->db->get_total_fonts();
44
 
 
 
 
 
45
  $this->process_fonts($selectedFonts);
46
 
47
  $fonts = implode("\n", $this->fonts);
@@ -51,12 +55,19 @@ class OMGF_AJAX_Generate extends OMGF_AJAX
51
  * If the file can be created and uploaded. Let's try to write it.
52
  */
53
  try {
54
- $stylesheet = fopen($file, 'w') or $this->throw_error(400, "Cannot create file {$file}");
55
  fwrite($stylesheet, $fonts);
56
  fclose($stylesheet);
57
- wp_die(__('Stylesheet was successfully generated and added to your theme\'s header.'));
 
 
 
 
 
 
 
58
  } catch (Exception $e) {
59
- $this->throw_error($e->getCode(), __("Stylesheet could not be generated: $e"));
60
  }
61
  }
62
 
@@ -71,7 +82,7 @@ class OMGF_AJAX_Generate extends OMGF_AJAX
71
  * This file is automagically generated by OMGF
72
  *
73
  * @author: Daan van den Bergh
74
- * @copyright: (c) 2019 Daan van den Bergh
75
  * @url: " . OMGF_SITE_URL . "
76
  */";
77
  }
@@ -86,17 +97,17 @@ class OMGF_AJAX_Generate extends OMGF_AJAX
86
  $i = 1;
87
 
88
  foreach ($fonts as $font) {
89
- $fontUrlEot = isset($font->url_eot) ? array(0 => esc_url_raw($font->url_eot)) : array();
90
- $fontSources = isset($font->url_woff2) ? array('woff2' => esc_url_raw($font->url_woff2)) : array();
91
- $fontSources = $fontSources + (isset($font->url_woff) ? array('woff' => esc_url_raw($font->url_woff)) : array());
92
- $fontSources = $fontSources + (isset($font->url_ttf) ? array('truetype' => esc_url_raw($font->url_ttf)) : array());
93
- $locals = explode(',', sanitize_text_field($font->local));
94
 
95
  $this->fonts[$i] = "@font-face { \n";
96
- $this->fonts[$i] .= $this->build_property('font-family', $font->font_family);
97
  $this->fonts[$i] .= $this->build_property('font-display', $fontDisplay);
98
- $this->fonts[$i] .= $this->build_property('font-style', $font->font_style);
99
- $this->fonts[$i] .= $this->build_property('font-weight', $font->font_weight);
100
  $this->fonts[$i] .= isset($fontUrlEot) ? " src: " . $this->build_source_string($fontUrlEot) : '';
101
  $this->fonts[$i] .= " src: " . $this->build_source_string($locals, 'local', false);
102
  // There'll always be at least one font available, so no need to check here if $fontSources is set.
10
  *
11
  * @package : OMGF
12
  * @author : Daan van den Bergh
13
+ * @copyright: (c) 2020 Daan van den Bergh
14
  * @url : https://daan.dev
15
  * * * * * * * * * * * * * * * * * * * */
16
 
42
 
43
  $selectedFonts = $this->db->get_total_fonts();
44
 
45
+ if (empty($selectedFonts)) {
46
+ OMGF_Admin_Notice::set_notice(__('Hmmm... Seems like there\'s nothing to do here. Have you tried using <strong>search</strong> or <strong>auto detect</strong>?', 'host-webfonts-local'), true, 'error');
47
+ }
48
+
49
  $this->process_fonts($selectedFonts);
50
 
51
  $fonts = implode("\n", $this->fonts);
55
  * If the file can be created and uploaded. Let's try to write it.
56
  */
57
  try {
58
+ $stylesheet = fopen($file, 'w') or OMGF_Admin_Notice::set_notice(__("Cannot create file {$file}", 'host-webfonts-local', true, 'error', 400));
59
  fwrite($stylesheet, $fonts);
60
  fclose($stylesheet);
61
+
62
+ OMGF_Admin_Notice::set_notice(__('Congratulations! Your stylesheet was generated successfully and added to your theme\'s header.', 'host-webfonts-local'), false);
63
+
64
+ $count = count($selectedFonts);
65
+ $review = 'https://wordpress.org/support/plugin/host-webfonts-local/reviews/?rate=5#new-post';
66
+ $tweet = "https://twitter.com/intent/tweet?text=I+just+optimized+$count+Google+Fonts+with+OMGF+for+@WordPress!+Try+it+for+yourself:&via=Dan0sz&hashtags=GoogleFonts,WordPress,Pagespeed,Insights&url=https://wordpress.org/plugins/host-webfonts-local/";
67
+
68
+ OMGF_Admin_Notice::set_notice(sprintf(__('OMGF has optimized %s fonts. Enjoy your performance boost! Would you be willing to <a href="%s" target="_blank">leave a review</a> or <a href="%s" target="_blank">tweet about it</a>?', 'host-webfonts-local'), $count, $review, $tweet), true, 'info');
69
  } catch (Exception $e) {
70
+ OMGF_Admin_Notice::set_notice(__("Stylesheet could not be generated:", 'host-webfonts-local') . " $e", true, 'error', $e->getCode());
71
  }
72
  }
73
 
82
  * This file is automagically generated by OMGF
83
  *
84
  * @author: Daan van den Bergh
85
+ * @copyright: (c) 2020 Daan van den Bergh
86
  * @url: " . OMGF_SITE_URL . "
87
  */";
88
  }
97
  $i = 1;
98
 
99
  foreach ($fonts as $font) {
100
+ $fontUrlEot = isset($font['url_eot']) ? array(0 => esc_url_raw($font['url_eot'])) : array();
101
+ $fontSources = isset($font['url_woff2']) ? array('woff2' => esc_url_raw($font['url_woff2'])) : array();
102
+ $fontSources = $fontSources + (isset($font['url_woff']) ? array('woff' => esc_url_raw($font['url_woff'])) : array());
103
+ $fontSources = $fontSources + (isset($font['url_ttf']) ? array('truetype' => esc_url_raw($font['url_ttf'])) : array());
104
+ $locals = explode(',', sanitize_text_field($font['local']));
105
 
106
  $this->fonts[$i] = "@font-face { \n";
107
+ $this->fonts[$i] .= $this->build_property('font-family', $font['font_family']);
108
  $this->fonts[$i] .= $this->build_property('font-display', $fontDisplay);
109
+ $this->fonts[$i] .= $this->build_property('font-style', $font['font_style']);
110
+ $this->fonts[$i] .= $this->build_property('font-weight', $font['font_weight']);
111
  $this->fonts[$i] .= isset($fontUrlEot) ? " src: " . $this->build_source_string($fontUrlEot) : '';
112
  $this->fonts[$i] .= " src: " . $this->build_source_string($locals, 'local', false);
113
  // There'll always be at least one font available, so no need to check here if $fontSources is set.
includes/class-admin.php CHANGED
@@ -10,7 +10,7 @@
10
  *
11
  * @package : OMGF
12
  * @author : Daan van den Bergh
13
- * @copyright: (c) 2019 Daan van den Bergh
14
  * @url : https://daan.dev
15
  * * * * * * * * * * * * * * * * * * * */
16
 
@@ -30,8 +30,8 @@ class OMGF_Admin
30
 
31
  $this->wpdb = $wpdb;
32
  // @formatter:off
33
- add_action('admin_notices', array($this, 'table_exists'));
34
  add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts'));
 
35
  // @formatter:on
36
  }
37
 
@@ -43,8 +43,8 @@ class OMGF_Admin
43
  public function enqueue_admin_scripts($hook)
44
  {
45
  if ($hook == 'settings_page_optimize-webfonts') {
46
- wp_enqueue_script(self::OMGF_ADMIN_JS_HANDLE, plugin_dir_url(OMGF_PLUGIN_FILE) . 'js/hwl-admin.js', array('jquery'), OMGF_STATIC_VERSION, true);
47
- wp_enqueue_style(self::OMGF_ADMIN_CSS_HANDLE, plugin_dir_url(OMGF_PLUGIN_FILE) . 'css/hwl-admin.css', array(), OMGF_STATIC_VERSION);
48
 
49
  $options = array(
50
  'auto_detect_enabled' => OMGF_AUTO_DETECT_ENABLED,
@@ -55,30 +55,6 @@ class OMGF_Admin
55
  }
56
  }
57
 
58
- /**
59
- * Check if main OMGF table exists on page load in Admin screen.
60
- */
61
- public function table_exists()
62
- {
63
- $table = OMGF_DB_TABLENAME;
64
-
65
- if (!$this->wpdb->get_var($this->wpdb->prepare("SHOW TABLES LIKE %s", $table))): ?>
66
- <div id="message" class="notice notice-error is-dismissible">
67
- <p><?php _e("Table $table does not exist and is necessary for OMGF to function properly. Try manually de-activating and activating the plugin via the WordPress plugins screen.", 'host-webfonts-local'); ?></p>
68
- </div>
69
- <?php
70
- update_option(OMGF_Admin_Settings::OMGF_SETTING_DB_VERSION, null);
71
-
72
- /**
73
- * Backwards compatibility
74
- *
75
- * @since v2.2.2
76
- */
77
- update_option('caos_webfonts_db_version', null);
78
- ?>
79
- <?php endif;
80
- }
81
-
82
  /**
83
  * @param $name
84
  *
@@ -88,4 +64,12 @@ class OMGF_Admin
88
  {
89
  return include OMGF_PLUGIN_DIR . 'templates/admin/block-' . $name . '.phtml';
90
  }
 
 
 
 
 
 
 
 
91
  }
10
  *
11
  * @package : OMGF
12
  * @author : Daan van den Bergh
13
+ * @copyright: (c) 2020 Daan van den Bergh
14
  * @url : https://daan.dev
15
  * * * * * * * * * * * * * * * * * * * */
16
 
30
 
31
  $this->wpdb = $wpdb;
32
  // @formatter:off
 
33
  add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts'));
34
+ add_action('admin_notices', array($this, 'add_notice'));
35
  // @formatter:on
36
  }
37
 
43
  public function enqueue_admin_scripts($hook)
44
  {
45
  if ($hook == 'settings_page_optimize-webfonts') {
46
+ wp_enqueue_script(self::OMGF_ADMIN_JS_HANDLE, plugin_dir_url(OMGF_PLUGIN_FILE) . 'js/omgf-admin.js', array('jquery'), OMGF_STATIC_VERSION, true);
47
+ wp_enqueue_style(self::OMGF_ADMIN_CSS_HANDLE, plugin_dir_url(OMGF_PLUGIN_FILE) . 'css/omgf-admin.css', array(), OMGF_STATIC_VERSION);
48
 
49
  $options = array(
50
  'auto_detect_enabled' => OMGF_AUTO_DETECT_ENABLED,
55
  }
56
  }
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  /**
59
  * @param $name
60
  *
64
  {
65
  return include OMGF_PLUGIN_DIR . 'templates/admin/block-' . $name . '.phtml';
66
  }
67
+
68
+ /**
69
+ * Add notice to admin screen.
70
+ */
71
+ public function add_notice()
72
+ {
73
+ OMGF_Admin_Notice::print_notice();
74
+ }
75
  }
includes/class-ajax.php CHANGED
@@ -10,7 +10,7 @@
10
  *
11
  * @package : OMGF
12
  * @author : Daan van den Bergh
13
- * @copyright: (c) 2019 Daan van den Bergh
14
  * @url : https://daan.dev
15
  * * * * * * * * * * * * * * * * * * * */
16
 
@@ -28,97 +28,61 @@ class OMGF_AJAX
28
  $this->db = new OMGF_DB();
29
 
30
  // @formatter:off
 
 
 
31
  add_action('wp_ajax_omgf_ajax_download_fonts', array($this, 'download_fonts'));
32
  add_action('wp_ajax_omgf_ajax_generate_styles', array($this, 'generate_styles'));
33
- add_action('wp_ajax_omgf_ajax_get_download_status', array($this, 'get_download_status'));
34
- add_action('wp_ajax_omgf_ajax_clean_queue', array($this, 'clean_queue'));
35
  add_action('wp_ajax_omgf_ajax_empty_dir', array($this, 'empty_directory'));
36
- add_action('wp_ajax_omgf_ajax_search_font_subsets', array($this, 'search_font_subsets'));
37
- add_action('wp_ajax_omgf_ajax_search_google_fonts', array($this, 'search_fonts'));
38
- add_action('wp_ajax_omgf_ajax_auto_detect', array($this, 'auto_detect'));
39
  // @formatter:on
40
  }
41
 
42
  /**
43
- * @return OMGF_AJAX_Download
44
  */
45
- public function download_fonts()
46
  {
47
- return new OMGF_AJAX_Download();
48
- }
49
 
50
- /**
51
- * @return OMGF_AJAX_Generate
52
- */
53
- public function generate_styles()
54
- {
55
- return new OMGF_AJAX_Generate();
56
- }
57
 
58
- /**
59
- * Get download status from DB.
60
- */
61
- public function get_download_status()
62
- {
63
- $status = json_encode($this->db->get_download_status());
64
 
65
- wp_die($status);
66
- }
67
 
68
- /**
69
- * AJAX-wrapper for hwlCleanQueue()
70
- */
71
- public function clean_queue()
72
- {
73
- update_option(OMGF_Admin_Settings::OMGF_SETTING_DETECTED_FONTS, '');
74
- update_option(OMGF_Admin_Settings::OMGF_SETTING_AUTO_DETECTION_ENABLED, '');
75
 
76
- wp_die($this->db->clean_queue());
77
- }
 
78
 
79
- /**
80
- * Empty cache directory.
81
- *
82
- * @return array
83
- */
84
- public function empty_directory()
85
- {
86
- update_option(OMGF_Admin_Settings::OMGF_SETTING_DETECTED_FONTS, '');
87
- update_option(OMGF_Admin_Settings::OMGF_SETTING_AUTO_DETECTION_ENABLED, '');
88
 
89
- return array_map('unlink', array_filter((array) glob(OMGF_UPLOAD_DIR . '/*')));
90
  }
91
 
92
  /**
93
- * Search the API for the requested fonts and return the available subsets.
94
  */
95
- public function search_font_subsets()
96
  {
97
- if (!function_exists('curl_exec')) {
98
- $this->throw_error(500, 'cURL is disabled on your server and required for OMGF to function properly. Contact your hosting provider for assistance to enable cURL on your server.');
99
- }
100
 
101
- try {
102
- $searchQueries = explode(',', sanitize_text_field($_POST['search_query']));
103
-
104
- foreach ($searchQueries as $searchQuery) {
105
- $request = curl_init();
106
- curl_setopt($request, CURLOPT_URL, OMGF_HELPER_URL . $searchQuery);
107
- curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
108
- $result = curl_exec($request);
109
- curl_close($request);
110
-
111
- $result = json_decode($result);
112
- $response[] = array(
113
- 'family' => $result->family,
114
- 'id' => $result->id,
115
- 'subsets' => $result->subsets
116
- );
117
- }
118
- wp_send_json_success($response);
119
- } catch (\Exception $e) {
120
- wp_send_json_error($e->getMessage(), $e->getCode());
121
- }
122
  }
123
 
124
  /**
@@ -126,100 +90,110 @@ class OMGF_AJAX
126
  */
127
  public function search_fonts()
128
  {
129
- try {
130
- $request = curl_init();
131
- $searchQuery = sanitize_text_field($_POST['search_query']);
132
- $subsets = implode(isset($_POST['search_subsets']) ? $_POST['search_subsets'] : array(), ',');
133
 
134
- curl_setopt($request, CURLOPT_URL, OMGF_HELPER_URL . $searchQuery . '?subsets=' . $subsets);
135
- curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
136
 
137
- $result = curl_exec($request);
138
 
139
- curl_close($request);
140
 
141
- $result = json_decode($result);
 
 
 
 
 
142
 
143
- if (!empty($_POST['used_styles']) && is_object($result) && $variants = $result->variants) {
144
- $used_styles['variants'] = array_values($this->process_used_styles($_POST['used_styles'], $variants));
145
 
146
- wp_send_json_success($used_styles);
147
- }
148
 
149
- wp_send_json_success($result);
150
- } catch (\Exception $e) {
151
- wp_send_json_error($e->getMessage(), $e->getCode());
 
 
152
  }
 
 
 
 
 
 
 
153
  }
154
 
155
  /**
156
- * @param $usedStyles
157
- * @param $availableStyles
158
- *
159
- * @return array
160
  */
161
- private function process_used_styles($usedStyles, $availableStyles)
162
  {
163
- foreach ($usedStyles as &$style) {
164
- $fontWeight = preg_replace('/[^0-9]/', '', $style);
165
- $fontStyle = preg_replace('/[^a-zA-Z]/', '', $style);
166
-
167
- if ($fontStyle == 'i') {
168
- $fontStyle = 'italic';
169
- }
170
-
171
- $style = $fontWeight . $fontStyle;
172
- }
173
-
174
- return array_filter(
175
- $availableStyles,
176
- function ($style) use ($usedStyles) {
177
- $fontStyle = $style->fontWeight . ($style->fontStyle !== 'normal' ? $style->fontStyle : '');
178
-
179
- return in_array($fontStyle, $usedStyles);
180
- }
181
- );
182
  }
183
 
184
  /**
185
- *
186
  */
187
- public function auto_detect()
188
  {
189
- $used_fonts = json_decode(get_option(OMGF_Admin_Settings::OMGF_SETTING_DETECTED_FONTS));
190
- $auto_detect = get_option(OMGF_Admin_Settings::OMGF_SETTING_AUTO_DETECTION_ENABLED);
191
 
192
- if ($used_fonts && $auto_detect) {
193
- new OMGF_AJAX_Detect($used_fonts);
194
- }
 
 
 
 
195
 
196
- if ((!$used_fonts && $auto_detect) || ($used_fonts && !$auto_detect)) {
197
- update_option(OMGF_Admin_Settings::OMGF_SETTING_DETECTED_FONTS, '');
198
- update_option(OMGF_Admin_Settings::OMGF_SETTING_AUTO_DETECTION_ENABLED, null);
199
- $this->throw_error(406, 'Something went wrong while trying to enable Auto Detection. <a href="javascript:location.reload()">Refresh this page</a> and try again.');
200
  }
201
 
202
- $this->enable_auto_detect();
203
-
204
- $url = get_permalink(get_posts()[0]->ID);
205
 
206
- wp_send_json_success(__("Auto-detection mode enabled. Open any page on your frontend (e.g. your <a href='$url' target='_blank'>latest post</a>). After the page is fully loaded, return here and <a href='javascript:location.reload()'>click here</a> to refresh this page. Then click 'Load fonts'.", "host-webfonts-local"));
207
  }
208
 
209
  /**
210
- *
211
  */
212
- private function enable_auto_detect()
213
  {
214
- update_option(OMGF_Admin_Settings::OMGF_SETTING_AUTO_DETECTION_ENABLED, true);
 
 
 
 
 
 
 
 
 
 
215
  }
216
 
217
  /**
218
- * @param $code
219
- * @param $message
220
  */
221
- protected function throw_error($code, $message)
222
  {
223
- wp_send_json_error(__($message, 'host-webfonts-local'), (int) $code);
 
 
 
 
 
 
 
 
 
224
  }
225
  }
10
  *
11
  * @package : OMGF
12
  * @author : Daan van den Bergh
13
+ * @copyright: (c) 2020 Daan van den Bergh
14
  * @url : https://daan.dev
15
  * * * * * * * * * * * * * * * * * * * */
16
 
28
  $this->db = new OMGF_DB();
29
 
30
  // @formatter:off
31
+ add_action('wp_ajax_omgf_ajax_search_font_subsets', array($this, 'search_font_subsets'));
32
+ add_action('wp_ajax_omgf_ajax_enable_auto_detect', [$this, 'enable_auto_detect']);
33
+ add_action('wp_ajax_omgf_ajax_search_google_fonts', array($this, 'search_fonts'));
34
  add_action('wp_ajax_omgf_ajax_download_fonts', array($this, 'download_fonts'));
35
  add_action('wp_ajax_omgf_ajax_generate_styles', array($this, 'generate_styles'));
36
+ add_action('wp_ajax_omgf_ajax_preload_font_style', array($this, 'preload_font_style'));
37
+ add_action('wp_ajax_omgf_ajax_refresh_font_style_list', array($this, 'refresh_font_style_list'));
38
  add_action('wp_ajax_omgf_ajax_empty_dir', array($this, 'empty_directory'));
 
 
 
39
  // @formatter:on
40
  }
41
 
42
  /**
43
+ * Search the API for the requested fonts and return the available subsets.
44
  */
45
+ public function search_font_subsets()
46
  {
47
+ delete_option(OMGF_Admin_Settings::OMGF_SETTING_SUBSETS);
48
+ delete_option(OMGF_Admin_Settings::OMGF_SETTING_FONTS);
49
 
50
+ if (!($query = $_POST['search_query'])) {
51
+ OMGF_Admin_Notice::set_notice(__('Search query not found.', 'host-webfonts-local'), true, 'warning');
52
+ }
 
 
 
 
53
 
54
+ $searchQueries = explode(',', sanitize_text_field($query));
 
 
 
 
 
55
 
56
+ foreach ($searchQueries as $searchQuery) {
57
+ $api = new OMGF_API();
58
 
59
+ $subsets = $api->get_subsets($searchQuery);
 
 
 
 
 
 
60
 
61
+ // If subset search comes back empty. Add a notice and skip to the next one.
62
+ if (empty($subsets)) {
63
+ OMGF_Admin_Notice::set_notice(sprintf(__('Font %s not found. Are you sure it\'s a Google Font?', 'host-webfonts-local'), $searchQuery), false, 'error');
64
 
65
+ continue;
66
+ }
67
+
68
+ $response[] = $subsets;
69
+ }
70
+
71
+ update_option(OMGF_Admin_Settings::OMGF_SETTING_SUBSETS, $response);
 
 
72
 
73
+ OMGF_Admin_Notice::set_notice(__('Subset search complete. Select subsets to generate a list of available font styles.', 'host-webfonts-local'));
74
  }
75
 
76
  /**
77
+ * Enable Auto Detect
78
  */
79
+ public function enable_auto_detect()
80
  {
81
+ update_option(OMGF_Admin_Settings::OMGF_SETTING_AUTO_DETECTION_ENABLED, true);
 
 
82
 
83
+ $url = get_permalink(get_posts()[0]->ID);
84
+
85
+ OMGF_Admin_Notice::set_notice(__("Auto-detection mode enabled. Open any page on your frontend (e.g. your <a href='$url' target='_blank'>latest post</a>). After the page is fully loaded, return here and <a href='javascript:location.reload()'>click here</a> to refresh this page.", "host-webfonts-local"));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  }
87
 
88
  /**
90
  */
91
  public function search_fonts()
92
  {
93
+ if (!isset($_POST['search_google_fonts'])) {
94
+ delete_option(OMGF_Admin_Settings::OMGF_SETTING_FONTS);
 
 
95
 
96
+ OMGF_Admin_Notice::set_notice(__('Search query not found. Did you select any subsets? If so, try again.'), true, 'warning');
97
+ }
98
 
99
+ $search_google_fonts = $_POST['search_google_fonts'];
100
 
101
+ $subsets = get_option(OMGF_Admin_Settings::OMGF_SETTING_SUBSETS);
102
 
103
+ // Unset subsets which were checked before, but aren't now.
104
+ foreach ($subsets as $index => &$font) {
105
+ if (!in_array($index, array_keys($search_google_fonts))) {
106
+ $font['selected_subsets'] = [];
107
+ }
108
+ }
109
 
110
+ // Overwrite current selected subsets in settings with new values.
111
+ $subsets = array_replace_recursive($subsets, $search_google_fonts);
112
 
113
+ update_option(OMGF_Admin_Settings::OMGF_SETTING_SUBSETS, $subsets);
 
114
 
115
+ // Retrieve available font styles.
116
+ foreach ($search_google_fonts as $font) {
117
+ $selected_subsets = implode($font['selected_subsets'], ',');
118
+ $api = new OMGF_API();
119
+ $fonts[] = $api->get_font_styles($font['subset_font'], $selected_subsets);
120
  }
121
+
122
+ // Create font styles list.
123
+ $fonts = array_merge(...$fonts);
124
+
125
+ update_option(OMGF_Admin_Settings::OMGF_SETTING_FONTS, $fonts);
126
+
127
+ OMGF_Admin_Notice::set_notice(count($fonts) . ' ' . __('font styles found. Trim the list to your needs and click <strong>Download Fonts</strong>.', 'host-webfonts-local'));
128
  }
129
 
130
  /**
131
+ * @return OMGF_AJAX_Download
 
 
 
132
  */
133
+ public function download_fonts()
134
  {
135
+ return new OMGF_AJAX_Download();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
  }
137
 
138
  /**
139
+ * @return OMGF_AJAX_Generate
140
  */
141
+ public function generate_styles()
142
  {
143
+ return new OMGF_AJAX_Generate();
144
+ }
145
 
146
+ /**
147
+ * Update options with font styles selected for preloading.
148
+ */
149
+ public function preload_font_style()
150
+ {
151
+ $fonts = get_option(OMGF_Admin_Settings::OMGF_SETTING_FONTS);
152
+ $preload_styles = $_POST['preload_font_styles'];
153
 
154
+ foreach ($fonts as &$font) {
155
+ if (in_array($font['font_id'], $preload_styles)) {
156
+ $font['preload'] = 1;
157
+ }
158
  }
159
 
160
+ update_option(OMGF_Admin_Settings::OMGF_SETTING_FONTS, $fonts);
 
 
161
 
162
+ OMGF_Admin_Notice::set_notice(count($preload_styles) . ' ' . __('fonts set to preload. If you haven\'t already, you can now <strong>download</strong> the <strong>fonts</strong>. Otherwise, just (re-)<strong>generate</strong> the <strong>stylesheet</strong>.', 'host-webfonts-local'));
163
  }
164
 
165
  /**
166
+ * Refresh font style list after rows have been removed.
167
  */
168
+ public function refresh_font_style_list()
169
  {
170
+ $fonts = get_option(OMGF_Admin_Settings::OMGF_SETTING_FONTS);
171
+
172
+ $current = $_POST['font_styles'];
173
+
174
+ $refreshed_list = array_filter($fonts, function ($font_style) use ($current) {
175
+ return in_array($font_style['font_id'], $current);
176
+ });
177
+
178
+ update_option(OMGF_Admin_Settings::OMGF_SETTING_FONTS, $refreshed_list);
179
+
180
+ OMGF_Admin_Notice::set_notice(count($fonts) - count($refreshed_list) . ' ' . __('fonts removed from list. If you haven\'t already, you can now <strong>download</strong> the <strong>fonts</strong>. Otherwise, just (re-)<strong>generate</strong> the <strong>stylesheet</strong>.', 'host-webfonts-local'));
181
  }
182
 
183
  /**
184
+ * Empty cache directory.
 
185
  */
186
+ public function empty_directory()
187
  {
188
+ try {
189
+ delete_option(OMGF_Admin_Settings::OMGF_SETTING_SUBSETS);
190
+ delete_option(OMGF_Admin_Settings::OMGF_SETTING_FONTS);
191
+
192
+ array_map('unlink', array_filter((array) glob(OMGF_UPLOAD_DIR . '/*')));
193
+
194
+ OMGF_Admin_Notice::set_notice(__('Cache directory successfully emptied.', 'host-webfonts-local'));
195
+ } catch (\Exception $e) {
196
+ OMGF_Admin_Notice::set_notice(__('Something went wrong while emptying the cache directory: ', 'host-webfonts-local') . $e->getMessage(), true, 'error', $e->getCode());
197
+ }
198
  }
199
  }
includes/class-api.php ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /* * * * * * * * * * * * * * * * * * * * *
3
+ *
4
+ * ██████╗ ███╗ ███╗ ██████╗ ███████╗
5
+ * ██╔═══██╗████╗ ████║██╔════╝ ██╔════╝
6
+ * ██║ ██║██╔████╔██║██║ ███╗█████╗
7
+ * ██║ ██║██║╚██╔╝██║██║ ██║██╔══╝
8
+ * ╚██████╔╝██║ ╚═╝ ██║╚██████╔╝██║
9
+ * ╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝
10
+ *
11
+ * @package : OMGF
12
+ * @author : Daan van den Bergh
13
+ * @copyright: (c) 2020 Daan van den Bergh
14
+ * @url : https://daan.dev
15
+ * * * * * * * * * * * * * * * * * * * */
16
+
17
+ defined('ABSPATH') || exit;
18
+
19
+ class OMGF_API
20
+ {
21
+ /**
22
+ * @param $query
23
+ *
24
+ * @return array
25
+ */
26
+ public function get_subsets($query)
27
+ {
28
+ $request = wp_remote_get(OMGF_HELPER_URL . $query)['body'];
29
+
30
+ if ($request == 'Not found') {
31
+ return [];
32
+ }
33
+
34
+ $result = json_decode($request);
35
+
36
+ return [
37
+ 'subset_family' => $result->family,
38
+ 'subset_font' => $result->id,
39
+ 'available_subsets' => $result->subsets,
40
+ 'selected_subsets' => []
41
+ ];
42
+ }
43
+
44
+ /**
45
+ * @param $font_family
46
+ * @param $selected_subsets
47
+ *
48
+ * @return array
49
+ */
50
+ public function get_font_styles($font_family, $selected_subsets)
51
+ {
52
+ $request = wp_remote_get(OMGF_HELPER_URL . $font_family . '?subsets=' . $selected_subsets);
53
+ $result = json_decode($request['body']);
54
+
55
+ foreach ($result->variants as $variant) {
56
+ $fonts[] = [
57
+ 'font_id' => $result->id . '-' . $variant->id,
58
+ 'font_family' => $variant->fontFamily,
59
+ 'font_weight' => $variant->fontWeight,
60
+ 'font_style' => $variant->fontStyle,
61
+ 'local' => implode($variant->local, ','),
62
+ 'preload' => 0,
63
+ 'downloaded' => 0,
64
+ 'url_ttf' => $variant->ttf,
65
+ 'url_woff' => $variant->woff,
66
+ 'url_woff2' => $variant->woff2,
67
+ 'url_eot' => $variant->eot
68
+ ];
69
+ }
70
+
71
+ return $fonts;
72
+ }
73
+ }
includes/class-db.php CHANGED
@@ -10,7 +10,7 @@
10
  *
11
  * @package : OMGF
12
  * @author : Daan van den Bergh
13
- * @copyright: (c) 2019 Daan van den Bergh
14
  * @url : https://daan.dev
15
  * * * * * * * * * * * * * * * * * * * */
16
 
@@ -31,24 +31,19 @@ class OMGF_DB
31
  $this->wpdb = $wpdb;
32
  }
33
 
34
- /**
35
- * @return array
36
- */
37
- public function get_download_status()
38
- {
39
- return array(
40
- "downloaded" => count($this->get_downloaded_fonts()),
41
- "total" => count($this->get_total_fonts())
42
- );
43
- }
44
-
45
  /**
46
  * @return array|\Exception
47
  */
48
  public function get_downloaded_fonts()
49
  {
50
  try {
51
- return $this->wpdb->get_results("SELECT * FROM " . OMGF_DB_TABLENAME . " WHERE downloaded = 1");
 
 
 
 
 
 
52
  } catch (\Exception $e) {
53
  return $e;
54
  }
@@ -60,60 +55,42 @@ class OMGF_DB
60
  public function get_preload_fonts()
61
  {
62
  try {
63
- return $this->wpdb->get_results("SELECT * FROM " . OMGF_DB_TABLENAME . " WHERE preload = 1");
 
 
 
 
 
 
64
  } catch(\Exception $e) {
65
  return $e;
66
  }
67
  }
68
 
69
  /**
70
- * @return array|\Exception
71
  */
72
  public function get_total_fonts()
73
  {
74
- try {
75
- return $this->wpdb->get_results("SELECT * FROM " . OMGF_DB_TABLENAME);
76
- } catch (\Exception $e) {
77
- return $e;
78
- }
79
  }
80
 
81
  /**
82
- * @return array|\Exception|null|object
83
  */
84
  public function get_subsets()
85
  {
86
- try {
87
- return $this->wpdb->get_results("SELECT * FROM " . OMGF_DB_TABLENAME . "_subsets");
88
- } catch (\Exception $e) {
89
- return $e;
90
- }
91
- }
92
-
93
- /**
94
- * @param $family
95
- *
96
- * @return array|Exception|object|null
97
- */
98
- public function get_fonts_by_family($family)
99
- {
100
- try {
101
- return $this->wpdb->get_results("SELECT * FROM " . OMGF_DB_TABLENAME . " WHERE font_family = '$family'");
102
- } catch (\Exception $e) {
103
- return $e;
104
- }
105
  }
106
 
107
  /**
108
- * @return Exception|void
109
  */
110
  public function clean_queue()
111
  {
112
- try {
113
- $this->wpdb->query("TRUNCATE TABLE " . OMGF_DB_TABLENAME);
114
- $this->wpdb->query("TRUNCATE TABLE " . OMGF_DB_TABLENAME . "_subsets");
115
- } catch (\Exception $e) {
116
- return $e;
117
- }
118
  }
119
  }
10
  *
11
  * @package : OMGF
12
  * @author : Daan van den Bergh
13
+ * @copyright: (c) 2020 Daan van den Bergh
14
  * @url : https://daan.dev
15
  * * * * * * * * * * * * * * * * * * * */
16
 
31
  $this->wpdb = $wpdb;
32
  }
33
 
 
 
 
 
 
 
 
 
 
 
 
34
  /**
35
  * @return array|\Exception
36
  */
37
  public function get_downloaded_fonts()
38
  {
39
  try {
40
+ $fonts = $this->get_total_fonts();
41
+
42
+ $downloaded = array_filter($fonts, function($font) {
43
+ return $font['downloaded'] == 1;
44
+ });
45
+
46
+ return $downloaded;
47
  } catch (\Exception $e) {
48
  return $e;
49
  }
55
  public function get_preload_fonts()
56
  {
57
  try {
58
+ $fonts = $this->get_total_fonts();
59
+
60
+ $preload = array_filter($fonts, function($font) {
61
+ return $font['preload'] == 1;
62
+ });
63
+
64
+ return $preload;
65
  } catch(\Exception $e) {
66
  return $e;
67
  }
68
  }
69
 
70
  /**
71
+ * @return array
72
  */
73
  public function get_total_fonts()
74
  {
75
+ return get_option(OMGF_Admin_Settings::OMGF_SETTING_FONTS) ?: [];
 
 
 
 
76
  }
77
 
78
  /**
79
+ * @return array
80
  */
81
  public function get_subsets()
82
  {
83
+ return get_option(OMGF_Admin_Settings::OMGF_SETTING_SUBSETS) ?: [];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  }
85
 
86
  /**
87
+ * @return bool
88
  */
89
  public function clean_queue()
90
  {
91
+ delete_option(OMGF_Admin_Settings::OMGF_SETTING_FONTS);
92
+ delete_option(OMGF_Admin_Settings::OMGF_SETTING_SUBSETS);
93
+
94
+ return true;
 
 
95
  }
96
  }
includes/class-omgf.php CHANGED
@@ -10,7 +10,7 @@
10
  *
11
  * @package : OMGF
12
  * @author : Daan van den Bergh
13
- * @copyright: (c) 2019 Daan van den Bergh
14
  * @url : https://daan.dev
15
  * * * * * * * * * * * * * * * * * * * */
16
 
@@ -26,6 +26,7 @@ class OMGF
26
  $this->define_constants();
27
 
28
  if (is_admin()) {
 
29
  $this->do_settings();
30
  $this->add_ajax_hooks();
31
  add_action('plugin_loaded', array($this, 'do_setup'));
@@ -49,7 +50,7 @@ class OMGF
49
  global $wpdb;
50
 
51
  define('OMGF_SITE_URL', 'https://daan.dev');
52
- define('OMGF_DB_TABLENAME', $wpdb->prefix . 'omgf_fonts');
53
  define('OMGF_DB_CHARSET', $wpdb->get_charset_collate());
54
  define('OMGF_HELPER_URL', 'https://google-webfonts-helper.herokuapp.com/api/fonts/');
55
  define('OMGF_FILENAME', 'fonts.css');
@@ -84,6 +85,18 @@ class OMGF
84
  return new OMGF_AJAX();
85
  }
86
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  /**
88
  * @return OMGF_Frontend_Functions
89
  */
10
  *
11
  * @package : OMGF
12
  * @author : Daan van den Bergh
13
+ * @copyright: (c) 2020 Daan van den Bergh
14
  * @url : https://daan.dev
15
  * * * * * * * * * * * * * * * * * * * */
16
 
26
  $this->define_constants();
27
 
28
  if (is_admin()) {
29
+ $this->do_auto_detect();
30
  $this->do_settings();
31
  $this->add_ajax_hooks();
32
  add_action('plugin_loaded', array($this, 'do_setup'));
50
  global $wpdb;
51
 
52
  define('OMGF_SITE_URL', 'https://daan.dev');
53
+ define('OMGF_DB_TABLENAME', $wpdb->prefix . 'omgf_fonts'); // legacy
54
  define('OMGF_DB_CHARSET', $wpdb->get_charset_collate());
55
  define('OMGF_HELPER_URL', 'https://google-webfonts-helper.herokuapp.com/api/fonts/');
56
  define('OMGF_FILENAME', 'fonts.css');
85
  return new OMGF_AJAX();
86
  }
87
 
88
+ /**
89
+ * @return OMGF_Admin_AutoDetect
90
+ */
91
+ private function do_auto_detect()
92
+ {
93
+ $fonts = json_decode(get_option(OMGF_Admin_Settings::OMGF_SETTING_DETECTED_FONTS));
94
+
95
+ if (OMGF_AUTO_DETECT_ENABLED && $fonts) {
96
+ return new OMGF_Admin_AutoDetect($fonts);
97
+ }
98
+ }
99
+
100
  /**
101
  * @return OMGF_Frontend_Functions
102
  */
includes/class-setup.php CHANGED
@@ -10,7 +10,7 @@
10
  *
11
  * @package : OMGF
12
  * @author : Daan van den Bergh
13
- * @copyright: (c) 2019 Daan van den Bergh
14
  * @url : https://daan.dev
15
  * * * * * * * * * * * * * * * * * * * */
16
 
@@ -21,8 +21,12 @@ class OMGF_Setup
21
  /** @var QM_DB $wpdb */
22
  private $wpdb;
23
 
 
24
  private $version;
25
 
 
 
 
26
  /**
27
  * OMGF_Admin_Setup constructor.
28
  */
@@ -30,8 +34,9 @@ class OMGF_Setup
30
  {
31
  global $wpdb;
32
 
33
- $this->wpdb = $wpdb;
34
- $this->version = get_option(OMGF_Admin_Settings::OMGF_SETTING_DB_VERSION) ?: get_option('caos_webfonts_db_version') ?: '1.0.0';
 
35
 
36
  if (version_compare($this->version, OMGF_DB_VERSION) < 0) {
37
  $this->run_db_updates();
@@ -43,133 +48,41 @@ class OMGF_Setup
43
  */
44
  public function run_db_updates()
45
  {
46
- if (version_compare($this->version, '1.6.1') < 0) {
47
- $this->create_webfonts_table();
48
- }
49
- if (version_compare($this->version, '1.7.0') < 0) {
50
- $this->create_subsets_table();
51
- }
52
- if (version_compare($this->version, '1.8.3') < 0) {
53
- $this->add_local_column();
54
- }
55
- if (version_compare($this->version, '2.2.2') < 0) {
56
- $this->rename_tables();
57
- $this->migrate_options();
58
- }
59
- if (version_compare($this->version, OMGF_DB_VERSION) < 0) {
60
- $this->add_preload_column();
61
- }
62
- }
63
 
64
- /**
65
- * Create the table where downloaded webfonts are registered.
66
- *
67
- * @since 1.6.1
68
- */
69
- private function create_webfonts_table()
70
- {
71
- $sql = "CREATE TABLE IF NOT EXISTS " . OMGF_DB_TABLENAME . " (
72
- font_id varchar(191) NOT NULL,
73
- font_family varchar(191) NOT NULL,
74
- font_weight mediumint(5) NOT NULL,
75
- font_style varchar(191) NOT NULL,
76
- downloaded tinyint(1) DEFAULT 0,
77
- url_ttf varchar(191) NULL,
78
- url_woff varchar(191) NULL,
79
- url_woff2 varchar(191) NULL,
80
- url_eot varchar(191) NULL,
81
- UNIQUE KEY (font_id)
82
- ) " . OMGF_DB_CHARSET . ";";
83
- $this->wpdb->query($sql);
84
-
85
- $this->set_db_version('1.6.1');
86
  }
87
 
88
- /**
89
- * Creates the subsets table.
90
- *
91
- * @since 1.7.0
92
- */
93
- private function create_subsets_table()
94
  {
95
- $sql = "CREATE TABLE IF NOT EXISTS " . OMGF_DB_TABLENAME . '_subsets' . " (
96
- subset_font varchar(32) NOT NULL,
97
- subset_family varchar(191) NOT NULL,
98
- available_subsets varchar(191) NOT NULL,
99
- selected_subsets varchar(191) NOT NULL,
100
- UNIQUE KEY (subset_font)
101
- ) " . OMGF_DB_CHARSET . ";";
102
- $this->wpdb->query($sql);
103
-
104
- $this->set_db_version('1.7.0');
105
- }
106
 
107
- /**
108
- * Adds 'local' column.
109
- *
110
- * @since 1.8.3
111
- */
112
- private function add_local_column()
113
- {
114
- $sql = "ALTER TABLE " . OMGF_DB_TABLENAME . " " .
115
- "ADD COLUMN local varchar(128) AFTER font_style;";
116
- $this->wpdb->query($sql);
117
 
118
- $this->set_db_version('1.8.3');
119
- }
120
 
121
- /**
122
- * Delete options with old plugin names and migrate them to new names.
123
- *
124
- * @since 2.2.2
125
- */
126
- private function migrate_options()
127
- {
128
- $table = $this->wpdb->prefix . 'options';
129
- $sql = "SELECT * FROM $table WHERE option_name LIKE '%caos_webfonts%'";
130
- $legacy_options = $this->wpdb->get_results($sql);
131
-
132
- foreach ($legacy_options as &$option) {
133
- $legacy_name = $option->option_name;
134
- $option->option_name = str_replace('caos_webfonts', 'omgf', $legacy_name);
135
- add_option($option->option_name, $option->option_value);
136
- delete_option($legacy_name);
137
  }
138
 
139
- $this->set_db_version('2.2.2');
140
- }
141
-
142
- /**
143
- * Rename tables using OMGF_DB_TABLENAME.
144
- *
145
- * @since 2.2.2
146
- */
147
- private function rename_tables()
148
- {
149
- // Migrate table. DO NOT TOUCH!
150
- $table = $this->wpdb->prefix . 'caos_webfonts';
151
- $subsets = $table . '_subsets';
152
- $sql = "ALTER TABLE $table RENAME TO " . OMGF_DB_TABLENAME;
153
- $sql_subsets = "ALTER TABLE $subsets RENAME TO " . OMGF_DB_TABLENAME . '_subsets';
154
-
155
- $this->wpdb->query($sql);
156
- $this->wpdb->query($sql_subsets);
157
-
158
- $this->set_db_version('2.2.2');
159
  }
160
 
161
  /**
162
- * Adds preload column to OMGF_DB_TABLENAME.
163
- *
164
- * @since 2.5.0
165
  */
166
- private function add_preload_column()
167
  {
168
- $sql = "ALTER TABLE " . OMGF_DB_TABLENAME . " " .
169
- "ADD COLUMN preload tinyint(1) DEFAULT 0 AFTER local;";
170
- $this->wpdb->query($sql);
171
 
172
- $this->set_db_version('2.5.0');
173
  }
174
 
175
  /**
10
  *
11
  * @package : OMGF
12
  * @author : Daan van den Bergh
13
+ * @copyright: (c) 2020 Daan van den Bergh
14
  * @url : https://daan.dev
15
  * * * * * * * * * * * * * * * * * * * */
16
 
21
  /** @var QM_DB $wpdb */
22
  private $wpdb;
23
 
24
+ /** @var string $version */
25
  private $version;
26
 
27
+ /** @var string $table */
28
+ private $table;
29
+
30
  /**
31
  * OMGF_Admin_Setup constructor.
32
  */
34
  {
35
  global $wpdb;
36
 
37
+ $this->wpdb = $wpdb;
38
+ $this->version = get_option(OMGF_Admin_Settings::OMGF_SETTING_DB_VERSION);
39
+ $this->table = OMGF_DB_TABLENAME;
40
 
41
  if (version_compare($this->version, OMGF_DB_VERSION) < 0) {
42
  $this->run_db_updates();
48
  */
49
  public function run_db_updates()
50
  {
51
+ $this->migrate_db();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
+ $this->drop_tables();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  }
55
 
56
+ private function migrate_db()
 
 
 
 
 
57
  {
58
+ $current_fonts = $this->wpdb->get_results("SELECT * FROM " . OMGF_DB_TABLENAME);
59
+ $fonts_array = json_decode(json_encode($current_fonts), true);
 
 
 
 
 
 
 
 
 
60
 
61
+ update_option(OMGF_Admin_Settings::OMGF_SETTING_FONTS, $fonts_array);
 
 
 
 
 
 
 
 
 
62
 
63
+ $current_subsets = $this->wpdb->get_results("SELECT * FROM " . OMGF_DB_TABLENAME . '_subsets');
64
+ $subsets_array = json_decode(json_encode($current_subsets), true);
65
 
66
+ foreach ($subsets_array as &$subset) {
67
+ $available = $subset['available_subsets'];
68
+ $selected = $subset['selected_subsets'];
69
+ $subset['available_subsets'] = strpos($available, ',') !== false ? explode(',', $available) : [ $available ];
70
+ $subset['selected_subsets'] = strpos($selected, ',') !== false ? explode(',', $selected) : [ $selected ];
 
 
 
 
 
 
 
 
 
 
 
71
  }
72
 
73
+ update_option(OMGF_Admin_Settings::OMGF_SETTING_SUBSETS, $subsets_array);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  }
75
 
76
  /**
77
+ * Drop all tables associated with OMGF.
 
 
78
  */
79
+ private function drop_tables()
80
  {
81
+ $this->wpdb->query(
82
+ 'DROP TABLE IF EXISTS ' . $this->table . ', ' . $this->table . '_subsets;'
83
+ );
84
 
85
+ $this->set_db_version(OMGF_DB_VERSION);
86
  }
87
 
88
  /**
includes/class-uninstall.php CHANGED
@@ -10,7 +10,7 @@
10
  *
11
  * @package : OMGF
12
  * @author : Daan van den Bergh
13
- * @copyright: (c) 2019 Daan van den Bergh
14
  * @url : https://daan.dev
15
  * * * * * * * * * * * * * * * * * * * */
16
 
@@ -24,9 +24,6 @@ class OMGF_Uninstall
24
  /** @var array $options */
25
  private $options;
26
 
27
- /** @var string $table */
28
- private $table;
29
-
30
  /** @var string $cacheDir */
31
  private $cacheDir;
32
 
@@ -45,11 +42,9 @@ class OMGF_Uninstall
45
 
46
  $this->wpdb = $wpdb;
47
  $this->options = $settings->get_settings();
48
- $this->table = OMGF_DB_TABLENAME;
49
  $this->cacheDir = OMGF_UPLOAD_DIR;
50
 
51
  $this->remove_db_entries();
52
- $this->drop_tables();
53
  $this->delete_files();
54
  $this->delete_dir();
55
  }
@@ -64,16 +59,6 @@ class OMGF_Uninstall
64
  }
65
  }
66
 
67
- /**
68
- * Drop all tables associated with OMGF.
69
- */
70
- private function drop_tables()
71
- {
72
- $this->wpdb->query(
73
- 'DROP TABLE IF EXISTS ' . $this->table . ', ' . $this->table . '_subsets;'
74
- );
75
- }
76
-
77
  /**
78
  * Delete all files stored in the cache directory.
79
  *
10
  *
11
  * @package : OMGF
12
  * @author : Daan van den Bergh
13
+ * @copyright: (c) 2020 Daan van den Bergh
14
  * @url : https://daan.dev
15
  * * * * * * * * * * * * * * * * * * * */
16
 
24
  /** @var array $options */
25
  private $options;
26
 
 
 
 
27
  /** @var string $cacheDir */
28
  private $cacheDir;
29
 
42
 
43
  $this->wpdb = $wpdb;
44
  $this->options = $settings->get_settings();
 
45
  $this->cacheDir = OMGF_UPLOAD_DIR;
46
 
47
  $this->remove_db_entries();
 
48
  $this->delete_files();
49
  $this->delete_dir();
50
  }
59
  }
60
  }
61
 
 
 
 
 
 
 
 
 
 
 
62
  /**
63
  * Delete all files stored in the cache directory.
64
  *
includes/frontend/class-functions.php CHANGED
@@ -10,7 +10,7 @@
10
  *
11
  * @package : OMGF
12
  * @author : Daan van den Bergh
13
- * @copyright: (c) 2019 Daan van den Bergh
14
  * @url : https://daan.dev
15
  * * * * * * * * * * * * * * * * * * * */
16
 
10
  *
11
  * @package : OMGF
12
  * @author : Daan van den Bergh
13
+ * @copyright: (c) 2020 Daan van den Bergh
14
  * @url : https://daan.dev
15
  * * * * * * * * * * * * * * * * * * * */
16
 
js/hwl-admin.js DELETED
@@ -1,531 +0,0 @@
1
- /* * * * * * * * * * * * * * * * * * * * *
2
- *
3
- * ██████╗ ███╗ ███╗ ██████╗ ███████╗
4
- * ██╔═══██╗████╗ ████║██╔════╝ ██╔════╝
5
- * ██║ ██║██╔████╔██║██║ ███╗█████╗
6
- * ██║ ██║██║╚██╔╝██║██║ ██║██╔══╝
7
- * ╚██████╔╝██║ ╚═╝ ██║╚██████╔╝██║
8
- * ╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝
9
- *
10
- * @package : OMGF
11
- * @author : Daan van den Bergh
12
- * @copyright: (c) 2019 Daan van den Bergh
13
- * @url : https://daan.dev
14
- * * * * * * * * * * * * * * * * * * * */
15
-
16
- /**
17
- * When user is done typing, trigger search.
18
- */
19
- function hwlClickSearch()
20
- {
21
- let input = jQuery('#search-field');
22
- searchQuery = input.val().replace(/\s/g, '-').toLowerCase();
23
- hwlSearchFontSubsets(searchQuery)
24
- }
25
-
26
- /**
27
- * Return available subsets for searched font.
28
- *
29
- * @param queriedFonts
30
- */
31
- function hwlSearchFontSubsets(queriedFonts)
32
- {
33
- let searchField = jQuery('#search-field');
34
- let searchButton = jQuery('#search-btn');
35
-
36
- jQuery.ajax({
37
- type: 'POST',
38
- url: ajaxurl,
39
- data: {
40
- action: 'omgf_ajax_search_font_subsets',
41
- search_query: queriedFonts
42
- },
43
- dataType: 'json',
44
- beforeSend: function() {
45
- hwlUpdateInputValue(searchButton, 'Searching...', '0 20px');
46
- searchField.val('');
47
- },
48
- error: function(response) {
49
- displayError(response.responseJSON.data);
50
- },
51
- success: function(response) {
52
- hwlUpdateInputValue(searchButton, 'Search', '0 36px');
53
- hwlRenderAvailableSubsets(response);
54
- }
55
- })
56
- }
57
-
58
- function hwlAutoDetectFonts()
59
- {
60
- let detectButton = jQuery('#detect-btn');
61
-
62
- jQuery.ajax({
63
- type: 'POST',
64
- url: ajaxurl,
65
- data: {
66
- action: 'omgf_ajax_auto_detect'
67
- },
68
- dataType: 'json',
69
- beforeSend: function() {
70
- if (omgf.auto_detect_enabled === '' && omgf.detected_fonts === '') {
71
- hwlCleanQueue();
72
- }
73
- },
74
- error: function (response) {
75
- displayError(response.responseJSON.data);
76
- },
77
- complete: function(response) {
78
- if (omgf.auto_detect_enabled === '' && omgf.detected_fonts === '') {
79
- hwlScrollTop();
80
- jQuery('#hwl-admin-notices').append("<div class='notice notice-success is-dismissible'><p>" + response.responseJSON.data + "</p></div>");
81
- hwlUpdateInputValue(detectButton, 'Enabled', '0 38px 1px');
82
- } else {
83
- try {
84
- hwlUpdateInputValue(detectButton, 'Auto-detect', '0 36px 1px');
85
- hwlRenderAvailableSubsets(response.responseJSON);
86
- } catch(error) {
87
- hwlScrollTop();
88
- jQuery('#hwl-admin-notices').append("<div class='notice notice-success is-dismissible'><p>Oops! Something went wrong. " + error + ". <a href='javascript:location.reload();'>Refresh this page</a> and try again. If it still fails, <a href='javascript:hwlEmptyDir();'>empty the cache</a> and <a href='javascript:location.reload();'>refresh this page</a>.");
89
- }
90
- }
91
- }
92
- })
93
- }
94
-
95
- /**
96
- * Run this after refresh when both statements return true.
97
- */
98
- if (omgf.auto_detect_enabled !== '' && omgf.detected_fonts !== '') {
99
- hwlAutoDetectFonts();
100
- }
101
-
102
- /**
103
- * Print available subsets
104
- *
105
- * @param response
106
- */
107
- function hwlRenderAvailableSubsets(response)
108
- {
109
- let data = response.data;
110
- let subsetArray = data.subsets === undefined ? data : data.subsets;
111
-
112
- for (let ii = 0; ii < subsetArray.length; ii++) {
113
- subsets = subsetArray[ii]['subsets'];
114
- family = subsetArray[ii]['family'];
115
-
116
- id = subsetArray[ii]['id'];
117
- usedStyles = subsetArray[ii]['used_styles'];
118
-
119
- if (subsets === null) {
120
- subsets = ['latin'];
121
- }
122
-
123
- length = subsets.length;
124
- renderedSubsets = [];
125
-
126
- for (let iii = 0; iii < length; iii++) {
127
- renderedSubsets[iii] = `<td><label><input name="${id}" value="${subsets[iii]}" type="checkbox" onclick='hwlGenerateSearchQuery("${id}", ${JSON.stringify(usedStyles)})' />${subsets[iii]}</label></td>`;
128
- }
129
-
130
- jQuery('#hwl-subsets').append('<tr valign="top" id="' + id + '"><td><input type="text" class="hwl-subset-font-family" value="' + family + '" readonly/></td>' + renderedSubsets + '</tr>');
131
- jQuery('#hwl-results').append("<tbody id='" + 'hwl-section-' + id + "'></tbody>");
132
- }
133
-
134
- if (data['auto-detect'] === true) {
135
- jQuery('#hwl-subsets input[type="checkbox"]').each(function() {
136
- /**
137
- * These fonts are used by WP Admin. But might be used by front-end as well.
138
- * That's why we do return them, but do not trigger a search by default.
139
- */
140
- if (this.getAttribute('name') !== 'open-sans' && this.getAttribute('name') !== 'noto-serif') {
141
- this.click();
142
- }
143
- });
144
- }
145
- }
146
-
147
- /**
148
- * Generate search query for selected subsets
149
- *
150
- * @param id
151
- * @param usedStyles
152
- */
153
- function hwlGenerateSearchQuery(id, usedStyles = null)
154
- {
155
- let subsets = [];
156
- checked = jQuery("input[name='" + id + "']:checked");
157
-
158
- jQuery.each(checked, function() {
159
- subsets.push(jQuery(this).val());
160
- });
161
-
162
- subsets.join();
163
- hwlSearchGoogleFonts(id, subsets, usedStyles);
164
- }
165
-
166
- /**
167
- * Triggers the AJAX-request to Google Webfont Helper.
168
- *
169
- * @param id
170
- * @param subsets
171
- * @param usedStyles
172
- */
173
- function hwlSearchGoogleFonts(id, subsets, usedStyles = null)
174
- {
175
- let loadingDiv = jQuery('#hwl-warning .loading');
176
- let errorDiv = jQuery('#hwl-warning .error');
177
- jQuery.ajax({
178
- type: 'POST',
179
- url: ajaxurl,
180
- data: {
181
- action: 'omgf_ajax_search_google_fonts',
182
- search_query: id,
183
- search_subsets: subsets,
184
- used_styles: usedStyles
185
- },
186
- dataType: 'json',
187
- beforeSend: function() {
188
- loadingDiv.show()
189
- },
190
- error: function(response) {
191
- displayError(response.responseJSON.data);
192
- },
193
- success: function(response) {
194
- loadingDiv.hide();
195
- errorDiv.hide();
196
- if(response['responseText'] !== 'Not found') {
197
- hwlRenderAvailableFonts(response);
198
- } else {
199
- errorDiv.show();
200
- }
201
- }
202
- })
203
- }
204
-
205
- function displayError(message) {
206
- let loadingDiv = jQuery('#hwl-warning .loading');
207
- let errorDiv = jQuery('#hwl-warning .error');
208
-
209
- loadingDiv.hide();
210
- errorDiv.show();
211
- hwlScrollTop();
212
- jQuery('#hwl-admin-notices').html("<div class='notice notice-success is-dismissible'><p>Oops! Something went wrong. " + message + ".");
213
- }
214
-
215
- /**
216
- * Displays the search results
217
- *
218
- * @param results
219
- */
220
- function hwlRenderAvailableFonts(results)
221
- {
222
- variants = results.data.variants === undefined ? results.responseJSON.data.variants : results.data.variants;
223
- length = variants.length;
224
- renderedFonts = [];
225
- for(iii = 0; iii < length; iii++) {
226
- fontFamily = variants[iii].fontFamily.replace(/'/g, '');
227
- fontId = variants[iii].id;
228
- font = fontFamily.replace(/\s+/g, '-').toLowerCase() + '-' + variants[iii].id;
229
- fontWeight = variants[iii].fontWeight;
230
- fontStyle = variants[iii].fontStyle;
231
- fontLocal = variants[iii].local;
232
- renderedFonts[iii] = `<tr id="row-${font}" valign="top" align="center">
233
- <td>
234
- <input readonly type="text" value="${fontFamily}" name="caos_webfonts_array][${font}][font-family]" />
235
- </td>
236
- <td>
237
- <input readonly type="text" value="${fontStyle}" name="caos_webfonts_array][${font}][font-style]" />
238
- </td>
239
- <td>
240
- <input readonly type="text" value="${fontWeight}" name="caos_webfonts_array][${font}][font-weight]" />
241
- </td>
242
- <td>
243
- <input type="checkbox" value="1" name="caos_webfonts_array][${font}][preload]" />
244
- </td>
245
- <td>
246
- <input type="hidden" value="${fontId}" name="caos_webfonts_array][${font}][id]" />
247
- <input type="hidden" value="${fontLocal}" name="caos_webfonts_array][${font}][local]" />
248
- <input type="hidden" value="${variants[iii].ttf}" name="caos_webfonts_array][${font}][url][ttf]" />
249
- <input type="hidden" value="${variants[iii].woff}" name="caos_webfonts_array][${font}][url][woff]" />
250
- <input type="hidden" value="${variants[iii].woff2}" name="caos_webfonts_array][${font}][url][woff2]" />
251
- <input type="hidden" value="${variants[iii].eot}" name="caos_webfonts_array][${font}][url][eot]" />
252
- <div class="hwl-remove">
253
- <a onclick="hwlRemoveRow('row-${font}')"><small>remove</small></a>
254
- </div>
255
- </td>
256
- </tr>`
257
- }
258
- console.log(fontFamily.replace(/\s+/g, '-').toLowerCase());
259
- jQuery('#hwl-section-' + fontFamily.replace(/\s+/g, '-').toLowerCase()).html(renderedFonts)
260
- }
261
-
262
- /**
263
- * Gathers all information about the subsets
264
- *
265
- * @returns {{}}
266
- */
267
- function hwlGatherSelectedSubsets()
268
- {
269
- rows = jQuery('#hwl-subsets tr');
270
- length = rows.length;
271
- subsets = {};
272
- jQuery(rows).each(function() {
273
- id = this.id;
274
- checkboxes = jQuery("input[name='" + id + "']");
275
- checked = jQuery("input[name='" + id + "']:checked");
276
-
277
- selectedSubsets = [];
278
- jQuery.each(checked, function() {
279
- selectedSubsets.push(jQuery(this).val());
280
- });
281
- selectedSubsets.join();
282
-
283
- availableSubsets = [];
284
- jQuery.each(checkboxes, function() {
285
- availableSubsets.push(jQuery(this).val());
286
- });
287
- availableSubsets.join();
288
-
289
- family = jQuery(this).find('.hwl-subset-font-family').val();
290
-
291
- subsets[id] = {};
292
- subsets[id]['family'] = {};
293
- subsets[id]['family'] = family;
294
- subsets[id]['selected'] = {};
295
- subsets[id]['selected'] = selectedSubsets;
296
- subsets[id]['available'] = {};
297
- subsets[id]['available'] = availableSubsets;
298
- });
299
-
300
- return subsets;
301
- }
302
-
303
- /**
304
- * Triggered when 'Download Fonts' is clicked.
305
- */
306
- function hwlDownloadFonts()
307
- {
308
- let hwlFonts = hwlSerializeArray(jQuery('#hwl-options-form'));
309
- let hwlSubsets = hwlGatherSelectedSubsets();
310
- let downloadButton = jQuery('#save-btn');
311
- jQuery.ajax({
312
- type: 'POST',
313
- url: ajaxurl,
314
- data: {
315
- action: 'omgf_ajax_download_fonts',
316
- subsets: hwlSubsets,
317
- fonts: hwlFonts,
318
- },
319
- beforeSend: function() {
320
- hwlUpdateStatusBar(0);
321
- hwlGetDownloadStatus();
322
- hwlUpdateInputValue(downloadButton, 'Downloading...', '0 14px 1px');
323
- },
324
- success: function() {
325
- clearTimeout(downloadStatus);
326
-
327
- hwlUpdateInputValue(downloadButton, 'Done!', '0 41px 1px');
328
- hwlUpdateStatusBar(100);
329
-
330
- setTimeout(function() {
331
- hwlUpdateInputValue(downloadButton, 'Download Fonts');
332
- }, 2500);
333
- },
334
- error: function(message) {
335
- clearTimeout(downloadStatus);
336
-
337
- errorText = message.responseJSON.data;
338
- errorCode = message.status;
339
-
340
- var errorMessage = '<div id="setting-error-settings_updated" class="error settings-error notice is-dismissible"><p><strong>Error: ' + errorCode + '</strong> - ' + errorText + '</p></div>';
341
-
342
- jQuery('html, body').animate({scrollTop: 0}, 800);
343
- jQuery(errorMessage).insertAfter('.wrap h1');
344
-
345
- hwlUpdateInputValue(downloadButton, 'Download Fonts');
346
- }
347
- })
348
- }
349
-
350
- /**
351
- * Gets a JSON object with the download progress information
352
- */
353
- function hwlGetDownloadStatus()
354
- {
355
- jQuery.ajax({
356
- type: 'POST',
357
- url: ajaxurl,
358
- data: {
359
- action: 'omgf_ajax_get_download_status'
360
- },
361
- dataType: 'text json',
362
- success: function(response) {
363
- downloaded = response.downloaded;
364
- total = response.total;
365
- progress = (100 / total) * downloaded;
366
-
367
- hwlUpdateStatusBar(progress);
368
- }
369
- });
370
- downloadStatus = setTimeout(hwlGetDownloadStatus, 1000);
371
- }
372
-
373
- /**
374
- * Updated Status-bar with the set progress
375
- *
376
- * @param progress
377
- */
378
- function hwlUpdateStatusBar(progress)
379
- {
380
- progress = Math.round(progress) + '%';
381
- jQuery('#caos-status-progress-bar').width(progress);
382
- jQuery('.caos-status-progress-percentage').html(progress);
383
- }
384
-
385
- /**
386
- * Call the generate-stylesheet script.
387
- */
388
- function hwlGenerateStylesheet()
389
- {
390
- let hwlFonts = hwlSerializeArray(jQuery('#hwl-options-form'));
391
- let generateButton = jQuery('#generate-btn');
392
- jQuery.ajax({
393
- type: 'POST',
394
- url: ajaxurl,
395
- data: {
396
- action: 'omgf_ajax_generate_styles',
397
- selected_fonts: hwlFonts
398
- },
399
- beforeSend: function() {
400
- hwlUpdateInputValue(generateButton, 'Generating...', '0 33px 1px');
401
- },
402
- success: function() {
403
- hwlUpdateInputValue(generateButton, 'Done!', '0 54px 1px');
404
- setTimeout(function() {
405
- hwlUpdateInputValue(generateButton, 'Generate Stylesheet');
406
- }, 2500);
407
- },
408
- error: function(response) {
409
- hwlScrollTop();
410
- jQuery('#hwl-admin-notices').append(
411
- `<div class="notice notice-error is-dismissible">
412
- <p>The stylesheet could not be created: ${response.responseText}</p>
413
- </div>`
414
- );
415
- hwlUpdateInputValue(generateButton, 'Generate Stylesheet');
416
- }
417
- })
418
- }
419
-
420
- /**
421
- * Updates the value of any input to show status updates
422
- *
423
- * @param input
424
- * @param text
425
- * @param padding
426
- */
427
- function hwlUpdateInputValue(input, text, padding = '0 10px 1px')
428
- {
429
- input.val(text);
430
- input.css('padding', padding);
431
- }
432
-
433
- /**
434
- * Remove all files within the configured cache dir.
435
- */
436
- function hwlEmptyDir()
437
- {
438
- jQuery.ajax({
439
- type: 'POST',
440
- url: ajaxurl,
441
- data: {
442
- action: 'omgf_ajax_empty_dir'
443
- },
444
- success: function() {
445
- hwlCleanQueue();
446
- hwlUpdateStatusBar(0)
447
- }
448
- });
449
- }
450
-
451
- /**
452
- * Trigger the DB clean-up and clean list.
453
- */
454
- function hwlCleanQueue()
455
- {
456
- jQuery.ajax({
457
- type: 'POST',
458
- url: ajaxurl,
459
- data: {
460
- action: 'omgf_ajax_clean_queue'
461
- },
462
- success: function() {
463
- jQuery('.caos-status-progress-percentage').html('0%');
464
- jQuery('#hwl-subsets, tbody[id^=hwl-section]').empty()
465
- }
466
- })
467
- }
468
-
469
- /**
470
- * Scroll to top-effect.
471
- */
472
- function hwlScrollTop()
473
- {
474
- setTimeout(function () {
475
- jQuery('html, body').animate({
476
- scrollTop: 0
477
- }, 1500)
478
- }, 1500)
479
- }
480
-
481
- /**
482
- * Serialize form data to a multi-dimensional array.
483
- */
484
- function hwlSerializeArray(data)
485
- {
486
- let result = [];
487
- data.each(function() {
488
- fields = {};
489
- jQuery.each(jQuery(this).serializeArray(), function() {
490
- fields[this.name] = this.value
491
- });
492
- result.push(fields)
493
- });
494
- return result
495
- }
496
-
497
- /**
498
- * Remove selected row.
499
- *
500
- * @param rowId
501
- */
502
- function hwlRemoveRow(rowId)
503
- {
504
- jQuery('#' + rowId).remove();
505
- }
506
-
507
- /**
508
- * TODO: Move all above functions within document.ready().
509
- */
510
- jQuery(document).ready(function($) {
511
- /**
512
- * Toggle different options that aren't compatible with each other.
513
- */
514
- $('#omgf_web_font_loader, #omgf_preload').click(function () {
515
- if (this.className === 'omgf_web_font_loader' && this.checked === true) {
516
- $('#omgf_preload').attr('checked', false);
517
- }
518
-
519
- if (this.className === 'omgf_preload' && this.checked === true) {
520
- $('#omgf_web_font_loader').attr('checked', false);
521
- }
522
- });
523
-
524
- $('#omgf_relative_url').click(function () {
525
- if (this.checked === true) {
526
- $('#omgf_cdn_url').prop('disabled', true);
527
- } else {
528
- $('#omgf_cdn_url').prop('disabled', false);
529
- }
530
- })
531
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
js/omgf-admin.js ADDED
@@ -0,0 +1,318 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* * * * * * * * * * * * * * * * * * * * *
2
+ *
3
+ * ██████╗ ███╗ ███╗ ██████╗ ███████╗
4
+ * ██╔═══██╗████╗ ████║██╔════╝ ██╔════╝
5
+ * ██║ ██║██╔████╔██║██║ ███╗█████╗
6
+ * ██║ ██║██║╚██╔╝██║██║ ██║██╔══╝
7
+ * ╚██████╔╝██║ ╚═╝ ██║╚██████╔╝██║
8
+ * ╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝
9
+ *
10
+ * @package : OMGF
11
+ * @author : Daan van den Bergh
12
+ * @copyright: (c) 2020 Daan van den Bergh
13
+ * @url : https://daan.dev
14
+ * * * * * * * * * * * * * * * * * * * */
15
+
16
+ jQuery(document).ready(function ($) {
17
+ var omgf_admin = {
18
+ // XHR
19
+ search_fonts_xhr: false,
20
+ preload_font_style_xhr: false,
21
+ refresh_font_style_list_xhr: false,
22
+ download_fonts_xhr: false,
23
+ generate_stylesheet_xhr: false,
24
+ empty_cache_directory_xhr: false,
25
+
26
+ // Data
27
+ font_families: [],
28
+ preload_font_styles: [],
29
+ font_style_list: [],
30
+
31
+ // Selectors
32
+ $loader: $('.omgf-loading'),
33
+ $font_families: $('.omgf-subset-font-family'),
34
+ $subsets: $('.omgf-subset'),
35
+ $preload_font_styles: $('.omgf-font-preload'),
36
+ $removed_font_style: $('.omgf-font-remove'),
37
+
38
+ // Timeout for User Interaction
39
+ timeout: 2000,
40
+
41
+ /**
42
+ * Initialize all on click events.
43
+ */
44
+ init: function () {
45
+ // Generate Stylesheet Section
46
+ this.$subsets.on('click', function () { setTimeout(omgf_admin.search_google_fonts, this.timeout)});
47
+ this.$preload_font_styles.on('click', function() { setTimeout(omgf_admin.preload_font_style, this.timeout)});
48
+ this.$removed_font_style.on('click', this.remove_font_style);
49
+
50
+ // Buttons
51
+ $('#omgf-search-subsets').on('click', this.click_search);
52
+ $('#omgf-auto-detect').on('click', this.enable_auto_detect);
53
+ $('#omgf-download').on('click', this.download_fonts);
54
+ $('#omgf-generate').on('click', this.generate_stylesheet);
55
+ $('#omgf-empty').on('click', this.empty_cache_directory);
56
+ },
57
+
58
+ /**
59
+ * Triggered when Search is clicked.
60
+ */
61
+ click_search: function () {
62
+ searchQuery = $('#omgf-search').val().replace(/\s/g, '-').toLowerCase();
63
+ omgf_admin.search_subsets(searchQuery);
64
+ },
65
+
66
+ /**
67
+ * Enable Auto Detect.
68
+ */
69
+ enable_auto_detect: function () {
70
+ $.ajax({
71
+ type: 'POST',
72
+ url: ajaxurl,
73
+ data: {
74
+ action: 'omgf_ajax_enable_auto_detect'
75
+ },
76
+ dataType: 'json',
77
+ beforeSend: function () {
78
+ omgf_admin.show_loader('.omgf-search-box');
79
+ },
80
+ complete: function () {
81
+ location.reload();
82
+ }
83
+ })
84
+ },
85
+
86
+ /**
87
+ * Triggered by Click Search
88
+ *
89
+ * @param query
90
+ */
91
+ search_subsets: function (query) {
92
+ jQuery.ajax({
93
+ type: 'POST',
94
+ url: ajaxurl,
95
+ data: {
96
+ action: 'omgf_ajax_search_font_subsets',
97
+ search_query: query
98
+ },
99
+ dataType: 'json',
100
+ beforeSend: function () {
101
+ omgf_admin.show_loader('.omgf-search-box')
102
+ },
103
+ complete: function() {
104
+ location.reload();
105
+ }
106
+ });
107
+ },
108
+
109
+ /**
110
+ * Show loader on element
111
+ *
112
+ * @param element
113
+ */
114
+ show_loader: function (element) {
115
+ let clone = omgf_admin.$loader.clone();
116
+
117
+ $(element).append(clone).css({
118
+ 'position': 'relative',
119
+ 'opacity': '0.5'
120
+ });
121
+
122
+ clone.show();
123
+ },
124
+
125
+ /**
126
+ * Triggered on Search
127
+ */
128
+ search_google_fonts: function () {
129
+ if (omgf_admin.search_fonts_xhr) {
130
+ omgf_admin.search_fonts_xhr.abort();
131
+ }
132
+
133
+ omgf_admin.font_families = omgf_admin.$font_families.map(function () {
134
+ return $(this).data('font-family');
135
+ }).get();
136
+
137
+ omgf_admin.font_families.forEach(function(font, index) {
138
+ omgf_admin.font_families[index] = {};
139
+ omgf_admin.font_families[index].selected_subsets = [];
140
+
141
+ $('input[data-subset-font-family="' + font + '"]:checked').each(function(i) {
142
+ omgf_admin.font_families[index].subset_font = font;
143
+ omgf_admin.font_families[index].selected_subsets[i] = this.value;
144
+ });
145
+ });
146
+
147
+ omgf_admin.search_fonts_xhr = $.ajax({
148
+ type: 'POST',
149
+ url: ajaxurl,
150
+ data: {
151
+ action: 'omgf_ajax_search_google_fonts',
152
+ search_google_fonts: omgf_admin.font_families,
153
+ },
154
+ dataType: 'json',
155
+ beforeSend: function() {
156
+ omgf_admin.show_loader('#omgf-font-styles-list');
157
+ },
158
+ complete: function () {
159
+ location.reload()
160
+ }
161
+ });
162
+ },
163
+
164
+ /**
165
+ * Triggered when preload is checked. If multiple are checked, all are processed at once.
166
+ */
167
+ preload_font_style: function() {
168
+ if (omgf_admin.preload_font_style_xhr) {
169
+ omgf_admin.preload_font_style_xhr.abort();
170
+ }
171
+
172
+ omgf_admin.preload_font_styles = $('.omgf-font-preload:checked').map(function () {
173
+ return $(this).data('preload');
174
+ }).get();
175
+
176
+ omgf_admin.preload_font_style_xhr = $.ajax({
177
+ type: 'POST',
178
+ url: ajaxurl,
179
+ data: {
180
+ action: 'omgf_ajax_preload_font_style',
181
+ preload_font_styles: omgf_admin.preload_font_styles
182
+ },
183
+ dataType: 'json',
184
+ success: function () {
185
+ location.reload();
186
+ }
187
+ });
188
+ },
189
+
190
+ /**
191
+ * Triggered when remove is clicked. If multiple are checked, all are processed at once.
192
+ */
193
+ remove_font_style: function() {
194
+ row = '#' + $(this).data('row');
195
+
196
+ omgf_admin.show_loader(row);
197
+ $(row).removeClass('omgf-font-style');
198
+
199
+ setTimeout(omgf_admin.refresh_font_style_list, this.timeout);
200
+ },
201
+
202
+ /**
203
+ * Triggered after remove to sync data to backend.
204
+ */
205
+ refresh_font_style_list: function () {
206
+ if (omgf_admin.refresh_font_style_list_xhr) {
207
+ omgf_admin.refresh_font_style_list_xhr.abort();
208
+ }
209
+
210
+ omgf_admin.font_style_list = $('.omgf-font-style').map(function () {
211
+ return $(this).data('font-id');
212
+ }).get();
213
+
214
+ omgf_admin.refresh_font_style_list_xhr = $.ajax({
215
+ type: 'POST',
216
+ url: ajaxurl,
217
+ data: {
218
+ action: 'omgf_ajax_refresh_font_style_list',
219
+ font_styles: omgf_admin.font_style_list
220
+ },
221
+ dataType: 'json',
222
+ success: function() {
223
+ location.reload();
224
+ }
225
+ });
226
+ },
227
+
228
+ /**
229
+ * Download fonts and refresh window.
230
+ */
231
+ download_fonts: function () {
232
+ if (omgf_admin.download_fonts_xhr) {
233
+ omgf_admin.download_fonts_xhr.abort();
234
+ }
235
+
236
+ omgf_admin.download_fonts_xhr = $.ajax({
237
+ type: 'POST',
238
+ url: ajaxurl,
239
+ data: {
240
+ action: 'omgf_ajax_download_fonts'
241
+ },
242
+ beforeSend: function() {
243
+ $('#omgf-download').attr('disabled', true);
244
+ omgf_admin.show_loader('.omgf-search-section');
245
+ },
246
+ complete: function() {
247
+ location.reload();
248
+ }
249
+ })
250
+ },
251
+
252
+ /**
253
+ * Generate stylesheet and refresh window.
254
+ */
255
+ generate_stylesheet: function () {
256
+ $.ajax({
257
+ type: 'POST',
258
+ url: ajaxurl,
259
+ data: {
260
+ action: 'omgf_ajax_generate_styles',
261
+ },
262
+ beforeSend: function() {
263
+ $('#omgf-generate').attr('disabled', true);
264
+ omgf_admin.show_loader('.omgf-search-section');
265
+ },
266
+ complete: function() {
267
+ location.reload();
268
+ }
269
+ })
270
+ },
271
+
272
+ /**
273
+ * Empty queue, db and cache directory.
274
+ */
275
+ empty_cache_directory: function () {
276
+ if (omgf_admin.empty_cache_directory_xhr) {
277
+ omgf_admin.empty_cache_directory_xhr.abort();
278
+ }
279
+
280
+ omgf_admin.empty_cache_directory_xhr = $.ajax({
281
+ type: 'POST',
282
+ url: ajaxurl,
283
+ data: {
284
+ action: 'omgf_ajax_empty_dir'
285
+ },
286
+ beforeSend: function() {
287
+ omgf_admin.show_loader('.omgf-search-section');
288
+ },
289
+ complete: function() {
290
+ location.reload();
291
+ }
292
+ });
293
+ },
294
+ };
295
+
296
+ omgf_admin.init();
297
+
298
+ /**
299
+ * Toggle different options that aren't compatible with each other.
300
+ */
301
+ $('#omgf_web_font_loader, #omgf_preload').click(function () {
302
+ if (this.className === 'omgf_web_font_loader' && this.checked === true) {
303
+ $('#omgf_preload').attr('checked', false);
304
+ }
305
+
306
+ if (this.className === 'omgf_preload' && this.checked === true) {
307
+ $('#omgf_web_font_loader').attr('checked', false);
308
+ }
309
+ });
310
+
311
+ $('#omgf_relative_url').click(function () {
312
+ if (this.checked === true) {
313
+ $('#omgf_cdn_url').prop('disabled', true);
314
+ } else {
315
+ $('#omgf_cdn_url').prop('disabled', false);
316
+ }
317
+ })
318
+ });
readme.txt CHANGED
@@ -2,8 +2,8 @@
2
  Contributors: DaanvandenBergh
3
  Tags: google, fonts, preload, font-display, webfonts, subsets, remove, minimize, external, requests, leverage, browser, cache
4
  Requires at least: 4.6
5
- Tested up to: 5.3
6
- Stable tag: 2.5.0
7
  Requires PHP: 7.0
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
@@ -20,7 +20,9 @@ That's it. You're done!
20
 
21
  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.
22
 
23
- = Features =
 
 
24
  - *Automatically detect* which Google Fonts your theme is using or,
25
  - Easily find additional fonts in any/multiple subsets,
26
  - Download them and generate a stylesheet, which is automatically added to your header using WordPress' wp_head()-function,
@@ -32,8 +34,6 @@ This will *decrease your pageload times*, *leverage browser cache*, *minimize DN
32
  - Auto-generates the local source for webfonts,
33
  - Automatically remove any fonts loaded from fonts.gstatic.com or fonts.googleapis.com.
34
 
35
- Please keep in mind that, although I try to make the configuration of this plugin as easy as possible, the concept of locally hosting a file or optimizing Google Fonts for *Pagespeed Insights* or *GT Metrix* has proven to be confusing for some people. If you're not sure of what your doing, please consult a SEO expert or Webdeveloper to help you with the configuration and optimization of your WordPress blog. Or [hire me to do it for you](https://woosh.dev/wordpress-services/omgf-expert-configuration/).
36
-
37
  == Installation ==
38
 
39
  1. Upload the plugin files to the `/wp-content/plugins/host-webfonts-local` directory, or install the plugin through the WordPress plugins screen directly.
@@ -50,7 +50,7 @@ Of course :) But first calm down and read this [comprehensive guide on how to co
50
 
51
  = I have another file I want to host locally. Could you make a plugin? =
52
 
53
- 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](https://wordpress.org/plugins/host-analyticsjs-local/). For anything else, please follow the steps in [this how-to](https://daan.dev/how-to/host-js-locally-crontab/).
54
 
55
  = How come my font isn't available in OMGF? =
56
 
@@ -78,12 +78,6 @@ The option in OMGF removes any fonts that are loaded in the conventional way. Ho
78
 
79
  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.
80
 
81
- = My fonts aren't being downloaded! What's going on? =
82
-
83
- First check your database if the table {prefix}_caos_webfonts exists. If it doesn't, remove the `caos_webfonts_db_version` from the `wp_options` table and reload the page. The table should be created and the issue should be resolved.
84
-
85
- If the issue still persists and 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.
86
-
87
  = The stylesheet isn't loaded? What's going on? =
88
 
89
  OMGF 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.
@@ -111,6 +105,19 @@ N/A
111
 
112
  == Changelog ==
113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  = 2.5.0 =
115
  Updated Welcome-panel with WoOSH!-services.
116
  Preload can now be used for certain fonts only (also combined with Web Font Loader).
2
  Contributors: DaanvandenBergh
3
  Tags: google, fonts, preload, font-display, webfonts, subsets, remove, minimize, external, requests, leverage, browser, cache
4
  Requires at least: 4.6
5
+ Tested up to: 5.4
6
+ Stable tag: 3.0.0
7
  Requires PHP: 7.0
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
20
 
21
  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.
22
 
23
+ Please keep in mind that, although I try to make the configuration of this plugin as easy as possible, the concept of locally hosting a file or optimizing Google Fonts for *Pagespeed Insights* or *GT Metrix* has proven to be confusing for some people. If you're not sure of what your doing, please consult a SEO expert or Webdeveloper to help you with the configuration of this plugin or [hire me to do it for you](https://woosh.dev/wordpress-services/omgf-expert-configuration/).
24
+
25
+ == Features ==
26
  - *Automatically detect* which Google Fonts your theme is using or,
27
  - Easily find additional fonts in any/multiple subsets,
28
  - Download them and generate a stylesheet, which is automatically added to your header using WordPress' wp_head()-function,
34
  - Auto-generates the local source for webfonts,
35
  - Automatically remove any fonts loaded from fonts.gstatic.com or fonts.googleapis.com.
36
 
 
 
37
  == Installation ==
38
 
39
  1. Upload the plugin files to the `/wp-content/plugins/host-webfonts-local` directory, or install the plugin through the WordPress plugins screen directly.
50
 
51
  = I have another file I want to host locally. Could you make a plugin? =
52
 
53
+ Maintaining three 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](https://wordpress.org/plugins/host-analyticsjs-local/). To host other 3rd party scripts and styles locally, try [HELL](https://wordpress.org/plugins/host-everything-local/). For anything else, please follow the steps in [this how-to](https://daan.dev/how-to/host-js-locally-crontab/).
54
 
55
  = How come my font isn't available in OMGF? =
56
 
78
 
79
  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.
80
 
 
 
 
 
 
 
81
  = The stylesheet isn't loaded? What's going on? =
82
 
83
  OMGF 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.
105
 
106
  == Changelog ==
107
 
108
+ = 3.0.0 =
109
+ *OMGF - CORONA EDITION*
110
+ * Moved Welcome-panel to the side.
111
+ * wp_remote_get() is now used instead of cURL.
112
+ * Complete code overhaul to increase performance and UX.
113
+ * Notices and errors are now more explanatory and dismissable.
114
+ * Fixed several bugs.
115
+ * OMGF now uses wp_options table, instead of own tables.
116
+ * Old tables are removed and data is migrated.
117
+ * Auto detect now works better than ever.
118
+ * Search now works bug free.
119
+ * WordPress' default admin fonts no longer show up as results.
120
+
121
  = 2.5.0 =
122
  Updated Welcome-panel with WoOSH!-services.
123
  Preload can now be used for certain fonts only (also combined with Web Font Loader).
templates/admin/block-basic-settings.phtml CHANGED
@@ -10,7 +10,7 @@
10
  *
11
  * @package : OMGF
12
  * @author : Daan van den Bergh
13
- * @copyright: (c) 2019 Daan van den Bergh
14
  * @url : https://daan.dev
15
  * * * * * * * * * * * * * * * * * * * */
16
 
10
  *
11
  * @package : OMGF
12
  * @author : Daan van den Bergh
13
+ * @copyright: (c) 2020 Daan van den Bergh
14
  * @url : https://daan.dev
15
  * * * * * * * * * * * * * * * * * * * */
16
 
templates/admin/block-generate-stylesheet.phtml CHANGED
@@ -10,7 +10,7 @@
10
  *
11
  * @package : OMGF
12
  * @author : Daan van den Bergh
13
- * @copyright: (c) 2019 Daan van den Bergh
14
  * @url : https://daan.dev
15
  * * * * * * * * * * * * * * * * * * * */
16
 
@@ -19,170 +19,137 @@ defined('ABSPATH') || exit;
19
  $db = new OMGF_DB();
20
  $utmTags = '?utm_source=omgf&utm_medium=plugin&utm_campaign=settings';
21
  ?>
22
- <div class="">
23
  <h3><?php _e('Generate Stylesheet', 'host-webfonts-local'); ?></h3>
24
- <div class="hwl-search-box">
25
- <input type="text" name="search-field"
26
- id="search-field" class="form-input-tip ui-autocomplete-input" placeholder="<?php _e('Search... (e.g. Roboto,Open Sans)', 'host-webfonts-local'); ?>"/>
27
- <input type="button" onclick="hwlClickSearch()" name="search-btn"
28
- id="search-btn" class="button button-primary button-hero" value="<?php _e('Search', 'host-webfonts-local'); ?>"/>
29
- <input type="button" onclick="hwlAutoDetectFonts()" name="detect-btn" id="detect-btn" class="button button-secondary button-hero" value="<?= __('Auto-detect', 'host-webfonts-local'); ?>" />
30
- </div>
31
- <p class="description">
32
- <?= __('Default WordPress fonts are left unchecked by default. Enable them only if you\'re sure that your theme uses them.', 'host-webfonts-local'); ?><br />
33
- <a href="<?= OMGF_SITE_URL; ?>/how-to/find-wordpress-website-fonts/<?= $utmTags; ?>"><?= __('Which fonts does my theme use?', 'host-webfonts-local'); ?></a>
34
- </p>
35
- <table>
36
- <tr id="row" valign="top">
37
- <th align="left" colspan="3"><?php _e('Available subsets', 'host-webfonts-local'); ?></th>
38
- </tr>
39
- <tbody id="hwl-subsets">
40
- <?php
41
- $subsetFonts = $db->get_subsets();
42
- ?>
43
- <?php if ($subsetFonts): ?>
44
- <?php foreach ($subsetFonts as $subsetFont): ?>
45
- <?php
46
- $availableSubsets = explode(',', $subsetFont->available_subsets);
47
- $selectedSubsets = explode(',', $subsetFont->selected_subsets);
48
- ?>
49
- <tr valign="top" id="<?= $subsetFont->subset_font; ?>">
50
- <td>
51
- <label>
52
- <input readonly type="text" class="hwl-subset-font-family" value="<?= $subsetFont->subset_family; ?>" />
53
- </label>
54
- </td>
55
- <?php foreach ($availableSubsets as $availableSubset): ?>
56
  <td>
57
  <label>
58
- <?php $checked = in_array($availableSubset, $selectedSubsets) ? 'checked="checked"' : ''; ?>
59
- <input name="<?= $subsetFont->subset_font; ?>" value="<?= $availableSubset; ?>" type="checkbox" onclick="hwlGenerateSearchQuery('<?= $subsetFont->subset_font; ?>')" <?= $checked; ?> />
60
- <?= $availableSubset; ?>
61
  </label>
62
  </td>
63
- <?php endforeach; ?>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  </tr>
65
- <?php endforeach; ?>
66
- <?php endif; ?>
67
- </tbody>
68
- </table>
69
- <table>
70
- <tr valign="top">
71
- <th align="left" colspan="3"><?php _e('Available fonts', 'host-webfonts-local'); ?></th>
72
- </tr>
73
- </table>
74
- <table id="hwl-results">
75
- <thead align="center">
76
- <th>
77
- <?= __('Font Family', 'host-webfonts-local'); ?>
78
- </th>
79
- <th>
80
- <?= __('Font Style', 'host-webfonts-local'); ?>
81
- </th>
82
- <th>
83
- <?= __('Font Weight', 'host-webfonts-local'); ?>
84
- </th>
85
- <th>
86
- <?= __('Preload? *', 'host-webfonts-local'); ?>
87
- </th>
88
- <th></th>
89
- </thead>
90
- <?php
91
- $savedFonts = $db->get_total_fonts();
92
- ?>
93
- <?php if ($savedFonts && $subsetFonts): ?>
94
- <?php foreach ($subsetFonts as $subsetFont): ?>
95
- <tbody align="center" id="hwl-section-<?= $subsetFont->subset_font; ?>">
96
- <?php
97
- $fonts = $db->get_fonts_by_family($subsetFont->subset_family);
98
- ?>
99
- <?php foreach ($fonts as $font):
100
- $fontId = $font->font_id;
101
- $arrayPath = "caos_webfonts_array][$fontId]";
102
  ?>
103
- <tr id="row-<?= $fontId; ?>" valign="top" align="center">
104
- <td>
105
- <input readonly type="text" value="<?= $font->font_family; ?>" name="<?= $arrayPath; ?>[font-family]"/>
106
  </td>
107
- <td>
108
- <input readonly type="text" value="<?= $font->font_style; ?>" name="<?= $arrayPath; ?>[font-style]"/>
109
  </td>
110
- <td>
111
- <input readonly type="text" value="<?= $font->font_weight; ?>" name="<?= $arrayPath; ?>[font-weight]"/>
112
  </td>
113
- <td>
114
- <input type="checkbox" value="1" <?= $font->preload == 1 ? 'checked' : ''; ?> name="<?= $arrayPath; ?>[preload]" />
115
  </td>
116
  <td>
117
- <input type="hidden" value="<?= $fontId; ?>" name="<?= $arrayPath; ?>[id]"/>
118
- <input type="hidden" value="<?= $font->local; ?>" name="<?= $arrayPath; ?>[local]"/>
119
- <input type="hidden" value="<?= $font->url_ttf; ?>" name="<?= $arrayPath; ?>[url][ttf]"/>
120
- <input type="hidden" value="<?= $font->url_woff; ?>" name="<?= $arrayPath; ?>[url][woff]"/>
121
- <input type="hidden" value="<?= $font->url_woff2; ?>" name="<?= $arrayPath; ?>[url][woff2]"/>
122
- <input type="hidden" value="<?= $font->url_eot; ?>" name="<?= $arrayPath; ?>[url][eot]"/>
123
- <div class="hwl-remove">
124
- <a onclick="hwlRemoveRow('row-<?= $fontId; ?>')">
125
- <small><?php _e('remove', 'host-webfonts-local'); ?></small>
126
- </a>
127
- </div>
128
  </td>
129
  </tr>
130
  <?php endforeach; ?>
 
 
 
 
 
131
  </tbody>
132
- <?php endforeach; ?>
133
- <?php endif; ?>
134
- <tbody id="hwl-warning">
135
- <tr class="loading" style="display: none;">
136
- <td colspan="3" align="center">
137
- <span class="spinner"></span>
138
- </td>
139
- </tr>
140
- <tr class="error" style="display: none;">
141
- <td colspan="3" align="center"><?php _e('No fonts available.', 'host-webfonts-local'); ?></td>
142
- </tr>
143
- </tbody>
144
- </table>
145
 
146
- <table>
147
  <tbody>
148
- <tr>
149
- <td colspan="5">
150
- <span class="description">* <?= __('Only the <strong>woff2</strong> format is preloaded, since this format is widely supported by Modern Browsers which support preload.'); ?></span>
151
- </td>
152
- </tr>
153
- <tr valign="center" align="center">
154
- <td>
155
- <input type="button" onclick="hwlDownloadFonts()" name="save-btn"
156
- id="save-btn" class="button-primary" value="<?php _e('Download Fonts', 'host-webfonts-local'); ?>"/>
157
- </td>
158
- <td>
159
- <input type="button" onclick="hwlGenerateStylesheet()" name="generate-btn"
160
- id="generate-btn" class="button-secondary" value="<?php _e('Generate Stylesheet', 'host-webfonts-local'); ?>"/>
161
- </td>
162
- <td>
163
- <a onclick="hwlCleanQueue()" name="clean-btn"
164
- id="clean-btn" class="button-cancel"><?php _e('Clean Queue', 'host-webfonts-local'); ?></a>
165
- </td>
166
- <td>
167
- <a onclick="hwlEmptyDir()" name="empty-btn"
168
- id="empty-btn" class="button-cancel"><?php _e('Empty Cache Directory', 'host-webfonts-local'); ?></a>
169
- </td>
170
- <td width="20%"></td>
171
- </tr>
172
- <tr valign="center">
173
- <?php
174
- $downloaded = $db->get_download_status()['downloaded'];
175
- $total = $db->get_download_status()['total'];
176
- $width = $downloaded && $total ? (100 / $total) * $downloaded : 0;
177
- ?>
178
- <td colspan="5">
179
- <div class="caos-status-total-bar" style="">
180
- <div id="caos-status-progress-bar" style="width: <?= $width; ?>%;">
181
- <span class="caos-status-progress-percentage"><?= $width . '%'; ?></span>
182
- </div>
183
- </div>
184
- </td>
185
- </tr>
186
  </tbody>
187
  </table>
188
  </div>
 
 
 
10
  *
11
  * @package : OMGF
12
  * @author : Daan van den Bergh
13
+ * @copyright: (c) 2020 Daan van den Bergh
14
  * @url : https://daan.dev
15
  * * * * * * * * * * * * * * * * * * * */
16
 
19
  $db = new OMGF_DB();
20
  $utmTags = '?utm_source=omgf&utm_medium=plugin&utm_campaign=settings';
21
  ?>
22
+ <div class="omgf-generate-stylesheet">
23
  <h3><?php _e('Generate Stylesheet', 'host-webfonts-local'); ?></h3>
24
+
25
+ <div class="omgf-search-section">
26
+ <div class="omgf-search-box">
27
+ <input type="text" id="omgf-search" class="form-input-tip ui-autocomplete-input" placeholder="<?php _e('Search... (e.g. Roboto,Open Sans)', 'host-webfonts-local'); ?>" />
28
+ <input type="button" id="omgf-search-subsets" class="button button-primary button-hero" value="<?php _e('Search', 'host-webfonts-local'); ?>"/>
29
+ <input type="button" id="omgf-auto-detect" class="button button-secondary button-hero" value="<?= __('Auto Detect', 'host-webfonts-local'); ?>" />
30
+ </div>
31
+ <table>
32
+ <tr id="row" valign="top">
33
+ <th align="left" colspan="3"><?php _e('Available subsets', 'host-webfonts-local'); ?></th>
34
+ </tr>
35
+ <tbody id="omgf-subsets">
36
+ <?php
37
+ $subsetFonts = $db->get_subsets();
38
+ ?>
39
+ <?php if ($subsetFonts): ?>
40
+ <?php foreach ($subsetFonts as $subsetFont): ?>
41
+ <tr valign="top" id="<?= $subsetFont['subset_font']; ?>">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  <td>
43
  <label>
44
+ <input readonly type="text" class="omgf-subset-font-family" data-font-family="<?= $subsetFont['subset_font']; ?>" value="<?= $subsetFont['subset_family']; ?>" />
 
 
45
  </label>
46
  </td>
47
+ <?php foreach ($subsetFont['available_subsets'] as $availableSubset): ?>
48
+ <td>
49
+ <label>
50
+ <?php $checked = in_array($availableSubset, $subsetFont['selected_subsets']) ? 'checked="checked"' : ''; ?>
51
+ <input class="omgf-subset" data-subset-font-family="<?= $subsetFont['subset_font']; ?>" value="<?= $availableSubset; ?>" type="checkbox" <?= $checked; ?> />
52
+ <?= $availableSubset; ?>
53
+ </label>
54
+ </td>
55
+ <?php endforeach; ?>
56
+ </tr>
57
+ <?php endforeach; ?>
58
+ <?php else: ?>
59
+ <tr colspan="3">
60
+ <td>
61
+ <em><?php _e('No subsets found. Use <strong>Search</strong> or <strong>Auto Detect</strong> start.', 'host-webfonts-local'); ?></em>
62
+ </td>
63
  </tr>
64
+ <?php endif; ?>
65
+ </tbody>
66
+ </table>
67
+ <table>
68
+ <tr valign="top">
69
+ <th align="left" colspan="3"><?php _e('Available fonts', 'host-webfonts-local'); ?></th>
70
+ </tr>
71
+ </table>
72
+ <table id="omgf-font-styles-list">
73
+ <?php
74
+ $savedFonts = $db->get_total_fonts();
75
+ ?>
76
+ <?php if ($savedFonts && $subsetFonts): ?>
77
+ <thead align="center">
78
+ <th>
79
+ <?= __('Font Family', 'host-webfonts-local'); ?>
80
+ </th>
81
+ <th>
82
+ <?= __('Font Style', 'host-webfonts-local'); ?>
83
+ </th>
84
+ <th>
85
+ <?= __('Font Weight', 'host-webfonts-local'); ?>
86
+ </th>
87
+ <th>
88
+ <?= __('Preload? *', 'host-webfonts-local'); ?>
89
+ </th>
90
+ <th></th>
91
+ </thead>
92
+ <tbody id="omgf-font-styles">
93
+ <?php foreach ($savedFonts as $font):
94
+ $fontId = $font['font_id'];
 
 
 
 
 
 
95
  ?>
96
+ <tr class="omgf-font-style" data-font-id="<?= $fontId; ?>" id="row-<?= $fontId; ?>">
97
+ <td style="width: 195px;">
98
+ <input readonly type="text" value="<?= $font['font_family']; ?>" />
99
  </td>
100
+ <td style="width: 150px;">
101
+ <input style="width: 150px;" readonly type="text" value="<?= $font['font_style']; ?>" />
102
  </td>
103
+ <td style="width: 100px;">
104
+ <input style="width: 100px;" readonly type="text" value="<?= $font['font_weight']; ?>" />
105
  </td>
106
+ <td align="center" style="width: 75px;">
107
+ <input type="checkbox" class="omgf-font-preload" data-preload="<?= $fontId; ?>" value="1" <?= $font['preload'] == 1 ? 'checked' : ''; ?> />
108
  </td>
109
  <td>
110
+ <input type="hidden" value="<?= $fontId; ?>" />
111
+ <input type="hidden" value="<?= $font['local']; ?>" />
112
+ <input type="hidden" value="<?= $font['url_ttf']; ?>" />
113
+ <input type="hidden" value="<?= $font['url_woff']; ?>" />
114
+ <input type="hidden" value="<?= $font['url_woff2']; ?>" />
115
+ <input type="hidden" value="<?= $font['url_eot']; ?>" />
116
+ <span data-row="row-<?= $fontId; ?>" class="omgf-font-remove notice-dismiss">&nbsp;</span>
 
 
 
 
117
  </td>
118
  </tr>
119
  <?php endforeach; ?>
120
+ <tr>
121
+ <td colspan="5">
122
+ <span class="description">* <?= __('Only the <strong>woff2</strong> format is preloaded, since this format is widely supported by Modern Browsers which support preload.', 'host-webfonts-local'); ?></span>
123
+ </td>
124
+ </tr>
125
  </tbody>
126
+ <?php else: ?>
127
+ <tr>
128
+ <td>
129
+ <em><?php _e('No fonts found. Choose <strong>subsets</strong> to start searching for fonts.', 'host-webfonts-local'); ?></em>
130
+ </td>
131
+ </tr>
132
+ <?php endif; ?>
133
+ </table>
134
+ </div>
 
 
 
 
135
 
136
+ <table id="omgf-control-panel">
137
  <tbody>
138
+ <tr valign="center">
139
+ <td>
140
+ <input type="button" id="omgf-download" class="button-primary" value="<?php _e('Download Fonts', 'host-webfonts-local'); ?>" />
141
+ </td>
142
+ <td>
143
+ <input type="button" id="omgf-generate" class="button-secondary" value="<?php _e('Generate Stylesheet', 'host-webfonts-local'); ?>" />
144
+ </td>
145
+ <td>
146
+ <a id="omgf-empty" class="button-cancel"><?php _e('Empty Cache Directory', 'host-webfonts-local'); ?></a>
147
+ </td>
148
+ <td style="width: 33%;"></td>
149
+ </tr>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  </tbody>
151
  </table>
152
  </div>
153
+ <div class="omgf-loading" style="display: none;">
154
+ <span class="spinner is-active"> </span>
155
+ </div>
templates/admin/block-welcome.phtml CHANGED
@@ -10,7 +10,7 @@
10
  *
11
  * @package : OMGF
12
  * @author : Daan van den Bergh
13
- * @copyright: (c) 2019 Daan van den Bergh
14
  * @url : https://daan.dev
15
  * * * * * * * * * * * * * * * * * * * */
16
 
@@ -21,12 +21,9 @@ $tweetUrl = 'https://twitter.com/intent/tweet?text=I+just+optimized+my+Google+Fo
21
  ?>
22
  <div id="welcome-panel" class="welcome-panel">
23
  <div class="welcome-panel-content">
24
- <p class="about-description">
25
- <?= get_plugin_data(OMGF_PLUGIN_FILE)['Description']; ?>
26
- </p>
27
  <div class="welcome-panel-column-container">
28
- <div class="welcome-panel-column" style="width: 31%; margin-right: 15px;">
29
- <h3><?php _e( 'Quickstart', 'host-webfonts-local') ;?></h3>
30
  <ul>
31
  <li class="welcome-icon dashicons-before dashicons-admin-settings">
32
  <?= sprintf(__('Click %sAuto-detect%s and wait for the on-screen instructions', 'host-webfonts-local'), '<a href="javascript: void(0);" onclick="hwlAutoDetectFonts();">', '</a>'); ?>
@@ -44,15 +41,9 @@ $tweetUrl = 'https://twitter.com/intent/tweet?text=I+just+optimized+my+Google+Fo
44
  <p>
45
  <?= sprintf(__('%sClick here%s for a more comprehensive guide.', 'host-webfonts-local'), '<a target="_blank" href="' . OMGF_SITE_URL . '/wordpress/host-google-fonts-locally/' . $utmTags . '">', '</a>'); ?>
46
  </p>
 
47
  </div>
48
- <div class="welcome-panel-column" style="width: 31%; margin-right: 15px;">
49
- <h3>
50
- <?php _e('Need Help?', 'host-webfonts-local'); ?>
51
- </h3>
52
- <p>
53
- <?= sprintf(__('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/' . $utmTags . '" 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>'); ?>
54
- </p>
55
- <hr/>
56
  <h3><span class="dashicons dashicons-dashboard"></span> <?php _e('Make WordPress <em>Faster</em> Than Superman', 'host-webfonts-local'); ?></h3>
57
  <p>
58
  <?= __('Superman can reach the other side of the world in <strong>3 seconds</strong>. Google wants your website to do it <strong>faster</strong>. Let\'s give Google a run for its money.', 'host-webfonts-local'); ?>
@@ -60,8 +51,9 @@ $tweetUrl = 'https://twitter.com/intent/tweet?text=I+just+optimized+my+Google+Fo
60
  <p>
61
  <a target="_blank" class="button button-primary button-hero" href="https://woosh.dev/wordpress-services/<?= $utmTags; ?>"><span class="dashicons dashicons-thumbs-up"></span> <?= __('Hire me', 'host-webfonts-local'); ?></a> <span><em>(<?= __('Starting at € 99,-', 'host-analyticsjs-local'); ?>)</em></span>
62
  </p>
 
63
  </div>
64
- <div class="welcome-panel-column welcome-panel-last" style="width: 33%;">
65
  <h3>
66
  <?php _e('Support OMGF by Spreading the Word!', 'host-webfonts-local'); ?>
67
  </h3>
@@ -78,6 +70,13 @@ $tweetUrl = 'https://twitter.com/intent/tweet?text=I+just+optimized+my+Google+Fo
78
  <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>
79
  <a target="_blank" class="button button-secondary button-hero" href="<?= $tweetUrl; ?>"><span class="dashicons-before dashicons-twitter"> <?php _e('Tweet', 'host-webfonts-local'); ?></span></a>
80
  </p>
 
 
 
 
 
 
 
81
  </div>
82
  </div>
83
  </div>
10
  *
11
  * @package : OMGF
12
  * @author : Daan van den Bergh
13
+ * @copyright: (c) 2020 Daan van den Bergh
14
  * @url : https://daan.dev
15
  * * * * * * * * * * * * * * * * * * * */
16
 
21
  ?>
22
  <div id="welcome-panel" class="welcome-panel">
23
  <div class="welcome-panel-content">
 
 
 
24
  <div class="welcome-panel-column-container">
25
+ <div class="welcome-panel-column" style="width: 100%; margin-right: 15px;">
26
+ <h2><?php _e( 'Quickstart', 'host-webfonts-local') ;?></h2>
27
  <ul>
28
  <li class="welcome-icon dashicons-before dashicons-admin-settings">
29
  <?= sprintf(__('Click %sAuto-detect%s and wait for the on-screen instructions', 'host-webfonts-local'), '<a href="javascript: void(0);" onclick="hwlAutoDetectFonts();">', '</a>'); ?>
41
  <p>
42
  <?= sprintf(__('%sClick here%s for a more comprehensive guide.', 'host-webfonts-local'), '<a target="_blank" href="' . OMGF_SITE_URL . '/wordpress/host-google-fonts-locally/' . $utmTags . '">', '</a>'); ?>
43
  </p>
44
+ <hr />
45
  </div>
46
+ <div class="welcome-panel-column" style="width: 100%; margin-right: 15px;">
 
 
 
 
 
 
 
47
  <h3><span class="dashicons dashicons-dashboard"></span> <?php _e('Make WordPress <em>Faster</em> Than Superman', 'host-webfonts-local'); ?></h3>
48
  <p>
49
  <?= __('Superman can reach the other side of the world in <strong>3 seconds</strong>. Google wants your website to do it <strong>faster</strong>. Let\'s give Google a run for its money.', 'host-webfonts-local'); ?>
51
  <p>
52
  <a target="_blank" class="button button-primary button-hero" href="https://woosh.dev/wordpress-services/<?= $utmTags; ?>"><span class="dashicons dashicons-thumbs-up"></span> <?= __('Hire me', 'host-webfonts-local'); ?></a> <span><em>(<?= __('Starting at € 99,-', 'host-analyticsjs-local'); ?>)</em></span>
53
  </p>
54
+ <hr />
55
  </div>
56
+ <div class="welcome-panel-column welcome-panel-last" style="width: 100%;">
57
  <h3>
58
  <?php _e('Support OMGF by Spreading the Word!', 'host-webfonts-local'); ?>
59
  </h3>
70
  <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>
71
  <a target="_blank" class="button button-secondary button-hero" href="<?= $tweetUrl; ?>"><span class="dashicons-before dashicons-twitter"> <?php _e('Tweet', 'host-webfonts-local'); ?></span></a>
72
  </p>
73
+ <hr />
74
+ <h3>
75
+ <?php _e('Need Help?', 'host-webfonts-local'); ?>
76
+ </h3>
77
+ <p>
78
+ <?= sprintf(__('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/' . $utmTags . '" 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>'); ?>
79
+ </p>
80
  </div>
81
  </div>
82
  </div>
templates/frontend-web-font-loader.phtml CHANGED
@@ -10,7 +10,7 @@
10
  *
11
  * @package : OMGF
12
  * @author : Daan van den Bergh
13
- * @copyright: (c) 2019 Daan van den Bergh
14
  * @url : https://daan.dev
15
  * * * * * * * * * * * * * * * * * * * */
16
 
@@ -21,7 +21,7 @@ $fonts = $db->get_downloaded_fonts();
21
  $families = array_unique(
22
  array_map(
23
  function ($i) {
24
- return $i->font_family;
25
  },
26
  $fonts
27
  )
@@ -45,10 +45,10 @@ $wflSrcUrl = plugin_dir_url(OMGF_PLUGIN_FILE) . 'js/libraries/webfont.js
45
  <?php
46
  $fvds = [];
47
  foreach ($fonts as $font) {
48
- if ($font->font_family != $family) {
49
  continue;
50
  }
51
- $fvds[] = mb_substr($font->font_style, 0, 1) . mb_substr($font->font_weight, 0, 1);
52
  }
53
  ?>
54
  '<?= $family; ?>:<?= implode(',', $fvds); ?>',
10
  *
11
  * @package : OMGF
12
  * @author : Daan van den Bergh
13
+ * @copyright: (c) 2020 Daan van den Bergh
14
  * @url : https://daan.dev
15
  * * * * * * * * * * * * * * * * * * * */
16
 
21
  $families = array_unique(
22
  array_map(
23
  function ($i) {
24
+ return $i['font_family'];
25
  },
26
  $fonts
27
  )
45
  <?php
46
  $fvds = [];
47
  foreach ($fonts as $font) {
48
+ if ($font['font_family'] != $family) {
49
  continue;
50
  }
51
+ $fvds[] = mb_substr($font['font_style'], 0, 1) . mb_substr($font['font_weight'], 0, 1);
52
  }
53
  ?>
54
  '<?= $family; ?>:<?= implode(',', $fvds); ?>',