PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.8.2
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.8.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 3.5.2 All 199 releases
betterdocs / includes / Editors / BlockEditor / Blocks / CodeSnippetTab.php

CodeSnippetTab.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.8.2, at includes/Editors/BlockEditor/Blocks/CodeSnippetTab.php

160 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace WPDeveloper\BetterDocs\Editors\BlockEditor\Blocks;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8
9 use WPDeveloper\BetterDocs\Editors\BlockEditor\Block;
10
11 class CodeSnippetTab extends Block {
12
13 protected $frontend_scripts = [ 'betterdocs-code-snippet-tab' ];
14 protected $frontend_styles = [ 'betterdocs-code-snippet-tab' ];
15
16 /**
17 * Unique name of the block
18 * @return string
19 */
20 public function get_name() {
21 return 'code-snippet-tab';
22 }
23
24 /**
25 * Block default attributes
26 * @return array
27 */
28 public function get_default_attributes() {
29 return [
30 'blockId' => '',
31 'blockMeta' => (object) [],
32 'resOption' => 'Desktop',
33 // [ { status, language, codeContent } ] — one status tab per entry.
34 // Must mirror the editor default in `src/attributes.js`: Gutenberg
35 // omits attributes that still equal their default from the saved
36 // markup, so an untouched block arrives here with no `responses` at
37 // all. Defaulting to an empty list rendered nothing on the frontend
38 // while the editor happily showed a 200 tab.
39 'responses' => [
40 [
41 'status' => '200',
42 'language' => 'json',
43 'codeContent' => "{\n \"success\": true\n}"
44 ]
45 ],
46 'showCopyButton' => true,
47 'showHeader' => true,
48 'showLineNumbers' => false,
49 'theme' => 'light'
50 ];
51 }
52
53 /**
54 * Register scripts and styles
55 */
56 public function register_scripts() {
57 $this->assets_manager->register( 'betterdocs-code-snippet-tab', 'public/css/code-snippet-tab.css' );
58 $this->assets_manager->register( 'betterdocs-code-snippet-tab', 'public/js/code-snippet-tab.js', [ 'wp-element' ] );
59
60 // Self-hosted Highlight.js (vendored under assets/static/vendor/highlightjs/) —
61 // same convention as the Code Snippet block; kept self-contained so the
62 // Code Snippet Tab works with no Code Snippet block on the page.
63 $this->assets_manager->localize(
64 'betterdocs-code-snippet-tab',
65 'BetterDocsCodeSnippetTabConfig',
66 [
67 'hljsUrl' => $this->assets_manager->asset_url( 'vendor/highlightjs/highlight.min.js' ),
68 'cssLight' => $this->assets_manager->asset_url( 'vendor/highlightjs/github.min.css' ),
69 'cssDark' => $this->assets_manager->asset_url( 'vendor/highlightjs/github-dark.min.css' )
70 ]
71 );
72
73 // Enqueue CodeMirror for Gutenberg editor
74 if ( is_admin() ) {
75 add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_editor_assets' ], 5 );
76 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_editor_assets' ], 5 );
77 }
78 }
79
80 /**
81 * Enqueue CodeMirror assets for the block editor
82 */
83 public function enqueue_editor_assets() {
84 $screen = get_current_screen();
85 if ( ! $screen || ( $screen->base !== 'post' && $screen->base !== 'site-editor' ) ) {
86 return;
87 }
88
89 if ( function_exists( 'wp_enqueue_code_editor' ) ) {
90 wp_enqueue_code_editor( [
91 'type' => 'text/html'
92 ] );
93 }
94
95 wp_enqueue_style( 'betterdocs-code-snippet-tab' );
96 wp_enqueue_script( 'betterdocs-code-snippet-tab' );
97 }
98
99 /**
100 * Render the block
101 * @param array $attributes
102 * @param string $content
103 */
104 public function render( $attributes, $content ) {
105 $this->views( 'templates/parts/code-snippet-tab' );
106 }
107
108 /**
109 * View parameters for the template
110 * @return array
111 */
112 public function view_params() {
113 $attributes = &$this->attributes;
114
115 return [
116 'responses' => $this->build_responses( $attributes ),
117 'show_copy_button' => $attributes['showCopyButton'],
118 'show_header' => isset( $attributes['showHeader'] ) ? $attributes['showHeader'] : true,
119 'show_line_numbers' => $attributes['showLineNumbers'],
120 'theme' => $attributes['theme'],
121 'block_id' => $attributes['blockId'],
122 'widget_type' => 'blocks'
123 ];
124 }
125
126 /**
127 * Normalise the `responses` attribute into the template's tab list. Each
128 * tab is `{ status, language, code }`; entries with empty code are dropped.
129 *
130 * @param array $attributes
131 * @return array<int, array{status: string, language: string, code: string}>
132 */
133 protected function build_responses( $attributes ) {
134 $tabs = [];
135
136 if ( empty( $attributes['responses'] ) || ! is_array( $attributes['responses'] ) ) {
137 return $tabs;
138 }
139
140 foreach ( $attributes['responses'] as $response ) {
141 if ( ! is_array( $response ) ) {
142 continue;
143 }
144
145 $code = isset( $response['codeContent'] ) ? (string) $response['codeContent'] : '';
146 if ( '' === $code ) {
147 continue;
148 }
149
150 $tabs[] = [
151 'status' => isset( $response['status'] ) ? (string) $response['status'] : '',
152 'language' => isset( $response['language'] ) ? (string) $response['language'] : 'json',
153 'code' => $code
154 ];
155 }
156
157 return $tabs;
158 }
159 }
160