PluginProbe
Elementor Website Builder – more than just a page builder / 3.20.0-dev2
Elementor Website Builder – more than just a page builder v3.20.0-dev2
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.20.0-dev2, at includes/controls/media.php

471 lines 13.9 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' => esc_html__( '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 if ( $once_dismissed ) {
267 if ( Hints::is_plugin_installed( 'image-optimization' ) ) {
268 $content = sprintf(
269 __( 'This image is large and may slow things down. %1$sActivate Image Optimizer%2$s to reduce size without losing quality.', 'elementor' ),
270 '<a href="' . Hints::get_plugin_action_url( 'image-optimization' ) . '" target="_blank">',
271 '</a>'
272 );
273 } else {
274 $content = sprintf(
275 __( 'This image is large and may slow things down. %1$sInstall Image Optimizer%2$s to reduce size without losing quality.', 'elementor' ),
276 '<a href="' . Hints::get_plugin_action_url( 'image-optimization' ) . '" target="_blank">',
277 '</a>'
278 );
279 }
280 } else {
281 $content = sprintf(
282 '%1$s <a href="%2$s" target="_blank">%3$s</a>',
283 esc_html__( 'Don’t let unoptimized images be the downfall of your site’s performance.', 'elementor' ),
284 Hints::get_plugin_action_url( 'image-optimization' ),
285 Hints::is_plugin_installed( 'image-optimization' ) ? esc_html__( 'Activate Image Optimizer!', 'elementor' ) : esc_html__( 'Install Image Optimizer!', 'elementor' )
286 );
287 }
288 $dismissible = $once_dismissed ? 'image_optimizer_hint' : 'image-optimization-once';
289 Hints::get_notice_template( [
290 'display' => ! $once_dismissed,
291 'type' => $once_dismissed ? 'warning' : 'info',
292 'content' => $content,
293 'icon' => true,
294 'dismissible' => $dismissible,
295 ] ); ?>
296 </div>
297 <?php } ?>
298 </div>
299 <# } /* endif isViewable() */ else { #>
300 <div class="elementor-control-media__file elementor-control-preview-area">
301 <div class="elementor-control-media__file__content">
302 <div class="elementor-control-media__file__content__label"><?php echo esc_html__( 'Click the media icon to upload file', 'elementor' ); ?></div>
303 <div class="elementor-control-media__file__content__info">
304 <div class="elementor-control-media__file__content__info__icon">
305 <i class="eicon-document-file"></i>
306 </div>
307 <div class="elementor-control-media__file__content__info__name"></div>
308 </div>
309 </div>
310 <div class="elementor-control-media__file__controls">
311 <div class="elementor-control-media__remove elementor-control-media__file__controls__remove" title="<?php echo esc_attr__( 'Remove', 'elementor' ); ?>">
312 <i class="eicon-trash-o" aria-hidden="true"></i>
313 <span class="elementor-screen-only"><?php echo esc_html__( 'Remove', 'elementor' ); ?></span>
314 </div>
315 <div class="elementor-control-media__file__controls__upload-button elementor-control-media-upload-button" title="<?php echo esc_attr__( 'Upload', 'elementor' ); ?>">
316 <i class="eicon-upload" aria-hidden="true"></i>
317 <span class="elementor-screen-only"><?php echo esc_html__( 'Upload', 'elementor' ); ?></span>
318 </div>
319 </div>
320 </div>
321 <# } #>
322 <# if ( data.description ) { #>
323 <div class="elementor-control-field-description">{{{ data.description }}}</div>
324 <# } #>
325
326 <# if ( data.has_sizes ) { #>
327 <div class="elementor-control-type-select e-control-image-size">
328 <div class="elementor-control-field">
329 <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>
330 <div class="elementor-control-input-wrapper elementor-control-unit-5">
331 <select class="e-image-size-select" id="<?php $this->print_control_uid( 'size' ); ?>" data-setting="size">
332 <?php foreach ( $this->get_image_sizes() as $size_key => $size_title ) : ?>
333 <option value="<?php echo esc_attr( $size_key ); ?>"><?php echo esc_html( $size_title ); ?></option>
334 <?php endforeach; ?>
335 </select>
336 </div>
337 </div>
338 </div>
339 <# } #>
340
341 <input type="hidden" data-setting="{{ data.name }}"/>
342 </div>
343 <?php
344 }
345
346 private function get_image_sizes() : array {
347 $wp_image_sizes = Group_Control_Image_Size::get_all_image_sizes();
348
349 $image_sizes = [];
350
351 foreach ( $wp_image_sizes as $size_key => $size_attributes ) {
352 $control_title = ucwords( str_replace( '_', ' ', $size_key ) );
353 if ( is_array( $size_attributes ) ) {
354 $control_title .= sprintf( ' - %d x %d', $size_attributes['width'], $size_attributes['height'] );
355 }
356
357 $image_sizes[ $size_key ] = $control_title;
358 }
359
360 $image_sizes[''] = esc_html_x( 'Full', 'Image Size Control', 'elementor' );
361
362 return $image_sizes;
363 }
364
365 /**
366 * Get media control default settings.
367 *
368 * Retrieve the default settings of the media control. Used to return the default
369 * settings while initializing the media control.
370 *
371 * @since 1.0.0
372 * @access protected
373 *
374 * @return array Control default settings.
375 */
376 protected function get_default_settings() {
377 return [
378 'label_block' => true,
379 'has_sizes' => false,
380 'ai' => [
381 'active' => true,
382 'type' => 'media',
383 'category' => 'photographic',
384 ],
385 'media_types' => [
386 'image',
387 ],
388 'dynamic' => [
389 'categories' => [ TagsModule::IMAGE_CATEGORY ],
390 'returnType' => 'object',
391 ],
392 ];
393 }
394
395 /**
396 * Get media control image title.
397 *
398 * Retrieve the `title` of the image selected by the media control.
399 *
400 * @since 1.0.0
401 * @access public
402 * @static
403 *
404 * @param array $attachment Media attachment.
405 *
406 * @return string Image title.
407 */
408 public static function get_image_title( $attachment ) {
409 if ( empty( $attachment['id'] ) ) {
410 return '';
411 }
412
413 return get_the_title( $attachment['id'] );
414 }
415
416 /**
417 * Get media control image alt.
418 *
419 * Retrieve the `alt` value of the image selected by the media control.
420 *
421 * @since 1.0.0
422 * @access public
423 * @static
424 *
425 * @param array $instance Media attachment.
426 *
427 * @return string Image alt.
428 */
429 public static function get_image_alt( $instance ) {
430 if ( empty( $instance['id'] ) ) {
431 // For `Insert From URL` images.
432 return isset( $instance['alt'] ) ? trim( self::sanitise_text( $instance['alt'] ) ) : '';
433 }
434
435 $attachment_id = $instance['id'];
436 if ( ! $attachment_id ) {
437 return '';
438 }
439
440 $attachment = get_post( $attachment_id );
441 if ( ! $attachment ) {
442 return '';
443 }
444
445 $alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
446 if ( ! $alt ) {
447 $alt = $attachment->post_excerpt;
448 if ( ! $alt ) {
449 $alt = $attachment->post_title;
450 }
451 }
452 return trim( self::sanitise_text( $alt ) );
453 }
454
455 public function get_style_value( $css_property, $control_value, array $control_data ) {
456 if ( 'URL' !== $css_property || empty( $control_value['id'] ) ) {
457 return parent::get_style_value( $css_property, $control_value, $control_data );
458 }
459
460 if ( empty( $control_value['size'] ) ) {
461 $control_value['size'] = 'full';
462 }
463
464 return wp_get_attachment_image_url( $control_value['id'], $control_value['size'] );
465 }
466
467 public static function sanitise_text( $string ) {
468 return esc_attr( strip_tags( $string ) );
469 }
470 }
471