<!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 = '<a href="' . esc_attr($url) . "\"$rel>$html</a>";
</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 < (int) $id ) ? 'attachment_' . $id : '';
</span><span class="cx">
</span><del>-        preg_match( '/width="([0-9]+)/', $html, $matches );
-        if ( ! isset($matches[1]) )
</del><ins>+        if ( ! preg_match( '/width="([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="' . $id . '" align="align' . $align
</span><del>-        . '" width="' . $width . '" caption="' . $alt . '"]' . $html . '[/caption]';
</del><ins>+        . '" width="' . $width . '" caption="' . addslashes($caption) . '"]' . $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']) && !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']) ) { ?>
</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->post_mime_type, 0, 5) == 'image' ) {
</span><ins>+                $alt = get_post_meta($post->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->ID);
</del><span class="cx">
</span><del>-                $form_fields['image_url']['value'] = $file;
</del><ins>+                $form_fields['image_alt'] = array(
+                        'value' => $alt,
+                        'label' => __('Alternate text'),
+                        'helps' => __('Alt text for the image, e.g. &#8220;The Mona Lisa&#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' => __('Alignment'),
</span><span class="cx">                         'input' => 'html',
</span><span class="cx">                         'html' => 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 =& get_post($attachment_id);
</span><span class="cx">         if ( substr($post->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' => array(
</span><span class="cx">                         'label' => __('Title'),
</span><del>-                        'value' => $edit_post->post_title,
</del><ins>+                        'value' => $edit_post->post_title
</ins><span class="cx">                 ),
</span><ins>+                'image_alt' => array(),
</ins><span class="cx">                 'post_excerpt' => array(
</span><span class="cx">                         'label' => __('Caption'),
</span><del>-                        'value' => $edit_post->post_excerpt,
</del><ins>+                        'value' => $edit_post->post_excerpt
</ins><span class="cx">                 ),
</span><span class="cx">                 'post_content' => array(
</span><span class="cx">                         'label' => __('Description'),
</span><span class="cx">                         'value' => $edit_post->post_content,
</span><del>-                        'input' => 'textarea',
</del><ins>+                        'input' => 'textarea'
</ins><span class="cx">                 ),
</span><span class="cx">                 'url' => array(
</span><span class="cx">                         'label' => __('Link URL'),
</span><span class="cx">                         'input' => 'html',
</span><span class="cx">                         'html' => image_link_input_fields($post, get_option('image_default_link_type')),
</span><del>-                        'helps' => __('Enter a link URL or click above for presets.'),
</del><ins>+                        'helps' => __('Enter a link URL or click above for presets.')
</ins><span class="cx">                 ),
</span><span class="cx">                 'menu_order' => array(
</span><span class="cx">                         'label' => __('Order'),
</span><span class="lines">@@ -1035,8 +1040,8 @@
</span><span class="cx">                         'label' => __('File URL'),
</span><span class="cx">                         'input' => 'html',
</span><span class="cx">                         'html' => "<input type='text' class='urlfield' readonly='readonly' name='attachments[$post->ID][url]' value='" . esc_attr($image_url) . "' /><br />",
</span><del>-                        'value' => isset($edit_post->post_url) ? $edit_post->post_url : '',
-                        'helps' => __('Location of the uploaded file.'),
</del><ins>+                        'value' => isset($edit_post->post_url) ? $edit_post->post_url : wp_get_attachment_url($post->ID),
+                        'helps' => __('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(/['"<>]+/g, '');
</del><ins>+                        title = f.title.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
</ins><span class="cx">                         title = ' title="'+title+'"';
</span><span class="cx">                 }
</span><span class="cx">
</span><del>-                if ( f.alt.value ) {
-                        alt = f.alt.value.replace(/['"<>]+/g, '');
</del><ins>+                if ( f.alt.value )
+                        alt = f.alt.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
+
</ins><span class="cx"> <?php if ( ! apply_filters( 'disable_captions', '' ) ) { ?>
</span><del>-                        caption = f.alt.value.replace(/'/g, '&#39;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
</del><ins>+                if ( f.caption.value )
+                        caption = f.caption.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
</ins><span class="cx"> <?php } ?>
</span><del>-                }
</del><span class="cx">
</span><span class="cx">                 cls = caption ? '' : ' class="'+t.align+'"';
</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. &#8220;The Mona Lisa&#8221;');
</del><ins>+        if ( !apply_filters( 'disable_captions', '' ) ) {
+                $caption = '
+                <tr>
+                        <th valign="top" scope="row" class="label">
+                                <span class="alignleft"><label for="caption">' . __('Image Caption') . '</label></span>
+                        </th>
+                        <td class="field"><input id="caption" name="caption" value="" type="text" /></td>
+                </tr>
+';
</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">                                 <span class="alignleft"><label for="title">' . __('Image Title') . '</label></span>
</span><span class="cx">                                 <span class="alignright"><abbr title="required" class="required">*</abbr></span>
</span><span class="cx">                         </th>
</span><del>-                        <td class="field"><p><input id="title" name="title" value="" type="text" aria-required="true" /></p></td>
</del><ins>+                        <td class="field"><input id="title" name="title" value="" type="text" aria-required="true" /></td>
</ins><span class="cx">                 </tr>
</span><span class="cx">
</span><span class="cx">                 <tr>
</span><span class="cx">                         <th valign="top" scope="row" class="label">
</span><del>-                                <span class="alignleft"><label for="alt">' . $alt . '</label></span>
</del><ins>+                                <span class="alignleft"><label for="alt">' . __('Alternate Text') . '</label></span>
</ins><span class="cx">                         </th>
</span><span class="cx">                         <td class="field"><input id="alt" name="alt" value="" type="text" aria-required="true" />
</span><del>-                        <p class="help">' . $alt_help . '</p></td>
</del><ins>+                        <p class="help">' . __('Alt text for the image, e.g. &#8220;The Mona Lisa&#8221;') . '</p></td>
</ins><span class="cx">                 </tr>
</span><del>-
</del><ins>+                ' . $caption . '
</ins><span class="cx">                 <tr class="align">
</span><span class="cx">                         <th valign="top" scope="row" class="label"><p><label for="align">' . __('Alignment') . '</label></p></th>
</span><span class="cx">                         <td class="field">
</span></span></pre>
</div>
</div>
</body>
</html>