| 1 |
<?php |
| 2 |
/** |
| 3 |
* Theme Builder Post Excerpt 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 current post excerpt with trimming options. |
| 20 |
*/ |
| 21 |
class TB_Post_Excerpt 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-excerpt'; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Widget title. |
| 35 |
* |
| 36 |
* @return string |
| 37 |
*/ |
| 38 |
public function get_title(): string |
| 39 |
{ |
| 40 |
return esc_html__('TB - Post Excerpt', '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-excerpt'; |
| 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-excerpt-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-excerpt-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 ['excerpt', 'summary', 'post', 'theme builder', '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 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_trim_mode', |
| 140 |
[ |
| 141 |
'label' => esc_html__('Trim By', 'king-addons'), |
| 142 |
'type' => Controls_Manager::SELECT, |
| 143 |
'options' => [ |
| 144 |
'words' => esc_html__('Words', 'king-addons'), |
| 145 |
'chars' => esc_html__('Characters', 'king-addons'), |
| 146 |
], |
| 147 |
'default' => 'words', |
| 148 |
] |
| 149 |
); |
| 150 |
|
| 151 |
$this->add_control( |
| 152 |
'kng_max_length', |
| 153 |
[ |
| 154 |
'label' => esc_html__('Max Length', 'king-addons'), |
| 155 |
'type' => Controls_Manager::NUMBER, |
| 156 |
'min' => 1, |
| 157 |
'default' => 25, |
| 158 |
] |
| 159 |
); |
| 160 |
|
| 161 |
$this->add_control( |
| 162 |
'kng_suffix', |
| 163 |
[ |
| 164 |
'label' => esc_html__('Suffix', 'king-addons'), |
| 165 |
'type' => Controls_Manager::TEXT, |
| 166 |
'dynamic' => ['active' => true], |
| 167 |
'default' => '…', |
| 168 |
] |
| 169 |
); |
| 170 |
|
| 171 |
$this->add_control( |
| 172 |
'kng_fallback_content', |
| 173 |
[ |
| 174 |
'label' => esc_html__('Fallback to Content if Empty', 'king-addons'), |
| 175 |
'type' => Controls_Manager::SWITCHER, |
| 176 |
'return_value' => 'yes', |
| 177 |
'default' => 'yes', |
| 178 |
] |
| 179 |
); |
| 180 |
|
| 181 |
$this->add_control( |
| 182 |
'kng_strip_html', |
| 183 |
[ |
| 184 |
'label' => $is_pro ? |
| 185 |
esc_html__('Strip HTML', 'king-addons') : |
| 186 |
sprintf(__('Strip HTML %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 187 |
'type' => Controls_Manager::SWITCHER, |
| 188 |
'return_value' => 'yes', |
| 189 |
'default' => '', |
| 190 |
'classes' => $is_pro ? '' : 'king-addons-pro-control', |
| 191 |
] |
| 192 |
); |
| 193 |
|
| 194 |
$this->end_controls_section(); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Style controls. |
| 199 |
* |
| 200 |
* @param bool $is_pro Whether Pro controls are enabled. |
| 201 |
* |
| 202 |
* @return void |
| 203 |
*/ |
| 204 |
protected function register_style_controls(bool $is_pro): void |
| 205 |
{ |
| 206 |
$this->start_controls_section( |
| 207 |
'kng_style_section', |
| 208 |
[ |
| 209 |
'label' => esc_html__('Style', 'king-addons'), |
| 210 |
'tab' => Controls_Manager::TAB_STYLE, |
| 211 |
] |
| 212 |
); |
| 213 |
|
| 214 |
$this->add_group_control( |
| 215 |
Group_Control_Typography::get_type(), |
| 216 |
[ |
| 217 |
'name' => 'kng_typography', |
| 218 |
'selector' => '{{WRAPPER}} .king-addons-tb-post-excerpt', |
| 219 |
] |
| 220 |
); |
| 221 |
|
| 222 |
$this->add_control( |
| 223 |
'kng_text_color', |
| 224 |
[ |
| 225 |
'label' => esc_html__('Text Color', 'king-addons'), |
| 226 |
'type' => Controls_Manager::COLOR, |
| 227 |
'selectors' => [ |
| 228 |
'{{WRAPPER}} .king-addons-tb-post-excerpt' => 'color: {{VALUE}};', |
| 229 |
], |
| 230 |
] |
| 231 |
); |
| 232 |
|
| 233 |
$this->add_responsive_control( |
| 234 |
'kng_alignment', |
| 235 |
[ |
| 236 |
'label' => esc_html__('Alignment', 'king-addons'), |
| 237 |
'type' => Controls_Manager::CHOOSE, |
| 238 |
'options' => [ |
| 239 |
'left' => [ |
| 240 |
'title' => esc_html__('Left', 'king-addons'), |
| 241 |
'icon' => 'eicon-text-align-left', |
| 242 |
], |
| 243 |
'center' => [ |
| 244 |
'title' => esc_html__('Center', 'king-addons'), |
| 245 |
'icon' => 'eicon-text-align-center', |
| 246 |
], |
| 247 |
'right' => [ |
| 248 |
'title' => esc_html__('Right', 'king-addons'), |
| 249 |
'icon' => 'eicon-text-align-right', |
| 250 |
], |
| 251 |
], |
| 252 |
'selectors' => [ |
| 253 |
'{{WRAPPER}} .king-addons-tb-post-excerpt' => 'text-align: {{VALUE}};', |
| 254 |
], |
| 255 |
] |
| 256 |
); |
| 257 |
|
| 258 |
$this->add_responsive_control( |
| 259 |
'kng_margin', |
| 260 |
[ |
| 261 |
'label' => esc_html__('Margin', 'king-addons'), |
| 262 |
'type' => Controls_Manager::DIMENSIONS, |
| 263 |
'size_units' => ['px', 'em', '%'], |
| 264 |
'selectors' => [ |
| 265 |
'{{WRAPPER}} .king-addons-tb-post-excerpt' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 266 |
], |
| 267 |
] |
| 268 |
); |
| 269 |
|
| 270 |
$this->add_responsive_control( |
| 271 |
'kng_padding', |
| 272 |
[ |
| 273 |
'label' => esc_html__('Padding', 'king-addons'), |
| 274 |
'type' => Controls_Manager::DIMENSIONS, |
| 275 |
'size_units' => ['px', 'em', '%'], |
| 276 |
'selectors' => [ |
| 277 |
'{{WRAPPER}} .king-addons-tb-post-excerpt' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 278 |
], |
| 279 |
] |
| 280 |
); |
| 281 |
|
| 282 |
$this->end_controls_section(); |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Pro upsell notice. |
| 287 |
* |
| 288 |
* @return void |
| 289 |
*/ |
| 290 |
protected function register_pro_notice_controls(): void |
| 291 |
{ |
| 292 |
if (king_addons_freemius()->can_use_premium_code__premium_only()) { |
| 293 |
return; |
| 294 |
} |
| 295 |
|
| 296 |
Core::renderProFeaturesSection( |
| 297 |
$this, |
| 298 |
'', |
| 299 |
Controls_Manager::RAW_HTML, |
| 300 |
'tb-post-excerpt', |
| 301 |
[ |
| 302 |
'Character-based trimming and HTML stripping', |
| 303 |
'Advanced sanitization options for excerpts', |
| 304 |
] |
| 305 |
); |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Render output helper. |
| 310 |
* |
| 311 |
* @param array<string, mixed> $settings Widget settings. |
| 312 |
* @param bool $is_pro Whether Pro options are enabled. |
| 313 |
* |
| 314 |
* @return void |
| 315 |
*/ |
| 316 |
protected function render_output(array $settings, bool $is_pro): void |
| 317 |
{ |
| 318 |
$post = get_post(); |
| 319 |
if (!$post instanceof \WP_Post) { |
| 320 |
return; |
| 321 |
} |
| 322 |
|
| 323 |
$trim_mode = $settings['kng_trim_mode'] ?? 'words'; |
| 324 |
$max_length = (int) ($settings['kng_max_length'] ?? 25); |
| 325 |
$suffix = (string) ($settings['kng_suffix'] ?? '…'); |
| 326 |
|
| 327 |
$excerpt = get_the_excerpt($post); |
| 328 |
if (empty($excerpt) && ('yes' === ($settings['kng_fallback_content'] ?? ''))) { |
| 329 |
$excerpt = wp_strip_all_tags(get_the_content(null, false, $post)); |
| 330 |
} |
| 331 |
|
| 332 |
if ('chars' === $trim_mode) { |
| 333 |
$text = $is_pro && ('yes' === ($settings['kng_strip_html'] ?? '')) ? wp_strip_all_tags($excerpt) : $excerpt; |
| 334 |
$excerpt = mb_strlen($text) > $max_length ? mb_substr($text, 0, $max_length) . $suffix : $text; |
| 335 |
} else { |
| 336 |
$excerpt = wp_trim_words($excerpt, $max_length, $suffix); |
| 337 |
} |
| 338 |
|
| 339 |
if ('' === trim((string) $excerpt)) { |
| 340 |
return; |
| 341 |
} |
| 342 |
|
| 343 |
printf( |
| 344 |
'<div class="king-addons-tb-post-excerpt">%s</div>', |
| 345 |
wp_kses_post($excerpt) |
| 346 |
); |
| 347 |
} |
| 348 |
} |
| 349 |
|