Make WordPress Core

Changeset 12055


Ignore:
Timestamp:
10/19/2009 05:09:41 AM (17 years ago)
Author:
azaozz
Message:

Separate fields for image alt and image caption in the uploader.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/media.php

    r12051 r12055  
    9999 * @return unknown
    100100 */
    101 function get_image_send_to_editor($id, $alt, $title, $align, $url='', $rel = false, $size='medium') {
     101function get_image_send_to_editor($id, $caption, $title, $align, $url='', $rel = false, $size='medium', $alt = '') {
    102102
    103103        $htmlalt = ( empty($alt) ) ? $title : $alt;
     
    110110                $html = '<a href="' . esc_attr($url) . "\"$rel>$html</a>";
    111111
    112         $html = apply_filters( 'image_send_to_editor', $html, $id, $alt, $title, $align, $url, $size );
     112        $html = apply_filters( 'image_send_to_editor', $html, $id, $caption, $title, $align, $url, $size, $alt );
    113113
    114114        return $html;
     
    149149 * @return unknown
    150150 */
    151 function image_add_caption( $html, $id, $alt, $title, $align, $url, $size ) {
    152 
    153         if ( empty($alt) || apply_filters( 'disable_captions', '' ) )
     151function image_add_caption( $html, $id, $caption, $title, $align, $url, $size, $alt = '' ) {
     152
     153        if ( empty($caption) || apply_filters( 'disable_captions', '' ) )
    154154                return $html;
    155155
    156156        $id = ( 0 < (int) $id ) ? 'attachment_' . $id : '';
    157157
    158         preg_match( '/width="([0-9]+)/', $html, $matches );
    159         if ( ! isset($matches[1]) )
     158        if ( ! preg_match( '/width="([0-9]+)/', $html, $matches ) )
    160159                return $html;
    161160
     
    166165                $align = 'none';
    167166
    168         $alt = ! empty($alt) ? addslashes($alt) : '';
    169 
    170167        $shcode = '[caption id="' . $id . '" align="align' . $align
    171         . '" width="' . $width . '" caption="' . $alt . '"]' . $html . '[/caption]';
     168        . '" width="' . $width . '" caption="' . addslashes($caption) . '"]' . $html . '[/caption]';
    172169
    173170        return apply_filters( 'image_add_caption_shortcode', $shcode, $html );
    174171}
    175 add_filter( 'image_send_to_editor', 'image_add_caption', 20, 7 );
     172add_filter( 'image_send_to_editor', 'image_add_caption', 20, 8 );
    176173
    177174/**
     
    432429                $post = apply_filters('attachment_fields_to_save', $post, $attachment);
    433430
     431                if ( isset($attachment['image_alt']) && !empty($attachment['image_alt']) ) {
     432                        $image_alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
     433                        if ( $image_alt != stripslashes($attachment['image_alt']) ) {
     434                                $image_alt = wp_strip_all_tags( stripslashes($attachment['image_alt']), true );
     435                                // update_meta expects slashed
     436                                update_post_meta( $attachment_id, '_wp_attachment_image_alt', addslashes($image_alt) );
     437                        }
     438                }
     439
    434440                if ( isset($post['errors']) ) {
    435441                        $errors[$attachment_id] = $post['errors'];
     
    440446                        wp_update_post($post);
    441447
    442                 foreach ( get_attachment_taxonomies($post) as $t )
     448                foreach ( get_attachment_taxonomies($post) as $t ) {
    443449                        if ( isset($attachment[$t]) )
    444450                                wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $attachment[$t])), $t, false);
     451                }
    445452        }
    446453
     
    890897function image_attachment_fields_to_edit($form_fields, $post) {
    891898        if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
     899                $alt = get_post_meta($post->ID, '_wp_attachment_image_alt', true);
     900                if ( empty($alt) )
     901                        $alt = '';
     902
    892903                $form_fields['post_title']['required'] = true;
    893                 $file = wp_get_attachment_url($post->ID);
    894 
    895                 $form_fields['image_url']['value'] = $file;
    896 
    897                 $form_fields['post_excerpt']['label'] = __('Caption');
    898                 $form_fields['post_excerpt']['helps'][] = __('Also used as alternate text for the image');
    899 
    900                 $form_fields['post_content']['label'] = __('Description');
     904
     905                $form_fields['image_alt'] = array(
     906                        'value' => $alt,
     907                        'label' => __('Alternate text'),
     908                        'helps' => __('Alt text for the image, e.g. &#8220;The Mona Lisa&#8221;')
     909                );
    901910
    902911                $form_fields['align'] = array(
     
    906915                );
    907916
    908                 $form_fields['image-size'] = image_size_input_fields($post, get_option('image_default_size'));
     917                $form_fields['image-size'] = image_size_input_fields( $post, get_option('image_default_size') );
     918
     919        } else {
     920                unset( $form_fields['image_alt'] );
    909921        }
    910922        return $form_fields;
     
    968980        if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
    969981                $url = $attachment['url'];
    970 
    971                 if ( isset($attachment['align']) )
    972                         $align = $attachment['align'];
    973                 else
    974                         $align = 'none';
    975 
    976                 if ( !empty($attachment['image-size']) )
    977                         $size = $attachment['image-size'];
    978                 else
    979                         $size = 'medium';
    980 
     982                $align = !empty($attachment['align']) ? $attachment['align'] : 'none';
     983                $size = !empty($attachment['image-size']) ? $attachment['image-size'] : 'medium';
     984                $alt = !empty($attachment['image_alt']) ? $attachment['image_alt'] : '';
    981985                $rel = ( $url == get_attachment_link($attachment_id) );
    982986
    983                 return get_image_send_to_editor($attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel, $size);
     987                return get_image_send_to_editor($attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel, $size, $alt);
    984988        }
    985989
     
    10111015                'post_title'   => array(
    10121016                        'label'      => __('Title'),
    1013                         'value'      => $edit_post->post_title,
     1017                        'value'      => $edit_post->post_title
    10141018                ),
     1019                'image_alt'   => array(),
    10151020                'post_excerpt' => array(
    10161021                        'label'      => __('Caption'),
    1017                         'value'      => $edit_post->post_excerpt,
     1022                        'value'      => $edit_post->post_excerpt
    10181023                ),
    10191024                'post_content' => array(
    10201025                        'label'      => __('Description'),
    10211026                        'value'      => $edit_post->post_content,
    1022                         'input'      => 'textarea',
     1027                        'input'      => 'textarea'
    10231028                ),
    10241029                'url'          => array(
     
    10261031                        'input'      => 'html',
    10271032                        'html'       => image_link_input_fields($post, get_option('image_default_link_type')),
    1028                         'helps'      => __('Enter a link URL or click above for presets.'),
     1033                        'helps'      => __('Enter a link URL or click above for presets.')
    10291034                ),
    10301035                'menu_order'   => array(
     
    10361041                        'input'      => 'html',
    10371042                        'html'       => "<input type='text' class='urlfield' readonly='readonly' name='attachments[$post->ID][url]' value='" . esc_attr($image_url) . "' /><br />",
    1038                         'value'      => isset($edit_post->post_url) ? $edit_post->post_url : '',
    1039                         'helps'      => __('Location of the uploaded file.'),
     1043                        'value'      => isset($edit_post->post_url) ? $edit_post->post_url : wp_get_attachment_url($post->ID),
     1044                        'helps'      => __('Location of the uploaded file.')
    10401045                )
    10411046        );
     
    15261531
    15271532        insert : function() {
    1528                 var t = this, html, f = document.forms[0], cls, title = '', alt = '', caption = null;
     1533                var t = this, html, f = document.forms[0], cls, title = '', alt = '', caption = '';
    15291534
    15301535                if ( '' == f.src.value || '' == t.width ) return false;
    15311536
    15321537                if ( f.title.value ) {
    1533                         title = f.title.value.replace(/['"<>]+/g, '');
     1538                        title = f.title.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
    15341539                        title = ' title="'+title+'"';
    15351540                }
    15361541
    1537                 if ( f.alt.value ) {
    1538                         alt = f.alt.value.replace(/['"<>]+/g, '');
     1542                if ( f.alt.value )
     1543                        alt = f.alt.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
     1544
    15391545<?php if ( ! apply_filters( 'disable_captions', '' ) ) { ?>
    1540                         caption = f.alt.value.replace(/'/g, '&#39;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
     1546                if ( f.caption.value )
     1547                        caption = f.caption.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
    15411548<?php } ?>
    1542                 }
    15431549
    15441550                cls = caption ? '' : ' class="'+t.align+'"';
     
    19161922function type_url_form_image() {
    19171923
    1918         if ( apply_filters( 'disable_captions', '' ) ) {
    1919                 $alt = __('Alternate Text');
    1920                 $alt_help = __('Alt text for the image, e.g. &#8220;The Mona Lisa&#8221;');
     1924        if ( !apply_filters( 'disable_captions', '' ) ) {
     1925                $caption = '
     1926                <tr>
     1927                        <th valign="top" scope="row" class="label">
     1928                                <span class="alignleft"><label for="caption">' . __('Image Caption') . '</label></span>
     1929                        </th>
     1930                        <td class="field"><input id="caption" name="caption" value="" type="text" /></td>
     1931                </tr>
     1932';
    19211933        } else {
    1922                 $alt = __('Image Caption');
    1923                 $alt_help = __('Also used as alternate text for the image');
     1934                $caption = '';
    19241935        }
    19251936
     
    19431954                                <span class="alignright"><abbr title="required" class="required">*</abbr></span>
    19441955                        </th>
    1945                         <td class="field"><p><input id="title" name="title" value="" type="text" aria-required="true" /></p></td>
     1956                        <td class="field"><input id="title" name="title" value="" type="text" aria-required="true" /></td>
    19461957                </tr>
    19471958
    19481959                <tr>
    19491960                        <th valign="top" scope="row" class="label">
    1950                                 <span class="alignleft"><label for="alt">' . $alt . '</label></span>
     1961                                <span class="alignleft"><label for="alt">' . __('Alternate Text') . '</label></span>
    19511962                        </th>
    19521963                        <td class="field"><input id="alt" name="alt" value="" type="text" aria-required="true" />
    1953                         <p class="help">' . $alt_help . '</p></td>
     1964                        <p class="help">' . __('Alt text for the image, e.g. &#8220;The Mona Lisa&#8221;') . '</p></td>
    19541965                </tr>
    1955 
     1966                ' . $caption . '
    19561967                <tr class="align">
    19571968                        <th valign="top" scope="row" class="label"><p><label for="align">' . __('Alignment') . '</label></p></th>
Note: See TracChangeset for help on using the changeset viewer.