[ Index ]

PHP Cross Reference of WordPress (Trunk)

title

Body

[close]

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

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


Generated: Thu Oct 23 03:37:05 2014 Cross-referenced by PHPXref 0.7