PluginProbe
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder / 51.1.78
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder v51.1.78
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_Featured_Image / TB_Featured_Image.php

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

385 lines 11.0 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 public function get_custom_help_url()
97 {
98 return 'mailto:bug@kingaddons.com?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 'default' => [],
171 'classes' => $is_pro ? '' : 'king-addons-pro-control',
172 ]
173 );
174
175 $this->add_control(
176 'kng_aspect_ratio',
177 [
178 'label' => $is_pro ?
179 esc_html__('Aspect Ratio', 'king-addons') :
180 sprintf(__('Aspect Ratio %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
181 'type' => Controls_Manager::SELECT,
182 'default' => '',
183 'options' => [
184 '' => esc_html__('Natural', 'king-addons'),
185 '16-9' => '16:9',
186 '4-3' => '4:3',
187 '1-1' => '1:1',
188 '3-4' => '3:4',
189 ],
190 'classes' => $is_pro ? '' : 'king-addons-pro-control',
191 ]
192 );
193
194 $this->end_controls_section();
195 }
196
197 /**
198 * Style controls.
199 *
200 * @param bool $is_pro Whether Pro controls are enabled.
201 *
202 * @return void
203 */
204 protected function register_style_controls(bool $is_pro): void
205 {
206 $this->start_controls_section(
207 'kng_style_section',
208 [
209 'label' => esc_html__('Style', 'king-addons'),
210 'tab' => Controls_Manager::TAB_STYLE,
211 ]
212 );
213
214 $this->add_responsive_control(
215 'kng_alignment',
216 [
217 'label' => esc_html__('Alignment', 'king-addons'),
218 'type' => Controls_Manager::CHOOSE,
219 'options' => [
220 'left' => [
221 'title' => esc_html__('Left', 'king-addons'),
222 'icon' => 'eicon-text-align-left',
223 ],
224 'center' => [
225 'title' => esc_html__('Center', 'king-addons'),
226 'icon' => 'eicon-text-align-center',
227 ],
228 'right' => [
229 'title' => esc_html__('Right', 'king-addons'),
230 'icon' => 'eicon-text-align-right',
231 ],
232 ],
233 'selectors' => [
234 '{{WRAPPER}} .king-addons-tb-featured-image' => 'text-align: {{VALUE}};',
235 ],
236 ]
237 );
238
239 $this->add_control(
240 'kng_border_radius',
241 [
242 'label' => esc_html__('Border Radius', 'king-addons'),
243 'type' => Controls_Manager::DIMENSIONS,
244 'size_units' => ['px', '%'],
245 'selectors' => [
246 '{{WRAPPER}} .king-addons-tb-featured-image img' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
247 ],
248 ]
249 );
250
251 $this->add_group_control(
252 Group_Control_Border::get_type(),
253 [
254 'name' => 'kng_border',
255 'selector' => '{{WRAPPER}} .king-addons-tb-featured-image img',
256 ]
257 );
258
259 $this->add_group_control(
260 Group_Control_Box_Shadow::get_type(),
261 [
262 'name' => 'kng_shadow',
263 'selector' => '{{WRAPPER}} .king-addons-tb-featured-image img',
264 ]
265 );
266
267 $this->add_responsive_control(
268 'kng_margin',
269 [
270 'label' => esc_html__('Margin', 'king-addons'),
271 'type' => Controls_Manager::DIMENSIONS,
272 'size_units' => ['px', 'em', '%'],
273 'selectors' => [
274 '{{WRAPPER}} .king-addons-tb-featured-image' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
275 ],
276 ]
277 );
278
279 if ($is_pro) {
280 $this->add_control(
281 'kng_hover_opacity',
282 [
283 'label' => esc_html__('Hover Opacity', 'king-addons'),
284 'type' => Controls_Manager::SLIDER,
285 'size_units' => ['px'],
286 'range' => [
287 'px' => ['min' => 0.3, 'max' => 1, 'step' => 0.01],
288 ],
289 'selectors' => [
290 '{{WRAPPER}} .king-addons-tb-featured-image img:hover' => 'opacity: {{SIZE}};',
291 ],
292 ]
293 );
294 }
295
296 $this->end_controls_section();
297 }
298
299 /**
300 * Pro upsell section.
301 *
302 * @return void
303 */
304 protected function register_pro_notice_controls(): void
305 {
306 if (king_addons_freemius()->can_use_premium_code__premium_only()) {
307 return;
308 }
309
310 Core::renderProFeaturesSection(
311 $this,
312 '',
313 Controls_Manager::RAW_HTML,
314 'tb-featured-image',
315 [
316 'Fallback image when no thumbnail set',
317 'Aspect ratio and object-fit options',
318 'Advanced hover overlays and opacity',
319 ]
320 );
321 }
322
323 /**
324 * Render output helper.
325 *
326 * @param array<string, mixed> $settings Widget settings.
327 * @param bool $is_pro Whether Pro is enabled.
328 *
329 * @return void
330 */
331 protected function render_output(array $settings, bool $is_pro): void
332 {
333 $post = get_post();
334 if (!$post instanceof \WP_Post) {
335 return;
336 }
337
338 $image_id = get_post_thumbnail_id($post);
339
340 if (!$image_id && $is_pro && !empty($settings['kng_fallback_image']['id'])) {
341 $image_id = (int) $settings['kng_fallback_image']['id'];
342 }
343
344 if (!$image_id) {
345 return;
346 }
347
348 $image_html = Group_Control_Image_Size::get_attachment_image_html($settings, 'kng_image_size', $image_id);
349 if (!$image_html) {
350 return;
351 }
352
353 $link_to = $settings['kng_link_to'] ?? 'none';
354 $link_open = '';
355 $link_close = '';
356
357 if ('post' === $link_to) {
358 $permalink = get_permalink($post);
359 if ($permalink) {
360 $link_open = '<a class="king-addons-tb-featured-image__link" href="' . esc_url($permalink) . '">';
361 $link_close = '</a>';
362 }
363 } elseif ('media' === $link_to) {
364 $url = wp_get_attachment_url($image_id);
365 if ($url) {
366 $link_open = '<a class="king-addons-tb-featured-image__link" href="' . esc_url($url) . '">';
367 $link_close = '</a>';
368 }
369 }
370
371 $wrapper_classes = ['king-addons-tb-featured-image'];
372 $ratio_class = $settings['kng_aspect_ratio'] ?? '';
373 if ($is_pro && $ratio_class) {
374 $wrapper_classes[] = 'king-addons-tb-featured-image--ratio';
375 $wrapper_classes[] = 'king-addons-tb-featured-image--ratio-' . $ratio_class;
376 }
377
378 echo '<div class="' . esc_attr(implode(' ', $wrapper_classes)) . '">';
379 echo $link_open; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
380 echo $image_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
381 echo $link_close; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
382 echo '</div>';
383 }
384 }
385