[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

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


Generated: Mon May 25 03:55:56 2015 Hosted by follow the white rabbit.