PluginProbe
Elementor Website Builder – more than just a page builder / 4.2.2
Elementor Website Builder – more than just a page builder v4.2.2
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / includes / controls / groups / image-size.php

image-size.php in Elementor Website Builder – more than just a page builder 4.2.2, at includes/controls/groups/image-size.php

406 lines 11.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit; // Exit if accessed directly.
6 }
7
8 /**
9 * Elementor image size control.
10 *
11 * A base control for creating image size control. Displays input fields to define
12 * one of the default image sizes (thumbnail, medium, medium_large, large) or custom
13 * image dimensions.
14 *
15 * @since 1.0.0
16 */
17 class Group_Control_Image_Size extends Group_Control_Base {
18
19 /**
20 * Fields.
21 *
22 * Holds all the image size control fields.
23 *
24 * @since 1.2.2
25 * @access protected
26 * @static
27 *
28 * @var array Image size control fields.
29 */
30 protected static $fields;
31
32 /**
33 * Get image size control type.
34 *
35 * Retrieve the control type, in this case `image-size`.
36 *
37 * @since 1.0.0
38 * @access public
39 * @static
40 *
41 * @return string Control type.
42 */
43 public static function get_type() {
44 return 'image-size';
45 }
46
47 /**
48 * Get attachment image HTML.
49 *
50 * Retrieve the attachment image HTML code.
51 *
52 * Note that some widgets use the same key for the media control that allows
53 * the image selection and for the image size control that allows the user
54 * to select the image size, in this case the third parameter should be null
55 * or the same as the second parameter. But when the widget uses different
56 * keys for the media control and the image size control, when calling this
57 * method you should pass the keys.
58 *
59 * @since 1.0.0
60 * @access public
61 * @static
62 *
63 * @param array $settings Control settings.
64 * @param string $image_size_key Optional. Settings key for image size.
65 * Default is `image`.
66 * @param string $image_key Optional. Settings key for image. Default
67 * is null. If not defined uses image size key
68 * as the image key.
69 *
70 * @return string Image HTML.
71 */
72 public static function get_attachment_image_html( $settings, $image_size_key = 'image', $image_key = null ) {
73 if ( ! $image_key ) {
74 $image_key = $image_size_key;
75 }
76
77 $image = $settings[ $image_key ];
78
79 // Old version of image settings.
80 if ( ! isset( $settings[ $image_size_key . '_size' ] ) ) {
81 $settings[ $image_size_key . '_size' ] = '';
82 }
83
84 $size = $settings[ $image_size_key . '_size' ];
85
86 $image_class = ! empty( $settings['hover_animation'] ) ? 'elementor-animation-' . $settings['hover_animation'] : '';
87
88 $html = '';
89
90 // If is the new version - with image size.
91 $image_sizes = get_intermediate_image_sizes();
92
93 $image_sizes[] = 'full';
94
95 if ( ! empty( $image['id'] ) && ! wp_attachment_is_image( $image['id'] ) ) {
96 $image['id'] = '';
97 }
98
99 $is_static_render_mode = Plugin::$instance->frontend->is_static_render_mode();
100
101 // On static mode don't use WP responsive images.
102 if ( ! empty( $image['id'] ) && in_array( $size, $image_sizes ) && ! $is_static_render_mode ) {
103 $image_class .= " attachment-$size size-$size wp-image-{$image['id']}";
104 $image_attr = [
105 'class' => trim( $image_class ),
106 ];
107
108 $html .= wp_get_attachment_image( $image['id'], $size, false, $image_attr );
109 } else {
110 $image_src = self::get_attachment_image_src( $image['id'], $image_size_key, $settings );
111
112 if ( ! $image_src && isset( $image['url'] ) ) {
113 $image_src = $image['url'];
114 }
115
116 if ( ! empty( $image_src ) ) {
117 $image_class_html = ! empty( $image_class ) ? ' class="' . esc_attr( $image_class ) . '"' : '';
118
119 $html .= sprintf(
120 '<img src="%1$s" title="%2$s" alt="%3$s"%4$s loading="lazy" />',
121 esc_url( $image_src ),
122 esc_attr( Control_Media::get_image_title( $image ) ),
123 esc_attr( Control_Media::get_image_alt( $image ) ),
124 $image_class_html
125 );
126 }
127 }
128
129 /**
130 * Get Attachment Image HTML
131 *
132 * Filters the Attachment Image HTML
133 *
134 * @since 2.4.0
135 * @param string $html the attachment image HTML string
136 * @param array $settings Control settings.
137 * @param string $image_size_key Optional. Settings key for image size.
138 * Default is `image`.
139 * @param string $image_key Optional. Settings key for image. Default
140 * is null. If not defined uses image size key
141 * as the image key.
142 */
143 return apply_filters( 'elementor/image_size/get_attachment_image_html', $html, $settings, $image_size_key, $image_key );
144 }
145
146 /**
147 * Safe print attachment image HTML.
148 *
149 * @uses get_attachment_image_html.
150 *
151 * @access public
152 * @static
153 *
154 * @param array $settings Control settings.
155 * @param string $image_size_key Optional. Settings key for image size.
156 * Default is `image`.
157 * @param string $image_key Optional. Settings key for image. Default
158 * is null. If not defined uses image size key
159 * as the image key.
160 */
161 public static function print_attachment_image_html( array $settings, $image_size_key = 'image', $image_key = null ) {
162 Utils::print_wp_kses_extended( self::get_attachment_image_html( $settings, $image_size_key, $image_key ), [ 'image' ] );
163 }
164
165 /**
166 * Get all image sizes.
167 *
168 * Retrieve available image sizes with data like `width`, `height` and `crop`.
169 *
170 * @since 1.0.0
171 * @access public
172 * @static
173 *
174 * @return array An array of available image sizes.
175 */
176 public static function get_all_image_sizes() {
177 global $_wp_additional_image_sizes;
178
179 $default_image_sizes = [ 'thumbnail', 'medium', 'medium_large', 'large' ];
180
181 $image_sizes = [];
182
183 foreach ( $default_image_sizes as $size ) {
184 $image_sizes[ $size ] = [
185 'width' => (int) get_option( $size . '_size_w' ),
186 'height' => (int) get_option( $size . '_size_h' ),
187 'crop' => (bool) get_option( $size . '_crop' ),
188 ];
189 }
190
191 if ( $_wp_additional_image_sizes ) {
192 $image_sizes = array_merge( $image_sizes, $_wp_additional_image_sizes );
193 }
194
195 /** This filter is documented in wp-admin/includes/media.php */
196 return apply_filters( 'image_size_names_choose', $image_sizes );
197 }
198
199 /**
200 * Get attachment image src.
201 *
202 * Retrieve the attachment image source URL.
203 *
204 * @since 1.0.0
205 * @access public
206 * @static
207 *
208 * @param string $attachment_id The attachment ID.
209 * @param string $image_size_key Settings key for image size.
210 * @param array $settings Control settings.
211 *
212 * @return string Attachment image source URL.
213 */
214 public static function get_attachment_image_src( $attachment_id, $image_size_key, array $settings ) {
215 if ( empty( $attachment_id ) ) {
216 return false;
217 }
218
219 $size = $settings[ $image_size_key . '_size' ];
220
221 if ( 'custom' !== $size ) {
222 $attachment_size = $size;
223 } else {
224 // Use BFI_Thumb script
225 // TODO: Please rewrite this code.
226 require_once ELEMENTOR_PATH . 'includes/libraries/bfi-thumb/bfi-thumb.php';
227
228 $custom_dimension = $settings[ $image_size_key . '_custom_dimension' ];
229
230 $attachment_size = [
231 // Defaults sizes
232 0 => null, // Width.
233 1 => null, // Height.
234
235 'bfi_thumb' => true,
236 'crop' => true,
237 ];
238
239 $has_custom_size = false;
240 if ( ! empty( $custom_dimension['width'] ) ) {
241 $has_custom_size = true;
242 $attachment_size[0] = $custom_dimension['width'];
243 }
244
245 if ( ! empty( $custom_dimension['height'] ) ) {
246 $has_custom_size = true;
247 $attachment_size[1] = $custom_dimension['height'];
248 }
249
250 if ( ! $has_custom_size ) {
251 $attachment_size = 'full';
252 }
253 }
254
255 $image_src = wp_get_attachment_image_src( $attachment_id, $attachment_size );
256
257 if ( empty( $image_src[0] ) && 'thumbnail' !== $attachment_size ) {
258 $image_src = wp_get_attachment_image_src( $attachment_id );
259 }
260
261 return ! empty( $image_src[0] ) ? $image_src[0] : '';
262 }
263
264 /**
265 * Get child default arguments.
266 *
267 * Retrieve the default arguments for all the child controls for a specific group
268 * control.
269 *
270 * @since 1.2.2
271 * @access protected
272 *
273 * @return array Default arguments for all the child controls.
274 */
275 protected function get_child_default_args() {
276 return [
277 'include' => [],
278 'exclude' => [],
279 ];
280 }
281
282 /**
283 * Init fields.
284 *
285 * Initialize image size control fields.
286 *
287 * @since 1.2.2
288 * @access protected
289 *
290 * @return array Control fields.
291 */
292 protected function init_fields() {
293 $fields = [];
294
295 $fields['size'] = [
296 'label' => esc_html__( 'Image Resolution', 'elementor' ),
297 'type' => Controls_Manager::SELECT,
298 ];
299
300 $fields['custom_dimension'] = [
301 'label' => esc_html__( 'Image Dimension', 'elementor' ),
302 'type' => Controls_Manager::IMAGE_DIMENSIONS,
303 'description' => esc_html__( 'You can crop the original image size to any custom size. You can also set a single value for height or width in order to keep the original size ratio.', 'elementor' ),
304 'condition' => [
305 'size' => 'custom',
306 ],
307 ];
308
309 return $fields;
310 }
311
312 /**
313 * Prepare fields.
314 *
315 * Process image size control fields before adding them to `add_control()`.
316 *
317 * @since 1.2.2
318 * @access protected
319 *
320 * @param array $fields Image size control fields.
321 *
322 * @return array Processed fields.
323 */
324 protected function prepare_fields( $fields ) {
325 $image_sizes = $this->get_image_sizes();
326
327 $args = $this->get_args();
328
329 if ( ! empty( $args['default'] ) && isset( $image_sizes[ $args['default'] ] ) ) {
330 $default_value = $args['default'];
331 } else {
332 // Get the first item for default value.
333 $default_value = array_keys( $image_sizes );
334 $default_value = array_shift( $default_value );
335 }
336
337 $fields['size']['options'] = $image_sizes;
338
339 $fields['size']['default'] = $default_value;
340
341 if ( ! isset( $image_sizes['custom'] ) ) {
342 unset( $fields['custom_dimension'] );
343 }
344
345 return parent::prepare_fields( $fields );
346 }
347
348 /**
349 * Get image sizes.
350 *
351 * Retrieve available image sizes after filtering `include` and `exclude` arguments.
352 *
353 * @since 2.0.0
354 * @access private
355 *
356 * @return array Filtered image sizes.
357 */
358 private function get_image_sizes() {
359 $wp_image_sizes = self::get_all_image_sizes();
360
361 $args = $this->get_args();
362
363 if ( $args['include'] ) {
364 $wp_image_sizes = array_intersect_key( $wp_image_sizes, array_flip( $args['include'] ) );
365 } elseif ( $args['exclude'] ) {
366 $wp_image_sizes = array_diff_key( $wp_image_sizes, array_flip( $args['exclude'] ) );
367 }
368
369 $image_sizes = [];
370
371 foreach ( $wp_image_sizes as $size_key => $size_attributes ) {
372 $control_title = ucwords( str_replace( '_', ' ', $size_key ) );
373 if ( is_array( $size_attributes ) ) {
374 $control_title .= sprintf( ' - %d x %d', $size_attributes['width'], $size_attributes['height'] );
375 }
376
377 $image_sizes[ $size_key ] = $control_title;
378 }
379
380 $image_sizes['full'] = esc_html__( 'Full', 'elementor' );
381
382 if ( ! empty( $args['include']['custom'] ) || ! in_array( 'custom', $args['exclude'] ) ) {
383 $image_sizes['custom'] = esc_html__( 'Custom', 'elementor' );
384 }
385
386 return $image_sizes;
387 }
388
389 /**
390 * Get default options.
391 *
392 * Retrieve the default options of the image size control. Used to return the
393 * default options while initializing the image size control.
394 *
395 * @since 1.9.0
396 * @access protected
397 *
398 * @return array Default image size control options.
399 */
400 protected function get_default_options() {
401 return [
402 'popover' => false,
403 ];
404 }
405 }
406