Changeset 2997
- Timestamp:
- 11/06/2005 05:43:01 AM (19 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-admin/inline-uploading.php
r2996 r2997 1 <?php 2 3 require_once('admin.php'); 4 5 if (!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 10 for ($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 28 function 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 37 switch($action) { 38 case 'delete': 39 40 wp_delete_object($object); 41 42 header("Location: ".basename(__FILE__)."?post=$post&all=$all&action=view&start=$start"); 43 die; 44 45 case 'save': 46 47 $overrides = array('action'=>'save'); 48 49 $file = wp_handle_upload($_FILES['image'], $overrides); 50 51 if ( 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']; 75 list($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 80 add_post_meta($id, 'imagedata', $imagedata); 81 82 if ( $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 89 header("Location: ".basename(__FILE__)."?post=$post&all=$all&action=view&last=true"); 90 die; 91 92 case 'upload': 93 94 $current_1 = ' class="current"'; 95 $back = $next = false; 96 break; 97 98 case 'view': 99 100 // How many images do we show? How many do we query? 101 $num = 5; 102 $double = $num * 2; 103 104 if ( $post && empty($all) ) { 105 $and_post = "AND post_parent = '$post'"; 106 $current_2 = ' class="current"'; 107 } else { 108 $current_3 = ' class="current"'; 109 } 110 111 if ( $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; 113 else 114 $start = (int) $start; 115 116 if ( $start < 0 ) 117 $start = 0; 118 119 if ( '' == $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 124 if ( count($images) > $num ) { 125 $next = $start + count($images) - $num; 126 } else { 127 $next = false; 128 } 129 130 if ( $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 = ''; 143 if ( 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&object=$object&all=$all&start=$start&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> 194 HERE; 195 $i++; 196 } 197 } 198 199 $images_width = $uwidth_sum + ( count($images) * 5 ) + 30; 200 201 break; 202 203 default: 204 die('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 216 function validateImageName() { 217 /* This is more for convenience than security. Server-side validation is very thorough.*/ 218 obj = document.getElementById('upload'); 219 r = /.jpg$|.gif$|.png$/i; 220 if ( obj.value.match(r) ) 221 return true; 222 alert('Please select a JPG, PNG or GIF file.'); 223 return false; 224 } 225 function cancelUpload() { 226 o = document.getElementById('uploadForm'); 227 o.method = 'GET'; 228 o.action.value = 'view'; 229 o.submit(); 230 } 231 function imagePopup(i) { 232 if ( popup ) 233 popup.style.display = 'none'; 234 target = document.getElementById('target'+i); 235 popup = document.getElementById('popup'+i); 236 //popup.style.top = (target.offsetTop + 3) + 'px'; 237 popup.style.left = (target.offsetLeft) + 'px'; 238 popup.style.display = 'block'; 239 } 240 function init() { 241 popup = false; 242 } 243 function 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 } 254 function 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"> 267 body { 268 font: 13px "Lucida Grande", "Lucida Sans Unicode", Tahoma, Verdana; 269 border: none; 270 margin: 0px; 271 height: 150px; 272 background: rgb(223, 232, 241); 273 } 274 form { 275 margin: 6px 2px 0px 6px; 276 } 277 #wrap { 278 clear: both; 279 margin: 0px; 280 padding: 0px; 281 height: 133px; 282 width: 100%; 283 overflow: auto; 284 } 285 #images { 286 clear: both; 287 margin: 0px; 288 padding: 5px 15px; 289 height: 96px; 290 white-space: nowrap; 291 width: <?php echo $images_width; ?>px; 292 } 293 #images img { 294 background-color: rgb(209, 226, 239); 295 } 296 <?php echo $images_style; ?> 297 .imagewrap { 298 margin-right: 5px; 299 height: 96px; 300 overflow: hidden; 301 } 302 .imagewrap * { 303 margin: 0px; 304 padding: 0px; 305 border: 0px; 306 } 307 .imagewrap a, .imagewrap a img, .imagewrap a:hover img, .imagewrap a:visited img, .imagewrap a:active img { 308 text-decoration: none; 309 float: left; 310 /*display: block;*/ 311 text-align: center; 312 } 313 #menu { 314 margin: 0px; 315 list-style: none; 316 background: rgb(109, 166, 209); 317 padding: 4px 0px 0px 8px; 318 text-align: left; 319 border-bottom: 3px solid rgb(68, 138, 189); 320 } 321 #menu li { 322 display: inline; 323 margin: 0px; 324 } 325 #menu a, #menu a:visited, #menu a:active { 326 padding: 1px 3px 3px; 327 text-decoration: none; 328 color: #234; 329 background: transparent; 330 } 331 #menu a:hover { 332 background: rgb(203, 214, 228); 333 color: #000; 334 } 335 #menu .current a, #menu .current a:hover, #menu .current a:visited, #menu .current a:active { 336 background: rgb(223, 232, 241); 337 padding-bottom: 3px; 338 color: #000; 339 border-right: 2px solid rgb(20, 86, 138); 340 } 341 .tip { 342 color: rgb(68, 138, 189); 343 padding: 1px 3px; 344 } 345 .inactive { 346 color: #579; 347 padding: 1px 3px; 348 } 349 .left { 350 float: left; 351 } 352 .right { 353 float: right; 354 } 355 .center { 356 text-align: center; 357 } 358 #menu li.spacer { 359 margin-left: 40px; 360 } 361 label { 362 float: left; 363 width: 18%; 364 } 365 #title, #descr { 366 width: 80%; 367 margin-top: 2px; 368 } 369 #descr { 370 height: 35px; 371 v-align: top; 372 } 373 #buttons { 374 width: 98%; 375 margin-top: 2px; 376 text-align: right; 377 } 378 .popup { 379 margin: 4px 4px; 380 padding: 3px; 381 position: absolute; 382 width: 114px; 383 height: 82px; 384 display: none; 385 background-color: rgb(223, 232, 241); 386 opacity: .90; 387 filter:alpha(opacity=90); 388 text-align: center; 389 } 390 .popup a, .popup a:visited, .popup a:active { 391 background-color: transparent; 392 display: block; 393 width: 100%; 394 text-decoration: none; 395 color: #246; 396 } 397 .popup a:hover { 398 background-color: #fff; 399 color: #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&post=<?php echo $post; ?>&all=<?php echo $all; ?>"><?php _e('Upload File'); ?></a></li> 406 <li<?php echo $current_2; ?>><a href="<?php echo basename(__FILE__); ?>?action=view&post=<?php echo $post; ?>"><?php _e('Browse Attached'); ?></a></li> 407 <li<?php echo $current_3; ?>><a href="<?php echo basename(__FILE__); ?>?action=view&post=<?php echo $post; ?>&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; ?>&post=<?php echo $post; ?>&all=<?php echo $all; ?>&start=0" title="<?php _e('First'); ?>">|<</a></li> 411 <li><a href="<?php echo basename(__FILE__); ?>?action=<?php echo $action; ?>&post=<?php echo $post; ?>&all=<?php echo $all; ?>&start=<?php echo $back; ?>" title="<?php _e('Back'); ?>"><<</a></li> 412 <?php else : ?> 413 <li class="inactive spacer">|<</li> 414 <li class="inactive"><<</li> 415 <?php endif; ?> 416 <?php if ( false !== $next ) : ?> 417 <li><a href="<?php echo basename(__FILE__); ?>?action=<?php echo $action; ?>&post=<?php echo $post; ?>&all=<?php echo $all; ?>&start=<?php echo $next; ?>" title="<?php _e('Next'); ?>">>></a></li> 418 <li><a href="<?php echo basename(__FILE__); ?>?action=<?php echo $action; ?>&post=<?php echo $post; ?>&all=<?php echo $all; ?>&last=true" title="<?php _e('Last'); ?>">>|</a></li> 419 <?php else : ?> 420 <li class="inactive">>></li> 421 <li class="inactive">>|</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
Note: See TracChangeset
for help on using the changeset viewer.