[ Index ]

PHP Cross Reference of WordPress

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