PluginProbe
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder / 51.1.63
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder v51.1.63
51.1.86 51.1.84 51.1.85 51.1.83 51.1.82 51.1.81 51.1.79 51.1.78 51.1.77 51.1.76 51.1.74 51.1.75 51.1.65 51.1.64 51.1.63 trunk 51.1.14 51.1.2 51.1.35 51.1.36 51.1.37 51.1.38 51.1.39 51.1.44 51.1.45 All 40 releases
king-addons / includes / widgets / TB_Post_Excerpt / TB_Post_Excerpt.php

TB_Post_Excerpt.php in King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder 51.1.63, at includes/widgets/TB_Post_Excerpt/TB_Post_Excerpt.php

348 lines 9.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 'eicon-editor-quote';
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'];
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:[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_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 'default' => '…',
167 ]
168 );
169
170 $this->add_control(
171 'kng_fallback_content',
172 [
173 'label' => esc_html__('Fallback to Content if Empty', 'king-addons'),
174 'type' => Controls_Manager::SWITCHER,
175 'return_value' => 'yes',
176 'default' => 'yes',
177 ]
178 );
179
180 $this->add_control(
181 'kng_strip_html',
182 [
183 'label' => $is_pro ?
184 esc_html__('Strip HTML', 'king-addons') :
185 sprintf(__('Strip HTML %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
186 'type' => Controls_Manager::SWITCHER,
187 'return_value' => 'yes',
188 'default' => '',
189 'classes' => $is_pro ? '' : 'king-addons-pro-control',
190 ]
191 );
192
193 $this->end_controls_section();
194 }
195
196 /**
197 * Style controls.
198 *
199 * @param bool $is_pro Whether Pro controls are enabled.
200 *
201 * @return void
202 */
203 protected function register_style_controls(bool $is_pro): void
204 {
205 $this->start_controls_section(
206 'kng_style_section',
207 [
208 'label' => esc_html__('Style', 'king-addons'),
209 'tab' => Controls_Manager::TAB_STYLE,
210 ]
211 );
212
213 $this->add_group_control(
214 Group_Control_Typography::get_type(),
215 [
216 'name' => 'kng_typography',
217 'selector' => '{{WRAPPER}} .king-addons-tb-post-excerpt',
218 ]
219 );
220
221 $this->add_control(
222 'kng_text_color',
223 [
224 'label' => esc_html__('Text Color', 'king-addons'),
225 'type' => Controls_Manager::COLOR,
226 'selectors' => [
227 '{{WRAPPER}} .king-addons-tb-post-excerpt' => 'color: {{VALUE}};',
228 ],
229 ]
230 );
231
232 $this->add_responsive_control(
233 'kng_alignment',
234 [
235 'label' => esc_html__('Alignment', 'king-addons'),
236 'type' => Controls_Manager::CHOOSE,
237 'options' => [
238 'left' => [
239 'title' => esc_html__('Left', 'king-addons'),
240 'icon' => 'eicon-text-align-left',
241 ],
242 'center' => [
243 'title' => esc_html__('Center', 'king-addons'),
244 'icon' => 'eicon-text-align-center',
245 ],
246 'right' => [
247 'title' => esc_html__('Right', 'king-addons'),
248 'icon' => 'eicon-text-align-right',
249 ],
250 ],
251 'selectors' => [
252 '{{WRAPPER}} .king-addons-tb-post-excerpt' => 'text-align: {{VALUE}};',
253 ],
254 ]
255 );
256
257 $this->add_responsive_control(
258 'kng_margin',
259 [
260 'label' => esc_html__('Margin', 'king-addons'),
261 'type' => Controls_Manager::DIMENSIONS,
262 'size_units' => ['px', 'em', '%'],
263 'selectors' => [
264 '{{WRAPPER}} .king-addons-tb-post-excerpt' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
265 ],
266 ]
267 );
268
269 $this->add_responsive_control(
270 'kng_padding',
271 [
272 'label' => esc_html__('Padding', 'king-addons'),
273 'type' => Controls_Manager::DIMENSIONS,
274 'size_units' => ['px', 'em', '%'],
275 'selectors' => [
276 '{{WRAPPER}} .king-addons-tb-post-excerpt' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
277 ],
278 ]
279 );
280
281 $this->end_controls_section();
282 }
283
284 /**
285 * Pro upsell notice.
286 *
287 * @return void
288 */
289 protected function register_pro_notice_controls(): void
290 {
291 if (king_addons_freemius()->can_use_premium_code__premium_only()) {
292 return;
293 }
294
295 Core::renderProFeaturesSection(
296 $this,
297 '',
298 Controls_Manager::RAW_HTML,
299 'tb-post-excerpt',
300 [
301 'Character-based trimming and HTML stripping',
302 'Advanced sanitization options for excerpts',
303 ]
304 );
305 }
306
307 /**
308 * Render output helper.
309 *
310 * @param array<string, mixed> $settings Widget settings.
311 * @param bool $is_pro Whether Pro options are enabled.
312 *
313 * @return void
314 */
315 protected function render_output(array $settings, bool $is_pro): void
316 {
317 $post = get_post();
318 if (!$post instanceof \WP_Post) {
319 return;
320 }
321
322 $trim_mode = $settings['kng_trim_mode'] ?? 'words';
323 $max_length = (int) ($settings['kng_max_length'] ?? 25);
324 $suffix = (string) ($settings['kng_suffix'] ?? '…');
325
326 $excerpt = get_the_excerpt($post);
327 if (empty($excerpt) && ('yes' === ($settings['kng_fallback_content'] ?? ''))) {
328 $excerpt = wp_strip_all_tags(get_the_content(null, false, $post));
329 }
330
331 if ('chars' === $trim_mode) {
332 $text = $is_pro && ('yes' === ($settings['kng_strip_html'] ?? '')) ? wp_strip_all_tags($excerpt) : $excerpt;
333 $excerpt = mb_strlen($text) > $max_length ? mb_substr($text, 0, $max_length) . $suffix : $text;
334 } else {
335 $excerpt = wp_trim_words($excerpt, $max_length, $suffix);
336 }
337
338 if ('' === trim((string) $excerpt)) {
339 return;
340 }
341
342 printf(
343 '<div class="king-addons-tb-post-excerpt">%s</div>',
344 wp_kses_post($excerpt)
345 );
346 }
347 }
348