toggle.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. <?php
  2. namespace Elementor;
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit; // Exit if accessed directly.
  5. }
  6. /**
  7. * Elementor toggle widget.
  8. *
  9. * Elementor widget that displays a collapsible display of content in an toggle
  10. * style, allowing the user to open multiple items.
  11. *
  12. * @since 1.0.0
  13. */
  14. class Widget_Toggle extends Widget_Base {
  15. /**
  16. * Get widget name.
  17. *
  18. * Retrieve toggle widget name.
  19. *
  20. * @since 1.0.0
  21. * @access public
  22. *
  23. * @return string Widget name.
  24. */
  25. public function get_name() {
  26. return 'toggle';
  27. }
  28. /**
  29. * Get widget title.
  30. *
  31. * Retrieve toggle widget title.
  32. *
  33. * @since 1.0.0
  34. * @access public
  35. *
  36. * @return string Widget title.
  37. */
  38. public function get_title() {
  39. return __( 'Toggle', 'elementor' );
  40. }
  41. /**
  42. * Get widget icon.
  43. *
  44. * Retrieve toggle widget icon.
  45. *
  46. * @since 1.0.0
  47. * @access public
  48. *
  49. * @return string Widget icon.
  50. */
  51. public function get_icon() {
  52. return 'eicon-toggle';
  53. }
  54. /**
  55. * Get widget keywords.
  56. *
  57. * Retrieve the list of keywords the widget belongs to.
  58. *
  59. * @since 2.1.0
  60. * @access public
  61. *
  62. * @return array Widget keywords.
  63. */
  64. public function get_keywords() {
  65. return [ 'tabs', 'accordion', 'toggle' ];
  66. }
  67. /**
  68. * Register toggle widget controls.
  69. *
  70. * Adds different input fields to allow the user to change and customize the widget settings.
  71. *
  72. * @since 1.0.0
  73. * @access protected
  74. */
  75. protected function _register_controls() {
  76. $this->start_controls_section(
  77. 'section_toggle',
  78. [
  79. 'label' => __( 'Toggle', 'elementor' ),
  80. ]
  81. );
  82. $repeater = new Repeater();
  83. $repeater->add_control(
  84. 'tab_title',
  85. [
  86. 'label' => __( 'Title & Content', 'elementor' ),
  87. 'type' => Controls_Manager::TEXT,
  88. 'default' => __( 'Toggle Title', 'elementor' ),
  89. 'label_block' => true,
  90. ]
  91. );
  92. $repeater->add_control(
  93. 'tab_content',
  94. [
  95. 'label' => __( 'Content', 'elementor' ),
  96. 'type' => Controls_Manager::WYSIWYG,
  97. 'default' => __( 'Toggle Content', 'elementor' ),
  98. 'show_label' => false,
  99. ]
  100. );
  101. $this->add_control(
  102. 'tabs',
  103. [
  104. 'label' => __( 'Toggle Items', 'elementor' ),
  105. 'type' => Controls_Manager::REPEATER,
  106. 'fields' => $repeater->get_controls(),
  107. 'default' => [
  108. [
  109. 'tab_title' => __( 'Toggle #1', 'elementor' ),
  110. 'tab_content' => __( 'Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.', 'elementor' ),
  111. ],
  112. [
  113. 'tab_title' => __( 'Toggle #2', 'elementor' ),
  114. 'tab_content' => __( 'Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.', 'elementor' ),
  115. ],
  116. ],
  117. 'title_field' => '{{{ tab_title }}}',
  118. ]
  119. );
  120. $this->add_control(
  121. 'view',
  122. [
  123. 'label' => __( 'View', 'elementor' ),
  124. 'type' => Controls_Manager::HIDDEN,
  125. 'default' => 'traditional',
  126. ]
  127. );
  128. $this->add_control(
  129. 'icon',
  130. [
  131. 'label' => __( 'Icon', 'elementor' ),
  132. 'type' => Controls_Manager::ICON,
  133. 'default' => is_rtl() ? 'fa fa-caret-left' : 'fa fa-caret-right',
  134. 'separator' => 'before',
  135. ]
  136. );
  137. $this->add_control(
  138. 'icon_active',
  139. [
  140. 'label' => __( 'Active Icon', 'elementor' ),
  141. 'type' => Controls_Manager::ICON,
  142. 'default' => 'fa fa-caret-up',
  143. 'condition' => [
  144. 'icon!' => '',
  145. ],
  146. ]
  147. );
  148. $this->add_control(
  149. 'title_html_tag',
  150. [
  151. 'label' => __( 'Title HTML Tag', 'elementor' ),
  152. 'type' => Controls_Manager::SELECT,
  153. 'options' => [
  154. 'h1' => 'H1',
  155. 'h2' => 'H2',
  156. 'h3' => 'H3',
  157. 'h4' => 'H4',
  158. 'h5' => 'H5',
  159. 'h6' => 'H6',
  160. 'div' => 'div',
  161. ],
  162. 'default' => 'div',
  163. 'separator' => 'before',
  164. ]
  165. );
  166. $this->end_controls_section();
  167. $this->start_controls_section(
  168. 'section_toggle_style',
  169. [
  170. 'label' => __( 'Toggle', 'elementor' ),
  171. 'tab' => Controls_Manager::TAB_STYLE,
  172. ]
  173. );
  174. $this->add_control(
  175. 'border_width',
  176. [
  177. 'label' => __( 'Border Width', 'elementor' ),
  178. 'type' => Controls_Manager::SLIDER,
  179. 'range' => [
  180. 'px' => [
  181. 'min' => 0,
  182. 'max' => 10,
  183. ],
  184. ],
  185. 'selectors' => [
  186. '{{WRAPPER}} .elementor-toggle .elementor-tab-title' => 'border-width: {{SIZE}}{{UNIT}};',
  187. '{{WRAPPER}} .elementor-toggle .elementor-tab-content' => 'border-width: {{SIZE}}{{UNIT}};',
  188. ],
  189. ]
  190. );
  191. $this->add_control(
  192. 'border_color',
  193. [
  194. 'label' => __( 'Border Color', 'elementor' ),
  195. 'type' => Controls_Manager::COLOR,
  196. 'selectors' => [
  197. '{{WRAPPER}} .elementor-toggle .elementor-tab-content' => 'border-bottom-color: {{VALUE}};',
  198. '{{WRAPPER}} .elementor-toggle .elementor-tab-title' => 'border-color: {{VALUE}};',
  199. ],
  200. ]
  201. );
  202. $this->add_responsive_control(
  203. 'space_between',
  204. [
  205. 'label' => __( 'Space Between', 'elementor' ),
  206. 'type' => Controls_Manager::SLIDER,
  207. 'range' => [
  208. 'px' => [
  209. 'min' => 0,
  210. 'max' => 100,
  211. ],
  212. ],
  213. 'selectors' => [
  214. '{{WRAPPER}} .elementor-toggle .elementor-toggle-item:not(:last-child)' => 'margin-bottom: {{SIZE}}{{UNIT}}',
  215. ],
  216. ]
  217. );
  218. $this->add_group_control(
  219. Group_Control_Box_Shadow::get_type(),
  220. [
  221. 'name' => 'box_shadow',
  222. 'selector' => '{{WRAPPER}} .elementor-toggle .elementor-toggle-item',
  223. ]
  224. );
  225. $this->end_controls_section();
  226. $this->start_controls_section(
  227. 'section_toggle_style_title',
  228. [
  229. 'label' => __( 'Title', 'elementor' ),
  230. 'tab' => Controls_Manager::TAB_STYLE,
  231. ]
  232. );
  233. $this->add_control(
  234. 'title_background',
  235. [
  236. 'label' => __( 'Background', 'elementor' ),
  237. 'type' => Controls_Manager::COLOR,
  238. 'selectors' => [
  239. '{{WRAPPER}} .elementor-toggle .elementor-tab-title' => 'background-color: {{VALUE}};',
  240. ],
  241. ]
  242. );
  243. $this->add_control(
  244. 'title_color',
  245. [
  246. 'label' => __( 'Color', 'elementor' ),
  247. 'type' => Controls_Manager::COLOR,
  248. 'selectors' => [
  249. '{{WRAPPER}} .elementor-toggle .elementor-tab-title' => 'color: {{VALUE}};',
  250. ],
  251. 'scheme' => [
  252. 'type' => Scheme_Color::get_type(),
  253. 'value' => Scheme_Color::COLOR_1,
  254. ],
  255. ]
  256. );
  257. $this->add_control(
  258. 'tab_active_color',
  259. [
  260. 'label' => __( 'Active Color', 'elementor' ),
  261. 'type' => Controls_Manager::COLOR,
  262. 'selectors' => [
  263. '{{WRAPPER}} .elementor-toggle .elementor-tab-title.elementor-active' => 'color: {{VALUE}};',
  264. ],
  265. 'scheme' => [
  266. 'type' => Scheme_Color::get_type(),
  267. 'value' => Scheme_Color::COLOR_4,
  268. ],
  269. ]
  270. );
  271. $this->add_group_control(
  272. Group_Control_Typography::get_type(),
  273. [
  274. 'name' => 'title_typography',
  275. 'selector' => '{{WRAPPER}} .elementor-toggle .elementor-tab-title',
  276. 'scheme' => Scheme_Typography::TYPOGRAPHY_1,
  277. ]
  278. );
  279. $this->add_responsive_control(
  280. 'title_padding',
  281. [
  282. 'label' => __( 'Padding', 'elementor' ),
  283. 'type' => Controls_Manager::DIMENSIONS,
  284. 'size_units' => [ 'px', 'em', '%' ],
  285. 'selectors' => [
  286. '{{WRAPPER}} .elementor-toggle .elementor-tab-title' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
  287. ],
  288. ]
  289. );
  290. $this->end_controls_section();
  291. $this->start_controls_section(
  292. 'section_toggle_style_icon',
  293. [
  294. 'label' => __( 'Icon', 'elementor' ),
  295. 'tab' => Controls_Manager::TAB_STYLE,
  296. 'condition' => [
  297. 'icon!' => '',
  298. ],
  299. ]
  300. );
  301. $this->add_control(
  302. 'icon_align',
  303. [
  304. 'label' => __( 'Alignment', 'elementor' ),
  305. 'type' => Controls_Manager::CHOOSE,
  306. 'options' => [
  307. 'left' => [
  308. 'title' => __( 'Start', 'elementor' ),
  309. 'icon' => 'eicon-h-align-left',
  310. ],
  311. 'right' => [
  312. 'title' => __( 'End', 'elementor' ),
  313. 'icon' => 'eicon-h-align-right',
  314. ],
  315. ],
  316. 'default' => is_rtl() ? 'right' : 'left',
  317. 'toggle' => false,
  318. 'label_block' => false,
  319. 'condition' => [
  320. 'icon!' => '',
  321. ],
  322. ]
  323. );
  324. $this->add_control(
  325. 'icon_color',
  326. [
  327. 'label' => __( 'Color', 'elementor' ),
  328. 'type' => Controls_Manager::COLOR,
  329. 'selectors' => [
  330. '{{WRAPPER}} .elementor-toggle .elementor-tab-title .elementor-toggle-icon .fa:before' => 'color: {{VALUE}};',
  331. ],
  332. 'condition' => [
  333. 'icon!' => '',
  334. ],
  335. ]
  336. );
  337. $this->add_control(
  338. 'icon_active_color',
  339. [
  340. 'label' => __( 'Active Color', 'elementor' ),
  341. 'type' => Controls_Manager::COLOR,
  342. 'selectors' => [
  343. '{{WRAPPER}} .elementor-toggle .elementor-tab-title.elementor-active .elementor-toggle-icon .fa:before' => 'color: {{VALUE}};',
  344. ],
  345. 'condition' => [
  346. 'icon!' => '',
  347. ],
  348. ]
  349. );
  350. $this->add_responsive_control(
  351. 'icon_space',
  352. [
  353. 'label' => __( 'Spacing', 'elementor' ),
  354. 'type' => Controls_Manager::SLIDER,
  355. 'range' => [
  356. 'px' => [
  357. 'min' => 0,
  358. 'max' => 100,
  359. ],
  360. ],
  361. 'selectors' => [
  362. '{{WRAPPER}} .elementor-toggle .elementor-toggle-icon.elementor-toggle-icon-left' => 'margin-right: {{SIZE}}{{UNIT}};',
  363. '{{WRAPPER}} .elementor-toggle .elementor-toggle-icon.elementor-toggle-icon-right' => 'margin-left: {{SIZE}}{{UNIT}};',
  364. ],
  365. 'condition' => [
  366. 'icon!' => '',
  367. ],
  368. ]
  369. );
  370. $this->end_controls_section();
  371. $this->start_controls_section(
  372. 'section_toggle_style_content',
  373. [
  374. 'label' => __( 'Content', 'elementor' ),
  375. 'tab' => Controls_Manager::TAB_STYLE,
  376. ]
  377. );
  378. $this->add_control(
  379. 'content_background_color',
  380. [
  381. 'label' => __( 'Background', 'elementor' ),
  382. 'type' => Controls_Manager::COLOR,
  383. 'selectors' => [
  384. '{{WRAPPER}} .elementor-toggle .elementor-tab-content' => 'background-color: {{VALUE}};',
  385. ],
  386. ]
  387. );
  388. $this->add_control(
  389. 'content_color',
  390. [
  391. 'label' => __( 'Color', 'elementor' ),
  392. 'type' => Controls_Manager::COLOR,
  393. 'selectors' => [
  394. '{{WRAPPER}} .elementor-toggle .elementor-tab-content' => 'color: {{VALUE}};',
  395. ],
  396. 'scheme' => [
  397. 'type' => Scheme_Color::get_type(),
  398. 'value' => Scheme_Color::COLOR_3,
  399. ],
  400. ]
  401. );
  402. $this->add_group_control(
  403. Group_Control_Typography::get_type(),
  404. [
  405. 'name' => 'content_typography',
  406. 'selector' => '{{WRAPPER}} .elementor-toggle .elementor-tab-content',
  407. 'scheme' => Scheme_Typography::TYPOGRAPHY_3,
  408. ]
  409. );
  410. $this->add_responsive_control(
  411. 'content_padding',
  412. [
  413. 'label' => __( 'Padding', 'elementor' ),
  414. 'type' => Controls_Manager::DIMENSIONS,
  415. 'size_units' => [ 'px', 'em', '%' ],
  416. 'selectors' => [
  417. '{{WRAPPER}} .elementor-toggle .elementor-tab-content' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
  418. ],
  419. ]
  420. );
  421. $this->end_controls_section();
  422. }
  423. /**
  424. * Render toggle widget output on the frontend.
  425. *
  426. * Written in PHP and used to generate the final HTML.
  427. *
  428. * @since 1.0.0
  429. * @access protected
  430. */
  431. protected function render() {
  432. $settings = $this->get_settings_for_display();
  433. $id_int = substr( $this->get_id_int(), 0, 3 );
  434. ?>
  435. <div class="elementor-toggle" role="tablist">
  436. <?php
  437. foreach ( $settings['tabs'] as $index => $item ) :
  438. $tab_count = $index + 1;
  439. $tab_title_setting_key = $this->get_repeater_setting_key( 'tab_title', 'tabs', $index );
  440. $tab_content_setting_key = $this->get_repeater_setting_key( 'tab_content', 'tabs', $index );
  441. $this->add_render_attribute( $tab_title_setting_key, [
  442. 'id' => 'elementor-tab-title-' . $id_int . $tab_count,
  443. 'class' => [ 'elementor-tab-title' ],
  444. 'tabindex' => $id_int . $tab_count,
  445. 'data-tab' => $tab_count,
  446. 'role' => 'tab',
  447. 'aria-controls' => 'elementor-tab-content-' . $id_int . $tab_count,
  448. ] );
  449. $this->add_render_attribute( $tab_content_setting_key, [
  450. 'id' => 'elementor-tab-content-' . $id_int . $tab_count,
  451. 'class' => [ 'elementor-tab-content', 'elementor-clearfix' ],
  452. 'data-tab' => $tab_count,
  453. 'role' => 'tabpanel',
  454. 'aria-labelledby' => 'elementor-tab-title-' . $id_int . $tab_count,
  455. ] );
  456. $this->add_inline_editing_attributes( $tab_content_setting_key, 'advanced' );
  457. ?>
  458. <div class="elementor-toggle-item">
  459. <<?php echo esc_html( $settings['title_html_tag'] ); ?> <?php echo $this->get_render_attribute_string( $tab_title_setting_key ); ?>>
  460. <?php if ( $settings['icon'] ) : ?>
  461. <span class="elementor-toggle-icon elementor-toggle-icon-<?php echo esc_attr( $settings['icon_align'] ); ?>" aria-hidden="true">
  462. <i class="elementor-toggle-icon-closed <?php echo esc_attr( $settings['icon'] ); ?>"></i>
  463. <i class="elementor-toggle-icon-opened <?php echo esc_attr( $settings['icon_active'] ); ?>"></i>
  464. </span>
  465. <?php endif; ?>
  466. <?php echo $item['tab_title']; ?>
  467. </<?php echo esc_html( $settings['title_html_tag'] ); ?>>
  468. <div <?php echo $this->get_render_attribute_string( $tab_content_setting_key ); ?>><?php echo $this->parse_text_editor( $item['tab_content'] ); ?></div>
  469. </div>
  470. <?php endforeach; ?>
  471. </div>
  472. <?php
  473. }
  474. /**
  475. * Render toggle widget output in the editor.
  476. *
  477. * Written as a Backbone JavaScript template and used to generate the live preview.
  478. *
  479. * @since 1.0.0
  480. * @access protected
  481. */
  482. protected function _content_template() {
  483. ?>
  484. <div class="elementor-toggle" role="tablist">
  485. <#
  486. if ( settings.tabs ) {
  487. var tabindex = view.getIDInt().toString().substr( 0, 3 );
  488. _.each( settings.tabs, function( item, index ) {
  489. var tabCount = index + 1,
  490. tabTitleKey = view.getRepeaterSettingKey( 'tab_title', 'tabs', index ),
  491. tabContentKey = view.getRepeaterSettingKey( 'tab_content', 'tabs', index );
  492. view.addRenderAttribute( tabTitleKey, {
  493. 'id': 'elementor-tab-title-' + tabindex + tabCount,
  494. 'class': [ 'elementor-tab-title' ],
  495. 'tabindex': tabindex + tabCount,
  496. 'data-tab': tabCount,
  497. 'role': 'tab',
  498. 'aria-controls': 'elementor-tab-content-' + tabindex + tabCount
  499. } );
  500. view.addRenderAttribute( tabContentKey, {
  501. 'id': 'elementor-tab-content-' + tabindex + tabCount,
  502. 'class': [ 'elementor-tab-content', 'elementor-clearfix' ],
  503. 'data-tab': tabCount,
  504. 'role': 'tabpanel',
  505. 'aria-labelledby': 'elementor-tab-title-' + tabindex + tabCount
  506. } );
  507. view.addInlineEditingAttributes( tabContentKey, 'advanced' );
  508. #>
  509. <div class="elementor-toggle-item">
  510. <{{{ settings.title_html_tag }}} {{{ view.getRenderAttributeString( tabTitleKey ) }}}>
  511. <# if ( settings.icon ) { #>
  512. <span class="elementor-toggle-icon elementor-toggle-icon-{{ settings.icon_align }}" aria-hidden="true">
  513. <i class="elementor-toggle-icon-closed {{ settings.icon }}"></i>
  514. <i class="elementor-toggle-icon-opened {{ settings.icon_active }}"></i>
  515. </span>
  516. <# } #>
  517. {{{ item.tab_title }}}
  518. </{{{ settings.title_html_tag }}}>
  519. <div {{{ view.getRenderAttributeString( tabContentKey ) }}}>{{{ item.tab_content }}}</div>
  520. </div>
  521. <#
  522. } );
  523. } #>
  524. </div>
  525. <?php
  526. }
  527. }