-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFontsamplerHelpers.php
executable file
·484 lines (400 loc) · 14.5 KB
/
FontsamplerHelpers.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<?php
/**
* Class FontsamplerHelpers
*
* Wrapper for misc helper functions useful throughout the plugin
*/
class FontsamplerHelpers {
private $fontsampler;
private $less;
private $features;
private $additional_features;
private $layout;
function __construct( $fontsampler ) {
$this->fontsampler = $fontsampler;
$this->less = new Less_Parser( array( 'compress' => true ) );
$this->layout = new FontsamplerLayout();
$this->features = array(
'fontsize' => array(
'name' => 'fontsize',
'label' => 'Size control',
'slider_label' => 'Label',
'slider_initial_label' => 'Initial px',
'slider_min_label' => 'Min px',
'slider_max_label' => 'Max px',
'label_installation_default' => $this->fontsampler->settings_defaults['fontsize_label'],
),
'letterspacing' => array(
'name' => 'letterspacing',
'label' => 'Letter spacing control',
'slider_label' => 'Label',
'slider_initial_label' => 'Initial px',
'slider_min_label' => 'Min px',
'slider_max_label' => 'Max px',
'label_installation_default' => $this->fontsampler->settings_defaults['letterspacing_label'],
),
'lineheight' => array(
'name' => 'lineheight',
'label' => 'Line height control',
'slider_label' => 'Label',
'slider_initial_label' => 'Initial %',
'slider_min_label' => 'Min %',
'slider_max_label' => 'Max %',
'label_installation_default' => $this->fontsampler->settings_defaults['lineheight_label'],
)
);
$this->additional_features = array(
'sampletexts' => 'Display dropdown selection for sample texts',
'fontpicker' => 'Display fontsname(s) as dropdown selection (for several fonts) or label (for a single font)',
'alignment' => 'Alignment controls',
'invert' => 'Allow inverting the text field to display negative text',
'opentype' => 'Display OpenType feature controls (automatic detection)',
'locl' => 'Add dropdown for switching language (activates locl features)',
'multiline' => 'Allow line breaks',
'buy' => 'Display a link to buy these fonts',
'specimen' => 'Display a link to a specimen',
);
$this->notdef_options = array(
"Do nothing (renders fallback font)",
"Highlight visually (renders fallback font)",
"Render .notdef instead (of font or else of fallback)",
"Block rendering",
);
// the default admin slider ranges for each field
$this->slider_ranges = array(
'fontsize' => array( 0, 255 ), // pixels
'letterspacing' => array( - 25, 25 ), // pixels
'lineheight' => array( 0, 500 ), // percent
);
}
/**
* @return array of keys of features that are boolean in fontsampler-edit and settings
*/
function get_checkbox_features() {
return array_merge( array_keys( $this->features ), array_keys( $this->additional_features ) );
}
/**
* @id - if false return the default css file and compile if needed
*
* @return string path to include styles css file
* (general plugin css, or custom css for a particular fontsampler)
*/
function get_css_file( $id = false, $css = false ) {
if ( false === $id ) {
// check path for existing file
// if not, create it by merging css template with settings
if ( ! file_exists( plugin_dir_path( __FILE__ ) . 'css/fontsampler-css.css' ) ) {
$default_settings = $this->fontsampler->db->get_settings();
if ( ! $this->write_css_from_settings( $default_settings ) ) {
// if creating the missing file failed return the base styles by themselves
return plugin_dir_url( __FILE__ ) . 'css/fontsampler-interface.css';
}
}
// return file path to the css that contains base css merged with settings css
return plugin_dir_url( __FILE__ ) . 'css/fontsampler-css.css';
} else {
$path = plugin_dir_path( __FILE__ ) . 'css/custom/fontsampler-interface-' . $id . '.css';
$url = plugin_dir_url( __FILE__ ) . 'css/custom/fontsampler-interface-' . $id . '.css';
if ( ! file_exists( $path ) ) {
$this->write_custom_css( $path, $css, $id );
}
return $url;
}
}
function write_custom_css_for_set( $set ) {
$defaults = $this->fontsampler->db->get_default_settings();
$css = array_filter( $set, function ( $item ) {
return substr( $item, 0, 4 ) === 'css_';
}, ARRAY_FILTER_USE_KEY );
// catch 5.6.30 errors about ARRAY_FILTER_USE_KEY, 5.6.33 check should
// catch those in the future
if (!is_array($css)) {
return false;
}
$supplemented = array();
foreach ( $css as $key => $value ) {
if ( null === $value ) {
// any null values, replace them from defaults, so we can render a custom css
// with all values filled
$supplemented[ $key ] = $defaults[ $key ];
} else {
// found a non null value => generate custom css!
$supplemented[ $key ] = $value;
}
}
$path = plugin_dir_path( __FILE__ ) . 'css/custom/fontsampler-interface-' . $set['id'] . '.css';
return $this->write_custom_css( $path, $css, $set['id'] );
}
/**
* @param $path
* @param null $css
* @param null $id
*
* Helper function that writes the css file specific to a particular set
*
* @return bool
*/
function write_custom_css( $path, $css = null, $id = null ) {
$input = plugin_dir_path( __FILE__ ) . 'css/fontsampler-interface.less';
$output = $path;
// the initial $input holds all less variables and declarations
// to make it more specific than the defaults, let's substitute all
// ".fontsampler-interface" with ".fontsampler-interface.fontsampler-id-X"
$content = file_get_contents( $input );
$content = str_replace( '.fontsampler-interface', '.fontsampler-interface.fontsampler-id-' . $id, $content );
$vars = $this->settings_array_css_to_less( $css );
return $this->write_less( $content, $output, $vars, false );
}
/**
* @param $settings db row of setting params as array
*/
function write_css_from_settings( $settings ) {
$input = plugin_dir_path( __FILE__ ) . 'css/fontsampler-css.less';
$output = plugin_dir_path( __FILE__ ) . 'css/fontsampler-css.css';
// reduce passed in settings row to only values for keys starting with css_ and prefix those keys with an @ for
// matching and replacing
$vars = $this->settings_array_css_to_less( $settings );
return $this->write_less( $input, $output, $vars );
}
/**
* @param $input - file path or string
* @param $output
* @param $vars - variables to supplement in less
* @param bool $is_path - signifies if @input is a file path, or a string
*
* @return bool
*/
function write_less( $input, $output, $vars, $is_path = true ) {
$m = new FontsamplerMessages();
$this->less = new Less_Parser( array( 'compress' => true ) );
if ( file_exists( $input ) || ! $is_path ) {
try {
if ( $is_path ) {
$this->less->parseFile( $input );
} else {
$this->less->parse( $input );
}
$this->less->ModifyVars( $vars );
$css = $this->less->getCss();
} catch ( Exception $e ) {
$m->error( $e->getMessage() );
return false;
}
// concat the base styles and the replaced template into the default css file
if ( false === $this->check_is_writeable( dirname($output) ) ) {
$m->error( 'Error: Permission to write to ' . ($output) . ' denied. Failed to update styles' );
return false;
}
$wrote = @file_put_contents($output, $css);
if ( false !== $wrote) {
return $output;
} else {
$m->error("Failed to write CSS file to $output - make sure the file and directory it is in are writeable.");
}
}
return false;
}
/**
* @param $set
*
* Function that is called to retrieve a CSS file for a set
* This either returns a string to the css file (just written or existing)
* of false if it fails or the provided $set is invalid or has no non-default values
*
* @return bool|string
*/
function get_custom_css( $set ) {
if ( false === $set ) {
return false;
}
if ( intval( $set['use_defaults'] ) === 1 ) {
return false;
}
$defaults = $this->fontsampler->db->get_default_settings();
$css = array_filter( $set, function ( $item ) {
return substr( $item, 0, 4 ) === 'css_';
}, ARRAY_FILTER_USE_KEY );
$supplemented = array();
$defaults_only = true; // detect if any of the values actually differ from the defaults
foreach ( $css as $key => $value ) {
if ( null === $value ) {
// any null values, replace them from defaults, so we can render a custom css
// with all values filled
$supplemented[ $key ] = $defaults[ $key ];
} else {
// found a non null value => generate custom css!
$defaults_only = false;
$supplemented[ $key ] = $value;
}
}
if ( $defaults_only ) {
return false;
}
return $this->get_css_file( $set['id'], $supplemented );
}
/**
* Make sure the passed in handle is writeable
*
* @param $handle
*
* @return bool
*/
function check_is_writeable( $handle ) {
if ( ( is_dir( $handle ) || is_file( $handle ) ) && true === is_writeable( $handle ) ) {
return true;
}
return false;
}
/*
* Helper that generates a json formatted strong with { formats: files, ... }
* for passed in $fonts ( array )
*/
function fontfiles_json( $font ) {
if ( empty( $font ) ) {
return false;
}
$fonts_object = '{';
foreach ( $this->fontsampler->font_formats as $format ) {
if ( ! empty( $font[ $format ] ) ) {
$fonts_object .= '"' . $format . '": "' . $font[ $format ] . '",';
}
}
$fonts_object = substr( $fonts_object, 0, - 1 );
$fonts_object .= '}';
return $fonts_object;
}
function is_legacy_format( $format ) {
return in_array( $format, $this->fontsampler->font_formats_legacy );
}
function get_best_file_from_fonts( $fonts ) {
$fontsFiltered = array();
if ( ! $fonts ) {
return false;
}
foreach ( $fonts as $font ) {
$formats = $this->fontsampler->font_formats;
$best = false;
while ( $best === false && sizeof( $formats ) > 0 ) {
$check = array_shift( $formats );
if ( isset( $font[ $check ] ) && ! empty( $font[ $check ] ) ) {
$best = $font[ $check ];
}
}
if ( false !== $best ) {
$best = str_replace(array('https:', 'http:'), '', $best);
$fontsFiltered[ $font['id'] ] = $best;
}
}
return sizeof( $fontsFiltered ) > 0 ? $fontsFiltered : false;
}
/**
* @return bool - true if folder exists or has successfully been created
*/
function check_and_create_folders() {
$customCssDir = plugin_dir_path( __FILE__ ) . 'css/custom';
$exists = is_dir( $customCssDir );
if ( ! $exists ) {
$exists = mkdir( $customCssDir );
}
return $exists;
}
/**
* @param $row - entire or partial row of fontsampler_settings table
*
* @return array - reduced to an array of only key-value paris whose key starts with css_
*/
function settings_array_css_to_less( $row ) {
$vars = array();
if (is_array($row)) {
foreach ( $row as $key => $value ) {
if ( false !== strpos( $key, 'css_' ) ) {
$vars[ $key ] = $value;
}
}
}
return $vars;
}
function hide_changelog() {
$plugin = get_plugin_data( realpath( dirname( __FILE__ ) . "/fontsampler.php" ) );
// this throws obscure error on PHP 5.6
//$option = update_option( $this->fontsampler::FONTSAMPLER_OPTION_LAST_CHANGELOG, $plugin['Version'] );
$option = update_option( 'fontsampler_last_changelog', $plugin['Version'] );
}
function extend_twig( $twig ) {
$twig->addGlobal( 'block_classes', $this->layout->blocks );
$twig->addGlobal( 'block_labels', $this->layout->labels );
$twig->addGlobal( 'plugin_dir_url', plugin_dir_url( __FILE__ ) );
$twig->addGlobal( 'features', $this->features );
$twig->addGlobal( 'additional_features', $this->additional_features );
$twig->addGlobal( 'formats', $this->fontsampler->font_formats );
$twig->addGlobal( 'slider_ranges', $this->slider_ranges );
$twig->addGlobal( 'is_rtl', is_rtl() );
$twig->addGlobal( 'settings_defaults', $this->fontsampler->settings_defaults );
$twig->addGlobal( 'notdef', $this->notdef_options );
}
/**
* Twig functions called from the extension
*/
function fontset_fontfiles_json ( $fontset ) {
return $this->fontfiles_json( $fontset );
}
function file_from_path ( $font, $format ) {
return substr( $font[ $format ], strrpos( $font[ $format ], '/' ) + 1 );
}
function submit_button ( $label = 'Submit', $type = 'primary' ) {
return submit_button( $label, $type );
}
function is_current ( $current ) {
$subpages = explode( ',', $current );
if ( ! isset( $_GET['subpage'] ) && in_array( 'index', $subpages ) ||
isset( $_GET['subpage'] ) && in_array( $_GET['subpage'], $subpages )
) {
return ' class=current ';
} else {
return '';
}
}
function wp_nonce_field ( $field ) {
return function_exists( 'wp_nonce_field' ) ? wp_nonce_field( $field ) : false;
}
function upload_link () {
return esc_url( get_upload_iframe_src( 'image' ) );
}
function image_src ( $image_id ) {
if ($image_id) {
return wp_get_attachment_image_src($image_id, 'full')[0];
}
return '';
}
function admin_hide_legacy_formats () {
return $this->fontsampler->admin_hide_legacy_formats;
}
function admin_proxy_urls () {
return get_option($this->fontsampler::FONTSAMPLER_OPTION_PROXY_URLS);
}
function admin_no_permalinks () {
return empty( get_option( 'permalink_structure' ) );
}
function image ( $src ) {
return plugin_dir_url( __FILE__ ) . $src;
}
function num_notifications () {
return $this->fontsampler->notifications->get_notifications()['num_notifications'];
}
function has_new_changelog () {
$plugin = get_plugin_data( realpath( dirname( __FILE__ ) . "/fontsampler.php" ) );
$option = get_option( 'fontsampler_last_changelog' );
// if no previous changelog has been marked as viewed, or the previously marked
// changelog is smaller than the current fontsampler plugin version, show the changelog
if ( false === $option || version_compare( $plugin['Version'], $option ) > 0 ) {
return true;
}
return false;
}
function wp_get_attachment_image_src ( $id, $option = 'full' ) {
return wp_get_attachment_image_src( $id, $option )[0];
}
function get_messages () {
return $this->fontsampler->msg->get_messages( true );
}
}