PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.7.0
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.7.0
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / Editors / Elementor / Widget / CodeSnippet.php

CodeSnippet.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.7.0, at includes/Editors/Elementor/Widget/CodeSnippet.php

608 lines 22.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPDeveloper\BetterDocs\Editors\Elementor\Widget;
4
5 use WPDeveloper\BetterDocs\Editors\Elementor\BaseWidget;
6 use Elementor\Controls_Manager;
7 use Elementor\Group_Control_Typography;
8
9 class CodeSnippet extends BaseWidget {
10
11 public function get_name() {
12 return 'betterdocs-code-snippet';
13 }
14
15 public function get_title() {
16 return __( 'BetterDocs Code Snippet', 'betterdocs' );
17 }
18
19 public function get_categories() {
20 return [ 'betterdocs-elements' ];
21 }
22
23 public function get_keywords() {
24 return [ 'betterdocs-elements', 'code', 'snippet', 'syntax', 'highlight', 'programming', 'betterdocs' ];
25 }
26
27 public function get_icon() {
28 return 'betterdocs-icon-code-snippet';
29 }
30
31 public function get_style_depends() {
32 return [ 'betterdocs-code-snippet' ];
33 }
34
35 public function get_script_depends() {
36 return [ 'betterdocs-code-snippet' ];
37 }
38
39 public function get_custom_help_url() {
40 return 'https://betterdocs.co/docs/configure-code-snippet-in-betterdocs/';
41 }
42
43 protected function register_controls() {
44 // Content Tab
45 $this->start_controls_section(
46 'content_section',
47 [
48 'label' => __( 'Code Snippet', 'betterdocs' ),
49 'tab' => Controls_Manager::TAB_CONTENT,
50 ]
51 );
52
53 $this->add_control(
54 'code_content',
55 [
56 'label' => __( 'Code', 'betterdocs' ),
57 'type' => Controls_Manager::CODE,
58 'language' => 'html', // Default language for CodeMirror
59 'rows' => 15,
60 'default' => "// Paste or type your code here…",
61 'placeholder' => __( 'Enter your code snippet here...', 'betterdocs' ),
62 'description' => __( 'Enter your code snippet. Use Tab for indentation and enjoy syntax highlighting in the editor.', 'betterdocs' ),
63 ]
64 );
65
66 $this->add_control(
67 'language',
68 [
69 'label' => __( 'Language', 'betterdocs' ),
70 'type' => Controls_Manager::SELECT,
71 'default' => 'javascript',
72 'options' => [
73 'javascript' => __( 'JavaScript', 'betterdocs' ),
74 'php' => __( 'PHP', 'betterdocs' ),
75 'python' => __( 'Python', 'betterdocs' ),
76 'java' => __( 'Java', 'betterdocs' ),
77 'ruby' => __( 'Ruby', 'betterdocs' ),
78 'bash' => __( 'Bash', 'betterdocs' ),
79 'json' => __( 'JSON', 'betterdocs' ),
80 'yaml' => __( 'YAML', 'betterdocs' ),
81 'html' => __( 'HTML', 'betterdocs' ),
82 'css' => __( 'CSS', 'betterdocs' ),
83 'sql' => __( 'SQL', 'betterdocs' ),
84 'xml' => __( 'XML', 'betterdocs' ),
85 'cpp' => __( 'C++', 'betterdocs' ),
86 'csharp' => __( 'C#', 'betterdocs' ),
87 'go' => __( 'Go', 'betterdocs' ),
88 'rust' => __( 'Rust', 'betterdocs' ),
89 'swift' => __( 'Swift', 'betterdocs' ),
90 'kotlin' => __( 'Kotlin', 'betterdocs' ),
91 'typescript' => __( 'TypeScript', 'betterdocs' ),
92 ],
93 'description' => __( 'Choose language for syntax highlighting.', 'betterdocs' ),
94 ]
95 );
96
97 $this->end_controls_section();
98
99 // Display Options Section
100 $this->start_controls_section(
101 'display_options_section',
102 [
103 'label' => __( 'Appearance', 'betterdocs' ),
104 'tab' => Controls_Manager::TAB_CONTENT,
105 ]
106 );
107
108 $this->add_control(
109 'theme',
110 [
111 'label' => __( 'Theme', 'betterdocs' ),
112 'type' => Controls_Manager::SELECT,
113 'default' => 'light',
114 'options' => [
115 'light' => __( 'Light', 'betterdocs' ),
116 'dark' => __( 'Dark', 'betterdocs' ),
117 ],
118 'description' => __( 'Choose light or dark styling for the code snippet block.', 'betterdocs' ),
119 ]
120 );
121
122 $this->add_control(
123 'show_header',
124 [
125 'label' => __( 'Show header bar', 'betterdocs' ),
126 'type' => Controls_Manager::SWITCHER,
127 'label_on' => __( 'Show', 'betterdocs' ),
128 'label_off' => __( 'Hide', 'betterdocs' ),
129 'return_value' => 'yes',
130 'default' => 'yes',
131 'description' => __( 'Toggle the filename header and copy button.', 'betterdocs' ),
132 ]
133 );
134
135 $this->add_control(
136 'show_copy_button',
137 [
138 'label' => __( 'Enable copy button', 'betterdocs' ),
139 'type' => Controls_Manager::SWITCHER,
140 'label_on' => __( 'Show', 'betterdocs' ),
141 'label_off' => __( 'Hide', 'betterdocs' ),
142 'return_value' => 'yes',
143 'default' => 'yes',
144 'description' => __( 'Show a one-click copy-to-clipboard icon.', 'betterdocs' ),
145 'condition' => [
146 'show_header' => 'yes',
147 ],
148 ]
149 );
150
151 $this->add_control(
152 'show_copy_tooltip',
153 [
154 'label' => __( 'Enable copy tooltip', 'betterdocs' ),
155 'type' => Controls_Manager::SWITCHER,
156 'label_on' => __( 'Show', 'betterdocs' ),
157 'label_off' => __( 'Hide', 'betterdocs' ),
158 'return_value' => 'yes',
159 'default' => 'no',
160 'description' => __( 'Display a tooltip (‘Copied!’) on copy button hover or click.', 'betterdocs' ),
161 'condition' => [
162 'show_header' => 'yes',
163 'show_copy_button' => 'yes',
164 ],
165 ]
166 );
167
168 $this->add_control(
169 'show_line_numbers',
170 [
171 'label' => __( 'Show line numbers', 'betterdocs' ),
172 'type' => Controls_Manager::SWITCHER,
173 'label_on' => __( 'Show', 'betterdocs' ),
174 'label_off' => __( 'Hide', 'betterdocs' ),
175 'return_value' => 'yes',
176 'default' => 'no',
177 'description' => __( 'Display line numbers in the code block.', 'betterdocs' ),
178 ]
179 );
180
181 $this->end_controls_section();
182
183 // File Preview Header Section
184 $this->start_controls_section(
185 'file_preview_section',
186 [
187 'label' => __( 'File Preview Header', 'betterdocs' ),
188 'tab' => Controls_Manager::TAB_CONTENT,
189 'condition' => [
190 'show_header' => 'yes',
191 ],
192 ]
193 );
194
195 $this->add_control(
196 'show_language_label',
197 [
198 'label' => __( 'Show Language Label', 'betterdocs' ),
199 'type' => Controls_Manager::SWITCHER,
200 'label_on' => __( 'Show', 'betterdocs' ),
201 'label_off' => __( 'Hide', 'betterdocs' ),
202 'return_value' => 'yes',
203 'default' => 'yes',
204 'description' => __( 'Display the programming language label in the header.', 'betterdocs' ),
205 ]
206 );
207
208 $this->add_control(
209 'file_name',
210 [
211 'label' => __( 'File Name', 'betterdocs' ),
212 'type' => Controls_Manager::TEXT,
213 'default' => 'filename.js',
214 'placeholder' => __( 'Enter filename with extension', 'betterdocs' ),
215 'description' => __( 'Enter the filename to display in the header (e.g., hero-section.tsx)', 'betterdocs' ),
216 ]
217 );
218
219 $this->add_control(
220 'show_traffic_lights',
221 [
222 'label' => __( 'Show window buttons', 'betterdocs' ),
223 'type' => Controls_Manager::SWITCHER,
224 'label_on' => __( 'Show', 'betterdocs' ),
225 'label_off' => __( 'Hide', 'betterdocs' ),
226 'return_value' => 'yes',
227 'default' => 'yes',
228 'description' => __( 'Display macOS-style close/minimize/maximize circles.', 'betterdocs' ),
229 ]
230 );
231
232 $this->add_control(
233 'show_file_icon',
234 [
235 'label' => __( 'Show language icon', 'betterdocs' ),
236 'type' => Controls_Manager::SWITCHER,
237 'label_on' => __( 'Show', 'betterdocs' ),
238 'label_off' => __( 'Hide', 'betterdocs' ),
239 'return_value' => 'yes',
240 'default' => 'yes',
241 'description' => __( 'Display the default icon for this file type.', 'betterdocs' ),
242 ]
243 );
244
245 $this->add_control(
246 'file_icon',
247 [
248 'label' => __( 'Custom language icon', 'betterdocs' ),
249 'type' => Controls_Manager::MEDIA,
250 'media_types' => [ 'image' ],
251 'description' => __( 'Upload a custom icon to override the default.', 'betterdocs' ),
252 'condition' => [
253 'show_file_icon' => 'yes',
254 ],
255 ]
256 );
257
258 $this->end_controls_section();
259
260 // Style Tab - Wrapper
261 $this->start_controls_section(
262 'wrapper_style_section',
263 [
264 'label' => __( 'Wrapper', 'betterdocs' ),
265 'tab' => Controls_Manager::TAB_STYLE,
266 ]
267 );
268
269 $this->add_responsive_control(
270 'wrapper_margin',
271 [
272 'label' => __( 'Margin', 'betterdocs' ),
273 'type' => Controls_Manager::DIMENSIONS,
274 'size_units' => [ 'px', 'em', '%' ],
275 'selectors' => [
276 '{{WRAPPER}} .betterdocs-code-snippet-wrapper' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
277 ],
278 ]
279 );
280
281 $this->add_responsive_control(
282 'wrapper_padding',
283 [
284 'label' => __( 'Padding', 'betterdocs' ),
285 'type' => Controls_Manager::DIMENSIONS,
286 'size_units' => [ 'px', 'em', '%' ],
287 'selectors' => [
288 '{{WRAPPER}} .betterdocs-code-snippet-wrapper' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
289 ],
290 ]
291 );
292
293 $this->add_control(
294 'wrapper_background_color',
295 [
296 'label' => __( 'Background Color', 'betterdocs' ),
297 'type' => Controls_Manager::COLOR,
298 'selectors' => [
299 '{{WRAPPER}} .betterdocs-code-snippet-wrapper' => 'background-color: {{VALUE}};',
300 ],
301 ]
302 );
303
304 $this->add_control(
305 'wrapper_border_color',
306 [
307 'label' => __( 'Border Color', 'betterdocs' ),
308 'type' => Controls_Manager::COLOR,
309 'selectors' => [
310 '{{WRAPPER}} .betterdocs-code-snippet-wrapper' => 'border-color: {{VALUE}};',
311 ],
312 ]
313 );
314
315 $this->add_responsive_control(
316 'wrapper_border_width',
317 [
318 'label' => __( 'Border Width', 'betterdocs' ),
319 'type' => Controls_Manager::SLIDER,
320 'size_units' => [ 'px' ],
321 'range' => [
322 'px' => [
323 'min' => 0,
324 'max' => 10,
325 ],
326 ],
327 'selectors' => [
328 '{{WRAPPER}} .betterdocs-code-snippet-wrapper' => 'border-width: {{SIZE}}{{UNIT}}; border-style: solid;',
329 ],
330 ]
331 );
332
333 $this->add_responsive_control(
334 'wrapper_border_radius',
335 [
336 'label' => __( 'Border Radius', 'betterdocs' ),
337 'type' => Controls_Manager::SLIDER,
338 'size_units' => [ 'px' ],
339 'range' => [
340 'px' => [
341 'min' => 0,
342 'max' => 50,
343 ],
344 ],
345 'selectors' => [
346 '{{WRAPPER}} .betterdocs-code-snippet-wrapper' => 'border-radius: {{SIZE}}{{UNIT}};',
347 ],
348 ]
349 );
350
351 $this->end_controls_section();
352
353 // Style Tab - Header
354 $this->start_controls_section(
355 'header_style_section',
356 [
357 'label' => __( 'Header', 'betterdocs' ),
358 'tab' => Controls_Manager::TAB_STYLE,
359 'condition' => [
360 'show_header' => 'yes',
361 ],
362 ]
363 );
364
365 $this->add_responsive_control(
366 'header_padding',
367 [
368 'label' => __( 'Padding', 'betterdocs' ),
369 'type' => Controls_Manager::DIMENSIONS,
370 'size_units' => [ 'px', 'em', '%' ],
371 'selectors' => [
372 '{{WRAPPER}} .betterdocs-code-snippet-header.betterdocs-file-preview-header' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
373 ],
374 ]
375 );
376
377 $this->add_control(
378 'header_background_color',
379 [
380 'label' => __( 'Background Color', 'betterdocs' ),
381 'type' => Controls_Manager::COLOR,
382 'selectors' => [
383 '{{WRAPPER}} .betterdocs-code-snippet-header.betterdocs-file-preview-header' => 'background-color: {{VALUE}};',
384 ],
385 ]
386 );
387
388 $this->add_control(
389 'header_border_color',
390 [
391 'label' => __( 'Border Color', 'betterdocs' ),
392 'type' => Controls_Manager::COLOR,
393 'selectors' => [
394 '{{WRAPPER}} .betterdocs-code-snippet-header.betterdocs-file-preview-header' => 'border-bottom-color: {{VALUE}};',
395 ],
396 ]
397 );
398
399 $this->add_responsive_control(
400 'header_border_width',
401 [
402 'label' => __( 'Border Width', 'betterdocs' ),
403 'type' => Controls_Manager::SLIDER,
404 'size_units' => [ 'px' ],
405 'range' => [
406 'px' => [
407 'min' => 0,
408 'max' => 10,
409 ],
410 ],
411 'selectors' => [
412 '{{WRAPPER}} .betterdocs-code-snippet-header.betterdocs-file-preview-header' => 'border-bottom-width: {{SIZE}}{{UNIT}}; border-bottom-style: solid;',
413 ],
414 'description' => __( 'Controls border bottom width', 'betterdocs' ),
415 ]
416 );
417
418 $this->add_group_control(
419 Group_Control_Typography::get_type(),
420 [
421 'name' => 'file_name_typography',
422 'label' => __( 'File Name Typography', 'betterdocs' ),
423 'selector' => '{{WRAPPER}} .betterdocs-code-snippet-header .file-name-text',
424 ]
425 );
426
427 $this->add_control(
428 'file_name_color',
429 [
430 'label' => __( 'File Name Color', 'betterdocs' ),
431 'type' => Controls_Manager::COLOR,
432 'selectors' => [
433 '{{WRAPPER}} .betterdocs-code-snippet-header .file-name-text' => 'color: {{VALUE}};',
434 ],
435 ]
436 );
437
438 $this->add_control(
439 'copy_button_color',
440 [
441 'label' => __( 'Copy Button Color', 'betterdocs' ),
442 'type' => Controls_Manager::COLOR,
443 'selectors' => [
444 '{{WRAPPER}} .betterdocs-code-snippet-copy-button' => 'color: {{VALUE}};',
445 ],
446 'condition' => [
447 'show_copy_button' => 'yes',
448 ],
449 ]
450 );
451
452 $this->add_control(
453 'copy_button_border_color',
454 [
455 'label' => __( 'Copy Button Border Color', 'betterdocs' ),
456 'type' => Controls_Manager::COLOR,
457 'selectors' => [
458 '{{WRAPPER}} .betterdocs-code-snippet-copy-button' => 'border-color: {{VALUE}};',
459 ],
460 'condition' => [
461 'show_copy_button' => 'yes',
462 ],
463 ]
464 );
465
466 $this->end_controls_section();
467
468
469
470 // Style Tab - Line Numbers
471 $this->start_controls_section(
472 'line_numbers_style_section',
473 [
474 'label' => __( 'Line Numbers', 'betterdocs' ),
475 'tab' => Controls_Manager::TAB_STYLE,
476 'condition' => [
477 'show_line_numbers' => 'yes',
478 ],
479 ]
480 );
481
482 $this->add_responsive_control(
483 'line_numbers_padding',
484 [
485 'label' => __( 'Padding', 'betterdocs' ),
486 'type' => Controls_Manager::DIMENSIONS,
487 'size_units' => [ 'px', 'em', '%' ],
488 'selectors' => [
489 '{{WRAPPER}} .betterdocs-code-snippet-line-numbers' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
490 ],
491 ]
492 );
493
494 $this->add_control(
495 'line_numbers_color',
496 [
497 'label' => __( 'Line Number Color', 'betterdocs' ),
498 'type' => Controls_Manager::COLOR,
499 'selectors' => [
500 '{{WRAPPER}} .betterdocs-code-snippet-line-numbers .line-number' => 'color: {{VALUE}};',
501 ],
502 ]
503 );
504
505 $this->add_control(
506 'line_numbers_background_color',
507 [
508 'label' => __( 'Line Number Background', 'betterdocs' ),
509 'type' => Controls_Manager::COLOR,
510 'selectors' => [
511 '{{WRAPPER}} .betterdocs-code-snippet-line-numbers' => 'background-color: {{VALUE}};',
512 ],
513 ]
514 );
515
516 $this->add_control(
517 'line_numbers_border_color',
518 [
519 'label' => __( 'Border Color', 'betterdocs' ),
520 'type' => Controls_Manager::COLOR,
521 'selectors' => [
522 '{{WRAPPER}} .betterdocs-code-snippet-line-numbers' => 'border-right-color: {{VALUE}};',
523 ],
524 ]
525 );
526
527 $this->add_responsive_control(
528 'line_numbers_border_width',
529 [
530 'label' => __( 'Border Width', 'betterdocs' ),
531 'type' => Controls_Manager::SLIDER,
532 'size_units' => [ 'px' ],
533 'range' => [
534 'px' => [
535 'min' => 0,
536 'max' => 10,
537 ],
538 ],
539 'selectors' => [
540 '{{WRAPPER}} .betterdocs-code-snippet-line-numbers' => 'border-right-width: {{SIZE}}{{UNIT}}; border-right-style: solid;',
541 ],
542 'description' => __( 'Controls border right width', 'betterdocs' ),
543 ]
544 );
545
546 $this->end_controls_section();
547
548 // Style Tab - Code Content Area
549 $this->start_controls_section(
550 'code_content_style_section',
551 [
552 'label' => __( 'Code Content Area', 'betterdocs' ),
553 'tab' => Controls_Manager::TAB_STYLE,
554 ]
555 );
556
557 $this->add_responsive_control(
558 'code_content_padding',
559 [
560 'label' => __( 'Padding', 'betterdocs' ),
561 'type' => Controls_Manager::DIMENSIONS,
562 'size_units' => [ 'px', 'em', '%' ],
563 'selectors' => [
564 '{{WRAPPER}} .betterdocs-code-snippet-code' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
565 ],
566 ]
567 );
568
569 $this->add_control(
570 'code_content_background_color',
571 [
572 'label' => __( 'Background Color', 'betterdocs' ),
573 'type' => Controls_Manager::COLOR,
574 'selectors' => [
575 '{{WRAPPER}} .betterdocs-code-snippet-code' => 'background-color: {{VALUE}};',
576 ],
577 ]
578 );
579
580 $this->end_controls_section();
581 }
582
583 public function view_params() {
584 $settings = $this->get_settings_for_display();
585
586 return [
587 'code_content' => $settings['code_content'],
588 'language' => $settings['language'],
589 'show_language_label' => $settings['show_language_label'] === 'yes',
590 'show_copy_button' => $settings['show_copy_button'] === 'yes',
591 'show_copy_tooltip' => isset( $settings['show_copy_tooltip'] ) ? $settings['show_copy_tooltip'] === 'yes' : false,
592 'show_header' => isset( $settings['show_header'] ) ? $settings['show_header'] === 'yes' : true,
593 'show_line_numbers' => $settings['show_line_numbers'] === 'yes',
594 'theme' => $settings['theme'],
595 'widget_type' => 'elementor',
596 // File Preview Header
597 'file_name' => $settings['file_name'],
598 'show_traffic_lights' => $settings['show_traffic_lights'] === 'yes',
599 'show_file_icon' => $settings['show_file_icon'] === 'yes',
600 'file_icon' => isset( $settings['file_icon']['url'] ) ? $settings['file_icon']['url'] : '',
601 ];
602 }
603
604 protected function render_callback() {
605 $this->views( 'widgets/code-snippet' );
606 }
607 }
608