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

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