<!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" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { 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, #msg p { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul { overflow: auto; }
#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>
<title>[12055] trunk/wp-admin/includes/media.php:
  Separate fields for image alt and image caption in the uploader.</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/12055">12055</a></dd>
<dt>Author</dt> <dd>azaozz</dd>
<dt>Date</dt> <dd>2009-10-19 05:09:41 +0000 (Mon, 19 Oct 2009)</dd>
</dl>

<h3>Log Message</h3>
<pre>Separate fields for image alt and image caption in the uploader.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminincludesmediaphp">trunk/wp-admin/includes/media.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadminincludesmediaphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/media.php (12054 => 12055)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/media.php        2009-10-18 13:58:44 UTC (rev 12054)
+++ trunk/wp-admin/includes/media.php        2009-10-19 05:09:41 UTC (rev 12055)
</span><span class="lines">@@ -98,7 +98,7 @@
</span><span class="cx">  * @param unknown_type $size
</span><span class="cx">  * @return unknown
</span><span class="cx">  */
</span><del>-function get_image_send_to_editor($id, $alt, $title, $align, $url='', $rel = false, $size='medium') {
</del><ins>+function get_image_send_to_editor($id, $caption, $title, $align, $url='', $rel = false, $size='medium', $alt = '') {
</ins><span class="cx"> 
</span><span class="cx">         $htmlalt = ( empty($alt) ) ? $title : $alt;
</span><span class="cx"> 
</span><span class="lines">@@ -109,7 +109,7 @@
</span><span class="cx">         if ( $url )
</span><span class="cx">                 $html = '&lt;a href=&quot;' . esc_attr($url) . &quot;\&quot;$rel&gt;$html&lt;/a&gt;&quot;;
</span><span class="cx"> 
</span><del>-        $html = apply_filters( 'image_send_to_editor', $html, $id, $alt, $title, $align, $url, $size );
</del><ins>+        $html = apply_filters( 'image_send_to_editor', $html, $id, $caption, $title, $align, $url, $size, $alt );
</ins><span class="cx"> 
</span><span class="cx">         return $html;
</span><span class="cx"> }
</span><span class="lines">@@ -148,15 +148,14 @@
</span><span class="cx">  * @param unknown_type $size
</span><span class="cx">  * @return unknown
</span><span class="cx">  */
</span><del>-function image_add_caption( $html, $id, $alt, $title, $align, $url, $size ) {
</del><ins>+function image_add_caption( $html, $id, $caption, $title, $align, $url, $size, $alt = '' ) {
</ins><span class="cx"> 
</span><del>-        if ( empty($alt) || apply_filters( 'disable_captions', '' ) )
</del><ins>+        if ( empty($caption) || apply_filters( 'disable_captions', '' ) )
</ins><span class="cx">                 return $html;
</span><span class="cx"> 
</span><span class="cx">         $id = ( 0 &lt; (int) $id ) ? 'attachment_' . $id : '';
</span><span class="cx"> 
</span><del>-        preg_match( '/width=&quot;([0-9]+)/', $html, $matches );
-        if ( ! isset($matches[1]) )
</del><ins>+        if ( ! preg_match( '/width=&quot;([0-9]+)/', $html, $matches ) )
</ins><span class="cx">                 return $html;
</span><span class="cx"> 
</span><span class="cx">         $width = $matches[1];
</span><span class="lines">@@ -165,14 +164,12 @@
</span><span class="cx">         if ( empty($align) )
</span><span class="cx">                 $align = 'none';
</span><span class="cx"> 
</span><del>-        $alt = ! empty($alt) ? addslashes($alt) : '';
-
</del><span class="cx">         $shcode = '[caption id=&quot;' . $id . '&quot; align=&quot;align' . $align
</span><del>-        . '&quot; width=&quot;' . $width . '&quot; caption=&quot;' . $alt . '&quot;]' . $html . '[/caption]';
</del><ins>+        . '&quot; width=&quot;' . $width . '&quot; caption=&quot;' . addslashes($caption) . '&quot;]' . $html . '[/caption]';
</ins><span class="cx"> 
</span><span class="cx">         return apply_filters( 'image_add_caption_shortcode', $shcode, $html );
</span><span class="cx"> }
</span><del>-add_filter( 'image_send_to_editor', 'image_add_caption', 20, 7 );
</del><ins>+add_filter( 'image_send_to_editor', 'image_add_caption', 20, 8 );
</ins><span class="cx"> 
</span><span class="cx"> /**
</span><span class="cx">  * {@internal Missing Short Description}}
</span><span class="lines">@@ -431,6 +428,15 @@
</span><span class="cx"> 
</span><span class="cx">                 $post = apply_filters('attachment_fields_to_save', $post, $attachment);
</span><span class="cx"> 
</span><ins>+                if ( isset($attachment['image_alt']) &amp;&amp; !empty($attachment['image_alt']) ) {
+                        $image_alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
+                        if ( $image_alt != stripslashes($attachment['image_alt']) ) {
+                                $image_alt = wp_strip_all_tags( stripslashes($attachment['image_alt']), true );
+                                // update_meta expects slashed
+                                update_post_meta( $attachment_id, '_wp_attachment_image_alt', addslashes($image_alt) );
+                        }
+                }
+
</ins><span class="cx">                 if ( isset($post['errors']) ) {
</span><span class="cx">                         $errors[$attachment_id] = $post['errors'];
</span><span class="cx">                         unset($post['errors']);
</span><span class="lines">@@ -439,9 +445,10 @@
</span><span class="cx">                 if ( $post != $_post )
</span><span class="cx">                         wp_update_post($post);
</span><span class="cx"> 
</span><del>-                foreach ( get_attachment_taxonomies($post) as $t )
</del><ins>+                foreach ( get_attachment_taxonomies($post) as $t ) {
</ins><span class="cx">                         if ( isset($attachment[$t]) )
</span><span class="cx">                                 wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $attachment[$t])), $t, false);
</span><ins>+                }
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         if ( isset($_POST['insert-gallery']) || isset($_POST['update-gallery']) ) { ?&gt;
</span><span class="lines">@@ -889,23 +896,28 @@
</span><span class="cx">  */
</span><span class="cx"> function image_attachment_fields_to_edit($form_fields, $post) {
</span><span class="cx">         if ( substr($post-&gt;post_mime_type, 0, 5) == 'image' ) {
</span><ins>+                $alt = get_post_meta($post-&gt;ID, '_wp_attachment_image_alt', true);
+                if ( empty($alt) )
+                        $alt = '';
+
</ins><span class="cx">                 $form_fields['post_title']['required'] = true;
</span><del>-                $file = wp_get_attachment_url($post-&gt;ID);
</del><span class="cx"> 
</span><del>-                $form_fields['image_url']['value'] = $file;
</del><ins>+                $form_fields['image_alt'] = array(
+                        'value' =&gt; $alt,
+                        'label' =&gt; __('Alternate text'),
+                        'helps' =&gt; __('Alt text for the image, e.g. &amp;#8220;The Mona Lisa&amp;#8221;')
+                );
</ins><span class="cx"> 
</span><del>-                $form_fields['post_excerpt']['label'] = __('Caption');
-                $form_fields['post_excerpt']['helps'][] = __('Also used as alternate text for the image');
-
-                $form_fields['post_content']['label'] = __('Description');
-
</del><span class="cx">                 $form_fields['align'] = array(
</span><span class="cx">                         'label' =&gt; __('Alignment'),
</span><span class="cx">                         'input' =&gt; 'html',
</span><span class="cx">                         'html'  =&gt; image_align_input_fields($post, get_option('image_default_align')),
</span><span class="cx">                 );
</span><span class="cx"> 
</span><del>-                $form_fields['image-size'] = image_size_input_fields($post, get_option('image_default_size'));
</del><ins>+                $form_fields['image-size'] = image_size_input_fields( $post, get_option('image_default_size') );
+
+        } else {
+                unset( $form_fields['image_alt'] );
</ins><span class="cx">         }
</span><span class="cx">         return $form_fields;
</span><span class="cx"> }
</span><span class="lines">@@ -967,20 +979,12 @@
</span><span class="cx">         $post =&amp; get_post($attachment_id);
</span><span class="cx">         if ( substr($post-&gt;post_mime_type, 0, 5) == 'image' ) {
</span><span class="cx">                 $url = $attachment['url'];
</span><del>-
-                if ( isset($attachment['align']) )
-                        $align = $attachment['align'];
-                else
-                        $align = 'none';
-
-                if ( !empty($attachment['image-size']) )
-                        $size = $attachment['image-size'];
-                else
-                        $size = 'medium';
-
</del><ins>+                $align = !empty($attachment['align']) ? $attachment['align'] : 'none';
+                $size = !empty($attachment['image-size']) ? $attachment['image-size'] : 'medium';
+                $alt = !empty($attachment['image_alt']) ? $attachment['image_alt'] : '';
</ins><span class="cx">                 $rel = ( $url == get_attachment_link($attachment_id) );
</span><span class="cx"> 
</span><del>-                return get_image_send_to_editor($attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel, $size);
</del><ins>+                return get_image_send_to_editor($attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel, $size, $alt);
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         return $html;
</span><span class="lines">@@ -1010,22 +1014,23 @@
</span><span class="cx">         $form_fields = array(
</span><span class="cx">                 'post_title'   =&gt; array(
</span><span class="cx">                         'label'      =&gt; __('Title'),
</span><del>-                        'value'      =&gt; $edit_post-&gt;post_title,
</del><ins>+                        'value'      =&gt; $edit_post-&gt;post_title
</ins><span class="cx">                 ),
</span><ins>+                'image_alt'   =&gt; array(),
</ins><span class="cx">                 'post_excerpt' =&gt; array(
</span><span class="cx">                         'label'      =&gt; __('Caption'),
</span><del>-                        'value'      =&gt; $edit_post-&gt;post_excerpt,
</del><ins>+                        'value'      =&gt; $edit_post-&gt;post_excerpt
</ins><span class="cx">                 ),
</span><span class="cx">                 'post_content' =&gt; array(
</span><span class="cx">                         'label'      =&gt; __('Description'),
</span><span class="cx">                         'value'      =&gt; $edit_post-&gt;post_content,
</span><del>-                        'input'      =&gt; 'textarea',
</del><ins>+                        'input'      =&gt; 'textarea'
</ins><span class="cx">                 ),
</span><span class="cx">                 'url'          =&gt; array(
</span><span class="cx">                         'label'      =&gt; __('Link URL'),
</span><span class="cx">                         'input'      =&gt; 'html',
</span><span class="cx">                         'html'       =&gt; image_link_input_fields($post, get_option('image_default_link_type')),
</span><del>-                        'helps'      =&gt; __('Enter a link URL or click above for presets.'),
</del><ins>+                        'helps'      =&gt; __('Enter a link URL or click above for presets.')
</ins><span class="cx">                 ),
</span><span class="cx">                 'menu_order'   =&gt; array(
</span><span class="cx">                         'label'      =&gt; __('Order'),
</span><span class="lines">@@ -1035,8 +1040,8 @@
</span><span class="cx">                         'label'      =&gt; __('File URL'),
</span><span class="cx">                         'input'      =&gt; 'html',
</span><span class="cx">                         'html'       =&gt; &quot;&lt;input type='text' class='urlfield' readonly='readonly' name='attachments[$post-&gt;ID][url]' value='&quot; . esc_attr($image_url) . &quot;' /&gt;&lt;br /&gt;&quot;,
</span><del>-                        'value'      =&gt; isset($edit_post-&gt;post_url) ? $edit_post-&gt;post_url : '',
-                        'helps'      =&gt; __('Location of the uploaded file.'),
</del><ins>+                        'value'      =&gt; isset($edit_post-&gt;post_url) ? $edit_post-&gt;post_url : wp_get_attachment_url($post-&gt;ID),
+                        'helps'      =&gt; __('Location of the uploaded file.')
</ins><span class="cx">                 )
</span><span class="cx">         );
</span><span class="cx"> 
</span><span class="lines">@@ -1525,21 +1530,22 @@
</span><span class="cx">         align : 'alignnone',
</span><span class="cx"> 
</span><span class="cx">         insert : function() {
</span><del>-                var t = this, html, f = document.forms[0], cls, title = '', alt = '', caption = null;
</del><ins>+                var t = this, html, f = document.forms[0], cls, title = '', alt = '', caption = '';
</ins><span class="cx"> 
</span><span class="cx">                 if ( '' == f.src.value || '' == t.width ) return false;
</span><span class="cx"> 
</span><span class="cx">                 if ( f.title.value ) {
</span><del>-                        title = f.title.value.replace(/['&quot;&lt;&gt;]+/g, '');
</del><ins>+                        title = f.title.value.replace(/'/g, '&amp;#039;').replace(/&quot;/g, '&amp;quot;').replace(/&lt;/g, '&amp;lt;').replace(/&gt;/g, '&amp;gt;');
</ins><span class="cx">                         title = ' title=&quot;'+title+'&quot;';
</span><span class="cx">                 }
</span><span class="cx"> 
</span><del>-                if ( f.alt.value ) {
-                        alt = f.alt.value.replace(/['&quot;&lt;&gt;]+/g, '');
</del><ins>+                if ( f.alt.value )
+                        alt = f.alt.value.replace(/'/g, '&amp;#039;').replace(/&quot;/g, '&amp;quot;').replace(/&lt;/g, '&amp;lt;').replace(/&gt;/g, '&amp;gt;');
+
</ins><span class="cx"> &lt;?php if ( ! apply_filters( 'disable_captions', '' ) ) { ?&gt;
</span><del>-                        caption = f.alt.value.replace(/'/g, '&amp;#39;').replace(/&quot;/g, '&amp;quot;').replace(/&lt;/g, '&amp;lt;').replace(/&gt;/g, '&amp;gt;');
</del><ins>+                if ( f.caption.value )
+                        caption = f.caption.value.replace(/'/g, '&amp;#039;').replace(/&quot;/g, '&amp;quot;').replace(/&lt;/g, '&amp;lt;').replace(/&gt;/g, '&amp;gt;');
</ins><span class="cx"> &lt;?php } ?&gt;
</span><del>-                }
</del><span class="cx"> 
</span><span class="cx">                 cls = caption ? '' : ' class=&quot;'+t.align+'&quot;';
</span><span class="cx"> 
</span><span class="lines">@@ -1915,12 +1921,17 @@
</span><span class="cx">  */
</span><span class="cx"> function type_url_form_image() {
</span><span class="cx"> 
</span><del>-        if ( apply_filters( 'disable_captions', '' ) ) {
-                $alt = __('Alternate Text');
-                $alt_help = __('Alt text for the image, e.g. &amp;#8220;The Mona Lisa&amp;#8221;');
</del><ins>+        if ( !apply_filters( 'disable_captions', '' ) ) {
+                $caption = '
+                &lt;tr&gt;
+                        &lt;th valign=&quot;top&quot; scope=&quot;row&quot; class=&quot;label&quot;&gt;
+                                &lt;span class=&quot;alignleft&quot;&gt;&lt;label for=&quot;caption&quot;&gt;' . __('Image Caption') . '&lt;/label&gt;&lt;/span&gt;
+                        &lt;/th&gt;
+                        &lt;td class=&quot;field&quot;&gt;&lt;input id=&quot;caption&quot; name=&quot;caption&quot; value=&quot;&quot; type=&quot;text&quot; /&gt;&lt;/td&gt;
+                &lt;/tr&gt;
+';
</ins><span class="cx">         } else {
</span><del>-                $alt = __('Image Caption');
-                $alt_help = __('Also used as alternate text for the image');
</del><ins>+                $caption = '';
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         $default_align = get_option('image_default_align');
</span><span class="lines">@@ -1942,17 +1953,17 @@
</span><span class="cx">                                 &lt;span class=&quot;alignleft&quot;&gt;&lt;label for=&quot;title&quot;&gt;' . __('Image Title') . '&lt;/label&gt;&lt;/span&gt;
</span><span class="cx">                                 &lt;span class=&quot;alignright&quot;&gt;&lt;abbr title=&quot;required&quot; class=&quot;required&quot;&gt;*&lt;/abbr&gt;&lt;/span&gt;
</span><span class="cx">                         &lt;/th&gt;
</span><del>-                        &lt;td class=&quot;field&quot;&gt;&lt;p&gt;&lt;input id=&quot;title&quot; name=&quot;title&quot; value=&quot;&quot; type=&quot;text&quot; aria-required=&quot;true&quot; /&gt;&lt;/p&gt;&lt;/td&gt;
</del><ins>+                        &lt;td class=&quot;field&quot;&gt;&lt;input id=&quot;title&quot; name=&quot;title&quot; value=&quot;&quot; type=&quot;text&quot; aria-required=&quot;true&quot; /&gt;&lt;/td&gt;
</ins><span class="cx">                 &lt;/tr&gt;
</span><span class="cx"> 
</span><span class="cx">                 &lt;tr&gt;
</span><span class="cx">                         &lt;th valign=&quot;top&quot; scope=&quot;row&quot; class=&quot;label&quot;&gt;
</span><del>-                                &lt;span class=&quot;alignleft&quot;&gt;&lt;label for=&quot;alt&quot;&gt;' . $alt . '&lt;/label&gt;&lt;/span&gt;
</del><ins>+                                &lt;span class=&quot;alignleft&quot;&gt;&lt;label for=&quot;alt&quot;&gt;' . __('Alternate Text') . '&lt;/label&gt;&lt;/span&gt;
</ins><span class="cx">                         &lt;/th&gt;
</span><span class="cx">                         &lt;td class=&quot;field&quot;&gt;&lt;input id=&quot;alt&quot; name=&quot;alt&quot; value=&quot;&quot; type=&quot;text&quot; aria-required=&quot;true&quot; /&gt;
</span><del>-                        &lt;p class=&quot;help&quot;&gt;' . $alt_help . '&lt;/p&gt;&lt;/td&gt;
</del><ins>+                        &lt;p class=&quot;help&quot;&gt;' . __('Alt text for the image, e.g. &amp;#8220;The Mona Lisa&amp;#8221;') . '&lt;/p&gt;&lt;/td&gt;
</ins><span class="cx">                 &lt;/tr&gt;
</span><del>-
</del><ins>+                ' . $caption . '
</ins><span class="cx">                 &lt;tr class=&quot;align&quot;&gt;
</span><span class="cx">                         &lt;th valign=&quot;top&quot; scope=&quot;row&quot; class=&quot;label&quot;&gt;&lt;p&gt;&lt;label for=&quot;align&quot;&gt;' . __('Alignment') . '&lt;/label&gt;&lt;/p&gt;&lt;/th&gt;
</span><span class="cx">                         &lt;td class=&quot;field&quot;&gt;
</span></span></pre>
</div>
</div>

</body>
</html>