Make WordPress Core

Changeset 22758


Ignore:
Timestamp:
11/21/2012 01:53:15 PM (14 years ago)
Author:
koopersmith
Message:

Media: Make the attachment object optional when using the wp.media.string methods. see #21390.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/js/media-upload.js

    r22715 r22758  
    9595                linkToUrl;
    9696
    97         linkToUrl = function( attachment, props ) {
    98                 var link = props.link,
     97        linkToUrl = function( props, attachment ) {
     98                var link = props.link || getUserSetting( 'urlbutton', 'post' ),
    9999                        url;
    100100
    101101                if ( 'file' === link )
    102                         url = attachment.get('url');
     102                        url = attachment.url;
    103103                else if ( 'post' === link )
    104                         url = attachment.get('link');
     104                        url = attachment.link;
    105105                else if ( 'custom' === link )
    106106                        url = props.linkUrl;
    … …  
    110110
    111111        wp.media.string = {
    112                 link: function( attachment, props ) {
    113                         var linkTo  = getUserSetting( 'urlbutton', 'post' ),
    114                                 options = {
    115                                         tag:     'a',
    116                                         content: attachment.get('title') || attachment.get('filename'),
    117                                         attrs:   {
    118                                                 rel: 'attachment wp-att-' + attachment.id
    119                                         }
    120                                 };
    121 
    122                         options.attrs.href = linkToUrl( attachment, props );
     112                link: function( props, attachment ) {
     113                        var options;
     114
     115                        props = _.defaults( props || {}, {
     116                                title:   '',
     117                                linkUrl: ''
     118                        });
     119
     120                        if ( attachment ) {
     121                                attachment = attachment.toJSON();
     122
     123                                _.extend( props, {
     124                                        title:   attachment.title || attachment.filename,
     125                                        linkUrl: linkToUrl( props, attachment ),
     126                                        rel:     'attachment wp-att-' + attachment.id
     127                                });
     128                        }
     129
     130                        options = {
     131                                tag:     'a',
     132                                content: props.title,
     133                                attrs:   {
     134                                        href: props.linkUrl
     135                                }
     136                        };
     137
     138                        if ( props.rel )
     139                                options.attrs.rel = props.rel;
    123140
    124141                        return wp.html.string( options );
    125142                },
    126143
    127                 image: function( attachment, props ) {
    128                         var classes, img, options, size, shortcode, html;
     144                image: function( props, attachment ) {
     145                        var classes = [],
     146                                img = {},
     147                                options, sizes, size, shortcode, html;
    129148
    130149                        props = _.defaults( props || {}, {
    131                                 img:   {},
    132150                                align: getUserSetting( 'align', 'none' ),
    133                                 size:  getUserSetting( 'imgsize', 'medium' ),
    134                                 link:  getUserSetting( 'urlbutton', 'post' )
     151                                size:  getUserSetting( 'imgsize', 'medium' )
    135152                        });
    136153
    137                         props.linkUrl = linkToUrl( attachment, props );
    138 
    139                         attachment = attachment.toJSON();
    140 
    141                         img     = _.clone( props.img );
    142                         classes = img['class'] ? img['class'].split(/\s+/) : [];
    143                         size    = attachment.sizes ? attachment.sizes[ props.size ] : {};
    144 
    145                         if ( ! size ) {
    146                                 delete props.size;
    147                                 size = attachment;
     154                        if ( attachment ) {
     155                                attachment = attachment.toJSON();
     156
     157                                classes.push( 'wp-image-' + attachment.id );
     158
     159                                sizes = attachment.sizes;
     160                                size = sizes && sizes[ props.size ] ? sizes[ props.size ] : attachment;
     161
     162                                _.extend( props, _.pick( attachment, 'align', 'caption' ), {
     163                                        width:     size.width,
     164                                        height:    size.height,
     165                                        src:       size.url,
     166                                        linkUrl:   linkToUrl( props, attachment ),
     167                                        captionId: 'attachment_' + attachment.id
     168                                });
    148169                        }
    149170
    … …  
    154175                        // Only assign the align class to the image if we're not printing
    155176                        // a caption, since the alignment is sent to the shortcode.
    156                         if ( props.align && ! attachment.caption )
     177                        if ( props.align && ! props.caption )
    157178                                classes.push( 'align' + props.align );
    158179
    159180                        if ( props.size )
    160181                                classes.push( 'size-' + props.size );
    161 
    162                         classes.push( 'wp-image-' + attachment.id );
    163182
    164183                        img['class'] = _.compact( classes ).join(' ');
    … …  
    171190                        };
    172191
    173                         // Generate the `href` based on the `link` property.
     192                        // Generate the `a` element options, if they exist.
    174193                        if ( props.linkUrl ) {
    175                                 props.anchor = props.anchor || {};
    176                                 props.anchor.href = props.linkUrl;
    177                         }
    178 
    179                         // Generate the `a` element options, if they exist.
    180                         if ( props.anchor ) {
    181194                                options = {
    182                                         tag:     'a',
    183                                         attrs:   props.anchor,
     195                                        tag:   'a',
     196                                        attrs: {
     197                                                href: props.linkUrl
     198                                        },
    184199                                        content: options
    185200                                };
    … …  
    189204
    190205                        // Generate the caption shortcode.
    191                         if ( attachment.caption ) {
     206                        if ( props.caption ) {
    192207                                shortcode = {
    193                                         id:    'attachment_' + attachment.id,
    194208                                        width: img.width
    195209                                };
     210
     211                                if ( props.captionId )
     212                                        shortcode.id = props.captionId;
    196213
    197214                                if ( props.align )
    … …  
    201218                                        tag:     'caption',
    202219                                        attrs:   shortcode,
    203                                         content: html + ' ' + attachment.caption
     220                                        content: html + ' ' + props.caption
    204221                                });
    205222                        }
    … …  
    381398
    382399                                        if ( 'image' === attachment.get('type') )
    383                                                 this.insert( wp.media.string.image( attachment, detail ) + ' ' );
     400                                                this.insert( wp.media.string.image( detail, attachment ) + ' ' );
    384401                                        else
    385                                                 this.insert( wp.media.string.link( attachment, detail ) + ' ' );
     402                                                this.insert( wp.media.string.link( detail, attachment ) + ' ' );
    386403                                }, this );
    387404                        }, this );
Note: See TracChangeset for help on using the changeset viewer.