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

567 lines 16.0 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 string $alignment_control_prefix_class Prefix class name for the button alignment control.
51 * @type string $alignment_default Default alignment for the button.
52 * @type array $icon_exclude_inline_options Set of icon types to exclude from icon controls.
53 * }
54 */
55 protected function register_button_content_controls( $args = [] ) {
56 $default_args = [
57 'section_condition' => [],
58 'button_default_text' => esc_html__( 'Click here', 'elementor' ),
59 'text_control_label' => esc_html__( 'Text', 'elementor' ),
60 'alignment_control_prefix_class' => 'elementor%s-align-',
61 'alignment_default' => '',
62 'icon_exclude_inline_options' => [],
63 ];
64
65 $args = wp_parse_args( $args, $default_args );
66
67 $this->add_control(
68 'button_type',
69 [
70 'label' => esc_html__( 'Type', 'elementor' ),
71 'type' => Controls_Manager::SELECT,
72 'default' => '',
73 'options' => [
74 '' => esc_html__( 'Default', 'elementor' ),
75 'info' => esc_html__( 'Info', 'elementor' ),
76 'success' => esc_html__( 'Success', 'elementor' ),
77 'warning' => esc_html__( 'Warning', 'elementor' ),
78 'danger' => esc_html__( 'Danger', 'elementor' ),
79 ],
80 'prefix_class' => 'elementor-button-',
81 'condition' => $args['section_condition'],
82 ]
83 );
84
85 $this->add_control(
86 'text',
87 [
88 'label' => $args['text_control_label'],
89 'type' => Controls_Manager::TEXT,
90 'dynamic' => [
91 'active' => true,
92 ],
93 'default' => $args['button_default_text'],
94 'placeholder' => $args['button_default_text'],
95 'condition' => $args['section_condition'],
96 ]
97 );
98
99 $this->add_control(
100 'link',
101 [
102 'label' => esc_html__( 'Link', 'elementor' ),
103 'type' => Controls_Manager::URL,
104 'dynamic' => [
105 'active' => true,
106 ],
107 'placeholder' => esc_html__( 'https://your-link.com', 'elementor' ),
108 'default' => [
109 'url' => '#',
110 ],
111 'condition' => $args['section_condition'],
112 ]
113 );
114
115 $this->add_responsive_control(
116 'align',
117 [
118 'label' => esc_html__( 'Alignment', 'elementor' ),
119 'type' => Controls_Manager::CHOOSE,
120 'options' => [
121 'left' => [
122 'title' => esc_html__( 'Left', 'elementor' ),
123 'icon' => 'eicon-text-align-left',
124 ],
125 'center' => [
126 'title' => esc_html__( 'Center', 'elementor' ),
127 'icon' => 'eicon-text-align-center',
128 ],
129 'right' => [
130 'title' => esc_html__( 'Right', 'elementor' ),
131 'icon' => 'eicon-text-align-right',
132 ],
133 'justify' => [
134 'title' => esc_html__( 'Justified', 'elementor' ),
135 'icon' => 'eicon-text-align-justify',
136 ],
137 ],
138 'prefix_class' => $args['alignment_control_prefix_class'],
139 'default' => $args['alignment_default'],
140 'condition' => $args['section_condition'],
141 ]
142 );
143
144 $this->add_control(
145 'size',
146 [
147 'label' => esc_html__( 'Size', 'elementor' ),
148 'type' => Controls_Manager::SELECT,
149 'default' => 'sm',
150 'options' => self::get_button_sizes(),
151 'style_transfer' => true,
152 'condition' => $args['section_condition'],
153 ]
154 );
155
156 $this->add_control(
157 'selected_icon',
158 [
159 'label' => esc_html__( 'Icon', 'elementor' ),
160 'type' => Controls_Manager::ICONS,
161 'fa4compatibility' => 'icon',
162 'skin' => 'inline',
163 'label_block' => false,
164 'condition' => $args['section_condition'],
165 'icon_exclude_inline_options' => $args['icon_exclude_inline_options'],
166 ]
167 );
168
169 $this->add_control(
170 'icon_align',
171 [
172 'label' => esc_html__( 'Icon Position', 'elementor' ),
173 'type' => Controls_Manager::SELECT,
174 'default' => 'left',
175 'options' => [
176 'left' => esc_html__( 'Before', 'elementor' ),
177 'right' => esc_html__( 'After', 'elementor' ),
178 ],
179 'condition' => array_merge( $args['section_condition'], [ 'selected_icon[value]!' => '' ] ),
180 ]
181 );
182
183 $this->add_control(
184 'icon_indent',
185 [
186 'label' => esc_html__( 'Icon Spacing', 'elementor' ),
187 'type' => Controls_Manager::SLIDER,
188 'range' => [
189 'px' => [
190 'max' => 50,
191 ],
192 ],
193 'selectors' => [
194 '{{WRAPPER}} .elementor-button .elementor-align-icon-right' => 'margin-left: {{SIZE}}{{UNIT}};',
195 '{{WRAPPER}} .elementor-button .elementor-align-icon-left' => 'margin-right: {{SIZE}}{{UNIT}};',
196 ],
197 'condition' => $args['section_condition'],
198 ]
199 );
200
201 $this->add_control(
202 'view',
203 [
204 'label' => esc_html__( 'View', 'elementor' ),
205 'type' => Controls_Manager::HIDDEN,
206 'default' => 'traditional',
207 'condition' => $args['section_condition'],
208 ]
209 );
210
211 $this->add_control(
212 'button_css_id',
213 [
214 'label' => esc_html__( 'Button ID', 'elementor' ),
215 'type' => Controls_Manager::TEXT,
216 'dynamic' => [
217 'active' => true,
218 ],
219 'default' => '',
220 'title' => esc_html__( 'Add your custom id WITHOUT the Pound key. e.g: my-id', 'elementor' ),
221 'description' => sprintf(
222 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' ),
223 '<code>',
224 '</code>'
225 ),
226 'separator' => 'before',
227 'condition' => $args['section_condition'],
228 ]
229 );
230 }
231
232 protected function register_button_style_controls( $args = [] ) {
233 $default_args = [
234 'section_condition' => [],
235 ];
236
237 $args = wp_parse_args( $args, $default_args );
238
239 $this->add_group_control(
240 Group_Control_Typography::get_type(),
241 [
242 'name' => 'typography',
243 'global' => [
244 'default' => Global_Typography::TYPOGRAPHY_ACCENT,
245 ],
246 'selector' => '{{WRAPPER}} .elementor-button',
247 'condition' => $args['section_condition'],
248 ]
249 );
250
251 $this->add_group_control(
252 Group_Control_Text_Shadow::get_type(),
253 [
254 'name' => 'text_shadow',
255 'selector' => '{{WRAPPER}} .elementor-button',
256 'condition' => $args['section_condition'],
257 ]
258 );
259
260 $this->start_controls_tabs( 'tabs_button_style', [
261 'condition' => $args['section_condition'],
262 ] );
263
264 $this->start_controls_tab(
265 'tab_button_normal',
266 [
267 'label' => esc_html__( 'Normal', 'elementor' ),
268 'condition' => $args['section_condition'],
269 ]
270 );
271
272 $this->add_control(
273 'button_text_color',
274 [
275 'label' => esc_html__( 'Text Color', 'elementor' ),
276 'type' => Controls_Manager::COLOR,
277 'default' => '',
278 'selectors' => [
279 '{{WRAPPER}} .elementor-button' => 'fill: {{VALUE}}; color: {{VALUE}};',
280 ],
281 'condition' => $args['section_condition'],
282 ]
283 );
284
285 $this->add_group_control(
286 Group_Control_Background::get_type(),
287 [
288 'name' => 'background',
289 'types' => [ 'classic', 'gradient' ],
290 'exclude' => [ 'image' ],
291 'selector' => '{{WRAPPER}} .elementor-button',
292 'fields_options' => [
293 'background' => [
294 'default' => 'classic',
295 ],
296 'color' => [
297 'global' => [
298 'default' => Global_Colors::COLOR_ACCENT,
299 ],
300 ],
301 ],
302 'condition' => $args['section_condition'],
303 ]
304 );
305
306 $this->end_controls_tab();
307
308 $this->start_controls_tab(
309 'tab_button_hover',
310 [
311 'label' => esc_html__( 'Hover', 'elementor' ),
312 'condition' => $args['section_condition'],
313 ]
314 );
315
316 $this->add_control(
317 'hover_color',
318 [
319 'label' => esc_html__( 'Text Color', 'elementor' ),
320 'type' => Controls_Manager::COLOR,
321 'selectors' => [
322 '{{WRAPPER}} .elementor-button:hover, {{WRAPPER}} .elementor-button:focus' => 'color: {{VALUE}};',
323 '{{WRAPPER}} .elementor-button:hover svg, {{WRAPPER}} .elementor-button:focus svg' => 'fill: {{VALUE}};',
324 ],
325 'condition' => $args['section_condition'],
326 ]
327 );
328
329 $this->add_group_control(
330 Group_Control_Background::get_type(),
331 [
332 'name' => 'button_background_hover',
333 'types' => [ 'classic', 'gradient' ],
334 'exclude' => [ 'image' ],
335 'selector' => '{{WRAPPER}} .elementor-button:hover, {{WRAPPER}} .elementor-button:focus',
336 'fields_options' => [
337 'background' => [
338 'default' => 'classic',
339 ],
340 ],
341 'condition' => $args['section_condition'],
342 ]
343 );
344
345 $this->add_control(
346 'button_hover_border_color',
347 [
348 'label' => esc_html__( 'Border Color', 'elementor' ),
349 'type' => Controls_Manager::COLOR,
350 'condition' => [
351 'border_border!' => '',
352 ],
353 'selectors' => [
354 '{{WRAPPER}} .elementor-button:hover, {{WRAPPER}} .elementor-button:focus' => 'border-color: {{VALUE}};',
355 ],
356 'condition' => $args['section_condition'],
357 ]
358 );
359
360 $this->add_control(
361 'hover_animation',
362 [
363 'label' => esc_html__( 'Hover Animation', 'elementor' ),
364 'type' => Controls_Manager::HOVER_ANIMATION,
365 'condition' => $args['section_condition'],
366 ]
367 );
368
369 $this->end_controls_tab();
370
371 $this->end_controls_tabs();
372
373 $this->add_group_control(
374 Group_Control_Border::get_type(),
375 [
376 'name' => 'border',
377 'selector' => '{{WRAPPER}} .elementor-button',
378 'separator' => 'before',
379 'condition' => $args['section_condition'],
380 ]
381 );
382
383 $this->add_responsive_control(
384 'border_radius',
385 [
386 'label' => esc_html__( 'Border Radius', 'elementor' ),
387 'type' => Controls_Manager::DIMENSIONS,
388 'size_units' => [ 'px', '%', 'em' ],
389 'selectors' => [
390 '{{WRAPPER}} .elementor-button' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
391 ],
392 'condition' => $args['section_condition'],
393 ]
394 );
395
396 $this->add_group_control(
397 Group_Control_Box_Shadow::get_type(),
398 [
399 'name' => 'button_box_shadow',
400 'selector' => '{{WRAPPER}} .elementor-button',
401 'condition' => $args['section_condition'],
402 ]
403 );
404
405 $this->add_responsive_control(
406 'text_padding',
407 [
408 'label' => esc_html__( 'Padding', 'elementor' ),
409 'type' => Controls_Manager::DIMENSIONS,
410 'size_units' => [ 'px', 'em', '%' ],
411 'selectors' => [
412 '{{WRAPPER}} .elementor-button' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
413 ],
414 'separator' => 'before',
415 'condition' => $args['section_condition'],
416 ]
417 );
418 }
419
420 /**
421 * Render button widget output on the frontend.
422 *
423 * Written in PHP and used to generate the final HTML.
424 *
425 * @param \Elementor\Widget_Base|null $instance
426 *
427 * @since 3.4.0
428 * @access protected
429 */
430 protected function render_button( Widget_Base $instance = null ) {
431 if ( empty( $instance ) ) {
432 $instance = $this;
433 }
434
435 $settings = $instance->get_settings_for_display();
436
437 $instance->add_render_attribute( 'wrapper', 'class', 'elementor-button-wrapper' );
438
439 if ( ! empty( $settings['link']['url'] ) ) {
440 $instance->add_link_attributes( 'button', $settings['link'] );
441 $instance->add_render_attribute( 'button', 'class', 'elementor-button-link' );
442 }
443
444 $instance->add_render_attribute( 'button', 'class', 'elementor-button' );
445 $instance->add_render_attribute( 'button', 'role', 'button' );
446
447 if ( ! empty( $settings['button_css_id'] ) ) {
448 $instance->add_render_attribute( 'button', 'id', $settings['button_css_id'] );
449 }
450
451 if ( ! empty( $settings['size'] ) ) {
452 $instance->add_render_attribute( 'button', 'class', 'elementor-size-' . $settings['size'] );
453 }
454
455 if ( ! empty( $settings['hover_animation'] ) ) {
456 $instance->add_render_attribute( 'button', 'class', 'elementor-animation-' . $settings['hover_animation'] );
457 }
458 ?>
459 <div <?php $instance->print_render_attribute_string( 'wrapper' ); ?>>
460 <a <?php $instance->print_render_attribute_string( 'button' ); ?>>
461 <?php $this->render_text( $instance ); ?>
462 </a>
463 </div>
464 <?php
465 }
466
467 /**
468 * Render button widget output in the editor.
469 *
470 * Written as a Backbone JavaScript template and used to generate the live preview.
471 *
472 * @since 3.4.0
473 * @access protected
474 */
475 protected function content_template() {
476 ?>
477 <#
478 view.addRenderAttribute( 'text', 'class', 'elementor-button-text' );
479 view.addInlineEditingAttributes( 'text', 'none' );
480 var iconHTML = elementor.helpers.renderIcon( view, settings.selected_icon, { 'aria-hidden': true }, 'i' , 'object' ),
481 migrated = elementor.helpers.isIconMigrated( settings, 'selected_icon' );
482 #>
483 <div class="elementor-button-wrapper">
484 <a id="{{ settings.button_css_id }}" class="elementor-button elementor-size-{{ settings.size }} elementor-animation-{{ settings.hover_animation }}" href="{{ settings.link.url }}" role="button">
485 <span class="elementor-button-content-wrapper">
486 <# if ( settings.icon || settings.selected_icon ) { #>
487 <span class="elementor-button-icon elementor-align-icon-{{ settings.icon_align }}">
488 <# if ( ( migrated || ! settings.icon ) && iconHTML.rendered ) { #>
489 {{{ iconHTML.value }}}
490 <# } else { #>
491 <i class="{{ settings.icon }}" aria-hidden="true"></i>
492 <# } #>
493 </span>
494 <# } #>
495 <span {{{ view.getRenderAttributeString( 'text' ) }}}>{{{ settings.text }}}</span>
496 </span>
497 </a>
498 </div>
499 <?php
500 }
501
502 /**
503 * Render button text.
504 *
505 * Render button widget text.
506 *
507 * @param \Elementor\Widget_Base|null $instance
508 *
509 * @since 3.4.0
510 * @access protected
511 */
512 protected function render_text( Widget_Base $instance = null ) {
513 // 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`.
514 if ( empty( $instance ) ) {
515 $instance = $this;
516 }
517
518 $settings = $instance->get_settings_for_display();
519
520 $migrated = isset( $settings['__fa4_migrated']['selected_icon'] );
521 $is_new = empty( $settings['icon'] ) && Icons_Manager::is_migration_allowed();
522
523 if ( ! $is_new && empty( $settings['icon_align'] ) ) {
524 // @todo: remove when deprecated
525 // added as bc in 2.6
526 //old default
527 $settings['icon_align'] = $instance->get_settings( 'icon_align' );
528 }
529
530 $instance->add_render_attribute( [
531 'content-wrapper' => [
532 'class' => 'elementor-button-content-wrapper',
533 ],
534 'icon-align' => [
535 'class' => [
536 'elementor-button-icon',
537 'elementor-align-icon-' . $settings['icon_align'],
538 ],
539 ],
540 'text' => [
541 'class' => 'elementor-button-text',
542 ],
543 ] );
544
545 // TODO: replace the protected with public
546 //$instance->add_inline_editing_attributes( 'text', 'none' );
547 ?>
548 <span <?php $instance->print_render_attribute_string( 'content-wrapper' ); ?>>
549 <?php if ( ! empty( $settings['icon'] ) || ! empty( $settings['selected_icon']['value'] ) ) : ?>
550 <span <?php $instance->print_render_attribute_string( 'icon-align' ); ?>>
551 <?php if ( $is_new || $migrated ) :
552 Icons_Manager::render_icon( $settings['selected_icon'], [ 'aria-hidden' => 'true' ] );
553 else : ?>
554 <i class="<?php echo esc_attr( $settings['icon'] ); ?>" aria-hidden="true"></i>
555 <?php endif; ?>
556 </span>
557 <?php endif; ?>
558 <span <?php $instance->print_render_attribute_string( 'text' ); ?>><?php $this->print_unescaped_setting( 'text' ); ?></span>
559 </span>
560 <?php
561 }
562
563 public function on_import( $element ) {
564 return Icons_Manager::on_import_migration( $element, 'icon', 'selected_icon' );
565 }
566 }
567