diff --git src/wp-admin/css/widgets.css src/wp-admin/css/widgets.css
index 627b581c72..ecc5502c6d 100644
|
|
|
152 | 152 | border: 1px solid #f00; |
153 | 153 | } |
154 | 154 | |
| 155 | .media-widget-extra { |
| 156 | text-align: left; |
| 157 | } |
| 158 | |
| 159 | .media-widget-image-link { |
| 160 | margin: 1em 0; |
| 161 | } |
| 162 | |
155 | 163 | /* Widget Dragging Helpers */ |
156 | 164 | .widget.ui-draggable-dragging { |
157 | 165 | min-width: 100%; |
diff --git src/wp-admin/js/widgets/media-image-widget.js src/wp-admin/js/widgets/media-image-widget.js
index 0cda681197..ddbe6b3e24 100644
|
|
|
30 | 30 | * @returns {void} |
31 | 31 | */ |
32 | 32 | renderPreview: function renderPreview() { |
33 | | var control = this, previewContainer, previewTemplate; |
| 33 | var control = this, previewContainer, previewTemplate, fieldsContainer, fieldsTemplate, linkInput; |
34 | 34 | if ( ! control.model.get( 'attachment_id' ) && ! control.model.get( 'url' ) ) { |
35 | 35 | return; |
36 | 36 | } |
37 | 37 | |
38 | 38 | previewContainer = control.$el.find( '.media-widget-preview' ); |
39 | 39 | previewTemplate = wp.template( 'wp-media-widget-image-preview' ); |
40 | | previewContainer.html( previewTemplate( _.extend( control.previewTemplateProps.toJSON() ) ) ); |
| 40 | previewContainer.html( previewTemplate( control.previewTemplateProps.toJSON() ) ); |
| 41 | |
| 42 | linkInput = control.$el.find( '.link' ); |
| 43 | if ( ! linkInput.is( document.activeElement ) ) { |
| 44 | fieldsContainer = control.$el.find( '.media-widget-fields' ); |
| 45 | fieldsTemplate = wp.template( 'wp-media-widget-image-fields' ); |
| 46 | fieldsContainer.html( fieldsTemplate( control.previewTemplateProps.toJSON() ) ); |
| 47 | } |
41 | 48 | }, |
42 | 49 | |
43 | 50 | /** |
… |
… |
|
64 | 71 | mediaFrame.$el.addClass( 'media-widget' ); |
65 | 72 | |
66 | 73 | updateCallback = function() { |
67 | | var mediaProps; |
| 74 | var mediaProps, linkType; |
68 | 75 | |
69 | 76 | // Update cached attachment object to avoid having to re-fetch. This also triggers re-rendering of preview. |
70 | 77 | mediaProps = mediaFrame.state().attributes.image.toJSON(); |
| 78 | linkType = mediaProps.link; |
| 79 | mediaProps.link = mediaProps.linkUrl; |
71 | 80 | control.selectedAttachment.set( mediaProps ); |
| 81 | control.displaySettings.set( 'link', linkType ); |
72 | 82 | |
73 | 83 | control.model.set( _.extend( |
74 | 84 | control.mapMediaToModelProps( mediaProps ), |
… |
… |
|
130 | 140 | * @returns {Object} Preview template props. |
131 | 141 | */ |
132 | 142 | mapModelToPreviewTemplateProps: function mapModelToPreviewTemplateProps() { |
133 | | var control = this, mediaFrameProps, url; |
| 143 | var control = this, previewTemplateProps, url; |
134 | 144 | url = control.model.get( 'url' ); |
135 | | mediaFrameProps = component.MediaWidgetControl.prototype.mapModelToPreviewTemplateProps.call( control ); |
136 | | mediaFrameProps.currentFilename = url ? url.replace( /\?.*$/, '' ).replace( /^.+\//, '' ) : ''; |
137 | | return mediaFrameProps; |
| 145 | previewTemplateProps = component.MediaWidgetControl.prototype.mapModelToPreviewTemplateProps.call( control ); |
| 146 | previewTemplateProps.currentFilename = url ? url.replace( /\?.*$/, '' ).replace( /^.+\//, '' ) : ''; |
| 147 | previewTemplateProps.link_url = control.model.get( 'link_url' ); |
| 148 | return previewTemplateProps; |
138 | 149 | } |
139 | 150 | }); |
140 | 151 | |
diff --git src/wp-admin/js/widgets/media-widgets.js src/wp-admin/js/widgets/media-widgets.js
index e922670a2c..2e0a8099ca 100644
|
|
wp.mediaWidgets = ( function( $ ) { |
513 | 513 | }); |
514 | 514 | }); |
515 | 515 | |
| 516 | // Update link_url attribute. |
| 517 | control.$el.on( 'input change', '.link', function updateExtra() { |
| 518 | var linkUrl = $.trim( $( this ).val() ), linkType = 'custom'; |
| 519 | if ( control.selectedAttachment.get( 'linkUrl' ) === linkUrl || control.selectedAttachment.get( 'link' ) === linkUrl ) { |
| 520 | linkType = 'post'; |
| 521 | } else if ( control.selectedAttachment.get( 'url' ) === linkUrl ) { |
| 522 | linkType = 'file'; |
| 523 | } |
| 524 | control.model.set( { |
| 525 | link_url: linkUrl, |
| 526 | link_type: linkType |
| 527 | }); |
| 528 | |
| 529 | // Update display settings for the next time the user opens to select from the media library. |
| 530 | control.displaySettings.set( { |
| 531 | link: linkType, |
| 532 | linkUrl: linkUrl |
| 533 | }); |
| 534 | }); |
| 535 | |
516 | 536 | /* |
517 | 537 | * Copy current display settings from the widget model to serve as basis |
518 | 538 | * of customized display settings for the current media frame session. |
… |
… |
wp.mediaWidgets = ( function( $ ) { |
819 | 839 | } |
820 | 840 | |
821 | 841 | if ( 'post' === mediaFrameProps.link ) { |
822 | | modelProps.link_url = mediaFrameProps.postUrl; |
| 842 | modelProps.link_url = mediaFrameProps.postUrl || mediaFrameProps.linkUrl; |
823 | 843 | } else if ( 'file' === mediaFrameProps.link ) { |
824 | 844 | modelProps.link_url = mediaFrameProps.url; |
825 | 845 | } |
diff --git src/wp-includes/widgets/class-wp-widget-media-image.php src/wp-includes/widgets/class-wp-widget-media-image.php
index 40485a595d..b9dc8307f1 100644
|
|
class WP_Widget_Media_Image extends WP_Widget_Media { |
95 | 95 | 'default' => 'none', |
96 | 96 | 'media_prop' => 'link', |
97 | 97 | 'description' => __( 'Link To' ), |
98 | | 'should_preview_update' => false, |
| 98 | 'should_preview_update' => true, |
99 | 99 | ), |
100 | 100 | 'link_url' => array( |
101 | 101 | 'type' => 'string', |
… |
… |
class WP_Widget_Media_Image extends WP_Widget_Media { |
103 | 103 | 'format' => 'uri', |
104 | 104 | 'media_prop' => 'linkUrl', |
105 | 105 | 'description' => __( 'URL' ), |
106 | | 'should_preview_update' => false, |
| 106 | 'should_preview_update' => true, |
107 | 107 | ), |
108 | 108 | 'image_classes' => array( |
109 | 109 | 'type' => 'string', |
… |
… |
class WP_Widget_Media_Image extends WP_Widget_Media { |
307 | 307 | parent::render_control_template_scripts(); |
308 | 308 | |
309 | 309 | ?> |
| 310 | <script type="text/html" id="tmpl-wp-media-widget-image-fields"> |
| 311 | <# var elementIdPrefix = 'el' + String( Math.random() ) + '_'; #> |
| 312 | <# if ( data.url ) { #> |
| 313 | <p class="media-widget-image-link"> |
| 314 | <label for="{{ elementIdPrefix }}linkUrl"><?php esc_html_e( 'Link to:' ); ?></label> |
| 315 | <input id="{{ elementIdPrefix }}linkUrl" type="text" class="widefat link extra" value="{{ data.link_url }}" placeholder="http://"> |
| 316 | </p> |
| 317 | <# } #> |
| 318 | </script> |
310 | 319 | <script type="text/html" id="tmpl-wp-media-widget-image-preview"> |
311 | | <# |
312 | | var describedById = 'describedBy-' + String( Math.random() ); |
313 | | #> |
| 320 | <# var describedById = 'describedBy-' + String( Math.random() ); #> |
314 | 321 | <# if ( data.error && 'missing_attachment' === data.error ) { #> |
315 | 322 | <div class="notice notice-error notice-alt notice-missing-attachment"> |
316 | 323 | <p><?php echo $this->l10n['missing_attachment']; ?></p> |
diff --git src/wp-includes/widgets/class-wp-widget-media.php src/wp-includes/widgets/class-wp-widget-media.php
index 59afe7d082..84c9b3ff92 100644
|
|
abstract class WP_Widget_Media extends WP_Widget { |
404 | 404 | <?php echo esc_html( $this->l10n['add_media'] ); ?> |
405 | 405 | </button> |
406 | 406 | </p> |
| 407 | <div class="media-widget-fields"> |
| 408 | </div> |
407 | 409 | </script> |
408 | 410 | <?php |
409 | 411 | } |