Make WordPress Core


Ignore:
Timestamp:
02/16/2008 09:54:47 PM (16 years ago)
Author:
ryan
Message:

Media handling updates from andy. fixes #5874

File:
1 edited

Legend:

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

    r6874 r6876  
    175175            wp_iframe( 'image_upload_form', get_option('siteurl') . '/wp-admin/media-upload.php?type=image', $_POST, $id );
    176176        else {
    177             image_send_to_editor($id, $_POST['image-alt'], $_POST['image-title'], $_POST['image-align'], $_POST['image-url']);
     177            media_send_to_editor(get_image_send_to_editor($id, $_POST['image-alt'], $_POST['image-title'], $_POST['image-align'], $_POST['image-url']));
    178178        }
    179179    }
     
    233233}
    234234
    235 function image_send_to_editor($id, $alt, $title, $align, $url='') {
     235function get_image_send_to_editor($id, $alt, $title, $align, $url='', $rel = false) {
    236236
    237237    $img_src = wp_get_attachment_url($id);
     
    244244    }
    245245
    246     $html = '<img src="'.attribute_escape($img_src).'" rel="attachment wp-att-'.attribute_escape($id).'" alt="'.attribute_escape($alt).'" title="'.attribute_escape($title).'"'.$hwstring.' class="align-'.attribute_escape($align).'" />';
    247 
     246    $html = '<img src="'.attribute_escape($img_src).'" alt="'.attribute_escape($alt).'" title="'.attribute_escape($title).'"'.$hwstring.' class="align-'.attribute_escape($align).'" />';
     247
     248    $rel = $rel ? ' rel="attachment wp-att-'.attribute_escape($id).'"' : '';
    248249    if ( $url )
    249         $html = '<a href="'.attribute_escape($url).'">'.$html.'</a>';
     250        $html = "<a href='".attribute_escape($url)."'$rel>$html</a>";
    250251       
    251252    $html = apply_filters( 'image_send_to_editor', $html, $id, $alt, $title, $align, $url );
    252253
    253     media_send_to_editor($html);
     254    return $html;
    254255}
    255256
     
    263264</script>
    264265    <?php
     266    exit;
    265267}
    266268
     
    431433    }
    432434
    433     if ( empty($_POST) ) {
    434         // no button click, we're just displaying the form
    435             wp_iframe( 'multimedia_upload_form' );
    436     } elseif ( empty($_POST['upload-button']) ) {
    437         // Insert multimedia button was clicked
    438         check_admin_referer('multimedia-form');
    439 
    440         if ( !empty($_POST['attachments']) ) foreach ( $_POST['attachments'] as $attachment_id => $attachment ) {
    441             $post = $_post = get_post($attachment_id, ARRAY_A);
     435    // no button click, we're just displaying the form
     436    if ( empty($_POST) )
     437        return wp_iframe( 'multimedia_upload_form' );
     438
     439    check_admin_referer('multimedia-form');
     440
     441    // Insert multimedia button was clicked
     442    if ( !empty($_FILES) ) {
     443        // Upload File button was clicked
     444
     445        $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
     446
     447        if ( is_wp_error($id) )
     448            $errors['upload_error'] = $id;
     449    }
     450
     451    if ( !empty($_POST['attachments']) ) foreach ( $_POST['attachments'] as $attachment_id => $attachment ) {
     452        $post = $_post = get_post($attachment_id, ARRAY_A);
     453        if ( isset($attachment['post_content']) )
    442454            $post['post_content'] = $attachment['post_content'];
     455        if ( isset($attachment['post_title']) )
    443456            $post['post_title'] = $attachment['post_title'];
    444             if ( $post != $_post )
    445                 wp_update_post($post);
    446 
    447             if ( $taxonomies = get_object_taxonomies('attachment') ) foreach ( $taxonomies as $t )
    448                 if ( isset($attachment[$t]) )
    449                     wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $attachment[$t])), $t, false);
     457        if ( isset($attachment['post_excerpt']) )
     458            $post['post_excerpt'] = $attachment['post_excerpt'];
     459
     460        $post = apply_filters('attachment_fields_to_save', $post, $attachment);
     461
     462        if ( isset($post['errors']) ) {
     463            $errors[$attachment_id] = $post['errors'];
     464            unset($post['errors']);
    450465        }
    451466
    452         media_send_to_editor('[gallery]');
    453     } else {
    454         // Upload File button was clicked
    455 
    456         $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
    457 
    458         wp_iframe( 'multimedia_upload_form' );
    459     }
    460 }
    461 
    462 function get_multimedia_items( $post_id ) {
     467        if ( $post != $_post )
     468            wp_update_post($post);
     469
     470        foreach ( get_attachment_taxonomies($post) as $t )
     471            if ( isset($attachment[$t]) )
     472                wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $attachment[$t])), $t, false);
     473    }
     474
     475    if ( isset($_POST['insert-multimedia']) )
     476        return media_send_to_editor('[gallery]');
     477
     478    if ( isset($_POST['send']) ) {
     479        $send_id = (int) array_shift(array_keys($_POST['send']));
     480        $attachment = $_POST['attachments'][$send_id];
     481        $html = apply_filters('media_send_to_editor', get_the_attachment_link($send_id, 0, array(125,125), !empty($attachment['post_content'])), $send_id, $attachment);
     482        return media_send_to_editor($html);
     483    }
     484
     485    wp_iframe( 'multimedia_upload_form', $errors );
     486}
     487
     488function get_multimedia_items( $post_id, $errors ) {
    463489    $attachments = get_children("post_parent=$post_id&post_type=attachment&orderby=\"menu_order ASC, ID ASC\"");
    464490
     
    468494    foreach ( $attachments as $id => $attachment ) {
    469495        $output .= "\n<div id='multimedia-item-$id' class='multimedia-item preloaded'><div id='media-upload-error-$id'></div><span class='filename'></span><div class='progress'><div class='bar'></div></div>";
    470         $output .= get_multimedia_item($id);
     496        $output .= get_multimedia_item($id, isset($errors[$id]) ? $errors[$id] : null);
    471497        $output .= "    <div class='progress clickmask'></div>\n</div>";
    472498    }
     
    475501}
    476502
    477 function get_multimedia_item( $attachment_id ) {
     503function get_attachment_taxonomies($attachment) {
     504    if ( is_int( $attachment ) )
     505        $attachment = get_post($attachment);
     506    else if ( is_array($attachment) )
     507        $attachment = (object) $attachment;
     508
     509    if ( ! is_object($attachment) )
     510        return array();
     511
     512    $filename = basename($attachment->guid);
     513
     514    $objects = array('attachment');
     515
     516    if ( false !== strpos($filename, '.') )
     517        $objects[] = 'attachment:' . substr($filename, strrpos($filename, '.') + 1);
     518    if ( !empty($attachment->post_mime_type) ) {
     519        $objects[] = 'attachment:' . $attachment->post_mime_type;
     520        if ( false !== strpos($attachment->post_mime_type, '/') )
     521            foreach ( explode('/', $attachment->post_mime_type) as $token )
     522                if ( !empty($token) )
     523                    $objects[] = "attachment:$token";
     524    }
     525
     526    $taxonomies = array();
     527    foreach ( $objects as $object )
     528        if ( $taxes = get_object_taxonomies($object) )
     529            $taxonomies = array_merge($taxonomies, $taxes);
     530
     531    return array_unique($taxonomies);
     532}
     533
     534function image_attachment_fields_to_edit($form_fields, $post) {
     535    if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
     536        $form_fields['post_title']['required'] = true;
     537        $form_fields['post_excerpt']['label'] = __('Alternate Text');
     538        $form_fields['post_content']['label'] = __('Description');
     539
     540        if ( strlen(trim($post->post_excerpt)) == 0 )
     541            $form_fields['post_excerpt']['helps'][] = __('Alternate Text helps people who can not see the image.');
     542
     543        $form_fields['_send']['url'] = array(
     544            'label' => __('Link URL'),
     545            'input' => 'html',
     546            'html'  => '',
     547            'helps'  => __('If filled, this will override the default link URL.'),
     548        );
     549        $form_fields['_send']['align'] = array(
     550            'label' => __('Alignment'),
     551            'input' => 'html',
     552            'html'  => "
     553                <input type='radio' name='attachments[$post->ID][align]' id='image-align-none-$post->ID' value='none' />
     554                <label for='image-align-none-$post->ID' class='align image-align-none-label'>" . __('None') . "</label>
     555                <input type='radio' name='attachments[$post->ID][align]' id='image-align-left-$post->ID' value='left' />
     556                <label for='image-align-left-$post->ID' class='align image-align-left-label'>" . __('Left') . "</label>
     557                <input type='radio' name='attachments[$post->ID][align]' id='image-align-center-$post->ID' value='center' />
     558                <label for='image-align-center-$post->ID' class='align image-align-center-label'>" . __('Center') . "</label>
     559                <input type='radio' name='attachments[$post->ID][align]' id='image-align-right-$post->ID' value='right' />
     560                <label for='image-align-right-$post->ID' class='align image-align-right-label'>" . __('Right') . "</label>\n",
     561        );
     562    }
     563    return $form_fields;
     564}
     565
     566add_filter('attachment_fields_to_edit', 'image_attachment_fields_to_edit', 10, 2);
     567
     568function image_attachment_fields_to_save($post, $attachment) {
     569    if ( substr($post['post_mime_type'], 0, 5) == 'image' ) {
     570        if ( strlen(trim($post['post_title'])) == 0 ) {
     571            $post['post_title'] = preg_replace('/\.\w+$/', '', basename($post['guid']));
     572            $post['errors']['post_title']['errors'][] = __('Empty Title filled from filename.');
     573        }
     574    }
     575
     576    return $post;
     577}
     578
     579add_filter('attachment_fields_to_save', 'image_attachment_fields_to_save', 10, 2);
     580
     581function image_media_send_to_editor($html, $attachment_id, $attachment) {
     582    $post =& get_post($attachment_id);
     583    if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
     584        if ( !empty($attachment['url']) )
     585            $url = $attachment['url'];
     586        elseif ( $rel = strlen(trim($post->post_content)) )
     587            $url = get_attachment_link($attachment_id);
     588        else
     589            $url = wp_get_attachment_url($attachment_id);
     590
     591        if ( isset($attachment['align']) )
     592            $align = $attachment['align'];
     593        else
     594            $align = 'none';
     595
     596        return get_image_send_to_editor($attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel);
     597    }
     598
     599    return $html;
     600}
     601
     602add_filter('media_send_to_editor', 'image_media_send_to_editor', 10, 3);
     603
     604function get_attachment_fields_to_edit($post, $errors = null) {
     605    if ( is_int($post) )
     606        $post =& get_post($post);
     607    if ( is_array($post) )
     608        $post = (object) $post;
     609
     610    $edit_post = sanitize_post($post, 'edit');
     611
     612    $form_fields = array(
     613        'post_title'   => array(
     614            'label'      => __('Title'),
     615            'value'      => $edit_post->post_title,
     616        ),
     617        'post_excerpt' => array(
     618            'label'      => __('Excerpt'),
     619            'value'      => $edit_post->post_excerpt,
     620        ),
     621        'post_content' => array(
     622            'label'      => __('Description'),
     623            'value'      => $edit_post->post_content,
     624            'input'      => 'textarea',
     625            'helps'      => array(__('If filled, the default link URL will be the attachment permalink.')),
     626        ),
     627    );
     628
     629    foreach ( get_attachment_taxonomies($post) as $taxonomy ) {
     630        $t = (array) get_taxonomy($taxonomy);
     631        if ( empty($t['label']) )
     632            $t['label'] = $taxonomy;
     633        if ( empty($t['args']) )
     634            $t['args'] = array();
     635
     636        $terms = get_object_term_cache($post->ID, $taxonomy);
     637        if ( empty($terms) )
     638            $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
     639
     640        $values = array();
     641
     642        foreach ( $terms as $term )
     643            $values[] = $term->name;
     644        $t['value'] = join(', ', $values);
     645
     646        $form_fields[$taxonomy] = $t;
     647    }
     648
     649    // Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default
     650    // The recursive merge is easily traversed with array casting: foreach( (array) $things as $thing )
     651    $form_fields = array_merge_recursive($form_fields, (array) $errors);
     652   
     653    $form_fields = apply_filters('attachment_fields_to_edit', $form_fields, $post);
     654
     655    return $form_fields;
     656}
     657
     658function get_multimedia_item( $attachment_id, $errors = null, $send = true ) {
    478659    $thumb_url = wp_get_attachment_thumb_url( $attachment_id );
    479660    if ( empty($thumb_url) )
     
    500681    }
    501682
    502     $delete_href = wp_nonce_url("post.php?action=delete-post&amp;post=$attachment_id", 'delete-post_' . $attachment_id);
    503     $delete = __('Delete');
    504 
     683    $form_fields = get_attachment_fields_to_edit($post, $errors);
     684
     685    $class = empty($errors) ? 'startclosed' : 'startopen';
    505686    $item = "
    506687    <a class='toggle describe-toggle-on' href='#'>$toggle_on</a>
    507688    <a class='toggle describe-toggle-off' href='#'>$toggle_off</a>
    508689    <span class='filename new'>$filename</span>
    509     <div class='slidetoggle describe'>
    510         <img class='thumbnail' src='$thumb_url' alt='' />
    511         <fieldset>
    512         <p><label for='attachments[$attachment_id][post_title]'>$title_label</label><input type='text' id='attachments[$attachment_id][post_title]' name='attachments[$attachment_id][post_title]' value='$title' /></p>
    513         <p><label for='attachments[$attachment_id][post_content]'>$description_label</label><input type='text' id='attachments[$attachment_id][post_content]' name='attachments[$attachment_id][post_content]' value='$description' /></p>
    514 ";
    515 
    516     if ( $taxonomies = get_object_taxonomies('attachment') ) foreach ( $taxonomies as $t ) {
    517         $tax = get_taxonomy($t);
    518         $t_title = !empty($tax->title) ? $tax->title : $t;
    519         if ( false === $terms = get_object_term_cache( $attachment_id, $t ) )
    520             $_terms = wp_get_object_terms($attachment_id, $t);
    521         if ( $_terms ) {
    522             foreach ( $_terms as $term )
    523                 $terms[] = $term->name;
    524             $terms = join(', ', $terms);
    525         } else
    526             $terms = '';
    527         $item .= "<p><label for='attachments[$attachment_id][$t]'>$t_title</label><input type='text' id='attachments[$attachment_id][$t]' name='attachments[$attachment_id][$t]' value='$terms' /></p>\n";
    528     }
    529 
    530     $item .= "
    531         </fieldset>
    532         <p class='delete'><a id='del$attachment_id' class='delete' href='$delete_href'>$delete</a></p>
    533     </div>
    534 ";
     690    <table class='slidetoggle describe $class'><tbody>
     691        <tr>
     692            <td class='A1B1' rowspan='4' colspan='2'><img class='thumbnail' src='$thumb_url' alt='' /></td>
     693            <td>$filename</td>
     694        </tr>
     695        <tr><td>$post->post_mime_type</td></tr>
     696        <tr><td>" . mysql2date($post->post_date, get_option('time_format')) . "</td></tr>
     697        <tr><td>" . apply_filters('multimedia_meta', '', $post) . "</tr></td>\n";
     698
     699    $defaults = array(
     700        'input'      => 'text',
     701        'required'   => false,
     702        'value'      => '',
     703        'extra_rows' => array(),
     704    );
     705
     706    $delete_href = wp_nonce_url("post.php?action=delete-post&amp;post=$attachment_id", 'delete-post_' . $attachment_id);
     707    $delete = __('Delete');
     708    $save = "<input type='submit' value='" . wp_specialchars(__('Save'), 1) . "' />";
     709    $send = "<input type='submit' value='" . wp_specialchars(__('Send to Editor'), 1) . "' id='send[$attachment_id]' name='send[$attachment_id]' />";
     710
     711    if ( empty($form_fields['save']) && empty($form_fields['_send']) ) {
     712        $form_fields['save'] = array('tr' => "\t\t<tr class='submit'><td colspan='2' class='del'><a id='del[$attachment_id]' class='delete' href='$delete_href'>$delete</a></td><td class='savesend'>$save$send</td></tr>\n");
     713    } elseif ( empty($form_fields['save']) ) {
     714        $form_fields['save'] = array('tr' => "\t\t<tr class='submit'><td></td><td></td><td class='savesend'>$save</td></tr>\n");
     715        foreach ( $form_fields['_send'] as $id => $field )
     716            $form_fields[$id] = $field;
     717        $form_fields['send'] = array('tr' => "\t\t<tr class='submit'><td colspan='2' class='del'><a id='del[$attachment_id]' class='delete' href='$delete_href'>$delete</a></td><td class='savesend'>$send</td>");
     718    }
     719
     720    $hidden_fields = array();
     721
     722    foreach ( $form_fields as $id => $field ) {
     723        if ( $id{0} == '_' )
     724            continue;
     725
     726        if ( !empty($field['tr']) ) {
     727            $item .= $field['tr'];
     728            continue;
     729        }
     730
     731        $field = array_merge($defaults, $field);
     732        $name = "attachments[$attachment_id][$id]";
     733
     734        if ( $field['input'] == 'hidden' ) {
     735            $hidden_fields[$name] = $field['value'];
     736            continue;
     737        }
     738
     739        $required = $field['required'] ? '<abbr title="required">*</abbr>' : '';
     740        $item .= "\t\t<tr class='$id'>\n\t\t\t<td class='label'><label for='$name'>{$field['label']}</label></td>\n\t\t\t<td class='required'>$required</td>\n\t\t\t<td class='field'>";
     741        if ( !empty($field[$field['input']]) )
     742            $item .= $field[$field['input']];
     743        elseif ( $field['input'] == 'textarea' ) {
     744            $item .= "<textarea type='text' id='$name' name='$name'>" . wp_specialchars($field['value'], 1) . "</textarea>";
     745        } else {
     746            $item .= "<input type='text' id='$name' name='$name' value='" . wp_specialchars($field['value'], 1) . "' />";
     747        }
     748        $item .= "</td>\n\t\t</tr>\n";
     749
     750        $extra_rows = array();
     751
     752        if ( !empty($field['errors']) )
     753            foreach ( array_unique((array) $field['errors']) as $error )
     754                $extra_rows['error'][] = $error;
     755
     756        if ( !empty($field['helps']) )
     757            foreach ( array_unique((array) $field['helps']) as $help )
     758                $extra_rows['help'][] = $help;
     759
     760        if ( !empty($field['extra_rows']) )
     761            foreach ( $field['extra_rows'] as $class => $rows )
     762                foreach ( (array) $rows as $html )
     763                    $extra_rows[$class][] = $html;
     764
     765        foreach ( $extra_rows as $class => $rows )
     766            foreach ( $rows as $html )
     767                $item .= "\t\t<tr><td colspan='2'></td><td class='$class'>$html</td></tr>\n";
     768    }
     769
     770    if ( !empty($form_fields['_final']) )
     771        $item .= "\t\t<tr class='final'><td colspan='3'>{$form_fields['_final']}</td></tr>\n";
     772    $item .= "\t</table>\n";
     773
     774    foreach ( $hidden_fields as $name => $value )
     775        $item .= "\t<input type='hidden' name='$name' id='$name' value='" . wp_specialchars($value, 1) . "' />\n";
    535776
    536777    return $item;
    537778}
    538779
    539 function multimedia_upload_form( $error = null ) {
     780function multimedia_upload_form( $errors = null ) {
    540781    $flash_action_url = get_option('siteurl') . '/wp-admin/async-upload.php?type=multimedia';
    541782    $form_action_url = get_option('siteurl') . '/wp-admin/media-upload.php?type=multimedia';
     
    548789</div>
    549790    <div id="media-upload-error">
    550 <?php if ($error) { ?>
    551     <?php echo $error->get_error_message(); ?>
     791<?php if (isset($errors['upload_error']) && is_wp_error($errors['upload_error'])) { ?>
     792    <?php echo $errors['upload_error']->get_error_message(); ?>
    552793<?php } ?>
    553794    </div>
     
    567808            swfupload_element_id : "flash-upload-ui", // id of the element displayed when swfupload is available
    568809            degraded_element_id : "html-upload-ui",   // when swfupload is unavailable
     810            swfupload_loaded_handler : uploadLoadedMultimedia,
    569811            //upload_start_handler : uploadStart,
    570812            upload_progress_handler : uploadProgressMultimedia,
     
    580822        });
    581823    $("#flash-browse-button").bind( "click", function(){swfu.selectFiles();});
    582     $("#insert-multimedia").bind( "click", function(){jQuery(this).parents('form').get(0).submit();});
    583     $(".multimedia-item.preloaded").each(function(){uploadSuccessMultimedia({id:this.id.replace(/[^0-9]/g, '')},'');jQuery('#insert-multimedia').attr('disabled', '');});
     824    var preloaded = $(".multimedia-item.preloaded");
     825    if ( preloaded ) {
     826        jQuery('#insert-multimedia').attr('disabled', '');
     827        preloaded.each(function(){uploadSuccessMultimedia({id:this.id.replace(/[^0-9]/g, '')},'');});
     828    }
    584829    $("a.delete").bind('click',function(){$.ajax({url:'admin-ajax.php',type:'post',data:{id:this.id.replace(/del/,''),action:'delete-post',_ajax_nonce:this.href.replace(/^.*wpnonce=/,'')}});$(this).parents(".multimedia-item").eq(0).slideToggle(300, function(){$(this).remove();});return false;});
    585830});
     
    610855<div id="multimedia-items">
    611856
    612 <?php echo get_multimedia_items($post_id); ?>
     857<?php echo get_multimedia_items($post_id, $errors); ?>
    613858
    614859</div>
     
    616861<p class="submit">
    617862    <a href="#" onClick="return top.tb_remove();" id="image-cancel" class="button-cancel"><?php _e('Cancel'); ?></a>
    618     <input type="button" class="submit" id="insert-multimedia" value="<?php _e('Insert gallery into post'); ?>" disabled="disabled" />
     863    <input type="submit" class="submit" id="insert-multimedia" name="insert-multimedia" value="<?php _e('Insert gallery into post'); ?>" disabled="disabled" />
    619864</p>
    620865
     
    627872
    628873add_action('admin_head_multimedia_upload_form', 'media_admin_css');
    629 add_filter('async_upload_multimedia', 'get_multimedia_item');
     874add_filter('async_upload_multimedia', 'get_multimedia_item', 10, 2);
    630875add_filter('media_upload_multimedia', 'multimedia_upload_handler');
    631876
    632877// Any 'attachment' taxonomy will be included in the description input form for the multi uploader
    633878// Example:
    634 //register_taxonomy('attachment_people', 'attachment', array('title' => 'People'));
     879register_taxonomy(
     880    //
     881    'image_people',
     882    'attachment:image',
     883    array(
     884        'label' => __('People'),
     885        'template' => __('People: %s'),
     886        'sort' => true,
     887        'args' => array(
     888            'orderby' => 'term_order'
     889        )
     890    )
     891);
    635892
    636893?>
Note: See TracChangeset for help on using the changeset viewer.