| 1 |
<?php |
| 2 |
|
| 3 |
use Elementor\Controls_Manager; |
| 4 |
use Elementor\Group_Control_Typography; |
| 5 |
use Elementor\Widget_Base; |
| 6 |
use Elementor\Core\Schemes; |
| 7 |
use Elementor\Group_Control_Text_Shadow; |
| 8 |
use ElementorPro\Plugin; |
| 9 |
|
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
exit; // Exit if accessed directly |
| 12 |
} |
| 13 |
|
| 14 |
class BetterDocs_Elementor_Toc extends Widget_Base |
| 15 |
{ |
| 16 |
|
| 17 |
public function get_name() |
| 18 |
{ |
| 19 |
return 'betterdocs-toc'; |
| 20 |
} |
| 21 |
|
| 22 |
public function get_title() |
| 23 |
{ |
| 24 |
return __('Doc Table of Contents', 'betterdocs'); |
| 25 |
} |
| 26 |
|
| 27 |
public function get_icon() |
| 28 |
{ |
| 29 |
return 'betterdocs-icon-Sidebar'; |
| 30 |
} |
| 31 |
|
| 32 |
public function get_categories() |
| 33 |
{ |
| 34 |
return ['betterdocs-elements']; |
| 35 |
} |
| 36 |
|
| 37 |
public function get_keywords() |
| 38 |
{ |
| 39 |
return ['betterdocs-elements', 'toc', 'table-of-content', 'betterdocs', 'docs']; |
| 40 |
} |
| 41 |
|
| 42 |
public function get_custom_help_url() |
| 43 |
{ |
| 44 |
return 'https://betterdocs.co/docs/single-doc-in-elementor'; |
| 45 |
} |
| 46 |
|
| 47 |
protected function _register_controls() |
| 48 |
{ |
| 49 |
$this->list_options(); |
| 50 |
|
| 51 |
$this->box_setting_style(); |
| 52 |
|
| 53 |
$this->title_settings(); |
| 54 |
|
| 55 |
$this->list_settings(); |
| 56 |
} |
| 57 |
|
| 58 |
public function list_options() |
| 59 |
{ |
| 60 |
$this->start_controls_section( |
| 61 |
'section_options', |
| 62 |
[ |
| 63 |
'label' => __('Controls', 'betterdocs') |
| 64 |
] |
| 65 |
); |
| 66 |
|
| 67 |
$this->add_control( |
| 68 |
'htags', |
| 69 |
[ |
| 70 |
'label' => __('Supported Heading Tags', 'betterdocs'), |
| 71 |
'type' => Controls_Manager::SELECT2, |
| 72 |
'multiple' => true, |
| 73 |
'options' => [ |
| 74 |
'1' => __('H1', 'betterdocs'), |
| 75 |
'2' => __('H2', 'betterdocs'), |
| 76 |
'3' => __('H3', 'betterdocs'), |
| 77 |
'4' => __('H4', 'betterdocs'), |
| 78 |
'5' => __('H5', 'betterdocs'), |
| 79 |
'6' => __('H6', 'betterdocs'), |
| 80 |
], |
| 81 |
'default' => ['1', '2', '3', '4', '5', '6'], |
| 82 |
] |
| 83 |
); |
| 84 |
|
| 85 |
$this->add_control( |
| 86 |
'list_hierarchy', |
| 87 |
[ |
| 88 |
'label' => __('List Hierarchy', 'betterdocs'), |
| 89 |
'type' => Controls_Manager::SWITCHER, |
| 90 |
'label_on' => __('Show', 'betterdocs'), |
| 91 |
'label_off' => __('Hide', 'betterdocs'), |
| 92 |
'return_value' => '1', |
| 93 |
'default' => '1', |
| 94 |
] |
| 95 |
); |
| 96 |
|
| 97 |
$this->add_control( |
| 98 |
'list_number', |
| 99 |
[ |
| 100 |
'label' => __('List Number', 'betterdocs'), |
| 101 |
'type' => Controls_Manager::SWITCHER, |
| 102 |
'label_on' => __('Show', 'betterdocs'), |
| 103 |
'label_off' => __('Hide', 'betterdocs'), |
| 104 |
'return_value' => '1', |
| 105 |
'default' => '1', |
| 106 |
] |
| 107 |
); |
| 108 |
|
| 109 |
$this->add_control( |
| 110 |
'collapsible_toc_mobile', |
| 111 |
[ |
| 112 |
'label' => __('Collapsible on small devices', 'betterdocs'), |
| 113 |
'type' => Controls_Manager::SWITCHER, |
| 114 |
'label_on' => __('Show', 'betterdocs'), |
| 115 |
'label_off' => __('Hide', 'betterdocs'), |
| 116 |
'return_value' => '1', |
| 117 |
'default' => '', |
| 118 |
] |
| 119 |
); |
| 120 |
|
| 121 |
$this->end_controls_section(); |
| 122 |
} |
| 123 |
|
| 124 |
public function box_setting_style() |
| 125 |
{ |
| 126 |
/** |
| 127 |
* ---------------------------------------------------------- |
| 128 |
* Section: Box Styles |
| 129 |
* ---------------------------------------------------------- |
| 130 |
*/ |
| 131 |
$this->start_controls_section( |
| 132 |
'section_card_settings', |
| 133 |
[ |
| 134 |
'label' => __('Box', 'betterdocs'), |
| 135 |
'tab' => Controls_Manager::TAB_STYLE, |
| 136 |
] |
| 137 |
); |
| 138 |
|
| 139 |
$this->add_responsive_control( |
| 140 |
'column_width', // Legacy control id but new control |
| 141 |
[ |
| 142 |
'label' => __('Box Width (%)', 'betterdocs'), |
| 143 |
'type' => Controls_Manager::NUMBER, |
| 144 |
'min' => 0, |
| 145 |
'max' => 100, |
| 146 |
'step' => 5, |
| 147 |
'default' => 100, |
| 148 |
'selectors' => [ |
| 149 |
'{{WRAPPER}} .betterdocs-toc' => 'width: {{VALUE}}%;', |
| 150 |
] |
| 151 |
] |
| 152 |
); |
| 153 |
|
| 154 |
$this->add_responsive_control( |
| 155 |
'column_background', |
| 156 |
[ |
| 157 |
'label' => esc_html__('Background Color', 'betterdocs'), |
| 158 |
'type' => Controls_Manager::COLOR, |
| 159 |
'selectors' => [ |
| 160 |
'{{WRAPPER}} .betterdocs-toc' => 'background-color: {{VALUE}};' |
| 161 |
], |
| 162 |
] |
| 163 |
); |
| 164 |
|
| 165 |
$this->add_responsive_control( |
| 166 |
'column_space', // Legacy control id but new control |
| 167 |
[ |
| 168 |
'label' => __('Box Spacing', 'betterdocs'), |
| 169 |
'type' => Controls_Manager::DIMENSIONS, |
| 170 |
'size_units' => ['px', '%', 'em'], |
| 171 |
'selectors' => [ |
| 172 |
'{{WRAPPER}} .betterdocs-toc' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 173 |
] |
| 174 |
] |
| 175 |
); |
| 176 |
|
| 177 |
$this->add_responsive_control( |
| 178 |
'column_padding', |
| 179 |
[ |
| 180 |
'label' => __('Box Padding', 'betterdocs'), |
| 181 |
'type' => Controls_Manager::DIMENSIONS, |
| 182 |
'size_units' => ['px', 'em', '%'], |
| 183 |
'selectors' => [ |
| 184 |
'{{WRAPPER}} .betterdocs-toc' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 185 |
], |
| 186 |
] |
| 187 |
); |
| 188 |
|
| 189 |
$this->add_responsive_control( |
| 190 |
'column_border_radius', |
| 191 |
[ |
| 192 |
'label' => esc_html__('Box Border Radius', 'betterdocs'), |
| 193 |
'type' => Controls_Manager::DIMENSIONS, |
| 194 |
'size_units' => ['px', 'em', '%'], |
| 195 |
'selectors' => [ |
| 196 |
'{{WRAPPER}} .betterdocs-toc' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' |
| 197 |
], |
| 198 |
] |
| 199 |
); |
| 200 |
|
| 201 |
$this->add_responsive_control( |
| 202 |
'align', |
| 203 |
[ |
| 204 |
'label' => __('Alignment', 'betterdocs'), |
| 205 |
'type' => Controls_Manager::CHOOSE, |
| 206 |
'options' => [ |
| 207 |
'left' => [ |
| 208 |
'title' => __('Left', 'betterdocs'), |
| 209 |
'icon' => 'eicon-text-align-left', |
| 210 |
], |
| 211 |
'center' => [ |
| 212 |
'title' => __('Center', 'betterdocs'), |
| 213 |
'icon' => 'eicon-text-align-center', |
| 214 |
], |
| 215 |
'right' => [ |
| 216 |
'title' => __('Right', 'betterdocs'), |
| 217 |
'icon' => 'eicon-text-align-right', |
| 218 |
], |
| 219 |
'justify' => [ |
| 220 |
'title' => __('Justified', 'betterdocs'), |
| 221 |
'icon' => 'eicon-text-align-justify', |
| 222 |
], |
| 223 |
], |
| 224 |
'default' => '', |
| 225 |
'selectors' => [ |
| 226 |
'{{WRAPPER}}' => 'text-align: {{VALUE}};', |
| 227 |
], |
| 228 |
] |
| 229 |
); |
| 230 |
|
| 231 |
$this->end_controls_section(); # end of 'Card Settings' |
| 232 |
} |
| 233 |
|
| 234 |
public function title_settings() |
| 235 |
{ |
| 236 |
$this->start_controls_section( |
| 237 |
'section_title', |
| 238 |
[ |
| 239 |
'label' => __('Title', 'betterdocs'), |
| 240 |
'tab' => Controls_Manager::TAB_STYLE, |
| 241 |
] |
| 242 |
); |
| 243 |
|
| 244 |
$this->add_control( |
| 245 |
'title_color', |
| 246 |
[ |
| 247 |
'label' => __('Text Color', 'betterdocs'), |
| 248 |
'type' => Controls_Manager::COLOR, |
| 249 |
'scheme' => [ |
| 250 |
'type' => Schemes\Color::get_type(), |
| 251 |
'value' => Schemes\Color::COLOR_1, |
| 252 |
], |
| 253 |
'selectors' => [ |
| 254 |
'{{WRAPPER}} .betterdocs-toc .toc-title' => 'color: {{VALUE}};', |
| 255 |
], |
| 256 |
] |
| 257 |
); |
| 258 |
|
| 259 |
$this->add_group_control( |
| 260 |
Group_Control_Typography::get_type(), |
| 261 |
[ |
| 262 |
'name' => 'typography', |
| 263 |
'scheme' => Schemes\Typography::TYPOGRAPHY_1, |
| 264 |
'selector' => '{{WRAPPER}} .betterdocs-toc .toc-title', |
| 265 |
] |
| 266 |
); |
| 267 |
|
| 268 |
$this->add_group_control( |
| 269 |
Group_Control_Text_Shadow::get_type(), |
| 270 |
[ |
| 271 |
'name' => 'text_shadow', |
| 272 |
'selector' => '{{WRAPPER}} .betterdocs-toc .toc-title', |
| 273 |
] |
| 274 |
); |
| 275 |
|
| 276 |
$this->add_control( |
| 277 |
'blend_mode', |
| 278 |
[ |
| 279 |
'label' => __('Blend Mode', 'betterdocs'), |
| 280 |
'type' => Controls_Manager::SELECT, |
| 281 |
'options' => [ |
| 282 |
'' => __('Normal', 'betterdocs'), |
| 283 |
'multiply' => 'Multiply', |
| 284 |
'screen' => 'Screen', |
| 285 |
'overlay' => 'Overlay', |
| 286 |
'darken' => 'Darken', |
| 287 |
'lighten' => 'Lighten', |
| 288 |
'color-dodge' => 'Color Dodge', |
| 289 |
'saturation' => 'Saturation', |
| 290 |
'color' => 'Color', |
| 291 |
'difference' => 'Difference', |
| 292 |
'exclusion' => 'Exclusion', |
| 293 |
'hue' => 'Hue', |
| 294 |
'luminosity' => 'Luminosity', |
| 295 |
], |
| 296 |
'selectors' => [ |
| 297 |
'{{WRAPPER}} .betterdocs-toc .toc-title' => 'mix-blend-mode: {{VALUE}}', |
| 298 |
], |
| 299 |
'separator' => 'none', |
| 300 |
] |
| 301 |
); |
| 302 |
|
| 303 |
$this->end_controls_section(); |
| 304 |
|
| 305 |
$this->update_control( |
| 306 |
'title', |
| 307 |
[ |
| 308 |
'dynamic' => [ |
| 309 |
'default' => Plugin::elementor()->dynamic_tags->tag_data_to_tag_text(null, 'betterdocs-title-tag'), |
| 310 |
], |
| 311 |
], |
| 312 |
[ |
| 313 |
'recursive' => true, |
| 314 |
] |
| 315 |
); |
| 316 |
} |
| 317 |
|
| 318 |
public function list_settings() |
| 319 |
{ |
| 320 |
/** |
| 321 |
* ---------------------------------------------------------- |
| 322 |
* Section: List Settinggs |
| 323 |
* ---------------------------------------------------------- |
| 324 |
*/ |
| 325 |
$this->start_controls_section( |
| 326 |
'section_article_settings', |
| 327 |
[ |
| 328 |
'label' => __('Toc List', 'betterdocs'), |
| 329 |
'tab' => Controls_Manager::TAB_STYLE |
| 330 |
] |
| 331 |
); |
| 332 |
|
| 333 |
|
| 334 |
$this->add_group_control( |
| 335 |
Group_Control_Typography::get_type(), |
| 336 |
[ |
| 337 |
'name' => 'list_item_typography', |
| 338 |
'selector' => '{{WRAPPER}} .betterdocs-toc ul li a', |
| 339 |
] |
| 340 |
); |
| 341 |
|
| 342 |
$this->add_control( |
| 343 |
'list_word_wrap', |
| 344 |
[ |
| 345 |
'label' => __('Word Wrap', 'betterdocs'), |
| 346 |
'type' => Controls_Manager::SELECT2, |
| 347 |
'multiple' => false, |
| 348 |
'options' => [ |
| 349 |
'normal' => 'normal', |
| 350 |
'break-word' => 'break-word', |
| 351 |
'initial' => 'initial', |
| 352 |
'inherit' => 'inherit', |
| 353 |
], |
| 354 |
'default' => 'normal', |
| 355 |
'selectors' => [ |
| 356 |
'{{WRAPPER}} .betterdocs-toc ul li a' => 'word-wrap: {{VALUE}};', |
| 357 |
], |
| 358 |
] |
| 359 |
); |
| 360 |
|
| 361 |
$this->add_control( |
| 362 |
'list_color', |
| 363 |
[ |
| 364 |
'label' => esc_html__('Color', 'betterdocs'), |
| 365 |
'type' => Controls_Manager::COLOR, |
| 366 |
'selectors' => [ |
| 367 |
'{{WRAPPER}} .betterdocs-toc ul li a' => 'color: {{VALUE}};', |
| 368 |
], |
| 369 |
] |
| 370 |
); |
| 371 |
|
| 372 |
$this->add_control( |
| 373 |
'list_hover_color', |
| 374 |
[ |
| 375 |
'label' => esc_html__('Hover Color', 'betterdocs'), |
| 376 |
'type' => Controls_Manager::COLOR, |
| 377 |
'selectors' => [ |
| 378 |
'{{WRAPPER}} .betterdocs-toc ul li a:hover' => 'color: {{VALUE}};', |
| 379 |
], |
| 380 |
] |
| 381 |
); |
| 382 |
|
| 383 |
$this->add_responsive_control( |
| 384 |
'list_margin', |
| 385 |
[ |
| 386 |
'label' => esc_html__('List Item Spacing', 'betterdocs'), |
| 387 |
'type' => Controls_Manager::DIMENSIONS, |
| 388 |
'size_units' => ['px', 'em', '%'], |
| 389 |
'selectors' => [ |
| 390 |
'{{WRAPPER}} .betterdocs-toc ul li' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 391 |
], |
| 392 |
] |
| 393 |
); |
| 394 |
|
| 395 |
$this->end_controls_section(); # end of 'Column Settings' |
| 396 |
|
| 397 |
} |
| 398 |
|
| 399 |
protected function render() |
| 400 |
{ |
| 401 |
$settings = $this->get_settings_for_display(); |
| 402 |
$toc_setting = [ |
| 403 |
'htags' => $settings['htags'], |
| 404 |
'hierarchy' => $settings['list_hierarchy'], |
| 405 |
'list_number' => $settings['list_number'], |
| 406 |
]; |
| 407 |
if (\Elementor\Plugin::$instance->editor->is_edit_mode()) { |
| 408 |
set_transient('betterdocs_toc_setting', $toc_setting); |
| 409 |
} |
| 410 |
$htags = implode(',', $settings['htags']); |
| 411 |
$shortcode = do_shortcode( |
| 412 |
"[betterdocs_toc |
| 413 |
htags='{$htags}' |
| 414 |
hierarchy='{$settings['list_hierarchy']}' |
| 415 |
list_number='{$settings['list_number']}' |
| 416 |
collapsible='{$settings['collapsible_toc_mobile']}']" |
| 417 |
); |
| 418 |
|
| 419 |
echo apply_filters('betterdocs_elementor_toc', $shortcode); |
| 420 |
} |
| 421 |
} |
| 422 |
|