PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.6.2
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.6.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 / ToC.php

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

107 lines 2.6 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\BlockEditor\Blocks;
4
5 use WPDeveloper\BetterDocs\Editors\BlockEditor\Block;
6
7 class ToC extends Block {
8
9 public $view_wrapper = 'betterdocs-toc-block';
10
11 public function get_name() {
12 return 'table-of-contents';
13 }
14
15 public function get_default_attributes() {
16 return [
17 'blockId' => '',
18 'toc_supported_tags' => [
19 [
20 'value' => 1,
21 'label' => 'H1'
22 ],
23 [
24 'value' => 2,
25 'label' => 'H2'
26 ],
27 [
28 'value' => 3,
29 'label' => 'H3'
30 ],
31 [
32 'value' => 4,
33 'label' => 'H4'
34 ],
35 [
36 'value' => 5,
37 'label' => 'H5'
38 ]
39 ],
40 'toc_list_heirarchy' => true,
41 'toc_list_number' => true,
42 'toc_collapsible_small_devices' => true,
43 'toc_title_text' => 'Table Of Contents'
44 ];
45 }
46
47 public function render( $attributes, $content ) {
48 // Check if post is password protected and user hasn't provided correct password
49 // Skip check in editor mode
50 if ( ! $this->is_editor_mode() && post_password_required() ) {
51 // Don't show ToC for password-protected posts until password is provided
52 return '';
53 }
54
55 $this->views( 'widgets/toc' );
56 }
57
58 /**
59 * Check if we're in editor mode
60 *
61 * @return bool
62 */
63 private function is_editor_mode() {
64 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- REST request context check.
65 $context = isset( $_REQUEST['context'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['context'] ) ) : '';
66 return defined( 'REST_REQUEST' ) && REST_REQUEST && 'edit' === $context;
67 }
68
69 public function view_params() {
70 $htags = [];
71
72 if ( ! empty( $this->attributes['toc_supported_tags'] ) ) {
73 $htags = array_map(
74 function ( $item ) {
75 return $item['value'];
76 },
77 $this->attributes['toc_supported_tags']
78 );
79 }
80
81 $toc_setting = [
82 'htags' => $htags,
83 'hierarchy' => $this->attributes['toc_list_heirarchy'],
84 'list_number' => $this->attributes['toc_list_number']
85 ];
86
87 //set TOC data in Transient, whenever TOC(block) is called.
88 set_transient( 'betterdocs_toc_setting', $toc_setting );
89
90 $htags = implode( ',', $htags );
91
92 $attributes = betterdocs()->template_helper->get_html_attributes(
93 [
94 'htags' => $htags,
95 'hierarchy' => $this->attributes['toc_list_heirarchy'],
96 'list_number' => $this->attributes['toc_list_number'],
97 'collapsible_on_mobile' => $this->attributes['toc_collapsible_small_devices'],
98 'toc_title' => $this->attributes['toc_title_text']
99 ]
100 );
101
102 return [
103 'attributes' => $attributes
104 ];
105 }
106 }
107