Make WordPress Core


Ignore:
Timestamp:
01/25/2008 07:21:11 PM (17 years ago)
Author:
matt
Message:

First pass at async upload, multi-upload, and gallery feature. Modified names from patch. Hat tip: tellyworth, skeltoac.

File:
1 edited

Legend:

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

    r6628 r6659  
    1515    $image_title = attribute_escape( @$values['image-title'] );
    1616    $image_align = @$values['image-url'];
    17 
     17    $post_id = $_GET['post_id'];
     18   
    1819?>
    1920<div id="media-upload-header">
     
    2526</ul>
    2627</div>
    27 <?php if ($error) { ?>
    28     <div id="media-upload-error">
    29     <?php echo $error->get_error_message(); ?>
    30     </div>
    31 <?php } ?>
     28<div id="media-upload-error">
     29<?php if ($error) {
     30    echo $error->get_error_message();
     31} ?>
     32</div>
     33<script type="text/javascript">
     34<!--
     35
     36jQuery(document).ready(function(){
     37    var swfu = new SWFUpload({
     38            upload_url : "<?php echo get_option('siteurl').'/wp-admin/async-upload.php'; ?>",
     39            flash_url : "<?php echo get_option('siteurl').'/wp-includes/js/swfupload/swfupload_f9.swf'; ?>",
     40            file_post_name: "async-upload",
     41            swfupload_element_id : "flash-upload-ui", // id of the element displayed when swfupload is available
     42            degraded_element_id : "html-upload-ui",   // when swfupload is unavailable
     43            //file_types : "*.jpg;*.gif;*.png",
     44            file_size_limit : "<?php echo wp_max_upload_size(); ?> B",
     45            post_params : {
     46                "post_id" : "<?php echo $post_id; ?>",
     47                "auth_cookie" : "<?php echo $_COOKIE[AUTH_COOKIE]; ?>",
     48                "type" : "image",
     49            },
     50            swfupload_loaded_handler : uploadLoadedImage,
     51            upload_progress_handler : uploadProgressImage,
     52            upload_success_handler : uploadSuccessImage,
     53            upload_error_handler: uploadError,
     54            file_queued_handler : fileQueuedImage,
     55            file_queue_error_handler : fileQueueError,
     56            file_dialog_complete_handler : fileDialogComplete,
     57
     58            custom_settings : {
     59                progressTarget : "flash-upload-ui",
     60                cancelButtonId : "btnCancel2"
     61            },
     62           
     63            debug: false,
     64       
     65        });
     66
     67    document.getElementById("flash-browse-button").onclick = function () { swfu.selectFile(); };
     68});
     69//-->
     70</script>
    3271<form enctype="multipart/form-data" method="post" action="<?php echo attribute_escape($action_url); ?>" id="image-upload" class="media-upload-form">
    33 <p><label for="image-file"><?php _e('Choose image'); ?></label>
     72<p id="flash-upload-ui">
     73    <label for="flash-browse-button"><?php _e('Choose image'); ?></label>
     74    <input id="flash-browse-button" type="button" value="<?php _e('Browse'); ?>" />
     75    <label for="image-file" class="form-help"><?php _e('Only PNG, JPG, GIF'); ?></label></p>
     76<p id="html-upload-ui"><label for="image-file"><?php _e('Choose image'); ?></label>
    3477    <input type="file" name="image-file" id="image-file" />
    3578    <label for="image-file" class="form-help"><?php _e('Only PNG, JPG, GIF'); ?></label></p>
     
    58101    <a href="#" onclick="return top.tb_remove();" id="image-cancel" class="button-cancel"><?php _e('Cancel'); ?></a>
    59102</p>
    60     <input type="hidden" name="parent_post_id" value="<?php echo attribute_escape('parent_post_id'); ?>" />
     103    <input type="hidden" name="post_id" value="<?php echo attribute_escape($post_id); ?>" />
    61104    <?php wp_nonce_field( 'inlineuploading' ); ?>
    62105</form>
     
    70113    }
    71114
    72     check_admin_referer('inlineuploading');
    73    
    74115    if ( empty($_POST['image-add']) ) {
    75116        // no button click, we're just displaying the form
     
    78119    else {
    79120        // Add Image button was clicked
    80         $id = image_upload_post();
     121        check_admin_referer('inlineuploading');
     122   
     123        // if the async flash uploader was used, the attachment has already been inserted and its ID is passed in post.
     124        // otherwise this is a regular form post and we still have to handle the upload and create the attachment.
     125        if ( !empty($_POST['attachment_id']) ) {
     126            $id = intval($_POST['attachment_id']);
     127            // store the title and alt into the attachment post
     128            wp_update_post(array(
     129                'ID' => $id,
     130                'post_title' => $_POST['image-title'],
     131                'post_content' => $_POST['image-alt'],
     132            ));
     133        }
     134        else {
     135            $id = image_upload_post();
     136        }
    81137       
    82138        // if the input was invalid, redisplay the form with its current values
     
    89145}
    90146
     147// this returns html to include in the single image upload form when the async flash upload has finished
     148// i.e. show a thumb of the image, and include the attachment id as a hidden input
     149function async_image_callback($id) {
     150    $thumb_url = wp_get_attachment_thumb_url($id);
     151    if ( empty($thumb_url) )
     152        $thumb_url = wp_mime_type_icon($id);
     153       
     154    if ($thumb_url) {
     155        $out = '<p><input type="hidden" name="attachment_id" id="attachment_id" value="'.intval($id).'" />'
     156            . '<img src="'.wp_get_attachment_thumb_url($id).'" class="pinkynail" /> '
     157            . basename(wp_get_attachment_url($id)).'</p>';
     158    }
     159    else {
     160        $out = '<p><input type="hidden" name="attachment_id" id="attachment_id" value="'.intval($id).'" />'
     161            . basename(wp_get_attachment_url($id)).'</p>';
     162    }
     163   
     164    $post = get_post($id);
     165    $title = addslashes($post->post_title);
     166    $alt = addslashes($post->post_content);
     167   
     168    // populate the input fields with post data (which in turn comes from exif/iptc)
     169    $out .= <<<EOF
     170<script type="text/javascript">
     171<!--
     172jQuery('#image-alt').val('{$alt}').attr('disabled', false);
     173jQuery('#image-title').val('{$title}').attr('disabled', false);
     174jQuery('#image-url').attr('disabled', false);
     175jQuery('#image-add').attr('disabled', false);
     176-->
     177</script>   
     178EOF;
     179
     180    return $out;
     181}
     182
     183add_filter('async_upload_image', 'async_image_callback');
     184
     185
    91186function image_send_to_editor($id, $alt, $title, $align, $url='') {
    92187   
     
    98193        $hwstring = ' width="'.intval($meta['width']).'" height="'.intval($meta['height']).'"';
    99194
    100     $html = '<img src="'.attribute_escape($img_src).'" alt="'.attribute_escape($alt).'" title="'.attribute_escape($title).'"'.$hwstring.' class="align-'.attribute_escape($align).'" />';
     195    $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).'" />';
    101196
    102197    if ( $url )
     
    153248        wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
    154249
     250    return $id;     
     251}
     252
     253// this handles the file upload POST itself, creating the attachment post
     254function media_handle_upload($file_id, $post_id, $post_data = array()) {
     255    $overrides = array('test_form'=>false);
     256    $file = wp_handle_upload($_FILES[$file_id], $overrides);
     257
     258    if ( isset($file['error']) )
     259        return new wp_error( 'upload_error', $file['error'] );
     260
     261    $url = $file['url'];
     262    $type = $file['type'];
     263    $file = $file['file'];
     264    $title = preg_replace('/\.[^.]+$/', '', basename($file));
     265    $content = '';
     266
     267    // use image exif/iptc data for title and caption defaults if possible
     268    if ( $image_meta = @wp_read_image_metadata($file) ) {
     269        if ( trim($image_meta['title']) )
     270            $title = $image_meta['title'];
     271        if ( trim($image_meta['caption']) )
     272            $content = $image_meta['caption'];
     273    }
     274
     275    // Construct the attachment array
     276    $attachment = array_merge( array(
     277        'post_mime_type' => $type,
     278        'guid' => $url,
     279        'post_parent' => $post_id,
     280        'post_title' => $title,
     281        'post_content' => $content,
     282    ), $post_data );
     283
     284    // Save the data
     285    $id = wp_insert_attachment($attachment, $file, $post_parent);
     286    if ( !is_wp_error($id) ) {
     287        wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
     288    }
     289
    155290    return $id;
    156291
    157     wp_redirect( get_option('siteurl') . "/wp-admin/upload.php?style=$style&tab=browse&action=view&ID=$id&post_id=$post_id");
    158        
    159 }
     292}
     293
    160294
    161295// wrap iframe content (produced by $content_func) in a doctype, html head/body etc
     
    195329    global $post_ID, $temp_ID;
    196330    $uploading_iframe_ID = (int) (0 == $post_ID ? $temp_ID : $post_ID);
    197     $uploading_iframe_src = wp_nonce_url("media-upload.php?type=image&amp;&amp;post_id=$uploading_iframe_ID", 'inlineuploading');
    198     $uploading_iframe_src = apply_filters('uploading_iframe_src', $uploading_iframe_src);
     331    $image_upload_iframe_src = wp_nonce_url("media-upload.php?type=image&amp;post_id=$uploading_iframe_ID", 'inlineuploading');
     332    $image_upload_iframe_src = apply_filters('image_upload_iframe_src', $image_upload_iframe_src);
     333    $multimedia_upload_iframe_src = wp_nonce_url("media-upload.php?type=multimedia&amp;post_id=$uploading_iframe_ID", 'inlineuploading');
     334    $multimedia_upload_iframe_src = apply_filters('multimedia_upload_iframe_src', $multimedia_upload_iframe_src);
    199335    $out = <<<EOF
    200 <a href="{$uploading_iframe_src}&TB_iframe=true&height=500&width=460" class="thickbox">
     336<a href="{$multimedia_upload_iframe_src}&TB_iframe=true&height=500&width=640" class="thickbox">Multimedia</a>
     337<a href="{$image_upload_iframe_src}&TB_iframe=true&height=500&width=460" class="thickbox">
    201338<img src="./images/media-buttons.gif" alt="" />
    202339</a>
     
    226363}
    227364
     365add_action('media_upload_multimedia', 'multimedia_upload_handler');
    228366add_action('media_upload_image', 'image_upload_handler');
    229367add_action('admin_head_image_upload_form', 'media_admin_css');
    230368
     369function multimedia_upload_handler() {
     370    if ( !current_user_can('upload_files') ) {
     371        return new wp_error( 'upload_not_allowed', __('You are not allowed to upload files.') );
     372    }
     373
     374    if ( empty($_POST) ) {
     375        // no button click, we're just displaying the form
     376            wp_iframe( 'multimedia_upload_form' );
     377    } elseif ( empty($_POST['upload-button']) ) {
     378        // Insert multimedia button was clicked
     379        check_admin_referer('multimedia-form');
     380
     381        if ( !empty($_POST['attachments']) ) foreach ( $_POST['attachments'] as $attachment_id => $attachment ) {
     382            $post = $_post = get_post($attachment_id, ARRAY_A);
     383            $post['post_content'] = $attachment['post_content'];
     384            $post['post_title'] = $attachment['post_title'];
     385            if ( $post != $_post )
     386                wp_update_post($post);
     387
     388            if ( $taxonomies = get_object_taxonomies('attachment') ) foreach ( $taxonomies as $t )
     389                if ( isset($attachment[$t]) )
     390                    wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $attachment[$t])), $t, false);
     391        }
     392
     393        media_send_to_editor('[gallery]');
     394    } else {
     395        // Upload File button was clicked
     396
     397        $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
     398
     399        wp_iframe( 'multimedia_upload_form' );
     400    }
     401}
     402
     403function get_multimedia_items( $post_id ) {
     404    $attachments = get_children("post_parent=$post_id&post_type=attachment&orderby=\"menu_order ASC, ID ASC\"");
     405
     406    if ( empty($attachments) )
     407        return '';
     408
     409    foreach ( $attachments as $id => $attachment ) {
     410        $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>";
     411        $output .= get_multimedia_item($id);
     412        $output .= "    <div class='progress clickmask'></div>\n</div>";
     413    }
     414
     415    return $output;
     416}
     417
     418function get_multimedia_item( $attachment_id ) {
     419    $thumb_url = wp_get_attachment_thumb_url( $attachment_id );
     420    if ( empty($thumb_url) )
     421        $thumb_url = wp_mime_type_icon( $attachment_id );
     422
     423    $title_label = __('Title');
     424    $description_label = __('Description');
     425    $tags_label = __('Tags');
     426
     427    $toggle_on = __('Describe &raquo;');
     428    $toggle_off = __('Describe &laquo;');
     429
     430    $post = get_post($attachment_id);
     431
     432    $filename = basename($post->guid);
     433    $title = attribute_escape($post->post_title);
     434    $description = attribute_escape($post->post_content);
     435    if ( $_tags = get_the_tags($attachment_id) ) {
     436        foreach ( $_tags as $tag )
     437            $tags[] = $tag->name;
     438        $tags = attribute_escape(join(', ', $tags));
     439    }
     440
     441    $delete_href = wp_nonce_url("post.php?action=delete-post&amp;post=$attachment_id", 'delete-post_' . $attachment_id);
     442    $delete = __('Delete');
     443
     444    $item = "
     445    <a class='toggle describe-toggle-on' href='#'>$toggle_on</a>
     446    <a class='toggle describe-toggle-off' href='#'>$toggle_off</a>
     447    <span class='filename new'>$filename</span>
     448    <div class='slidetoggle describe'>
     449        <img class='thumbnail' src='$thumb_url' alt='' />
     450        <fieldset>
     451        <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>
     452        <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>
     453";
     454
     455    if ( $taxonomies = get_object_taxonomies('attachment') ) foreach ( $taxonomies as $t ) {
     456        $tax = get_taxonomy($t);
     457        $t_title = !empty($tax->title) ? $tax->title : $t;
     458        if ( false === $terms = get_object_term_cache( $attachment_id, $t ) )
     459            $_terms = wp_get_object_terms($attachment_id, $t);
     460        if ( $_terms ) {
     461            foreach ( $_terms as $term )
     462                $terms[] = $term->name;
     463            $terms = join(', ', $terms);
     464        } else
     465            $terms = '';
     466        $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";
     467    }
     468
     469    $item .= "
     470        </fieldset>
     471        <p><a id='del$attachment_id' class='delete' href='$delete_href'>$delete</a></p>
     472    </div>
     473";
     474
     475    return $item;
     476}
     477
     478function multimedia_upload_form( $error = null ) {
     479    $flash_action_url = get_option('siteurl') . '/wp-admin/async-upload.php?type=multimedia';
     480    $form_action_url = get_option('siteurl') . '/wp-admin/media-upload.php?type=multimedia';
     481
     482    $post_id = intval($_REQUEST['post_id']);
     483
    231484?>
     485<div id="media-upload-header">
     486<h3>Add Images</h3>
     487</div>
     488    <div id="media-upload-error">
     489<?php if ($error) { ?>
     490    <?php echo $error->get_error_message(); ?>
     491<?php } ?>
     492    </div>
     493<script type="text/javascript">
     494<!--
     495jQuery(function($){
     496    swfu = new SWFUpload({
     497            upload_url : "<?php echo attribute_escape( $flash_action_url ); ?>",
     498            flash_url : "<?php echo get_option('siteurl').'/wp-includes/js/swfupload/swfupload_f9.swf'; ?>",
     499            file_post_name: "async-upload",
     500            file_types: "*.*",
     501            post_params : {
     502                "post_id" : "<?php echo $post_id; ?>",
     503                "auth_cookie" : "<?php echo $_COOKIE[AUTH_COOKIE]; ?>",
     504                "type" : "multimedia"
     505            },
     506            swfupload_element_id : "flash-upload-ui", // id of the element displayed when swfupload is available
     507            degraded_element_id : "html-upload-ui",   // when swfupload is unavailable
     508            //upload_start_handler : uploadStart,
     509            upload_progress_handler : uploadProgressMultimedia,
     510            //upload_error_handler : uploadError,
     511            upload_success_handler : uploadSuccessMultimedia,
     512            upload_complete_handler : uploadCompleteMultimedia,
     513            file_dialog_start_handler : fileDialogStart,
     514            file_queued_handler : fileQueuedMultimedia,
     515            file_queue_error_handler : fileQueueError,
     516            file_dialog_complete_handler : fileDialogComplete,
     517
     518            debug: false,
     519        });
     520    $("#flash-browse-button").bind( "click", function(){swfu.selectFiles();});
     521    $("#insert-multimedia").bind( "click", function(){jQuery(this).parents('form').get(0).submit();});
     522    $(".multimedia-item.preloaded").each(function(){uploadSuccessMultimedia({id:this.id.replace(/[^0-9]/g, '')},'');jQuery('#insert-multimedia').attr('disabled', '');});
     523    $("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;});
     524});
     525//-->
     526</script>
     527<p id="flash-upload-ui" style="display:none">
     528    <input id="flash-browse-button" type="button" value="<?php _e('Choose Files'); ?>" />
     529    <label for="image-file" class="form-help"><?php _e('Only PNG, JPG, GIF'); ?></label>
     530</p>
     531
     532<form enctype="multipart/form-data" method="post" action="<?php echo attribute_escape($form_action_url); ?>" class="media-upload-form">
     533
     534<div id="html-upload-ui">
     535    <p><label for="async-upload"><?php _e('Choose image'); ?></label>
     536    <input type="file" name="async-upload" id="async-upload" />
     537    <label for="image-file" class="form-help"><?php _e('Only PNG, JPG, GIF'); ?></label>
     538    </p>
     539    <p>
     540    <button id="upload-button" name="upload-button" value="1" class="button-ok"><?php _e('Add Image'); ?></button>
     541    <a href="#" onClick="return top.tb_remove();" id="image-cancel" class="button-cancel"><?php _e('Cancel'); ?></a>
     542    </p>
     543    <input type="hidden" name="post_id" id="post_id" value="<?php echo $post_id; ?>" />
     544    <br style="clear:both" />
     545</div>
     546
     547
     548
     549<div id="multimedia-items">
     550
     551<?php echo get_multimedia_items($post_id); ?>
     552
     553</div>
     554
     555<p class="submit">
     556    <a href="#" onClick="return top.tb_remove();" id="image-cancel" class="button-cancel"><?php _e('Cancel'); ?></a>
     557    <input type="button" class="submit" id="insert-multimedia" value="<?php _e('Insert gallery into post'); ?>" disabled="disabled" />
     558</p>
     559
     560<?php wp_nonce_field('multimedia-form'); ?>
     561
     562</form>
     563
     564<?php
     565}
     566
     567add_action('admin_head_multimedia_upload_form', 'media_admin_css');
     568add_filter('async_upload_multimedia', 'get_multimedia_item');
     569add_filter('media_upload_multimedia', 'multimedia_upload_handler');
     570
     571// Any 'attachment' taxonomy will be included in the description input form for the multi uploader
     572// Example:
     573//register_taxonomy('attachment_people', 'attachment', array('title' => 'People'));
     574
     575?>
Note: See TracChangeset for help on using the changeset viewer.