Media: Make the attachment object optional when using the wp.media.string methods. see #21390.

git-svn-id: https://develop.svn.wordpress.org/trunk@22758 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Daryl Koopersmith 2012-11-21 13:53:15 +00:00
parent b59a8855db
commit 4c2e0d6b45
1 changed files with 61 additions and 44 deletions

View File

@ -94,14 +94,14 @@ var tb_position;
var workflows = {}, var workflows = {},
linkToUrl; linkToUrl;
linkToUrl = function( attachment, props ) { linkToUrl = function( props, attachment ) {
var link = props.link, var link = props.link || getUserSetting( 'urlbutton', 'post' ),
url; url;
if ( 'file' === link ) if ( 'file' === link )
url = attachment.get('url'); url = attachment.url;
else if ( 'post' === link ) else if ( 'post' === link )
url = attachment.get('link'); url = attachment.link;
else if ( 'custom' === link ) else if ( 'custom' === link )
url = props.linkUrl; url = props.linkUrl;
@ -109,42 +109,63 @@ var tb_position;
}; };
wp.media.string = { wp.media.string = {
link: function( attachment, props ) { link: function( props, attachment ) {
var linkTo = getUserSetting( 'urlbutton', 'post' ), var options;
options = {
tag: 'a',
content: attachment.get('title') || attachment.get('filename'),
attrs: {
rel: 'attachment wp-att-' + attachment.id
}
};
options.attrs.href = linkToUrl( attachment, props ); props = _.defaults( props || {}, {
title: '',
linkUrl: ''
});
if ( attachment ) {
attachment = attachment.toJSON();
_.extend( props, {
title: attachment.title || attachment.filename,
linkUrl: linkToUrl( props, attachment ),
rel: 'attachment wp-att-' + attachment.id
});
}
options = {
tag: 'a',
content: props.title,
attrs: {
href: props.linkUrl
}
};
if ( props.rel )
options.attrs.rel = props.rel;
return wp.html.string( options ); return wp.html.string( options );
}, },
image: function( attachment, props ) { image: function( props, attachment ) {
var classes, img, options, size, shortcode, html; var classes = [],
img = {},
options, sizes, size, shortcode, html;
props = _.defaults( props || {}, { props = _.defaults( props || {}, {
img: {},
align: getUserSetting( 'align', 'none' ), align: getUserSetting( 'align', 'none' ),
size: getUserSetting( 'imgsize', 'medium' ), size: getUserSetting( 'imgsize', 'medium' )
link: getUserSetting( 'urlbutton', 'post' )
}); });
props.linkUrl = linkToUrl( attachment, props ); if ( attachment ) {
attachment = attachment.toJSON();
attachment = attachment.toJSON(); classes.push( 'wp-image-' + attachment.id );
img = _.clone( props.img ); sizes = attachment.sizes;
classes = img['class'] ? img['class'].split(/\s+/) : []; size = sizes && sizes[ props.size ] ? sizes[ props.size ] : attachment;
size = attachment.sizes ? attachment.sizes[ props.size ] : {};
if ( ! size ) { _.extend( props, _.pick( attachment, 'align', 'caption' ), {
delete props.size; width: size.width,
size = attachment; height: size.height,
src: size.url,
linkUrl: linkToUrl( props, attachment ),
captionId: 'attachment_' + attachment.id
});
} }
img.width = size.width; img.width = size.width;
@ -153,14 +174,12 @@ var tb_position;
// Only assign the align class to the image if we're not printing // Only assign the align class to the image if we're not printing
// a caption, since the alignment is sent to the shortcode. // a caption, since the alignment is sent to the shortcode.
if ( props.align && ! attachment.caption ) if ( props.align && ! props.caption )
classes.push( 'align' + props.align ); classes.push( 'align' + props.align );
if ( props.size ) if ( props.size )
classes.push( 'size-' + props.size ); classes.push( 'size-' + props.size );
classes.push( 'wp-image-' + attachment.id );
img['class'] = _.compact( classes ).join(' '); img['class'] = _.compact( classes ).join(' ');
// Generate `img` tag options. // Generate `img` tag options.
@ -170,17 +189,13 @@ var tb_position;
single: true single: true
}; };
// Generate the `href` based on the `link` property.
if ( props.linkUrl ) {
props.anchor = props.anchor || {};
props.anchor.href = props.linkUrl;
}
// Generate the `a` element options, if they exist. // Generate the `a` element options, if they exist.
if ( props.anchor ) { if ( props.linkUrl ) {
options = { options = {
tag: 'a', tag: 'a',
attrs: props.anchor, attrs: {
href: props.linkUrl
},
content: options content: options
}; };
} }
@ -188,19 +203,21 @@ var tb_position;
html = wp.html.string( options ); html = wp.html.string( options );
// Generate the caption shortcode. // Generate the caption shortcode.
if ( attachment.caption ) { if ( props.caption ) {
shortcode = { shortcode = {
id: 'attachment_' + attachment.id,
width: img.width width: img.width
}; };
if ( props.captionId )
shortcode.id = props.captionId;
if ( props.align ) if ( props.align )
shortcode.align = 'align' + props.align; shortcode.align = 'align' + props.align;
html = wp.shortcode.string({ html = wp.shortcode.string({
tag: 'caption', tag: 'caption',
attrs: shortcode, attrs: shortcode,
content: html + ' ' + attachment.caption content: html + ' ' + props.caption
}); });
} }
@ -380,9 +397,9 @@ var tb_position;
delete details[ attachment.cid ]; delete details[ attachment.cid ];
if ( 'image' === attachment.get('type') ) if ( 'image' === attachment.get('type') )
this.insert( wp.media.string.image( attachment, detail ) + ' ' ); this.insert( wp.media.string.image( detail, attachment ) + ' ' );
else else
this.insert( wp.media.string.link( attachment, detail ) + ' ' ); this.insert( wp.media.string.link( detail, attachment ) + ' ' );
}, this ); }, this );
}, this ); }, this );