image-gallery.php (9231B)
1 <?php 2 namespace Elementor; 3 4 use Elementor\Core\Kits\Documents\Tabs\Global_Typography; 5 6 if ( ! defined( 'ABSPATH' ) ) { 7 exit; // Exit if accessed directly. 8 } 9 10 /** 11 * Elementor image gallery widget. 12 * 13 * Elementor widget that displays a set of images in an aligned grid. 14 * 15 * @since 1.0.0 16 */ 17 class Widget_Image_Gallery extends Widget_Base { 18 19 /** 20 * Get widget name. 21 * 22 * Retrieve image gallery widget name. 23 * 24 * @since 1.0.0 25 * @access public 26 * 27 * @return string Widget name. 28 */ 29 public function get_name() { 30 return 'image-gallery'; 31 } 32 33 /** 34 * Get widget title. 35 * 36 * Retrieve image gallery widget title. 37 * 38 * @since 1.0.0 39 * @access public 40 * 41 * @return string Widget title. 42 */ 43 public function get_title() { 44 return esc_html__( 'Basic Gallery', 'elementor' ); 45 } 46 47 /** 48 * Get widget icon. 49 * 50 * Retrieve image gallery widget icon. 51 * 52 * @since 1.0.0 53 * @access public 54 * 55 * @return string Widget icon. 56 */ 57 public function get_icon() { 58 return 'eicon-gallery-grid'; 59 } 60 61 /** 62 * Get widget keywords. 63 * 64 * Retrieve the list of keywords the widget belongs to. 65 * 66 * @since 2.1.0 67 * @access public 68 * 69 * @return array Widget keywords. 70 */ 71 public function get_keywords() { 72 return [ 'image', 'photo', 'visual', 'gallery' ]; 73 } 74 75 /** 76 * Register image gallery widget controls. 77 * 78 * Adds different input fields to allow the user to change and customize the widget settings. 79 * 80 * @since 3.1.0 81 * @access protected 82 */ 83 protected function register_controls() { 84 $this->start_controls_section( 85 'section_gallery', 86 [ 87 'label' => esc_html__( 'Image Gallery', 'elementor' ), 88 ] 89 ); 90 91 $this->add_control( 92 'wp_gallery', 93 [ 94 'label' => esc_html__( 'Add Images', 'elementor' ), 95 'type' => Controls_Manager::GALLERY, 96 'show_label' => false, 97 'dynamic' => [ 98 'active' => true, 99 ], 100 ] 101 ); 102 103 $this->add_group_control( 104 Group_Control_Image_Size::get_type(), 105 [ 106 'name' => 'thumbnail', // Usage: `{name}_size` and `{name}_custom_dimension`, in this case `thumbnail_size` and `thumbnail_custom_dimension`. 107 'exclude' => [ 'custom' ], 108 'separator' => 'none', 109 ] 110 ); 111 112 $gallery_columns = range( 1, 10 ); 113 $gallery_columns = array_combine( $gallery_columns, $gallery_columns ); 114 115 $this->add_control( 116 'gallery_columns', 117 [ 118 'label' => esc_html__( 'Columns', 'elementor' ), 119 'type' => Controls_Manager::SELECT, 120 'default' => 4, 121 'options' => $gallery_columns, 122 ] 123 ); 124 125 $this->add_control( 126 'gallery_link', 127 [ 128 'label' => esc_html__( 'Link', 'elementor' ), 129 'type' => Controls_Manager::SELECT, 130 'default' => 'file', 131 'options' => [ 132 'file' => esc_html__( 'Media File', 'elementor' ), 133 'attachment' => esc_html__( 'Attachment Page', 'elementor' ), 134 'none' => esc_html__( 'None', 'elementor' ), 135 ], 136 ] 137 ); 138 139 $this->add_control( 140 'open_lightbox', 141 [ 142 'label' => esc_html__( 'Lightbox', 'elementor' ), 143 'type' => Controls_Manager::SELECT, 144 'default' => 'default', 145 'options' => [ 146 'default' => esc_html__( 'Default', 'elementor' ), 147 'yes' => esc_html__( 'Yes', 'elementor' ), 148 'no' => esc_html__( 'No', 'elementor' ), 149 ], 150 'condition' => [ 151 'gallery_link' => 'file', 152 ], 153 ] 154 ); 155 156 $this->add_control( 157 'gallery_rand', 158 [ 159 'label' => esc_html__( 'Order By', 'elementor' ), 160 'type' => Controls_Manager::SELECT, 161 'options' => [ 162 '' => esc_html__( 'Default', 'elementor' ), 163 'rand' => esc_html__( 'Random', 'elementor' ), 164 ], 165 'default' => '', 166 ] 167 ); 168 169 $this->add_control( 170 'view', 171 [ 172 'label' => esc_html__( 'View', 'elementor' ), 173 'type' => Controls_Manager::HIDDEN, 174 'default' => 'traditional', 175 ] 176 ); 177 178 $this->end_controls_section(); 179 180 $this->start_controls_section( 181 'section_gallery_images', 182 [ 183 'label' => esc_html__( 'Images', 'elementor' ), 184 'tab' => Controls_Manager::TAB_STYLE, 185 ] 186 ); 187 188 $this->add_control( 189 'image_spacing', 190 [ 191 'label' => esc_html__( 'Spacing', 'elementor' ), 192 'type' => Controls_Manager::SELECT, 193 'options' => [ 194 '' => esc_html__( 'Default', 'elementor' ), 195 'custom' => esc_html__( 'Custom', 'elementor' ), 196 ], 197 'prefix_class' => 'gallery-spacing-', 198 'default' => '', 199 ] 200 ); 201 202 $columns_margin = is_rtl() ? '0 0 -{{SIZE}}{{UNIT}} -{{SIZE}}{{UNIT}};' : '0 -{{SIZE}}{{UNIT}} -{{SIZE}}{{UNIT}} 0;'; 203 $columns_padding = is_rtl() ? '0 0 {{SIZE}}{{UNIT}} {{SIZE}}{{UNIT}};' : '0 {{SIZE}}{{UNIT}} {{SIZE}}{{UNIT}} 0;'; 204 205 $this->add_control( 206 'image_spacing_custom', 207 [ 208 'label' => esc_html__( 'Image Spacing', 'elementor' ), 209 'type' => Controls_Manager::SLIDER, 210 'show_label' => false, 211 'range' => [ 212 'px' => [ 213 'max' => 100, 214 ], 215 ], 216 'default' => [ 217 'size' => 15, 218 ], 219 'selectors' => [ 220 '{{WRAPPER}} .gallery-item' => 'padding:' . $columns_padding, 221 '{{WRAPPER}} .gallery' => 'margin: ' . $columns_margin, 222 ], 223 'condition' => [ 224 'image_spacing' => 'custom', 225 ], 226 ] 227 ); 228 229 $this->add_group_control( 230 Group_Control_Border::get_type(), 231 [ 232 'name' => 'image_border', 233 'selector' => '{{WRAPPER}} .gallery-item img', 234 'separator' => 'before', 235 ] 236 ); 237 238 $this->add_control( 239 'image_border_radius', 240 [ 241 'label' => esc_html__( 'Border Radius', 'elementor' ), 242 'type' => Controls_Manager::DIMENSIONS, 243 'size_units' => [ 'px', '%' ], 244 'selectors' => [ 245 '{{WRAPPER}} .gallery-item img' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', 246 ], 247 ] 248 ); 249 250 $this->end_controls_section(); 251 252 $this->start_controls_section( 253 'section_caption', 254 [ 255 'label' => esc_html__( 'Caption', 'elementor' ), 256 'tab' => Controls_Manager::TAB_STYLE, 257 ] 258 ); 259 260 $this->add_control( 261 'gallery_display_caption', 262 [ 263 'label' => esc_html__( 'Display', 'elementor' ), 264 'type' => Controls_Manager::SELECT, 265 'default' => '', 266 'options' => [ 267 '' => esc_html__( 'Show', 'elementor' ), 268 'none' => esc_html__( 'Hide', 'elementor' ), 269 ], 270 'selectors' => [ 271 '{{WRAPPER}} .gallery-item .gallery-caption' => 'display: {{VALUE}};', 272 ], 273 ] 274 ); 275 276 $this->add_control( 277 'align', 278 [ 279 'label' => esc_html__( 'Alignment', 'elementor' ), 280 'type' => Controls_Manager::CHOOSE, 281 'options' => [ 282 'left' => [ 283 'title' => esc_html__( 'Left', 'elementor' ), 284 'icon' => 'eicon-text-align-left', 285 ], 286 'center' => [ 287 'title' => esc_html__( 'Center', 'elementor' ), 288 'icon' => 'eicon-text-align-center', 289 ], 290 'right' => [ 291 'title' => esc_html__( 'Right', 'elementor' ), 292 'icon' => 'eicon-text-align-right', 293 ], 294 'justify' => [ 295 'title' => esc_html__( 'Justified', 'elementor' ), 296 'icon' => 'eicon-text-align-justify', 297 ], 298 ], 299 'default' => 'center', 300 'selectors' => [ 301 '{{WRAPPER}} .gallery-item .gallery-caption' => 'text-align: {{VALUE}};', 302 ], 303 'condition' => [ 304 'gallery_display_caption' => '', 305 ], 306 ] 307 ); 308 309 $this->add_control( 310 'text_color', 311 [ 312 'label' => esc_html__( 'Text Color', 'elementor' ), 313 'type' => Controls_Manager::COLOR, 314 'default' => '', 315 'selectors' => [ 316 '{{WRAPPER}} .gallery-item .gallery-caption' => 'color: {{VALUE}};', 317 ], 318 'condition' => [ 319 'gallery_display_caption' => '', 320 ], 321 ] 322 ); 323 324 $this->add_group_control( 325 Group_Control_Typography::get_type(), 326 [ 327 'name' => 'typography', 328 'global' => [ 329 'default' => Global_Typography::TYPOGRAPHY_ACCENT, 330 ], 331 'selector' => '{{WRAPPER}} .gallery-item .gallery-caption', 332 'condition' => [ 333 'gallery_display_caption' => '', 334 ], 335 ] 336 ); 337 338 $this->add_group_control( 339 Group_Control_Text_Shadow::get_type(), 340 [ 341 'name' => 'caption_shadow', 342 'selector' => '{{WRAPPER}} .gallery-item .gallery-caption', 343 ] 344 ); 345 346 $this->end_controls_section(); 347 } 348 349 /** 350 * Render image gallery widget output on the frontend. 351 * 352 * Written in PHP and used to generate the final HTML. 353 * 354 * @since 1.0.0 355 * @access protected 356 */ 357 protected function render() { 358 $settings = $this->get_settings_for_display(); 359 360 if ( ! $settings['wp_gallery'] ) { 361 return; 362 } 363 364 $ids = wp_list_pluck( $settings['wp_gallery'], 'id' ); 365 366 $this->add_render_attribute( 'shortcode', 'ids', implode( ',', $ids ) ); 367 $this->add_render_attribute( 'shortcode', 'size', $settings['thumbnail_size'] ); 368 369 if ( $settings['gallery_columns'] ) { 370 $this->add_render_attribute( 'shortcode', 'columns', $settings['gallery_columns'] ); 371 } 372 373 if ( $settings['gallery_link'] ) { 374 $this->add_render_attribute( 'shortcode', 'link', $settings['gallery_link'] ); 375 } 376 377 if ( ! empty( $settings['gallery_rand'] ) ) { 378 $this->add_render_attribute( 'shortcode', 'orderby', $settings['gallery_rand'] ); 379 } 380 ?> 381 <div class="elementor-image-gallery"> 382 <?php 383 add_filter( 'wp_get_attachment_link', [ $this, 'add_lightbox_data_to_image_link' ], 10, 2 ); 384 385 echo do_shortcode( '[gallery ' . $this->get_render_attribute_string( 'shortcode' ) . ']' ); 386 387 remove_filter( 'wp_get_attachment_link', [ $this, 'add_lightbox_data_to_image_link' ] ); 388 ?> 389 </div> 390 <?php 391 } 392 }