PluginProbe
Elementor Website Builder – more than just a page builder / 3.0.12
Elementor Website Builder – more than just a page builder v3.0.12
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 3.0.12, at includes/controls/groups/image-size.php

382 lines 10.2 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";
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="' . $image_class . '"' : '';
118
119 $html .= sprintf( '<img src="%s" title="%s" alt="%s"%s />', esc_attr( $image_src ), Control_Media::get_image_title( $image ), Control_Media::get_image_alt( $image ), $image_class_html );
120 }
121 }
122
123 /**
124 * Get Attachment Image HTML
125 *
126 * Filters the Attachment Image HTML
127 *
128 * @since 2.4.0
129 * @param string $html the attachment image HTML string
130 * @param array $settings Control settings.
131 * @param string $image_size_key Optional. Settings key for image size.
132 * Default is `image`.
133 * @param string $image_key Optional. Settings key for image. Default
134 * is null. If not defined uses image size key
135 * as the image key.
136 */
137 return apply_filters( 'elementor/image_size/get_attachment_image_html', $html, $settings, $image_size_key, $image_key );
138 }
139
140 /**
141 * Get all image sizes.
142 *
143 * Retrieve available image sizes with data like `width`, `height` and `crop`.
144 *
145 * @since 1.0.0
146 * @access public
147 * @static
148 *
149 * @return array An array of available image sizes.
150 */
151 public static function get_all_image_sizes() {
152 global $_wp_additional_image_sizes;
153
154 $default_image_sizes = [ 'thumbnail', 'medium', 'medium_large', 'large' ];
155
156 $image_sizes = [];
157
158 foreach ( $default_image_sizes as $size ) {
159 $image_sizes[ $size ] = [
160 'width' => (int) get_option( $size . '_size_w' ),
161 'height' => (int) get_option( $size . '_size_h' ),
162 'crop' => (bool) get_option( $size . '_crop' ),
163 ];
164 }
165
166 if ( $_wp_additional_image_sizes ) {
167 $image_sizes = array_merge( $image_sizes, $_wp_additional_image_sizes );
168 }
169
170 /** This filter is documented in wp-admin/includes/media.php */
171 return apply_filters( 'image_size_names_choose', $image_sizes );
172 }
173
174 /**
175 * Get attachment image src.
176 *
177 * Retrieve the attachment image source URL.
178 *
179 * @since 1.0.0
180 * @access public
181 * @static
182 *
183 * @param string $attachment_id The attachment ID.
184 * @param string $image_size_key Settings key for image size.
185 * @param array $settings Control settings.
186 *
187 * @return string Attachment image source URL.
188 */
189 public static function get_attachment_image_src( $attachment_id, $image_size_key, array $settings ) {
190 if ( empty( $attachment_id ) ) {
191 return false;
192 }
193
194 $size = $settings[ $image_size_key . '_size' ];
195
196 if ( 'custom' !== $size ) {
197 $attachment_size = $size;
198 } else {
199 // Use BFI_Thumb script
200 // TODO: Please rewrite this code.
201 require_once ELEMENTOR_PATH . 'includes/libraries/bfi-thumb/bfi-thumb.php';
202
203 $custom_dimension = $settings[ $image_size_key . '_custom_dimension' ];
204
205 $attachment_size = [
206 // Defaults sizes
207 0 => null, // Width.
208 1 => null, // Height.
209
210 'bfi_thumb' => true,
211 'crop' => true,
212 ];
213
214 $has_custom_size = false;
215 if ( ! empty( $custom_dimension['width'] ) ) {
216 $has_custom_size = true;
217 $attachment_size[0] = $custom_dimension['width'];
218 }
219
220 if ( ! empty( $custom_dimension['height'] ) ) {
221 $has_custom_size = true;
222 $attachment_size[1] = $custom_dimension['height'];
223 }
224
225 if ( ! $has_custom_size ) {
226 $attachment_size = 'full';
227 }
228 }
229
230 $image_src = wp_get_attachment_image_src( $attachment_id, $attachment_size );
231
232 if ( empty( $image_src[0] ) && 'thumbnail' !== $attachment_size ) {
233 $image_src = wp_get_attachment_image_src( $attachment_id );
234 }
235
236 return ! empty( $image_src[0] ) ? $image_src[0] : '';
237 }
238
239 /**
240 * Get child default arguments.
241 *
242 * Retrieve the default arguments for all the child controls for a specific group
243 * control.
244 *
245 * @since 1.2.2
246 * @access protected
247 *
248 * @return array Default arguments for all the child controls.
249 */
250 protected function get_child_default_args() {
251 return [
252 'include' => [],
253 'exclude' => [],
254 ];
255 }
256
257 /**
258 * Init fields.
259 *
260 * Initialize image size control fields.
261 *
262 * @since 1.2.2
263 * @access protected
264 *
265 * @return array Control fields.
266 */
267 protected function init_fields() {
268 $fields = [];
269
270 $fields['size'] = [
271 'label' => _x( 'Image Size', 'Image Size Control', 'elementor' ),
272 'type' => Controls_Manager::SELECT,
273 ];
274
275 $fields['custom_dimension'] = [
276 'label' => _x( 'Image Dimension', 'Image Size Control', 'elementor' ),
277 'type' => Controls_Manager::IMAGE_DIMENSIONS,
278 'description' => __( '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' ),
279 'condition' => [
280 'size' => 'custom',
281 ],
282 'separator' => 'none',
283 ];
284
285 return $fields;
286 }
287
288 /**
289 * Prepare fields.
290 *
291 * Process image size control fields before adding them to `add_control()`.
292 *
293 * @since 1.2.2
294 * @access protected
295 *
296 * @param array $fields Image size control fields.
297 *
298 * @return array Processed fields.
299 */
300 protected function prepare_fields( $fields ) {
301 $image_sizes = $this->get_image_sizes();
302
303 $args = $this->get_args();
304
305 if ( ! empty( $args['default'] ) && isset( $image_sizes[ $args['default'] ] ) ) {
306 $default_value = $args['default'];
307 } else {
308 // Get the first item for default value.
309 $default_value = array_keys( $image_sizes );
310 $default_value = array_shift( $default_value );
311 }
312
313 $fields['size']['options'] = $image_sizes;
314
315 $fields['size']['default'] = $default_value;
316
317 if ( ! isset( $image_sizes['custom'] ) ) {
318 unset( $fields['custom_dimension'] );
319 }
320
321 return parent::prepare_fields( $fields );
322 }
323
324 /**
325 * Get image sizes.
326 *
327 * Retrieve available image sizes after filtering `include` and `exclude` arguments.
328 *
329 * @since 2.0.0
330 * @access private
331 *
332 * @return array Filtered image sizes.
333 */
334 private function get_image_sizes() {
335 $wp_image_sizes = self::get_all_image_sizes();
336
337 $args = $this->get_args();
338
339 if ( $args['include'] ) {
340 $wp_image_sizes = array_intersect_key( $wp_image_sizes, array_flip( $args['include'] ) );
341 } elseif ( $args['exclude'] ) {
342 $wp_image_sizes = array_diff_key( $wp_image_sizes, array_flip( $args['exclude'] ) );
343 }
344
345 $image_sizes = [];
346
347 foreach ( $wp_image_sizes as $size_key => $size_attributes ) {
348 $control_title = ucwords( str_replace( '_', ' ', $size_key ) );
349 if ( is_array( $size_attributes ) ) {
350 $control_title .= sprintf( ' - %d x %d', $size_attributes['width'], $size_attributes['height'] );
351 }
352
353 $image_sizes[ $size_key ] = $control_title;
354 }
355
356 $image_sizes['full'] = _x( 'Full', 'Image Size Control', 'elementor' );
357
358 if ( ! empty( $args['include']['custom'] ) || ! in_array( 'custom', $args['exclude'] ) ) {
359 $image_sizes['custom'] = _x( 'Custom', 'Image Size Control', 'elementor' );
360 }
361
362 return $image_sizes;
363 }
364
365 /**
366 * Get default options.
367 *
368 * Retrieve the default options of the image size control. Used to return the
369 * default options while initializing the image size control.
370 *
371 * @since 1.9.0
372 * @access protected
373 *
374 * @return array Default image size control options.
375 */
376 protected function get_default_options() {
377 return [
378 'popover' => false,
379 ];
380 }
381 }
382