source: trunk/www.guidonia.net/wp/wp-admin/upload.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 14 years ago
File size: 15.2 KB
Line 
1<?php
2/**
3 * Media Library administration panel.
4 *
5 * @package WordPress
6 * @subpackage Administration
7 */
8
9/** WordPress Administration Bootstrap */
10require_once('admin.php');
11wp_enqueue_script( 'wp-ajax-response' );
12wp_enqueue_script( 'jquery-ui-draggable' );
13
14if (!current_user_can('upload_files'))
15 wp_die(__('You do not have permission to upload files.'));
16
17if ( isset($_GET['find_detached'] ) ) {
18 check_admin_referer('bulk-media');
19
20 if ( ! current_user_can('edit_posts') )
21 wp_die( __('You are not allowed to scan for lost attachments.') );
22
23 $all_posts = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_type = 'post' OR post_type = 'page'");
24 $all_att = $wpdb->get_results("SELECT ID, post_parent FROM $wpdb->posts WHERE post_type = 'attachment'");
25
26 $lost = array();
27 foreach ( (array) $all_att as $att ) {
28 if ( $att->post_parent > 0 && ! in_array($att->post_parent, $all_posts) )
29 $lost[] = $att->ID;
30 }
31 $_GET['detached'] = 1;
32
33} elseif ( isset($_GET['found_post_id']) && isset($_GET['media']) ) {
34 check_admin_referer('bulk-media');
35
36 if ( ! ( $parent_id = (int) $_GET['found_post_id'] ) )
37 return;
38
39 $parent = &get_post($parent_id);
40 if ( !current_user_can('edit_post', $parent_id) )
41 wp_die( __('You are not allowed to edit this post.') );
42
43 $attach = array();
44 foreach( (array) $_GET['media'] as $att_id ) {
45 $att_id = (int) $att_id;
46
47 if ( !current_user_can('edit_post', $att_id) )
48 continue;
49
50 $attach[] = $att_id;
51 }
52
53 if ( ! empty($attach) ) {
54 $attach = implode(',', $attach);
55 $attached = $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_parent = %d WHERE post_type = 'attachment' AND ID IN ($attach)", $parent_id) );
56 }
57
58 if ( isset($attached) ) {
59 $location = 'upload.php';
60 if ( $referer = wp_get_referer() ) {
61 if ( false !== strpos($referer, 'upload.php') )
62 $location = $referer;
63 }
64
65 $location = add_query_arg( array( 'detached' => 1, 'attached' => $attached ) , $location );
66 wp_redirect($location);
67 exit;
68 }
69
70} elseif ( isset($_GET['action']) && isset($_GET['media']) && ( -1 != $_GET['action'] || -1 != $_GET['action2'] ) ) {
71 check_admin_referer('bulk-media');
72 $doaction = ( -1 != $_GET['action'] ) ? $_GET['action'] : $_GET['action2'];
73
74 if ( 'delete' == $doaction ) {
75 foreach( (array) $_GET['media'] as $post_id_del ) {
76 $post_del = & get_post($post_id_del);
77
78 if ( !current_user_can('delete_post', $post_id_del) )
79 wp_die( __('You are not allowed to delete this post.') );
80
81 if ( $post_del->post_type == 'attachment' )
82 if ( ! wp_delete_attachment($post_id_del) )
83 wp_die( __('Error in deleting...') );
84 }
85
86 $location = 'upload.php';
87 if ( $referer = wp_get_referer() ) {
88 if ( false !== strpos($referer, 'upload.php') )
89 $location = $referer;
90 }
91
92 $location = add_query_arg('message', 2, $location);
93 $location = remove_query_arg('posted', $location);
94 wp_redirect($location);
95 exit;
96 }
97} elseif ( isset($_GET['_wp_http_referer']) && ! empty($_GET['_wp_http_referer']) ) {
98 wp_redirect( remove_query_arg( array('_wp_http_referer', '_wpnonce'), stripslashes($_SERVER['REQUEST_URI']) ) );
99 exit;
100}
101
102$title = __('Media Library');
103$parent_file = 'upload.php';
104
105if ( ! isset( $_GET['paged'] ) || $_GET['paged'] < 1 )
106 $_GET['paged'] = 1;
107
108if ( isset($_GET['detached']) ) {
109
110 if ( !empty($lost) ) {
111 $start = ( $_GET['paged'] - 1 ) * 50;
112 $page_links_total = ceil(count($lost) / 50);
113 $lost = implode(',', $lost);
114
115 $orphans = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE post_type = 'attachment' AND ID IN ($lost) LIMIT $start, 50" );
116 } else {
117 $start = ( $_GET['paged'] - 1 ) * 25;
118 $orphans = $wpdb->get_results( "SELECT SQL_CALC_FOUND_ROWS * FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent < 1 LIMIT $start, 25" );
119 $page_links_total = ceil($wpdb->get_var( "SELECT FOUND_ROWS()" ) / 25);
120 }
121
122 $post_mime_types = array(
123 'image' => array(__('Images'), __('Manage Images'), _n_noop('Image (%s)', 'Images (%s)')),
124 'audio' => array(__('Audio'), __('Manage Audio'), _n_noop('Audio (%s)', 'Audio (%s)')),
125 'video' => array(__('Video'), __('Manage Video'), _n_noop('Video (%s)', 'Video (%s)')),
126 );
127 $post_mime_types = apply_filters('post_mime_types', $post_mime_types);
128
129 $avail_post_mime_types = get_available_post_mime_types('attachment');
130
131 if ( isset($_GET['post_mime_type']) && !array_intersect( (array) $_GET['post_mime_type'], array_keys($post_mime_types) ) )
132 unset($_GET['post_mime_type']);
133
134} else {
135 list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
136}
137
138wp_enqueue_script('media');
139require_once('admin-header.php'); ?>
140
141<?php
142if ( isset($_GET['posted']) && (int) $_GET['posted'] ) {
143 $_GET['message'] = '1';
144 $_SERVER['REQUEST_URI'] = remove_query_arg(array('posted'), $_SERVER['REQUEST_URI']);
145}
146
147if ( isset($_GET['attached']) && (int) $_GET['attached'] ) {
148 $attached = (int) $_GET['attached'];
149 $message = sprintf( _n('Reattached %d attachment', 'Reattached %d attachments', $attached), $attached );
150 $_SERVER['REQUEST_URI'] = remove_query_arg(array('attached'), $_SERVER['REQUEST_URI']);
151}
152
153$messages[1] = __('Media attachment updated.');
154$messages[2] = __('Media deleted.');
155$messages[3] = __('Error saving media attachment.');
156
157if ( isset($_GET['message']) && (int) $_GET['message'] ) {
158 $message = $messages[$_GET['message']];
159 $_SERVER['REQUEST_URI'] = remove_query_arg(array('message'), $_SERVER['REQUEST_URI']);
160}
161?>
162
163<?php do_action('restrict_manage_posts'); ?>
164
165<div class="wrap">
166<?php screen_icon(); ?>
167<h2><?php echo esc_html( $title );
168if ( isset($_GET['s']) && $_GET['s'] )
169 printf( '<span class="subtitle">' . __('Search results for &#8220;%s&#8221;') . '</span>', esc_html( get_search_query() ) ); ?>
170</h2>
171
172<?php
173if ( isset($message) ) { ?>
174<div id="message" class="updated fade"><p><?php echo $message; ?></p></div>
175<?php
176}
177?>
178
179<ul class="subsubsub">
180<?php
181$type_links = array();
182$_num_posts = (array) wp_count_attachments();
183$_total_posts = array_sum( $_num_posts );
184$matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
185foreach ( $matches as $type => $reals )
186 foreach ( $reals as $real )
187 $num_posts[$type] = ( isset( $num_posts[$type] ) ) ? $num_posts[$type] + $_num_posts[$real] : $_num_posts[$real];
188
189$class = empty($_GET['post_mime_type']) && ! isset($_GET['detached']) ? ' class="current"' : '';
190$type_links[] = "<li><a href='upload.php'$class>" . sprintf( _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $_total_posts, 'uploaded files' ), number_format_i18n( $_total_posts ) ) . '</a>';
191foreach ( $post_mime_types as $mime_type => $label ) {
192 $class = '';
193
194 if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
195 continue;
196
197 if ( !empty($_GET['post_mime_type']) && wp_match_mime_types($mime_type, $_GET['post_mime_type']) )
198 $class = ' class="current"';
199
200 $type_links[] = "<li><a href='upload.php?post_mime_type=$mime_type'$class>" . sprintf( _n( $label[2][0], $label[2][1], $num_posts[$mime_type] ), number_format_i18n( $num_posts[$mime_type] )) . '</a>';
201}
202$class = isset($_GET['detached']) ? ' class="current"' : '';
203$type_links[] = '<li><a href="upload.php?detached=1"' . $class . '>' . __('Unattached') . '</a>';
204
205echo implode( " |</li>\n", $type_links) . '</li>';
206unset($type_links);
207?>
208</ul>
209
210<form class="search-form" action="" method="get">
211<p class="search-box">
212 <label class="screen-reader-text" for="media-search-input"><?php _e( 'Search Media' ); ?>:</label>
213 <input type="text" id="media-search-input" name="s" value="<?php the_search_query(); ?>" />
214 <input type="submit" value="<?php esc_attr_e( 'Search Media' ); ?>" class="button" />
215</p>
216</form>
217
218<form id="posts-filter" action="" method="get">
219<div class="tablenav">
220<?php
221if ( ! isset($page_links_total) )
222 $page_links_total = $wp_query->max_num_pages;
223
224$page_links = paginate_links( array(
225 'base' => add_query_arg( 'paged', '%#%' ),
226 'format' => '',
227 'prev_text' => __('&laquo;'),
228 'next_text' => __('&raquo;'),
229 'total' => $page_links_total,
230 'current' => $_GET['paged']
231));
232
233if ( $page_links ) : ?>
234<div class="tablenav-pages"><?php $page_links_text = sprintf( '<span class="displaying-num">' . __( 'Displaying %s&#8211;%s of %s' ) . '</span>%s',
235 number_format_i18n( ( $_GET['paged'] - 1 ) * $wp_query->query_vars['posts_per_page'] + 1 ),
236 number_format_i18n( min( $_GET['paged'] * $wp_query->query_vars['posts_per_page'], $wp_query->found_posts ) ),
237 number_format_i18n( $wp_query->found_posts ),
238 $page_links
239); echo $page_links_text; ?></div>
240<?php endif; ?>
241
242<div class="alignleft actions">
243<select name="action" class="select-action">
244<option value="-1" selected="selected"><?php _e('Bulk Actions'); ?></option>
245<option value="delete"><?php _e('Delete'); ?></option>
246<?php if ( isset($orphans) ) { ?>
247<option value="attach"><?php _e('Attach to a post'); ?></option>
248<?php } ?>
249</select>
250<input type="submit" value="<?php esc_attr_e('Apply'); ?>" name="doaction" id="doaction" class="button-secondary action" />
251<?php wp_nonce_field('bulk-media'); ?>
252
253<?php
254if ( ! is_singular() && ! isset($_GET['detached']) ) {
255 $arc_query = "SELECT DISTINCT YEAR(post_date) AS yyear, MONTH(post_date) AS mmonth FROM $wpdb->posts WHERE post_type = 'attachment' ORDER BY post_date DESC";
256
257 $arc_result = $wpdb->get_results( $arc_query );
258
259 $month_count = count($arc_result);
260
261 if ( $month_count && !( 1 == $month_count && 0 == $arc_result[0]->mmonth ) ) : ?>
262<select name='m'>
263<option<?php selected( @$_GET['m'], 0 ); ?> value='0'><?php _e('Show all dates'); ?></option>
264<?php
265foreach ($arc_result as $arc_row) {
266 if ( $arc_row->yyear == 0 )
267 continue;
268 $arc_row->mmonth = zeroise( $arc_row->mmonth, 2 );
269
270 if ( $arc_row->yyear . $arc_row->mmonth == $_GET['m'] )
271 $default = ' selected="selected"';
272 else
273 $default = '';
274
275 echo "<option$default value='" . esc_attr("$arc_row->yyear$arc_row->mmonth") . "'>";
276 echo $wp_locale->get_month($arc_row->mmonth) . " $arc_row->yyear";
277 echo "</option>\n";
278}
279?>
280</select>
281<?php endif; // month_count ?>
282
283<input type="submit" id="post-query-submit" value="<?php esc_attr_e('Filter'); ?>" class="button-secondary" />
284
285<?php } // ! is_singular ?>
286
287<?php if ( isset($_GET['detached']) ) { ?>
288 <input type="submit" id="find_detached" name="find_detached" value="<?php esc_attr_e('Scan for lost attachments'); ?>" class="button-secondary" />
289<?php } ?>
290
291</div>
292
293<br class="clear" />
294</div>
295
296<div class="clear"></div>
297
298<?php if ( isset($orphans) ) { ?>
299<table class="widefat" cellspacing="0">
300<thead>
301<tr>
302 <th scope="col" class="check-column"><input type="checkbox" /></th>
303 <th scope="col"></th>
304 <th scope="col"><?php /* translators: column name in media */ echo _x('Media', 'media column name'); ?></th>
305 <th scope="col"><?php /* translators: column name in media */ echo _x('Author', 'media column name'); ?></th>
306 <th scope="col"><?php /* translators: column name in media */ echo _x('Date Added', 'media column name'); ?></th>
307</tr>
308</thead>
309
310<tfoot>
311<tr>
312 <th scope="col" class="check-column"><input type="checkbox" /></th>
313 <th scope="col"></th>
314 <th scope="col"><?php /* translators: column name in media */ echo _x('Media', 'media column name'); ?></th>
315 <th scope="col"><?php /* translators: column name in media */ echo _x('Author', 'media column name'); ?></th>
316 <th scope="col"><?php /* translators: column name in media */ echo _x('Date Added', 'media column name'); ?></th>
317</tr>
318</tfoot>
319
320<tbody id="the-list" class="list:post">
321<?php
322 if ( $orphans ) {
323 foreach ( $orphans as $post ) {
324 $class = 'alternate' == $class ? '' : 'alternate';
325 $att_title = esc_html( _draft_or_post_title($post->ID) );
326?>
327 <tr id='post-<?php echo $post->ID; ?>' class='<?php echo $class; ?>' valign="top">
328 <th scope="row" class="check-column"><input type="checkbox" name="media[]" value="<?php echo esc_attr($post->ID); ?>" /></th>
329
330 <td class="media-icon"><?php
331 if ( $thumb = wp_get_attachment_image( $post->ID, array(80, 60), true ) ) { ?>
332 <a href="media.php?action=edit&amp;attachment_id=<?php echo $post->ID; ?>" title="<?php echo esc_attr(sprintf(__('Edit &#8220;%s&#8221;'), $att_title)); ?>"><?php echo $thumb; ?></a>
333<?php } ?></td>
334
335 <td class="media column-media"><strong><a href="<?php echo get_edit_post_link( $post->ID ); ?>" title="<?php echo esc_attr(sprintf(__('Edit &#8220;%s&#8221;'), $att_title)); ?>"><?php echo $att_title; ?></a></strong><br />
336 <?php echo strtoupper(preg_replace('/^.*?\.(\w+)$/', '$1', get_attached_file($post->ID))); ?>
337
338 <div class="row-actions">
339 <?php
340 $actions = array();
341 if ( current_user_can('edit_post', $post->ID) )
342 $actions['edit'] = '<a href="' . get_edit_post_link($post->ID, true) . '">' . __('Edit') . '</a>';
343 if ( current_user_can('delete_post', $post->ID) )
344 $actions['delete'] = "<a class='submitdelete' href='" . wp_nonce_url("post.php?action=delete&amp;post=$post->ID", 'delete-post_' . $post->ID) . "' onclick=\"if ( confirm('" . esc_js(sprintf( ('draft' == $post->post_status) ? __("You are about to delete this attachment '%s'\n 'Cancel' to stop, 'OK' to delete.") : __("You are about to delete this attachment '%s'\n 'Cancel' to stop, 'OK' to delete."), $post->post_title )) . "') ) { return true;}return false;\">" . __('Delete') . "</a>";
345 $actions['view'] = '<a href="' . get_permalink($post->ID) . '" title="' . esc_attr(sprintf(__('View &#8220;%s&#8221;'), $title)) . '" rel="permalink">' . __('View') . '</a>';
346 if ( current_user_can('edit_post', $post->ID) )
347 $actions['attach'] = '<a href="#the-list" onclick="findPosts.open(\'media[]\',\''.$post->ID.'\');return false;">'.__('Attach').'</a>';
348 $actions = apply_filters( 'media_row_actions', $actions, $post );
349 $action_count = count($actions);
350 $i = 0;
351 foreach ( $actions as $action => $link ) {
352 ++$i;
353 ( $i == $action_count ) ? $sep = '' : $sep = ' | ';
354 echo "<span class='$action'>$link$sep</span>";
355 } ?>
356 </div></td>
357 <td class="author column-author"><?php $author = get_userdata($post->post_author); echo $author->display_name; ?></td>
358<?php if ( '0000-00-00 00:00:00' == $post->post_date && 'date' == $column_name ) {
359 $t_time = $h_time = __('Unpublished');
360 } else {
361 $t_time = get_the_time(__('Y/m/d g:i:s A'));
362 $m_time = $post->post_date;
363 $time = get_post_time( 'G', true );
364 if ( ( abs($t_diff = time() - $time) ) < 86400 ) {
365 if ( $t_diff < 0 )
366 $h_time = sprintf( __('%s from now'), human_time_diff( $time ) );
367 else
368 $h_time = sprintf( __('%s ago'), human_time_diff( $time ) );
369 } else {
370 $h_time = mysql2date(__('Y/m/d'), $m_time);
371 }
372 } ?>
373 <td class="date column-date"><?php echo $h_time ?></td>
374 </tr>
375<?php }
376
377 } else { ?>
378 <tr><td colspan="5"><?php _e('No media attachments found.') ?></td></tr>
379<?php } ?>
380</tbody>
381</table>
382
383<?php find_posts_div();
384
385} else {
386 include( 'edit-attachment-rows.php' );
387} ?>
388
389<div id="ajax-response"></div>
390
391<div class="tablenav">
392
393<?php
394if ( $page_links )
395 echo "<div class='tablenav-pages'>$page_links_text</div>";
396?>
397
398<div class="alignleft actions">
399<select name="action2" class="select-action">
400<option value="-1" selected="selected"><?php _e('Bulk Actions'); ?></option>
401<option value="delete"><?php _e('Delete'); ?></option>
402<?php if ( isset($orphans) ) { ?>
403<option value="attach"><?php _e('Attach to a post'); ?></option>
404<?php } ?>
405</select>
406<input type="submit" value="<?php esc_attr_e('Apply'); ?>" name="doaction2" id="doaction2" class="button-secondary action" />
407</div>
408
409<br class="clear" />
410</div>
411</form>
412<br class="clear" />
413
414</div>
415
416<?php
417include('admin-footer.php');
Note: See TracBrowser for help on using the repository browser.