-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFontsamplerPlugin.php
executable file
·891 lines (766 loc) · 35.1 KB
/
FontsamplerPlugin.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
<?php
/**
* Class FontsamplerPlugin
*
* Main plugin class containing the frontend and backend routines
* for rendering Fontsampler instances from shortcodes
*/
class FontsamplerPlugin {
public $font_formats;
public $settings_defaults;
public $font_formats_legacy;
public $default_features;
public $admin_hide_legacy_formats;
public $fontsampler_db_version;
public $twig;
// helper classes
public $msg;
public $db;
public $helpers;
public $notifications;
private $forms;
const FONTSAMPLER_OPTION_DB_VERSION = 'fontsampler_db_version';
const FONTSAMPLER_OPTION_LAST_CHANGELOG = 'fontsampler_last_changelog'; // note, hardcoded also in FontsamplerHelpers.php:351 due to bug
const FONTSAMPLER_OPTION_HIDE_LEGACY_FORMATS = 'fontsampler_hide_legacy_formats';
const FONTSAMPLER_OPTION_PROXY_URLS = 'fontsampler_proxy_urls';
const FONTSAMPLER_PROXY_URL = 'fontsamplerfile';
const FONTSAMPLER_PROXY_QUERY_VAR = 'fontsamplerfile';
public function __construct($wpdb, $twig) {
$this->wpdb = $wpdb;
$this->twig = $twig;
// TODO combined default_features and boolean options as array of objects
// with "isBoolean" attribute
$this->default_features = array(
'fontsize',
'letterspacing',
'lineheight',
'sampletexts',
'alignment',
'invert',
'multiline',
'opentype',
'locl',
'fontpicker',
'buy',
'specimen',
);
// note: font_formats order matters: most preferred to least preferred
// note: so far no feature detection and no fallbacks, so woff2 last until fixed
$this->font_formats = array('woff', 'ttf', 'eot');//, 'woff2' );
$this->font_formats_legacy = array('eot', 'ttf');
$this->settings_defaults = array(
/* translators: Default label for font size slider in the front end */
'fontsize_label' => __('Size', 'fontsampler'),
'fontsize_min' => '8',
'fontsize_max' => '96',
'fontsize_initial' => '14',
'fontsize_unit' => 'px',
/* translators: Default label for letter spacing slider in the front end */
'letterspacing_label' => __('Letter spacing', 'fontsampler'),
'letterspacing_min' => '-5',
'letterspacing_max' => '5',
'letterspacing_initial' => '0',
'letterspacing_unit' => 'px',
/* translators: Default label for the line height slider in the front end */
'lineheight_label' => __('Line height', 'fontsampler'),
'lineheight_min' => '70',
'lineheight_max' => '300',
'lineheight_initial' => '110',
'lineheight_unit' => '%',
'alignment_initial' => 'left',
'sample_texts' => "hamburgerfontstiv\nabcdefghijklmnopqrstuvwxyz\nABCDEFGHIJKLMNOPQRSTUVWXYZ\nThe quick brown fox jumps over the lazy cat",
/* translators: Default selection in the sample text dropdown */
'sample_texts_default_option' => __('Select a sample text', 'fontsampler'),
'locl_options' => null,
/* translators: Default selection in the language (locl feature) dropdown */
'locl_default_option' => __('Select language', 'fontsampler'),
'css_color_text' => '#333333',
'css_color_background' => '#ffffff',
'css_color_label' => '#333333',
'css_value_size_label' => 'inherit',
'css_value_fontfamily_label' => 'inherit',
'css_value_lineheight_label' => 'normal',
'css_color_button_background' => '#efefef',
'css_color_button_background_inactive' => '#dfdfdf',
'css_color_highlight' => '#efefef',
'css_color_highlight_hover' => '#dedede',
'css_color_line' => '#333333',
'css_color_handle' => '#333333',
'css_value_column_gutter' => '10px',
'css_value_row_height' => '30px',
'css_value_row_gutter' => '10px',
'css_color_notdef' => '#dedede',
'fontsize' => 1,
'letterspacing' => 1,
'lineheight' => 1,
'sampletexts' => 0,
'locl' => 0,
'alignment' => 0,
'invert' => 0,
'multiline' => 1,
'opentype' => 0,
'fontpicker' => 0,
'buy' => 0,
'specimen' => 0,
'notdef' => 0,
/* translators: Default label for the buy button in the front end */
'buy_label' => __('Buy', 'fontsampler'),
'buy_image' => null,
'buy_url' => null,
'buy_type' => 'label',
'buy_target' => '_blank',
/* translators: Default label for the specimen download button in the front end */
'specimen_label' => __('Specimen', 'fontsampler'),
'specimen_image' => null,
'specimen_url' => null,
'specimen_type' => 'label',
'specimen_target' => '_blank',
'ui_columns' => 3,
'ui_order' => null,
'is_ltr' => 1,
'notdef' => 0, // by default do nothing on notdef glyph input
'initial' => null // initial fontset_id
);
}
public function init() {
// instantiate all needed helper subclasses
$this->msg = new FontsamplerMessages();
$this->helpers = new FontsamplerHelpers($this);
$this->db = new FontsamplerDatabase($this->wpdb, $this);
$this->notifications = new FontsamplerNotifications($this);
// keep track of db versions and migrations via this
// simply set this to the current PLUGIN VERSION number when bumping it
// i.e. a database update always bumps the version number of the plugin as well
$this->fontsampler_db_version = '0.4.0';
$current_db_version = get_option(self::FONTSAMPLER_OPTION_DB_VERSION);
// if no previous db version has been registered assume new install and set
// to current version
if (!$current_db_version) {
add_option(self::FONTSAMPLER_OPTION_DB_VERSION, $this->fontsampler_db_version);
$current_db_version = $this->fontsampler_db_version;
}
if (version_compare($current_db_version, $this->fontsampler_db_version) < 0) {
$this->db->migrate_db();
}
// check if to display legacy formats or not
$option_legacy_formats = get_option(self::FONTSAMPLER_OPTION_HIDE_LEGACY_FORMATS);
// set the option in the db, if it's unset; default to hiding the legacy formats
if ($option_legacy_formats === false) {
add_option(self::FONTSAMPLER_OPTION_HIDE_LEGACY_FORMATS, '1');
$this->admin_hide_legacy_formats = 1;
} else {
$this->admin_hide_legacy_formats = $option_legacy_formats;
}
// check if to proxy files urls or not
$option_proxy_urls = get_option(self::FONTSAMPLER_OPTION_PROXY_URLS);
// set the option in the db, if it's unset; default to no
if ($option_proxy_urls === false) {
add_option(self::FONTSAMPLER_OPTION_PROXY_URLS, '0');
$this->admin_hide_legacy_formats = 0;
} else {
$this->admin_hide_legacy_formats = $option_proxy_urls;
}
// hoist some of those settings defaults to the twig engine, so some things
// are available globally in all twig templates
$this->helpers->extend_twig($this->twig);
}
/*
* DIFFERENT HOOKS
*/
/*
* Register the [fontsampler id=XX] hook for use in pages and posts
*/
public function fontsampler_shortcode($atts) {
global $wp_styles;
// on printing the shortcode enqueue the scripts to be output to the footer
wp_enqueue_script('fontsampler-js');
// when the shortcode was detected already in the header the styles should
// be added already; if the styles are not enqueued (for example if called
// via do_shortcode()) then add them now, regardless of them getting added
// to the html body, which is less ideal
if (is_null($wp_styles) || !in_array('fontsampler-css', array_keys($wp_styles->registered))) {
$this->enqueue_styles();
}
// merge in possibly passed in attributes
$attributes = shortcode_atts(array('id' => '0', 'fonts' => null, 'text' => null), $atts);
$id = intval($attributes['id']);
// store any text that was passed in as part of the shortcode
// this will overwrite the text defined in the settings of this fontsampler
// or if only fonts are passed in as part of the shortcode the fontsampler
// will used that text
$attribute_text = $attributes['text'];
// do nothing if missing id
if (0 != $id) {
$set = $this->db->get_set($id);
$css = $this->helpers->get_custom_css($set); // returns false or link to generated custom css
$fonts = $this->helpers->get_best_file_from_fonts($this->db->get_fontset_for_set(intval($attributes['id'])));
if (false !== $css) {
wp_enqueue_style('fontsampler-interface-' . $id, $css, array(), false);
}
if (false == $set || false == $fonts) {
if (current_user_can('edit_posts') || current_user_can('edit_pages')) {
return '<div class="fontsampler-warning"><strong>The typesampler with ID ' . $attributes['id'] . ' can not be displayed because some files or the type sampler set are missing!</strong> <em>You are seeing this notice because you have rights to edit posts - regular users will see an empty spot here.</em></div>';
} else {
return '<!-- typesampler #' . $attributes['id'] . ' failed to render -->';
}
}
// some of these get overwritten from defaults, but list them all here explicitly
$options = array_merge($set, $this->settings_defaults, $this->db->get_settings());
$initialFont = isset($fonts[$set['initial_font']]) ? $fonts[$set['initial_font']] : false;
if ($initialFont) {
$firstFont = array_filter($set['fonts'], function ($item) use ($set) {
if ($item['id'] === $set['initial_font']) {
return $item;
}
});
$initialFontNameOverwrite = array_pop($firstFont)['name'];
}
$settings = $this->db->get_settings();
$layout = new FontsamplerLayout();
$blocks = $layout->stringToArray($set['ui_order'], $set);
// get the calculated initial values for data-font-size- etc, where the set overwrites options
// where available
$data_initial = array();
foreach ($set as $key => $value) {
$data_initial[$key] = $value;
if ($value === null && $options[$key] !== null) {
$data_initial[$key] = $options[$key];
}
}
// create an array for fontnames to overwrite
$fontNameOverwrites = array();
foreach ($set['fonts'] as $font) {
foreach ($this->font_formats as $format) {
if (!empty($font[$format])) {
$fontNameOverwrites[$font[$format]] = $font['name'];
}
}
}
// buffer output until return
ob_start(); ?>
<div class='fontsampler-wrapper on-loading'
data-fonts='<?php echo implode(',', $fonts); ?>'
<?php if ($initialFont) : ?>
data-initial-font='<?php echo $initialFont; ?>'
data-initial-font-name-overwrite='<?php echo $initialFontNameOverwrite; ?>'
<?php endif; ?>
<?php if (!empty($fontNameOverwrites)): ?>
data-overwrites='<?php echo json_encode($fontNameOverwrites); ?>'
<?php endif; ?>
>
<?php
// include, aka echo, template with replaced values from $replace above
include 'includes/interface.php';
echo '</div>';
// return all that's been buffered
return ob_get_clean();
} elseif ($attributes['fonts'] !== null) {
// you cannot pass in a json encoded string (because the square brackets would
// be interpreted as the end of the shortcode), so the json encoded string is
// EXPECTED to be a json encoded array, but missing the opening and ending
// square brackets
$fonts_passed_in = json_decode('[' . $attributes['fonts'] . ']');
$fonts_passed_in = array_map(function ($item, $index) {
$item = (array) $item;
// the fake font id is needed for fontsampler internals,
// but since these are not fonts exising in the DB the
// passed in array index will suffice as ID
$item['id'] = $index;
return $item;
}, $fonts_passed_in, array_keys($fonts_passed_in));
// retrieve an array with the must suitable webfont files
$fonts = $this->helpers->get_best_file_from_fonts($fonts_passed_in);
// some of these get overwritten from defaults, but list them all here explicitly
// $set = $this->db->get_set( $id );
$set = $this->db->get_settings();
$set['id'] = 0;
$options = array_merge($set, $this->settings_defaults, $this->db->get_settings());
$settings = $this->db->get_settings();
$layout = new FontsamplerLayout();
$blocks = $layout->stringToArray($set['ui_order'], $set);
$initialFont = array_filter($fonts_passed_in, function ($item) {
return isset($item['initial']) && $item['initial'] === true;
});
$initialFont = array_shift($initialFont) ;
$initialFontNameOverwrite = $initialFont['name'];
$initialFont = $this->helpers->get_best_file_from_fonts(array($initialFont));
$initialFont = is_array($initialFont) ? array_shift($initialFont) : '';
$data_initial = $settings;
// create an array for fontnames to overwrite
$fontNameOverwrites = array();
foreach ($fonts_passed_in as $font) {
foreach ($this->font_formats as $format) {
if (!empty($font[$format])) {
$fontNameOverwrites[$font[$format]] = $font['name'];
}
}
}
ob_start(); ?>
<div class='fontsampler-wrapper on-loading'
data-fonts='<?php echo implode(',', $fonts); ?>'
<?php if ($initialFont) : ?>
data-initial-font='<?php echo $initialFont; ?>'
<?php endif; ?>
<?php if (!empty($fontNameOverwrites)): ?>
data-overwrites='<?php echo json_encode($fontNameOverwrites); ?>'
data-initial-font-name-overwrite='<?php echo $initialFontNameOverwrite; ?>'
<?php endif; ?>
>
<?php
// include, aka echo, template with replaced values from $replace above
include 'includes/interface.php';
echo '</div>';
return ob_get_clean();
}
}
/**
* To output the plugin styles only on pages that have the shortcode check
* in the 'wp' action hook if this page seems to have the shortcode and enqueue
* the styles if so
*/
public function check_shortcodes_enqueue_styles() {
global $post;
if ($post) {
if (has_shortcode($post->post_content, 'fontsampler') || apply_filters('fontsampler_enqueue_styles', false)) {
$this->enqueue_styles();
}
}
}
/**
* The actual style enqueue
*/
public function enqueue_styles() {
wp_enqueue_style('fontsampler-css', $this->helpers->get_css_file());
}
/*
* Register scripts and styles needed in the admin panel
*/
public function fontsampler_admin_enqueues($hook) {
// Load only on fontsampler plugin page hooks
if (preg_match('/fontsampler/i', $hook) === 0) {
return;
}
wp_enqueue_script('fontsampler-clipboard', plugin_dir_url(__FILE__) . 'admin/js/clipboard.min.js');
wp_enqueue_script('fontsampler-admin-main-js', plugin_dir_url(__FILE__) . 'admin/js/fontsampler-admin.js', array(
'jquery',
'wp-color-picker',
'jquery-ui-sortable',
'jquery-ui-accordion',
'fontsampler-clipboard', // make clipboard a global requirement
), false, true);
wp_localize_script('fontsampler-admin-main-js', 'nonce', wp_create_nonce('fontsampler-admin-ajax'));
wp_enqueue_style('wp-color-picker');
wp_enqueue_style('jquery-ui-accordion');
wp_enqueue_style('fontsampler-css-admin', $this->helpers->get_css_file());
wp_enqueue_style('fontsampler_admin_css', plugin_dir_url(__FILE__) . '/admin/css/fontsampler-admin.css', false, '1.0.0');
wp_enqueue_media();
}
/*
* Add the fontsampler admin menu to the sidebar
*/
public function fontsampler_plugin_setup_menu() {
$numNotifications = $this->notifications->get_notifications()['num_notifications'];
$notifications = '';
if ($numNotifications > 0) {
$notifications = ' <span class="update-plugins count-' . $numNotifications . '">
<span class="plugin-count">' . $numNotifications . '</span></span>';
}
add_menu_page(
'Fontsampler plugin page',
'Fontsampler' . $notifications,
'manage_options',
'fontsampler',
array(
$this,
'fontsampler_admin_init',
),
'dashicons-editor-paragraph'
);
add_submenu_page(
'fontsampler',
'New Fontsampler',
'New Fontsampler',
'manage_options',
'fontsampler-new',
array($this, 'fontsampler_admin_init')
);
add_submenu_page(
'fontsampler',
'Settings',
'Settings',
'manage_options',
'fontsampler-settings',
array($this, 'fontsampler_admin_init')
);
// replace default entry label for main Fontsampler menu entry
global $submenu;
$submenu['fontsampler'][0][0] = 'All Fontsamplers';
}
/**
* Register the text deomain for translations
*/
public function fontsampler_load_text_domain() {
load_plugin_textdomain('fontsampler', false, dirname(plugin_basename(__FILE__)) . '/lang/');
}
/*
* Expand allowed upload types to include font files
*/
public function allow_font_upload_types($existing_mimes = array()) {
// $existing_mimes['woff'] = 'application/font-woff';
// $existing_mimes['woff2'] = 'application/font-woff2';
// $existing_mimes['eot'] = 'application/eot';
// $existing_mimes['ttf'] = 'application/ttf';
$existing_mimes['woff'] = 'font/woff';
$existing_mimes['woff2'] = 'font/woff2';
$existing_mimes['ttf'] = 'font/truetype';
$existing_mimes['otf'] = 'font/opentype';
return $existing_mimes;
}
/**
* Fix Upload MIME detection
*
* A workaround needed in addition to adding upload_mimes since 4.7.1
* Still required in 5.3.x
*
* After adding the font mime types to allowed mimes in the check for validity
* of a file check here explicitly if the file extension of the file is listed
* in the allowed mime types (which it is after we added it)
*
* @param checked [ext, type, proper_filename]
* @param file
* @param filename
* @param mimes
*/
function check_upload_extension($checked, $file, $filename, $mimes) {
if (false === $checked['ext'] && false === $checked['type'] && false === $checked['proper_filename']) {
$filetype = wp_check_filetype($filename);
$wp_mimes = get_allowed_mime_types();
if (in_array($filetype['ext'], array_keys($wp_mimes))) {
$checked['ext'] = true;
$checked['type'] = true;
return $checked;
}
}
return $checked;
}
/*
* Augment the plugin description links
*/
public function add_action_links($links) {
$mylinks = array(
'<a href="' . admin_url('admin.php?page=fontsampler') . '">Settings</a>',
);
return array_merge($links, $mylinks);
}
/*
* React to the plugin being activated
*/
public function fontsampler_activate() {
// If this is a new install the changelog doesnot need to be shown, set the "viewed" option
if (false === get_option('fontsampler_last_changelog')) {
// this throws obscure error on PHP 5.6
//$option = update_option( $this->fontsampler::FONTSAMPLER_OPTION_LAST_CHANGELOG, $plugin['Version'] );
$plugin = get_plugin_data(realpath(dirname(__FILE__) . '/fontsampler.php'));
update_option('fontsampler_last_changelog', $plugin['Version']);
}
$this->db = new FontsamplerDatabase($this->wpdb, $this);
$this->helpers = new FontsamplerHelpers($this);
$this->db->check_and_create_tables();
$this->helpers->check_and_create_folders();
flush_rewrite_rules();
}
/**
* Rewriting the custom proxy endpoint to serve a files
*/
public function fontsampler_template_redirect() {
$id = get_query_var($this::FONTSAMPLER_PROXY_QUERY_VAR);
if ($id) {
$post = get_post($id);
$path = get_attached_file($id);
if ($post && is_file($path) && is_readable($path)) {
$file = file_get_contents($path);
header('Content-Type: ' . $post->post_mime_type);
header('Content-Length: ' . filesize($path));
echo $file;
exit();
}
}
}
/**
* Add the rewrite query var to the allowed vars
*/
function fontsampler_query_vars($vars) {
$vars[] = $this::FONTSAMPLER_PROXY_QUERY_VAR;
return $vars;
}
/*
* FLOW CONTROL
*/
/*
* Rendering the admin interface and dealing for form interactions
*/
public function fontsampler_admin_init() {
global $title;
// if entering this hook from the submenu callbacks, load different subpages
if ($title === 'Settings') {
$_GET['subpage'] = 'settings';
}
if ($title === 'New Fontsampler') {
$_GET['subpage'] = 'set_create';
}
echo '<section id="fontsampler-admin" class="';
echo $this->admin_hide_legacy_formats
? 'fontsampler-admin-hide-legacy-formats'
: 'fontsampler-admin-show-legacy-formats';
echo '">';
$this->db->check_and_create_tables();
// check upload folder is writable
$dir = wp_upload_dir();
$upload = $dir['basedir'];
if (!is_dir($upload)) {
echo '<p>Uploads folder does not exist! Make sure Wordpress has writing
permissions to create the uploads folder at: <em>' . $upload . '</em></p>';
}
// handle any kind of form processing and output any messages inside a notice, if there were any
if ('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['action'])) {
$this->forms = new FontsamplerFormhandler($this, $_POST, $_FILES);
// buffer the output generated during form processing, then, if
// not empty, print any output wrapped in a notice element
ob_start(function ($buffer) {
if (!empty($buffer)) {
return '<div class="notice">' . $buffer . '</div>';
}
});
$id = isset($_POST['id']) && intval($_POST['id']) > 0 ? intval($_POST['id']) : null;
switch ($_POST['action']) {
case 'edit_font':
if (!isset($id)) {
$this->forms->handle_font_insert();
} else {
$this->forms->handle_font_edit($id);
}
break;
case 'duplicate_set':
$id = $id = isset($_GET['id']) && intval($_GET['id']) > 0 ? intval($_GET['id']) : null;
if (isset($id)) {
$new = $this->forms->handle_duplicate_set($id);
if ($new) {
// Redirect to same admin address, but new set id
$_GET['id'] = $new;
$args = http_build_query($_GET);
wp_safe_redirect($_SERVER['REQUEST_URI'] . '?' . $args);
exit();
}
}
break;
case 'delete_font':
$this->forms->handle_font_delete($id);
break;
case 'edit_set':
if (!isset($id)) {
$this->forms->handle_set_insert();
} else {
$this->forms->handle_set_edit($id);
}
break;
case 'delete_set':
$this->forms->handle_set_delete($id);
break;
case 'edit_settings':
$this->forms->handle_settings_edit();
break;
case 'reset_settings':
if ($this->forms->handle_settings_reset()) {
$this->msg->add_info('Settings successfully reset. You may have to refresh the page for the reset CSS to reload.');
}
break;
case 'fix_default_settings':
if ($this->db->fix_settings_from_defaults()) {
$this->msg->add_info('Settings successfully restored from defaults');
}
break;
case 'hide_changelog':
$this->helpers->hide_changelog();
break;
default:
$this->msg->add_notice('Form submitted, but no matching action found for ' . $_POST['action']);
break;
}
ob_end_flush();
}
$subpage = isset($_GET['subpage']) ? $_GET['subpage'] : '';
switch ($subpage) {
case 'set_create':
$defaults = $this->db->get_default_settings();
$set = $defaults;
$set['use_defaults'] = 1;
$set['alignment_initial'] = null;
// generate all necessary info for the live layout preview
$layout = new FontsamplerLayout();
$str = $layout->sanitizeString($set['ui_order'], $set);
$layout->stringToArray($str);
$ui_order = !empty($set['ui_order'])
? $layout->sanitizeString($set['ui_order'], $set)
: $layout->arrayToString($layout->getDefaultBlocks(), $set);
$blocks = array_merge($layout->getDefaultBlocks(), $layout->stringToArray($set['ui_order'], $set));
echo $this->twig->render('set-edit.twig', array(
'set' => $set,
'defaults' => $defaults,
'fonts' => $this->db->get_fontfile_posts(),
'ui_order' => $ui_order,
'blocks' => $blocks
));
break;
case 'set_edit':
$defaults = $this->db->get_settings();
$set = $this->db->get_set(intval($_GET['id']));
// generate all necessary info for the live layout preview
$layout = new FontsamplerLayout();
$str = $layout->sanitizeString($set['ui_order'], $set);
$layout->stringToArray($str);
$ui_order = !empty($set['ui_order'])
? $layout->sanitizeString($set['ui_order'], $set)
: $layout->arrayToString($layout->getDefaultBlocks(), $set);
$blocks = array_merge($layout->getDefaultBlocks(), $layout->stringToArray($set['ui_order'], $set));
// grab all possible included fonts
$fonts = $this->db->get_fontfile_posts();
$fonts_order = implode(',', array_map(function ($font) {
return $font['id'];
}, $set['fonts']));
if (empty($set)) {
$this->msg->add_error('No set selected');
}
echo $this->twig->render('set-edit.twig', array(
'set' => $set,
'defaults' => $defaults,
'fonts' => $fonts,
'fonts_order' => $fonts_order,
'ui_order' => $ui_order,
'blocks' => $blocks
));
break;
case 'set_delete':
$set = $this->db->get_set(intval($_GET['id']));
echo $this->twig->render('set-delete.twig', array('set' => $set));
break;
case 'fonts':
$offset = isset($_GET['offset']) ? intval($_GET['offset']) : 0;
$num_rows = isset($_GET['num_rows']) ? intval($_GET['num_rows']) : 10;
$initials = $this->db->get_fontsets_initials();
$pagination = new FontsamplerPagination($initials, $num_rows, false, $offset);
echo $this->twig->render(
'fontsets.twig',
array(
'fonts' => $this->db->get_fontsets($offset, $num_rows),
'pagination' => $pagination->pages('?page=fontsampler&subpage=fonts&offset=###first###&num_rows=###items###', sizeof($initials)),
)
);
break;
case 'font_create':
echo $this->twig->render('fontset-edit.twig');
break;
case 'font_edit':
echo $this->twig->render('fontset-edit.twig', array(
'font' => $this->db->get_fontset(intval($_GET['id']))
));
break;
case 'font_delete':
$font = $this->db->get_fontset(intval($_GET['id']));
echo $this->twig->render('fontset-delete.twig', array('font' => $font));
break;
case 'settings':
// $this->helpers->check_is_writeable( plugin_dir_path( __FILE__ ) . 'css/fontsampler-css.css', true );
$settings = $this->db->get_default_settings();
// generate all necessary info for the live layout preview
$layout = new FontsamplerLayout();
$str = $layout->sanitizeString($settings['ui_order'], $settings);
$layout->stringToArray($str);
$ui_order = empty($settings['ui_order'])
? $layout->arrayToString($layout->getDefaultBlocks(), $settings)
: $layout->sanitizeString($settings['ui_order'], $settings);
$blocks = array_merge($layout->getDefaultBlocks(), $layout->stringToArray($settings['ui_order'], $settings));
echo $this->twig->render('settings.twig', array(
'set' => $settings,
'defaults' => $settings, // for the most part use 'set', but some sliders read the "default" value
'ui_order' => $ui_order,
'blocks' => $blocks
));
break;
case 'settings_reset':
echo $this->twig->render('settings-reset.twig');
break;
case 'changelog':
echo $this->twig->render('changelog.twig');
break;
case 'about':
echo $this->twig->render('about.twig');
break;
case 'notifications':
$notifications = $this->notifications->get_notifications();
echo $this->twig->render('notifications.twig', array(
'missing_files' => $notifications['fonts_missing_files'],
'missing_fonts' => $notifications['sets_missing_fonts'],
'missing_names' => $notifications['fonts_missing_name'],
'missing_settings' => $notifications['settings_defaults'],
'folder_permissions' => $notifications['folder_permissions']
));
break;
default:
$offset = isset($_GET['offset']) ? intval($_GET['offset']) : 0;
$num_rows = isset($_GET['num_rows']) ? intval($_GET['num_rows']) : 10;
$initials = $this->db->get_samples_ids();
$pagination = new FontsamplerPagination($initials, $num_rows, true, $offset);
$sets = $this->db->get_sets($offset, $num_rows);
echo $this->twig->render(
'sets.twig',
array(
'sets' => $sets,
'pagination' => $pagination->pages('?page=fontsampler&subpage=sets&offset=###first###&num_rows=###items###', sizeof($initials)),
)
);
break;
}
echo '</section>';
}
/**
* Registered ajax action to get a mockup fontsampler in the admin interface
* for layout previewing
*/
public function ajax_get_mock_fontsampler() {
check_ajax_referer('fontsampler-admin-ajax', 'security');
if (!current_user_can('edit_posts')) {
die();
}
$layout = new FontsamplerLayout();
$data = $_POST['data'];
// data['ui_order'] contains a string with all the blocks transmitted
// from the admin UI
$fields = array_keys($layout->stringToArray($data['ui_order']));
// fill all these with a simple "1", like they would be fetched from a
// set in the db that has those fields enabled (or content in them signifiying
// they should render)
$fieldsFromUI = array_combine($fields, array_fill(0, sizeof($fields), 1));
// emulate a set from the passed in mock data
// if any field like "specimen" got not just passed in ui_order but as explicit
// field with content, that overwrites the "1" array value
$set = array_merge($fieldsFromUI, $data, array(
'multiline' => 0,
'is_ltr' => 1
));
$font = plugin_dir_url(__FILE__) . 'admin/fonts/PTS55F-webfont.woff';
$set['fonts'] = array('woff' => $font);
// from this set create all blocks; pass in the generated set to make
// sure all fields sync
$blocks = $layout->stringToArray($set['ui_order'], $set);
$options = $this->db->get_settings();
$settings = $options; ?>
<div class='fontsampler-wrapper on-loading'
data-fonts='<?php echo $font; ?>'
data-initial-font='<?php echo $font; ?>'
>
<?php include 'includes/interface.php'; ?>
</div>
<?php
die();
}
}