| 1 |
<?php |
| 2 |
/** |
| 3 |
* Theme Builder Post Title widget (Free). |
| 4 |
* |
| 5 |
* @package King_Addons |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace King_Addons; |
| 9 |
|
| 10 |
use Elementor\Controls_Manager; |
| 11 |
use Elementor\Group_Control_Typography; |
| 12 |
use Elementor\Widget_Base; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Renders the current post title in Theme Builder templates. |
| 20 |
*/ |
| 21 |
class TB_Post_Title extends Widget_Base |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Widget slug. |
| 25 |
* |
| 26 |
* @return string |
| 27 |
*/ |
| 28 |
public function get_name(): string |
| 29 |
{ |
| 30 |
return 'king-addons-tb-post-title'; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Widget title. |
| 35 |
* |
| 36 |
* @return string |
| 37 |
*/ |
| 38 |
public function get_title(): string |
| 39 |
{ |
| 40 |
return esc_html__('TB - Post Title', 'king-addons'); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Widget icon. |
| 45 |
* |
| 46 |
* @return string |
| 47 |
*/ |
| 48 |
public function get_icon(): string |
| 49 |
{ |
| 50 |
return 'king-addons-icon king-addons-tb-post-title'; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Style dependencies. |
| 55 |
* |
| 56 |
* @return array<int, string> |
| 57 |
*/ |
| 58 |
public function get_style_depends(): array |
| 59 |
{ |
| 60 |
return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-tb-post-title-style']; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Script dependencies. |
| 65 |
* |
| 66 |
* @return array<int, string> |
| 67 |
*/ |
| 68 |
public function get_script_depends(): array |
| 69 |
{ |
| 70 |
return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-tb-post-title-script']; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Categories. |
| 75 |
* |
| 76 |
* @return array<int, string> |
| 77 |
*/ |
| 78 |
public function get_categories(): array |
| 79 |
{ |
| 80 |
return ['king-addons-theme-builder']; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Keywords. |
| 85 |
* |
| 86 |
* @return array<int, string> |
| 87 |
*/ |
| 88 |
public function get_keywords(): array |
| 89 |
{ |
| 90 |
return ['title', 'post', 'heading', 'theme builder', 'king-addons']; |
| 91 |
} |
| 92 |
|
| 93 |
public function get_custom_help_url() |
| 94 |
{ |
| 95 |
return 'mailto:[email protected]?subject=Bug Report - King Addons&body=Please describe the issue'; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Register controls. |
| 100 |
* |
| 101 |
* @return void |
| 102 |
*/ |
| 103 |
public function register_controls(): void |
| 104 |
{ |
| 105 |
$this->register_content_controls(false); |
| 106 |
$this->register_style_controls(false); |
| 107 |
$this->register_pro_notice_controls(); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Render widget output. |
| 112 |
* |
| 113 |
* @return void |
| 114 |
*/ |
| 115 |
public function render(): void |
| 116 |
{ |
| 117 |
$settings = $this->get_settings_for_display(); |
| 118 |
$this->render_output($settings, false); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Content controls. |
| 123 |
* |
| 124 |
* @param bool $is_pro Whether pro controls are enabled. |
| 125 |
* |
| 126 |
* @return void |
| 127 |
*/ |
| 128 |
protected function register_content_controls(bool $is_pro): void |
| 129 |
{ |
| 130 |
$this->start_controls_section( |
| 131 |
'kng_content_section', |
| 132 |
[ |
| 133 |
'label' => KING_ADDONS_ELEMENTOR_ICON . esc_html__('Content', 'king-addons'), |
| 134 |
'tab' => Controls_Manager::TAB_CONTENT, |
| 135 |
] |
| 136 |
); |
| 137 |
|
| 138 |
$this->add_control( |
| 139 |
'kng_html_tag', |
| 140 |
[ |
| 141 |
'label' => esc_html__('HTML Tag', 'king-addons'), |
| 142 |
'type' => Controls_Manager::SELECT, |
| 143 |
'default' => 'h1', |
| 144 |
'options' => [ |
| 145 |
'h1' => 'H1', |
| 146 |
'h2' => 'H2', |
| 147 |
'h3' => 'H3', |
| 148 |
'div' => 'div', |
| 149 |
'span' => 'span', |
| 150 |
], |
| 151 |
] |
| 152 |
); |
| 153 |
|
| 154 |
$this->add_control( |
| 155 |
'kng_link_to', |
| 156 |
[ |
| 157 |
'label' => esc_html__('Link', 'king-addons'), |
| 158 |
'type' => Controls_Manager::SELECT, |
| 159 |
'default' => 'none', |
| 160 |
'options' => [ |
| 161 |
'none' => esc_html__('None', 'king-addons'), |
| 162 |
'post' => esc_html__('Post Permalink', 'king-addons'), |
| 163 |
], |
| 164 |
] |
| 165 |
); |
| 166 |
|
| 167 |
$this->add_control( |
| 168 |
'kng_truncate_mode', |
| 169 |
[ |
| 170 |
'label' => $is_pro ? |
| 171 |
esc_html__('Truncate Mode', 'king-addons') : |
| 172 |
sprintf(__('Truncate Mode %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 173 |
'type' => Controls_Manager::SELECT, |
| 174 |
'options' => [ |
| 175 |
'none' => esc_html__('None', 'king-addons'), |
| 176 |
'words' => esc_html__('Words', 'king-addons'), |
| 177 |
'chars' => esc_html__('Characters', 'king-addons'), |
| 178 |
], |
| 179 |
'default' => 'none', |
| 180 |
'classes' => $is_pro ? '' : 'king-addons-pro-control no-distance', |
| 181 |
] |
| 182 |
); |
| 183 |
|
| 184 |
$this->add_control( |
| 185 |
'kng_truncate_length', |
| 186 |
[ |
| 187 |
'label' => $is_pro ? |
| 188 |
esc_html__('Truncate Length', 'king-addons') : |
| 189 |
sprintf(__('Truncate Length %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 190 |
'type' => Controls_Manager::NUMBER, |
| 191 |
'min' => 1, |
| 192 |
'default' => 10, |
| 193 |
'condition' => [ |
| 194 |
'kng_truncate_mode!' => 'none', |
| 195 |
], |
| 196 |
'classes' => $is_pro ? '' : 'king-addons-pro-control', |
| 197 |
] |
| 198 |
); |
| 199 |
|
| 200 |
$this->add_control( |
| 201 |
'kng_hover_underline', |
| 202 |
[ |
| 203 |
'label' => $is_pro ? |
| 204 |
esc_html__('Hover Underline', 'king-addons') : |
| 205 |
sprintf(__('Hover Underline %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 206 |
'type' => Controls_Manager::SWITCHER, |
| 207 |
'return_value' => 'yes', |
| 208 |
'default' => '', |
| 209 |
'classes' => $is_pro ? '' : 'king-addons-pro-control', |
| 210 |
] |
| 211 |
); |
| 212 |
|
| 213 |
$this->end_controls_section(); |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Style controls. |
| 218 |
* |
| 219 |
* @param bool $is_pro Whether pro controls are enabled. |
| 220 |
* |
| 221 |
* @return void |
| 222 |
*/ |
| 223 |
protected function register_style_controls(bool $is_pro): void |
| 224 |
{ |
| 225 |
$this->start_controls_section( |
| 226 |
'kng_style_section', |
| 227 |
[ |
| 228 |
'label' => esc_html__('Style', 'king-addons'), |
| 229 |
'tab' => Controls_Manager::TAB_STYLE, |
| 230 |
] |
| 231 |
); |
| 232 |
|
| 233 |
$this->add_group_control( |
| 234 |
Group_Control_Typography::get_type(), |
| 235 |
[ |
| 236 |
'name' => 'kng_typography', |
| 237 |
'selector' => '{{WRAPPER}} .king-addons-tb-post-title', |
| 238 |
] |
| 239 |
); |
| 240 |
|
| 241 |
$this->add_control( |
| 242 |
'kng_color', |
| 243 |
[ |
| 244 |
'label' => esc_html__('Text Color', 'king-addons'), |
| 245 |
'type' => Controls_Manager::COLOR, |
| 246 |
'selectors' => [ |
| 247 |
'{{WRAPPER}} .king-addons-tb-post-title' => 'color: {{VALUE}};', |
| 248 |
], |
| 249 |
] |
| 250 |
); |
| 251 |
|
| 252 |
$this->add_control( |
| 253 |
'kng_color_hover', |
| 254 |
[ |
| 255 |
'label' => esc_html__('Hover Color', 'king-addons'), |
| 256 |
'type' => Controls_Manager::COLOR, |
| 257 |
'selectors' => [ |
| 258 |
'{{WRAPPER}} .king-addons-tb-post-title:hover' => 'color: {{VALUE}};', |
| 259 |
'{{WRAPPER}} .king-addons-tb-post-title a:hover' => 'color: {{VALUE}};', |
| 260 |
], |
| 261 |
] |
| 262 |
); |
| 263 |
|
| 264 |
$this->add_responsive_control( |
| 265 |
'kng_alignment', |
| 266 |
[ |
| 267 |
'label' => esc_html__('Alignment', 'king-addons'), |
| 268 |
'type' => Controls_Manager::CHOOSE, |
| 269 |
'options' => [ |
| 270 |
'left' => [ |
| 271 |
'title' => esc_html__('Left', 'king-addons'), |
| 272 |
'icon' => 'eicon-text-align-left', |
| 273 |
], |
| 274 |
'center' => [ |
| 275 |
'title' => esc_html__('Center', 'king-addons'), |
| 276 |
'icon' => 'eicon-text-align-center', |
| 277 |
], |
| 278 |
'right' => [ |
| 279 |
'title' => esc_html__('Right', 'king-addons'), |
| 280 |
'icon' => 'eicon-text-align-right', |
| 281 |
], |
| 282 |
'justify' => [ |
| 283 |
'title' => esc_html__('Justify', 'king-addons'), |
| 284 |
'icon' => 'eicon-text-align-justify', |
| 285 |
], |
| 286 |
], |
| 287 |
'selectors' => [ |
| 288 |
'{{WRAPPER}} .king-addons-tb-post-title' => 'text-align: {{VALUE}};', |
| 289 |
], |
| 290 |
] |
| 291 |
); |
| 292 |
|
| 293 |
$this->add_responsive_control( |
| 294 |
'kng_margin', |
| 295 |
[ |
| 296 |
'label' => esc_html__('Margin', 'king-addons'), |
| 297 |
'type' => Controls_Manager::DIMENSIONS, |
| 298 |
'size_units' => ['px', 'em', '%'], |
| 299 |
'selectors' => [ |
| 300 |
'{{WRAPPER}} .king-addons-tb-post-title' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 301 |
], |
| 302 |
] |
| 303 |
); |
| 304 |
|
| 305 |
$this->add_responsive_control( |
| 306 |
'kng_padding', |
| 307 |
[ |
| 308 |
'label' => esc_html__('Padding', 'king-addons'), |
| 309 |
'type' => Controls_Manager::DIMENSIONS, |
| 310 |
'size_units' => ['px', 'em', '%'], |
| 311 |
'selectors' => [ |
| 312 |
'{{WRAPPER}} .king-addons-tb-post-title' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 313 |
], |
| 314 |
] |
| 315 |
); |
| 316 |
|
| 317 |
if ($is_pro) { |
| 318 |
$this->add_control( |
| 319 |
'kng_underline_color', |
| 320 |
[ |
| 321 |
'label' => esc_html__('Underline Color', 'king-addons'), |
| 322 |
'type' => Controls_Manager::COLOR, |
| 323 |
'condition' => [ |
| 324 |
'kng_hover_underline' => 'yes', |
| 325 |
], |
| 326 |
'selectors' => [ |
| 327 |
'{{WRAPPER}} .king-addons-tb-post-title--underline::after' => 'background-color: {{VALUE}};', |
| 328 |
], |
| 329 |
] |
| 330 |
); |
| 331 |
} |
| 332 |
|
| 333 |
$this->end_controls_section(); |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* Pro upsell notice. |
| 338 |
* |
| 339 |
* @return void |
| 340 |
*/ |
| 341 |
protected function register_pro_notice_controls(): void |
| 342 |
{ |
| 343 |
if (king_addons_freemius()->can_use_premium_code__premium_only()) { |
| 344 |
return; |
| 345 |
} |
| 346 |
|
| 347 |
Core::renderProFeaturesSection( |
| 348 |
$this, |
| 349 |
'', |
| 350 |
Controls_Manager::RAW_HTML, |
| 351 |
'tb-post-title', |
| 352 |
[ |
| 353 |
'Title truncation by words or characters', |
| 354 |
'Hover underline and advanced hover effects', |
| 355 |
] |
| 356 |
); |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* Render output helper. |
| 361 |
* |
| 362 |
* @param array<string, mixed> $settings Widget settings. |
| 363 |
* @param bool $is_pro Whether pro mode is enabled. |
| 364 |
* |
| 365 |
* @return void |
| 366 |
*/ |
| 367 |
protected function render_output(array $settings, bool $is_pro): void |
| 368 |
{ |
| 369 |
$post = get_post(); |
| 370 |
if (!$post instanceof \WP_Post) { |
| 371 |
return; |
| 372 |
} |
| 373 |
|
| 374 |
$title = get_the_title($post); |
| 375 |
if ('' === (string) $title) { |
| 376 |
return; |
| 377 |
} |
| 378 |
|
| 379 |
$tag = isset($settings['kng_html_tag']) ? strtolower((string) $settings['kng_html_tag']) : 'h1'; |
| 380 |
$allowed_tags = ['h1', 'h2', 'h3', 'div', 'span']; |
| 381 |
if (!in_array($tag, $allowed_tags, true)) { |
| 382 |
$tag = 'h1'; |
| 383 |
} |
| 384 |
|
| 385 |
$truncate_mode = $settings['kng_truncate_mode'] ?? 'none'; |
| 386 |
$truncate_length = (int) ($settings['kng_truncate_length'] ?? 0); |
| 387 |
if ($is_pro && $truncate_mode !== 'none' && $truncate_length > 0) { |
| 388 |
if ('words' === $truncate_mode) { |
| 389 |
$title = wp_trim_words($title, $truncate_length, '…'); |
| 390 |
} elseif ('chars' === $truncate_mode) { |
| 391 |
$title = mb_substr($title, 0, $truncate_length) . '…'; |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
$link_to = $settings['kng_link_to'] ?? 'none'; |
| 396 |
$title_html = esc_html($title); |
| 397 |
|
| 398 |
if ('post' === $link_to) { |
| 399 |
$permalink = get_permalink($post); |
| 400 |
if ($permalink) { |
| 401 |
$title_html = sprintf( |
| 402 |
'<a href="%1$s" class="king-addons-tb-post-title__link">%2$s</a>', |
| 403 |
esc_url($permalink), |
| 404 |
$title_html |
| 405 |
); |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
$classes = ['king-addons-tb-post-title']; |
| 410 |
if ($is_pro && ('yes' === ($settings['kng_hover_underline'] ?? ''))) { |
| 411 |
$classes[] = 'king-addons-tb-post-title--underline'; |
| 412 |
} |
| 413 |
|
| 414 |
printf( |
| 415 |
'<%1$s class="%2$s">%3$s</%1$s>', |
| 416 |
esc_attr($tag), |
| 417 |
esc_attr(implode(' ', $classes)), |
| 418 |
$title_html // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 419 |
); |
| 420 |
} |
| 421 |
} |
| 422 |
|