| 1 |
<?php |
| 2 |
namespace Elementor\Modules\AtomicWidgets\Widgets; |
| 3 |
|
| 4 |
use Elementor\Modules\AtomicWidgets\Controls\Dynamic_Section; |
| 5 |
use Elementor\Modules\AtomicWidgets\Controls\Section; |
| 6 |
use Elementor\Modules\AtomicWidgets\Controls\Types\Link_Control; |
| 7 |
use Elementor\Modules\AtomicWidgets\Controls\Types\Select_Control; |
| 8 |
use Elementor\Modules\AtomicWidgets\Controls\Types\Textarea_Control; |
| 9 |
use Elementor\Modules\AtomicWidgets\Base\Atomic_Widget_Base; |
| 10 |
use Elementor\Modules\AtomicWidgets\PropTypes\Classes_Prop_Type; |
| 11 |
use Elementor\Modules\AtomicWidgets\PropTypes\Link_Prop_Type; |
| 12 |
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type; |
| 13 |
use Elementor\Utils; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
class Atomic_Heading extends Atomic_Widget_Base { |
| 20 |
public function get_name() { |
| 21 |
return 'a-heading'; |
| 22 |
} |
| 23 |
|
| 24 |
public function get_title() { |
| 25 |
return esc_html__( 'Atomic Heading', 'elementor' ); |
| 26 |
} |
| 27 |
|
| 28 |
public function get_icon() { |
| 29 |
return 'eicon-t-letter'; |
| 30 |
} |
| 31 |
|
| 32 |
protected function render() { |
| 33 |
$settings = $this->get_atomic_settings(); |
| 34 |
|
| 35 |
$format = $this->get_heading_template( ! empty( $settings['link']['href'] ) ); |
| 36 |
$args = $this->get_template_args( $settings ); |
| 37 |
|
| 38 |
echo sprintf( $format, ...$args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 39 |
} |
| 40 |
|
| 41 |
private function get_heading_template( bool $is_link_enabled ): string { |
| 42 |
return $is_link_enabled ? '<%1$s %2$s><a %3$s>%4$s</a></%1$s>' : '<%1$s %2$s>%3$s</%1$s>'; |
| 43 |
} |
| 44 |
|
| 45 |
private function get_template_args( array $settings ): array { |
| 46 |
$tag = $settings['tag']; |
| 47 |
$title = esc_html( $settings['title'] ); |
| 48 |
$attrs = array_filter( [ |
| 49 |
'class' => $settings['classes'] ?? '', |
| 50 |
] ); |
| 51 |
|
| 52 |
$default_args = [ |
| 53 |
Utils::validate_html_tag( $tag ), |
| 54 |
Utils::render_html_attributes( $attrs ), |
| 55 |
]; |
| 56 |
|
| 57 |
if ( ! empty( $settings['link']['href'] ) ) { |
| 58 |
$link_args = [ |
| 59 |
Utils::render_html_attributes( $settings['link'] ), |
| 60 |
esc_html( $title ), |
| 61 |
]; |
| 62 |
|
| 63 |
return array_merge( $default_args, $link_args ); |
| 64 |
} |
| 65 |
|
| 66 |
$default_args[] = $title; |
| 67 |
|
| 68 |
return $default_args; |
| 69 |
} |
| 70 |
|
| 71 |
protected function define_atomic_controls(): array { |
| 72 |
return [ |
| 73 |
Section::make() |
| 74 |
->set_label( __( 'Content', 'elementor' ) ) |
| 75 |
->set_items( [ |
| 76 |
Select_Control::bind_to( 'tag' ) |
| 77 |
->set_label( esc_html__( 'Tag', 'elementor' ) ) |
| 78 |
->set_options( [ |
| 79 |
[ |
| 80 |
'value' => 'h1', |
| 81 |
'label' => 'H1', |
| 82 |
], |
| 83 |
[ |
| 84 |
'value' => 'h2', |
| 85 |
'label' => 'H2', |
| 86 |
], |
| 87 |
[ |
| 88 |
'value' => 'h3', |
| 89 |
'label' => 'H3', |
| 90 |
], |
| 91 |
[ |
| 92 |
'value' => 'h4', |
| 93 |
'label' => 'H4', |
| 94 |
], |
| 95 |
[ |
| 96 |
'value' => 'h5', |
| 97 |
'label' => 'H5', |
| 98 |
], |
| 99 |
[ |
| 100 |
'value' => 'h6', |
| 101 |
'label' => 'H6', |
| 102 |
], |
| 103 |
]), |
| 104 |
|
| 105 |
Textarea_Control::bind_to( 'title' ) |
| 106 |
->set_label( __( 'Title', 'elementor' ) ) |
| 107 |
->set_placeholder( __( 'Type your title here', 'elementor' ) ), |
| 108 |
|
| 109 |
Link_Control::bind_to( 'link' ), |
| 110 |
] ), |
| 111 |
]; |
| 112 |
} |
| 113 |
|
| 114 |
protected static function define_props_schema(): array { |
| 115 |
return [ |
| 116 |
'classes' => Classes_Prop_Type::make() |
| 117 |
->default( [] ), |
| 118 |
|
| 119 |
'tag' => String_Prop_Type::make() |
| 120 |
->enum( [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ] ) |
| 121 |
->default( 'h2' ), |
| 122 |
|
| 123 |
'title' => String_Prop_Type::make() |
| 124 |
->default( __( 'Your Title Here', 'elementor' ) ), |
| 125 |
|
| 126 |
'link' => Link_Prop_Type::make(), |
| 127 |
]; |
| 128 |
} |
| 129 |
} |
| 130 |
|