From a830567a92a60739b57a7479bb6644776e215ee4 Mon Sep 17 00:00:00 2001 From: Daryl Koopersmith Date: Wed, 10 Oct 2012 21:54:21 +0000 Subject: [PATCH] Correctly insert non-image attachments as links. * Adds `wp.media.string.link( attachment )` for printing attachment links. * Adds the `link` attribute to `Attachment` models that corresponds to the link to the attachment post. see #21390, #21836. git-svn-id: https://develop.svn.wordpress.org/trunk@22170 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-admin/js/media-upload.js | 9 ++++++--- wp-includes/js/mce-view.js | 18 ++++++++++++++++++ wp-includes/media.php | 1 + 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/wp-admin/js/media-upload.js b/wp-admin/js/media-upload.js index caf609de44..4713d068a1 100644 --- a/wp-admin/js/media-upload.js +++ b/wp-admin/js/media-upload.js @@ -108,9 +108,12 @@ var tb_position; } ) ); workflow.on( 'update:insert', function( selection ) { - this.insert( '\n' + selection.map( function( attachment ) { - return wp.media.string.image( attachment ); - }).join('\n\n') + '\n' ); + this.insert( selection.map( function( attachment ) { + if ( 'image' === attachment.get('type') ) + return '\n' + wp.media.string.image( attachment ) + '\n'; + else + return wp.media.string.link( attachment ) + ' '; + }).join('') ); }, this ); workflow.on( 'update:gallery', function( selection ) { diff --git a/wp-includes/js/mce-view.js b/wp-includes/js/mce-view.js index 0c88864d2e..88425e0a8c 100644 --- a/wp-includes/js/mce-view.js +++ b/wp-includes/js/mce-view.js @@ -371,6 +371,24 @@ window.wp = window.wp || {}; var mceview = wp.mce.view; wp.media.string = {}; + + wp.media.string.link = function( attachment ) { + var linkTo = getUserSetting( 'urlbutton', 'post' ), + options = { + tag: 'a', + content: attachment.get('title') || attachment.get('filename'), + attrs: { + rel: 'attachment wp-att-' + attachment.id + } + }; + + // Attachments can be linked to attachment post pages or to the direct + // URL. `none` is not a valid option. + options.attrs.href = ( linkTo === 'file' ) ? attachment.get('url') : attachment.get('link'); + + return wp.html.string( options ); + }; + wp.media.string.image = function( attachment, props ) { var classes, img, options, size; diff --git a/wp-includes/media.php b/wp-includes/media.php index 6d72e3d30b..87762f7de2 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -1246,6 +1246,7 @@ function wp_prepare_attachment_for_js( $attachment ) { 'title' => $attachment->post_title, 'filename' => basename( $attachment->guid ), 'url' => $attachment_url, + 'link' => get_attachment_link( $attachment->ID ), 'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ), 'author' => $attachment->post_author, 'description' => $attachment->post_content,