-
Notifications
You must be signed in to change notification settings - Fork 0
/
typekit.php
333 lines (294 loc) · 8.57 KB
/
typekit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php
/*
Plugin Name: Adobe Fonts (formerly Typekit) for WordPress
Plugin URI: https://om4.io/plugins/adobe-fonts-for-wordpress/
Description: Use a range of over 25,000 of high quality fonts on your WordPress website by integrating the <a href="https://fonts.adobe.com">Adobe Fonts</a> font service into your WordPress blog.
Version: 1.10.1
Author: OM4
Author URI: https://om4.io/
Text Domain: typekit-fonts-for-wordpress
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
/*
Copyright 2009-2024 OM4 (email: [email protected] web: https://om4.io/)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* Adobe Fonts (formerly Typekit) functionality.
*/
class OM4_Typekit {
/**
* The version of the database schema used by this plugin
*
* @var int
*/
private $db_version = 1;
/**
* The version of the plugin that is currently installed
*
* @var int
*/
private $installed_version;
/**
* The name of the option used to store the plugin's settings
*
* @var string
*/
private $option_name = 'OM4_Typekit';
/**
* The format for the Adobe Fonts JavaScript embed code
*
* @var string
*/
public $embedcode_advanced = '<script>
(function(d) {
var config = {
kitId: \'%1$s\',
scriptTimeout: 3000,
async: %2$s
},
h=d.documentElement,t=setTimeout(function(){h.className=h.className.replace(/\bwf-loading\b/g,"")+" wf-inactive";},config.scriptTimeout),tk=d.createElement("script"),f=false,s=d.getElementsByTagName("script")[0],a;h.className+=" wf-loading";tk.src=\'https://use.typekit.net/\'+config.kitId+\'.js\';tk.async=true;tk.onload=tk.onreadystatechange=function(){a=this.readyState;if(f||a&&a!="complete"&&a!="loaded")return;f=true;clearTimeout(t);try{Typekit.load(config)}catch(e){}};s.parentNode.insertBefore(tk,s)
})(document);
</script>';
// phpcs:disable WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet
/**
* The format for the Adobe Fonts CSS file URL
*
* @var string
*/
public $embedcode_css = '<link rel="stylesheet" href="https://use.typekit.net/%s.css">';
// phpcs:enable WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet
/**
* The regular expression used to validate the Adobe Fonts Account/Web Projects ID
*
* @var string
*/
public $kitid_regexp = '#([a-z0-9]*)#i';
/**
* The format for the Adobe Fonts CSS file URL. Used in HTTP requests to verify that the URL doesn't produce a 404 error
*
* @var string
*/
public $embedcodeurl = 'https://use.typekit.net/%s.css';
const EMBED_METHOD_CSS = 'css';
const EMBED_METHOD_JAVASCRIPT = 'js';
/**
* Default settings
*
* @var array<string,string>
*/
private $settings = array(
'id' => '',
'method' => self::EMBED_METHOD_CSS,
'css' => '',
'async' => '',
);
/**
* Class Constructor
*/
public function __construct() {
register_activation_hook( __FILE__, array( $this, 'activate' ) );
add_action( 'init', array( $this, 'initialise' ) );
add_action( 'plugins_loaded', array( $this, 'load_domain' ) );
add_action( 'wp_head', array( $this, 'header_code' ), 99 );
$data = get_option( $this->option_name );
if ( is_array( $data ) ) {
$this->installed_version = intval( $data['version'] );
$this->settings = $data['settings'];
}
}
/**
* Load up the relevant language pack if we're using WordPress in a different language.
*
* @return void
*/
public function load_domain() {
load_plugin_textdomain( 'typekit-fonts-for-wordpress' );
}
/**
* Plugin Activation Tasks
*
* @return void
*/
public function activate() {
// There aren't really any installation tasks at the moment.
if ( ! $this->installed_version ) {
$this->installed_version = $this->db_version;
$this->save_settings();
}
}
/**
* Performs any upgrade tasks if required
*
* @return void
*/
public function check_version() {
if ( $this->installed_version !== $this->db_version ) {
// Upgrade tasks.
if ( 0 === $this->installed_version ) {
++$this->installed_version;
}
$this->save_settings();
}
}
/**
* Initialise the plugin.
* Set up the admin interface if necessary
*
* @return void
*/
public function initialise() {
$this->check_version();
if ( is_admin() ) {
// WP Dashboard.
require_once 'typekit-admin.php';
new OM4_Typekit_Admin( $this );
}
}
/**
* Saves the plugin's settings to the database
*
* @return void
*/
public function save_settings() {
$data = array_merge( array( 'version' => $this->installed_version ), array( 'settings' => $this->settings ) );
update_option( $this->option_name, $data );
}
/**
* Retrieve the Adobe Fonts embed code if the unique account id has been set
*
* @return string The Adobe Fonts embed code if the unique account ID has been set, otherwise an empty string.
*/
public function get_embed_code() {
$id = $this->get_account_id();
if ( '' !== $id ) {
switch ( $this->get_embed_method() ) {
case self::EMBED_METHOD_CSS:
return sprintf( $this->embedcode_css, $id );
case self::EMBED_METHOD_JAVASCRIPT:
$async = $this->get_async() ? 'true' : 'false';
return sprintf( $this->embedcode_advanced, $id, $async );
}
}
return '';
}
/**
* Get the stored Adobe Fonts Account/Web Projects ID
*
* @return string The account ID if it has been specified, otherwise an empty string
*/
public function get_account_id() {
if ( strlen( $this->settings['id'] ) ) {
return $this->settings['id'];
}
return '';
}
/**
* Get the stored value for the async parameter.
*
* Defaults to true.
*
* @return bool
*/
public function get_async() {
if ( isset( $this->settings['async'] ) && false === $this->settings['async'] ) {
return false;
} else {
return true;
}
}
/**
* Get the stored value for the embed method.
*
* @return string
*/
public function get_embed_method() {
if ( isset( $this->settings['method'] ) ) {
return $this->settings['method'];
} else {
// No embed method chosen, so default to the JS method.
return self::EMBED_METHOD_JAVASCRIPT;
}
}
/**
* Parse and save the Adobe Fonts Account/Web Projects ID
*
* @param string $id The Adobe Fonts Account/Web Projects ID.
* @return void
*/
public function parse_kit_id( $id ) {
if ( preg_match( $this->kitid_regexp, $id, $matches ) && 2 === count( $matches ) ) {
$this->settings['id'] = $matches[0];
} else {
$this->settings['id'] = '';
}
}
/**
* Parse and save the embed method.
*
* @param string $method Embed method.
* @return void
*/
public function parse_embed_method( $method ) {
if ( self::EMBED_METHOD_JAVASCRIPT === $method ) {
$this->settings['method'] = self::EMBED_METHOD_JAVASCRIPT;
} else {
$this->settings['method'] = self::EMBED_METHOD_CSS;
$this->settings['async'] = '';
}
}
/**
* Retrieve the custom CSS rules
*
* @return string The custom CSS rules
*/
public function get_css_rules() {
return $this->settings['css'];
}
/**
* Parse and save the custom css rules.
* The input is sanitized by stripping all HTML tags
*
* @param string $code CSS code.
* @return void
*/
public function set_css_rules( $code ) {
$this->settings['css'] = '';
$code = wp_strip_all_tags( $code );
if ( strlen( $code ) ) {
$this->settings['css'] = $code;
}
}
/**
* Display the plugin's javascript and css code in the site's header
*
* @return void
*/
public function header_code() {
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
echo '<!-- BEGIN Adobe Fonts for WordPress -->';
echo $this->get_embed_code();
// If CSS settings exist, echo them within style tags.
if ( strlen( $this->settings['css'] ) ) {
echo "<style type='text/css'>{$this->settings['css']}</style>";
}
echo '<!-- END Adobe Fonts for WordPress -->';
// phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
if ( defined( 'ABSPATH' ) && defined( 'WPINC' ) ) {
if ( ! isset( $GLOBALS['OM4_Typekit'] ) ) {
$GLOBALS['OM4_Typekit'] = new OM4_Typekit();
}
}