[ 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   * Defines the default media upload tabs
  11   *
  12   * @since 2.5.0
  13   *
  14   * @return array default tabs
  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      /**
  25       * Filter the available tabs in the legacy (pre-3.5.0) media popup.
  26       *
  27       * @since 2.5.0
  28       *
  29       * @param array $_default_tabs An array of media tabs.
  30       */
  31      return apply_filters( 'media_upload_tabs', $_default_tabs );
  32  }
  33  
  34  /**
  35   * Adds the gallery tab back to the tabs array if post has image attachments
  36   *
  37   * @since 2.5.0
  38   *
  39   * @param array $tabs
  40   * @return array $tabs with gallery if post has image attachment
  41   */
  42  function update_gallery_tab($tabs) {
  43      global $wpdb;
  44  
  45      if ( !isset($_REQUEST['post_id']) ) {
  46          unset($tabs['gallery']);
  47          return $tabs;
  48      }
  49  
  50      $post_id = intval($_REQUEST['post_id']);
  51  
  52      if ( $post_id )
  53          $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 ) ) );
  54  
  55      if ( empty($attachments) ) {
  56          unset($tabs['gallery']);
  57          return $tabs;
  58      }
  59  
  60      $tabs['gallery'] = sprintf(__('Gallery (%s)'), "<span id='attachments-count'>$attachments</span>");
  61  
  62      return $tabs;
  63  }
  64  add_filter('media_upload_tabs', 'update_gallery_tab');
  65  
  66  /**
  67   * {@internal Missing Short Description}}
  68   *
  69   * @since 2.5.0
  70   */
  71  function the_media_upload_tabs() {
  72      global $redir_tab;
  73      $tabs = media_upload_tabs();
  74      $default = 'type';
  75  
  76      if ( !empty($tabs) ) {
  77          echo "<ul id='sidemenu'>\n";
  78          if ( isset($redir_tab) && array_key_exists($redir_tab, $tabs) ) {
  79              $current = $redir_tab;
  80          } elseif ( isset($_GET['tab']) && array_key_exists($_GET['tab'], $tabs) ) {
  81              $current = $_GET['tab'];
  82          } else {
  83              /** This filter is documented in wp-admin/media-upload.php */
  84              $current = apply_filters( 'media_upload_default_tab', $default );
  85          }
  86  
  87          foreach ( $tabs as $callback => $text ) {
  88              $class = '';
  89  
  90              if ( $current == $callback )
  91                  $class = " class='current'";
  92  
  93              $href = add_query_arg(array('tab' => $callback, 's' => false, 'paged' => false, 'post_mime_type' => false, 'm' => false));
  94              $link = "<a href='" . esc_url($href) . "'$class>$text</a>";
  95              echo "\t<li id='" . esc_attr("tab-$callback") . "'>$link</li>\n";
  96          }
  97          echo "</ul>\n";
  98      }
  99  }
 100  
 101  /**
 102   * {@internal Missing Short Description}}
 103   *
 104   * @since 2.5.0
 105   *
 106   * @param integer $id image attachment id
 107   * @param string $caption image caption
 108   * @param string $alt image alt attribute
 109   * @param string $title image title attribute
 110   * @param string $align image css alignment property
 111   * @param string $url image src url
 112   * @param string|bool $rel image rel attribute
 113   * @param string $size image size (thumbnail, medium, large, full or added  with add_image_size() )
 114   * @return string the html to insert into editor
 115   */
 116  function get_image_send_to_editor($id, $caption, $title, $align, $url='', $rel = false, $size='medium', $alt = '') {
 117  
 118      $html = get_image_tag($id, $alt, '', $align, $size);
 119  
 120      $rel = $rel ? ' rel="attachment wp-att-' . esc_attr($id).'"' : '';
 121  
 122      if ( $url )
 123          $html = '<a href="' . esc_attr($url) . "\"$rel>$html</a>";
 124  
 125      /**
 126       * Filter the image HTML markup to send to the editor.
 127       *
 128       * @since 2.5.0
 129       *
 130       * @param string $html    The image HTML markup to send.
 131       * @param int    $id      The attachment id.
 132       * @param string $caption The image caption.
 133       * @param string $title   The image title.
 134       * @param string $align   The image alignment.
 135       * @param string $url     The image source URL.
 136       * @param string $size    The image size.
 137       * @param string $alt     The image alternative, or alt, text.
 138       */
 139      $html = apply_filters( 'image_send_to_editor', $html, $id, $caption, $title, $align, $url, $size, $alt );
 140  
 141      return $html;
 142  }
 143  
 144  /**
 145   * Adds image shortcode with caption to editor
 146   *
 147   * @since 2.6.0
 148   *
 149   * @param string $html
 150   * @param integer $id
 151   * @param string $caption image caption
 152   * @param string $alt image alt attribute
 153   * @param string $title image title attribute
 154   * @param string $align image css alignment property
 155   * @param string $url image src url
 156   * @param string $size image size (thumbnail, medium, large, full or added with add_image_size() )
 157   * @return string
 158   */
 159  function image_add_caption( $html, $id, $caption, $title, $align, $url, $size, $alt = '' ) {
 160  
 161      /**
 162       * Filter the caption text.
 163       *
 164       * Note: If the caption text is empty, the caption shortcode will not be appended
 165       * to the image HTML when inserted into the editor.
 166       *
 167       * Passing an empty value also prevents the {@see 'image_add_caption_shortcode'}
 168       * filter from being evaluated at the end of {@see image_add_caption()}.
 169       *
 170       * @since 4.1.0
 171       *
 172       * @param string $caption The original caption text.
 173       * @param int    $id      The attachment ID.
 174       */
 175      $caption = apply_filters( 'image_add_caption_text', $caption, $id );
 176  
 177      /**
 178       * Filter whether to disable captions.
 179       *
 180       * Prevents image captions from being appended to image HTML when inserted into the editor.
 181       *
 182       * @since 2.6.0
 183       *
 184       * @param bool $bool Whether to disable appending captions. Returning true to the filter
 185       *                   will disable captions. Default empty string.
 186       */
 187      if ( empty($caption) || apply_filters( 'disable_captions', '' ) )
 188          return $html;
 189  
 190      $id = ( 0 < (int) $id ) ? 'attachment_' . $id : '';
 191  
 192      if ( ! preg_match( '/width=["\']([0-9]+)/', $html, $matches ) )
 193          return $html;
 194  
 195      $width = $matches[1];
 196  
 197      $caption = str_replace( array("\r\n", "\r"), "\n", $caption);
 198      $caption = preg_replace_callback( '/<[a-zA-Z0-9]+(?: [^<>]+>)*/', '_cleanup_image_add_caption', $caption );
 199  
 200      // Convert any remaining line breaks to <br>.
 201      $caption = preg_replace( '/[ \n\t]*\n[ \t]*/', '<br />', $caption );
 202  
 203      $html = preg_replace( '/(class=["\'][^\'"]*)align(none|left|right|center)\s?/', '$1', $html );
 204      if ( empty($align) )
 205          $align = 'none';
 206  
 207      $shcode = '[caption id="' . $id . '" align="align' . $align    . '" width="' . $width . '"]' . $html . ' ' . $caption . '[/caption]';
 208  
 209      /**
 210       * Filter the image HTML markup including the caption shortcode.
 211       *
 212       * @since 2.6.0
 213       *
 214       * @param string $shcode The image HTML markup with caption shortcode.
 215       * @param string $html   The image HTML markup.
 216       */
 217      return apply_filters( 'image_add_caption_shortcode', $shcode, $html );
 218  }
 219  add_filter( 'image_send_to_editor', 'image_add_caption', 20, 8 );
 220  
 221  /**
 222   * Private preg_replace callback used in image_add_caption()
 223   *
 224   * @access private
 225   * @since 3.4.0
 226   */
 227  function _cleanup_image_add_caption( $matches ) {
 228      // Remove any line breaks from inside the tags.
 229      return preg_replace( '/[\r\n\t]+/', ' ', $matches[0] );
 230  }
 231  
 232  /**
 233   * Adds image html to editor
 234   *
 235   * @since 2.5.0
 236   *
 237   * @param string $html
 238   */
 239  function media_send_to_editor($html) {
 240  ?>
 241  <script type="text/javascript">
 242  var win = window.dialogArguments || opener || parent || top;
 243  win.send_to_editor('<?php echo addslashes($html); ?>');
 244  </script>
 245  <?php
 246      exit;
 247  }
 248  
 249  /**
 250   * Save a file submitted from a POST request and create an attachment post for it.
 251   *
 252   * @since 2.5.0
 253   *
 254   * @param string $file_id   Index of the {@link $_FILES} array that the file was sent. Required.
 255   * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
 256   *                          be set to 0, creating a media item that has no relationship to a post.
 257   * @param array  $post_data Overwrite some of the attachment. Optional.
 258   * @param array  $overrides Override the {@link wp_handle_upload()} behavior. Optional.
 259   * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
 260   */
 261  function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array( 'test_form' => false )) {
 262  
 263      $time = current_time('mysql');
 264      if ( $post = get_post($post_id) ) {
 265          if ( substr( $post->post_date, 0, 4 ) > 0 )
 266              $time = $post->post_date;
 267      }
 268  
 269      $name = $_FILES[$file_id]['name'];
 270      $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
 271  
 272      if ( isset($file['error']) )
 273          return new WP_Error( 'upload_error', $file['error'] );
 274  
 275      $name_parts = pathinfo($name);
 276      $name = trim( substr( $name, 0, -(1 + strlen($name_parts['extension'])) ) );
 277  
 278      $url = $file['url'];
 279      $type = $file['type'];
 280      $file = $file['file'];
 281      $title = $name;
 282      $content = '';
 283  
 284      if ( preg_match( '#^audio#', $type ) ) {
 285          $meta = wp_read_audio_metadata( $file );
 286  
 287          if ( ! empty( $meta['title'] ) )
 288              $title = $meta['title'];
 289  
 290          $content = '';
 291  
 292          if ( ! empty( $title ) ) {
 293  
 294              if ( ! empty( $meta['album'] ) && ! empty( $meta['artist'] ) ) {
 295                  /* translators: 1: audio track title, 2: album title, 3: artist name */
 296                  $content .= sprintf( __( '"%1$s" from %2$s by %3$s.' ), $title, $meta['album'], $meta['artist'] );
 297              } elseif ( ! empty( $meta['album'] ) ) {
 298                  /* translators: 1: audio track title, 2: album title */
 299                  $content .= sprintf( __( '"%1$s" from %2$s.' ), $title, $meta['album'] );
 300              } elseif ( ! empty( $meta['artist'] ) ) {
 301                  /* translators: 1: audio track title, 2: artist name */
 302                  $content .= sprintf( __( '"%1$s" by %2$s.' ), $title, $meta['artist'] );
 303              } else {
 304                  $content .= sprintf( __( '"%s".' ), $title );
 305              }
 306  
 307          } elseif ( ! empty( $meta['album'] ) ) {
 308  
 309              if ( ! empty( $meta['artist'] ) ) {
 310                  /* translators: 1: audio album title, 2: artist name */
 311                  $content .= sprintf( __( '%1$s by %2$s.' ), $meta['album'], $meta['artist'] );
 312              } else {
 313                  $content .= $meta['album'] . '.';
 314              }
 315  
 316          } elseif ( ! empty( $meta['artist'] ) ) {
 317  
 318              $content .= $meta['artist'] . '.';
 319  
 320          }
 321  
 322          if ( ! empty( $meta['year'] ) )
 323              $content .= ' ' . sprintf( __( 'Released: %d.' ), $meta['year'] );
 324  
 325          if ( ! empty( $meta['track_number'] ) ) {
 326              $track_number = explode( '/', $meta['track_number'] );
 327              if ( isset( $track_number[1] ) )
 328                  $content .= ' ' . sprintf( __( 'Track %1$s of %2$s.' ), number_format_i18n( $track_number[0] ), number_format_i18n( $track_number[1] ) );
 329              else
 330                  $content .= ' ' . sprintf( __( 'Track %1$s.' ), number_format_i18n( $track_number[0] ) );
 331          }
 332  
 333          if ( ! empty( $meta['genre'] ) )
 334              $content .= ' ' . sprintf( __( 'Genre: %s.' ), $meta['genre'] );
 335  
 336      // Use image exif/iptc data for title and caption defaults if possible.
 337      } elseif ( 0 === strpos( $type, 'image/' ) && $image_meta = @wp_read_image_metadata( $file ) ) {
 338          if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) )
 339              $title = $image_meta['title'];
 340          if ( trim( $image_meta['caption'] ) )
 341              $content = $image_meta['caption'];
 342      }
 343  
 344      // Construct the attachment array
 345      $attachment = array_merge( array(
 346          'post_mime_type' => $type,
 347          'guid' => $url,
 348          'post_parent' => $post_id,
 349          'post_title' => $title,
 350          'post_content' => $content,
 351      ), $post_data );
 352  
 353      // This should never be set as it would then overwrite an existing attachment.
 354      if ( isset( $attachment['ID'] ) )
 355          unset( $attachment['ID'] );
 356  
 357      // Save the data
 358      $id = wp_insert_attachment($attachment, $file, $post_id);
 359      if ( !is_wp_error($id) ) {
 360          wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
 361      }
 362  
 363      return $id;
 364  
 365  }
 366  
 367  /**
 368   * This handles a sideloaded file in the same way as an uploaded file is handled by {@link media_handle_upload()}
 369   *
 370   * @since 2.6.0
 371   *
 372   * @param array $file_array Array similar to a {@link $_FILES} upload array
 373   * @param int $post_id The post ID the media is associated with
 374   * @param string $desc Description of the sideloaded file
 375   * @param array $post_data allows you to overwrite some of the attachment
 376   * @return int|object The ID of the attachment or a WP_Error on failure
 377   */
 378  function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array()) {
 379      $overrides = array('test_form'=>false);
 380  
 381      $time = current_time( 'mysql' );
 382      if ( $post = get_post( $post_id ) ) {
 383          if ( substr( $post->post_date, 0, 4 ) > 0 )
 384              $time = $post->post_date;
 385      }
 386  
 387      $file = wp_handle_sideload( $file_array, $overrides, $time );
 388      if ( isset($file['error']) )
 389          return new WP_Error( 'upload_error', $file['error'] );
 390  
 391      $url = $file['url'];
 392      $type = $file['type'];
 393      $file = $file['file'];
 394      $title = preg_replace('/\.[^.]+$/', '', basename($file));
 395      $content = '';
 396  
 397      // Use image exif/iptc data for title and caption defaults if possible.
 398      if ( $image_meta = @wp_read_image_metadata($file) ) {
 399          if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) )
 400              $title = $image_meta['title'];
 401          if ( trim( $image_meta['caption'] ) )
 402              $content = $image_meta['caption'];
 403      }
 404  
 405      if ( isset( $desc ) )
 406          $title = $desc;
 407  
 408      // Construct the attachment array.
 409      $attachment = array_merge( array(
 410          'post_mime_type' => $type,
 411          'guid' => $url,
 412          'post_parent' => $post_id,
 413          'post_title' => $title,
 414          'post_content' => $content,
 415      ), $post_data );
 416  
 417      // This should never be set as it would then overwrite an existing attachment.
 418      if ( isset( $attachment['ID'] ) )
 419          unset( $attachment['ID'] );
 420  
 421      // Save the attachment metadata
 422      $id = wp_insert_attachment($attachment, $file, $post_id);
 423      if ( !is_wp_error($id) )
 424          wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
 425  
 426      return $id;
 427  }
 428  
 429  /**
 430   * Adds the iframe to display content for the media upload page
 431   *
 432   * @since 2.5.0
 433   *
 434   * @param string|callable $content_func
 435   */
 436  function wp_iframe($content_func /* ... */) {
 437      _wp_admin_html_begin();
 438  ?>
 439  <title><?php bloginfo('name') ?> &rsaquo; <?php _e('Uploads'); ?> &#8212; <?php _e('WordPress'); ?></title>
 440  <?php
 441  
 442  wp_enqueue_style( 'colors' );
 443  // Check callback name for 'media'
 444  if ( ( is_array( $content_func ) && ! empty( $content_func[1] ) && 0 === strpos( (string) $content_func[1], 'media' ) )
 445      || ( ! is_array( $content_func ) && 0 === strpos( $content_func, 'media' ) ) )
 446      wp_enqueue_style( 'media' );
 447  wp_enqueue_style( 'ie' );
 448  ?>
 449  <script type="text/javascript">
 450  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();}}};
 451  var ajaxurl = '<?php echo admin_url( 'admin-ajax.php', 'relative' ); ?>', pagenow = 'media-upload-popup', adminpage = 'media-upload-popup',
 452  isRtl = <?php echo (int) is_rtl(); ?>;
 453  </script>
 454  <?php
 455      /** This action is documented in wp-admin/admin-header.php */
 456      do_action( 'admin_enqueue_scripts', 'media-upload-popup' );
 457  
 458      /**
 459       * Fires when admin styles enqueued for the legacy (pre-3.5.0) media upload popup are printed.
 460       *
 461       * @since 2.9.0
 462       */
 463      do_action( 'admin_print_styles-media-upload-popup' );
 464  
 465      /** This action is documented in wp-admin/admin-header.php */
 466      do_action( 'admin_print_styles' );
 467  
 468      /**
 469       * Fires when admin scripts enqueued for the legacy (pre-3.5.0) media upload popup are printed.
 470       *
 471       * @since 2.9.0
 472       */
 473      do_action( 'admin_print_scripts-media-upload-popup' );
 474  
 475      /** This action is documented in wp-admin/admin-header.php */
 476      do_action( 'admin_print_scripts' );
 477  
 478      /**
 479       * Fires when scripts enqueued for the admin header for the legacy (pre-3.5.0)
 480       * media upload popup are printed.
 481       *
 482       * @since 2.9.0
 483       */
 484      do_action( 'admin_head-media-upload-popup' );
 485  
 486      /** This action is documented in wp-admin/admin-header.php */
 487      do_action( 'admin_head' );
 488  
 489  if ( is_string( $content_func ) ) {
 490      /**
 491       * Fires in the admin header for each specific form tab in the legacy
 492       * (pre-3.5.0) media upload popup.
 493       *
 494       * The dynamic portion of the hook, `$content_func`, refers to the form
 495       * callback for the media upload type. Possible values include
 496       * 'media_upload_type_form', 'media_upload_type_url_form', and
 497       * 'media_upload_library_form'.
 498       *
 499       * @since 2.5.0
 500       */
 501      do_action( "admin_head_{$content_func}" );
 502  }
 503  ?>
 504  </head>
 505  <body<?php if ( isset($GLOBALS['body_id']) ) echo ' id="' . $GLOBALS['body_id'] . '"'; ?> class="wp-core-ui no-js">
 506  <script type="text/javascript">
 507  document.body.className = document.body.className.replace('no-js', 'js');
 508  </script>
 509  <?php
 510      $args = func_get_args();
 511      $args = array_slice($args, 1);
 512      call_user_func_array($content_func, $args);
 513  
 514      /** This action is documented in wp-admin/admin-footer.php */
 515      do_action( 'admin_print_footer_scripts' );
 516  ?>
 517  <script type="text/javascript">if(typeof wpOnload=='function')wpOnload();</script>
 518  </body>
 519  </html>
 520  <?php
 521  }
 522  
 523  /**
 524   * Adds the media button to the editor
 525   *
 526   * @since 2.5.0
 527   *
 528   * @param string $editor_id
 529   */
 530  function media_buttons($editor_id = 'content') {
 531      static $instance = 0;
 532      $instance++;
 533  
 534      $post = get_post();
 535      if ( ! $post && ! empty( $GLOBALS['post_ID'] ) )
 536          $post = $GLOBALS['post_ID'];
 537  
 538      wp_enqueue_media( array(
 539          'post' => $post
 540      ) );
 541  
 542      $img = '<span class="wp-media-buttons-icon"></span> ';
 543  
 544      $id_attribute = $instance === 1 ? ' id="insert-media-button"' : '';
 545      printf( '<a href="#"%s class="button insert-media add_media" data-editor="%s" title="%s">%s</a>',
 546          $id_attribute,
 547          esc_attr( $editor_id ),
 548          esc_attr__( 'Add Media' ),
 549          $img . __( 'Add Media' )
 550      );
 551      /**
 552       * Filter the legacy (pre-3.5.0) media buttons.
 553       *
 554       * @since 2.5.0
 555       * @deprecated 3.5.0 Use 'media_buttons' action instead.
 556       *
 557       * @param string $string Media buttons context. Default empty.
 558       */
 559      $legacy_filter = apply_filters( 'media_buttons_context', '' );
 560  
 561      if ( $legacy_filter ) {
 562          // #WP22559. Close <a> if a plugin started by closing <a> to open their own <a> tag.
 563          if ( 0 === stripos( trim( $legacy_filter ), '</a>' ) )
 564              $legacy_filter .= '</a>';
 565          echo $legacy_filter;
 566      }
 567  }
 568  add_action( 'media_buttons', 'media_buttons' );
 569  
 570  /**
 571   *
 572   * @global int $post_ID
 573   * @param string $type
 574   * @param int $post_id
 575   * @param string $tab
 576   * @return string
 577   */
 578  function get_upload_iframe_src( $type = null, $post_id = null, $tab = null ) {
 579      global $post_ID;
 580  
 581      if ( empty( $post_id ) )
 582          $post_id = $post_ID;
 583  
 584      $upload_iframe_src = add_query_arg( 'post_id', (int) $post_id, admin_url('media-upload.php') );
 585  
 586      if ( $type && 'media' != $type )
 587          $upload_iframe_src = add_query_arg('type', $type, $upload_iframe_src);
 588  
 589      if ( ! empty( $tab ) )
 590          $upload_iframe_src = add_query_arg('tab', $tab, $upload_iframe_src);
 591  
 592      /**
 593       * Filter the upload iframe source URL for a specific media type.
 594       *
 595       * The dynamic portion of the hook name, `$type`, refers to the type
 596       * of media uploaded.
 597       *
 598       * @since 3.0.0
 599       *
 600       * @param string $upload_iframe_src The upload iframe source URL by type.
 601       */
 602      $upload_iframe_src = apply_filters( $type . '_upload_iframe_src', $upload_iframe_src );
 603  
 604      return add_query_arg('TB_iframe', true, $upload_iframe_src);
 605  }
 606  
 607  /**
 608   * {@internal Missing Short Description}}
 609   *
 610   * @since 2.5.0
 611   *
 612   * @return mixed void|object WP_Error on failure
 613   */
 614  function media_upload_form_handler() {
 615      check_admin_referer('media-form');
 616  
 617      $errors = null;
 618  
 619      if ( isset($_POST['send']) ) {
 620          $keys = array_keys($_POST['send']);
 621          $send_id = (int) array_shift($keys);
 622      }
 623  
 624      if ( !empty($_POST['attachments']) ) foreach ( $_POST['attachments'] as $attachment_id => $attachment ) {
 625          $post = $_post = get_post($attachment_id, ARRAY_A);
 626  
 627          if ( !current_user_can( 'edit_post', $attachment_id ) )
 628              continue;
 629  
 630          if ( isset($attachment['post_content']) )
 631              $post['post_content'] = $attachment['post_content'];
 632          if ( isset($attachment['post_title']) )
 633              $post['post_title'] = $attachment['post_title'];
 634          if ( isset($attachment['post_excerpt']) )
 635              $post['post_excerpt'] = $attachment['post_excerpt'];
 636          if ( isset($attachment['menu_order']) )
 637              $post['menu_order'] = $attachment['menu_order'];
 638  
 639          if ( isset($send_id) && $attachment_id == $send_id ) {
 640              if ( isset($attachment['post_parent']) )
 641                  $post['post_parent'] = $attachment['post_parent'];
 642          }
 643  
 644          /**
 645           * Filter the attachment fields to be saved.
 646           *
 647           * @since 2.5.0
 648           *
 649           * @see wp_get_attachment_metadata()
 650           *
 651           * @param WP_Post $post       The WP_Post object.
 652           * @param array   $attachment An array of attachment metadata.
 653           */
 654          $post = apply_filters( 'attachment_fields_to_save', $post, $attachment );
 655  
 656          if ( isset($attachment['image_alt']) ) {
 657              $image_alt = wp_unslash( $attachment['image_alt'] );
 658              if ( $image_alt != get_post_meta($attachment_id, '_wp_attachment_image_alt', true) ) {
 659                  $image_alt = wp_strip_all_tags( $image_alt, true );
 660  
 661                  // Update_meta expects slashed.
 662                  update_post_meta( $attachment_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
 663              }
 664          }
 665  
 666          if ( isset($post['errors']) ) {
 667              $errors[$attachment_id] = $post['errors'];
 668              unset($post['errors']);
 669          }
 670  
 671          if ( $post != $_post )
 672              wp_update_post($post);
 673  
 674          foreach ( get_attachment_taxonomies($post) as $t ) {
 675              if ( isset($attachment[$t]) )
 676                  wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $attachment[$t])), $t, false);
 677          }
 678      }
 679  
 680      if ( isset($_POST['insert-gallery']) || isset($_POST['update-gallery']) ) { ?>
 681          <script type="text/javascript">
 682          var win = window.dialogArguments || opener || parent || top;
 683          win.tb_remove();
 684          </script>
 685          <?php
 686          exit;
 687      }
 688  
 689      if ( isset($send_id) ) {
 690          $attachment = wp_unslash( $_POST['attachments'][$send_id] );
 691  
 692          $html = isset( $attachment['post_title'] ) ? $attachment['post_title'] : '';
 693          if ( !empty($attachment['url']) ) {
 694              $rel = '';
 695              if ( strpos($attachment['url'], 'attachment_id') || get_attachment_link($send_id) == $attachment['url'] )
 696                  $rel = " rel='attachment wp-att-" . esc_attr($send_id) . "'";
 697              $html = "<a href='{$attachment['url']}'$rel>$html</a>";
 698          }
 699  
 700          /**
 701           * Filter the HTML markup for a media item sent to the editor.
 702           *
 703           * @since 2.5.0
 704           *
 705           * @see wp_get_attachment_metadata()
 706           *
 707           * @param string $html       HTML markup for a media item sent to the editor.
 708           * @param int    $send_id    The first key from the $_POST['send'] data.
 709           * @param array  $attachment Array of attachment metadata.
 710           */
 711          $html = apply_filters( 'media_send_to_editor', $html, $send_id, $attachment );
 712          return media_send_to_editor($html);
 713      }
 714  
 715      return $errors;
 716  }
 717  
 718  /**
 719   * {@internal Missing Short Description}}
 720   *
 721   * @since 2.5.0
 722   *
 723   * @return null|string
 724   */
 725  function wp_media_upload_handler() {
 726      $errors = array();
 727      $id = 0;
 728  
 729      if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
 730          check_admin_referer('media-form');
 731          // Upload File button was clicked
 732          $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
 733          unset($_FILES);
 734          if ( is_wp_error($id) ) {
 735              $errors['upload_error'] = $id;
 736              $id = false;
 737          }
 738      }
 739  
 740      if ( !empty($_POST['insertonlybutton']) ) {
 741          $src = $_POST['src'];
 742          if ( !empty($src) && !strpos($src, '://') )
 743              $src = "http://$src";
 744  
 745          if ( isset( $_POST['media_type'] ) && 'image' != $_POST['media_type'] ) {
 746              $title = esc_html( wp_unslash( $_POST['title'] ) );
 747              if ( empty( $title ) )
 748                  $title = esc_html( basename( $src ) );
 749  
 750              if ( $title && $src )
 751                  $html = "<a href='" . esc_url($src) . "'>$title</a>";
 752  
 753              $type = 'file';
 754              if ( ( $ext = preg_replace( '/^.+?\.([^.]+)$/', '$1', $src ) ) && ( $ext_type = wp_ext2type( $ext ) )
 755                  && ( 'audio' == $ext_type || 'video' == $ext_type ) )
 756                      $type = $ext_type;
 757  
 758              /**
 759               * Filter the URL sent to the editor for a specific media type.
 760               *
 761               * The dynamic portion of the hook name, `$type`, refers to the type
 762               * of media being sent.
 763               *
 764               * @since 3.3.0
 765               *
 766               * @param string $html  HTML markup sent to the editor.
 767               * @param string $src   Media source URL.
 768               * @param string $title Media title.
 769               */
 770              $html = apply_filters( $type . '_send_to_editor_url', $html, esc_url_raw( $src ), $title );
 771          } else {
 772              $align = '';
 773              $alt = esc_attr( wp_unslash( $_POST['alt'] ) );
 774              if ( isset($_POST['align']) ) {
 775                  $align = esc_attr( wp_unslash( $_POST['align'] ) );
 776                  $class = " class='align$align'";
 777              }
 778              if ( !empty($src) )
 779                  $html = "<img src='" . esc_url($src) . "' alt='$alt'$class />";
 780  
 781              /**
 782               * Filter the image URL sent to the editor.
 783               *
 784               * @since 2.8.0
 785               *
 786               * @param string $html  HTML markup sent to the editor for an image.
 787               * @param string $src   Image source URL.
 788               * @param string $alt   Image alternate, or alt, text.
 789               * @param string $align The image alignment. Default 'alignnone'. Possible values include
 790               *                      'alignleft', 'aligncenter', 'alignright', 'alignnone'.
 791               */
 792              $html = apply_filters( 'image_send_to_editor_url', $html, esc_url_raw( $src ), $alt, $align );
 793          }
 794  
 795          return media_send_to_editor($html);
 796      }
 797  
 798      if ( isset( $_POST['save'] ) ) {
 799          $errors['upload_notice'] = __('Saved.');
 800          return media_upload_gallery();
 801      } elseif ( ! empty( $_POST ) ) {
 802          $return = media_upload_form_handler();
 803  
 804          if ( is_string($return) )
 805              return $return;
 806          if ( is_array($return) )
 807              $errors = $return;
 808      }
 809  
 810      if ( isset($_GET['tab']) && $_GET['tab'] == 'type_url' ) {
 811          $type = 'image';
 812          if ( isset( $_GET['type'] ) && in_array( $_GET['type'], array( 'video', 'audio', 'file' ) ) )
 813              $type = $_GET['type'];
 814          return wp_iframe( 'media_upload_type_url_form', $type, $errors, $id );
 815      }
 816  
 817      return wp_iframe( 'media_upload_type_form', 'image', $errors, $id );
 818  }
 819  
 820  /**
 821   * Download an image from the specified URL and attach it to a post.
 822   *
 823   * @since 2.6.0
 824   *
 825   * @param string $file The URL of the image to download
 826   * @param int $post_id The post ID the media is to be associated with
 827   * @param string $desc Optional. Description of the image
 828   * @return string|WP_Error Populated HTML img tag on success
 829   */
 830  function media_sideload_image( $file, $post_id, $desc = null ) {
 831      if ( ! empty( $file ) ) {
 832          // Set variables for storage, fix file filename for query strings.
 833          preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
 834          $file_array = array();
 835          $file_array['name'] = basename( $matches[0] );
 836  
 837          // Download file to temp location.
 838          $file_array['tmp_name'] = download_url( $file );
 839  
 840          // If error storing temporarily, return the error.
 841          if ( is_wp_error( $file_array['tmp_name'] ) ) {
 842              return $file_array['tmp_name'];
 843          }
 844  
 845          // Do the validation and storage stuff.
 846          $id = media_handle_sideload( $file_array, $post_id, $desc );
 847  
 848          // If error storing permanently, unlink.
 849          if ( is_wp_error( $id ) ) {
 850              @unlink( $file_array['tmp_name'] );
 851              return $id;
 852          }
 853  
 854          $src = wp_get_attachment_url( $id );
 855      }
 856  
 857      // Finally check to make sure the file has been saved, then return the HTML.
 858      if ( ! empty( $src ) ) {
 859          $alt = isset( $desc ) ? esc_attr( $desc ) : '';
 860          $html = "<img src='$src' alt='$alt' />";
 861          return $html;
 862      }
 863  }
 864  
 865  /**
 866   * {@internal Missing Short Description}}
 867   *
 868   * @since 2.5.0
 869   *
 870   * @return string|null
 871   */
 872  function media_upload_gallery() {
 873      $errors = array();
 874  
 875      if ( !empty($_POST) ) {
 876          $return = media_upload_form_handler();
 877  
 878          if ( is_string($return) )
 879              return $return;
 880          if ( is_array($return) )
 881              $errors = $return;
 882      }
 883  
 884      wp_enqueue_script('admin-gallery');
 885      return wp_iframe( 'media_upload_gallery_form', $errors );
 886  }
 887  
 888  /**
 889   * {@internal Missing Short Description}}
 890   *
 891   * @since 2.5.0
 892   *
 893   * @return string|null
 894   */
 895  function media_upload_library() {
 896      $errors = array();
 897      if ( !empty($_POST) ) {
 898          $return = media_upload_form_handler();
 899  
 900          if ( is_string($return) )
 901              return $return;
 902          if ( is_array($return) )
 903              $errors = $return;
 904      }
 905  
 906      return wp_iframe( 'media_upload_library_form', $errors );
 907  }
 908  
 909  /**
 910   * Retrieve HTML for the image alignment radio buttons with the specified one checked.
 911   *
 912   * @since 2.7.0
 913   *
 914   * @param WP_Post $post
 915   * @param string $checked
 916   * @return string
 917   */
 918  function image_align_input_fields( $post, $checked = '' ) {
 919  
 920      if ( empty($checked) )
 921          $checked = get_user_setting('align', 'none');
 922  
 923      $alignments = array('none' => __('None'), 'left' => __('Left'), 'center' => __('Center'), 'right' => __('Right'));
 924      if ( !array_key_exists( (string) $checked, $alignments ) )
 925          $checked = 'none';
 926  
 927      $out = array();
 928      foreach ( $alignments as $name => $label ) {
 929          $name = esc_attr($name);
 930          $out[] = "<input type='radio' name='attachments[{$post->ID}][align]' id='image-align-{$name}-{$post->ID}' value='$name'".
 931              ( $checked == $name ? " checked='checked'" : "" ) .
 932              " /><label for='image-align-{$name}-{$post->ID}' class='align image-align-{$name}-label'>$label</label>";
 933      }
 934      return join("\n", $out);
 935  }
 936  
 937  /**
 938   * Retrieve HTML for the size radio buttons with the specified one checked.
 939   *
 940   * @since 2.7.0
 941   *
 942   * @param WP_Post $post
 943   * @param bool|string $check
 944   * @return array
 945   */
 946  function image_size_input_fields( $post, $check = '' ) {
 947      /**
 948       * Filter the names and labels of the default image sizes.
 949       *
 950       * @since 3.3.0
 951       *
 952       * @param array $size_names Array of image sizes and their names. Default values
 953       *                          include 'Thumbnail', 'Medium', 'Large', 'Full Size'.
 954       */
 955      $size_names = apply_filters( 'image_size_names_choose', array(
 956          'thumbnail' => __( 'Thumbnail' ),
 957          'medium'    => __( 'Medium' ),
 958          'large'     => __( 'Large' ),
 959          'full'      => __( 'Full Size' )
 960      ) );
 961  
 962      if ( empty( $check ) ) {
 963          $check = get_user_setting('imgsize', 'medium');
 964      }
 965      $out = array();
 966  
 967      foreach ( $size_names as $size => $label ) {
 968          $downsize = image_downsize( $post->ID, $size );
 969          $checked = '';
 970  
 971          // Is this size selectable?
 972          $enabled = ( $downsize[3] || 'full' == $size );
 973          $css_id = "image-size-{$size}-{$post->ID}";
 974  
 975          // If this size is the default but that's not available, don't select it.
 976          if ( $size == $check ) {
 977              if ( $enabled ) {
 978                  $checked = " checked='checked'";
 979              } else {
 980                  $check = '';
 981              }
 982          } elseif ( ! $check && $enabled && 'thumbnail' != $size ) {
 983              /*
 984               * If $check is not enabled, default to the first available size
 985               * that's bigger than a thumbnail.
 986               */
 987              $check = $size;
 988              $checked = " checked='checked'";
 989          }
 990  
 991          $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 />";
 992  
 993          $html .= "<label for='{$css_id}'>$label</label>";
 994  
 995          // Only show the dimensions if that choice is available.
 996          if ( $enabled ) {
 997              $html .= " <label for='{$css_id}' class='help'>" . sprintf( "(%d&nbsp;&times;&nbsp;%d)", $downsize[1], $downsize[2] ). "</label>";
 998          }
 999          $html .= '</div>';
1000  
1001          $out[] = $html;
1002      }
1003  
1004      return array(
1005          'label' => __( 'Size' ),
1006          'input' => 'html',
1007          'html'  => join( "\n", $out ),
1008      );
1009  }
1010  
1011  /**
1012   * Retrieve HTML for the Link URL buttons with the default link type as specified.
1013   *
1014   * @since 2.7.0
1015   *
1016   * @param WP_Post $post
1017   * @param string $url_type
1018   * @return string
1019   */
1020  function image_link_input_fields($post, $url_type = '') {
1021  
1022      $file = wp_get_attachment_url($post->ID);
1023      $link = get_attachment_link($post->ID);
1024  
1025      if ( empty($url_type) )
1026          $url_type = get_user_setting('urlbutton', 'post');
1027  
1028      $url = '';
1029      if ( $url_type == 'file' )
1030          $url = $file;
1031      elseif ( $url_type == 'post' )
1032          $url = $link;
1033  
1034      return "
1035      <input type='text' class='text urlfield' name='attachments[$post->ID][url]' value='" . esc_attr($url) . "' /><br />
1036      <button type='button' class='button urlnone' data-link-url=''>" . __('None') . "</button>
1037      <button type='button' class='button urlfile' data-link-url='" . esc_attr($file) . "'>" . __('File URL') . "</button>
1038      <button type='button' class='button urlpost' data-link-url='" . esc_attr($link) . "'>" . __('Attachment Post URL') . "</button>
1039  ";
1040  }
1041  
1042  function wp_caption_input_textarea($edit_post) {
1043      // Post data is already escaped.
1044      $name = "attachments[{$edit_post->ID}][post_excerpt]";
1045  
1046      return '<textarea name="' . $name . '" id="' . $name . '">' . $edit_post->post_excerpt . '</textarea>';
1047  }
1048  
1049  /**
1050   * {@internal Missing Short Description}}
1051   *
1052   * @since 2.5.0
1053   *
1054   * @param array $form_fields
1055   * @param object $post
1056   * @return array
1057   */
1058  function image_attachment_fields_to_edit($form_fields, $post) {
1059      return $form_fields;
1060  }
1061  
1062  /**
1063   * {@internal Missing Short Description}}
1064   *
1065   * @since 2.5.0
1066   *
1067   * @param array   $form_fields An array of attachment form fields.
1068   * @param WP_Post $post        The WP_Post attachment object.
1069   * @return array Filtered attachment form fields.
1070   */
1071  function media_single_attachment_fields_to_edit( $form_fields, $post ) {
1072      unset($form_fields['url'], $form_fields['align'], $form_fields['image-size']);
1073      return $form_fields;
1074  }
1075  
1076  /**
1077   * {@internal Missing Short Description}}
1078   *
1079   * @since 2.8.0
1080   *
1081   * @param array   $form_fields An array of attachment form fields.
1082   * @param WP_Post $post        The WP_Post attachment object.
1083   * @return array Filtered attachment form fields.
1084   */
1085  function media_post_single_attachment_fields_to_edit( $form_fields, $post ) {
1086      unset($form_fields['image_url']);
1087      return $form_fields;
1088  }
1089  
1090  /**
1091   * Filters input from media_upload_form_handler() and assigns a default
1092   * post_title from the file name if none supplied.
1093   *
1094   * Illustrates the use of the attachment_fields_to_save filter
1095   * which can be used to add default values to any field before saving to DB.
1096   *
1097   * @since 2.5.0
1098   *
1099   * @param array $post       The WP_Post attachment object converted to an array.
1100   * @param array $attachment An array of attachment metadata.
1101   * @return array Filtered attachment post object.
1102   */
1103  function image_attachment_fields_to_save( $post, $attachment ) {
1104      if ( substr( $post['post_mime_type'], 0, 5 ) == 'image' ) {
1105          if ( strlen( trim( $post['post_title'] ) ) == 0 ) {
1106              $attachment_url = ( isset( $post['attachment_url'] ) ) ? $post['attachment_url'] : $post['guid'];
1107              $post['post_title'] = preg_replace( '/\.\w+$/', '', wp_basename( $attachment_url ) );
1108              $post['errors']['post_title']['errors'][] = __( 'Empty Title filled from filename.' );
1109          }
1110      }
1111  
1112      return $post;
1113  }
1114  
1115  add_filter( 'attachment_fields_to_save', 'image_attachment_fields_to_save', 10, 2 );
1116  
1117  /**
1118   * {@internal Missing Short Description}}
1119   *
1120   * @since 2.5.0
1121   *
1122   * @param string $html
1123   * @param integer $attachment_id
1124   * @param array $attachment
1125   * @return string
1126   */
1127  function image_media_send_to_editor($html, $attachment_id, $attachment) {
1128      $post = get_post($attachment_id);
1129      if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
1130          $url = $attachment['url'];
1131          $align = !empty($attachment['align']) ? $attachment['align'] : 'none';
1132          $size = !empty($attachment['image-size']) ? $attachment['image-size'] : 'medium';
1133          $alt = !empty($attachment['image_alt']) ? $attachment['image_alt'] : '';
1134          $rel = ( $url == get_attachment_link($attachment_id) );
1135  
1136          return get_image_send_to_editor($attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel, $size, $alt);
1137      }
1138  
1139      return $html;
1140  }
1141  
1142  add_filter('media_send_to_editor', 'image_media_send_to_editor', 10, 3);
1143  
1144  /**
1145   * {@internal Missing Short Description}}
1146   *
1147   * @since 2.5.0
1148   *
1149   * @param WP_Post $post
1150   * @param array $errors
1151   * @return array
1152   */
1153  function get_attachment_fields_to_edit($post, $errors = null) {
1154      if ( is_int($post) )
1155          $post = get_post($post);
1156      if ( is_array($post) )
1157          $post = new WP_Post( (object) $post );
1158  
1159      $image_url = wp_get_attachment_url($post->ID);
1160  
1161      $edit_post = sanitize_post($post, 'edit');
1162  
1163      $form_fields = array(
1164          'post_title'   => array(
1165              'label'      => __('Title'),
1166              'value'      => $edit_post->post_title
1167          ),
1168          'image_alt'   => array(),
1169          'post_excerpt' => array(
1170              'label'      => __('Caption'),
1171              'input'      => 'html',
1172              'html'       => wp_caption_input_textarea($edit_post)
1173          ),
1174          'post_content' => array(
1175              'label'      => __('Description'),
1176              'value'      => $edit_post->post_content,
1177              'input'      => 'textarea'
1178          ),
1179          'url'          => array(
1180              'label'      => __('Link URL'),
1181              'input'      => 'html',
1182              'html'       => image_link_input_fields($post, get_option('image_default_link_type')),
1183              'helps'      => __('Enter a link URL or click above for presets.')
1184          ),
1185          'menu_order'   => array(
1186              'label'      => __('Order'),
1187              'value'      => $edit_post->menu_order
1188          ),
1189          'image_url'    => array(
1190              'label'      => __('File URL'),
1191              'input'      => 'html',
1192              'html'       => "<input type='text' class='text urlfield' readonly='readonly' name='attachments[$post->ID][url]' value='" . esc_attr($image_url) . "' /><br />",
1193              'value'      => wp_get_attachment_url($post->ID),
1194              'helps'      => __('Location of the uploaded file.')
1195          )
1196      );
1197  
1198      foreach ( get_attachment_taxonomies($post) as $taxonomy ) {
1199          $t = (array) get_taxonomy($taxonomy);
1200          if ( ! $t['public'] || ! $t['show_ui'] )
1201              continue;
1202          if ( empty($t['label']) )
1203              $t['label'] = $taxonomy;
1204          if ( empty($t['args']) )
1205              $t['args'] = array();
1206  
1207          $terms = get_object_term_cache($post->ID, $taxonomy);
1208          if ( false === $terms )
1209              $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
1210  
1211          $values = array();
1212  
1213          foreach ( $terms as $term )
1214              $values[] = $term->slug;
1215          $t['value'] = join(', ', $values);
1216  
1217          $form_fields[$taxonomy] = $t;
1218      }
1219  
1220      // Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default
1221      // The recursive merge is easily traversed with array casting: foreach( (array) $things as $thing )
1222      $form_fields = array_merge_recursive($form_fields, (array) $errors);
1223  
1224      // This was formerly in image_attachment_fields_to_edit().
1225      if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
1226          $alt = get_post_meta($post->ID, '_wp_attachment_image_alt', true);
1227          if ( empty($alt) )
1228              $alt = '';
1229  
1230          $form_fields['post_title']['required'] = true;
1231  
1232          $form_fields['image_alt'] = array(
1233              'value' => $alt,
1234              'label' => __('Alternative Text'),
1235              'helps' => __('Alt text for the image, e.g. &#8220;The Mona Lisa&#8221;')
1236          );
1237  
1238          $form_fields['align'] = array(
1239              'label' => __('Alignment'),
1240              'input' => 'html',
1241              'html'  => image_align_input_fields($post, get_option('image_default_align')),
1242          );
1243  
1244          $form_fields['image-size'] = image_size_input_fields( $post, get_option('image_default_size', 'medium') );
1245  
1246      } else {
1247          unset( $form_fields['image_alt'] );
1248      }
1249  
1250      /**
1251       * Filter the attachment fields to edit.
1252       *
1253       * @since 2.5.0
1254       *
1255       * @param array   $form_fields An array of attachment form fields.
1256       * @param WP_Post $post        The WP_Post attachment object.
1257       */
1258      $form_fields = apply_filters( 'attachment_fields_to_edit', $form_fields, $post );
1259  
1260      return $form_fields;
1261  }
1262  
1263  /**
1264   * Retrieve HTML for media items of post gallery.
1265   *
1266   * The HTML markup retrieved will be created for the progress of SWF Upload
1267   * component. Will also create link for showing and hiding the form to modify
1268   * the image attachment.
1269   *
1270   * @since 2.5.0
1271   *
1272   * @param int $post_id Optional. Post ID.
1273   * @param array $errors Errors for attachment, if any.
1274   * @return string
1275   */
1276  function get_media_items( $post_id, $errors ) {
1277      $attachments = array();
1278      if ( $post_id ) {
1279          $post = get_post($post_id);
1280          if ( $post && $post->post_type == 'attachment' )
1281              $attachments = array($post->ID => $post);
1282          else
1283              $attachments = get_children( array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'orderby' => 'menu_order ASC, ID', 'order' => 'DESC') );
1284      } else {
1285          if ( is_array($GLOBALS['wp_the_query']->posts) )
1286              foreach ( $GLOBALS['wp_the_query']->posts as $attachment )
1287                  $attachments[$attachment->ID] = $attachment;
1288      }
1289  
1290      $output = '';
1291      foreach ( (array) $attachments as $id => $attachment ) {
1292          if ( $attachment->post_status == 'trash' )
1293              continue;
1294          if ( $item = get_media_item( $id, array( 'errors' => isset($errors[$id]) ? $errors[$id] : null) ) )
1295              $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>";
1296      }
1297  
1298      return $output;
1299  }
1300  
1301  /**
1302   * Retrieve HTML form for modifying the image attachment.
1303   *
1304   * @since 2.5.0
1305   *
1306   * @param int $attachment_id Attachment ID for modification.
1307   * @param string|array $args Optional. Override defaults.
1308   * @return string HTML form for attachment.
1309   */
1310  function get_media_item( $attachment_id, $args = null ) {
1311      global $redir_tab;
1312  
1313      if ( ( $attachment_id = intval( $attachment_id ) ) && $thumb_url = wp_get_attachment_image_src( $attachment_id, 'thumbnail', true ) )
1314          $thumb_url = $thumb_url[0];
1315      else
1316          $thumb_url = false;
1317  
1318      $post = get_post( $attachment_id );
1319      $current_post_id = !empty( $_GET['post_id'] ) ? (int) $_GET['post_id'] : 0;
1320  
1321      $default_args = array(
1322          'errors' => null,
1323          'send' => $current_post_id ? post_type_supports( get_post_type( $current_post_id ), 'editor' ) : true,
1324          'delete' => true,
1325          'toggle' => true,
1326          'show_title' => true
1327      );
1328      $args = wp_parse_args( $args, $default_args );
1329  
1330      /**
1331       * Filter the arguments used to retrieve an image for the edit image form.
1332       *
1333       * @since 3.1.0
1334       *
1335       * @see get_media_item
1336       *
1337       * @param array $args An array of arguments.
1338       */
1339      $r = apply_filters( 'get_media_item_args', $args );
1340  
1341      $toggle_on  = __( 'Show' );
1342      $toggle_off = __( 'Hide' );
1343  
1344      $filename = esc_html( wp_basename( $post->guid ) );
1345      $title = esc_attr( $post->post_title );
1346  
1347      $post_mime_types = get_post_mime_types();
1348      $keys = array_keys( wp_match_mime_types( array_keys( $post_mime_types ), $post->post_mime_type ) );
1349      $type = array_shift( $keys );
1350      $type_html = "<input type='hidden' id='type-of-$attachment_id' value='" . esc_attr( $type ) . "' />";
1351  
1352      $form_fields = get_attachment_fields_to_edit( $post, $r['errors'] );
1353  
1354      if ( $r['toggle'] ) {
1355          $class = empty( $r['errors'] ) ? 'startclosed' : 'startopen';
1356          $toggle_links = "
1357      <a class='toggle describe-toggle-on' href='#'>$toggle_on</a>
1358      <a class='toggle describe-toggle-off' href='#'>$toggle_off</a>";
1359      } else {
1360          $class = '';
1361          $toggle_links = '';
1362      }
1363  
1364      $display_title = ( !empty( $title ) ) ? $title : $filename; // $title shouldn't ever be empty, but just in case
1365      $display_title = $r['show_title'] ? "<div class='filename new'><span class='title'>" . wp_html_excerpt( $display_title, 60, '&hellip;' ) . "</span></div>" : '';
1366  
1367      $gallery = ( ( isset( $_REQUEST['tab'] ) && 'gallery' == $_REQUEST['tab'] ) || ( isset( $redir_tab ) && 'gallery' == $redir_tab ) );
1368      $order = '';
1369  
1370      foreach ( $form_fields as $key => $val ) {
1371          if ( 'menu_order' == $key ) {
1372              if ( $gallery )
1373                  $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>";
1374              else
1375                  $order = "<input type='hidden' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ) . "' />";
1376  
1377              unset( $form_fields['menu_order'] );
1378              break;
1379          }
1380      }
1381  
1382      $media_dims = '';
1383      $meta = wp_get_attachment_metadata( $post->ID );
1384      if ( isset( $meta['width'], $meta['height'] ) )
1385          $media_dims .= "<span id='media-dims-$post->ID'>{$meta['width']}&nbsp;&times;&nbsp;{$meta['height']}</span> ";
1386  
1387      /**
1388       * Filter the media metadata.
1389       *
1390       * @since 2.5.0
1391       *
1392       * @param string  $media_dims The HTML markup containing the media dimensions.
1393       * @param WP_Post $post       The WP_Post attachment object.
1394       */
1395      $media_dims = apply_filters( 'media_meta', $media_dims, $post );
1396  
1397      $image_edit_button = '';
1398      if ( wp_attachment_is_image( $post->ID ) && wp_image_editor_supports( array( 'mime_type' => $post->post_mime_type ) ) ) {
1399          $nonce = wp_create_nonce( "image_editor-$post->ID" );
1400          $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' ) . "' /> <span class='spinner'></span>";
1401      }
1402  
1403      $attachment_url = get_permalink( $attachment_id );
1404  
1405      $item = "
1406      $type_html
1407      $toggle_links
1408      $order
1409      $display_title
1410      <table class='slidetoggle describe $class'>
1411          <thead class='media-item-info' id='media-head-$post->ID'>
1412          <tr>
1413              <td class='A1B1' id='thumbnail-head-$post->ID'>
1414              <p><a href='$attachment_url' target='_blank'><img class='thumbnail' src='$thumb_url' alt='' /></a></p>
1415              <p>$image_edit_button</p>
1416              </td>
1417              <td>
1418              <p><strong>" . __('File name:') . "</strong> $filename</p>
1419              <p><strong>" . __('File type:') . "</strong> $post->post_mime_type</p>
1420              <p><strong>" . __('Upload date:') . "</strong> " . mysql2date( get_option('date_format'), $post->post_date ). '</p>';
1421              if ( !empty( $media_dims ) )
1422                  $item .= "<p><strong>" . __('Dimensions:') . "</strong> $media_dims</p>\n";
1423  
1424              $item .= "</td></tr>\n";
1425  
1426      $item .= "
1427          </thead>
1428          <tbody>
1429          <tr><td colspan='2' class='imgedit-response' id='imgedit-response-$post->ID'></td></tr>
1430          <tr><td style='display:none' colspan='2' class='image-editor' id='image-editor-$post->ID'></td></tr>\n";
1431  
1432      $defaults = array(
1433          'input'      => 'text',
1434          'required'   => false,
1435          'value'      => '',
1436          'extra_rows' => array(),
1437      );
1438  
1439      if ( $r['send'] ) {
1440          $r['send'] = get_submit_button( __( 'Insert into Post' ), 'button', "send[$attachment_id]", false );
1441      }
1442  
1443      $delete = empty( $r['delete'] ) ? '' : $r['delete'];
1444      if ( $delete && current_user_can( 'delete_post', $attachment_id ) ) {
1445          if ( !EMPTY_TRASH_DAYS ) {
1446              $delete = "<a href='" . wp_nonce_url( "post.php?action=delete&amp;post=$attachment_id", 'delete-post_' . $attachment_id ) . "' id='del[$attachment_id]' class='delete-permanently'>" . __( 'Delete Permanently' ) . '</a>';
1447          } elseif ( !MEDIA_TRASH ) {
1448              $delete = "<a href='#' class='del-link' onclick=\"document.getElementById('del_attachment_$attachment_id').style.display='block';return false;\">" . __( 'Delete' ) . "</a>
1449               <div id='del_attachment_$attachment_id' class='del-attachment' style='display:none;'><p>" . sprintf( __( 'You are about to delete <strong>%s</strong>.' ), $filename ) . "</p>
1450               <a href='" . wp_nonce_url( "post.php?action=delete&amp;post=$attachment_id", 'delete-post_' . $attachment_id ) . "' id='del[$attachment_id]' class='button'>" . __( 'Continue' ) . "</a>
1451               <a href='#' class='button' onclick=\"this.parentNode.style.display='none';return false;\">" . __( 'Cancel' ) . "</a>
1452               </div>";
1453          } else {
1454              $delete = "<a href='" . wp_nonce_url( "post.php?action=trash&amp;post=$attachment_id", 'trash-post_' . $attachment_id ) . "' id='del[$attachment_id]' class='delete'>" . __( 'Move to Trash' ) . "</a>
1455              <a href='" . wp_nonce_url( "post.php?action=untrash&amp;post=$attachment_id", 'untrash-post_' . $attachment_id ) . "' id='undo[$attachment_id]' class='undo hidden'>" . __( 'Undo' ) . "</a>";
1456          }
1457      } else {
1458          $delete = '';
1459      }
1460  
1461      $thumbnail = '';
1462      $calling_post_id = 0;
1463      if ( isset( $_GET['post_id'] ) ) {
1464          $calling_post_id = absint( $_GET['post_id'] );
1465      } elseif ( isset( $_POST ) && count( $_POST ) ) {// Like for async-upload where $_GET['post_id'] isn't set
1466          $calling_post_id = $post->post_parent;
1467      }
1468      if ( 'image' == $type && $calling_post_id && current_theme_supports( 'post-thumbnails', get_post_type( $calling_post_id ) )
1469          && post_type_supports( get_post_type( $calling_post_id ), 'thumbnail' ) && get_post_thumbnail_id( $calling_post_id ) != $attachment_id ) {
1470          $ajax_nonce = wp_create_nonce( "set_post_thumbnail-$calling_post_id" );
1471          $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>";
1472      }
1473  
1474      if ( ( $r['send'] || $thumbnail || $delete ) && !isset( $form_fields['buttons'] ) ) {
1475          $form_fields['buttons'] = array( 'tr' => "\t\t<tr class='submit'><td></td><td class='savesend'>" . $r['send'] . " $thumbnail $delete</td></tr>\n" );
1476      }
1477      $hidden_fields = array();
1478  
1479      foreach ( $form_fields as $id => $field ) {
1480          if ( $id[0] == '_' )
1481              continue;
1482  
1483          if ( !empty( $field['tr'] ) ) {
1484              $item .= $field['tr'];
1485              continue;
1486          }
1487  
1488          $field = array_merge( $defaults, $field );
1489          $name = "attachments[$attachment_id][$id]";
1490  
1491          if ( $field['input'] == 'hidden' ) {
1492              $hidden_fields[$name] = $field['value'];
1493              continue;
1494          }
1495  
1496          $required      = $field['required'] ? '<span class="alignright"><abbr title="required" class="required">*</abbr></span>' : '';
1497          $aria_required = $field['required'] ? " aria-required='true' " : '';
1498          $class  = $id;
1499          $class .= $field['required'] ? ' form-required' : '';
1500  
1501          $item .= "\t\t<tr class='$class'>\n\t\t\t<th 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'>";
1502          if ( !empty( $field[ $field['input'] ] ) )
1503              $item .= $field[ $field['input'] ];
1504          elseif ( $field['input'] == 'textarea' ) {
1505              if ( 'post_content' == $id && user_can_richedit() ) {
1506                  // Sanitize_post() skips the post_content when user_can_richedit.
1507                  $field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES );
1508              }
1509              // Post_excerpt is already escaped by sanitize_post() in get_attachment_fields_to_edit().
1510              $item .= "<textarea id='$name' name='$name' $aria_required>" . $field['value'] . '</textarea>';
1511          } else {
1512              $item .= "<input type='text' class='text' id='$name' name='$name' value='" . esc_attr( $field['value'] ) . "' $aria_required />";
1513          }
1514          if ( !empty( $field['helps'] ) )
1515              $item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>';
1516          $item .= "</td>\n\t\t</tr>\n";
1517  
1518          $extra_rows = array();
1519  
1520          if ( !empty( $field['errors'] ) )
1521              foreach ( array_unique( (array) $field['errors'] ) as $error )
1522                  $extra_rows['error'][] = $error;
1523  
1524          if ( !empty( $field['extra_rows'] ) )
1525              foreach ( $field['extra_rows'] as $class => $rows )
1526                  foreach ( (array) $rows as $html )
1527                      $extra_rows[$class][] = $html;
1528  
1529          foreach ( $extra_rows as $class => $rows )
1530              foreach ( $rows as $html )
1531                  $item .= "\t\t<tr><td></td><td class='$class'>$html</td></tr>\n";
1532      }
1533  
1534      if ( !empty( $form_fields['_final'] ) )
1535          $item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
1536      $item .= "\t</tbody>\n";
1537      $item .= "\t</table>\n";
1538  
1539      foreach ( $hidden_fields as $name => $value )
1540          $item .= "\t<input type='hidden' name='$name' id='$name' value='" . esc_attr( $value ) . "' />\n";
1541  
1542      if ( $post->post_parent < 1 && isset( $_REQUEST['post_id'] ) ) {
1543          $parent = (int) $_REQUEST['post_id'];
1544          $parent_name = "attachments[$attachment_id][post_parent]";
1545          $item .= "\t<input type='hidden' name='$parent_name' id='$parent_name' value='$parent' />\n";
1546      }
1547  
1548      return $item;
1549  }
1550  
1551  function get_compat_media_markup( $attachment_id, $args = null ) {
1552      $post = get_post( $attachment_id );
1553  
1554      $default_args = array(
1555          'errors' => null,
1556          'in_modal' => false,
1557      );
1558  
1559      $user_can_edit = current_user_can( 'edit_post', $attachment_id );
1560  
1561      $args = wp_parse_args( $args, $default_args );
1562  
1563      /** This filter is documented in wp-admin/includes/media.php */
1564      $args = apply_filters( 'get_media_item_args', $args );
1565  
1566      $form_fields = array();
1567  
1568      if ( $args['in_modal'] ) {
1569          foreach ( get_attachment_taxonomies($post) as $taxonomy ) {
1570              $t = (array) get_taxonomy($taxonomy);
1571              if ( ! $t['public'] || ! $t['show_ui'] )
1572                  continue;
1573              if ( empty($t['label']) )
1574                  $t['label'] = $taxonomy;
1575              if ( empty($t['args']) )
1576                  $t['args'] = array();
1577  
1578              $terms = get_object_term_cache($post->ID, $taxonomy);
1579              if ( false === $terms )
1580                  $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
1581  
1582              $values = array();
1583  
1584              foreach ( $terms as $term )
1585                  $values[] = $term->slug;
1586              $t['value'] = join(', ', $values);
1587              $t['taxonomy'] = true;
1588  
1589              $form_fields[$taxonomy] = $t;
1590          }
1591      }
1592  
1593      // Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default
1594      // The recursive merge is easily traversed with array casting: foreach( (array) $things as $thing )
1595      $form_fields = array_merge_recursive($form_fields, (array) $args['errors'] );
1596  
1597      /** This filter is documented in wp-admin/includes/media.php */
1598      $form_fields = apply_filters( 'attachment_fields_to_edit', $form_fields, $post );
1599  
1600      unset( $form_fields['image-size'], $form_fields['align'], $form_fields['image_alt'],
1601          $form_fields['post_title'], $form_fields['post_excerpt'], $form_fields['post_content'],
1602          $form_fields['url'], $form_fields['menu_order'], $form_fields['image_url'] );
1603  
1604      /** This filter is documented in wp-admin/includes/media.php */
1605      $media_meta = apply_filters( 'media_meta', '', $post );
1606  
1607      $defaults = array(
1608          'input'         => 'text',
1609          'required'      => false,
1610          'value'         => '',
1611          'extra_rows'    => array(),
1612          'show_in_edit'  => true,
1613          'show_in_modal' => true,
1614      );
1615  
1616      $hidden_fields = array();
1617  
1618      $item = '';
1619      foreach ( $form_fields as $id => $field ) {
1620          if ( $id[0] == '_' )
1621              continue;
1622  
1623          $name = "attachments[$attachment_id][$id]";
1624          $id_attr = "attachments-$attachment_id-$id";
1625  
1626          if ( !empty( $field['tr'] ) ) {
1627              $item .= $field['tr'];
1628              continue;
1629          }
1630  
1631          $field = array_merge( $defaults, $field );
1632  
1633          if ( ( ! $field['show_in_edit'] && ! $args['in_modal'] ) || ( ! $field['show_in_modal'] && $args['in_modal'] ) )
1634              continue;
1635  
1636          if ( $field['input'] == 'hidden' ) {
1637              $hidden_fields[$name] = $field['value'];
1638              continue;
1639          }
1640  
1641          $readonly      = ! $user_can_edit && ! empty( $field['taxonomy'] ) ? " readonly='readonly' " : '';
1642          $required      = $field['required'] ? '<span class="alignright"><abbr title="required" class="required">*</abbr></span>' : '';
1643          $aria_required = $field['required'] ? " aria-required='true' " : '';
1644          $class  = 'compat-field-' . $id;
1645          $class .= $field['required'] ? ' form-required' : '';
1646  
1647          $item .= "\t\t<tr class='$class'>";
1648          $item .= "\t\t\t<th scope='row' class='label'><label for='$id_attr'><span class='alignleft'>{$field['label']}</span>$required<br class='clear' /></label>";
1649          $item .= "</th>\n\t\t\t<td class='field'>";
1650  
1651          if ( !empty( $field[ $field['input'] ] ) )
1652              $item .= $field[ $field['input'] ];
1653          elseif ( $field['input'] == 'textarea' ) {
1654              if ( 'post_content' == $id && user_can_richedit() ) {
1655                  // sanitize_post() skips the post_content when user_can_richedit.
1656                  $field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES );
1657              }
1658              $item .= "<textarea id='$id_attr' name='$name' $aria_required>" . $field['value'] . '</textarea>';
1659          } else {
1660              $item .= "<input type='text' class='text' id='$id_attr' name='$name' value='" . esc_attr( $field['value'] ) . "' $readonly $aria_required />";
1661          }
1662          if ( !empty( $field['helps'] ) )
1663              $item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>';
1664          $item .= "</td>\n\t\t</tr>\n";
1665  
1666          $extra_rows = array();
1667  
1668          if ( !empty( $field['errors'] ) )
1669              foreach ( array_unique( (array) $field['errors'] ) as $error )
1670                  $extra_rows['error'][] = $error;
1671  
1672          if ( !empty( $field['extra_rows'] ) )
1673              foreach ( $field['extra_rows'] as $class => $rows )
1674                  foreach ( (array) $rows as $html )
1675                      $extra_rows[$class][] = $html;
1676  
1677          foreach ( $extra_rows as $class => $rows )
1678              foreach ( $rows as $html )
1679                  $item .= "\t\t<tr><td></td><td class='$class'>$html</td></tr>\n";
1680      }
1681  
1682      if ( !empty( $form_fields['_final'] ) )
1683          $item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
1684      if ( $item )
1685          $item = '<table class="compat-attachment-fields">' . $item . '</table>';
1686  
1687      foreach ( $hidden_fields as $hidden_field => $value ) {
1688          $item .= '<input type="hidden" name="' . esc_attr( $hidden_field ) . '" value="' . esc_attr( $value ) . '" />' . "\n";
1689      }
1690  
1691      if ( $item )
1692          $item = '<input type="hidden" name="attachments[' . $attachment_id . '][menu_order]" value="' . esc_attr( $post->menu_order ) . '" />' . $item;
1693  
1694      return array(
1695          'item'   => $item,
1696          'meta'   => $media_meta,
1697      );
1698  }
1699  
1700  /**
1701   * {@internal Missing Short Description}}
1702   *
1703   * @since 2.5.0
1704   */
1705  function media_upload_header() {
1706      $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
1707      echo '<script type="text/javascript">post_id = ' . $post_id . ";</script>\n";
1708      if ( empty( $_GET['chromeless'] ) ) {
1709          echo '<div id="media-upload-header">';
1710          the_media_upload_tabs();
1711          echo '</div>';
1712      }
1713  }
1714  
1715  /**
1716   * {@internal Missing Short Description}}
1717   *
1718   * @since 2.5.0
1719   *
1720   * @param array $errors
1721   */
1722  function media_upload_form( $errors = null ) {
1723      global $type, $tab, $is_IE, $is_opera;
1724  
1725      if ( ! _device_can_upload() ) {
1726          echo '<p>' . sprintf( __('The web browser on your device cannot be used to upload files. You may be able to use the <a href="%s">native app for your device</a> instead.'), 'https://apps.wordpress.org/' ) . '</p>';
1727          return;
1728      }
1729  
1730      $upload_action_url = admin_url('async-upload.php');
1731      $post_id = isset($_REQUEST['post_id']) ? intval($_REQUEST['post_id']) : 0;
1732      $_type = isset($type) ? $type : '';
1733      $_tab = isset($tab) ? $tab : '';
1734  
1735      $max_upload_size = wp_max_upload_size();
1736      if ( ! $max_upload_size ) {
1737          $max_upload_size = 0;
1738      }
1739  ?>
1740  
1741  <div id="media-upload-notice"><?php
1742  
1743      if (isset($errors['upload_notice']) )
1744          echo $errors['upload_notice'];
1745  
1746  ?></div>
1747  <div id="media-upload-error"><?php
1748  
1749      if (isset($errors['upload_error']) && is_wp_error($errors['upload_error']))
1750          echo $errors['upload_error']->get_error_message();
1751  
1752  ?></div>
1753  <?php
1754  if ( is_multisite() && !is_upload_space_available() ) {
1755      /**
1756       * Fires when an upload will exceed the defined upload space quota for a network site.
1757       *
1758       * @since 3.5.0
1759       */
1760      do_action( 'upload_ui_over_quota' );
1761      return;
1762  }
1763  
1764  /**
1765   * Fires just before the legacy (pre-3.5.0) upload interface is loaded.
1766   *
1767   * @since 2.6.0
1768   */
1769  do_action( 'pre-upload-ui' );
1770  
1771  $post_params = array(
1772      "post_id" => $post_id,
1773      "_wpnonce" => wp_create_nonce('media-form'),
1774      "type" => $_type,
1775      "tab" => $_tab,
1776      "short" => "1",
1777  );
1778  
1779  /**
1780   * Filter the media upload post parameters.
1781   *
1782   * @since 3.1.0 As 'swfupload_post_params'
1783   * @since 3.3.0
1784   *
1785   * @param array $post_params An array of media upload parameters used by Plupload.
1786   */
1787  $post_params = apply_filters( 'upload_post_params', $post_params );
1788  
1789  $plupload_init = array(
1790      'runtimes'            => 'html5,flash,silverlight,html4',
1791      'browse_button'       => 'plupload-browse-button',
1792      'container'           => 'plupload-upload-ui',
1793      'drop_element'        => 'drag-drop-area',
1794      'file_data_name'      => 'async-upload',
1795      'url'                 => $upload_action_url,
1796      'flash_swf_url'       => includes_url( 'js/plupload/plupload.flash.swf' ),
1797      'silverlight_xap_url' => includes_url( 'js/plupload/plupload.silverlight.xap' ),
1798      'filters' => array(
1799          'max_file_size'   => $max_upload_size . 'b',
1800      ),
1801      'multipart_params'    => $post_params,
1802  );
1803  
1804  // Currently only iOS Safari supports multiple files uploading but iOS 7.x has a bug that prevents uploading of videos
1805  // when enabled. See #29602.
1806  if ( wp_is_mobile() && strpos( $_SERVER['HTTP_USER_AGENT'], 'OS 7_' ) !== false &&
1807      strpos( $_SERVER['HTTP_USER_AGENT'], 'like Mac OS X' ) !== false ) {
1808  
1809      $plupload_init['multi_selection'] = false;
1810  }
1811  
1812  /**
1813   * Filter the default Plupload settings.
1814   *
1815   * @since 3.3.0
1816   *
1817   * @param array $plupload_init An array of default settings used by Plupload.
1818   */
1819  $plupload_init = apply_filters( 'plupload_init', $plupload_init );
1820  
1821  ?>
1822  
1823  <script type="text/javascript">
1824  <?php
1825  // Verify size is an int. If not return default value.
1826  $large_size_h = absint( get_option('large_size_h') );
1827  if( !$large_size_h )
1828      $large_size_h = 1024;
1829  $large_size_w = absint( get_option('large_size_w') );
1830  if( !$large_size_w )
1831      $large_size_w = 1024;
1832  ?>
1833  var resize_height = <?php echo $large_size_h; ?>, resize_width = <?php echo $large_size_w; ?>,
1834  wpUploaderInit = <?php echo wp_json_encode( $plupload_init ); ?>;
1835  </script>
1836  
1837  <div id="plupload-upload-ui" class="hide-if-no-js">
1838  <?php
1839  /**
1840   * Fires before the upload interface loads.
1841   *
1842   * @since 2.6.0 As 'pre-flash-upload-ui'
1843   * @since 3.3.0
1844   */
1845  do_action( 'pre-plupload-upload-ui' ); ?>
1846  <div id="drag-drop-area">
1847      <div class="drag-drop-inside">
1848      <p class="drag-drop-info"><?php _e('Drop files here'); ?></p>
1849      <p><?php _ex('or', 'Uploader: Drop files here - or - Select Files'); ?></p>
1850      <p class="drag-drop-buttons"><input id="plupload-browse-button" type="button" value="<?php esc_attr_e('Select Files'); ?>" class="button" /></p>
1851      </div>
1852  </div>
1853  <?php
1854  /**
1855   * Fires after the upload interface loads.
1856   *
1857   * @since 2.6.0 As 'post-flash-upload-ui'
1858   * @since 3.3.0
1859   */
1860  do_action( 'post-plupload-upload-ui' ); ?>
1861  </div>
1862  
1863  <div id="html-upload-ui" class="hide-if-js">
1864      <?php
1865      /**
1866       * Fires before the upload button in the media upload interface.
1867       *
1868       * @since 2.6.0
1869       */
1870      do_action( 'pre-html-upload-ui' );
1871      ?>
1872      <p id="async-upload-wrap">
1873          <label class="screen-reader-text" for="async-upload"><?php _e('Upload'); ?></label>
1874          <input type="file" name="async-upload" id="async-upload" />
1875          <?php submit_button( __( 'Upload' ), 'button', 'html-upload', false ); ?>
1876          <a href="#" onclick="try{top.tb_remove();}catch(e){}; return false;"><?php _e('Cancel'); ?></a>
1877      </p>
1878      <div class="clear"></div>
1879  <?php
1880  /**
1881   * Fires after the upload button in the media upload interface.
1882   *
1883   * @since 2.6.0
1884   */
1885  do_action( 'post-html-upload-ui' );
1886  ?>
1887  </div>
1888  
1889  <p class="max-upload-size"><?php printf( __( 'Maximum upload file size: %s.' ), esc_html( size_format( $max_upload_size ) ) ); ?></p>
1890  <?php
1891  
1892      /**
1893       * Fires on the post upload UI screen.
1894       *
1895       * Legacy (pre-3.5.0) media workflow hook.
1896       *
1897       * @since 2.6.0
1898       */
1899      do_action( 'post-upload-ui' );
1900  }
1901  
1902  /**
1903   * {@internal Missing Short Description}}
1904   *
1905   * @since 2.5.0
1906   *
1907   * @param string $type
1908   * @param object $errors
1909   * @param integer $id
1910   */
1911  function media_upload_type_form($type = 'file', $errors = null, $id = null) {
1912  
1913      media_upload_header();
1914  
1915      $post_id = isset( $_REQUEST['post_id'] )? intval( $_REQUEST['post_id'] ) : 0;
1916  
1917      $form_action_url = admin_url("media-upload.php?type=$type&tab=type&post_id=$post_id");
1918  
1919      /**
1920       * Filter the media upload form action URL.
1921       *
1922       * @since 2.6.0
1923       *
1924       * @param string $form_action_url The media upload form action URL.
1925       * @param string $type            The type of media. Default 'file'.
1926       */
1927      $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type );
1928      $form_class = 'media-upload-form type-form validate';
1929  
1930      if ( get_user_setting('uploader') )
1931          $form_class .= ' html-uploader';
1932  ?>
1933  
1934  <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="<?php echo $type; ?>-form">
1935  <?php submit_button( '', 'hidden', 'save', false ); ?>
1936  <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
1937  <?php wp_nonce_field('media-form'); ?>
1938  
1939  <h3 class="media-title"><?php _e('Add media files from your computer'); ?></h3>
1940  
1941  <?php media_upload_form( $errors ); ?>
1942  
1943  <script type="text/javascript">
1944  jQuery(function($){
1945      var preloaded = $(".media-item.preloaded");
1946      if ( preloaded.length > 0 ) {
1947          preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
1948      }
1949      updateMediaForm();
1950  });
1951  </script>
1952  <div id="media-items"><?php
1953  
1954  if ( $id ) {
1955      if ( !is_wp_error($id) ) {
1956          add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2);
1957          echo get_media_items( $id, $errors );
1958      } else {
1959          echo '<div id="media-upload-error">'.esc_html($id->get_error_message()).'</div></div>';
1960          exit;
1961      }
1962  }
1963  ?></div>
1964  
1965  <p class="savebutton ml-submit">
1966  <?php submit_button( __( 'Save all changes' ), 'button', 'save', false ); ?>
1967  </p>
1968  </form>
1969  <?php
1970  }
1971  
1972  /**
1973   * {@internal Missing Short Description}}
1974   *
1975   * @since 2.7.0
1976   *
1977   * @param string $type
1978   * @param object $errors
1979   * @param integer $id
1980   */
1981  function media_upload_type_url_form($type = null, $errors = null, $id = null) {
1982      if ( null === $type )
1983          $type = 'image';
1984  
1985      media_upload_header();
1986  
1987      $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
1988  
1989      $form_action_url = admin_url("media-upload.php?type=$type&tab=type&post_id=$post_id");
1990      /** This filter is documented in wp-admin/includes/media.php */
1991      $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type );
1992      $form_class = 'media-upload-form type-form validate';
1993  
1994      if ( get_user_setting('uploader') )
1995          $form_class .= ' html-uploader';
1996  ?>
1997  
1998  <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="<?php echo $type; ?>-form">
1999  <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
2000  <?php wp_nonce_field('media-form'); ?>
2001  
2002  <h3 class="media-title"><?php _e('Insert media from another website'); ?></h3>
2003  
2004  <script type="text/javascript">
2005  var addExtImage = {
2006  
2007      width : '',
2008      height : '',
2009      align : 'alignnone',
2010  
2011      insert : function() {
2012          var t = this, html, f = document.forms[0], cls, title = '', alt = '', caption = '';
2013  
2014          if ( '' == f.src.value || '' == t.width )
2015              return false;
2016  
2017          if ( f.alt.value )
2018              alt = f.alt.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
2019  
2020  <?php
2021      /** This filter is documented in wp-admin/includes/media.php */
2022      if ( ! apply_filters( 'disable_captions', '' ) ) {
2023          ?>
2024          if ( f.caption.value ) {
2025              caption = f.caption.value.replace(/\r\n|\r/g, '\n');
2026              caption = caption.replace(/<[a-zA-Z0-9]+( [^<>]+)?>/g, function(a){
2027                  return a.replace(/[\r\n\t]+/, ' ');
2028              });
2029  
2030              caption = caption.replace(/\s*\n\s*/g, '<br />');
2031          }
2032  <?php } ?>
2033  
2034          cls = caption ? '' : ' class="'+t.align+'"';
2035  
2036          html = '<img alt="'+alt+'" src="'+f.src.value+'"'+cls+' width="'+t.width+'" height="'+t.height+'" />';
2037  
2038          if ( f.url.value ) {
2039              url = f.url.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
2040              html = '<a href="'+url+'">'+html+'</a>';
2041          }
2042  
2043          if ( caption )
2044              html = '[caption id="" align="'+t.align+'" width="'+t.width+'"]'+html+caption+'[/caption]';
2045  
2046          var win = window.dialogArguments || opener || parent || top;
2047          win.send_to_editor(html);
2048          return false;
2049      },
2050  
2051      resetImageData : function() {
2052          var t = addExtImage;
2053  
2054          t.width = t.height = '';
2055          document.getElementById('go_button').style.color = '#bbb';
2056          if ( ! document.forms[0].src.value )
2057              document.getElementById('status_img').innerHTML = '*';
2058          else document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/no.png' ) ); ?>" alt="" />';
2059      },
2060  
2061      updateImageData : function() {
2062          var t = addExtImage;
2063  
2064          t.width = t.preloadImg.width;
2065          t.height = t.preloadImg.height;
2066          document.getElementById('go_button').style.color = '#333';
2067          document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/yes.png' ) ); ?>" alt="" />';
2068      },
2069  
2070      getImageData : function() {
2071          if ( jQuery('table.describe').hasClass('not-image') )
2072              return;
2073  
2074          var t = addExtImage, src = document.forms[0].src.value;
2075  
2076          if ( ! src ) {
2077              t.resetImageData();
2078              return false;
2079          }
2080  
2081          document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/spinner-2x.gif' ) ); ?>" alt="" width="16" height="16" />';
2082          t.preloadImg = new Image();
2083          t.preloadImg.onload = t.updateImageData;
2084          t.preloadImg.onerror = t.resetImageData;
2085          t.preloadImg.src = src;
2086      }
2087  };
2088  
2089  jQuery(document).ready( function($) {
2090      $('.media-types input').click( function() {
2091          $('table.describe').toggleClass('not-image', $('#not-image').prop('checked') );
2092      });
2093  });
2094  </script>
2095  
2096  <div id="media-items">
2097  <div class="media-item media-blank">
2098  <?php
2099  /**
2100   * Filter the insert media from URL form HTML.
2101   *
2102   * @since 3.3.0
2103   *
2104   * @param string $form_html The insert from URL form HTML.
2105   */
2106  echo apply_filters( 'type_url_form_media', wp_media_insert_url_form( $type ) );
2107  ?>
2108  </div>
2109  </div>
2110  </form>
2111  <?php
2112  }
2113  
2114  /**
2115   * Adds gallery form to upload iframe
2116   *
2117   * @since 2.5.0
2118   *
2119   * @param array $errors
2120   */
2121  function media_upload_gallery_form($errors) {
2122      global $redir_tab, $type;
2123  
2124      $redir_tab = 'gallery';
2125      media_upload_header();
2126  
2127      $post_id = intval($_REQUEST['post_id']);
2128      $form_action_url = admin_url("media-upload.php?type=$type&tab=gallery&post_id=$post_id");
2129      /** This filter is documented in wp-admin/includes/media.php */
2130      $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type );
2131      $form_class = 'media-upload-form validate';
2132  
2133      if ( get_user_setting('uploader') )
2134          $form_class .= ' html-uploader';
2135  ?>
2136  
2137  <script type="text/javascript">
2138  jQuery(function($){
2139      var preloaded = $(".media-item.preloaded");
2140      if ( preloaded.length > 0 ) {
2141          preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
2142          updateMediaForm();
2143      }
2144  });
2145  </script>
2146  <div id="sort-buttons" class="hide-if-no-js">
2147  <span>
2148  <?php _e('All Tabs:'); ?>
2149  <a href="#" id="showall"><?php _e('Show'); ?></a>
2150  <a href="#" id="hideall" style="display:none;"><?php _e('Hide'); ?></a>
2151  </span>
2152  <?php _e('Sort Order:'); ?>
2153  <a href="#" id="asc"><?php _e('Ascending'); ?></a> |
2154  <a href="#" id="desc"><?php _e('Descending'); ?></a> |
2155  <a href="#" id="clear"><?php _ex('Clear', 'verb'); ?></a>
2156  </div>
2157  <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="gallery-form">
2158  <?php wp_nonce_field('media-form'); ?>
2159  <?php //media_upload_form( $errors ); ?>
2160  <table class="widefat">
2161  <thead><tr>
2162  <th><?php _e('Media'); ?></th>
2163  <th class="order-head"><?php _e('Order'); ?></th>
2164  <th class="actions-head"><?php _e('Actions'); ?></th>
2165  </tr></thead>
2166  </table>
2167  <div id="media-items">
2168  <?php add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2); ?>
2169  <?php echo get_media_items($post_id, $errors); ?>
2170  </div>
2171  
2172  <p class="ml-submit">
2173  <?php submit_button( __( 'Save all changes' ), 'button savebutton', 'save', false, array( 'id' => 'save-all', 'style' => 'display: none;' ) ); ?>
2174  <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
2175  <input type="hidden" name="type" value="<?php echo esc_attr( $GLOBALS['type'] ); ?>" />
2176  <input type="hidden" name="tab" value="<?php echo esc_attr( $GLOBALS['tab'] ); ?>" />
2177  </p>
2178  
2179  <div id="gallery-settings" style="display:none;">
2180  <div class="title"><?php _e('Gallery Settings'); ?></div>
2181  <table id="basic" class="describe"><tbody>
2182      <tr>
2183      <th scope="row" class="label">
2184          <label>
2185          <span class="alignleft"><?php _e('Link thumbnails to:'); ?></span>
2186          </label>
2187      </th>
2188      <td class="field">
2189          <input type="radio" name="linkto" id="linkto-file" value="file" />
2190          <label for="linkto-file" class="radio"><?php _e('Image File'); ?></label>
2191  
2192          <input type="radio" checked="checked" name="linkto" id="linkto-post" value="post" />
2193          <label for="linkto-post" class="radio"><?php _e('Attachment Page'); ?></label>
2194      </td>
2195      </tr>
2196  
2197      <tr>
2198      <th scope="row" class="label">
2199          <label>
2200          <span class="alignleft"><?php _e('Order images by:'); ?></span>
2201          </label>
2202      </th>
2203      <td class="field">
2204          <select id="orderby" name="orderby">
2205              <option value="menu_order" selected="selected"><?php _e('Menu order'); ?></option>
2206              <option value="title"><?php _e('Title'); ?></option>
2207              <option value="post_date"><?php _e('Date/Time'); ?></option>
2208              <option value="rand"><?php _e('Random'); ?></option>
2209          </select>
2210      </td>
2211      </tr>
2212  
2213      <tr>
2214      <th scope="row" class="label">
2215          <label>
2216          <span class="alignleft"><?php _e('Order:'); ?></span>
2217          </label>
2218      </th>
2219      <td class="field">
2220          <input type="radio" checked="checked" name="order" id="order-asc" value="asc" />
2221          <label for="order-asc" class="radio"><?php _e('Ascending'); ?></label>
2222  
2223          <input type="radio" name="order" id="order-desc" value="desc" />
2224          <label for="order-desc" class="radio"><?php _e('Descending'); ?></label>
2225      </td>
2226      </tr>
2227  
2228      <tr>
2229      <th scope="row" class="label">
2230          <label>
2231          <span class="alignleft"><?php _e('Gallery columns:'); ?></span>
2232          </label>
2233      </th>
2234      <td class="field">
2235          <select id="columns" name="columns">
2236              <option value="1">1</option>
2237              <option value="2">2</option>
2238              <option value="3" selected="selected">3</option>
2239              <option value="4">4</option>
2240              <option value="5">5</option>
2241              <option value="6">6</option>
2242              <option value="7">7</option>
2243              <option value="8">8</option>
2244              <option value="9">9</option>
2245          </select>
2246      </td>
2247      </tr>
2248  </tbody></table>
2249  
2250  <p class="ml-submit">
2251  <input type="button" class="button" style="display:none;" onMouseDown="wpgallery.update();" name="insert-gallery" id="insert-gallery" value="<?php esc_attr_e( 'Insert gallery' ); ?>" />
2252  <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' ); ?>" />
2253  </p>
2254  </div>
2255  </form>
2256  <?php
2257  }
2258  
2259  /**
2260   * {@internal Missing Short Description}}
2261   *
2262   * @since 2.5.0
2263   *
2264   * @param array $errors
2265   */
2266  function media_upload_library_form($errors) {
2267      global $wpdb, $wp_query, $wp_locale, $type, $tab, $post_mime_types;
2268  
2269      media_upload_header();
2270  
2271      $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
2272  
2273      $form_action_url = admin_url("media-upload.php?type=$type&tab=library&post_id=$post_id");
2274      /** This filter is documented in wp-admin/includes/media.php */
2275      $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type );
2276      $form_class = 'media-upload-form validate';
2277  
2278      if ( get_user_setting('uploader') )
2279          $form_class .= ' html-uploader';
2280  
2281      $q = $_GET;
2282      $q['posts_per_page'] = 10;
2283      $q['paged'] = isset( $q['paged'] ) ? intval( $q['paged'] ) : 0;
2284      if ( $q['paged'] < 1 ) {
2285          $q['paged'] = 1;
2286      }
2287      $q['offset'] = ( $q['paged'] - 1 ) * 10;
2288      if ( $q['offset'] < 1 ) {
2289          $q['offset'] = 0;
2290      }
2291  
2292      list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query( $q );
2293  
2294  ?>
2295  
2296  <form id="filter" method="get">
2297  <input type="hidden" name="type" value="<?php echo esc_attr( $type ); ?>" />
2298  <input type="hidden" name="tab" value="<?php echo esc_attr( $tab ); ?>" />
2299  <input type="hidden" name="post_id" value="<?php echo (int) $post_id; ?>" />
2300  <input type="hidden" name="post_mime_type" value="<?php echo isset( $_GET['post_mime_type'] ) ? esc_attr( $_GET['post_mime_type'] ) : ''; ?>" />
2301  <input type="hidden" name="context" value="<?php echo isset( $_GET['context'] ) ? esc_attr( $_GET['context'] ) : ''; ?>" />
2302  
2303  <p id="media-search" class="search-box">
2304      <label class="screen-reader-text" for="media-search-input"><?php _e('Search Media');?>:</label>
2305      <input type="search" id="media-search-input" name="s" value="<?php the_search_query(); ?>" />
2306      <?php submit_button( __( 'Search Media' ), 'button', '', false ); ?>
2307  </p>
2308  
2309  <ul class="subsubsub">
2310  <?php
2311  $type_links = array();
2312  $_num_posts = (array) wp_count_attachments();
2313  $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
2314  foreach ( $matches as $_type => $reals )
2315      foreach ( $reals as $real )
2316          if ( isset($num_posts[$_type]) )
2317              $num_posts[$_type] += $_num_posts[$real];
2318          else
2319              $num_posts[$_type] = $_num_posts[$real];
2320  // If available type specified by media button clicked, filter by that type
2321  if ( empty($_GET['post_mime_type']) && !empty($num_posts[$type]) ) {
2322      $_GET['post_mime_type'] = $type;
2323      list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
2324  }
2325  if ( empty($_GET['post_mime_type']) || $_GET['post_mime_type'] == 'all' )
2326      $class = ' class="current"';
2327  else
2328      $class = '';
2329  $type_links[] = '<li><a href="' . esc_url(add_query_arg(array('post_mime_type'=>'all', 'paged'=>false, 'm'=>false))) . '"' . $class . '>' . __('All Types') . '</a>';
2330  foreach ( $post_mime_types as $mime_type => $label ) {
2331      $class = '';
2332  
2333      if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
2334          continue;
2335  
2336      if ( isset($_GET['post_mime_type']) && wp_match_mime_types($mime_type, $_GET['post_mime_type']) )
2337          $class = ' class="current"';
2338  
2339      $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>';
2340  }
2341  /**
2342   * Filter the media upload mime type list items.
2343   *
2344   * Returned values should begin with an `<li>` tag.
2345   *
2346   * @since 3.1.0
2347   *
2348   * @param array $type_links An array of list items containing mime type link HTML.
2349   */
2350  echo implode(' | </li>', apply_filters( 'media_upload_mime_type_links', $type_links ) ) . '</li>';
2351  unset($type_links);
2352  ?>
2353  </ul>
2354  
2355  <div class="tablenav">
2356  
2357  <?php
2358  $page_links = paginate_links( array(
2359      'base' => add_query_arg( 'paged', '%#%' ),
2360      'format' => '',
2361      'prev_text' => __('&laquo;'),
2362      'next_text' => __('&raquo;'),
2363      'total' => ceil($wp_query->found_posts / 10),
2364      'current' => $q['paged'],
2365  ));
2366  
2367  if ( $page_links )
2368      echo "<div class='tablenav-pages'>$page_links</div>";
2369  ?>
2370  
2371  <div class="alignleft actions">
2372  <?php
2373  
2374  $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";
2375  
2376  $arc_result = $wpdb->get_results( $arc_query );
2377  
2378  $month_count = count($arc_result);
2379  $selected_month = isset( $_GET['m'] ) ? $_GET['m'] : 0;
2380  
2381  if ( $month_count && !( 1 == $month_count && 0 == $arc_result[0]->mmonth ) ) { ?>
2382  <select name='m'>
2383  <option<?php selected( $selected_month, 0 ); ?> value='0'><?php _e( 'All dates' ); ?></option>
2384  <?php
2385  foreach ($arc_result as $arc_row) {
2386      if ( $arc_row->yyear == 0 )
2387          continue;
2388      $arc_row->mmonth = zeroise( $arc_row->mmonth, 2 );
2389  
2390      if ( $arc_row->yyear . $arc_row->mmonth == $selected_month )
2391          $default = ' selected="selected"';
2392      else
2393          $default = '';
2394  
2395      echo "<option$default value='" . esc_attr( $arc_row->yyear . $arc_row->mmonth ) . "'>";
2396      echo esc_html( $wp_locale->get_month($arc_row->mmonth) . " $arc_row->yyear" );
2397      echo "</option>\n";
2398  }
2399  ?>
2400  </select>
2401  <?php } ?>
2402  
2403  <?php submit_button( __( 'Filter &#187;' ), 'button', 'post-query-submit', false ); ?>
2404  
2405  </div>
2406  
2407  <br class="clear" />
2408  </div>
2409  </form>
2410  
2411  <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="library-form">
2412  
2413  <?php wp_nonce_field('media-form'); ?>
2414  <?php //media_upload_form( $errors ); ?>
2415  
2416  <script type="text/javascript">
2417  <!--
2418  jQuery(function($){
2419      var preloaded = $(".media-item.preloaded");
2420      if ( preloaded.length > 0 ) {
2421          preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
2422          updateMediaForm();
2423      }
2424  });
2425  -->
2426  </script>
2427  
2428  <div id="media-items">
2429  <?php add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2); ?>
2430  <?php echo get_media_items(null, $errors); ?>
2431  </div>
2432  <p class="ml-submit">
2433  <?php submit_button( __( 'Save all changes' ), 'button savebutton', 'save', false ); ?>
2434  <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
2435  </p>
2436  </form>
2437  <?php
2438  }
2439  
2440  /**
2441   * Creates the form for external url
2442   *
2443   * @since 2.7.0
2444   *
2445   * @param string $default_view
2446   * @return string the form html
2447   */
2448  function wp_media_insert_url_form( $default_view = 'image' ) {
2449      /** This filter is documented in wp-admin/includes/media.php */
2450      if ( ! apply_filters( 'disable_captions', '' ) ) {
2451          $caption = '
2452          <tr class="image-only">
2453              <th scope="row" class="label">
2454                  <label for="caption"><span class="alignleft">' . __('Image Caption') . '</span></label>
2455              </th>
2456              <td class="field"><textarea id="caption" name="caption"></textarea></td>
2457          </tr>
2458  ';
2459      } else {
2460          $caption = '';
2461      }
2462  
2463      $default_align = get_option('image_default_align');
2464      if ( empty($default_align) )
2465          $default_align = 'none';
2466  
2467      if ( 'image' == $default_view ) {
2468          $view = 'image-only';
2469          $table_class = '';
2470      } else {
2471          $view = $table_class = 'not-image';
2472      }
2473  
2474      return '
2475      <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>
2476      <table class="describe ' . $table_class . '"><tbody>
2477          <tr>
2478              <th scope="row" class="label" style="width:130px;">
2479                  <label for="src"><span class="alignleft">' . __('URL') . '</span></label>
2480                  <span class="alignright"><abbr id="status_img" title="required" class="required">*</abbr></span>
2481              </th>
2482              <td class="field"><input id="src" name="src" value="" type="text" aria-required="true" onblur="addExtImage.getImageData()" /></td>
2483          </tr>
2484  
2485          <tr>
2486              <th scope="row" class="label">
2487                  <label for="title"><span class="alignleft">' . __('Title') . '</span></label>
2488                  <span class="alignright"><abbr title="required" class="required">*</abbr></span>
2489              </th>
2490              <td class="field"><input id="title" name="title" value="" type="text" aria-required="true" /></td>
2491          </tr>
2492  
2493          <tr class="not-image"><td></td><td><p class="help">' . __('Link text, e.g. &#8220;Ransom Demands (PDF)&#8221;') . '</p></td></tr>
2494  
2495          <tr class="image-only">
2496              <th scope="row" class="label">
2497                  <label for="alt"><span class="alignleft">' . __('Alternative Text') . '</span></label>
2498              </th>
2499              <td class="field"><input id="alt" name="alt" value="" type="text" aria-required="true" />
2500              <p class="help">' . __('Alt text for the image, e.g. &#8220;The Mona Lisa&#8221;') . '</p></td>
2501          </tr>
2502          ' . $caption . '
2503          <tr class="align image-only">
2504              <th scope="row" class="label"><p><label for="align">' . __('Alignment') . '</label></p></th>
2505              <td class="field">
2506                  <input name="align" id="align-none" value="none" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'none' ? ' checked="checked"' : '').' />
2507                  <label for="align-none" class="align image-align-none-label">' . __('None') . '</label>
2508                  <input name="align" id="align-left" value="left" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'left' ? ' checked="checked"' : '').' />
2509                  <label for="align-left" class="align image-align-left-label">' . __('Left') . '</label>
2510                  <input name="align" id="align-center" value="center" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'center' ? ' checked="checked"' : '').' />
2511                  <label for="align-center" class="align image-align-center-label">' . __('Center') . '</label>
2512                  <input name="align" id="align-right" value="right" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'right' ? ' checked="checked"' : '').' />
2513                  <label for="align-right" class="align image-align-right-label">' . __('Right') . '</label>
2514              </td>
2515          </tr>
2516  
2517          <tr class="image-only">
2518              <th scope="row" class="label">
2519                  <label for="url"><span class="alignleft">' . __('Link Image To:') . '</span></label>
2520              </th>
2521              <td class="field"><input id="url" name="url" value="" type="text" /><br />
2522  
2523              <button type="button" class="button" value="" onclick="document.forms[0].url.value=null">' . __('None') . '</button>
2524              <button type="button" class="button" value="" onclick="document.forms[0].url.value=document.forms[0].src.value">' . __('Link to image') . '</button>
2525              <p class="help">' . __('Enter a link URL or click above for presets.') . '</p></td>
2526          </tr>
2527          <tr class="image-only">
2528              <td></td>
2529              <td>
2530                  <input type="button" class="button" id="go_button" style="color:#bbb;" onclick="addExtImage.insert()" value="' . esc_attr__('Insert into Post') . '" />
2531              </td>
2532          </tr>
2533          <tr class="not-image">
2534              <td></td>
2535              <td>
2536                  ' . get_submit_button( __( 'Insert into Post' ), 'button', 'insertonlybutton', false ) . '
2537              </td>
2538          </tr>
2539      </tbody></table>
2540  ';
2541  
2542  }
2543  
2544  /**
2545   * Displays the multi-file uploader message.
2546   *
2547   * @since 2.6.0
2548   */
2549  function media_upload_flash_bypass() {
2550      $browser_uploader = admin_url( 'media-new.php?browser-uploader' );
2551  
2552      if ( $post = get_post() )
2553          $browser_uploader .= '&amp;post_id=' . intval( $post->ID );
2554      elseif ( ! empty( $GLOBALS['post_ID'] ) )
2555          $browser_uploader .= '&amp;post_id=' . intval( $GLOBALS['post_ID'] );
2556  
2557      ?>
2558      <p class="upload-flash-bypass">
2559      <?php printf( __( 'You are using the multi-file uploader. Problems? Try the <a href="%1$s" target="%2$s">browser uploader</a> instead.' ), $browser_uploader, '_blank' ); ?>
2560      </p>
2561      <?php
2562  }
2563  add_action('post-plupload-upload-ui', 'media_upload_flash_bypass');
2564  
2565  /**
2566   * Displays the browser's built-in uploader message.
2567   *
2568   * @since 2.6.0
2569   */
2570  function media_upload_html_bypass() {
2571      ?>
2572      <p class="upload-html-bypass hide-if-no-js">
2573         <?php _e('You are using the browser&#8217;s built-in file uploader. The WordPress uploader includes multiple file selection and drag and drop capability. <a href="#">Switch to the multi-file uploader</a>.'); ?>
2574      </p>
2575      <?php
2576  }
2577  add_action('post-html-upload-ui', 'media_upload_html_bypass');
2578  
2579  /**
2580   * Used to display a "After a file has been uploaded..." help message.
2581   *
2582   * @since 3.3.0
2583   */
2584  function media_upload_text_after() {}
2585  
2586  /**
2587   * Displays the checkbox to scale images.
2588   *
2589   * @since 3.3.0
2590   */
2591  function media_upload_max_image_resize() {
2592      $checked = get_user_setting('upload_resize') ? ' checked="true"' : '';
2593      $a = $end = '';
2594  
2595      if ( current_user_can( 'manage_options' ) ) {
2596          $a = '<a href="' . esc_url( admin_url( 'options-media.php' ) ) . '" target="_blank">';
2597          $end = '</a>';
2598      }
2599  ?>
2600  <p class="hide-if-no-js"><label>
2601  <input name="image_resize" type="checkbox" id="image_resize" value="true"<?php echo $checked; ?> />
2602  <?php
2603      /* translators: %1$s is link start tag, %2$s is link end tag, %3$d is width, %4$d is height*/
2604      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' ) );
2605  ?>
2606  </label></p>
2607  <?php
2608  }
2609  
2610  /**
2611   * Displays the out of storage quota message in Multisite.
2612   *
2613   * @since 3.5.0
2614   */
2615  function multisite_over_quota_message() {
2616      echo '<p>' . sprintf( __( 'Sorry, you have used all of your storage quota of %s MB.' ), get_space_allowed() ) . '</p>';
2617  }
2618  
2619  /**
2620   * Displays the image and editor in the post editor
2621   *
2622   * @since 3.5.0
2623   */
2624  function edit_form_image_editor( $post ) {
2625      $open = isset( $_GET['image-editor'] );
2626      if ( $open )
2627          require_once  ABSPATH . 'wp-admin/includes/image-edit.php';
2628  
2629      $thumb_url = false;
2630      if ( $attachment_id = intval( $post->ID ) )
2631          $thumb_url = wp_get_attachment_image_src( $attachment_id, array( 900, 450 ), true );
2632  
2633      $alt_text = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
2634  
2635      $att_url = wp_get_attachment_url( $post->ID ); ?>
2636      <div class="wp_attachment_holder">
2637      <?php
2638      if ( wp_attachment_is_image( $post->ID ) ) :
2639          $image_edit_button = '';
2640          if ( wp_image_editor_supports( array( 'mime_type' => $post->post_mime_type ) ) ) {
2641              $nonce = wp_create_nonce( "image_editor-$post->ID" );
2642              $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' ) . "' /> <span class='spinner'></span>";
2643          }
2644      ?>
2645  
2646          <div class="imgedit-response" id="imgedit-response-<?php echo $attachment_id; ?>"></div>
2647  
2648          <div<?php if ( $open ) echo ' style="display:none"'; ?> class="wp_attachment_image" id="media-head-<?php echo $attachment_id; ?>">
2649              <p id="thumbnail-head-<?php echo $attachment_id; ?>"><img class="thumbnail" src="<?php echo set_url_scheme( $thumb_url[0] ); ?>" style="max-width:100%" alt="" /></p>
2650              <p><?php echo $image_edit_button; ?></p>
2651          </div>
2652          <div<?php if ( ! $open ) echo ' style="display:none"'; ?> class="image-editor" id="image-editor-<?php echo $attachment_id; ?>">
2653              <?php if ( $open ) wp_image_editor( $attachment_id ); ?>
2654          </div>
2655      <?php
2656      elseif ( $attachment_id && 0 === strpos( $post->post_mime_type, 'audio/' ) ):
2657  
2658          wp_maybe_generate_attachment_metadata( $post );
2659  
2660          echo wp_audio_shortcode( array( 'src' => $att_url ) );
2661  
2662      elseif ( $attachment_id && 0 === strpos( $post->post_mime_type, 'video/' ) ):
2663  
2664          wp_maybe_generate_attachment_metadata( $post );
2665  
2666          $meta = wp_get_attachment_metadata( $attachment_id );
2667          $w = ! empty( $meta['width'] ) ? min( $meta['width'], 640 ) : 0;
2668          $h = ! empty( $meta['height'] ) ? $meta['height'] : 0;
2669          if ( $h && $w < $meta['width'] ) {
2670              $h = round( ( $meta['height'] * $w ) / $meta['width'] );
2671          }
2672  
2673          $attr = array( 'src' => $att_url );
2674          if ( ! empty( $w ) && ! empty( $h ) ) {
2675              $attr['width'] = $w;
2676              $attr['height'] = $h;
2677          }
2678  
2679          $thumb_id = get_post_thumbnail_id( $attachment_id );
2680          if ( ! empty( $thumb_id ) ) {
2681              $attr['poster'] = wp_get_attachment_url( $thumb_id );
2682          }
2683  
2684          echo wp_video_shortcode( $attr );
2685  
2686      endif; ?>
2687      </div>
2688      <div class="wp_attachment_details edit-form-section">
2689          <p>
2690              <label for="attachment_caption"><strong><?php _e( 'Caption' ); ?></strong></label><br />
2691              <textarea class="widefat" name="excerpt" id="attachment_caption"><?php echo $post->post_excerpt; ?></textarea>
2692          </p>
2693  
2694  
2695      <?php if ( 'image' === substr( $post->post_mime_type, 0, 5 ) ) : ?>
2696          <p>
2697              <label for="attachment_alt"><strong><?php _e( 'Alternative Text' ); ?></strong></label><br />
2698              <input type="text" class="widefat" name="_wp_attachment_image_alt" id="attachment_alt" value="<?php echo esc_attr( $alt_text ); ?>" />
2699          </p>
2700      <?php endif; ?>
2701  
2702      <?php
2703          $quicktags_settings = array( 'buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,close' );
2704          $editor_args = array(
2705              'textarea_name' => 'content',
2706              'textarea_rows' => 5,
2707              'media_buttons' => false,
2708              'tinymce' => false,
2709              'quicktags' => $quicktags_settings,
2710          );
2711      ?>
2712  
2713      <label for="content"><strong><?php _e( 'Description' ); ?></strong><?php
2714      if ( preg_match( '#^(audio|video)/#', $post->post_mime_type ) ) {
2715          echo ': ' . __( 'Displayed on attachment pages.' );
2716      } ?></label>
2717      <?php wp_editor( $post->post_content, 'attachment_content', $editor_args ); ?>
2718  
2719      </div>
2720      <?php
2721      $extras = get_compat_media_markup( $post->ID );
2722      echo $extras['item'];
2723      echo '<input type="hidden" id="image-edit-context" value="edit-attachment" />' . "\n";
2724  }
2725  
2726  /**
2727   * Displays non-editable attachment metadata in the publish metabox
2728   *
2729   * @since 3.5.0
2730   */
2731  function attachment_submitbox_metadata() {
2732      $post = get_post();
2733  
2734      $filename = esc_html( wp_basename( $post->guid ) );
2735  
2736      $media_dims = '';
2737      $meta = wp_get_attachment_metadata( $post->ID );
2738      if ( isset( $meta['width'], $meta['height'] ) )
2739          $media_dims .= "<span id='media-dims-$post->ID'>{$meta['width']}&nbsp;&times;&nbsp;{$meta['height']}</span> ";
2740      /** This filter is documented in wp-admin/includes/media.php */
2741      $media_dims = apply_filters( 'media_meta', $media_dims, $post );
2742  
2743      $att_url = wp_get_attachment_url( $post->ID );
2744  ?>
2745      <div class="misc-pub-section misc-pub-attachment">
2746              <label for="attachment_url"><?php _e( 'File URL:' ); ?></label>
2747              <input type="text" class="widefat urlfield" readonly="readonly" name="attachment_url" value="<?php echo esc_attr($att_url); ?>" />
2748      </div>
2749      <div class="misc-pub-section misc-pub-filename">
2750          <?php _e( 'File name:' ); ?> <strong><?php echo $filename; ?></strong>
2751      </div>
2752      <div class="misc-pub-section misc-pub-filetype">
2753          <?php _e( 'File type:' ); ?> <strong><?php
2754              if ( preg_match( '/^.*?\.(\w+)$/', get_attached_file( $post->ID ), $matches ) ) {
2755                  echo esc_html( strtoupper( $matches[1] ) );
2756                  list( $mime_type ) = explode( '/', $post->post_mime_type );
2757                  if ( $mime_type !== 'image' && ! empty( $meta['mime_type'] ) ) {
2758                      if ( $meta['mime_type'] !== "$mime_type/" . strtolower( $matches[1] ) ) {
2759                          echo ' (' . $meta['mime_type'] . ')';
2760                      }
2761                  }
2762              } else {
2763                  echo strtoupper( str_replace( 'image/', '', $post->post_mime_type ) );
2764              }
2765          ?></strong>
2766      </div>
2767  
2768      <?php
2769          $file  = get_attached_file( $post->ID );
2770          $file_size = false;
2771  
2772          if ( isset( $meta['filesize'] ) )
2773              $file_size = $meta['filesize'];
2774          elseif ( file_exists( $file ) )
2775              $file_size = filesize( $file );
2776  
2777          if ( ! empty( $file_size ) ) : ?>
2778              <div class="misc-pub-section misc-pub-filesize">
2779                  <?php _e( 'File size:' ); ?> <strong><?php echo size_format( $file_size ); ?></strong>
2780              </div>
2781              <?php
2782          endif;
2783  
2784      if ( preg_match( '#^(audio|video)/#', $post->post_mime_type ) ) {
2785  
2786          /**
2787           * Filter the audio and video metadata fields to be shown in the publish meta box.
2788           *
2789           * The key for each item in the array should correspond to an attachment
2790           * metadata key, and the value should be the desired label.
2791           *
2792           * @since 3.7.0
2793           *
2794           * @param array $fields An array of the attachment metadata keys and labels.
2795           */
2796          $fields = apply_filters( 'media_submitbox_misc_sections', array(
2797              'length_formatted' => __( 'Length:' ),
2798              'bitrate'          => __( 'Bitrate:' ),
2799          ) );
2800  
2801          foreach ( $fields as $key => $label ) {
2802              if ( empty( $meta[ $key ] ) ) {
2803                  continue;
2804              }
2805      ?>
2806          <div class="misc-pub-section misc-pub-mime-meta misc-pub-<?php echo sanitize_html_class( $key ); ?>">
2807              <?php echo $label ?> <strong><?php
2808                  switch ( $key ) {
2809                      case 'bitrate' :
2810                          echo round( $meta['bitrate'] / 1000 ) . 'kb/s';
2811                          if ( ! empty( $meta['bitrate_mode'] ) ) {
2812                              echo ' ' . strtoupper( esc_html( $meta['bitrate_mode'] ) );
2813                          }
2814                          break;
2815                      default:
2816                          echo esc_html( $meta[ $key ] );
2817                          break;
2818                  }
2819              ?></strong>
2820          </div>
2821      <?php
2822          }
2823  
2824          /**
2825           * Filter the audio attachment metadata fields to be shown in the publish meta box.
2826           *
2827           * The key for each item in the array should correspond to an attachment
2828           * metadata key, and the value should be the desired label.
2829           *
2830           * @since 3.7.0
2831           *
2832           * @param array $fields An array of the attachment metadata keys and labels.
2833           */
2834          $audio_fields = apply_filters( 'audio_submitbox_misc_sections', array(
2835              'dataformat' => __( 'Audio Format:' ),
2836              'codec'      => __( 'Audio Codec:' )
2837          ) );
2838  
2839          foreach ( $audio_fields as $key => $label ) {
2840              if ( empty( $meta['audio'][ $key ] ) ) {
2841                  continue;
2842              }
2843      ?>
2844          <div class="misc-pub-section misc-pub-audio misc-pub-<?php echo sanitize_html_class( $key ); ?>">
2845              <?php echo $label; ?> <strong><?php echo esc_html( $meta['audio'][$key] ); ?></strong>
2846          </div>
2847      <?php
2848          }
2849  
2850      }
2851  
2852      if ( $media_dims ) : ?>
2853      <div class="misc-pub-section misc-pub-dimensions">
2854          <?php _e( 'Dimensions:' ); ?> <strong><?php echo $media_dims; ?></strong>
2855      </div>
2856  <?php
2857      endif;
2858  }
2859  
2860  add_filter( 'async_upload_image', 'get_media_item', 10, 2 );
2861  add_filter( 'async_upload_audio', 'get_media_item', 10, 2 );
2862  add_filter( 'async_upload_video', 'get_media_item', 10, 2 );
2863  add_filter( 'async_upload_file',  'get_media_item', 10, 2 );
2864  
2865  add_action( 'media_upload_image', 'wp_media_upload_handler' );
2866  add_action( 'media_upload_audio', 'wp_media_upload_handler' );
2867  add_action( 'media_upload_video', 'wp_media_upload_handler' );
2868  add_action( 'media_upload_file',  'wp_media_upload_handler' );
2869  
2870  add_filter( 'media_upload_gallery', 'media_upload_gallery' );
2871  add_filter( 'media_upload_library', 'media_upload_library' );
2872  
2873  add_action( 'attachment_submitbox_misc_actions', 'attachment_submitbox_metadata' );
2874  
2875  /**
2876   * Parse ID3v2, ID3v1, and getID3 comments to extract usable data
2877   *
2878   * @since 3.6.0
2879   *
2880   * @param array $metadata An existing array with data
2881   * @param array $data Data supplied by ID3 tags
2882   */
2883  function wp_add_id3_tag_data( &$metadata, $data ) {
2884      foreach ( array( 'id3v2', 'id3v1' ) as $version ) {
2885          if ( ! empty( $data[$version]['comments'] ) ) {
2886              foreach ( $data[$version]['comments'] as $key => $list ) {
2887                  if ( 'length' !== $key && ! empty( $list ) ) {
2888                      $metadata[$key] = reset( $list );
2889                      // Fix bug in byte stream analysis.
2890                      if ( 'terms_of_use' === $key && 0 === strpos( $metadata[$key], 'yright notice.' ) )
2891                          $metadata[$key] = 'Cop' . $metadata[$key];
2892                  }
2893              }
2894              break;
2895          }
2896      }
2897  
2898      if ( ! empty( $data['id3v2']['APIC'] ) ) {
2899          $image = reset( $data['id3v2']['APIC']);
2900          if ( ! empty( $image['data'] ) ) {
2901              $metadata['image'] = array(
2902                  'data' => $image['data'],
2903                  'mime' => $image['image_mime'],
2904                  'width' => $image['image_width'],
2905                  'height' => $image['image_height']
2906              );
2907          }
2908      } elseif ( ! empty( $data['comments']['picture'] ) ) {
2909          $image = reset( $data['comments']['picture'] );
2910          if ( ! empty( $image['data'] ) ) {
2911              $metadata['image'] = array(
2912                  'data' => $image['data'],
2913                  'mime' => $image['image_mime']
2914              );
2915          }
2916      }
2917  }
2918  
2919  /**
2920   * Retrieve metadata from a video file's ID3 tags
2921   *
2922   * @since 3.6.0
2923   *
2924   * @param string $file Path to file.
2925   * @return array|bool Returns array of metadata, if found.
2926   */
2927  function wp_read_video_metadata( $file ) {
2928      if ( ! file_exists( $file ) )
2929          return false;
2930  
2931      $metadata = array();
2932  
2933      if ( ! class_exists( 'getID3' ) )
2934          require( ABSPATH . WPINC . '/ID3/getid3.php' );
2935      $id3 = new getID3();
2936      $data = $id3->analyze( $file );
2937  
2938      if ( isset( $data['video']['lossless'] ) )
2939          $metadata['lossless'] = $data['video']['lossless'];
2940      if ( ! empty( $data['video']['bitrate'] ) )
2941          $metadata['bitrate'] = (int) $data['video']['bitrate'];
2942      if ( ! empty( $data['video']['bitrate_mode'] ) )
2943          $metadata['bitrate_mode'] = $data['video']['bitrate_mode'];
2944      if ( ! empty( $data['filesize'] ) )
2945          $metadata['filesize'] = (int) $data['filesize'];
2946      if ( ! empty( $data['mime_type'] ) )
2947          $metadata['mime_type'] = $data['mime_type'];
2948      if ( ! empty( $data['playtime_seconds'] ) )
2949          $metadata['length'] = (int) round( $data['playtime_seconds'] );
2950      if ( ! empty( $data['playtime_string'] ) )
2951          $metadata['length_formatted'] = $data['playtime_string'];
2952      if ( ! empty( $data['video']['resolution_x'] ) )
2953          $metadata['width'] = (int) $data['video']['resolution_x'];
2954      if ( ! empty( $data['video']['resolution_y'] ) )
2955          $metadata['height'] = (int) $data['video']['resolution_y'];
2956      if ( ! empty( $data['fileformat'] ) )
2957          $metadata['fileformat'] = $data['fileformat'];
2958      if ( ! empty( $data['video']['dataformat'] ) )
2959          $metadata['dataformat'] = $data['video']['dataformat'];
2960      if ( ! empty( $data['video']['encoder'] ) )
2961          $metadata['encoder'] = $data['video']['encoder'];
2962      if ( ! empty( $data['video']['codec'] ) )
2963          $metadata['codec'] = $data['video']['codec'];
2964  
2965      if ( ! empty( $data['audio'] ) ) {
2966          unset( $data['audio']['streams'] );
2967          $metadata['audio'] = $data['audio'];
2968      }
2969  
2970      wp_add_id3_tag_data( $metadata, $data );
2971  
2972      return $metadata;
2973  }
2974  
2975  /**
2976   * Retrieve metadata from a audio file's ID3 tags
2977   *
2978   * @since 3.6.0
2979   *
2980   * @param string $file Path to file.
2981   * @return array|boolean Returns array of metadata, if found.
2982   */
2983  function wp_read_audio_metadata( $file ) {
2984      if ( ! file_exists( $file ) )
2985          return false;
2986      $metadata = array();
2987  
2988      if ( ! class_exists( 'getID3' ) )
2989          require( ABSPATH . WPINC . '/ID3/getid3.php' );
2990      $id3 = new getID3();
2991      $data = $id3->analyze( $file );
2992  
2993      if ( ! empty( $data['audio'] ) ) {
2994          unset( $data['audio']['streams'] );
2995          $metadata = $data['audio'];
2996      }
2997  
2998      if ( ! empty( $data['fileformat'] ) )
2999          $metadata['fileformat'] = $data['fileformat'];
3000      if ( ! empty( $data['filesize'] ) )
3001          $metadata['filesize'] = (int) $data['filesize'];
3002      if ( ! empty( $data['mime_type'] ) )
3003          $metadata['mime_type'] = $data['mime_type'];
3004      if ( ! empty( $data['playtime_seconds'] ) )
3005          $metadata['length'] = (int) round( $data['playtime_seconds'] );
3006      if ( ! empty( $data['playtime_string'] ) )
3007          $metadata['length_formatted'] = $data['playtime_string'];
3008  
3009      wp_add_id3_tag_data( $metadata, $data );
3010  
3011      return $metadata;
3012  }


Generated: Thu Mar 5 03:55:53 2015 Hosted by follow the white rabbit.