| 1 |
<?php |
| 2 |
/** |
| 3 |
* Theme Builder Author Info 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 |
* Shows the author whose archive is being viewed. |
| 20 |
* |
| 21 |
* TB - Author Box describes the author of a single post; this one reads the |
| 22 |
* author from the archive query, so it belongs on an Author template. |
| 23 |
*/ |
| 24 |
class TB_Author_Info 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-info'; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Widget title. |
| 38 |
* |
| 39 |
* @return string |
| 40 |
*/ |
| 41 |
public function get_title(): string |
| 42 |
{ |
| 43 |
return esc_html__('TB - Author Info', '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-info'; |
| 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-info-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-info-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', 'archive', '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 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_show_avatar', |
| 143 |
[ |
| 144 |
'label' => esc_html__('Avatar', 'king-addons'), |
| 145 |
'type' => Controls_Manager::SWITCHER, |
| 146 |
'default' => 'yes', |
| 147 |
] |
| 148 |
); |
| 149 |
|
| 150 |
$this->add_responsive_control( |
| 151 |
'kng_avatar_size', |
| 152 |
[ |
| 153 |
'label' => esc_html__('Avatar Size', 'king-addons'), |
| 154 |
'type' => Controls_Manager::SLIDER, |
| 155 |
'size_units' => ['px'], |
| 156 |
'range' => ['px' => ['min' => 32, 'max' => 200]], |
| 157 |
'default' => ['unit' => 'px', 'size' => 96], |
| 158 |
'condition' => ['kng_show_avatar' => 'yes'], |
| 159 |
'selectors' => [ |
| 160 |
'{{WRAPPER}} .king-addons-tb-author-info__avatar img' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};', |
| 161 |
], |
| 162 |
] |
| 163 |
); |
| 164 |
|
| 165 |
$this->add_control( |
| 166 |
'kng_show_bio', |
| 167 |
[ |
| 168 |
'label' => esc_html__('Biography', 'king-addons'), |
| 169 |
'type' => Controls_Manager::SWITCHER, |
| 170 |
'default' => 'yes', |
| 171 |
] |
| 172 |
); |
| 173 |
|
| 174 |
$this->add_control( |
| 175 |
'kng_show_post_count', |
| 176 |
[ |
| 177 |
'label' => esc_html__('Post Count', 'king-addons'), |
| 178 |
'type' => Controls_Manager::SWITCHER, |
| 179 |
'default' => 'yes', |
| 180 |
] |
| 181 |
); |
| 182 |
|
| 183 |
$this->add_control( |
| 184 |
'kng_post_count_format', |
| 185 |
[ |
| 186 |
'label' => esc_html__('Post Count Format', 'king-addons'), |
| 187 |
'type' => Controls_Manager::TEXT, |
| 188 |
'default' => esc_html__('{count} posts', 'king-addons'), |
| 189 |
'condition' => ['kng_show_post_count' => 'yes'], |
| 190 |
] |
| 191 |
); |
| 192 |
|
| 193 |
$this->add_control( |
| 194 |
'kng_show_website', |
| 195 |
[ |
| 196 |
'label' => esc_html__('Website Link', 'king-addons'), |
| 197 |
'type' => Controls_Manager::SWITCHER, |
| 198 |
'default' => '', |
| 199 |
] |
| 200 |
); |
| 201 |
|
| 202 |
$this->add_control( |
| 203 |
'kng_social_links', |
| 204 |
[ |
| 205 |
'label' => $is_pro ? |
| 206 |
esc_html__('Social Links', 'king-addons') : |
| 207 |
sprintf(__('Social Links %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 208 |
'type' => Controls_Manager::SWITCHER, |
| 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_name_typography', |
| 237 |
'selector' => '{{WRAPPER}} .king-addons-tb-author-info__name', |
| 238 |
] |
| 239 |
); |
| 240 |
|
| 241 |
$this->add_control( |
| 242 |
'kng_name_color', |
| 243 |
[ |
| 244 |
'label' => esc_html__('Name Color', 'king-addons'), |
| 245 |
'type' => Controls_Manager::COLOR, |
| 246 |
'selectors' => [ |
| 247 |
'{{WRAPPER}} .king-addons-tb-author-info__name' => 'color: {{VALUE}};', |
| 248 |
], |
| 249 |
] |
| 250 |
); |
| 251 |
|
| 252 |
$this->add_control( |
| 253 |
'kng_bio_color', |
| 254 |
[ |
| 255 |
'label' => esc_html__('Biography Color', 'king-addons'), |
| 256 |
'type' => Controls_Manager::COLOR, |
| 257 |
'selectors' => [ |
| 258 |
'{{WRAPPER}} .king-addons-tb-author-info__bio' => 'color: {{VALUE}};', |
| 259 |
], |
| 260 |
] |
| 261 |
); |
| 262 |
|
| 263 |
$this->add_control( |
| 264 |
'kng_count_color', |
| 265 |
[ |
| 266 |
'label' => esc_html__('Post Count Color', 'king-addons'), |
| 267 |
'type' => Controls_Manager::COLOR, |
| 268 |
'selectors' => [ |
| 269 |
'{{WRAPPER}} .king-addons-tb-author-info__count' => 'color: {{VALUE}};', |
| 270 |
], |
| 271 |
] |
| 272 |
); |
| 273 |
|
| 274 |
$this->add_control( |
| 275 |
'kng_bg_color', |
| 276 |
[ |
| 277 |
'label' => esc_html__('Background Color', 'king-addons'), |
| 278 |
'type' => Controls_Manager::COLOR, |
| 279 |
'selectors' => [ |
| 280 |
'{{WRAPPER}} .king-addons-tb-author-info' => 'background-color: {{VALUE}};', |
| 281 |
], |
| 282 |
] |
| 283 |
); |
| 284 |
|
| 285 |
$this->add_responsive_control( |
| 286 |
'kng_padding', |
| 287 |
[ |
| 288 |
'label' => esc_html__('Padding', 'king-addons'), |
| 289 |
'type' => Controls_Manager::DIMENSIONS, |
| 290 |
'size_units' => ['px', 'em'], |
| 291 |
'selectors' => [ |
| 292 |
'{{WRAPPER}} .king-addons-tb-author-info' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 293 |
], |
| 294 |
] |
| 295 |
); |
| 296 |
|
| 297 |
$this->add_responsive_control( |
| 298 |
'kng_alignment', |
| 299 |
[ |
| 300 |
'label' => esc_html__('Alignment', 'king-addons'), |
| 301 |
'type' => Controls_Manager::CHOOSE, |
| 302 |
'options' => [ |
| 303 |
'flex-start' => [ |
| 304 |
'title' => esc_html__('Left', 'king-addons'), |
| 305 |
'icon' => 'eicon-text-align-left', |
| 306 |
], |
| 307 |
'center' => [ |
| 308 |
'title' => esc_html__('Center', 'king-addons'), |
| 309 |
'icon' => 'eicon-text-align-center', |
| 310 |
], |
| 311 |
'flex-end' => [ |
| 312 |
'title' => esc_html__('Right', 'king-addons'), |
| 313 |
'icon' => 'eicon-text-align-right', |
| 314 |
], |
| 315 |
], |
| 316 |
'selectors_dictionary' => [ |
| 317 |
'flex-start' => 'align-items: flex-start; text-align: left;', |
| 318 |
'center' => 'align-items: center; text-align: center;', |
| 319 |
'flex-end' => 'align-items: flex-end; text-align: right;', |
| 320 |
], |
| 321 |
'selectors' => [ |
| 322 |
'{{WRAPPER}} .king-addons-tb-author-info' => '{{VALUE}}', |
| 323 |
], |
| 324 |
] |
| 325 |
); |
| 326 |
|
| 327 |
$this->end_controls_section(); |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Pro upsell. |
| 332 |
* |
| 333 |
* @return void |
| 334 |
*/ |
| 335 |
protected function register_pro_notice_controls(): void |
| 336 |
{ |
| 337 |
if (king_addons_freemius()->can_use_premium_code__premium_only()) { |
| 338 |
return; |
| 339 |
} |
| 340 |
|
| 341 |
Core::renderProFeaturesSection( |
| 342 |
$this, |
| 343 |
'', |
| 344 |
Controls_Manager::RAW_HTML, |
| 345 |
'tb-author-info', |
| 346 |
[ |
| 347 |
'Social profile links', |
| 348 |
'Custom avatar source', |
| 349 |
'Follow button', |
| 350 |
] |
| 351 |
); |
| 352 |
} |
| 353 |
|
| 354 |
/** |
| 355 |
* Resolve the author for the current view. |
| 356 |
* |
| 357 |
* @return \WP_User|null |
| 358 |
*/ |
| 359 |
protected function resolve_author(): ?\WP_User |
| 360 |
{ |
| 361 |
$author = null; |
| 362 |
|
| 363 |
if (is_author()) { |
| 364 |
$queried = get_queried_object(); |
| 365 |
if ($queried instanceof \WP_User) { |
| 366 |
$author = $queried; |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
if (!$author instanceof \WP_User && is_singular()) { |
| 371 |
$post_author = (int) get_post_field('post_author', (int) get_the_ID()); |
| 372 |
if ($post_author) { |
| 373 |
$found = get_user_by('id', $post_author); |
| 374 |
if ($found instanceof \WP_User) { |
| 375 |
$author = $found; |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
if (!$author instanceof \WP_User && \Elementor\Plugin::$instance->editor->is_edit_mode()) { |
| 381 |
$users = get_users(['number' => 1, 'orderby' => 'ID']); |
| 382 |
if (!empty($users[0]) && $users[0] instanceof \WP_User) { |
| 383 |
$author = $users[0]; |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
return $author instanceof \WP_User ? $author : null; |
| 388 |
} |
| 389 |
|
| 390 |
/** |
| 391 |
* Render output helper. |
| 392 |
* |
| 393 |
* @param array<string, mixed> $settings Settings. |
| 394 |
* @param bool $is_pro Pro flag. |
| 395 |
* |
| 396 |
* @return void |
| 397 |
*/ |
| 398 |
protected function render_output(array $settings, bool $is_pro): void |
| 399 |
{ |
| 400 |
$author = $this->resolve_author(); |
| 401 |
|
| 402 |
if (!$author instanceof \WP_User) { |
| 403 |
if (\Elementor\Plugin::$instance->editor->is_edit_mode()) { |
| 404 |
Core::renderEditorHint( |
| 405 |
esc_html__('This widget reads the author of the current archive. Preview the template from an author URL to see real data.', 'king-addons') |
| 406 |
); |
| 407 |
} |
| 408 |
return; |
| 409 |
} |
| 410 |
|
| 411 |
$show_avatar = 'yes' === ($settings['kng_show_avatar'] ?? 'yes'); |
| 412 |
$show_bio = 'yes' === ($settings['kng_show_bio'] ?? 'yes'); |
| 413 |
$show_count = 'yes' === ($settings['kng_show_post_count'] ?? 'yes'); |
| 414 |
$show_site = 'yes' === ($settings['kng_show_website'] ?? ''); |
| 415 |
|
| 416 |
echo '<div class="king-addons-tb-author-info">'; |
| 417 |
|
| 418 |
if ($show_avatar) { |
| 419 |
$avatar = get_avatar($author->ID, 192); |
| 420 |
if ($avatar) { |
| 421 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 422 |
echo '<div class="king-addons-tb-author-info__avatar">' . $avatar . '</div>'; |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
echo '<div class="king-addons-tb-author-info__name">' . esc_html($author->display_name) . '</div>'; |
| 427 |
|
| 428 |
if ($show_count) { |
| 429 |
$count = (int) count_user_posts($author->ID, 'post', true); |
| 430 |
$format = (string) ($settings['kng_post_count_format'] ?? '{count} posts'); |
| 431 |
$text = strtr($format, ['{count}' => number_format_i18n($count)]); |
| 432 |
echo '<div class="king-addons-tb-author-info__count">' . esc_html($text) . '</div>'; |
| 433 |
} |
| 434 |
|
| 435 |
if ($show_bio) { |
| 436 |
$bio = (string) get_the_author_meta('description', $author->ID); |
| 437 |
if ('' !== $bio) { |
| 438 |
echo '<div class="king-addons-tb-author-info__bio">' . esc_html($bio) . '</div>'; |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
if ($show_site) { |
| 443 |
$url = (string) $author->user_url; |
| 444 |
if ('' !== $url) { |
| 445 |
echo '<a class="king-addons-tb-author-info__site" href="' . esc_url($url) . '" rel="nofollow">' |
| 446 |
. esc_html($url) . '</a>'; |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
if ($is_pro && 'yes' === ($settings['kng_social_links'] ?? '')) { |
| 451 |
$links = []; |
| 452 |
$networks = [ |
| 453 |
'facebook' => esc_html__('Facebook', 'king-addons'), |
| 454 |
'twitter' => esc_html__('Twitter', 'king-addons'), |
| 455 |
'instagram' => esc_html__('Instagram', 'king-addons'), |
| 456 |
'linkedin' => esc_html__('LinkedIn', 'king-addons'), |
| 457 |
'youtube' => esc_html__('YouTube', 'king-addons'), |
| 458 |
]; |
| 459 |
foreach ($networks as $meta_key => $label) { |
| 460 |
$url = (string) get_user_meta($author->ID, $meta_key, true); |
| 461 |
if ('' === $url) { |
| 462 |
$url = (string) get_the_author_meta($meta_key, $author->ID); |
| 463 |
} |
| 464 |
if ('' === $url || !preg_match('#^https?://#i', $url)) { |
| 465 |
continue; |
| 466 |
} |
| 467 |
$links[] = '<a class="king-addons-tb-author-info__social-link" href="' . esc_url($url) . '" rel="nofollow">' |
| 468 |
. esc_html($label) . '</a>'; |
| 469 |
} |
| 470 |
if ($links) { |
| 471 |
echo '<div class="king-addons-tb-author-info__social">' . implode('', $links) . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built from escaped parts. |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
echo '</div>'; |
| 476 |
} |
| 477 |
} |
| 478 |
|