-/* global getUserSetting, tinymce, QTags, wpActiveEditor */
+/* global getUserSetting, tinymce, QTags */
// WordPress, TinyMCE, and Media
// -----------------------------
fallbacks = function( props ) {
// Generate alt fallbacks and strip tags.
if ( 'image' === props.type && ! props.alt ) {
- props.alt = props.caption || props.title || '';
+ if ( props.caption ) {
+ props.alt = props.caption;
+ } else if ( props.title !== props.url ) {
+ props.alt = props.title;
+ } else {
+ props.alt = '';
+ }
+
props.alt = props.alt.replace( /<\/?[^>]+>/g, '' );
props.alt = props.alt.replace( /[\r\n]+/g, ' ' );
}
var img = {},
options, classes, shortcode, html;
+ props.type = 'image';
props = wp.media.string.props( props, attachment );
classes = props.classes || [];
}
};
+ wp.media.embed = {
+ coerce : wp.media.coerce,
+
+ defaults : {
+ url : '',
+ width: '',
+ height: ''
+ },
+
+ edit : function( data, isURL ) {
+ var frame, props = {}, shortcode;
+
+ if ( isURL ) {
+ props.url = data.replace(/<[^>]+>/g, '');
+ } else {
+ shortcode = wp.shortcode.next( 'embed', data ).shortcode;
+
+ props = _.defaults( shortcode.attrs.named, this.defaults );
+ if ( shortcode.content ) {
+ props.url = shortcode.content;
+ }
+ }
+
+ frame = wp.media({
+ frame: 'post',
+ state: 'embed',
+ metadata: props
+ });
+
+ return frame;
+ },
+
+ shortcode : function( model ) {
+ var self = this, content;
+
+ _.each( this.defaults, function( value, key ) {
+ model[ key ] = self.coerce( model, key );
+
+ if ( value === model[ key ] ) {
+ delete model[ key ];
+ }
+ });
+
+ content = model.url;
+ delete model.url;
+
+ return new wp.shortcode({
+ tag: 'embed',
+ attrs: model,
+ content: content
+ });
+ }
+ };
+
wp.media.collection = function(attributes) {
var collections = {};
- return _.extend( attributes, {
+ return _.extend( {
coerce : wp.media.coerce,
/**
* Retrieve attachments based on the properties of the passed shortcode
shortcode: function( attachments ) {
var props = attachments.props.toJSON(),
attrs = _.pick( props, 'orderby', 'order' ),
- shortcode, clone, self = this;
+ shortcode, clone;
if ( attachments.type ) {
attrs.type = attachments.type;
delete attrs.orderby;
}
- // Remove default attributes from the shortcode.
- _.each( this.defaults, function( value, key ) {
- attrs[ key ] = self.coerce( attrs, key );
- if ( value === attrs[ key ] ) {
- delete attrs[ key ];
- }
- });
+ attrs = this.setDefaults( attrs );
shortcode = new wp.shortcode({
tag: this.tag,
}).open();
return this.frame;
+ },
+
+ setDefaults: function( attrs ) {
+ var self = this;
+ // Remove default attributes from the shortcode.
+ _.each( this.defaults, function( value, key ) {
+ attrs[ key ] = self.coerce( attrs, key );
+ if ( value === attrs[ key ] ) {
+ delete attrs[ key ];
+ }
+ });
+
+ return attrs;
}
- });
+ }, attributes );
};
+ wp.media._galleryDefaults = {
+ itemtag: 'dl',
+ icontag: 'dt',
+ captiontag: 'dd',
+ columns: '3',
+ link: 'post',
+ size: 'thumbnail',
+ order: 'ASC',
+ id: wp.media.view.settings.post && wp.media.view.settings.post.id,
+ orderby : 'menu_order ID'
+ };
+
+ if ( wp.media.view.settings.galleryDefaults ) {
+ wp.media.galleryDefaults = _.extend( {}, wp.media._galleryDefaults, wp.media.view.settings.galleryDefaults );
+ } else {
+ wp.media.galleryDefaults = wp.media._galleryDefaults;
+ }
+
wp.media.gallery = new wp.media.collection({
tag: 'gallery',
type : 'image',
editTitle : wp.media.view.l10n.editGalleryTitle,
- defaults : {
- itemtag: 'dl',
- icontag: 'dt',
- captiontag: 'dd',
- columns: '3',
- link: 'post',
- size: 'thumbnail',
- order: 'ASC',
- id: wp.media.view.settings.post && wp.media.view.settings.post.id,
- orderby : 'menu_order ID'
+ defaults : wp.media.galleryDefaults,
+
+ setDefaults: function( attrs ) {
+ var self = this, changed = ! _.isEqual( wp.media.galleryDefaults, wp.media._galleryDefaults );
+ _.each( this.defaults, function( value, key ) {
+ attrs[ key ] = self.coerce( attrs, key );
+ if ( value === attrs[ key ] && ( ! changed || value === wp.media._galleryDefaults[ key ] ) ) {
+ delete attrs[ key ];
+ }
+ } );
+ return attrs;
}
});
settings.post.featuredImageId = id;
- wp.media.post( 'set-post-thumbnail', {
- json: true,
+ wp.media.post( 'get-post-thumbnail-html', {
post_id: settings.post.id,
thumbnail_id: settings.post.featuredImageId,
_wpnonce: settings.post.nonce
}).done( function( html ) {
+ if ( html == '0' ) {
+ window.alert( window.setPostThumbnailL10n.error );
+ return;
+ }
$( '.inside', '#postimagediv' ).html( html );
});
},
+ /**
+ * Remove the featured image id, save the post thumbnail data and
+ * set the HTML in the post meta box to no featured image.
+ */
+ remove: function() {
+ wp.media.featuredImage.set( -1 );
+ },
/**
* The Featured Image workflow
*
*/
frame: function() {
if ( this._frame ) {
+ wp.media.frame = this._frame;
return this._frame;
}
wp.media.featuredImage.frame().open();
}).on( 'click', '#remove-post-thumbnail', function() {
- wp.media.view.settings.post.featuredImageId = -1;
+ wp.media.featuredImage.remove();
+ return false;
});
}
};
* @param {string} html Content to send to the editor
*/
insert: function( html ) {
- var editor,
+ var editor, wpActiveEditor,
hasTinymce = ! _.isUndefined( window.tinymce ),
- hasQuicktags = ! _.isUndefined( window.QTags ),
+ hasQuicktags = ! _.isUndefined( window.QTags );
+
+ if ( this.activeEditor ) {
+ wpActiveEditor = window.wpActiveEditor = this.activeEditor;
+ } else {
wpActiveEditor = window.wpActiveEditor;
+ }
// Delegate to the global `send_to_editor` if it exists.
// This attempts to play nice with any themes/plugins that have
if ( 'link' === type ) {
_.defaults( embed, {
- title: embed.url,
+ linkText: embed.url,
linkUrl: embed.url
});
}
// If an empty `id` is provided, default to `wpActiveEditor`.
- id = wpActiveEditor;
+ id = window.wpActiveEditor;
// If that doesn't work, fall back to `tinymce.activeEditor.id`.
if ( ! id && ! _.isUndefined( window.tinymce ) && tinymce.activeEditor ) {
*/
link: function( embed ) {
return wp.media.post( 'send-link-to-editor', {
- nonce: wp.media.view.settings.nonce.sendToEditor,
- src: embed.linkUrl,
- title: embed.title,
- html: wp.media.string.link( embed ),
- post_id: wp.media.view.settings.post.id
+ nonce: wp.media.view.settings.nonce.sendToEditor,
+ src: embed.linkUrl,
+ link_text: embed.linkText,
+ html: wp.media.string.link( embed ),
+ post_id: wp.media.view.settings.post.id
});
}
},
options = options || {};
id = this.id( id );
-/*
- // Save a bookmark of the caret position in IE.
- if ( ! _.isUndefined( window.tinymce ) ) {
- editor = tinymce.get( id );
-
- if ( tinymce.isIE && editor && ! editor.isHidden() ) {
- editor.focus();
- editor.windowManager.insertimagebookmark = editor.selection.getBookmark();
- }
- }
-*/
+ this.activeEditor = id;
+
workflow = this.get( id );
// Redo workflow if state has changed
workflow = this.add( id, options );
}
+ wp.media.frame = workflow;
+
return workflow.open();
},
*/
init: function() {
$(document.body)
- .on( 'click', '.insert-media', function( event ) {
+ .on( 'click.add-media-button', '.insert-media', function( event ) {
var elem = $( event.currentTarget ),
editor = elem.data('editor'),
options = {
// Prevents Opera from showing the outline of the button
// above the modal.
//
- // See: http://core.trac.wordpress.org/ticket/22445
+ // See: https://core.trac.wordpress.org/ticket/22445
elem.blur();
if ( elem.hasClass( 'gallery' ) ) {