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_Archive_Posts / TB_Archive_Posts.php

TB_Archive_Posts.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_Archive_Posts/TB_Archive_Posts.php

392 lines 10.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Theme Builder Archive Posts 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 * Renders the current archive/search main query.
20 */
21 class TB_Archive_Posts 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-posts';
31 }
32
33 /**
34 * Widget title.
35 *
36 * @return string
37 */
38 public function get_title(): string
39 {
40 return esc_html__('TB - Archive Posts', 'king-addons');
41 }
42
43 /**
44 * Widget icon.
45 *
46 * @return string
47 */
48 public function get_icon(): string
49 {
50 return 'eicon-post-list';
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-posts-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-posts-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', 'loop', 'posts', 'search', '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 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_layout_section',
127 [
128 'label' => KING_ADDONS_ELEMENTOR_ICON . esc_html__('Layout', 'king-addons'),
129 'tab' => Controls_Manager::TAB_CONTENT,
130 ]
131 );
132
133 $this->add_control(
134 'kng_layout',
135 [
136 'label' => esc_html__('Layout', 'king-addons'),
137 'type' => Controls_Manager::SELECT,
138 'default' => 'grid',
139 'options' => [
140 'grid' => esc_html__('Grid', 'king-addons'),
141 'list' => esc_html__('List', 'king-addons'),
142 'cards' => $is_pro ? esc_html__('Cards (Pro)', 'king-addons') : sprintf(__('Cards %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
143 ],
144 ]
145 );
146
147 $this->add_responsive_control(
148 'kng_columns',
149 [
150 'label' => esc_html__('Columns', 'king-addons'),
151 'type' => Controls_Manager::SLIDER,
152 'size_units' => ['px'],
153 'range' => [
154 'px' => ['min' => 1, 'max' => 4, 'step' => 1],
155 ],
156 'default' => [
157 'size' => 3,
158 'unit' => 'px',
159 ],
160 ]
161 );
162
163 $this->add_responsive_control(
164 'kng_gap',
165 [
166 'label' => esc_html__('Gap', 'king-addons'),
167 'type' => Controls_Manager::SLIDER,
168 'size_units' => ['px'],
169 'range' => [
170 'px' => ['min' => 0, 'max' => 60],
171 ],
172 'default' => [
173 'size' => 16,
174 'unit' => 'px',
175 ],
176 'selectors' => [
177 '{{WRAPPER}} .king-addons-tb-archive-posts__grid' => 'gap: {{SIZE}}{{UNIT}};',
178 ],
179 ]
180 );
181
182 $this->add_control(
183 'kng_show_image',
184 [
185 'label' => esc_html__('Show Featured Image', 'king-addons'),
186 'type' => Controls_Manager::SWITCHER,
187 'return_value' => 'yes',
188 'default' => 'yes',
189 ]
190 );
191
192 $this->add_control(
193 'kng_show_title',
194 [
195 'label' => esc_html__('Show Title', 'king-addons'),
196 'type' => Controls_Manager::SWITCHER,
197 'return_value' => 'yes',
198 'default' => 'yes',
199 ]
200 );
201
202 $this->add_control(
203 'kng_show_meta',
204 [
205 'label' => esc_html__('Show Meta', 'king-addons'),
206 'type' => Controls_Manager::SWITCHER,
207 'return_value' => 'yes',
208 'default' => 'yes',
209 ]
210 );
211
212 $this->add_control(
213 'kng_show_excerpt',
214 [
215 'label' => esc_html__('Show Excerpt', 'king-addons'),
216 'type' => Controls_Manager::SWITCHER,
217 'return_value' => 'yes',
218 'default' => '',
219 ]
220 );
221
222 $this->add_control(
223 'kng_show_button',
224 [
225 'label' => esc_html__('Show Read More', 'king-addons'),
226 'type' => Controls_Manager::SWITCHER,
227 'return_value' => 'yes',
228 'default' => '',
229 ]
230 );
231
232 $this->end_controls_section();
233 }
234
235 /**
236 * Style controls.
237 *
238 * @param bool $is_pro Whether Pro controls are enabled.
239 *
240 * @return void
241 */
242 protected function register_style_controls(bool $is_pro): void
243 {
244 $this->start_controls_section(
245 'kng_style_section',
246 [
247 'label' => esc_html__('Style', 'king-addons'),
248 'tab' => Controls_Manager::TAB_STYLE,
249 ]
250 );
251
252 $this->add_group_control(
253 Group_Control_Typography::get_type(),
254 [
255 'name' => 'kng_title_typography',
256 'label' => esc_html__('Title Typography', 'king-addons'),
257 'selector' => '{{WRAPPER}} .king-addons-tb-archive-posts__title',
258 ]
259 );
260
261 $this->add_group_control(
262 Group_Control_Typography::get_type(),
263 [
264 'name' => 'kng_meta_typography',
265 'label' => esc_html__('Meta Typography', 'king-addons'),
266 'selector' => '{{WRAPPER}} .king-addons-tb-archive-posts__meta',
267 ]
268 );
269
270 $this->add_control(
271 'kng_title_color',
272 [
273 'label' => esc_html__('Title Color', 'king-addons'),
274 'type' => Controls_Manager::COLOR,
275 'selectors' => [
276 '{{WRAPPER}} .king-addons-tb-archive-posts__title a' => 'color: {{VALUE}};',
277 ],
278 ]
279 );
280
281 $this->add_control(
282 'kng_meta_color',
283 [
284 'label' => esc_html__('Meta Color', 'king-addons'),
285 'type' => Controls_Manager::COLOR,
286 'selectors' => [
287 '{{WRAPPER}} .king-addons-tb-archive-posts__meta' => 'color: {{VALUE}};',
288 ],
289 ]
290 );
291
292 $this->end_controls_section();
293 }
294
295 /**
296 * Pro upsell.
297 *
298 * @return void
299 */
300 protected function register_pro_notice_controls(): void
301 {
302 if (king_addons_freemius()->can_use_premium_code__premium_only()) {
303 return;
304 }
305
306 Core::renderProFeaturesSection(
307 $this,
308 '',
309 Controls_Manager::RAW_HTML,
310 'tb-archive-posts',
311 [
312 'Card presets and animations',
313 'Built-in pagination and skins',
314 ]
315 );
316 }
317
318 /**
319 * Render output helper.
320 *
321 * @param array<string, mixed> $settings Settings.
322 * @param bool $is_pro Pro flag.
323 *
324 * @return void
325 */
326 protected function render_output(array $settings, bool $is_pro): void
327 {
328 global $wp_query;
329 if (!$wp_query instanceof \WP_Query || !$wp_query->have_posts()) {
330 return;
331 }
332
333 $wp_query->rewind_posts();
334
335 $columns = isset($settings['kng_columns']['size']) ? max(1, (int) $settings['kng_columns']['size']) : 3;
336 $layout = $settings['kng_layout'] ?? 'grid';
337
338 $classes = [
339 'king-addons-tb-archive-posts',
340 'king-addons-tb-archive-posts--' . $layout,
341 ];
342
343 echo '<div class="' . esc_attr(implode(' ', $classes)) . '" style="--kng-archive-columns:' . esc_attr((string) $columns) . '">';
344 echo '<div class="king-addons-tb-archive-posts__grid">';
345
346 while ($wp_query->have_posts()) {
347 $wp_query->the_post();
348 $this->render_card($settings);
349 }
350
351 echo '</div></div>';
352
353 wp_reset_postdata();
354 }
355
356 /**
357 * Render single card.
358 *
359 * @param array<string, mixed> $settings Settings.
360 *
361 * @return void
362 */
363 protected function render_card(array $settings): void
364 {
365 echo '<article class="king-addons-tb-archive-posts__item">';
366
367 if ('yes' === ($settings['kng_show_image'] ?? 'yes') && has_post_thumbnail()) {
368 echo '<a class="king-addons-tb-archive-posts__thumb" href="' . esc_url(get_permalink()) . '">';
369 the_post_thumbnail('medium');
370 echo '</a>';
371 }
372
373 if ('yes' === ($settings['kng_show_title'] ?? 'yes')) {
374 echo '<h3 class="king-addons-tb-archive-posts__title"><a href="' . esc_url(get_permalink()) . '">' . esc_html(get_the_title()) . '</a></h3>';
375 }
376
377 if ('yes' === ($settings['kng_show_meta'] ?? 'yes')) {
378 echo '<div class="king-addons-tb-archive-posts__meta">' . esc_html(get_the_date()) . '</div>';
379 }
380
381 if ('yes' === ($settings['kng_show_excerpt'] ?? '')) {
382 echo '<div class="king-addons-tb-archive-posts__excerpt">' . esc_html(wp_trim_words(get_the_excerpt(), 18, '')) . '</div>';
383 }
384
385 if ('yes' === ($settings['kng_show_button'] ?? '')) {
386 echo '<a class="king-addons-tb-archive-posts__button" href="' . esc_url(get_permalink()) . '">' . esc_html__('Read more', 'king-addons') . '</a>';
387 }
388
389 echo '</article>';
390 }
391 }
392