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

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