3 function wp_upload_display( $dims = false, $href = '' ) {
6 $attachment_data = wp_get_attachment_metadata( $id );
7 $is_image = (int) wp_attachment_is_image();
8 $filesystem_path = get_attached_file( $id );
9 if ( !isset($attachment_data['width']) && $is_image ) {
10 if ( $image_data = getimagesize( $filesystem_path ) ) {
11 $attachment_data['width'] = $image_data[0];
12 $attachment_data['height'] = $image_data[1];
13 wp_update_attachment_metadata( $id, $attachment_data );
16 if ( isset($attachment_data['width']) )
17 list($width,$height) = wp_shrink_dimensions($attachment_data['width'], $attachment_data['height'], 171, 128);
19 $post_title = attribute_escape( the_title( '', '', false ) );
20 $post_content = attribute_escape(apply_filters( 'content_edit_pre', $post->post_content ));
23 $innerHTML = get_attachment_innerHTML( $id, false, $dims );
24 if ( $image_src = get_attachment_icon_src() ) {
25 $image_rel = wp_make_link_relative($image_src);
26 $innerHTML = ' ' . str_replace($image_src, $image_rel, $innerHTML);
30 $src_base = wp_get_attachment_url();
31 $src = wp_make_link_relative( $src_base );
32 $src_base = str_replace($src, '', $src_base);
34 if ( !trim($post_title) )
35 $post_title = basename($src);
40 $r .= "<a id='file-link-$id' href='$href' title='$post_title' class='file-link $class'>\n";
41 if ( $href || $image_src )
42 $r .= "\t\t\t$innerHTML";
45 $size = @filesize($filesystem_path);
47 $r .= "\t\t\t\t<span class='upload-file-size'>".size_format($size)."</span>\n";
48 $r .= "\n\t\t<div class='upload-file-data'>\n\t\t\t<p>\n";
49 $r .= "\t\t\t\t<input type='hidden' name='attachment-url-$id' id='attachment-url-$id' value='$src' />\n";
50 $r .= "\t\t\t\t<input type='hidden' name='attachment-url-base-$id' id='attachment-url-base-$id' value='$src_base' />\n";
52 if ( !$thumb_base = wp_get_attachment_thumb_url() )
53 $thumb_base = wp_mime_type_icon();
55 $thumb_rel = wp_make_link_relative( $thumb_base );
56 $thumb_base = str_replace( $thumb_rel, '', $thumb_base );
57 $r .= "\t\t\t\t<input type='hidden' name='attachment-thumb-url-$id' id='attachment-thumb-url-$id' value='$thumb_rel' />\n";
58 $r .= "\t\t\t\t<input type='hidden' name='attachment-thumb-url-base-$id' id='attachment-thumb-url-base-$id' value='$thumb_base' />\n";
61 $r .= "\t\t\t\t<input type='hidden' name='attachment-is-image-$id' id='attachment-is-image-$id' value='$is_image' />\n";
63 if ( isset($width) ) {
64 $r .= "\t\t\t\t<input type='hidden' name='attachment-width-$id' id='attachment-width-$id' value='$width' />\n";
65 $r .= "\t\t\t\t<input type='hidden' name='attachment-height-$id' id='attachment-height-$id' value='$height' />\n";
67 $r .= "\t\t\t\t<input type='hidden' name='attachment-page-url-$id' id='attachment-page-url-$id' value='" . get_attachment_link( $id ) . "' />\n";
68 $r .= "\t\t\t\t<input type='hidden' name='attachment-title-$id' id='attachment-title-$id' value='$post_title' />\n";
69 $r .= "\t\t\t\t<input type='hidden' name='attachment-description-$id' id='attachment-description-$id' value='$post_content' />\n";
70 $r .= "\t\t\t</p>\n\t\t</div>\n";
74 function wp_upload_view() {
75 global $style, $post_id, $style;
77 $attachment_data = wp_get_attachment_metadata( $id );
79 <div id="upload-file">
81 <h2><?php if ( !isset($attachment_data['width']) && 'inline' != $style )
82 echo "<a href='" . wp_get_attachment_url() . "' title='" . __('Direct link to file') . "'>";
84 if ( !isset($attachment_data['width']) && 'inline' != $style )
89 echo '<a href="' . get_permalink() . '">' . __('view') . '</a>';
91 echo '<a href="' . clean_url(add_query_arg('action', 'edit')) . '" title="' . __('Edit this file') . '">' . __('edit') . '</a>';
93 echo '<a href="' . clean_url(remove_query_arg(array('action', 'ID'))) . '" title="' . __('Browse your files') . '">' . __('cancel') . '</a>';
94 echo ' ]'; ?></span>
97 <div id="upload-file-view" class="alignleft">
98 <?php if ( isset($attachment_data['width']) && 'inline' != $style )
99 echo "<a href='" . wp_get_attachment_url() . "' title='" . __('Direct link to file') . "'>";
100 echo wp_upload_display( array(171, 128) );
101 if ( isset($attachment_data['width']) && 'inline' != $style )
104 <?php the_attachment_links( $id ); ?>
106 <?php echo "<form action='' id='browse-form'><input type='hidden' id='nonce-value' value='" . wp_create_nonce( 'inlineuploading' ) . "' /></form>\n";
109 function wp_upload_form() {
111 global $post_id, $tab, $style;
112 $enctype = $id ? '' : ' enctype="multipart/form-data"';
113 $post_id = (int) $post_id;
115 <form<?php echo $enctype; ?> id="upload-file" method="post" action="<?php echo get_option('siteurl') . '/wp-admin/upload.php?style=' . attribute_escape($style . '&tab=upload&post_id=' . $post_id); ?>">
118 $attachment = get_post_to_edit( $id );
119 $attachment_data = wp_get_attachment_metadata( $id );
121 <div id="file-title">
122 <h2><?php if ( !isset($attachment_data['width']) && 'inline' != $style )
123 echo "<a href='" . wp_get_attachment_url() . "' title='" . __('Direct link to file') . "'>";
125 if ( !isset($attachment_data['width']) && 'inline' != $style )
130 echo '<a href="' . get_permalink() . '">' . __('view') . '</a>';
131 echo ' | ';
132 echo '<a href="' . clean_url(add_query_arg('action', 'view')) . '">' . __('links') . '</a>';
133 echo ' | ';
134 echo '<a href="' . clean_url(remove_query_arg(array('action','ID'))) . '" title="' . __('Browse your files') . '">' . __('cancel') . '</a>';
135 echo ' ]'; ?></span>
138 <div id="upload-file-view" class="alignleft">
139 <?php if ( isset($attachment_data['width']) && 'inline' != $style )
140 echo "<a href='" . wp_get_attachment_url() . "' title='" . __('Direct link to file') . "'>";
141 echo wp_upload_display( array(171, 128) );
142 if ( isset($attachment_data['width']) && 'inline' != $style )
146 <table><col /><col class="widefat" />
149 <th scope="row"><label for="url"><?php _e('URL'); ?></label></th>
150 <td><input type="text" id="url" class="readonly" value="<?php echo wp_get_attachment_url(); ?>" readonly="readonly" /></td>
154 <th scope="row"><label for="upload"><?php _e('File'); ?></label></th>
155 <td><input type="file" id="upload" name="image" /></td>
159 <th scope="row"><label for="post_title"><?php _e('Title'); ?></label></th>
160 <td><input type="text" id="post_title" name="post_title" value="<?php echo $attachment->post_title; ?>" /></td>
163 <th scope="row"><label for="post_content"><?php _e('Description'); ?></label></th>
164 <td><textarea name="post_content" id="post_content"><?php echo $attachment->post_content; ?></textarea></td>
166 <tr id="buttons" class="submit">
168 <?php if ( $id ) : ?>
169 <input type="submit" name="delete" id="delete" class="delete alignleft" value="<?php _e('Delete File'); ?>" />
171 <input type="hidden" name="from_tab" value="<?php echo $tab; ?>" />
172 <input type="hidden" name="action" value="<?php echo $id ? 'save' : 'upload'; ?>" />
173 <?php if ( $post_id ) : ?>
174 <input type="hidden" name="post_id" value="<?php echo $post_id; ?>" />
175 <?php endif; if ( $id ) : ?>
176 <input type="hidden" name="ID" value="<?php echo $id; ?>" />
178 <?php wp_nonce_field( 'inlineuploading' ); ?>
180 <input type="submit" value="<?php $id ? _e('Save »') : _e('Upload »'); ?>" />
189 function wp_upload_tab_upload() {
193 function wp_upload_tab_upload_action() {
195 if ( isset($_POST['delete']) )
200 global $from_tab, $post_id, $style;
202 $from_tab = 'upload';
204 check_admin_referer( 'inlineuploading' );
206 global $post_id, $post_title, $post_content;
208 if ( !current_user_can( 'upload_files' ) )
209 wp_die( __('You are not allowed to upload files.')
210 . " <a href='" . get_option('siteurl') . "/wp-admin/upload.php?style=" . attribute_escape($style . "&tab=browse-all&post_id=$post_id") . "'>"
211 . __('Browse Files') . '</a>'
214 $overrides = array('action'=>'upload');
216 $file = wp_handle_upload($_FILES['image'], $overrides);
218 if ( isset($file['error']) )
219 wp_die($file['error'] . "<br /><a href='" . get_option('siteurl')
220 . "/wp-admin/upload.php?style=" . attribute_escape($style . "&tab=$from_tab&post_id=$post_id") . "'>" . __('Back to Image Uploading') . '</a>'
224 $type = $file['type'];
225 $file = $file['file'];
226 $filename = basename($file);
228 // Construct the attachment array
230 'post_title' => $post_title,
231 'post_content' => $post_content,
232 'post_type' => 'attachment',
233 'post_parent' => $post_id,
234 'post_mime_type' => $type,
239 $id = wp_insert_attachment($attachment, $file, $post_id);
241 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
243 wp_redirect( get_option('siteurl') . "/wp-admin/upload.php?style=$style&tab=browse&action=view&ID=$id&post_id=$post_id");
248 global $from_tab, $post_id, $style;
250 $from_tab = 'upload';
251 check_admin_referer( 'inlineuploading' );
253 wp_update_post($_POST);
254 wp_redirect( get_option('siteurl') . "/wp-admin/upload.php?style=$style&tab=$from_tab&post_id=$post_id");
259 global $ID, $post_id, $from_tab, $style;
261 $from_tab = 'upload';
263 check_admin_referer( 'inlineuploading' );
265 if ( !current_user_can('edit_post', (int) $ID) )
266 wp_die( __('You are not allowed to delete this attachment.')
267 . " <a href='" . get_option('siteurl') . "/wp-admin/upload.php?style=" . attribute_escape($style . "&tab=$from_tab&post_id=$post_id") . "'>"
268 . __('Go back') . '</a>'
271 wp_delete_attachment($ID);
273 wp_redirect( get_option('siteurl') . "/wp-admin/upload.php?style=$style&tab=$from_tab&post_id=$post_id" );
280 add_action( 'upload_files_upload', 'wp_upload_tab_upload_action' );
282 function wp_upload_grab_attachments( $obj ) {
283 $obj->is_attachment = true;
286 function wp_upload_posts_where( $where ) {
288 return $where . " AND post_parent = '" . (int) $post_id . "'";
291 function wp_upload_tab_browse() {
292 global $action, $paged;
293 $old_vars = compact( 'paged' );
299 $attachments = query_posts("attachment_id=$ID");
300 if ( have_posts() ) : while ( have_posts() ) : the_post();
301 'edit' == $action ? wp_upload_form() : wp_upload_view();
305 global $tab, $post_id, $style;
306 add_action( 'pre_get_posts', 'wp_upload_grab_attachments' );
307 if ( 'browse' == $tab && $post_id )
308 add_filter( 'posts_where', 'wp_upload_posts_where' );
309 $attachments = query_posts("what_to_show=posts&post_status=any&posts_per_page=10&paged=$paged");
311 echo "<ul id='upload-files'>\n";
312 if ( have_posts() ) : while ( have_posts() ) : the_post();
313 $href = wp_specialchars( add_query_arg( array(
314 'action' => 'inline' == $style ? 'view' : 'edit',
315 'ID' => get_the_ID())
318 echo "\t<li id='file-";
320 echo "' class='alignleft'>\n";
321 echo wp_upload_display( array(128,128), $href );
325 echo "\t<li>" . __('There are no attachments to show.') . "</li>\n";
329 echo "<form action='' id='browse-form'><input type='hidden' id='nonce-value' value='" . wp_create_nonce( 'inlineuploading' ) . "' /></form>\n";
337 function wp_upload_tab_browse_action() {
339 if ( 'inline' == $style )
340 wp_enqueue_script('upload');
343 add_action( 'upload_files_browse', 'wp_upload_tab_browse_action' );
344 add_action( 'upload_files_browse-all', 'wp_upload_tab_browse_action' );
346 function wp_upload_admin_head() {
347 wp_admin_css( 'css/upload' );
348 if ( 'inline' == @$_GET['style'] ) {
349 echo "<style type='text/css' media='screen'>\n";
350 echo "\t#upload-menu { position: absolute; z-index: 2; }\n";
351 echo "\tbody > #upload-menu { position: fixed; }\n";
352 echo "\t#upload-content { top: 2em; }\n";
353 echo "\t#upload-file { position: absolute; top: 15px; }\n";