| 1 |
<?php |
| 2 |
namespace Elementor; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; // Exit if accessed directly. |
| 6 |
} |
| 7 |
|
| 8 |
use Elementor\Core\Kits\Documents\Tabs\Global_Colors; |
| 9 |
use Elementor\Core\Kits\Documents\Tabs\Global_Typography; |
| 10 |
|
| 11 |
/** |
| 12 |
* Elementor image widget. |
| 13 |
* |
| 14 |
* Elementor widget that displays an image into the page. |
| 15 |
* |
| 16 |
* @since 1.0.0 |
| 17 |
*/ |
| 18 |
class Widget_Image extends Widget_Base { |
| 19 |
|
| 20 |
/** |
| 21 |
* Get widget name. |
| 22 |
* |
| 23 |
* Retrieve image widget name. |
| 24 |
* |
| 25 |
* @since 1.0.0 |
| 26 |
* @access public |
| 27 |
* |
| 28 |
* @return string Widget name. |
| 29 |
*/ |
| 30 |
public function get_name() { |
| 31 |
return 'image'; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Get widget title. |
| 36 |
* |
| 37 |
* Retrieve image widget title. |
| 38 |
* |
| 39 |
* @since 1.0.0 |
| 40 |
* @access public |
| 41 |
* |
| 42 |
* @return string Widget title. |
| 43 |
*/ |
| 44 |
public function get_title() { |
| 45 |
return esc_html__( 'Image', 'elementor' ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Get widget icon. |
| 50 |
* |
| 51 |
* Retrieve image widget icon. |
| 52 |
* |
| 53 |
* @since 1.0.0 |
| 54 |
* @access public |
| 55 |
* |
| 56 |
* @return string Widget icon. |
| 57 |
*/ |
| 58 |
public function get_icon() { |
| 59 |
return 'eicon-image'; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get widget categories. |
| 64 |
* |
| 65 |
* Retrieve the list of categories the image widget belongs to. |
| 66 |
* |
| 67 |
* Used to determine where to display the widget in the editor. |
| 68 |
* |
| 69 |
* @since 2.0.0 |
| 70 |
* @access public |
| 71 |
* |
| 72 |
* @return array Widget categories. |
| 73 |
*/ |
| 74 |
public function get_categories() { |
| 75 |
return [ 'basic' ]; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Get widget keywords. |
| 80 |
* |
| 81 |
* Retrieve the list of keywords the widget belongs to. |
| 82 |
* |
| 83 |
* @since 2.1.0 |
| 84 |
* @access public |
| 85 |
* |
| 86 |
* @return array Widget keywords. |
| 87 |
*/ |
| 88 |
public function get_keywords() { |
| 89 |
return [ 'image', 'photo', 'visual' ]; |
| 90 |
} |
| 91 |
|
| 92 |
protected function is_dynamic_content(): bool { |
| 93 |
return false; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Get style dependencies. |
| 98 |
* |
| 99 |
* Retrieve the list of style dependencies the widget requires. |
| 100 |
* |
| 101 |
* @since 3.24.0 |
| 102 |
* @access public |
| 103 |
* |
| 104 |
* @return array Widget style dependencies. |
| 105 |
*/ |
| 106 |
public function get_style_depends(): array { |
| 107 |
return [ 'widget-image' ]; |
| 108 |
} |
| 109 |
|
| 110 |
public function has_widget_inner_wrapper(): bool { |
| 111 |
return ! Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' ); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Register image widget controls. |
| 116 |
* |
| 117 |
* Adds different input fields to allow the user to change and customize the widget settings. |
| 118 |
* |
| 119 |
* @since 3.1.0 |
| 120 |
* @access protected |
| 121 |
*/ |
| 122 |
protected function register_controls() { |
| 123 |
$this->start_controls_section( |
| 124 |
'section_image', |
| 125 |
[ |
| 126 |
'label' => esc_html__( 'Image', 'elementor' ), |
| 127 |
] |
| 128 |
); |
| 129 |
|
| 130 |
$this->add_control( |
| 131 |
'image', |
| 132 |
[ |
| 133 |
'label' => esc_html__( 'Choose Image', 'elementor' ), |
| 134 |
'type' => Controls_Manager::MEDIA, |
| 135 |
'dynamic' => [ |
| 136 |
'active' => true, |
| 137 |
], |
| 138 |
'default' => [ |
| 139 |
'url' => Utils::get_placeholder_image_src(), |
| 140 |
], |
| 141 |
] |
| 142 |
); |
| 143 |
|
| 144 |
$this->add_group_control( |
| 145 |
Group_Control_Image_Size::get_type(), |
| 146 |
[ |
| 147 |
'name' => 'image', // Usage: `{name}_size` and `{name}_custom_dimension`, in this case `image_size` and `image_custom_dimension`. |
| 148 |
'default' => 'large', |
| 149 |
'condition' => [ |
| 150 |
'image[url]!' => '', |
| 151 |
], |
| 152 |
] |
| 153 |
); |
| 154 |
|
| 155 |
$this->add_control( |
| 156 |
'caption_source', |
| 157 |
[ |
| 158 |
'label' => esc_html__( 'Caption', 'elementor' ), |
| 159 |
'type' => Controls_Manager::SELECT, |
| 160 |
'options' => [ |
| 161 |
'none' => esc_html__( 'None', 'elementor' ), |
| 162 |
'attachment' => esc_html__( 'Attachment Caption', 'elementor' ), |
| 163 |
'custom' => esc_html__( 'Custom Caption', 'elementor' ), |
| 164 |
], |
| 165 |
'default' => 'none', |
| 166 |
'condition' => [ |
| 167 |
'image[url]!' => '', |
| 168 |
], |
| 169 |
] |
| 170 |
); |
| 171 |
|
| 172 |
$this->add_control( |
| 173 |
'caption', |
| 174 |
[ |
| 175 |
'label' => esc_html__( 'Custom Caption', 'elementor' ), |
| 176 |
'type' => Controls_Manager::TEXT, |
| 177 |
'default' => '', |
| 178 |
'placeholder' => esc_html__( 'Enter your image caption', 'elementor' ), |
| 179 |
'condition' => [ |
| 180 |
'image[url]!' => '', |
| 181 |
'caption_source' => 'custom', |
| 182 |
], |
| 183 |
'dynamic' => [ |
| 184 |
'active' => true, |
| 185 |
], |
| 186 |
] |
| 187 |
); |
| 188 |
|
| 189 |
$this->add_control( |
| 190 |
'link_to', |
| 191 |
[ |
| 192 |
'label' => esc_html__( 'Link', 'elementor' ), |
| 193 |
'type' => Controls_Manager::SELECT, |
| 194 |
'default' => 'none', |
| 195 |
'options' => [ |
| 196 |
'none' => esc_html__( 'None', 'elementor' ), |
| 197 |
'file' => esc_html__( 'Media File', 'elementor' ), |
| 198 |
'custom' => esc_html__( 'Custom URL', 'elementor' ), |
| 199 |
], |
| 200 |
'condition' => [ |
| 201 |
'image[url]!' => '', |
| 202 |
], |
| 203 |
] |
| 204 |
); |
| 205 |
|
| 206 |
$this->add_control( |
| 207 |
'link', |
| 208 |
[ |
| 209 |
'label' => esc_html__( 'Link', 'elementor' ), |
| 210 |
'type' => Controls_Manager::URL, |
| 211 |
'dynamic' => [ |
| 212 |
'active' => true, |
| 213 |
], |
| 214 |
'condition' => [ |
| 215 |
'image[url]!' => '', |
| 216 |
'link_to' => 'custom', |
| 217 |
], |
| 218 |
'show_label' => false, |
| 219 |
] |
| 220 |
); |
| 221 |
|
| 222 |
$this->add_control( |
| 223 |
'open_lightbox', |
| 224 |
[ |
| 225 |
'label' => esc_html__( 'Lightbox', 'elementor' ), |
| 226 |
'type' => Controls_Manager::SELECT, |
| 227 |
'description' => sprintf( |
| 228 |
/* translators: 1: Link open tag, 2: Link close tag. */ |
| 229 |
esc_html__( 'Manage your site’s lightbox settings in the %1$sLightbox panel%2$s.', 'elementor' ), |
| 230 |
'<a href="javascript: $e.run( \'panel/global/open\' ).then( () => $e.route( \'panel/global/settings-lightbox\' ) )">', |
| 231 |
'</a>' |
| 232 |
), |
| 233 |
'default' => 'default', |
| 234 |
'options' => [ |
| 235 |
'default' => esc_html__( 'Default', 'elementor' ), |
| 236 |
'yes' => esc_html__( 'Yes', 'elementor' ), |
| 237 |
'no' => esc_html__( 'No', 'elementor' ), |
| 238 |
], |
| 239 |
'condition' => [ |
| 240 |
'image[url]!' => '', |
| 241 |
'link_to' => 'file', |
| 242 |
], |
| 243 |
] |
| 244 |
); |
| 245 |
|
| 246 |
$this->end_controls_section(); |
| 247 |
|
| 248 |
$this->start_controls_section( |
| 249 |
'section_style_image', |
| 250 |
[ |
| 251 |
'label' => esc_html__( 'Image', 'elementor' ), |
| 252 |
'tab' => Controls_Manager::TAB_STYLE, |
| 253 |
] |
| 254 |
); |
| 255 |
|
| 256 |
$this->add_responsive_control( |
| 257 |
'align', |
| 258 |
[ |
| 259 |
'label' => esc_html__( 'Alignment', 'elementor' ), |
| 260 |
'type' => Controls_Manager::CHOOSE, |
| 261 |
'options' => [ |
| 262 |
'start' => [ |
| 263 |
'title' => esc_html__( 'Start', 'elementor' ), |
| 264 |
'icon' => 'eicon-text-align-left', |
| 265 |
], |
| 266 |
'center' => [ |
| 267 |
'title' => esc_html__( 'Center', 'elementor' ), |
| 268 |
'icon' => 'eicon-text-align-center', |
| 269 |
], |
| 270 |
'end' => [ |
| 271 |
'title' => esc_html__( 'End', 'elementor' ), |
| 272 |
'icon' => 'eicon-text-align-right', |
| 273 |
], |
| 274 |
], |
| 275 |
'classes' => 'elementor-control-start-end', |
| 276 |
'selectors_dictionary' => [ |
| 277 |
'left' => is_rtl() ? 'end' : 'start', |
| 278 |
'right' => is_rtl() ? 'start' : 'end', |
| 279 |
], |
| 280 |
'selectors' => [ |
| 281 |
'{{WRAPPER}}' => 'text-align: {{VALUE}};', |
| 282 |
], |
| 283 |
] |
| 284 |
); |
| 285 |
|
| 286 |
$this->add_responsive_control( |
| 287 |
'width', |
| 288 |
[ |
| 289 |
'label' => esc_html__( 'Width', 'elementor' ), |
| 290 |
'type' => Controls_Manager::SLIDER, |
| 291 |
'default' => [ |
| 292 |
'unit' => '%', |
| 293 |
], |
| 294 |
'tablet_default' => [ |
| 295 |
'unit' => '%', |
| 296 |
], |
| 297 |
'mobile_default' => [ |
| 298 |
'unit' => '%', |
| 299 |
], |
| 300 |
'size_units' => [ 'px', '%', 'em', 'rem', 'vw', 'custom' ], |
| 301 |
'range' => [ |
| 302 |
'%' => [ |
| 303 |
'min' => 1, |
| 304 |
'max' => 100, |
| 305 |
], |
| 306 |
'px' => [ |
| 307 |
'min' => 1, |
| 308 |
'max' => 1000, |
| 309 |
], |
| 310 |
'vw' => [ |
| 311 |
'min' => 1, |
| 312 |
'max' => 100, |
| 313 |
], |
| 314 |
], |
| 315 |
'selectors' => [ |
| 316 |
'{{WRAPPER}} img' => 'width: {{SIZE}}{{UNIT}};', |
| 317 |
], |
| 318 |
] |
| 319 |
); |
| 320 |
|
| 321 |
$this->add_responsive_control( |
| 322 |
'space', |
| 323 |
[ |
| 324 |
'label' => esc_html__( 'Max Width', 'elementor' ), |
| 325 |
'type' => Controls_Manager::SLIDER, |
| 326 |
'default' => [ |
| 327 |
'unit' => '%', |
| 328 |
], |
| 329 |
'tablet_default' => [ |
| 330 |
'unit' => '%', |
| 331 |
], |
| 332 |
'mobile_default' => [ |
| 333 |
'unit' => '%', |
| 334 |
], |
| 335 |
'size_units' => [ 'px', '%', 'em', 'rem', 'vw', 'custom' ], |
| 336 |
'range' => [ |
| 337 |
'%' => [ |
| 338 |
'min' => 1, |
| 339 |
'max' => 100, |
| 340 |
], |
| 341 |
'px' => [ |
| 342 |
'min' => 1, |
| 343 |
'max' => 1000, |
| 344 |
], |
| 345 |
'vw' => [ |
| 346 |
'min' => 1, |
| 347 |
'max' => 100, |
| 348 |
], |
| 349 |
], |
| 350 |
'selectors' => [ |
| 351 |
'{{WRAPPER}} img' => 'max-width: {{SIZE}}{{UNIT}};', |
| 352 |
], |
| 353 |
] |
| 354 |
); |
| 355 |
|
| 356 |
$this->add_responsive_control( |
| 357 |
'height', |
| 358 |
[ |
| 359 |
'label' => esc_html__( 'Height', 'elementor' ), |
| 360 |
'type' => Controls_Manager::SLIDER, |
| 361 |
'size_units' => [ 'px', '%', 'em', 'rem', 'vh', 'custom' ], |
| 362 |
'range' => [ |
| 363 |
'px' => [ |
| 364 |
'min' => 1, |
| 365 |
'max' => 500, |
| 366 |
], |
| 367 |
'vh' => [ |
| 368 |
'min' => 1, |
| 369 |
'max' => 100, |
| 370 |
], |
| 371 |
], |
| 372 |
'selectors' => [ |
| 373 |
'{{WRAPPER}} img' => 'height: {{SIZE}}{{UNIT}};', |
| 374 |
], |
| 375 |
] |
| 376 |
); |
| 377 |
|
| 378 |
$this->add_responsive_control( |
| 379 |
'object-fit', |
| 380 |
[ |
| 381 |
'label' => esc_html__( 'Object Fit', 'elementor' ), |
| 382 |
'type' => Controls_Manager::SELECT, |
| 383 |
'condition' => [ |
| 384 |
'height[size]!' => '', |
| 385 |
], |
| 386 |
'options' => [ |
| 387 |
'' => esc_html__( 'Default', 'elementor' ), |
| 388 |
'fill' => esc_html__( 'Fill', 'elementor' ), |
| 389 |
'cover' => esc_html__( 'Cover', 'elementor' ), |
| 390 |
'contain' => esc_html__( 'Contain', 'elementor' ), |
| 391 |
'scale-down' => esc_html__( 'Scale Down', 'elementor' ), |
| 392 |
], |
| 393 |
'default' => '', |
| 394 |
'selectors' => [ |
| 395 |
'{{WRAPPER}} img' => 'object-fit: {{VALUE}};', |
| 396 |
], |
| 397 |
] |
| 398 |
); |
| 399 |
|
| 400 |
$this->add_responsive_control( |
| 401 |
'object-position', |
| 402 |
[ |
| 403 |
'label' => esc_html__( 'Object Position', 'elementor' ), |
| 404 |
'type' => Controls_Manager::SELECT, |
| 405 |
'options' => [ |
| 406 |
'center center' => esc_html__( 'Center Center', 'elementor' ), |
| 407 |
'center left' => esc_html__( 'Center Left', 'elementor' ), |
| 408 |
'center right' => esc_html__( 'Center Right', 'elementor' ), |
| 409 |
'top center' => esc_html__( 'Top Center', 'elementor' ), |
| 410 |
'top left' => esc_html__( 'Top Left', 'elementor' ), |
| 411 |
'top right' => esc_html__( 'Top Right', 'elementor' ), |
| 412 |
'bottom center' => esc_html__( 'Bottom Center', 'elementor' ), |
| 413 |
'bottom left' => esc_html__( 'Bottom Left', 'elementor' ), |
| 414 |
'bottom right' => esc_html__( 'Bottom Right', 'elementor' ), |
| 415 |
], |
| 416 |
'default' => 'center center', |
| 417 |
'selectors' => [ |
| 418 |
'{{WRAPPER}} img' => 'object-position: {{VALUE}};', |
| 419 |
], |
| 420 |
'condition' => [ |
| 421 |
'height[size]!' => '', |
| 422 |
'object-fit' => [ 'cover', 'contain', 'scale-down' ], |
| 423 |
], |
| 424 |
] |
| 425 |
); |
| 426 |
|
| 427 |
$this->add_control( |
| 428 |
'separator_panel_style', |
| 429 |
[ |
| 430 |
'type' => Controls_Manager::DIVIDER, |
| 431 |
'style' => 'thick', |
| 432 |
] |
| 433 |
); |
| 434 |
|
| 435 |
$this->start_controls_tabs( 'image_effects' ); |
| 436 |
|
| 437 |
$this->start_controls_tab( 'normal', |
| 438 |
[ |
| 439 |
'label' => esc_html__( 'Normal', 'elementor' ), |
| 440 |
] |
| 441 |
); |
| 442 |
|
| 443 |
$this->add_control( |
| 444 |
'opacity', |
| 445 |
[ |
| 446 |
'label' => esc_html__( 'Opacity', 'elementor' ), |
| 447 |
'type' => Controls_Manager::SLIDER, |
| 448 |
'range' => [ |
| 449 |
'px' => [ |
| 450 |
'max' => 1, |
| 451 |
'min' => 0.10, |
| 452 |
'step' => 0.01, |
| 453 |
], |
| 454 |
], |
| 455 |
'selectors' => [ |
| 456 |
'{{WRAPPER}} img' => 'opacity: {{SIZE}};', |
| 457 |
], |
| 458 |
] |
| 459 |
); |
| 460 |
|
| 461 |
$this->add_group_control( |
| 462 |
Group_Control_Css_Filter::get_type(), |
| 463 |
[ |
| 464 |
'name' => 'css_filters', |
| 465 |
'selector' => '{{WRAPPER}} img', |
| 466 |
] |
| 467 |
); |
| 468 |
|
| 469 |
$this->end_controls_tab(); |
| 470 |
|
| 471 |
$this->start_controls_tab( 'hover', |
| 472 |
[ |
| 473 |
'label' => esc_html__( 'Hover', 'elementor' ), |
| 474 |
] |
| 475 |
); |
| 476 |
|
| 477 |
$this->add_control( |
| 478 |
'opacity_hover', |
| 479 |
[ |
| 480 |
'label' => esc_html__( 'Opacity', 'elementor' ), |
| 481 |
'type' => Controls_Manager::SLIDER, |
| 482 |
'range' => [ |
| 483 |
'px' => [ |
| 484 |
'max' => 1, |
| 485 |
'min' => 0.10, |
| 486 |
'step' => 0.01, |
| 487 |
], |
| 488 |
], |
| 489 |
'selectors' => [ |
| 490 |
'{{WRAPPER}}:hover img' => 'opacity: {{SIZE}};', |
| 491 |
], |
| 492 |
] |
| 493 |
); |
| 494 |
|
| 495 |
$this->add_group_control( |
| 496 |
Group_Control_Css_Filter::get_type(), |
| 497 |
[ |
| 498 |
'name' => 'css_filters_hover', |
| 499 |
'selector' => '{{WRAPPER}}:hover img', |
| 500 |
] |
| 501 |
); |
| 502 |
|
| 503 |
$this->add_control( |
| 504 |
'background_hover_transition', |
| 505 |
[ |
| 506 |
'label' => esc_html__( 'Transition Duration', 'elementor' ) . ' (s)', |
| 507 |
'type' => Controls_Manager::SLIDER, |
| 508 |
'range' => [ |
| 509 |
'px' => [ |
| 510 |
'min' => 0, |
| 511 |
'max' => 3, |
| 512 |
'step' => 0.1, |
| 513 |
], |
| 514 |
], |
| 515 |
'selectors' => [ |
| 516 |
'{{WRAPPER}} img' => 'transition-duration: {{SIZE}}s', |
| 517 |
], |
| 518 |
] |
| 519 |
); |
| 520 |
|
| 521 |
$this->add_control( |
| 522 |
'hover_animation', |
| 523 |
[ |
| 524 |
'label' => esc_html__( 'Hover Animation', 'elementor' ), |
| 525 |
'type' => Controls_Manager::HOVER_ANIMATION, |
| 526 |
] |
| 527 |
); |
| 528 |
|
| 529 |
$this->end_controls_tab(); |
| 530 |
|
| 531 |
$this->end_controls_tabs(); |
| 532 |
|
| 533 |
$this->add_group_control( |
| 534 |
Group_Control_Border::get_type(), |
| 535 |
[ |
| 536 |
'name' => 'image_border', |
| 537 |
'selector' => '{{WRAPPER}} img', |
| 538 |
'separator' => 'before', |
| 539 |
] |
| 540 |
); |
| 541 |
|
| 542 |
$this->add_responsive_control( |
| 543 |
'image_border_radius', |
| 544 |
[ |
| 545 |
'label' => esc_html__( 'Border Radius', 'elementor' ), |
| 546 |
'type' => Controls_Manager::DIMENSIONS, |
| 547 |
'size_units' => [ 'px', '%', 'em', 'rem', 'custom' ], |
| 548 |
'selectors' => [ |
| 549 |
'{{WRAPPER}} img' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 550 |
], |
| 551 |
] |
| 552 |
); |
| 553 |
|
| 554 |
$this->add_group_control( |
| 555 |
Group_Control_Box_Shadow::get_type(), |
| 556 |
[ |
| 557 |
'name' => 'image_box_shadow', |
| 558 |
'exclude' => [ |
| 559 |
'box_shadow_position', |
| 560 |
], |
| 561 |
'selector' => '{{WRAPPER}} img', |
| 562 |
] |
| 563 |
); |
| 564 |
|
| 565 |
$this->end_controls_section(); |
| 566 |
|
| 567 |
$this->start_controls_section( |
| 568 |
'section_style_caption', |
| 569 |
[ |
| 570 |
'label' => esc_html__( 'Caption', 'elementor' ), |
| 571 |
'tab' => Controls_Manager::TAB_STYLE, |
| 572 |
'condition' => [ |
| 573 |
'image[url]!' => '', |
| 574 |
'caption_source!' => 'none', |
| 575 |
], |
| 576 |
] |
| 577 |
); |
| 578 |
|
| 579 |
$this->add_responsive_control( |
| 580 |
'caption_align', |
| 581 |
[ |
| 582 |
'label' => esc_html__( 'Alignment', 'elementor' ), |
| 583 |
'type' => Controls_Manager::CHOOSE, |
| 584 |
'options' => [ |
| 585 |
'start' => [ |
| 586 |
'title' => esc_html__( 'Start', 'elementor' ), |
| 587 |
'icon' => 'eicon-text-align-left', |
| 588 |
], |
| 589 |
'center' => [ |
| 590 |
'title' => esc_html__( 'Center', 'elementor' ), |
| 591 |
'icon' => 'eicon-text-align-center', |
| 592 |
], |
| 593 |
'end' => [ |
| 594 |
'title' => esc_html__( 'End', 'elementor' ), |
| 595 |
'icon' => 'eicon-text-align-right', |
| 596 |
], |
| 597 |
'justify' => [ |
| 598 |
'title' => esc_html__( 'Justified', 'elementor' ), |
| 599 |
'icon' => 'eicon-text-align-justify', |
| 600 |
], |
| 601 |
], |
| 602 |
'classes' => 'elementor-control-start-end', |
| 603 |
'selectors_dictionary' => [ |
| 604 |
'left' => is_rtl() ? 'end' : 'start', |
| 605 |
'right' => is_rtl() ? 'start' : 'end', |
| 606 |
], |
| 607 |
'default' => '', |
| 608 |
'selectors' => [ |
| 609 |
'{{WRAPPER}} .widget-image-caption' => 'text-align: {{VALUE}};', |
| 610 |
], |
| 611 |
] |
| 612 |
); |
| 613 |
|
| 614 |
$this->add_control( |
| 615 |
'text_color', |
| 616 |
[ |
| 617 |
'label' => esc_html__( 'Text Color', 'elementor' ), |
| 618 |
'type' => Controls_Manager::COLOR, |
| 619 |
'default' => '', |
| 620 |
'selectors' => [ |
| 621 |
'{{WRAPPER}} .widget-image-caption' => 'color: {{VALUE}};', |
| 622 |
], |
| 623 |
'global' => [ |
| 624 |
'default' => Global_Colors::COLOR_TEXT, |
| 625 |
], |
| 626 |
] |
| 627 |
); |
| 628 |
|
| 629 |
$this->add_control( |
| 630 |
'caption_background_color', |
| 631 |
[ |
| 632 |
'label' => esc_html__( 'Background Color', 'elementor' ), |
| 633 |
'type' => Controls_Manager::COLOR, |
| 634 |
'selectors' => [ |
| 635 |
'{{WRAPPER}} .widget-image-caption' => 'background-color: {{VALUE}};', |
| 636 |
], |
| 637 |
] |
| 638 |
); |
| 639 |
|
| 640 |
$this->add_group_control( |
| 641 |
Group_Control_Typography::get_type(), |
| 642 |
[ |
| 643 |
'name' => 'caption_typography', |
| 644 |
'selector' => '{{WRAPPER}} .widget-image-caption', |
| 645 |
'global' => [ |
| 646 |
'default' => Global_Typography::TYPOGRAPHY_TEXT, |
| 647 |
], |
| 648 |
] |
| 649 |
); |
| 650 |
|
| 651 |
$this->add_group_control( |
| 652 |
Group_Control_Text_Shadow::get_type(), |
| 653 |
[ |
| 654 |
'name' => 'caption_text_shadow', |
| 655 |
'selector' => '{{WRAPPER}} .widget-image-caption', |
| 656 |
] |
| 657 |
); |
| 658 |
|
| 659 |
$this->add_responsive_control( |
| 660 |
'caption_space', |
| 661 |
[ |
| 662 |
'label' => esc_html__( 'Spacing', 'elementor' ), |
| 663 |
'type' => Controls_Manager::SLIDER, |
| 664 |
'size_units' => [ 'px', 'em', 'rem', 'custom' ], |
| 665 |
'range' => [ |
| 666 |
'px' => [ |
| 667 |
'max' => 100, |
| 668 |
], |
| 669 |
'em' => [ |
| 670 |
'min' => 0, |
| 671 |
'max' => 10, |
| 672 |
], |
| 673 |
'rem' => [ |
| 674 |
'min' => 0, |
| 675 |
'max' => 10, |
| 676 |
], |
| 677 |
], |
| 678 |
'selectors' => [ |
| 679 |
'{{WRAPPER}} .widget-image-caption' => 'margin-block-start: {{SIZE}}{{UNIT}};', |
| 680 |
], |
| 681 |
] |
| 682 |
); |
| 683 |
|
| 684 |
$this->end_controls_section(); |
| 685 |
} |
| 686 |
|
| 687 |
/** |
| 688 |
* Check if the current widget has caption |
| 689 |
* |
| 690 |
* @access private |
| 691 |
* @since 2.3.0 |
| 692 |
* |
| 693 |
* @param array $settings |
| 694 |
* |
| 695 |
* @return boolean |
| 696 |
*/ |
| 697 |
private function has_caption( $settings ) { |
| 698 |
return ( ! empty( $settings['caption_source'] ) && 'none' !== $settings['caption_source'] ); |
| 699 |
} |
| 700 |
|
| 701 |
/** |
| 702 |
* Get the caption for current widget. |
| 703 |
* |
| 704 |
* @access private |
| 705 |
* @since 2.3.0 |
| 706 |
* @param array $settings |
| 707 |
* |
| 708 |
* @return string |
| 709 |
*/ |
| 710 |
private function get_caption( $settings ) { |
| 711 |
$caption = ''; |
| 712 |
if ( ! empty( $settings['caption_source'] ) ) { |
| 713 |
switch ( $settings['caption_source'] ) { |
| 714 |
case 'attachment': |
| 715 |
$caption = wp_get_attachment_caption( $settings['image']['id'] ); |
| 716 |
break; |
| 717 |
case 'custom': |
| 718 |
$caption = ! Utils::is_empty( $settings['caption'] ) ? $settings['caption'] : ''; |
| 719 |
} |
| 720 |
} |
| 721 |
return $caption; |
| 722 |
} |
| 723 |
|
| 724 |
/** |
| 725 |
* Render image widget output on the frontend. |
| 726 |
* |
| 727 |
* Written in PHP and used to generate the final HTML. |
| 728 |
* |
| 729 |
* @since 1.0.0 |
| 730 |
* @access protected |
| 731 |
*/ |
| 732 |
protected function render() { |
| 733 |
$settings = $this->get_settings_for_display(); |
| 734 |
|
| 735 |
if ( empty( $settings['image']['url'] ) ) { |
| 736 |
return; |
| 737 |
} |
| 738 |
|
| 739 |
$has_caption = $this->has_caption( $settings ); |
| 740 |
|
| 741 |
$link = $this->get_link_url( $settings ); |
| 742 |
|
| 743 |
if ( $link ) { |
| 744 |
$this->add_link_attributes( 'link', $link ); |
| 745 |
|
| 746 |
if ( Plugin::$instance->editor->is_edit_mode() ) { |
| 747 |
$this->add_render_attribute( 'link', [ |
| 748 |
'class' => 'elementor-clickable', |
| 749 |
] ); |
| 750 |
} |
| 751 |
|
| 752 |
if ( 'custom' !== $settings['link_to'] ) { |
| 753 |
$this->add_lightbox_data_attributes( 'link', $settings['image']['id'], $settings['open_lightbox'] ); |
| 754 |
} |
| 755 |
} ?> |
| 756 |
<?php if ( $has_caption ) : ?> |
| 757 |
<figure class="wp-caption"> |
| 758 |
<?php endif; ?> |
| 759 |
<?php if ( $link ) : ?> |
| 760 |
<a <?php $this->print_render_attribute_string( 'link' ); ?>> |
| 761 |
<?php endif; ?> |
| 762 |
<?php Group_Control_Image_Size::print_attachment_image_html( $settings ); ?> |
| 763 |
<?php if ( $link ) : ?> |
| 764 |
</a> |
| 765 |
<?php endif; ?> |
| 766 |
<?php if ( $has_caption ) : ?> |
| 767 |
<figcaption class="widget-image-caption wp-caption-text"><?php |
| 768 |
echo wp_kses_post( $this->get_caption( $settings ) ); |
| 769 |
?></figcaption> |
| 770 |
<?php endif; ?> |
| 771 |
<?php if ( $has_caption ) : ?> |
| 772 |
</figure> |
| 773 |
<?php endif; ?> |
| 774 |
<?php |
| 775 |
} |
| 776 |
|
| 777 |
public function render_markdown(): string { |
| 778 |
$settings = $this->get_settings_for_display(); |
| 779 |
|
| 780 |
if ( empty( $settings['image']['url'] ) ) { |
| 781 |
return ''; |
| 782 |
} |
| 783 |
|
| 784 |
$url = esc_url( $settings['image']['url'] ); |
| 785 |
$alt = $this->get_image_alt_text( $settings ); |
| 786 |
$image_md = ''; |
| 787 |
|
| 788 |
$link = $this->get_link_url( $settings ); |
| 789 |
|
| 790 |
if ( ! empty( $link['url'] ) && $link['url'] !== $url ) { |
| 791 |
$image_md = '[' . $image_md . '](' . esc_url( $link['url'] ) . ')'; |
| 792 |
} |
| 793 |
|
| 794 |
$caption = $this->get_visible_caption( $settings ); |
| 795 |
|
| 796 |
if ( ! empty( $caption ) ) { |
| 797 |
$image_md .= "\n" . $caption; |
| 798 |
} |
| 799 |
|
| 800 |
return $image_md; |
| 801 |
} |
| 802 |
|
| 803 |
private function get_image_alt_text( array $settings ): string { |
| 804 |
if ( empty( $settings['image']['id'] ) ) { |
| 805 |
return ''; |
| 806 |
} |
| 807 |
|
| 808 |
$attachment_id = $settings['image']['id']; |
| 809 |
|
| 810 |
$alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ); |
| 811 |
|
| 812 |
if ( ! empty( $alt ) ) { |
| 813 |
return $alt; |
| 814 |
} |
| 815 |
|
| 816 |
$attachment = get_post( $attachment_id ); |
| 817 |
|
| 818 |
if ( ! $attachment ) { |
| 819 |
return ''; |
| 820 |
} |
| 821 |
|
| 822 |
return $attachment->post_title ?? ''; |
| 823 |
} |
| 824 |
|
| 825 |
private function get_visible_caption( array $settings ): string { |
| 826 |
if ( ! $this->has_caption( $settings ) ) { |
| 827 |
return ''; |
| 828 |
} |
| 829 |
|
| 830 |
return Utils::html_to_plain_text( $this->get_caption( $settings ) ); |
| 831 |
} |
| 832 |
|
| 833 |
/** |
| 834 |
* Render image widget output in the editor. |
| 835 |
* |
| 836 |
* Written as a Backbone JavaScript template and used to generate the live preview. |
| 837 |
* |
| 838 |
* @since 2.9.0 |
| 839 |
* @access protected |
| 840 |
*/ |
| 841 |
protected function content_template() { |
| 842 |
?> |
| 843 |
<# if ( settings.image.url ) { |
| 844 |
var image = { |
| 845 |
id: settings.image.id, |
| 846 |
url: settings.image.url, |
| 847 |
size: settings.image_size, |
| 848 |
dimension: settings.image_custom_dimension, |
| 849 |
model: view.getEditModel() |
| 850 |
}; |
| 851 |
|
| 852 |
var image_url = elementor.imagesManager.getImageUrl( image ); |
| 853 |
|
| 854 |
if ( ! image_url ) { |
| 855 |
return; |
| 856 |
} |
| 857 |
|
| 858 |
var hasCaption = function() { |
| 859 |
if( ! settings.caption_source || 'none' === settings.caption_source ) { |
| 860 |
return false; |
| 861 |
} |
| 862 |
return true; |
| 863 |
} |
| 864 |
|
| 865 |
var ensureAttachmentData = function( id ) { |
| 866 |
if ( 'undefined' === typeof wp.media.attachment( id ).get( 'caption' ) ) { |
| 867 |
wp.media.attachment( id ).fetch().then( function( data ) { |
| 868 |
view.render(); |
| 869 |
} ); |
| 870 |
} |
| 871 |
} |
| 872 |
|
| 873 |
var getAttachmentCaption = function( id ) { |
| 874 |
if ( ! id ) { |
| 875 |
return ''; |
| 876 |
} |
| 877 |
ensureAttachmentData( id ); |
| 878 |
return wp.media.attachment( id ).get( 'caption' ); |
| 879 |
} |
| 880 |
|
| 881 |
var getCaption = function() { |
| 882 |
if ( ! hasCaption() ) { |
| 883 |
return ''; |
| 884 |
} |
| 885 |
return 'custom' === settings.caption_source ? settings.caption : getAttachmentCaption( settings.image.id ); |
| 886 |
} |
| 887 |
|
| 888 |
var link_url; |
| 889 |
|
| 890 |
if ( 'custom' === settings.link_to ) { |
| 891 |
link_url = settings.link?.url; |
| 892 |
} |
| 893 |
|
| 894 |
if ( 'file' === settings.link_to ) { |
| 895 |
link_url = settings.image.url; |
| 896 |
} |
| 897 |
|
| 898 |
var imgClass = ''; |
| 899 |
|
| 900 |
if ( '' !== settings.hover_animation ) { |
| 901 |
imgClass = 'elementor-animation-' + settings.hover_animation; |
| 902 |
} |
| 903 |
|
| 904 |
if ( hasCaption() ) { |
| 905 |
#><figure class="wp-caption"><# |
| 906 |
} |
| 907 |
|
| 908 |
if ( link_url ) { |
| 909 |
#><a class="elementor-clickable" data-elementor-open-lightbox="{{ settings.open_lightbox }}" href="{{ elementor.helpers.sanitizeUrl( link_url ) }}"><# |
| 910 |
} |
| 911 |
#><img src="{{ image_url }}" class="{{ imgClass }}" /><# |
| 912 |
|
| 913 |
if ( link_url ) { |
| 914 |
#></a><# |
| 915 |
} |
| 916 |
|
| 917 |
if ( hasCaption() ) { |
| 918 |
#><figcaption class="widget-image-caption wp-caption-text">{{{ getCaption() }}}</figcaption><# |
| 919 |
} |
| 920 |
|
| 921 |
if ( hasCaption() ) { |
| 922 |
#></figure><# |
| 923 |
} |
| 924 |
} #> |
| 925 |
<?php |
| 926 |
} |
| 927 |
|
| 928 |
/** |
| 929 |
* Retrieve image widget link URL. |
| 930 |
* |
| 931 |
* @since 3.11.0 |
| 932 |
* @access protected |
| 933 |
* |
| 934 |
* @param array $settings |
| 935 |
* |
| 936 |
* @return array|string|false An array/string containing the link URL, or false if no link. |
| 937 |
*/ |
| 938 |
protected function get_link_url( $settings ) { |
| 939 |
if ( 'none' === $settings['link_to'] ) { |
| 940 |
return false; |
| 941 |
} |
| 942 |
|
| 943 |
if ( 'custom' === $settings['link_to'] ) { |
| 944 |
if ( empty( $settings['link']['url'] ) ) { |
| 945 |
return false; |
| 946 |
} |
| 947 |
|
| 948 |
return $settings['link']; |
| 949 |
} |
| 950 |
|
| 951 |
return [ |
| 952 |
'url' => $settings['image']['url'], |
| 953 |
]; |
| 954 |
} |
| 955 |
} |
| 956 |
|