| 1 |
<?php |
| 2 |
namespace Easyel\EasyElements\Widgets; |
| 3 |
use Elementor\Controls_Manager; |
| 4 |
use Elementor\Widget_Base; |
| 5 |
|
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
class Easyel_Page_Title_Widget extends \Elementor\Widget_Base { |
| 11 |
|
| 12 |
public function get_name() { |
| 13 |
return 'eel-page-title'; |
| 14 |
} |
| 15 |
|
| 16 |
public function get_title() { |
| 17 |
return __( 'Page Title', 'easy-elements' ); |
| 18 |
} |
| 19 |
|
| 20 |
public function get_icon() { |
| 21 |
return 'easyicon easyelIcon-page-title'; |
| 22 |
} |
| 23 |
|
| 24 |
public function get_categories() { |
| 25 |
return [ 'easyelements_header_footer_category' ]; |
| 26 |
} |
| 27 |
|
| 28 |
public function get_keywords() { |
| 29 |
return [ 'page', 'link', 'title', 'text' ]; |
| 30 |
} |
| 31 |
|
| 32 |
public function get_style_depends() { |
| 33 |
return [ |
| 34 |
'eel-page-title', |
| 35 |
]; |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
protected function register_controls() { |
| 41 |
|
| 42 |
// ===================================================================== |
| 43 |
// CONTENT TAB |
| 44 |
// ===================================================================== |
| 45 |
|
| 46 |
// --- Main Title ----------------------------------------------------- |
| 47 |
$this->start_controls_section( |
| 48 |
'section_main_title', |
| 49 |
[ |
| 50 |
'label' => esc_html__( 'Main Title', 'easy-elements' ), |
| 51 |
'tab' => Controls_Manager::TAB_CONTENT, |
| 52 |
] |
| 53 |
); |
| 54 |
|
| 55 |
$this->add_control( |
| 56 |
'custom_title_show_hide', |
| 57 |
[ |
| 58 |
'label' => esc_html__( 'Show Main Title', 'easy-elements' ), |
| 59 |
'type' => Controls_Manager::SWITCHER, |
| 60 |
'default' => 'yes', |
| 61 |
'label_on' => esc_html__( 'Show', 'easy-elements' ), |
| 62 |
'label_off' => esc_html__( 'Hide', 'easy-elements' ), |
| 63 |
'return_value' => 'yes', |
| 64 |
] |
| 65 |
); |
| 66 |
|
| 67 |
$this->add_control( |
| 68 |
'custom_title', |
| 69 |
[ |
| 70 |
'label' => esc_html__( 'Custom Title Text', 'easy-elements' ), |
| 71 |
'type' => Controls_Manager::TEXTAREA, |
| 72 |
'label_block' => true, |
| 73 |
'default' => '', |
| 74 |
'placeholder' => esc_html__( 'Enter custom title (leave blank for page title)', 'easy-elements' ), |
| 75 |
'description' => esc_html__( 'Leave blank to use the current page title automatically.', 'easy-elements' ), |
| 76 |
'condition' => [ |
| 77 |
'custom_title_show_hide' => 'yes', |
| 78 |
], |
| 79 |
] |
| 80 |
); |
| 81 |
|
| 82 |
$this->add_control( |
| 83 |
'title_tag', |
| 84 |
[ |
| 85 |
'label' => esc_html__( 'HTML Tag', 'easy-elements' ), |
| 86 |
'type' => Controls_Manager::SELECT, |
| 87 |
'options' => [ |
| 88 |
'h1' => 'H1', |
| 89 |
'h2' => 'H2', |
| 90 |
'h3' => 'H3', |
| 91 |
'div' => 'Div', |
| 92 |
'span' => 'Span', |
| 93 |
'p' => 'Paragraph', |
| 94 |
], |
| 95 |
'default' => 'h1', |
| 96 |
'condition' => [ |
| 97 |
'custom_title_show_hide' => 'yes', |
| 98 |
], |
| 99 |
] |
| 100 |
); |
| 101 |
|
| 102 |
$this->add_control( |
| 103 |
'title_size', |
| 104 |
[ |
| 105 |
'label' => esc_html__( 'Title Size Preset', 'easy-elements' ), |
| 106 |
'type' => Controls_Manager::SELECT, |
| 107 |
'label_block' => true, |
| 108 |
'default' => 'large', |
| 109 |
'options' => [ |
| 110 |
'small' => esc_html__( 'Small', 'easy-elements' ), |
| 111 |
'large' => esc_html__( 'Large', 'easy-elements' ), |
| 112 |
], |
| 113 |
'condition' => [ |
| 114 |
'custom_title_show_hide' => 'yes', |
| 115 |
], |
| 116 |
] |
| 117 |
); |
| 118 |
|
| 119 |
$this->end_controls_section(); |
| 120 |
|
| 121 |
// --- Sub Heading ---------------------------------------------------- |
| 122 |
$this->start_controls_section( |
| 123 |
'section_sub_heading', |
| 124 |
[ |
| 125 |
'label' => esc_html__( 'Sub Heading', 'easy-elements' ), |
| 126 |
'tab' => Controls_Manager::TAB_CONTENT, |
| 127 |
] |
| 128 |
); |
| 129 |
|
| 130 |
$this->add_control( |
| 131 |
'heading_title', |
| 132 |
[ |
| 133 |
'label' => esc_html__( 'Sub Heading Text', 'easy-elements' ), |
| 134 |
'type' => Controls_Manager::TEXTAREA, |
| 135 |
'label_block' => true, |
| 136 |
'description' => esc_html__( 'Shown under the main title. Wrap any words in {{ }} to highlight them — e.g. An {{experienced team}} for your business.', 'easy-elements' ), |
| 137 |
] |
| 138 |
); |
| 139 |
|
| 140 |
$this->add_control( |
| 141 |
'heading_title_tag', |
| 142 |
[ |
| 143 |
'label' => esc_html__( 'HTML Tag', 'easy-elements' ), |
| 144 |
'type' => Controls_Manager::SELECT, |
| 145 |
'default' => 'h2', |
| 146 |
'options' => [ |
| 147 |
'h1' => 'H1', |
| 148 |
'h2' => 'H2', |
| 149 |
'h3' => 'H3', |
| 150 |
'div' => 'Div', |
| 151 |
'span' => 'Span', |
| 152 |
'p' => 'Paragraph', |
| 153 |
], |
| 154 |
] |
| 155 |
); |
| 156 |
|
| 157 |
$this->end_controls_section(); |
| 158 |
|
| 159 |
// --- Description ---------------------------------------------------- |
| 160 |
$this->start_controls_section( |
| 161 |
'section_description', |
| 162 |
[ |
| 163 |
'label' => esc_html__( 'Description', 'easy-elements' ), |
| 164 |
'tab' => Controls_Manager::TAB_CONTENT, |
| 165 |
] |
| 166 |
); |
| 167 |
|
| 168 |
$this->add_control( |
| 169 |
'custom_description', |
| 170 |
[ |
| 171 |
'label' => esc_html__( 'Description Text', 'easy-elements' ), |
| 172 |
'type' => Controls_Manager::TEXTAREA, |
| 173 |
'default' => '', |
| 174 |
'label_block' => true, |
| 175 |
'placeholder' => esc_html__( 'Enter description', 'easy-elements' ), |
| 176 |
'description' => esc_html__( 'Optional short text shown below the headings.', 'easy-elements' ), |
| 177 |
] |
| 178 |
); |
| 179 |
|
| 180 |
$this->end_controls_section(); |
| 181 |
|
| 182 |
// --- Layout (alignment + decorative border) ------------------------- |
| 183 |
$this->start_controls_section( |
| 184 |
'section_layout', |
| 185 |
[ |
| 186 |
'label' => esc_html__( 'Layout', 'easy-elements' ), |
| 187 |
'tab' => Controls_Manager::TAB_CONTENT, |
| 188 |
] |
| 189 |
); |
| 190 |
|
| 191 |
$this->add_responsive_control( |
| 192 |
'alignment', |
| 193 |
[ |
| 194 |
'label' => esc_html__( 'Alignment', 'easy-elements' ), |
| 195 |
'type' => Controls_Manager::CHOOSE, |
| 196 |
'options' => [ |
| 197 |
'left' => [ |
| 198 |
'title' => __( 'Left', 'easy-elements' ), |
| 199 |
'icon' => 'eicon-text-align-left', |
| 200 |
], |
| 201 |
'center' => [ |
| 202 |
'title' => __( 'Center', 'easy-elements' ), |
| 203 |
'icon' => 'eicon-text-align-center', |
| 204 |
], |
| 205 |
'right' => [ |
| 206 |
'title' => __( 'Right', 'easy-elements' ), |
| 207 |
'icon' => 'eicon-text-align-right', |
| 208 |
], |
| 209 |
], |
| 210 |
'default' => 'left', |
| 211 |
'toggle' => true, |
| 212 |
'selectors' => [ |
| 213 |
'{{WRAPPER}} .eel-page-title-wrapper' => 'text-align: {{VALUE}};', |
| 214 |
], |
| 215 |
] |
| 216 |
); |
| 217 |
|
| 218 |
$this->add_control( |
| 219 |
'border_show_hide', |
| 220 |
[ |
| 221 |
'label' => esc_html__( 'Decorative Border', 'easy-elements' ), |
| 222 |
'type' => Controls_Manager::SWITCHER, |
| 223 |
'default' => 'yes', |
| 224 |
'label_on' => esc_html__( 'Show', 'easy-elements' ), |
| 225 |
'label_off' => esc_html__( 'Hide', 'easy-elements' ), |
| 226 |
'return_value' => 'yes', |
| 227 |
'description' => esc_html__( 'Adds a small accent bar beside the main title.', 'easy-elements' ), |
| 228 |
'separator' => 'before', |
| 229 |
] |
| 230 |
); |
| 231 |
|
| 232 |
$this->add_control( |
| 233 |
'border_position', |
| 234 |
[ |
| 235 |
'label' => esc_html__( 'Border Position', 'easy-elements' ), |
| 236 |
'type' => Controls_Manager::SELECT, |
| 237 |
'options' => [ |
| 238 |
'left' => esc_html__( 'Left', 'easy-elements' ), |
| 239 |
'right' => esc_html__( 'Right', 'easy-elements' ), |
| 240 |
], |
| 241 |
'default' => 'left', |
| 242 |
'condition' => [ |
| 243 |
'border_show_hide' => 'yes', |
| 244 |
], |
| 245 |
] |
| 246 |
); |
| 247 |
|
| 248 |
$this->end_controls_section(); |
| 249 |
|
| 250 |
// ===================================================================== |
| 251 |
// STYLE TAB |
| 252 |
// ===================================================================== |
| 253 |
|
| 254 |
// --- General Style (decorative border color) ------------------------ |
| 255 |
$this->start_controls_section( |
| 256 |
'section_general_style', |
| 257 |
[ |
| 258 |
'label' => esc_html__( 'General', 'easy-elements' ), |
| 259 |
'tab' => Controls_Manager::TAB_STYLE, |
| 260 |
] |
| 261 |
); |
| 262 |
|
| 263 |
// Original misspelled ID preserved on purpose — renaming would orphan |
| 264 |
// every saved value across existing pages. |
| 265 |
$this->add_control( |
| 266 |
'boder_color', |
| 267 |
[ |
| 268 |
'label' => esc_html__( 'Border Color', 'easy-elements' ), |
| 269 |
'type' => Controls_Manager::COLOR, |
| 270 |
'condition' => [ |
| 271 |
'border_show_hide' => 'yes', |
| 272 |
], |
| 273 |
'selectors' => [ |
| 274 |
'{{WRAPPER}} .eel-border-left' => 'background: {{VALUE}};', |
| 275 |
], |
| 276 |
] |
| 277 |
); |
| 278 |
|
| 279 |
$this->end_controls_section(); |
| 280 |
|
| 281 |
// --- Main Title Style ---------------------------------------------- |
| 282 |
$this->start_controls_section( |
| 283 |
'section_main_title_style', |
| 284 |
[ |
| 285 |
'label' => esc_html__( 'Main Title', 'easy-elements' ), |
| 286 |
'tab' => Controls_Manager::TAB_STYLE, |
| 287 |
'condition' => [ |
| 288 |
'custom_title_show_hide' => 'yes', |
| 289 |
], |
| 290 |
] |
| 291 |
); |
| 292 |
|
| 293 |
$this->add_control( |
| 294 |
'title_color', |
| 295 |
[ |
| 296 |
'label' => esc_html__( 'Color', 'easy-elements' ), |
| 297 |
'type' => Controls_Manager::COLOR, |
| 298 |
'selectors' => [ |
| 299 |
'{{WRAPPER}} .eel-page-title' => 'color: {{VALUE}};', |
| 300 |
], |
| 301 |
] |
| 302 |
); |
| 303 |
|
| 304 |
$this->add_group_control( |
| 305 |
\Elementor\Group_Control_Typography::get_type(), |
| 306 |
[ |
| 307 |
'name' => 'title_typography', |
| 308 |
'selector' => '{{WRAPPER}} .eel-page-title', |
| 309 |
] |
| 310 |
); |
| 311 |
|
| 312 |
$this->add_responsive_control( |
| 313 |
'title_margin', |
| 314 |
[ |
| 315 |
'label' => esc_html__( 'Margin', 'easy-elements' ), |
| 316 |
'type' => Controls_Manager::DIMENSIONS, |
| 317 |
'size_units' => [ 'px', '%', 'em' ], |
| 318 |
'default' => [ |
| 319 |
'top' => '0', |
| 320 |
'right' => '0', |
| 321 |
'bottom' => '0', |
| 322 |
'left' => '0', |
| 323 |
'unit' => 'px', |
| 324 |
], |
| 325 |
'selectors' => [ |
| 326 |
'{{WRAPPER}} .eel-page-title' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 327 |
], |
| 328 |
] |
| 329 |
); |
| 330 |
|
| 331 |
$this->end_controls_section(); |
| 332 |
|
| 333 |
// --- Sub Heading Style --------------------------------------------- |
| 334 |
$this->start_controls_section( |
| 335 |
'section_sub_heading_style', |
| 336 |
[ |
| 337 |
'label' => esc_html__( 'Sub Heading', 'easy-elements' ), |
| 338 |
'tab' => Controls_Manager::TAB_STYLE, |
| 339 |
] |
| 340 |
); |
| 341 |
|
| 342 |
$this->add_control( |
| 343 |
'heading_title_color', |
| 344 |
[ |
| 345 |
'label' => esc_html__( 'Color', 'easy-elements' ), |
| 346 |
'type' => Controls_Manager::COLOR, |
| 347 |
'selectors' => [ |
| 348 |
'{{WRAPPER}} .eel-heading-title' => 'color: {{VALUE}};', |
| 349 |
], |
| 350 |
] |
| 351 |
); |
| 352 |
|
| 353 |
$this->add_control( |
| 354 |
'highlighted_span_color', |
| 355 |
[ |
| 356 |
'label' => esc_html__( 'Highlighted Word Color', 'easy-elements' ), |
| 357 |
'type' => Controls_Manager::COLOR, |
| 358 |
'description' => esc_html__( 'Color applied to words wrapped in {{ }} inside the sub heading.', 'easy-elements' ), |
| 359 |
'selectors' => [ |
| 360 |
'{{WRAPPER}} .eel-highlighted' => 'color: {{VALUE}};', |
| 361 |
], |
| 362 |
] |
| 363 |
); |
| 364 |
|
| 365 |
$this->add_group_control( |
| 366 |
\Elementor\Group_Control_Typography::get_type(), |
| 367 |
[ |
| 368 |
'name' => 'heading_title_typography', |
| 369 |
'selector' => '{{WRAPPER}} .eel-heading-title', |
| 370 |
] |
| 371 |
); |
| 372 |
|
| 373 |
$this->add_responsive_control( |
| 374 |
'heading_title_margin', |
| 375 |
[ |
| 376 |
'label' => esc_html__( 'Margin', 'easy-elements' ), |
| 377 |
'type' => Controls_Manager::DIMENSIONS, |
| 378 |
'selectors' => [ |
| 379 |
'{{WRAPPER}} .eel-heading-title' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 380 |
], |
| 381 |
] |
| 382 |
); |
| 383 |
|
| 384 |
$this->end_controls_section(); |
| 385 |
|
| 386 |
// --- Description Style --------------------------------------------- |
| 387 |
$this->start_controls_section( |
| 388 |
'section_description_style', |
| 389 |
[ |
| 390 |
'label' => esc_html__( 'Description', 'easy-elements' ), |
| 391 |
'tab' => Controls_Manager::TAB_STYLE, |
| 392 |
] |
| 393 |
); |
| 394 |
|
| 395 |
$this->add_control( |
| 396 |
'description_color', |
| 397 |
[ |
| 398 |
'label' => esc_html__( 'Color', 'easy-elements' ), |
| 399 |
'type' => Controls_Manager::COLOR, |
| 400 |
'selectors' => [ |
| 401 |
'{{WRAPPER}} .eel-page-description' => 'color: {{VALUE}};', |
| 402 |
], |
| 403 |
] |
| 404 |
); |
| 405 |
|
| 406 |
$this->add_group_control( |
| 407 |
\Elementor\Group_Control_Typography::get_type(), |
| 408 |
[ |
| 409 |
'name' => 'description_typography', |
| 410 |
'selector' => '{{WRAPPER}} .eel-page-description', |
| 411 |
] |
| 412 |
); |
| 413 |
|
| 414 |
$this->add_responsive_control( |
| 415 |
'description_margin', |
| 416 |
[ |
| 417 |
'label' => esc_html__( 'Margin', 'easy-elements' ), |
| 418 |
'type' => Controls_Manager::DIMENSIONS, |
| 419 |
'selectors' => [ |
| 420 |
'{{WRAPPER}} .eel-page-description' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 421 |
], |
| 422 |
] |
| 423 |
); |
| 424 |
|
| 425 |
$this->end_controls_section(); |
| 426 |
} |
| 427 |
|
| 428 |
/** |
| 429 |
* Get the dynamic page title based on the current page context. |
| 430 |
* |
| 431 |
* @return string |
| 432 |
*/ |
| 433 |
private function get_dynamic_page_title() { |
| 434 |
|
| 435 |
// Elementor editor mode — use the current document title |
| 436 |
if ( \Elementor\Plugin::$instance->editor->is_edit_mode() || wp_doing_ajax() ) { |
| 437 |
$document = \Elementor\Plugin::$instance->documents->get_current(); |
| 438 |
if ( $document ) { |
| 439 |
$post_id = $document->get_main_id(); |
| 440 |
if ( $post_id ) { |
| 441 |
return get_the_title( $post_id ); |
| 442 |
} |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
if ( is_home() ) { |
| 447 |
return get_option( 'page_for_posts' ) ? get_the_title( get_option( 'page_for_posts' ) ) : __( 'Blog', 'easy-elements' ); |
| 448 |
} elseif ( is_category() ) { |
| 449 |
return single_cat_title( '', false ); |
| 450 |
} elseif ( is_archive() ) { |
| 451 |
// Strip the "Archives:" / "Category:" / "Tag:" etc. prefix so only the clean name shows. |
| 452 |
add_filter( 'get_the_archive_title_prefix', '__return_empty_string' ); |
| 453 |
$archive_title = get_the_archive_title(); |
| 454 |
remove_filter( 'get_the_archive_title_prefix', '__return_empty_string' ); |
| 455 |
return $archive_title; |
| 456 |
} elseif ( is_search() ) { |
| 457 |
// translators: %s is the search query entered by the user. |
| 458 |
return sprintf( __( 'Search Results for "%s"', 'easy-elements' ), get_search_query() ); |
| 459 |
} elseif ( is_404() ) { |
| 460 |
return __( 'Page Not Found', 'easy-elements' ); |
| 461 |
} elseif ( is_tag() ) { |
| 462 |
return single_tag_title( '', false ); |
| 463 |
} elseif ( is_author() ) { |
| 464 |
return get_the_author(); |
| 465 |
} elseif ( is_date() ) { |
| 466 |
return get_the_date(); |
| 467 |
} elseif ( is_post_type_archive() ) { |
| 468 |
return post_type_archive_title( '', false ); |
| 469 |
} elseif ( is_singular() ) { |
| 470 |
return get_the_title(); |
| 471 |
} else { |
| 472 |
return get_the_title(); |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
protected function render() { |
| 478 |
$settings = $this->get_settings_for_display(); |
| 479 |
$title = (!empty($settings['custom_title']) && (!isset($settings['custom_title_show_hide']) || $settings['custom_title_show_hide'] === 'yes')) |
| 480 |
? $settings['custom_title'] |
| 481 |
: $this->get_dynamic_page_title(); |
| 482 |
$description = !empty($settings['custom_description']) ? $settings['custom_description'] : ''; |
| 483 |
$tag = !empty($settings['title_tag']) ? $settings['title_tag'] : 'h1'; |
| 484 |
$title_size = !empty($settings['title_size']) ? $settings['title_size'] : 'large'; |
| 485 |
$heading_title = !empty($settings['heading_title']) ? $settings['heading_title'] : ''; |
| 486 |
$heading_title_tag = !empty($settings['heading_title_tag']) ? $settings['heading_title_tag'] : 'h2'; |
| 487 |
$border_position = !empty($settings['border_position']) ? $settings['border_position'] : 'left'; |
| 488 |
$border_show_hide = !empty($settings['border_show_hide']) ? $settings['border_show_hide'] : ''; |
| 489 |
|
| 490 |
$processed_heading_title = ''; |
| 491 |
if (!empty($heading_title)) { |
| 492 |
$processed_heading_title = preg_replace('/\{\{([^}]+)\}\}/', '<span class="eel-highlighted">$1</span>', $heading_title); |
| 493 |
} |
| 494 |
?> |
| 495 |
<div class="eel-page-title-wrapper eel-title-size-<?php echo esc_attr($title_size); ?> eel-border-position-<?php echo esc_attr($border_position); ?>"> |
| 496 |
|
| 497 |
<?php if ( isset( $settings['custom_title_show_hide']) && !empty( $settings['custom_title_show_hide'] ) && $settings['custom_title_show_hide'] === 'yes' ){ ?> |
| 498 |
<<?php echo esc_attr($tag); ?> class="eel-page-title"> |
| 499 |
<?php if ($border_show_hide == 'yes') { ?> |
| 500 |
<span class="eel-border-left"></span> |
| 501 |
<?php } ?> |
| 502 |
<?php echo wp_kses_post($title); ?> |
| 503 |
</<?php echo esc_attr($tag); ?>> |
| 504 |
<?php } ?> |
| 505 |
<?php if ($heading_title): ?> |
| 506 |
<<?php echo esc_attr($heading_title_tag); ?> class="eel-heading-title"><?php echo wp_kses_post($processed_heading_title); ?></<?php echo esc_attr($heading_title_tag); ?>> |
| 507 |
<?php endif; ?> |
| 508 |
|
| 509 |
<?php if ($description): ?> |
| 510 |
<div class="eel-page-description"><?php echo wp_kses_post($description); ?></div> |
| 511 |
<?php endif; ?> |
| 512 |
</div> |
| 513 |
<?php |
| 514 |
} |
| 515 |
|
| 516 |
} |
| 517 |
|