groups
1 year ago
alert.php
2 years ago
animation.php
1 year ago
base-data.php
3 years ago
base-icon-font.php
3 years ago
base-multiple.php
3 years ago
base-ui.php
3 years ago
base-units.php
2 years ago
base.php
3 years ago
box-shadow.php
3 years ago
button.php
3 years ago
choose.php
3 years ago
code.php
3 years ago
color.php
3 years ago
date-time.php
3 years ago
deprecated-notice.php
3 years ago
dimensions.php
3 years ago
divider.php
2 years ago
exit-animation.php
1 year ago
font.php
3 years ago
gallery.php
1 year ago
gaps.php
2 years ago
heading.php
3 years ago
hidden.php
3 years ago
hover-animation.php
1 year ago
icon.php
2 years ago
icons.php
2 years ago
image-dimensions.php
3 years ago
media.php
1 year ago
notice.php
1 year ago
number.php
3 years ago
popover-toggle.php
3 years ago
raw-html.php
3 years ago
repeater.php
1 year ago
section.php
2 years ago
select.php
3 years ago
select2.php
2 years ago
slider.php
3 years ago
structure.php
2 years ago
switcher.php
3 years ago
tab.php
2 years ago
tabs.php
2 years ago
text-shadow.php
3 years ago
text.php
3 years ago
textarea.php
3 years ago
url.php
1 year ago
wp-widget.php
3 years ago
wysiwyg.php
3 years ago
media.php
458 lines
| 1 | <?php |
| 2 | namespace Elementor; |
| 3 | |
| 4 | use Elementor\Core\Utils\Hints; |
| 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 | 'size' => '', |
| 51 | ]; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Import media images. |
| 56 | * |
| 57 | * Used to import media control files from external sites while importing |
| 58 | * Elementor template JSON file, and replacing the old data. |
| 59 | * |
| 60 | * @since 1.0.0 |
| 61 | * @access public |
| 62 | * |
| 63 | * @param array $settings Control settings |
| 64 | * |
| 65 | * @return array Control settings. |
| 66 | */ |
| 67 | public function on_import( $settings ) { |
| 68 | if ( empty( $settings['url'] ) ) { |
| 69 | return $settings; |
| 70 | } |
| 71 | |
| 72 | $settings = Plugin::$instance->templates_manager->get_import_images_instance()->import( $settings ); |
| 73 | |
| 74 | if ( ! $settings ) { |
| 75 | $settings = [ |
| 76 | 'id' => '', |
| 77 | 'url' => Utils::get_placeholder_image_src(), |
| 78 | ]; |
| 79 | } |
| 80 | |
| 81 | return $settings; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Support SVG and JSON Import |
| 86 | * |
| 87 | * Called by the 'upload_mimes' filter. Adds SVG and JSON mime types to the list of WordPress' allowed mime types. |
| 88 | * |
| 89 | * @since 3.4.6 |
| 90 | * @deprecated 3.5.0 |
| 91 | * |
| 92 | * @param $mimes |
| 93 | * @return mixed |
| 94 | */ |
| 95 | public function support_svg_and_json_import( $mimes ) { |
| 96 | Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __METHOD__, '3.5.0' ); |
| 97 | |
| 98 | return $mimes; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Enqueue media control scripts and styles. |
| 103 | * |
| 104 | * Used to register and enqueue custom scripts and styles used by the media |
| 105 | * control. |
| 106 | * |
| 107 | * @since 1.0.0 |
| 108 | * @access public |
| 109 | */ |
| 110 | public function enqueue() { |
| 111 | global $wp_version; |
| 112 | |
| 113 | $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 114 | wp_enqueue_media(); |
| 115 | |
| 116 | wp_enqueue_style( |
| 117 | 'media', |
| 118 | admin_url( '/css/media' . $suffix . '.css' ), |
| 119 | [], |
| 120 | $wp_version |
| 121 | ); |
| 122 | |
| 123 | wp_register_script( |
| 124 | 'image-edit', |
| 125 | '/wp-admin/js/image-edit' . $suffix . '.js', |
| 126 | [ |
| 127 | 'jquery', |
| 128 | 'json2', |
| 129 | 'imgareaselect', |
| 130 | ], |
| 131 | $wp_version, |
| 132 | true |
| 133 | ); |
| 134 | |
| 135 | wp_enqueue_script( 'image-edit' ); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Render media control output in the editor. |
| 140 | * |
| 141 | * Used to generate the control HTML in the editor using Underscore JS |
| 142 | * template. The variables for the class are available using `data` JS |
| 143 | * object. |
| 144 | * |
| 145 | * @since 1.0.0 |
| 146 | * @access public |
| 147 | */ |
| 148 | public function content_template() { |
| 149 | ?> |
| 150 | <# |
| 151 | // For BC. |
| 152 | if ( data.media_type ) { |
| 153 | data.media_types = [ data.media_type ]; |
| 154 | } |
| 155 | |
| 156 | if ( data.should_include_svg_inline_option ) { |
| 157 | data.media_types.push( 'svg' ); |
| 158 | } |
| 159 | |
| 160 | // Determine if the current media type is viewable. |
| 161 | const isViewable = () => { |
| 162 | const viewable = [ |
| 163 | 'image', |
| 164 | 'video', |
| 165 | 'svg', |
| 166 | ]; |
| 167 | |
| 168 | // Make sure that all media types are viewable. |
| 169 | return data.media_types.every( ( type ) => viewable.includes( type ) ); |
| 170 | }; |
| 171 | |
| 172 | // Get the preview type for the current media type. |
| 173 | const getPreviewType = () => { |
| 174 | if ( data.media_types.includes( 'video' ) ) { |
| 175 | return 'video'; |
| 176 | } |
| 177 | |
| 178 | if ( data.media_types.includes( 'image' ) || data.media_types.includes( 'svg' ) ) { |
| 179 | return 'image'; |
| 180 | } |
| 181 | |
| 182 | return 'none'; |
| 183 | } |
| 184 | |
| 185 | // Retrieve a button label by media type. |
| 186 | const getButtonLabel = ( mediaType ) => { |
| 187 | switch( mediaType ) { |
| 188 | case 'image': |
| 189 | return '<?php esc_html_e( 'Choose Image', 'elementor' ); ?>'; |
| 190 | |
| 191 | case 'video': |
| 192 | return '<?php esc_html_e( 'Choose Video', 'elementor' ); ?>'; |
| 193 | |
| 194 | case 'svg': |
| 195 | return '<?php esc_html_e( 'Choose SVG', 'elementor' ); ?>'; |
| 196 | |
| 197 | default: |
| 198 | return '<?php esc_html_e( 'Choose File', 'elementor' ); ?>'; |
| 199 | } |
| 200 | } |
| 201 | #> |
| 202 | <div class="elementor-control-field elementor-control-media"> |
| 203 | <label class="elementor-control-title">{{{ data.label }}}</label> |
| 204 | <# |
| 205 | if ( isViewable() ) { |
| 206 | let inputWrapperClasses = 'elementor-control-input-wrapper'; |
| 207 | |
| 208 | if ( ! data.label_block ) { |
| 209 | inputWrapperClasses += ' elementor-control-unit-5'; |
| 210 | } |
| 211 | #> |
| 212 | <div class="{{{ inputWrapperClasses }}}"> |
| 213 | <div class="elementor-control-media__content elementor-control-tag-area elementor-control-preview-area"> |
| 214 | <div class="elementor-control-media-area"> |
| 215 | <div class="elementor-control-media__remove elementor-control-media__content__remove" title="<?php echo esc_attr__( 'Remove', 'elementor' ); ?>"> |
| 216 | <i class="eicon-trash-o" aria-hidden="true"></i> |
| 217 | <span class="elementor-screen-only"><?php echo esc_html__( 'Remove', 'elementor' ); ?></span> |
| 218 | </div> |
| 219 | <# |
| 220 | switch( getPreviewType() ) { |
| 221 | case 'image': |
| 222 | #> |
| 223 | <div class="elementor-control-media__preview"></div> |
| 224 | <# |
| 225 | break; |
| 226 | |
| 227 | case 'video': |
| 228 | #> |
| 229 | <video class="elementor-control-media-video" preload="metadata"></video> |
| 230 | <i class="eicon-video-camera" aria-hidden="true"></i> |
| 231 | <# |
| 232 | break; |
| 233 | } |
| 234 | #> |
| 235 | </div> |
| 236 | <div class="elementor-control-media-upload-button elementor-control-media__content__upload-button"> |
| 237 | <i class="eicon-plus-circle" aria-hidden="true"></i> |
| 238 | <span class="elementor-screen-only"><?php echo esc_html__( 'Add', 'elementor' ); ?></span> |
| 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 | |
| 251 | <?php /* ?> |
| 252 | <div class="elementor-control-media__warnings" role="alert" style="display: none;"> |
| 253 | <?php |
| 254 | Hints::get_notice_template( [ |
| 255 | 'type' => 'warning', |
| 256 | 'content' => esc_html__( 'This image doesn’t contain ALT text - which is necessary for accessibility and SEO.', 'elementor' ), |
| 257 | 'icon' => true, |
| 258 | ] ); |
| 259 | ?> |
| 260 | </div> |
| 261 | <?php */ ?> |
| 262 | |
| 263 | <?php if ( Hints::should_display_hint( 'image-optimization' ) ) : ?> |
| 264 | <div class="elementor-control-media__promotions" role="alert" style="display: none;"> |
| 265 | <?php |
| 266 | Hints::get_notice_template( [ |
| 267 | 'display' => ! Hints::is_dismissed( 'image-optimization' ), |
| 268 | 'type' => 'info', |
| 269 | 'content' => __( 'Optimize your images to enhance site performance by using Image Optimizer.', 'elementor' ), |
| 270 | 'icon' => true, |
| 271 | 'dismissible' => 'image_optimizer_hint', |
| 272 | 'button_text' => Hints::is_plugin_installed( 'image-optimization' ) ? __( 'Activate Plugin', 'elementor' ) : __( 'Install Plugin', 'elementor' ), |
| 273 | 'button_event' => 'image_optimizer_hint', |
| 274 | 'button_data' => [ |
| 275 | 'action_url' => Hints::get_plugin_action_url( 'image-optimization' ), |
| 276 | ], |
| 277 | ] ); ?> |
| 278 | </div> |
| 279 | <?php endif; ?> |
| 280 | |
| 281 | </div> |
| 282 | <# } /* endif isViewable() */ else { #> |
| 283 | <div class="elementor-control-media__file elementor-control-preview-area"> |
| 284 | <div class="elementor-control-media__file__content"> |
| 285 | <div class="elementor-control-media__file__content__label"><?php echo esc_html__( 'Click the media icon to upload file', 'elementor' ); ?></div> |
| 286 | <div class="elementor-control-media__file__content__info"> |
| 287 | <div class="elementor-control-media__file__content__info__icon"> |
| 288 | <i class="eicon-document-file"></i> |
| 289 | </div> |
| 290 | <div class="elementor-control-media__file__content__info__name"></div> |
| 291 | </div> |
| 292 | </div> |
| 293 | <div class="elementor-control-media__file__controls"> |
| 294 | <div class="elementor-control-media__remove elementor-control-media__file__controls__remove" title="<?php echo esc_attr__( 'Remove', 'elementor' ); ?>"> |
| 295 | <i class="eicon-trash-o" aria-hidden="true"></i> |
| 296 | <span class="elementor-screen-only"><?php echo esc_html__( 'Remove', 'elementor' ); ?></span> |
| 297 | </div> |
| 298 | <div class="elementor-control-media__file__controls__upload-button elementor-control-media-upload-button" title="<?php echo esc_attr__( 'Upload', 'elementor' ); ?>"> |
| 299 | <i class="eicon-upload" aria-hidden="true"></i> |
| 300 | <span class="elementor-screen-only"><?php echo esc_html__( 'Upload', 'elementor' ); ?></span> |
| 301 | </div> |
| 302 | </div> |
| 303 | </div> |
| 304 | <# } #> |
| 305 | <# if ( data.description ) { #> |
| 306 | <div class="elementor-control-field-description">{{{ data.description }}}</div> |
| 307 | <# } #> |
| 308 | |
| 309 | <# if ( data.has_sizes ) { #> |
| 310 | <div class="elementor-control-type-select e-control-image-size"> |
| 311 | <div class="elementor-control-field"> |
| 312 | <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> |
| 313 | <div class="elementor-control-input-wrapper elementor-control-unit-5"> |
| 314 | <select class="e-image-size-select" id="<?php $this->print_control_uid( 'size' ); ?>" data-setting="size"> |
| 315 | <?php foreach ( $this->get_image_sizes() as $size_key => $size_title ) : ?> |
| 316 | <option value="<?php echo esc_attr( $size_key ); ?>"><?php echo esc_html( $size_title ); ?></option> |
| 317 | <?php endforeach; ?> |
| 318 | </select> |
| 319 | </div> |
| 320 | </div> |
| 321 | </div> |
| 322 | <# } #> |
| 323 | |
| 324 | <input type="hidden" data-setting="{{ data.name }}"/> |
| 325 | </div> |
| 326 | <?php |
| 327 | } |
| 328 | |
| 329 | private function get_image_sizes() : array { |
| 330 | $wp_image_sizes = Group_Control_Image_Size::get_all_image_sizes(); |
| 331 | |
| 332 | $image_sizes = []; |
| 333 | |
| 334 | foreach ( $wp_image_sizes as $size_key => $size_attributes ) { |
| 335 | $control_title = ucwords( str_replace( '_', ' ', $size_key ) ); |
| 336 | if ( is_array( $size_attributes ) ) { |
| 337 | $control_title .= sprintf( ' - %d x %d', $size_attributes['width'], $size_attributes['height'] ); |
| 338 | } |
| 339 | |
| 340 | $image_sizes[ $size_key ] = $control_title; |
| 341 | } |
| 342 | |
| 343 | $image_sizes[''] = esc_html_x( 'Full', 'Image Size Control', 'elementor' ); |
| 344 | |
| 345 | return $image_sizes; |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Get media control default settings. |
| 350 | * |
| 351 | * Retrieve the default settings of the media control. Used to return the default |
| 352 | * settings while initializing the media control. |
| 353 | * |
| 354 | * @since 1.0.0 |
| 355 | * @access protected |
| 356 | * |
| 357 | * @return array Control default settings. |
| 358 | */ |
| 359 | protected function get_default_settings() { |
| 360 | return [ |
| 361 | 'label_block' => true, |
| 362 | 'has_sizes' => false, |
| 363 | 'ai' => [ |
| 364 | 'active' => true, |
| 365 | 'type' => 'media', |
| 366 | 'category' => 'photographic', |
| 367 | ], |
| 368 | 'media_types' => [ |
| 369 | 'image', |
| 370 | ], |
| 371 | 'dynamic' => [ |
| 372 | 'categories' => [ TagsModule::IMAGE_CATEGORY ], |
| 373 | 'returnType' => 'object', |
| 374 | ], |
| 375 | ]; |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Get media control image title. |
| 380 | * |
| 381 | * Retrieve the `title` of the image selected by the media control. |
| 382 | * |
| 383 | * @since 1.0.0 |
| 384 | * @access public |
| 385 | * @static |
| 386 | * |
| 387 | * @param array $attachment Media attachment. |
| 388 | * |
| 389 | * @return string Image title. |
| 390 | */ |
| 391 | public static function get_image_title( $attachment ) { |
| 392 | if ( empty( $attachment['id'] ) ) { |
| 393 | return ''; |
| 394 | } |
| 395 | |
| 396 | return get_the_title( $attachment['id'] ); |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Get media control image alt. |
| 401 | * |
| 402 | * Retrieve the `alt` value of the image selected by the media control. |
| 403 | * |
| 404 | * @since 1.0.0 |
| 405 | * @access public |
| 406 | * @static |
| 407 | * |
| 408 | * @param array $instance Media attachment. |
| 409 | * |
| 410 | * @return string Image alt. |
| 411 | */ |
| 412 | public static function get_image_alt( $instance ) { |
| 413 | if ( empty( $instance['id'] ) ) { |
| 414 | // For `Insert From URL` images. |
| 415 | return isset( $instance['alt'] ) ? trim( self::sanitise_text( $instance['alt'] ) ) : ''; |
| 416 | } |
| 417 | |
| 418 | $attachment_id = $instance['id']; |
| 419 | if ( ! $attachment_id ) { |
| 420 | return ''; |
| 421 | } |
| 422 | |
| 423 | $attachment = get_post( $attachment_id ); |
| 424 | if ( ! $attachment ) { |
| 425 | return ''; |
| 426 | } |
| 427 | |
| 428 | $alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ); |
| 429 | if ( ! $alt ) { |
| 430 | if ( Utils::has_invalid_post_permissions( $attachment ) ) { |
| 431 | return ''; |
| 432 | } |
| 433 | |
| 434 | $alt = $attachment->post_excerpt; |
| 435 | if ( ! $alt ) { |
| 436 | $alt = $attachment->post_title; |
| 437 | } |
| 438 | } |
| 439 | return trim( self::sanitise_text( $alt ) ); |
| 440 | } |
| 441 | |
| 442 | public function get_style_value( $css_property, $control_value, array $control_data ) { |
| 443 | if ( 'URL' !== $css_property || empty( $control_value['id'] ) ) { |
| 444 | return parent::get_style_value( $css_property, $control_value, $control_data ); |
| 445 | } |
| 446 | |
| 447 | if ( empty( $control_value['size'] ) ) { |
| 448 | $control_value['size'] = 'full'; |
| 449 | } |
| 450 | |
| 451 | return wp_get_attachment_image_url( $control_value['id'], $control_value['size'] ); |
| 452 | } |
| 453 | |
| 454 | public static function sanitise_text( $string ) { |
| 455 | return esc_attr( strip_tags( $string ) ); |
| 456 | } |
| 457 | } |
| 458 |