PageRenderTime 24ms CodeModel.GetById 4ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/includes/media.php

https://bitbucket.org/abnopanda/wordpress
PHP | 2345 lines | 2286 code | 18 blank | 41 comment | 11 complexity | e4b411a17d748fbb59a5985102763ef4 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   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 */
  16function 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 * Adds the gallery tab back to the tabs array if post has image attachments
  29 *
  30 * @since 2.5.0
  31 *
  32 * @param array $tabs
  33 * @return array $tabs with gallery if post has image attachment
  34 */
  35function 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}
  57add_filter('media_upload_tabs', 'update_gallery_tab');
  58
  59/**
  60 * {@internal Missing Short Description}}
  61 *
  62 * @since 2.5.0
  63 */
  64function 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 integer $id image attachment id
  98 * @param string $caption image caption
  99 * @param string $alt image alt attribute
 100 * @param string $title image title attribute
 101 * @param string $align image css alignment property
 102 * @param string $url image src url
 103 * @param string|bool $rel image rel attribute
 104 * @param string $size image size (thumbnail, medium, large, full or added  with add_image_size() )
 105 * @return string the html to insert into editor
 106 */
 107function get_image_send_to_editor($id, $caption, $title, $align, $url='', $rel = false, $size='medium', $alt = '') {
 108
 109	$html = get_image_tag($id, $alt, '', $align, $size);
 110
 111	$rel = $rel ? ' rel="attachment wp-att-' . esc_attr($id).'"' : '';
 112
 113	if ( $url )
 114		$html = '<a href="' . esc_attr($url) . "\"$rel>$html</a>";
 115
 116	$html = apply_filters( 'image_send_to_editor', $html, $id, $caption, $title, $align, $url, $size, $alt );
 117
 118	return $html;
 119}
 120
 121/**
 122 * Adds image shortcode with caption to editor
 123 *
 124 * @since 2.6.0
 125 *
 126 * @param string $html
 127 * @param integer $id
 128 * @param string $caption image caption
 129 * @param string $alt image alt attribute
 130 * @param string $title image title attribute
 131 * @param string $align image css alignment property
 132 * @param string $url image src url
 133 * @param string $size image size (thumbnail, medium, large, full or added with add_image_size() )
 134 * @return string
 135 */
 136function image_add_caption( $html, $id, $caption, $title, $align, $url, $size, $alt = '' ) {
 137
 138	if ( empty($caption) || apply_filters( 'disable_captions', '' ) )
 139		return $html;
 140
 141	$id = ( 0 < (int) $id ) ? 'attachment_' . $id : '';
 142
 143	if ( ! preg_match( '/width=["\']([0-9]+)/', $html, $matches ) )
 144		return $html;
 145
 146	$width = $matches[1];
 147
 148	$caption = str_replace( array("\r\n", "\r"), "\n", $caption);
 149	$caption = preg_replace_callback( '/<[a-zA-Z0-9]+(?: [^<>]+>)*/', '_cleanup_image_add_caption', $caption );
 150	// convert any remaining line breaks to <br>
 151	$caption = preg_replace( '/[ \n\t]*\n[ \t]*/', '<br />', $caption );
 152
 153	$html = preg_replace( '/(class=["\'][^\'"]*)align(none|left|right|center)\s?/', '$1', $html );
 154	if ( empty($align) )
 155		$align = 'none';
 156
 157	$shcode = '[caption id="' . $id . '" align="align' . $align	. '" width="' . $width . '"]' . $html . ' ' . $caption . '[/caption]';
 158
 159	return apply_filters( 'image_add_caption_shortcode', $shcode, $html );
 160}
 161add_filter( 'image_send_to_editor', 'image_add_caption', 20, 8 );
 162
 163/**
 164 * Private preg_replace callback used in image_add_caption()
 165 *
 166 * @access private
 167 * @since 3.4.0
 168 */
 169function _cleanup_image_add_caption( $matches ) {
 170	// remove any line breaks from inside the tags
 171	return preg_replace( '/[\r\n\t]+/', ' ', $matches[0] );
 172}
 173
 174/**
 175 * Adds image html to editor
 176 *
 177 * @since 2.5.0
 178 *
 179 * @param string $html
 180 */
 181function media_send_to_editor($html) {
 182?>
 183<script type="text/javascript">
 184/* <![CDATA[ */
 185var win = window.dialogArguments || opener || parent || top;
 186win.send_to_editor('<?php echo addslashes($html); ?>');
 187/* ]]> */
 188</script>
 189<?php
 190	exit;
 191}
 192
 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 */
 204function 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 */
 269function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array()) {
 270	$overrides = array('test_form'=>false);
 271
 272	$time = current_time( 'mysql' );
 273	if ( $post = get_post( $post_id ) ) {
 274		if ( substr( $post->post_date, 0, 4 ) > 0 )
 275			$time = $post->post_date;
 276	}
 277
 278	$file = wp_handle_sideload( $file_array, $overrides, $time );
 279	if ( isset($file['error']) )
 280		return new WP_Error( 'upload_error', $file['error'] );
 281
 282	$url = $file['url'];
 283	$type = $file['type'];
 284	$file = $file['file'];
 285	$title = preg_replace('/\.[^.]+$/', '', basename($file));
 286	$content = '';
 287
 288	// use image exif/iptc data for title and caption defaults if possible
 289	if ( $image_meta = @wp_read_image_metadata($file) ) {
 290		if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) )
 291			$title = $image_meta['title'];
 292		if ( trim( $image_meta['caption'] ) )
 293			$content = $image_meta['caption'];
 294	}
 295
 296	if ( isset( $desc ) )
 297		$title = $desc;
 298
 299	// Construct the attachment array
 300	$attachment = array_merge( array(
 301		'post_mime_type' => $type,
 302		'guid' => $url,
 303		'post_parent' => $post_id,
 304		'post_title' => $title,
 305		'post_content' => $content,
 306	), $post_data );
 307
 308	// This should never be set as it would then overwrite an existing attachment.
 309	if ( isset( $attachment['ID'] ) )
 310		unset( $attachment['ID'] );
 311
 312	// Save the attachment metadata
 313	$id = wp_insert_attachment($attachment, $file, $post_id);
 314	if ( !is_wp_error($id) )
 315		wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
 316
 317	return $id;
 318}
 319
 320/**
 321 * Adds the iframe to display content for the media upload page
 322 *
 323 * @since 2.5.0
 324 *
 325 * @param array $content_func
 326 */
 327function wp_iframe($content_func /* ... */) {
 328	_wp_admin_html_begin();
 329?>
 330<title><?php bloginfo('name') ?> &rsaquo; <?php _e('Uploads'); ?> &#8212; <?php _e('WordPress'); ?></title>
 331<?php
 332
 333wp_enqueue_style( 'colors' );
 334// Check callback name for 'media'
 335if ( ( is_array( $content_func ) && ! empty( $content_func[1] ) && 0 === strpos( (string) $content_func[1], 'media' ) )
 336	|| ( ! is_array( $content_func ) && 0 === strpos( $content_func, 'media' ) ) )
 337	wp_enqueue_style( 'media' );
 338wp_enqueue_style( 'ie' );
 339?>
 340<script type="text/javascript">
 341//<![CDATA[
 342addLoadEvent = 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();}}};
 343var 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(); ?>'};
 344var ajaxurl = '<?php echo admin_url( 'admin-ajax.php', 'relative' ); ?>', pagenow = 'media-upload-popup', adminpage = 'media-upload-popup',
 345isRtl = <?php echo (int) is_rtl(); ?>;
 346//]]>
 347</script>
 348<?php
 349do_action('admin_enqueue_scripts', 'media-upload-popup');
 350do_action('admin_print_styles-media-upload-popup');
 351do_action('admin_print_styles');
 352do_action('admin_print_scripts-media-upload-popup');
 353do_action('admin_print_scripts');
 354do_action('admin_head-media-upload-popup');
 355do_action('admin_head');
 356
 357if ( is_string($content_func) )
 358	do_action( "admin_head_{$content_func}" );
 359?>
 360</head>
 361<body<?php if ( isset($GLOBALS['body_id']) ) echo ' id="' . $GLOBALS['body_id'] . '"'; ?> class="no-js">
 362<script type="text/javascript">
 363document.body.className = document.body.className.replace('no-js', 'js');
 364</script>
 365<?php
 366	$args = func_get_args();
 367	$args = array_slice($args, 1);
 368	call_user_func_array($content_func, $args);
 369
 370	do_action('admin_print_footer_scripts');
 371?>
 372<script type="text/javascript">if(typeof wpOnload=='function')wpOnload();</script>
 373</body>
 374</html>
 375<?php
 376}
 377
 378/**
 379 * Adds the media button to the editor
 380 *
 381 * @since 2.5.0
 382 *
 383 * @param string $editor_id
 384 */
 385function media_buttons($editor_id = 'content') {
 386	wp_enqueue_media( array(
 387		'post' => get_post()
 388	) );
 389
 390	// $context = apply_filters('media_buttons_context', __('Upload/Insert %s'));
 391
 392	$img = '<span class="wp-media-buttons-icon"></span> ';
 393
 394	echo '<a href="#" class="button insert-media add_media" data-editor="' . esc_attr( $editor_id ) . '" title="' . esc_attr__( 'Add Media' ) . '">' . $img . __( 'Add Media' ) . '</a>';
 395
 396	// 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>';
 397}
 398add_action( 'media_buttons', 'media_buttons' );
 399
 400function get_upload_iframe_src( $type = null, $post_id = null, $tab = null ) {
 401	global $post_ID;
 402
 403	if ( empty( $post_id ) )
 404		$post_id = $post_ID;
 405
 406	$upload_iframe_src = add_query_arg( 'post_id', (int) $post_id, admin_url('media-upload.php') );
 407
 408	if ( $type && 'media' != $type )
 409		$upload_iframe_src = add_query_arg('type', $type, $upload_iframe_src);
 410
 411	if ( ! empty( $tab ) )
 412		$upload_iframe_src = add_query_arg('tab', $tab, $upload_iframe_src);
 413
 414	$upload_iframe_src = apply_filters($type . '_upload_iframe_src', $upload_iframe_src);
 415
 416	return add_query_arg('TB_iframe', true, $upload_iframe_src);
 417}
 418
 419/**
 420 * {@internal Missing Short Description}}
 421 *
 422 * @since 2.5.0
 423 *
 424 * @return mixed void|object WP_Error on failure
 425 */
 426function media_upload_form_handler() {
 427	check_admin_referer('media-form');
 428
 429	$errors = null;
 430
 431	if ( isset($_POST['send']) ) {
 432		$keys = array_keys($_POST['send']);
 433		$send_id = (int) array_shift($keys);
 434	}
 435
 436	if ( !empty($_POST['attachments']) ) foreach ( $_POST['attachments'] as $attachment_id => $attachment ) {
 437		$post = $_post = get_post($attachment_id, ARRAY_A);
 438		$post_type_object = get_post_type_object( $post[ 'post_type' ] );
 439
 440		if ( !current_user_can( $post_type_object->cap->edit_post, $attachment_id ) )
 441			continue;
 442
 443		if ( isset($attachment['post_content']) )
 444			$post['post_content'] = $attachment['post_content'];
 445		if ( isset($attachment['post_title']) )
 446			$post['post_title'] = $attachment['post_title'];
 447		if ( isset($attachment['post_excerpt']) )
 448			$post['post_excerpt'] = $attachment['post_excerpt'];
 449		if ( isset($attachment['menu_order']) )
 450			$post['menu_order'] = $attachment['menu_order'];
 451
 452		if ( isset($send_id) && $attachment_id == $send_id ) {
 453			if ( isset($attachment['post_parent']) )
 454				$post['post_parent'] = $attachment['post_parent'];
 455		}
 456
 457		$post = apply_filters('attachment_fields_to_save', $post, $attachment);
 458
 459		if ( isset($attachment['image_alt']) ) {
 460			$image_alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
 461			if ( $image_alt != stripslashes($attachment['image_alt']) ) {
 462				$image_alt = wp_strip_all_tags( stripslashes($attachment['image_alt']), true );
 463				// update_meta expects slashed
 464				update_post_meta( $attachment_id, '_wp_attachment_image_alt', addslashes($image_alt) );
 465			}
 466		}
 467
 468		if ( isset($post['errors']) ) {
 469			$errors[$attachment_id] = $post['errors'];
 470			unset($post['errors']);
 471		}
 472
 473		if ( $post != $_post )
 474			wp_update_post($post);
 475
 476		foreach ( get_attachment_taxonomies($post) as $t ) {
 477			if ( isset($attachment[$t]) )
 478				wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $attachment[$t])), $t, false);
 479		}
 480	}
 481
 482	if ( isset($_POST['insert-gallery']) || isset($_POST['update-gallery']) ) { ?>
 483		<script type="text/javascript">
 484		/* <![CDATA[ */
 485		var win = window.dialogArguments || opener || parent || top;
 486		win.tb_remove();
 487		/* ]]> */
 488		</script>
 489		<?php
 490		exit;
 491	}
 492
 493	if ( isset($send_id) ) {
 494		$attachment = stripslashes_deep( $_POST['attachments'][$send_id] );
 495
 496		$html = isset( $attachment['post_title'] ) ? $attachment['post_title'] : '';
 497		if ( !empty($attachment['url']) ) {
 498			$rel = '';
 499			if ( strpos($attachment['url'], 'attachment_id') || get_attachment_link($send_id) == $attachment['url'] )
 500				$rel = " rel='attachment wp-att-" . esc_attr($send_id) . "'";
 501			$html = "<a href='{$attachment['url']}'$rel>$html</a>";
 502		}
 503
 504		$html = apply_filters('media_send_to_editor', $html, $send_id, $attachment);
 505		return media_send_to_editor($html);
 506	}
 507
 508	return $errors;
 509}
 510
 511/**
 512 * {@internal Missing Short Description}}
 513 *
 514 * @since 2.5.0
 515 *
 516 * @return mixed
 517 */
 518function wp_media_upload_handler() {
 519	$errors = array();
 520	$id = 0;
 521
 522	if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
 523		check_admin_referer('media-form');
 524		// Upload File button was clicked
 525		$id = media_handle_upload('async-upload', $_REQUEST['post_id']);
 526		unset($_FILES);
 527		if ( is_wp_error($id) ) {
 528			$errors['upload_error'] = $id;
 529			$id = false;
 530		}
 531	}
 532
 533	if ( !empty($_POST['insertonlybutton']) ) {
 534		$src = $_POST['src'];
 535		if ( !empty($src) && !strpos($src, '://') )
 536			$src = "http://$src";
 537
 538		if ( isset( $_POST['media_type'] ) && 'image' != $_POST['media_type'] ) {
 539			$title = esc_html( stripslashes( $_POST['title'] ) );
 540			if ( empty( $title ) )
 541				$title = esc_html( basename( $src ) );
 542
 543			if ( $title && $src )
 544				$html = "<a href='" . esc_url($src) . "'>$title</a>";
 545
 546			$type = 'file';
 547			if ( ( $ext = preg_replace( '/^.+?\.([^.]+)$/', '$1', $src ) ) && ( $ext_type = wp_ext2type( $ext ) )
 548				&& ( 'audio' == $ext_type || 'video' == $ext_type ) )
 549					$type = $ext_type;
 550
 551			$html = apply_filters( $type . '_send_to_editor_url', $html, esc_url_raw( $src ), $title );
 552		} else {
 553			$align = '';
 554			$alt = esc_attr( stripslashes( $_POST['alt'] ) );
 555			if ( isset($_POST['align']) ) {
 556				$align = esc_attr( stripslashes( $_POST['align'] ) );
 557				$class = " class='align$align'";
 558			}
 559			if ( !empty($src) )
 560				$html = "<img src='" . esc_url($src) . "' alt='$alt'$class />";
 561
 562			$html = apply_filters( 'image_send_to_editor_url', $html, esc_url_raw( $src ), $alt, $align );
 563		}
 564
 565		return media_send_to_editor($html);
 566	}
 567
 568	if ( !empty($_POST) ) {
 569		$return = media_upload_form_handler();
 570
 571		if ( is_string($return) )
 572			return $return;
 573		if ( is_array($return) )
 574			$errors = $return;
 575	}
 576
 577	if ( isset($_POST['save']) ) {
 578		$errors['upload_notice'] = __('Saved.');
 579		return media_upload_gallery();
 580	}
 581
 582	if ( isset($_GET['tab']) && $_GET['tab'] == 'type_url' ) {
 583		$type = 'image';
 584		if ( isset( $_GET['type'] ) && in_array( $_GET['type'], array( 'video', 'audio', 'file' ) ) )
 585			$type = $_GET['type'];
 586		return wp_iframe( 'media_upload_type_url_form', $type, $errors, $id );
 587	}
 588
 589	return wp_iframe( 'media_upload_type_form', 'image', $errors, $id );
 590}
 591
 592/**
 593 * Download an image from the specified URL and attach it to a post.
 594 *
 595 * @since 2.6.0
 596 *
 597 * @param string $file The URL of the image to download
 598 * @param int $post_id The post ID the media is to be associated with
 599 * @param string $desc Optional. Description of the image
 600 * @return string|WP_Error Populated HTML img tag on success
 601 */
 602function media_sideload_image($file, $post_id, $desc = null) {
 603	if ( ! empty($file) ) {
 604		// Download file to temp location
 605		$tmp = download_url( $file );
 606
 607		// Set variables for storage
 608		// fix file filename for query strings
 609		preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
 610		$file_array['name'] = basename($matches[0]);
 611		$file_array['tmp_name'] = $tmp;
 612
 613		// If error storing temporarily, unlink
 614		if ( is_wp_error( $tmp ) ) {
 615			@unlink($file_array['tmp_name']);
 616			$file_array['tmp_name'] = '';
 617		}
 618
 619		// do the validation and storage stuff
 620		$id = media_handle_sideload( $file_array, $post_id, $desc );
 621		// If error storing permanently, unlink
 622		if ( is_wp_error($id) ) {
 623			@unlink($file_array['tmp_name']);
 624			return $id;
 625		}
 626
 627		$src = wp_get_attachment_url( $id );
 628	}
 629
 630	// Finally check to make sure the file has been saved, then return the html
 631	if ( ! empty($src) ) {
 632		$alt = isset($desc) ? esc_attr($desc) : '';
 633		$html = "<img src='$src' alt='$alt' />";
 634		return $html;
 635	}
 636}
 637
 638/**
 639 * {@internal Missing Short Description}}
 640 *
 641 * @since 2.5.0
 642 *
 643 * @return unknown
 644 */
 645function media_upload_gallery() {
 646	$errors = array();
 647
 648	if ( !empty($_POST) ) {
 649		$return = media_upload_form_handler();
 650
 651		if ( is_string($return) )
 652			return $return;
 653		if ( is_array($return) )
 654			$errors = $return;
 655	}
 656
 657	wp_enqueue_script('admin-gallery');
 658	return wp_iframe( 'media_upload_gallery_form', $errors );
 659}
 660
 661/**
 662 * {@internal Missing Short Description}}
 663 *
 664 * @since 2.5.0
 665 *
 666 * @return unknown
 667 */
 668function media_upload_library() {
 669	$errors = array();
 670	if ( !empty($_POST) ) {
 671		$return = media_upload_form_handler();
 672
 673		if ( is_string($return) )
 674			return $return;
 675		if ( is_array($return) )
 676			$errors = $return;
 677	}
 678
 679	return wp_iframe( 'media_upload_library_form', $errors );
 680}
 681
 682/**
 683 * Retrieve HTML for the image alignment radio buttons with the specified one checked.
 684 *
 685 * @since 2.7.0
 686 *
 687 * @param object $post
 688 * @param string $checked
 689 * @return string
 690 */
 691function image_align_input_fields( $post, $checked = '' ) {
 692
 693	if ( empty($checked) )
 694		$checked = get_user_setting('align', 'none');
 695
 696	$alignments = array('none' => __('None'), 'left' => __('Left'), 'center' => __('Center'), 'right' => __('Right'));
 697	if ( !array_key_exists( (string) $checked, $alignments ) )
 698		$checked = 'none';
 699
 700	$out = array();
 701	foreach ( $alignments as $name => $label ) {
 702		$name = esc_attr($name);
 703		$out[] = "<input type='radio' name='attachments[{$post->ID}][align]' id='image-align-{$name}-{$post->ID}' value='$name'".
 704		 	( $checked == $name ? " checked='checked'" : "" ) .
 705			" /><label for='image-align-{$name}-{$post->ID}' class='align image-align-{$name}-label'>$label</label>";
 706	}
 707	return join("\n", $out);
 708}
 709
 710/**
 711 * Retrieve HTML for the size radio buttons with the specified one checked.
 712 *
 713 * @since 2.7.0
 714 *
 715 * @param object $post
 716 * @param bool|string $check
 717 * @return array
 718 */
 719function image_size_input_fields( $post, $check = '' ) {
 720
 721		// get a list of the actual pixel dimensions of each possible intermediate version of this image
 722		$size_names = apply_filters( 'image_size_names_choose', array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full Size')) );
 723
 724		if ( empty($check) )
 725			$check = get_user_setting('imgsize', 'medium');
 726
 727		foreach ( $size_names as $size => $label ) {
 728			$downsize = image_downsize($post->ID, $size);
 729			$checked = '';
 730
 731			// is this size selectable?
 732			$enabled = ( $downsize[3] || 'full' == $size );
 733			$css_id = "image-size-{$size}-{$post->ID}";
 734			// if this size is the default but that's not available, don't select it
 735			if ( $size == $check ) {
 736				if ( $enabled )
 737					$checked = " checked='checked'";
 738				else
 739					$check = '';
 740			} elseif ( !$check && $enabled && 'thumbnail' != $size ) {
 741				// if $check is not enabled, default to the first available size that's bigger than a thumbnail
 742				$check = $size;
 743				$checked = " checked='checked'";
 744			}
 745
 746			$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 />";
 747
 748			$html .= "<label for='{$css_id}'>$label</label>";
 749			// only show the dimensions if that choice is available
 750			if ( $enabled )
 751				$html .= " <label for='{$css_id}' class='help'>" . sprintf( "(%d&nbsp;&times;&nbsp;%d)", $downsize[1], $downsize[2] ). "</label>";
 752
 753			$html .= '</div>';
 754
 755			$out[] = $html;
 756		}
 757
 758		return array(
 759			'label' => __('Size'),
 760			'input' => 'html',
 761			'html'  => join("\n", $out),
 762		);
 763}
 764
 765/**
 766 * Retrieve HTML for the Link URL buttons with the default link type as specified.
 767 *
 768 * @since 2.7.0
 769 *
 770 * @param object $post
 771 * @param string $url_type
 772 * @return string
 773 */
 774function image_link_input_fields($post, $url_type = '') {
 775
 776	$file = wp_get_attachment_url($post->ID);
 777	$link = get_attachment_link($post->ID);
 778
 779	if ( empty($url_type) )
 780		$url_type = get_user_setting('urlbutton', 'post');
 781
 782	$url = '';
 783	if ( $url_type == 'file' )
 784		$url = $file;
 785	elseif ( $url_type == 'post' )
 786		$url = $link;
 787
 788	return "
 789	<input type='text' class='text urlfield' name='attachments[$post->ID][url]' value='" . esc_attr($url) . "' /><br />
 790	<button type='button' class='button urlnone' data-link-url=''>" . __('None') . "</button>
 791	<button type='button' class='button urlfile' data-link-url='" . esc_attr($file) . "'>" . __('File URL') . "</button>
 792	<button type='button' class='button urlpost' data-link-url='" . esc_attr($link) . "'>" . __('Attachment Post URL') . "</button>
 793";
 794}
 795
 796function wp_caption_input_textarea($edit_post) {
 797	// post data is already escaped
 798	$name = "attachments[{$edit_post->ID}][post_excerpt]";
 799
 800	return '<textarea name="' . $name . '" id="' . $name . '">' . $edit_post->post_excerpt . '</textarea>';
 801}
 802
 803/**
 804 * {@internal Missing Short Description}}
 805 *
 806 * @since 2.5.0
 807 *
 808 * @param array $form_fields
 809 * @param object $post
 810 * @return array
 811 */
 812function image_attachment_fields_to_edit($form_fields, $post) {
 813	return $form_fields;
 814}
 815
 816/**
 817 * {@internal Missing Short Description}}
 818 *
 819 * @since 2.5.0
 820 *
 821 * @param array $form_fields
 822 * @param object $post {@internal $post not used}}
 823 * @return array
 824 */
 825function media_single_attachment_fields_to_edit( $form_fields, $post ) {
 826	unset($form_fields['url'], $form_fields['align'], $form_fields['image-size']);
 827	return $form_fields;
 828}
 829
 830/**
 831 * {@internal Missing Short Description}}
 832 *
 833 * @since 2.8.0
 834 *
 835 * @param array $form_fields
 836 * @param object $post {@internal $post not used}}
 837 * @return array
 838 */
 839function media_post_single_attachment_fields_to_edit( $form_fields, $post ) {
 840	unset($form_fields['image_url']);
 841	return $form_fields;
 842}
 843
 844/**
 845 * Filters input from media_upload_form_handler() and assigns a default
 846 * post_title from the file name if none supplied.
 847 *
 848 * Illustrates the use of the attachment_fields_to_save filter
 849 * which can be used to add default values to any field before saving to DB.
 850 *
 851 * @since 2.5.0
 852 *
 853 * @param object $post
 854 * @param array $attachment {@internal $attachment not used}}
 855 * @return array
 856 */
 857function image_attachment_fields_to_save($post, $attachment) {
 858	if ( substr($post['post_mime_type'], 0, 5) == 'image' ) {
 859		if ( strlen(trim($post['post_title'])) == 0 ) {
 860			$post['post_title'] = preg_replace('/\.\w+$/', '', basename($post['guid']));
 861			$post['errors']['post_title']['errors'][] = __('Empty Title filled from filename.');
 862		}
 863	}
 864
 865	return $post;
 866}
 867
 868add_filter('attachment_fields_to_save', 'image_attachment_fields_to_save', 10, 2);
 869
 870/**
 871 * {@internal Missing Short Description}}
 872 *
 873 * @since 2.5.0
 874 *
 875 * @param string $html
 876 * @param integer $attachment_id
 877 * @param array $attachment
 878 * @return array
 879 */
 880function image_media_send_to_editor($html, $attachment_id, $attachment) {
 881	$post = get_post($attachment_id);
 882	if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
 883		$url = $attachment['url'];
 884		$align = !empty($attachment['align']) ? $attachment['align'] : 'none';
 885		$size = !empty($attachment['image-size']) ? $attachment['image-size'] : 'medium';
 886		$alt = !empty($attachment['image_alt']) ? $attachment['image_alt'] : '';
 887		$rel = ( $url == get_attachment_link($attachment_id) );
 888
 889		return get_image_send_to_editor($attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel, $size, $alt);
 890	}
 891
 892	return $html;
 893}
 894
 895add_filter('media_send_to_editor', 'image_media_send_to_editor', 10, 3);
 896
 897/**
 898 * {@internal Missing Short Description}}
 899 *
 900 * @since 2.5.0
 901 *
 902 * @param object $post
 903 * @param array $errors
 904 * @return array
 905 */
 906function get_attachment_fields_to_edit($post, $errors = null) {
 907	if ( is_int($post) )
 908		$post = get_post($post);
 909	if ( is_array($post) )
 910		$post = new WP_Post( (object) $post );
 911
 912	$image_url = wp_get_attachment_url($post->ID);
 913
 914	$edit_post = sanitize_post($post, 'edit');
 915
 916	$form_fields = array(
 917		'post_title'   => array(
 918			'label'      => __('Title'),
 919			'value'      => $edit_post->post_title
 920		),
 921		'image_alt'   => array(),
 922		'post_excerpt' => array(
 923			'label'      => __('Caption'),
 924			'input'      => 'html',
 925			'html'       => wp_caption_input_textarea($edit_post)
 926		),
 927		'post_content' => array(
 928			'label'      => __('Description'),
 929			'value'      => $edit_post->post_content,
 930			'input'      => 'textarea'
 931		),
 932		'url'          => array(
 933			'label'      => __('Link URL'),
 934			'input'      => 'html',
 935			'html'       => image_link_input_fields($post, get_option('image_default_link_type')),
 936			'helps'      => __('Enter a link URL or click above for presets.')
 937		),
 938		'menu_order'   => array(
 939			'label'      => __('Order'),
 940			'value'      => $edit_post->menu_order
 941		),
 942		'image_url'	=> array(
 943			'label'      => __('File URL'),
 944			'input'      => 'html',
 945			'html'       => "<input type='text' class='text urlfield' readonly='readonly' name='attachments[$post->ID][url]' value='" . esc_attr($image_url) . "' /><br />",
 946			'value'      => wp_get_attachment_url($post->ID),
 947			'helps'      => __('Location of the uploaded file.')
 948		)
 949	);
 950
 951	foreach ( get_attachment_taxonomies($post) as $taxonomy ) {
 952		$t = (array) get_taxonomy($taxonomy);
 953		if ( ! $t['public'] || ! $t['show_ui'] )
 954			continue;
 955		if ( empty($t['label']) )
 956			$t['label'] = $taxonomy;
 957		if ( empty($t['args']) )
 958			$t['args'] = array();
 959
 960		$terms = get_object_term_cache($post->ID, $taxonomy);
 961		if ( false === $terms )
 962			$terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
 963
 964		$values = array();
 965
 966		foreach ( $terms as $term )
 967			$values[] = $term->slug;
 968		$t['value'] = join(', ', $values);
 969
 970		$form_fields[$taxonomy] = $t;
 971	}
 972
 973	// Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default
 974	// The recursive merge is easily traversed with array casting: foreach( (array) $things as $thing )
 975	$form_fields = array_merge_recursive($form_fields, (array) $errors);
 976
 977	// This was formerly in image_attachment_fields_to_edit().
 978	if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
 979		$alt = get_post_meta($post->ID, '_wp_attachment_image_alt', true);
 980		if ( empty($alt) )
 981			$alt = '';
 982
 983		$form_fields['post_title']['required'] = true;
 984
 985		$form_fields['image_alt'] = array(
 986			'value' => $alt,
 987			'label' => __('Alternative Text'),
 988			'helps' => __('Alt text for the image, e.g. &#8220;The Mona Lisa&#8221;')
 989		);
 990
 991		$form_fields['align'] = array(
 992			'label' => __('Alignment'),
 993			'input' => 'html',
 994			'html'  => image_align_input_fields($post, get_option('image_default_align')),
 995		);
 996
 997		$form_fields['image-size'] = image_size_input_fields( $post, get_option('image_default_size', 'medium') );
 998
 999	} else {
1000		unset( $form_fields['image_alt'] );
1001	}
1002
1003	$form_fields = apply_filters('attachment_fields_to_edit', $form_fields, $post);
1004
1005	return $form_fields;
1006}
1007
1008/**
1009 * Retrieve HTML for media items of post gallery.
1010 *
1011 * The HTML markup retrieved will be created for the progress of SWF Upload
1012 * component. Will also create link for showing and hiding the form to modify
1013 * the image attachment.
1014 *
1015 * @since 2.5.0
1016 *
1017 * @param int $post_id Optional. Post ID.
1018 * @param array $errors Errors for attachment, if any.
1019 * @return string
1020 */
1021function get_media_items( $post_id, $errors ) {
1022	$attachments = array();
1023	if ( $post_id ) {
1024		$post = get_post($post_id);
1025		if ( $post && $post->post_type == 'attachment' )
1026			$attachments = array($post->ID => $post);
1027		else
1028			$attachments = get_children( array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'orderby' => 'menu_order ASC, ID', 'order' => 'DESC') );
1029	} else {
1030		if ( is_array($GLOBALS['wp_the_query']->posts) )
1031			foreach ( $GLOBALS['wp_the_query']->posts as $attachment )
1032				$attachments[$attachment->ID] = $attachment;
1033	}
1034
1035	$output = '';
1036	foreach ( (array) $attachments as $id => $attachment ) {
1037		if ( $attachment->post_status == 'trash' )
1038			continue;
1039		if ( $item = get_media_item( $id, array( 'errors' => isset($errors[$id]) ? $errors[$id] : null) ) )
1040			$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>";
1041	}
1042
1043	return $output;
1044}
1045
1046/**
1047 * Retrieve HTML form for modifying the image attachment.
1048 *
1049 * @since 2.5.0
1050 *
1051 * @param int $attachment_id Attachment ID for modification.
1052 * @param string|array $args Optional. Override defaults.
1053 * @return string HTML form for attachment.
1054 */
1055function get_media_item( $attachment_id, $args = null ) {
1056	global $redir_tab;
1057
1058	if ( ( $attachment_id = intval( $attachment_id ) ) && $thumb_url = wp_get_attachment_image_src( $attachment_id, 'thumbnail', true ) )
1059		$thumb_url = $thumb_url[0];
1060	else
1061		$thumb_url = false;
1062
1063	$post = get_post( $attachment_id );
1064	$current_post_id = !empty( $_GET['post_id'] ) ? (int) $_GET['post_id'] : 0;
1065
1066	$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 );
1067	$args = wp_parse_args( $args, $default_args );
1068	$args = apply_filters( 'get_media_item_args', $args );
1069	extract( $args, EXTR_SKIP );
1070
1071	$toggle_on  = __( 'Show' );
1072	$toggle_off = __( 'Hide' );
1073
1074	$filename = esc_html( wp_basename( $post->guid ) );
1075	$title = esc_attr( $post->post_title );
1076
1077	if ( $_tags = get_the_tags( $attachment_id ) ) {
1078		foreach ( $_tags as $tag )
1079			$tags[] = $tag->name;
1080		$tags = esc_attr( join( ', ', $tags ) );
1081	}
1082
1083	$post_mime_types = get_post_mime_types();
1084	$keys = array_keys( wp_match_mime_types( array_keys( $post_mime_types ), $post->post_mime_type ) );
1085	$type = array_shift( $keys );
1086	$type_html = "<input type='hidden' id='type-of-$attachment_id' value='" . esc_attr( $type ) . "' />";
1087
1088	$form_fields = get_attachment_fields_to_edit( $post, $errors );
1089
1090	if ( $toggle ) {
1091		$class = empty( $errors ) ? 'startclosed' : 'startopen';
1092		$toggle_links = "
1093	<a class='toggle describe-toggle-on' href='#'>$toggle_on</a>
1094	<a class='toggle describe-toggle-off' href='#'>$toggle_off</a>";
1095	} else {
1096		$class = '';
1097		$toggle_links = '';
1098	}
1099
1100	$display_title = ( !empty( $title ) ) ? $title : $filename; // $title shouldn't ever be empty, but just in case
1101	$display_title = $show_title ? "<div class='filename new'><span class='title'>" . wp_html_excerpt( $display_title, 60 ) . "</span></div>" : '';
1102
1103	$gallery = ( ( isset( $_REQUEST['tab'] ) && 'gallery' == $_REQUEST['tab'] ) || ( isset( $redir_tab ) && 'gallery' == $redir_tab ) );
1104	$order = '';
1105
1106	foreach ( $form_fields as $key => $val ) {
1107		if ( 'menu_order' == $key ) {
1108			if ( $gallery )
1109				$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>";
1110			else
1111				$order = "<input type='hidden' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ) . "' />";
1112
1113			unset( $form_fields['menu_order'] );
1114			break;
1115		}
1116	}
1117
1118	$media_dims = '';
1119	$meta = wp_get_attachment_metadata( $post->ID );
1120	if ( is_array( $meta ) && array_key_exists( 'width', $meta ) && array_key_exists( 'height', $meta ) )
1121		$media_dims .= "<span id='media-dims-$post->ID'>{$meta['width']}&nbsp;&times;&nbsp;{$meta['height']}</span> ";
1122	$media_dims = apply_filters( 'media_meta', $media_dims, $post );
1123
1124	$image_edit_button = '';
1125	if ( wp_image_editor_supports( array( 'mime_type' => $post->post_mime_type ) ) ) {
1126		$nonce = wp_create_nonce( "image_editor-$post->ID" );
1127		$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>";
1128	}
1129
1130	$attachment_url = get_permalink( $attachment_id );
1131
1132	$item = "
1133	$type_html
1134	$toggle_links
1135	$order
1136	$display_title
1137	<table class='slidetoggle describe $class'>
1138		<thead class='media-item-info' id='media-head-$post->ID'>
1139		<tr valign='top'>
1140			<td class='A1B1' id='thumbnail-head-$post->ID'>
1141			<p><a href='$attachment_url' target='_blank'><img class='thumbnail' src='$thumb_url' alt='' /></a></p>
1142			<p>$image_edit_button</p>
1143			</td>
1144			<td>
1145			<p><strong>" . __('File name:') . "</strong> $filename</p>
1146			<p><strong>" . __('File type:') . "</strong> $post->post_mime_type</p>
1147			<p><strong>" . __('Upload date:') . "</strong> " . mysql2date( get_option('date_format'), $post->post_date ). '</p>';
1148			if ( !empty( $media_dims ) )
1149				$item .= "<p><strong>" . __('Dimensions:') . "</strong> $media_dims</p>\n";
1150
1151			$item .= "</td></tr>\n";
1152
1153	$item .= "
1154		</thead>
1155		<tbody>
1156		<tr><td colspan='2' class='imgedit-response' id='imgedit-response-$post->ID'></td></tr>
1157		<tr><td style='display:none' colspan='2' class='image-editor' id='image-editor-$post->ID'></td></tr>\n";
1158
1159	$defaults = array(
1160		'input'      => 'text',
1161		'required'   => false,
1162		'value'      => '',
1163		'extra_rows' => array(),
1164	);
1165
1166	if ( $send )
1167		$send = get_submit_button( __( 'Insert into Post' ), 'button', "send[$attachment_id]", false );
1168	if ( $delete && current_user_can( 'delete_post', $attachment_id ) ) {
1169		if ( !EMPTY_TRASH_DAYS ) {
1170			$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>';
1171		} elseif ( !MEDIA_TRASH ) {
1172			$delete = "<a href='#' class='del-link' onclick=\"document.getElementById('del_attachment_$attachment_id').style.display='block';return false;\">" . __( 'Delete' ) . "</a>
1173			 <div id='del_attachment_$attachment_id' class='del-attachment' style='display:none;'><p>" . sprintf( __( 'You are about to delete <strong>%s</strong>.' ), $filename ) . "</p>
1174			 <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>
1175			 <a href='#' class='button' onclick=\"this.parentNode.style.display='none';return false;\">" . __( 'Cancel' ) . "</a>
1176			 </div>";
1177		} else {
1178			$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>
1179			<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>";
1180		}
1181	} else {
1182		$delete = '';
1183	}
1184
1185	$thumbnail = '';
1186	$calling_post_id = 0;
1187	if ( isset( $_GET['post_id'] ) )
1188		$calling_post_id = absint( $_GET['post_id'] );
1189	elseif ( isset( $_POST ) && count( $_POST ) ) // Like for async-upload where $_GET['post_id'] isn't set
1190		$calling_post_id = $post->post_parent;
1191	if ( 'image' == $type && $calling_post_id && current_theme_supports( 'post-thumbnails', get_post_type( $calling_post_id ) )
1192		&& post_type_supports( get_post_type( $calling_post_id ), 'thumbnail' ) && get_post_thumbnail_id( $calling_post_id ) != $attachment_id ) {
1193		$ajax_nonce = wp_create_nonce( "set_post_thumbnail-$calling_post_id" );
1194		$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>";
1195	}
1196
1197	if ( ( $send || $thumbnail || $delete ) && !isset( $form_fields['buttons'] ) )
1198		$form_fields['buttons'] = array( 'tr' => "\t\t<tr class='submit'><td></td><td class='savesend'>$send $thumbnail $delete</td></tr>\n" );
1199
1200	$hidden_fields = array();
1201
1202	foreach ( $form_fields as $id => $field ) {
1203		if ( $id[0] == '_' )
1204			continue;
1205
1206		if ( !empty( $field['tr'] ) ) {
1207			$item .= $field['tr'];
1208			continue;
1209		}
1210
1211		$field = array_merge( $defaults, $field );
1212		$name = "attachments[$attachment_id][$id]";
1213
1214		if ( $field['input'] == 'hidden' ) {
1215			$hidden_fields[$name] = $field['value'];
1216			continue;
1217		}
1218
1219		$required      = $field['required'] ? '<span class="alignright"><abbr title="required" class="required">*</abbr></span>' : '';
1220		$aria_required = $field['required'] ? " aria-required='true' " : '';
1221		$class  = $id;
1222		$class .= $field['required'] ? ' form-required' : '';
1223
1224		$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'>";
1225		if ( !empty( $field[ $field['input'] ] ) )
1226			$item .= $field[ $field['input'] ];
1227		elseif ( $field['input'] == 'textarea' ) {
1228			if ( 'post_content' == $id && user_can_richedit() ) {
1229				// sanitize_post() skips the post_content when user_can_richedit
1230				$field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES );
1231			}
1232			// post_excerpt is already escaped by sanitize_post() in get_attachment_fields_to_edit()
1233			$item .= "<textarea id='$name' name='$name' $aria_required>" . $field['value'] . '</textarea>';
1234		} else {
1235			$item .= "<input type='text' class='text' id='$name' name='$name' value='" . esc_attr( $field['value'] ) . "' $aria_required />";
1236		}
1237		if ( !empty( $field['helps'] ) )
1238			$item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>';
1239		$item .= "</td>\n\t\t</tr>\n";
1240
1241		$extra_rows = array();
1242
1243		if ( !empty( $field['errors'] ) )
1244			foreach ( array_unique( (array) $field['errors'] ) as $error )
1245				$extra_rows['error'][] = $error;
1246
1247		if ( !empty( $field['extra_rows'] ) )
1248			foreach ( $field['extra_rows'] as $class => $rows )
1249				foreach ( (array) $rows as $html )
1250					$extra_rows[$class][] = $html;
1251
1252		foreach ( $extra_rows as $class => $rows )
1253			foreach ( $rows as $html )
1254				$item .= "\t\t<tr><td></td><td class='$class'>$html</td></tr>\n";
1255	}
1256
1257	if ( !empty( $form_fields['_final'] ) )
1258		$item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
1259	$item .= "\t</tbody>\n";
1260	$item .= "\t</table>\n";
1261
1262	foreach ( $hidden_fields as $name => $value )
1263		$item .= "\t<input type='hidden' name='$name' id='$name' value='" . esc_attr( $value ) . "' />\n";
1264
1265	if ( $post->post_parent < 1 && isset( $_REQUEST['post_id'] ) ) {
1266		$parent = (int) $_REQUEST['post_id'];
1267		$parent_name = "attachments[$attachment_id][post_parent]";
1268		$item .= "\t<input type='hidden' name='$parent_name' id='$parent_name' value='$parent' />\n";
1269	}
1270
1271	return $item;
1272}
1273
1274function get_compat_media_markup( $attachment_id, $args = null ) {
1275	$post = get_post( $attachment_id );
1276
1277	$default_args = array(
1278		'errors' => null,
1279		'taxonomies' => false,
1280	);
1281
1282	$args = wp_parse_args( $args, $default_args );
1283	$args = apply_filters( 'get_media_item_args', $args );
1284
1285	$form_fields = array();
1286
1287	if ( $args['taxonomies'] ) {
1288		foreach ( get_attachment_taxonomies($post) as $taxonomy ) {
1289			$t = (array) get_taxonomy($taxonomy);
1290			if ( ! $t['public'] || ! $t['show_ui'] )
1291				continue;
1292			if ( empty($t['label']) )
1293				$t['label'] = $taxonomy;
1294			if ( empty($t['args']) )
1295				$t['args'] = array();
1296
1297			$terms = get_object_term_cache($post->ID, $taxonomy);
1298			if ( false === $terms )
1299				$terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
1300
1301			$values = array();
1302
1303			foreach ( $terms as $term )
1304				$values[] = $term->slug;
1305			$t['value'] = join(', ', $values);
1306
1307			$form_fields[$taxonomy] = $t;
1308		}
1309	}
1310
1311	// Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default
1312	// The recursive merge is easily traversed with array casting: foreach( (array) $things as $thing )
1313	$form_fields = array_merge_recursive($form_fields, (array) $args['errors'] );
1314
1315	$form_fields = apply_filters( 'attachment_fields_to_edit', $form_fields, $post );
1316
1317	unset( $form_fields['image-size'], $form_fields['align'], $form_fields['image_alt'],
1318		$form_fields['post_title'], $form_fields['post_excerpt'], $form_fields['post_content'],
1319		$form_fields['url'], $form_fields['menu_order'], $form_fields['image_url'] );
1320
1321	$media_meta = apply_filters( 'media_meta', '', $post );
1322
1323	$defaults = array(
1324		'input'      => 'text',
1325		'required'   => false,
1326		'value'      => '',
1327		'extra_rows' => array(),
1328	);
1329
1330	$hidden_fields = array();
1331
1332	$item = '';
1333	foreach ( $form_fields as $id => $field ) {
1334		if ( $id[0] == '_' )
1335			continue;
1336
1337		$name = "attachments[$attachment_id][$id]";
1338		$id_attr = "attachments-$attachment_id-$id";
1339
1340		if ( !empty( $field['tr'] ) ) {
1341			$item .= $field['tr'];
1342			continue;
1343		}
1344
1345		$field = array_merge( $defaults, $field );
1346
1347		if ( $field['input'] == 'hidden' ) {
1348			$hidden_fields[$name] = $field['value'];
1349			continue;
1350		}
1351
1352		$required      = $field['required'] ? '<span class="alignright"><abbr title="required" class="required">*</abbr></span>' : '';
1353		$aria_required = $field['required'] ? " aria-required='true' " : '';
1354		$class  = 'compat-field-' . $id;
1355		$class .= $field['required'] ? ' form-required' : '';
1356
1357		$item .= "\t\t<tr class='$class'>";
1358		$item .= "\t\t\t<th valign='top' scope='row' class='label'><label for='$id_attr'><span class='alignleft'>{$field['label']}</span>$required<br class='clear' /></label>";
1359		$item .= "</th>\n\t\t\t<td class='field'>";
1360
1361		if ( !empty( $field[ $field['input'] ] ) )
1362			$item .= $field[ $field['input'] ];
1363		elseif ( $field['input'] == 'textarea' ) {
1364			if ( 'post_content' == $id && user_can_richedit() ) {
1365				// sanitize_post() skips the post_content when user_can_richedit
1366				$field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES );
1367			}
1368			$item .= "<textarea id='$id_attr' name='$name' $aria_required>" . $field['value'] . '</textarea>';
1369		} else {
1370			$item .= "<input type='text' class='text' id='$id_attr' name='$name' value='" . esc_attr( $field['value'] ) . "' $aria_required />";
1371		}
1372		if ( !empty( $field['helps'] ) )
1373			$item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>';
1374		$item .= "</td>\n\t\t</tr>\n";
1375
1376		$extra_rows = array();
1377
1378		if ( !empty( $field['errors'] ) )
1379			foreach ( array_unique( (array) $field['errors'] ) as $error )
1380				$extra_rows['error'][] = $error;
1381
1382		if ( !empty( $field['extra_rows'] ) )
1383			foreach ( $field['extra_rows'] as $class => $rows )
1384				foreach ( (array) $rows as $html )
1385					$extra_rows[$class][] = $html;
1386
1387		foreach ( $extra_rows as $class => $rows )
1388			foreach ( $rows as $html )
1389				$item .= "\t\t<tr><td></td><td class='$class'>$html</td></tr>\n";
1390	}
1391
1392	if ( !empty( $form_fields['_final'] ) )
1393		$item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
1394	if ( $item )
1395		$item = '<table class="compat-attachment-fields">' . $item . '</table>';
1396
1397	return array(
1398		'item'   => $item,
1399		'hidden' => $hidden_fields,
1400		'meta'   => $media_meta,
1401	);
1402}
1403
1404/**
1405 * {@internal Missing Short Description}}
1406 *
1407 * @since 2.5.0
1408 */
1409function media_upload_header() {
1410	$post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
1411	echo '<script type="text/javascript">post_id = ' . $post_id . ";</script>\n";
1412	if ( empty( $_GET['chromeless'] ) ) {
1413		echo '<div id="media-upload-header">';
1414		the_media_upload_tabs();
1415		echo '</div>';
1416	}
1417}
1418
1419/**
1420 * {@internal Missing Short Description}}
1421 *
1422 * @since 2.5.0
1423 *
1424 * @param unknown_type $errors
1425 */
1426function media_upload_form( $errors = null ) {
1427	global $type, $tab, $pagenow, $is_IE, $is_opera;
1428
1429	if ( ! _device_can_upload() ) {
1430		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>';
1431		return;
1432	}
1433
1434	$upload_action_url = admin_url('async-upload.php');
1435	$post_id = isset($_REQUEST['post_id']) ? intval($_REQUEST['post_id']) : 0;
1436	$_type = isset($type) ? $type : '';
1437	$_tab = isset($tab) ? $tab : '';
1438
1439	$upload_size_unit = $max_upload_size = wp_max_upload_size();
1440	$sizes = array( 'KB', 'MB', 'GB' );
1441
1442	for ( $u = -1; $upload_size_unit > 1024 && $u < count( $sizes ) - 1; $u++ ) {
1443		$upload_size_unit /= 1024;
1444	}
1445
1446	if ( $u < 0 ) {
1447		$upload_size_unit = 0;
1448		$u = 0;
1449	} else {
1450		$upload_size_unit = (int) $upload_size_unit;
1451	}
1452?>
1453
1454<div id="media-upload-notice"><?php
1455
1456	if (isset($errors['upload_notice']) )
1457		echo $errors['upload_notice'];
1458
1459?></div>
1460<div id="media-upload-error"><?php
1461
1462	if (isset($errors['upload_error']) && is_wp_error($errors['upload_error']))
1463		echo $errors['upload_error']->get_error_message();
1464
1465?></div>
1466<?php
1467if ( is_multisite() && !is_upload_space_available() ) {
1468	do_action( 'upload_ui_over_quota' );
1469	return;
1470}
1471
1472do_action('pre-upload-ui');
1473
1474$post_params = array(
1475		"post_id" => $post_id,
1476		"_wpnonce" => wp_create_nonce('media-form'),
1477		"type" => $_type,
1478		"tab" => $_tab,
1479		"short" => "1",
1480);
1481
1482$post_params = apply_filters( 'upload_post_params', $post_params ); // hook change! old name: 'swfupload_post_params'
1483
1484$plupload_init = array(
1485	'runtimes' => 'html5,silverlight,flash,html4',
1486	'browse_button' => 'plupload-browse-button',
1487	'container' => 'plupload-upload-ui',
1488	'drop_element' => 'drag-drop-area',
1489	'file_data_name' => 'async-upload',
1490	'multiple_queues' => true,
1491	'max_file_size' => $max_upload_size . 'b',
1492	'url' => $upload_action_url,
1493	'flash_swf_url' => includes_url('js/plupload/plupload.flash.swf'),
1494	'silverlight_xap_url' => includes_url('js/plupload/plupload.silverlight.xap'),
1495	'filters' => array( array('title' => __( 'Allowed Files' ), 'extensions' => '*') ),
1496	'multipart' => true,
1497	'urlstream_upload' => true,
1498	'multipart_params' => $post_params
1499);
1500
1501$plupload_init = apply_filters( 'plupload_init', $plupload_init );
1502
1503?>
1504
1505<script type="text/javascript">
1506<?php
1507// Verify size is an int. If not return default value.
1508$large_size_h = absint( get_option('large_size_h') );
1509if( !$large_size_h )
1510	$large_size_h = 1024;
1511$large_size_w = absint( get_option('large_size_w') );
1512if( !$large_size_w )
1513	$large_size_w = 1024;
1514?>
1515var resize_height = <?php echo $large_size_h; ?>, resize_width = <?php echo $large_size_w; ?>,
1516wpUploaderInit = <?php echo json_encode($plupload_init); ?>;
1517</script>
1518
1519<div id="plupload-upload-ui" class="hide-if-no-js">
1520<?php do_action('pre-plupload-upload-ui'); // hook change, old name: 'pre-flash-upload-ui' ?>
1521<div id="drag-drop-area">
1522	<div class="drag-drop-inside">
1523	<p class="drag-drop-info"><?php _e('Drop files here'); ?></p>
1524	<p><?php _ex('or', 'Uploader: Drop files here - or - Select Files'); ?></p>
1525	<p class="drag-drop-buttons"><input id="pl…

Large files files are truncated, but you can click here to view the full file