PluginProbe
Elementor Website Builder – more than just a page builder / 3.14.0-beta4
Elementor Website Builder – more than just a page builder v3.14.0-beta4
4.3.0-beta3 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 All 452 releases
elementor / includes / widgets / traits / button-trait.php

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

594 lines 16.8 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 'ai' => [
220 'active' => false,
221 ],
222 'default' => '',
223 'title' => esc_html__( 'Add your custom id WITHOUT the Pound key. e.g: my-id', 'elementor' ),
224 'description' => sprintf(
225 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' ),
226 '<code>',
227 '</code>'
228 ),
229 'separator' => 'before',
230 'condition' => $args['section_condition'],
231 ]
232 );
233 }
234
235 protected function register_button_style_controls( $args = [] ) {
236 $default_args = [
237 'section_condition' => [],
238 ];
239
240 $args = wp_parse_args( $args, $default_args );
241
242 $this->add_group_control(
243 Group_Control_Typography::get_type(),
244 [
245 'name' => 'typography',
246 'global' => [
247 'default' => Global_Typography::TYPOGRAPHY_ACCENT,
248 ],
249 'selector' => '{{WRAPPER}} .elementor-button',
250 'condition' => $args['section_condition'],
251 ]
252 );
253
254 $this->add_group_control(
255 Group_Control_Text_Shadow::get_type(),
256 [
257 'name' => 'text_shadow',
258 'selector' => '{{WRAPPER}} .elementor-button',
259 'condition' => $args['section_condition'],
260 ]
261 );
262
263 $this->start_controls_tabs( 'tabs_button_style', [
264 'condition' => $args['section_condition'],
265 ] );
266
267 $this->start_controls_tab(
268 'tab_button_normal',
269 [
270 'label' => esc_html__( 'Normal', 'elementor' ),
271 'condition' => $args['section_condition'],
272 ]
273 );
274
275 $this->add_control(
276 'button_text_color',
277 [
278 'label' => esc_html__( 'Text Color', 'elementor' ),
279 'type' => Controls_Manager::COLOR,
280 'default' => '',
281 'selectors' => [
282 '{{WRAPPER}} .elementor-button' => 'fill: {{VALUE}}; color: {{VALUE}};',
283 ],
284 'condition' => $args['section_condition'],
285 ]
286 );
287
288 $this->add_group_control(
289 Group_Control_Background::get_type(),
290 [
291 'name' => 'background',
292 'types' => [ 'classic', 'gradient' ],
293 'exclude' => [ 'image' ],
294 'selector' => '{{WRAPPER}} .elementor-button',
295 'fields_options' => [
296 'background' => [
297 'default' => 'classic',
298 ],
299 'color' => [
300 'global' => [
301 'default' => Global_Colors::COLOR_ACCENT,
302 ],
303 ],
304 ],
305 'condition' => $args['section_condition'],
306 ]
307 );
308
309 $this->end_controls_tab();
310
311 $this->start_controls_tab(
312 'tab_button_hover',
313 [
314 'label' => esc_html__( 'Hover', 'elementor' ),
315 'condition' => $args['section_condition'],
316 ]
317 );
318
319 $this->add_control(
320 'hover_color',
321 [
322 'label' => esc_html__( 'Text Color', 'elementor' ),
323 'type' => Controls_Manager::COLOR,
324 'selectors' => [
325 '{{WRAPPER}} .elementor-button:hover, {{WRAPPER}} .elementor-button:focus' => 'color: {{VALUE}};',
326 '{{WRAPPER}} .elementor-button:hover svg, {{WRAPPER}} .elementor-button:focus svg' => 'fill: {{VALUE}};',
327 ],
328 'condition' => $args['section_condition'],
329 ]
330 );
331
332 $this->add_group_control(
333 Group_Control_Background::get_type(),
334 [
335 'name' => 'button_background_hover',
336 'types' => [ 'classic', 'gradient' ],
337 'exclude' => [ 'image' ],
338 'selector' => '{{WRAPPER}} .elementor-button:hover, {{WRAPPER}} .elementor-button:focus',
339 'fields_options' => [
340 'background' => [
341 'default' => 'classic',
342 ],
343 ],
344 'condition' => $args['section_condition'],
345 ]
346 );
347
348 $this->add_control(
349 'button_hover_border_color',
350 [
351 'label' => esc_html__( 'Border Color', 'elementor' ),
352 'type' => Controls_Manager::COLOR,
353 'condition' => [
354 'border_border!' => '',
355 ],
356 'selectors' => [
357 '{{WRAPPER}} .elementor-button:hover, {{WRAPPER}} .elementor-button:focus' => 'border-color: {{VALUE}};',
358 ],
359 'condition' => $args['section_condition'],
360 ]
361 );
362
363 $this->add_control(
364 'hover_animation',
365 [
366 'label' => esc_html__( 'Hover Animation', 'elementor' ),
367 'type' => Controls_Manager::HOVER_ANIMATION,
368 'condition' => $args['section_condition'],
369 ]
370 );
371
372 $this->end_controls_tab();
373
374 $this->end_controls_tabs();
375
376 $this->add_group_control(
377 Group_Control_Border::get_type(),
378 [
379 'name' => 'border',
380 'selector' => '{{WRAPPER}} .elementor-button',
381 'separator' => 'before',
382 'condition' => $args['section_condition'],
383 ]
384 );
385
386 $this->add_responsive_control(
387 'border_radius',
388 [
389 'label' => esc_html__( 'Border Radius', 'elementor' ),
390 'type' => Controls_Manager::DIMENSIONS,
391 'size_units' => [ 'px', '%', 'em', 'rem', 'custom' ],
392 'selectors' => [
393 '{{WRAPPER}} .elementor-button' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
394 ],
395 'condition' => $args['section_condition'],
396 ]
397 );
398
399 $this->add_group_control(
400 Group_Control_Box_Shadow::get_type(),
401 [
402 'name' => 'button_box_shadow',
403 'selector' => '{{WRAPPER}} .elementor-button',
404 'condition' => $args['section_condition'],
405 ]
406 );
407
408 $this->add_responsive_control(
409 'text_padding',
410 [
411 'label' => esc_html__( 'Padding', 'elementor' ),
412 'type' => Controls_Manager::DIMENSIONS,
413 'size_units' => [ 'px', '%', 'em', 'rem', 'vw', 'custom' ],
414 'selectors' => [
415 '{{WRAPPER}} .elementor-button' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
416 ],
417 'separator' => 'before',
418 'condition' => $args['section_condition'],
419 ]
420 );
421 }
422
423 /**
424 * Render button widget output on the frontend.
425 *
426 * Written in PHP and used to generate the final HTML.
427 *
428 * @param \Elementor\Widget_Base|null $instance
429 *
430 * @since 3.4.0
431 * @access protected
432 */
433 protected function render_button( Widget_Base $instance = null ) {
434 if ( empty( $instance ) ) {
435 $instance = $this;
436 }
437
438 $settings = $instance->get_settings_for_display();
439
440 $instance->add_render_attribute( 'wrapper', 'class', 'elementor-button-wrapper' );
441
442 $instance->add_render_attribute( 'button', 'class', 'elementor-button' );
443
444 if ( ! empty( $settings['link']['url'] ) ) {
445 $instance->add_link_attributes( 'button', $settings['link'] );
446 $instance->add_render_attribute( 'button', 'class', 'elementor-button-link' );
447 } else {
448 $instance->add_render_attribute( 'button', 'role', 'button' );
449 }
450
451 if ( ! empty( $settings['button_css_id'] ) ) {
452 $instance->add_render_attribute( 'button', 'id', $settings['button_css_id'] );
453 }
454
455 if ( ! empty( $settings['size'] ) ) {
456 $instance->add_render_attribute( 'button', 'class', 'elementor-size-' . $settings['size'] );
457 }
458
459 if ( ! empty( $settings['hover_animation'] ) ) {
460 $instance->add_render_attribute( 'button', 'class', 'elementor-animation-' . $settings['hover_animation'] );
461 }
462 ?>
463 <div <?php $instance->print_render_attribute_string( 'wrapper' ); ?>>
464 <a <?php $instance->print_render_attribute_string( 'button' ); ?>>
465 <?php $this->render_text( $instance ); ?>
466 </a>
467 </div>
468 <?php
469 }
470
471 /**
472 * Render button widget output in the editor.
473 *
474 * Written as a Backbone JavaScript template and used to generate the live preview.
475 *
476 * @since 3.4.0
477 * @access protected
478 */
479 protected function content_template() {
480 ?>
481 <#
482 view.addRenderAttribute( 'wrapper', 'class', 'elementor-button-wrapper' );
483
484 view.addRenderAttribute( 'button', 'class', 'elementor-button' );
485
486 if ( '' !== settings.link.url ) {
487 view.addRenderAttribute( 'button', 'href', settings.link.url );
488 view.addRenderAttribute( 'button', 'class', 'elementor-button-link' );
489 } else {
490 view.addRenderAttribute( 'button', 'role', 'button' );
491 }
492
493 if ( '' !== settings.button_css_id ) {
494 view.addRenderAttribute( 'button', 'id', settings.button_css_id );
495 }
496
497 if ( '' !== settings.size ) {
498 view.addRenderAttribute( 'button', 'class', 'elementor-size-' + settings.size );
499 }
500
501 if ( '' !== settings.hover_animation ) {
502 view.addRenderAttribute( 'button', 'class', 'elementor-animation-' + settings.hover_animation );
503 }
504
505 view.addRenderAttribute( 'text', 'class', 'elementor-button-text' );
506 view.addInlineEditingAttributes( 'text', 'none' );
507 var iconHTML = elementor.helpers.renderIcon( view, settings.selected_icon, { 'aria-hidden': true }, 'i' , 'object' ),
508 migrated = elementor.helpers.isIconMigrated( settings, 'selected_icon' );
509 #>
510 <div {{{ view.getRenderAttributeString( 'wrapper' ) }}}>
511 <a {{{ view.getRenderAttributeString( 'button' ) }}}>
512 <span class="elementor-button-content-wrapper">
513 <# if ( settings.icon || settings.selected_icon ) { #>
514 <span class="elementor-button-icon elementor-align-icon-{{ settings.icon_align }}">
515 <# if ( ( migrated || ! settings.icon ) && iconHTML.rendered ) { #>
516 {{{ iconHTML.value }}}
517 <# } else { #>
518 <i class="{{ settings.icon }}" aria-hidden="true"></i>
519 <# } #>
520 </span>
521 <# } #>
522 <span {{{ view.getRenderAttributeString( 'text' ) }}}>{{{ settings.text }}}</span>
523 </span>
524 </a>
525 </div>
526 <?php
527 }
528
529 /**
530 * Render button text.
531 *
532 * Render button widget text.
533 *
534 * @param \Elementor\Widget_Base|null $instance
535 *
536 * @since 3.4.0
537 * @access protected
538 */
539 protected function render_text( Widget_Base $instance = null ) {
540 // 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`.
541 if ( empty( $instance ) ) {
542 $instance = $this;
543 }
544
545 $settings = $instance->get_settings_for_display();
546
547 $migrated = isset( $settings['__fa4_migrated']['selected_icon'] );
548 $is_new = empty( $settings['icon'] ) && Icons_Manager::is_migration_allowed();
549
550 if ( ! $is_new && empty( $settings['icon_align'] ) ) {
551 // @todo: remove when deprecated
552 // added as bc in 2.6
553 //old default
554 $settings['icon_align'] = $instance->get_settings( 'icon_align' );
555 }
556
557 $instance->add_render_attribute( [
558 'content-wrapper' => [
559 'class' => 'elementor-button-content-wrapper',
560 ],
561 'icon-align' => [
562 'class' => [
563 'elementor-button-icon',
564 'elementor-align-icon-' . $settings['icon_align'],
565 ],
566 ],
567 'text' => [
568 'class' => 'elementor-button-text',
569 ],
570 ] );
571
572 // TODO: replace the protected with public
573 //$instance->add_inline_editing_attributes( 'text', 'none' );
574 ?>
575 <span <?php $instance->print_render_attribute_string( 'content-wrapper' ); ?>>
576 <?php if ( ! empty( $settings['icon'] ) || ! empty( $settings['selected_icon']['value'] ) ) : ?>
577 <span <?php $instance->print_render_attribute_string( 'icon-align' ); ?>>
578 <?php if ( $is_new || $migrated ) :
579 Icons_Manager::render_icon( $settings['selected_icon'], [ 'aria-hidden' => 'true' ] );
580 else : ?>
581 <i class="<?php echo esc_attr( $settings['icon'] ); ?>" aria-hidden="true"></i>
582 <?php endif; ?>
583 </span>
584 <?php endif; ?>
585 <span <?php $instance->print_render_attribute_string( 'text' ); ?>><?php $this->print_unescaped_setting( 'text' ); ?></span>
586 </span>
587 <?php
588 }
589
590 public function on_import( $element ) {
591 return Icons_Manager::on_import_migration( $element, 'icon', 'selected_icon' );
592 }
593 }
594