2014-08-25 15:37:16 -04:00
|
|
|
/* global getUserSetting, tinymce, QTags */
|
2013-11-15 00:16:09 -05:00
|
|
|
|
2012-11-21 11:46:32 -05:00
|
|
|
// WordPress, TinyMCE, and Media
|
|
|
|
// -----------------------------
|
2014-02-27 14:22:14 -05:00
|
|
|
(function($, _){
|
2014-01-20 18:35:15 -05:00
|
|
|
/**
|
|
|
|
* Stores the editors' `wp.media.controller.Frame` instances.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
*/
|
2014-02-27 14:22:14 -05:00
|
|
|
var workflows = {};
|
2012-11-21 11:46:32 -05:00
|
|
|
|
2014-03-19 01:31:15 -04:00
|
|
|
/**
|
|
|
|
* A helper mixin function to avoid truthy and falsey values being
|
|
|
|
* passed as an input that expects booleans. If key is undefined in the map,
|
|
|
|
* but has a default value, set it.
|
|
|
|
*
|
|
|
|
* @param {object} attrs Map of props from a shortcode or settings.
|
|
|
|
* @param {string} key The key within the passed map to check for a value.
|
|
|
|
* @returns {mixed|undefined} The original or coerced value of key within attrs
|
|
|
|
*/
|
|
|
|
wp.media.coerce = function ( attrs, key ) {
|
|
|
|
if ( _.isUndefined( attrs[ key ] ) && ! _.isUndefined( this.defaults[ key ] ) ) {
|
|
|
|
attrs[ key ] = this.defaults[ key ];
|
|
|
|
} else if ( 'true' === attrs[ key ] ) {
|
|
|
|
attrs[ key ] = true;
|
|
|
|
} else if ( 'false' === attrs[ key ] ) {
|
|
|
|
attrs[ key ] = false;
|
|
|
|
}
|
|
|
|
return attrs[ key ];
|
|
|
|
};
|
|
|
|
|
2014-01-20 18:35:15 -05:00
|
|
|
/**
|
|
|
|
* wp.media.string
|
2014-01-22 12:17:11 -05:00
|
|
|
* @namespace
|
2014-01-20 18:35:15 -05:00
|
|
|
*/
|
2012-11-21 11:46:32 -05:00
|
|
|
wp.media.string = {
|
2014-01-20 18:35:15 -05:00
|
|
|
/**
|
|
|
|
* Joins the `props` and `attachment` objects,
|
|
|
|
* outputting the proper object format based on the
|
|
|
|
* attachment's type.
|
|
|
|
*
|
2014-01-22 12:17:11 -05:00
|
|
|
* @global wp.media.view.settings
|
2014-01-22 13:38:11 -05:00
|
|
|
* @global getUserSetting()
|
2014-01-20 18:35:15 -05:00
|
|
|
*
|
2014-01-22 12:17:11 -05:00
|
|
|
* @param {Object} [props={}] Attachment details (align, link, size, etc).
|
2014-01-21 10:55:13 -05:00
|
|
|
* @param {Object} attachment The attachment object, media version of Post.
|
2014-01-20 18:35:15 -05:00
|
|
|
* @returns {Object} Joined props
|
|
|
|
*/
|
2012-11-21 11:46:32 -05:00
|
|
|
props: function( props, attachment ) {
|
2013-01-04 03:02:16 -05:00
|
|
|
var link, linkUrl, size, sizes, fallbacks,
|
|
|
|
defaultProps = wp.media.view.settings.defaultProps;
|
2012-11-29 20:38:41 -05: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 12:34:45 -05:00
|
|
|
props.alt = props.alt.replace( /[\r\n]+/g, ' ' );
|
2012-11-29 20:38:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return props;
|
|
|
|
};
|
2012-11-21 11:46:32 -05:00
|
|
|
|
|
|
|
props = props ? _.clone( props ) : {};
|
|
|
|
|
2014-01-20 18:35:15 -05:00
|
|
|
if ( attachment && attachment.type ) {
|
2012-11-21 11:46:32 -05:00
|
|
|
props.type = attachment.type;
|
2014-01-20 18:35:15 -05:00
|
|
|
}
|
2012-11-21 11:46:32 -05:00
|
|
|
|
|
|
|
if ( 'image' === props.type ) {
|
|
|
|
props = _.defaults( props || {}, {
|
2013-01-04 03:02:16 -05:00
|
|
|
align: defaultProps.align || getUserSetting( 'align', 'none' ),
|
|
|
|
size: defaultProps.size || getUserSetting( 'imgsize', 'medium' ),
|
2012-11-21 11:46:32 -05:00
|
|
|
url: '',
|
|
|
|
classes: []
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// All attachment-specific settings follow.
|
2014-01-20 18:35:15 -05:00
|
|
|
if ( ! attachment ) {
|
2012-11-29 20:38:41 -05:00
|
|
|
return fallbacks( props );
|
2014-01-20 18:35:15 -05:00
|
|
|
}
|
2012-11-29 20:38:41 -05:00
|
|
|
|
|
|
|
props.title = props.title || attachment.title;
|
2012-11-21 11:46:32 -05:00
|
|
|
|
2013-01-04 03:02:16 -05:00
|
|
|
link = props.link || defaultProps.link || getUserSetting( 'urlbutton', 'file' );
|
2014-01-20 18:35:15 -05:00
|
|
|
if ( 'file' === link || 'embed' === link ) {
|
2012-11-21 11:46:32 -05:00
|
|
|
linkUrl = attachment.url;
|
2014-01-20 18:35:15 -05:00
|
|
|
} else if ( 'post' === link ) {
|
2012-11-21 11:46:32 -05:00
|
|
|
linkUrl = attachment.link;
|
2014-01-20 18:35:15 -05:00
|
|
|
} else if ( 'custom' === link ) {
|
2012-11-21 11:46:32 -05:00
|
|
|
linkUrl = props.linkUrl;
|
2014-01-20 18:35:15 -05:00
|
|
|
}
|
2012-11-21 11:46:32 -05:00
|
|
|
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 16:23:55 -05:00
|
|
|
_.extend( props, _.pick( attachment, 'align', 'caption', 'alt' ), {
|
2012-11-21 11:46:32 -05:00
|
|
|
width: size.width,
|
|
|
|
height: size.height,
|
|
|
|
src: size.url,
|
|
|
|
captionId: 'attachment_' + attachment.id
|
|
|
|
});
|
2013-03-16 01:25:44 -04:00
|
|
|
} else if ( 'video' === attachment.type || 'audio' === attachment.type ) {
|
|
|
|
_.extend( props, _.pick( attachment, 'title', 'type', 'icon', 'mime' ) );
|
2012-11-21 11:46:32 -05:00
|
|
|
// Format properties for non-images.
|
|
|
|
} else {
|
2012-11-29 20:38:41 -05:00
|
|
|
props.title = props.title || attachment.filename;
|
|
|
|
props.rel = props.rel || 'attachment wp-att-' + attachment.id;
|
2012-11-21 11:46:32 -05:00
|
|
|
}
|
|
|
|
|
2012-11-29 20:38:41 -05:00
|
|
|
return fallbacks( props );
|
2012-11-21 11:46:32 -05:00
|
|
|
},
|
2014-01-20 18:35:15 -05:00
|
|
|
/**
|
2014-01-22 13:38:11 -05:00
|
|
|
* Create link markup that is suitable for passing to the editor
|
2014-01-20 18:35:15 -05:00
|
|
|
*
|
|
|
|
* @global wp.html.string
|
|
|
|
*
|
2014-01-21 10:55:13 -05:00
|
|
|
* @param {Object} props Attachment details (align, link, size, etc).
|
|
|
|
* @param {Object} attachment The attachment object, media version of Post.
|
2014-01-20 18:35:15 -05:00
|
|
|
* @returns {string} The link markup
|
|
|
|
*/
|
2012-11-21 11:46:32 -05:00
|
|
|
link: function( props, attachment ) {
|
|
|
|
var options;
|
|
|
|
|
|
|
|
props = wp.media.string.props( props, attachment );
|
|
|
|
|
|
|
|
options = {
|
|
|
|
tag: 'a',
|
|
|
|
content: props.title,
|
|
|
|
attrs: {
|
|
|
|
href: props.linkUrl
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-01-20 18:35:15 -05:00
|
|
|
if ( props.rel ) {
|
2012-11-21 11:46:32 -05:00
|
|
|
options.attrs.rel = props.rel;
|
2014-01-20 18:35:15 -05:00
|
|
|
}
|
2012-11-21 11:46:32 -05:00
|
|
|
|
|
|
|
return wp.html.string( options );
|
|
|
|
},
|
2014-01-20 18:35:15 -05:00
|
|
|
/**
|
2014-01-22 13:38:11 -05:00
|
|
|
* Create an Audio shortcode string that is suitable for passing to the editor
|
2014-01-20 18:35:15 -05:00
|
|
|
*
|
2014-01-21 10:55:13 -05:00
|
|
|
* @param {Object} props Attachment details (align, link, size, etc).
|
|
|
|
* @param {Object} attachment The attachment object, media version of Post.
|
2014-01-20 18:35:15 -05:00
|
|
|
* @returns {string} The audio shortcode
|
|
|
|
*/
|
2013-03-16 01:25:44 -04:00
|
|
|
audio: function( props, attachment ) {
|
2013-07-23 01:39:08 -04:00
|
|
|
return wp.media.string._audioVideo( 'audio', props, attachment );
|
2013-03-16 01:25:44 -04:00
|
|
|
},
|
2014-01-20 18:35:15 -05:00
|
|
|
/**
|
2014-01-22 13:38:11 -05:00
|
|
|
* Create a Video shortcode string that is suitable for passing to the editor
|
2014-01-20 18:35:15 -05:00
|
|
|
*
|
2014-01-21 10:55:13 -05:00
|
|
|
* @param {Object} props Attachment details (align, link, size, etc).
|
|
|
|
* @param {Object} attachment The attachment object, media version of Post.
|
2014-01-20 18:35:15 -05:00
|
|
|
* @returns {string} The video shortcode
|
|
|
|
*/
|
2013-03-16 01:25:44 -04:00
|
|
|
video: function( props, attachment ) {
|
2013-07-23 01:39:08 -04:00
|
|
|
return wp.media.string._audioVideo( 'video', props, attachment );
|
|
|
|
},
|
2014-01-20 18:35:15 -05:00
|
|
|
/**
|
2014-01-22 13:38:11 -05:00
|
|
|
* Helper function to create a media shortcode string
|
2014-01-20 18:35:15 -05:00
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @global wp.shortcode
|
|
|
|
* @global wp.media.view.settings
|
|
|
|
*
|
|
|
|
* @param {string} type The shortcode tag name: 'audio' or 'video'.
|
2014-01-21 10:55:13 -05:00
|
|
|
* @param {Object} props Attachment details (align, link, size, etc).
|
|
|
|
* @param {Object} attachment The attachment object, media version of Post.
|
2014-01-20 18:35:15 -05:00
|
|
|
* @returns {string} The media shortcode
|
|
|
|
*/
|
2013-07-23 01:39:08 -04:00
|
|
|
_audioVideo: function( type, props, attachment ) {
|
|
|
|
var shortcode, html, extension;
|
2013-03-16 01:25:44 -04:00
|
|
|
|
|
|
|
props = wp.media.string.props( props, attachment );
|
2013-07-23 01:39:08 -04:00
|
|
|
if ( props.link !== 'embed' )
|
|
|
|
return wp.media.string.link( props );
|
2013-03-16 01:25:44 -04:00
|
|
|
|
|
|
|
shortcode = {};
|
|
|
|
|
2013-07-23 01:39:08 -04:00
|
|
|
if ( 'video' === type ) {
|
2014-04-07 02:20:16 -04:00
|
|
|
if ( attachment.image && -1 === attachment.image.src.indexOf( attachment.icon ) ) {
|
2014-03-22 23:36:15 -04:00
|
|
|
shortcode.poster = attachment.image.src;
|
|
|
|
}
|
|
|
|
|
2014-01-20 18:35:15 -05:00
|
|
|
if ( attachment.width ) {
|
2013-07-23 01:39:08 -04:00
|
|
|
shortcode.width = attachment.width;
|
2014-01-20 18:35:15 -05:00
|
|
|
}
|
2013-07-23 01:39:08 -04:00
|
|
|
|
2014-01-20 18:35:15 -05:00
|
|
|
if ( attachment.height ) {
|
2013-07-23 01:39:08 -04:00
|
|
|
shortcode.height = attachment.height;
|
2014-01-20 18:35:15 -05:00
|
|
|
}
|
2013-07-23 01:39:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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 01:25:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
html = wp.shortcode.string({
|
2013-07-23 01:39:08 -04:00
|
|
|
tag: type,
|
2013-03-16 01:25:44 -04:00
|
|
|
attrs: shortcode
|
|
|
|
});
|
|
|
|
|
|
|
|
return html;
|
|
|
|
},
|
2014-01-20 18:35:15 -05:00
|
|
|
/**
|
2014-01-22 13:38:11 -05:00
|
|
|
* Create image markup, optionally with a link and/or wrapped in a caption shortcode,
|
|
|
|
* that is suitable for passing to the editor
|
2014-01-20 18:35:15 -05:00
|
|
|
*
|
|
|
|
* @global wp.html
|
|
|
|
* @global wp.shortcode
|
|
|
|
*
|
2014-01-21 10:55:13 -05:00
|
|
|
* @param {Object} props Attachment details (align, link, size, etc).
|
|
|
|
* @param {Object} attachment The attachment object, media version of Post.
|
2014-01-20 18:35:15 -05:00
|
|
|
* @returns {string}
|
|
|
|
*/
|
2012-11-21 11:46:32 -05:00
|
|
|
image: function( props, attachment ) {
|
|
|
|
var img = {},
|
|
|
|
options, classes, shortcode, html;
|
|
|
|
|
|
|
|
props = wp.media.string.props( props, attachment );
|
|
|
|
classes = props.classes || [];
|
|
|
|
|
2014-02-27 15:11:14 -05:00
|
|
|
img.src = ! _.isUndefined( attachment ) ? attachment.url : props.url;
|
2012-11-21 11:46:32 -05: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.
|
2014-01-20 18:35:15 -05:00
|
|
|
if ( props.align && ! props.caption ) {
|
2012-11-21 11:46:32 -05:00
|
|
|
classes.push( 'align' + props.align );
|
2014-01-20 18:35:15 -05:00
|
|
|
}
|
2012-11-21 11:46:32 -05:00
|
|
|
|
2014-01-20 18:35:15 -05:00
|
|
|
if ( props.size ) {
|
2012-11-21 11:46:32 -05:00
|
|
|
classes.push( 'size-' + props.size );
|
2014-01-20 18:35:15 -05:00
|
|
|
}
|
2012-11-21 11:46:32 -05:00
|
|
|
|
|
|
|
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 = {};
|
|
|
|
|
2014-01-20 18:35:15 -05:00
|
|
|
if ( img.width ) {
|
2012-11-21 11:46:32 -05:00
|
|
|
shortcode.width = img.width;
|
2014-01-20 18:35:15 -05:00
|
|
|
}
|
2012-11-21 11:46:32 -05:00
|
|
|
|
2014-01-20 18:35:15 -05:00
|
|
|
if ( props.captionId ) {
|
2012-11-21 11:46:32 -05:00
|
|
|
shortcode.id = props.captionId;
|
2014-01-20 18:35:15 -05:00
|
|
|
}
|
2012-11-21 11:46:32 -05:00
|
|
|
|
2014-01-20 18:35:15 -05:00
|
|
|
if ( props.align ) {
|
2012-11-21 11:46:32 -05:00
|
|
|
shortcode.align = 'align' + props.align;
|
2014-01-20 18:35:15 -05:00
|
|
|
}
|
2012-11-21 11:46:32 -05:00
|
|
|
|
|
|
|
html = wp.shortcode.string({
|
|
|
|
tag: 'caption',
|
|
|
|
attrs: shortcode,
|
|
|
|
content: html + ' ' + props.caption
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return html;
|
|
|
|
}
|
|
|
|
};
|
2014-03-06 11:06:14 -05:00
|
|
|
|
2014-06-13 17:42:15 -04:00
|
|
|
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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-27 14:22:14 -05:00
|
|
|
wp.media.collection = function(attributes) {
|
|
|
|
var collections = {};
|
|
|
|
|
2014-07-24 16:00:16 -04:00
|
|
|
return _.extend( {
|
2014-03-19 01:31:15 -04:00
|
|
|
coerce : wp.media.coerce,
|
2014-02-27 16:51:14 -05:00
|
|
|
/**
|
|
|
|
* Retrieve attachments based on the properties of the passed shortcode
|
|
|
|
*
|
|
|
|
* @global wp.media.query
|
|
|
|
*
|
|
|
|
* @param {wp.shortcode} shortcode An instance of wp.shortcode().
|
|
|
|
* @returns {wp.media.model.Attachments} A Backbone.Collection containing
|
|
|
|
* the media items belonging to a collection.
|
|
|
|
* The query[ this.tag ] property is a Backbone.Model
|
|
|
|
* containing the 'props' for the collection.
|
|
|
|
*/
|
2014-02-27 14:22:14 -05:00
|
|
|
attachments: function( shortcode ) {
|
|
|
|
var shortcodeString = shortcode.string(),
|
|
|
|
result = collections[ shortcodeString ],
|
|
|
|
attrs, args, query, others, self = this;
|
2014-02-20 13:07:12 -05:00
|
|
|
|
2014-02-27 14:22:14 -05:00
|
|
|
delete collections[ shortcodeString ];
|
2014-02-25 12:32:14 -05:00
|
|
|
if ( result ) {
|
|
|
|
return result;
|
2014-02-20 13:07:12 -05:00
|
|
|
}
|
2014-02-25 12:32:14 -05:00
|
|
|
// Fill the default shortcode attributes.
|
|
|
|
attrs = _.defaults( shortcode.attrs.named, this.defaults );
|
|
|
|
args = _.pick( attrs, 'orderby', 'order' );
|
2014-02-20 13:07:12 -05:00
|
|
|
|
2014-02-27 14:22:14 -05:00
|
|
|
args.type = this.type;
|
2014-02-25 12:32:14 -05:00
|
|
|
args.perPage = -1;
|
2014-02-20 13:07:12 -05:00
|
|
|
|
2014-02-25 12:32:14 -05:00
|
|
|
// Mark the `orderby` override attribute.
|
|
|
|
if ( undefined !== attrs.orderby ) {
|
|
|
|
attrs._orderByField = attrs.orderby;
|
2014-02-20 13:07:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( 'rand' === attrs.orderby ) {
|
|
|
|
attrs._orderbyRandom = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map the `orderby` attribute to the corresponding model property.
|
|
|
|
if ( ! attrs.orderby || /^menu_order(?: ID)?$/i.test( attrs.orderby ) ) {
|
|
|
|
args.orderby = 'menuOrder';
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 ) {
|
|
|
|
args.uploadedTo = attrs.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect the attributes that were not included in `args`.
|
|
|
|
others = _.omit( attrs, 'id', 'ids', 'include', 'exclude', 'orderby', 'order' );
|
|
|
|
|
2014-02-27 14:22:14 -05:00
|
|
|
_.each( this.defaults, function( value, key ) {
|
|
|
|
others[ key ] = self.coerce( others, key );
|
|
|
|
});
|
|
|
|
|
2014-02-20 13:07:12 -05:00
|
|
|
query = wp.media.query( args );
|
2014-02-27 14:22:14 -05:00
|
|
|
query[ this.tag ] = new Backbone.Model( others );
|
2014-02-20 13:07:12 -05:00
|
|
|
return query;
|
2014-02-27 14:22:14 -05:00
|
|
|
},
|
2014-02-27 16:51:14 -05:00
|
|
|
/**
|
|
|
|
* Triggered when clicking 'Insert {label}' or 'Update {label}'
|
|
|
|
*
|
|
|
|
* @global wp.shortcode
|
|
|
|
* @global wp.media.model.Attachments
|
|
|
|
*
|
|
|
|
* @param {wp.media.model.Attachments} attachments A Backbone.Collection containing
|
|
|
|
* the media items belonging to a collection.
|
|
|
|
* The query[ this.tag ] property is a Backbone.Model
|
|
|
|
* containing the 'props' for the collection.
|
|
|
|
* @returns {wp.shortcode}
|
|
|
|
*/
|
2014-02-27 14:22:14 -05:00
|
|
|
shortcode: function( attachments ) {
|
|
|
|
var props = attachments.props.toJSON(),
|
|
|
|
attrs = _.pick( props, 'orderby', 'order' ),
|
2014-07-27 07:49:15 -04:00
|
|
|
shortcode, clone;
|
2014-02-20 13:07:12 -05:00
|
|
|
|
2014-03-27 13:58:15 -04:00
|
|
|
if ( attachments.type ) {
|
|
|
|
attrs.type = attachments.type;
|
|
|
|
delete attachments.type;
|
|
|
|
}
|
|
|
|
|
2014-02-27 14:22:14 -05:00
|
|
|
if ( attachments[this.tag] ) {
|
|
|
|
_.extend( attrs, attachments[this.tag].toJSON() );
|
|
|
|
}
|
2014-03-20 09:33:14 -04:00
|
|
|
|
2014-02-27 14:22:14 -05: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.
|
|
|
|
attrs.ids = attachments.pluck('id');
|
|
|
|
|
|
|
|
// Copy the `uploadedTo` post ID.
|
|
|
|
if ( props.uploadedTo ) {
|
|
|
|
attrs.id = props.uploadedTo;
|
|
|
|
}
|
|
|
|
// Check if the gallery is randomly ordered.
|
2014-02-25 12:32:14 -05:00
|
|
|
delete attrs.orderby;
|
2014-02-20 13:07:12 -05:00
|
|
|
|
2014-02-27 14:22:14 -05:00
|
|
|
if ( attrs._orderbyRandom ) {
|
|
|
|
attrs.orderby = 'rand';
|
|
|
|
} else if ( attrs._orderByField && attrs._orderByField != 'rand' ) {
|
|
|
|
attrs.orderby = attrs._orderByField;
|
2014-02-20 13:07:12 -05:00
|
|
|
}
|
|
|
|
|
2014-02-27 14:22:14 -05:00
|
|
|
delete attrs._orderbyRandom;
|
|
|
|
delete attrs._orderByField;
|
2014-02-20 13:07:12 -05:00
|
|
|
|
2014-02-27 14:22:14 -05: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;
|
|
|
|
}
|
2014-02-20 13:07:12 -05:00
|
|
|
|
2014-07-24 16:00:16 -04:00
|
|
|
attrs = this.setDefaults( attrs );
|
2014-02-20 13:07:12 -05:00
|
|
|
|
2014-02-27 14:22:14 -05:00
|
|
|
shortcode = new wp.shortcode({
|
|
|
|
tag: this.tag,
|
|
|
|
attrs: attrs,
|
|
|
|
type: 'single'
|
|
|
|
});
|
2014-02-20 13:07:12 -05:00
|
|
|
|
2014-02-27 14:22:14 -05:00
|
|
|
// Use a cloned version of the gallery.
|
|
|
|
clone = new wp.media.model.Attachments( attachments.models, {
|
|
|
|
props: props
|
|
|
|
});
|
|
|
|
clone[ this.tag ] = attachments[ this.tag ];
|
|
|
|
collections[ shortcode.string() ] = clone;
|
2014-02-20 13:07:12 -05:00
|
|
|
|
2014-02-27 14:22:14 -05:00
|
|
|
return shortcode;
|
|
|
|
},
|
2014-02-27 16:51:14 -05:00
|
|
|
/**
|
|
|
|
* Triggered when double-clicking a collection shortcode placeholder
|
|
|
|
* in the editor
|
|
|
|
*
|
|
|
|
* @global wp.shortcode
|
|
|
|
* @global wp.media.model.Selection
|
|
|
|
* @global wp.media.view.l10n
|
|
|
|
*
|
|
|
|
* @param {string} content Content that is searched for possible
|
|
|
|
* shortcode markup matching the passed tag name,
|
|
|
|
*
|
|
|
|
* @this wp.media.{prop}
|
|
|
|
*
|
|
|
|
* @returns {wp.media.view.MediaFrame.Select} A media workflow.
|
|
|
|
*/
|
2014-02-27 14:22:14 -05:00
|
|
|
edit: function( content ) {
|
|
|
|
var shortcode = wp.shortcode.next( this.tag, content ),
|
|
|
|
defaultPostId = this.defaults.id,
|
2014-03-27 13:58:15 -04:00
|
|
|
attachments, selection, state;
|
2014-02-25 12:32:14 -05:00
|
|
|
|
2014-02-27 14:22:14 -05:00
|
|
|
// Bail if we didn't match the shortcode or all of the content.
|
|
|
|
if ( ! shortcode || shortcode.content !== content ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-25 12:32:14 -05:00
|
|
|
|
2014-02-27 14:22:14 -05:00
|
|
|
// Ignore the rest of the match object.
|
|
|
|
shortcode = shortcode.shortcode;
|
2014-02-20 13:07:12 -05:00
|
|
|
|
2014-02-27 14:22:14 -05:00
|
|
|
if ( _.isUndefined( shortcode.get('id') ) && ! _.isUndefined( defaultPostId ) ) {
|
|
|
|
shortcode.set( 'id', defaultPostId );
|
|
|
|
}
|
2014-02-20 13:07:12 -05:00
|
|
|
|
2014-02-27 14:22:14 -05:00
|
|
|
attachments = this.attachments( shortcode );
|
2014-02-20 13:07:12 -05:00
|
|
|
|
2014-02-27 14:22:14 -05:00
|
|
|
selection = new wp.media.model.Selection( attachments.models, {
|
|
|
|
props: attachments.props.toJSON(),
|
|
|
|
multiple: true
|
|
|
|
});
|
2014-02-25 12:32:14 -05:00
|
|
|
|
2014-02-27 14:22:14 -05:00
|
|
|
selection[ this.tag ] = attachments[ this.tag ];
|
2014-02-20 13:07:12 -05:00
|
|
|
|
2014-02-27 14:22:14 -05:00
|
|
|
// 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');
|
|
|
|
});
|
2014-02-20 13:07:12 -05:00
|
|
|
|
2014-02-27 14:22:14 -05:00
|
|
|
// Destroy the previous gallery frame.
|
|
|
|
if ( this.frame ) {
|
|
|
|
this.frame.dispose();
|
2014-02-25 12:32:14 -05:00
|
|
|
}
|
2014-02-20 13:07:12 -05:00
|
|
|
|
2014-03-27 13:58:15 -04:00
|
|
|
if ( shortcode.attrs.named.type && 'video' === shortcode.attrs.named.type ) {
|
|
|
|
state = 'video-' + this.tag + '-edit';
|
|
|
|
} else {
|
|
|
|
state = this.tag + '-edit';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the current frame.
|
2014-02-27 14:22:14 -05:00
|
|
|
this.frame = wp.media({
|
|
|
|
frame: 'post',
|
2014-03-27 13:58:15 -04:00
|
|
|
state: state,
|
2014-02-27 14:22:14 -05:00
|
|
|
title: this.editTitle,
|
|
|
|
editing: true,
|
|
|
|
multiple: true,
|
|
|
|
selection: selection
|
|
|
|
}).open();
|
|
|
|
|
|
|
|
return this.frame;
|
2014-07-24 16:00:16 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
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;
|
2014-02-24 13:08:16 -05:00
|
|
|
}
|
2014-07-24 16:00:16 -04:00
|
|
|
}, attributes );
|
2014-02-27 14:22:14 -05:00
|
|
|
};
|
2014-02-24 13:08:16 -05:00
|
|
|
|
2014-07-24 16:00:16 -04:00
|
|
|
wp.media._galleryDefaults = {
|
2014-07-03 22:03:14 -04:00
|
|
|
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 ) {
|
2014-07-24 16:00:16 -04:00
|
|
|
wp.media.galleryDefaults = _.extend( {}, wp.media._galleryDefaults, wp.media.view.settings.galleryDefaults );
|
|
|
|
} else {
|
|
|
|
wp.media.galleryDefaults = wp.media._galleryDefaults;
|
2014-07-03 22:03:14 -04:00
|
|
|
}
|
|
|
|
|
2014-02-27 14:22:14 -05:00
|
|
|
wp.media.gallery = new wp.media.collection({
|
|
|
|
tag: 'gallery',
|
|
|
|
type : 'image',
|
|
|
|
editTitle : wp.media.view.l10n.editGalleryTitle,
|
2014-07-24 16:00:16 -04:00
|
|
|
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;
|
|
|
|
}
|
2014-02-27 14:22:14 -05:00
|
|
|
});
|
|
|
|
|
2014-01-20 18:35:15 -05:00
|
|
|
/**
|
|
|
|
* wp.media.featuredImage
|
2014-01-22 12:17:11 -05:00
|
|
|
* @namespace
|
2014-01-20 18:35:15 -05:00
|
|
|
*/
|
2012-12-05 04:43:10 -05:00
|
|
|
wp.media.featuredImage = {
|
2014-01-20 18:35:15 -05:00
|
|
|
/**
|
|
|
|
* Get the featured image post ID
|
|
|
|
*
|
|
|
|
* @global wp.media.view.settings
|
|
|
|
*
|
2014-01-21 10:55:13 -05:00
|
|
|
* @returns {wp.media.view.settings.post.featuredImageId|number}
|
2014-01-20 18:35:15 -05:00
|
|
|
*/
|
2012-12-05 04:43:10 -05:00
|
|
|
get: function() {
|
|
|
|
return wp.media.view.settings.post.featuredImageId;
|
|
|
|
},
|
2014-01-20 18:35:15 -05:00
|
|
|
/**
|
|
|
|
* Set the featured image id, save the post thumbnail data and
|
|
|
|
* set the HTML in the post meta box to the new featured image.
|
|
|
|
*
|
|
|
|
* @global wp.media.view.settings
|
|
|
|
* @global wp.media.post
|
|
|
|
*
|
2014-01-22 13:38:11 -05:00
|
|
|
* @param {number} id The post ID of the featured image, or -1 to unset it.
|
2014-01-20 18:35:15 -05:00
|
|
|
*/
|
2012-12-05 04:43:10 -05:00
|
|
|
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 );
|
|
|
|
});
|
|
|
|
},
|
2014-01-20 18:35:15 -05:00
|
|
|
/**
|
|
|
|
* The Featured Image workflow
|
|
|
|
*
|
|
|
|
* @global wp.media.controller.FeaturedImage
|
|
|
|
* @global wp.media.view.l10n
|
|
|
|
*
|
2014-01-21 11:30:11 -05:00
|
|
|
* @this wp.media.featuredImage
|
|
|
|
*
|
2014-01-21 10:55:13 -05:00
|
|
|
* @returns {wp.media.view.MediaFrame.Select} A media workflow.
|
2014-01-20 18:35:15 -05:00
|
|
|
*/
|
2012-12-05 04:43:10 -05:00
|
|
|
frame: function() {
|
2014-01-20 18:35:15 -05:00
|
|
|
if ( this._frame ) {
|
2015-09-16 20:10:26 -04:00
|
|
|
wp.media.frame = this._frame;
|
2012-12-05 04:43:10 -05:00
|
|
|
return this._frame;
|
2014-01-20 18:35:15 -05:00
|
|
|
}
|
2012-12-05 04:43:10 -05:00
|
|
|
|
|
|
|
this._frame = wp.media({
|
|
|
|
state: 'featured-image',
|
2014-03-06 17:55:14 -05:00
|
|
|
states: [ new wp.media.controller.FeaturedImage() , new wp.media.controller.EditImage() ]
|
2012-12-05 04:43:10 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
this._frame.on( 'toolbar:create:featured-image', function( toolbar ) {
|
2014-01-21 11:30:11 -05:00
|
|
|
/**
|
|
|
|
* @this wp.media.view.MediaFrame.Select
|
|
|
|
*/
|
2012-12-05 04:43:10 -05:00
|
|
|
this.createSelectToolbar( toolbar, {
|
|
|
|
text: wp.media.view.l10n.setFeaturedImage
|
|
|
|
});
|
|
|
|
}, this._frame );
|
|
|
|
|
2014-03-06 17:55:14 -05:00
|
|
|
this._frame.on( 'content:render:edit-image', function() {
|
|
|
|
var selection = this.state('featured-image').get('selection'),
|
|
|
|
view = new wp.media.view.EditImage( { model: selection.single(), controller: this } ).render();
|
|
|
|
|
|
|
|
this.content.set( view );
|
|
|
|
|
|
|
|
// after bringing in the frame, load the actual editor via an ajax call
|
|
|
|
view.loadEditor();
|
|
|
|
|
|
|
|
}, this._frame );
|
|
|
|
|
2012-12-05 04:43:10 -05:00
|
|
|
this._frame.state('featured-image').on( 'select', this.select );
|
|
|
|
return this._frame;
|
|
|
|
},
|
2014-01-20 18:35:15 -05:00
|
|
|
/**
|
2014-01-22 13:38:11 -05:00
|
|
|
* 'select' callback for Featured Image workflow, triggered when
|
|
|
|
* the 'Set Featured Image' button is clicked in the media modal.
|
|
|
|
*
|
2014-01-20 18:35:15 -05:00
|
|
|
* @global wp.media.view.settings
|
2014-01-21 11:30:11 -05:00
|
|
|
*
|
|
|
|
* @this wp.media.controller.FeaturedImage
|
2014-01-20 18:35:15 -05:00
|
|
|
*/
|
2012-12-05 04:43:10 -05:00
|
|
|
select: function() {
|
2014-01-22 13:38:11 -05:00
|
|
|
var selection = this.get('selection').single();
|
2012-12-05 04:43:10 -05:00
|
|
|
|
2014-01-22 13:38:11 -05:00
|
|
|
if ( ! wp.media.view.settings.post.featuredImageId ) {
|
2012-12-05 04:43:10 -05:00
|
|
|
return;
|
2014-01-20 18:35:15 -05:00
|
|
|
}
|
2012-12-05 04:43:10 -05:00
|
|
|
|
|
|
|
wp.media.featuredImage.set( selection ? selection.id : -1 );
|
|
|
|
},
|
2014-01-20 18:35:15 -05:00
|
|
|
/**
|
|
|
|
* Open the content media manager to the 'featured image' tab when
|
|
|
|
* the post thumbnail is clicked.
|
|
|
|
*
|
|
|
|
* Update the featured image id when the 'remove' link is clicked.
|
|
|
|
*
|
|
|
|
* @global wp.media.view.settings
|
|
|
|
*/
|
2012-12-05 04:43:10 -05:00
|
|
|
init: function() {
|
|
|
|
$('#postimagediv').on( 'click', '#set-post-thumbnail', function( event ) {
|
|
|
|
event.preventDefault();
|
|
|
|
// Stop propagation to prevent thickbox from activating.
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
|
|
wp.media.featuredImage.frame().open();
|
|
|
|
}).on( 'click', '#remove-post-thumbnail', function() {
|
|
|
|
wp.media.view.settings.post.featuredImageId = -1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$( wp.media.featuredImage.init );
|
|
|
|
|
2014-01-20 18:35:15 -05:00
|
|
|
/**
|
|
|
|
* wp.media.editor
|
2014-01-22 12:17:11 -05:00
|
|
|
* @namespace
|
2014-01-20 18:35:15 -05:00
|
|
|
*/
|
2012-11-21 11:46:32 -05:00
|
|
|
wp.media.editor = {
|
2014-01-20 18:35:15 -05:00
|
|
|
/**
|
2014-01-22 13:38:11 -05:00
|
|
|
* Send content to the editor
|
|
|
|
*
|
2014-01-20 18:35:15 -05:00
|
|
|
* @global tinymce
|
|
|
|
* @global QTags
|
2014-01-21 10:55:13 -05:00
|
|
|
* @global wpActiveEditor
|
2014-01-22 13:38:11 -05:00
|
|
|
* @global tb_remove() - Possibly overloaded by legacy plugins
|
2014-01-20 18:35:15 -05:00
|
|
|
*
|
2014-01-22 13:38:11 -05:00
|
|
|
* @param {string} html Content to send to the editor
|
2014-01-20 18:35:15 -05:00
|
|
|
*/
|
2014-01-14 18:35:11 -05:00
|
|
|
insert: function( html ) {
|
2014-08-25 15:37:16 -04:00
|
|
|
var editor, wpActiveEditor,
|
2014-02-27 16:38:17 -05:00
|
|
|
hasTinymce = ! _.isUndefined( window.tinymce ),
|
2014-08-25 15:37:16 -04:00
|
|
|
hasQuicktags = ! _.isUndefined( window.QTags );
|
|
|
|
|
|
|
|
if ( this.activeEditor ) {
|
|
|
|
wpActiveEditor = window.wpActiveEditor = this.activeEditor;
|
|
|
|
} else {
|
2014-01-14 18:35:11 -05:00
|
|
|
wpActiveEditor = window.wpActiveEditor;
|
2014-08-25 15:37:16 -04:00
|
|
|
}
|
2012-11-21 12:01:40 -05:00
|
|
|
|
|
|
|
// 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.
|
2014-01-14 18:35:11 -05:00
|
|
|
if ( window.send_to_editor ) {
|
2012-11-21 12:01:40 -05:00
|
|
|
return window.send_to_editor.apply( this, arguments );
|
2014-01-14 18:35:11 -05:00
|
|
|
}
|
2012-11-21 12:01:40 -05:00
|
|
|
|
|
|
|
if ( ! wpActiveEditor ) {
|
2014-01-14 18:35:11 -05:00
|
|
|
if ( hasTinymce && tinymce.activeEditor ) {
|
|
|
|
editor = tinymce.activeEditor;
|
|
|
|
wpActiveEditor = window.wpActiveEditor = editor.id;
|
|
|
|
} else if ( ! hasQuicktags ) {
|
2012-11-21 12:01:40 -05:00
|
|
|
return false;
|
|
|
|
}
|
2014-01-14 18:35:11 -05:00
|
|
|
} else if ( hasTinymce ) {
|
|
|
|
editor = tinymce.get( wpActiveEditor );
|
2012-11-21 12:01:40 -05:00
|
|
|
}
|
|
|
|
|
2014-01-14 18:35:11 -05:00
|
|
|
if ( editor && ! editor.isHidden() ) {
|
|
|
|
editor.execCommand( 'mceInsertContent', false, html );
|
|
|
|
} else if ( hasQuicktags ) {
|
|
|
|
QTags.insertContent( html );
|
2012-11-21 12:01:40 -05:00
|
|
|
} else {
|
2014-01-14 18:35:11 -05:00
|
|
|
document.getElementById( wpActiveEditor ).value += html;
|
2012-11-21 12:01:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the old thickbox remove function exists, call it in case
|
|
|
|
// a theme/plugin overloaded it.
|
2014-01-14 18:35:11 -05:00
|
|
|
if ( window.tb_remove ) {
|
2012-11-21 12:01:40 -05:00
|
|
|
try { window.tb_remove(); } catch( e ) {}
|
2014-01-14 18:35:11 -05:00
|
|
|
}
|
2012-11-21 12:01:40 -05:00
|
|
|
},
|
2012-11-21 11:46:32 -05:00
|
|
|
|
2014-01-20 18:35:15 -05:00
|
|
|
/**
|
2014-01-22 13:38:11 -05:00
|
|
|
* Setup 'workflow' and add to the 'workflows' cache. 'open' can
|
|
|
|
* subsequently be called upon it.
|
|
|
|
*
|
2014-01-20 18:35:15 -05:00
|
|
|
* @global wp.media.view.l10n
|
|
|
|
*
|
|
|
|
* @param {string} id A slug used to identify the workflow.
|
|
|
|
* @param {Object} [options={}]
|
|
|
|
*
|
2014-01-21 11:30:11 -05:00
|
|
|
* @this wp.media.editor
|
|
|
|
*
|
2014-01-21 10:55:13 -05:00
|
|
|
* @returns {wp.media.view.MediaFrame.Select} A media workflow.
|
2014-01-20 18:35:15 -05:00
|
|
|
*/
|
2012-11-21 11:46:32 -05:00
|
|
|
add: function( id, options ) {
|
|
|
|
var workflow = this.get( id );
|
|
|
|
|
2014-01-20 18:35:15 -05:00
|
|
|
// only add once: if exists return existing
|
|
|
|
if ( workflow ) {
|
2013-06-19 17:55:17 -04:00
|
|
|
return workflow;
|
2014-01-20 18:35:15 -05:00
|
|
|
}
|
2013-06-19 17:55:17 -04:00
|
|
|
|
2012-11-21 11:46:32 -05:00
|
|
|
workflow = workflows[ id ] = wp.media( _.defaults( options || {}, {
|
|
|
|
frame: 'post',
|
2012-12-03 20:26:03 -05:00
|
|
|
state: 'insert',
|
2012-11-28 12:18:50 -05:00
|
|
|
title: wp.media.view.l10n.addMedia,
|
2012-11-21 11:46:32 -05:00
|
|
|
multiple: true
|
|
|
|
} ) );
|
|
|
|
|
|
|
|
workflow.on( 'insert', function( selection ) {
|
2012-11-22 01:30:25 -05:00
|
|
|
var state = workflow.state();
|
2012-11-21 11:46:32 -05:00
|
|
|
|
|
|
|
selection = selection || state.get('selection');
|
|
|
|
|
2012-11-22 01:30:25 -05:00
|
|
|
if ( ! selection )
|
2012-11-21 11:46:32 -05:00
|
|
|
return;
|
|
|
|
|
2012-12-03 23:38:42 -05:00
|
|
|
$.when.apply( $, selection.map( function( attachment ) {
|
2012-11-22 01:30:25 -05:00
|
|
|
var display = state.display( attachment ).toJSON();
|
2014-01-21 11:30:11 -05:00
|
|
|
/**
|
|
|
|
* @this wp.media.editor
|
|
|
|
*/
|
2012-12-03 23:38:42 -05:00
|
|
|
return this.send.attachment( display, attachment.toJSON() );
|
|
|
|
}, this ) ).done( function() {
|
2013-11-15 00:16:09 -05:00
|
|
|
wp.media.editor.insert( _.toArray( arguments ).join('\n\n') );
|
2012-12-03 23:38:42 -05:00
|
|
|
});
|
2012-11-21 11:46:32 -05:00
|
|
|
}, this );
|
|
|
|
|
2012-11-30 10:19:11 -05:00
|
|
|
workflow.state('gallery-edit').on( 'update', function( selection ) {
|
2014-01-21 11:30:11 -05:00
|
|
|
/**
|
|
|
|
* @this wp.media.editor
|
|
|
|
*/
|
2012-11-21 11:46:32 -05:00
|
|
|
this.insert( wp.media.gallery.shortcode( selection ).string() );
|
|
|
|
}, this );
|
|
|
|
|
2014-02-24 13:08:16 -05:00
|
|
|
workflow.state('playlist-edit').on( 'update', function( selection ) {
|
2014-02-24 15:26:14 -05:00
|
|
|
/**
|
|
|
|
* @this wp.media.editor
|
|
|
|
*/
|
2014-02-24 13:08:16 -05:00
|
|
|
this.insert( wp.media.playlist.shortcode( selection ).string() );
|
|
|
|
}, this );
|
|
|
|
|
|
|
|
workflow.state('video-playlist-edit').on( 'update', function( selection ) {
|
2014-02-24 15:26:14 -05:00
|
|
|
/**
|
|
|
|
* @this wp.media.editor
|
|
|
|
*/
|
2014-03-27 13:58:15 -04:00
|
|
|
this.insert( wp.media.playlist.shortcode( selection ).string() );
|
2014-02-24 13:08:16 -05:00
|
|
|
}, this );
|
|
|
|
|
2012-11-30 10:19:11 -05:00
|
|
|
workflow.state('embed').on( 'select', function() {
|
2014-01-21 11:30:11 -05:00
|
|
|
/**
|
|
|
|
* @this wp.media.editor
|
|
|
|
*/
|
2012-12-03 20:26:03 -05:00
|
|
|
var state = workflow.state(),
|
|
|
|
type = state.get('type'),
|
|
|
|
embed = state.props.toJSON();
|
2012-11-21 11:46:32 -05:00
|
|
|
|
|
|
|
embed.url = embed.url || '';
|
|
|
|
|
2012-12-03 20:26:03 -05:00
|
|
|
if ( 'link' === type ) {
|
2012-11-21 11:46:32 -05:00
|
|
|
_.defaults( embed, {
|
2015-04-21 10:36:25 -04:00
|
|
|
linkText: embed.url,
|
2012-11-21 11:46:32 -05:00
|
|
|
linkUrl: embed.url
|
|
|
|
});
|
|
|
|
|
2012-12-03 23:38:42 -05:00
|
|
|
this.send.link( embed ).done( function( resp ) {
|
|
|
|
wp.media.editor.insert( resp );
|
|
|
|
});
|
2012-11-21 11:46:32 -05:00
|
|
|
|
2012-12-03 20:26:03 -05:00
|
|
|
} else if ( 'image' === type ) {
|
2012-11-21 11:46:32 -05:00
|
|
|
_.defaults( embed, {
|
|
|
|
title: embed.url,
|
|
|
|
linkUrl: '',
|
|
|
|
align: 'none',
|
|
|
|
link: 'none'
|
|
|
|
});
|
|
|
|
|
2014-01-20 18:35:15 -05:00
|
|
|
if ( 'none' === embed.link ) {
|
2012-11-21 11:46:32 -05:00
|
|
|
embed.linkUrl = '';
|
2014-01-20 18:35:15 -05:00
|
|
|
} else if ( 'file' === embed.link ) {
|
2012-11-21 11:46:32 -05:00
|
|
|
embed.linkUrl = embed.url;
|
2014-01-20 18:35:15 -05:00
|
|
|
}
|
2012-11-21 11:46:32 -05:00
|
|
|
|
|
|
|
this.insert( wp.media.string.image( embed ) );
|
|
|
|
}
|
|
|
|
}, this );
|
|
|
|
|
2012-12-05 04:43:10 -05:00
|
|
|
workflow.state('featured-image').on( 'select', wp.media.featuredImage.select );
|
2012-12-02 21:38:10 -05:00
|
|
|
workflow.setState( workflow.options.state );
|
2012-11-21 11:46:32 -05:00
|
|
|
return workflow;
|
|
|
|
},
|
2014-01-20 18:35:15 -05:00
|
|
|
/**
|
|
|
|
* Determines the proper current workflow id
|
|
|
|
*
|
|
|
|
* @global wpActiveEditor
|
|
|
|
* @global tinymce
|
|
|
|
*
|
2014-01-22 13:38:11 -05:00
|
|
|
* @param {string} [id=''] A slug used to identify the workflow.
|
|
|
|
*
|
2014-01-21 10:55:13 -05:00
|
|
|
* @returns {wpActiveEditor|string|tinymce.activeEditor.id}
|
2014-01-20 18:35:15 -05:00
|
|
|
*/
|
2012-12-02 21:38:10 -05:00
|
|
|
id: function( id ) {
|
2014-01-20 18:35:15 -05:00
|
|
|
if ( id ) {
|
2012-12-02 21:38:10 -05:00
|
|
|
return id;
|
2014-01-20 18:35:15 -05:00
|
|
|
}
|
2012-12-02 21:38:10 -05:00
|
|
|
|
|
|
|
// If an empty `id` is provided, default to `wpActiveEditor`.
|
2014-08-25 15:37:16 -04:00
|
|
|
id = window.wpActiveEditor;
|
2012-12-02 21:38:10 -05:00
|
|
|
|
|
|
|
// If that doesn't work, fall back to `tinymce.activeEditor.id`.
|
2014-02-27 16:38:17 -05:00
|
|
|
if ( ! id && ! _.isUndefined( window.tinymce ) && tinymce.activeEditor ) {
|
2012-12-02 21:38:10 -05:00
|
|
|
id = tinymce.activeEditor.id;
|
2014-01-20 18:35:15 -05:00
|
|
|
}
|
2012-12-02 21:38:10 -05:00
|
|
|
|
|
|
|
// Last but not least, fall back to the empty string.
|
|
|
|
id = id || '';
|
|
|
|
return id;
|
|
|
|
},
|
2014-01-20 18:35:15 -05:00
|
|
|
/**
|
|
|
|
* Return the workflow specified by id
|
|
|
|
*
|
2014-01-22 13:38:11 -05:00
|
|
|
* @param {string} id A slug used to identify the workflow.
|
2014-01-21 11:30:11 -05:00
|
|
|
*
|
|
|
|
* @this wp.media.editor
|
|
|
|
*
|
2014-01-20 18:35:15 -05:00
|
|
|
* @returns {wp.media.view.MediaFrame} A media workflow.
|
|
|
|
*/
|
2012-11-21 11:46:32 -05:00
|
|
|
get: function( id ) {
|
2012-12-02 21:38:10 -05:00
|
|
|
id = this.id( id );
|
2012-11-21 11:46:32 -05:00
|
|
|
return workflows[ id ];
|
|
|
|
},
|
2014-01-20 18:35:15 -05:00
|
|
|
/**
|
|
|
|
* Remove the workflow represented by id from the workflow cache
|
|
|
|
*
|
2014-01-22 13:38:11 -05:00
|
|
|
* @param {string} id A slug used to identify the workflow.
|
2014-01-21 11:30:11 -05:00
|
|
|
*
|
|
|
|
* @this wp.media.editor
|
2014-01-20 18:35:15 -05:00
|
|
|
*/
|
2012-11-21 11:46:32 -05:00
|
|
|
remove: function( id ) {
|
2012-12-02 21:38:10 -05:00
|
|
|
id = this.id( id );
|
2012-11-21 11:46:32 -05:00
|
|
|
delete workflows[ id ];
|
|
|
|
},
|
2014-01-22 13:38:11 -05:00
|
|
|
/**
|
|
|
|
* @namespace
|
|
|
|
*/
|
2012-11-21 11:46:32 -05:00
|
|
|
send: {
|
2014-01-20 18:35:15 -05:00
|
|
|
/**
|
2014-01-21 10:55:13 -05:00
|
|
|
* Called when sending an attachment to the editor
|
|
|
|
* from the medial modal.
|
|
|
|
*
|
2014-01-20 18:35:15 -05:00
|
|
|
* @global wp.media.view.settings
|
|
|
|
* @global wp.media.post
|
|
|
|
*
|
2014-01-21 10:55:13 -05:00
|
|
|
* @param {Object} props Attachment details (align, link, size, etc).
|
|
|
|
* @param {Object} attachment The attachment object, media version of Post.
|
2014-01-20 18:35:15 -05:00
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
2012-11-21 11:46:32 -05:00
|
|
|
attachment: function( props, attachment ) {
|
|
|
|
var caption = attachment.caption,
|
|
|
|
options, html;
|
|
|
|
|
|
|
|
// If captions are disabled, clear the caption.
|
2014-01-20 18:35:15 -05:00
|
|
|
if ( ! wp.media.view.settings.captions ) {
|
2012-11-21 11:46:32 -05:00
|
|
|
delete attachment.caption;
|
2014-01-20 18:35:15 -05:00
|
|
|
}
|
2012-11-21 11:46:32 -05:00
|
|
|
|
|
|
|
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-05 19:41:06 -05:00
|
|
|
id: attachment.id,
|
2012-12-05 22:36:17 -05:00
|
|
|
post_content: attachment.description,
|
2012-12-06 20:13:04 -05:00
|
|
|
post_excerpt: caption
|
2012-11-21 11:46:32 -05:00
|
|
|
};
|
|
|
|
|
2014-01-20 18:35:15 -05:00
|
|
|
if ( props.linkUrl ) {
|
2012-11-25 22:33:30 -05:00
|
|
|
options.url = props.linkUrl;
|
2014-01-20 18:35:15 -05:00
|
|
|
}
|
2012-11-25 22:33:30 -05:00
|
|
|
|
2012-11-21 11:46:32 -05:00
|
|
|
if ( 'image' === attachment.type ) {
|
|
|
|
html = wp.media.string.image( props );
|
|
|
|
|
|
|
|
_.each({
|
2012-11-26 17:08:50 -05:00
|
|
|
align: 'align',
|
2012-11-25 22:33:30 -05:00
|
|
|
size: 'image-size',
|
2012-11-26 17:08:50 -05:00
|
|
|
alt: 'image_alt'
|
2012-11-21 11:46:32 -05:00
|
|
|
}, function( option, prop ) {
|
|
|
|
if ( props[ prop ] )
|
|
|
|
options[ option ] = props[ prop ];
|
|
|
|
});
|
2013-03-16 01:25:44 -04:00
|
|
|
} else if ( 'video' === attachment.type ) {
|
2013-03-29 16:35:23 -04:00
|
|
|
html = wp.media.string.video( props, attachment );
|
2013-03-16 01:25:44 -04:00
|
|
|
} else if ( 'audio' === attachment.type ) {
|
2013-03-29 16:35:23 -04:00
|
|
|
html = wp.media.string.audio( props, attachment );
|
2012-11-21 11:46:32 -05:00
|
|
|
} else {
|
|
|
|
html = wp.media.string.link( props );
|
2012-11-26 17:08:50 -05:00
|
|
|
options.post_title = props.title;
|
2012-11-21 11:46:32 -05:00
|
|
|
}
|
|
|
|
|
2012-12-02 11:55:58 -05:00
|
|
|
return wp.media.post( 'send-attachment-to-editor', {
|
2012-11-21 11:46:32 -05:00
|
|
|
nonce: wp.media.view.settings.nonce.sendToEditor,
|
|
|
|
attachment: options,
|
2012-11-26 11:34:42 -05:00
|
|
|
html: html,
|
2012-12-03 02:17:10 -05:00
|
|
|
post_id: wp.media.view.settings.post.id
|
2012-11-21 11:46:32 -05:00
|
|
|
});
|
|
|
|
},
|
2014-01-20 18:35:15 -05:00
|
|
|
/**
|
2014-01-21 10:55:13 -05:00
|
|
|
* Called when 'Insert From URL' source is not an image. Example: YouTube url.
|
|
|
|
*
|
2014-01-20 18:35:15 -05:00
|
|
|
* @global wp.media.view.settings
|
|
|
|
*
|
|
|
|
* @param {Object} embed
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
2012-11-21 11:46:32 -05:00
|
|
|
link: function( embed ) {
|
2012-12-02 11:55:58 -05:00
|
|
|
return wp.media.post( 'send-link-to-editor', {
|
2015-04-06 14:25:27 -04:00
|
|
|
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
|
2012-11-21 11:46:32 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2014-01-20 18:35:15 -05:00
|
|
|
/**
|
2014-01-22 13:38:11 -05:00
|
|
|
* Open a workflow
|
|
|
|
*
|
|
|
|
* @param {string} [id=undefined] Optional. A slug used to identify the workflow.
|
2014-01-22 12:17:11 -05:00
|
|
|
* @param {Object} [options={}]
|
2014-01-21 11:30:11 -05:00
|
|
|
*
|
|
|
|
* @this wp.media.editor
|
|
|
|
*
|
2014-01-20 18:35:15 -05:00
|
|
|
* @returns {wp.media.view.MediaFrame}
|
|
|
|
*/
|
2013-04-25 01:43:14 -04:00
|
|
|
open: function( id, options ) {
|
2014-08-12 00:27:18 -04:00
|
|
|
var workflow;
|
2012-12-02 21:38:10 -05:00
|
|
|
|
2013-04-25 01:43:14 -04:00
|
|
|
options = options || {};
|
|
|
|
|
2012-12-02 21:38:10 -05:00
|
|
|
id = this.id( id );
|
2014-08-25 15:37:16 -04:00
|
|
|
this.activeEditor = id;
|
2014-08-03 18:34:16 -04:00
|
|
|
|
2012-12-02 21:38:10 -05:00
|
|
|
workflow = this.get( id );
|
|
|
|
|
2013-04-25 01:43:14 -04:00
|
|
|
// Redo workflow if state has changed
|
2014-01-20 18:35:15 -05:00
|
|
|
if ( ! workflow || ( workflow.options && options.state !== workflow.options.state ) ) {
|
2013-04-25 01:43:14 -04:00
|
|
|
workflow = this.add( id, options );
|
2014-01-20 18:35:15 -05:00
|
|
|
}
|
2012-12-02 21:38:10 -05:00
|
|
|
|
2015-09-16 20:10:26 -04:00
|
|
|
wp.media.frame = workflow;
|
|
|
|
|
2012-12-02 21:38:10 -05:00
|
|
|
return workflow.open();
|
|
|
|
},
|
|
|
|
|
2014-01-20 18:35:15 -05:00
|
|
|
/**
|
|
|
|
* Bind click event for .insert-media using event delegation
|
|
|
|
*
|
|
|
|
* @global wp.media.view.l10n
|
|
|
|
*/
|
2012-11-21 11:46:32 -05:00
|
|
|
init: function() {
|
2014-03-14 09:17:16 -04:00
|
|
|
$(document.body)
|
2014-10-01 15:08:16 -04:00
|
|
|
.on( 'click.add-media-button', '.insert-media', function( event ) {
|
2014-03-14 09:17:16 -04:00
|
|
|
var elem = $( event.currentTarget ),
|
|
|
|
editor = elem.data('editor'),
|
|
|
|
options = {
|
|
|
|
frame: 'post',
|
|
|
|
state: 'insert',
|
|
|
|
title: wp.media.view.l10n.addMedia,
|
|
|
|
multiple: true
|
|
|
|
};
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
// Remove focus from the `.insert-media` button.
|
|
|
|
// Prevents Opera from showing the outline of the button
|
|
|
|
// above the modal.
|
|
|
|
//
|
2014-09-29 09:28:16 -04:00
|
|
|
// See: https://core.trac.wordpress.org/ticket/22445
|
2014-03-14 09:17:16 -04:00
|
|
|
elem.blur();
|
|
|
|
|
|
|
|
if ( elem.hasClass( 'gallery' ) ) {
|
|
|
|
options.state = 'gallery';
|
|
|
|
options.title = wp.media.view.l10n.createGalleryTitle;
|
|
|
|
}
|
2014-03-13 19:39:16 -04:00
|
|
|
|
2014-03-14 09:17:16 -04:00
|
|
|
wp.media.editor.open( editor, options );
|
2014-03-19 01:31:15 -04:00
|
|
|
});
|
2014-03-01 16:34:17 -05:00
|
|
|
|
|
|
|
// Initialize and render the Editor drag-and-drop uploader.
|
|
|
|
new wp.media.view.EditorUploader().render();
|
2012-11-21 11:46:32 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-12-02 21:38:10 -05:00
|
|
|
_.bindAll( wp.media.editor, 'open' );
|
2012-11-21 11:46:32 -05:00
|
|
|
$( wp.media.editor.init );
|
2014-02-27 14:22:14 -05:00
|
|
|
}(jQuery, _));
|