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

279 lines 7.7 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 $settings = Plugin::$instance->templates_manager->get_import_images_instance()->import( $settings );
71
72 if ( ! $settings ) {
73 $settings = [
74 'id' => '',
75 'url' => Utils::get_placeholder_image_src(),
76 ];
77 }
78
79 return $settings;
80 }
81
82 /**
83 * Enqueue media control scripts and styles.
84 *
85 * Used to register and enqueue custom scripts and styles used by the media
86 * control.
87 *
88 * @since 1.0.0
89 * @access public
90 */
91 public function enqueue() {
92 global $wp_version;
93
94 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
95 wp_enqueue_media();
96
97 wp_enqueue_style(
98 'media',
99 admin_url( '/css/media' . $suffix . '.css' ),
100 [],
101 $wp_version
102 );
103
104 wp_register_script(
105 'image-edit',
106 '/wp-admin/js/image-edit' . $suffix . '.js',
107 [
108 'jquery',
109 'json2',
110 'imgareaselect',
111 ],
112 $wp_version,
113 true
114 );
115
116 wp_enqueue_script( 'image-edit' );
117 }
118
119 /**
120 * Render media control output in the editor.
121 *
122 * Used to generate the control HTML in the editor using Underscore JS
123 * template. The variables for the class are available using `data` JS
124 * object.
125 *
126 * @since 1.0.0
127 * @access public
128 */
129 public function content_template() {
130 ?>
131 <div class="elementor-control-field elementor-control-media">
132 <label class="elementor-control-title">{{{ data.label }}}</label>
133 <# if ( 'image' === data.media_type || 'video' === data.media_type ) {
134 var inputWrapperClasses = 'elementor-control-input-wrapper elementor-aspect-ratio-219';
135
136 if ( ! data.label_block ) {
137 inputWrapperClasses += ' elementor-control-unit-5';
138 }
139 #>
140 <div class="{{{ inputWrapperClasses }}}">
141 <div class="elementor-control-media__content elementor-control-tag-area elementor-control-preview-area elementor-fit-aspect-ratio">
142 <div class="elementor-control-media-upload-button elementor-control-media__content__upload-button elementor-fit-aspect-ratio">
143 <i class="eicon-plus-circle" aria-hidden="true"></i>
144 </div>
145 <div class="elementor-control-media-area elementor-fit-aspect-ratio">
146 <div class="elementor-control-media__remove elementor-control-media__content__remove" title="<?php echo __( 'Remove', 'elementor' ); ?>">
147 <i class="eicon-trash-o"></i>
148 </div>
149 <# if( 'image' === data.media_type ) { #>
150 <div class="elementor-control-media__preview elementor-fit-aspect-ratio"></div>
151 <# } else if( 'video' === data.media_type ) { #>
152 <video class="elementor-control-media-video" preload="metadata"></video>
153 <i class="eicon-video-camera"></i>
154 <# } #>
155 </div>
156 <div class="elementor-control-media__tools elementor-control-dynamic-switcher-wrapper">
157 <# if( 'image' === data.media_type ) { #>
158 <div class="elementor-control-media__tool elementor-control-media__replace" data-media-type="image"><?php echo __( 'Choose Image', 'elementor' ); ?></div>
159 <# if ( data.should_include_svg_inline_option) { #>
160 <div class="elementor-control-media__tool elementor-control-media__replace" data-media-type="svg"><?php echo __( 'Choose Svg', 'elementor' ); ?></div>
161 <# } #>
162 <# } else if( 'video' === data.media_type ) { #>
163 <div class="elementor-control-media__tool elementor-control-media__replace" data-media-type="video"><?php echo __( 'Choose Video', 'elementor' ); ?></div>
164 <# } #>
165 </div>
166 </div>
167 </div>
168 <# } else { #>
169 <div class="elementor-control-media__file elementor-control-preview-area">
170 <div class="elementor-control-media__file__content">
171 <div class="elementor-control-media__file__content__label"><?php echo __( 'Click the media icon to upload file', 'elementor' ); ?></div>
172 <div class="elementor-control-media__file__content__info">
173 <div class="elementor-control-media__file__content__info__icon">
174 <i class="eicon-document-file"></i>
175 </div>
176 <div class="elementor-control-media__file__content__info__name"></div>
177 </div>
178 </div>
179 <div class="elementor-control-media__file__controls">
180 <div class="elementor-control-media__remove elementor-control-media__file__controls__remove" title="<?php echo __( 'Remove', 'elementor' ); ?>">
181 <i class="eicon-trash-o"></i>
182 </div>
183 <div class="elementor-control-media__file__controls__upload-button elementor-control-media-upload-button" title="<?php echo __( 'Upload', 'elementor' ); ?>">
184 <i class="eicon-upload"></i>
185 </div>
186 </div>
187 </div>
188 <# } #>
189 <# if ( data.description ) { #>
190 <div class="elementor-control-field-description">{{{ data.description }}}</div>
191 <# } #>
192 <input type="hidden" data-setting="{{ data.name }}"/>
193 </div>
194 <?php
195 }
196
197 /**
198 * Get media control default settings.
199 *
200 * Retrieve the default settings of the media control. Used to return the default
201 * settings while initializing the media control.
202 *
203 * @since 1.0.0
204 * @access protected
205 *
206 * @return array Control default settings.
207 */
208 protected function get_default_settings() {
209 return [
210 'label_block' => true,
211 'media_type' => 'image',
212 'should_include_svg_inline_option' => false,
213 'dynamic' => [
214 'categories' => [ TagsModule::IMAGE_CATEGORY ],
215 'returnType' => 'object',
216 ],
217 ];
218 }
219
220 /**
221 * Get media control image title.
222 *
223 * Retrieve the `title` of the image selected by the media control.
224 *
225 * @since 1.0.0
226 * @access public
227 * @static
228 *
229 * @param array $attachment Media attachment.
230 *
231 * @return string Image title.
232 */
233 public static function get_image_title( $attachment ) {
234 if ( empty( $attachment['id'] ) ) {
235 return '';
236 }
237
238 return get_the_title( $attachment['id'] );
239 }
240
241 /**
242 * Get media control image alt.
243 *
244 * Retrieve the `alt` value of the image selected by the media control.
245 *
246 * @since 1.0.0
247 * @access public
248 * @static
249 *
250 * @param array $instance Media attachment.
251 *
252 * @return string Image alt.
253 */
254 public static function get_image_alt( $instance ) {
255 if ( empty( $instance['id'] ) ) {
256 return '';
257 }
258
259 $attachment_id = $instance['id'];
260 if ( ! $attachment_id ) {
261 return '';
262 }
263
264 $attachment = get_post( $attachment_id );
265 if ( ! $attachment ) {
266 return '';
267 }
268
269 $alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
270 if ( ! $alt ) {
271 $alt = $attachment->post_excerpt;
272 if ( ! $alt ) {
273 $alt = $attachment->post_title;
274 }
275 }
276 return trim( strip_tags( $alt ) );
277 }
278 }
279