button.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. <?php
  2. namespace Elementor;
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit; // Exit if accessed directly.
  5. }
  6. /**
  7. * Elementor button widget.
  8. *
  9. * Elementor widget that displays a button with the ability to control every
  10. * aspect of the button design.
  11. *
  12. * @since 1.0.0
  13. */
  14. class Widget_Button extends Widget_Base {
  15. /**
  16. * Get widget name.
  17. *
  18. * Retrieve button 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 'button';
  27. }
  28. /**
  29. * Get widget title.
  30. *
  31. * Retrieve button 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 __( 'Button', 'elementor' );
  40. }
  41. /**
  42. * Get widget icon.
  43. *
  44. * Retrieve button 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-button';
  53. }
  54. /**
  55. * Get widget categories.
  56. *
  57. * Retrieve the list of categories the button widget belongs to.
  58. *
  59. * Used to determine where to display the widget in the editor.
  60. *
  61. * @since 2.0.0
  62. * @access public
  63. *
  64. * @return array Widget categories.
  65. */
  66. public function get_categories() {
  67. return [ 'basic' ];
  68. }
  69. /**
  70. * Get button sizes.
  71. *
  72. * Retrieve an array of button sizes for the button widget.
  73. *
  74. * @since 1.0.0
  75. * @access public
  76. * @static
  77. *
  78. * @return array An array containing button sizes.
  79. */
  80. public static function get_button_sizes() {
  81. return [
  82. 'xs' => __( 'Extra Small', 'elementor' ),
  83. 'sm' => __( 'Small', 'elementor' ),
  84. 'md' => __( 'Medium', 'elementor' ),
  85. 'lg' => __( 'Large', 'elementor' ),
  86. 'xl' => __( 'Extra Large', 'elementor' ),
  87. ];
  88. }
  89. /**
  90. * Register button widget controls.
  91. *
  92. * Adds different input fields to allow the user to change and customize the widget settings.
  93. *
  94. * @since 1.0.0
  95. * @access protected
  96. */
  97. protected function _register_controls() {
  98. $this->start_controls_section(
  99. 'section_button',
  100. [
  101. 'label' => __( 'Button', 'elementor' ),
  102. ]
  103. );
  104. $this->add_control(
  105. 'button_type',
  106. [
  107. 'label' => __( 'Type', 'elementor' ),
  108. 'type' => Controls_Manager::SELECT,
  109. 'default' => '',
  110. 'options' => [
  111. '' => __( 'Default', 'elementor' ),
  112. 'info' => __( 'Info', 'elementor' ),
  113. 'success' => __( 'Success', 'elementor' ),
  114. 'warning' => __( 'Warning', 'elementor' ),
  115. 'danger' => __( 'Danger', 'elementor' ),
  116. ],
  117. 'prefix_class' => 'elementor-button-',
  118. ]
  119. );
  120. $this->add_control(
  121. 'text',
  122. [
  123. 'label' => __( 'Text', 'elementor' ),
  124. 'type' => Controls_Manager::TEXT,
  125. 'dynamic' => [
  126. 'active' => true,
  127. ],
  128. 'default' => __( 'Click here', 'elementor' ),
  129. 'placeholder' => __( 'Click here', 'elementor' ),
  130. ]
  131. );
  132. $this->add_control(
  133. 'link',
  134. [
  135. 'label' => __( 'Link', 'elementor' ),
  136. 'type' => Controls_Manager::URL,
  137. 'dynamic' => [
  138. 'active' => true,
  139. ],
  140. 'placeholder' => __( 'https://your-link.com', 'elementor' ),
  141. 'default' => [
  142. 'url' => '#',
  143. ],
  144. ]
  145. );
  146. $this->add_responsive_control(
  147. 'align',
  148. [
  149. 'label' => __( 'Alignment', 'elementor' ),
  150. 'type' => Controls_Manager::CHOOSE,
  151. 'options' => [
  152. 'left' => [
  153. 'title' => __( 'Left', 'elementor' ),
  154. 'icon' => 'fa fa-align-left',
  155. ],
  156. 'center' => [
  157. 'title' => __( 'Center', 'elementor' ),
  158. 'icon' => 'fa fa-align-center',
  159. ],
  160. 'right' => [
  161. 'title' => __( 'Right', 'elementor' ),
  162. 'icon' => 'fa fa-align-right',
  163. ],
  164. 'justify' => [
  165. 'title' => __( 'Justified', 'elementor' ),
  166. 'icon' => 'fa fa-align-justify',
  167. ],
  168. ],
  169. 'prefix_class' => 'elementor%s-align-',
  170. 'default' => '',
  171. ]
  172. );
  173. $this->add_control(
  174. 'size',
  175. [
  176. 'label' => __( 'Size', 'elementor' ),
  177. 'type' => Controls_Manager::SELECT,
  178. 'default' => 'sm',
  179. 'options' => self::get_button_sizes(),
  180. 'style_transfer' => true,
  181. ]
  182. );
  183. $this->add_control(
  184. 'icon',
  185. [
  186. 'label' => __( 'Icon', 'elementor' ),
  187. 'type' => Controls_Manager::ICON,
  188. 'label_block' => true,
  189. 'default' => '',
  190. ]
  191. );
  192. $this->add_control(
  193. 'icon_align',
  194. [
  195. 'label' => __( 'Icon Position', 'elementor' ),
  196. 'type' => Controls_Manager::SELECT,
  197. 'default' => 'left',
  198. 'options' => [
  199. 'left' => __( 'Before', 'elementor' ),
  200. 'right' => __( 'After', 'elementor' ),
  201. ],
  202. 'condition' => [
  203. 'icon!' => '',
  204. ],
  205. ]
  206. );
  207. $this->add_control(
  208. 'icon_indent',
  209. [
  210. 'label' => __( 'Icon Spacing', 'elementor' ),
  211. 'type' => Controls_Manager::SLIDER,
  212. 'range' => [
  213. 'px' => [
  214. 'max' => 50,
  215. ],
  216. ],
  217. 'condition' => [
  218. 'icon!' => '',
  219. ],
  220. 'selectors' => [
  221. '{{WRAPPER}} .elementor-button .elementor-align-icon-right' => 'margin-left: {{SIZE}}{{UNIT}};',
  222. '{{WRAPPER}} .elementor-button .elementor-align-icon-left' => 'margin-right: {{SIZE}}{{UNIT}};',
  223. ],
  224. ]
  225. );
  226. $this->add_control(
  227. 'view',
  228. [
  229. 'label' => __( 'View', 'elementor' ),
  230. 'type' => Controls_Manager::HIDDEN,
  231. 'default' => 'traditional',
  232. ]
  233. );
  234. $this->add_control(
  235. 'button_css_id',
  236. [
  237. 'label' => __( 'Button ID', 'elementor' ),
  238. 'type' => Controls_Manager::TEXT,
  239. 'default' => '',
  240. 'title' => __( 'Add your custom id WITHOUT the Pound key. e.g: my-id', 'elementor' ),
  241. 'label_block' => false,
  242. 'description' => __( 'Please make sure the ID is unique and not used elsewhere on the page this form is displayed. This field allows <code>A-z 0-9</code> & underscore chars without spaces.', 'elementor' ),
  243. 'separator' => 'before',
  244. ]
  245. );
  246. $this->end_controls_section();
  247. $this->start_controls_section(
  248. 'section_style',
  249. [
  250. 'label' => __( 'Button', 'elementor' ),
  251. 'tab' => Controls_Manager::TAB_STYLE,
  252. ]
  253. );
  254. $this->add_group_control(
  255. Group_Control_Typography::get_type(),
  256. [
  257. 'name' => 'typography',
  258. 'scheme' => Scheme_Typography::TYPOGRAPHY_4,
  259. 'selector' => '{{WRAPPER}} a.elementor-button, {{WRAPPER}} .elementor-button',
  260. ]
  261. );
  262. $this->start_controls_tabs( 'tabs_button_style' );
  263. $this->start_controls_tab(
  264. 'tab_button_normal',
  265. [
  266. 'label' => __( 'Normal', 'elementor' ),
  267. ]
  268. );
  269. $this->add_control(
  270. 'button_text_color',
  271. [
  272. 'label' => __( 'Text Color', 'elementor' ),
  273. 'type' => Controls_Manager::COLOR,
  274. 'default' => '',
  275. 'selectors' => [
  276. '{{WRAPPER}} a.elementor-button, {{WRAPPER}} .elementor-button' => 'color: {{VALUE}};',
  277. ],
  278. ]
  279. );
  280. $this->add_control(
  281. 'background_color',
  282. [
  283. 'label' => __( 'Background Color', 'elementor' ),
  284. 'type' => Controls_Manager::COLOR,
  285. 'scheme' => [
  286. 'type' => Scheme_Color::get_type(),
  287. 'value' => Scheme_Color::COLOR_4,
  288. ],
  289. 'selectors' => [
  290. '{{WRAPPER}} a.elementor-button, {{WRAPPER}} .elementor-button' => 'background-color: {{VALUE}};',
  291. ],
  292. ]
  293. );
  294. $this->end_controls_tab();
  295. $this->start_controls_tab(
  296. 'tab_button_hover',
  297. [
  298. 'label' => __( 'Hover', 'elementor' ),
  299. ]
  300. );
  301. $this->add_control(
  302. 'hover_color',
  303. [
  304. 'label' => __( 'Text Color', 'elementor' ),
  305. 'type' => Controls_Manager::COLOR,
  306. 'selectors' => [
  307. '{{WRAPPER}} a.elementor-button:hover, {{WRAPPER}} .elementor-button:hover' => 'color: {{VALUE}};',
  308. ],
  309. ]
  310. );
  311. $this->add_control(
  312. 'button_background_hover_color',
  313. [
  314. 'label' => __( 'Background Color', 'elementor' ),
  315. 'type' => Controls_Manager::COLOR,
  316. 'selectors' => [
  317. '{{WRAPPER}} a.elementor-button:hover, {{WRAPPER}} .elementor-button:hover' => 'background-color: {{VALUE}};',
  318. ],
  319. ]
  320. );
  321. $this->add_control(
  322. 'button_hover_border_color',
  323. [
  324. 'label' => __( 'Border Color', 'elementor' ),
  325. 'type' => Controls_Manager::COLOR,
  326. 'condition' => [
  327. 'border_border!' => '',
  328. ],
  329. 'selectors' => [
  330. '{{WRAPPER}} a.elementor-button:hover, {{WRAPPER}} .elementor-button:hover' => 'border-color: {{VALUE}};',
  331. ],
  332. ]
  333. );
  334. $this->add_control(
  335. 'hover_animation',
  336. [
  337. 'label' => __( 'Hover Animation', 'elementor' ),
  338. 'type' => Controls_Manager::HOVER_ANIMATION,
  339. ]
  340. );
  341. $this->end_controls_tab();
  342. $this->end_controls_tabs();
  343. $this->add_group_control(
  344. Group_Control_Border::get_type(),
  345. [
  346. 'name' => 'border',
  347. 'placeholder' => '1px',
  348. 'default' => '1px',
  349. 'selector' => '{{WRAPPER}} .elementor-button',
  350. 'separator' => 'before',
  351. ]
  352. );
  353. $this->add_control(
  354. 'border_radius',
  355. [
  356. 'label' => __( 'Border Radius', 'elementor' ),
  357. 'type' => Controls_Manager::DIMENSIONS,
  358. 'size_units' => [ 'px', '%' ],
  359. 'selectors' => [
  360. '{{WRAPPER}} a.elementor-button, {{WRAPPER}} .elementor-button' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
  361. ],
  362. ]
  363. );
  364. $this->add_group_control(
  365. Group_Control_Box_Shadow::get_type(),
  366. [
  367. 'name' => 'button_box_shadow',
  368. 'selector' => '{{WRAPPER}} .elementor-button',
  369. ]
  370. );
  371. $this->add_responsive_control(
  372. 'text_padding',
  373. [
  374. 'label' => __( 'Padding', 'elementor' ),
  375. 'type' => Controls_Manager::DIMENSIONS,
  376. 'size_units' => [ 'px', 'em', '%' ],
  377. 'selectors' => [
  378. '{{WRAPPER}} a.elementor-button, {{WRAPPER}} .elementor-button' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
  379. ],
  380. 'separator' => 'before',
  381. ]
  382. );
  383. $this->end_controls_section();
  384. }
  385. /**
  386. * Render button widget output on the frontend.
  387. *
  388. * Written in PHP and used to generate the final HTML.
  389. *
  390. * @since 1.0.0
  391. * @access protected
  392. */
  393. protected function render() {
  394. $settings = $this->get_settings_for_display();
  395. $this->add_render_attribute( 'wrapper', 'class', 'elementor-button-wrapper' );
  396. if ( ! empty( $settings['link']['url'] ) ) {
  397. $this->add_render_attribute( 'button', 'href', $settings['link']['url'] );
  398. $this->add_render_attribute( 'button', 'class', 'elementor-button-link' );
  399. if ( $settings['link']['is_external'] ) {
  400. $this->add_render_attribute( 'button', 'target', '_blank' );
  401. }
  402. if ( $settings['link']['nofollow'] ) {
  403. $this->add_render_attribute( 'button', 'rel', 'nofollow' );
  404. }
  405. }
  406. $this->add_render_attribute( 'button', 'class', 'elementor-button' );
  407. $this->add_render_attribute( 'button', 'role', 'button' );
  408. if ( ! empty( $settings['button_css_id'] ) ) {
  409. $this->add_render_attribute( 'button', 'id', $settings['button_css_id'] );
  410. }
  411. if ( ! empty( $settings['size'] ) ) {
  412. $this->add_render_attribute( 'button', 'class', 'elementor-size-' . $settings['size'] );
  413. }
  414. if ( $settings['hover_animation'] ) {
  415. $this->add_render_attribute( 'button', 'class', 'elementor-animation-' . $settings['hover_animation'] );
  416. }
  417. ?>
  418. <div <?php echo $this->get_render_attribute_string( 'wrapper' ); ?>>
  419. <a <?php echo $this->get_render_attribute_string( 'button' ); ?>>
  420. <?php $this->render_text(); ?>
  421. </a>
  422. </div>
  423. <?php
  424. }
  425. /**
  426. * Render button widget output in the editor.
  427. *
  428. * Written as a Backbone JavaScript template and used to generate the live preview.
  429. *
  430. * @since 1.0.0
  431. * @access protected
  432. */
  433. protected function _content_template() {
  434. ?>
  435. <#
  436. view.addRenderAttribute( 'text', 'class', 'elementor-button-text' );
  437. view.addInlineEditingAttributes( 'text', 'none' );
  438. #>
  439. <div class="elementor-button-wrapper">
  440. <a id="{{ settings.button_css_id }}" class="elementor-button elementor-size-{{ settings.size }} elementor-animation-{{ settings.hover_animation }}" href="{{ settings.link.url }}" role="button">
  441. <span class="elementor-button-content-wrapper">
  442. <# if ( settings.icon ) { #>
  443. <span class="elementor-button-icon elementor-align-icon-{{ settings.icon_align }}">
  444. <i class="{{ settings.icon }}" aria-hidden="true"></i>
  445. </span>
  446. <# } #>
  447. <span {{{ view.getRenderAttributeString( 'text' ) }}}>{{{ settings.text }}}</span>
  448. </span>
  449. </a>
  450. </div>
  451. <?php
  452. }
  453. /**
  454. * Render button text.
  455. *
  456. * Render button widget text.
  457. *
  458. * @since 1.5.0
  459. * @access protected
  460. */
  461. protected function render_text() {
  462. $settings = $this->get_settings_for_display();
  463. $this->add_render_attribute( [
  464. 'content-wrapper' => [
  465. 'class' => 'elementor-button-content-wrapper',
  466. ],
  467. 'icon-align' => [
  468. 'class' => [
  469. 'elementor-button-icon',
  470. 'elementor-align-icon-' . $settings['icon_align'],
  471. ],
  472. ],
  473. 'text' => [
  474. 'class' => 'elementor-button-text',
  475. ],
  476. ] );
  477. $this->add_inline_editing_attributes( 'text', 'none' );
  478. ?>
  479. <span <?php echo $this->get_render_attribute_string( 'content-wrapper' ); ?>>
  480. <?php if ( ! empty( $settings['icon'] ) ) : ?>
  481. <span <?php echo $this->get_render_attribute_string( 'icon-align' ); ?>>
  482. <i class="<?php echo esc_attr( $settings['icon'] ); ?>" aria-hidden="true"></i>
  483. </span>
  484. <?php endif; ?>
  485. <span <?php echo $this->get_render_attribute_string( 'text' ); ?>><?php echo $settings['text']; ?></span>
  486. </span>
  487. <?php
  488. }
  489. }