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