| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Builder\Widgets; |
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; // Exit if accessed directly. |
| 6 |
} |
| 7 |
|
| 8 |
use Elementor\Controls_Manager; |
| 9 |
use Elementor\Group_Control_Border; |
| 10 |
use Elementor\Group_Control_Box_Shadow; |
| 11 |
use Elementor\Group_Control_Image_Size; |
| 12 |
use Elementor\Plugin; |
| 13 |
use Elementor\Widget_Base; |
| 14 |
|
| 15 |
class Featured_Image extends Widget_Base { |
| 16 |
|
| 17 |
public function get_name() { |
| 18 |
return 'tl-post-fatured-image'; |
| 19 |
} |
| 20 |
|
| 21 |
public function get_title() { |
| 22 |
return esc_html__( 'Featured Image', 'templately' ); |
| 23 |
} |
| 24 |
|
| 25 |
public function get_icon() { |
| 26 |
return 'eicon-featured-image templately-widget-icon'; |
| 27 |
} |
| 28 |
|
| 29 |
public function get_categories() { |
| 30 |
return [ 'theme-elements-single' ]; |
| 31 |
} |
| 32 |
|
| 33 |
public function get_keywords() { |
| 34 |
return [ 'featured image', 'image', 'featured' ]; |
| 35 |
} |
| 36 |
|
| 37 |
protected function register_controls() { |
| 38 |
$this->start_controls_section( |
| 39 |
'section_title', |
| 40 |
[ |
| 41 |
'label' => esc_html__( 'Featured Image', 'templately' ), |
| 42 |
] |
| 43 |
); |
| 44 |
|
| 45 |
$this->add_group_control( |
| 46 |
Group_Control_Image_Size::get_type(), |
| 47 |
[ |
| 48 |
'name' => 'image', |
| 49 |
'default' => 'full', |
| 50 |
'exclude' => [ 'custom' ] |
| 51 |
] |
| 52 |
); |
| 53 |
|
| 54 |
$this->end_controls_section(); |
| 55 |
|
| 56 |
$this->start_controls_section( |
| 57 |
'section_image_style', |
| 58 |
[ |
| 59 |
'label' => esc_html__( 'Image', 'templately' ), |
| 60 |
'tab' => Controls_Manager::TAB_STYLE, |
| 61 |
] |
| 62 |
); |
| 63 |
|
| 64 |
$this->add_responsive_control( |
| 65 |
'align', |
| 66 |
[ |
| 67 |
'label' => esc_html__( 'Alignment', 'templately' ), |
| 68 |
'type' => Controls_Manager::CHOOSE, |
| 69 |
'options' => [ |
| 70 |
'left' => [ |
| 71 |
'title' => esc_html__( 'Left', 'templately' ), |
| 72 |
'icon' => 'eicon-text-align-left', |
| 73 |
], |
| 74 |
'center' => [ |
| 75 |
'title' => esc_html__( 'Center', 'templately' ), |
| 76 |
'icon' => 'eicon-text-align-center', |
| 77 |
], |
| 78 |
'right' => [ |
| 79 |
'title' => esc_html__( 'Right', 'templately' ), |
| 80 |
'icon' => 'eicon-text-align-right', |
| 81 |
], |
| 82 |
], |
| 83 |
'selectors' => [ |
| 84 |
'{{WRAPPER}}' => 'text-align: {{VALUE}};', |
| 85 |
], |
| 86 |
] |
| 87 |
); |
| 88 |
|
| 89 |
$this->add_responsive_control( |
| 90 |
'width', |
| 91 |
[ |
| 92 |
'label' => esc_html__( 'Width', 'templately' ), |
| 93 |
'type' => Controls_Manager::SLIDER, |
| 94 |
'default' => [ |
| 95 |
'unit' => '%', |
| 96 |
], |
| 97 |
'tablet_default' => [ |
| 98 |
'unit' => '%', |
| 99 |
], |
| 100 |
'mobile_default' => [ |
| 101 |
'unit' => '%', |
| 102 |
], |
| 103 |
'size_units' => [ 'px', '%', 'em', 'rem', 'vw', 'custom' ], |
| 104 |
'range' => [ |
| 105 |
'%' => [ |
| 106 |
'min' => 1, |
| 107 |
'max' => 100, |
| 108 |
], |
| 109 |
'px' => [ |
| 110 |
'min' => 1, |
| 111 |
'max' => 1000, |
| 112 |
], |
| 113 |
'vw' => [ |
| 114 |
'min' => 1, |
| 115 |
'max' => 100, |
| 116 |
], |
| 117 |
], |
| 118 |
'selectors' => [ |
| 119 |
'{{WRAPPER}} img' => 'width: {{SIZE}}{{UNIT}};', |
| 120 |
], |
| 121 |
] |
| 122 |
); |
| 123 |
|
| 124 |
$this->add_responsive_control( |
| 125 |
'max-width', |
| 126 |
[ |
| 127 |
'label' => esc_html__( 'Max Width', 'templately' ), |
| 128 |
'type' => Controls_Manager::SLIDER, |
| 129 |
'default' => [ |
| 130 |
'unit' => '%', |
| 131 |
], |
| 132 |
'tablet_default' => [ |
| 133 |
'unit' => '%', |
| 134 |
], |
| 135 |
'mobile_default' => [ |
| 136 |
'unit' => '%', |
| 137 |
], |
| 138 |
'size_units' => [ 'px', '%', 'em', 'rem', 'vw', 'custom' ], |
| 139 |
'range' => [ |
| 140 |
'%' => [ |
| 141 |
'min' => 1, |
| 142 |
'max' => 100, |
| 143 |
], |
| 144 |
'px' => [ |
| 145 |
'min' => 1, |
| 146 |
'max' => 1000, |
| 147 |
], |
| 148 |
'vw' => [ |
| 149 |
'min' => 1, |
| 150 |
'max' => 100, |
| 151 |
], |
| 152 |
], |
| 153 |
'selectors' => [ |
| 154 |
'{{WRAPPER}} img' => 'max-width: {{SIZE}}{{UNIT}};', |
| 155 |
], |
| 156 |
] |
| 157 |
); |
| 158 |
|
| 159 |
$this->add_responsive_control( |
| 160 |
'height', |
| 161 |
[ |
| 162 |
'label' => esc_html__( 'Height', 'templately' ), |
| 163 |
'type' => Controls_Manager::SLIDER, |
| 164 |
'size_units' => [ 'px', '%', 'em', 'rem', 'vh', 'custom' ], |
| 165 |
'range' => [ |
| 166 |
'px' => [ |
| 167 |
'min' => 1, |
| 168 |
'max' => 500, |
| 169 |
], |
| 170 |
'vh' => [ |
| 171 |
'min' => 1, |
| 172 |
'max' => 100, |
| 173 |
], |
| 174 |
], |
| 175 |
'selectors' => [ |
| 176 |
'{{WRAPPER}} img' => 'height: {{SIZE}}{{UNIT}};', |
| 177 |
], |
| 178 |
] |
| 179 |
); |
| 180 |
|
| 181 |
$this->add_responsive_control( |
| 182 |
'object-fit', |
| 183 |
[ |
| 184 |
'label' => esc_html__( 'Object Fit', 'templately' ), |
| 185 |
'type' => Controls_Manager::SELECT, |
| 186 |
'condition' => [ |
| 187 |
'height[size]!' => '', |
| 188 |
], |
| 189 |
'options' => [ |
| 190 |
'' => esc_html__( 'Default', 'templately' ), |
| 191 |
'fill' => esc_html__( 'Fill', 'templately' ), |
| 192 |
'cover' => esc_html__( 'Cover', 'templately' ), |
| 193 |
'contain' => esc_html__( 'Contain', 'templately' ), |
| 194 |
], |
| 195 |
'default' => '', |
| 196 |
'selectors' => [ |
| 197 |
'{{WRAPPER}} img' => 'object-fit: {{VALUE}};', |
| 198 |
], |
| 199 |
] |
| 200 |
); |
| 201 |
|
| 202 |
$this->add_responsive_control( |
| 203 |
'object-position', |
| 204 |
[ |
| 205 |
'label' => esc_html__( 'Object Position', 'templately' ), |
| 206 |
'type' => Controls_Manager::SELECT, |
| 207 |
'options' => [ |
| 208 |
'center center' => esc_html__( 'Center Center', 'templately' ), |
| 209 |
'center left' => esc_html__( 'Center Left', 'templately' ), |
| 210 |
'center right' => esc_html__( 'Center Right', 'templately' ), |
| 211 |
'top center' => esc_html__( 'Top Center', 'templately' ), |
| 212 |
'top left' => esc_html__( 'Top Left', 'templately' ), |
| 213 |
'top right' => esc_html__( 'Top Right', 'templately' ), |
| 214 |
'bottom center' => esc_html__( 'Bottom Center', 'templately' ), |
| 215 |
'bottom left' => esc_html__( 'Bottom Left', 'templately' ), |
| 216 |
'bottom right' => esc_html__( 'Bottom Right', 'templately' ), |
| 217 |
], |
| 218 |
'default' => 'center center', |
| 219 |
'selectors' => [ |
| 220 |
'{{WRAPPER}} img' => 'object-position: {{VALUE}};', |
| 221 |
], |
| 222 |
'condition' => [ |
| 223 |
'height[size]!' => '', |
| 224 |
'object-fit' => 'cover', |
| 225 |
], |
| 226 |
] |
| 227 |
); |
| 228 |
|
| 229 |
$this->add_group_control( |
| 230 |
Group_Control_Border::get_type(), |
| 231 |
[ |
| 232 |
'name' => 'image_border', |
| 233 |
'selector' => '{{WRAPPER}} img', |
| 234 |
'separator' => 'before', |
| 235 |
] |
| 236 |
); |
| 237 |
|
| 238 |
$this->add_responsive_control( |
| 239 |
'image_border_radius', |
| 240 |
[ |
| 241 |
'label' => esc_html__( 'Border Radius', 'templately' ), |
| 242 |
'type' => Controls_Manager::DIMENSIONS, |
| 243 |
'size_units' => [ 'px', '%', 'em', 'rem', 'custom' ], |
| 244 |
'selectors' => [ |
| 245 |
'{{WRAPPER}} img' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 246 |
], |
| 247 |
] |
| 248 |
); |
| 249 |
|
| 250 |
$this->add_group_control( |
| 251 |
Group_Control_Box_Shadow::get_type(), |
| 252 |
[ |
| 253 |
'name' => 'image_box_shadow', |
| 254 |
'exclude' => [ |
| 255 |
'box_shadow_position', |
| 256 |
], |
| 257 |
'selector' => '{{WRAPPER}} img', |
| 258 |
] |
| 259 |
); |
| 260 |
|
| 261 |
$this->end_controls_section(); |
| 262 |
} |
| 263 |
|
| 264 |
protected function get_dynamic_post_ID() { |
| 265 |
$latest_cpt = get_posts( "post_type=post&numberposts=1" ); |
| 266 |
|
| 267 |
return Plugin::$instance->editor->is_edit_mode() || isset( $_GET['preview_id'] ) || isset( $_GET['preview'] ) ? $latest_cpt[0]->ID : get_the_ID(); |
| 268 |
} |
| 269 |
|
| 270 |
protected function render() { |
| 271 |
$settings = $this->get_settings_for_display(); |
| 272 |
$size = $settings['image_size']; |
| 273 |
$attachment_id = get_post_thumbnail_id( $this->get_dynamic_post_ID() ); |
| 274 |
$image_src = wp_get_attachment_image_src( $attachment_id, $size ); |
| 275 |
$image_src = $image_src[0] ?? ''; |
| 276 |
$image_title = get_the_title( $attachment_id ); |
| 277 |
$image_alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ?? ''; |
| 278 |
$image_class_html = "attachment-{$size} size-{$size} wp-image-{$attachment_id}"; |
| 279 |
|
| 280 |
printf( |
| 281 |
'<img src="%1$s" title="%2$s" alt="%3$s" class="%4$s" loading="lazy" />', |
| 282 |
esc_url( $image_src ), |
| 283 |
esc_attr( $image_title ), |
| 284 |
esc_attr( $image_alt ), |
| 285 |
esc_attr( $image_class_html ) |
| 286 |
); |
| 287 |
} |
| 288 |
} |
| 289 |
|