PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.9.2
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.9.2
4.9.2 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 All 200 releases
betterdocs / includes / Editors / Elementor / Widget / CodeSnippetTab.php

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

384 lines 13.4 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 use Elementor\Repeater;
9
10 class CodeSnippetTab extends BaseWidget {
11
12 public function get_name() {
13 return 'betterdocs-code-snippet-tab';
14 }
15
16 public function get_title() {
17 return __( 'BetterDocs Code Snippet Tab', 'betterdocs' );
18 }
19
20 public function get_categories() {
21 return [ 'betterdocs-elements' ];
22 }
23
24 public function get_keywords() {
25 return [ 'betterdocs-elements', 'response', 'api', 'http', 'status', 'code', 'betterdocs' ];
26 }
27
28 public function get_icon() {
29 return 'betterdocs-icon-code-snippet';
30 }
31
32 public function get_style_depends() {
33 return [ 'betterdocs-code-snippet-tab' ];
34 }
35
36 public function get_script_depends() {
37 return [ 'betterdocs-code-snippet-tab' ];
38 }
39
40 protected function register_controls() {
41 // Content Tab
42 $this->start_controls_section(
43 'content_section',
44 [
45 'label' => __( 'Responses', 'betterdocs' ),
46 'tab' => Controls_Manager::TAB_CONTENT,
47 ]
48 );
49
50 $language_options = [
51 'json' => __( 'JSON', 'betterdocs' ),
52 'xml' => __( 'XML', 'betterdocs' ),
53 'yaml' => __( 'YAML', 'betterdocs' ),
54 'javascript' => __( 'JavaScript', 'betterdocs' ),
55 'php' => __( 'PHP', 'betterdocs' ),
56 'python' => __( 'Python', 'betterdocs' ),
57 'bash' => __( 'Bash', 'betterdocs' ),
58 'html' => __( 'HTML', 'betterdocs' ),
59 'text' => __( 'Plain Text', 'betterdocs' ),
60 ];
61
62 $response_repeater = new Repeater();
63
64 $response_repeater->add_control(
65 'response_status',
66 [
67 'label' => __( 'Status / Response type', 'betterdocs' ),
68 'type' => Controls_Manager::TEXT,
69 'default' => '200',
70 'placeholder' => __( 'e.g. 200, 404', 'betterdocs' ),
71 'description' => __( 'Shown as the tab label. A colored dot is derived from the first digit (2xx green, 4xx/5xx red).', 'betterdocs' ),
72 ]
73 );
74
75 $response_repeater->add_control(
76 'response_language',
77 [
78 'label' => __( 'Language', 'betterdocs' ),
79 'type' => Controls_Manager::SELECT,
80 'default' => 'json',
81 'options' => $language_options,
82 ]
83 );
84
85 $response_repeater->add_control(
86 'response_code',
87 [
88 'label' => __( 'Response Body', 'betterdocs' ),
89 'type' => Controls_Manager::CODE,
90 'language' => 'html',
91 'rows' => 12,
92 'default' => '',
93 'placeholder' => __( 'Paste the example response here…', 'betterdocs' ),
94 ]
95 );
96
97 $this->add_control(
98 'responses',
99 [
100 'label' => __( 'Responses', 'betterdocs' ),
101 'type' => Controls_Manager::REPEATER,
102 'fields' => $response_repeater->get_controls(),
103 'default' => [
104 [
105 'response_status' => '200',
106 'response_language' => 'json',
107 'response_code' => "{\n \"success\": true\n}",
108 ],
109 ],
110 'title_field' => '{{{ response_status }}}',
111 'description' => __( 'Add one entry per HTTP status. Each becomes a tab. Click Add Item for another response.', 'betterdocs' ),
112 ]
113 );
114
115 $this->end_controls_section();
116
117 // Appearance Section
118 $this->start_controls_section(
119 'display_options_section',
120 [
121 'label' => __( 'Appearance', 'betterdocs' ),
122 'tab' => Controls_Manager::TAB_CONTENT,
123 ]
124 );
125
126 $this->add_control(
127 'theme',
128 [
129 'label' => __( 'Theme', 'betterdocs' ),
130 'type' => Controls_Manager::SELECT,
131 'default' => 'light',
132 'options' => [
133 'light' => __( 'Light', 'betterdocs' ),
134 'dark' => __( 'Dark', 'betterdocs' ),
135 ],
136 'description' => __( 'Choose light or dark styling for the response block.', 'betterdocs' ),
137 ]
138 );
139
140 $this->add_control(
141 'show_header',
142 [
143 'label' => __( 'Show header bar', 'betterdocs' ),
144 'type' => Controls_Manager::SWITCHER,
145 'label_on' => __( 'Show', 'betterdocs' ),
146 'label_off' => __( 'Hide', 'betterdocs' ),
147 'return_value' => 'yes',
148 'default' => 'yes',
149 'description' => __( 'Toggle the status tabs + copy button header.', 'betterdocs' ),
150 ]
151 );
152
153 $this->add_control(
154 'show_copy_button',
155 [
156 'label' => __( 'Enable copy button', 'betterdocs' ),
157 'type' => Controls_Manager::SWITCHER,
158 'label_on' => __( 'Show', 'betterdocs' ),
159 'label_off' => __( 'Hide', 'betterdocs' ),
160 'return_value' => 'yes',
161 'default' => 'yes',
162 'condition' => [
163 'show_header' => '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 ]
178 );
179
180 $this->end_controls_section();
181
182 // Style Tab - Wrapper
183 $this->start_controls_section(
184 'wrapper_style_section',
185 [
186 'label' => __( 'Wrapper', 'betterdocs' ),
187 'tab' => Controls_Manager::TAB_STYLE,
188 ]
189 );
190
191 $this->add_responsive_control(
192 'wrapper_margin',
193 [
194 'label' => __( 'Margin', 'betterdocs' ),
195 'type' => Controls_Manager::DIMENSIONS,
196 'size_units' => [ 'px', 'em', '%' ],
197 'selectors' => [
198 '{{WRAPPER}} .betterdocs-code-snippet-wrapper' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
199 ],
200 ]
201 );
202
203 $this->add_responsive_control(
204 'wrapper_padding',
205 [
206 'label' => __( 'Padding', 'betterdocs' ),
207 'type' => Controls_Manager::DIMENSIONS,
208 'size_units' => [ 'px', 'em', '%' ],
209 'selectors' => [
210 '{{WRAPPER}} .betterdocs-code-snippet-wrapper' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
211 ],
212 ]
213 );
214
215 $this->add_control(
216 'wrapper_background_color',
217 [
218 'label' => __( 'Background Color', 'betterdocs' ),
219 'type' => Controls_Manager::COLOR,
220 'selectors' => [
221 '{{WRAPPER}} .betterdocs-code-snippet-wrapper' => 'background-color: {{VALUE}};',
222 ],
223 ]
224 );
225
226 $this->add_control(
227 'wrapper_border_color',
228 [
229 'label' => __( 'Border Color', 'betterdocs' ),
230 'type' => Controls_Manager::COLOR,
231 'selectors' => [
232 '{{WRAPPER}} .betterdocs-code-snippet-wrapper' => 'border-color: {{VALUE}};',
233 ],
234 ]
235 );
236
237 $this->add_responsive_control(
238 'wrapper_border_radius',
239 [
240 'label' => __( 'Border Radius', 'betterdocs' ),
241 'type' => Controls_Manager::SLIDER,
242 'size_units' => [ 'px' ],
243 'range' => [
244 'px' => [
245 'min' => 0,
246 'max' => 50,
247 ],
248 ],
249 'selectors' => [
250 '{{WRAPPER}} .betterdocs-code-snippet-wrapper' => 'border-radius: {{SIZE}}{{UNIT}};',
251 ],
252 ]
253 );
254
255 $this->end_controls_section();
256
257 // Style Tab - Header
258 $this->start_controls_section(
259 'header_style_section',
260 [
261 'label' => __( 'Header', 'betterdocs' ),
262 'tab' => Controls_Manager::TAB_STYLE,
263 'condition' => [
264 'show_header' => 'yes',
265 ],
266 ]
267 );
268
269 $this->add_responsive_control(
270 'header_padding',
271 [
272 'label' => __( 'Padding', 'betterdocs' ),
273 'type' => Controls_Manager::DIMENSIONS,
274 'size_units' => [ 'px', 'em', '%' ],
275 'selectors' => [
276 '{{WRAPPER}} .betterdocs-code-snippet-header.betterdocs-file-preview-header' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
277 ],
278 ]
279 );
280
281 $this->add_control(
282 'header_background_color',
283 [
284 'label' => __( 'Background Color', 'betterdocs' ),
285 'type' => Controls_Manager::COLOR,
286 'selectors' => [
287 '{{WRAPPER}} .betterdocs-code-snippet-header.betterdocs-file-preview-header' => 'background-color: {{VALUE}};',
288 ],
289 ]
290 );
291
292 $this->add_group_control(
293 Group_Control_Typography::get_type(),
294 [
295 'name' => 'tab_typography',
296 'label' => __( 'Tab Typography', 'betterdocs' ),
297 'selector' => '{{WRAPPER}} .betterdocs-code-snippet-tab-item',
298 ]
299 );
300
301 $this->add_control(
302 'copy_button_color',
303 [
304 'label' => __( 'Copy Button Color', 'betterdocs' ),
305 'type' => Controls_Manager::COLOR,
306 'selectors' => [
307 '{{WRAPPER}} .betterdocs-code-snippet-copy-button' => 'color: {{VALUE}};',
308 ],
309 'condition' => [
310 'show_copy_button' => 'yes',
311 ],
312 ]
313 );
314
315 $this->end_controls_section();
316
317 // Style Tab - Code Content Area
318 $this->start_controls_section(
319 'code_content_style_section',
320 [
321 'label' => __( 'Code Content Area', 'betterdocs' ),
322 'tab' => Controls_Manager::TAB_STYLE,
323 ]
324 );
325
326 $this->add_responsive_control(
327 'code_content_padding',
328 [
329 'label' => __( 'Padding', 'betterdocs' ),
330 'type' => Controls_Manager::DIMENSIONS,
331 'size_units' => [ 'px', 'em', '%' ],
332 'selectors' => [
333 '{{WRAPPER}} .betterdocs-code-snippet-code' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
334 ],
335 ]
336 );
337
338 $this->add_control(
339 'code_content_background_color',
340 [
341 'label' => __( 'Background Color', 'betterdocs' ),
342 'type' => Controls_Manager::COLOR,
343 'selectors' => [
344 '{{WRAPPER}} .betterdocs-code-snippet-code' => 'background-color: {{VALUE}};',
345 ],
346 ]
347 );
348
349 $this->end_controls_section();
350 }
351
352 public function view_params() {
353 $settings = $this->get_settings_for_display();
354
355 $responses = [];
356 if ( ! empty( $settings['responses'] ) && is_array( $settings['responses'] ) ) {
357 foreach ( $settings['responses'] as $response ) {
358 $code = isset( $response['response_code'] ) ? (string) $response['response_code'] : '';
359 if ( '' === $code ) {
360 continue;
361 }
362 $responses[] = [
363 'status' => isset( $response['response_status'] ) ? (string) $response['response_status'] : '',
364 'language' => isset( $response['response_language'] ) ? (string) $response['response_language'] : 'json',
365 'code' => $code
366 ];
367 }
368 }
369
370 return [
371 'responses' => $responses,
372 'show_copy_button' => $settings['show_copy_button'] === 'yes',
373 'show_header' => isset( $settings['show_header'] ) ? $settings['show_header'] === 'yes' : true,
374 'show_line_numbers' => $settings['show_line_numbers'] === 'yes',
375 'theme' => $settings['theme'],
376 'widget_type' => 'elementor'
377 ];
378 }
379
380 protected function render_callback() {
381 $this->views( 'widgets/code-snippet-tab' );
382 }
383 }
384