| 1 |
<?php |
| 2 |
/** |
| 3 |
* Theme Builder Author Box widget (Free). |
| 4 |
* |
| 5 |
* @package King_Addons |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace King_Addons; |
| 9 |
|
| 10 |
use Elementor\Controls_Manager; |
| 11 |
use Elementor\Group_Control_Border; |
| 12 |
use Elementor\Group_Control_Box_Shadow; |
| 13 |
use Elementor\Group_Control_Typography; |
| 14 |
use Elementor\Repeater; |
| 15 |
use Elementor\Widget_Base; |
| 16 |
|
| 17 |
if (!defined('ABSPATH')) { |
| 18 |
exit; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Displays the post author information. |
| 23 |
*/ |
| 24 |
class TB_Author_Box extends Widget_Base |
| 25 |
{ |
| 26 |
/** |
| 27 |
* Widget slug. |
| 28 |
* |
| 29 |
* @return string |
| 30 |
*/ |
| 31 |
public function get_name(): string |
| 32 |
{ |
| 33 |
return 'king-addons-tb-author-box'; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Widget title. |
| 38 |
* |
| 39 |
* @return string |
| 40 |
*/ |
| 41 |
public function get_title(): string |
| 42 |
{ |
| 43 |
return esc_html__('TB - Author Box', 'king-addons'); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Widget icon. |
| 48 |
* |
| 49 |
* @return string |
| 50 |
*/ |
| 51 |
public function get_icon(): string |
| 52 |
{ |
| 53 |
return 'king-addons-icon king-addons-tb-author-box'; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Style dependencies. |
| 58 |
* |
| 59 |
* @return array<int, string> |
| 60 |
*/ |
| 61 |
public function get_style_depends(): array |
| 62 |
{ |
| 63 |
return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-tb-author-box-style']; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Script dependencies. |
| 68 |
* |
| 69 |
* @return array<int, string> |
| 70 |
*/ |
| 71 |
public function get_script_depends(): array |
| 72 |
{ |
| 73 |
return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-tb-author-box-script']; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Categories. |
| 78 |
* |
| 79 |
* @return array<int, string> |
| 80 |
*/ |
| 81 |
public function get_categories(): array |
| 82 |
{ |
| 83 |
return ['king-addons-theme-builder']; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Keywords. |
| 88 |
* |
| 89 |
* @return array<int, string> |
| 90 |
*/ |
| 91 |
public function get_keywords(): array |
| 92 |
{ |
| 93 |
return ['author', 'bio', 'profile', 'theme builder', 'king-addons']; |
| 94 |
} |
| 95 |
|
| 96 |
public function get_custom_help_url() |
| 97 |
{ |
| 98 |
return 'mailto:[email protected]?subject=Bug Report - King Addons&body=Please describe the issue'; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Register controls. |
| 103 |
* |
| 104 |
* @return void |
| 105 |
*/ |
| 106 |
public function register_controls(): void |
| 107 |
{ |
| 108 |
$this->register_content_controls(false); |
| 109 |
$this->register_style_controls(false); |
| 110 |
$this->register_pro_notice_controls(); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Render widget output. |
| 115 |
* |
| 116 |
* @return void |
| 117 |
*/ |
| 118 |
public function render(): void |
| 119 |
{ |
| 120 |
$settings = $this->get_settings_for_display(); |
| 121 |
$this->render_output($settings, false); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Content controls. |
| 126 |
* |
| 127 |
* @param bool $is_pro Whether Pro controls are enabled. |
| 128 |
* |
| 129 |
* @return void |
| 130 |
*/ |
| 131 |
protected function register_content_controls(bool $is_pro): void |
| 132 |
{ |
| 133 |
$this->start_controls_section( |
| 134 |
'kng_content_section', |
| 135 |
[ |
| 136 |
'label' => KING_ADDONS_ELEMENTOR_ICON . esc_html__('Content', 'king-addons'), |
| 137 |
'tab' => Controls_Manager::TAB_CONTENT, |
| 138 |
] |
| 139 |
); |
| 140 |
|
| 141 |
$this->add_control( |
| 142 |
'kng_layout', |
| 143 |
[ |
| 144 |
'label' => esc_html__('Layout', 'king-addons'), |
| 145 |
'type' => Controls_Manager::SELECT, |
| 146 |
'default' => 'side', |
| 147 |
'options' => [ |
| 148 |
'side' => esc_html__('Avatar Left', 'king-addons'), |
| 149 |
'stacked' => esc_html__('Avatar Top', 'king-addons'), |
| 150 |
'center' => esc_html__('Centered', 'king-addons'), |
| 151 |
], |
| 152 |
] |
| 153 |
); |
| 154 |
|
| 155 |
$this->add_control( |
| 156 |
'kng_show_avatar', |
| 157 |
[ |
| 158 |
'label' => esc_html__('Show Avatar', 'king-addons'), |
| 159 |
'type' => Controls_Manager::SWITCHER, |
| 160 |
'return_value' => 'yes', |
| 161 |
'default' => 'yes', |
| 162 |
] |
| 163 |
); |
| 164 |
|
| 165 |
$this->add_control( |
| 166 |
'kng_avatar_size', |
| 167 |
[ |
| 168 |
'label' => esc_html__('Avatar Size', 'king-addons'), |
| 169 |
'type' => Controls_Manager::SLIDER, |
| 170 |
'size_units' => ['px'], |
| 171 |
'range' => [ |
| 172 |
'px' => ['min' => 30, 'max' => 200], |
| 173 |
], |
| 174 |
'default' => [ |
| 175 |
'size' => 72, |
| 176 |
'unit' => 'px', |
| 177 |
], |
| 178 |
'condition' => [ |
| 179 |
'kng_show_avatar' => 'yes', |
| 180 |
], |
| 181 |
] |
| 182 |
); |
| 183 |
|
| 184 |
$this->add_control( |
| 185 |
'kng_link_type', |
| 186 |
[ |
| 187 |
'label' => esc_html__('Name Link', 'king-addons'), |
| 188 |
'type' => Controls_Manager::SELECT, |
| 189 |
'default' => 'archive', |
| 190 |
'options' => [ |
| 191 |
'none' => esc_html__('None', 'king-addons'), |
| 192 |
'archive' => esc_html__('Author Archive', 'king-addons'), |
| 193 |
'custom' => $is_pro ? |
| 194 |
esc_html__('Custom URL', 'king-addons') : |
| 195 |
sprintf(__('Custom URL %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 196 |
], |
| 197 |
] |
| 198 |
); |
| 199 |
|
| 200 |
$this->add_control( |
| 201 |
'kng_custom_link', |
| 202 |
[ |
| 203 |
'label' => esc_html__('Custom URL', 'king-addons'), |
| 204 |
'type' => Controls_Manager::URL, |
| 205 |
'dynamic' => ['active' => true], |
| 206 |
'placeholder' => 'https://example.com', |
| 207 |
'condition' => [ |
| 208 |
'kng_link_type' => 'custom', |
| 209 |
], |
| 210 |
'classes' => $is_pro ? '' : 'king-addons-pro-control', |
| 211 |
] |
| 212 |
); |
| 213 |
|
| 214 |
$this->add_control( |
| 215 |
'kng_show_website', |
| 216 |
[ |
| 217 |
'label' => esc_html__('Show Website', 'king-addons'), |
| 218 |
'type' => Controls_Manager::SWITCHER, |
| 219 |
'return_value' => 'yes', |
| 220 |
'default' => 'yes', |
| 221 |
] |
| 222 |
); |
| 223 |
|
| 224 |
$this->add_control( |
| 225 |
'kng_show_bio', |
| 226 |
[ |
| 227 |
'label' => esc_html__('Show Bio', 'king-addons'), |
| 228 |
'type' => Controls_Manager::SWITCHER, |
| 229 |
'return_value' => 'yes', |
| 230 |
'default' => 'yes', |
| 231 |
] |
| 232 |
); |
| 233 |
|
| 234 |
if ($is_pro) { |
| 235 |
$repeater = new Repeater(); |
| 236 |
$repeater->add_control( |
| 237 |
'kng_social_label', |
| 238 |
[ |
| 239 |
'label' => esc_html__('Label', 'king-addons'), |
| 240 |
'type' => Controls_Manager::TEXT, |
| 241 |
'default' => esc_html__('Social', 'king-addons'), |
| 242 |
] |
| 243 |
); |
| 244 |
$repeater->add_control( |
| 245 |
'kng_social_url', |
| 246 |
[ |
| 247 |
'label' => esc_html__('URL', 'king-addons'), |
| 248 |
'type' => Controls_Manager::URL, |
| 249 |
'placeholder' => 'https://example.com', |
| 250 |
] |
| 251 |
); |
| 252 |
|
| 253 |
$this->add_control( |
| 254 |
'kng_social_links', |
| 255 |
[ |
| 256 |
'label' => esc_html__('Social Links', 'king-addons'), |
| 257 |
'type' => Controls_Manager::REPEATER, |
| 258 |
'fields' => $repeater->get_controls(), |
| 259 |
'title_field' => '{{ kng_social_label }}', |
| 260 |
] |
| 261 |
); |
| 262 |
} |
| 263 |
|
| 264 |
$this->end_controls_section(); |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Style controls. |
| 269 |
* |
| 270 |
* @param bool $is_pro Whether Pro controls are enabled. |
| 271 |
* |
| 272 |
* @return void |
| 273 |
*/ |
| 274 |
protected function register_style_controls(bool $is_pro): void |
| 275 |
{ |
| 276 |
$this->start_controls_section( |
| 277 |
'kng_style_section', |
| 278 |
[ |
| 279 |
'label' => esc_html__('Box', 'king-addons'), |
| 280 |
'tab' => Controls_Manager::TAB_STYLE, |
| 281 |
] |
| 282 |
); |
| 283 |
|
| 284 |
$this->add_control( |
| 285 |
'kng_box_background', |
| 286 |
[ |
| 287 |
'label' => esc_html__('Background', 'king-addons'), |
| 288 |
'type' => Controls_Manager::COLOR, |
| 289 |
'selectors' => [ |
| 290 |
'{{WRAPPER}} .king-addons-tb-author-box' => 'background-color: {{VALUE}};', |
| 291 |
], |
| 292 |
] |
| 293 |
); |
| 294 |
|
| 295 |
$this->add_group_control( |
| 296 |
Group_Control_Border::get_type(), |
| 297 |
[ |
| 298 |
'name' => 'kng_box_border', |
| 299 |
'selector' => '{{WRAPPER}} .king-addons-tb-author-box', |
| 300 |
] |
| 301 |
); |
| 302 |
|
| 303 |
$this->add_group_control( |
| 304 |
Group_Control_Box_Shadow::get_type(), |
| 305 |
[ |
| 306 |
'name' => 'kng_box_shadow', |
| 307 |
'selector' => '{{WRAPPER}} .king-addons-tb-author-box', |
| 308 |
] |
| 309 |
); |
| 310 |
|
| 311 |
$this->add_responsive_control( |
| 312 |
'kng_box_padding', |
| 313 |
[ |
| 314 |
'label' => esc_html__('Padding', 'king-addons'), |
| 315 |
'type' => Controls_Manager::DIMENSIONS, |
| 316 |
'size_units' => ['px', 'em', '%'], |
| 317 |
'selectors' => [ |
| 318 |
'{{WRAPPER}} .king-addons-tb-author-box' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 319 |
], |
| 320 |
] |
| 321 |
); |
| 322 |
|
| 323 |
$this->end_controls_section(); |
| 324 |
|
| 325 |
$this->start_controls_section( |
| 326 |
'kng_content_style', |
| 327 |
[ |
| 328 |
'label' => esc_html__('Content', 'king-addons'), |
| 329 |
'tab' => Controls_Manager::TAB_STYLE, |
| 330 |
] |
| 331 |
); |
| 332 |
|
| 333 |
$this->add_group_control( |
| 334 |
Group_Control_Typography::get_type(), |
| 335 |
[ |
| 336 |
'name' => 'kng_name_typography', |
| 337 |
'label' => esc_html__('Name Typography', 'king-addons'), |
| 338 |
'selector' => '{{WRAPPER}} .king-addons-tb-author-box__name', |
| 339 |
] |
| 340 |
); |
| 341 |
|
| 342 |
$this->add_control( |
| 343 |
'kng_name_color', |
| 344 |
[ |
| 345 |
'label' => esc_html__('Name Color', 'king-addons'), |
| 346 |
'type' => Controls_Manager::COLOR, |
| 347 |
'selectors' => [ |
| 348 |
'{{WRAPPER}} .king-addons-tb-author-box__name' => 'color: {{VALUE}};', |
| 349 |
'{{WRAPPER}} .king-addons-tb-author-box__name a' => 'color: {{VALUE}};', |
| 350 |
], |
| 351 |
] |
| 352 |
); |
| 353 |
|
| 354 |
$this->add_group_control( |
| 355 |
Group_Control_Typography::get_type(), |
| 356 |
[ |
| 357 |
'name' => 'kng_bio_typography', |
| 358 |
'label' => esc_html__('Bio Typography', 'king-addons'), |
| 359 |
'selector' => '{{WRAPPER}} .king-addons-tb-author-box__bio, {{WRAPPER}} .king-addons-tb-author-box__website', |
| 360 |
] |
| 361 |
); |
| 362 |
|
| 363 |
$this->add_control( |
| 364 |
'kng_text_color', |
| 365 |
[ |
| 366 |
'label' => esc_html__('Text Color', 'king-addons'), |
| 367 |
'type' => Controls_Manager::COLOR, |
| 368 |
'selectors' => [ |
| 369 |
'{{WRAPPER}} .king-addons-tb-author-box__bio' => 'color: {{VALUE}};', |
| 370 |
'{{WRAPPER}} .king-addons-tb-author-box__website' => 'color: {{VALUE}};', |
| 371 |
'{{WRAPPER}} .king-addons-tb-author-box__website a' => 'color: {{VALUE}};', |
| 372 |
], |
| 373 |
] |
| 374 |
); |
| 375 |
|
| 376 |
$this->end_controls_section(); |
| 377 |
} |
| 378 |
|
| 379 |
/** |
| 380 |
* Pro upsell. |
| 381 |
* |
| 382 |
* @return void |
| 383 |
*/ |
| 384 |
protected function register_pro_notice_controls(): void |
| 385 |
{ |
| 386 |
if (king_addons_freemius()->can_use_premium_code__premium_only()) { |
| 387 |
return; |
| 388 |
} |
| 389 |
|
| 390 |
Core::renderProFeaturesSection( |
| 391 |
$this, |
| 392 |
'', |
| 393 |
Controls_Manager::RAW_HTML, |
| 394 |
'tb-author-box', |
| 395 |
[ |
| 396 |
'Custom link target for author name', |
| 397 |
'Social icons list with custom URLs', |
| 398 |
'Additional layout and badge styles', |
| 399 |
] |
| 400 |
); |
| 401 |
} |
| 402 |
|
| 403 |
/** |
| 404 |
* Render output helper. |
| 405 |
* |
| 406 |
* @param array<string, mixed> $settings Settings. |
| 407 |
* @param bool $is_pro Whether Pro is enabled. |
| 408 |
* |
| 409 |
* @return void |
| 410 |
*/ |
| 411 |
protected function render_output(array $settings, bool $is_pro): void |
| 412 |
{ |
| 413 |
$post = get_post(); |
| 414 |
if (!$post instanceof \WP_Post) { |
| 415 |
return; |
| 416 |
} |
| 417 |
|
| 418 |
$author_id = (int) $post->post_author; |
| 419 |
$name = get_the_author_meta('display_name', $author_id); |
| 420 |
$bio = get_the_author_meta('description', $author_id); |
| 421 |
$website = get_the_author_meta('user_url', $author_id); |
| 422 |
|
| 423 |
$layout = $settings['kng_layout'] ?? 'side'; |
| 424 |
$classes = ['king-addons-tb-author-box', 'king-addons-tb-author-box--' . $layout]; |
| 425 |
|
| 426 |
echo '<div class="' . esc_attr(implode(' ', $classes)) . '">'; |
| 427 |
|
| 428 |
if ('yes' === ($settings['kng_show_avatar'] ?? 'yes')) { |
| 429 |
$size = isset($settings['kng_avatar_size']['size']) ? (int) $settings['kng_avatar_size']['size'] : 72; |
| 430 |
echo '<div class="king-addons-tb-author-box__avatar">' . get_avatar($author_id, $size) . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 431 |
} |
| 432 |
|
| 433 |
echo '<div class="king-addons-tb-author-box__content">'; |
| 434 |
|
| 435 |
if ($name) { |
| 436 |
$name_html = esc_html($name); |
| 437 |
$link_type = $settings['kng_link_type'] ?? 'archive'; |
| 438 |
if ('archive' === $link_type) { |
| 439 |
$url = get_author_posts_url($author_id); |
| 440 |
$name_html = '<a href="' . esc_url($url) . '">' . $name_html . '</a>'; |
| 441 |
} elseif ('custom' === $link_type && $is_pro && !empty($settings['kng_custom_link']['url'])) { |
| 442 |
$this->add_link_attributes('kng_author_name', $settings['kng_custom_link']); |
| 443 |
$name_html = '<a ' . $this->get_render_attribute_string('kng_author_name') . '>' . $name_html . '</a>'; |
| 444 |
} |
| 445 |
|
| 446 |
echo '<div class="king-addons-tb-author-box__name">' . $name_html . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 447 |
} |
| 448 |
|
| 449 |
if ('yes' === ($settings['kng_show_bio'] ?? 'yes') && $bio) { |
| 450 |
echo '<div class="king-addons-tb-author-box__bio">' . esc_html($bio) . '</div>'; |
| 451 |
} |
| 452 |
|
| 453 |
if ('yes' === ($settings['kng_show_website'] ?? 'yes') && $website) { |
| 454 |
echo '<div class="king-addons-tb-author-box__website"><a href="' . esc_url($website) . '">' . esc_html($website) . '</a></div>'; |
| 455 |
} |
| 456 |
|
| 457 |
if ($is_pro && !empty($settings['kng_social_links'])) { |
| 458 |
echo '<div class="king-addons-tb-author-box__social">'; |
| 459 |
$social_i = 0; |
| 460 |
foreach ($settings['kng_social_links'] as $social) { |
| 461 |
if (empty($social['kng_social_url']['url'])) { |
| 462 |
continue; |
| 463 |
} |
| 464 |
$key = 'kng_social_' . sanitize_html_class((string) ($social['_id'] ?? $social_i)); |
| 465 |
$social_i++; |
| 466 |
$this->add_link_attributes($key, $social['kng_social_url']); |
| 467 |
$this->add_render_attribute($key, 'class', 'king-addons-tb-author-box__social-link'); |
| 468 |
$label = $social['kng_social_label'] ?? esc_html__('Social', 'king-addons'); |
| 469 |
echo '<a ' . $this->get_render_attribute_string($key) . '>' . esc_html($label) . '</a>'; |
| 470 |
} |
| 471 |
echo '</div>'; |
| 472 |
} |
| 473 |
|
| 474 |
echo '</div></div>'; |
| 475 |
} |
| 476 |
} |
| 477 |
|