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

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

380 lines 10.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Theme Builder Featured Image widget (Free).
4 *
5 * @package King_Addons
6 */
7
8 namespace King_Addons;
9
10 use Elementor\Controls_Manager;
11 use Elementor\Group_Control_Border;
12 use Elementor\Group_Control_Box_Shadow;
13 use Elementor\Group_Control_Image_Size;
14 use Elementor\Group_Control_Typography;
15 use Elementor\Widget_Base;
16
17 if (!defined('ABSPATH')) {
18 exit;
19 }
20
21 /**
22 * Displays the current post featured image.
23 */
24 class TB_Featured_Image extends Widget_Base
25 {
26 /**
27 * Widget slug.
28 *
29 * @return string
30 */
31 public function get_name(): string
32 {
33 return 'king-addons-tb-featured-image';
34 }
35
36 /**
37 * Widget title.
38 *
39 * @return string
40 */
41 public function get_title(): string
42 {
43 return esc_html__('TB - Featured Image', 'king-addons');
44 }
45
46 /**
47 * Widget icon.
48 *
49 * @return string
50 */
51 public function get_icon(): string
52 {
53 return 'eicon-post';
54 }
55
56 /**
57 * Style dependencies.
58 *
59 * @return array<int, string>
60 */
61 public function get_style_depends(): array
62 {
63 return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-tb-featured-image-style'];
64 }
65
66 /**
67 * Script dependencies.
68 *
69 * @return array<int, string>
70 */
71 public function get_script_depends(): array
72 {
73 return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-tb-featured-image-script'];
74 }
75
76 /**
77 * Categories.
78 *
79 * @return array<int, string>
80 */
81 public function get_categories(): array
82 {
83 return ['king-addons'];
84 }
85
86 /**
87 * Keywords.
88 *
89 * @return array<int, string>
90 */
91 public function get_keywords(): array
92 {
93 return ['featured image', 'post image', 'thumbnail', 'theme builder', 'king-addons'];
94 }
95
96 /**
97 * Register controls.
98 *
99 * @return void
100 */
101 public function register_controls(): void
102 {
103 $this->register_content_controls(false);
104 $this->register_style_controls(false);
105 $this->register_pro_notice_controls();
106 }
107
108 /**
109 * Render widget output.
110 *
111 * @return void
112 */
113 public function render(): void
114 {
115 $settings = $this->get_settings_for_display();
116 $this->render_output($settings, false);
117 }
118
119 /**
120 * Content controls.
121 *
122 * @param bool $is_pro Whether Pro controls are enabled.
123 *
124 * @return void
125 */
126 protected function register_content_controls(bool $is_pro): void
127 {
128 $this->start_controls_section(
129 'kng_content_section',
130 [
131 'label' => KING_ADDONS_ELEMENTOR_ICON . esc_html__('Content', 'king-addons'),
132 'tab' => Controls_Manager::TAB_CONTENT,
133 ]
134 );
135
136 $this->add_group_control(
137 Group_Control_Image_Size::get_type(),
138 [
139 'name' => 'kng_image_size',
140 'default' => 'large',
141 ]
142 );
143
144 $this->add_control(
145 'kng_link_to',
146 [
147 'label' => esc_html__('Link', 'king-addons'),
148 'type' => Controls_Manager::SELECT,
149 'default' => 'none',
150 'options' => [
151 'none' => esc_html__('None', 'king-addons'),
152 'post' => esc_html__('Post', 'king-addons'),
153 'media' => esc_html__('Media File', 'king-addons'),
154 ],
155 ]
156 );
157
158 $this->add_control(
159 'kng_fallback_image',
160 [
161 'label' => $is_pro ?
162 esc_html__('Fallback Image', 'king-addons') :
163 sprintf(__('Fallback Image %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
164 'type' => Controls_Manager::MEDIA,
165 'default' => [],
166 'classes' => $is_pro ? '' : 'king-addons-pro-control',
167 ]
168 );
169
170 $this->add_control(
171 'kng_aspect_ratio',
172 [
173 'label' => $is_pro ?
174 esc_html__('Aspect Ratio', 'king-addons') :
175 sprintf(__('Aspect Ratio %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
176 'type' => Controls_Manager::SELECT,
177 'default' => '',
178 'options' => [
179 '' => esc_html__('Natural', 'king-addons'),
180 '16-9' => '16:9',
181 '4-3' => '4:3',
182 '1-1' => '1:1',
183 '3-4' => '3:4',
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_responsive_control(
210 'kng_alignment',
211 [
212 'label' => esc_html__('Alignment', 'king-addons'),
213 'type' => Controls_Manager::CHOOSE,
214 'options' => [
215 'left' => [
216 'title' => esc_html__('Left', 'king-addons'),
217 'icon' => 'eicon-text-align-left',
218 ],
219 'center' => [
220 'title' => esc_html__('Center', 'king-addons'),
221 'icon' => 'eicon-text-align-center',
222 ],
223 'right' => [
224 'title' => esc_html__('Right', 'king-addons'),
225 'icon' => 'eicon-text-align-right',
226 ],
227 ],
228 'selectors' => [
229 '{{WRAPPER}} .king-addons-tb-featured-image' => 'text-align: {{VALUE}};',
230 ],
231 ]
232 );
233
234 $this->add_control(
235 'kng_border_radius',
236 [
237 'label' => esc_html__('Border Radius', 'king-addons'),
238 'type' => Controls_Manager::DIMENSIONS,
239 'size_units' => ['px', '%'],
240 'selectors' => [
241 '{{WRAPPER}} .king-addons-tb-featured-image img' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
242 ],
243 ]
244 );
245
246 $this->add_group_control(
247 Group_Control_Border::get_type(),
248 [
249 'name' => 'kng_border',
250 'selector' => '{{WRAPPER}} .king-addons-tb-featured-image img',
251 ]
252 );
253
254 $this->add_group_control(
255 Group_Control_Box_Shadow::get_type(),
256 [
257 'name' => 'kng_shadow',
258 'selector' => '{{WRAPPER}} .king-addons-tb-featured-image img',
259 ]
260 );
261
262 $this->add_responsive_control(
263 'kng_margin',
264 [
265 'label' => esc_html__('Margin', 'king-addons'),
266 'type' => Controls_Manager::DIMENSIONS,
267 'size_units' => ['px', 'em', '%'],
268 'selectors' => [
269 '{{WRAPPER}} .king-addons-tb-featured-image' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
270 ],
271 ]
272 );
273
274 if ($is_pro) {
275 $this->add_control(
276 'kng_hover_opacity',
277 [
278 'label' => esc_html__('Hover Opacity', 'king-addons'),
279 'type' => Controls_Manager::SLIDER,
280 'size_units' => ['px'],
281 'range' => [
282 'px' => ['min' => 0.3, 'max' => 1, 'step' => 0.01],
283 ],
284 'selectors' => [
285 '{{WRAPPER}} .king-addons-tb-featured-image img:hover' => 'opacity: {{SIZE}};',
286 ],
287 ]
288 );
289 }
290
291 $this->end_controls_section();
292 }
293
294 /**
295 * Pro upsell section.
296 *
297 * @return void
298 */
299 protected function register_pro_notice_controls(): void
300 {
301 if (king_addons_freemius()->can_use_premium_code__premium_only()) {
302 return;
303 }
304
305 Core::renderProFeaturesSection(
306 $this,
307 '',
308 Controls_Manager::RAW_HTML,
309 'tb-featured-image',
310 [
311 'Fallback image when no thumbnail set',
312 'Aspect ratio and object-fit options',
313 'Advanced hover overlays and opacity',
314 ]
315 );
316 }
317
318 /**
319 * Render output helper.
320 *
321 * @param array<string, mixed> $settings Widget settings.
322 * @param bool $is_pro Whether Pro is enabled.
323 *
324 * @return void
325 */
326 protected function render_output(array $settings, bool $is_pro): void
327 {
328 $post = get_post();
329 if (!$post instanceof \WP_Post) {
330 return;
331 }
332
333 $image_id = get_post_thumbnail_id($post);
334
335 if (!$image_id && $is_pro && !empty($settings['kng_fallback_image']['id'])) {
336 $image_id = (int) $settings['kng_fallback_image']['id'];
337 }
338
339 if (!$image_id) {
340 return;
341 }
342
343 $image_html = Group_Control_Image_Size::get_attachment_image_html($settings, 'kng_image_size', $image_id);
344 if (!$image_html) {
345 return;
346 }
347
348 $link_to = $settings['kng_link_to'] ?? 'none';
349 $link_open = '';
350 $link_close = '';
351
352 if ('post' === $link_to) {
353 $permalink = get_permalink($post);
354 if ($permalink) {
355 $link_open = '<a class="king-addons-tb-featured-image__link" href="' . esc_url($permalink) . '">';
356 $link_close = '</a>';
357 }
358 } elseif ('media' === $link_to) {
359 $url = wp_get_attachment_url($image_id);
360 if ($url) {
361 $link_open = '<a class="king-addons-tb-featured-image__link" href="' . esc_url($url) . '">';
362 $link_close = '</a>';
363 }
364 }
365
366 $wrapper_classes = ['king-addons-tb-featured-image'];
367 $ratio_class = $settings['kng_aspect_ratio'] ?? '';
368 if ($is_pro && $ratio_class) {
369 $wrapper_classes[] = 'king-addons-tb-featured-image--ratio';
370 $wrapper_classes[] = 'king-addons-tb-featured-image--ratio-' . $ratio_class;
371 }
372
373 echo '<div class="' . esc_attr(implode(' ', $wrapper_classes)) . '">';
374 echo $link_open; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
375 echo $image_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
376 echo $link_close; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
377 echo '</div>';
378 }
379 }
380