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

463 lines 13.0 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" title="<?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
266 <?php if ( Hints::should_display_hint( 'image-optimization' ) ) : ?>
267 <div class="elementor-control-media__promotions" role="alert" style="display: none;">
268 <?php
269 Hints::get_notice_template( [
270 'display' => ! Hints::is_dismissed( 'image-optimization' ),
271 'type' => 'info',
272 'content' => __( 'Optimize your images to enhance site performance by using Image Optimizer.', 'elementor' ),
273 'icon' => true,
274 'dismissible' => 'image_optimizer_hint',
275 'button_text' => Hints::is_plugin_installed( 'image-optimization' ) ? __( 'Activate Plugin', 'elementor' ) : __( 'Install Plugin', 'elementor' ),
276 'button_event' => 'image_optimizer_hint',
277 'button_data' => [
278 'action_url' => Hints::get_plugin_action_url( 'image-optimization' ),
279 ],
280 ] ); ?>
281 </div>
282 <?php endif; ?>
283
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
325 <div class="elementor-control-field-description"><?php echo esc_html__( 'Image size settings don’t apply to Dynamic Images.', 'elementor' ); ?></div>
326 </div>
327 <# } #>
328
329 <input type="hidden" data-setting="{{ data.name }}"/>
330 </div>
331 <?php
332 }
333
334 private function get_image_sizes(): array {
335 $wp_image_sizes = Group_Control_Image_Size::get_all_image_sizes();
336
337 $image_sizes = [];
338
339 foreach ( $wp_image_sizes as $size_key => $size_attributes ) {
340 $control_title = ucwords( str_replace( '_', ' ', $size_key ) );
341 if ( is_array( $size_attributes ) ) {
342 $control_title .= sprintf( ' - %d x %d', $size_attributes['width'], $size_attributes['height'] );
343 }
344
345 $image_sizes[ $size_key ] = $control_title;
346 }
347
348 $image_sizes[''] = esc_html_x( 'Full', 'Image Size Control', 'elementor' );
349
350 return $image_sizes;
351 }
352
353 /**
354 * Get media control default settings.
355 *
356 * Retrieve the default settings of the media control. Used to return the default
357 * settings while initializing the media control.
358 *
359 * @since 1.0.0
360 * @access protected
361 *
362 * @return array Control default settings.
363 */
364 protected function get_default_settings() {
365 return [
366 'label_block' => true,
367 'has_sizes' => false,
368 'ai' => [
369 'active' => true,
370 'type' => 'media',
371 'category' => 'photographic',
372 ],
373 'media_types' => [
374 'image',
375 ],
376 'dynamic' => [
377 'categories' => [ TagsModule::IMAGE_CATEGORY ],
378 'returnType' => 'object',
379 ],
380 ];
381 }
382
383 /**
384 * Get media control image title.
385 *
386 * Retrieve the `title` of the image selected by the media control.
387 *
388 * @since 1.0.0
389 * @access public
390 * @static
391 *
392 * @param array $attachment Media attachment.
393 *
394 * @return string Image title.
395 */
396 public static function get_image_title( $attachment ) {
397 if ( empty( $attachment['id'] ) ) {
398 return '';
399 }
400
401 return get_the_title( $attachment['id'] );
402 }
403
404 /**
405 * Get media control image alt.
406 *
407 * Retrieve the `alt` value of the image selected by the media control.
408 *
409 * @since 1.0.0
410 * @access public
411 * @static
412 *
413 * @param array $instance Media attachment.
414 *
415 * @return string Image alt.
416 */
417 public static function get_image_alt( $instance ) {
418 if ( empty( $instance['id'] ) ) {
419 // For `Insert From URL` images.
420 return isset( $instance['alt'] ) ? trim( self::sanitise_text( $instance['alt'] ) ) : '';
421 }
422
423 $attachment_id = $instance['id'];
424 if ( ! $attachment_id ) {
425 return '';
426 }
427
428 $attachment = get_post( $attachment_id );
429 if ( ! $attachment ) {
430 return '';
431 }
432
433 $alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
434 if ( ! $alt ) {
435 if ( Utils::has_invalid_post_permissions( $attachment ) ) {
436 return '';
437 }
438
439 $alt = $attachment->post_excerpt;
440 if ( ! $alt ) {
441 $alt = $attachment->post_title;
442 }
443 }
444 return trim( self::sanitise_text( $alt ) );
445 }
446
447 public function get_style_value( $css_property, $control_value, array $control_data ) {
448 if ( 'URL' !== $css_property || empty( $control_value['id'] ) ) {
449 return parent::get_style_value( $css_property, $control_value, $control_data );
450 }
451
452 if ( empty( $control_value['size'] ) ) {
453 $control_value['size'] = 'full';
454 }
455
456 return wp_get_attachment_image_url( $control_value['id'], $control_value['size'] );
457 }
458
459 public static function sanitise_text( $string ) {
460 return esc_attr( strip_tags( $string ) );
461 }
462 }
463