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


Generated: Wed Jul 1 03:55:52 2015 Hosted by follow the white rabbit.