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

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