| 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 |
use Elementor\Modules\ContentSanitizer\Interfaces\Sanitizable; |
| 11 |
use Elementor\Modules\Promotions\Controls\Promotion_Control; |
| 12 |
|
| 13 |
/** |
| 14 |
* Elementor heading widget. |
| 15 |
* |
| 16 |
* Elementor widget that displays an eye-catching headlines. |
| 17 |
* |
| 18 |
* @since 1.0.0 |
| 19 |
*/ |
| 20 |
class Widget_Heading extends Widget_Base implements Sanitizable { |
| 21 |
|
| 22 |
/** |
| 23 |
* Get widget name. |
| 24 |
* |
| 25 |
* Retrieve heading widget name. |
| 26 |
* |
| 27 |
* @since 1.0.0 |
| 28 |
* @access public |
| 29 |
* |
| 30 |
* @return string Widget name. |
| 31 |
*/ |
| 32 |
public function get_name() { |
| 33 |
return 'heading'; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Get widget title. |
| 38 |
* |
| 39 |
* Retrieve heading widget title. |
| 40 |
* |
| 41 |
* @since 1.0.0 |
| 42 |
* @access public |
| 43 |
* |
| 44 |
* @return string Widget title. |
| 45 |
*/ |
| 46 |
public function get_title() { |
| 47 |
return esc_html__( 'Heading', 'elementor' ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Get widget icon. |
| 52 |
* |
| 53 |
* Retrieve heading widget icon. |
| 54 |
* |
| 55 |
* @since 1.0.0 |
| 56 |
* @access public |
| 57 |
* |
| 58 |
* @return string Widget icon. |
| 59 |
*/ |
| 60 |
public function get_icon() { |
| 61 |
return 'eicon-t-letter'; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Get widget categories. |
| 66 |
* |
| 67 |
* Retrieve the list of categories the heading widget belongs to. |
| 68 |
* |
| 69 |
* Used to determine where to display the widget in the editor. |
| 70 |
* |
| 71 |
* @since 2.0.0 |
| 72 |
* @access public |
| 73 |
* |
| 74 |
* @return array Widget categories. |
| 75 |
*/ |
| 76 |
public function get_categories() { |
| 77 |
return [ 'basic' ]; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Get widget keywords. |
| 82 |
* |
| 83 |
* Retrieve the list of keywords the widget belongs to. |
| 84 |
* |
| 85 |
* @since 2.1.0 |
| 86 |
* @access public |
| 87 |
* |
| 88 |
* @return array Widget keywords. |
| 89 |
*/ |
| 90 |
public function get_keywords() { |
| 91 |
return [ 'heading', 'title', 'text' ]; |
| 92 |
} |
| 93 |
|
| 94 |
protected function is_dynamic_content(): bool { |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Get style dependencies. |
| 100 |
* |
| 101 |
* Retrieve the list of style dependencies the widget requires. |
| 102 |
* |
| 103 |
* @since 3.24.0 |
| 104 |
* @access public |
| 105 |
* |
| 106 |
* @return array Widget style dependencies. |
| 107 |
*/ |
| 108 |
public function get_style_depends(): array { |
| 109 |
return [ 'widget-heading' ]; |
| 110 |
} |
| 111 |
|
| 112 |
public function has_widget_inner_wrapper(): bool { |
| 113 |
return ! Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' ); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Remove data attributes from the html. |
| 118 |
* |
| 119 |
* @param string $content Heading title. |
| 120 |
* @return string |
| 121 |
*/ |
| 122 |
public function sanitize( $content ): string { |
| 123 |
$allowed_tags = wp_kses_allowed_html( 'post' ); |
| 124 |
$allowed_tags_for_heading = []; |
| 125 |
$non_allowed_tags = [ 'img' ]; |
| 126 |
|
| 127 |
foreach ( $allowed_tags as $tag => $attributes ) { |
| 128 |
if ( in_array( $tag, $non_allowed_tags, true ) ) { |
| 129 |
continue; |
| 130 |
} |
| 131 |
|
| 132 |
$filtered_attributes = array_filter( $attributes, function( $attribute ) { |
| 133 |
return ! substr( $attribute, 0, 5 ) === 'data-'; |
| 134 |
}, ARRAY_FILTER_USE_KEY ); |
| 135 |
|
| 136 |
$allowed_tags_for_heading[ $tag ] = $filtered_attributes; |
| 137 |
} |
| 138 |
|
| 139 |
return wp_kses( $content, $allowed_tags_for_heading ); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Register heading widget controls. |
| 144 |
* |
| 145 |
* Adds different input fields to allow the user to change and customize the widget settings. |
| 146 |
* |
| 147 |
* @since 3.1.0 |
| 148 |
* @access protected |
| 149 |
*/ |
| 150 |
protected function register_controls() { |
| 151 |
$this->start_controls_section( |
| 152 |
'section_title', |
| 153 |
[ |
| 154 |
'label' => esc_html__( 'Heading', 'elementor' ), |
| 155 |
] |
| 156 |
); |
| 157 |
|
| 158 |
$this->add_control( |
| 159 |
'title', |
| 160 |
[ |
| 161 |
'label' => esc_html__( 'Title', 'elementor' ), |
| 162 |
'type' => Controls_Manager::TEXTAREA, |
| 163 |
'ai' => [ |
| 164 |
'type' => 'text', |
| 165 |
], |
| 166 |
'dynamic' => [ |
| 167 |
'active' => true, |
| 168 |
], |
| 169 |
'placeholder' => esc_html__( 'Enter your title', 'elementor' ), |
| 170 |
'default' => esc_html__( 'Add Your Heading Text Here', 'elementor' ), |
| 171 |
] |
| 172 |
); |
| 173 |
|
| 174 |
$this->add_control( |
| 175 |
'link', |
| 176 |
[ |
| 177 |
'label' => esc_html__( 'Link', 'elementor' ), |
| 178 |
'type' => Controls_Manager::URL, |
| 179 |
'dynamic' => [ |
| 180 |
'active' => true, |
| 181 |
], |
| 182 |
'default' => [ |
| 183 |
'url' => '', |
| 184 |
], |
| 185 |
] |
| 186 |
); |
| 187 |
|
| 188 |
$this->add_control( |
| 189 |
'size', |
| 190 |
[ |
| 191 |
'label' => esc_html__( 'Size', 'elementor' ), |
| 192 |
'type' => Controls_Manager::SELECT, |
| 193 |
'options' => [ |
| 194 |
'default' => esc_html__( 'Default', 'elementor' ), |
| 195 |
'small' => esc_html__( 'Small', 'elementor' ), |
| 196 |
'medium' => esc_html__( 'Medium', 'elementor' ), |
| 197 |
'large' => esc_html__( 'Large', 'elementor' ), |
| 198 |
'xl' => esc_html__( 'XL', 'elementor' ), |
| 199 |
'xxl' => esc_html__( 'XXL', 'elementor' ), |
| 200 |
], |
| 201 |
'default' => 'default', |
| 202 |
'condition' => [ |
| 203 |
'size!' => 'default', // a workaround to hide the control, unless it's in use (not default). |
| 204 |
], |
| 205 |
] |
| 206 |
); |
| 207 |
|
| 208 |
$this->add_control( |
| 209 |
'header_size', |
| 210 |
[ |
| 211 |
'label' => esc_html__( 'HTML Tag', 'elementor' ), |
| 212 |
'type' => Controls_Manager::SELECT, |
| 213 |
'options' => [ |
| 214 |
'h1' => 'H1', |
| 215 |
'h2' => 'H2', |
| 216 |
'h3' => 'H3', |
| 217 |
'h4' => 'H4', |
| 218 |
'h5' => 'H5', |
| 219 |
'h6' => 'H6', |
| 220 |
'div' => 'div', |
| 221 |
'span' => 'span', |
| 222 |
'p' => 'p', |
| 223 |
], |
| 224 |
'default' => 'h2', |
| 225 |
] |
| 226 |
); |
| 227 |
|
| 228 |
if ( ! Utils::has_pro() ) { |
| 229 |
$this->add_control( |
| 230 |
Utils::ANIMATED_HEADLINE . '_promotion', |
| 231 |
[ |
| 232 |
'label' => esc_html__( 'Animated Headline widget', 'elementor' ), |
| 233 |
'type' => Promotion_Control::TYPE, |
| 234 |
] |
| 235 |
); |
| 236 |
} |
| 237 |
|
| 238 |
$this->end_controls_section(); |
| 239 |
|
| 240 |
$this->start_controls_section( |
| 241 |
'section_title_style', |
| 242 |
[ |
| 243 |
'label' => esc_html__( 'Heading', 'elementor' ), |
| 244 |
'tab' => Controls_Manager::TAB_STYLE, |
| 245 |
] |
| 246 |
); |
| 247 |
|
| 248 |
$this->add_responsive_control( |
| 249 |
'align', |
| 250 |
[ |
| 251 |
'label' => esc_html__( 'Alignment', 'elementor' ), |
| 252 |
'type' => Controls_Manager::CHOOSE, |
| 253 |
'options' => [ |
| 254 |
'left' => [ |
| 255 |
'title' => esc_html__( 'Left', 'elementor' ), |
| 256 |
'icon' => 'eicon-text-align-left', |
| 257 |
], |
| 258 |
'center' => [ |
| 259 |
'title' => esc_html__( 'Center', 'elementor' ), |
| 260 |
'icon' => 'eicon-text-align-center', |
| 261 |
], |
| 262 |
'right' => [ |
| 263 |
'title' => esc_html__( 'Right', 'elementor' ), |
| 264 |
'icon' => 'eicon-text-align-right', |
| 265 |
], |
| 266 |
'justify' => [ |
| 267 |
'title' => esc_html__( 'Justified', 'elementor' ), |
| 268 |
'icon' => 'eicon-text-align-justify', |
| 269 |
], |
| 270 |
], |
| 271 |
'default' => '', |
| 272 |
'selectors' => [ |
| 273 |
'{{WRAPPER}}' => 'text-align: {{VALUE}};', |
| 274 |
], |
| 275 |
'separator' => 'after', |
| 276 |
] |
| 277 |
); |
| 278 |
|
| 279 |
$this->add_group_control( |
| 280 |
Group_Control_Typography::get_type(), |
| 281 |
[ |
| 282 |
'name' => 'typography', |
| 283 |
'global' => [ |
| 284 |
'default' => Global_Typography::TYPOGRAPHY_PRIMARY, |
| 285 |
], |
| 286 |
'selector' => '{{WRAPPER}} .elementor-heading-title', |
| 287 |
] |
| 288 |
); |
| 289 |
|
| 290 |
$this->add_group_control( |
| 291 |
Group_Control_Text_Stroke::get_type(), |
| 292 |
[ |
| 293 |
'name' => 'text_stroke', |
| 294 |
'selector' => '{{WRAPPER}} .elementor-heading-title', |
| 295 |
] |
| 296 |
); |
| 297 |
|
| 298 |
$this->add_group_control( |
| 299 |
Group_Control_Text_Shadow::get_type(), |
| 300 |
[ |
| 301 |
'name' => 'text_shadow', |
| 302 |
'selector' => '{{WRAPPER}} .elementor-heading-title', |
| 303 |
] |
| 304 |
); |
| 305 |
|
| 306 |
$this->add_control( |
| 307 |
'blend_mode', |
| 308 |
[ |
| 309 |
'label' => esc_html__( 'Blend Mode', 'elementor' ), |
| 310 |
'type' => Controls_Manager::SELECT, |
| 311 |
'options' => [ |
| 312 |
'' => esc_html__( 'Normal', 'elementor' ), |
| 313 |
'multiply' => esc_html__( 'Multiply', 'elementor' ), |
| 314 |
'screen' => esc_html__( 'Screen', 'elementor' ), |
| 315 |
'overlay' => esc_html__( 'Overlay', 'elementor' ), |
| 316 |
'darken' => esc_html__( 'Darken', 'elementor' ), |
| 317 |
'lighten' => esc_html__( 'Lighten', 'elementor' ), |
| 318 |
'color-dodge' => esc_html__( 'Color Dodge', 'elementor' ), |
| 319 |
'saturation' => esc_html__( 'Saturation', 'elementor' ), |
| 320 |
'color' => esc_html__( 'Color', 'elementor' ), |
| 321 |
'difference' => esc_html__( 'Difference', 'elementor' ), |
| 322 |
'exclusion' => esc_html__( 'Exclusion', 'elementor' ), |
| 323 |
'hue' => esc_html__( 'Hue', 'elementor' ), |
| 324 |
'luminosity' => esc_html__( 'Luminosity', 'elementor' ), |
| 325 |
], |
| 326 |
'selectors' => [ |
| 327 |
'{{WRAPPER}} .elementor-heading-title' => 'mix-blend-mode: {{VALUE}}', |
| 328 |
], |
| 329 |
] |
| 330 |
); |
| 331 |
|
| 332 |
$this->add_control( |
| 333 |
'separator', |
| 334 |
[ |
| 335 |
'type' => Controls_Manager::DIVIDER, |
| 336 |
] |
| 337 |
); |
| 338 |
|
| 339 |
$this->start_controls_tabs( 'title_colors' ); |
| 340 |
|
| 341 |
$this->start_controls_tab( |
| 342 |
'title_colors_normal', |
| 343 |
[ |
| 344 |
'label' => esc_html__( 'Normal', 'elementor' ), |
| 345 |
] |
| 346 |
); |
| 347 |
|
| 348 |
$this->add_control( |
| 349 |
'title_color', |
| 350 |
[ |
| 351 |
'label' => esc_html__( 'Text Color', 'elementor' ), |
| 352 |
'type' => Controls_Manager::COLOR, |
| 353 |
'global' => [ |
| 354 |
'default' => Global_Colors::COLOR_PRIMARY, |
| 355 |
], |
| 356 |
'selectors' => [ |
| 357 |
'{{WRAPPER}} .elementor-heading-title' => 'color: {{VALUE}};', |
| 358 |
], |
| 359 |
] |
| 360 |
); |
| 361 |
|
| 362 |
$this->end_controls_tab(); |
| 363 |
|
| 364 |
$this->start_controls_tab( |
| 365 |
'title_colors_hover', |
| 366 |
[ |
| 367 |
'label' => esc_html__( 'Hover', 'elementor' ), |
| 368 |
] |
| 369 |
); |
| 370 |
|
| 371 |
$this->add_control( |
| 372 |
'title_hover_color', |
| 373 |
[ |
| 374 |
'label' => esc_html__( 'Link Color', 'elementor' ), |
| 375 |
'type' => Controls_Manager::COLOR, |
| 376 |
'selectors' => [ |
| 377 |
'{{WRAPPER}} .elementor-heading-title a:hover, {{WRAPPER}} .elementor-heading-title a:focus' => 'color: {{VALUE}};', |
| 378 |
], |
| 379 |
] |
| 380 |
); |
| 381 |
|
| 382 |
$this->add_control( |
| 383 |
'title_hover_color_transition_duration', |
| 384 |
[ |
| 385 |
'label' => esc_html__( 'Transition Duration', 'elementor' ), |
| 386 |
'type' => Controls_Manager::SLIDER, |
| 387 |
'size_units' => [ 's', 'ms', 'custom' ], |
| 388 |
'default' => [ |
| 389 |
'unit' => 's', |
| 390 |
], |
| 391 |
'selectors' => [ |
| 392 |
'{{WRAPPER}} .elementor-heading-title a' => 'transition-duration: {{SIZE}}{{UNIT}};', |
| 393 |
], |
| 394 |
] |
| 395 |
); |
| 396 |
|
| 397 |
$this->end_controls_tab(); |
| 398 |
|
| 399 |
$this->end_controls_tabs(); |
| 400 |
|
| 401 |
$this->end_controls_section(); |
| 402 |
} |
| 403 |
|
| 404 |
/** |
| 405 |
* Render heading widget output on the frontend. |
| 406 |
* |
| 407 |
* Written in PHP and used to generate the final HTML. |
| 408 |
* |
| 409 |
* @since 1.0.0 |
| 410 |
* @access protected |
| 411 |
*/ |
| 412 |
protected function render() { |
| 413 |
$settings = $this->get_settings_for_display(); |
| 414 |
|
| 415 |
if ( '' === $settings['title'] ) { |
| 416 |
return; |
| 417 |
} |
| 418 |
|
| 419 |
$this->add_render_attribute( 'title', 'class', 'elementor-heading-title' ); |
| 420 |
|
| 421 |
if ( ! empty( $settings['size'] ) ) { |
| 422 |
$this->add_render_attribute( 'title', 'class', 'elementor-size-' . $settings['size'] ); |
| 423 |
} else { |
| 424 |
$this->add_render_attribute( 'title', 'class', 'elementor-size-default' ); |
| 425 |
} |
| 426 |
|
| 427 |
$this->add_inline_editing_attributes( 'title' ); |
| 428 |
|
| 429 |
$title = $this->should_sanitize( $settings ) ? wp_kses_post( $settings['title'] ) : $settings['title']; |
| 430 |
|
| 431 |
if ( ! empty( $settings['link']['url'] ) ) { |
| 432 |
$this->add_link_attributes( 'url', $settings['link'] ); |
| 433 |
|
| 434 |
$title = sprintf( '<a %1$s>%2$s</a>', $this->get_render_attribute_string( 'url' ), $title ); |
| 435 |
} |
| 436 |
|
| 437 |
$title_html = sprintf( '<%1$s %2$s>%3$s</%1$s>', Utils::validate_html_tag( $settings['header_size'] ), $this->get_render_attribute_string( 'title' ), $title ); |
| 438 |
|
| 439 |
// PHPCS - the variable $title_html holds safe data. |
| 440 |
echo $title_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 441 |
} |
| 442 |
|
| 443 |
/** |
| 444 |
* Render heading widget output in the editor. |
| 445 |
* |
| 446 |
* Written as a Backbone JavaScript template and used to generate the live preview. |
| 447 |
* |
| 448 |
* @since 2.9.0 |
| 449 |
* @access protected |
| 450 |
*/ |
| 451 |
protected function content_template() { |
| 452 |
?> |
| 453 |
<# |
| 454 |
let title = elementor.helpers.sanitize( settings.title, { ALLOW_DATA_ATTR: false } ); |
| 455 |
|
| 456 |
if ( '' !== settings.link.url ) { |
| 457 |
title = '<a href="' + elementor.helpers.sanitizeUrl( settings.link.url ) + '">' + title + '</a>'; |
| 458 |
} |
| 459 |
|
| 460 |
view.addRenderAttribute( 'title', 'class', [ 'elementor-heading-title' ] ); |
| 461 |
|
| 462 |
if ( '' !== settings.size ) { |
| 463 |
view.addRenderAttribute( 'title', 'class', [ 'elementor-size-' + settings.size ] ); |
| 464 |
} else { |
| 465 |
view.addRenderAttribute( 'title', 'class', [ 'elementor-size-default' ] ); |
| 466 |
} |
| 467 |
|
| 468 |
view.addInlineEditingAttributes( 'title' ); |
| 469 |
|
| 470 |
var headerSizeTag = elementor.helpers.validateHTMLTag( settings.header_size ), |
| 471 |
title_html = '<' + headerSizeTag + ' ' + view.getRenderAttributeString( 'title' ) + '>' + title + '</' + headerSizeTag + '>'; |
| 472 |
|
| 473 |
print( title_html ); |
| 474 |
#> |
| 475 |
<?php |
| 476 |
} |
| 477 |
|
| 478 |
/** |
| 479 |
* Check if the content should be sanitized. Sanitizing should be applied for non-admin users in the editor and for shortcodes. |
| 480 |
* |
| 481 |
* @return bool |
| 482 |
*/ |
| 483 |
private function should_sanitize( array $settings ): bool { |
| 484 |
return ( is_admin() && ! current_user_can( 'manage_options' ) ) || ! empty( $settings['isShortcode'] ); |
| 485 |
} |
| 486 |
} |
| 487 |
|