PluginProbe
Elementor Website Builder – more than just a page builder / 3.19.2
Elementor Website Builder – more than just a page builder v3.19.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 / media.php

media.php in Elementor Website Builder – more than just a page builder 3.19.2, at includes/controls/media.php

457 lines 13.4 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 use Elementor\Core\Files\Uploads_Manager;
5 use Elementor\Core\Utils\Hints;
6 use Elementor\Modules\DynamicTags\Module as TagsModule;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 /**
13 * Elementor media control.
14 *
15 * A base control for creating a media chooser control. Based on the WordPress
16 * media library. Used to select an image from the WordPress media library.
17 *
18 * @since 1.0.0
19 */
20 class Control_Media extends Control_Base_Multiple {
21
22 /**
23 * Get media control type.
24 *
25 * Retrieve the control type, in this case `media`.
26 *
27 * @since 1.0.0
28 * @access public
29 *
30 * @return string Control type.
31 */
32 public function get_type() {
33 return 'media';
34 }
35
36 /**
37 * Get media control default values.
38 *
39 * Retrieve the default value of the media control. Used to return the default
40 * values while initializing the media control.
41 *
42 * @since 1.0.0
43 * @access public
44 *
45 * @return array Control default value.
46 */
47 public function get_default_value() {
48 return [
49 'url' => '',
50 'id' => '',
51 'size' => '',
52 ];
53 }
54
55 /**
56 * Import media images.
57 *
58 * Used to import media control files from external sites while importing
59 * Elementor template JSON file, and replacing the old data.
60 *
61 * @since 1.0.0
62 * @access public
63 *
64 * @param array $settings Control settings
65 *
66 * @return array Control settings.
67 */
68 public function on_import( $settings ) {
69 if ( empty( $settings['url'] ) ) {
70 return $settings;
71 }
72
73 $settings = Plugin::$instance->templates_manager->get_import_images_instance()->import( $settings );
74
75 if ( ! $settings ) {
76 $settings = [
77 'id' => '',
78 'url' => Utils::get_placeholder_image_src(),
79 ];
80 }
81
82 return $settings;
83 }
84
85 /**
86 * Support SVG and JSON Import
87 *
88 * Called by the 'upload_mimes' filter. Adds SVG and JSON mime types to the list of WordPress' allowed mime types.
89 *
90 * @since 3.4.6
91 * @deprecated 3.5.0
92 *
93 * @param $mimes
94 * @return mixed
95 */
96 public function support_svg_and_json_import( $mimes ) {
97 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __METHOD__, '3.5.0' );
98
99 return $mimes;
100 }
101
102 /**
103 * Enqueue media control scripts and styles.
104 *
105 * Used to register and enqueue custom scripts and styles used by the media
106 * control.
107 *
108 * @since 1.0.0
109 * @access public
110 */
111 public function enqueue() {
112 global $wp_version;
113
114 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
115 wp_enqueue_media();
116
117 wp_enqueue_style(
118 'media',
119 admin_url( '/css/media' . $suffix . '.css' ),
120 [],
121 $wp_version
122 );
123
124 wp_register_script(
125 'image-edit',
126 '/wp-admin/js/image-edit' . $suffix . '.js',
127 [
128 'jquery',
129 'json2',
130 'imgareaselect',
131 ],
132 $wp_version,
133 true
134 );
135
136 wp_enqueue_script( 'image-edit' );
137 }
138
139 /**
140 * Render media control output in the editor.
141 *
142 * Used to generate the control HTML in the editor using Underscore JS
143 * template. The variables for the class are available using `data` JS
144 * object.
145 *
146 * @since 1.0.0
147 * @access public
148 */
149 public function content_template() {
150 ?>
151 <#
152 // For BC.
153 if ( data.media_type ) {
154 data.media_types = [ data.media_type ];
155 }
156
157 if ( data.should_include_svg_inline_option ) {
158 data.media_types.push( 'svg' );
159 }
160
161 // Determine if the current media type is viewable.
162 const isViewable = () => {
163 const viewable = [
164 'image',
165 'video',
166 'svg',
167 ];
168
169 // Make sure that all media types are viewable.
170 return data.media_types.every( ( type ) => viewable.includes( type ) );
171 };
172
173 // Get the preview type for the current media type.
174 const getPreviewType = () => {
175 if ( data.media_types.includes( 'video' ) ) {
176 return 'video';
177 }
178
179 if ( data.media_types.includes( 'image' ) || data.media_types.includes( 'svg' ) ) {
180 return 'image';
181 }
182
183 return 'none';
184 }
185
186 // Retrieve a button label by media type.
187 const getButtonLabel = ( mediaType ) => {
188 switch( mediaType ) {
189 case 'image':
190 return '<?php esc_html_e( 'Choose Image', 'elementor' ); ?>';
191
192 case 'video':
193 return '<?php esc_html_e( 'Choose Video', 'elementor' ); ?>';
194
195 case 'svg':
196 return '<?php esc_html_e( 'Choose SVG', 'elementor' ); ?>';
197
198 default:
199 return '<?php esc_html_e( 'Choose File', 'elementor' ); ?>';
200 }
201 }
202 #>
203 <div class="elementor-control-field elementor-control-media">
204 <label class="elementor-control-title">{{{ data.label }}}</label>
205 <#
206 if ( isViewable() ) {
207 let inputWrapperClasses = 'elementor-control-input-wrapper';
208
209 if ( ! data.label_block ) {
210 inputWrapperClasses += ' elementor-control-unit-5';
211 }
212 #>
213 <div class="{{{ inputWrapperClasses }}}">
214 <div class="elementor-control-media__content elementor-control-tag-area elementor-control-preview-area">
215 <div class="elementor-control-media-area">
216 <div class="elementor-control-media__remove elementor-control-media__content__remove" title="<?php echo esc_attr__( 'Remove', 'elementor' ); ?>">
217 <i class="eicon-trash-o" aria-hidden="true"></i>
218 <span class="elementor-screen-only"><?php echo esc_html__( 'Remove', 'elementor' ); ?></span>
219 </div>
220 <#
221 switch( getPreviewType() ) {
222 case 'image':
223 #>
224 <div class="elementor-control-media__preview"></div>
225 <#
226 break;
227
228 case 'video':
229 #>
230 <video class="elementor-control-media-video" preload="metadata"></video>
231 <i class="eicon-video-camera" aria-hidden="true"></i>
232 <#
233 break;
234 }
235 #>
236 </div>
237 <div class="elementor-control-media-upload-button elementor-control-media__content__upload-button">
238 <i class="eicon-plus-circle" aria-hidden="true"></i>
239 <span class="elementor-screen-only"><?php echo esc_html__( 'Add', 'elementor' ); ?></span>
240 </div>
241 <div class="elementor-control-media__tools elementor-control-dynamic-switcher-wrapper">
242 <#
243 data.media_types.forEach( ( type ) => {
244 #>
245 <div class="elementor-control-media__tool elementor-control-media__replace" data-media-type="{{{ type }}}">{{{ getButtonLabel( type ) }}}</div>
246 <#
247 } );
248 #>
249 </div>
250 </div>
251 <?php if ( ! Hints::should_display_hint( 'image-optimization-once' ) && ! Hints::should_display_hint( 'image-optimization' ) ) { ?>
252 <div class="elementor-control-media__warnings elementor-descriptor" role="alert" style="display: none;">
253 <?php
254 Hints::get_notice_template( [
255 'type' => 'warning',
256 'content' => __( 'This image doesn’t contain ALT text - which is necessary for accessibility and SEO.', 'elementor' ),
257 'icon' => true,
258 ] );
259 ?>
260 </div>
261 <?php } ?>
262 <?php if ( Hints::should_display_hint( 'image-optimization-once' ) || Hints::should_display_hint( 'image-optimization' ) ) { ?>
263 <div class="elementor-control-media__promotions elementor-descriptor" role="alert" style="display: none;">
264 <?php
265 $once_dismissed = Hints::is_dismissed( 'image-optimization-once' );
266 $content = $once_dismissed ?
267 __( 'Whoa! This image is quite large and might slow things down. Use Image Optimizer to reduce size without losing quality.', 'elementor' ) :
268 __( "Don't let unoptimized images be the downfall of your site's performance. Use Image Optimizer!", 'elementor' );
269 $dismissible = $once_dismissed ? 'image_optimizer_hint' : 'image-optimization-once';
270 Hints::get_notice_template( [
271 'display' => ! $once_dismissed,
272 'type' => $once_dismissed ? 'warning' : 'info',
273 'content' => $content,
274 'icon' => true,
275 'dismissible' => $dismissible,
276 'button_text' => Hints::is_plugin_installed( 'image-optimization' ) ? __( 'Activate Plugin', 'elementor' ) : __( 'Install Plugin', 'elementor' ),
277 'button_event' => $dismissible,
278 'button_data' => [
279 'action_url' => Hints::get_plugin_action_url( 'image-optimization' ),
280 ],
281 ] ); ?>
282 </div>
283 <?php } ?>
284 </div>
285 <# } /* endif isViewable() */ else { #>
286 <div class="elementor-control-media__file elementor-control-preview-area">
287 <div class="elementor-control-media__file__content">
288 <div class="elementor-control-media__file__content__label"><?php echo esc_html__( 'Click the media icon to upload file', 'elementor' ); ?></div>
289 <div class="elementor-control-media__file__content__info">
290 <div class="elementor-control-media__file__content__info__icon">
291 <i class="eicon-document-file"></i>
292 </div>
293 <div class="elementor-control-media__file__content__info__name"></div>
294 </div>
295 </div>
296 <div class="elementor-control-media__file__controls">
297 <div class="elementor-control-media__remove elementor-control-media__file__controls__remove" title="<?php echo esc_attr__( 'Remove', 'elementor' ); ?>">
298 <i class="eicon-trash-o" aria-hidden="true"></i>
299 <span class="elementor-screen-only"><?php echo esc_html__( 'Remove', 'elementor' ); ?></span>
300 </div>
301 <div class="elementor-control-media__file__controls__upload-button elementor-control-media-upload-button" title="<?php echo esc_attr__( 'Upload', 'elementor' ); ?>">
302 <i class="eicon-upload" aria-hidden="true"></i>
303 <span class="elementor-screen-only"><?php echo esc_html__( 'Upload', 'elementor' ); ?></span>
304 </div>
305 </div>
306 </div>
307 <# } #>
308 <# if ( data.description ) { #>
309 <div class="elementor-control-field-description">{{{ data.description }}}</div>
310 <# } #>
311
312 <# if ( data.has_sizes ) { #>
313 <div class="elementor-control-type-select e-control-image-size">
314 <div class="elementor-control-field">
315 <label class="elementor-control-title" data-e-responsive-switcher-sibling="false" for="<?php $this->print_control_uid( 'size' ); ?>"><?php echo esc_html__( 'Image Resolution', 'elementor' ); ?></label>
316 <div class="elementor-control-input-wrapper elementor-control-unit-5">
317 <select class="e-image-size-select" id="<?php $this->print_control_uid( 'size' ); ?>" data-setting="size">
318 <?php foreach ( $this->get_image_sizes() as $size_key => $size_title ) : ?>
319 <option value="<?php echo esc_attr( $size_key ); ?>"><?php echo esc_html( $size_title ); ?></option>
320 <?php endforeach; ?>
321 </select>
322 </div>
323 </div>
324 </div>
325 <# } #>
326
327 <input type="hidden" data-setting="{{ data.name }}"/>
328 </div>
329 <?php
330 }
331
332 private function get_image_sizes() : array {
333 $wp_image_sizes = Group_Control_Image_Size::get_all_image_sizes();
334
335 $image_sizes = [];
336
337 foreach ( $wp_image_sizes as $size_key => $size_attributes ) {
338 $control_title = ucwords( str_replace( '_', ' ', $size_key ) );
339 if ( is_array( $size_attributes ) ) {
340 $control_title .= sprintf( ' - %d x %d', $size_attributes['width'], $size_attributes['height'] );
341 }
342
343 $image_sizes[ $size_key ] = $control_title;
344 }
345
346 $image_sizes[''] = esc_html_x( 'Full', 'Image Size Control', 'elementor' );
347
348 return $image_sizes;
349 }
350
351 /**
352 * Get media control default settings.
353 *
354 * Retrieve the default settings of the media control. Used to return the default
355 * settings while initializing the media control.
356 *
357 * @since 1.0.0
358 * @access protected
359 *
360 * @return array Control default settings.
361 */
362 protected function get_default_settings() {
363 return [
364 'label_block' => true,
365 'has_sizes' => false,
366 'ai' => [
367 'active' => true,
368 'type' => 'media',
369 'category' => 'photographic',
370 ],
371 'media_types' => [
372 'image',
373 ],
374 'dynamic' => [
375 'categories' => [ TagsModule::IMAGE_CATEGORY ],
376 'returnType' => 'object',
377 ],
378 ];
379 }
380
381 /**
382 * Get media control image title.
383 *
384 * Retrieve the `title` of the image selected by the media control.
385 *
386 * @since 1.0.0
387 * @access public
388 * @static
389 *
390 * @param array $attachment Media attachment.
391 *
392 * @return string Image title.
393 */
394 public static function get_image_title( $attachment ) {
395 if ( empty( $attachment['id'] ) ) {
396 return '';
397 }
398
399 return get_the_title( $attachment['id'] );
400 }
401
402 /**
403 * Get media control image alt.
404 *
405 * Retrieve the `alt` value of the image selected by the media control.
406 *
407 * @since 1.0.0
408 * @access public
409 * @static
410 *
411 * @param array $instance Media attachment.
412 *
413 * @return string Image alt.
414 */
415 public static function get_image_alt( $instance ) {
416 if ( empty( $instance['id'] ) ) {
417 // For `Insert From URL` images.
418 return isset( $instance['alt'] ) ? trim( self::sanitise_text( $instance['alt'] ) ) : '';
419 }
420
421 $attachment_id = $instance['id'];
422 if ( ! $attachment_id ) {
423 return '';
424 }
425
426 $attachment = get_post( $attachment_id );
427 if ( ! $attachment ) {
428 return '';
429 }
430
431 $alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
432 if ( ! $alt ) {
433 $alt = $attachment->post_excerpt;
434 if ( ! $alt ) {
435 $alt = $attachment->post_title;
436 }
437 }
438 return trim( self::sanitise_text( $alt ) );
439 }
440
441 public function get_style_value( $css_property, $control_value, array $control_data ) {
442 if ( 'URL' !== $css_property || empty( $control_value['id'] ) ) {
443 return parent::get_style_value( $css_property, $control_value, $control_data );
444 }
445
446 if ( empty( $control_value['size'] ) ) {
447 $control_value['size'] = 'full';
448 }
449
450 return wp_get_attachment_image_url( $control_value['id'], $control_value['size'] );
451 }
452
453 public static function sanitise_text( $string ) {
454 return esc_attr( strip_tags( $string ) );
455 }
456 }
457