1 // WordPress, TinyMCE, and Media
2 // -----------------------------
4 // Stores the editors' `wp.media.controller.Frame` instances.
8 // Joins the `props` and `attachment` objects,
9 // outputting the proper object format based on the
11 props: function( props, attachment ) {
12 var link, linkUrl, size, sizes, fallbacks;
14 // Final fallbacks run after all processing has been completed.
15 fallbacks = function( props ) {
16 // Generate alt fallbacks and strip tags.
17 if ( 'image' === props.type && ! props.alt ) {
18 props.alt = props.caption || props.title || '';
19 props.alt = props.alt.replace( /<\/?[^>]+>/g, '' );
25 props = props ? _.clone( props ) : {};
27 if ( attachment && attachment.type )
28 props.type = attachment.type;
30 if ( 'image' === props.type ) {
31 props = _.defaults( props || {}, {
32 align: getUserSetting( 'align', 'none' ),
33 size: getUserSetting( 'imgsize', 'medium' ),
39 // All attachment-specific settings follow.
41 return fallbacks( props );
43 props.title = props.title || attachment.title;
45 link = props.link || getUserSetting( 'urlbutton', 'post' );
46 if ( 'file' === link )
47 linkUrl = attachment.url;
48 else if ( 'post' === link )
49 linkUrl = attachment.link;
50 else if ( 'custom' === link )
51 linkUrl = props.linkUrl;
52 props.linkUrl = linkUrl || '';
54 // Format properties for images.
55 if ( 'image' === attachment.type ) {
56 props.classes.push( 'wp-image-' + attachment.id );
58 sizes = attachment.sizes;
59 size = sizes && sizes[ props.size ] ? sizes[ props.size ] : attachment;
61 _.extend( props, _.pick( attachment, 'align', 'caption', 'alt' ), {
65 captionId: 'attachment_' + attachment.id
68 // Format properties for non-images.
70 props.title = props.title || attachment.filename;
71 props.rel = props.rel || 'attachment wp-att-' + attachment.id;
74 return fallbacks( props );
77 link: function( props, attachment ) {
80 props = wp.media.string.props( props, attachment );
91 options.attrs.rel = props.rel;
93 return wp.html.string( options );
96 image: function( props, attachment ) {
98 options, classes, shortcode, html;
100 props = wp.media.string.props( props, attachment );
101 classes = props.classes || [];
104 _.extend( img, _.pick( props, 'width', 'height', 'alt' ) );
106 // Only assign the align class to the image if we're not printing
107 // a caption, since the alignment is sent to the shortcode.
108 if ( props.align && ! props.caption )
109 classes.push( 'align' + props.align );
112 classes.push( 'size-' + props.size );
114 img['class'] = _.compact( classes ).join(' ');
116 // Generate `img` tag options.
123 // Generate the `a` element options, if they exist.
124 if ( props.linkUrl ) {
134 html = wp.html.string( options );
136 // Generate the caption shortcode.
137 if ( props.caption ) {
141 shortcode.width = img.width;
143 if ( props.captionId )
144 shortcode.id = props.captionId;
147 shortcode.align = 'align' + props.align;
149 html = wp.shortcode.string({
152 content: html + ' ' + props.caption
160 wp.media.gallery = (function() {
166 id: wp.media.view.settings.post.id,
172 orderby: 'menu_order ID'
175 attachments: function( shortcode ) {
176 var shortcodeString = shortcode.string(),
177 result = galleries[ shortcodeString ],
178 attrs, args, query, others;
180 delete galleries[ shortcodeString ];
185 // Fill the default shortcode attributes.
186 attrs = _.defaults( shortcode.attrs.named, wp.media.gallery.defaults );
187 args = _.pick( attrs, 'orderby', 'order' );
192 // Mark the `orderby` override attribute.
193 if ( 'rand' === attrs.orderby )
194 attrs._orderbyRandom = true;
196 // Map the `orderby` attribute to the corresponding model property.
197 if ( ! attrs.orderby || /^menu_order(?: ID)?$/i.test( attrs.orderby ) )
198 args.orderby = 'menuOrder';
200 // Map the `ids` param to the correct query args.
202 args.post__in = attrs.ids.split(',');
203 args.orderby = 'post__in';
204 } else if ( attrs.include ) {
205 args.post__in = attrs.include.split(',');
209 args.post__not_in = attrs.exclude.split(',');
211 if ( ! args.post__in )
212 args.uploadedTo = attrs.id;
214 // Collect the attributes that were not included in `args`.
215 others = _.omit( attrs, 'id', 'ids', 'include', 'exclude', 'orderby', 'order' );
217 query = wp.media.query( args );
218 query.gallery = new Backbone.Model( others );
222 shortcode: function( attachments ) {
223 var props = attachments.props.toJSON(),
224 attrs = _.pick( props, 'orderby', 'order' ),
227 if ( attachments.gallery )
228 _.extend( attrs, attachments.gallery.toJSON() );
230 // Convert all gallery shortcodes to use the `ids` property.
231 // Ignore `post__in` and `post__not_in`; the attachments in
232 // the collection will already reflect those properties.
233 attrs.ids = attachments.pluck('id');
235 // Copy the `uploadedTo` post ID.
236 if ( props.uploadedTo )
237 attrs.id = props.uploadedTo;
239 // Check if the gallery is randomly ordered.
240 if ( attrs._orderbyRandom )
241 attrs.orderby = 'rand';
242 delete attrs._orderbyRandom;
244 // If the `ids` attribute is set and `orderby` attribute
245 // is the default value, clear it for cleaner output.
246 if ( attrs.ids && 'post__in' === attrs.orderby )
247 delete attrs.orderby;
249 // Remove default attributes from the shortcode.
250 _.each( wp.media.gallery.defaults, function( value, key ) {
251 if ( value === attrs[ key ] )
255 shortcode = new wp.shortcode({
261 // Use a cloned version of the gallery.
262 clone = new wp.media.model.Attachments( attachments.models, {
265 clone.gallery = attachments.gallery;
266 galleries[ shortcode.string() ] = clone;
271 edit: function( content ) {
272 var shortcode = wp.shortcode.next( 'gallery', content ),
273 defaultPostId = wp.media.gallery.defaults.id,
274 attachments, selection;
276 // Bail if we didn't match the shortcode or all of the content.
277 if ( ! shortcode || shortcode.content !== content )
280 // Ignore the rest of the match object.
281 shortcode = shortcode.shortcode;
283 if ( _.isUndefined( shortcode.get('id') ) && ! _.isUndefined( defaultPostId ) )
284 shortcode.set( 'id', defaultPostId );
286 attachments = wp.media.gallery.attachments( shortcode );
288 selection = new wp.media.model.Selection( attachments.models, {
289 props: attachments.props.toJSON(),
293 selection.gallery = attachments.gallery;
295 // Fetch the query's attachments, and then break ties from the
296 // query to allow for sorting.
297 selection.more().done( function() {
298 // Break ties with the query.
299 selection.props.set({ query: false });
300 selection.unmirror();
301 selection.props.unset('orderby');
304 // Destroy the previous gallery frame.
306 this.frame.dispose();
308 // Store the current gallery frame.
309 this.frame = wp.media({
311 state: 'gallery-edit',
312 title: wp.media.view.l10n.editGalleryTitle,
323 wp.media.featuredImage = {
325 return wp.media.view.settings.post.featuredImageId;
328 set: function( id ) {
329 var settings = wp.media.view.settings;
331 settings.post.featuredImageId = id;
333 wp.media.post( 'set-post-thumbnail', {
335 post_id: settings.post.id,
336 thumbnail_id: settings.post.featuredImageId,
337 _wpnonce: settings.post.nonce
338 }).done( function( html ) {
339 $( '.inside', '#postimagediv' ).html( html );
347 this._frame = wp.media({
348 state: 'featured-image',
349 states: [ new wp.media.controller.FeaturedImage() ]
352 this._frame.on( 'toolbar:create:featured-image', function( toolbar ) {
353 this.createSelectToolbar( toolbar, {
354 text: wp.media.view.l10n.setFeaturedImage
358 this._frame.state('featured-image').on( 'select', this.select );
363 var settings = wp.media.view.settings,
364 selection = this.get('selection').single();
366 if ( ! settings.post.featuredImageId )
369 wp.media.featuredImage.set( selection ? selection.id : -1 );
373 // Open the content media manager to the 'featured image' tab when
374 // the post thumbnail is clicked.
375 $('#postimagediv').on( 'click', '#set-post-thumbnail', function( event ) {
376 event.preventDefault();
377 // Stop propagation to prevent thickbox from activating.
378 event.stopPropagation();
380 wp.media.featuredImage.frame().open();
382 // Update the featured image id when the 'remove' link is clicked.
383 }).on( 'click', '#remove-post-thumbnail', function() {
384 wp.media.view.settings.post.featuredImageId = -1;
389 $( wp.media.featuredImage.init );
392 insert: function( h ) {
393 var mce = typeof(tinymce) != 'undefined',
394 qt = typeof(QTags) != 'undefined',
395 wpActiveEditor = window.wpActiveEditor,
398 // Delegate to the global `send_to_editor` if it exists.
399 // This attempts to play nice with any themes/plugins that have
400 // overridden the insert functionality.
401 if ( window.send_to_editor )
402 return window.send_to_editor.apply( this, arguments );
404 if ( ! wpActiveEditor ) {
405 if ( mce && tinymce.activeEditor ) {
406 ed = tinymce.activeEditor;
407 wpActiveEditor = window.wpActiveEditor = ed.id;
412 if ( tinymce.activeEditor && (tinymce.activeEditor.id == 'mce_fullscreen' || tinymce.activeEditor.id == 'wp_mce_fullscreen') )
413 ed = tinymce.activeEditor;
415 ed = tinymce.get(wpActiveEditor);
418 if ( ed && !ed.isHidden() ) {
419 // restore caret position on IE
420 if ( tinymce.isIE && ed.windowManager.insertimagebookmark )
421 ed.selection.moveToBookmark(ed.windowManager.insertimagebookmark);
423 if ( h.indexOf('[caption') !== -1 ) {
424 if ( ed.wpSetImgCaption )
425 h = ed.wpSetImgCaption(h);
426 } else if ( h.indexOf('[gallery') !== -1 ) {
427 if ( ed.plugins.wpgallery )
428 h = ed.plugins.wpgallery._do_gallery(h);
429 } else if ( h.indexOf('[embed') === 0 ) {
430 if ( ed.plugins.wordpress )
431 h = ed.plugins.wordpress._setEmbed(h);
434 ed.execCommand('mceInsertContent', false, h);
436 QTags.insertContent(h);
438 document.getElementById(wpActiveEditor).value += h;
441 // If the old thickbox remove function exists, call it in case
442 // a theme/plugin overloaded it.
443 if ( window.tb_remove )
444 try { window.tb_remove(); } catch( e ) {}
447 add: function( id, options ) {
448 var workflow = this.get( id );
453 workflow = workflows[ id ] = wp.media( _.defaults( options || {}, {
456 title: wp.media.view.l10n.addMedia,
460 workflow.on( 'insert', function( selection ) {
461 var state = workflow.state();
463 selection = selection || state.get('selection');
468 $.when.apply( $, selection.map( function( attachment ) {
469 var display = state.display( attachment ).toJSON();
470 return this.send.attachment( display, attachment.toJSON() );
471 }, this ) ).done( function() {
472 wp.media.editor.insert( _.toArray( arguments ).join("\n\n") );
476 workflow.state('gallery-edit').on( 'update', function( selection ) {
477 this.insert( wp.media.gallery.shortcode( selection ).string() );
480 workflow.state('embed').on( 'select', function() {
481 var state = workflow.state(),
482 type = state.get('type'),
483 embed = state.props.toJSON();
485 embed.url = embed.url || '';
487 if ( 'link' === type ) {
493 this.send.link( embed ).done( function( resp ) {
494 wp.media.editor.insert( resp );
497 } else if ( 'image' === type ) {
505 if ( 'none' === embed.link )
507 else if ( 'file' === embed.link )
508 embed.linkUrl = embed.url;
510 this.insert( wp.media.string.image( embed ) );
514 workflow.state('featured-image').on( 'select', wp.media.featuredImage.select );
515 workflow.setState( workflow.options.state );
523 // If an empty `id` is provided, default to `wpActiveEditor`.
526 // If that doesn't work, fall back to `tinymce.activeEditor.id`.
527 if ( ! id && typeof tinymce !== 'undefined' && tinymce.activeEditor )
528 id = tinymce.activeEditor.id;
530 // Last but not least, fall back to the empty string.
535 get: function( id ) {
537 return workflows[ id ];
540 remove: function( id ) {
542 delete workflows[ id ];
546 attachment: function( props, attachment ) {
547 var caption = attachment.caption,
550 // If captions are disabled, clear the caption.
551 if ( ! wp.media.view.settings.captions )
552 delete attachment.caption;
554 props = wp.media.string.props( props, attachment );
558 post_content: attachment.description,
559 post_excerpt: caption
563 options.url = props.linkUrl;
565 if ( 'image' === attachment.type ) {
566 html = wp.media.string.image( props );
572 }, function( option, prop ) {
574 options[ option ] = props[ prop ];
578 html = wp.media.string.link( props );
579 options.post_title = props.title;
582 return wp.media.post( 'send-attachment-to-editor', {
583 nonce: wp.media.view.settings.nonce.sendToEditor,
586 post_id: wp.media.view.settings.post.id
590 link: function( embed ) {
591 return wp.media.post( 'send-link-to-editor', {
592 nonce: wp.media.view.settings.nonce.sendToEditor,
595 html: wp.media.string.link( embed ),
596 post_id: wp.media.view.settings.post.id
601 open: function( id ) {
602 var workflow, editor;
606 // Save a bookmark of the caret position in IE.
607 if ( typeof tinymce !== 'undefined' ) {
608 editor = tinymce.get( id );
610 if ( tinymce.isIE && editor && ! editor.isHidden() ) {
612 editor.windowManager.insertimagebookmark = editor.selection.getBookmark();
616 workflow = this.get( id );
618 // Initialize the editor's workflow if we haven't yet.
620 workflow = this.add( id );
622 return workflow.open();
626 $(document.body).on( 'click', '.insert-media', function( event ) {
628 editor = $this.data('editor');
630 event.preventDefault();
632 // Remove focus from the `.insert-media` button.
633 // Prevents Opera from showing the outline of the button
636 // See: http://core.trac.wordpress.org/ticket/22445
639 wp.media.editor.open( editor );
644 _.bindAll( wp.media.editor, 'open' );
645 $( wp.media.editor.init );