| @@ -5,79 +5,102 @@ | ||
| 5 | 5 | use WPDeveloper\BetterDocs\Editors\BlockEditor\Block; |
| 6 | 6 | |
| 7 | 7 | class ToC extends Block { |
| 8 | 8 | |
| 9 | - public $view_wrapper = 'betterdocs-toc-block'; | |
| 9 | + public $view_wrapper = 'betterdocs-toc-block'; | |
| 10 | 10 | |
| 11 | - public function get_name() { | |
| 12 | - return 'table-of-contents'; | |
| 13 | - } | |
| 11 | + public function get_name() { | |
| 12 | + return 'table-of-contents'; | |
| 13 | + } | |
| 14 | 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 | - } | |
| 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 | 46 | |
| 47 | - public function render( $attributes, $content ) { | |
| 48 | - $this->views( 'widgets/toc' ); | |
| 49 | - } | |
| 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 | + } | |
| 50 | 54 | |
| 51 | - public function view_params() { | |
| 52 | - $htags = []; | |
| 55 | + $this->views( 'widgets/toc' ); | |
| 56 | + } | |
| 53 | 57 | |
| 54 | - if ( ! empty( $this->attributes['toc_supported_tags'] ) ) { | |
| 55 | - $htags = array_map( function ( $item ) { | |
| 56 | - return $item['value']; | |
| 57 | - }, $this->attributes['toc_supported_tags'] ); | |
| 58 | - } | |
| 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 | + } | |
| 59 | 68 | |
| 60 | - $toc_setting = [ | |
| 61 | - 'htags' => $htags, | |
| 62 | - 'hierarchy' => $this->attributes['toc_list_heirarchy'], | |
| 63 | - 'list_number' => $this->attributes['toc_list_number'] | |
| 64 | - ]; | |
| 69 | + public function view_params() { | |
| 70 | + $htags = []; | |
| 65 | 71 | |
| 66 | - //set TOC data in Transient, whenever TOC(block) is called. | |
| 67 | - set_transient( 'betterdocs_toc_setting', $toc_setting ); | |
| 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 | + } | |
| 68 | 80 | |
| 69 | - $htags = implode( ',', $htags ); | |
| 81 | + $toc_setting = [ | |
| 82 | + 'htags' => $htags, | |
| 83 | + 'hierarchy' => $this->attributes['toc_list_heirarchy'], | |
| 84 | + 'list_number' => $this->attributes['toc_list_number'] | |
| 85 | + ]; | |
| 70 | 86 | |
| 71 | - $attributes = betterdocs()->template_helper->get_html_attributes( [ | |
| 72 | - 'htags' => $htags, | |
| 73 | - 'hierarchy' => $this->attributes['toc_list_heirarchy'], | |
| 74 | - 'list_number' => $this->attributes['toc_list_number'], | |
| 75 | - 'collapsible_on_mobile' => $this->attributes['toc_collapsible_small_devices'], | |
| 76 | - 'toc_title' => $this->attributes['toc_title_text'] | |
| 77 | - ] ); | |
| 87 | + //set TOC data in Transient, whenever TOC(block) is called. | |
| 88 | + set_transient( 'betterdocs_toc_setting', $toc_setting ); | |
| 78 | 89 | |
| 79 | - return [ | |
| 80 | - 'attributes' => $attributes | |
| 81 | - ]; | |
| 82 | - } | |
| 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 | + } | |
| 83 | 106 | } |