PluginProbe
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder / 51.1.86
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder v51.1.86
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_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.86, at includes/widgets/TB_Featured_Image/TB_Featured_Image.php

386 lines 11.1 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 'king-addons-icon king-addons-tb-featured-image';
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-theme-builder'];
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 public function get_custom_help_url()
97 {
98 return 'mailto:[email protected]?subject=Bug Report - King Addons&body=Please describe the issue';
99 }
100
101 /**
102 * Register controls.
103 *
104 * @return void
105 */
106 public function register_controls(): void
107 {
108 $this->register_content_controls(false);
109 $this->register_style_controls(false);
110 $this->register_pro_notice_controls();
111 }
112
113 /**
114 * Render widget output.
115 *
116 * @return void
117 */
118 public function render(): void
119 {
120 $settings = $this->get_settings_for_display();
121 $this->render_output($settings, false);
122 }
123
124 /**
125 * Content controls.
126 *
127 * @param bool $is_pro Whether Pro controls are enabled.
128 *
129 * @return void
130 */
131 protected function register_content_controls(bool $is_pro): void
132 {
133 $this->start_controls_section(
134 'kng_content_section',
135 [
136 'label' => KING_ADDONS_ELEMENTOR_ICON . esc_html__('Content', 'king-addons'),
137 'tab' => Controls_Manager::TAB_CONTENT,
138 ]
139 );
140
141 $this->add_group_control(
142 Group_Control_Image_Size::get_type(),
143 [
144 'name' => 'kng_image_size',
145 'default' => 'large',
146 ]
147 );
148
149 $this->add_control(
150 'kng_link_to',
151 [
152 'label' => esc_html__('Link', 'king-addons'),
153 'type' => Controls_Manager::SELECT,
154 'default' => 'none',
155 'options' => [
156 'none' => esc_html__('None', 'king-addons'),
157 'post' => esc_html__('Post', 'king-addons'),
158 'media' => esc_html__('Media File', 'king-addons'),
159 ],
160 ]
161 );
162
163 $this->add_control(
164 'kng_fallback_image',
165 [
166 'label' => $is_pro ?
167 esc_html__('Fallback Image', 'king-addons') :
168 sprintf(__('Fallback Image %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
169 'type' => Controls_Manager::MEDIA,
170 'dynamic' => ['active' => true],
171 'default' => [],
172 'classes' => $is_pro ? '' : 'king-addons-pro-control',
173 ]
174 );
175
176 $this->add_control(
177 'kng_aspect_ratio',
178 [
179 'label' => $is_pro ?
180 esc_html__('Aspect Ratio', 'king-addons') :
181 sprintf(__('Aspect Ratio %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
182 'type' => Controls_Manager::SELECT,
183 'default' => '',
184 'options' => [
185 '' => esc_html__('Natural', 'king-addons'),
186 '16-9' => '16:9',
187 '4-3' => '4:3',
188 '1-1' => '1:1',
189 '3-4' => '3:4',
190 ],
191 'classes' => $is_pro ? '' : 'king-addons-pro-control',
192 ]
193 );
194
195 $this->end_controls_section();
196 }
197
198 /**
199 * Style controls.
200 *
201 * @param bool $is_pro Whether Pro controls are enabled.
202 *
203 * @return void
204 */
205 protected function register_style_controls(bool $is_pro): void
206 {
207 $this->start_controls_section(
208 'kng_style_section',
209 [
210 'label' => esc_html__('Style', 'king-addons'),
211 'tab' => Controls_Manager::TAB_STYLE,
212 ]
213 );
214
215 $this->add_responsive_control(
216 'kng_alignment',
217 [
218 'label' => esc_html__('Alignment', 'king-addons'),
219 'type' => Controls_Manager::CHOOSE,
220 'options' => [
221 'left' => [
222 'title' => esc_html__('Left', 'king-addons'),
223 'icon' => 'eicon-text-align-left',
224 ],
225 'center' => [
226 'title' => esc_html__('Center', 'king-addons'),
227 'icon' => 'eicon-text-align-center',
228 ],
229 'right' => [
230 'title' => esc_html__('Right', 'king-addons'),
231 'icon' => 'eicon-text-align-right',
232 ],
233 ],
234 'selectors' => [
235 '{{WRAPPER}} .king-addons-tb-featured-image' => 'text-align: {{VALUE}};',
236 ],
237 ]
238 );
239
240 $this->add_control(
241 'kng_border_radius',
242 [
243 'label' => esc_html__('Border Radius', 'king-addons'),
244 'type' => Controls_Manager::DIMENSIONS,
245 'size_units' => ['px', '%'],
246 'selectors' => [
247 '{{WRAPPER}} .king-addons-tb-featured-image img' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
248 ],
249 ]
250 );
251
252 $this->add_group_control(
253 Group_Control_Border::get_type(),
254 [
255 'name' => 'kng_border',
256 'selector' => '{{WRAPPER}} .king-addons-tb-featured-image img',
257 ]
258 );
259
260 $this->add_group_control(
261 Group_Control_Box_Shadow::get_type(),
262 [
263 'name' => 'kng_shadow',
264 'selector' => '{{WRAPPER}} .king-addons-tb-featured-image img',
265 ]
266 );
267
268 $this->add_responsive_control(
269 'kng_margin',
270 [
271 'label' => esc_html__('Margin', 'king-addons'),
272 'type' => Controls_Manager::DIMENSIONS,
273 'size_units' => ['px', 'em', '%'],
274 'selectors' => [
275 '{{WRAPPER}} .king-addons-tb-featured-image' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
276 ],
277 ]
278 );
279
280 if ($is_pro) {
281 $this->add_control(
282 'kng_hover_opacity',
283 [
284 'label' => esc_html__('Hover Opacity', 'king-addons'),
285 'type' => Controls_Manager::SLIDER,
286 'size_units' => ['px'],
287 'range' => [
288 'px' => ['min' => 0.3, 'max' => 1, 'step' => 0.01],
289 ],
290 'selectors' => [
291 '{{WRAPPER}} .king-addons-tb-featured-image img:hover' => 'opacity: {{SIZE}};',
292 ],
293 ]
294 );
295 }
296
297 $this->end_controls_section();
298 }
299
300 /**
301 * Pro upsell section.
302 *
303 * @return void
304 */
305 protected function register_pro_notice_controls(): void
306 {
307 if (king_addons_freemius()->can_use_premium_code__premium_only()) {
308 return;
309 }
310
311 Core::renderProFeaturesSection(
312 $this,
313 '',
314 Controls_Manager::RAW_HTML,
315 'tb-featured-image',
316 [
317 'Fallback image when no thumbnail set',
318 'Aspect ratio and object-fit options',
319 'Advanced hover overlays and opacity',
320 ]
321 );
322 }
323
324 /**
325 * Render output helper.
326 *
327 * @param array<string, mixed> $settings Widget settings.
328 * @param bool $is_pro Whether Pro is enabled.
329 *
330 * @return void
331 */
332 protected function render_output(array $settings, bool $is_pro): void
333 {
334 $post = get_post();
335 if (!$post instanceof \WP_Post) {
336 return;
337 }
338
339 $image_id = get_post_thumbnail_id($post);
340
341 if (!$image_id && $is_pro && !empty($settings['kng_fallback_image']['id'])) {
342 $image_id = (int) $settings['kng_fallback_image']['id'];
343 }
344
345 if (!$image_id) {
346 return;
347 }
348
349 $image_html = Core::getAttachmentImageHTML($settings, 'kng_image_size', (int) $image_id);
350 if (!$image_html) {
351 return;
352 }
353
354 $link_to = $settings['kng_link_to'] ?? 'none';
355 $link_open = '';
356 $link_close = '';
357
358 if ('post' === $link_to) {
359 $permalink = get_permalink($post);
360 if ($permalink) {
361 $link_open = '<a class="king-addons-tb-featured-image__link" href="' . esc_url($permalink) . '">';
362 $link_close = '</a>';
363 }
364 } elseif ('media' === $link_to) {
365 $url = wp_get_attachment_url($image_id);
366 if ($url) {
367 $link_open = '<a class="king-addons-tb-featured-image__link" href="' . esc_url($url) . '">';
368 $link_close = '</a>';
369 }
370 }
371
372 $wrapper_classes = ['king-addons-tb-featured-image'];
373 $ratio_class = $settings['kng_aspect_ratio'] ?? '';
374 if ($is_pro && $ratio_class) {
375 $wrapper_classes[] = 'king-addons-tb-featured-image--ratio';
376 $wrapper_classes[] = 'king-addons-tb-featured-image--ratio-' . $ratio_class;
377 }
378
379 echo '<div class="' . esc_attr(implode(' ', $wrapper_classes)) . '">';
380 echo $link_open; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
381 echo $image_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
382 echo $link_close; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
383 echo '</div>';
384 }
385 }
386