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

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