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

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