PluginProbe
Elementor Website Builder – more than just a page builder / 3.21.4
Elementor Website Builder – more than just a page builder v3.21.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.21.4, at includes/widgets/traits/button-trait.php

634 lines 17.9 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 $this->add_control(
136 'icon_align',
137 [
138 'label' => esc_html__( 'Icon Position', 'elementor' ),
139 'type' => Controls_Manager::CHOOSE,
140 'default' => 'left',
141 'options' => [
142 'left' => [
143 'title' => esc_html__( 'Left', 'elementor' ),
144 'icon' => 'eicon-h-align-left',
145 ],
146 'right' => [
147 'title' => esc_html__( 'Right', 'elementor' ),
148 'icon' => 'eicon-h-align-right',
149 ],
150 ],
151 'condition' => array_merge( $args['section_condition'], [ 'selected_icon[value]!' => '' ] ),
152 ]
153 );
154
155 $this->add_control(
156 'icon_indent',
157 [
158 'label' => esc_html__( 'Icon Spacing', 'elementor' ),
159 'type' => Controls_Manager::SLIDER,
160 'size_units' => [ 'px', 'em', 'rem', 'custom' ],
161 'range' => [
162 'px' => [
163 'max' => 50,
164 ],
165 'em' => [
166 'max' => 5,
167 ],
168 'rem' => [
169 'max' => 5,
170 ],
171 ],
172 'selectors' => [
173 '{{WRAPPER}} .elementor-button .elementor-align-icon-right' => 'margin-left: {{SIZE}}{{UNIT}};',
174 '{{WRAPPER}} .elementor-button .elementor-align-icon-left' => 'margin-right: {{SIZE}}{{UNIT}};',
175 ],
176 'condition' => array_merge( $args['section_condition'], [ 'selected_icon[value]!' => '' ] ),
177 ]
178 );
179
180 $this->add_control(
181 'button_css_id',
182 [
183 'label' => esc_html__( 'Button ID', 'elementor' ),
184 'type' => Controls_Manager::TEXT,
185 'dynamic' => [
186 'active' => true,
187 ],
188 'ai' => [
189 'active' => false,
190 ],
191 'default' => '',
192 'title' => esc_html__( 'Add your custom id WITHOUT the Pound key. e.g: my-id', 'elementor' ),
193 'description' => sprintf(
194 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' ),
195 '<code>',
196 '</code>'
197 ),
198 'separator' => 'before',
199 'condition' => $args['section_condition'],
200 ]
201 );
202 }
203
204 /**
205 * @since 3.4.0
206 *
207 * @param array $args {
208 * An array of values for the button adjustments.
209 *
210 * @type array $section_condition Set of conditions to hide the controls.
211 * @type string $alignment_default Default alignment for the button.
212 * @type string $alignment_control_prefix_class Prefix class name for the button alignment control.
213 * }
214 */
215 protected function register_button_style_controls( $args = [] ) {
216 $default_args = [
217 'section_condition' => [],
218 'alignment_default' => '',
219 'alignment_control_prefix_class' => 'elementor%s-align-',
220 ];
221
222 $args = wp_parse_args( $args, $default_args );
223
224 $this->add_responsive_control(
225 'align',
226 [
227 'label' => esc_html__( 'Alignment', 'elementor' ),
228 'type' => Controls_Manager::CHOOSE,
229 'options' => [
230 'left' => [
231 'title' => esc_html__( 'Left', 'elementor' ),
232 'icon' => 'eicon-text-align-left',
233 ],
234 'center' => [
235 'title' => esc_html__( 'Center', 'elementor' ),
236 'icon' => 'eicon-text-align-center',
237 ],
238 'right' => [
239 'title' => esc_html__( 'Right', 'elementor' ),
240 'icon' => 'eicon-text-align-right',
241 ],
242 'justify' => [
243 'title' => esc_html__( 'Justified', 'elementor' ),
244 'icon' => 'eicon-text-align-justify',
245 ],
246 ],
247 'prefix_class' => $args['alignment_control_prefix_class'],
248 'default' => $args['alignment_default'],
249 'condition' => $args['section_condition'],
250 ]
251 );
252
253 $this->add_group_control(
254 Group_Control_Typography::get_type(),
255 [
256 'name' => 'typography',
257 'global' => [
258 'default' => Global_Typography::TYPOGRAPHY_ACCENT,
259 ],
260 'selector' => '{{WRAPPER}} .elementor-button',
261 'condition' => $args['section_condition'],
262 ]
263 );
264
265 $this->add_group_control(
266 Group_Control_Text_Shadow::get_type(),
267 [
268 'name' => 'text_shadow',
269 'selector' => '{{WRAPPER}} .elementor-button',
270 'condition' => $args['section_condition'],
271 ]
272 );
273
274 $this->start_controls_tabs( 'tabs_button_style', [
275 'condition' => $args['section_condition'],
276 ] );
277
278 $this->start_controls_tab(
279 'tab_button_normal',
280 [
281 'label' => esc_html__( 'Normal', 'elementor' ),
282 'condition' => $args['section_condition'],
283 ]
284 );
285
286 $this->add_control(
287 'button_text_color',
288 [
289 'label' => esc_html__( 'Text Color', 'elementor' ),
290 'type' => Controls_Manager::COLOR,
291 'default' => '',
292 'selectors' => [
293 '{{WRAPPER}} .elementor-button' => 'fill: {{VALUE}}; color: {{VALUE}};',
294 ],
295 'condition' => $args['section_condition'],
296 ]
297 );
298
299 $this->add_group_control(
300 Group_Control_Background::get_type(),
301 [
302 'name' => 'background',
303 'types' => [ 'classic', 'gradient' ],
304 'exclude' => [ 'image' ],
305 'selector' => '{{WRAPPER}} .elementor-button',
306 'fields_options' => [
307 'background' => [
308 'default' => 'classic',
309 ],
310 'color' => [
311 'global' => [
312 'default' => Global_Colors::COLOR_ACCENT,
313 ],
314 ],
315 ],
316 'condition' => $args['section_condition'],
317 ]
318 );
319
320 $this->end_controls_tab();
321
322 $this->start_controls_tab(
323 'tab_button_hover',
324 [
325 'label' => esc_html__( 'Hover', 'elementor' ),
326 'condition' => $args['section_condition'],
327 ]
328 );
329
330 $this->add_control(
331 'hover_color',
332 [
333 'label' => esc_html__( 'Text Color', 'elementor' ),
334 'type' => Controls_Manager::COLOR,
335 'selectors' => [
336 '{{WRAPPER}} .elementor-button:hover, {{WRAPPER}} .elementor-button:focus' => 'color: {{VALUE}};',
337 '{{WRAPPER}} .elementor-button:hover svg, {{WRAPPER}} .elementor-button:focus svg' => 'fill: {{VALUE}};',
338 ],
339 'condition' => $args['section_condition'],
340 ]
341 );
342
343 $this->add_group_control(
344 Group_Control_Background::get_type(),
345 [
346 'name' => 'button_background_hover',
347 'types' => [ 'classic', 'gradient' ],
348 'exclude' => [ 'image' ],
349 'selector' => '{{WRAPPER}} .elementor-button:hover, {{WRAPPER}} .elementor-button:focus',
350 'fields_options' => [
351 'background' => [
352 'default' => 'classic',
353 ],
354 ],
355 'condition' => $args['section_condition'],
356 ]
357 );
358
359 $this->add_control(
360 'button_hover_border_color',
361 [
362 'label' => esc_html__( 'Border Color', 'elementor' ),
363 'type' => Controls_Manager::COLOR,
364 'condition' => [
365 'border_border!' => '',
366 ],
367 'selectors' => [
368 '{{WRAPPER}} .elementor-button:hover, {{WRAPPER}} .elementor-button:focus' => 'border-color: {{VALUE}};',
369 ],
370 'condition' => $args['section_condition'],
371 ]
372 );
373
374 $this->add_control(
375 'button_hover_transition_duration',
376 [
377 'label' => esc_html__( 'Transition Duration', 'elementor' ),
378 'type' => Controls_Manager::SLIDER,
379 'size_units' => [ 's', 'ms', 'custom' ],
380 'default' => [
381 'unit' => 's',
382 ],
383 'selectors' => [
384 '{{WRAPPER}} .elementor-button' => 'transition-duration: {{SIZE}}{{UNIT}};',
385 ],
386 ]
387 );
388
389 $this->add_control(
390 'hover_animation',
391 [
392 'label' => esc_html__( 'Hover Animation', 'elementor' ),
393 'type' => Controls_Manager::HOVER_ANIMATION,
394 'condition' => $args['section_condition'],
395 ]
396 );
397
398 $this->end_controls_tab();
399
400 $this->end_controls_tabs();
401
402 $this->add_group_control(
403 Group_Control_Border::get_type(),
404 [
405 'name' => 'border',
406 'selector' => '{{WRAPPER}} .elementor-button',
407 'separator' => 'before',
408 'condition' => $args['section_condition'],
409 ]
410 );
411
412 $this->add_responsive_control(
413 'border_radius',
414 [
415 'label' => esc_html__( 'Border Radius', 'elementor' ),
416 'type' => Controls_Manager::DIMENSIONS,
417 'size_units' => [ 'px', '%', 'em', 'rem', 'custom' ],
418 'selectors' => [
419 '{{WRAPPER}} .elementor-button' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
420 ],
421 'condition' => $args['section_condition'],
422 ]
423 );
424
425 $this->add_group_control(
426 Group_Control_Box_Shadow::get_type(),
427 [
428 'name' => 'button_box_shadow',
429 'selector' => '{{WRAPPER}} .elementor-button',
430 'condition' => $args['section_condition'],
431 ]
432 );
433
434 $this->add_responsive_control(
435 'text_padding',
436 [
437 'label' => esc_html__( 'Padding', 'elementor' ),
438 'type' => Controls_Manager::DIMENSIONS,
439 'size_units' => [ 'px', '%', 'em', 'rem', 'vw', 'custom' ],
440 'selectors' => [
441 '{{WRAPPER}} .elementor-button' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
442 ],
443 'separator' => 'before',
444 'condition' => $args['section_condition'],
445 ]
446 );
447 }
448
449 /**
450 * Render button widget output on the frontend.
451 *
452 * Written in PHP and used to generate the final HTML.
453 *
454 * @param \Elementor\Widget_Base|null $instance
455 *
456 * @since 3.4.0
457 * @access protected
458 */
459 protected function render_button( Widget_Base $instance = null ) {
460 if ( empty( $instance ) ) {
461 $instance = $this;
462 }
463
464 $settings = $instance->get_settings_for_display();
465
466 if ( empty( $settings['text'] ) && empty( $settings['selected_icon']['value'] ) ) {
467 return;
468 }
469
470 $instance->add_render_attribute( 'wrapper', 'class', 'elementor-button-wrapper' );
471
472 $instance->add_render_attribute( 'button', 'class', 'elementor-button' );
473
474 if ( ! empty( $settings['link']['url'] ) ) {
475 $instance->add_link_attributes( 'button', $settings['link'] );
476 $instance->add_render_attribute( 'button', 'class', 'elementor-button-link' );
477 } else {
478 $instance->add_render_attribute( 'button', 'role', 'button' );
479 }
480
481 if ( ! empty( $settings['button_css_id'] ) ) {
482 $instance->add_render_attribute( 'button', 'id', $settings['button_css_id'] );
483 }
484
485 if ( ! empty( $settings['size'] ) ) {
486 $instance->add_render_attribute( 'button', 'class', 'elementor-size-' . $settings['size'] );
487 } else {
488 $instance->add_render_attribute( 'button', 'class', 'elementor-size-sm' ); // BC, to make sure the class is always present
489 }
490
491 if ( ! empty( $settings['hover_animation'] ) ) {
492 $instance->add_render_attribute( 'button', 'class', 'elementor-animation-' . $settings['hover_animation'] );
493 }
494 ?>
495 <div <?php $instance->print_render_attribute_string( 'wrapper' ); ?>>
496 <a <?php $instance->print_render_attribute_string( 'button' ); ?>>
497 <?php $this->render_text( $instance ); ?>
498 </a>
499 </div>
500 <?php
501 }
502
503 /**
504 * Render button widget output in the editor.
505 *
506 * Written as a Backbone JavaScript template and used to generate the live preview.
507 *
508 * @since 3.4.0
509 * @access protected
510 */
511 protected function content_template() {
512 ?>
513 <#
514 if ( '' === settings.text && '' === settings.selected_icon.value ) {
515 return;
516 }
517
518 view.addRenderAttribute( 'wrapper', 'class', 'elementor-button-wrapper' );
519
520 view.addRenderAttribute( 'button', 'class', 'elementor-button' );
521
522 if ( '' !== settings.link.url ) {
523 view.addRenderAttribute( 'button', 'href', settings.link.url );
524 view.addRenderAttribute( 'button', 'class', 'elementor-button-link' );
525 } else {
526 view.addRenderAttribute( 'button', 'role', 'button' );
527 }
528
529 if ( '' !== settings.button_css_id ) {
530 view.addRenderAttribute( 'button', 'id', settings.button_css_id );
531 }
532
533 if ( '' !== settings.size ) {
534 view.addRenderAttribute( 'button', 'class', 'elementor-size-' + settings.size );
535 }
536
537 if ( '' !== settings.hover_animation ) {
538 view.addRenderAttribute( 'button', 'class', 'elementor-animation-' + settings.hover_animation );
539 }
540
541 view.addRenderAttribute( 'text', 'class', 'elementor-button-text' );
542 view.addInlineEditingAttributes( 'text', 'none' );
543 var iconHTML = elementor.helpers.renderIcon( view, settings.selected_icon, { 'aria-hidden': true }, 'i' , 'object' ),
544 migrated = elementor.helpers.isIconMigrated( settings, 'selected_icon' );
545 #>
546 <div {{{ view.getRenderAttributeString( 'wrapper' ) }}}>
547 <a {{{ view.getRenderAttributeString( 'button' ) }}}>
548 <span class="elementor-button-content-wrapper">
549 <# if ( settings.icon || settings.selected_icon ) { #>
550 <span class="elementor-button-icon elementor-align-icon-{{ settings.icon_align }}">
551 <# if ( ( migrated || ! settings.icon ) && iconHTML.rendered ) { #>
552 {{{ iconHTML.value }}}
553 <# } else { #>
554 <i class="{{ settings.icon }}" aria-hidden="true"></i>
555 <# } #>
556 </span>
557 <# } #>
558 <# if ( settings.text ) { #>
559 <span {{{ view.getRenderAttributeString( 'text' ) }}}>{{{ settings.text }}}</span>
560 <# } #>
561 </span>
562 </a>
563 </div>
564 <?php
565 }
566
567 /**
568 * Render button text.
569 *
570 * Render button widget text.
571 *
572 * @param \Elementor\Widget_Base|null $instance
573 *
574 * @since 3.4.0
575 * @access protected
576 */
577 protected function render_text( Widget_Base $instance = null ) {
578 // 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`.
579 if ( empty( $instance ) ) {
580 $instance = $this;
581 }
582
583 $settings = $instance->get_settings_for_display();
584
585 $migrated = isset( $settings['__fa4_migrated']['selected_icon'] );
586 $is_new = empty( $settings['icon'] ) && Icons_Manager::is_migration_allowed();
587
588 if ( ! $is_new && empty( $settings['icon_align'] ) ) {
589 // @todo: remove when deprecated
590 // added as bc in 2.6
591 //old default
592 $settings['icon_align'] = $instance->get_settings( 'icon_align' );
593 }
594
595 $instance->add_render_attribute( [
596 'content-wrapper' => [
597 'class' => 'elementor-button-content-wrapper',
598 ],
599 'icon-align' => [
600 'class' => [
601 'elementor-button-icon',
602 'elementor-align-icon-' . $settings['icon_align'],
603 ],
604 ],
605 'text' => [
606 'class' => 'elementor-button-text',
607 ],
608 ] );
609
610 // TODO: replace the protected with public
611 //$instance->add_inline_editing_attributes( 'text', 'none' );
612 ?>
613 <span <?php $instance->print_render_attribute_string( 'content-wrapper' ); ?>>
614 <?php if ( ! empty( $settings['icon'] ) || ! empty( $settings['selected_icon']['value'] ) ) : ?>
615 <span <?php $instance->print_render_attribute_string( 'icon-align' ); ?>>
616 <?php if ( $is_new || $migrated ) :
617 Icons_Manager::render_icon( $settings['selected_icon'], [ 'aria-hidden' => 'true' ] );
618 else : ?>
619 <i class="<?php echo esc_attr( $settings['icon'] ); ?>" aria-hidden="true"></i>
620 <?php endif; ?>
621 </span>
622 <?php endif; ?>
623 <?php if ( ! empty( $settings['text'] ) ) : ?>
624 <span <?php $instance->print_render_attribute_string( 'text' ); ?>><?php $this->print_unescaped_setting( 'text' ); ?></span>
625 <?php endif; ?>
626 </span>
627 <?php
628 }
629
630 public function on_import( $element ) {
631 return Icons_Manager::on_import_migration( $element, 'icon', 'selected_icon' );
632 }
633 }
634