-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcs-code-block.php
More file actions
1564 lines (1394 loc) · 63.1 KB
/
cs-code-block.php
File metadata and controls
1564 lines (1394 loc) · 63.1 KB
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
/**
* Plugin Name: CloudScale Code Block
* Plugin URI: https://andrewbaker.ninja
* Description: Syntax highlighted code block with auto language detection, clipboard copy, dark/light mode toggle, code block migrator, and read only SQL query tool. Works as a Gutenberg block and as a [cs_code] shortcode.
* Version: 1.7.8
* Author: Andrew Baker
* Author URI: https://andrewbaker.ninja
* License: GPL v2 or later
* Text Domain: cs-code-block
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class CloudScale_Code_Block {
const VERSION = '1.7.8';
const HLJS_VERSION = '11.11.1';
const HLJS_CDN = 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/';
const TOOLS_SLUG = 'cloudscale-code-sql';
/**
* Available theme pairs. Each entry maps a slug to its dark and light CDN filenames,
* display label, and background colours for the wrapper/toolbar.
*/
public static function get_theme_registry(): array {
return [
'atom-one' => [
'label' => 'Atom One',
'dark_css' => 'atom-one-dark',
'light_css' => 'atom-one-light',
'dark_bg' => '#282c34',
'dark_toolbar' => '#21252b',
'light_bg' => '#fafafa',
'light_toolbar'=> '#e8eaed',
],
'github' => [
'label' => 'GitHub',
'dark_css' => 'github-dark',
'light_css' => 'github',
'dark_bg' => '#24292e',
'dark_toolbar' => '#1f2428',
'light_bg' => '#fff',
'light_toolbar'=> '#f6f8fa',
],
'monokai' => [
'label' => 'Monokai',
'dark_css' => 'monokai',
'light_css' => 'atom-one-light',
'dark_bg' => '#272822',
'dark_toolbar' => '#1e1f1c',
'light_bg' => '#fafafa',
'light_toolbar'=> '#e8eaed',
],
'nord' => [
'label' => 'Nord',
'dark_css' => 'nord',
'light_css' => 'atom-one-light',
'dark_bg' => '#2e3440',
'dark_toolbar' => '#272c36',
'light_bg' => '#fafafa',
'light_toolbar'=> '#e8eaed',
],
'dracula' => [
'label' => 'Dracula',
'dark_css' => 'dracula',
'light_css' => 'atom-one-light',
'dark_bg' => '#282a36',
'dark_toolbar' => '#21222c',
'light_bg' => '#fafafa',
'light_toolbar'=> '#e8eaed',
],
'tokyo-night' => [
'label' => 'Tokyo Night',
'dark_css' => 'tokyo-night-dark',
'light_css' => 'tokyo-night-light',
'dark_bg' => '#1a1b26',
'dark_toolbar' => '#16161e',
'light_bg' => '#d5d6db',
'light_toolbar'=> '#c8c9ce',
],
'vs2015' => [
'label' => 'VS 2015 / VS Code',
'dark_css' => 'vs2015',
'light_css' => 'vs',
'dark_bg' => '#1e1e1e',
'dark_toolbar' => '#181818',
'light_bg' => '#fff',
'light_toolbar'=> '#f3f3f3',
],
'stackoverflow' => [
'label' => 'Stack Overflow',
'dark_css' => 'stackoverflow-dark',
'light_css' => 'stackoverflow-light',
'dark_bg' => '#1c1b1b',
'dark_toolbar' => '#151414',
'light_bg' => '#f6f6f6',
'light_toolbar'=> '#e8e8e8',
],
'night-owl' => [
'label' => 'Night Owl',
'dark_css' => 'night-owl',
'light_css' => 'atom-one-light',
'dark_bg' => '#011627',
'dark_toolbar' => '#001122',
'light_bg' => '#fafafa',
'light_toolbar'=> '#e8eaed',
],
'gruvbox' => [
'label' => 'Gruvbox',
'dark_css' => 'base16/gruvbox-dark-hard',
'light_css' => 'base16/gruvbox-light-hard',
'dark_bg' => '#1d2021',
'dark_toolbar' => '#171819',
'light_bg' => '#f9f5d7',
'light_toolbar'=> '#ece8c8',
],
'solarized' => [
'label' => 'Solarized',
'dark_css' => 'base16/solarized-dark',
'light_css' => 'base16/solarized-light',
'dark_bg' => '#002b36',
'dark_toolbar' => '#002530',
'light_bg' => '#fdf6e3',
'light_toolbar'=> '#eee8d5',
],
'panda' => [
'label' => 'Panda',
'dark_css' => 'panda-syntax-dark',
'light_css' => 'panda-syntax-light',
'dark_bg' => '#292a2b',
'dark_toolbar' => '#222324',
'light_bg' => '#e6e6e6',
'light_toolbar'=> '#d9d9d9',
],
'tomorrow' => [
'label' => 'Tomorrow Night',
'dark_css' => 'tomorrow-night-bright',
'light_css' => 'atom-one-light',
'dark_bg' => '#000',
'dark_toolbar' => '#0a0a0a',
'light_bg' => '#fafafa',
'light_toolbar'=> '#e8eaed',
],
'shades-of-purple' => [
'label' => 'Shades of Purple',
'dark_css' => 'shades-of-purple',
'light_css' => 'atom-one-light',
'dark_bg' => '#2d2b55',
'dark_toolbar' => '#252347',
'light_bg' => '#fafafa',
'light_toolbar'=> '#e8eaed',
],
];
}
private static $instance_count = 0;
private static $assets_enqueued = false;
public static function init() {
add_action( 'init', [ __CLASS__, 'register_block' ] );
add_action( 'init', [ __CLASS__, 'register_shortcode' ] );
add_action( 'enqueue_block_editor_assets', [ __CLASS__, 'enqueue_convert_script' ] );
add_action( 'admin_menu', [ __CLASS__, 'add_tools_page' ] );
add_action( 'admin_init', [ __CLASS__, 'register_settings' ] );
add_action( 'admin_enqueue_scripts', [ __CLASS__, 'enqueue_admin_assets' ] );
// Migration AJAX
add_action( 'wp_ajax_cs_migrate_scan', [ __CLASS__, 'ajax_scan' ] );
add_action( 'wp_ajax_cs_migrate_preview', [ __CLASS__, 'ajax_preview' ] );
add_action( 'wp_ajax_cs_migrate_single', [ __CLASS__, 'ajax_migrate_single' ] );
add_action( 'wp_ajax_cs_migrate_all', [ __CLASS__, 'ajax_migrate_all' ] );
// SQL AJAX
add_action( 'wp_ajax_cs_sql_run', [ __CLASS__, 'ajax_sql_run' ] );
}
/* ==================================================================
1. BLOCK REGISTRATION
================================================================== */
public static function register_block() {
$cdn = self::HLJS_CDN . self::HLJS_VERSION;
wp_register_script(
'hljs-core',
$cdn . '/highlight.min.js',
[],
self::HLJS_VERSION,
true
);
// Register both theme stylesheets from the selected pair
$pair_slug = get_option( 'cs_code_theme_pair', 'atom-one' );
$registry = self::get_theme_registry();
$pair = isset( $registry[ $pair_slug ] ) ? $registry[ $pair_slug ] : $registry['atom-one'];
wp_register_style(
'hljs-theme-dark',
$cdn . '/styles/' . $pair['dark_css'] . '.min.css',
[],
self::HLJS_VERSION
);
wp_register_style(
'hljs-theme-light',
$cdn . '/styles/' . $pair['light_css'] . '.min.css',
[],
self::HLJS_VERSION
);
wp_register_style(
'cs-code-block-frontend',
plugins_url( 'assets/cs-code-block.css', __FILE__ ),
[ 'hljs-theme-dark', 'hljs-theme-light' ],
filemtime( plugin_dir_path( __FILE__ ) . 'assets/cs-code-block.css' )
);
wp_register_script(
'cs-code-block-frontend',
plugins_url( 'assets/cs-code-block.js', __FILE__ ),
[ 'hljs-core' ],
filemtime( plugin_dir_path( __FILE__ ) . 'assets/cs-code-block.js' ),
true
);
wp_register_style(
'cs-code-block-editor',
plugins_url( 'assets/cs-code-block-editor.css', __FILE__ ),
[],
self::VERSION
);
wp_register_script(
'cloudscale-code-block-editor-script',
plugins_url( 'blocks/code/editor.js', __FILE__ ),
[ 'wp-blocks', 'wp-element', 'wp-block-editor', 'wp-components', 'wp-i18n', 'wp-data', 'wp-hooks' ],
self::VERSION,
true
);
register_block_type(
__DIR__ . '/blocks/code',
[
'render_callback' => [ __CLASS__, 'render_block' ],
'editor_script' => 'cloudscale-code-block-editor-script',
]
);
}
/* ==================================================================
1b. CONVERT SCRIPT
================================================================== */
public static function enqueue_convert_script() {
wp_enqueue_script(
'cs-code-block-convert',
plugins_url( 'assets/cs-convert.js', __FILE__ ),
[ 'wp-blocks', 'wp-data' ],
self::VERSION,
true
);
}
/* ==================================================================
2. RENDER (shared by block + shortcode)
================================================================== */
public static function render_block( $attributes, $block_content = '' ) {
self::maybe_enqueue_frontend();
self::$instance_count++;
$id = 'cs-code-' . self::$instance_count;
$code = isset( $attributes['content'] ) ? $attributes['content'] : '';
$lang = isset( $attributes['language'] ) ? $attributes['language'] : '';
$title = isset( $attributes['title'] ) ? $attributes['title'] : '';
$theme = isset( $attributes['theme'] ) ? $attributes['theme'] : '';
return self::build_html( $id, $code, $lang, $title, $theme );
}
private static function build_html( $id, $code, $lang, $title, $theme ) {
$lang_class = $lang ? 'language-' . esc_attr( $lang ) : '';
$theme_attr = $theme ? ' data-theme="' . esc_attr( $theme ) . '"' : '';
$cloudscale_link = '<a class="cs-code-brand" href="https://andrewbaker.ninja/2026/02/27/building-a-better-code-block-for-wordpress-cloudscale-code-block-plugin/" target="_blank" rel="noopener noreferrer"><span class="cs-brand-bolt">⚡</span> Powered by CloudScale</a>';
$title_html = '';
if ( $title ) {
$title_html = '<div class="cs-code-title">' . esc_html( $title ) . '</div>';
}
ob_start();
?>
<div class="cs-code-wrapper" id="<?php echo esc_attr( $id ); ?>"<?php echo $theme_attr; ?>>
<div class="cs-code-toolbar">
<?php echo $cloudscale_link; ?>
<?php echo $title_html; ?>
<div class="cs-code-actions">
<span class="cs-code-lang-badge"></span>
<button class="cs-code-lines-toggle" title="Toggle line numbers" aria-label="Toggle line numbers">
<svg class="cs-icon-lines" xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="10" y1="6" x2="21" y2="6"/><line x1="10" y1="12" x2="21" y2="12"/><line x1="10" y1="18" x2="21" y2="18"/><text x="4" y="7" font-size="7" fill="currentColor" stroke="none" font-family="monospace">1</text><text x="4" y="13" font-size="7" fill="currentColor" stroke="none" font-family="monospace">2</text><text x="4" y="19" font-size="7" fill="currentColor" stroke="none" font-family="monospace">3</text></svg>
</button>
<button class="cs-code-theme-toggle" title="Toggle light/dark mode" aria-label="Toggle theme">
<svg class="cs-icon-sun" xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="5"/><line x1="12" y1="1" x2="12" y2="3"/><line x1="12" y1="21" x2="12" y2="23"/><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"/><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"/><line x1="1" y1="12" x2="3" y2="12"/><line x1="21" y1="12" x2="23" y2="12"/><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"/><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"/></svg>
<svg class="cs-icon-moon" xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/></svg>
</button>
<button class="cs-code-copy" title="Copy to clipboard" aria-label="Copy code">
<svg class="cs-icon-copy" xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg>
<svg class="cs-icon-check" xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"/></svg>
</button>
</div>
</div>
<div class="cs-code-body">
<pre><code class="<?php echo $lang_class; ?>"><?php echo esc_html( $code ); ?></code></pre>
</div>
</div>
<?php
return ob_get_clean();
}
private static function maybe_enqueue_frontend() {
if ( self::$assets_enqueued ) {
return;
}
self::$assets_enqueued = true;
wp_enqueue_style( 'hljs-theme-dark' );
wp_enqueue_style( 'hljs-theme-light' );
wp_enqueue_style( 'cs-code-block-frontend' );
wp_enqueue_script( 'hljs-core' );
wp_enqueue_script( 'cs-code-block-frontend' );
$default_theme = get_option( 'cs_code_default_theme', 'dark' );
$pair_slug = get_option( 'cs_code_theme_pair', 'atom-one' );
$registry = self::get_theme_registry();
$pair = isset( $registry[ $pair_slug ] ) ? $registry[ $pair_slug ] : $registry['atom-one'];
wp_localize_script( 'cs-code-block-frontend', 'csCodeConfig', [
'defaultTheme' => $default_theme,
'themePair' => $pair_slug,
'darkBg' => $pair['dark_bg'],
'darkToolbar' => $pair['dark_toolbar'],
'lightBg' => $pair['light_bg'],
'lightToolbar' => $pair['light_toolbar'],
] );
}
/* ==================================================================
3. SHORTCODE [cs_code]
================================================================== */
public static function register_shortcode() {
add_shortcode( 'cs_code', [ __CLASS__, 'render_shortcode' ] );
}
public static function render_shortcode( $atts, $content = null ) {
$atts = shortcode_atts( [
'lang' => '',
'theme' => '',
'title' => '',
], $atts, 'cs_code' );
$code = self::decode_shortcode_content( $content );
return self::render_block( [
'content' => $code,
'language' => $atts['lang'],
'title' => $atts['title'],
'theme' => $atts['theme'],
] );
}
private static function decode_shortcode_content( $content ) {
$content = preg_replace( '#^<p>|</p>$#i', '', trim( $content ) );
$content = str_replace(
[ '<br />', '<br/>', '<br>', '“', '”', '‘', '’', ' ', '&' ],
[ "\n", "\n", "\n", '"', '"', "'", "'", ' ', '&' ],
$content
);
$content = html_entity_decode( $content, ENT_QUOTES, 'UTF-8' );
return trim( $content );
}
/* ==================================================================
4. SETTINGS
================================================================== */
public static function register_settings() {
register_setting( 'cs_code_settings', 'cs_code_default_theme', [
'type' => 'string',
'sanitize_callback' => function ( $val ) {
return in_array( $val, [ 'dark', 'light' ] ) ? $val : 'dark';
},
'default' => 'dark',
] );
$valid_themes = array_keys( self::get_theme_registry() );
register_setting( 'cs_code_settings', 'cs_code_theme_pair', [
'type' => 'string',
'sanitize_callback' => function ( $val ) use ( $valid_themes ) {
return in_array( $val, $valid_themes, true ) ? $val : 'atom-one';
},
'default' => 'atom-one',
] );
}
/* ==================================================================
5. COMBINED TOOLS PAGE (Code Block Migrator + SQL Command)
================================================================== */
public static function add_tools_page() {
add_management_page(
'CloudScale Code and SQL',
'CloudScale Code and SQL',
'manage_options',
self::TOOLS_SLUG,
[ __CLASS__, 'render_tools_page' ]
);
}
public static function enqueue_admin_assets( $hook ) {
if ( $hook !== 'tools_page_' . self::TOOLS_SLUG ) {
return;
}
// Tabs CSS
wp_enqueue_style(
'cs-admin-tabs',
plugins_url( 'assets/cs-admin-tabs.css', __FILE__ ),
[],
self::VERSION
);
// Migrate CSS + JS
wp_enqueue_style(
'cs-code-migrate',
plugins_url( 'assets/cs-code-migrate.css', __FILE__ ),
[],
self::VERSION
);
wp_enqueue_script(
'cs-code-migrate',
plugins_url( 'assets/cs-code-migrate.js', __FILE__ ),
[],
self::VERSION,
true
);
wp_localize_script( 'cs-code-migrate', 'csMigrate', [
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( self::MIGRATE_NONCE ),
] );
}
public static function render_tools_page() {
$active_tab = isset( $_GET['tab'] ) ? sanitize_key( $_GET['tab'] ) : 'migrate';
$base_url = admin_url( 'tools.php?page=' . self::TOOLS_SLUG );
?>
<style>
/* ============================================================
CloudScale Code and SQL — Admin UI v1.7.2
Colour palette mirrors CloudScale Page Views.
============================================================ */
#cs-app * { box-sizing: border-box; }
#cs-app {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
font-size: 13px;
color: #1a2332;
max-width: 1200px;
margin-top: 10px;
}
#cs-banner {
background: linear-gradient(135deg, #1a3a8f 0%, #1e6fd9 60%, #0fb8e0 100%);
border-radius: 8px 8px 0 0;
padding: 20px 28px;
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
gap: 12px;
}
#cs-banner-title {
font-size: 22px;
font-weight: 700;
color: #fff;
letter-spacing: -.3px;
}
#cs-banner-sub {
font-size: 12px;
color: rgba(255,255,255,.75);
margin-top: 3px;
}
#cs-banner-right { display: flex; gap: 10px; align-items: center; }
.cs-badge {
display: inline-block;
padding: 5px 14px;
border-radius: 20px;
font-size: 12px;
font-weight: 600;
white-space: nowrap;
}
.cs-badge-green { background: #1db954; color: #fff; }
.cs-badge-orange { background: #f47c20; color: #fff; }
/* Tab bar */
#cs-tab-bar {
background: #1a2d6b;
display: flex;
gap: 0;
padding: 0 20px;
}
#cs-app .cs-tab {
background: transparent;
border: none;
color: rgba(255,255,255,.6);
font-size: 13px;
font-weight: 600;
padding: 12px 20px;
cursor: pointer;
border-bottom: 3px solid transparent;
transition: all .15s;
text-decoration: none;
display: inline-flex;
align-items: center;
gap: 6px;
}
#cs-app .cs-tab:hover { color: #fff; }
#cs-app .cs-tab:focus { outline: none; box-shadow: none; }
#cs-app .cs-tab.active { color: #fff; border-bottom-color: #f47c20; }
/* Tab content */
.cs-tab-content { display: none; padding: 20px 0 0; }
.cs-tab-content.active { display: block; }
/* Section headers */
#cs-app .cs-section-header {
background: linear-gradient(135deg, #1a3a8f, #1e6fd9);
color: #fff;
font-size: 12px;
font-weight: 700;
text-transform: uppercase;
letter-spacing: .06em;
padding: 10px 16px;
display: flex;
justify-content: space-between;
align-items: center;
}
#cs-app .cs-section-header-green { background: linear-gradient(135deg, #1a7a3a, #1db954); }
#cs-app .cs-section-header-orange { background: linear-gradient(135deg, #c45c00, #f47c20); }
#cs-app .cs-section-header-red { background: linear-gradient(135deg, #8b1a1a, #e53e3e); }
#cs-app .cs-section-header-purple { background: linear-gradient(135deg, #5b21b6, #7c3aed); }
#cs-app .cs-section-header-teal { background: linear-gradient(135deg, #0d6a5c, #0fb8e0); }
#cs-app .cs-section-header code {
background: rgba(255,255,255,.2);
color: #fff;
padding: 1px 6px;
border-radius: 3px;
font-size: 11px;
}
.cs-header-hint {
font-size: 11px;
font-weight: 400;
opacity: .85;
}
/* Panels */
#cs-app .cs-panel {
background: #fff;
border: 1.5px solid #dce3ef;
border-radius: 8px;
overflow: hidden;
margin-bottom: 18px;
}
#cs-app .cs-panel-body {
padding: 20px 24px;
}
/* Fields */
#cs-app .cs-field-row {
display: flex;
gap: 24px;
flex-wrap: wrap;
align-items: flex-start;
}
#cs-app .cs-field { flex: 1; min-width: 200px; }
#cs-app .cs-label {
display: block;
font-size: 13px;
font-weight: 600;
color: #1a2332;
margin-bottom: 6px;
}
#cs-app .cs-input,
#cs-app .cs-field select {
width: 100%;
max-width: 400px;
padding: 5px 8px;
border: 1.5px solid #dce3ef;
border-radius: 4px;
font-size: 13px;
background: #fff;
color: #1a2332;
}
#cs-app .cs-hint {
display: block;
margin-top: 4px;
font-size: 11px;
color: #1db954;
font-style: italic;
padding-left: 8px;
border-left: 3px solid #1db954;
}
/* Buttons */
#cs-app .cs-btn-primary {
background: linear-gradient(135deg, #1a3a8f, #1e6fd9);
border: none;
border-radius: 5px;
color: #fff;
font-size: 12px;
font-weight: 700;
padding: 6px 16px;
cursor: pointer;
transition: opacity .15s;
}
#cs-app .cs-btn-primary:hover { opacity: .88; }
#cs-app .cs-btn-orange {
background: linear-gradient(135deg, #c45c00, #f47c20);
border: none;
border-radius: 5px;
color: #fff;
font-size: 12px;
font-weight: 700;
padding: 6px 16px;
cursor: pointer;
transition: opacity .15s;
}
#cs-app .cs-btn-orange:hover { opacity: .88; }
#cs-app .cs-btn-pink {
background: linear-gradient(135deg, #b5348a, #d946a6);
border: none;
border-radius: 5px;
color: #fff;
font-size: 12px;
font-weight: 700;
padding: 6px 16px;
cursor: pointer;
transition: opacity .15s;
}
#cs-app .cs-btn-pink:hover { opacity: .88; }
/* Settings saved */
#cs-app .cs-settings-saved {
color: #1db954;
font-weight: 700;
font-size: 12px;
opacity: 0;
transition: opacity 0.3s;
}
#cs-app .cs-settings-saved.visible { opacity: 1; }
/* SQL textarea */
#cs-app .cs-sql-textarea {
width: 100%;
min-height: 80px;
font-family: 'SF Mono', 'Fira Code', 'Courier New', monospace;
font-size: 13px;
line-height: 1.5;
padding: 12px;
border: 1.5px solid #1a2d6b;
border-radius: 6px;
background: #0d1b2a;
color: #e0e0e0;
resize: vertical;
}
#cs-app .cs-sql-textarea:focus {
outline: none;
border-color: #1e6fd9;
box-shadow: 0 0 0 2px rgba(30,111,217,.25);
}
/* SQL table */
#cs-app .cs-sql-table {
width: 100%;
border-collapse: collapse;
font-family: 'SF Mono', 'Fira Code', monospace;
font-size: 12px;
}
#cs-app .cs-sql-table th {
text-align: left;
padding: 8px 10px;
border-bottom: 2px solid #1a3a8f;
background: #f0f4ff;
font-size: 11px;
text-transform: uppercase;
letter-spacing: .04em;
color: #1a3a8f;
white-space: nowrap;
position: sticky;
top: 0;
}
#cs-app .cs-sql-table td {
padding: 6px 10px;
border-bottom: 1px solid #f0f4ff;
max-width: 500px;
overflow: hidden;
text-overflow: ellipsis;
word-break: break-all;
}
#cs-app .cs-sql-table tr:nth-child(even) { background: #f8faff; }
#cs-app .cs-sql-table tr:hover td { background: #edf2ff; }
/* Quick query buttons */
#cs-app .cs-quick-group-label {
font-size: 12px;
font-weight: 700;
text-transform: uppercase;
letter-spacing: .04em;
color: #1a3a8f;
margin: 16px 0 8px;
padding: 0;
}
#cs-app .cs-quick-group-label:first-child { margin-top: 0; }
#cs-app .cs-quick-grid {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-bottom: 4px;
}
#cs-app .cs-quick-btn {
background: #f0f4ff;
border: 1.5px solid #c5d2f0;
border-radius: 5px;
padding: 5px 14px;
font-size: 12px;
font-weight: 600;
color: #1a3a8f;
cursor: pointer;
transition: all .15s;
white-space: nowrap;
}
#cs-app .cs-quick-btn:hover {
background: #dce6ff;
border-color: #1a3a8f;
}
/* Migrate toolbar */
#cs-app .cs-migrate-toolbar {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 16px;
}
#cs-app .cs-migrate-toolbar .dashicons {
font-size: 16px;
width: 16px;
height: 16px;
line-height: 16px;
margin-top: 2px;
}
#cs-app .cs-migrate-hint {
color: #aaa;
font-style: italic;
text-align: center;
padding: 30px 0;
font-size: 13px;
}
#cs-app .cs-status { font-size: 12px; color: #888; }
</style>
<div class="wrap">
<div id="cs-app">
<!-- Banner -->
<div id="cs-banner">
<div>
<div id="cs-banner-title">⚡ CloudScale Code and SQL</div>
<div id="cs-banner-sub">Syntax highlighting, code migration, and database query tool · v<?php echo esc_html( self::VERSION ); ?></div>
</div>
<div id="cs-banner-right">
<span class="cs-badge cs-badge-green">✅ Totally Free</span>
<span class="cs-badge cs-badge-orange">andrewbaker.ninja</span>
</div>
</div>
<!-- Tab bar -->
<div id="cs-tab-bar">
<a href="<?php echo esc_url( $base_url . '&tab=migrate' ); ?>"
class="cs-tab <?php echo $active_tab === 'migrate' ? 'active' : ''; ?>">
🔄 Code Migrator
</a>
<a href="<?php echo esc_url( $base_url . '&tab=sql' ); ?>"
class="cs-tab <?php echo $active_tab === 'sql' ? 'active' : ''; ?>">
🗄️ SQL Command
</a>
</div>
<?php if ( $active_tab === 'migrate' ) : ?>
<div class="cs-tab-content active">
<?php self::render_settings_panel(); ?>
<?php self::render_migrate_panel(); ?>
</div>
<?php elseif ( $active_tab === 'sql' ) : ?>
<div class="cs-tab-content active">
<?php self::render_sql_panel(); ?>
</div>
<?php endif; ?>
</div>
</div>
<?php
}
/* ==================================================================
5a. Settings panel (inline on Migrator tab)
================================================================== */
private static function render_settings_panel() {
$theme = get_option( 'cs_code_default_theme', 'dark' );
$pair_slug = get_option( 'cs_code_theme_pair', 'atom-one' );
$registry = self::get_theme_registry();
$nonce = wp_create_nonce( 'cs_code_settings_inline' );
?>
<div class="cs-panel">
<div class="cs-section-header cs-section-header-teal">
<span>🎨 CODE BLOCK SETTINGS</span>
</div>
<div class="cs-panel-body">
<div class="cs-field-row">
<div class="cs-field">
<label class="cs-label" for="cs-settings-pair">Color Theme:</label>
<select id="cs-settings-pair" name="cs_code_theme_pair" class="cs-input">
<?php foreach ( $registry as $slug => $info ) : ?>
<option value="<?php echo esc_attr( $slug ); ?>" <?php selected( $pair_slug, $slug ); ?>>
<?php echo esc_html( $info['label'] ); ?>
</option>
<?php endforeach; ?>
</select>
<span class="cs-hint">Syntax highlighting color scheme loaded from CDN.</span>
</div>
<div class="cs-field">
<label class="cs-label" for="cs-settings-theme">Default Mode:</label>
<select id="cs-settings-theme" name="cs_code_default_theme" class="cs-input">
<option value="dark" <?php selected( $theme, 'dark' ); ?>>Dark</option>
<option value="light" <?php selected( $theme, 'light' ); ?>>Light</option>
</select>
<span class="cs-hint">Visitors can still toggle per block.</span>
</div>
</div>
<div style="margin-top:14px;display:flex;align-items:center;gap:10px">
<button type="button" class="cs-btn-primary" id="cs-settings-save">💾 Save Settings</button>
<span class="cs-settings-saved" id="cs-settings-saved">✓ Saved</span>
</div>
</div>
</div>
<script>
(function() {
var saveBtn = document.getElementById('cs-settings-save');
var selPair = document.getElementById('cs-settings-pair');
var selTheme = document.getElementById('cs-settings-theme');
var savedMsg = document.getElementById('cs-settings-saved');
saveBtn.addEventListener('click', function() {
saveBtn.disabled = true;
saveBtn.textContent = 'Saving...';
var fd = new FormData();
fd.append('action', 'cs_save_theme_setting');
fd.append('nonce', <?php echo wp_json_encode( $nonce ); ?>);
fd.append('theme', selTheme.value);
fd.append('theme_pair', selPair.value);
fetch(ajaxurl, { method: 'POST', body: fd })
.then(function(r) { return r.json(); })
.then(function(resp) {
saveBtn.disabled = false;
saveBtn.textContent = '💾 Save Settings';
if (resp.success) {
savedMsg.classList.add('visible');
setTimeout(function() { savedMsg.classList.remove('visible'); }, 2000);
}
})
.catch(function() {
saveBtn.disabled = false;
saveBtn.textContent = '💾 Save Settings';
});
});
})();
</script>
<?php
}
/* ==================================================================
5b. Migrate panel
================================================================== */
private static function render_migrate_panel() {
?>
<div class="cs-panel">
<div class="cs-section-header">
<span>🔄 CODE BLOCK MIGRATOR</span>
</div>
<div class="cs-panel-body">
<p style="color:#555;margin:0 0 16px;font-size:13px;line-height:1.6">
Scan your posts for legacy WordPress code blocks, preview changes, then migrate one at a time or all at once.
</p>
<div class="cs-migrate-toolbar">
<button id="cs-scan-btn" class="cs-btn-primary" style="padding:8px 20px;font-size:13px">
<span class="dashicons dashicons-search" style="font-size:14px;width:14px;height:14px;margin-top:1px"></span> Scan Posts
</button>
<button id="cs-migrate-all-btn" class="cs-btn-orange" style="padding:8px 20px;font-size:13px;opacity:.5;pointer-events:none" disabled>
<span class="dashicons dashicons-update" style="font-size:14px;width:14px;height:14px;margin-top:1px"></span> Migrate All Remaining
</button>
<span id="cs-scan-status" class="cs-status"></span>
</div>
<div id="cs-results-area">
<p class="cs-migrate-hint">Click <strong>Scan Posts</strong> to find all posts with legacy code blocks.</p>
</div>
</div>
</div>
<div id="cs-preview-modal" class="cs-modal" style="display:none;">
<div class="cs-modal-backdrop"></div>
<div class="cs-modal-content">
<div class="cs-modal-header">
<h2 id="cs-modal-title">Preview</h2>
<button class="cs-modal-close">×</button>
</div>
<div class="cs-modal-body" id="cs-modal-body">
Loading...
</div>
<div class="cs-modal-footer">
<button id="cs-modal-migrate-btn" class="cs-btn-primary" data-post-id="" style="padding:8px 20px">
<span class="dashicons dashicons-yes-alt"></span> Migrate This Post
</button>
<button class="cs-modal-close-btn" style="background:#fff;border:1.5px solid #dce3ef;border-radius:5px;padding:6px 16px;font-size:12px;font-weight:600;cursor:pointer">Cancel</button>
</div>
</div>
</div>
<?php
}
/* ==================================================================
5c. SQL Command panel
================================================================== */
private static function render_sql_panel() {
$sql_nonce = wp_create_nonce( 'cs_sql_nonce' );
global $wpdb;
$prefix = $wpdb->prefix;
?>
<div class="cs-panel">
<div class="cs-section-header cs-section-header-purple">
<span>🗄️ SQL Query</span>
<span class="cs-header-hint">Table prefix: <code><?php echo esc_html( $prefix ); ?></code> · ⚠ Read only (SELECT, SHOW, DESCRIBE, EXPLAIN)</span>
</div>
<div class="cs-panel-body">
<textarea id="cs-sql-input" class="cs-sql-textarea" placeholder="SELECT option_name, option_value FROM <?php echo esc_attr( $prefix ); ?>options WHERE option_name = 'siteurl';"></textarea>
<div style="display:flex;align-items:center;gap:10px;margin-top:12px">
<button type="button" class="cs-btn-primary" id="cs-sql-run" style="padding:8px 20px;font-size:13px">▶ Run Query</button>
<button type="button" class="cs-btn-pink" id="cs-sql-clear">🧹 Clear</button>
<span id="cs-sql-status" style="font-size:12px;color:#888"></span>
<span style="margin-left:auto;font-size:11px;color:#999">Enter or Ctrl+Enter to run</span>
</div>
</div>
</div>
<div class="cs-panel">
<div class="cs-section-header cs-section-header-green">
<span>📊 Results</span>
<span id="cs-sql-meta" style="font-size:12px;opacity:0.85"></span>
</div>
<div class="cs-panel-body">
<div id="cs-sql-results" style="overflow-x:auto;font-size:13px">
<div style="text-align:center;color:#999;padding:40px 0">Run a query to see results here</div>
</div>
</div>
</div>
<div class="cs-panel">
<div class="cs-section-header cs-section-header-orange">
<span>⚡ Quick Queries</span>
</div>
<div class="cs-panel-body">
<p class="cs-quick-group-label">🏥 Health and Diagnostics</p>
<div class="cs-quick-grid">
<button type="button" class="cs-quick-btn cs-sql-quick" data-sql="SELECT @@version AS mysql_version, @@global.max_connections AS max_connections, @@global.wait_timeout AS wait_timeout_sec, @@global.max_allowed_packet / 1024 / 1024 AS max_packet_mb, DATABASE() AS current_db;">
🩺 Database health check
</button>
<button type="button" class="cs-quick-btn cs-sql-quick" data-sql="SELECT option_id, option_name, LEFT(option_value, 200) AS option_value_preview FROM <?php echo esc_attr( $prefix ); ?>options WHERE option_name IN ('siteurl','home','blogname','blogdescription','wp_version','db_version');">
🏠 Site identity options
</button>
<button type="button" class="cs-quick-btn cs-sql-quick" data-sql="SELECT table_name, engine, table_rows, ROUND(data_length/1024/1024, 2) AS data_mb, ROUND(index_length/1024/1024, 2) AS index_mb, ROUND((data_length + index_length)/1024/1024, 2) AS total_mb FROM information_schema.tables WHERE table_schema = DATABASE() ORDER BY (data_length + index_length) DESC;">
📊 Table names, sizes and rows
</button>
</div>
<p class="cs-quick-group-label">📈 Content Summary</p>