Changeset 2997

Show
Ignore:
Timestamp:
11/06/05 05:43:01 (3 years ago)
Author:
ryan
Message:

Really move image-uploading.php to inline-uploading.php

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/wp-admin/inline-uploading.php

    r2994 r2997  
     1<?php 
     2 
     3require_once('admin.php'); 
     4 
     5if (!current_user_can('edit_posts')) 
     6    die(__('You do not have permission to edit posts.')); 
     7 
     8$wpvarstoreset = array('action', 'post', 'all', 'last', 'link', 'sort', 'start', 'imgtitle', 'descr', 'object', 'flickrtag'); 
     9 
     10for ($i=0; $i<count($wpvarstoreset); $i += 1) { 
     11    $wpvar = $wpvarstoreset[$i]; 
     12    if (!isset($$wpvar)) { 
     13        if (empty($_POST["$wpvar"])) { 
     14            if (empty($_GET["$wpvar"])) { 
     15                $$wpvar = ''; 
     16            } else { 
     17            $$wpvar = $_GET["$wpvar"]; 
     18            } 
     19        } else { 
     20            $$wpvar = $_POST["$wpvar"]; 
     21        } 
     22    } 
     23} 
     24 
     25$post = (int) $post; 
     26$images_width = 1; 
     27 
     28function get_udims($width, $height) { 
     29    if ( $height <= 96 && $width <= 128 ) 
     30        return array($width, $height); 
     31    elseif ( $width / $height > 4 / 3 ) 
     32        return array(128, (int) ($height / $width * 128)); 
     33    else 
     34        return array((int) ($width / $height * 96), 96); 
     35} 
     36 
     37switch($action) { 
     38case 'delete': 
     39 
     40wp_delete_object($object); 
     41 
     42header("Location: ".basename(__FILE__)."?post=$post&all=$all&action=view&start=$start"); 
     43die; 
     44 
     45case 'save': 
     46 
     47$overrides = array('action'=>'save'); 
     48 
     49$file = wp_handle_upload($_FILES['image'], $overrides); 
     50 
     51if ( isset($file['error']) ) 
     52    die($file['error'] . '<a href="' . basename(__FILE__) . '?action=upload&post="' . $post . '">Back to Image Uploading</a>'); 
     53 
     54$url = $file['url']; 
     55$file = $file['file']; 
     56$filename = basename($file); 
     57 
     58// Construct the object array 
     59$object = array( 
     60    'post_title' => $imgtitle ? $imgtitle : $filename, 
     61    'post_content' => $descr, 
     62    'post_status' => 'object', 
     63    'post_parent' => $post, 
     64    'post_type' => $_FILES['image']['type'], 
     65    'guid' => $url 
     66    ); 
     67 
     68// Save the data 
     69$id = wp_attach_object($object, $post); 
     70 
     71// Generate the object's postmeta. 
     72$imagesize = getimagesize($file); 
     73$imagedata['width'] = $imagesize['0']; 
     74$imagedata['height'] = $imagesize['1']; 
     75list($uwidth, $uheight) = get_udims($imagedata['width'], $imagedata['height']); 
     76$imagedata['hwstring_small'] = "height='$uheight' width='$uwidth'"; 
     77$imagedata['file'] = $file; 
     78$imagedata['thumb'] = "thumb-$filename"; 
     79 
     80add_post_meta($id, 'imagedata', $imagedata); 
     81 
     82if ( $imagedata['width'] * $imagedata['height'] < 3 * 1024 * 1024 ) { 
     83    if ( $imagedata['width'] > 128 && $imagedata['width'] >= $imagedata['height'] * 4 / 3 ) 
     84        $error = wp_create_thumbnail($file['file'], 128); 
     85    elseif ( $imagedata['height'] > 96 ) 
     86        $error = wp_create_thumbnail($file, 96); 
     87} 
     88 
     89header("Location: ".basename(__FILE__)."?post=$post&all=$all&action=view&last=true"); 
     90die; 
     91 
     92case 'upload': 
     93 
     94$current_1 = ' class="current"'; 
     95$back = $next = false; 
     96break; 
     97 
     98case 'view': 
     99 
     100// How many images do we show? How many do we query? 
     101$num = 5; 
     102$double = $num * 2; 
     103 
     104if ( $post && empty($all) ) { 
     105    $and_post = "AND post_parent = '$post'"; 
     106    $current_2 = ' class="current"'; 
     107} else { 
     108    $current_3 = ' class="current"'; 
     109} 
     110 
     111if ( $last ) 
     112    $start = $wpdb->get_var("SELECT count(ID) FROM $wpdb->posts WHERE post_status = 'object' AND left(post_type, 5) = 'image' $and_post") - $num; 
     113else 
     114    $start = (int) $start; 
     115 
     116if ( $start < 0 ) 
     117    $start = 0; 
     118 
     119if ( '' == $sort ) 
     120    $sort = "ID"; 
     121 
     122$images = $wpdb->get_results("SELECT ID, post_date, post_title, guid FROM $wpdb->posts WHERE post_status = 'object' AND left(post_type, 5) = 'image' $and_post ORDER BY $sort LIMIT $start, $double", ARRAY_A); 
     123 
     124if ( count($images) > $num ) { 
     125    $next = $start + count($images) - $num; 
     126} else { 
     127    $next = false; 
     128} 
     129 
     130if ( $start > 0 ) { 
     131    $back = $start - $num; 
     132    if ( $back < 1 ) 
     133        $back = '0'; 
     134} else { 
     135    $back = false; 
     136} 
     137 
     138$i = 0; 
     139$uwidth_sum = 0; 
     140$images_html = ''; 
     141$images_style = ''; 
     142$images_script = ''; 
     143if ( count($images) > 0 ) { 
     144    $images = array_slice( $images, 0, $num ); 
     145    $__delete = __('DELETE'); 
     146    $__subpost_on = __('SUBPOST <strong>ON</strong>'); 
     147    $__subpost_off = __('SUBPOST <strong>OFF</strong>'); 
     148    $__thumbnail_on = __('THUMBNAIL <strong>ON</strong>'); 
     149    $__thumbnail_off = __('THUMBNAIL <strong>OFF</strong>'); 
     150    $__no_thumbnail = __('<del>THUMBNAIL</del>'); 
     151    $__close = __('CLOSE'); 
     152    $__confirmdelete = __('Delete this photo from the server?'); 
     153    $__nothumb = __('There is no thumbnail associated with this photo.'); 
     154    $images_script .= "subposton = '$__subpost_on';\nsubpostoff = '$__subpost_off';\n"; 
     155    $images_script .= "thumbnailon = '$__thumbnail_on';\nthumbnailoff = '$__thumbnail_off';\n"; 
     156    foreach ( $images as $key => $image ) { 
     157        $meta = get_post_meta($image['ID'], 'imagedata', true); 
     158        if (!is_array($meta)) { 
     159            wp_delete_object($image['ID']); 
     160            continue; 
     161        } 
     162        $image = array_merge($image, $meta); 
     163        if ( ($image['width'] > 128 || $image['height'] > 96) && !empty($image['thumb']) && file_exists(dirname($image['file']).'/'.$image['thumb']) ) { 
     164            $src = str_replace(basename($image['guid']), '', $image['guid']) . $image['thumb']; 
     165            $images_script .= "src".$i."a = '$src';\nsrc".$i."b = '".$image['guid']."';\n"; 
     166            $thumb = 'true'; 
     167            $thumbtext = $__thumbnail_on; 
     168        } else { 
     169            $src = $image['guid']; 
     170            $thumb = 'false'; 
     171            $thumbtext = $__no_thumbnail; 
     172        } 
     173        list($image['uwidth'], $image['uheight']) = get_udims($image['width'], $image['height']); 
     174        $height_width = 'height="'.$image['uheight'].'" width="'.$image['uwidth'].'"'; 
     175        $uwidth_sum += 128; 
     176        $xpadding = (128 - $image['uwidth']) / 2; 
     177        $ypadding = (96 - $image['uheight']) / 2; 
     178        $object = $image['ID']; 
     179        $images_style .= "#target$i img { padding: {$ypadding}px {$xpadding}px; }\n"; 
     180        $href = get_subpost_link($object); 
     181        $images_script .= "href".$i."a = '$href';\nhref".$i."b = '{$image['guid']}';\n"; 
     182        $images_html .= <<<HERE 
     183<div id='target$i' class='imagewrap left'> 
     184    <div id='popup$i' class='popup'> 
     185        <a id="L$i" onclick="toggleLink($i);return false;" href="javascript:void();">$__subpost_on</a> 
     186        <a id="I$i" onclick="if($thumb)toggleImage($i);else alert('$__nothumb');return false;" href="javascript:void();">$thumbtext</a> 
     187        <a onclick="return confirm('$__confirmdelete')" href="<?php echo basename(__FILE__); ?>?action=delete&amp;object=$object&amp;all=$all&amp;start=$start&amp;post=$post">$__delete</a> 
     188        <a onclick="popup.style.display='none';return false;" href="javascript:void()">$__close</a> 
     189    </div> 
     190    <a id="link$i" class="imagelink" href="$href" onclick="imagePopup($i);return false;" title="{$image['post_title']}"> 
     191        <img id='image$i' src='$src' alt='{$image['post_title']}' $height_width /> 
     192    </a> 
     193</div> 
     194HERE; 
     195        $i++; 
     196    } 
     197} 
     198 
     199$images_width = $uwidth_sum + ( count($images) * 5 ) + 30; 
     200 
     201break; 
     202 
     203default: 
     204die('This script was not meant to be called directly.'); 
     205} 
     206 
     207?> 
     208<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
     209<html xmlns="http://www.w3.org/1999/xhtml"> 
     210<head> 
     211<meta http-equiv="imagetoolbar" content="no" /> 
     212<script type="text/javascript"> 
     213/* Define any variables we'll need, such as alternate URLs. */ 
     214<?php echo $images_script; ?> 
     215 
     216function validateImageName() { 
     217/* This is more for convenience than security. Server-side validation is very thorough.*/ 
     218obj = document.getElementById('upload'); 
     219r = /.jpg$|.gif$|.png$/i; 
     220if ( obj.value.match(r) ) 
     221return true; 
     222alert('Please select a JPG, PNG or GIF file.'); 
     223return false; 
     224} 
     225function cancelUpload() { 
     226o = document.getElementById('uploadForm'); 
     227o.method = 'GET'; 
     228o.action.value = 'view'; 
     229o.submit(); 
     230} 
     231function imagePopup(i) { 
     232if ( popup ) 
     233popup.style.display = 'none'; 
     234target = document.getElementById('target'+i); 
     235popup = document.getElementById('popup'+i); 
     236//popup.style.top = (target.offsetTop + 3) + 'px'; 
     237popup.style.left = (target.offsetLeft) + 'px'; 
     238popup.style.display = 'block'; 
     239} 
     240function init() { 
     241popup = false; 
     242} 
     243function toggleLink(n) { 
     244    o=document.getElementById('link'+n); 
     245    oi=document.getElementById('L'+n); 
     246    if ( oi.innerHTML == subposton ) { 
     247        o.href = eval('href'+n+'b'); 
     248        oi.innerHTML = subpostoff; 
     249    } else { 
     250        o.href = eval('href'+n+'a'); 
     251        oi.innerHTML = subposton; 
     252    } 
     253} 
     254function toggleImage(n) { 
     255    o = document.getElementById('image'+n); 
     256    oi = document.getElementById('I'+n); 
     257    if ( oi.innerHTML == thumbnailon ) { 
     258        o.src = eval('src'+n+'b'); 
     259        oi.innerHTML = thumbnailoff; 
     260    } else { 
     261        o.src = eval('src'+n+'a'); 
     262        oi.innerHTML = thumbnailon; 
     263    } 
     264} 
     265</script> 
     266<style type="text/css"> 
     267body { 
     268font: 13px "Lucida Grande", "Lucida Sans Unicode", Tahoma, Verdana; 
     269border: none; 
     270margin: 0px; 
     271height: 150px; 
     272background: rgb(223, 232, 241); 
     273} 
     274form { 
     275margin: 6px 2px 0px 6px; 
     276} 
     277#wrap { 
     278clear: both; 
     279margin: 0px; 
     280padding: 0px; 
     281height: 133px; 
     282width: 100%; 
     283overflow: auto; 
     284} 
     285#images { 
     286clear: both; 
     287margin: 0px; 
     288padding: 5px 15px; 
     289height: 96px; 
     290white-space: nowrap; 
     291width: <?php echo $images_width; ?>px; 
     292} 
     293#images img { 
     294background-color: rgb(209, 226, 239); 
     295} 
     296<?php echo $images_style; ?> 
     297.imagewrap { 
     298margin-right: 5px; 
     299height: 96px; 
     300overflow: hidden; 
     301} 
     302.imagewrap * { 
     303margin: 0px; 
     304padding: 0px; 
     305border: 0px; 
     306} 
     307.imagewrap a, .imagewrap a img, .imagewrap a:hover img, .imagewrap a:visited img, .imagewrap a:active img { 
     308text-decoration: none; 
     309float: left; 
     310/*display: block;*/ 
     311text-align: center; 
     312} 
     313#menu { 
     314margin: 0px; 
     315list-style: none; 
     316background: rgb(109, 166, 209); 
     317padding: 4px 0px 0px 8px; 
     318text-align: left; 
     319border-bottom: 3px solid rgb(68, 138, 189); 
     320} 
     321#menu li { 
     322display: inline; 
     323margin: 0px; 
     324} 
     325#menu a, #menu a:visited, #menu a:active { 
     326padding: 1px 3px 3px; 
     327text-decoration: none; 
     328color: #234; 
     329background: transparent; 
     330} 
     331#menu a:hover { 
     332background: rgb(203, 214, 228); 
     333color: #000; 
     334} 
     335#menu .current a, #menu .current a:hover, #menu .current a:visited, #menu .current a:active { 
     336background: rgb(223, 232, 241); 
     337padding-bottom: 3px; 
     338color: #000; 
     339border-right: 2px solid rgb(20, 86, 138); 
     340} 
     341.tip { 
     342color: rgb(68, 138, 189); 
     343padding: 1px 3px; 
     344} 
     345.inactive { 
     346color: #579; 
     347padding: 1px 3px; 
     348} 
     349.left { 
     350float: left; 
     351} 
     352.right { 
     353float: right; 
     354} 
     355.center { 
     356text-align: center; 
     357} 
     358#menu li.spacer { 
     359margin-left: 40px; 
     360} 
     361label { 
     362float: left; 
     363width: 18%; 
     364} 
     365#title, #descr { 
     366width: 80%; 
     367margin-top: 2px; 
     368} 
     369#descr { 
     370height: 35px; 
     371v-align: top; 
     372} 
     373#buttons { 
     374width: 98%; 
     375margin-top: 2px; 
     376text-align: right; 
     377} 
     378.popup { 
     379margin: 4px 4px; 
     380padding: 3px; 
     381position: absolute; 
     382width: 114px; 
     383height: 82px; 
     384display: none; 
     385background-color: rgb(223, 232, 241); 
     386opacity: .90; 
     387filter:alpha(opacity=90); 
     388text-align: center; 
     389} 
     390.popup a, .popup a:visited, .popup a:active { 
     391background-color: transparent; 
     392display: block; 
     393width: 100%; 
     394text-decoration: none; 
     395color: #246; 
     396} 
     397.popup a:hover { 
     398background-color: #fff; 
     399color: #000; 
     400} 
     401</style> 
     402</head> 
     403<body onload="init()"> 
     404<ul id="menu"> 
     405<li<?php echo $current_1; ?>><a href="<?php echo basename(__FILE__); ?>?action=upload&amp;post=<?php echo $post; ?>&amp;all=<?php echo $all; ?>"><?php _e('Upload File'); ?></a></li> 
     406<li<?php echo $current_2; ?>><a href="<?php echo basename(__FILE__); ?>?action=view&amp;post=<?php echo $post; ?>"><?php _e('Browse Attached'); ?></a></li> 
     407<li<?php echo $current_3; ?>><a href="<?php echo basename(__FILE__); ?>?action=view&amp;post=<?php echo $post; ?>&amp;all=true"><?php _e('Browse All'); ?></a></li> 
     408<li> </li> 
     409<?php if ( false !== $back ) : ?> 
     410<li class="spacer"><a href="<?php echo basename(__FILE__); ?>?action=<?php echo $action; ?>&amp;post=<?php echo $post; ?>&amp;all=<?php echo $all; ?>&amp;start=0" title="<?php _e('First'); ?>">|&lt;</a></li> 
     411<li><a href="<?php echo basename(__FILE__); ?>?action=<?php echo $action; ?>&amp;post=<?php echo $post; ?>&amp;all=<?php echo $all; ?>&amp;start=<?php echo $back; ?>" title="<?php _e('Back'); ?>">&lt;&lt;</a></li> 
     412<?php else : ?> 
     413<li class="inactive spacer">|&lt;</li> 
     414<li class="inactive">&lt;&lt;</li> 
     415<?php endif; ?> 
     416<?php if ( false !== $next ) : ?> 
     417<li><a href="<?php echo basename(__FILE__); ?>?action=<?php echo $action; ?>&amp;post=<?php echo $post; ?>&amp;all=<?php echo $all; ?>&amp;start=<?php echo $next; ?>" title="<?php _e('Next'); ?>">&gt;&gt;</a></li> 
     418<li><a href="<?php echo basename(__FILE__); ?>?action=<?php echo $action; ?>&amp;post=<?php echo $post; ?>&amp;all=<?php echo $all; ?>&amp;last=true" title="<?php _e('Last'); ?>">&gt;|</a></li> 
     419<?php else : ?> 
     420<li class="inactive">&gt;&gt;</li> 
     421<li class="inactive">&gt;|</li> 
     422<?php endif; ?> 
     423</ul> 
     424<?php if ( $action == 'view' ) : ?> 
     425<span class="left tip"><?php _e('Drag and drop photos to post'); ?></span> 
     426<span class="right tip"><?php _e('Click photos for more options'); ?></span> 
     427<div id="wrap"> 
     428<div id="images"> 
     429<?php echo $images_html; ?> 
     430</div> 
     431</div> 
     432<?php elseif ( $action == 'upload' ) : ?> 
     433<div class="tip"></div> 
     434<form enctype="multipart/form-data" id="uploadForm" method="POST" action="<?php echo basename(__FILE__); ?>" onsubmit="return validateImageName()"> 
     435<label for="upload"><?php _e('Image:'); ?></label><input type="file" id="upload" name="image" onchange="validateImageName()" /> 
     436<label for="title"><?php _e('Title:'); ?></label><input type="text" id="title" name="imgtitle" /> 
     437<label for="descr"><?php _e('Description:'); ?></label><input type="textarea" name="descr" id="descr" value="" /> 
     438<input type="hidden" name="action" value="save" /> 
     439<input type="hidden" name="post" value="<?php echo $post; ?>" /> 
     440<input type="hidden" name="all" value="<?php echo $all; ?>" /> 
     441<div id="buttons"> 
     442<input type="submit" value="<?php _e('Upload'); ?>" /> 
     443<input type="button" value="<?php _e('Cancel'); ?>" onclick="cancelUpload()" /> 
     444</div> 
     445</form> 
     446<?php endif; ?> 
     447</body> 
     448</html> 
     449 
     450 
     451