root/tags/1.3/wp-admin/includes/upload.php

Revision 1127, 13.4 kB (checked in by donncha, 1 year ago)

gettext the disable upload message
Remove mention of paid-upgrades.php, props momo360modena, fixes #470

Line 
1 <?php
2
3 function wp_upload_display( $dims = false, $href = '' ) {
4     global $post;
5     $id = get_the_ID();
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 );
14         }
15     }
16     if ( isset($attachment_data['width']) )
17         list($width,$height) = wp_shrink_dimensions($attachment_data['width'], $attachment_data['height'], 171, 128);
18
19     $post_title = attribute_escape( the_title( '', '', false ) );
20     $post_content = attribute_escape(apply_filters( 'content_edit_pre', $post->post_content ));
21
22     $class = 'text';
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 = '&nbsp;' . str_replace($image_src, $image_rel, $innerHTML);
27         $class = 'image';
28     }
29
30     $src_base = wp_get_attachment_url();
31     $src = wp_make_link_relative( $src_base );
32     $src_base = str_replace($src, '', $src_base);
33
34     if ( !trim($post_title) )
35         $post_title = basename($src);
36
37     $r = '';
38
39     if ( $href )
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";
43     if ( $href )
44         $r .= "</a>\n";
45     $size = @filesize($filesystem_path);
46     if ( !empty($size) )
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";
51
52     if ( !$thumb_base = wp_get_attachment_thumb_url() )
53         $thumb_base = wp_mime_type_icon();
54     if ( $thumb_base ) {
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";
59     }
60
61     $r .= "\t\t\t\t<input type='hidden' name='attachment-is-image-$id' id='attachment-is-image-$id' value='$is_image' />\n";
62
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";
66     }
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";
71     return $r;
72 }
73
74 function wp_upload_view() {
75     global $style, $post_id, $style;
76     $id = get_the_ID();
77     $attachment_data = wp_get_attachment_metadata( $id );
78 ?>
79     <div id="upload-file">
80         <div id="file-title">
81             <h2><?php if ( !isset($attachment_data['width']) && 'inline' != $style )
82                     echo "<a href='" . wp_get_attachment_url() . "' title='" . __('Direct link to file') . "'>";
83                 the_title();
84                 if ( !isset($attachment_data['width']) && 'inline' != $style )
85                     echo '</a>';
86             ?></h2>
87             <span><?php
88                 echo '[&nbsp;';
89                 echo '<a href="' . get_permalink() . '">' . __('view') . '</a>';
90                 echo '&nbsp;|&nbsp;';
91                     echo '<a href="' . clean_url(add_query_arg('action', 'edit')) . '" title="' . __('Edit this file') . '">' . __('edit') . '</a>';
92                 echo '&nbsp;|&nbsp;';
93                 echo '<a href="' . clean_url(remove_query_arg(array('action', 'ID'))) . '" title="' . __('Browse your files') . '">' . __('cancel') . '</a>';
94                 echo '&nbsp;]'; ?></span>
95         </div>
96
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 )
102             echo '</a>'; ?>
103         </div>
104         <?php the_attachment_links( $id ); ?>
105     </div>
106 <?php    echo "<form action='' id='browse-form'><input type='hidden' id='nonce-value' value='" . wp_create_nonce( 'inlineuploading' )  . "' /></form>\n";
107 }
108
109 function wp_upload_form() {
110     if ( defined('DISABLE_UPLOADS') && constant('DISABLE_UPLOADS') ) {
111         if ( defined('DISABLE_UPLOADS_MESSAGE') && constant('DISABLE_UPLOADS_MESSAGE') ) {
112             return print(__(DISABLE_UPLOADS_MESSAGE));
113         } else {
114             return print(__('Sorry, uploads are temporarily disabled.'));
115         }
116     }
117     $id = get_the_ID();
118     global $post_id, $tab, $style;
119     $enctype = $id ? '' : ' enctype="multipart/form-data"';
120     $post_id = (int) $post_id;
121 ?>
122     <form<?php echo $enctype; ?> id="upload-file" method="post" action="<?php echo get_option('siteurl') . '/wp-admin/upload.php?style=' . attribute_escape($style . '&amp;tab=upload&amp;post_id=' . $post_id); ?>">
123 <?php
124     if ( $id ) :
125         $attachment = get_post_to_edit( $id );
126         $attachment_data = wp_get_attachment_metadata( $id );
127 ?>
128         <div id="file-title">
129             <h2><?php if ( !isset($attachment_data['width']) && 'inline' != $style )
130                     echo "<a href='" . wp_get_attachment_url() . "' title='" . __('Direct link to file') . "'>";
131                 the_title();
132                 if ( !isset($attachment_data['width']) && 'inline' != $style )
133                     echo '</a>';
134             ?></h2>
135             <span><?php
136                 echo '[&nbsp;';
137                 echo '<a href="' . get_permalink() . '">' . __('view') . '</a>';
138                 echo '&nbsp;|&nbsp;';
139                     echo '<a href="' . clean_url(add_query_arg('action', 'view')) . '">' . __('links') . '</a>';
140                 echo '&nbsp;|&nbsp;';
141                 echo '<a href="' . clean_url(remove_query_arg(array('action','ID'))) . '" title="' . __('Browse your files') . '">' . __('cancel') . '</a>';
142                 echo '&nbsp;]'; ?></span>
143         </div>
144
145     <div id="upload-file-view" class="alignleft">
146 <?php        if ( isset($attachment_data['width']) && 'inline' != $style )
147             echo "<a href='" . wp_get_attachment_url() . "' title='" . __('Direct link to file') . "'>";
148         echo wp_upload_display( array(171, 128) );
149         if ( isset($attachment_data['width']) && 'inline' != $style )
150             echo '</a>'; ?>
151     </div>
152 <?php    endif; ?>
153         <table><col /><col class="widefat" />
154 <?php    if ( $id ): ?>
155             <tr>
156                 <th scope="row"><label for="url"><?php _e('URL'); ?></label></th>
157                 <td><input type="text" id="url" class="readonly" value="<?php echo wp_get_attachment_url(); ?>" readonly="readonly" /></td>
158             </tr>
159 <?php    else : ?>
160             <tr>
161                 <th scope="row"><label for="upload"><?php _e('File'); ?></label></th>
162                 <td><input type="file" id="upload" name="image" /></td>
163             </tr>
164 <?php    endif; ?>
165             <tr>
166                 <th scope="row"><label for="post_title"><?php _e('Title'); ?></label></th>
167                 <td><input type="text" id="post_title" name="post_title" value="<?php echo $attachment->post_title; ?>" /></td>
168             </tr>
169             <tr>
170                 <th scope="row"><label for="post_content"><?php _e('Description'); ?></label></th>
171                 <td><textarea name="post_content" id="post_content"><?php echo $attachment->post_content; ?></textarea></td>
172             </tr>
173             <tr id="buttons" class="submit">
174                 <td colspan='2'>
175 <?php    if ( $id ) : ?>
176                     <input type="submit" name="delete" id="delete" class="delete alignleft" value="<?php _e('Delete File'); ?>" />
177 <?php    else : ?>
178                     <p class="alignleft" style="margin:.25em 0; padding: 4px 0;"><?php display_space_usage(); ?></p>
179 <?php    endif; ?>
180                     <input type="hidden" name="from_tab" value="<?php echo $tab; ?>" />
181                     <input type="hidden" name="action" value="<?php echo $id ? 'save' : 'upload'; ?>" />
182 <?php    if ( $post_id ) : ?>
183                     <input type="hidden" name="post_id" value="<?php echo $post_id; ?>" />
184 <?php    endif; if ( $id ) : ?>
185                     <input type="hidden" name="ID" value="<?php echo $id; ?>" />
186 <?php    endif; ?>
187                     <?php wp_nonce_field( 'inlineuploading' ); ?>
188                     <div class="submit">
189                         <input type="submit" value="<?php $id ? _e('Save &raquo;') : _e('Upload &raquo;'); ?>" />
190                     </div>
191                 </td>
192             </tr>
193         </table>
194     </form>
195 <?php
196 }
197
198 function wp_upload_tab_upload() {
199     wp_upload_form();
200 }
201
202 function wp_upload_tab_upload_action() {
203     global $action;
204     if ( isset($_POST['delete']) )
205         $action = 'delete';
206
207     switch ( $action ) :
208     case 'upload' :
209         if ( defined('DISABLE_UPLOADS') && constant('DISABLE_UPLOADS') ) {
210             if ( defined('DISABLE_UPLOADS_MESSAGE') && constant('DISABLE_UPLOADS_MESSAGE') ) {
211                 wp_die(__(DISABLE_UPLOADS_MESSAGE));
212             } else {
213                 wp_die(__('Sorry, uploads are temporarily disabled.'));
214             }
215         }
216         global $from_tab, $post_id, $style;
217         if ( !$from_tab )
218             $from_tab = 'upload';
219
220         check_admin_referer( 'inlineuploading' );
221
222         global $post_id, $post_title, $post_content;
223
224         if ( !current_user_can( 'upload_files' ) )
225             wp_die( __('You are not allowed to upload files.')
226                 . " <a href='" . get_option('siteurl') . "/wp-admin/upload.php?style=" . attribute_escape($style . "&amp;tab=browse-all&amp;post_id=$post_id") . "'>"
227                 . __('Browse Files') . '</a>'
228             );
229
230         $overrides = array('action'=>'upload');
231
232         $file = wp_handle_upload($_FILES['image'], $overrides);
233
234         if ( isset($file['error']) )
235             wp_die($file['error'] . "<br /><a href='" . get_option('siteurl')
236             . "/wp-admin/upload.php?style=" . attribute_escape($style . "&amp;tab=$from_tab&amp;post_id=$post_id") . "'>" . __('Back to Image Uploading') . '</a>'
237         );
238
239         $url = $file['url'];
240         $type = $file['type'];
241         $file = $file['file'];
242         $filename = basename($file);
243
244         // Construct the attachment array
245         $attachment = array(
246             'post_title' => $post_title,
247             'post_content' => $post_content,
248             'post_type' => 'attachment',
249             'post_parent' => $post_id,
250             'post_mime_type' => $type,
251             'guid' => $url
252         );
253
254         // Save the data
255         $id = wp_insert_attachment($attachment, $file, $post_id);
256
257         wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
258
259         wp_redirect( get_option('siteurl') . "/wp-admin/upload.php?style=$style&tab=browse&action=view&ID=$id&post_id=$post_id");
260         die;
261         break;
262
263     case 'save' :
264         global $from_tab, $post_id, $style;
265         if ( !$from_tab )
266             $from_tab = 'upload';
267         check_admin_referer( 'inlineuploading' );
268
269         wp_update_post($_POST);
270         wp_redirect( get_option('siteurl') . "/wp-admin/upload.php?style=$style&tab=$from_tab&post_id=$post_id");
271         die;
272         break;
273
274     case 'delete' :
275         global $ID, $post_id, $from_tab, $style;
276         if ( !$from_tab )
277             $from_tab = 'upload';
278
279         check_admin_referer( 'inlineuploading' );
280
281         if ( !current_user_can('edit_post', (int) $ID) )
282             wp_die( __('You are not allowed to delete this attachment.')
283                 . " <a href='" . get_option('siteurl') . "/wp-admin/upload.php?style=" . attribute_escape($style . "&amp;tab=$from_tab&amp;post_id=$post_id") . "'>"
284                 . __('Go back') . '</a>'
285             );
286
287         wp_delete_attachment($ID);
288
289         wp_redirect( get_option('siteurl') . "/wp-admin/upload.php?style=$style&tab=$from_tab&post_id=$post_id" );
290         die;
291         break;
292
293     endswitch;
294 }
295
296 add_action( 'upload_files_upload', 'wp_upload_tab_upload_action' );
297
298 function wp_upload_grab_attachments( $obj ) {
299     $obj->is_attachment = true;
300 }
301
302 function wp_upload_posts_where( $where ) {
303     global $post_id;
304     return $where . " AND post_parent = '" . (int) $post_id . "'";
305 }
306
307 function wp_upload_tab_browse() {
308     global $action, $paged;
309     $old_vars = compact( 'paged' );
310
311     switch ( $action ) :
312     case 'edit' :
313     case 'view' :
314         global $ID;
315         $attachments = query_posts("attachment_id=$ID");
316         if ( have_posts() ) : while ( have_posts() ) : the_post();
317             'edit' == $action ? wp_upload_form() : wp_upload_view();
318         endwhile; endif;
319         break;
320     default :
321         global $tab, $post_id, $style;
322         add_action( 'pre_get_posts', 'wp_upload_grab_attachments' );
323         if ( 'browse' == $tab && $post_id )
324             add_filter( 'posts_where', 'wp_upload_posts_where' );
325         $attachments = query_posts("what_to_show=posts&post_status=any&posts_per_page=10&paged=$paged");
326
327         echo "<ul id='upload-files'>\n";
328         if ( have_posts() ) : while ( have_posts() ) : the_post();
329             $href = wp_specialchars( add_query_arg( array(
330                 'action' => 'inline' == $style ? 'view' : 'edit',
331                 'ID' => get_the_ID())
332              ), 1 );
333
334             echo "\t<li id='file-";
335             the_ID();
336             echo "' class='alignleft'>\n";
337             echo wp_upload_display( array(128,128), $href );
338             echo "\t</li>\n";
339         endwhile;
340         else :
341             echo "\t<li>" . __('There are no attachments to show.') . "</li>\n";
342         endif;
343         echo "</ul>\n\n";
344
345         echo "<form action='' id='browse-form'><input type='hidden' id='nonce-value' value='" . wp_create_nonce( 'inlineuploading' )  . "' /></form>\n";
346         break;
347     endswitch;
348
349     extract($old_vars);
350
351     echo "<div class='clear'></div>";
352 }
353
354
355 function wp_upload_tab_browse_action() {
356     global $style;
357     if ( 'inline' == $style )
358         wp_enqueue_script('upload');
359 }
360
361 add_action( 'upload_files_browse', 'wp_upload_tab_browse_action' );
362 add_action( 'upload_files_browse-all', 'wp_upload_tab_browse_action' );
363
364 function wp_upload_admin_head() {
365     wp_admin_css( 'css/upload' );
366     if ( 'inline' == @$_GET['style'] ) {
367         echo "<style type='text/css' media='screen'>\n";
368         echo "\t#upload-menu { position: absolute; z-index: 2; }\n";
369         echo "\tbody > #upload-menu { position: fixed; }\n";
370         echo "\t#upload-content { top: 2em; }\n";
371         echo "\t#upload-file { position: absolute; top: 15px; }\n";
372         echo "</style>";
373     }
374 }
375
376 ?>
377
Note: See TracBrowser for help on using the browser.