[ XREF Home ] [ Index ]

PHP Cross Reference of WordPress Trunk

Provided by Yoast

title

Body

[close]

/wp-admin/includes/ -> media.php (source)

   1  <?php
   2  /**
   3   * WordPress Administration Media API.
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   */
   8  
   9  /**
  10   * {@internal Missing Short Description}}
  11   *
  12   * @since 2.5.0
  13   *
  14   * @return unknown
  15   */
  16  function media_upload_tabs() {
  17      $_default_tabs = array(
  18          'type' => __('From Computer'), // handler action suffix => tab text
  19          'type_url' => __('From URL'),
  20          'gallery' => __('Gallery'),
  21          'library' => __('Media Library')
  22      );
  23  
  24      return apply_filters('media_upload_tabs', $_default_tabs);
  25  }
  26  
  27  /**
  28   * {@internal Missing Short Description}}
  29   *
  30   * @since 2.5.0
  31   *
  32   * @param unknown_type $tabs
  33   * @return unknown
  34   */
  35  function update_gallery_tab($tabs) {
  36      global $wpdb;
  37  
  38      if ( !isset($_REQUEST['post_id']) ) {
  39          unset($tabs['gallery']);
  40          return $tabs;
  41      }
  42  
  43      $post_id = intval($_REQUEST['post_id']);
  44  
  45      if ( $post_id )
  46          $attachments = intval( $wpdb->get_var( $wpdb->prepare( "SELECT count(*) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent = %d", $post_id ) ) );
  47  
  48      if ( empty($attachments) ) {
  49          unset($tabs['gallery']);
  50          return $tabs;
  51      }
  52  
  53      $tabs['gallery'] = sprintf(__('Gallery (%s)'), "<span id='attachments-count'>$attachments</span>");
  54  
  55      return $tabs;
  56  }
  57  add_filter('media_upload_tabs', 'update_gallery_tab');
  58  
  59  /**
  60   * {@internal Missing Short Description}}
  61   *
  62   * @since 2.5.0
  63   */
  64  function the_media_upload_tabs() {
  65      global $redir_tab;
  66      $tabs = media_upload_tabs();
  67  
  68      if ( !empty($tabs) ) {
  69          echo "<ul id='sidemenu'>\n";
  70          if ( isset($redir_tab) && array_key_exists($redir_tab, $tabs) )
  71              $current = $redir_tab;
  72          elseif ( isset($_GET['tab']) && array_key_exists($_GET['tab'], $tabs) )
  73              $current = $_GET['tab'];
  74          else
  75              $current = apply_filters('media_upload_default_tab', 'type');
  76  
  77          foreach ( $tabs as $callback => $text ) {
  78              $class = '';
  79              if ( $current == $callback )
  80                  $class = " class='current'";
  81              $href = add_query_arg(array('tab'=>$callback, 's'=>false, 'paged'=>false, 'post_mime_type'=>false, 'm'=>false));
  82              $link = "<a href='" . esc_url($href) . "'$class>$text</a>";
  83              echo "\t<li id='" . esc_attr("tab-$callback") . "'>$link</li>\n";
  84          }
  85          echo "</ul>\n";
  86      }
  87  }
  88  
  89  /**
  90   * {@internal Missing Short Description}}
  91   *
  92   * @since 2.5.0
  93   *
  94   * @param unknown_type $id
  95   * @param unknown_type $alt
  96   * @param unknown_type $title
  97   * @param unknown_type $align
  98   * @param unknown_type $url
  99   * @param unknown_type $rel
 100   * @param unknown_type $size
 101   * @return unknown
 102   */
 103  function get_image_send_to_editor($id, $caption, $title, $align, $url='', $rel = false, $size='medium', $alt = '') {
 104  
 105      $html = get_image_tag($id, $alt, $title, $align, $size);
 106  
 107      $rel = $rel ? ' rel="attachment wp-att-' . esc_attr($id).'"' : '';
 108  
 109      if ( $url )
 110          $html = '<a href="' . esc_attr($url) . "\"$rel>$html</a>";
 111  
 112      $html = apply_filters( 'image_send_to_editor', $html, $id, $caption, $title, $align, $url, $size, $alt );
 113  
 114      return $html;
 115  }
 116  
 117  /**
 118   * {@internal Missing Short Description}}
 119   *
 120   * @since 2.6.0
 121   *
 122   * @param unknown_type $html
 123   * @param unknown_type $id
 124   * @param unknown_type $alt
 125   * @param unknown_type $title
 126   * @param unknown_type $align
 127   * @param unknown_type $url
 128   * @param unknown_type $size
 129   * @return unknown
 130   */
 131  function image_add_caption( $html, $id, $caption, $title, $align, $url, $size, $alt = '' ) {
 132  
 133      if ( empty($caption) || apply_filters( 'disable_captions', '' ) )
 134          return $html;
 135  
 136      $id = ( 0 < (int) $id ) ? 'attachment_' . $id : '';
 137  
 138      if ( ! preg_match( '/width="([0-9]+)/', $html, $matches ) )
 139          return $html;
 140  
 141      $width = $matches[1];
 142  
 143      $caption = str_replace(    array( '>',    '<',    '"',      "'" ),
 144                              array( '&gt;', '&lt;', '&quot;', '&#039;' ),
 145                              $caption
 146                            );
 147  
 148      $html = preg_replace( '/(class=["\'][^\'"]*)align(none|left|right|center)\s?/', '$1', $html );
 149      if ( empty($align) )
 150          $align = 'none';
 151  
 152      $shcode = '[caption id="' . $id . '" align="align' . $align
 153      . '" width="' . $width . '" caption="' . addslashes($caption) . '"]' . $html . '[/caption]';
 154  
 155      return apply_filters( 'image_add_caption_shortcode', $shcode, $html );
 156  }
 157  add_filter( 'image_send_to_editor', 'image_add_caption', 20, 8 );
 158  
 159  /**
 160   * {@internal Missing Short Description}}
 161   *
 162   * @since 2.5.0
 163   *
 164   * @param unknown_type $html
 165   */
 166  function media_send_to_editor($html) {
 167  ?>
 168  <script type="text/javascript">
 169  /* <![CDATA[ */
 170  var win = window.dialogArguments || opener || parent || top;
 171  win.send_to_editor('<?php echo addslashes($html); ?>');
 172  /* ]]> */
 173  </script>
 174  <?php
 175      exit;
 176  }
 177  
 178  /**
 179   * {@internal Missing Short Description}}
 180   *
 181   * This handles the file upload POST itself, creating the attachment post.
 182   *
 183   * @since 2.5.0
 184   *
 185   * @param string $file_id Index into the {@link $_FILES} array of the upload
 186   * @param int $post_id The post ID the media is associated with
 187   * @param array $post_data allows you to overwrite some of the attachment
 188   * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior
 189   * @return int the ID of the attachment
 190   */
 191  function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array( 'test_form' => false )) {
 192  
 193      $time = current_time('mysql');
 194      if ( $post = get_post($post_id) ) {
 195          if ( substr( $post->post_date, 0, 4 ) > 0 )
 196              $time = $post->post_date;
 197      }
 198  
 199      $name = $_FILES[$file_id]['name'];
 200      $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
 201  
 202      if ( isset($file['error']) )
 203          return new WP_Error( 'upload_error', $file['error'] );
 204  
 205      $name_parts = pathinfo($name);
 206      $name = trim( substr( $name, 0, -(1 + strlen($name_parts['extension'])) ) );
 207  
 208      $url = $file['url'];
 209      $type = $file['type'];
 210      $file = $file['file'];
 211      $title = $name;
 212      $content = '';
 213  
 214      // use image exif/iptc data for title and caption defaults if possible
 215      if ( $image_meta = @wp_read_image_metadata($file) ) {
 216          if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) )
 217              $title = $image_meta['title'];
 218          if ( trim( $image_meta['caption'] ) )
 219              $content = $image_meta['caption'];
 220      }
 221  
 222      // Construct the attachment array
 223      $attachment = array_merge( array(
 224          'post_mime_type' => $type,
 225          'guid' => $url,
 226          'post_parent' => $post_id,
 227          'post_title' => $title,
 228          'post_content' => $content,
 229      ), $post_data );
 230  
 231      // Save the data
 232      $id = wp_insert_attachment($attachment, $file, $post_id);
 233      if ( !is_wp_error($id) ) {
 234          wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
 235      }
 236  
 237      return $id;
 238  
 239  }
 240  
 241  /**
 242   * This handles a sideloaded file in the same way as an uploaded file is handled by {@link media_handle_upload()}
 243   *
 244   * @since 2.6.0
 245   *
 246   * @param array $file_array Array similar to a {@link $_FILES} upload array
 247   * @param int $post_id The post ID the media is associated with
 248   * @param string $desc Description of the sideloaded file
 249   * @param array $post_data allows you to overwrite some of the attachment
 250   * @return int|object The ID of the attachment or a WP_Error on failure
 251   */
 252  function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array()) {
 253      $overrides = array('test_form'=>false);
 254  
 255      $file = wp_handle_sideload($file_array, $overrides);
 256      if ( isset($file['error']) )
 257          return new WP_Error( 'upload_error', $file['error'] );
 258  
 259      $url = $file['url'];
 260      $type = $file['type'];
 261      $file = $file['file'];
 262      $title = preg_replace('/\.[^.]+$/', '', basename($file));
 263      $content = '';
 264  
 265      // use image exif/iptc data for title and caption defaults if possible
 266      if ( $image_meta = @wp_read_image_metadata($file) ) {
 267          if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) )
 268              $title = $image_meta['title'];
 269          if ( trim( $image_meta['caption'] ) )
 270              $content = $image_meta['caption'];
 271      }
 272  
 273      $title = isset($desc) ? $desc : '';
 274  
 275      // Construct the attachment array
 276      $attachment = array_merge( array(
 277          'post_mime_type' => $type,
 278          'guid' => $url,
 279          'post_parent' => $post_id,
 280          'post_title' => $title,
 281          'post_content' => $content,
 282      ), $post_data );
 283  
 284      // Save the attachment metadata
 285      $id = wp_insert_attachment($attachment, $file, $post_id);
 286      if ( !is_wp_error($id) )
 287          wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
 288  
 289      return $id;
 290  }
 291  
 292  /**
 293   * {@internal Missing Short Description}}
 294   *
 295   * Wrap iframe content (produced by $content_func) in a doctype, html head/body
 296   * etc any additional function args will be passed to content_func.
 297   *
 298   * @since 2.5.0
 299   *
 300   * @param unknown_type $content_func
 301   */
 302  function wp_iframe($content_func /* ... */) {
 303  ?>
 304  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 305  <html xmlns="http://www.w3.org/1999/xhtml" <?php do_action('admin_xml_ns'); ?> <?php language_attributes(); ?>>
 306  <head>
 307  <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php echo get_option('blog_charset'); ?>" />
 308  <title><?php bloginfo('name') ?> &rsaquo; <?php _e('Uploads'); ?> &#8212; <?php _e('WordPress'); ?></title>
 309  <?php
 310  wp_enqueue_style( 'global' );
 311  wp_enqueue_style( 'wp-admin' );
 312  wp_enqueue_style( 'colors' );
 313  // Check callback name for 'media'
 314  if ( ( is_array( $content_func ) && ! empty( $content_func[1] ) && 0 === strpos( (string) $content_func[1], 'media' ) )
 315      || ( ! is_array( $content_func ) && 0 === strpos( $content_func, 'media' ) ) )
 316      wp_enqueue_style( 'media' );
 317  wp_enqueue_style( 'ie' );
 318  ?>
 319  <script type="text/javascript">
 320  //<![CDATA[
 321  addLoadEvent = function(func){if(typeof jQuery!="undefined")jQuery(document).ready(func);else if(typeof wpOnload!='function'){wpOnload=func;}else{var oldonload=wpOnload;wpOnload=function(){oldonload();func();}}};
 322  var userSettings = {'url':'<?php echo SITECOOKIEPATH; ?>','uid':'<?php if ( ! isset($current_user) ) $current_user = wp_get_current_user(); echo $current_user->ID; ?>','time':'<?php echo time(); ?>'};
 323  var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>', pagenow = 'media-upload-popup', adminpage = 'media-upload-popup',
 324  isRtl = <?php echo (int) is_rtl(); ?>;
 325  //]]>
 326  </script>
 327  <?php
 328  do_action('admin_enqueue_scripts', 'media-upload-popup');
 329  do_action('admin_print_styles-media-upload-popup');
 330  do_action('admin_print_styles');
 331  do_action('admin_print_scripts-media-upload-popup');
 332  do_action('admin_print_scripts');
 333  do_action('admin_head-media-upload-popup');
 334  do_action('admin_head');
 335  
 336  if ( is_string($content_func) )
 337      do_action( "admin_head_{$content_func}" );
 338  ?>
 339  </head>
 340  <body<?php if ( isset($GLOBALS['body_id']) ) echo ' id="' . $GLOBALS['body_id'] . '"'; ?> class="no-js">
 341  <script type="text/javascript">
 342  //<![CDATA[
 343  (function(){
 344  var c = document.body.className;
 345  c = c.replace(/no-js/, 'js');
 346  document.body.className = c;
 347  })();
 348  //]]>
 349  </script>
 350  <?php
 351      $args = func_get_args();
 352      $args = array_slice($args, 1);
 353      call_user_func_array($content_func, $args);
 354  
 355      do_action('admin_print_footer_scripts');
 356  ?>
 357  <script type="text/javascript">if(typeof wpOnload=='function')wpOnload();</script>
 358  </body>
 359  </html>
 360  <?php
 361  }
 362  
 363  /**
 364   * {@internal Missing Short Description}}
 365   *
 366   * @since 2.5.0
 367   */
 368  function media_buttons() {
 369      $do_image = $do_audio = $do_video = true;
 370      if ( is_multisite() ) {
 371          $media_buttons = get_site_option( 'mu_media_buttons' );
 372          if ( empty($media_buttons['image']) )
 373              $do_image = false;
 374          if ( empty($media_buttons['audio']) )
 375              $do_audio = false;
 376          if ( empty($media_buttons['video']) )
 377              $do_video = false;
 378      }
 379      $out = '';
 380  
 381      if ( $do_image )
 382          $out .= _media_button(__('Add an Image'), 'images/media-button-image.gif?ver=20100531', 'image');
 383      if ( $do_video )
 384          $out .= _media_button(__('Add Video'), 'images/media-button-video.gif?ver=20100531', 'video');
 385      if ( $do_audio )
 386          $out .= _media_button(__('Add Audio'), 'images/media-button-music.gif?ver=20100531', 'audio');
 387  
 388      $out .= _media_button(__('Add Media'), 'images/media-button-other.gif?ver=20100531', 'media');
 389  
 390      $context = apply_filters('media_buttons_context', __('Upload/Insert %s'));
 391  
 392      printf($context, $out);
 393  }
 394  add_action( 'media_buttons', 'media_buttons' );
 395  
 396  function _media_button($title, $icon, $type) {
 397      return "<a href='" . esc_url( get_upload_iframe_src($type) ) . "' id='add_$type' class='thickbox' title='$title'><img src='" . esc_url( admin_url( $icon ) ) . "' alt='$title' onclick='return false;' /></a>";
 398  }
 399  
 400  function get_upload_iframe_src($type) {
 401      global $post_ID, $temp_ID;
 402      $uploading_iframe_ID = (int) (0 == $post_ID ? $temp_ID : $post_ID);
 403      $upload_iframe_src = add_query_arg('post_id', $uploading_iframe_ID, 'media-upload.php');
 404  
 405      if ( 'media' != $type )
 406          $upload_iframe_src = add_query_arg('type', $type, $upload_iframe_src);
 407      $upload_iframe_src = apply_filters($type . '_upload_iframe_src', $upload_iframe_src);
 408  
 409      return add_query_arg('TB_iframe', true, $upload_iframe_src);
 410  }
 411  
 412  /**
 413   * {@internal Missing Short Description}}
 414   *
 415   * @since 2.5.0
 416   *
 417   * @return unknown
 418   */
 419  function media_upload_form_handler() {
 420      check_admin_referer('media-form');
 421  
 422      $errors = null;
 423  
 424      if ( isset($_POST['send']) ) {
 425          $keys = array_keys($_POST['send']);
 426          $send_id = (int) array_shift($keys);
 427      }
 428  
 429      if ( !empty($_POST['attachments']) ) foreach ( $_POST['attachments'] as $attachment_id => $attachment ) {
 430          $post = $_post = get_post($attachment_id, ARRAY_A);
 431          if ( isset($attachment['post_content']) )
 432              $post['post_content'] = $attachment['post_content'];
 433          if ( isset($attachment['post_title']) )
 434              $post['post_title'] = $attachment['post_title'];
 435          if ( isset($attachment['post_excerpt']) )
 436              $post['post_excerpt'] = $attachment['post_excerpt'];
 437          if ( isset($attachment['menu_order']) )
 438              $post['menu_order'] = $attachment['menu_order'];
 439  
 440          if ( isset($send_id) && $attachment_id == $send_id ) {
 441              if ( isset($attachment['post_parent']) )
 442                  $post['post_parent'] = $attachment['post_parent'];
 443          }
 444  
 445          $post = apply_filters('attachment_fields_to_save', $post, $attachment);
 446  
 447          if ( isset($attachment['image_alt']) ) {
 448              $image_alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
 449              if ( $image_alt != stripslashes($attachment['image_alt']) ) {
 450                  $image_alt = wp_strip_all_tags( stripslashes($attachment['image_alt']), true );
 451                  // update_meta expects slashed
 452                  update_post_meta( $attachment_id, '_wp_attachment_image_alt', addslashes($image_alt) );
 453              }
 454          }
 455  
 456          if ( isset($post['errors']) ) {
 457              $errors[$attachment_id] = $post['errors'];
 458              unset($post['errors']);
 459          }
 460  
 461          if ( $post != $_post )
 462              wp_update_post($post);
 463  
 464          foreach ( get_attachment_taxonomies($post) as $t ) {
 465              if ( isset($attachment[$t]) )
 466                  wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $attachment[$t])), $t, false);
 467          }
 468      }
 469  
 470      if ( isset($_POST['insert-gallery']) || isset($_POST['update-gallery']) ) { ?>
 471          <script type="text/javascript">
 472          /* <![CDATA[ */
 473          var win = window.dialogArguments || opener || parent || top;
 474          win.tb_remove();
 475          /* ]]> */
 476          </script>
 477          <?php
 478          exit;
 479      }
 480  
 481      if ( isset($send_id) ) {
 482          $attachment = stripslashes_deep( $_POST['attachments'][$send_id] );
 483  
 484          $html = $attachment['post_title'];
 485          if ( !empty($attachment['url']) ) {
 486              $rel = '';
 487              if ( strpos($attachment['url'], 'attachment_id') || get_attachment_link($send_id) == $attachment['url'] )
 488                  $rel = " rel='attachment wp-att-" . esc_attr($send_id) . "'";
 489              $html = "<a href='{$attachment['url']}'$rel>$html</a>";
 490          }
 491  
 492          $html = apply_filters('media_send_to_editor', $html, $send_id, $attachment);
 493          return media_send_to_editor($html);
 494      }
 495  
 496      return $errors;
 497  }
 498  
 499  /**
 500   * {@internal Missing Short Description}}
 501   *
 502   * @since 2.5.0
 503   *
 504   * @return unknown
 505   */
 506  function media_upload_image() {
 507      $errors = array();
 508      $id = 0;
 509  
 510      if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
 511          check_admin_referer('media-form');
 512          // Upload File button was clicked
 513          $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
 514          unset($_FILES);
 515          if ( is_wp_error($id) ) {
 516              $errors['upload_error'] = $id;
 517              $id = false;
 518          }
 519      }
 520  
 521      if ( !empty($_POST['insertonlybutton']) ) {
 522          $alt = $align = '';
 523  
 524          $src = $_POST['insertonly']['src'];
 525          if ( !empty($src) && !strpos($src, '://') )
 526              $src = "http://$src";
 527          $alt = esc_attr($_POST['insertonly']['alt']);
 528          if ( isset($_POST['insertonly']['align']) ) {
 529              $align = esc_attr($_POST['insertonly']['align']);
 530              $class = " class='align$align'";
 531          }
 532          if ( !empty($src) )
 533              $html = "<img src='" . esc_url($src) . "' alt='$alt'$class />";
 534  
 535          $html = apply_filters('image_send_to_editor_url', $html, esc_url_raw($src), $alt, $align);
 536          return media_send_to_editor($html);
 537      }
 538  
 539      if ( !empty($_POST) ) {
 540          $return = media_upload_form_handler();
 541  
 542          if ( is_string($return) )
 543              return $return;
 544          if ( is_array($return) )
 545              $errors = $return;
 546      }
 547  
 548      if ( isset($_POST['save']) ) {
 549          $errors['upload_notice'] = __('Saved.');
 550          return media_upload_gallery();
 551      }
 552  
 553      if ( isset($_GET['tab']) && $_GET['tab'] == 'type_url' )
 554          return wp_iframe( 'media_upload_type_url_form', 'image', $errors, $id );
 555  
 556      return wp_iframe( 'media_upload_type_form', 'image', $errors, $id );
 557  }
 558  
 559  /**
 560   * Download an image from the specified URL and attach it to a post.
 561   *
 562   * @since 2.6.0
 563   *
 564   * @param string $file The URL of the image to download
 565   * @param int $post_id The post ID the media is to be associated with
 566   * @param string $desc Optional. Description of the image
 567   * @return string|WP_Error Populated HTML img tag on success
 568   */
 569  function media_sideload_image($file, $post_id, $desc = null) {
 570      if ( ! empty($file) ) {
 571          // Download file to temp location
 572          $tmp = download_url( $file );
 573  
 574          // Set variables for storage
 575          // fix file filename for query strings
 576          preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $file, $matches);
 577          $file_array['name'] = basename($matches[0]);
 578          $file_array['tmp_name'] = $tmp;
 579  
 580          // If error storing temporarily, unlink
 581          if ( is_wp_error( $tmp ) ) {
 582              @unlink($file_array['tmp_name']);
 583              $file_array['tmp_name'] = '';
 584          }
 585  
 586          // do the validation and storage stuff
 587          $id = media_handle_sideload( $file_array, $post_id, $desc );
 588          // If error storing permanently, unlink
 589          if ( is_wp_error($id) ) {
 590              @unlink($file_array['tmp_name']);
 591              return $id;
 592          }
 593  
 594          $src = wp_get_attachment_url( $id );
 595      }
 596  
 597      // Finally check to make sure the file has been saved, then return the html
 598      if ( ! empty($src) ) {
 599          $alt = isset($desc) ? esc_attr($desc) : '';
 600          $html = "<img src='$src' alt='$alt' />";
 601          return $html;
 602      }
 603  }
 604  
 605  /**
 606   * {@internal Missing Short Description}}
 607   *
 608   * @since 2.5.0
 609   *
 610   * @return unknown
 611   */
 612  function media_upload_audio() {
 613      $errors = array();
 614      $id = 0;
 615  
 616      if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
 617          check_admin_referer('media-form');
 618          // Upload File button was clicked
 619          $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
 620          unset($_FILES);
 621          if ( is_wp_error($id) ) {
 622              $errors['upload_error'] = $id;
 623              $id = false;
 624          }
 625      }
 626  
 627      if ( !empty($_POST['insertonlybutton']) ) {
 628          $href = $_POST['insertonly']['href'];
 629          if ( !empty($href) && !strpos($href, '://') )
 630              $href = "http://$href";
 631  
 632          $title = esc_attr($_POST['insertonly']['title']);
 633          if ( empty($title) )
 634              $title = esc_attr( basename($href) );
 635  
 636          if ( !empty($title) && !empty($href) )
 637              $html = "<a href='" . esc_url($href) . "' >$title</a>";
 638  
 639          $html = apply_filters('audio_send_to_editor_url', $html, $href, $title);
 640  
 641          return media_send_to_editor($html);
 642      }
 643  
 644      if ( !empty($_POST) ) {
 645          $return = media_upload_form_handler();
 646  
 647          if ( is_string($return) )
 648              return $return;
 649          if ( is_array($return) )
 650              $errors = $return;
 651      }
 652  
 653      if ( isset($_POST['save']) ) {
 654          $errors['upload_notice'] = __('Saved.');
 655          return media_upload_gallery();
 656      }
 657  
 658      if ( isset($_GET['tab']) && $_GET['tab'] == 'type_url' )
 659          return wp_iframe( 'media_upload_type_url_form', 'audio', $errors, $id );
 660  
 661      return wp_iframe( 'media_upload_type_form', 'audio', $errors, $id );
 662  }
 663  
 664  /**
 665   * {@internal Missing Short Description}}
 666   *
 667   * @since 2.5.0
 668   *
 669   * @return unknown
 670   */
 671  function media_upload_video() {
 672      $errors = array();
 673      $id = 0;
 674  
 675      if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
 676          check_admin_referer('media-form');
 677          // Upload File button was clicked
 678          $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
 679          unset($_FILES);
 680          if ( is_wp_error($id) ) {
 681              $errors['upload_error'] = $id;
 682              $id = false;
 683          }
 684      }
 685  
 686      if ( !empty($_POST['insertonlybutton']) ) {
 687          $href = $_POST['insertonly']['href'];
 688          if ( !empty($href) && !strpos($href, '://') )
 689              $href = "http://$href";
 690  
 691          $title = esc_attr($_POST['insertonly']['title']);
 692          if ( empty($title) )
 693              $title = esc_attr( basename($href) );
 694  
 695          if ( !empty($title) && !empty($href) )
 696              $html = "<a href='" . esc_url($href) . "' >$title</a>";
 697  
 698          $html = apply_filters('video_send_to_editor_url', $html, $href, $title);
 699  
 700          return media_send_to_editor($html);
 701      }
 702  
 703      if ( !empty($_POST) ) {
 704          $return = media_upload_form_handler();
 705  
 706          if ( is_string($return) )
 707              return $return;
 708          if ( is_array($return) )
 709              $errors = $return;
 710      }
 711  
 712      if ( isset($_POST['save']) ) {
 713          $errors['upload_notice'] = __('Saved.');
 714          return media_upload_gallery();
 715      }
 716  
 717      if ( isset($_GET['tab']) && $_GET['tab'] == 'type_url' )
 718          return wp_iframe( 'media_upload_type_url_form', 'video', $errors, $id );
 719  
 720      return wp_iframe( 'media_upload_type_form', 'video', $errors, $id );
 721  }
 722  
 723  /**
 724   * {@internal Missing Short Description}}
 725   *
 726   * @since 2.5.0
 727   *
 728   * @return unknown
 729   */
 730  function media_upload_file() {
 731      $errors = array();
 732      $id = 0;
 733  
 734      if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
 735          check_admin_referer('media-form');
 736          // Upload File button was clicked
 737          $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
 738          unset($_FILES);
 739          if ( is_wp_error($id) ) {
 740              $errors['upload_error'] = $id;
 741              $id = false;
 742          }
 743      }
 744  
 745      if ( !empty($_POST['insertonlybutton']) ) {
 746          $href = $_POST['insertonly']['href'];
 747          if ( !empty($href) && !strpos($href, '://') )
 748              $href = "http://$href";
 749  
 750          $title = esc_attr($_POST['insertonly']['title']);
 751          if ( empty($title) )
 752              $title = basename($href);
 753          if ( !empty($title) && !empty($href) )
 754              $html = "<a href='" . esc_url($href) . "' >$title</a>";
 755          $html = apply_filters('file_send_to_editor_url', $html, esc_url_raw($href), $title);
 756          return media_send_to_editor($html);
 757      }
 758  
 759      if ( !empty($_POST) ) {
 760          $return = media_upload_form_handler();
 761  
 762          if ( is_string($return) )
 763              return $return;
 764          if ( is_array($return) )
 765              $errors = $return;
 766      }
 767  
 768      if ( isset($_POST['save']) ) {
 769          $errors['upload_notice'] = __('Saved.');
 770          return media_upload_gallery();
 771      }
 772  
 773      if ( isset($_GET['tab']) && $_GET['tab'] == 'type_url' )
 774          return wp_iframe( 'media_upload_type_url_form', 'file', $errors, $id );
 775  
 776      return wp_iframe( 'media_upload_type_form', 'file', $errors, $id );
 777  }
 778  
 779  /**
 780   * {@internal Missing Short Description}}
 781   *
 782   * @since 2.5.0
 783   *
 784   * @return unknown
 785   */
 786  function media_upload_gallery() {
 787      $errors = array();
 788  
 789      if ( !empty($_POST) ) {
 790          $return = media_upload_form_handler();
 791  
 792          if ( is_string($return) )
 793              return $return;
 794          if ( is_array($return) )
 795              $errors = $return;
 796      }
 797  
 798      wp_enqueue_script('admin-gallery');
 799      return wp_iframe( 'media_upload_gallery_form', $errors );
 800  }
 801  
 802  /**
 803   * {@internal Missing Short Description}}
 804   *
 805   * @since 2.5.0
 806   *
 807   * @return unknown
 808   */
 809  function media_upload_library() {
 810      $errors = array();
 811      if ( !empty($_POST) ) {
 812          $return = media_upload_form_handler();
 813  
 814          if ( is_string($return) )
 815              return $return;
 816          if ( is_array($return) )
 817              $errors = $return;
 818      }
 819  
 820      return wp_iframe( 'media_upload_library_form', $errors );
 821  }
 822  
 823  /**
 824   * Retrieve HTML for the image alignment radio buttons with the specified one checked.
 825   *
 826   * @since 2.7.0
 827   *
 828   * @param unknown_type $post
 829   * @param unknown_type $checked
 830   * @return unknown
 831   */
 832  function image_align_input_fields( $post, $checked = '' ) {
 833  
 834      if ( empty($checked) )
 835          $checked = get_user_setting('align', 'none');
 836  
 837      $alignments = array('none' => __('None'), 'left' => __('Left'), 'center' => __('Center'), 'right' => __('Right'));
 838      if ( !array_key_exists( (string) $checked, $alignments ) )
 839          $checked = 'none';
 840  
 841      $out = array();
 842      foreach ( $alignments as $name => $label ) {
 843          $name = esc_attr($name);
 844          $out[] = "<input type='radio' name='attachments[{$post->ID}][align]' id='image-align-{$name}-{$post->ID}' value='$name'".
 845               ( $checked == $name ? " checked='checked'" : "" ) .
 846              " /><label for='image-align-{$name}-{$post->ID}' class='align image-align-{$name}-label'>$label</label>";
 847      }
 848      return join("\n", $out);
 849  }
 850  
 851  /**
 852   * Retrieve HTML for the size radio buttons with the specified one checked.
 853   *
 854   * @since 2.7.0
 855   *
 856   * @param unknown_type $post
 857   * @param unknown_type $check
 858   * @return unknown
 859   */
 860  function image_size_input_fields( $post, $check = '' ) {
 861  
 862          // get a list of the actual pixel dimensions of each possible intermediate version of this image
 863          $size_names = array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full Size'));
 864  
 865          if ( empty($check) )
 866              $check = get_user_setting('imgsize', 'medium');
 867  
 868          foreach ( $size_names as $size => $label ) {
 869              $downsize = image_downsize($post->ID, $size);
 870              $checked = '';
 871  
 872              // is this size selectable?
 873              $enabled = ( $downsize[3] || 'full' == $size );
 874              $css_id = "image-size-{$size}-{$post->ID}";
 875              // if this size is the default but that's not available, don't select it
 876              if ( $size == $check ) {
 877                  if ( $enabled )
 878                      $checked = " checked='checked'";
 879                  else
 880                      $check = '';
 881              } elseif ( !$check && $enabled && 'thumbnail' != $size ) {
 882                  // if $check is not enabled, default to the first available size that's bigger than a thumbnail
 883                  $check = $size;
 884                  $checked = " checked='checked'";
 885              }
 886  
 887              $html = "<div class='image-size-item'><input type='radio' " . disabled( $enabled, false, false ) . "name='attachments[$post->ID][image-size]' id='{$css_id}' value='{$size}'$checked />";
 888  
 889              $html .= "<label for='{$css_id}'>$label</label>";
 890              // only show the dimensions if that choice is available
 891              if ( $enabled )
 892                  $html .= " <label for='{$css_id}' class='help'>" . sprintf( "(%d&nbsp;&times;&nbsp;%d)", $downsize[1], $downsize[2] ). "</label>";
 893  
 894              $html .= '</div>';
 895  
 896              $out[] = $html;
 897          }
 898  
 899          return array(
 900              'label' => __('Size'),
 901              'input' => 'html',
 902              'html'  => join("\n", $out),
 903          );
 904  }
 905  
 906  /**
 907   * Retrieve HTML for the Link URL buttons with the default link type as specified.
 908   *
 909   * @since 2.7.0
 910   *
 911   * @param unknown_type $post
 912   * @param unknown_type $url_type
 913   * @return unknown
 914   */
 915  function image_link_input_fields($post, $url_type = '') {
 916  
 917      $file = wp_get_attachment_url($post->ID);
 918      $link = get_attachment_link($post->ID);
 919  
 920      if ( empty($url_type) )
 921          $url_type = get_user_setting('urlbutton', 'post');
 922  
 923      $url = '';
 924      if ( $url_type == 'file' )
 925          $url = $file;
 926      elseif ( $url_type == 'post' )
 927          $url = $link;
 928  
 929      return "
 930      <input type='text' class='text urlfield' name='attachments[$post->ID][url]' value='" . esc_attr($url) . "' /><br />
 931      <button type='button' class='button urlnone' title=''>" . __('None') . "</button>
 932      <button type='button' class='button urlfile' title='" . esc_attr($file) . "'>" . __('File URL') . "</button>
 933      <button type='button' class='button urlpost' title='" . esc_attr($link) . "'>" . __('Post URL') . "</button>
 934  ";
 935  }
 936  
 937  /**
 938   * {@internal Missing Short Description}}
 939   *
 940   * @since 2.5.0
 941   *
 942   * @param unknown_type $form_fields
 943   * @param unknown_type $post
 944   * @return unknown
 945   */
 946  function image_attachment_fields_to_edit($form_fields, $post) {
 947      if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
 948          $alt = get_post_meta($post->ID, '_wp_attachment_image_alt', true);
 949          if ( empty($alt) )
 950              $alt = '';
 951  
 952          $form_fields['post_title']['required'] = true;
 953  
 954          $form_fields['image_alt'] = array(
 955              'value' => $alt,
 956              'label' => __('Alternate Text'),
 957              'helps' => __('Alt text for the image, e.g. &#8220;The Mona Lisa&#8221;')
 958          );
 959  
 960          $form_fields['align'] = array(
 961              'label' => __('Alignment'),
 962              'input' => 'html',
 963              'html'  => image_align_input_fields($post, get_option('image_default_align')),
 964          );
 965  
 966          $form_fields['image-size'] = image_size_input_fields( $post, get_option('image_default_size', 'medium') );
 967  
 968      } else {
 969          unset( $form_fields['image_alt'] );
 970      }
 971      return $form_fields;
 972  }
 973  
 974  add_filter('attachment_fields_to_edit', 'image_attachment_fields_to_edit', 10, 2);
 975  
 976  /**
 977   * {@internal Missing Short Description}}
 978   *
 979   * @since 2.5.0
 980   *
 981   * @param unknown_type $form_fields
 982   * @param unknown_type $post
 983   * @return unknown
 984   */
 985  function media_single_attachment_fields_to_edit( $form_fields, $post ) {
 986      unset($form_fields['url'], $form_fields['align'], $form_fields['image-size']);
 987      return $form_fields;
 988  }
 989  
 990  /**
 991   * {@internal Missing Short Description}}
 992   *
 993   * @since 2.8.0
 994   *
 995   * @param unknown_type $form_fields
 996   * @param unknown_type $post
 997   * @return unknown
 998   */
 999  function media_post_single_attachment_fields_to_edit( $form_fields, $post ) {
1000      unset($form_fields['image_url']);
1001      return $form_fields;
1002  }
1003  
1004  /**
1005   * {@internal Missing Short Description}}
1006   *
1007   * @since 2.5.0
1008   *
1009   * @param unknown_type $post
1010   * @param unknown_type $attachment
1011   * @return unknown
1012   */
1013  function image_attachment_fields_to_save($post, $attachment) {
1014      if ( substr($post['post_mime_type'], 0, 5) == 'image' ) {
1015          if ( strlen(trim($post['post_title'])) == 0 ) {
1016              $post['post_title'] = preg_replace('/\.\w+$/', '', basename($post['guid']));
1017              $post['errors']['post_title']['errors'][] = __('Empty Title filled from filename.');
1018          }
1019      }
1020  
1021      return $post;
1022  }
1023  
1024  add_filter('attachment_fields_to_save', 'image_attachment_fields_to_save', 10, 2);
1025  
1026  /**
1027   * {@internal Missing Short Description}}
1028   *
1029   * @since 2.5.0
1030   *
1031   * @param unknown_type $html
1032   * @param unknown_type $attachment_id
1033   * @param unknown_type $attachment
1034   * @return unknown
1035   */
1036  function image_media_send_to_editor($html, $attachment_id, $attachment) {
1037      $post =& get_post($attachment_id);
1038      if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
1039          $url = $attachment['url'];
1040          $align = !empty($attachment['align']) ? $attachment['align'] : 'none';
1041          $size = !empty($attachment['image-size']) ? $attachment['image-size'] : 'medium';
1042          $alt = !empty($attachment['image_alt']) ? $attachment['image_alt'] : '';
1043          $rel = ( $url == get_attachment_link($attachment_id) );
1044  
1045          return get_image_send_to_editor($attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel, $size, $alt);
1046      }
1047  
1048      return $html;
1049  }
1050  
1051  add_filter('media_send_to_editor', 'image_media_send_to_editor', 10, 3);
1052  
1053  /**
1054   * {@internal Missing Short Description}}
1055   *
1056   * @since 2.5.0
1057   *
1058   * @param unknown_type $post
1059   * @param unknown_type $errors
1060   * @return unknown
1061   */
1062  function get_attachment_fields_to_edit($post, $errors = null) {
1063      if ( is_int($post) )
1064          $post =& get_post($post);
1065      if ( is_array($post) )
1066          $post = (object) $post;
1067  
1068      $image_url = wp_get_attachment_url($post->ID);
1069  
1070      $edit_post = sanitize_post($post, 'edit');
1071  
1072  
1073  
1074      $form_fields = array(
1075          'post_title'   => array(
1076              'label'      => __('Title'),
1077              'value'      => $edit_post->post_title
1078          ),
1079          'image_alt'   => array(),
1080          'post_excerpt' => array(
1081              'label'      => __('Caption'),
1082              'value'      => $edit_post->post_excerpt
1083          ),
1084          'post_content' => array(
1085              'label'      => __('Description'),
1086              'value'      => $edit_post->post_content,
1087              'input'      => 'textarea'
1088          ),
1089          'url'          => array(
1090              'label'      => __('Link URL'),
1091              'input'      => 'html',
1092              'html'       => image_link_input_fields($post, get_option('image_default_link_type')),
1093              'helps'      => __('Enter a link URL or click above for presets.')
1094          ),
1095          'menu_order'   => array(
1096              'label'      => __('Order'),
1097              'value'      => $edit_post->menu_order
1098          ),
1099          'image_url'    => array(
1100              'label'      => __('File URL'),
1101              'input'      => 'html',
1102              'html'       => "<input type='text' class='text urlfield' readonly='readonly' name='attachments[$post->ID][url]' value='" . esc_attr($image_url) . "' /><br />",
1103              'value'      => wp_get_attachment_url($post->ID),
1104              'helps'      => __('Location of the uploaded file.')
1105          )
1106      );
1107  
1108      foreach ( get_attachment_taxonomies($post) as $taxonomy ) {
1109          $t = (array) get_taxonomy($taxonomy);
1110          if ( ! $t['public'] )
1111              continue;
1112          if ( empty($t['label']) )
1113              $t['label'] = $taxonomy;
1114          if ( empty($t['args']) )
1115              $t['args'] = array();
1116  
1117          $terms = get_object_term_cache($post->ID, $taxonomy);
1118          if ( empty($terms) )
1119              $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
1120  
1121          $values = array();
1122  
1123          foreach ( $terms as $term )
1124              $values[] = $term->name;
1125          $t['value'] = join(', ', $values);
1126  
1127          $form_fields[$taxonomy] = $t;
1128      }
1129  
1130      // Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default
1131      // The recursive merge is easily traversed with array casting: foreach( (array) $things as $thing )
1132      $form_fields = array_merge_recursive($form_fields, (array) $errors);
1133  
1134      $form_fields = apply_filters('attachment_fields_to_edit', $form_fields, $post);
1135  
1136      return $form_fields;
1137  }
1138  
1139  /**
1140   * Retrieve HTML for media items of post gallery.
1141   *
1142   * The HTML markup retrieved will be created for the progress of SWF Upload
1143   * component. Will also create link for showing and hiding the form to modify
1144   * the image attachment.
1145   *
1146   * @since 2.5.0
1147   *
1148   * @param int $post_id Optional. Post ID.
1149   * @param array $errors Errors for attachment, if any.
1150   * @return string
1151   */
1152  function get_media_items( $post_id, $errors ) {
1153      $attachments = array();
1154      if ( $post_id ) {
1155          $post = get_post($post_id);
1156          if ( $post && $post->post_type == 'attachment' )
1157              $attachments = array($post->ID => $post);
1158          else
1159              $attachments = get_children( array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'orderby' => 'menu_order ASC, ID', 'order' => 'DESC') );
1160      } else {
1161          if ( is_array($GLOBALS['wp_the_query']->posts) )
1162              foreach ( $GLOBALS['wp_the_query']->posts as $attachment )
1163                  $attachments[$attachment->ID] = $attachment;
1164      }
1165  
1166      $output = '';
1167      foreach ( (array) $attachments as $id => $attachment ) {
1168          if ( $attachment->post_status == 'trash' )
1169              continue;
1170          if ( $item = get_media_item( $id, array( 'errors' => isset($errors[$id]) ? $errors[$id] : null) ) )
1171              $output .= "\n<div id='media-item-$id' class='media-item child-of-$attachment->post_parent preloaded'><div class='progress'><div class='bar'></div></div><div id='media-upload-error-$id'></div><div class='filename'></div>$item\n</div>";
1172      }
1173  
1174      return $output;
1175  }
1176  
1177  /**
1178   * Retrieve HTML form for modifying the image attachment.
1179   *
1180   * @since 2.5.0
1181   *
1182   * @param int $attachment_id Attachment ID for modification.
1183   * @param string|array $args Optional. Override defaults.
1184   * @return string HTML form for attachment.
1185   */
1186  function get_media_item( $attachment_id, $args = null ) {
1187      global $redir_tab;
1188  
1189      if ( ( $attachment_id = intval( $attachment_id ) ) && $thumb_url = wp_get_attachment_image_src( $attachment_id, 'thumbnail', true ) )
1190          $thumb_url = $thumb_url[0];
1191      else
1192          $thumb_url = false;
1193  
1194      $post = get_post( $attachment_id );
1195  
1196      $default_args = array( 'errors' => null, 'send' => $post->post_parent ? post_type_supports( get_post_type( $post->post_parent ), 'editor' ) : true, 'delete' => true, 'toggle' => true, 'show_title' => true );
1197      $args = wp_parse_args( $args, $default_args );
1198      $args = apply_filters( 'get_media_item_args', $args );
1199      extract( $args, EXTR_SKIP );
1200  
1201      $toggle_on  = __( 'Show' );
1202      $toggle_off = __( 'Hide' );
1203  
1204      $filename = esc_html( basename( $post->guid ) );
1205      $title = esc_attr( $post->post_title );
1206  
1207      if ( $_tags = get_the_tags( $attachment_id ) ) {
1208          foreach ( $_tags as $tag )
1209              $tags[] = $tag->name;
1210          $tags = esc_attr( join( ', ', $tags ) );
1211      }
1212  
1213      $post_mime_types = get_post_mime_types();
1214      $keys = array_keys( wp_match_mime_types( array_keys( $post_mime_types ), $post->post_mime_type ) );
1215      $type = array_shift( $keys );
1216      $type_html = "<input type='hidden' id='type-of-$attachment_id' value='" . esc_attr( $type ) . "' />";
1217  
1218      $form_fields = get_attachment_fields_to_edit( $post, $errors );
1219  
1220      if ( $toggle ) {
1221          $class = empty( $errors ) ? 'startclosed' : 'startopen';
1222          $toggle_links = "
1223      <a class='toggle describe-toggle-on' href='#'>$toggle_on</a>
1224      <a class='toggle describe-toggle-off' href='#'>$toggle_off</a>";
1225      } else {
1226          $class = 'form-table';
1227          $toggle_links = '';
1228      }
1229  
1230      $display_title = ( !empty( $title ) ) ? $title : $filename; // $title shouldn't ever be empty, but just in case
1231      $display_title = $show_title ? "<div class='filename new'><span class='title'>" . wp_html_excerpt( $display_title, 60 ) . "</span></div>" : '';
1232  
1233      $gallery = ( ( isset( $_REQUEST['tab'] ) && 'gallery' == $_REQUEST['tab'] ) || ( isset( $redir_tab ) && 'gallery' == $redir_tab ) );
1234      $order = '';
1235  
1236      foreach ( $form_fields as $key => $val ) {
1237          if ( 'menu_order' == $key ) {
1238              if ( $gallery )
1239                  $order = "<div class='menu_order'> <input class='menu_order_input' type='text' id='attachments[$attachment_id][menu_order]' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ). "' /></div>";
1240              else
1241                  $order = "<input type='hidden' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ) . "' />";
1242  
1243              unset( $form_fields['menu_order'] );
1244              break;
1245          }
1246      }
1247  
1248      $media_dims = '';
1249      $meta = wp_get_attachment_metadata( $post->ID );
1250      if ( is_array( $meta ) && array_key_exists( 'width', $meta ) && array_key_exists( 'height', $meta ) )
1251          $media_dims .= "<span id='media-dims-$post->ID'>{$meta['width']}&nbsp;&times;&nbsp;{$meta['height']}</span> ";
1252      $media_dims = apply_filters( 'media_meta', $media_dims, $post );
1253  
1254      $image_edit_button = '';
1255      if ( gd_edit_image_support( $post->post_mime_type ) ) {
1256          $nonce = wp_create_nonce( "image_editor-$post->ID" );
1257          $image_edit_button = "<input type='button' id='imgedit-open-btn-$post->ID' onclick='imageEdit.open( $post->ID, \"$nonce\" )' class='button' value='" . esc_attr__( 'Edit Image' ) . "' /> <img src='" . esc_url( admin_url( 'images/wpspin_light.gif' ) ) . "' class='imgedit-wait-spin' alt='' />";
1258      }
1259  
1260      $attachment_url = get_permalink( $attachment_id );
1261  
1262      $item = "
1263      $type_html
1264      $toggle_links
1265      $order
1266      $display_title
1267      <table class='slidetoggle describe $class'>
1268          <thead class='media-item-info' id='media-head-$post->ID'>
1269          <tr valign='top'>
1270              <td class='A1B1' id='thumbnail-head-$post->ID'>
1271              <p><a href='$attachment_url' target='_blank'><img class='thumbnail' src='$thumb_url' alt='' style='margin-top: 3px' /></a></p>
1272              <p>$image_edit_button</p>
1273              </td>
1274              <td>
1275              <p><strong>" . __('File name:') . "</strong> $filename</p>
1276              <p><strong>" . __('File type:') . "</strong> $post->post_mime_type</p>
1277              <p><strong>" . __('Upload date:') . "</strong> " . mysql2date( get_option('date_format'), $post->post_date ). '</p>';
1278              if ( !empty( $media_dims ) )
1279                  $item .= "<p><strong>" . __('Dimensions:') . "</strong> $media_dims</p>\n";
1280  
1281              $item .= "</td></tr>\n";
1282  
1283  
1284  
1285      $item .= "
1286          </thead>
1287          <tbody>
1288          <tr><td colspan='2' class='imgedit-response' id='imgedit-response-$post->ID'></td></tr>
1289          <tr><td style='display:none' colspan='2' class='image-editor' id='image-editor-$post->ID'></td></tr>\n";
1290  
1291      $defaults = array(
1292          'input'      => 'text',
1293          'required'   => false,
1294          'value'      => '',
1295          'extra_rows' => array(),
1296      );
1297  
1298      if ( $send )
1299          $send = get_submit_button( __( 'Insert into Post' ), 'button', "send[$attachment_id]", false );
1300      if ( $delete && current_user_can( 'delete_post', $attachment_id ) ) {
1301          if ( !EMPTY_TRASH_DAYS ) {
1302              $delete = "<a href='" . wp_nonce_url( "post.php?action=delete&amp;post=$attachment_id", 'delete-attachment_' . $attachment_id ) . "' id='del[$attachment_id]' class='delete'>" . __( 'Delete Permanently' ) . '</a>';
1303          } elseif ( !MEDIA_TRASH ) {
1304              $delete = "<a href='#' class='del-link' onclick=\"document.getElementById('del_attachment_$attachment_id').style.display='block';return false;\">" . __( 'Delete' ) . "</a>
1305               <div id='del_attachment_$attachment_id' class='del-attachment' style='display:none;'>" . sprintf( __( 'You are about to delete <strong>%s</strong>.' ), $filename ) . "
1306               <a href='" . wp_nonce_url( "post.php?action=delete&amp;post=$attachment_id", 'delete-attachment_' . $attachment_id ) . "' id='del[$attachment_id]' class='button'>" . __( 'Continue' ) . "</a>
1307               <a href='#' class='button' onclick=\"this.parentNode.style.display='none';return false;\">" . __( 'Cancel' ) . "</a>
1308               </div>";
1309          } else {
1310              $delete = "<a href='" . wp_nonce_url( "post.php?action=trash&amp;post=$attachment_id", 'trash-attachment_' . $attachment_id ) . "' id='del[$attachment_id]' class='delete'>" . __( 'Move to Trash' ) . "</a>
1311              <a href='" . wp_nonce_url( "post.php?action=untrash&amp;post=$attachment_id", 'untrash-attachment_' . $attachment_id ) . "' id='undo[$attachment_id]' class='undo hidden'>" . __( 'Undo' ) . "</a>";
1312          }
1313      } else {
1314          $delete = '';
1315      }
1316  
1317      $thumbnail = '';
1318      $calling_post_id = 0;
1319      if ( isset( $_GET['post_id'] ) )
1320          $calling_post_id = absint( $_GET['post_id'] );
1321      elseif ( isset( $_POST ) && count( $_POST ) ) // Like for async-upload where $_GET['post_id'] isn't set
1322          $calling_post_id = $post->post_parent;
1323      if ( 'image' == $type && $calling_post_id && current_theme_supports( 'post-thumbnails', get_post_type( $calling_post_id ) ) && get_post_thumbnail_id( $calling_post_id ) != $attachment_id ) {
1324          $ajax_nonce = wp_create_nonce( "set_post_thumbnail-$calling_post_id" );
1325          $thumbnail = "<a class='wp-post-thumbnail' id='wp-post-thumbnail-" . $attachment_id . "' href='#' onclick='WPSetAsThumbnail(\"$attachment_id\", \"$ajax_nonce\");return false;'>" . esc_html__( "Use as featured image" ) . "</a>";
1326      }
1327  
1328      if ( ( $send || $thumbnail || $delete ) && !isset( $form_fields['buttons'] ) )
1329          $form_fields['buttons'] = array( 'tr' => "\t\t<tr class='submit'><td></td><td class='savesend'>$send $thumbnail $delete</td></tr>\n" );
1330  
1331      $hidden_fields = array();
1332  
1333      foreach ( $form_fields as $id => $field ) {
1334          if ( $id[0] == '_' )
1335              continue;
1336  
1337          if ( !empty( $field['tr'] ) ) {
1338              $item .= $field['tr'];
1339              continue;
1340          }
1341  
1342          $field = array_merge( $defaults, $field );
1343          $name = "attachments[$attachment_id][$id]";
1344  
1345          if ( $field['input'] == 'hidden' ) {
1346              $hidden_fields[$name] = $field['value'];
1347              continue;
1348          }
1349  
1350          $required      = $field['required'] ? '<span class="alignright"><abbr title="required" class="required">*</abbr></span>' : '';
1351          $aria_required = $field['required'] ? " aria-required='true' " : '';
1352          $class  = $id;
1353          $class .= $field['required'] ? ' form-required' : '';
1354  
1355          $item .= "\t\t<tr class='$class'>\n\t\t\t<th valign='top' scope='row' class='label'><label for='$name'><span class='alignleft'>{$field['label']}</span>$required<br class='clear' /></label></th>\n\t\t\t<td class='field'>";
1356          if ( !empty( $field[ $field['input'] ] ) )
1357              $item .= $field[ $field['input'] ];
1358          elseif ( $field['input'] == 'textarea' ) {
1359              if ( user_can_richedit() ) { // textarea_escaped when user_can_richedit() = false
1360                  $field['value'] = esc_textarea( $field['value'] );
1361              }
1362              $item .= "<textarea id='$name' name='$name' $aria_required>" . $field['value'] . '</textarea>';
1363          } else {
1364              $item .= "<input type='text' class='text' id='$name' name='$name' value='" . esc_attr( $field['value'] ) . "' $aria_required />";
1365          }
1366          if ( !empty( $field['helps'] ) )
1367              $item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>';
1368          $item .= "</td>\n\t\t</tr>\n";
1369  
1370          $extra_rows = array();
1371  
1372          if ( !empty( $field['errors'] ) )
1373              foreach ( array_unique( (array) $field['errors'] ) as $error )
1374                  $extra_rows['error'][] = $error;
1375  
1376          if ( !empty( $field['extra_rows'] ) )
1377              foreach ( $field['extra_rows'] as $class => $rows )
1378                  foreach ( (array) $rows as $html )
1379                      $extra_rows[$class][] = $html;
1380  
1381          foreach ( $extra_rows as $class => $rows )
1382              foreach ( $rows as $html )
1383                  $item .= "\t\t<tr><td></td><td class='$class'>$html</td></tr>\n";
1384      }
1385  
1386      if ( !empty( $form_fields['_final'] ) )
1387          $item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
1388      $item .= "\t</tbody>\n";
1389      $item .= "\t</table>\n";
1390  
1391      foreach ( $hidden_fields as $name => $value )
1392          $item .= "\t<input type='hidden' name='$name' id='$name' value='" . esc_attr( $value ) . "' />\n";
1393  
1394      if ( $post->post_parent < 1 && isset( $_REQUEST['post_id'] ) ) {
1395          $parent = (int) $_REQUEST['post_id'];
1396          $parent_name = "attachments[$attachment_id][post_parent]";
1397          $item .= "\t<input type='hidden' name='$parent_name' id='$parent_name' value='$parent' />\n";
1398      }
1399  
1400      return $item;
1401  }
1402  
1403  /**
1404   * {@internal Missing Short Description}}
1405   *
1406   * @since 2.5.0
1407   */
1408  function media_upload_header() {
1409      ?>
1410      <script type="text/javascript">post_id = <?php echo intval($_REQUEST['post_id']); ?>;</script>
1411      <div id="media-upload-header">
1412      <?php the_media_upload_tabs(); ?>
1413      </div>
1414      <?php
1415  }
1416  
1417  /**
1418   * {@internal Missing Short Description}}
1419   *
1420   * @since 2.5.0
1421   *
1422   * @param unknown_type $errors
1423   */
1424  function media_upload_form( $errors = null ) {
1425      global $type, $tab, $pagenow;
1426  
1427      $flash_action_url = admin_url('async-upload.php');
1428  
1429      // If Mac and mod_security, no Flash. :(
1430      $flash = true;
1431      if ( false !== stripos($_SERVER['HTTP_USER_AGENT'], 'mac') && apache_mod_loaded('mod_security') )
1432          $flash = false;
1433  
1434      $flash = apply_filters('flash_uploader', $flash);
1435      $post_id = isset($_REQUEST['post_id']) ? intval($_REQUEST['post_id']) : 0;
1436  
1437      $upload_size_unit = $max_upload_size =  wp_max_upload_size();
1438      $sizes = array( 'KB', 'MB', 'GB' );
1439      for ( $u = -1; $upload_size_unit > 1024 && $u < count( $sizes ) - 1; $u++ )
1440          $upload_size_unit /= 1024;
1441      if ( $u < 0 ) {
1442          $upload_size_unit = 0;
1443          $u = 0;
1444      } else {
1445          $upload_size_unit = (int) $upload_size_unit;
1446      }
1447  ?>
1448  <script type="text/javascript">
1449  //<![CDATA[
1450  var uploaderMode = 0;
1451  jQuery(document).ready(function($){
1452      uploaderMode = getUserSetting('uploader');
1453      $('.upload-html-bypass a').click(function(){deleteUserSetting('uploader');uploaderMode=0;swfuploadPreLoad();return false;});
1454      $('.upload-flash-bypass a').click(function(){setUserSetting('uploader', '1');uploaderMode=1;swfuploadPreLoad();return false;});
1455  });
1456  //]]>
1457  </script>
1458  <div id="media-upload-notice">
1459  <?php if (isset($errors['upload_notice']) ) { ?>
1460      <?php echo $errors['upload_notice']; ?>
1461  <?php } ?>
1462  </div>
1463  <div id="media-upload-error">
1464  <?php if (isset($errors['upload_error']) && is_wp_error($errors['upload_error'])) { ?>
1465      <?php echo $errors['upload_error']->get_error_message(); ?>
1466  <?php } ?>
1467  </div>
1468  <?php
1469  // Check quota for this blog if multisite
1470  if ( is_multisite() && !is_upload_space_available() ) {
1471      echo '<p>' . sprintf( __( 'Sorry, you have filled your storage quota (%s MB).' ), get_space_allowed() ) . '</p>';
1472      return;
1473  }
1474  
1475  do_action('pre-upload-ui');
1476  
1477  if ( $flash ) :
1478  
1479  // Set the post params, which SWFUpload will post back with the file, and pass
1480  // them through a filter.
1481  $post_params = array(
1482          "post_id" => $post_id,
1483          "auth_cookie" => (is_ssl() ? $_COOKIE[SECURE_AUTH_COOKIE] : $_COOKIE[AUTH_COOKIE]),
1484          "logged_in_cookie" => $_COOKIE[LOGGED_IN_COOKIE],
1485          "_wpnonce" => wp_create_nonce('media-form'),
1486          "type" => $type,
1487          "tab" => $tab,
1488          "short" => "1",
1489  );
1490  $post_params = apply_filters( 'swfupload_post_params', $post_params );
1491  $p = array();
1492  foreach ( $post_params as $param => $val )
1493      $p[] = "\t\t'$param' : '$val'";
1494  $post_params_str = implode( ", \n", $p );
1495  
1496  // #8545. wmode=transparent cannot be used with SWFUpload
1497  if ( 'media-new.php' == $pagenow ) {
1498      $upload_image_path = get_user_option( 'admin_color' );
1499      if ( 'classic' != $upload_image_path )
1500          $upload_image_path = 'fresh';
1501      $upload_image_path = admin_url( 'images/upload-' . $upload_image_path . '.png?ver=20101205' );
1502  } else {
1503      $upload_image_path = includes_url( 'images/upload.png?ver=20100531' );
1504  }
1505  
1506  ?>
1507  <script type="text/javascript">
1508  //<![CDATA[
1509  var swfu;
1510  SWFUpload.onload = function() {
1511      var settings = {
1512              button_text: '<span class="button"><?php _e('Select Files'); ?><\/span>',
1513              button_text_style: '.button { text-align: center; font-weight: bold; font-family:"Lucida Grande",Verdana,Arial,"Bitstream Vera Sans",sans-serif; font-size: 11px; text-shadow: 0 1px 0 #FFFFFF; color:#464646; }',
1514              button_height: "23",
1515              button_width: "132",
1516              button_text_top_padding: 3,
1517              button_image_url: '<?php echo $upload_image_path; ?>',
1518              button_placeholder_id: "flash-browse-button",
1519              upload_url : "<?php echo esc_attr( $flash_action_url ); ?>",
1520              flash_url : "<?php echo includes_url('js/swfupload/swfupload.swf'); ?>",
1521              file_post_name: "async-upload",
1522              file_types: "<?php echo apply_filters('upload_file_glob', '*.*'); ?>",
1523              post_params : {
1524                  <?php echo $post_params_str; ?>
1525              },
1526              file_size_limit : "<?php echo $max_upload_size; ?>b",
1527              file_dialog_start_handler : fileDialogStart,
1528              file_queued_handler : fileQueued,
1529              upload_start_handler : uploadStart,
1530              upload_progress_handler : uploadProgress,
1531              upload_error_handler : uploadError,
1532              upload_success_handler : <?php echo apply_filters( 'swfupload_success_handler', 'uploadSuccess' ); ?>,
1533              upload_complete_handler : uploadComplete,
1534              file_queue_error_handler : fileQueueError,
1535              file_dialog_complete_handler : fileDialogComplete,
1536              swfupload_pre_load_handler: swfuploadPreLoad,
1537              swfupload_load_failed_handler: swfuploadLoadFailed,
1538              custom_settings : {
1539                  degraded_element_id : "html-upload-ui", // id of the element displayed when swfupload is unavailable
1540                  swfupload_element_id : "flash-upload-ui" // id of the element displayed when swfupload is available
1541              },
1542              debug: false
1543          };
1544          swfu = new SWFUpload(settings);
1545  };
1546  //]]>
1547  </script>
1548  
1549  <div id="flash-upload-ui" class="hide-if-no-js">
1550  <?php do_action('pre-flash-upload-ui'); ?>
1551  
1552      <div>
1553      <?php _e( 'Choose files to upload' ); ?>
1554      <div id="flash-browse-button"></div>
1555      <span><input id="cancel-upload" disabled="disabled" onclick="cancelUpload()" type="button" value="<?php esc_attr_e('Cancel Upload'); ?>" class="button" /></span>
1556      </div>
1557      <p class="media-upload-size"><?php printf( __( 'Maximum upload file size: %d%s' ), $upload_size_unit, $sizes[$u] ); ?></p>
1558  <?php do_action('post-flash-upload-ui'); ?>
1559      <p class="howto"><?php _e('After a file has been uploaded, you can add titles and descriptions.'); ?></p>
1560  </div>
1561  <?php endif; // $flash ?>
1562  
1563  <div id="html-upload-ui" class="hide-if-js">
1564  <?php do_action('pre-html-upload-ui'); ?>
1565      <p id="async-upload-wrap">
1566          <label class="screen-reader-text" for="async-upload"><?php _e('Upload'); ?></label>
1567          <input type="file" name="async-upload" id="async-upload" />
1568          <?php submit_button( __( 'Upload' ), 'button', 'html-upload', false ); ?>
1569          <a href="#" onclick="try{top.tb_remove();}catch(e){}; return false;"><?php _e('Cancel'); ?></a>
1570      </p>
1571      <div class="clear"></div>
1572      <p class="media-upload-size"><?php printf( __( 'Maximum upload file size: %d%s' ), $upload_size_unit, $sizes[$u] ); ?></p>
1573      <?php if ( is_lighttpd_before_150() ): ?>
1574      <p><?php _e('If you want to use all capabilities of the uploader, like uploading multiple files at once, please update to lighttpd 1.5.'); ?></p>
1575      <?php endif;?>
1576  <?php do_action('post-html-upload-ui', $flash); ?>
1577  </div>
1578  <?php do_action('post-upload-ui'); ?>
1579  <?php
1580  }
1581  
1582  /**
1583   * {@internal Missing Short Description}}
1584   *
1585   * @since 2.5.0
1586   *
1587   * @param unknown_type $type
1588   * @param unknown_type $errors
1589   * @param unknown_type $id
1590   */
1591  function media_upload_type_form($type = 'file', $errors = null, $id = null) {
1592      media_upload_header();
1593  
1594      $post_id = isset( $_REQUEST['post_id'] )? intval( $_REQUEST['post_id'] ) : 0;
1595  
1596      $form_action_url = admin_url("media-upload.php?type=$type&tab=type&post_id=$post_id");
1597      $form_action_url = apply_filters('media_upload_form_url', $form_action_url, $type);
1598  ?>
1599  
1600  <form enctype="multipart/form-data" method="post" action="<?php echo esc_attr($form_action_url); ?>" class="media-upload-form type-form validate" id="<?php echo $type; ?>-form">
1601  <?php submit_button( '', 'hidden', 'save', false ); ?>
1602  <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
1603  <?php wp_nonce_field('media-form'); ?>
1604  
1605  <h3 class="media-title"><?php _e('Add media files from your computer'); ?></h3>
1606  
1607  <?php media_upload_form( $errors ); ?>
1608  
1609  <script type="text/javascript">
1610  //<![CDATA[
1611  jQuery(function($){
1612      var preloaded = $(".media-item.preloaded");
1613      if ( preloaded.length > 0 ) {
1614          preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
1615      }
1616      updateMediaForm();
1617  });
1618  //]]>
1619  </script>
1620  <div id="media-items">
1621  <?php
1622  if ( $id ) {
1623      if ( !is_wp_error($id) ) {
1624          add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2);
1625          echo get_media_items( $id, $errors );
1626      } else {
1627          echo '<div id="media-upload-error">'.esc_html($id->get_error_message()).'</div>';
1628          exit;
1629      }
1630  }
1631  ?>
1632  </div>
1633  <p class="savebutton ml-submit">
1634  <?php submit_button( __( 'Save all changes' ), 'button', 'save', false ); ?>
1635  </p>
1636  </form>
1637  <?php
1638  }
1639  
1640  /**
1641   * {@internal Missing Short Description}}
1642   *
1643   * @since 2.7.0
1644   *
1645   * @param unknown_type $type
1646   * @param unknown_type $errors
1647   * @param unknown_type $id
1648   */
1649  function media_upload_type_url_form($type = 'file', $errors = null, $id = null) {
1650      media_upload_header();
1651  
1652      $post_id = intval($_REQUEST['post_id']);
1653  
1654      $form_action_url = admin_url("media-upload.php?type=$type&tab=type&post_id=$post_id");
1655      $form_action_url = apply_filters('media_upload_form_url', $form_action_url, $type);
1656  
1657      $callback = "type_url_form_$type";
1658  ?>
1659  
1660  <form enctype="multipart/form-data" method="post" action="<?php echo esc_attr($form_action_url); ?>" class="media-upload-form type-form validate" id="<?php echo $type; ?>-form">
1661  <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
1662  <?php wp_nonce_field('media-form'); ?>
1663  
1664  <?php if ( is_callable($callback) ) { ?>
1665  
1666  <h3 class="media-title"><?php _e('Add media file from URL'); ?></h3>
1667  
1668  <script type="text/javascript">
1669  //<![CDATA[
1670  var addExtImage = {
1671  
1672      width : '',
1673      height : '',
1674      align : 'alignnone',
1675  
1676      insert : function() {
1677          var t = this, html, f = document.forms[0], cls, title = '', alt = '', caption = '';
1678  
1679          if ( '' == f.src.value || '' == t.width )
1680              return false;
1681  
1682          if ( f.title.value ) {
1683              title = f.title.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
1684              title = ' title="'+title+'"';
1685          }
1686  
1687          if ( f.alt.value )
1688              alt = f.alt.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
1689  
1690  <?php if ( ! apply_filters( 'disable_captions', '' ) ) { ?>
1691          if ( f.caption.value )
1692              caption = f.caption.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
1693  <?php } ?>
1694  
1695          cls = caption ? '' : ' class="'+t.align+'"';
1696  
1697          html = '<img alt="'+alt+'" src="'+f.src.value+'"'+title+cls+' width="'+t.width+'" height="'+t.height+'" />';
1698  
1699          if ( f.url.value )
1700              html = '<a href="'+f.url.value+'">'+html+'</a>';
1701  
1702          if ( caption )
1703              html = '[caption id="" align="'+t.align+'" width="'+t.width+'" caption="'+caption+'"]'+html+'[/caption]';
1704  
1705          var win = window.dialogArguments || opener || parent || top;
1706          win.send_to_editor(html);
1707          return false;
1708      },
1709  
1710      resetImageData : function() {
1711          var t = addExtImage;
1712  
1713          t.width = t.height = '';
1714          document.getElementById('go_button').style.color = '#bbb';
1715          if ( ! document.forms[0].src.value )
1716              document.getElementById('status_img').innerHTML = '*';
1717          else document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/no.png' ) ); ?>" alt="" />';
1718      },
1719  
1720      updateImageData : function() {
1721          var t = addExtImage;
1722  
1723          t.width = t.preloadImg.width;
1724          t.height = t.preloadImg.height;
1725          document.getElementById('go_button').style.color = '#333';
1726          document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/yes.png' ) ); ?>" alt="" />';
1727      },
1728  
1729      getImageData : function() {
1730          var t = addExtImage, src = document.forms[0].src.value;
1731  
1732          if ( ! src ) {
1733              t.resetImageData();
1734              return false;
1735          }
1736          document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" alt="" />';
1737          t.preloadImg = new Image();
1738          t.preloadImg.onload = t.updateImageData;
1739          t.preloadImg.onerror = t.resetImageData;
1740          t.preloadImg.src = src;
1741      }
1742  }
1743  //]]>
1744  </script>
1745  
1746  <div id="media-items">
1747  <div class="media-item media-blank">
1748  <?php echo apply_filters($callback, call_user_func($callback)); ?>
1749  </div>
1750  </div>
1751  </form>
1752  <?php
1753      } else {
1754          wp_die( __('Unknown action.') );
1755      }
1756  }
1757  
1758  /**
1759   * {@internal Missing Short Description}}
1760   *
1761   * @since 2.5.0
1762   *
1763   * @param unknown_type $errors
1764   */
1765  function media_upload_gallery_form($errors) {
1766      global $redir_tab, $type;
1767  
1768      $redir_tab = 'gallery';
1769      media_upload_header();
1770  
1771      $post_id = intval($_REQUEST['post_id']);
1772      $form_action_url = admin_url("media-upload.php?type=$type&tab=gallery&post_id=$post_id");
1773      $form_action_url = apply_filters('media_upload_form_url', $form_action_url, $type);
1774  ?>
1775  
1776  <script type="text/javascript">
1777  <!--
1778  jQuery(function($){
1779      var preloaded = $(".media-item.preloaded");
1780      if ( preloaded.length > 0 ) {
1781          preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
1782          updateMediaForm();
1783      }
1784  });
1785  -->
1786  </script>
1787  <div id="sort-buttons" class="hide-if-no-js">
1788  <span>
1789  <?php _e('All Tabs:'); ?>
1790  <a href="#" id="showall"><?php _e('Show'); ?></a>
1791  <a href="#" id="hideall" style="display:none;"><?php _e('Hide'); ?></a>
1792  </span>
1793  <?php _e('Sort Order:'); ?>
1794  <a href="#" id="asc"><?php _e('Ascending'); ?></a> |
1795  <a href="#" id="desc"><?php _e('Descending'); ?></a> |
1796  <a href="#" id="clear"><?php _ex('Clear', 'verb'); ?></a>
1797  </div>
1798  <form enctype="multipart/form-data" method="post" action="<?php echo esc_attr($form_action_url); ?>" class="media-upload-form validate" id="gallery-form">
1799  <?php wp_nonce_field('media-form'); ?>
1800  <?php //media_upload_form( $errors ); ?>
1801  <table class="widefat" cellspacing="0">
1802  <thead><tr>
1803  <th><?php _e('Media'); ?></th>
1804  <th class="order-head"><?php _e('Order'); ?></th>
1805  <th class="actions-head"><?php _e('Actions'); ?></th>
1806  </tr></thead>
1807  </table>
1808  <div id="media-items">
1809  <?php add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2); ?>
1810  <?php echo get_media_items($post_id, $errors); ?>
1811  </div>
1812  
1813  <p class="ml-submit">
1814  <?php submit_button( __( 'Save all changes' ), 'button savebutton', 'save', false, array( 'id' => 'save-all', 'style' => 'display: none;' ) ); ?>
1815  <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
1816  <input type="hidden" name="type" value="<?php echo esc_attr( $GLOBALS['type'] ); ?>" />
1817  <input type="hidden" name="tab" value="<?php echo esc_attr( $GLOBALS['tab'] ); ?>" />
1818  </p>
1819  
1820  <div id="gallery-settings" style="display:none;">
1821  <div class="title"><?php _e('Gallery Settings'); ?></div>
1822  <table id="basic" class="describe"><tbody>
1823      <tr>
1824      <th scope="row" class="label">
1825          <label>
1826          <span class="alignleft"><?php _e('Link thumbnails to:'); ?></span>
1827          </label>
1828      </th>
1829      <td class="field">
1830          <input type="radio" name="linkto" id="linkto-file" value="file" />
1831          <label for="linkto-file" class="radio"><?php _e('Image File'); ?></label>
1832  
1833          <input type="radio" checked="checked" name="linkto" id="linkto-post" value="post" />
1834          <label for="linkto-post" class="radio"><?php _e('Attachment Page'); ?></label>
1835      </td>
1836      </tr>
1837  
1838      <tr>
1839      <th scope="row" class="label">
1840          <label>
1841          <span class="alignleft"><?php _e('Order images by:'); ?></span>
1842          </label>
1843      </th>
1844      <td class="field">
1845          <select id="orderby" name="orderby">
1846              <option value="menu_order" selected="selected"><?php _e('Menu order'); ?></option>
1847              <option value="title"><?php _e('Title'); ?></option>
1848              <option value="ID"><?php _e('Date/Time'); ?></option>
1849              <option value="rand"><?php _e('Random'); ?></option>
1850          </select>
1851      </td>
1852      </tr>
1853  
1854      <tr>
1855      <th scope="row" class="label">
1856          <label>
1857          <span class="alignleft"><?php _e('Order:'); ?></span>
1858          </label>
1859      </th>
1860      <td class="field">
1861          <input type="radio" checked="checked" name="order" id="order-asc" value="asc" />
1862          <label for="order-asc" class="radio"><?php _e('Ascending'); ?></label>
1863  
1864          <input type="radio" name="order" id="order-desc" value="desc" />
1865          <label for="order-desc" class="radio"><?php _e('Descending'); ?></label>
1866      </td>
1867      </tr>
1868  
1869      <tr>
1870      <th scope="row" class="label">
1871          <label>
1872          <span class="alignleft"><?php _e('Gallery columns:'); ?></span>
1873          </label>
1874      </th>
1875      <td class="field">
1876          <select id="columns" name="columns">
1877              <option value="1">1</option>
1878              <option value="2">2</option>
1879              <option value="3" selected="selected">3</option>
1880              <option value="4">4</option>
1881              <option value="5">5</option>
1882              <option value="6">6</option>
1883              <option value="7">7</option>
1884              <option value="8">8</option>
1885              <option value="9">9</option>
1886          </select>
1887      </td>
1888      </tr>
1889  </tbody></table>
1890  
1891  <p class="ml-submit">
1892  <input type="button" class="button" style="display:none;" onMouseDown="wpgallery.update();" name="insert-gallery" id="insert-gallery" value="<?php esc_attr_e( 'Insert gallery' ); ?>" />
1893  <input type="button" class="button" style="display:none;" onMouseDown="wpgallery.update();" name="update-gallery" id="update-gallery" value="<?php esc_attr_e( 'Update gallery settings' ); ?>" />
1894  </p>
1895  </div>
1896  </form>
1897  <?php
1898  }
1899  
1900  /**
1901   * {@internal Missing Short Description}}
1902   *
1903   * @since 2.5.0
1904   *
1905   * @param unknown_type $errors
1906   */
1907  function media_upload_library_form($errors) {
1908      global $wpdb, $wp_query, $wp_locale, $type, $tab, $post_mime_types;
1909  
1910      media_upload_header();
1911  
1912      $post_id = intval($_REQUEST['post_id']);
1913  
1914      $form_action_url = admin_url("media-upload.php?type=$type&tab=library&post_id=$post_id");
1915      $form_action_url = apply_filters('media_upload_form_url', $form_action_url, $type);
1916  
1917      $_GET['paged'] = isset( $_GET['paged'] ) ? intval($_GET['paged']) : 0;
1918      if ( $_GET['paged'] < 1 )
1919          $_GET['paged'] = 1;
1920      $start = ( $_GET['paged'] - 1 ) * 10;
1921      if ( $start < 1 )
1922          $start = 0;
1923      add_filter( 'post_limits', create_function( '$a', "return 'LIMIT $start, 10';" ) );
1924  
1925      list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
1926  
1927  ?>
1928  
1929  <form id="filter" action="" method="get">
1930  <input type="hidden" name="type" value="<?php echo esc_attr( $type ); ?>" />
1931  <input type="hidden" name="tab" value="<?php echo esc_attr( $tab ); ?>" />
1932  <input type="hidden" name="post_id" value="<?php echo (int) $post_id; ?>" />
1933  <input type="hidden" name="post_mime_type" value="<?php echo isset( $_GET['post_mime_type'] ) ? esc_attr( $_GET['post_mime_type'] ) : ''; ?>" />
1934  
1935  <p id="media-search" class="search-box">
1936      <label class="screen-reader-text" for="media-search-input"><?php _e('Search Media');?>:</label>
1937      <input type="text" id="media-search-input" name="s" value="<?php the_search_query(); ?>" />
1938      <?php submit_button( __( 'Search Media' ), 'button', '', false ); ?>
1939  </p>
1940  
1941  <ul class="subsubsub">
1942  <?php
1943  $type_links = array();
1944  $_num_posts = (array) wp_count_attachments();
1945  $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
1946  foreach ( $matches as $_type => $reals )
1947      foreach ( $reals as $real )
1948          if ( isset($num_posts[$_type]) )
1949              $num_posts[$_type] += $_num_posts[$real];
1950          else
1951              $num_posts[$_type] = $_num_posts[$real];
1952  // If available type specified by media button clicked, filter by that type
1953  if ( empty($_GET['post_mime_type']) && !empty($num_posts[$type]) ) {
1954      $_GET['post_mime_type'] = $type;
1955      list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
1956  }
1957  if ( empty($_GET['post_mime_type']) || $_GET['post_mime_type'] == 'all' )
1958      $class = ' class="current"';
1959  else
1960      $class = '';
1961  $type_links[] = "<li><a href='" . esc_url(add_query_arg(array('post_mime_type'=>'all', 'paged'=>false, 'm'=>false))) . "'$class>".__('All Types')."</a>";
1962  foreach ( $post_mime_types as $mime_type => $label ) {
1963      $class = '';
1964  
1965      if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
1966          continue;
1967  
1968      if ( isset($_GET['post_mime_type']) && wp_match_mime_types($mime_type, $_GET['post_mime_type']) )
1969          $class = ' class="current"';
1970  
1971      $type_links[] = "<li><a href='" . esc_url(add_query_arg(array('post_mime_type'=>$mime_type, 'paged'=>false))) . "'$class>" . sprintf( translate_nooped_plural( $label[2], $num_posts[$mime_type] ), "<span id='$mime_type-counter'>" . number_format_i18n( $num_posts[$mime_type] ) . '</span>') . '</a>';
1972  }
1973  echo implode(' | </li>', apply_filters( 'media_upload_mime_type_links', $type_links ) ) . '</li>';
1974  unset($type_links);
1975  ?>
1976  </ul>
1977  
1978  <div class="tablenav">
1979  
1980  <?php
1981  $page_links = paginate_links( array(
1982      'base' => add_query_arg( 'paged', '%#%' ),
1983      'format' => '',
1984      'prev_text' => __('&laquo;'),
1985      'next_text' => __('&raquo;'),
1986      'total' => ceil($wp_query->found_posts / 10),
1987      'current' => $_GET['paged']
1988  ));
1989  
1990  if ( $page_links )
1991      echo "<div class='tablenav-pages'>$page_links</div>";
1992  ?>
1993  
1994  <div class="alignleft actions">
1995  <?php
1996  
1997  $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";
1998  
1999  $arc_result = $wpdb->get_results( $arc_query );
2000  
2001  $month_count = count($arc_result);
2002  
2003  if ( $month_count && !( 1 == $month_count && 0 == $arc_result[0]->mmonth ) ) { ?>
2004  <select name='m'>
2005  <option<?php selected( @$_GET['m'], 0 ); ?> value='0'><?php _e('Show all dates'); ?></option>
2006  <?php
2007  foreach ($arc_result as $arc_row) {
2008      if ( $arc_row->yyear == 0 )
2009          continue;
2010      $arc_row->mmonth = zeroise( $arc_row->mmonth, 2 );
2011  
2012      if ( isset($_GET['m']) && ( $arc_row->yyear . $arc_row->mmonth == $_GET['m'] ) )
2013          $default = ' selected="selected"';
2014      else
2015          $default = '';
2016  
2017      echo "<option$default value='" . esc_attr( $arc_row->yyear . $arc_row->mmonth ) . "'>";
2018      echo esc_html( $wp_locale->get_month($arc_row->mmonth) . " $arc_row->yyear" );
2019      echo "</option>\n";
2020  }
2021  ?>
2022  </select>
2023  <?php } ?>
2024  
2025  <?php submit_button( __( 'Filter &#187;' ), 'secondary', 'post-query-submit', false ); ?>
2026  
2027  </div>
2028  
2029  <br class="clear" />
2030  </div>
2031  </form>
2032  
2033  <form enctype="multipart/form-data" method="post" action="<?php echo esc_attr($form_action_url); ?>" class="media-upload-form validate" id="library-form">
2034  
2035  <?php wp_nonce_field('media-form'); ?>
2036  <?php //media_upload_form( $errors ); ?>
2037  
2038  <script type="text/javascript">
2039  <!--
2040  jQuery(function($){
2041      var preloaded = $(".media-item.preloaded");
2042      if ( preloaded.length > 0 ) {
2043          preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
2044          updateMediaForm();
2045      }
2046  });
2047  -->
2048  </script>
2049  
2050  <div id="media-items">
2051  <?php add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2); ?>
2052  <?php echo get_media_items(null, $errors); ?>
2053  </div>
2054  <p class="ml-submit">
2055  <?php submit_button( __( 'Save all changes' ), 'button savebutton', 'save', false ); ?>
2056  <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
2057  </p>
2058  </form>
2059  <?php
2060  }
2061  
2062  /**
2063   * {@internal Missing Short Description}}
2064   *
2065   * @since 2.7.0
2066   *
2067   * @return unknown
2068   */
2069  function type_url_form_image() {
2070      if ( !apply_filters( 'disable_captions', '' ) ) {
2071          $caption = '
2072          <tr>
2073              <th valign="top" scope="row" class="label">
2074                  <span class="alignleft"><label for="caption">' . __('Image Caption') . '</label></span>
2075              </th>
2076              <td class="field"><input id="caption" name="caption" value="" type="text" /></td>
2077          </tr>
2078  ';
2079      } else {
2080          $caption = '';
2081      }
2082  
2083      $default_align = get_option('image_default_align');
2084      if ( empty($default_align) )
2085          $default_align = 'none';
2086  
2087      return '
2088      <h4 class="media-sub-title">' . __('Insert an image from another web site') . '</h4>
2089      <table class="describe"><tbody>
2090          <tr>
2091              <th valign="top" scope="row" class="label" style="width:130px;">
2092                  <span class="alignleft"><label for="src">' . __('Image URL') . '</label></span>
2093                  <span class="alignright"><abbr id="status_img" title="required" class="required">*</abbr></span>
2094              </th>
2095              <td class="field"><input id="src" name="src" value="" type="text" aria-required="true" onblur="addExtImage.getImageData()" /></td>
2096          </tr>
2097  
2098          <tr>
2099              <th valign="top" scope="row" class="label">
2100                  <span class="alignleft"><label for="title">' . __('Image Title') . '</label></span>
2101                  <span class="alignright"><abbr title="required" class="required">*</abbr></span>
2102              </th>
2103              <td class="field"><input id="title" name="title" value="" type="text" aria-required="true" /></td>
2104          </tr>
2105  
2106          <tr>
2107              <th valign="top" scope="row" class="label">