PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.9.3
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.9.3
4.9.3 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 All 201 releases
← All changes | includes/Editors/BlockEditor/Blocks/ToC.php +89 -63 3.4.2 → 4.9.3 View file →
@@ -4,77 +4,103 @@
4 4
5 5 use WPDeveloper\BetterDocs\Editors\BlockEditor\Block;
6 6
7 7 class ToC extends Block {
8 - public function get_name() {
9 - return 'table-of-contents';
10 - }
11 8
12 - public function get_default_attributes() {
13 - return [
14 - 'blockId' => '',
15 - 'toc_supported_tags' => [
16 - [
17 - 'value' => 1,
18 - 'label' => 'H1'
19 - ],
20 - [
21 - 'value' => 2,
22 - 'label' => 'H2'
23 - ],
24 - [
25 - 'value' => 3,
26 - 'label' => 'H3'
27 - ],
28 - [
29 - 'value' => 4,
30 - 'label' => 'H4'
31 - ],
32 - [
33 - 'value' => 5,
34 - 'label' => 'H5'
35 - ]
36 - ],
37 - 'toc_list_heirarchy' => true,
38 - 'toc_list_number' => true,
39 - 'toc_collapsible_small_devices' => true,
40 - 'toc_title_text' => 'Table Of Contents'
41 - ];
42 - }
9 + public $view_wrapper = 'betterdocs-toc-block';
43 10
44 - public function render( $attributes, $content ) {
45 - $this->views( 'widgets/toc' );
46 - }
11 + public function get_name() {
12 + return 'table-of-contents';
13 + }
47 14
48 - public function view_params() {
49 - $htags = [];
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 + }
50 46
51 - if ( ! empty( $this->attributes['toc_supported_tags'] ) ) {
52 - $htags = array_map( function ( $item ) {
53 - return $item['value'];
54 - }, $this->attributes['toc_supported_tags'] );
55 - }
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 + }
56 54
57 - $toc_setting = [
58 - 'htags' => $htags,
59 - 'hierarchy' => $this->attributes['toc_list_heirarchy'],
60 - 'list_number' => $this->attributes['toc_list_number']
61 - ];
55 + $this->views( 'widgets/toc' );
56 + }
62 57
63 - //set TOC data in Transient, whenever TOC(block) is called.
64 - set_transient( 'betterdocs_toc_setting', $toc_setting );
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 + }
65 68
66 - $htags = implode( ',', $htags );
69 + public function view_params() {
70 + $htags = [];
67 71
68 - $attributes = betterdocs()->template_helper->get_html_attributes( [
69 - 'htags' => $htags,
70 - 'hierarchy' => $this->attributes['toc_list_heirarchy'],
71 - 'list_number' => $this->attributes['toc_list_number'],
72 - 'collapsible_on_mobile' => $this->attributes['toc_collapsible_small_devices'],
73 - 'toc_title' => $this->attributes['toc_title_text']
74 - ] );
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 + }
75 80
76 - return [
77 - 'attributes' => $attributes
78 - ];
79 - }
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 + }
80 106 }