| 1 |
<?php |
| 2 |
/** |
| 3 |
* Theme Builder Archive Description 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 |
* Displays archive description or author bio. |
| 20 |
*/ |
| 21 |
class TB_Archive_Description 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-archive-description'; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Widget title. |
| 35 |
* |
| 36 |
* @return string |
| 37 |
*/ |
| 38 |
public function get_title(): string |
| 39 |
{ |
| 40 |
return esc_html__('TB - Archive Description', 'king-addons'); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Widget icon. |
| 45 |
* |
| 46 |
* @return string |
| 47 |
*/ |
| 48 |
public function get_icon(): string |
| 49 |
{ |
| 50 |
return 'eicon-archive-description'; |
| 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-archive-description-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-archive-description-script']; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Categories. |
| 75 |
* |
| 76 |
* @return array<int, string> |
| 77 |
*/ |
| 78 |
public function get_categories(): array |
| 79 |
{ |
| 80 |
return ['king-addons']; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Keywords. |
| 85 |
* |
| 86 |
* @return array<int, string> |
| 87 |
*/ |
| 88 |
public function get_keywords(): array |
| 89 |
{ |
| 90 |
return ['archive', 'description', 'bio', 'taxonomy', 'king-addons']; |
| 91 |
} |
| 92 |
|
| 93 |
public function get_custom_help_url() |
| 94 |
{ |
| 95 |
return 'mailto:bug@kingaddons.com?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 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_trim_words', |
| 140 |
[ |
| 141 |
'label' => $is_pro ? |
| 142 |
esc_html__('Trim Length (words)', 'king-addons') : |
| 143 |
sprintf(__('Trim Length (words) %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 144 |
'type' => Controls_Manager::NUMBER, |
| 145 |
'min' => 0, |
| 146 |
'default' => 0, |
| 147 |
'classes' => $is_pro ? '' : 'king-addons-pro-control', |
| 148 |
] |
| 149 |
); |
| 150 |
|
| 151 |
$this->add_control( |
| 152 |
'kng_strip_html', |
| 153 |
[ |
| 154 |
'label' => esc_html__('Strip HTML', 'king-addons'), |
| 155 |
'type' => Controls_Manager::SWITCHER, |
| 156 |
'return_value' => 'yes', |
| 157 |
'default' => 'yes', |
| 158 |
] |
| 159 |
); |
| 160 |
|
| 161 |
$this->add_control( |
| 162 |
'kng_empty_behavior', |
| 163 |
[ |
| 164 |
'label' => esc_html__('Empty Behavior', 'king-addons'), |
| 165 |
'type' => Controls_Manager::SELECT, |
| 166 |
'default' => 'hide', |
| 167 |
'options' => [ |
| 168 |
'hide' => esc_html__('Hide Widget', 'king-addons'), |
| 169 |
'placeholder' => $is_pro ? |
| 170 |
esc_html__('Show Placeholder', 'king-addons') : |
| 171 |
sprintf(__('Show Placeholder %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 172 |
], |
| 173 |
] |
| 174 |
); |
| 175 |
|
| 176 |
$this->add_control( |
| 177 |
'kng_placeholder_text', |
| 178 |
[ |
| 179 |
'label' => esc_html__('Placeholder Text', 'king-addons'), |
| 180 |
'type' => Controls_Manager::TEXT, |
| 181 |
'default' => esc_html__('No description available.', 'king-addons'), |
| 182 |
'condition' => [ |
| 183 |
'kng_empty_behavior' => 'placeholder', |
| 184 |
], |
| 185 |
'classes' => $is_pro ? '' : 'king-addons-pro-control', |
| 186 |
] |
| 187 |
); |
| 188 |
|
| 189 |
$this->end_controls_section(); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Style controls. |
| 194 |
* |
| 195 |
* @param bool $is_pro Whether Pro controls are enabled. |
| 196 |
* |
| 197 |
* @return void |
| 198 |
*/ |
| 199 |
protected function register_style_controls(bool $is_pro): void |
| 200 |
{ |
| 201 |
$this->start_controls_section( |
| 202 |
'kng_style_section', |
| 203 |
[ |
| 204 |
'label' => esc_html__('Style', 'king-addons'), |
| 205 |
'tab' => Controls_Manager::TAB_STYLE, |
| 206 |
] |
| 207 |
); |
| 208 |
|
| 209 |
$this->add_group_control( |
| 210 |
Group_Control_Typography::get_type(), |
| 211 |
[ |
| 212 |
'name' => 'kng_typography', |
| 213 |
'selector' => '{{WRAPPER}} .king-addons-tb-archive-description', |
| 214 |
] |
| 215 |
); |
| 216 |
|
| 217 |
$this->add_control( |
| 218 |
'kng_color', |
| 219 |
[ |
| 220 |
'label' => esc_html__('Color', 'king-addons'), |
| 221 |
'type' => Controls_Manager::COLOR, |
| 222 |
'selectors' => [ |
| 223 |
'{{WRAPPER}} .king-addons-tb-archive-description' => 'color: {{VALUE}};', |
| 224 |
], |
| 225 |
] |
| 226 |
); |
| 227 |
|
| 228 |
$this->add_responsive_control( |
| 229 |
'kng_alignment', |
| 230 |
[ |
| 231 |
'label' => esc_html__('Alignment', 'king-addons'), |
| 232 |
'type' => Controls_Manager::CHOOSE, |
| 233 |
'options' => [ |
| 234 |
'left' => [ |
| 235 |
'title' => esc_html__('Left', 'king-addons'), |
| 236 |
'icon' => 'eicon-text-align-left', |
| 237 |
], |
| 238 |
'center' => [ |
| 239 |
'title' => esc_html__('Center', 'king-addons'), |
| 240 |
'icon' => 'eicon-text-align-center', |
| 241 |
], |
| 242 |
'right' => [ |
| 243 |
'title' => esc_html__('Right', 'king-addons'), |
| 244 |
'icon' => 'eicon-text-align-right', |
| 245 |
], |
| 246 |
], |
| 247 |
'selectors' => [ |
| 248 |
'{{WRAPPER}} .king-addons-tb-archive-description' => 'text-align: {{VALUE}};', |
| 249 |
], |
| 250 |
] |
| 251 |
); |
| 252 |
|
| 253 |
$this->add_responsive_control( |
| 254 |
'kng_margin', |
| 255 |
[ |
| 256 |
'label' => esc_html__('Margin', 'king-addons'), |
| 257 |
'type' => Controls_Manager::DIMENSIONS, |
| 258 |
'size_units' => ['px', 'em', '%'], |
| 259 |
'selectors' => [ |
| 260 |
'{{WRAPPER}} .king-addons-tb-archive-description' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 261 |
], |
| 262 |
] |
| 263 |
); |
| 264 |
|
| 265 |
$this->end_controls_section(); |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Pro upsell. |
| 270 |
* |
| 271 |
* @return void |
| 272 |
*/ |
| 273 |
protected function register_pro_notice_controls(): void |
| 274 |
{ |
| 275 |
if (king_addons_freemius()->can_use_premium_code__premium_only()) { |
| 276 |
return; |
| 277 |
} |
| 278 |
|
| 279 |
Core::renderProFeaturesSection( |
| 280 |
$this, |
| 281 |
'', |
| 282 |
Controls_Manager::RAW_HTML, |
| 283 |
'tb-archive-description', |
| 284 |
[ |
| 285 |
'Trim length and placeholder handling', |
| 286 |
'Advanced formatting and typography', |
| 287 |
] |
| 288 |
); |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Render output helper. |
| 293 |
* |
| 294 |
* @param array<string, mixed> $settings Settings. |
| 295 |
* @param bool $is_pro Pro flag. |
| 296 |
* |
| 297 |
* @return void |
| 298 |
*/ |
| 299 |
protected function render_output(array $settings, bool $is_pro): void |
| 300 |
{ |
| 301 |
$description = get_the_archive_description(); |
| 302 |
if ($settings['kng_strip_html'] ?? 'yes') { |
| 303 |
$description = wp_strip_all_tags((string) $description, true); |
| 304 |
} |
| 305 |
|
| 306 |
if ($is_pro && !empty($settings['kng_trim_words'])) { |
| 307 |
$description = wp_trim_words($description, (int) $settings['kng_trim_words'], '…'); |
| 308 |
} |
| 309 |
|
| 310 |
if (empty($description)) { |
| 311 |
if ('placeholder' === ($settings['kng_empty_behavior'] ?? 'hide') && $is_pro && !empty($settings['kng_placeholder_text'])) { |
| 312 |
$description = $settings['kng_placeholder_text']; |
| 313 |
} else { |
| 314 |
return; |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
echo '<div class="king-addons-tb-archive-description">' . wp_kses_post($description) . '</div>'; |
| 319 |
} |
| 320 |
} |
| 321 |
|