PageRenderTime 905ms CodeModel.GetById 103ms app.highlight 498ms RepoModel.GetById 25ms app.codeStats 5ms

/wp-admin/includes/media.php

https://github.com/dedavidd/piratenpartij.nl
PHP | 2973 lines | 2906 code | 18 blank | 49 comment | 12 complexity | 7fb4c0ceac8992cb120a00347adf889d MD5 | raw file

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

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

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