| 1 |
<?php |
| 2 |
|
| 3 |
namespace HelloPlus\Classes; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
use Elementor\{ |
| 10 |
Controls_Manager, |
| 11 |
Group_Control_Css_Filter, |
| 12 |
Group_Control_Box_Shadow, |
| 13 |
Group_Control_Image_Size, |
| 14 |
Widget_Base |
| 15 |
}; |
| 16 |
|
| 17 |
use Elementor\Core\Kits\Documents\Tabs\{ |
| 18 |
Global_Typography, |
| 19 |
Global_Colors, |
| 20 |
}; |
| 21 |
|
| 22 |
use Elementor\Utils as Elementor_Utils; |
| 23 |
|
| 24 |
use HelloPlus\Classes\Ehp_Shapes; |
| 25 |
|
| 26 |
class Ehp_Image { |
| 27 |
private $context = []; |
| 28 |
private $defaults = []; |
| 29 |
private ?Widget_Base $widget = null; |
| 30 |
|
| 31 |
const EHP_PREFIX = 'ehp-'; |
| 32 |
const CLASSNAME_IMAGE = 'ehp-image'; |
| 33 |
|
| 34 |
public function set_context( array $context ) { |
| 35 |
$this->context = $context; |
| 36 |
} |
| 37 |
|
| 38 |
public function get_attachment_image_html_filter( $html ) { |
| 39 |
$settings = $this->widget->get_settings_for_display(); |
| 40 |
$widget_name = $this->context['widget_name']; |
| 41 |
|
| 42 |
$image_classnames = [ |
| 43 |
self::CLASSNAME_IMAGE . '__img', |
| 44 |
self::EHP_PREFIX . $widget_name . '__img', |
| 45 |
]; |
| 46 |
|
| 47 |
if ( ! empty( $settings['show_image_border'] ) && 'yes' === $settings['show_image_border'] ) { |
| 48 |
$image_classnames[] = 'has-border'; |
| 49 |
} |
| 50 |
|
| 51 |
$shapes = new Ehp_Shapes( $this->widget, [ |
| 52 |
'container_prefix' => 'image', |
| 53 |
'widget_name' => $widget_name, |
| 54 |
] ); |
| 55 |
$image_classnames = array_merge( $image_classnames, $shapes->get_shape_classnames() ); |
| 56 |
|
| 57 |
$html = str_replace( '<img ', '<img class="' . esc_attr( implode( ' ', $image_classnames ) ) . '" ', $html ); |
| 58 |
return $html; |
| 59 |
} |
| 60 |
|
| 61 |
public function render() { |
| 62 |
$settings = $this->widget->get_settings_for_display(); |
| 63 |
$widget_name = $this->context['widget_name']; |
| 64 |
|
| 65 |
$image = $this->defaults['image'] ?? $settings['image']; |
| 66 |
|
| 67 |
$has_image = ! empty( $image['url'] ); |
| 68 |
$image_wrapper_classnames = [ |
| 69 |
self::CLASSNAME_IMAGE, |
| 70 |
self::EHP_PREFIX . $widget_name . '__image-container', |
| 71 |
]; |
| 72 |
|
| 73 |
$this->widget->add_render_attribute( 'image', [ |
| 74 |
'class' => $image_wrapper_classnames, |
| 75 |
] ); |
| 76 |
|
| 77 |
$settings_control_value = $this->defaults['settings'] ?? $settings; |
| 78 |
$image_key = $this->defaults['image_key'] ?? 'image'; |
| 79 |
|
| 80 |
if ( $has_image ) : |
| 81 |
?> |
| 82 |
<div <?php $this->widget->print_render_attribute_string( 'image' ); ?>> |
| 83 |
<?php |
| 84 |
add_filter( 'elementor/image_size/get_attachment_image_html', [ $this, 'get_attachment_image_html_filter' ], 10, 4 ); |
| 85 |
Group_Control_Image_Size::print_attachment_image_html( $settings_control_value, $image_key ); |
| 86 |
remove_filter( 'elementor/image_size/get_attachment_image_html', [ $this, 'get_attachment_image_html_filter' ], 10 ); |
| 87 |
?> |
| 88 |
</div> |
| 89 |
<?php |
| 90 |
endif; //has_image |
| 91 |
} |
| 92 |
|
| 93 |
public function add_content_section() { |
| 94 |
$this->widget->add_control( |
| 95 |
'image', |
| 96 |
[ |
| 97 |
'label' => esc_html__( 'Choose Image', 'hello-plus' ), |
| 98 |
'type' => Controls_Manager::MEDIA, |
| 99 |
'default' => [ |
| 100 |
'url' => Elementor_Utils::get_placeholder_image_src(), |
| 101 |
], |
| 102 |
'dynamic' => [ |
| 103 |
'active' => true, |
| 104 |
], |
| 105 |
] |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
public function add_style_controls() { |
| 110 |
$widget_name = $this->context['widget_name']; |
| 111 |
$defaults = [ |
| 112 |
'has_image_width_slider' => $this->defaults['has_image_width_slider'] ?? true, |
| 113 |
'has_image_width_dropdown' => $this->defaults['has_image_width_dropdown'] ?? false, |
| 114 |
]; |
| 115 |
|
| 116 |
$this->widget->add_control( |
| 117 |
'image_stretch', |
| 118 |
[ |
| 119 |
'label' => esc_html__( 'Stretch', 'hello-plus' ), |
| 120 |
'type' => Controls_Manager::SWITCHER, |
| 121 |
'label_on' => esc_html__( 'Yes', 'hello-plus' ), |
| 122 |
'label_off' => esc_html__( 'No', 'hello-plus' ), |
| 123 |
'return_value' => 'yes', |
| 124 |
'default' => 'no', |
| 125 |
] |
| 126 |
); |
| 127 |
|
| 128 |
$this->widget->add_responsive_control( |
| 129 |
'image_height', |
| 130 |
[ |
| 131 |
'label' => esc_html__( 'Height', 'hello-plus' ), |
| 132 |
'type' => Controls_Manager::SLIDER, |
| 133 |
'size_units' => [ 'px', 'em', 'rem', '%', 'custom' ], |
| 134 |
'range' => [ |
| 135 |
'px' => [ |
| 136 |
'max' => 1500, |
| 137 |
], |
| 138 |
'%' => [ |
| 139 |
'max' => 100, |
| 140 |
], |
| 141 |
], |
| 142 |
'default' => [ |
| 143 |
'size' => 380, |
| 144 |
'unit' => 'px', |
| 145 |
], |
| 146 |
'selectors' => [ |
| 147 |
'{{WRAPPER}} .ehp-' . $widget_name => '--' . $widget_name . '-image-height: {{SIZE}}{{UNIT}};', |
| 148 |
], |
| 149 |
'condition' => [ |
| 150 |
'image_stretch!' => 'yes', |
| 151 |
], |
| 152 |
] |
| 153 |
); |
| 154 |
|
| 155 |
if ( $defaults['has_image_width_slider'] ) { |
| 156 |
$this->widget->add_responsive_control( |
| 157 |
'image_width', |
| 158 |
[ |
| 159 |
'label' => esc_html__( 'Width', 'hello-plus' ), |
| 160 |
'type' => Controls_Manager::SLIDER, |
| 161 |
'size_units' => [ 'px', 'em', 'rem', '%', 'custom' ], |
| 162 |
'range' => [ |
| 163 |
'px' => [ |
| 164 |
'max' => 1500, |
| 165 |
], |
| 166 |
'%' => [ |
| 167 |
'max' => 100, |
| 168 |
], |
| 169 |
], |
| 170 |
'default' => [ |
| 171 |
'size' => 100, |
| 172 |
'unit' => '%', |
| 173 |
], |
| 174 |
'selectors' => [ |
| 175 |
'{{WRAPPER}} .ehp-' . $widget_name => '--' . $widget_name . '-image-width: {{SIZE}}{{UNIT}};', |
| 176 |
], |
| 177 |
'condition' => [ |
| 178 |
'image_stretch!' => 'yes', |
| 179 |
], |
| 180 |
] |
| 181 |
); |
| 182 |
} |
| 183 |
|
| 184 |
if ( $defaults['has_image_width_dropdown'] ) { |
| 185 |
$this->widget->add_responsive_control( |
| 186 |
'image_width', |
| 187 |
[ |
| 188 |
'label' => esc_html__( 'Width', 'hello-plus' ), |
| 189 |
'type' => Controls_Manager::SELECT, |
| 190 |
'options' => [ |
| 191 |
'50%' => '50%', |
| 192 |
'40%' => '40%', |
| 193 |
'30%' => '30%', |
| 194 |
], |
| 195 |
'default' => '50%', |
| 196 |
'selectors' => [ |
| 197 |
'{{WRAPPER}} .ehp-' . $widget_name => '--' . $widget_name . '-image-width: {{VALUE}};', |
| 198 |
], |
| 199 |
'condition' => [ |
| 200 |
'image_stretch!' => 'yes', |
| 201 |
], |
| 202 |
] |
| 203 |
); |
| 204 |
} |
| 205 |
|
| 206 |
$this->widget->add_responsive_control( |
| 207 |
'image_min_height', |
| 208 |
[ |
| 209 |
'label' => esc_html__( 'Min Height', 'hello-plus' ), |
| 210 |
'type' => Controls_Manager::SLIDER, |
| 211 |
'size_units' => [ 'px', 'em', 'rem', '%', 'custom' ], |
| 212 |
'range' => [ |
| 213 |
'px' => [ |
| 214 |
'max' => 1500, |
| 215 |
], |
| 216 |
'%' => [ |
| 217 |
'max' => 100, |
| 218 |
], |
| 219 |
], |
| 220 |
'selectors' => [ |
| 221 |
'{{WRAPPER}} .ehp-' . $widget_name => '--' . $widget_name . '-image-min-height: {{SIZE}}{{UNIT}};', |
| 222 |
], |
| 223 |
'condition' => [ |
| 224 |
'image_stretch' => 'yes', |
| 225 |
], |
| 226 |
] |
| 227 |
); |
| 228 |
|
| 229 |
$this->widget->add_responsive_control( |
| 230 |
'image_position', |
| 231 |
[ |
| 232 |
'label' => esc_html__( 'Position', 'hello-plus' ), |
| 233 |
'type' => Controls_Manager::SELECT, |
| 234 |
'desktop_default' => 'center center', |
| 235 |
'tablet_default' => 'center center', |
| 236 |
'mobile_default' => 'center center', |
| 237 |
'options' => [ |
| 238 |
'' => esc_html__( 'Default', 'hello-plus' ), |
| 239 |
'center center' => esc_html__( 'Center Center', 'hello-plus' ), |
| 240 |
'center left' => esc_html__( 'Center Left', 'hello-plus' ), |
| 241 |
'center right' => esc_html__( 'Center Right', 'hello-plus' ), |
| 242 |
'top center' => esc_html__( 'Top Center', 'hello-plus' ), |
| 243 |
'top left' => esc_html__( 'Top Left', 'hello-plus' ), |
| 244 |
'top right' => esc_html__( 'Top Right', 'hello-plus' ), |
| 245 |
'bottom center' => esc_html__( 'Bottom Center', 'hello-plus' ), |
| 246 |
'bottom left' => esc_html__( 'Bottom Left', 'hello-plus' ), |
| 247 |
'bottom right' => esc_html__( 'Bottom Right', 'hello-plus' ), |
| 248 |
], |
| 249 |
'selectors' => [ |
| 250 |
'{{WRAPPER}} .ehp-' . $widget_name => '--' . $widget_name . '-image-position: {{VALUE}}', |
| 251 |
], |
| 252 |
] |
| 253 |
); |
| 254 |
|
| 255 |
$this->widget->add_group_control( |
| 256 |
Group_Control_Css_Filter::get_type(), |
| 257 |
[ |
| 258 |
'name' => 'image_css_filters', |
| 259 |
'selector' => '{{WRAPPER}} .ehp-' . $widget_name . '__image-container img', |
| 260 |
] |
| 261 |
); |
| 262 |
|
| 263 |
$this->widget->add_control( |
| 264 |
'show_image_border', |
| 265 |
[ |
| 266 |
'label' => esc_html__( 'Border', 'hello-plus' ), |
| 267 |
'type' => Controls_Manager::SWITCHER, |
| 268 |
'label_on' => esc_html__( 'Yes', 'hello-plus' ), |
| 269 |
'label_off' => esc_html__( 'No', 'hello-plus' ), |
| 270 |
'return_value' => 'yes', |
| 271 |
'default' => 'no', |
| 272 |
'separator' => 'before', |
| 273 |
] |
| 274 |
); |
| 275 |
|
| 276 |
$this->widget->add_control( |
| 277 |
'image_border_width', |
| 278 |
[ |
| 279 |
'label' => __( 'Border Width', 'hello-plus' ), |
| 280 |
'type' => Controls_Manager::SLIDER, |
| 281 |
'size_units' => [ 'px' ], |
| 282 |
'range' => [ |
| 283 |
'px' => [ |
| 284 |
'min' => 0, |
| 285 |
'max' => 10, |
| 286 |
'step' => 1, |
| 287 |
], |
| 288 |
], |
| 289 |
'default' => [ |
| 290 |
'size' => 1, |
| 291 |
'unit' => 'px', |
| 292 |
], |
| 293 |
'selectors' => [ |
| 294 |
'{{WRAPPER}} .ehp-' . $widget_name => '--' . $widget_name . '-image-border-width: {{SIZE}}{{UNIT}};', |
| 295 |
], |
| 296 |
'condition' => [ |
| 297 |
'show_image_border' => 'yes', |
| 298 |
], |
| 299 |
] |
| 300 |
); |
| 301 |
|
| 302 |
$this->widget->add_control( |
| 303 |
'image_border_color', |
| 304 |
[ |
| 305 |
'label' => esc_html__( 'Color', 'hello-plus' ), |
| 306 |
'type' => Controls_Manager::COLOR, |
| 307 |
'global' => [ |
| 308 |
'default' => Global_Colors::COLOR_TEXT, |
| 309 |
], |
| 310 |
'selectors' => [ |
| 311 |
'{{WRAPPER}} .ehp-' . $widget_name => '--' . $widget_name . '-image-border-color: {{VALUE}}', |
| 312 |
], |
| 313 |
'condition' => [ |
| 314 |
'show_image_border' => 'yes', |
| 315 |
], |
| 316 |
] |
| 317 |
); |
| 318 |
|
| 319 |
$shapes = new Ehp_Shapes( $this->widget, [ |
| 320 |
'widget_name' => $widget_name, |
| 321 |
'container_prefix' => 'image', |
| 322 |
] ); |
| 323 |
$shapes->add_style_controls(); |
| 324 |
|
| 325 |
$this->widget->add_group_control( |
| 326 |
Group_Control_Box_Shadow::get_type(), |
| 327 |
[ |
| 328 |
'name' => 'image_box_shadow', |
| 329 |
'selector' => '{{WRAPPER}} .ehp-' . $widget_name . '__image-container img', |
| 330 |
] |
| 331 |
); |
| 332 |
} |
| 333 |
|
| 334 |
public function __construct( Widget_Base $widget, $context = [], $defaults = [] ) { |
| 335 |
$this->widget = $widget; |
| 336 |
$this->context = $context; |
| 337 |
$this->defaults = $defaults; |
| 338 |
} |
| 339 |
} |
| 340 |
|