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


Generated: Wed Jul 23 03:55:39 2014 Hosted by follow the white rabbit.