2005-11-12 23:40:18 -05:00
< ? php
require_once ( 'admin.php' );
if ( ! current_user_can ( 'edit_posts' ))
die ( __ ( 'You do not have permission to edit posts.' ));
2005-11-15 17:55:24 -05:00
$wpvarstoreset = array ( 'action' , 'post' , 'all' , 'last' , 'link' , 'sort' , 'start' , 'imgtitle' , 'descr' , 'attachment' , 'flickrtag' );
2005-11-12 23:40:18 -05:00
for ( $i = 0 ; $i < count ( $wpvarstoreset ); $i += 1 ) {
$wpvar = $wpvarstoreset [ $i ];
if ( ! isset ( $$wpvar )) {
if ( empty ( $_POST [ " $wpvar " ])) {
if ( empty ( $_GET [ " $wpvar " ])) {
$$wpvar = '' ;
} else {
$$wpvar = $_GET [ " $wpvar " ];
}
} else {
$$wpvar = $_POST [ " $wpvar " ];
}
}
}
$post = ( int ) $post ;
$images_width = 1 ;
function get_udims ( $width , $height ) {
if ( $height <= 96 && $width <= 128 )
return array ( $width , $height );
elseif ( $width / $height > 4 / 3 )
return array ( 128 , ( int ) ( $height / $width * 128 ));
else
return array (( int ) ( $width / $height * 96 ), 96 );
}
switch ( $action ) {
case 'delete' :
2005-11-15 17:55:24 -05:00
wp_delete_attachment ( $attachment );
2005-11-12 23:40:18 -05:00
header ( " Location: " . basename ( __FILE__ ) . " ?post= $post &all= $all &action=view&start= $start " );
die ;
case 'save' :
$overrides = array ( 'action' => 'save' );
$file = wp_handle_upload ( $_FILES [ 'image' ], $overrides );
if ( isset ( $file [ 'error' ]) )
die ( $file [ 'error' ] . '<a href="' . basename ( __FILE__ ) . '?action=upload&post="' . $post . '">Back to Image Uploading</a>' );
$url = $file [ 'url' ];
$file = $file [ 'file' ];
$filename = basename ( $file );
2005-11-15 17:55:24 -05:00
// Construct the attachment array
$attachment = array (
2005-11-12 23:40:18 -05:00
'post_title' => $imgtitle ? $imgtitle : $filename ,
'post_content' => $descr ,
2005-11-15 17:55:24 -05:00
'post_status' => 'attachment' ,
2005-11-12 23:40:18 -05:00
'post_parent' => $post ,
2005-11-15 17:55:24 -05:00
'post_mime_type' => $_FILES [ 'image' ][ 'type' ],
2005-11-12 23:40:18 -05:00
'guid' => $url
);
// Save the data
2005-11-15 17:55:24 -05:00
$id = wp_insert_attachment ( $attachment , $file , $post );
2005-11-12 23:40:18 -05:00
2005-11-15 17:55:24 -05:00
// Generate the attachment's postmeta.
2005-11-12 23:40:18 -05:00
$imagesize = getimagesize ( $file );
$imagedata [ 'width' ] = $imagesize [ '0' ];
$imagedata [ 'height' ] = $imagesize [ '1' ];
list ( $uwidth , $uheight ) = get_udims ( $imagedata [ 'width' ], $imagedata [ 'height' ]);
$imagedata [ 'hwstring_small' ] = " height=' $uheight ' width=' $uwidth ' " ;
$imagedata [ 'file' ] = $file ;
$imagedata [ 'thumb' ] = " thumb- $filename " ;
2005-11-18 04:25:47 -05:00
add_post_meta ( $id , '_wp_attachment_metadata' , $imagedata );
2005-11-12 23:40:18 -05:00
if ( $imagedata [ 'width' ] * $imagedata [ 'height' ] < 3 * 1024 * 1024 ) {
if ( $imagedata [ 'width' ] > 128 && $imagedata [ 'width' ] >= $imagedata [ 'height' ] * 4 / 3 )
2005-11-15 17:55:24 -05:00
$error = wp_create_thumbnail ( $file , 128 );
2005-11-12 23:40:18 -05:00
elseif ( $imagedata [ 'height' ] > 96 )
$error = wp_create_thumbnail ( $file , 96 );
}
header ( " Location: " . basename ( __FILE__ ) . " ?post= $post &all= $all &action=view&last=true " );
die ;
case 'upload' :
$current_1 = ' class="current"' ;
$back = $next = false ;
break ;
case 'view' :
// How many images do we show? How many do we query?
$num = 5 ;
$double = $num * 2 ;
if ( $post && empty ( $all ) ) {
$and_post = " AND post_parent = ' $post ' " ;
$current_2 = ' class="current"' ;
} else {
$current_3 = ' class="current"' ;
}
if ( $last )
2005-11-15 17:55:24 -05:00
$start = $wpdb -> get_var ( " SELECT count(ID) FROM $wpdb->posts WHERE post_status = 'attachment' AND left(post_mime_type, 5) = 'image' $and_post " ) - $num ;
2005-11-12 23:40:18 -05:00
else
$start = ( int ) $start ;
if ( $start < 0 )
$start = 0 ;
if ( '' == $sort )
2005-11-16 22:19:19 -05:00
$sort = " post_date_gmt DESC " ;
2005-11-12 23:40:18 -05:00
2005-11-15 17:55:24 -05:00
$images = $wpdb -> get_results ( " SELECT ID, post_date, post_title, guid FROM $wpdb->posts WHERE post_status = 'attachment' AND left(post_mime_type, 5) = 'image' $and_post ORDER BY $sort LIMIT $start , $double " , ARRAY_A );
2005-11-12 23:40:18 -05:00
if ( count ( $images ) > $num ) {
$next = $start + count ( $images ) - $num ;
} else {
$next = false ;
}
if ( $start > 0 ) {
$back = $start - $num ;
if ( $back < 1 )
$back = '0' ;
} else {
$back = false ;
}
$uwidth_sum = 0 ;
$images_html = '' ;
$images_style = '' ;
$images_script = '' ;
if ( count ( $images ) > 0 ) {
$images = array_slice ( $images , 0 , $num );
2005-11-16 22:19:19 -05:00
$__delete = __ ( 'Delete' );
$__attachment_on = __ ( 'Link to Page' );
$__attachment_off = __ ( 'Link to Image' );
$__thumbnail_on = __ ( 'Use Thumbnail' );
$__thumbnail_off = __ ( 'Use Full Image' );
$__no_thumbnail = __ ( '<del>No Thumbnail</del>' );
$__close = __ ( 'Close Options' );
2005-11-12 23:40:18 -05:00
$__confirmdelete = __ ( 'Delete this photo from the server?' );
$__nothumb = __ ( 'There is no thumbnail associated with this photo.' );
2005-11-15 17:55:24 -05:00
$images_script .= " attachmenton = ' $__attachment_on '; \n attachmentoff = ' $__attachment_off '; \n " ;
2005-11-12 23:40:18 -05:00
$images_script .= " thumbnailon = ' $__thumbnail_on '; \n thumbnailoff = ' $__thumbnail_off '; \n " ;
foreach ( $images as $key => $image ) {
2005-11-18 04:25:47 -05:00
$attachment_ID = $image [ 'ID' ];
$meta = get_post_meta ( $attachment_ID , '_wp_attachment_metadata' , true );
2005-11-12 23:40:18 -05:00
if ( ! is_array ( $meta )) {
2005-11-18 04:25:47 -05:00
$meta = get_post_meta ( $attachment_ID , 'imagedata' , true ); // Try 1.6 Alpha meta key
if ( ! is_array ( $meta )) {
continue ;
} else {
add_post_meta ( $attachment_ID , '_wp_attachment_metadata' , $meta );
}
2005-11-12 23:40:18 -05:00
}
$image = array_merge ( $image , $meta );
if ( ( $image [ 'width' ] > 128 || $image [ 'height' ] > 96 ) && ! empty ( $image [ 'thumb' ]) && file_exists ( dirname ( $image [ 'file' ]) . '/' . $image [ 'thumb' ]) ) {
$src = str_replace ( basename ( $image [ 'guid' ]), '' , $image [ 'guid' ]) . $image [ 'thumb' ];
2005-11-18 04:25:47 -05:00
$images_script .= " src " . $attachment_ID . " a = ' $src '; \n src " . $attachment_ID . " b = ' " . $image [ 'guid' ] . " '; \n " ;
2005-11-12 23:40:18 -05:00
$thumb = 'true' ;
$thumbtext = $__thumbnail_on ;
} else {
$src = $image [ 'guid' ];
$thumb = 'false' ;
$thumbtext = $__no_thumbnail ;
}
list ( $image [ 'uwidth' ], $image [ 'uheight' ]) = get_udims ( $image [ 'width' ], $image [ 'height' ]);
$height_width = 'height="' . $image [ 'uheight' ] . '" width="' . $image [ 'uwidth' ] . '"' ;
$uwidth_sum += 128 ;
$xpadding = ( 128 - $image [ 'uwidth' ]) / 2 ;
$ypadding = ( 96 - $image [ 'uheight' ]) / 2 ;
2005-11-18 04:25:47 -05:00
$images_style .= " #target { $attachment_ID } img { padding: { $ypadding } px { $xpadding } px; } \n " ;
$href = get_attachment_link ( $attachment_ID );
$images_script .= " href { $attachment_ID } a = ' $href '; \n href { $attachment_ID } b = ' { $image [ 'guid' ] } '; \n " ;
2005-11-12 23:40:18 -05:00
$images_html .= "
2005-11-18 04:25:47 -05:00
< div id = 'target{$attachment_ID}' class = 'imagewrap left' >
< div id = 'popup{$attachment_ID}' class = 'popup' >
< a id = \ " L { $attachment_ID } \" onclick= \" toggleLink( { $attachment_ID } );return false; \" href= \" javascript:void(); \" > $__attachment_on </a>
< a id = \ " I { $attachment_ID } \" onclick= \" if( $thumb )toggleImage( { $attachment_ID } );else alert(' $__nothumb ');return false; \" href= \" javascript:void(); \" > $thumbtext </a>
< a onclick = \ " return confirm(' $__confirmdelete ') \" href= \" " . basename ( __FILE__ ) . " ?action=delete&attachment= { $attachment_ID } &all= $all &start= $start &post= $post\ " > $__delete </ a >
2005-11-12 23:40:18 -05:00
< a onclick = \ " popup.style.display='none';return false; \" href= \" javascript:void() \" > $__close </a>
</ div >
2005-11-18 04:25:47 -05:00
< a id = \ " { $attachment_ID } \" rel= \" attachment \" class= \" imagelink \" href= \" $href\ " onclick = \ " imagePopup( { $attachment_ID } );return false; \" title= \" { $image [ 'post_title' ] } \" >
< img id = \ " image { $attachment_ID } \" src= \" $src\ " alt = \ " { $attachment_ID } \" $height_width />
2005-11-12 23:40:18 -05:00
</ a >
</ div >
" ;
}
}
$images_width = $uwidth_sum + ( count ( $images ) * 5 ) + 30 ;
break ;
default :
die ( 'This script was not meant to be called directly.' );
}
?>
<! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
< html xmlns = " http://www.w3.org/1999/xhtml " >
< head >
2005-11-14 04:45:13 -05:00
< meta http - equiv = " Content-Type " content = " <?php bloginfo('html_type'); ?>; charset=<?php echo get_settings('blog_charset'); ?> " />
2005-11-12 23:40:18 -05:00
< meta http - equiv = " imagetoolbar " content = " no " />
< script type = " text/javascript " >
/* Define any variables we'll need, such as alternate URLs. */
< ? php echo $images_script ; ?>
function validateImageName () {
/* This is more for convenience than security. Server-side validation is very thorough.*/
obj = document . getElementById ( 'upload' );
r = /. jpg $ |. gif $ |. png $ / i ;
if ( obj . value . match ( r ) )
return true ;
alert ( 'Please select a JPG, PNG or GIF file.' );
return false ;
}
function cancelUpload () {
o = document . getElementById ( 'uploadForm' );
o . method = 'GET' ;
o . action . value = 'view' ;
o . submit ();
}
function imagePopup ( i ) {
if ( popup )
popup . style . display = 'none' ;
target = document . getElementById ( 'target' + i );
popup = document . getElementById ( 'popup' + i );
//popup.style.top = (target.offsetTop + 3) + 'px';
popup . style . left = ( target . offsetLeft ) + 'px' ;
popup . style . display = 'block' ;
}
function init () {
popup = false ;
}
function toggleLink ( n ) {
2005-11-18 04:25:47 -05:00
o = document . getElementById ( n );
2005-11-12 23:40:18 -05:00
oi = document . getElementById ( 'L' + n );
2005-11-15 17:55:24 -05:00
if ( oi . innerHTML == attachmenton ) {
2005-11-12 23:40:18 -05:00
o . href = eval ( 'href' + n + 'b' );
2005-11-15 17:55:24 -05:00
oi . innerHTML = attachmentoff ;
2005-11-12 23:40:18 -05:00
} else {
o . href = eval ( 'href' + n + 'a' );
2005-11-15 17:55:24 -05:00
oi . innerHTML = attachmenton ;
2005-11-12 23:40:18 -05:00
}
}
function toggleImage ( n ) {
o = document . getElementById ( 'image' + n );
oi = document . getElementById ( 'I' + n );
if ( oi . innerHTML == thumbnailon ) {
o . src = eval ( 'src' + n + 'b' );
oi . innerHTML = thumbnailoff ;
} else {
o . src = eval ( 'src' + n + 'a' );
oi . innerHTML = thumbnailon ;
}
}
</ script >
< style type = " text/css " >
body {
2005-11-16 22:19:19 -05:00
font : 13 px " Lucida Grande " , " Lucida Sans Unicode " , Tahoma , Verdana ;
border : none ;
margin : 0 px ;
height : 150 px ;
background : #dfe8f1;
2005-11-12 23:40:18 -05:00
}
form {
2005-11-16 22:19:19 -05:00
margin : 6 px 2 px 0 px 6 px ;
2005-11-12 23:40:18 -05:00
}
#wrap {
2005-11-16 22:19:19 -05:00
clear : both ;
margin : 0 px ;
padding : 0 px ;
height : 133 px ;
width : 100 % ;
overflow : auto ;
2005-11-12 23:40:18 -05:00
}
#images {
2005-11-16 22:19:19 -05:00
clear : both ;
margin : 0 px ;
padding : 5 px 15 px ;
height : 96 px ;
white - space : nowrap ;
width : < ? php echo $images_width ; ?> px;
2005-11-12 23:40:18 -05:00
}
#images img {
2005-11-16 22:19:19 -05:00
background - color : rgb ( 209 , 226 , 239 );
2005-11-12 23:40:18 -05:00
}
< ? php echo $images_style ; ?>
. imagewrap {
2005-11-16 22:19:19 -05:00
margin - right : 5 px ;
height : 96 px ;
overflow : hidden ;
2005-11-12 23:40:18 -05:00
}
. imagewrap * {
2005-11-16 22:19:19 -05:00
margin : 0 px ;
padding : 0 px ;
border : 0 px ;
2005-11-12 23:40:18 -05:00
}
. imagewrap a , . imagewrap a img , . imagewrap a : hover img , . imagewrap a : visited img , . imagewrap a : active img {
2005-11-16 22:19:19 -05:00
text - decoration : none ;
float : left ;
text - align : center ;
2005-11-12 23:40:18 -05:00
}
2005-11-16 22:19:19 -05:00
#upload-menu {
background : #fff;
margin : 0 ;
padding : 0 ;
list - style : none ;
height : 2 em ;
border - bottom : 1 px solid #448abd;
}
#upload-menu li {
float : left ;
margin : 0 0 0 1 em ;
}
#upload-menu a {
display : block ;
padding : 5 px ;
text - decoration : none ;
color : #000;
border - top : 3 px solid #fff;
}
#upload-menu .current a {
background : #dfe8f1;
border - right : 2 px solid #448abd;
}
#upload-menu a:hover {
background : #dfe8f1;
color : #000;
}
2005-11-12 23:40:18 -05:00
. tip {
2005-11-16 22:19:19 -05:00
color : rgb ( 68 , 138 , 189 );
padding : 2 px 1 em ;
2005-11-12 23:40:18 -05:00
}
. inactive {
2005-11-16 22:19:19 -05:00
color : #fff;
padding : 1 px 3 px ;
2005-11-12 23:40:18 -05:00
}
. left {
2005-11-16 22:19:19 -05:00
float : left ;
2005-11-12 23:40:18 -05:00
}
. right {
2005-11-16 22:19:19 -05:00
float : right ;
2005-11-12 23:40:18 -05:00
}
. center {
2005-11-16 22:19:19 -05:00
text - align : center ;
2005-11-12 23:40:18 -05:00
}
2005-11-16 22:19:19 -05:00
#upload-menu li.spacer {
margin - left : 40 px ;
2005-11-12 23:40:18 -05:00
}
2005-11-16 22:19:19 -05:00
2005-11-12 23:40:18 -05:00
#title, #descr {
2005-11-16 22:19:19 -05:00
width : 80 % ;
margin - top : 2 px ;
2005-11-12 23:40:18 -05:00
}
#descr {
2005-11-16 22:19:19 -05:00
height : 35 px ;
v - align : top ;
2005-11-12 23:40:18 -05:00
}
#buttons {
2005-11-16 22:19:19 -05:00
width : 98 % ;
margin - top : 2 px ;
text - align : right ;
2005-11-12 23:40:18 -05:00
}
. popup {
2005-11-16 22:19:19 -05:00
margin : 4 px 4 px ;
padding : 3 px ;
position : absolute ;
width : 114 px ;
height : 82 px ;
display : none ;
background - color : rgb ( 223 , 232 , 241 );
opacity : . 90 ;
filter : alpha ( opacity = 90 );
text - align : center ;
2005-11-12 23:40:18 -05:00
}
. popup a , . popup a : visited , . popup a : active {
2005-11-16 22:19:19 -05:00
background - color : transparent ;
display : block ;
width : 100 % ;
text - decoration : none ;
color : #246;
2005-11-12 23:40:18 -05:00
}
. popup a : hover {
2005-11-16 22:19:19 -05:00
background - color : #fff;
color : #000;
2005-11-12 23:40:18 -05:00
}
</ style >
</ head >
< body onload = " init() " >
2005-11-16 22:19:19 -05:00
< ul id = " upload-menu " >
< li < ? php echo $current_1 ; ?> ><a href="<?php echo basename(__FILE__); ?>?action=upload&post=<?php echo $post; ?>&all=<?php echo $all; ?>"><?php _e('Upload Image'); ?></a></li>
< ? php if ( $attachments = $wpdb -> get_results ( " SELECT ID FROM $wpdb->posts WHERE post_parent = ' $post ' " ) ) { ?>
< li < ? php echo $current_2 ; ?> ><a href="<?php echo basename(__FILE__); ?>?action=view&post=<?php echo $post; ?>"><?php _e('Attached Images'); ?></a></li>
< ? php } ?>
< li < ? php echo $current_3 ; ?> ><a href="<?php echo basename(__FILE__); ?>?action=view&post=<?php echo $post; ?>&all=true"><?php _e('All Images'); ?></a></li>
2005-11-12 23:40:18 -05:00
< li > </ li >
2005-11-16 22:19:19 -05:00
< ? php if ( $action != 'upload' ) { ?>
2005-11-12 23:40:18 -05:00
< ? php if ( false !== $back ) : ?>
2005-11-16 22:19:19 -05:00
< 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'); ?> " >|& laquo ; </ a ></ li >
< li >< a href = " <?php echo basename(__FILE__); ?>?action=<?php echo $action ; ?>&post=<?php echo $post ; ?>&all=<?php echo $all ; ?>&start=<?php echo $back ; ?> " " >« <?php _e('Back'); ?></a></li>
2005-11-12 23:40:18 -05:00
< ? php else : ?>
< li class = " inactive spacer " >|& lt ; </ li >
< li class = " inactive " >& lt ; & lt ; </ li >
< ? php endif ; ?>
< ? php if ( false !== $next ) : ?>
2005-11-16 22:19:19 -05:00
< li >< a href = " <?php echo basename(__FILE__); ?>?action=<?php echo $action ; ?>&post=<?php echo $post ; ?>&all=<?php echo $all ; ?>&start=<?php echo $next ; ?> " >< ? php _e ( 'Next' ); ?> »</a></li>
< 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'); ?> " >& raquo ; |</ a ></ li >
2005-11-12 23:40:18 -05:00
< ? php else : ?>
< li class = " inactive " >& gt ; & gt ; </ li >
< li class = " inactive " >& gt ; |</ li >
< ? php endif ; ?>
2005-11-16 22:19:19 -05:00
< ? php } // endif not upload?>
2005-11-12 23:40:18 -05:00
</ ul >
< ? php if ( $action == 'view' ) : ?>
2005-11-16 22:19:19 -05:00
< span class = " left tip " >< ? php _e ( 'You can drag and drop these photos into your post. Click on the thumbnail for more options.' ); ?> </span>
< span class = " right tip " ></ span >
2005-11-12 23:40:18 -05:00
< div id = " wrap " >
< div id = " images " >
< ? php echo $images_html ; ?>
</ div >
</ div >
< ? php elseif ( $action == 'upload' ) : ?>
< div class = " tip " ></ div >
< form enctype = " multipart/form-data " id = " uploadForm " method = " POST " action = " <?php echo basename(__FILE__); ?> " onsubmit = " return validateImageName() " >
2005-11-16 22:19:19 -05:00
< table style = " width: 100% " >
< tr >
< th scope = " row " style = " width: 6em; text-align: right; " >< label for = " upload " >< ? php _e ( 'Image:' ); ?> </label></th>
< td >< input type = " file " id = " upload " name = " image " onchange = " validateImageName() " /></ td >
</ tr >
< tr >
< th scope = " row " style = " text-align: right; " >< label for = " title " >< ? php _e ( 'Title:' ); ?> </label></th>
< td >< input type = " text " id = " title " name = " imgtitle " /></ td >
</ tr >
< tr >
< th scope = " row " style = " text-align: right; " >< label for = " descr " >< ? php _e ( 'Description:' ); ?> </th>
< td >< input type = " textarea " name = " descr " id = " descr " value = " " /></ td >
</ tr >
</ table >
< p class = " submit " >
2005-11-12 23:40:18 -05:00
< input type = " hidden " name = " action " value = " save " />
< input type = " hidden " name = " post " value = " <?php echo $post ; ?> " />
< input type = " hidden " name = " all " value = " <?php echo $all ; ?> " />
< input type = " submit " value = " <?php _e('Upload'); ?> " />
< input type = " button " value = " <?php _e('Cancel'); ?> " onclick = " cancelUpload() " />
</ div >
</ form >
< ? php endif ; ?>
</ body >
</ html >