button.php (14891B)
1 <?php 2 namespace Elementor; 3 4 if ( ! defined( 'ABSPATH' ) ) { 5 exit; // Exit if accessed directly. 6 } 7 8 use Elementor\Core\Kits\Documents\Tabs\Global_Colors; 9 use Elementor\Core\Kits\Documents\Tabs\Global_Typography; 10 11 /** 12 * Elementor button widget. 13 * 14 * Elementor widget that displays a button with the ability to control every 15 * aspect of the button design. 16 * 17 * @since 1.0.0 18 */ 19 class Widget_Button extends Widget_Base { 20 21 /** 22 * Get widget name. 23 * 24 * Retrieve button widget name. 25 * 26 * @since 1.0.0 27 * @access public 28 * 29 * @return string Widget name. 30 */ 31 public function get_name() { 32 return 'button'; 33 } 34 35 /** 36 * Get widget title. 37 * 38 * Retrieve button widget title. 39 * 40 * @since 1.0.0 41 * @access public 42 * 43 * @return string Widget title. 44 */ 45 public function get_title() { 46 return esc_html__( 'Button', 'elementor' ); 47 } 48 49 /** 50 * Get widget icon. 51 * 52 * Retrieve button widget icon. 53 * 54 * @since 1.0.0 55 * @access public 56 * 57 * @return string Widget icon. 58 */ 59 public function get_icon() { 60 return 'eicon-button'; 61 } 62 63 /** 64 * Get widget categories. 65 * 66 * Retrieve the list of categories the button widget belongs to. 67 * 68 * Used to determine where to display the widget in the editor. 69 * 70 * @since 2.0.0 71 * @access public 72 * 73 * @return array Widget categories. 74 */ 75 public function get_categories() { 76 return [ 'basic' ]; 77 } 78 79 /** 80 * Get button sizes. 81 * 82 * Retrieve an array of button sizes for the button widget. 83 * 84 * @since 1.0.0 85 * @access public 86 * @static 87 * 88 * @return array An array containing button sizes. 89 */ 90 public static function get_button_sizes() { 91 return [ 92 'xs' => esc_html__( 'Extra Small', 'elementor' ), 93 'sm' => esc_html__( 'Small', 'elementor' ), 94 'md' => esc_html__( 'Medium', 'elementor' ), 95 'lg' => esc_html__( 'Large', 'elementor' ), 96 'xl' => esc_html__( 'Extra Large', 'elementor' ), 97 ]; 98 } 99 100 /** 101 * Register button widget controls. 102 * 103 * Adds different input fields to allow the user to change and customize the widget settings. 104 * 105 * @since 3.1.0 106 * @access protected 107 */ 108 protected function register_controls() { 109 $this->start_controls_section( 110 'section_button', 111 [ 112 'label' => esc_html__( 'Button', 'elementor' ), 113 ] 114 ); 115 116 $this->add_control( 117 'button_type', 118 [ 119 'label' => esc_html__( 'Type', 'elementor' ), 120 'type' => Controls_Manager::SELECT, 121 'default' => '', 122 'options' => [ 123 '' => esc_html__( 'Default', 'elementor' ), 124 'info' => esc_html__( 'Info', 'elementor' ), 125 'success' => esc_html__( 'Success', 'elementor' ), 126 'warning' => esc_html__( 'Warning', 'elementor' ), 127 'danger' => esc_html__( 'Danger', 'elementor' ), 128 ], 129 'prefix_class' => 'elementor-button-', 130 ] 131 ); 132 133 $this->add_control( 134 'text', 135 [ 136 'label' => esc_html__( 'Text', 'elementor' ), 137 'type' => Controls_Manager::TEXT, 138 'dynamic' => [ 139 'active' => true, 140 ], 141 'default' => esc_html__( 'Click here', 'elementor' ), 142 'placeholder' => esc_html__( 'Click here', 'elementor' ), 143 ] 144 ); 145 146 $this->add_control( 147 'link', 148 [ 149 'label' => esc_html__( 'Link', 'elementor' ), 150 'type' => Controls_Manager::URL, 151 'dynamic' => [ 152 'active' => true, 153 ], 154 'placeholder' => esc_html__( 'https://your-link.com', 'elementor' ), 155 'default' => [ 156 'url' => '#', 157 ], 158 ] 159 ); 160 161 $this->add_responsive_control( 162 'align', 163 [ 164 'label' => esc_html__( 'Alignment', 'elementor' ), 165 'type' => Controls_Manager::CHOOSE, 166 'options' => [ 167 'left' => [ 168 'title' => esc_html__( 'Left', 'elementor' ), 169 'icon' => 'eicon-text-align-left', 170 ], 171 'center' => [ 172 'title' => esc_html__( 'Center', 'elementor' ), 173 'icon' => 'eicon-text-align-center', 174 ], 175 'right' => [ 176 'title' => esc_html__( 'Right', 'elementor' ), 177 'icon' => 'eicon-text-align-right', 178 ], 179 'justify' => [ 180 'title' => esc_html__( 'Justified', 'elementor' ), 181 'icon' => 'eicon-text-align-justify', 182 ], 183 ], 184 'prefix_class' => 'elementor%s-align-', 185 'default' => '', 186 ] 187 ); 188 189 $this->add_control( 190 'size', 191 [ 192 'label' => esc_html__( 'Size', 'elementor' ), 193 'type' => Controls_Manager::SELECT, 194 'default' => 'sm', 195 'options' => self::get_button_sizes(), 196 'style_transfer' => true, 197 ] 198 ); 199 200 $this->add_control( 201 'selected_icon', 202 [ 203 'label' => esc_html__( 'Icon', 'elementor' ), 204 'type' => Controls_Manager::ICONS, 205 'fa4compatibility' => 'icon', 206 'skin' => 'inline', 207 'label_block' => false, 208 ] 209 ); 210 211 $this->add_control( 212 'icon_align', 213 [ 214 'label' => esc_html__( 'Icon Position', 'elementor' ), 215 'type' => Controls_Manager::SELECT, 216 'default' => 'left', 217 'options' => [ 218 'left' => esc_html__( 'Before', 'elementor' ), 219 'right' => esc_html__( 'After', 'elementor' ), 220 ], 221 'condition' => [ 222 'selected_icon[value]!' => '', 223 ], 224 ] 225 ); 226 227 $this->add_control( 228 'icon_indent', 229 [ 230 'label' => esc_html__( 'Icon Spacing', 'elementor' ), 231 'type' => Controls_Manager::SLIDER, 232 'range' => [ 233 'px' => [ 234 'max' => 50, 235 ], 236 ], 237 'selectors' => [ 238 '{{WRAPPER}} .elementor-button .elementor-align-icon-right' => 'margin-left: {{SIZE}}{{UNIT}};', 239 '{{WRAPPER}} .elementor-button .elementor-align-icon-left' => 'margin-right: {{SIZE}}{{UNIT}};', 240 ], 241 ] 242 ); 243 244 $this->add_control( 245 'view', 246 [ 247 'label' => esc_html__( 'View', 'elementor' ), 248 'type' => Controls_Manager::HIDDEN, 249 'default' => 'traditional', 250 ] 251 ); 252 253 $this->add_control( 254 'button_css_id', 255 [ 256 'label' => esc_html__( 'Button ID', 'elementor' ), 257 'type' => Controls_Manager::TEXT, 258 'dynamic' => [ 259 'active' => true, 260 ], 261 'default' => '', 262 'title' => esc_html__( 'Add your custom id WITHOUT the Pound key. e.g: my-id', 'elementor' ), 263 'description' => sprintf( 264 /* translators: %1$s Code open tag, %2$s: Code close tag. */ 265 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' ), 266 '<code>', 267 '</code>' 268 ), 269 'separator' => 'before', 270 271 ] 272 ); 273 274 $this->end_controls_section(); 275 276 $this->start_controls_section( 277 'section_style', 278 [ 279 'label' => esc_html__( 'Button', 'elementor' ), 280 'tab' => Controls_Manager::TAB_STYLE, 281 ] 282 ); 283 284 $this->add_group_control( 285 Group_Control_Typography::get_type(), 286 [ 287 'name' => 'typography', 288 'global' => [ 289 'default' => Global_Typography::TYPOGRAPHY_ACCENT, 290 ], 291 'selector' => '{{WRAPPER}} .elementor-button', 292 ] 293 ); 294 295 $this->add_group_control( 296 Group_Control_Text_Shadow::get_type(), 297 [ 298 'name' => 'text_shadow', 299 'selector' => '{{WRAPPER}} .elementor-button', 300 ] 301 ); 302 303 $this->start_controls_tabs( 'tabs_button_style' ); 304 305 $this->start_controls_tab( 306 'tab_button_normal', 307 [ 308 'label' => esc_html__( 'Normal', 'elementor' ), 309 ] 310 ); 311 312 $this->add_control( 313 'button_text_color', 314 [ 315 'label' => esc_html__( 'Text Color', 'elementor' ), 316 'type' => Controls_Manager::COLOR, 317 'default' => '', 318 'selectors' => [ 319 '{{WRAPPER}} .elementor-button' => 'fill: {{VALUE}}; color: {{VALUE}};', 320 ], 321 ] 322 ); 323 324 $this->add_group_control( 325 Group_Control_Background::get_type(), 326 [ 327 'name' => 'background', 328 'label' => esc_html__( 'Background', 'elementor' ), 329 'types' => [ 'classic', 'gradient' ], 330 'exclude' => [ 'image' ], 331 'selector' => '{{WRAPPER}} .elementor-button', 332 'fields_options' => [ 333 'background' => [ 334 'default' => 'classic', 335 ], 336 'color' => [ 337 'global' => [ 338 'default' => Global_Colors::COLOR_ACCENT, 339 ], 340 ], 341 ], 342 ] 343 ); 344 345 $this->end_controls_tab(); 346 347 $this->start_controls_tab( 348 'tab_button_hover', 349 [ 350 'label' => esc_html__( 'Hover', 'elementor' ), 351 ] 352 ); 353 354 $this->add_control( 355 'hover_color', 356 [ 357 'label' => esc_html__( 'Text Color', 'elementor' ), 358 'type' => Controls_Manager::COLOR, 359 'selectors' => [ 360 '{{WRAPPER}} .elementor-button:hover, {{WRAPPER}} .elementor-button:focus' => 'color: {{VALUE}};', 361 '{{WRAPPER}} .elementor-button:hover svg, {{WRAPPER}} .elementor-button:focus svg' => 'fill: {{VALUE}};', 362 ], 363 ] 364 ); 365 366 $this->add_group_control( 367 Group_Control_Background::get_type(), 368 [ 369 'name' => 'button_background_hover', 370 'label' => esc_html__( 'Background', 'elementor' ), 371 'types' => [ 'classic', 'gradient' ], 372 'exclude' => [ 'image' ], 373 'selector' => '{{WRAPPER}} .elementor-button:hover, {{WRAPPER}} .elementor-button:focus', 374 'fields_options' => [ 375 'background' => [ 376 'default' => 'classic', 377 ], 378 ], 379 ] 380 ); 381 382 $this->add_control( 383 'button_hover_border_color', 384 [ 385 'label' => esc_html__( 'Border Color', 'elementor' ), 386 'type' => Controls_Manager::COLOR, 387 'condition' => [ 388 'border_border!' => '', 389 ], 390 'selectors' => [ 391 '{{WRAPPER}} .elementor-button:hover, {{WRAPPER}} .elementor-button:focus' => 'border-color: {{VALUE}};', 392 ], 393 ] 394 ); 395 396 $this->add_control( 397 'hover_animation', 398 [ 399 'label' => esc_html__( 'Hover Animation', 'elementor' ), 400 'type' => Controls_Manager::HOVER_ANIMATION, 401 ] 402 ); 403 404 $this->end_controls_tab(); 405 406 $this->end_controls_tabs(); 407 408 $this->add_group_control( 409 Group_Control_Border::get_type(), 410 [ 411 'name' => 'border', 412 'selector' => '{{WRAPPER}} .elementor-button', 413 'separator' => 'before', 414 ] 415 ); 416 417 $this->add_control( 418 'border_radius', 419 [ 420 'label' => esc_html__( 'Border Radius', 'elementor' ), 421 'type' => Controls_Manager::DIMENSIONS, 422 'size_units' => [ 'px', '%', 'em' ], 423 'selectors' => [ 424 '{{WRAPPER}} .elementor-button' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', 425 ], 426 ] 427 ); 428 429 $this->add_group_control( 430 Group_Control_Box_Shadow::get_type(), 431 [ 432 'name' => 'button_box_shadow', 433 'selector' => '{{WRAPPER}} .elementor-button', 434 ] 435 ); 436 437 $this->add_responsive_control( 438 'text_padding', 439 [ 440 'label' => esc_html__( 'Padding', 'elementor' ), 441 'type' => Controls_Manager::DIMENSIONS, 442 'size_units' => [ 'px', 'em', '%' ], 443 'selectors' => [ 444 '{{WRAPPER}} .elementor-button' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', 445 ], 446 'separator' => 'before', 447 ] 448 ); 449 450 $this->end_controls_section(); 451 } 452 453 /** 454 * Render button widget output on the frontend. 455 * 456 * Written in PHP and used to generate the final HTML. 457 * 458 * @since 1.0.0 459 * @access protected 460 */ 461 protected function render() { 462 $settings = $this->get_settings_for_display(); 463 464 $this->add_render_attribute( 'wrapper', 'class', 'elementor-button-wrapper' ); 465 466 if ( ! empty( $settings['link']['url'] ) ) { 467 $this->add_link_attributes( 'button', $settings['link'] ); 468 $this->add_render_attribute( 'button', 'class', 'elementor-button-link' ); 469 } 470 471 $this->add_render_attribute( 'button', 'class', 'elementor-button' ); 472 $this->add_render_attribute( 'button', 'role', 'button' ); 473 474 if ( ! empty( $settings['button_css_id'] ) ) { 475 $this->add_render_attribute( 'button', 'id', $settings['button_css_id'] ); 476 } 477 478 if ( ! empty( $settings['size'] ) ) { 479 $this->add_render_attribute( 'button', 'class', 'elementor-size-' . $settings['size'] ); 480 } 481 482 if ( $settings['hover_animation'] ) { 483 $this->add_render_attribute( 'button', 'class', 'elementor-animation-' . $settings['hover_animation'] ); 484 } 485 ?> 486 <div <?php $this->print_render_attribute_string( 'wrapper' ); ?>> 487 <a <?php $this->print_render_attribute_string( 'button' ); ?>> 488 <?php $this->render_text(); ?> 489 </a> 490 </div> 491 <?php 492 } 493 494 /** 495 * Render button widget output in the editor. 496 * 497 * Written as a Backbone JavaScript template and used to generate the live preview. 498 * 499 * @since 2.9.0 500 * @access protected 501 */ 502 protected function content_template() { 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 class="elementor-button-wrapper"> 511 <a id="{{ settings.button_css_id }}" class="elementor-button elementor-size-{{ settings.size }} elementor-animation-{{ settings.hover_animation }}" href="{{ settings.link.url }}" role="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 * @since 1.5.0 535 * @access protected 536 */ 537 protected function render_text() { 538 $settings = $this->get_settings_for_display(); 539 540 $migrated = isset( $settings['__fa4_migrated']['selected_icon'] ); 541 $is_new = empty( $settings['icon'] ) && Icons_Manager::is_migration_allowed(); 542 543 if ( ! $is_new && empty( $settings['icon_align'] ) ) { 544 // @todo: remove when deprecated 545 // added as bc in 2.6 546 //old default 547 $settings['icon_align'] = $this->get_settings( 'icon_align' ); 548 } 549 550 $this->add_render_attribute( [ 551 'content-wrapper' => [ 552 'class' => 'elementor-button-content-wrapper', 553 ], 554 'icon-align' => [ 555 'class' => [ 556 'elementor-button-icon', 557 'elementor-align-icon-' . $settings['icon_align'], 558 ], 559 ], 560 'text' => [ 561 'class' => 'elementor-button-text', 562 ], 563 ] ); 564 565 $this->add_inline_editing_attributes( 'text', 'none' ); 566 ?> 567 <span <?php $this->print_render_attribute_string( 'content-wrapper' ); ?>> 568 <?php if ( ! empty( $settings['icon'] ) || ! empty( $settings['selected_icon']['value'] ) ) : ?> 569 <span <?php $this->print_render_attribute_string( 'icon-align' ); ?>> 570 <?php if ( $is_new || $migrated ) : 571 Icons_Manager::render_icon( $settings['selected_icon'], [ 'aria-hidden' => 'true' ] ); 572 else : ?> 573 <i class="<?php echo esc_attr( $settings['icon'] ); ?>" aria-hidden="true"></i> 574 <?php endif; ?> 575 </span> 576 <?php endif; ?> 577 <span <?php $this->print_render_attribute_string( 'text' ); ?>><?php $this->print_unescaped_setting( 'text' ); ?></span> 578 </span> 579 <?php 580 } 581 582 public function on_import( $element ) { 583 return Icons_Manager::on_import_migration( $element, 'icon', 'selected_icon' ); 584 } 585 }