[ Index ]

PHP Cross Reference of Wordpress 2.7.1

title

Body

[close]

/wp-admin/ -> upload.php (source)

   1  <?php
   2  /**
   3   * Media Library administration panel.
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   */
   8  
   9  /** WordPress Administration Bootstrap */
  10  require_once ('admin.php');
  11  wp_enqueue_script( 'wp-ajax-response' );
  12  wp_enqueue_script( 'jquery-ui-draggable' );
  13  wp_enqueue_script( 'jquery-ui-resizable' );
  14  
  15  if (!current_user_can('upload_files'))
  16      wp_die(__('You do not have permission to upload files.'));
  17  
  18  if ( isset($_GET['find_detached'] ) ) {
  19      check_admin_referer('bulk-media');
  20  
  21      if ( ! current_user_can('edit_posts') )
  22          wp_die( __('You are not allowed to scan for lost attachments.') );
  23  
  24      $all_posts = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_type = 'post' OR post_type = 'page'");
  25      $all_att = $wpdb->get_results("SELECT ID, post_parent FROM $wpdb->posts WHERE post_type = 'attachment'");
  26  
  27      $lost = array();
  28      foreach ( (array) $all_att as $att ) {
  29          if ( $att->post_parent > 0 && ! in_array($att->post_parent, $all_posts) )
  30              $lost[] = $att->ID;
  31      }
  32      $_GET['detached'] = 1;
  33  
  34  } elseif ( isset($_GET['found_post_id']) && isset($_GET['media']) ) {
  35      check_admin_referer('bulk-media');
  36  
  37      if ( ! ( $parent_id = (int) $_GET['found_post_id'] ) )
  38          return;
  39  
  40      $parent = &get_post($parent_id);
  41      if ( !current_user_can('edit_post', $parent_id) )
  42          wp_die( __('You are not allowed to edit this post.') );
  43  
  44      $attach = array();
  45      foreach( (array) $_GET['media'] as $att_id ) {
  46          $att_id = (int) $att_id;
  47  
  48          if ( !current_user_can('edit_post', $att_id) )
  49              continue;
  50  
  51          $attach[] = $att_id;
  52      }
  53  
  54      if ( ! empty($attach) ) {
  55          $attach = implode(',', $attach);
  56          $attached = $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_parent = %d WHERE post_type = 'attachment' AND ID IN ($attach)", $parent_id) );
  57      }
  58  
  59      if ( isset($attached) ) {
  60          $location = 'upload.php';
  61          if ( $referer = wp_get_referer() ) {
  62              if ( false !== strpos($referer, 'upload.php') )
  63                  $location = $referer;
  64          }
  65  
  66          $location = add_query_arg( array( 'detached' => 1, 'attached' => $attached ) , $location );
  67          wp_redirect($location);
  68          exit;
  69      }
  70  
  71  } elseif ( isset($_GET['action']) && isset($_GET['media']) && ( -1 != $_GET['action'] || -1 != $_GET['action2'] ) ) {
  72      check_admin_referer('bulk-media');
  73      $doaction = ( -1 != $_GET['action'] ) ? $_GET['action'] : $_GET['action2'];
  74  
  75      if ( 'delete' == $doaction ) {
  76          foreach( (array) $_GET['media'] as $post_id_del ) {
  77              $post_del = & get_post($post_id_del);
  78  
  79              if ( !current_user_can('delete_post', $post_id_del) )
  80                  wp_die( __('You are not allowed to delete this post.') );
  81  
  82              if ( $post_del->post_type == 'attachment' )
  83                  if ( ! wp_delete_attachment($post_id_del) )
  84                      wp_die( __('Error in deleting...') );
  85          }
  86  
  87          $location = 'upload.php';
  88          if ( $referer = wp_get_referer() ) {
  89              if ( false !== strpos($referer, 'upload.php') )
  90                  $location = $referer;
  91          }
  92  
  93          $location = add_query_arg('message', 2, $location);
  94          $location = remove_query_arg('posted', $location);
  95          wp_redirect($location);
  96          exit;
  97      }
  98  } elseif ( isset($_GET['_wp_http_referer']) && ! empty($_GET['_wp_http_referer']) ) {
  99       wp_redirect( remove_query_arg( array('_wp_http_referer', '_wpnonce'), stripslashes($_SERVER['REQUEST_URI']) ) );
 100       exit;
 101  }
 102  
 103  $title = __('Media Library');
 104  $parent_file = 'upload.php';
 105  
 106  if ( ! isset( $_GET['paged'] ) || $_GET['paged'] < 1 )
 107      $_GET['paged'] = 1;
 108  
 109  if ( isset($_GET['detached']) ) {
 110  
 111      if ( !empty($lost) ) {
 112          $start = ( $_GET['paged'] - 1 ) * 50;
 113          $page_links_total = ceil(count($lost) / 50);
 114          $lost = implode(',', $lost);
 115  
 116          $orphans = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE post_type = 'attachment' AND ID IN ($lost) LIMIT $start, 50" );
 117      } else {
 118          $start = ( $_GET['paged'] - 1 ) * 25;
 119          $orphans = $wpdb->get_results( "SELECT SQL_CALC_FOUND_ROWS * FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent < 1 LIMIT $start, 25" );
 120          $page_links_total = ceil($wpdb->get_var( "SELECT FOUND_ROWS()" ) / 25);
 121      }
 122  
 123      $post_mime_types = array(
 124                  'image' => array(__('Images'), __('Manage Images'), __ngettext_noop('Image (%s)', 'Images (%s)')),
 125                  'audio' => array(__('Audio'), __('Manage Audio'), __ngettext_noop('Audio (%s)', 'Audio (%s)')),
 126                  'video' => array(__('Video'), __('Manage Video'), __ngettext_noop('Video (%s)', 'Video (%s)')),
 127              );
 128      $post_mime_types = apply_filters('post_mime_types', $post_mime_types);
 129  
 130      $avail_post_mime_types = get_available_post_mime_types('attachment');
 131  
 132      if ( isset($_GET['post_mime_type']) && !array_intersect( (array) $_GET['post_mime_type'], array_keys($post_mime_types) ) )
 133          unset($_GET['post_mime_type']);
 134  
 135  } else {
 136      list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
 137  }
 138  
 139  require_once ('admin-header.php'); ?>
 140  
 141  <?php
 142  if ( isset($_GET['posted']) && (int) $_GET['posted'] ) {
 143      $_GET['message'] = '1';
 144      $_SERVER['REQUEST_URI'] = remove_query_arg(array('posted'), $_SERVER['REQUEST_URI']);
 145  }
 146  
 147  if ( isset($_GET['attached']) && (int) $_GET['attached'] ) {
 148      $attached = (int) $_GET['attached'];
 149      $message = sprintf( __ngettext('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  
 157  if ( 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 wp_specialchars( $title );
 168  if ( isset($_GET['s']) && $_GET['s'] )
 169      printf( '<span class="subtitle">' . __('Search results for &#8220;%s&#8221;') . '</span>', wp_specialchars( get_search_query() ) ); ?>
 170  </h2>
 171  
 172  <?php
 173  if ( 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));
 185  foreach ( $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( __ngettext( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $_total_posts ), number_format_i18n( $_total_posts ) ) . '</a>';
 191  foreach ( $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( __ngettext( $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  
 205  echo implode( " |</li>\n", $type_links) . '</li>';
 206  unset($type_links);
 207  ?>
 208  </ul>
 209  
 210  <form class="search-form" action="" method="get">
 211  <p class="search-box">
 212      <label class="hidden" for="media-search-input"><?php _e( 'Search Media' ); ?>:</label>
 213      <input type="text" class="search-input" id="media-search-input" name="s" value="<?php the_search_query(); ?>" />
 214      <input type="submit" value="<?php _e( 'Search Media' ); ?>" class="button" />
 215  </p>
 216  </form>
 217  
 218  <form id="posts-filter" action="" method="get">
 219  <div class="tablenav">
 220  <?php
 221  if ( ! 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  
 233  if ( $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 _e('Apply'); ?>" name="doaction" id="doaction" class="button-secondary action" />
 251  <?php wp_nonce_field('bulk-media'); ?>
 252  
 253  <?php
 254  if ( ! 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
 265  foreach ($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='$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 _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 _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 echo _c('Media|media column header'); ?></th>
 305      <th scope="col"><?php echo _c('Date Added|media column header'); ?></th>
 306  </tr>
 307  </thead>
 308  
 309  <tfoot>
 310  <tr>
 311      <th scope="col" class="check-column"><input type="checkbox" /></th>
 312      <th scope="col"></th>
 313      <th scope="col"><?php echo _c('Media|media column header'); ?></th>
 314      <th scope="col"><?php echo _c('Date Added|media column header'); ?></th>
 315  </tr>
 316  </tfoot>
 317  
 318  <tbody id="the-list" class="list:post">
 319  <?php
 320      if ( $orphans ) {
 321          foreach ( $orphans as $post ) {
 322              $class = 'alternate' == $class ? '' : 'alternate';
 323              $att_title = wp_specialchars( _draft_or_post_title($post->ID) );
 324  ?>
 325      <tr id='post-<?php echo $post->ID; ?>' class='<?php echo $class; ?>' valign="top">
 326          <th scope="row" class="check-column"><input type="checkbox" name="media[]" value="<?php echo $post->ID; ?>" /></th>
 327  
 328          <td class="media-icon"><?php
 329          if ( $thumb = wp_get_attachment_image( $post->ID, array(80, 60), true ) ) { ?>
 330              <a href="media.php?action=edit&amp;attachment_id=<?php echo $post->ID; ?>" title="<?php echo attribute_escape(sprintf(__('Edit "%s"'), $att_title)); ?>"><?php echo $thumb; ?></a>
 331  <?php    } ?></td>
 332  
 333          <td><strong><a href="<?php echo get_edit_post_link( $post->ID ); ?>" title="<?php echo attribute_escape(sprintf(__('Edit "%s"'), $att_title)); ?>"><?php echo $att_title; ?></a></strong><br />
 334          <?php echo strtoupper(preg_replace('/^.*?\.(\w+)$/', '$1', get_attached_file($post->ID))); ?>
 335  
 336          <p>
 337          <?php
 338          $actions = array();
 339          if ( current_user_can('edit_post', $post->ID) )
 340              $actions['edit'] = '<a href="' . get_edit_post_link($post->ID, true) . '">' . __('Edit') . '</a>';
 341          if ( current_user_can('delete_post', $post->ID) )
 342              $actions['delete'] = "<a class='submitdelete' href='" . wp_nonce_url("post.php?action=delete&amp;post=$post->ID", 'delete-post_' . $post->ID) . "' onclick=\"if ( confirm('" . js_escape(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>";
 343          $actions['view'] = '<a href="' . get_permalink($post->ID) . '" title="' . attribute_escape(sprintf(__('View "%s"'), $title)) . '" rel="permalink">' . __('View') . '</a>';
 344          if ( current_user_can('edit_post', $post->ID) )
 345              $actions['attach'] = '<a href="#the-list" onclick="findPosts.open(\'media[]\',\''.$post->ID.'\');return false;">'.__('Attach').'</a>';
 346          $action_count = count($actions);
 347          $i = 0;
 348          foreach ( $actions as $action => $link ) {
 349              ++$i;
 350              ( $i == $action_count ) ? $sep = '' : $sep = ' | ';
 351              echo "<span class='$action'>$link$sep</span>";
 352          } ?>
 353          </p></td>
 354  
 355  <?php    if ( '0000-00-00 00:00:00' == $post->post_date && 'date' == $column_name ) {
 356              $t_time = $h_time = __('Unpublished');
 357          } else {
 358              $t_time = get_the_time(__('Y/m/d g:i:s A'));
 359              $m_time = $post->post_date;
 360              $time = get_post_time( 'G', true );
 361              if ( ( abs($t_diff = time() - $time) ) < 86400 ) {
 362                  if ( $t_diff < 0 )
 363                      $h_time = sprintf( __('%s from now'), human_time_diff( $time ) );
 364                  else
 365                      $h_time = sprintf( __('%s ago'), human_time_diff( $time ) );
 366              } else {
 367                  $h_time = mysql2date(__('Y/m/d'), $m_time);
 368              }
 369          } ?>
 370          <td><?php echo $h_time ?></td>
 371      </tr>
 372  <?php    }
 373  
 374      } else { ?>
 375      <tr><td colspan="5"><?php _e('No posts found.') ?></td></tr>
 376  <?php } ?>
 377  </tbody>
 378  </table>
 379  
 380  <?php find_posts_div();
 381  
 382  } else {
 383      include ( 'edit-attachment-rows.php' );
 384  } ?>
 385  
 386  <div id="ajax-response"></div>
 387  
 388  <div class="tablenav">
 389  
 390  <?php
 391  if ( $page_links )
 392      echo "<div class='tablenav-pages'>$page_links_text</div>";
 393  ?>
 394  
 395  <div class="alignleft actions">
 396  <select name="action2" class="select-action">
 397  <option value="-1" selected="selected"><?php _e('Bulk Actions'); ?></option>
 398  <option value="delete"><?php _e('Delete'); ?></option>
 399  <?php if ( isset($orphans) ) { ?>
 400  <option value="attach"><?php _e('Attach to a post'); ?></option>
 401  <?php } ?>
 402  </select>
 403  <input type="submit" value="<?php _e('Apply'); ?>" name="doaction2" id="doaction2" class="button-secondary action" />
 404  </div>
 405  
 406  <br class="clear" />
 407  </div>
 408  </form>
 409  <br class="clear" />
 410  
 411  </div>
 412  
 413  <script type="text/javascript">
 414  /* <![CDATA[ */
 415  (function($){
 416      $(document).ready(function(){
 417          $('#doaction, #doaction2').click(function(e){
 418              if ( $('select[name^="action"]').val() == 'delete' ) {
 419                  var m = '<?php echo js_escape(__("You are about to delete the selected attachments.\n  'Cancel' to stop, 'OK' to delete.")); ?>';
 420                  return showNotice.warn(m);
 421              } else if ( $('select[name^="action"]').val() == 'attach' ) {
 422                  e.preventDefault();
 423                  findPosts.open();
 424              }
 425          });
 426      });
 427  })(jQuery);
 428  columns.init('upload');
 429  /* ]]> */
 430  </script>
 431  
 432  <?php
 433  
 434  include ('admin-footer.php');
 435  ?>


Generated: Mon Mar 23 16:23:02 2009 Cross-referenced by PHPXref 0.7