PluginProbe
Elementor Website Builder – more than just a page builder / 3.24.4
Elementor Website Builder – more than just a page builder v3.24.4
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / includes / widgets / traits / button-trait.php

button-trait.php in Elementor Website Builder – more than just a page builder 3.24.4, at includes/widgets/traits/button-trait.php

682 lines 19.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Includes\Widgets\Traits;
3
4 use Elementor\Controls_Manager;
5 use Elementor\Core\Kits\Documents\Tabs\Global_Colors;
6 use Elementor\Core\Kits\Documents\Tabs\Global_Typography;
7 use Elementor\Group_Control_Background;
8 use Elementor\Group_Control_Border;
9 use Elementor\Group_Control_Box_Shadow;
10 use Elementor\Group_Control_Text_Shadow;
11 use Elementor\Group_Control_Typography;
12 use Elementor\Icons_Manager;
13 use Elementor\Widget_Base;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit; // Exit if accessed directly
17 }
18
19 trait Button_Trait {
20 /**
21 * Get button sizes.
22 *
23 * Retrieve an array of button sizes for the button widget.
24 *
25 * @since 3.4.0
26 * @access public
27 * @static
28 *
29 * @return array An array containing button sizes.
30 */
31 public static function get_button_sizes() {
32 return [
33 'xs' => esc_html__( 'Extra Small', 'elementor' ),
34 'sm' => esc_html__( 'Small', 'elementor' ),
35 'md' => esc_html__( 'Medium', 'elementor' ),
36 'lg' => esc_html__( 'Large', 'elementor' ),
37 'xl' => esc_html__( 'Extra Large', 'elementor' ),
38 ];
39 }
40
41 /**
42 * @since 3.4.0
43 *
44 * @param array $args {
45 * An array of values for the button adjustments.
46 *
47 * @type array $section_condition Set of conditions to hide the controls.
48 * @type string $button_text Text contained in button.
49 * @type string $text_control_label Name for the label of the text control.
50 * @type array $icon_exclude_inline_options Set of icon types to exclude from icon controls.
51 * }
52 */
53 protected function register_button_content_controls( $args = [] ) {
54 $default_args = [
55 'section_condition' => [],
56 'button_default_text' => esc_html__( 'Click here', 'elementor' ),
57 'text_control_label' => esc_html__( 'Text', 'elementor' ),
58 'icon_exclude_inline_options' => [],
59 ];
60
61 $args = wp_parse_args( $args, $default_args );
62
63 $this->add_control(
64 'button_type',
65 [
66 'label' => esc_html__( 'Type', 'elementor' ),
67 'type' => Controls_Manager::SELECT,
68 'default' => '',
69 'options' => [
70 '' => esc_html__( 'Default', 'elementor' ),
71 'info' => esc_html__( 'Info', 'elementor' ),
72 'success' => esc_html__( 'Success', 'elementor' ),
73 'warning' => esc_html__( 'Warning', 'elementor' ),
74 'danger' => esc_html__( 'Danger', 'elementor' ),
75 ],
76 'prefix_class' => 'elementor-button-',
77 'condition' => $args['section_condition'],
78 ]
79 );
80
81 $this->add_control(
82 'text',
83 [
84 'label' => $args['text_control_label'],
85 'type' => Controls_Manager::TEXT,
86 'dynamic' => [
87 'active' => true,
88 ],
89 'default' => $args['button_default_text'],
90 'placeholder' => $args['button_default_text'],
91 'condition' => $args['section_condition'],
92 ]
93 );
94
95 $this->add_control(
96 'link',
97 [
98 'label' => esc_html__( 'Link', 'elementor' ),
99 'type' => Controls_Manager::URL,
100 'dynamic' => [
101 'active' => true,
102 ],
103 'default' => [
104 'url' => '#',
105 ],
106 'condition' => $args['section_condition'],
107 ]
108 );
109
110 $this->add_control(
111 'size',
112 [
113 'label' => esc_html__( 'Size', 'elementor' ),
114 'type' => Controls_Manager::SELECT,
115 'default' => 'sm',
116 'options' => self::get_button_sizes(),
117 'style_transfer' => true,
118 'condition' => array_merge( $args['section_condition'], [ 'size[value]!' => 'sm' ] ), // a workaround to hide the control, unless it's in use (not default).
119 ]
120 );
121
122 $this->add_control(
123 'selected_icon',
124 [
125 'label' => esc_html__( 'Icon', 'elementor' ),
126 'type' => Controls_Manager::ICONS,
127 'fa4compatibility' => 'icon',
128 'skin' => 'inline',
129 'label_block' => false,
130 'condition' => $args['section_condition'],
131 'icon_exclude_inline_options' => $args['icon_exclude_inline_options'],
132 ]
133 );
134
135 $start = is_rtl() ? 'right' : 'left';
136 $end = is_rtl() ? 'left' : 'right';
137
138 $this->add_control(
139 'icon_align',
140 [
141 'label' => esc_html__( 'Icon Position', 'elementor' ),
142 'type' => Controls_Manager::CHOOSE,
143 'default' => is_rtl() ? 'row-reverse' : 'row',
144 'options' => [
145 'row' => [
146 'title' => esc_html__( 'Start', 'elementor' ),
147 'icon' => "eicon-h-align-{$start}",
148 ],
149 'row-reverse' => [
150 'title' => esc_html__( 'End', 'elementor' ),
151 'icon' => "eicon-h-align-{$end}",
152 ],
153 ],
154 'selectors_dictionary' => [
155 'left' => is_rtl() ? 'row-reverse' : 'row',
156 'right' => is_rtl() ? 'row' : 'row-reverse',
157 ],
158 'selectors' => [
159 '{{WRAPPER}} .elementor-button-content-wrapper' => 'flex-direction: {{VALUE}};',
160 ],
161 'condition' => array_merge(
162 $args['section_condition'],
163 [
164 'text!' => '',
165 'selected_icon[value]!' => '',
166 ]
167 ),
168 ]
169 );
170
171 $this->add_control(
172 'icon_indent',
173 [
174 'label' => esc_html__( 'Icon Spacing', 'elementor' ),
175 'type' => Controls_Manager::SLIDER,
176 'size_units' => [ 'px', 'em', 'rem', 'custom' ],
177 'range' => [
178 'px' => [
179 'max' => 50,
180 ],
181 'em' => [
182 'max' => 5,
183 ],
184 'rem' => [
185 'max' => 5,
186 ],
187 ],
188 'selectors' => [
189 '{{WRAPPER}} .elementor-button .elementor-button-content-wrapper' => 'gap: {{SIZE}}{{UNIT}};',
190 ],
191 'condition' => array_merge(
192 $args['section_condition'],
193 [
194 'text!' => '',
195 'selected_icon[value]!' => '',
196 ]
197 ),
198 ]
199 );
200
201 $this->add_control(
202 'button_css_id',
203 [
204 'label' => esc_html__( 'Button ID', 'elementor' ),
205 'type' => Controls_Manager::TEXT,
206 'dynamic' => [
207 'active' => true,
208 ],
209 'ai' => [
210 'active' => false,
211 ],
212 'default' => '',
213 'title' => esc_html__( 'Add your custom id WITHOUT the Pound key. e.g: my-id', 'elementor' ),
214 'description' => sprintf(
215 esc_html__( 'Please make sure the ID is unique and not used elsewhere on the page this form is displayed. This field allows %1$sA-z 0-9%2$s & underscore chars without spaces.', 'elementor' ),
216 '<code>',
217 '</code>'
218 ),
219 'separator' => 'before',
220 'condition' => $args['section_condition'],
221 ]
222 );
223 }
224
225 /**
226 * @since 3.4.0
227 *
228 * @param array $args {
229 * An array of values for the button adjustments.
230 *
231 * @type array $section_condition Set of conditions to hide the controls.
232 * @type string $alignment_default Default position for the button.
233 * @type string $alignment_control_prefix_class Prefix class name for the button position control.
234 * @type string $content_alignment_default Default alignment for the button content.
235 * }
236 */
237 protected function register_button_style_controls( $args = [] ) {
238 $default_args = [
239 'section_condition' => [],
240 'alignment_default' => '',
241 'alignment_control_prefix_class' => 'elementor%s-align-',
242 'content_alignment_default' => '',
243 ];
244
245 $args = wp_parse_args( $args, $default_args );
246
247 $this->add_responsive_control(
248 'align',
249 [
250 'label' => esc_html__( 'Position', 'elementor' ),
251 'type' => Controls_Manager::CHOOSE,
252 'options' => [
253 'left' => [
254 'title' => esc_html__( 'Left', 'elementor' ),
255 'icon' => 'eicon-h-align-left',
256 ],
257 'center' => [
258 'title' => esc_html__( 'Center', 'elementor' ),
259 'icon' => 'eicon-h-align-center',
260 ],
261 'right' => [
262 'title' => esc_html__( 'Right', 'elementor' ),
263 'icon' => 'eicon-h-align-right',
264 ],
265 'justify' => [
266 'title' => esc_html__( 'Stretch', 'elementor' ),
267 'icon' => 'eicon-h-align-stretch',
268 ],
269 ],
270 'prefix_class' => $args['alignment_control_prefix_class'],
271 'default' => $args['alignment_default'],
272 'condition' => $args['section_condition'],
273 ]
274 );
275
276 $start = is_rtl() ? 'right' : 'left';
277 $end = is_rtl() ? 'left' : 'right';
278
279 $this->add_responsive_control(
280 'content_align',
281 [
282 'label' => esc_html__( 'Alignment', 'elementor' ),
283 'type' => Controls_Manager::CHOOSE,
284 'options' => [
285 'start' => [
286 'title' => esc_html__( 'Start', 'elementor' ),
287 'icon' => "eicon-text-align-{$start}",
288 ],
289 'center' => [
290 'title' => esc_html__( 'Center', 'elementor' ),
291 'icon' => 'eicon-text-align-center',
292 ],
293 'end' => [
294 'title' => esc_html__( 'End', 'elementor' ),
295 'icon' => "eicon-text-align-{$end}",
296 ],
297 'space-between' => [
298 'title' => esc_html__( 'Space between', 'elementor' ),
299 'icon' => 'eicon-text-align-justify',
300 ],
301 ],
302 'default' => $args['content_alignment_default'],
303 'selectors' => [
304 '{{WRAPPER}} .elementor-button .elementor-button-content-wrapper' => 'justify-content: {{VALUE}};',
305 ],
306 'condition' => array_merge( $args['section_condition'], [ 'align' => 'justify' ] ),
307 ]
308 );
309
310 $this->add_group_control(
311 Group_Control_Typography::get_type(),
312 [
313 'name' => 'typography',
314 'global' => [
315 'default' => Global_Typography::TYPOGRAPHY_ACCENT,
316 ],
317 'selector' => '{{WRAPPER}} .elementor-button',
318 'condition' => $args['section_condition'],
319 ]
320 );
321
322 $this->add_group_control(
323 Group_Control_Text_Shadow::get_type(),
324 [
325 'name' => 'text_shadow',
326 'selector' => '{{WRAPPER}} .elementor-button',
327 'condition' => $args['section_condition'],
328 ]
329 );
330
331 $this->start_controls_tabs( 'tabs_button_style', [
332 'condition' => $args['section_condition'],
333 ] );
334
335 $this->start_controls_tab(
336 'tab_button_normal',
337 [
338 'label' => esc_html__( 'Normal', 'elementor' ),
339 'condition' => $args['section_condition'],
340 ]
341 );
342
343 $this->add_control(
344 'button_text_color',
345 [
346 'label' => esc_html__( 'Text Color', 'elementor' ),
347 'type' => Controls_Manager::COLOR,
348 'default' => '',
349 'selectors' => [
350 '{{WRAPPER}} .elementor-button' => 'fill: {{VALUE}}; color: {{VALUE}};',
351 ],
352 'condition' => $args['section_condition'],
353 ]
354 );
355
356 $this->add_group_control(
357 Group_Control_Background::get_type(),
358 [
359 'name' => 'background',
360 'types' => [ 'classic', 'gradient' ],
361 'exclude' => [ 'image' ],
362 'selector' => '{{WRAPPER}} .elementor-button',
363 'fields_options' => [
364 'background' => [
365 'default' => 'classic',
366 ],
367 'color' => [
368 'global' => [
369 'default' => Global_Colors::COLOR_ACCENT,
370 ],
371 ],
372 ],
373 'condition' => $args['section_condition'],
374 ]
375 );
376
377 $this->end_controls_tab();
378
379 $this->start_controls_tab(
380 'tab_button_hover',
381 [
382 'label' => esc_html__( 'Hover', 'elementor' ),
383 'condition' => $args['section_condition'],
384 ]
385 );
386
387 $this->add_control(
388 'hover_color',
389 [
390 'label' => esc_html__( 'Text Color', 'elementor' ),
391 'type' => Controls_Manager::COLOR,
392 'selectors' => [
393 '{{WRAPPER}} .elementor-button:hover, {{WRAPPER}} .elementor-button:focus' => 'color: {{VALUE}};',
394 '{{WRAPPER}} .elementor-button:hover svg, {{WRAPPER}} .elementor-button:focus svg' => 'fill: {{VALUE}};',
395 ],
396 'condition' => $args['section_condition'],
397 ]
398 );
399
400 $this->add_group_control(
401 Group_Control_Background::get_type(),
402 [
403 'name' => 'button_background_hover',
404 'types' => [ 'classic', 'gradient' ],
405 'exclude' => [ 'image' ],
406 'selector' => '{{WRAPPER}} .elementor-button:hover, {{WRAPPER}} .elementor-button:focus',
407 'fields_options' => [
408 'background' => [
409 'default' => 'classic',
410 ],
411 ],
412 'condition' => $args['section_condition'],
413 ]
414 );
415
416 $this->add_control(
417 'button_hover_border_color',
418 [
419 'label' => esc_html__( 'Border Color', 'elementor' ),
420 'type' => Controls_Manager::COLOR,
421 'condition' => [
422 'border_border!' => '',
423 ],
424 'selectors' => [
425 '{{WRAPPER}} .elementor-button:hover, {{WRAPPER}} .elementor-button:focus' => 'border-color: {{VALUE}};',
426 ],
427 'condition' => $args['section_condition'],
428 ]
429 );
430
431 $this->add_control(
432 'button_hover_transition_duration',
433 [
434 'label' => esc_html__( 'Transition Duration', 'elementor' ),
435 'type' => Controls_Manager::SLIDER,
436 'size_units' => [ 's', 'ms', 'custom' ],
437 'default' => [
438 'unit' => 's',
439 ],
440 'selectors' => [
441 '{{WRAPPER}} .elementor-button' => 'transition-duration: {{SIZE}}{{UNIT}};',
442 ],
443 ]
444 );
445
446 $this->add_control(
447 'hover_animation',
448 [
449 'label' => esc_html__( 'Hover Animation', 'elementor' ),
450 'type' => Controls_Manager::HOVER_ANIMATION,
451 'condition' => $args['section_condition'],
452 ]
453 );
454
455 $this->end_controls_tab();
456
457 $this->end_controls_tabs();
458
459 $this->add_group_control(
460 Group_Control_Border::get_type(),
461 [
462 'name' => 'border',
463 'selector' => '{{WRAPPER}} .elementor-button',
464 'separator' => 'before',
465 'condition' => $args['section_condition'],
466 ]
467 );
468
469 $this->add_responsive_control(
470 'border_radius',
471 [
472 'label' => esc_html__( 'Border Radius', 'elementor' ),
473 'type' => Controls_Manager::DIMENSIONS,
474 'size_units' => [ 'px', '%', 'em', 'rem', 'custom' ],
475 'selectors' => [
476 '{{WRAPPER}} .elementor-button' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
477 ],
478 'condition' => $args['section_condition'],
479 ]
480 );
481
482 $this->add_group_control(
483 Group_Control_Box_Shadow::get_type(),
484 [
485 'name' => 'button_box_shadow',
486 'selector' => '{{WRAPPER}} .elementor-button',
487 'condition' => $args['section_condition'],
488 ]
489 );
490
491 $this->add_responsive_control(
492 'text_padding',
493 [
494 'label' => esc_html__( 'Padding', 'elementor' ),
495 'type' => Controls_Manager::DIMENSIONS,
496 'size_units' => [ 'px', '%', 'em', 'rem', 'vw', 'custom' ],
497 'selectors' => [
498 '{{WRAPPER}} .elementor-button' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
499 ],
500 'separator' => 'before',
501 'condition' => $args['section_condition'],
502 ]
503 );
504 }
505
506 /**
507 * Render button widget output on the frontend.
508 *
509 * Written in PHP and used to generate the final HTML.
510 *
511 * @param \Elementor\Widget_Base|null $instance
512 *
513 * @since 3.4.0
514 * @access protected
515 */
516 protected function render_button( Widget_Base $instance = null ) {
517 if ( empty( $instance ) ) {
518 $instance = $this;
519 }
520
521 $settings = $instance->get_settings_for_display();
522
523 if ( empty( $settings['text'] ) && empty( $settings['selected_icon']['value'] ) ) {
524 return;
525 }
526
527 $instance->add_render_attribute( 'wrapper', 'class', 'elementor-button-wrapper' );
528
529 $instance->add_render_attribute( 'button', 'class', 'elementor-button' );
530
531 if ( ! empty( $settings['link']['url'] ) ) {
532 $instance->add_link_attributes( 'button', $settings['link'] );
533 $instance->add_render_attribute( 'button', 'class', 'elementor-button-link' );
534 } else {
535 $instance->add_render_attribute( 'button', 'role', 'button' );
536 }
537
538 if ( ! empty( $settings['button_css_id'] ) ) {
539 $instance->add_render_attribute( 'button', 'id', $settings['button_css_id'] );
540 }
541
542 if ( ! empty( $settings['size'] ) ) {
543 $instance->add_render_attribute( 'button', 'class', 'elementor-size-' . $settings['size'] );
544 } else {
545 $instance->add_render_attribute( 'button', 'class', 'elementor-size-sm' ); // BC, to make sure the class is always present
546 }
547
548 if ( ! empty( $settings['hover_animation'] ) ) {
549 $instance->add_render_attribute( 'button', 'class', 'elementor-animation-' . $settings['hover_animation'] );
550 }
551 ?>
552 <div <?php $instance->print_render_attribute_string( 'wrapper' ); ?>>
553 <a <?php $instance->print_render_attribute_string( 'button' ); ?>>
554 <?php $this->render_text( $instance ); ?>
555 </a>
556 </div>
557 <?php
558 }
559
560 /**
561 * Render button widget output in the editor.
562 *
563 * Written as a Backbone JavaScript template and used to generate the live preview.
564 *
565 * @since 3.4.0
566 * @access protected
567 */
568 protected function content_template() {
569 ?>
570 <#
571 if ( '' === settings.text && '' === settings.selected_icon.value ) {
572 return;
573 }
574
575 view.addRenderAttribute( 'wrapper', 'class', 'elementor-button-wrapper' );
576
577 view.addRenderAttribute( 'button', 'class', 'elementor-button' );
578
579 if ( '' !== settings.link.url ) {
580 view.addRenderAttribute( 'button', 'href', elementor.helpers.sanitizeUrl( settings.link.url ) );
581 view.addRenderAttribute( 'button', 'class', 'elementor-button-link' );
582 } else {
583 view.addRenderAttribute( 'button', 'role', 'button' );
584 }
585
586 if ( '' !== settings.button_css_id ) {
587 view.addRenderAttribute( 'button', 'id', settings.button_css_id );
588 }
589
590 if ( '' !== settings.size ) {
591 view.addRenderAttribute( 'button', 'class', 'elementor-size-' + settings.size );
592 }
593
594 if ( '' !== settings.hover_animation ) {
595 view.addRenderAttribute( 'button', 'class', 'elementor-animation-' + settings.hover_animation );
596 }
597
598 view.addRenderAttribute( 'icon', 'class', 'elementor-button-icon' );
599 view.addRenderAttribute( 'text', 'class', 'elementor-button-text' );
600 view.addInlineEditingAttributes( 'text', 'none' );
601 var iconHTML = elementor.helpers.renderIcon( view, settings.selected_icon, { 'aria-hidden': true }, 'i' , 'object' ),
602 migrated = elementor.helpers.isIconMigrated( settings, 'selected_icon' );
603 #>
604 <div {{{ view.getRenderAttributeString( 'wrapper' ) }}}>
605 <a {{{ view.getRenderAttributeString( 'button' ) }}}>
606 <span class="elementor-button-content-wrapper">
607 <# if ( settings.icon || settings.selected_icon ) { #>
608 <span {{{ view.getRenderAttributeString( 'icon' ) }}}>
609 <# if ( ( migrated || ! settings.icon ) && iconHTML.rendered ) { #>
610 {{{ iconHTML.value }}}
611 <# } else { #>
612 <i class="{{ settings.icon }}" aria-hidden="true"></i>
613 <# } #>
614 </span>
615 <# } #>
616 <# if ( settings.text ) { #>
617 <span {{{ view.getRenderAttributeString( 'text' ) }}}>{{{ settings.text }}}</span>
618 <# } #>
619 </span>
620 </a>
621 </div>
622 <?php
623 }
624
625 /**
626 * Render button text.
627 *
628 * Render button widget text.
629 *
630 * @param \Elementor\Widget_Base|null $instance
631 *
632 * @since 3.4.0
633 * @access protected
634 */
635 protected function render_text( Widget_Base $instance = null ) {
636 // The default instance should be `$this` (a Button widget), unless the Trait is being used from outside of a widget (e.g. `Skin_Base`) which should pass an `$instance`.
637 if ( empty( $instance ) ) {
638 $instance = $this;
639 }
640
641 $settings = $instance->get_settings_for_display();
642
643 $migrated = isset( $settings['__fa4_migrated']['selected_icon'] );
644 $is_new = empty( $settings['icon'] ) && Icons_Manager::is_migration_allowed();
645
646 $instance->add_render_attribute( [
647 'content-wrapper' => [
648 'class' => 'elementor-button-content-wrapper',
649 ],
650 'icon' => [
651 'class' => 'elementor-button-icon',
652 ],
653 'text' => [
654 'class' => 'elementor-button-text',
655 ],
656 ] );
657
658 // TODO: replace the protected with public
659 //$instance->add_inline_editing_attributes( 'text', 'none' );
660 ?>
661 <span <?php $instance->print_render_attribute_string( 'content-wrapper' ); ?>>
662 <?php if ( ! empty( $settings['icon'] ) || ! empty( $settings['selected_icon']['value'] ) ) : ?>
663 <span <?php $instance->print_render_attribute_string( 'icon' ); ?>>
664 <?php if ( $is_new || $migrated ) :
665 Icons_Manager::render_icon( $settings['selected_icon'], [ 'aria-hidden' => 'true' ] );
666 else : ?>
667 <i class="<?php echo esc_attr( $settings['icon'] ); ?>" aria-hidden="true"></i>
668 <?php endif; ?>
669 </span>
670 <?php endif; ?>
671 <?php if ( ! empty( $settings['text'] ) ) : ?>
672 <span <?php $instance->print_render_attribute_string( 'text' ); ?>><?php $this->print_unescaped_setting( 'text' ); ?></span>
673 <?php endif; ?>
674 </span>
675 <?php
676 }
677
678 public function on_import( $element ) {
679 return Icons_Manager::on_import_migration( $element, 'icon', 'selected_icon' );
680 }
681 }
682