| 1 |
<?php |
| 2 |
namespace Elementor\Modules\AtomicWidgets\Elements\Atomic_Heading; |
| 3 |
|
| 4 |
use Elementor\Modules\AtomicWidgets\Controls\Section; |
| 5 |
use Elementor\Modules\AtomicWidgets\Controls\Types\Inline_Editing_Control; |
| 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\Text_Control; |
| 9 |
use Elementor\Modules\AtomicWidgets\Elements\Base\Atomic_Widget_Base; |
| 10 |
use Elementor\Modules\AtomicWidgets\Elements\Base\Has_Template; |
| 11 |
use Elementor\Modules\AtomicWidgets\Elements\Base\Html_Tag_Computer; |
| 12 |
use Elementor\Modules\AtomicWidgets\PropTypes\Attributes_Prop_Type; |
| 13 |
use Elementor\Modules\AtomicWidgets\PropTypes\Classes_Prop_Type; |
| 14 |
use Elementor\Modules\AtomicWidgets\PropTypes\Escaped_Html_Prop_Type; |
| 15 |
use Elementor\Modules\AtomicWidgets\PropTypes\Link_Prop_Type; |
| 16 |
use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type; |
| 17 |
use Elementor\Modules\AtomicWidgets\PropTypes\Size_Prop_Type; |
| 18 |
use Elementor\Modules\AtomicWidgets\Styles\Style_Definition; |
| 19 |
use Elementor\Modules\AtomicWidgets\Styles\Style_Variant; |
| 20 |
use Elementor\Modules\Components\PropTypes\Overridable_Prop_Type; |
| 21 |
|
| 22 |
if ( ! defined( 'ABSPATH' ) ) { |
| 23 |
exit; // Exit if accessed directly. |
| 24 |
} |
| 25 |
|
| 26 |
class Atomic_Heading extends Atomic_Widget_Base { |
| 27 |
use Has_Template; |
| 28 |
|
| 29 |
const LINK_BASE_STYLE_KEY = 'link-base'; |
| 30 |
|
| 31 |
public static $widget_description = 'Display a heading with customizable tag, styles, and link options.'; |
| 32 |
|
| 33 |
public static function get_element_type(): string { |
| 34 |
return 'e-heading'; |
| 35 |
} |
| 36 |
|
| 37 |
public function get_title() { |
| 38 |
return esc_html__( 'Heading', 'elementor' ); |
| 39 |
} |
| 40 |
|
| 41 |
public function get_keywords() { |
| 42 |
return [ 'ato', 'atom', 'atoms', 'atomic', 'heading', 'title', 'text', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ]; |
| 43 |
} |
| 44 |
|
| 45 |
public function get_icon() { |
| 46 |
return 'eicon-e-heading'; |
| 47 |
} |
| 48 |
|
| 49 |
public static function html_tag_follows_link(): bool { |
| 50 |
return false; |
| 51 |
} |
| 52 |
|
| 53 |
public static function get_computed_html_tag( array $settings ): string { |
| 54 |
return Html_Tag_Computer::compute( $settings, 'h2', [ |
| 55 |
Html_Tag_Computer::FOLLOW_LINK_OPTION => static::html_tag_follows_link(), |
| 56 |
] ); |
| 57 |
} |
| 58 |
|
| 59 |
protected static function define_props_schema(): array { |
| 60 |
return [ |
| 61 |
'classes' => Classes_Prop_Type::make() |
| 62 |
->default( [] ), |
| 63 |
|
| 64 |
'tag' => String_Prop_Type::make() |
| 65 |
->enum( [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ] ) |
| 66 |
->default( 'h2' ) |
| 67 |
->description( 'The HTML tag for the heading element. One of: h1, h2, h3, h4, h5, or h6. Do not use p, span, or div.' ), |
| 68 |
|
| 69 |
'title' => Escaped_Html_Prop_Type::make() |
| 70 |
->default( __( 'This is a title', 'elementor' ) ) |
| 71 |
->description( 'The text content of the heading.' ) |
| 72 |
->alias( 'text', 'content', 'heading' ), |
| 73 |
|
| 74 |
'link' => Link_Prop_Type::make(), |
| 75 |
|
| 76 |
'attributes' => Attributes_Prop_Type::make()->meta( Overridable_Prop_Type::ignore() ), |
| 77 |
]; |
| 78 |
} |
| 79 |
|
| 80 |
protected function define_atomic_controls(): array { |
| 81 |
$content_section = Section::make() |
| 82 |
->set_label( __( 'Content', 'elementor' ) ) |
| 83 |
->set_id( 'content' ) |
| 84 |
->set_items( [ |
| 85 |
Inline_Editing_Control::bind_to( 'title' ) |
| 86 |
->set_placeholder( __( 'Type your title here', 'elementor' ) ) |
| 87 |
->set_label( __( 'Title', 'elementor' ) ), |
| 88 |
] ); |
| 89 |
|
| 90 |
return [ |
| 91 |
$content_section, |
| 92 |
Section::make() |
| 93 |
->set_label( __( 'Settings', 'elementor' ) ) |
| 94 |
->set_id( 'settings' ) |
| 95 |
->set_items( $this->get_settings_controls() ), |
| 96 |
]; |
| 97 |
} |
| 98 |
|
| 99 |
protected function get_settings_controls(): array { |
| 100 |
return [ |
| 101 |
Select_Control::bind_to( 'tag' ) |
| 102 |
->set_options([ |
| 103 |
[ |
| 104 |
'value' => 'h1', |
| 105 |
'label' => 'H1', |
| 106 |
], |
| 107 |
[ |
| 108 |
'value' => 'h2', |
| 109 |
'label' => 'H2', |
| 110 |
], |
| 111 |
[ |
| 112 |
'value' => 'h3', |
| 113 |
'label' => 'H3', |
| 114 |
], |
| 115 |
[ |
| 116 |
'value' => 'h4', |
| 117 |
'label' => 'H4', |
| 118 |
], |
| 119 |
[ |
| 120 |
'value' => 'h5', |
| 121 |
'label' => 'H5', |
| 122 |
], |
| 123 |
[ |
| 124 |
'value' => 'h6', |
| 125 |
'label' => 'H6', |
| 126 |
], |
| 127 |
]) |
| 128 |
->set_label( __( 'Tag', 'elementor' ) ), |
| 129 |
Link_Control::bind_to( 'link' ) |
| 130 |
->set_placeholder( __( 'Type or paste your URL', 'elementor' ) ) |
| 131 |
->set_label( __( 'Link', 'elementor' ) ) |
| 132 |
->set_meta( [ |
| 133 |
'topDivider' => true, |
| 134 |
] ), |
| 135 |
Text_Control::bind_to( '_cssid' ) |
| 136 |
->set_label( __( 'ID', 'elementor' ) ) |
| 137 |
->set_meta( $this->get_css_id_control_meta() ), |
| 138 |
]; |
| 139 |
} |
| 140 |
|
| 141 |
protected function define_base_styles(): array { |
| 142 |
$margin_value = Size_Prop_Type::generate( [ |
| 143 |
'unit' => 'px', |
| 144 |
'size' => 0 , |
| 145 |
] ); |
| 146 |
|
| 147 |
return [ |
| 148 |
'base' => Style_Definition::make() |
| 149 |
->add_variant( |
| 150 |
Style_Variant::make() |
| 151 |
->add_prop( 'margin', $margin_value ) |
| 152 |
), |
| 153 |
self::LINK_BASE_STYLE_KEY => Style_Definition::make() |
| 154 |
->add_variant( |
| 155 |
Style_Variant::make() |
| 156 |
->add_prop( 'all', 'unset' ) |
| 157 |
->add_prop( 'cursor', 'pointer' ) |
| 158 |
), |
| 159 |
]; |
| 160 |
} |
| 161 |
|
| 162 |
protected function get_templates(): array { |
| 163 |
return [ |
| 164 |
'elementor/elements/atomic-heading' => __DIR__ . '/atomic-heading.html.twig', |
| 165 |
]; |
| 166 |
} |
| 167 |
|
| 168 |
public function render_markdown(): string { |
| 169 |
$settings = $this->get_atomic_settings(); |
| 170 |
$title = wp_strip_all_tags( $settings['title'] ?? '' ); |
| 171 |
|
| 172 |
if ( empty( $title ) ) { |
| 173 |
return ''; |
| 174 |
} |
| 175 |
|
| 176 |
$tag = $settings['tag'] ?? 'h2'; |
| 177 |
$level_map = [ |
| 178 |
'h1' => 1, |
| 179 |
'h2' => 2, |
| 180 |
'h3' => 3, |
| 181 |
'h4' => 4, |
| 182 |
'h5' => 5, |
| 183 |
'h6' => 6, |
| 184 |
]; |
| 185 |
$level = $level_map[ $tag ] ?? 2; |
| 186 |
|
| 187 |
$md = str_repeat( '#', $level ) . ' ' . $title; |
| 188 |
|
| 189 |
if ( ! empty( $settings['link']['href'] ) ) { |
| 190 |
$md = str_repeat( '#', $level ) . ' [' . $title . '](' . esc_url( $settings['link']['href'] ) . ')'; |
| 191 |
} |
| 192 |
|
| 193 |
return $md; |
| 194 |
} |
| 195 |
} |
| 196 |
|