Вход Регистрация
Файл: wordpress/wp-includes/js/media-editor.js
Строк: 1027
<?php
/* global getUserSetting, tinymce, QTags */

// WordPress, TinyMCE, and Media
// -----------------------------
(function($, _){
    
/**
     * Stores the editors' `wp.media.controller.Frame` instances.
     *
     * @static
     */
    
var workflows = {};

    
/**
     * 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 ( attrskey ) {
        if ( 
_.isUndefinedattrskey ] ) && ! _.isUndefinedthis.defaultskey ] ) ) {
            
attrskey ] = this.defaultskey ];
        } else if ( 
'true' === attrskey ] ) {
            
attrskey ] = true;
        } else if ( 
'false' === attrskey ] ) {
            
attrskey ] = false;
        }
        return 
attrskey ];
    };

    
/**
     * wp.media.string
     * @namespace
     */
    
wp.media.string = {
        
/**
         * Joins the `props` and `attachment` objects,
         * outputting the proper object format based on the
         * attachment's type.
         *
         * @global wp.media.view.settings
         * @global getUserSetting()
         *
         * @param {Object} [props={}] Attachment details (align, link, size, etc).
         * @param {Object} attachment The attachment object, media version of Post.
         * @returns {Object} Joined props
         */
        
props: function( propsattachment ) {
            var 
linklinkUrlsizesizesfallbacks,
                
defaultProps wp.media.view.settings.defaultProps;

            
// 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'' );
                    
props.alt props.alt.replace( /[rn]+/g' ' );
                }

                return 
props;
            };

            
props props _.clone( props ) : {};

            if ( 
attachment && attachment.type ) {
                
props.type attachment.type;
            }

            if ( 
'image' === props.type ) {
                
props _.defaultsprops || {}, {
                    
align:   defaultProps.align || getUserSetting'align''none' ),
                    
size:    defaultProps.size  || getUserSetting'imgsize''medium' ),
                    
url:     '',
                    
classes: []
                });
            }

            
// All attachment-specific settings follow.
            
if ( ! attachment ) {
                return 
fallbacksprops );
            }

            
props.title props.title || attachment.title;

            
link props.link || defaultProps.link || getUserSetting'urlbutton''file' );
            if ( 
'file' === link || 'embed' === link ) {
                
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 && sizesprops.size ] ? sizesprops.size ] : attachment;

                
_.extendprops_.pickattachment'align''caption''alt' ), {
                    
width:     size.width,
                    
height:    size.height,
                    
src:       size.url,
                    
captionId'attachment_' attachment.id
                
});
            } else if ( 
'video' === attachment.type || 'audio' === attachment.type ) {
                
_.extendprops_.pickattachment'title''type''icon''mime' ) );
            
// Format properties for non-images.
            
} else {
                
props.title props.title || attachment.filename;
                
props.rel props.rel || 'attachment wp-att-' attachment.id;
            }

            return 
fallbacksprops );
        },
        
/**
         * Create link markup that is suitable for passing to the editor
         *
         * @global wp.html.string
         *
         * @param {Object} props Attachment details (align, link, size, etc).
         * @param {Object} attachment The attachment object, media version of Post.
         * @returns {string} The link markup
         */
        
link: function( propsattachment ) {
            var 
options;

            
props wp.media.string.propspropsattachment );

            
options = {
                
tag:     'a',
                
contentprops.title,
                
attrs:   {
                    
hrefprops.linkUrl
                
}
            };

            if ( 
props.rel ) {
                
options.attrs.rel props.rel;
            }

            return 
wp.html.stringoptions );
        },
        
/**
         * Create an Audio shortcode string that is suitable for passing to the editor
         *
         * @param {Object} props Attachment details (align, link, size, etc).
         * @param {Object} attachment The attachment object, media version of Post.
         * @returns {string} The audio shortcode
         */
        
audio: function( propsattachment ) {
            return 
wp.media.string._audioVideo'audio'propsattachment );
        },
        
/**
         * Create a Video shortcode string that is suitable for passing to the editor
         *
         * @param {Object} props Attachment details (align, link, size, etc).
         * @param {Object} attachment The attachment object, media version of Post.
         * @returns {string} The video shortcode
         */
        
video: function( propsattachment ) {
            return 
wp.media.string._audioVideo'video'propsattachment );
        },
        
/**
         * Helper function to create a media shortcode string
         *
         * @access private
         *
         * @global wp.shortcode
         * @global wp.media.view.settings
         *
         * @param {string} type The shortcode tag name: 'audio' or 'video'.
         * @param {Object} props Attachment details (align, link, size, etc).
         * @param {Object} attachment The attachment object, media version of Post.
         * @returns {string} The media shortcode
         */
        
_audioVideo: function( typepropsattachment ) {
            var 
shortcodehtmlextension;

            
props wp.media.string.propspropsattachment );
            if ( 
props.link !== 'embed' )
                return 
wp.media.string.linkprops );

            
shortcode = {};

            if ( 
'video' === type ) {
                if ( 
attachment.image && -=== attachment.image.src.indexOfattachment.icon ) ) {
                    
shortcode.poster attachment.image.src;
                }

                if ( 
attachment.width ) {
                    
shortcode.width attachment.width;
                }

                if ( 
attachment.height ) {
                    
shortcode.height attachment.height;
                }
            }

            
extension attachment.filename.split('.').pop();

            if ( 
_.containswp.media.view.settings.embedExtsextension ) ) {
                
shortcode[extension] = attachment.url;
            } else {
                
// Render unsupported audio and video files as links.
                
return wp.media.string.linkprops );
            }

            
html wp.shortcode.string({
                
tag:     type,
                
attrs:   shortcode
            
});

            return 
html;
        },
        
/**
         * Create image markup, optionally with a link and/or wrapped in a caption shortcode,
         *  that is suitable for passing to the editor
         *
         * @global wp.html
         * @global wp.shortcode
         *
         * @param {Object} props Attachment details (align, link, size, etc).
         * @param {Object} attachment The attachment object, media version of Post.
         * @returns {string}
         */
        
image: function( propsattachment ) {
            var 
img = {},
                
optionsclassesshortcodehtml;

            
props wp.media.string.propspropsattachment );
            
classes props.classes || [];

            
img.src = ! _.isUndefinedattachment ) ? attachment.url props.url;
            
_.extendimg_.pickprops'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'] = _.compactclasses ).join(' ');

            
// Generate `img` tag options.
            
options = {
                
tag:    'img',
                
attrs:  img,
                
singletrue
            
};

            
// Generate the `a` element options, if they exist.
            
if ( props.linkUrl ) {
                
options = {
                    
tag:   'a',
                    
attrs: {
                        
hrefprops.linkUrl
                    
},
                    
contentoptions
                
};
            }

            
html wp.html.stringoptions );

            
// 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,
                    
contenthtml ' ' props.caption
                
});
            }

            return 
html;
        }
    };

    
wp.media.embed = {
        
coerce wp.media.coerce,

        
defaults : {
            
url '',
            
width'',
            
height''
        
},

        
edit : function( dataisURL ) {
            var 
frameprops = {}, shortcode;

            if ( 
isURL ) {
                
props.url data.replace(/<[^>]+>/g'');
            } else {
                
shortcode wp.shortcode.next'embed'data ).shortcode;

                
props _.defaultsshortcode.attrs.namedthis.defaults );
                if ( 
shortcode.content ) {
                    
props.url shortcode.content;
                }
            }

            
frame wp.media({
                
frame'post',
                
state'embed',
                
metadataprops
            
});

            return 
frame;
        },

        
shortcode : function( model ) {
            var 
self thiscontent;

            
_.eachthis.defaults, function( valuekey ) {
                
modelkey ] = self.coercemodelkey );

                if ( 
value === modelkey ] ) {
                    
delete modelkey ];
                }
            });

            
content model.url;
            
delete model.url;

            return new 
wp.shortcode({
                
tag'embed',
                
attrsmodel,
                
contentcontent
            
});
        }
    };

    
wp.media.collection = function(attributes) {
        var 
collections = {};

        return 
_.extend( {
            
coerce wp.media.coerce,
            
/**
             * 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.
             */
            
attachments: function( shortcode ) {
                var 
shortcodeString shortcode.string(),
                    
result collectionsshortcodeString ],
                    
attrsargsqueryothersself this;

                
delete collectionsshortcodeString ];
                if ( 
result ) {
                    return 
result;
                }
                
// Fill the default shortcode attributes.
                
attrs _.defaultsshortcode.attrs.namedthis.defaults );
                
args  _.pickattrs'orderby''order' );

                
args.type    this.type;
                
args.perPage = -1;

                
// Mark the `orderby` override attribute.
                
if ( undefined !== attrs.orderby ) {
                    
attrs._orderByField attrs.orderby;
                }

                if ( 
'rand' === attrs.orderby ) {
                    
attrs._orderbyRandom true;
                }

                
// Map the `orderby` attribute to the corresponding model property.
                
if ( ! attrs.orderby || /^menu_order(?: ID)?$/i.testattrs.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 _.omitattrs'id''ids''include''exclude''orderby''order' );

                
_.eachthis.defaults, function( valuekey ) {
                    
otherskey ] = self.coerceotherskey );
                });

                
query wp.media.queryargs );
                
querythis.tag ] = new Backbone.Modelothers );
                return 
query;
            },
            
/**
             * 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}
             */
            
shortcode: function( attachments ) {
                var 
props attachments.props.toJSON(),
                    
attrs _.pickprops'orderby''order' ),
                    
shortcode, clone;

                if ( 
attachments.type ) {
                    
attrs.type attachments.type;
                    
delete attachments.type;
                }

                if ( 
attachments[this.tag] ) {
                    
_.extendattrsattachments[this.tag].toJSON() );
                }

                
// 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.
                
delete attrs.orderby;

                if ( 
attrs._orderbyRandom ) {
                    
attrs.orderby 'rand';
                } else if ( 
attrs._orderByField && attrs._orderByField != 'rand' ) {
                    
attrs.orderby attrs._orderByField;
                }

                
delete attrs._orderbyRandom;
                
delete attrs._orderByField;

                
// 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;
                }

                
attrs this.setDefaultsattrs );

                
shortcode = new wp.shortcode({
                    
tag:    this.tag,
                    
attrs:  attrs,
                    
type:   'single'
                
});

                
// Use a cloned version of the gallery.
                
clone = new wp.media.model.Attachmentsattachments.models, {
                    
propsprops
                
});
                clone[ 
this.tag ] = attachmentsthis.tag ];
                
collectionsshortcode.string() ] = clone;

                return 
shortcode;
            },
            
/**
             * 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.
             */
            
edit: function( content ) {
                var 
shortcode wp.shortcode.nextthis.tagcontent ),
                    
defaultPostId this.defaults.id,
                    
attachmentsselectionstate;

                
// 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 ( 
_.isUndefinedshortcode.get('id') ) && ! _.isUndefineddefaultPostId ) ) {
                    
shortcode.set'id'defaultPostId );
                }

                
attachments this.attachmentsshortcode );

                
selection = new wp.media.model.Selectionattachments.models, {
                    
props:    attachments.props.toJSON(),
                    
multipletrue
                
});

                
selectionthis.tag ] = attachmentsthis.tag ];

                
// 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({ queryfalse });
                    
selection.unmirror();
                    
selection.props.unset('orderby');
                });

                
// Destroy the previous gallery frame.
                
if ( this.frame ) {
                    
this.frame.dispose();
                }

                if ( 
shortcode.attrs.named.type && 'video' === shortcode.attrs.named.type ) {
                    
state 'video-' this.tag '-edit';
                } else {
                    
state this.tag '-edit';
                }

                
// Store the current frame.
                
this.frame wp.media({
                    
frame:     'post',
                    
state:     state,
                    
title:     this.editTitle,
                    
editing:   true,
                    
multiple:  true,
                    
selectionselection
                
}).open();

                return 
this.frame;
            },

            
setDefaults: function( attrs ) {
                var 
self this;
                
// Remove default attributes from the shortcode.
                
_.eachthis.defaults, function( valuekey ) {
                    
attrskey ] = self.coerceattrskey );
                    if ( 
value === attrskey ] ) {
                        
delete attrskey ];
                    }
                });

                return 
attrs;
            }
        }, 
attributes );
    };

    
wp.media._galleryDefaults = {
        
itemtag'dl',
        
icontag'dt',
        
captiontag'dd',
        
columns'3',
        
link'post',
        
size'thumbnail',
        
order'ASC',
        
idwp.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._galleryDefaultswp.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 wp.media.galleryDefaults,

        
setDefaults: function( attrs ) {
            var 
self thischanged = ! _.isEqualwp.media.galleryDefaultswp.media._galleryDefaults );
            
_.eachthis.defaults, function( valuekey ) {
                
attrskey ] = self.coerceattrskey );
                if ( 
value === attrskey ] && ( ! changed || value === wp.media._galleryDefaultskey ] ) ) {
                    
delete attrskey ];
                }
            } );
            return 
attrs;
        }
    });

    
/**
     * wp.media.featuredImage
     * @namespace
     */
    
wp.media.featuredImage = {
        
/**
         * Get the featured image post ID
         *
         * @global wp.media.view.settings
         *
         * @returns {wp.media.view.settings.post.featuredImageId|number}
         */
        
get: function() {
            return 
wp.media.view.settings.post.featuredImageId;
        },
        
/**
         * 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
         *
         * @param {number} id The post ID of the featured image, or -1 to unset it.
         */
        
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_idsettings.post.featuredImageId,
                
_wpnonce:     settings.post.nonce
            
}).done( function( html ) {
                $( 
'.inside''#postimagediv' ).htmlhtml );
            });
        },
        
/**
         * The Featured Image workflow
         *
         * @global wp.media.controller.FeaturedImage
         * @global wp.media.view.l10n
         *
         * @this wp.media.featuredImage
         *
         * @returns {wp.media.view.MediaFrame.Select} A media workflow.
         */
        
frame: function() {
            if ( 
this._frame ) {
                return 
this._frame;
            }

            
this._frame wp.media({
                
state'featured-image',
                
states: [ new wp.media.controller.FeaturedImage() , new wp.media.controller.EditImage() ]
            });

            
this._frame.on'toolbar:create:featured-image', function( toolbar ) {
                
/**
                 * @this wp.media.view.MediaFrame.Select
                 */
                
this.createSelectToolbartoolbar, {
                    
textwp.media.view.l10n.setFeaturedImage
                
});
            }, 
this._frame );

            
this._frame.on'content:render:edit-image', function() {
                var 
selection this.state('featured-image').get('selection'),
                    
view = new wp.media.view.EditImage( { modelselection.single(), controllerthis } ).render();

                
this.content.setview );

                
// after bringing in the frame, load the actual editor via an ajax call
                
view.loadEditor();

            }, 
this._frame );

            
this._frame.state('featured-image').on'select'this.select );
            return 
this._frame;
        },
        
/**
         * 'select' callback for Featured Image workflow, triggered when
         *  the 'Set Featured Image' button is clicked in the media modal.
         *
         * @global wp.media.view.settings
         *
         * @this wp.media.controller.FeaturedImage
         */
        
select: function() {
            var 
selection this.get('selection').single();

            if ( ! 
wp.media.view.settings.post.featuredImageId ) {
                return;
            }

            
wp.media.featuredImage.setselection selection.id : -);
        },
        
/**
         * 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
         */
        
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 );

    
/**
     * wp.media.editor
     * @namespace
     */
    
wp.media.editor = {
        
/**
         * Send content to the editor
         *
         * @global tinymce
         * @global QTags
         * @global wpActiveEditor
         * @global tb_remove() - Possibly overloaded by legacy plugins
         *
         * @param {string} html Content to send to the editor
         */
        
insert: function( html ) {
            var 
editorwpActiveEditor,
                
hasTinymce = ! _.isUndefinedwindow.tinymce ),
                
hasQuicktags = ! _.isUndefinedwindow.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
            // overridden the insert functionality.
            
if ( window.send_to_editor ) {
                return 
window.send_to_editor.applythisarguments );
            }

            if ( ! 
wpActiveEditor ) {
                if ( 
hasTinymce && tinymce.activeEditor ) {
                    
editor tinymce.activeEditor;
                    
wpActiveEditor window.wpActiveEditor editor.id;
                } else if ( ! 
hasQuicktags ) {
                    return 
false;
                }
            } else if ( 
hasTinymce ) {
                
editor tinymce.getwpActiveEditor );
            }

            if ( 
editor && ! editor.isHidden() ) {
                
editor.execCommand'mceInsertContent'falsehtml );
            } else if ( 
hasQuicktags ) {
                
QTags.insertContenthtml );
            } else {
                
document.getElementByIdwpActiveEditor ).value += html;
            }

            
// 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( ) {}
            }
        },

        
/**
         * Setup 'workflow' and add to the 'workflows' cache. 'open' can
         *  subsequently be called upon it.
         *
         * @global wp.media.view.l10n
         *
         * @param {string} id A slug used to identify the workflow.
         * @param {Object} [options={}]
         *
         * @this wp.media.editor
         *
         * @returns {wp.media.view.MediaFrame.Select} A media workflow.
         */
        
add: function( idoptions ) {
            var 
workflow this.getid );

            
// only add once: if exists return existing
            
if ( workflow ) {
                return 
workflow;
            }

            
workflow workflowsid ] = wp.media_.defaultsoptions || {}, {
                
frame:    'post',
                
state:    'insert',
                
title:    wp.media.view.l10n.addMedia,
                
multipletrue
            
} ) );

            
workflow.on'insert', function( selection ) {
                var 
state workflow.state();

                
selection selection || state.get('selection');

                if ( ! 
selection )
                    return;

                $.
when.apply( $, selection.map( function( attachment ) {
                    var 
display state.displayattachment ).toJSON();
                    
/**
                     * @this wp.media.editor
                     */
                    
return this.send.attachmentdisplayattachment.toJSON() );
                }, 
this ) ).done( function() {
                    
wp.media.editor.insert_.toArrayarguments ).join('nn') );
                });
            }, 
this );

            
workflow.state('gallery-edit').on'update', function( selection ) {
                
/**
                 * @this wp.media.editor
                 */
                
this.insertwp.media.gallery.shortcodeselection ).string() );
            }, 
this );

            
workflow.state('playlist-edit').on'update', function( selection ) {
                
/**
                 * @this wp.media.editor
                 */
                
this.insertwp.media.playlist.shortcodeselection ).string() );
            }, 
this );

            
workflow.state('video-playlist-edit').on'update', function( selection ) {
                
/**
                 * @this wp.media.editor
                 */
                
this.insertwp.media.playlist.shortcodeselection ).string() );
            }, 
this );

            
workflow.state('embed').on'select', function() {
                
/**
                 * @this wp.media.editor
                 */
                
var state workflow.state(),
                    
type state.get('type'),
                    
embed state.props.toJSON();

                
embed.url embed.url || '';

                if ( 
'link' === type ) {
                    
_.defaultsembed, {
                        
linkTextembed.url,
                        
linkUrlembed.url
                    
});

                    
this.send.linkembed ).done( function( resp ) {
                        
wp.media.editor.insertresp );
                    });

                } else if ( 
'image' === type ) {
                    
_.defaultsembed, {
                        
title:   embed.url,
                        
linkUrl'',
                        
align:   'none',
                        
link:    'none'
                    
});

                    if ( 
'none' === embed.link ) {
                        
embed.linkUrl '';
                    } else if ( 
'file' === embed.link ) {
                        
embed.linkUrl embed.url;
                    }

                    
this.insertwp.media.string.imageembed ) );
                }
            }, 
this );

            
workflow.state('featured-image').on'select'wp.media.featuredImage.select );
            
workflow.setStateworkflow.options.state );
            return 
workflow;
        },
        
/**
         * Determines the proper current workflow id
         *
         * @global wpActiveEditor
         * @global tinymce
         *
         * @param {string} [id=''] A slug used to identify the workflow.
         *
         * @returns {wpActiveEditor|string|tinymce.activeEditor.id}
         */
        
id: function( id ) {
            if ( 
id ) {
                return 
id;
            }

            
// If an empty `id` is provided, default to `wpActiveEditor`.
            
id window.wpActiveEditor;

            
// If that doesn't work, fall back to `tinymce.activeEditor.id`.
            
if ( ! id && ! _.isUndefinedwindow.tinymce ) && tinymce.activeEditor ) {
                
id tinymce.activeEditor.id;
            }

            
// Last but not least, fall back to the empty string.
            
id id || '';
            return 
id;
        },
        
/**
         * Return the workflow specified by id
         *
         * @param {string} id A slug used to identify the workflow.
         *
         * @this wp.media.editor
         *
         * @returns {wp.media.view.MediaFrame} A media workflow.
         */
        
get: function( id ) {
            
id this.idid );
            return 
workflowsid ];
        },
        
/**
         * Remove the workflow represented by id from the workflow cache
         *
         * @param {string} id A slug used to identify the workflow.
         *
         * @this wp.media.editor
         */
        
remove: function( id ) {
            
id this.idid );
            
delete workflowsid ];
        },
        
/**
         * @namespace
         */
        
send: {
            
/**
             * Called when sending an attachment to the editor
             *   from the medial modal.
             *
             * @global wp.media.view.settings
             * @global wp.media.post
             *
             * @param {Object} props Attachment details (align, link, size, etc).
             * @param {Object} attachment The attachment object, media version of Post.
             * @returns {Promise}
             */
            
attachment: function( propsattachment ) {
                var 
caption attachment.caption,
                    
optionshtml;

                
// If captions are disabled, clear the caption.
                
if ( ! wp.media.view.settings.captions ) {
                    
delete attachment.caption;
                }

                
props wp.media.string.propspropsattachment );

                
options = {
                    
id:           attachment.id,
                    
post_contentattachment.description,
                    
post_excerptcaption
                
};

                if ( 
props.linkUrl ) {
                    
options.url props.linkUrl;
                }

                if ( 
'image' === attachment.type ) {
                    
html wp.media.string.imageprops );

                    
_.each({
                        
align'align',
                        
size:  'image-size',
                        
alt:   'image_alt'
                    
}, function( optionprop ) {
                        if ( 
propsprop ] )
                            
optionsoption ] = propsprop ];
                    });
                } else if ( 
'video' === attachment.type ) {
                    
html wp.media.string.videopropsattachment );
                } else if ( 
'audio' === attachment.type ) {
                    
html wp.media.string.audiopropsattachment );
                } else {
                    
html wp.media.string.linkprops );
                    
options.post_title props.title;
                }

                return 
wp.media.post'send-attachment-to-editor', {
                    
nonce:      wp.media.view.settings.nonce.sendToEditor,
                    
attachmentoptions,
                    
html:       html,
                    
post_id:    wp.media.view.settings.post.id
                
});
            },
            
/**
             * Called when 'Insert From URL' source is not an image. Example: YouTube url.
             *
             * @global wp.media.view.settings
             *
             * @param {Object} embed
             * @returns {Promise}
             */
            
link: function( embed ) {
                return 
wp.media.post'send-link-to-editor', {
                    
nonce:     wp.media.view.settings.nonce.sendToEditor,
                    
src:       embed.linkUrl,
                    
link_textembed.linkText,
                    
html:      wp.media.string.linkembed ),
                    
post_id:   wp.media.view.settings.post.id
                
});
            }
        },
        
/**
         * Open a workflow
         *
         * @param {string} [id=undefined] Optional. A slug used to identify the workflow.
         * @param {Object} [options={}]
         *
         * @this wp.media.editor
         *
         * @returns {wp.media.view.MediaFrame}
         */
        
open: function( idoptions ) {
            var 
workflow;

            
options options || {};

            
id this.idid );
            
this.activeEditor id;

            
workflow this.getid );

            
// Redo workflow if state has changed
            
if ( ! workflow || ( workflow.options && options.state !== workflow.options.state ) ) {
                
workflow this.addidoptions );
            }

            return 
workflow.open();
        },

        
/**
         * Bind click event for .insert-media using event delegation
         *
         * @global wp.media.view.l10n
         */
        
init: function() {
            $(
document.body)
                .
on'click.add-media-button''.insert-media', function( event ) {
                    var 
elem = $( event.currentTarget ),
                        
editor elem.data('editor'),
                        
options = {
                            
frame:    'post',
                            
state:    'insert',
                            
title:    wp.media.view.l10n.addMedia,
                            
multipletrue
                        
};

                    
event.preventDefault();

                    
// Remove focus from the `.insert-media` button.
                    // Prevents Opera from showing the outline of the button
                    // above the modal.
                    //
                    // See: https://core.trac.wordpress.org/ticket/22445
                    
elem.blur();

                    if ( 
elem.hasClass'gallery' ) ) {
                        
options.state 'gallery';
                        
options.title wp.media.view.l10n.createGalleryTitle;
                    }

                    
wp.media.editor.openeditoroptions );
                });

            
// Initialize and render the Editor drag-and-drop uploader.
            
new wp.media.view.EditorUploader().render();
        }
    };

    
_.bindAllwp.media.editor'open' );
    $( 
wp.media.editor.init );
}(
jQuery_));
?>
Онлайн: 0
Реклама