PluginProbe
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder / 51.1.49
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder v51.1.49
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 51.1.46 51.1.47 51.1.49 All 37 releases
king-addons / includes / widgets / TB_Post_Excerpt / TB_Post_Excerpt.php

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

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