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

364 lines 9.2 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\Modules\DynamicTags\Module as TagsModule;
5
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit; // Exit if accessed directly.
8 }
9
10 /**
11 * Elementor media control.
12 *
13 * A base control for creating a media chooser control. Based on the WordPress
14 * media library. Used to select an image from the WordPress media library.
15 *
16 * @since 1.0.0
17 */
18 class Control_Media extends Control_Base_Multiple {
19
20 /**
21 * Get media control type.
22 *
23 * Retrieve the control type, in this case `media`.
24 *
25 * @since 1.0.0
26 * @access public
27 *
28 * @return string Control type.
29 */
30 public function get_type() {
31 return 'media';
32 }
33
34 /**
35 * Get media control default values.
36 *
37 * Retrieve the default value of the media control. Used to return the default
38 * values while initializing the media control.
39 *
40 * @since 1.0.0
41 * @access public
42 *
43 * @return array Control default value.
44 */
45 public function get_default_value() {
46 return [
47 'url' => '',
48 'id' => '',
49 ];
50 }
51
52 /**
53 * Import media images.
54 *
55 * Used to import media control files from external sites while importing
56 * Elementor template JSON file, and replacing the old data.
57 *
58 * @since 1.0.0
59 * @access public
60 *
61 * @param array $settings Control settings
62 *
63 * @return array Control settings.
64 */
65 public function on_import( $settings ) {
66 if ( empty( $settings['url'] ) ) {
67 return $settings;
68 }
69
70 add_filter( 'upload_mimes', [ $this, 'support_svg_and_json_import' ], 100 );
71
72 $settings = Plugin::$instance->templates_manager->get_import_images_instance()->import( $settings );
73
74 remove_filter( 'upload_mimes', [ $this, 'support_svg_and_json_import' ], 100 );
75
76 if ( ! $settings ) {
77 $settings = [
78 'id' => '',
79 'url' => Utils::get_placeholder_image_src(),
80 ];
81 }
82
83 return $settings;
84 }
85
86 /**
87 * Support SVG and JSON Import
88 *
89 * Called by the 'upload_mimes' filter. Adds SVG and JSON mime types to the list of WordPress' allowed mime types.
90 *
91 * @since 3.4.0
92 *
93 * @param $mimes
94 * @return mixed
95 */
96 public function support_svg_and_json_import( $mimes ) {
97 $mimes['svg'] = 'image/svg+xml';
98 $mimes['json'] = 'application/json';
99
100 return $mimes;
101 }
102
103 /**
104 * Enqueue media control scripts and styles.
105 *
106 * Used to register and enqueue custom scripts and styles used by the media
107 * control.
108 *
109 * @since 1.0.0
110 * @access public
111 */
112 public function enqueue() {
113 global $wp_version;
114
115 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
116 wp_enqueue_media();
117
118 wp_enqueue_style(
119 'media',
120 admin_url( '/css/media' . $suffix . '.css' ),
121 [],
122 $wp_version
123 );
124
125 wp_register_script(
126 'image-edit',
127 '/wp-admin/js/image-edit' . $suffix . '.js',
128 [
129 'jquery',
130 'json2',
131 'imgareaselect',
132 ],
133 $wp_version,
134 true
135 );
136
137 wp_enqueue_script( 'image-edit' );
138 }
139
140 /**
141 * Render media control output in the editor.
142 *
143 * Used to generate the control HTML in the editor using Underscore JS
144 * template. The variables for the class are available using `data` JS
145 * object.
146 *
147 * @since 1.0.0
148 * @access public
149 */
150 public function content_template() {
151 ?>
152 <#
153 // For BC.
154 if ( data.media_type ) {
155 data.media_types = [ data.media_type ];
156 }
157
158 if ( data.should_include_svg_inline_option ) {
159 data.media_types.push( 'svg' );
160 }
161
162 // Determine if the current media type is viewable.
163 const isViewable = () => {
164 const viewable = [
165 'image',
166 'video',
167 'svg',
168 ];
169
170 // Make sure that all media types are viewable.
171 return data.media_types.every( ( type ) => viewable.includes( type ) );
172 };
173
174 // Get the preview type for the current media type.
175 const getPreviewType = () => {
176 if ( data.media_types.includes( 'video' ) ) {
177 return 'video';
178 }
179
180 if ( data.media_types.includes( 'image' ) || data.media_types.includes( 'svg' ) ) {
181 return 'image';
182 }
183
184 return 'none';
185 }
186
187 // Retrieve a button label by media type.
188 const getButtonLabel = ( mediaType ) => {
189 switch( mediaType ) {
190 case 'image':
191 return '<?php esc_html_e( 'Choose Image', 'elementor' ); ?>';
192
193 case 'video':
194 return '<?php esc_html_e( 'Choose Video', 'elementor' ); ?>';
195
196 case 'svg':
197 return '<?php esc_html_e( 'Choose SVG', 'elementor' ); ?>';
198
199 default:
200 return '<?php esc_html_e( 'Choose File', 'elementor' ); ?>';
201 }
202 }
203 #>
204 <div class="elementor-control-field elementor-control-media">
205 <label class="elementor-control-title">{{{ data.label }}}</label>
206 <#
207 if ( isViewable() ) {
208 let inputWrapperClasses = 'elementor-control-input-wrapper elementor-aspect-ratio-219';
209
210 if ( ! data.label_block ) {
211 inputWrapperClasses += ' elementor-control-unit-5';
212 }
213 #>
214 <div class="{{{ inputWrapperClasses }}}">
215 <div class="elementor-control-media__content elementor-control-tag-area elementor-control-preview-area elementor-fit-aspect-ratio">
216 <div class="elementor-control-media-area elementor-fit-aspect-ratio">
217 <div class="elementor-control-media__remove elementor-control-media__content__remove" title="<?php echo esc_html__( 'Remove', 'elementor' ); ?>">
218 <i class="eicon-trash-o"></i>
219 </div>
220 <#
221 switch( getPreviewType() ) {
222 case 'image':
223 #>
224 <div class="elementor-control-media__preview elementor-fit-aspect-ratio"></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"></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 </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 </div>
251 <# } /* endif isViewable() */ else { #>
252 <div class="elementor-control-media__file elementor-control-preview-area">
253 <div class="elementor-control-media__file__content">
254 <div class="elementor-control-media__file__content__label"><?php echo esc_html__( 'Click the media icon to upload file', 'elementor' ); ?></div>
255 <div class="elementor-control-media__file__content__info">
256 <div class="elementor-control-media__file__content__info__icon">
257 <i class="eicon-document-file"></i>
258 </div>
259 <div class="elementor-control-media__file__content__info__name"></div>
260 </div>
261 </div>
262 <div class="elementor-control-media__file__controls">
263 <div class="elementor-control-media__remove elementor-control-media__file__controls__remove" title="<?php echo esc_html__( 'Remove', 'elementor' ); ?>">
264 <i class="eicon-trash-o"></i>
265 </div>
266 <div class="elementor-control-media__file__controls__upload-button elementor-control-media-upload-button" title="<?php echo esc_html__( 'Upload', 'elementor' ); ?>">
267 <i class="eicon-upload"></i>
268 </div>
269 </div>
270 </div>
271 <# } #>
272 <# if ( data.description ) { #>
273 <div class="elementor-control-field-description">{{{ data.description }}}</div>
274 <# } #>
275 <input type="hidden" data-setting="{{ data.name }}"/>
276 </div>
277 <?php
278 }
279
280 /**
281 * Get media control default settings.
282 *
283 * Retrieve the default settings of the media control. Used to return the default
284 * settings while initializing the media control.
285 *
286 * @since 1.0.0
287 * @access protected
288 *
289 * @return array Control default settings.
290 */
291 protected function get_default_settings() {
292 return [
293 'label_block' => true,
294 'media_types' => [
295 'image',
296 ],
297 'dynamic' => [
298 'categories' => [ TagsModule::IMAGE_CATEGORY ],
299 'returnType' => 'object',
300 ],
301 ];
302 }
303
304 /**
305 * Get media control image title.
306 *
307 * Retrieve the `title` of the image selected by the media control.
308 *
309 * @since 1.0.0
310 * @access public
311 * @static
312 *
313 * @param array $attachment Media attachment.
314 *
315 * @return string Image title.
316 */
317 public static function get_image_title( $attachment ) {
318 if ( empty( $attachment['id'] ) ) {
319 return '';
320 }
321
322 return get_the_title( $attachment['id'] );
323 }
324
325 /**
326 * Get media control image alt.
327 *
328 * Retrieve the `alt` value of the image selected by the media control.
329 *
330 * @since 1.0.0
331 * @access public
332 * @static
333 *
334 * @param array $instance Media attachment.
335 *
336 * @return string Image alt.
337 */
338 public static function get_image_alt( $instance ) {
339 if ( empty( $instance['id'] ) ) {
340 // For `Insert From URL` images.
341 return isset( $instance['alt'] ) ? trim( strip_tags( $instance['alt'] ) ) : '';
342 }
343
344 $attachment_id = $instance['id'];
345 if ( ! $attachment_id ) {
346 return '';
347 }
348
349 $attachment = get_post( $attachment_id );
350 if ( ! $attachment ) {
351 return '';
352 }
353
354 $alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
355 if ( ! $alt ) {
356 $alt = $attachment->post_excerpt;
357 if ( ! $alt ) {
358 $alt = $attachment->post_title;
359 }
360 }
361 return trim( strip_tags( $alt ) );
362 }
363 }
364