2012-11-21 17:46:32 +01:00
|
|
|
// WordPress, TinyMCE, and Media
|
|
|
|
// -----------------------------
|
|
|
|
(function($){
|
|
|
|
// Stores the editors' `wp.media.controller.Frame` instances.
|
2012-11-21 17:50:58 +01:00
|
|
|
var workflows = {};
|
2012-11-21 17:46:32 +01:00
|
|
|
|
|
|
|
wp.media.string = {
|
|
|
|
// Joins the `props` and `attachment` objects,
|
|
|
|
// outputting the proper object format based on the
|
|
|
|
// attachment's type.
|
|
|
|
props: function( props, attachment ) {
|
2013-01-04 09:02:16 +01:00
|
|
|
var link, linkUrl, size, sizes, fallbacks,
|
|
|
|
defaultProps = wp.media.view.settings.defaultProps;
|
2012-11-30 02:38:41 +01:00
|
|
|
|
|
|
|
// Final fallbacks run after all processing has been completed.
|
|
|
|
fallbacks = function( props ) {
|
|
|
|
// Generate alt fallbacks and strip tags.
|
|
|
|
if ( 'image' === props.type && ! props.alt ) {
|
|
|
|
props.alt = props.caption || props.title || '';
|
|
|
|
props.alt = props.alt.replace( /<\/?[^>]+>/g, '' );
|
2013-01-14 18:34:45 +01:00
|
|
|
props.alt = props.alt.replace( /[\r\n]+/g, ' ' );
|
2012-11-30 02:38:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return props;
|
|
|
|
};
|
2012-11-21 17:46:32 +01:00
|
|
|
|
|
|
|
props = props ? _.clone( props ) : {};
|
|
|
|
|
|
|
|
if ( attachment && attachment.type )
|
|
|
|
props.type = attachment.type;
|
|
|
|
|
|
|
|
if ( 'image' === props.type ) {
|
|
|
|
props = _.defaults( props || {}, {
|
2013-01-04 09:02:16 +01:00
|
|
|
align: defaultProps.align || getUserSetting( 'align', 'none' ),
|
|
|
|
size: defaultProps.size || getUserSetting( 'imgsize', 'medium' ),
|
2012-11-21 17:46:32 +01:00
|
|
|
url: '',
|
|
|
|
classes: []
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// All attachment-specific settings follow.
|
|
|
|
if ( ! attachment )
|
2012-11-30 02:38:41 +01:00
|
|
|
return fallbacks( props );
|
|
|
|
|
|
|
|
props.title = props.title || attachment.title;
|
2012-11-21 17:46:32 +01:00
|
|
|
|
2013-01-04 09:02:16 +01:00
|
|
|
link = props.link || defaultProps.link || getUserSetting( 'urlbutton', 'file' );
|
2013-07-23 07:39:08 +02:00
|
|
|
if ( 'file' === link || 'embed' === link )
|
2012-11-21 17:46:32 +01:00
|
|
|
linkUrl = attachment.url;
|
|
|
|
else if ( 'post' === link )
|
|
|
|
linkUrl = attachment.link;
|
|
|
|
else if ( 'custom' === link )
|
|
|
|
linkUrl = props.linkUrl;
|
|
|
|
props.linkUrl = linkUrl || '';
|
|
|
|
|
|
|
|
// Format properties for images.
|
|
|
|
if ( 'image' === attachment.type ) {
|
|
|
|
props.classes.push( 'wp-image-' + attachment.id );
|
|
|
|
|
|
|
|
sizes = attachment.sizes;
|
|
|
|
size = sizes && sizes[ props.size ] ? sizes[ props.size ] : attachment;
|
|
|
|
|
2012-11-29 22:23:55 +01:00
|
|
|
_.extend( props, _.pick( attachment, 'align', 'caption', 'alt' ), {
|
2012-11-21 17:46:32 +01:00
|
|
|
width: size.width,
|
|
|
|
height: size.height,
|
|
|
|
src: size.url,
|
|
|
|
captionId: 'attachment_' + attachment.id
|
|
|
|
});
|
2013-03-16 06:25:44 +01:00
|
|
|
} else if ( 'video' === attachment.type || 'audio' === attachment.type ) {
|
|
|
|
_.extend( props, _.pick( attachment, 'title', 'type', 'icon', 'mime' ) );
|
2012-11-21 17:46:32 +01:00
|
|
|
// Format properties for non-images.
|
|
|
|
} else {
|
2012-11-30 02:38:41 +01:00
|
|
|
props.title = props.title || attachment.filename;
|
|
|
|
props.rel = props.rel || 'attachment wp-att-' + attachment.id;
|
2012-11-21 17:46:32 +01:00
|
|
|
}
|
|
|
|
|
2012-11-30 02:38:41 +01:00
|
|
|
return fallbacks( props );
|
2012-11-21 17:46:32 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
link: function( props, attachment ) {
|
|
|
|
var options;
|
|
|
|
|
|
|
|
props = wp.media.string.props( props, attachment );
|
|
|
|
|
|
|
|
options = {
|
|
|
|
tag: 'a',
|
|
|
|
content: props.title,
|
|
|
|
attrs: {
|
|
|
|
href: props.linkUrl
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( props.rel )
|
|
|
|
options.attrs.rel = props.rel;
|
|
|
|
|
|
|
|
return wp.html.string( options );
|
|
|
|
},
|
|
|
|
|
2013-03-16 06:25:44 +01:00
|
|
|
audio: function( props, attachment ) {
|
2013-07-23 07:39:08 +02:00
|
|
|
return wp.media.string._audioVideo( 'audio', props, attachment );
|
2013-03-16 06:25:44 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
video: function( props, attachment ) {
|
2013-07-23 07:39:08 +02:00
|
|
|
return wp.media.string._audioVideo( 'video', props, attachment );
|
|
|
|
},
|
|
|
|
|
|
|
|
_audioVideo: function( type, props, attachment ) {
|
|
|
|
var shortcode, html, extension;
|
2013-03-16 06:25:44 +01:00
|
|
|
|
|
|
|
props = wp.media.string.props( props, attachment );
|
2013-07-23 07:39:08 +02:00
|
|
|
if ( props.link !== 'embed' )
|
|
|
|
return wp.media.string.link( props );
|
2013-03-16 06:25:44 +01:00
|
|
|
|
|
|
|
shortcode = {};
|
|
|
|
|
2013-07-23 07:39:08 +02:00
|
|
|
if ( 'video' === type ) {
|
|
|
|
if ( attachment.width )
|
|
|
|
shortcode.width = attachment.width;
|
|
|
|
|
|
|
|
if ( attachment.height )
|
|
|
|
shortcode.height = attachment.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
extension = attachment.filename.split('.').pop();
|
|
|
|
|
|
|
|
if ( _.contains( wp.media.view.settings.embedExts, extension ) ) {
|
|
|
|
shortcode[extension] = attachment.url;
|
|
|
|
} else {
|
|
|
|
// Render unsupported audio and video files as links.
|
|
|
|
return wp.media.string.link( props );
|
2013-03-16 06:25:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
html = wp.shortcode.string({
|
2013-07-23 07:39:08 +02:00
|
|
|
tag: type,
|
2013-03-16 06:25:44 +01:00
|
|
|
attrs: shortcode
|
|
|
|
});
|
|
|
|
|
|
|
|
return html;
|
|
|
|
},
|
|
|
|
|
2012-11-21 17:46:32 +01:00
|
|
|
image: function( props, attachment ) {
|
|
|
|
var img = {},
|
|
|
|
options, classes, shortcode, html;
|
|
|
|
|
|
|
|
props = wp.media.string.props( props, attachment );
|
|
|
|
classes = props.classes || [];
|
|
|
|
|
2013-04-17 19:57:51 +02:00
|
|
|
img.src = typeof attachment !== 'undefined' ? attachment.url : props.url;
|
2012-11-21 17:46:32 +01:00
|
|
|
_.extend( img, _.pick( props, 'width', 'height', 'alt' ) );
|
|
|
|
|
|
|
|
// Only assign the align class to the image if we're not printing
|
|
|
|
// a caption, since the alignment is sent to the shortcode.
|
|
|
|
if ( props.align && ! props.caption )
|
|
|
|
classes.push( 'align' + props.align );
|
|
|
|
|
|
|
|
if ( props.size )
|
|
|
|
classes.push( 'size-' + props.size );
|
|
|
|
|
|
|
|
img['class'] = _.compact( classes ).join(' ');
|
|
|
|
|
|
|
|
// Generate `img` tag options.
|
|
|
|
options = {
|
|
|
|
tag: 'img',
|
|
|
|
attrs: img,
|
|
|
|
single: true
|
|
|
|
};
|
|
|
|
|
|
|
|
// Generate the `a` element options, if they exist.
|
|
|
|
if ( props.linkUrl ) {
|
|
|
|
options = {
|
|
|
|
tag: 'a',
|
|
|
|
attrs: {
|
|
|
|
href: props.linkUrl
|
|
|
|
},
|
|
|
|
content: options
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
html = wp.html.string( options );
|
|
|
|
|
|
|
|
// Generate the caption shortcode.
|
|
|
|
if ( props.caption ) {
|
|
|
|
shortcode = {};
|
|
|
|
|
|
|
|
if ( img.width )
|
|
|
|
shortcode.width = img.width;
|
|
|
|
|
|
|
|
if ( props.captionId )
|
|
|
|
shortcode.id = props.captionId;
|
|
|
|
|
|
|
|
if ( props.align )
|
|
|
|
shortcode.align = 'align' + props.align;
|
|
|
|
|
|
|
|
html = wp.shortcode.string({
|
|
|
|
tag: 'caption',
|
|
|
|
attrs: shortcode,
|
|
|
|
content: html + ' ' + props.caption
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return html;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
wp.media.gallery = (function() {
|
|
|
|
var galleries = {};
|
|
|
|
|
|
|
|
return {
|
|
|
|
defaults: {
|
|
|
|
order: 'ASC',
|
2012-12-03 08:17:10 +01:00
|
|
|
id: wp.media.view.settings.post.id,
|
2012-11-21 17:46:32 +01:00
|
|
|
itemtag: 'dl',
|
|
|
|
icontag: 'dt',
|
|
|
|
captiontag: 'dd',
|
2013-01-03 07:09:09 +01:00
|
|
|
columns: '3',
|
2013-05-03 00:07:37 +02:00
|
|
|
link: 'post',
|
2012-11-28 00:20:12 +01:00
|
|
|
size: 'thumbnail',
|
|
|
|
orderby: 'menu_order ID'
|
2012-11-21 17:46:32 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
attachments: function( shortcode ) {
|
|
|
|
var shortcodeString = shortcode.string(),
|
|
|
|
result = galleries[ shortcodeString ],
|
|
|
|
attrs, args, query, others;
|
|
|
|
|
|
|
|
delete galleries[ shortcodeString ];
|
|
|
|
|
|
|
|
if ( result )
|
|
|
|
return result;
|
|
|
|
|
2012-11-28 00:20:12 +01:00
|
|
|
// Fill the default shortcode attributes.
|
|
|
|
attrs = _.defaults( shortcode.attrs.named, wp.media.gallery.defaults );
|
2012-11-21 17:46:32 +01:00
|
|
|
args = _.pick( attrs, 'orderby', 'order' );
|
|
|
|
|
|
|
|
args.type = 'image';
|
|
|
|
args.perPage = -1;
|
|
|
|
|
2012-12-03 06:32:25 +01:00
|
|
|
// Mark the `orderby` override attribute.
|
2013-10-07 21:02:10 +02:00
|
|
|
if( undefined !== attrs.orderby )
|
|
|
|
attrs._orderByField = attrs.orderby;
|
|
|
|
|
2012-12-03 06:32:25 +01:00
|
|
|
if ( 'rand' === attrs.orderby )
|
|
|
|
attrs._orderbyRandom = true;
|
|
|
|
|
2012-11-28 00:20:12 +01:00
|
|
|
// Map the `orderby` attribute to the corresponding model property.
|
|
|
|
if ( ! attrs.orderby || /^menu_order(?: ID)?$/i.test( attrs.orderby ) )
|
|
|
|
args.orderby = 'menuOrder';
|
|
|
|
|
2012-11-21 17:46:32 +01:00
|
|
|
// Map the `ids` param to the correct query args.
|
|
|
|
if ( attrs.ids ) {
|
|
|
|
args.post__in = attrs.ids.split(',');
|
|
|
|
args.orderby = 'post__in';
|
|
|
|
} else if ( attrs.include ) {
|
|
|
|
args.post__in = attrs.include.split(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( attrs.exclude )
|
|
|
|
args.post__not_in = attrs.exclude.split(',');
|
|
|
|
|
|
|
|
if ( ! args.post__in )
|
2012-11-30 16:11:44 +01:00
|
|
|
args.uploadedTo = attrs.id;
|
2012-11-21 17:46:32 +01:00
|
|
|
|
|
|
|
// Collect the attributes that were not included in `args`.
|
2012-11-29 02:06:21 +01:00
|
|
|
others = _.omit( attrs, 'id', 'ids', 'include', 'exclude', 'orderby', 'order' );
|
2012-11-21 17:46:32 +01:00
|
|
|
|
2012-12-02 17:55:58 +01:00
|
|
|
query = wp.media.query( args );
|
2012-11-21 17:46:32 +01:00
|
|
|
query.gallery = new Backbone.Model( others );
|
|
|
|
return query;
|
|
|
|
},
|
|
|
|
|
|
|
|
shortcode: function( attachments ) {
|
|
|
|
var props = attachments.props.toJSON(),
|
2012-11-28 00:20:12 +01:00
|
|
|
attrs = _.pick( props, 'orderby', 'order' ),
|
2012-11-21 17:46:32 +01:00
|
|
|
shortcode, clone;
|
|
|
|
|
|
|
|
if ( attachments.gallery )
|
|
|
|
_.extend( attrs, attachments.gallery.toJSON() );
|
|
|
|
|
2012-11-28 00:20:12 +01:00
|
|
|
// Convert all gallery shortcodes to use the `ids` property.
|
|
|
|
// Ignore `post__in` and `post__not_in`; the attachments in
|
|
|
|
// the collection will already reflect those properties.
|
2012-11-21 17:46:32 +01:00
|
|
|
attrs.ids = attachments.pluck('id');
|
|
|
|
|
2012-11-30 16:11:44 +01:00
|
|
|
// Copy the `uploadedTo` post ID.
|
|
|
|
if ( props.uploadedTo )
|
|
|
|
attrs.id = props.uploadedTo;
|
2012-11-28 00:20:12 +01:00
|
|
|
|
2012-12-03 06:32:25 +01:00
|
|
|
// Check if the gallery is randomly ordered.
|
2013-10-07 21:02:10 +02:00
|
|
|
delete attrs.orderby;
|
|
|
|
|
2012-12-03 06:32:25 +01:00
|
|
|
if ( attrs._orderbyRandom )
|
|
|
|
attrs.orderby = 'rand';
|
2013-10-07 21:02:10 +02:00
|
|
|
else if ( attrs._orderByField && attrs._orderByField != 'rand' )
|
|
|
|
attrs.orderby = attrs._orderByField;
|
|
|
|
|
2012-12-03 06:32:25 +01:00
|
|
|
delete attrs._orderbyRandom;
|
2013-10-07 21:02:10 +02:00
|
|
|
delete attrs._orderByField;
|
2012-12-03 06:32:25 +01:00
|
|
|
|
2012-11-21 17:46:32 +01:00
|
|
|
// If the `ids` attribute is set and `orderby` attribute
|
|
|
|
// is the default value, clear it for cleaner output.
|
|
|
|
if ( attrs.ids && 'post__in' === attrs.orderby )
|
|
|
|
delete attrs.orderby;
|
|
|
|
|
|
|
|
// Remove default attributes from the shortcode.
|
|
|
|
_.each( wp.media.gallery.defaults, function( value, key ) {
|
|
|
|
if ( value === attrs[ key ] )
|
|
|
|
delete attrs[ key ];
|
|
|
|
});
|
|
|
|
|
|
|
|
shortcode = new wp.shortcode({
|
|
|
|
tag: 'gallery',
|
|
|
|
attrs: attrs,
|
|
|
|
type: 'single'
|
|
|
|
});
|
|
|
|
|
|
|
|
// Use a cloned version of the gallery.
|
|
|
|
clone = new wp.media.model.Attachments( attachments.models, {
|
|
|
|
props: props
|
|
|
|
});
|
|
|
|
clone.gallery = attachments.gallery;
|
|
|
|
galleries[ shortcode.string() ] = clone;
|
|
|
|
|
|
|
|
return shortcode;
|
|
|
|
},
|
|
|
|
|
|
|
|
edit: function( content ) {
|
|
|
|
var shortcode = wp.shortcode.next( 'gallery', content ),
|
|
|
|
defaultPostId = wp.media.gallery.defaults.id,
|
|
|
|
attachments, selection;
|
|
|
|
|
|
|
|
// Bail if we didn't match the shortcode or all of the content.
|
|
|
|
if ( ! shortcode || shortcode.content !== content )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Ignore the rest of the match object.
|
|
|
|
shortcode = shortcode.shortcode;
|
|
|
|
|
|
|
|
if ( _.isUndefined( shortcode.get('id') ) && ! _.isUndefined( defaultPostId ) )
|
|
|
|
shortcode.set( 'id', defaultPostId );
|
|
|
|
|
|
|
|
attachments = wp.media.gallery.attachments( shortcode );
|
|
|
|
|
|
|
|
selection = new wp.media.model.Selection( attachments.models, {
|
|
|
|
props: attachments.props.toJSON(),
|
|
|
|
multiple: true
|
|
|
|
});
|
|
|
|
|
|
|
|
selection.gallery = attachments.gallery;
|
|
|
|
|
|
|
|
// Fetch the query's attachments, and then break ties from the
|
|
|
|
// query to allow for sorting.
|
|
|
|
selection.more().done( function() {
|
|
|
|
// Break ties with the query.
|
|
|
|
selection.props.set({ query: false });
|
|
|
|
selection.unmirror();
|
|
|
|
selection.props.unset('orderby');
|
|
|
|
});
|
|
|
|
|
2012-11-28 00:20:12 +01:00
|
|
|
// Destroy the previous gallery frame.
|
|
|
|
if ( this.frame )
|
|
|
|
this.frame.dispose();
|
|
|
|
|
|
|
|
// Store the current gallery frame.
|
|
|
|
this.frame = wp.media({
|
2012-11-21 17:46:32 +01:00
|
|
|
frame: 'post',
|
|
|
|
state: 'gallery-edit',
|
|
|
|
title: wp.media.view.l10n.editGalleryTitle,
|
|
|
|
editing: true,
|
|
|
|
multiple: true,
|
|
|
|
selection: selection
|
2012-12-03 05:38:10 +01:00
|
|
|
}).open();
|
2012-11-28 00:20:12 +01:00
|
|
|
|
|
|
|
return this.frame;
|
2012-11-21 17:46:32 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}());
|
|
|
|
|
2012-12-05 10:43:10 +01:00
|
|
|
wp.media.featuredImage = {
|
|
|
|
get: function() {
|
|
|
|
return wp.media.view.settings.post.featuredImageId;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function( id ) {
|
|
|
|
var settings = wp.media.view.settings;
|
|
|
|
|
|
|
|
settings.post.featuredImageId = id;
|
|
|
|
|
|
|
|
wp.media.post( 'set-post-thumbnail', {
|
|
|
|
json: true,
|
|
|
|
post_id: settings.post.id,
|
|
|
|
thumbnail_id: settings.post.featuredImageId,
|
|
|
|
_wpnonce: settings.post.nonce
|
|
|
|
}).done( function( html ) {
|
|
|
|
$( '.inside', '#postimagediv' ).html( html );
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
frame: function() {
|
|
|
|
if ( this._frame )
|
|
|
|
return this._frame;
|
|
|
|
|
|
|
|
this._frame = wp.media({
|
|
|
|
state: 'featured-image',
|
|
|
|
states: [ new wp.media.controller.FeaturedImage() ]
|
|
|
|
});
|
|
|
|
|
|
|
|
this._frame.on( 'toolbar:create:featured-image', function( toolbar ) {
|
|
|
|
this.createSelectToolbar( toolbar, {
|
|
|
|
text: wp.media.view.l10n.setFeaturedImage
|
|
|
|
});
|
|
|
|
}, this._frame );
|
|
|
|
|
|
|
|
this._frame.state('featured-image').on( 'select', this.select );
|
|
|
|
return this._frame;
|
|
|
|
},
|
|
|
|
|
|
|
|
select: function() {
|
|
|
|
var settings = wp.media.view.settings,
|
|
|
|
selection = this.get('selection').single();
|
|
|
|
|
|
|
|
if ( ! settings.post.featuredImageId )
|
|
|
|
return;
|
|
|
|
|
|
|
|
wp.media.featuredImage.set( selection ? selection.id : -1 );
|
|
|
|
},
|
|
|
|
|
|
|
|
init: function() {
|
|
|
|
// Open the content media manager to the 'featured image' tab when
|
|
|
|
// the post thumbnail is clicked.
|
|
|
|
$('#postimagediv').on( 'click', '#set-post-thumbnail', function( event ) {
|
|
|
|
event.preventDefault();
|
|
|
|
// Stop propagation to prevent thickbox from activating.
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
|
|
wp.media.featuredImage.frame().open();
|
|
|
|
|
|
|
|
// Update the featured image id when the 'remove' link is clicked.
|
|
|
|
}).on( 'click', '#remove-post-thumbnail', function() {
|
|
|
|
wp.media.view.settings.post.featuredImageId = -1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$( wp.media.featuredImage.init );
|
|
|
|
|
2012-11-21 17:46:32 +01:00
|
|
|
wp.media.editor = {
|
2012-11-21 18:01:40 +01:00
|
|
|
insert: function( h ) {
|
|
|
|
var mce = typeof(tinymce) != 'undefined',
|
|
|
|
qt = typeof(QTags) != 'undefined',
|
|
|
|
wpActiveEditor = window.wpActiveEditor,
|
|
|
|
ed;
|
|
|
|
|
|
|
|
// Delegate to the global `send_to_editor` if it exists.
|
|
|
|
// This attempts to play nice with any themes/plugins that have
|
|
|
|
// overridden the insert functionality.
|
|
|
|
if ( window.send_to_editor )
|
|
|
|
return window.send_to_editor.apply( this, arguments );
|
|
|
|
|
|
|
|
if ( ! wpActiveEditor ) {
|
|
|
|
if ( mce && tinymce.activeEditor ) {
|
|
|
|
ed = tinymce.activeEditor;
|
|
|
|
wpActiveEditor = window.wpActiveEditor = ed.id;
|
|
|
|
} else if ( !qt ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if ( mce ) {
|
|
|
|
if ( tinymce.activeEditor && (tinymce.activeEditor.id == 'mce_fullscreen' || tinymce.activeEditor.id == 'wp_mce_fullscreen') )
|
|
|
|
ed = tinymce.activeEditor;
|
|
|
|
else
|
|
|
|
ed = tinymce.get(wpActiveEditor);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ed && !ed.isHidden() ) {
|
|
|
|
// restore caret position on IE
|
|
|
|
if ( tinymce.isIE && ed.windowManager.insertimagebookmark )
|
|
|
|
ed.selection.moveToBookmark(ed.windowManager.insertimagebookmark);
|
|
|
|
|
2012-12-05 01:59:25 +01:00
|
|
|
if ( h.indexOf('[caption') !== -1 ) {
|
2012-11-21 18:01:40 +01:00
|
|
|
if ( ed.wpSetImgCaption )
|
|
|
|
h = ed.wpSetImgCaption(h);
|
2012-12-05 01:59:25 +01:00
|
|
|
} else if ( h.indexOf('[gallery') !== -1 ) {
|
2012-11-21 18:01:40 +01:00
|
|
|
if ( ed.plugins.wpgallery )
|
|
|
|
h = ed.plugins.wpgallery._do_gallery(h);
|
|
|
|
} else if ( h.indexOf('[embed') === 0 ) {
|
|
|
|
if ( ed.plugins.wordpress )
|
|
|
|
h = ed.plugins.wordpress._setEmbed(h);
|
|
|
|
}
|
|
|
|
|
|
|
|
ed.execCommand('mceInsertContent', false, h);
|
|
|
|
} else if ( qt ) {
|
|
|
|
QTags.insertContent(h);
|
|
|
|
} else {
|
|
|
|
document.getElementById(wpActiveEditor).value += h;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the old thickbox remove function exists, call it in case
|
|
|
|
// a theme/plugin overloaded it.
|
|
|
|
if ( window.tb_remove )
|
|
|
|
try { window.tb_remove(); } catch( e ) {}
|
|
|
|
},
|
2012-11-21 17:46:32 +01:00
|
|
|
|
|
|
|
add: function( id, options ) {
|
|
|
|
var workflow = this.get( id );
|
|
|
|
|
2013-06-19 23:55:17 +02:00
|
|
|
if ( workflow ) // only add once: if exists return existing
|
|
|
|
return workflow;
|
|
|
|
|
2012-11-21 17:46:32 +01:00
|
|
|
workflow = workflows[ id ] = wp.media( _.defaults( options || {}, {
|
|
|
|
frame: 'post',
|
2012-12-04 02:26:03 +01:00
|
|
|
state: 'insert',
|
2012-11-28 18:18:50 +01:00
|
|
|
title: wp.media.view.l10n.addMedia,
|
2012-11-21 17:46:32 +01:00
|
|
|
multiple: true
|
|
|
|
} ) );
|
|
|
|
|
|
|
|
workflow.on( 'insert', function( selection ) {
|
2012-11-22 07:30:25 +01:00
|
|
|
var state = workflow.state();
|
2012-11-21 17:46:32 +01:00
|
|
|
|
|
|
|
selection = selection || state.get('selection');
|
|
|
|
|
2012-11-22 07:30:25 +01:00
|
|
|
if ( ! selection )
|
2012-11-21 17:46:32 +01:00
|
|
|
return;
|
|
|
|
|
2012-12-04 05:38:42 +01:00
|
|
|
$.when.apply( $, selection.map( function( attachment ) {
|
2012-11-22 07:30:25 +01:00
|
|
|
var display = state.display( attachment ).toJSON();
|
2012-12-04 05:38:42 +01:00
|
|
|
return this.send.attachment( display, attachment.toJSON() );
|
|
|
|
}, this ) ).done( function() {
|
2012-12-04 21:52:58 +01:00
|
|
|
wp.media.editor.insert( _.toArray( arguments ).join("\n\n") );
|
2012-12-04 05:38:42 +01:00
|
|
|
});
|
2012-11-21 17:46:32 +01:00
|
|
|
}, this );
|
|
|
|
|
2012-11-30 16:19:11 +01:00
|
|
|
workflow.state('gallery-edit').on( 'update', function( selection ) {
|
2012-11-21 17:46:32 +01:00
|
|
|
this.insert( wp.media.gallery.shortcode( selection ).string() );
|
|
|
|
}, this );
|
|
|
|
|
2012-11-30 16:19:11 +01:00
|
|
|
workflow.state('embed').on( 'select', function() {
|
2012-12-04 02:26:03 +01:00
|
|
|
var state = workflow.state(),
|
|
|
|
type = state.get('type'),
|
|
|
|
embed = state.props.toJSON();
|
2012-11-21 17:46:32 +01:00
|
|
|
|
|
|
|
embed.url = embed.url || '';
|
|
|
|
|
2012-12-04 02:26:03 +01:00
|
|
|
if ( 'link' === type ) {
|
2012-11-21 17:46:32 +01:00
|
|
|
_.defaults( embed, {
|
|
|
|
title: embed.url,
|
|
|
|
linkUrl: embed.url
|
|
|
|
});
|
|
|
|
|
2012-12-04 05:38:42 +01:00
|
|
|
this.send.link( embed ).done( function( resp ) {
|
|
|
|
wp.media.editor.insert( resp );
|
|
|
|
});
|
2012-11-21 17:46:32 +01:00
|
|
|
|
2012-12-04 02:26:03 +01:00
|
|
|
} else if ( 'image' === type ) {
|
2012-11-21 17:46:32 +01:00
|
|
|
_.defaults( embed, {
|
|
|
|
title: embed.url,
|
|
|
|
linkUrl: '',
|
|
|
|
align: 'none',
|
|
|
|
link: 'none'
|
|
|
|
});
|
|
|
|
|
|
|
|
if ( 'none' === embed.link )
|
|
|
|
embed.linkUrl = '';
|
|
|
|
else if ( 'file' === embed.link )
|
|
|
|
embed.linkUrl = embed.url;
|
|
|
|
|
|
|
|
this.insert( wp.media.string.image( embed ) );
|
|
|
|
}
|
|
|
|
}, this );
|
|
|
|
|
2012-12-05 10:43:10 +01:00
|
|
|
workflow.state('featured-image').on( 'select', wp.media.featuredImage.select );
|
2012-12-03 03:38:10 +01:00
|
|
|
workflow.setState( workflow.options.state );
|
2012-11-21 17:46:32 +01:00
|
|
|
return workflow;
|
|
|
|
},
|
|
|
|
|
2012-12-03 03:38:10 +01:00
|
|
|
id: function( id ) {
|
|
|
|
if ( id )
|
|
|
|
return id;
|
|
|
|
|
|
|
|
// If an empty `id` is provided, default to `wpActiveEditor`.
|
|
|
|
id = wpActiveEditor;
|
|
|
|
|
|
|
|
// If that doesn't work, fall back to `tinymce.activeEditor.id`.
|
|
|
|
if ( ! id && typeof tinymce !== 'undefined' && tinymce.activeEditor )
|
|
|
|
id = tinymce.activeEditor.id;
|
|
|
|
|
|
|
|
// Last but not least, fall back to the empty string.
|
|
|
|
id = id || '';
|
|
|
|
return id;
|
|
|
|
},
|
|
|
|
|
2012-11-21 17:46:32 +01:00
|
|
|
get: function( id ) {
|
2012-12-03 03:38:10 +01:00
|
|
|
id = this.id( id );
|
2012-11-21 17:46:32 +01:00
|
|
|
return workflows[ id ];
|
|
|
|
},
|
|
|
|
|
|
|
|
remove: function( id ) {
|
2012-12-03 03:38:10 +01:00
|
|
|
id = this.id( id );
|
2012-11-21 17:46:32 +01:00
|
|
|
delete workflows[ id ];
|
|
|
|
},
|
|
|
|
|
|
|
|
send: {
|
|
|
|
attachment: function( props, attachment ) {
|
|
|
|
var caption = attachment.caption,
|
|
|
|
options, html;
|
|
|
|
|
|
|
|
// If captions are disabled, clear the caption.
|
|
|
|
if ( ! wp.media.view.settings.captions )
|
|
|
|
delete attachment.caption;
|
|
|
|
|
|
|
|
props = wp.media.string.props( props, attachment );
|
|
|
|
|
|
|
|
options = {
|
Restore the Description field to the media UI in 3.5.
We tried in vain -- a noble but ultimately failed effort -- to reduce the number of fields for attachments from four (title, caption, alt, description) to one (caption for images, title otherwise). Alternative text needed to stay for accessibility reasons, of course.
Eventually title returned due to heavy plugin reliance. Description is too used by too many plugins (often times incorrectly -- the caption is more likely the proper field), hence its less-than-triumphant return today.
Version 3.5 has tried to streamline media in a number of ways. Removing fields may have been too much at once, as it forced not only a user interface change, but a paradigm change as well.
Finally, on upload we populate the description field with IPTC/EXIF captions, rather than the caption field. See #22768, this should be fixed. For now, Description stays.
This commit also restores 'Title' attribute editing to the main tab of the Edit Image dialog. The "Title" field no longer populates title attributes for <img> tags by design (for accessibility and other purposes, see #18984). So, here is a more obvious 'workaround' for the tooltip community.
Finally, this:
* Cleans up the post.php attachment editor, including by showing a prettier form of the mime type.
* Enables plugins to specifically hide attachment_fields_to_edit from either post.php (where you can create meta boxes) or the modal (which you may not want to clutter), for compatibility reasons.
* Hides the 'Describe this file...' placeholder when a field is read-only in the modal.
props nacin, helenyhou.
fixes #22759.
git-svn-id: http://core.svn.wordpress.org/trunk@23083 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2012-12-06 01:41:06 +01:00
|
|
|
id: attachment.id,
|
2012-12-06 04:36:17 +01:00
|
|
|
post_content: attachment.description,
|
2012-12-07 02:13:04 +01:00
|
|
|
post_excerpt: caption
|
2012-11-21 17:46:32 +01:00
|
|
|
};
|
|
|
|
|
2012-11-26 04:33:30 +01:00
|
|
|
if ( props.linkUrl )
|
|
|
|
options.url = props.linkUrl;
|
|
|
|
|
2012-11-21 17:46:32 +01:00
|
|
|
if ( 'image' === attachment.type ) {
|
|
|
|
html = wp.media.string.image( props );
|
|
|
|
|
|
|
|
_.each({
|
2012-11-26 23:08:50 +01:00
|
|
|
align: 'align',
|
2012-11-26 04:33:30 +01:00
|
|
|
size: 'image-size',
|
2012-11-26 23:08:50 +01:00
|
|
|
alt: 'image_alt'
|
2012-11-21 17:46:32 +01:00
|
|
|
}, function( option, prop ) {
|
|
|
|
if ( props[ prop ] )
|
|
|
|
options[ option ] = props[ prop ];
|
|
|
|
});
|
2013-03-16 06:25:44 +01:00
|
|
|
} else if ( 'video' === attachment.type ) {
|
2013-03-29 21:35:23 +01:00
|
|
|
html = wp.media.string.video( props, attachment );
|
2013-03-16 06:25:44 +01:00
|
|
|
} else if ( 'audio' === attachment.type ) {
|
2013-03-29 21:35:23 +01:00
|
|
|
html = wp.media.string.audio( props, attachment );
|
2012-11-21 17:46:32 +01:00
|
|
|
} else {
|
|
|
|
html = wp.media.string.link( props );
|
2012-11-26 23:08:50 +01:00
|
|
|
options.post_title = props.title;
|
2012-11-21 17:46:32 +01:00
|
|
|
}
|
|
|
|
|
2012-12-02 17:55:58 +01:00
|
|
|
return wp.media.post( 'send-attachment-to-editor', {
|
2012-11-21 17:46:32 +01:00
|
|
|
nonce: wp.media.view.settings.nonce.sendToEditor,
|
|
|
|
attachment: options,
|
2012-11-26 17:34:42 +01:00
|
|
|
html: html,
|
2012-12-03 08:17:10 +01:00
|
|
|
post_id: wp.media.view.settings.post.id
|
2012-11-21 17:46:32 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
link: function( embed ) {
|
2012-12-02 17:55:58 +01:00
|
|
|
return wp.media.post( 'send-link-to-editor', {
|
2012-11-27 15:58:08 +01:00
|
|
|
nonce: wp.media.view.settings.nonce.sendToEditor,
|
|
|
|
src: embed.linkUrl,
|
|
|
|
title: embed.title,
|
|
|
|
html: wp.media.string.link( embed ),
|
2012-12-03 08:17:10 +01:00
|
|
|
post_id: wp.media.view.settings.post.id
|
2012-11-21 17:46:32 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-04-25 07:43:14 +02:00
|
|
|
open: function( id, options ) {
|
2012-12-03 03:38:10 +01:00
|
|
|
var workflow, editor;
|
|
|
|
|
2013-04-25 07:43:14 +02:00
|
|
|
options = options || {};
|
|
|
|
|
2012-12-03 03:38:10 +01:00
|
|
|
id = this.id( id );
|
|
|
|
|
|
|
|
// Save a bookmark of the caret position in IE.
|
|
|
|
if ( typeof tinymce !== 'undefined' ) {
|
|
|
|
editor = tinymce.get( id );
|
|
|
|
|
|
|
|
if ( tinymce.isIE && editor && ! editor.isHidden() ) {
|
|
|
|
editor.focus();
|
|
|
|
editor.windowManager.insertimagebookmark = editor.selection.getBookmark();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
workflow = this.get( id );
|
|
|
|
|
2013-04-25 07:43:14 +02:00
|
|
|
// Redo workflow if state has changed
|
|
|
|
if ( ! workflow || ( workflow.options && options.state !== workflow.options.state ) )
|
|
|
|
workflow = this.add( id, options );
|
2012-12-03 03:38:10 +01:00
|
|
|
|
|
|
|
return workflow.open();
|
|
|
|
},
|
|
|
|
|
2012-11-21 17:46:32 +01:00
|
|
|
init: function() {
|
2012-11-27 20:09:00 +01:00
|
|
|
$(document.body).on( 'click', '.insert-media', function( event ) {
|
2012-11-21 17:46:32 +01:00
|
|
|
var $this = $(this),
|
2013-04-25 07:43:14 +02:00
|
|
|
editor = $this.data('editor'),
|
|
|
|
options = {
|
|
|
|
frame: 'post',
|
|
|
|
state: 'insert',
|
|
|
|
title: wp.media.view.l10n.addMedia,
|
|
|
|
multiple: true
|
|
|
|
};
|
2012-11-21 17:46:32 +01:00
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
// Remove focus from the `.insert-media` button.
|
|
|
|
// Prevents Opera from showing the outline of the button
|
|
|
|
// above the modal.
|
|
|
|
//
|
|
|
|
// See: http://core.trac.wordpress.org/ticket/22445
|
|
|
|
$this.blur();
|
|
|
|
|
2013-04-25 07:43:14 +02:00
|
|
|
if ( $this.hasClass( 'gallery' ) ) {
|
|
|
|
options.state = 'gallery';
|
|
|
|
options.title = wp.media.view.l10n.createGalleryTitle;
|
|
|
|
}
|
|
|
|
|
|
|
|
wp.media.editor.open( editor, options );
|
2012-11-27 20:09:00 +01:00
|
|
|
});
|
2012-11-21 17:46:32 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-12-03 03:38:10 +01:00
|
|
|
_.bindAll( wp.media.editor, 'open' );
|
2012-11-21 17:46:32 +01:00
|
|
|
$( wp.media.editor.init );
|
2012-11-27 20:09:00 +01:00
|
|
|
}(jQuery));
|