-
Notifications
You must be signed in to change notification settings - Fork 8
/
FontsamplerPlugin.php
executable file
·788 lines (660 loc) · 26.5 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
<?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';
function __construct( $wpdb, $twig ) {
$this->wpdb = $wpdb;
$this->twig = $twig;
}
function init() {
// 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
);
// 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;
}
// 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
*/
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 (!in_array('fontsampler-css', array_keys($wp_styles->registered))) {
echo "shortcode called, but styles not present";
$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'] ) ) );
var_dump($fonts);
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 = [];
foreach ($set['fonts'] as $font) {
foreach ($this->font_formats as $format) {
if (!empty($font[$format])) {
$fileName = basename($font[$format]);
$fontNameOverwrites[$fileName] = $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();
}
else if ( $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 = [];
foreach ($fonts_passed_in as $font) {
foreach ($this->font_formats as $format) {
if (!empty($font[$format])) {
$fileName = basename($font[$format]);
$fontNameOverwrites[$fileName] = $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
*/
function check_shortcodes_enqueue_styles() {
global $post;
if ( has_shortcode( $post->post_content, 'fontsampler') || apply_filters('fontsampler_enqueue_styles', false) ) {
$this->enqueue_styles();
}
}
/**
* The actual style enqueue
*/
function enqueue_styles() {
wp_enqueue_style( 'fontsampler-css', $this->helpers->get_css_file() );
}
/*
* Register scripts and styles needed in the admin panel
*/
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_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
*/
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
*/
function fontsampler_load_text_domain() {
load_plugin_textdomain( 'fontsampler', false, dirname( plugin_basename(__FILE__) ) . '/lang/' );
}
/*
* Expand allowed upload types to include font files
*/
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';
return $existing_mimes;
}
/*
* Augment the plugin description links
*/
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
*/
function fontsampler_activate() {
$this->db->check_and_create_tables();
$this->helpers->check_and_create_folders();
}
/*
* FLOW CONTROL
*/
/*
* Rendering the admin interface and dealing for form interactions
*/
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 '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
*/
function ajax_get_mock_fontsampler () {
check_ajax_referer( 'ajax_get_mock_fontsampler', 'action', false );
$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();
}
}