Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
502 lines (449 sloc) 15.4 KB
<?php
namespace MCCustomWidget\Widgets;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Scheme_Typography;
use Elementor\Scheme_Color;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* Elementor Button fixed
*
* Elementor widget for fixed button.
*
* @since 1.0.0
*/
class MC_Button_Fixed extends Widget_Base {
/**
* Retrieve button widget name.
*
* @access public
*
* @return string Widget name.
*/
public function get_name() {
return 'button-fixed';
}
/**
* Retrieve button widget title.
*
* @access public
*
* @return string Widget title.
*/
public function get_title() {
return __( 'Button Fixed', 'elementor' );
}
/**
* Retrieve button widget icon.
*
* @access public
*
* @return string Widget icon.
*/
public function get_icon() {
return 'eicon-button';
}
/**
* Retrieve the list of categories the widget belongs to.
*
* Used to determine where to display the widget in the editor.
*
* Note that currently Elementor supports only one category.
* When multiple categories passed, Elementor uses the first one.
*
* @since 1.0.0
*
* @access public
*
* @return array Widget categories.
*/
public function get_categories() {
return [ 'general' ];
}
/**
* Retrieve the list of scripts the widget depended on.
*
* Used to set scripts dependencies required to run the widget.
*
* @since 1.0.0
*
* @access public
*
* @return array Widget scripts dependencies.
*/
public function get_script_depends() {
return [ 'button-fixed-js' ];
}
/**
* Retrieve button sizes.
*
* @access public
* @static
*
* @return array An array containing button sizes.
*/
public static function get_button_sizes() {
return [
'xs' => __( 'Extra Small', 'elementor' ),
'sm' => __( 'Small', 'elementor' ),
'md' => __( 'Medium', 'elementor' ),
'lg' => __( 'Large', 'elementor' ),
'xl' => __( 'Extra Large', 'elementor' ),
];
}
/**
* Register button widget controls.
*
* Adds different input fields to allow the user to change and customize the widget settings.
*
* @access protected
*/
protected function _register_controls() {
$this->start_controls_section(
'section_button',
[
'label' => __( 'Button Fixed', 'elementor' ),
]
);
$this->add_control(
'button_type',
[
'label' => __( 'Type', 'elementor' ),
'type' => Controls_Manager::SELECT,
'default' => '',
'options' => [
'' => __( 'Default', 'elementor' ),
'info' => __( 'Info', 'elementor' ),
'success' => __( 'Success', 'elementor' ),
'warning' => __( 'Warning', 'elementor' ),
'danger' => __( 'Danger', 'elementor' ),
],
'prefix_class' => 'elementor-button-',
]
);
$this->add_control(
'text',
[
'label' => __( 'Text', 'elementor' ),
'type' => Controls_Manager::TEXT,
'default' => __( 'Click me', 'elementor' ),
'placeholder' => __( 'Click me', 'elementor' ),
]
);
$this->add_control(
'link',
[
'label' => __( 'Link', 'elementor' ),
'type' => Controls_Manager::URL,
'placeholder' => 'http://your-link.com',
'default' => [
'url' => '#',
],
]
);
$this->add_responsive_control(
'align_fixed',
[
'label' => __( 'Alignment', 'elementor' ),
'type' => Controls_Manager::CHOOSE,
'options' => [
'left_fixed' => [
'title' => __( 'Left', 'elementor' ),
'icon' => 'fa fa-align-left',
],
'right_fixed' => [
'title' => __( 'Right', 'elementor' ),
'icon' => 'fa fa-align-right',
],
],
'prefix_class' => 'elementor%s-align-fixed',
'default' => 'left_fixed',
]
);
$this->add_control(
'size',
[
'label' => __( 'Size', 'elementor' ),
'type' => Controls_Manager::SELECT,
'default' => 'sm',
'options' => self::get_button_sizes(),
]
);
$this->add_control(
'icon',
[
'label' => __( 'Icon', 'elementor' ),
'type' => Controls_Manager::ICON,
'label_block' => true,
'default' => '',
]
);
$this->add_control(
'icon_align',
[
'label' => __( 'Icon Position', 'elementor' ),
'type' => Controls_Manager::SELECT,
'default' => 'left',
'options' => [
'left' => __( 'Before', 'elementor' ),
'right' => __( 'After', 'elementor' ),
],
'condition' => [
'icon!' => '',
],
]
);
$this->add_control(
'icon_indent',
[
'label' => __( 'Icon Spacing', 'elementor' ),
'type' => Controls_Manager::SLIDER,
'range' => [
'px' => [
'max' => 50,
],
],
'condition' => [
'icon!' => '',
],
'selectors' => [
'{{WRAPPER}} .elementor-button .elementor-align-icon-right' => 'margin-left: {{SIZE}}{{UNIT}};',
'{{WRAPPER}} .elementor-button .elementor-align-icon-left' => 'margin-right: {{SIZE}}{{UNIT}};',
],
]
);
$this->add_control(
'view',
[
'label' => __( 'View', 'elementor' ),
'type' => Controls_Manager::HIDDEN,
'default' => 'traditional',
]
);
$this->end_controls_section();
$this->start_controls_section(
'section_style',
[
'label' => __( 'Button', 'elementor' ),
'tab' => Controls_Manager::TAB_STYLE,
]
);
$this->add_group_control(
Group_Control_Typography::get_type(),
[
'name' => 'typography',
'label' => __( 'Typography', 'elementor' ),
'scheme' => Scheme_Typography::TYPOGRAPHY_4,
'selector' => '{{WRAPPER}} a.elementor-button, {{WRAPPER}} .elementor-button',
]
);
$this->start_controls_tabs( 'tabs_button_style' );
$this->start_controls_tab(
'tab_button_normal',
[
'label' => __( 'Normal', 'elementor' ),
]
);
$this->add_control(
'button_text_color',
[
'label' => __( 'Text Color', 'elementor' ),
'type' => Controls_Manager::COLOR,
'default' => '',
'selectors' => [
'{{WRAPPER}} a.elementor-button, {{WRAPPER}} .elementor-button' => 'color: {{VALUE}};',
],
]
);
$this->add_control(
'background_color',
[
'label' => __( 'Background Color', 'elementor' ),
'type' => Controls_Manager::COLOR,
'scheme' => [
'type' => Scheme_Color::get_type(),
'value' => Scheme_Color::COLOR_4,
],
'selectors' => [
'{{WRAPPER}} a.elementor-button, {{WRAPPER}} .elementor-button' => 'background-color: {{VALUE}};',
],
]
);
$this->end_controls_tab();
$this->start_controls_tab(
'tab_button_hover',
[
'label' => __( 'Hover', 'elementor' ),
]
);
$this->add_control(
'hover_color',
[
'label' => __( 'Text Color', 'elementor' ),
'type' => Controls_Manager::COLOR,
'selectors' => [
'{{WRAPPER}} a.elementor-button:hover, {{WRAPPER}} .elementor-button:hover' => 'color: {{VALUE}};',
],
]
);
$this->add_control(
'button_background_hover_color',
[
'label' => __( 'Background Color', 'elementor' ),
'type' => Controls_Manager::COLOR,
'selectors' => [
'{{WRAPPER}} a.elementor-button:hover, {{WRAPPER}} .elementor-button:hover' => 'background-color: {{VALUE}};',
],
]
);
$this->add_control(
'button_hover_border_color',
[
'label' => __( 'Border Color', 'elementor' ),
'type' => Controls_Manager::COLOR,
'condition' => [
'border_border!' => '',
],
'selectors' => [
'{{WRAPPER}} a.elementor-button:hover, {{WRAPPER}} .elementor-button:hover' => 'border-color: {{VALUE}};',
],
]
);
$this->add_control(
'hover_animation',
[
'label' => __( 'Animation', 'elementor' ),
'type' => Controls_Manager::HOVER_ANIMATION,
]
);
$this->end_controls_tab();
$this->end_controls_tabs();
$this->add_group_control(
Group_Control_Border::get_type(),
[
'name' => 'border',
'label' => __( 'Border', 'elementor' ),
'placeholder' => '1px',
'default' => '1px',
'selector' => '{{WRAPPER}} .elementor-button',
'separator' => 'before',
]
);
$this->add_control(
'border_radius',
[
'label' => __( 'Border Radius', 'elementor' ),
'type' => Controls_Manager::DIMENSIONS,
'size_units' => [ 'px', '%' ],
'selectors' => [
'{{WRAPPER}} a.elementor-button, {{WRAPPER}} .elementor-button' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
],
]
);
$this->add_group_control(
Group_Control_Box_Shadow::get_type(),
[
'name' => 'button_box_shadow',
'selector' => '{{WRAPPER}} .elementor-button',
]
);
$this->add_control(
'text_padding',
[
'label' => __( 'Text Padding', 'elementor' ),
'type' => Controls_Manager::DIMENSIONS,
'size_units' => [ 'px', 'em', '%' ],
'selectors' => [
'{{WRAPPER}} a.elementor-button, {{WRAPPER}} .elementor-button' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
],
'separator' => 'before',
]
);
$this->end_controls_section();
}
/**
* Render button widget output on the frontend.
*
* Written in PHP and used to generate the final HTML.
*
* @access protected
*/
protected function render() {
$settings = $this->get_settings();
$this->add_render_attribute( 'wrapper', 'class', 'elementor-button-wrapper '. $settings['align_fixed']);
if ( ! empty( $settings['link']['url'] ) ) {
$this->add_render_attribute( 'button', 'href', $settings['link']['url'] );
$this->add_render_attribute( 'button', 'class', 'elementor-button-link' );
if ( $settings['link']['is_external'] ) {
$this->add_render_attribute( 'button', 'target', '_blank' );
}
if ( $settings['link']['nofollow'] ) {
$this->add_render_attribute( 'button', 'rel', 'nofollow' );
}
}
$this->add_render_attribute( 'button', 'class', 'elementor-button' );
if ( ! empty( $settings['size'] ) ) {
$this->add_render_attribute( 'button', 'class', 'elementor-size-' . $settings['size'] );
}
if ( $settings['hover_animation'] ) {
$this->add_render_attribute( 'button', 'class', 'elementor-animation-' . $settings['hover_animation'] );
}
if ( $settings['align_fixed'] ) {
$this->add_render_attribute( 'button', 'class', 'elementor-fixed-pos-' . $settings['align_fixed'] );
}
?>
<div <?php echo $this->get_render_attribute_string( 'wrapper' ); ?>>
<a <?php echo $this->get_render_attribute_string( 'button' ); ?>>
<?php $this->render_text(); ?>
</a>
</div>
<?php
}
/**
* Render button widget output in the editor.
*
* Written as a Backbone JavaScript template and used to generate the live preview.
*
* @access protected
*/
protected function _content_template() {
?>
<div class="elementor-button-wrapper button-fixed-wrapper {{ settings.align_fixed }}">
<a class="elementor-button elementor-size-{{ settings.size }} elementor-animation-{{ settings.hover_animation }}" href="{{ settings.link.url }}">
<span class="elementor-button-content-wrapper">
<# if ( settings.icon ) { #>
<span class="elementor-button-icon elementor-align-icon-{{ settings.icon_align }}">
<i class="{{ settings.icon }}"></i>
</span>
<# } #>
<span class="elementor-button-text">{{{ settings.text }}}</span>
</span>
</a>
</div>
<?php
}
/**
* Render button widget text.
*
* @access protected
*/
protected function render_text() {
$settings = $this->get_settings();
$this->add_render_attribute( 'content-wrapper', 'class', 'elementor-button-content-wrapper' );
$this->add_render_attribute( 'icon-align', 'class', 'elementor-align-icon-' . $settings['icon_align'] );
$this->add_render_attribute( 'icon-align', 'class', 'elementor-button-icon' );
?>
<span <?php echo $this->get_render_attribute_string( 'content-wrapper' ); ?>>
<?php if ( ! empty( $settings['icon'] ) ) : ?>
<span <?php echo $this->get_render_attribute_string( 'icon-align' ); ?>>
<i class="<?php echo esc_attr( $settings['icon'] ); ?>"></i>
</span>
<?php endif; ?>
<span class="elementor-button-text"><?php echo $settings['text']; ?></span>
</span>
<?php
}
}