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

269 lines 7.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 $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 <div class="elementor-control-input-wrapper elementor-aspect-ratio-219">
135 <div class="elementor-control-media__content elementor-control-tag-area elementor-control-preview-area elementor-fit-aspect-ratio">
136 <div class="elementor-control-media-upload-button elementor-control-media__content__upload-button elementor-fit-aspect-ratio">
137 <i class="eicon-plus-circle" aria-hidden="true"></i>
138 </div>
139 <div class="elementor-control-media-area elementor-fit-aspect-ratio">
140 <div class="elementor-control-media__remove elementor-control-media__content__remove" title="<?php echo __( 'Remove', 'elementor' ); ?>">
141 <i class="eicon-trash-o"></i>
142 </div>
143 <# if( 'image' === data.media_type ) { #>
144 <div class="elementor-control-media__preview elementor-fit-aspect-ratio"></div>
145 <# } else if( 'video' === data.media_type ) { #>
146 <video class="elementor-control-media-video" preload="metadata"></video>
147 <i class="eicon-video-camera"></i>
148 <# } #>
149 </div>
150 <div class="elementor-control-media__tools elementor-control-dynamic-switcher-wrapper">
151 <# if( 'image' === data.media_type ) { #>
152 <div class="elementor-control-media__tool elementor-control-media__replace"><?php echo __( 'Choose Image', 'elementor' ); ?></div>
153 <# } else if( 'video' === data.media_type ) { #>
154 <div class="elementor-control-media__tool elementor-control-media__replace"><?php echo __( 'Choose Video', 'elementor' ); ?></div>
155 <# } #>
156 </div>
157 </div>
158 </div>
159 <# } else { #>
160 <div class="elementor-control-media__file elementor-control-preview-area">
161 <div class="elementor-control-media__file__content">
162 <div class="elementor-control-media__file__content__label"><?php echo __( 'Click the media icon to upload file', 'elementor' ); ?></div>
163 <div class="elementor-control-media__file__content__info">
164 <div class="elementor-control-media__file__content__info__icon">
165 <i class="eicon-document-file"></i>
166 </div>
167 <div class="elementor-control-media__file__content__info__name"></div>
168 </div>
169 </div>
170 <div class="elementor-control-media__file__controls">
171 <div class="elementor-control-media__remove elementor-control-media__file__controls__remove" title="<?php echo __( 'Remove', 'elementor' ); ?>">
172 <i class="eicon-trash-o"></i>
173 </div>
174 <div class="elementor-control-media__file__controls__upload-button elementor-control-media-upload-button" title="<?php echo __( 'Upload', 'elementor' ); ?>">
175 <i class="eicon-upload"></i>
176 </div>
177 </div>
178 </div>
179 <# } #>
180 <# if ( data.description ) { #>
181 <div class="elementor-control-field-description">{{{ data.description }}}</div>
182 <# } #>
183 <input type="hidden" data-setting="{{ data.name }}"/>
184 </div>
185 <?php
186 }
187
188 /**
189 * Get media control default settings.
190 *
191 * Retrieve the default settings of the media control. Used to return the default
192 * settings while initializing the media control.
193 *
194 * @since 1.0.0
195 * @access protected
196 *
197 * @return array Control default settings.
198 */
199 protected function get_default_settings() {
200 return [
201 'label_block' => true,
202 'media_type' => 'image',
203 'dynamic' => [
204 'categories' => [ TagsModule::IMAGE_CATEGORY ],
205 'returnType' => 'object',
206 ],
207 ];
208 }
209
210 /**
211 * Get media control image title.
212 *
213 * Retrieve the `title` of the image selected by the media control.
214 *
215 * @since 1.0.0
216 * @access public
217 * @static
218 *
219 * @param array $attachment Media attachment.
220 *
221 * @return string Image title.
222 */
223 public static function get_image_title( $attachment ) {
224 if ( empty( $attachment['id'] ) ) {
225 return '';
226 }
227
228 return get_the_title( $attachment['id'] );
229 }
230
231 /**
232 * Get media control image alt.
233 *
234 * Retrieve the `alt` value of the image selected by the media control.
235 *
236 * @since 1.0.0
237 * @access public
238 * @static
239 *
240 * @param array $instance Media attachment.
241 *
242 * @return string Image alt.
243 */
244 public static function get_image_alt( $instance ) {
245 if ( empty( $instance['id'] ) ) {
246 return '';
247 }
248
249 $attachment_id = $instance['id'];
250 if ( ! $attachment_id ) {
251 return '';
252 }
253
254 $attachment = get_post( $attachment_id );
255 if ( ! $attachment ) {
256 return '';
257 }
258
259 $alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
260 if ( ! $alt ) {
261 $alt = $attachment->post_excerpt;
262 if ( ! $alt ) {
263 $alt = $attachment->post_title;
264 }
265 }
266 return trim( strip_tags( $alt ) );
267 }
268 }
269