<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[22768] trunk: Media: Backwards compatibility for the many send_to_editor filters.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/22768">22768</a></dd>
<dt>Author</dt> <dd>koopersmith</dd>
<dt>Date</dt> <dd>2012-11-21 16:02:20 +0000 (Wed, 21 Nov 2012)</dd>
</dl>

<h3>Log Message</h3>
<pre>Media: Backwards compatibility for the many send_to_editor filters. props nacin. see <a href="http://core.trac.wordpress.org/ticket/22186">#22186</a>, <a href="http://core.trac.wordpress.org/ticket/21390">#21390</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminadminajaxphp">trunk/wp-admin/admin-ajax.php</a></li>
<li><a href="#trunkwpadminincludesajaxactionsphp">trunk/wp-admin/includes/ajax-actions.php</a></li>
<li><a href="#trunkwpadminjsmediauploadjs">trunk/wp-admin/js/media-upload.js</a></li>
<li><a href="#trunkwpincludesmediaphp">trunk/wp-includes/media.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadminadminajaxphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/admin-ajax.php (22767 => 22768)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/admin-ajax.php        2012-11-21 15:37:43 UTC (rev 22767)
+++ trunk/wp-admin/admin-ajax.php        2012-11-21 16:02:20 UTC (rev 22768)
</span><span class="lines">@@ -54,7 +54,8 @@
</span><span class="cx">         'sample-permalink', 'inline-save', 'inline-save-tax', 'find_posts', 'widgets-order',
</span><span class="cx">         'save-widget', 'set-post-thumbnail', 'date_format', 'time_format', 'wp-fullscreen-save-post',
</span><span class="cx">         'wp-remove-post-lock', 'dismiss-wp-pointer', 'upload-attachment', 'get-attachment',
</span><del>-        'query-attachments', 'save-attachment', 'save-attachment-compat',
</del><ins>+        'query-attachments', 'save-attachment', 'save-attachment-compat', 'send-link-to-editor',
+        'send-attachment-to-editor',
</ins><span class="cx"> );
</span><span class="cx"> 
</span><span class="cx"> // Register core Ajax calls.
</span></span></pre></div>
<a id="trunkwpadminincludesajaxactionsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/ajax-actions.php (22767 => 22768)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/ajax-actions.php        2012-11-21 15:37:43 UTC (rev 22767)
+++ trunk/wp-admin/includes/ajax-actions.php        2012-11-21 16:02:20 UTC (rev 22768)
</span><span class="lines">@@ -1907,3 +1907,91 @@
</span><span class="cx"> 
</span><span class="cx">         wp_send_json_success( $attachment );
</span><span class="cx"> }
</span><ins>+
+/**
+ * Generates the HTML to send an attachment to the editor.
+ * Backwards compatible with the media_send_to_editor filter and the chain
+ * of filters that follow.
+ *
+ * @since 3.5.0
+ */
+function wp_ajax_send_attachment_to_editor() {
+        check_ajax_referer( 'media-send-to-editor', 'nonce' );
+
+        $attachment = stripslashes_deep( $_POST['attachment'] );
+
+        $id = intval( $attachment['id'] );
+
+        if ( ! $post = get_post( $id ) )
+                wp_send_json_error();
+
+        if ( ! current_user_can( 'edit_post', $id ) )
+                wp_send_json_error();
+
+        if ( 'attachment' != $post-&gt;post_type )
+                wp_send_json_error();
+
+        $html = isset( $attachment['title'] ) ? $attachment['title'] : '';
+        if ( ! empty( $attachment['url'] ) ) {
+                $rel = '';
+                if ( strpos($attachment['url'], 'attachment_id') || get_attachment_link( $id ) == $attachment['url'] )
+                        $rel = ' rel=&quot;attachment wp-att-' . $id . '&quot;';
+                $html = '&lt;a href=&quot;' . esc_url( $attachment['url'] ) . '&quot;' . $rel . '&gt;' . $html . '&lt;/a&gt;';
+        }
+
+        remove_filter( 'media_send_to_editor', 'image_media_send_to_editor', 10, 3 );
+
+        if ( 'image' === substr( $post-&gt;post_mime_type, 0, 5 ) ) {
+                $url = $attachment['url'];
+                $align = isset( $attachment['image-align'] ) ? $attachment['image-align'] : 'none';
+                $size = isset( $attachment['image-size'] ) ? $attachment['image-size'] : 'medium';
+                $alt = isset( $attachment['image-alt'] ) ? $attachment['image-alt'] : '';
+                $caption = isset( $attachment['caption'] ) ? $attachment['caption'] : '';
+                $title = ''; // We no longer insert title tags into &lt;img&gt; tags, as they are redundant.
+                $html = get_image_send_to_editor( $id, $caption, $title, $align, $url, (bool) $rel, $size, $alt );
+        }
+
+        $html = apply_filters( 'media_send_to_editor', $html, $id, $attachment );
+
+        wp_send_json_success( $html );
+}
+
+/**
+ * Generates the HTML to send a non-image embed link to the editor.
+ *
+ * Backwards compatible with the following filters:
+ * - file_send_to_editor_url
+ * - audio_send_to_editor_url
+ * - video_send_to_editor_url
+ *
+ * @since 3.5.0
+ */
+function wp_ajax_send_link_to_editor() {
+        check_ajax_referer( 'media-send-to-editor', 'nonce' );
+
+        if ( ! $src = stripslashes( $_POST['src'] ) )
+                wp_send_json_error();
+
+        if ( ! strpos( $src, '://' ) )
+                $src = 'http://' . $src;
+
+        if ( ! $src = esc_url_raw( $src ) )
+                wp_send_json_error();
+
+        if ( ! $title = trim( stripslashes( $_POST['title'] ) ) )
+                $title = wp_basename( $src );
+
+        $html = '';
+        if ( $title )
+                $html = '&lt;a href=&quot;' . esc_url( $src ) . '&quot;&gt;' . $title . '&lt;/a&gt;';
+
+        // Figure out what filter to run:
+        $type = 'file';
+        if ( ( $ext = preg_replace( '/^.+?\.([^.]+)$/', '$1', $src ) ) &amp;&amp; ( $ext_type = wp_ext2type( $ext ) )
+                &amp;&amp; ( 'audio' == $ext_type || 'video' == $ext_type ) )
+                        $type = $ext_type;
+
+        $html = apply_filters( $type . '_send_to_editor_url', $html, $src, $title );
+
+        wp_send_json_success( $html );
+}
</ins></span></pre></div>
<a id="trunkwpadminjsmediauploadjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/js/media-upload.js (22767 => 22768)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/js/media-upload.js        2012-11-21 15:37:43 UTC (rev 22767)
+++ trunk/wp-admin/js/media-upload.js        2012-11-21 16:02:20 UTC (rev 22768)
</span><span class="lines">@@ -109,22 +109,69 @@
</span><span class="cx">         };
</span><span class="cx"> 
</span><span class="cx">         wp.media.string = {
</span><del>-                link: function( props, attachment ) {
-                        var options;
</del><ins>+                // Joins the `props` and `attachment` objects,
+                // outputting the proper object format based on the
+                // attachment's type.
+                props: function( props, attachment ) {
+                        var link, linkUrl, size, sizes;
</ins><span class="cx"> 
</span><del>-                        props = _.defaults( props || {}, {
-                                title:   '',
-                                linkUrl: ''
-                        });
</del><ins>+                        props = props ? _.clone( props ) : {};
</ins><span class="cx"> 
</span><del>-                        if ( attachment ) {
</del><ins>+                        if ( attachment &amp;&amp; attachment.type )
+                                props.type = attachment.type;
+
+                        if ( 'image' === props.type ) {
+                                props = _.defaults( props || {}, {
+                                        align:   getUserSetting( 'align', 'none' ),
+                                        size:    getUserSetting( 'imgsize', 'medium' ),
+                                        url:     '',
+                                        classes: []
+                                });
+                        }
+
+                        // All attachment-specific settings follow.
+                        if ( ! attachment )
+                                return props;
+
+                        link = props.link || getUserSetting( 'urlbutton', 'post' );
+                        if ( 'file' === 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 &amp;&amp; sizes[ props.size ] ? sizes[ props.size ] : attachment;
+
+                                _.extend( props, _.pick( attachment, 'align', 'caption' ), {
+                                        width:     size.width,
+                                        height:    size.height,
+                                        src:       size.url,
+                                        captionId: 'attachment_' + attachment.id
+                                });
+
+                        // Format properties for non-images.
+                        } else {
</ins><span class="cx">                                 _.extend( props, {
</span><span class="cx">                                         title:   attachment.title || attachment.filename,
</span><del>-                                        linkUrl: linkToUrl( props, attachment ),
</del><span class="cx">                                         rel:     'attachment wp-att-' + attachment.id
</span><span class="cx">                                 });
</span><span class="cx">                         }
</span><span class="cx"> 
</span><ins>+                        return props;
+                },
+
+                link: function( props, attachment ) {
+                        var options;
+
+                        props = wp.media.string.props( props, attachment );
+
</ins><span class="cx">                         options = {
</span><span class="cx">                                 tag:     'a',
</span><span class="cx">                                 content: props.title,
</span><span class="lines">@@ -140,31 +187,12 @@
</span><span class="cx">                 },
</span><span class="cx"> 
</span><span class="cx">                 image: function( props, attachment ) {
</span><del>-                        var classes = [],
-                                img = {},
-                                options, sizes, size, shortcode, html;
</del><ins>+                        var img = {},
+                                options, classes, shortcode, html;
</ins><span class="cx"> 
</span><del>-                        props = _.defaults( props || {}, {
-                                align: getUserSetting( 'align', 'none' ),
-                                size:  getUserSetting( 'imgsize', 'medium' ),
-                                url:   ''
-                        });
</del><ins>+                        props = wp.media.string.props( props, attachment );
+                        classes = props.classes || [];
</ins><span class="cx"> 
</span><del>-                        if ( attachment ) {
-                                classes.push( 'wp-image-' + attachment.id );
-
-                                sizes = attachment.sizes;
-                                size = sizes &amp;&amp; sizes[ props.size ] ? sizes[ props.size ] : attachment;
-
-                                _.extend( props, _.pick( attachment, 'align', 'caption' ), {
-                                        width:     size.width,
-                                        height:    size.height,
-                                        src:       size.url,
-                                        linkUrl:   linkToUrl( props, attachment ),
-                                        captionId: 'attachment_' + attachment.id
-                                });
-                        }
-
</del><span class="cx">                         img.src = props.url;
</span><span class="cx">                         _.extend( img, _.pick( props, 'width', 'height', 'alt' ) );
</span><span class="cx"> 
</span><span class="lines">@@ -395,14 +423,7 @@
</span><span class="cx"> 
</span><span class="cx">                                         attachment = attachment.toJSON();
</span><span class="cx"> 
</span><del>-                                        // If captions are disabled, clear the caption.
-                                        if ( ! wp.media.view.settings.captions )
-                                                delete attachment.caption;
-
-                                        if ( 'image' === attachment.type )
-                                                this.insert( wp.media.string.image( detail, attachment ) + ' ' );
-                                        else
-                                                this.insert( wp.media.string.link( detail, attachment ) + ' ' );
</del><ins>+                                        this.send.attachment( detail, attachment );
</ins><span class="cx">                                 }, this );
</span><span class="cx">                         }, this );
</span><span class="cx"> 
</span><span class="lines">@@ -421,7 +442,7 @@
</span><span class="cx">                                                 linkUrl: embed.url
</span><span class="cx">                                         });
</span><span class="cx"> 
</span><del>-                                        this.insert( wp.media.string.link( embed ) );
</del><ins>+                                        this.send.link( embed );
</ins><span class="cx"> 
</span><span class="cx">                                 } else if ( 'image' === embed.type ) {
</span><span class="cx">                                         _.defaults( embed, {
</span><span class="lines">@@ -451,6 +472,61 @@
</span><span class="cx">                         delete workflows[ id ];
</span><span class="cx">                 },
</span><span class="cx"> 
</span><ins>+                send: {
+                        attachment: function( props, attachment ) {
+                                var caption = attachment.caption,
+                                        options, html;
+
+                                // If captions are disabled, clear the caption.
+                                if ( ! wp.media.view.settings.captions )
+                                        delete attachment.caption;
+
+                                props = wp.media.string.props( props, attachment );
+
+                                options = {
+                                        id: attachment.id
+                                };
+
+                                if ( 'image' === attachment.type ) {
+                                        html = wp.media.string.image( props );
+                                        options['caption'] = caption;
+
+                                        _.each({
+                                                align:   'image-align',
+                                                size:    'image-size',
+                                                alt:     'image-alt',
+                                                linkUrl: 'url'
+                                        }, function( option, prop ) {
+                                                if ( props[ prop ] )
+                                                        options[ option ] = props[ prop ];
+                                        });
+
+                                } else {
+                                        html = wp.media.string.link( props );
+                                        options.title = props.title;
+                                }
+
+                                return media.post( 'send-attachment-to-editor', {
+                                        nonce:      wp.media.view.settings.nonce.sendToEditor,
+                                        attachment: options,
+                                        html:       html
+                                }).done( function( resp ) {
+                                        wp.media.editor.insert( resp );
+                                });
+                        },
+
+                        link: function( embed ) {
+                                return media.post( 'send-link-to-editor', {
+                                        nonce: wp.media.view.settings.nonce.sendToEditor,
+                                        src:   embed.linkUrl,
+                                        title: embed.title,
+                                        html:  wp.media.string.link( embed )
+                                }).done( function( resp ) {
+                                        wp.media.editor.insert( resp );
+                                });
+                        }
+                },
+
</ins><span class="cx">                 init: function() {
</span><span class="cx">                         $(document.body).on('click', '.insert-media', function( event ) {
</span><span class="cx">                                 var $this = $(this),
</span></span></pre></div>
<a id="trunkwpincludesmediaphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/media.php (22767 => 22768)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/media.php        2012-11-21 15:37:43 UTC (rev 22767)
+++ trunk/wp-includes/media.php        2012-11-21 16:02:20 UTC (rev 22768)
</span><span class="lines">@@ -1328,6 +1328,9 @@
</span><span class="cx">                 'tabUrl'    =&gt; add_query_arg( array( 'chromeless' =&gt; true ), admin_url('media-upload.php') ),
</span><span class="cx">                 'mimeTypes' =&gt; wp_list_pluck( get_post_mime_types(), 0 ),
</span><span class="cx">                 'captions'  =&gt; ! apply_filters( 'disable_captions', '' ),
</span><ins>+                'nonce'     =&gt; array(
+                        'sendToEditor' =&gt; wp_create_nonce( 'media-send-to-editor' ),
+                ),
</ins><span class="cx">         );
</span><span class="cx"> 
</span><span class="cx">         $post = null;
</span></span></pre>
</div>
</div>

</body>
</html>