Make WordPress Core


Ignore:
Timestamp:
02/26/2008 07:30:10 PM (17 years ago)
Author:
ryan
Message:

Media upload updates from andy. see #5911

File:
1 edited

Legend:

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

    r7002 r7043  
    11<?php
    22
    3 function image_upload_tabs() {
     3function media_upload_tabs() {
    44    $_default_tabs = array(
    5         'image_upload_handler' => __('From Computer'), // handler function name => tab text
     5        'computer' => __('From Computer'), // handler action suffix => tab text
     6        'attachments' => __('Attachments'),
     7        'library' => __('Media Library'),
    68    );
    79
    8     return apply_filters('image_upload_tabs', $_default_tabs);
    9 }
    10 
    11 function the_image_upload_tabs() {
    12     $tabs = image_upload_tabs();
     10    return apply_filters('media_upload_tabs', $_default_tabs);
     11}
     12
     13function update_attachments_tab($tabs) {
     14    global $wpdb;
     15    if ( !isset($_REQUEST['post_id']) ) {
     16        unset($tabs['attachments']);
     17        return $tabs;
     18    }
     19    if ( intval($_REQUEST['post_id']) )
     20        $attachments = $wpdb->get_var($wpdb->prepare("SELECT count(*) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = %d", $_REQUEST['post_id']));
     21
     22    $tabs['attachments'] = sprintf(__('Attachments (%s)'), "<span id='attachments-count'>$attachments</span>");
     23
     24    return $tabs;
     25}
     26add_filter('media_upload_tabs', 'update_attachments_tab');
     27
     28function the_media_upload_tabs() {
     29    $tabs = media_upload_tabs();
    1330
    1431    if ( !empty($tabs) ) {
     
    2138            if ( ++$i == count($tabs) )
    2239                $class = ' class="last"';
    23             if ( $callback == $current )
    24                 $disabled = ' disabled="disabled"';
    25             else
    26                 $disabled = '';
    2740            $href = add_query_arg('tab', $callback);
    2841            if ( $callback == $current )
     
    3043            else
    3144                $link = "<a href='$href'>$text</a>";
    32             echo "\t<li$class>$link</li>\n";
     45            echo "\t<li id='tab-$callback'$class>$link</li>\n";
    3346        }
    3447        echo "</ul>\n";
     
    144157    global $post_ID, $temp_ID;
    145158    $uploading_iframe_ID = (int) (0 == $post_ID ? $temp_ID : $post_ID);
    146     $multimedia_upload_iframe_src = "media-upload.php?type=multimedia&amp;post_id=$uploading_iframe_ID";
    147     $multimedia_upload_iframe_src = apply_filters('multimedia_upload_iframe_src', $multimedia_upload_iframe_src);
    148     echo "<a href='$multimedia_upload_iframe_src&amp;TB_iframe=true&amp;height=500&amp;width=640' class='button-secondary thickbox'>" . __('Add media'). '</a>';
     159    $media_upload_iframe_src = "media-upload.php?type=media&amp;post_id=$uploading_iframe_ID";
     160    $media_upload_iframe_src = apply_filters('media_upload_iframe_src', $media_upload_iframe_src);
     161    echo "<a href='$media_upload_iframe_src&amp;TB_iframe=true&amp;height=500&amp;width=640' class='button-secondary thickbox'>" . __('Add media'). '</a>';
    149162}
    150163add_action( 'media_buttons', 'media_buttons' );
     
    170183}
    171184
    172 add_action('media_upload_multimedia', 'multimedia_upload_handler');
    173 add_action('admin_head_image_upload_form', 'media_admin_css');
    174 
    175 function multimedia_upload_handler() {
    176     if ( !current_user_can('upload_files') ) {
    177         return new wp_error( 'upload_not_allowed', __('You are not allowed to upload files.') );
    178     }
    179 
    180     // no button click, we're just displaying the form
    181     if ( empty($_POST) )
    182         return wp_iframe( 'multimedia_upload_form' );
    183 
    184     check_admin_referer('multimedia-form');
    185 
    186     // Insert multimedia button was clicked
     185add_action('media_upload_media', 'media_upload_handler');
     186
     187function media_upload_form_handler() {
     188    check_admin_referer('media-form');
     189
     190    // Insert media button was clicked
    187191    if ( !empty($_FILES) ) {
    188192        // Upload File button was clicked
     
    218222    }
    219223
    220     if ( isset($_POST['insert-multimedia']) )
     224    if ( isset($_POST['insert-media']) )
    221225        return media_send_to_editor('[gallery]');
    222226
     
    228232    }
    229233
    230     wp_iframe( 'multimedia_upload_form', $errors );
    231 }
    232 
    233 function get_multimedia_items( $post_id, $errors ) {
    234     $attachments = get_children("post_parent=$post_id&post_type=attachment&orderby=\"menu_order ASC, ID ASC\"");
     234    return $errors;
     235}
     236
     237function media_upload_computer() {
     238    if ( !empty($_POST) ) {
     239        $return = media_upload_form_handler();
     240   
     241        if ( is_string($return) )
     242            return $return;
     243        if ( is_array($return) )
     244            $errors = $return;
     245    }
     246
     247    return wp_iframe( 'media_upload_computer_form', $errors );
     248}
     249
     250function media_upload_attachments() {
     251    if ( !empty($_POST) ) {
     252        $return = media_upload_form_handler();
     253   
     254        if ( is_string($return) )
     255            return $return;
     256        if ( is_array($return) )
     257            $errors = $return;
     258    }
     259
     260    return wp_iframe( 'media_upload_attachments_form', $errors );
     261}
     262
     263function media_upload_library() {
     264    if ( empty($_POST) )
     265        wp_iframe( 'media_upload_library_form', $errors );
     266}
     267
     268function get_media_items( $post_id, $errors ) {
     269    if ( $post_id )
     270        $attachments = get_children("post_parent=$post_id&post_type=attachment&orderby=menu_order ASC, ID&order=DESC");
     271    else
     272        $attachments = get_paged_attachments();
    235273
    236274    if ( empty($attachments) )
     
    238276
    239277    foreach ( $attachments as $id => $attachment )
    240         if ( $item = get_multimedia_item($id, isset($errors[$id]) ? $errors[$id] : null) )
    241             $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>$item<div class='progress clickmask'></div>\n</div>";
     278        if ( $item = get_media_item($id, isset($errors[$id]) ? $errors[$id] : null) )
     279            $output .= "\n<div id='media-item-$id' class='media-item preloaded'><div id='media-upload-error-$id'></div><span class='filename'></span><div class='progress'><div class='bar'></div></div>$item<div class='progress clickmask'></div>\n</div>";
    242280
    243281    return $output;
     
    416454}
    417455
    418 function get_multimedia_item( $attachment_id, $errors = null, $send = true ) {
     456function get_media_item( $attachment_id, $errors = null, $send = true ) {
    419457    if ( ( $attachment_id = intval($attachment_id) ) && $thumb_url = get_attachment_icon_src( $attachment_id ) )
    420         $thumb_url = $thumb_url[1];
     458        $thumb_url = $thumb_url[0];
    421459    else
    422460        return false;
     
    454492        <tr><td>$post->post_mime_type</td></tr>
    455493        <tr><td>" . mysql2date($post->post_date, get_option('time_format')) . "</td></tr>
    456         <tr><td>" . apply_filters('multimedia_meta', '', $post) . "</tr></td>\n";
     494        <tr><td>" . apply_filters('media_meta', '', $post) . "</tr></td>\n";
    457495
    458496    $defaults = array(
     
    537575}
    538576
    539 function multimedia_upload_form( $errors = null ) {
    540     $flash_action_url = get_option('siteurl') . '/wp-admin/async-upload.php?type=multimedia';
    541     $form_action_url = get_option('siteurl') . '/wp-admin/media-upload.php?type=multimedia';
     577function media_upload_header() {
     578    ?>
     579    <div id="media-upload-header">
     580    <h3><?php _e('Add Media'); ?></h3>
     581    <?php the_media_upload_tabs(); ?>
     582    </div>
     583    <?php
     584}
     585
     586function media_upload_form( $errors = null ) {
     587    $flash_action_url = get_option('siteurl') . '/wp-admin/async-upload.php?type=media';
    542588
    543589    $post_id = intval($_REQUEST['post_id']);
    544590
    545591?>
    546 <div id="media-upload-header">
    547 <h3><?php _e('Add Media'); ?></h3>
    548 <?php the_image_upload_tabs(); ?>
    549 </div>
    550 
    551592<div id="media-upload-error">
    552593<?php if (isset($errors['upload_error']) && is_wp_error($errors['upload_error'])) { ?>
     
    565606                "post_id" : "<?php echo $post_id; ?>",
    566607                "auth_cookie" : "<?php echo $_COOKIE[AUTH_COOKIE]; ?>",
    567                 "type" : "multimedia"
     608                "type" : "media"
    568609            },
    569610            swfupload_element_id : "flash-upload-ui", // id of the element displayed when swfupload is available
     
    583624        });
    584625    $("#flash-browse-button").bind( "click", function(){swfu.selectFiles();});
    585     var preloaded = $(".multimedia-item.preloaded");
    586     if ( preloaded.length > 0 ) {
    587         jQuery('#insert-multimedia').attr('disabled', '');
    588         preloaded.each(function(){uploadSuccess({id:this.id.replace(/[^0-9]/g, '')},'');});
    589     }
    590626});
    591627//-->
    592628</script>
    593629
    594 <form enctype="multipart/form-data" method="post" action="<?php echo attribute_escape($form_action_url); ?>" class="media-upload-form">
    595630
    596631<div id="flash-upload-ui">
    597632    <p><input id="flash-browse-button" type="button" value="<?php _e('Choose files to upload'); ?>" class="button" /></p>
    598     <p><?php _e('As each upload completes, you can add titles and descriptions below.'); ?></p>
     633    <p><?php _e('After a file has been uploaded, you can add titles and descriptions below.'); ?></p>
    599634</div>
    600635
     
    611646    <br style="clear:both" />
    612647</div>
    613 
    614 <div id="multimedia-items">
    615 
    616 <?php echo get_multimedia_items($post_id, $errors); ?>
    617 
     648<?php
     649}
     650
     651function media_upload_computer_form( $errors = null ) {
     652    media_upload_header();
     653
     654    $post_id = intval($_REQUEST['post_id']);
     655
     656    $form_action_url = get_option('siteurl') . "/wp-admin/media-upload.php?type=media&tab=computer&post_id=$post_id";
     657
     658?>
     659
     660<form enctype="multipart/form-data" method="post" action="<?php echo attribute_escape($form_action_url); ?>" class="media-upload-form" id="computer-form">
     661<?php wp_nonce_field('media-form'); ?>
     662<?php media_upload_form( $errors ); ?>
     663
     664<div id="media-items"></div>
     665<p class="submit">
     666    <input type="submit" class="submit insert-gallery" name="insert-media" value="<?php _e('Insert gallery into post'); ?>" />
     667</p>
     668</form>
     669
     670<?php
     671}
     672
     673function media_upload_attachments_form($errors) {
     674    media_upload_header();
     675
     676    $post_id = intval($_REQUEST['post_id']);
     677
     678    $form_action_url = get_option('siteurl') . "/wp-admin/media-upload.php?type=media&tab=attachments&post_id=$post_id";
     679
     680?>
     681
     682<script type="text/javascript">
     683<!--
     684jQuery(function($){
     685    var preloaded = $(".media-item.preloaded");
     686    if ( preloaded.length > 0 ) {
     687        preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
     688        updateMediaForm();
     689    }
     690});
     691-->
     692</script>
     693
     694<form enctype="multipart/form-data" method="post" action="<?php echo attribute_escape($form_action_url); ?>" class="media-upload-form" id="attachments-form">
     695<?php wp_nonce_field('media-form'); ?>
     696<?php //media_upload_form( $errors ); ?>
     697
     698<div id="media-items">
     699<?php echo get_media_items($post_id, $errors); ?>
    618700</div>
    619 
    620701<p class="submit">
    621     <input type="submit" class="submit insert-gallery" name="insert-multimedia" value="<?php _e('Insert gallery into post'); ?>" />
     702    <input type="submit" class="submit insert-gallery" name="insert-media" value="<?php _e('Insert gallery into post'); ?>" />
    622703</p>
    623 
    624 <?php wp_nonce_field('multimedia-form'); ?>
    625 
     704<input type="hidden" name="post_id" id="post_id" value="<?php echo $post_id; ?>" />
    626705</form>
    627 
    628706<?php
    629707}
    630708
    631 add_action('admin_head_multimedia_upload_form', 'media_admin_css');
    632 add_filter('async_upload_multimedia', 'get_multimedia_item', 10, 2);
    633 add_filter('media_upload_multimedia', 'multimedia_upload_handler');
     709function media_upload_library_form($errors) {
     710    media_upload_header();
     711}
     712
     713add_filter('async_upload_media', 'get_media_item', 10, 2);
     714
     715add_filter('media_upload_computer', 'media_upload_computer');
     716add_action('admin_head_media_upload_computer_form', 'media_admin_css');
     717
     718add_filter('media_upload_attachments', 'media_upload_attachments');
     719add_action('admin_head_media_upload_attachments_form', 'media_admin_css');
     720
     721add_filter('media_upload_library', 'media_upload_library');
     722add_action('admin_head_media_upload_library_form', 'media_admin_css');
    634723
    635724
Note: See TracChangeset for help on using the changeset viewer.