-
Notifications
You must be signed in to change notification settings - Fork 247
/
crayon_settings_wp.class.php
1273 lines (1139 loc) · 60.2 KB
/
crayon_settings_wp.class.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
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
require_once('global.php');
require_once(CRAYON_LANGS_PHP);
require_once(CRAYON_THEMES_PHP);
require_once(CRAYON_FONTS_PHP);
require_once(CRAYON_SETTINGS_PHP);
/* Manages global settings within WP and integrates them with CrayonSettings.
CrayonHighlighter and any non-WP classes will only use CrayonSettings to separate
the implementation of global settings and ensure any system can use them. */
class CrayonSettingsWP {
// Properties and Constants ===============================================
// A copy of the current options in db
private static $options = NULL;
// Posts containing crayons in db
private static $crayon_posts = NULL;
// Posts containing legacy tags in db
private static $crayon_legacy_posts = NULL;
// An array of cache names for use with Transients API
private static $cache = NULL;
// Array of settings to pass to js
private static $js_settings = NULL;
private static $js_strings = NULL;
private static $admin_js_settings = NULL;
private static $admin_js_strings = NULL;
private static $admin_page = '';
private static $is_fully_loaded = FALSE;
const SETTINGS = 'crayon_fields';
const FIELDS = 'crayon_settings';
const OPTIONS = 'crayon_options';
const POSTS = 'crayon_posts';
const LEGACY_POSTS = 'crayon_legacy_posts';
const CACHE = 'crayon_cache';
const GENERAL = 'crayon_general';
const DEBUG = 'crayon_debug';
const ABOUT = 'crayon_about';
// Used on submit
const LOG_CLEAR = 'log_clear';
const LOG_EMAIL_ADMIN = 'log_email_admin';
const LOG_EMAIL_DEV = 'log_email_dev';
const SAMPLE_CODE = 'sample-code';
const CACHE_CLEAR = 'crayon-cache-clear';
private function __construct() {
}
// Methods ================================================================
public static function admin_load() {
self::$admin_page = $admin_page = add_options_page('Crayon Syntax Highlighter ' . crayon__('Settings'), 'Crayon', 'manage_options', 'crayon_settings', 'CrayonSettingsWP::settings');
add_action("admin_print_scripts-$admin_page", 'CrayonSettingsWP::admin_scripts');
add_action("admin_print_styles-$admin_page", 'CrayonSettingsWP::admin_styles');
add_action("admin_print_scripts-$admin_page", 'CrayonThemeEditorWP::admin_resources');
// Register settings, second argument is option name stored in db
register_setting(self::FIELDS, self::OPTIONS, 'CrayonSettingsWP::settings_validate');
add_action("admin_head-$admin_page", 'CrayonSettingsWP::admin_init');
// Register settings for post page
add_action("admin_print_styles-post-new.php", 'CrayonSettingsWP::admin_scripts');
add_action("admin_print_styles-post.php", 'CrayonSettingsWP::admin_scripts');
add_action("admin_print_styles-post-new.php", 'CrayonSettingsWP::admin_styles');
add_action("admin_print_styles-post.php", 'CrayonSettingsWP::admin_styles');
// TODO deprecated since WP 3.3, remove eventually
global $wp_version;
if ($wp_version >= '3.3') {
add_action("load-$admin_page", 'CrayonSettingsWP::help_screen');
} else {
add_filter('contextual_help', 'CrayonSettingsWP::cont_help', 10, 3);
}
}
public static function admin_styles() {
global $CRAYON_VERSION;
if (CRAYON_MINIFY) {
wp_enqueue_style('crayon', plugins_url(CRAYON_STYLE_MIN, __FILE__), array('editor-buttons'), $CRAYON_VERSION);
} else {
wp_enqueue_style('crayon', plugins_url(CRAYON_STYLE, __FILE__), array(), $CRAYON_VERSION);
wp_enqueue_style('crayon_global', plugins_url(CRAYON_STYLE_GLOBAL, __FILE__), array(), $CRAYON_VERSION);
wp_enqueue_style('crayon_admin', plugins_url(CRAYON_STYLE_ADMIN, __FILE__), array('editor-buttons'), $CRAYON_VERSION);
}
}
public static function admin_scripts() {
global $CRAYON_VERSION;
if (CRAYON_MINIFY) {
CrayonWP::enqueue_resources();
} else {
wp_enqueue_script('crayon_util_js', plugins_url(CRAYON_JS_UTIL, __FILE__), array('jquery'), $CRAYON_VERSION);
self::other_scripts();
}
self::init_js_settings();
if (is_admin()) {
wp_enqueue_script('crayon_admin_js', plugins_url(CRAYON_JS_ADMIN, __FILE__), array('jquery', 'crayon_js', 'wpdialogs'), $CRAYON_VERSION);
self::init_admin_js_settings();
}
}
public static function other_scripts() {
global $CRAYON_VERSION;
self::load_settings(TRUE);
$deps = array('jquery', 'crayon_util_js');
if (CrayonGlobalSettings::val(CrayonSettings::POPUP) || is_admin()) {
// TODO include anyway and minify
wp_enqueue_script('crayon_jquery_popup', plugins_url(CRAYON_JQUERY_POPUP, __FILE__), array('jquery'), $CRAYON_VERSION);
$deps[] = 'crayon_jquery_popup';
}
wp_enqueue_script('crayon_js', plugins_url(CRAYON_JS, __FILE__), $deps, $CRAYON_VERSION);
}
public static function init_js_settings() {
// This stores JS variables used in AJAX calls and in the JS files
global $CRAYON_VERSION;
self::load_settings(TRUE);
if (!self::$js_settings) {
self::$js_settings = array(
'version' => $CRAYON_VERSION,
'is_admin' => intval(is_admin()),
'ajaxurl' => admin_url('admin-ajax.php'),
'prefix' => CrayonSettings::PREFIX,
'setting' => CrayonSettings::SETTING,
'selected' => CrayonSettings::SETTING_SELECTED,
'changed' => CrayonSettings::SETTING_CHANGED,
'special' => CrayonSettings::SETTING_SPECIAL,
'orig_value' => CrayonSettings::SETTING_ORIG_VALUE,
'debug' => CRAYON_DEBUG
);
}
if (!self::$js_strings) {
self::$js_strings = array(
'copy' => crayon__('Press %s to Copy, %s to Paste'),
'minimize' => crayon__('Click To Expand Code')
);
}
if (CRAYON_MINIFY) {
wp_localize_script('crayon_js', 'CrayonSyntaxSettings', self::$js_settings);
wp_localize_script('crayon_js', 'CrayonSyntaxStrings', self::$js_strings);
} else {
wp_localize_script('crayon_util_js', 'CrayonSyntaxSettings', self::$js_settings);
wp_localize_script('crayon_util_js', 'CrayonSyntaxStrings', self::$js_strings);
}
}
public static function init_admin_js_settings() {
if (!self::$admin_js_settings) {
// We need to load themes at this stage
CrayonSettingsWP::load_settings();
$themes_ = CrayonResources::themes()->get();
$stockThemes = array();
$userThemes = array();
foreach ($themes_ as $theme) {
$id = $theme->id();
$name = $theme->name();
if ($theme->user()) {
$userThemes[$id] = $name;
} else {
$stockThemes[$id] = $name;
}
}
self::$admin_js_settings = array(
'themes' => array_merge($stockThemes, $userThemes),
'stockThemes' => $stockThemes,
'userThemes' => $userThemes,
'defaultTheme' => CrayonThemes::DEFAULT_THEME,
'themesURL' => CrayonResources::themes()->dirurl(false),
'userThemesURL' => CrayonResources::themes()->dirurl(true),
'sampleCode' => self::SAMPLE_CODE,
'dialogFunction' => 'wpdialog'
);
wp_localize_script('crayon_admin_js', 'CrayonAdminSettings', self::$admin_js_settings);
}
if (!self::$admin_js_strings) {
self::$admin_js_strings = array(
'prompt' => crayon__("Prompt"),
'value' => crayon__("Value"),
'alert' => crayon__("Alert"),
'no' => crayon__("No"),
'yes' => crayon__("Yes"),
'confirm' => crayon__("Confirm"),
'changeCode' => crayon__("Change Code")
);
wp_localize_script('crayon_admin_js', 'CrayonAdminStrings', self::$admin_js_strings);
}
}
public static function settings() {
if (!current_user_can('manage_options')) {
wp_die(crayon__('You do not have sufficient permissions to access this page.'));
}
?>
<script type="text/javascript">
jQuery(document).ready(function () {
CrayonSyntaxAdmin.init();
});
</script>
<div id="crayon-main-wrap" class="wrap">
<div id="icon-options-general" class="icon32">
<br>
</div>
<h2>
Crayon Syntax Highlighter
<?php crayon_e('Settings'); ?>
</h2>
<?php self::help(); ?>
<form id="crayon-settings-form" action="options.php" method="post">
<?php
settings_fields(self::FIELDS);
?>
<?php
do_settings_sections(self::SETTINGS);
?>
<p class="submit">
<input type="submit" name="submit" id="submit" class="button-primary"
value="<?php
crayon_e('Save Changes');
?>"/><span style="width:10px; height: 5px; float:left;"></span>
<input type="submit"
name="<?php echo self::OPTIONS; ?>[reset]"
id="reset"
class="button-primary"
value="<?php
crayon_e('Reset Settings');
?>"/>
</p>
</form>
</div>
<div id="crayon-theme-editor-wrap" class="wrap"></div>
<?php
}
// Load the global settings and update them from the db
public static function load_settings($just_load_settings = FALSE) {
if (self::$options === NULL) {
// Load settings from db
if (!(self::$options = get_option(self::OPTIONS))) {
self::$options = CrayonSettings::get_defaults_array();
update_option(self::OPTIONS, self::$options);
}
// Initialise default global settings and update them from db
CrayonGlobalSettings::set(self::$options);
}
if (!self::$is_fully_loaded && !$just_load_settings) {
// Load everything else as well
// For local file loading
// This is used to decouple WP functions from internal Crayon classes
CrayonGlobalSettings::site_url(home_url());
CrayonGlobalSettings::site_path(ABSPATH);
CrayonGlobalSettings::plugin_path(plugins_url('', __FILE__));
$upload = wp_upload_dir();
CrayonLog::debug($upload, "WP UPLOAD FUNCTION");
CrayonGlobalSettings::upload_path(CrayonUtil::path_slash($upload['basedir']) . CRAYON_DIR);
CrayonGlobalSettings::upload_url($upload['baseurl'] . '/' . CRAYON_DIR);
CrayonLog::debug(CrayonGlobalSettings::upload_path(), "UPLOAD PATH");
CrayonGlobalSettings::set_mkdir('wp_mkdir_p');
// Load all available languages and themes
CrayonResources::langs()->load();
CrayonResources::themes()->load();
// Ensure all missing settings in db are replaced by default values
$changed = FALSE;
foreach (CrayonSettings::get_defaults_array() as $name => $value) {
// Add missing settings
if (!array_key_exists($name, self::$options)) {
self::$options[$name] = $value;
$changed = TRUE;
}
}
// A setting was missing, update options
if ($changed) {
update_option(self::OPTIONS, self::$options);
}
self::$is_fully_loaded = TRUE;
}
}
public static function get_settings() {
return get_option(self::OPTIONS);
}
// Saves settings from CrayonGlobalSettings, or provided array, to the db
public static function save_settings($settings = NULL) {
if ($settings === NULL) {
$settings = CrayonGlobalSettings::get_array();
}
update_option(self::OPTIONS, $settings);
}
// Crayon posts
/**
* This loads the posts marked as containing Crayons
*/
public static function load_posts() {
if (self::$crayon_posts === NULL) {
// Load from db
if (!(self::$crayon_posts = get_option(self::POSTS))) {
// Posts don't exist! Scan for them. This will fill self::$crayon_posts
self::$crayon_posts = CrayonWP::scan_posts();
update_option(self::POSTS, self::$crayon_posts);
}
}
return self::$crayon_posts;
}
/**
* This looks through all posts and marks those which contain Crayons
*/
// public static function scan_and_save_posts() {
// self::save_posts(CrayonWP::scan_posts(TRUE, TRUE));
// }
/**
* Saves the marked posts to the db
*/
public static function save_posts($posts = NULL) {
if ($posts === NULL) {
$posts = self::$crayon_posts;
}
update_option(self::POSTS, $posts);
self::load_posts();
}
/**
* Adds a post as containing a Crayon
*/
public static function add_post($id, $save = TRUE) {
self::load_posts();
if (!in_array($id, self::$crayon_posts)) {
self::$crayon_posts[] = $id;
}
if ($save) {
self::save_posts();
}
}
/**
* Removes a post as not containing a Crayon
*/
public static function remove_post($id, $save = TRUE) {
self::load_posts();
$key = array_search($id, self::$crayon_posts);
if ($key === false) {
return;
}
unset(self::$crayon_posts[$key]);
if ($save) {
self::save_posts();
}
}
public static function remove_posts() {
self::$crayon_posts = array();
self::save_posts();
}
// Crayon legacy posts
/**
* This loads the posts marked as containing Crayons
*/
public static function load_legacy_posts($force = FALSE) {
if (self::$crayon_legacy_posts === NULL || $force) {
// Load from db
if (!(self::$crayon_legacy_posts = get_option(self::LEGACY_POSTS))) {
// Posts don't exist! Scan for them. This will fill self::$crayon_legacy_posts
self::$crayon_legacy_posts = CrayonWP::scan_legacy_posts();
update_option(self::LEGACY_POSTS, self::$crayon_legacy_posts);
}
}
return self::$crayon_legacy_posts;
}
/**
* This looks through all posts and marks those which contain Crayons
*/
// public static function scan_and_save_posts() {
// self::save_posts(CrayonWP::scan_posts(TRUE, TRUE));
// }
/**
* Saves the marked posts to the db
*/
public static function save_legacy_posts($posts = NULL) {
if ($posts === NULL) {
$posts = self::$crayon_legacy_posts;
}
update_option(self::LEGACY_POSTS, $posts);
self::load_legacy_posts();
}
/**
* Adds a post as containing a Crayon
*/
public static function add_legacy_post($id, $save = TRUE) {
self::load_legacy_posts();
if (!in_array($id, self::$crayon_legacy_posts)) {
self::$crayon_legacy_posts[] = $id;
}
if ($save) {
self::save_legacy_posts();
}
}
/**
* Removes a post as not containing a Crayon
*/
public static function remove_legacy_post($id, $save = TRUE) {
self::load_legacy_posts();
$key = array_search($id, self::$crayon_legacy_posts);
if ($key === false) {
return;
}
unset(self::$crayon_legacy_posts[$key]);
if ($save) {
self::save_legacy_posts();
}
}
public static function remove_legacy_posts() {
self::$crayon_legacy_posts = array();
self::save_legacy_posts();
}
// Cache
public static function add_cache($name) {
self::load_cache();
if (!in_array($name, self::$cache)) {
self::$cache[] = $name;
}
self::save_cache();
}
public static function remove_cache($name) {
self::load_cache();
$key = array_search($name, self::$cache);
if ($key === false) {
return;
}
unset(self::$cache[$key]);
self::save_cache();
}
public static function clear_cache() {
self::load_cache();
foreach (self::$cache as $name) {
delete_transient($name);
}
self::$cache = array();
self::save_cache();
}
public static function load_cache() {
// Load cache from db
if (!(self::$cache = get_option(self::CACHE))) {
self::$cache = array();
update_option(self::CACHE, self::$cache);
}
}
public static function save_cache() {
update_option(self::CACHE, self::$cache);
self::load_cache();
}
// Paths
public static function admin_init() {
// Load default settings if they don't exist
self::load_settings();
// General
// Some of these will the $editor arguments, if TRUE it will alter for use in the Tag Editor
self::add_section(self::GENERAL, crayon__('General'));
self::add_field(self::GENERAL, crayon__('Theme'), 'theme');
self::add_field(self::GENERAL, crayon__('Font'), 'font');
self::add_field(self::GENERAL, crayon__('Metrics'), 'metrics');
self::add_field(self::GENERAL, crayon__('Toolbar'), 'toolbar');
self::add_field(self::GENERAL, crayon__('Lines'), 'lines');
self::add_field(self::GENERAL, crayon__('Code'), 'code');
self::add_field(self::GENERAL, crayon__('Tags'), 'tags');
self::add_field(self::GENERAL, crayon__('Languages'), 'langs');
self::add_field(self::GENERAL, crayon__('Files'), 'files');
self::add_field(self::GENERAL, crayon__('Posts'), 'posts');
self::add_field(self::GENERAL, crayon__('Tag Editor'), 'tag_editor');
self::add_field(self::GENERAL, crayon__('Misc'), 'misc');
// Debug
self::add_section(self::DEBUG, crayon__('Debug'));
self::add_field(self::DEBUG, crayon__('Errors'), 'errors');
self::add_field(self::DEBUG, crayon__('Log'), 'log');
// ABOUT
self::add_section(self::ABOUT, crayon__('About'));
$image = '<div id="crayon-logo">
<img src="' . plugins_url(CRAYON_LOGO, __FILE__) . '" /><br/></div>';
self::add_field(self::ABOUT, $image, 'info');
}
// Wrapper functions
private static function add_section($name, $title, $callback = NULL) {
$callback = (empty($callback) ? 'blank' : $callback);
add_settings_section($name, $title, 'CrayonSettingsWP::' . $callback, self::SETTINGS);
}
private static function add_field($section, $title, $callback, $args = array()) {
$unique = preg_replace('#\\s#', '_', strtolower($title));
add_settings_field($unique, $title, 'CrayonSettingsWP::' . $callback, self::SETTINGS, $section, $args);
}
// Validates all the settings passed from the form in $inputs
public static function settings_validate($inputs) {
// Load current settings from db
self::load_settings(TRUE);
global $CRAYON_EMAIL;
// When reset button is pressed, remove settings so default loads next time
if (array_key_exists('reset', $inputs)) {
self::clear_cache();
return array();
}
// Convert old tags
if (array_key_exists('convert', $inputs)) {
$encode = array_key_exists('convert_encode', $inputs);
CrayonWP::convert_tags($encode);
}
// Refresh internal tag management
if (array_key_exists('refresh_tags', $inputs)) {
CrayonWP::refresh_posts();
}
// Clear the log if needed
if (array_key_exists(self::LOG_CLEAR, $_POST)) {
CrayonLog::clear();
}
// Send to admin
if (array_key_exists(self::LOG_EMAIL_ADMIN, $_POST)) {
CrayonLog::email(get_bloginfo('admin_email'));
}
// Send to developer
if (array_key_exists(self::LOG_EMAIL_DEV, $_POST)) {
CrayonLog::email($CRAYON_EMAIL, get_bloginfo('admin_email'));
}
// Clear the cache
if (array_key_exists(self::CACHE_CLEAR, $_POST)) {
self::clear_cache();
}
// If settings don't exist in input, set them to default
$global_settings = CrayonSettings::get_defaults();
$ignored = array(CrayonSettings::HIDE_HELP);
foreach ($global_settings as $setting) {
// XXX Ignore some settings
if (in_array($setting->name(), $ignored)) {
$inputs[$setting->name()] = CrayonGlobalSettings::val($setting->name());
continue;
}
// If boolean setting is not in input, then it is set to FALSE in the form
if (!array_key_exists($setting->name(), $inputs)) {
// For booleans, set to FALSE (unchecked boxes are not sent as POST)
if (is_bool($setting->def())) {
$inputs[$setting->name()] = FALSE;
} else {
/* For array settings, set the input as the value, which by default is the
default index */
if (is_array($setting->def())) {
$inputs[$setting->name()] = $setting->value();
} else {
$inputs[$setting->name()] = $setting->def();
}
}
}
}
$refresh = array(
// These should trigger a refresh of which posts contain crayons, since they affect capturing
CrayonSettings::INLINE_TAG => TRUE,
CrayonSettings::INLINE_TAG_CAPTURE => TRUE,
CrayonSettings::CODE_TAG_CAPTURE => TRUE,
CrayonSettings::BACKQUOTE => TRUE,
CrayonSettings::CAPTURE_PRE => TRUE,
CrayonSettings::CAPTURE_MINI_TAG => TRUE,
CrayonSettings::PLAIN_TAG => TRUE
);
// Validate inputs
foreach ($inputs as $input => $value) {
// Convert all array setting values to ints
$inputs[$input] = $value = CrayonSettings::validate($input, $value);
// Clear cache when changed
if (CrayonGlobalSettings::has_changed($input, CrayonSettings::CACHE, $value)) {
self::clear_cache();
}
if (isset($refresh[$input])) {
if (CrayonGlobalSettings::has_changed($input, $input, $value)) {
// Needs to take place, in case it refresh depends on changed value
CrayonGlobalSettings::set($input, $value);
CrayonWP::refresh_posts();
}
}
}
return $inputs;
}
// Section callback functions
public static function blank() {
} // Used for required callbacks with blank content
// Input Drawing ==========================================================
private static function input($args) {
$id = '';
$size = 40;
$margin = FALSE;
$preview = 1;
$break = FALSE;
$type = 'text';
extract($args);
echo '<input id="', CrayonSettings::PREFIX, $id, '" name="', self::OPTIONS, '[', $id, ']" class="' . CrayonSettings::SETTING . '" size="', $size, '" type="', $type, '" value="',
self::$options[$id], '" style="margin-left: ', ($margin ? '20px' : '0px'), ';" crayon-preview="', ($preview ? 1 : 0), '" />', ($break ? CRAYON_BR : '');
}
private static function checkbox($args, $line_break = TRUE, $preview = TRUE) {
if (empty($args) || !is_array($args) || count($args) != 2) {
return;
}
$id = $args[0];
$text = $args[1];
$checked = (!array_key_exists($id, self::$options)) ? FALSE : self::$options[$id] == TRUE;
$checked_str = $checked ? ' checked="checked"' : '';
echo '<input id="', CrayonSettings::PREFIX, $id, '" name="', self::OPTIONS, '[', $id, ']" type="checkbox" class="' . CrayonSettings::SETTING . '" value="1"', $checked_str,
' crayon-preview="', ($preview ? 1 : 0), '" /> ', '<label for="', CrayonSettings::PREFIX, $id, '">', $text, '</label>', ($line_break ? CRAYON_BR : '');
}
// Draws a dropdown by loading the default value (an array) from a setting
private static function dropdown($id, $line_break = TRUE, $preview = TRUE, $echo = TRUE, $resources = NULL, $selected = NULL) {
if (!array_key_exists($id, self::$options)) {
return;
}
$resources = $resources != NULL ? $resources : CrayonGlobalSettings::get($id)->def();
$return = '<select id="' . CrayonSettings::PREFIX . $id . '" name="' . self::OPTIONS . '[' . $id . ']" class="' . CrayonSettings::SETTING . '" crayon-preview="' . ($preview ? 1 : 0) . '">';
foreach ($resources as $k => $v) {
if (is_array($v) && count($v)) {
$data = $v[0];
$text = $v[1];
} else {
$text = $v;
}
$is_selected = $selected !== NULL && $selected == $k ? 'selected' : selected(self::$options[$id], $k, FALSE);
$return .= '<option ' . (isset($data) ? 'data-value="' . $data . '"' : '') . ' value="' . $k . '" ' . $is_selected . '>' . $text . '</option>';
}
$return .= '</select>' . ($line_break ? CRAYON_BR : '');
if ($echo) {
echo $return;
} else {
return $return;
}
}
private static function button($args = array()) {
extract($args);
CrayonUtil::set_var($id, '');
CrayonUtil::set_var($class, '');
CrayonUtil::set_var($onclick, '');
CrayonUtil::set_var($title, '');
return '<a id="' . $id . '" class="button-primary ' . $class . '" onclick="' . $onclick . '">' . $title . '</a>';
}
private static function info_span($name, $text) {
echo '<span id="', $name, '-info">', $text, '</span>';
}
private static function span($text) {
echo '<span>', $text, '</span>';
}
// General Fields =========================================================
public static function help() {
global $CRAYON_WEBSITE, $CRAYON_TWITTER, $CRAYON_GIT, $CRAYON_PLUGIN_WP, $CRAYON_DONATE;
if (CrayonGlobalSettings::val(CrayonSettings::HIDE_HELP)) {
return;
}
echo '<div id="crayon-help" class="updated settings-error crayon-help">
<p><strong>Howdy, coder!</strong> Thanks for using Crayon. <strong>Useful Links:</strong> <a href="' . $CRAYON_WEBSITE . '" target="_blank">Documentation</a>, <a href="' . $CRAYON_GIT . '" target="_blank">GitHub</a>, <a href="' . $CRAYON_PLUGIN_WP . '" target="_blank">Plugin Page</a>, <a href="' . $CRAYON_TWITTER . '" target="_blank">Twitter</a>. Crayon has always been free. If you value my work please consider a <a href="' . $CRAYON_DONATE . '">small donation</a> to show your appreciation. Thanks! <a class="crayon-help-close">X</a></p></div>
';
}
public static function help_screen() {
$screen = get_current_screen();
if ($screen->id != self::$admin_page) {
return;
}
}
public static function metrics() {
echo '<div id="crayon-section-metrics" class="crayon-hide-inline">';
self::checkbox(array(CrayonSettings::HEIGHT_SET, '<span class="crayon-span-50">' . crayon__('Height') . ' </span>'), FALSE);
self::dropdown(CrayonSettings::HEIGHT_MODE, FALSE);
echo ' ';
self::input(array('id' => CrayonSettings::HEIGHT, 'size' => 8));
echo ' ';
self::dropdown(CrayonSettings::HEIGHT_UNIT);
self::checkbox(array(CrayonSettings::WIDTH_SET, '<span class="crayon-span-50">' . crayon__('Width') . ' </span>'), FALSE);
self::dropdown(CrayonSettings::WIDTH_MODE, FALSE);
echo ' ';
self::input(array('id' => CrayonSettings::WIDTH, 'size' => 8));
echo ' ';
self::dropdown(CrayonSettings::WIDTH_UNIT);
$text = array(crayon__('Top Margin') => array(CrayonSettings::TOP_SET, CrayonSettings::TOP_MARGIN),
crayon__('Bottom Margin') => array(CrayonSettings::BOTTOM_SET, CrayonSettings::BOTTOM_MARGIN),
crayon__('Left Margin') => array(CrayonSettings::LEFT_SET, CrayonSettings::LEFT_MARGIN),
crayon__('Right Margin') => array(CrayonSettings::RIGHT_SET, CrayonSettings::RIGHT_MARGIN));
foreach ($text as $p => $s) {
$set = $s[0];
$margin = $s[1];
$preview = ($p == crayon__('Left Margin') || $p == crayon__('Right Margin'));
self::checkbox(array($set, '<span class="crayon-span-110">' . $p . '</span>'), FALSE, $preview);
echo ' ';
self::input(array('id' => $margin, 'size' => 8, 'preview' => FALSE));
echo '<span class="crayon-span-margin">', crayon__('Pixels'), '</span>', CRAYON_BR;
}
echo '<span class="crayon-span" style="min-width: 135px;">' . crayon__('Horizontal Alignment') . ' </span>';
self::dropdown(CrayonSettings::H_ALIGN);
echo '<div id="crayon-subsection-float">';
self::checkbox(array(CrayonSettings::FLOAT_ENABLE, crayon__('Allow floating elements to surround Crayon')), FALSE, FALSE);
echo '</div>';
echo '<span class="crayon-span-100">' . crayon__('Inline Margin') . ' </span>';
self::input(array('id' => CrayonSettings::INLINE_MARGIN, 'size' => 2));
echo '<span class="crayon-span-margin">', crayon__('Pixels'), '</span>';
echo '</div>';
}
public static function toolbar() {
echo '<div id="crayon-section-toolbar" class="crayon-hide-inline">';
self::span(crayon__('Display the Toolbar') . ' ');
self::dropdown(CrayonSettings::TOOLBAR);
echo '<div id="crayon-subsection-toolbar">';
self::checkbox(array(CrayonSettings::TOOLBAR_OVERLAY, crayon__('Overlay the toolbar on code rather than push it down when possible')));
self::checkbox(array(CrayonSettings::TOOLBAR_HIDE, crayon__('Toggle the toolbar on single click when it is overlayed')));
self::checkbox(array(CrayonSettings::TOOLBAR_DELAY, crayon__('Delay hiding the toolbar on MouseOut')));
echo '</div>';
self::checkbox(array(CrayonSettings::SHOW_TITLE, crayon__('Display the title when provided')));
self::span(crayon__('Display the language') . ' ');
self::dropdown(CrayonSettings::SHOW_LANG);
echo '</div>';
}
public static function lines() {
echo '<div id="crayon-section-lines" class="crayon-hide-inline">';
self::checkbox(array(CrayonSettings::STRIPED, crayon__('Display striped code lines')));
self::checkbox(array(CrayonSettings::MARKING, crayon__('Enable line marking for important lines')));
self::checkbox(array(CrayonSettings::RANGES, crayon__('Enable line ranges for showing only parts of code')));
self::checkbox(array(CrayonSettings::NUMS, crayon__('Display line numbers by default')));
self::checkbox(array(CrayonSettings::NUMS_TOGGLE, crayon__('Enable line number toggling')));
self::checkbox(array(CrayonSettings::WRAP, crayon__('Wrap lines by default')));
self::checkbox(array(CrayonSettings::WRAP_TOGGLE, crayon__('Enable line wrap toggling')));
self::span(crayon__('Start line numbers from') . ' ');
self::input(array('id' => CrayonSettings::START_LINE, 'size' => 2, 'break' => TRUE));
echo '</div>';
}
public static function langs() {
echo '<a name="langs"></a>';
// Specialised dropdown for languages
if (array_key_exists(CrayonSettings::FALLBACK_LANG, self::$options)) {
if (($langs = CrayonParser::parse_all()) != FALSE) {
$langs = CrayonLangs::sort_by_name($langs);
self::span(crayon__('When no language is provided, use the fallback') . ': ');
self::dropdown(CrayonSettings::FALLBACK_LANG, FALSE, TRUE, TRUE, $langs);
// Information about parsing
$parsed = CrayonResources::langs()->is_parsed();
$count = count($langs);
echo '</select>', CRAYON_BR, ($parsed ? '' : '<span class="crayon-error">'),
sprintf(crayon_n('%d language has been detected.', '%d languages have been detected.', $count), $count), ' ',
$parsed ? crayon__('Parsing was successful') : crayon__('Parsing was unsuccessful'),
($parsed ? '. ' : '</span>');
// Check if fallback from db is loaded
$db_fallback = self::$options[CrayonSettings::FALLBACK_LANG]; // Fallback name from db
if (!CrayonResources::langs()->is_loaded($db_fallback) || !CrayonResources::langs()->exists($db_fallback)) {
echo '<br/><span class="crayon-error">', sprintf(crayon__('The selected language with id %s could not be loaded'), '<strong>' . $db_fallback . '</strong>'), '. </span>';
}
// Language parsing info
echo CRAYON_BR, '<div id="crayon-subsection-langs-info"><div>' . self::button(array('id' => 'show-langs', 'title' => crayon__('Show Languages'))) . '</div></div>';
} else {
echo crayon__('No languages could be parsed.');
}
}
}
public static function show_langs() {
CrayonSettingsWP::load_settings();
require_once(CRAYON_PARSER_PHP);
if (($langs = CrayonParser::parse_all()) != FALSE) {
$langs = CrayonLangs::sort_by_name($langs);
echo '<table class="crayon-table" cellspacing="0" cellpadding="0"><tr class="crayon-table-header">',
'<td>', crayon__('ID'), '</td><td>', crayon__('Name'), '</td><td>', crayon__('Version'), '</td><td>', crayon__('File Extensions'), '</td><td>', crayon__('Aliases'), '</td><td>', crayon__('State'), '</td></tr>';
$keys = array_values($langs);
for ($i = 0; $i < count($langs); $i++) {
$lang = $keys[$i];
$tr = ($i == count($langs) - 1) ? 'crayon-table-last' : '';
echo '<tr class="', $tr, '">',
'<td>', $lang->id(), '</td>',
'<td>', $lang->name(), '</td>',
'<td>', $lang->version(), '</td>',
'<td>', implode(', ', $lang->ext()), '</td>',
'<td>', implode(', ', $lang->alias()), '</td>',
'<td class="', strtolower(CrayonUtil::space_to_hyphen($lang->state_info())), '">',
$lang->state_info(), '</td>',
'</tr>';
}
echo '</table><br/>' . crayon__("Languages that have the same extension as their name don't need to explicitly map extensions.");
} else {
echo crayon__('No languages could be found.');
}
exit();
}
public static function posts() {
echo '<a name="posts"></a>';
echo self::button(array('id' => 'show-posts', 'title' => crayon__('Show Crayon Posts')));
echo ' <input type="submit" name="', self::OPTIONS, '[refresh_tags]" id="refresh_tags" class="button-primary" value="', crayon__('Refresh'), '" />';
echo self::help_button('http://aramk.com/blog/2012/09/26/internal-post-management-crayon/');
echo '<div id="crayon-subsection-posts-info"></div>';
}
public static function post_cmp($a, $b) {
$a = $a->post_modified;
$b = $b->post_modified;
if ($a == $b) {
return 0;
} else {
return $a < $b ? 1 : -1;
}
}
public static function show_posts() {
CrayonSettingsWP::load_settings();
$postIDs = self::load_posts();
$legacy_posts = self::load_legacy_posts();
// Avoids O(n^2) by using a hash map, tradeoff in using strval
$legacy_map = array();
foreach ($legacy_posts as $legacyID) {
$legacy_map[strval($legacyID)] = TRUE;
}
echo '<table class="crayon-table" cellspacing="0" cellpadding="0"><tr class="crayon-table-header">',
'<td>', crayon__('ID'), '</td><td>', crayon__('Title'), '</td><td>', crayon__('Posted'), '</td><td>', crayon__('Modifed'), '</td><td>', crayon__('Contains Legacy Tags?'), '</td></tr>';
$posts = array();
for ($i = 0; $i < count($postIDs); $i++) {
$posts[$i] = get_post($postIDs[$i]);
}
usort($posts, 'CrayonSettingsWP::post_cmp');
for ($i = 0; $i < count($posts); $i++) {
$post = $posts[$i];
$postID = $post->ID;
$title = $post->post_title;
$title = !empty($title) ? $title : 'N/A';
$tr = ($i == count($posts) - 1) ? 'crayon-table-last' : '';
echo '<tr class="', $tr, '">',
'<td>', $postID, '</td>',
'<td><a href="', $post->guid, '" target="_blank">', $title, '</a></td>',
'<td>', $post->post_date, '</td>',
'<td>', $post->post_modified, '</td>',
'<td>', isset($legacy_map[strval($postID)]) ? '<span style="color: red;">' . crayon__('Yes') . '</a>' : crayon__('No'), '</td>',
'</tr>';
}
echo '</table>';
exit();
}
public static function show_preview() {
echo '<div id="content">';
self::load_settings(); // Run first to ensure global settings loaded
$crayon = CrayonWP::instance();
// Settings to prevent from validating
$preview_settings = array(self::SAMPLE_CODE);
// Load settings from GET and validate
foreach ($_POST as $key => $value) {
// echo $key, ' ', $value , '<br/>';
$value = stripslashes($value);
if (!in_array($key, $preview_settings)) {
$_POST[$key] = CrayonSettings::validate($key, $value);
} else {
$_POST[$key] = $value;
}
}
$crayon->settings($_POST);
if (!isset($crayon_preview_dont_override_get) || !$crayon_preview_dont_override_get) {
$settings = array(CrayonSettings::TOP_SET => TRUE, CrayonSettings::TOP_MARGIN => 10,
CrayonSettings::BOTTOM_SET => FALSE, CrayonSettings::BOTTOM_MARGIN => 0);
$crayon->settings($settings);
}
// Print the theme CSS
$theme_id = $crayon->setting_val(CrayonSettings::THEME);
if ($theme_id != NULL) {
echo CrayonResources::themes()->get_css($theme_id, date('U'));
}
$font_id = $crayon->setting_val(CrayonSettings::FONT);
if ($font_id != NULL /*&& $font_id != CrayonFonts::DEFAULT_FONT*/) {
echo CrayonResources::fonts()->get_css($font_id);
}
// Load custom code based on language
$lang = $crayon->setting_val(CrayonSettings::FALLBACK_LANG);
$path = CrayonGlobalSettings::plugin_path() . CRAYON_UTIL_DIR . '/sample/' . $lang . '.txt';
if (isset($_POST[self::SAMPLE_CODE])) {
$crayon->code($_POST[self::SAMPLE_CODE]);
} else if ($lang && @file_exists($path)) {
$crayon->url($path);
} else {
$code = "
// A sample class
class Human {
private int age = 0;
public void birthday() {
age++;
print('Happy Birthday!');
}
}
";
$crayon->code($code);
}
$crayon->title('Sample Code');
$crayon->marked('5-7');
$crayon->output($highlight = true, $nums = true, $print = true);
echo '</div>';
crayon_load_plugin_textdomain();
exit();
}
public static function theme($editor = FALSE) {
$db_theme = self::$options[CrayonSettings::THEME]; // Theme name from db
if (!array_key_exists(CrayonSettings::THEME, self::$options)) {
$db_theme = '';
}
$themes_array = CrayonResources::themes()->get_array();
// Mark user themes
foreach ($themes_array as $id => $name) {
$mark = CrayonResources::themes()->get($id)->user() ? ' *' : '';
$themes_array[$id] = array($name, $name . $mark);
}
$missing_theme = !CrayonResources::themes()->is_loaded($db_theme) || !CrayonResources::themes()->exists($db_theme);
self::dropdown(CrayonSettings::THEME, FALSE, FALSE, TRUE, $themes_array, $missing_theme ? CrayonThemes::DEFAULT_THEME : NULL);
if ($editor) {
return;
}
// Theme editor
if (CRAYON_THEME_EDITOR) {
// echo '<a id="crayon-theme-editor-button" class="button-primary crayon-admin-button" loading="'. crayon__('Loading...') .'" loaded="'. crayon__('Theme Editor') .'" >'. crayon__('Theme Editor') .'</a></br>';
echo '<div id="crayon-theme-editor-admin-buttons">';
$buttons = array('edit' => crayon__('Edit'), 'duplicate' => crayon__('Duplicate'), 'submit' => crayon__('Submit'),
'delete' => crayon__('Delete'));
foreach ($buttons as $k => $v) {
echo '<a id="crayon-theme-editor-', $k, '-button" class="button-secondary crayon-admin-button" loading="', crayon__('Loading...'), '" loaded="', $v, '" >', $v, '</a>';
}
echo '<span class="crayon-span-5"></span>', self::help_button('http://aramk.com/blog/2012/12/27/crayon-theme-editor/'), '<span class="crayon-span-5"></span>', crayon__("Duplicate a Stock Theme into a User Theme to allow editing.");
echo '</br></div>';
}