From f891bb32f2ea9e7325ae1c8aa00b543a78615a0d Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Mon, 4 Jan 2016 00:32:56 +0000 Subject: [PATCH] Media: when inserting an attachment in the editor and it is not an image, ensure the link is set to something else than `none`. Props eherman24, azaozz. Fizes #35153 for trunk. git-svn-id: https://develop.svn.wordpress.org/trunk@36166 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/js/media-views.js | 23 ++++++++++++++++++- .../js/media/controllers/library.js | 23 ++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/js/media-views.js b/src/wp-includes/js/media-views.js index 5d2fe60562..9e08201541 100644 --- a/src/wp-includes/js/media-views.js +++ b/src/wp-includes/js/media-views.js @@ -1214,13 +1214,34 @@ Library = wp.media.controller.State.extend({ * @returns {Object} */ defaultDisplaySettings: function( attachment ) { - var settings = this._defaultDisplaySettings; + var settings = _.clone( this._defaultDisplaySettings ); + if ( settings.canEmbed = this.canEmbed( attachment ) ) { settings.link = 'embed'; + } else if ( ! this.isImageAttachment( attachment ) && settings.link === 'none' ) { + settings.link = 'file'; } + return settings; }, + /** + * Whether an attachment is image. + * + * @since 4.4.1 + * + * @param {wp.media.model.Attachment} attachment + * @returns {Boolean} + */ + isImageAttachment: function( attachment ) { + // If uploading, we know the filename but not the mime type. + if ( attachment.get('uploading') ) { + return /\.(jpe?g|png|gif)$/i.test( attachment.get('filename') ); + } + + return attachment.get('type') === 'image'; + }, + /** * Whether an attachment can be embedded (audio or video). * diff --git a/src/wp-includes/js/media/controllers/library.js b/src/wp-includes/js/media/controllers/library.js index e17a0cf69a..c16fab60b8 100644 --- a/src/wp-includes/js/media/controllers/library.js +++ b/src/wp-includes/js/media/controllers/library.js @@ -171,13 +171,34 @@ Library = wp.media.controller.State.extend({ * @returns {Object} */ defaultDisplaySettings: function( attachment ) { - var settings = this._defaultDisplaySettings; + var settings = _.clone( this._defaultDisplaySettings ); + if ( settings.canEmbed = this.canEmbed( attachment ) ) { settings.link = 'embed'; + } else if ( ! this.isImageAttachment( attachment ) && settings.link === 'none' ) { + settings.link = 'file'; } + return settings; }, + /** + * Whether an attachment is image. + * + * @since 4.4.1 + * + * @param {wp.media.model.Attachment} attachment + * @returns {Boolean} + */ + isImageAttachment: function( attachment ) { + // If uploading, we know the filename but not the mime type. + if ( attachment.get('uploading') ) { + return /\.(jpe?g|png|gif)$/i.test( attachment.get('filename') ); + } + + return attachment.get('type') === 'image'; + }, + /** * Whether an attachment can be embedded (audio or video). *