wp/wp-admin/includes/media.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
     9 /**
     9 /**
    10  * Defines the default media upload tabs
    10  * Defines the default media upload tabs
    11  *
    11  *
    12  * @since 2.5.0
    12  * @since 2.5.0
    13  *
    13  *
    14  * @return array default tabs
    14  * @return string[] Default tabs.
    15  */
    15  */
    16 function media_upload_tabs() {
    16 function media_upload_tabs() {
    17 	$_default_tabs = array(
    17 	$_default_tabs = array(
    18 		'type'     => __( 'From Computer' ), // handler action suffix => tab text
    18 		'type'     => __( 'From Computer' ), // Handler action suffix => tab text.
    19 		'type_url' => __( 'From URL' ),
    19 		'type_url' => __( 'From URL' ),
    20 		'gallery'  => __( 'Gallery' ),
    20 		'gallery'  => __( 'Gallery' ),
    21 		'library'  => __( 'Media Library' ),
    21 		'library'  => __( 'Media Library' ),
    22 	);
    22 	);
    23 
    23 
    24 	/**
    24 	/**
    25 	 * Filters the available tabs in the legacy (pre-3.5.0) media popup.
    25 	 * Filters the available tabs in the legacy (pre-3.5.0) media popup.
    26 	 *
    26 	 *
    27 	 * @since 2.5.0
    27 	 * @since 2.5.0
    28 	 *
    28 	 *
    29 	 * @param array $_default_tabs An array of media tabs.
    29 	 * @param string[] $_default_tabs An array of media tabs.
    30 	 */
    30 	 */
    31 	return apply_filters( 'media_upload_tabs', $_default_tabs );
    31 	return apply_filters( 'media_upload_tabs', $_default_tabs );
    32 }
    32 }
    33 
    33 
    34 /**
    34 /**
    58 	if ( empty( $attachments ) ) {
    58 	if ( empty( $attachments ) ) {
    59 		unset( $tabs['gallery'] );
    59 		unset( $tabs['gallery'] );
    60 		return $tabs;
    60 		return $tabs;
    61 	}
    61 	}
    62 
    62 
       
    63 	/* translators: %s: Number of attachments. */
    63 	$tabs['gallery'] = sprintf( __( 'Gallery (%s)' ), "<span id='attachments-count'>$attachments</span>" );
    64 	$tabs['gallery'] = sprintf( __( 'Gallery (%s)' ), "<span id='attachments-count'>$attachments</span>" );
    64 
    65 
    65 	return $tabs;
    66 	return $tabs;
    66 }
    67 }
    67 
    68 
    77 	$tabs    = media_upload_tabs();
    78 	$tabs    = media_upload_tabs();
    78 	$default = 'type';
    79 	$default = 'type';
    79 
    80 
    80 	if ( ! empty( $tabs ) ) {
    81 	if ( ! empty( $tabs ) ) {
    81 		echo "<ul id='sidemenu'>\n";
    82 		echo "<ul id='sidemenu'>\n";
       
    83 
    82 		if ( isset( $redir_tab ) && array_key_exists( $redir_tab, $tabs ) ) {
    84 		if ( isset( $redir_tab ) && array_key_exists( $redir_tab, $tabs ) ) {
    83 			$current = $redir_tab;
    85 			$current = $redir_tab;
    84 		} elseif ( isset( $_GET['tab'] ) && array_key_exists( $_GET['tab'], $tabs ) ) {
    86 		} elseif ( isset( $_GET['tab'] ) && array_key_exists( $_GET['tab'], $tabs ) ) {
    85 			$current = $_GET['tab'];
    87 			$current = $_GET['tab'];
    86 		} else {
    88 		} else {
   105 				)
   107 				)
   106 			);
   108 			);
   107 			$link = "<a href='" . esc_url( $href ) . "'$class>$text</a>";
   109 			$link = "<a href='" . esc_url( $href ) . "'$class>$text</a>";
   108 			echo "\t<li id='" . esc_attr( "tab-$callback" ) . "'>$link</li>\n";
   110 			echo "\t<li id='" . esc_attr( "tab-$callback" ) . "'>$link</li>\n";
   109 		}
   111 		}
       
   112 
   110 		echo "</ul>\n";
   113 		echo "</ul>\n";
   111 	}
   114 	}
   112 }
   115 }
   113 
   116 
   114 /**
   117 /**
   115  * Retrieves the image HTML to send to the editor.
   118  * Retrieves the image HTML to send to the editor.
   116  *
   119  *
   117  * @since 2.5.0
   120  * @since 2.5.0
   118  *
   121  *
   119  * @param int          $id      Image attachment id.
   122  * @param int          $id      Image attachment ID.
   120  * @param string       $caption Image caption.
   123  * @param string       $caption Image caption.
   121  * @param string       $title   Image title attribute.
   124  * @param string       $title   Image title attribute.
   122  * @param string       $align   Image CSS alignment property.
   125  * @param string       $align   Image CSS alignment property.
   123  * @param string       $url     Optional. Image src URL. Default empty.
   126  * @param string       $url     Optional. Image src URL. Default empty.
   124  * @param bool|string  $rel     Optional. Value for rel attribute or whether to add a default value. Default false.
   127  * @param bool|string  $rel     Optional. Value for rel attribute or whether to add a default value. Default false.
   149 	 * Filters the image HTML markup to send to the editor when inserting an image.
   152 	 * Filters the image HTML markup to send to the editor when inserting an image.
   150 	 *
   153 	 *
   151 	 * @since 2.5.0
   154 	 * @since 2.5.0
   152 	 *
   155 	 *
   153 	 * @param string       $html    The image HTML markup to send.
   156 	 * @param string       $html    The image HTML markup to send.
   154 	 * @param int          $id      The attachment id.
   157 	 * @param int          $id      The attachment ID.
   155 	 * @param string       $caption The image caption.
   158 	 * @param string       $caption The image caption.
   156 	 * @param string       $title   The image title.
   159 	 * @param string       $title   The image title.
   157 	 * @param string       $align   The image alignment.
   160 	 * @param string       $align   The image alignment.
   158 	 * @param string       $url     The image source URL.
   161 	 * @param string       $url     The image source URL.
   159 	 * @param string|array $size    Size of image. Image size or array of width and height values
   162 	 * @param string|array $size    Size of image. Image size or array of width and height values
   164 
   167 
   165 	return $html;
   168 	return $html;
   166 }
   169 }
   167 
   170 
   168 /**
   171 /**
   169  * Adds image shortcode with caption to editor
   172  * Adds image shortcode with caption to editor.
   170  *
   173  *
   171  * @since 2.6.0
   174  * @since 2.6.0
   172  *
   175  *
   173  * @param string  $html    The image HTML markup to send.
   176  * @param string  $html    The image HTML markup to send.
   174  * @param integer $id      Image attachment ID.
   177  * @param integer $id      Image attachment ID.
   203 	 *
   206 	 *
   204 	 * Prevents image captions from being appended to image HTML when inserted into the editor.
   207 	 * Prevents image captions from being appended to image HTML when inserted into the editor.
   205 	 *
   208 	 *
   206 	 * @since 2.6.0
   209 	 * @since 2.6.0
   207 	 *
   210 	 *
   208 	 * @param bool $bool Whether to disable appending captions. Returning true to the filter
   211 	 * @param bool $bool Whether to disable appending captions. Returning true from the filter
   209 	 *                   will disable captions. Default empty string.
   212 	 *                   will disable captions. Default empty string.
   210 	 */
   213 	 */
   211 	if ( empty( $caption ) || apply_filters( 'disable_captions', '' ) ) {
   214 	if ( empty( $caption ) || apply_filters( 'disable_captions', '' ) ) {
   212 		return $html;
   215 		return $html;
   213 	}
   216 	}
   221 	$width = $matches[1];
   224 	$width = $matches[1];
   222 
   225 
   223 	$caption = str_replace( array( "\r\n", "\r" ), "\n", $caption );
   226 	$caption = str_replace( array( "\r\n", "\r" ), "\n", $caption );
   224 	$caption = preg_replace_callback( '/<[a-zA-Z0-9]+(?: [^<>]+>)*/', '_cleanup_image_add_caption', $caption );
   227 	$caption = preg_replace_callback( '/<[a-zA-Z0-9]+(?: [^<>]+>)*/', '_cleanup_image_add_caption', $caption );
   225 
   228 
   226 	// Convert any remaining line breaks to <br>.
   229 	// Convert any remaining line breaks to <br />.
   227 	$caption = preg_replace( '/[ \n\t]*\n[ \t]*/', '<br />', $caption );
   230 	$caption = preg_replace( '/[ \n\t]*\n[ \t]*/', '<br />', $caption );
   228 
   231 
   229 	$html = preg_replace( '/(class=["\'][^\'"]*)align(none|left|right|center)\s?/', '$1', $html );
   232 	$html = preg_replace( '/(class=["\'][^\'"]*)align(none|left|right|center)\s?/', '$1', $html );
   230 	if ( empty( $align ) ) {
   233 	if ( empty( $align ) ) {
   231 		$align = 'none';
   234 		$align = 'none';
   243 	 */
   246 	 */
   244 	return apply_filters( 'image_add_caption_shortcode', $shcode, $html );
   247 	return apply_filters( 'image_add_caption_shortcode', $shcode, $html );
   245 }
   248 }
   246 
   249 
   247 /**
   250 /**
   248  * Private preg_replace callback used in image_add_caption()
   251  * Private preg_replace callback used in image_add_caption().
   249  *
   252  *
   250  * @access private
   253  * @access private
   251  * @since 3.4.0
   254  * @since 3.4.0
   252  */
   255  */
   253 function _cleanup_image_add_caption( $matches ) {
   256 function _cleanup_image_add_caption( $matches ) {
   254 	// Remove any line breaks from inside the tags.
   257 	// Remove any line breaks from inside the tags.
   255 	return preg_replace( '/[\r\n\t]+/', ' ', $matches[0] );
   258 	return preg_replace( '/[\r\n\t]+/', ' ', $matches[0] );
   256 }
   259 }
   257 
   260 
   258 /**
   261 /**
   259  * Adds image html to editor
   262  * Adds image HTML to editor.
   260  *
   263  *
   261  * @since 2.5.0
   264  * @since 2.5.0
   262  *
   265  *
   263  * @param string $html
   266  * @param string $html
   264  */
   267  */
   265 function media_send_to_editor( $html ) {
   268 function media_send_to_editor( $html ) {
   266 	?>
   269 	?>
   267 <script type="text/javascript">
   270 	<script type="text/javascript">
   268 var win = window.dialogArguments || opener || parent || top;
   271 	var win = window.dialogArguments || opener || parent || top;
   269 win.send_to_editor( <?php echo wp_json_encode( $html ); ?> );
   272 	win.send_to_editor( <?php echo wp_json_encode( $html ); ?> );
   270 </script>
   273 	</script>
   271 	<?php
   274 	<?php
   272 	exit;
   275 	exit;
   273 }
   276 }
   274 
   277 
   275 /**
   278 /**
   276  * Save a file submitted from a POST request and create an attachment post for it.
   279  * Saves a file submitted from a POST request and create an attachment post for it.
   277  *
   280  *
   278  * @since 2.5.0
   281  * @since 2.5.0
   279  *
   282  *
   280  * @param string $file_id   Index of the `$_FILES` array that the file was sent. Required.
   283  * @param string $file_id   Index of the `$_FILES` array that the file was sent. Required.
   281  * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
   284  * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
   282  *                          be set to 0, creating a media item that has no relationship to a post.
   285  *                          be set to 0, creating a media item that has no relationship to a post.
   283  * @param array  $post_data Overwrite some of the attachment. Optional.
   286  * @param array  $post_data Optional. Overwrite some of the attachment.
   284  * @param array  $overrides Override the wp_handle_upload() behavior. Optional.
   287  * @param array  $overrides Optional. Override the wp_handle_upload() behavior.
   285  * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
   288  * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
   286  */
   289  */
   287 function media_handle_upload( $file_id, $post_id, $post_data = array(), $overrides = array( 'test_form' => false ) ) {
   290 function media_handle_upload( $file_id, $post_id, $post_data = array(), $overrides = array( 'test_form' => false ) ) {
   288 
       
   289 	$time = current_time( 'mysql' );
   291 	$time = current_time( 'mysql' );
   290 	if ( $post = get_post( $post_id ) ) {
   292 	$post = get_post( $post_id );
       
   293 
       
   294 	if ( $post ) {
   291 		// The post date doesn't usually matter for pages, so don't backdate this upload.
   295 		// The post date doesn't usually matter for pages, so don't backdate this upload.
   292 		if ( 'page' !== $post->post_type && substr( $post->post_date, 0, 4 ) > 0 ) {
   296 		if ( 'page' !== $post->post_type && substr( $post->post_date, 0, 4 ) > 0 ) {
   293 			$time = $post->post_date;
   297 			$time = $post->post_date;
   294 		}
   298 		}
   295 	}
   299 	}
   319 		}
   323 		}
   320 
   324 
   321 		if ( ! empty( $title ) ) {
   325 		if ( ! empty( $title ) ) {
   322 
   326 
   323 			if ( ! empty( $meta['album'] ) && ! empty( $meta['artist'] ) ) {
   327 			if ( ! empty( $meta['album'] ) && ! empty( $meta['artist'] ) ) {
   324 				/* translators: 1: audio track title, 2: album title, 3: artist name */
   328 				/* translators: 1: Audio track title, 2: Album title, 3: Artist name. */
   325 				$content .= sprintf( __( '"%1$s" from %2$s by %3$s.' ), $title, $meta['album'], $meta['artist'] );
   329 				$content .= sprintf( __( '"%1$s" from %2$s by %3$s.' ), $title, $meta['album'], $meta['artist'] );
   326 			} elseif ( ! empty( $meta['album'] ) ) {
   330 			} elseif ( ! empty( $meta['album'] ) ) {
   327 				/* translators: 1: audio track title, 2: album title */
   331 				/* translators: 1: Audio track title, 2: Album title. */
   328 				$content .= sprintf( __( '"%1$s" from %2$s.' ), $title, $meta['album'] );
   332 				$content .= sprintf( __( '"%1$s" from %2$s.' ), $title, $meta['album'] );
   329 			} elseif ( ! empty( $meta['artist'] ) ) {
   333 			} elseif ( ! empty( $meta['artist'] ) ) {
   330 				/* translators: 1: audio track title, 2: artist name */
   334 				/* translators: 1: Audio track title, 2: Artist name. */
   331 				$content .= sprintf( __( '"%1$s" by %2$s.' ), $title, $meta['artist'] );
   335 				$content .= sprintf( __( '"%1$s" by %2$s.' ), $title, $meta['artist'] );
   332 			} else {
   336 			} else {
   333 				/* translators: 1: audio track title */
   337 				/* translators: %s: Audio track title. */
   334 				$content .= sprintf( __( '"%s".' ), $title );
   338 				$content .= sprintf( __( '"%s".' ), $title );
   335 			}
   339 			}
   336 		} elseif ( ! empty( $meta['album'] ) ) {
   340 		} elseif ( ! empty( $meta['album'] ) ) {
   337 
   341 
   338 			if ( ! empty( $meta['artist'] ) ) {
   342 			if ( ! empty( $meta['artist'] ) ) {
   339 				/* translators: 1: audio album title, 2: artist name */
   343 				/* translators: 1: Audio album title, 2: Artist name. */
   340 				$content .= sprintf( __( '%1$s by %2$s.' ), $meta['album'], $meta['artist'] );
   344 				$content .= sprintf( __( '%1$s by %2$s.' ), $meta['album'], $meta['artist'] );
   341 			} else {
   345 			} else {
   342 				$content .= $meta['album'] . '.';
   346 				$content .= $meta['album'] . '.';
   343 			}
   347 			}
   344 		} elseif ( ! empty( $meta['artist'] ) ) {
   348 		} elseif ( ! empty( $meta['artist'] ) ) {
   346 			$content .= $meta['artist'] . '.';
   350 			$content .= $meta['artist'] . '.';
   347 
   351 
   348 		}
   352 		}
   349 
   353 
   350 		if ( ! empty( $meta['year'] ) ) {
   354 		if ( ! empty( $meta['year'] ) ) {
   351 			/* translators: Audio file track information. %d: Year of audio track release */
   355 			/* translators: Audio file track information. %d: Year of audio track release. */
   352 			$content .= ' ' . sprintf( __( 'Released: %d.' ), $meta['year'] );
   356 			$content .= ' ' . sprintf( __( 'Released: %d.' ), $meta['year'] );
   353 		}
   357 		}
   354 
   358 
   355 		if ( ! empty( $meta['track_number'] ) ) {
   359 		if ( ! empty( $meta['track_number'] ) ) {
   356 			$track_number = explode( '/', $meta['track_number'] );
   360 			$track_number = explode( '/', $meta['track_number'] );
       
   361 
   357 			if ( isset( $track_number[1] ) ) {
   362 			if ( isset( $track_number[1] ) ) {
   358 				/* translators: Audio file track information. 1: Audio track number, 2: Total audio tracks */
   363 				/* translators: Audio file track information. 1: Audio track number, 2: Total audio tracks. */
   359 				$content .= ' ' . sprintf( __( 'Track %1$s of %2$s.' ), number_format_i18n( $track_number[0] ), number_format_i18n( $track_number[1] ) );
   364 				$content .= ' ' . sprintf( __( 'Track %1$s of %2$s.' ), number_format_i18n( $track_number[0] ), number_format_i18n( $track_number[1] ) );
   360 			} else {
   365 			} else {
   361 				/* translators: Audio file track information. %s: Audio track number */
   366 				/* translators: Audio file track information. %s: Audio track number. */
   362 				$content .= ' ' . sprintf( __( 'Track %s.' ), number_format_i18n( $track_number[0] ) );
   367 				$content .= ' ' . sprintf( __( 'Track %s.' ), number_format_i18n( $track_number[0] ) );
   363 			}
   368 			}
   364 		}
   369 		}
   365 
   370 
   366 		if ( ! empty( $meta['genre'] ) ) {
   371 		if ( ! empty( $meta['genre'] ) ) {
   367 			/* translators: Audio file genre information. %s: Audio genre name */
   372 			/* translators: Audio file genre information. %s: Audio genre name. */
   368 			$content .= ' ' . sprintf( __( 'Genre: %s.' ), $meta['genre'] );
   373 			$content .= ' ' . sprintf( __( 'Genre: %s.' ), $meta['genre'] );
   369 		}
   374 		}
   370 
   375 
   371 		// Use image exif/iptc data for title and caption defaults if possible.
   376 		// Use image exif/iptc data for title and caption defaults if possible.
   372 	} elseif ( 0 === strpos( $type, 'image/' ) && $image_meta = wp_read_image_metadata( $file ) ) {
   377 	} elseif ( 0 === strpos( $type, 'image/' ) ) {
   373 		if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) {
   378 		$image_meta = wp_read_image_metadata( $file );
   374 			$title = $image_meta['title'];
   379 
   375 		}
   380 		if ( $image_meta ) {
   376 
   381 			if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) {
   377 		if ( trim( $image_meta['caption'] ) ) {
   382 				$title = $image_meta['title'];
   378 			$excerpt = $image_meta['caption'];
   383 			}
   379 		}
   384 
   380 	}
   385 			if ( trim( $image_meta['caption'] ) ) {
   381 
   386 				$excerpt = $image_meta['caption'];
   382 	// Construct the attachment array
   387 			}
       
   388 		}
       
   389 	}
       
   390 
       
   391 	// Construct the attachment array.
   383 	$attachment = array_merge(
   392 	$attachment = array_merge(
   384 		array(
   393 		array(
   385 			'post_mime_type' => $type,
   394 			'post_mime_type' => $type,
   386 			'guid'           => $url,
   395 			'guid'           => $url,
   387 			'post_parent'    => $post_id,
   396 			'post_parent'    => $post_id,
   393 	);
   402 	);
   394 
   403 
   395 	// This should never be set as it would then overwrite an existing attachment.
   404 	// This should never be set as it would then overwrite an existing attachment.
   396 	unset( $attachment['ID'] );
   405 	unset( $attachment['ID'] );
   397 
   406 
   398 	// Save the data
   407 	// Save the data.
   399 	$id = wp_insert_attachment( $attachment, $file, $post_id, true );
   408 	$attachment_id = wp_insert_attachment( $attachment, $file, $post_id, true );
   400 	if ( ! is_wp_error( $id ) ) {
   409 
   401 		wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
   410 	if ( ! is_wp_error( $attachment_id ) ) {
   402 	}
   411 		// Set a custom header with the attachment_id.
   403 
   412 		// Used by the browser/client to resume creating image sub-sizes after a PHP fatal error.
   404 	return $id;
   413 		if ( ! headers_sent() ) {
   405 
   414 			header( 'X-WP-Upload-Attachment-ID: ' . $attachment_id );
       
   415 		}
       
   416 
       
   417 		// The image sub-sizes are created during wp_generate_attachment_metadata().
       
   418 		// This is generally slow and may cause timeouts or out of memory errors.
       
   419 		wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
       
   420 	}
       
   421 
       
   422 	return $attachment_id;
   406 }
   423 }
   407 
   424 
   408 /**
   425 /**
   409  * Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload().
   426  * Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload().
   410  *
   427  *
   411  * @since 2.6.0
   428  * @since 2.6.0
       
   429  * @since 5.3.0 The `$post_id` parameter was made optional.
   412  *
   430  *
   413  * @param array  $file_array Array similar to a `$_FILES` upload array.
   431  * @param array  $file_array Array similar to a `$_FILES` upload array.
   414  * @param int    $post_id    The post ID the media is associated with.
   432  * @param int    $post_id    Optional. The post ID the media is associated with.
   415  * @param string $desc       Optional. Description of the side-loaded file. Default null.
   433  * @param string $desc       Optional. Description of the side-loaded file. Default null.
   416  * @param array  $post_data  Optional. Post data to override. Default empty array.
   434  * @param array  $post_data  Optional. Post data to override. Default empty array.
   417  * @return int|object The ID of the attachment or a WP_Error on failure.
   435  * @return int|WP_Error The ID of the attachment or a WP_Error on failure.
   418  */
   436  */
   419 function media_handle_sideload( $file_array, $post_id, $desc = null, $post_data = array() ) {
   437 function media_handle_sideload( $file_array, $post_id = 0, $desc = null, $post_data = array() ) {
   420 	$overrides = array( 'test_form' => false );
   438 	$overrides = array( 'test_form' => false );
   421 
   439 
   422 	$time = current_time( 'mysql' );
   440 	$time = current_time( 'mysql' );
   423 	if ( $post = get_post( $post_id ) ) {
   441 	$post = get_post( $post_id );
       
   442 
       
   443 	if ( $post ) {
   424 		if ( substr( $post->post_date, 0, 4 ) > 0 ) {
   444 		if ( substr( $post->post_date, 0, 4 ) > 0 ) {
   425 			$time = $post->post_date;
   445 			$time = $post->post_date;
   426 		}
   446 		}
   427 	}
   447 	}
   428 
   448 
   429 	$file = wp_handle_sideload( $file_array, $overrides, $time );
   449 	$file = wp_handle_sideload( $file_array, $overrides, $time );
       
   450 
   430 	if ( isset( $file['error'] ) ) {
   451 	if ( isset( $file['error'] ) ) {
   431 		return new WP_Error( 'upload_error', $file['error'] );
   452 		return new WP_Error( 'upload_error', $file['error'] );
   432 	}
   453 	}
   433 
   454 
   434 	$url     = $file['url'];
   455 	$url     = $file['url'];
   436 	$file    = $file['file'];
   457 	$file    = $file['file'];
   437 	$title   = preg_replace( '/\.[^.]+$/', '', wp_basename( $file ) );
   458 	$title   = preg_replace( '/\.[^.]+$/', '', wp_basename( $file ) );
   438 	$content = '';
   459 	$content = '';
   439 
   460 
   440 	// Use image exif/iptc data for title and caption defaults if possible.
   461 	// Use image exif/iptc data for title and caption defaults if possible.
   441 	if ( $image_meta = wp_read_image_metadata( $file ) ) {
   462 	$image_meta = wp_read_image_metadata( $file );
       
   463 
       
   464 	if ( $image_meta ) {
   442 		if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) {
   465 		if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) {
   443 			$title = $image_meta['title'];
   466 			$title = $image_meta['title'];
   444 		}
   467 		}
       
   468 
   445 		if ( trim( $image_meta['caption'] ) ) {
   469 		if ( trim( $image_meta['caption'] ) ) {
   446 			$content = $image_meta['caption'];
   470 			$content = $image_meta['caption'];
   447 		}
   471 		}
   448 	}
   472 	}
   449 
   473 
   464 	);
   488 	);
   465 
   489 
   466 	// This should never be set as it would then overwrite an existing attachment.
   490 	// This should never be set as it would then overwrite an existing attachment.
   467 	unset( $attachment['ID'] );
   491 	unset( $attachment['ID'] );
   468 
   492 
   469 	// Save the attachment metadata
   493 	// Save the attachment metadata.
   470 	$id = wp_insert_attachment( $attachment, $file, $post_id, true );
   494 	$attachment_id = wp_insert_attachment( $attachment, $file, $post_id, true );
   471 	if ( ! is_wp_error( $id ) ) {
   495 
   472 		wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
   496 	if ( ! is_wp_error( $attachment_id ) ) {
   473 	}
   497 		wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
   474 
   498 	}
   475 	return $id;
   499 
   476 }
   500 	return $attachment_id;
   477 
   501 }
   478 /**
   502 
   479  * Adds the iframe to display content for the media upload page
   503 /**
       
   504  * Outputs the iframe to display the media upload page.
   480  *
   505  *
   481  * @since 2.5.0
   506  * @since 2.5.0
       
   507  * @since 5.3.0 Formalized the existing and already documented `...$args` parameter
       
   508  *              by adding it to the function signature.
   482  *
   509  *
   483  * @global int $body_id
   510  * @global int $body_id
   484  *
   511  *
   485  * @param string|callable $content_func
   512  * @param callable $content_func Function that outputs the content.
   486  */
   513  * @param mixed    ...$args      Optional additional parameters to pass to the callback function when it's called.
   487 function wp_iframe( $content_func /* ... */ ) {
   514  */
       
   515 function wp_iframe( $content_func, ...$args ) {
   488 	_wp_admin_html_begin();
   516 	_wp_admin_html_begin();
   489 	?>
   517 	?>
   490 <title><?php bloginfo( 'name' ); ?> &rsaquo; <?php _e( 'Uploads' ); ?> &#8212; <?php _e( 'WordPress' ); ?></title>
   518 	<title><?php bloginfo( 'name' ); ?> &rsaquo; <?php _e( 'Uploads' ); ?> &#8212; <?php _e( 'WordPress' ); ?></title>
   491 	<?php
   519 	<?php
   492 
   520 
   493 	wp_enqueue_style( 'colors' );
   521 	wp_enqueue_style( 'colors' );
   494 	// Check callback name for 'media'
   522 	// Check callback name for 'media'.
   495 	if ( ( is_array( $content_func ) && ! empty( $content_func[1] ) && 0 === strpos( (string) $content_func[1], 'media' ) )
   523 	if (
   496 	|| ( ! is_array( $content_func ) && 0 === strpos( $content_func, 'media' ) ) ) {
   524 		( is_array( $content_func ) && ! empty( $content_func[1] ) && 0 === strpos( (string) $content_func[1], 'media' ) ) ||
       
   525 		( ! is_array( $content_func ) && 0 === strpos( $content_func, 'media' ) )
       
   526 	) {
   497 		wp_enqueue_style( 'deprecated-media' );
   527 		wp_enqueue_style( 'deprecated-media' );
   498 	}
   528 	}
   499 	wp_enqueue_style( 'ie' );
   529 
   500 	?>
   530 	?>
   501 <script type="text/javascript">
   531 	<script type="text/javascript">
   502 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();}}};
   532 	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();}}};
   503 var ajaxurl = '<?php echo admin_url( 'admin-ajax.php', 'relative' ); ?>', pagenow = 'media-upload-popup', adminpage = 'media-upload-popup',
   533 	var ajaxurl = '<?php echo admin_url( 'admin-ajax.php', 'relative' ); ?>', pagenow = 'media-upload-popup', adminpage = 'media-upload-popup',
   504 isRtl = <?php echo (int) is_rtl(); ?>;
   534 	isRtl = <?php echo (int) is_rtl(); ?>;
   505 </script>
   535 	</script>
   506 	<?php
   536 	<?php
   507 	/** This action is documented in wp-admin/admin-header.php */
   537 	/** This action is documented in wp-admin/admin-header.php */
   508 	do_action( 'admin_enqueue_scripts', 'media-upload-popup' );
   538 	do_action( 'admin_enqueue_scripts', 'media-upload-popup' );
   509 
   539 
   510 	/**
   540 	/**
   511 	 * Fires when admin styles enqueued for the legacy (pre-3.5.0) media upload popup are printed.
   541 	 * Fires when admin styles enqueued for the legacy (pre-3.5.0) media upload popup are printed.
   512 	 *
   542 	 *
   513 	 * @since 2.9.0
   543 	 * @since 2.9.0
   514 	 */
   544 	 */
   515 	do_action( 'admin_print_styles-media-upload-popup' );
   545 	do_action( 'admin_print_styles-media-upload-popup' );  // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
   516 
   546 
   517 	/** This action is documented in wp-admin/admin-header.php */
   547 	/** This action is documented in wp-admin/admin-header.php */
   518 	do_action( 'admin_print_styles' );
   548 	do_action( 'admin_print_styles' );
   519 
   549 
   520 	/**
   550 	/**
   521 	 * Fires when admin scripts enqueued for the legacy (pre-3.5.0) media upload popup are printed.
   551 	 * Fires when admin scripts enqueued for the legacy (pre-3.5.0) media upload popup are printed.
   522 	 *
   552 	 *
   523 	 * @since 2.9.0
   553 	 * @since 2.9.0
   524 	 */
   554 	 */
   525 	do_action( 'admin_print_scripts-media-upload-popup' );
   555 	do_action( 'admin_print_scripts-media-upload-popup' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
   526 
   556 
   527 	/** This action is documented in wp-admin/admin-header.php */
   557 	/** This action is documented in wp-admin/admin-header.php */
   528 	do_action( 'admin_print_scripts' );
   558 	do_action( 'admin_print_scripts' );
   529 
   559 
   530 	/**
   560 	/**
   531 	 * Fires when scripts enqueued for the admin header for the legacy (pre-3.5.0)
   561 	 * Fires when scripts enqueued for the admin header for the legacy (pre-3.5.0)
   532 	 * media upload popup are printed.
   562 	 * media upload popup are printed.
   533 	 *
   563 	 *
   534 	 * @since 2.9.0
   564 	 * @since 2.9.0
   535 	 */
   565 	 */
   536 	do_action( 'admin_head-media-upload-popup' );
   566 	do_action( 'admin_head-media-upload-popup' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
   537 
   567 
   538 	/** This action is documented in wp-admin/admin-header.php */
   568 	/** This action is documented in wp-admin/admin-header.php */
   539 	do_action( 'admin_head' );
   569 	do_action( 'admin_head' );
   540 
   570 
   541 	if ( is_string( $content_func ) ) {
   571 	if ( is_string( $content_func ) ) {
   552 		 */
   582 		 */
   553 		do_action( "admin_head_{$content_func}" );
   583 		do_action( "admin_head_{$content_func}" );
   554 	}
   584 	}
   555 
   585 
   556 	$body_id_attr = '';
   586 	$body_id_attr = '';
       
   587 
   557 	if ( isset( $GLOBALS['body_id'] ) ) {
   588 	if ( isset( $GLOBALS['body_id'] ) ) {
   558 		$body_id_attr = ' id="' . $GLOBALS['body_id'] . '"';
   589 		$body_id_attr = ' id="' . $GLOBALS['body_id'] . '"';
   559 	}
   590 	}
       
   591 
   560 	?>
   592 	?>
   561 </head>
   593 	</head>
   562 <body<?php echo $body_id_attr; ?> class="wp-core-ui no-js">
   594 	<body<?php echo $body_id_attr; ?> class="wp-core-ui no-js">
   563 <script type="text/javascript">
   595 	<script type="text/javascript">
   564 document.body.className = document.body.className.replace('no-js', 'js');
   596 	document.body.className = document.body.className.replace('no-js', 'js');
   565 </script>
   597 	</script>
   566 	<?php
   598 	<?php
   567 	$args = func_get_args();
   599 
   568 	$args = array_slice( $args, 1 );
       
   569 	call_user_func_array( $content_func, $args );
   600 	call_user_func_array( $content_func, $args );
   570 
   601 
   571 	/** This action is documented in wp-admin/admin-footer.php */
   602 	/** This action is documented in wp-admin/admin-footer.php */
   572 	do_action( 'admin_print_footer_scripts' );
   603 	do_action( 'admin_print_footer_scripts' );
       
   604 
   573 	?>
   605 	?>
   574 <script type="text/javascript">if(typeof wpOnload=='function')wpOnload();</script>
   606 	<script type="text/javascript">if(typeof wpOnload=='function')wpOnload();</script>
   575 </body>
   607 	</body>
   576 </html>
   608 	</html>
   577 	<?php
   609 	<?php
   578 }
   610 }
   579 
   611 
   580 /**
   612 /**
   581  * Adds the media button to the editor
   613  * Adds the media button to the editor
   582  *
   614  *
   583  * @since 2.5.0
   615  * @since 2.5.0
   584  *
   616  *
   585  * @global int $post_ID
   617  * @global int $post_ID
   586  *
       
   587  * @staticvar int $instance
       
   588  *
   618  *
   589  * @param string $editor_id
   619  * @param string $editor_id
   590  */
   620  */
   591 function media_buttons( $editor_id = 'content' ) {
   621 function media_buttons( $editor_id = 'content' ) {
   592 	static $instance = 0;
   622 	static $instance = 0;
   593 	$instance++;
   623 	$instance++;
   594 
   624 
   595 	$post = get_post();
   625 	$post = get_post();
       
   626 
   596 	if ( ! $post && ! empty( $GLOBALS['post_ID'] ) ) {
   627 	if ( ! $post && ! empty( $GLOBALS['post_ID'] ) ) {
   597 		$post = $GLOBALS['post_ID'];
   628 		$post = $GLOBALS['post_ID'];
   598 	}
   629 	}
   599 
   630 
   600 	wp_enqueue_media(
   631 	wp_enqueue_media( array( 'post' => $post ) );
   601 		array(
       
   602 			'post' => $post,
       
   603 		)
       
   604 	);
       
   605 
   632 
   606 	$img = '<span class="wp-media-buttons-icon"></span> ';
   633 	$img = '<span class="wp-media-buttons-icon"></span> ';
   607 
   634 
   608 	$id_attribute = $instance === 1 ? ' id="insert-media-button"' : '';
   635 	$id_attribute = 1 === $instance ? ' id="insert-media-button"' : '';
       
   636 
   609 	printf(
   637 	printf(
   610 		'<button type="button"%s class="button insert-media add_media" data-editor="%s">%s</button>',
   638 		'<button type="button"%s class="button insert-media add_media" data-editor="%s">%s</button>',
   611 		$id_attribute,
   639 		$id_attribute,
   612 		esc_attr( $editor_id ),
   640 		esc_attr( $editor_id ),
   613 		$img . __( 'Add Media' )
   641 		$img . __( 'Add Media' )
   614 	);
   642 	);
       
   643 
   615 	/**
   644 	/**
   616 	 * Filters the legacy (pre-3.5.0) media buttons.
   645 	 * Filters the legacy (pre-3.5.0) media buttons.
   617 	 *
   646 	 *
   618 	 * Use {@see 'media_buttons'} action instead.
   647 	 * Use {@see 'media_buttons'} action instead.
   619 	 *
   648 	 *
   620 	 * @since 2.5.0
   649 	 * @since 2.5.0
   621 	 * @deprecated 3.5.0 Use {@see 'media_buttons'} action instead.
   650 	 * @deprecated 3.5.0 Use {@see 'media_buttons'} action instead.
   622 	 *
   651 	 *
   623 	 * @param string $string Media buttons context. Default empty.
   652 	 * @param string $string Media buttons context. Default empty.
   624 	 */
   653 	 */
   625 	$legacy_filter = apply_filters( 'media_buttons_context', '' );
   654 	$legacy_filter = apply_filters_deprecated( 'media_buttons_context', array( '' ), '3.5.0', 'media_buttons' );
   626 
   655 
   627 	if ( $legacy_filter ) {
   656 	if ( $legacy_filter ) {
   628 		// #WP22559. Close <a> if a plugin started by closing <a> to open their own <a> tag.
   657 		// #WP22559. Close <a> if a plugin started by closing <a> to open their own <a> tag.
   629 		if ( 0 === stripos( trim( $legacy_filter ), '</a>' ) ) {
   658 		if ( 0 === stripos( trim( $legacy_filter ), '</a>' ) ) {
   630 			$legacy_filter .= '</a>';
   659 			$legacy_filter .= '</a>';
   634 }
   663 }
   635 
   664 
   636 /**
   665 /**
   637  * @global int $post_ID
   666  * @global int $post_ID
   638  * @param string $type
   667  * @param string $type
   639  * @param int $post_id
   668  * @param int    $post_id
   640  * @param string $tab
   669  * @param string $tab
   641  * @return string
   670  * @return string
   642  */
   671  */
   643 function get_upload_iframe_src( $type = null, $post_id = null, $tab = null ) {
   672 function get_upload_iframe_src( $type = null, $post_id = null, $tab = null ) {
   644 	global $post_ID;
   673 	global $post_ID;
   647 		$post_id = $post_ID;
   676 		$post_id = $post_ID;
   648 	}
   677 	}
   649 
   678 
   650 	$upload_iframe_src = add_query_arg( 'post_id', (int) $post_id, admin_url( 'media-upload.php' ) );
   679 	$upload_iframe_src = add_query_arg( 'post_id', (int) $post_id, admin_url( 'media-upload.php' ) );
   651 
   680 
   652 	if ( $type && 'media' != $type ) {
   681 	if ( $type && 'media' !== $type ) {
   653 		$upload_iframe_src = add_query_arg( 'type', $type, $upload_iframe_src );
   682 		$upload_iframe_src = add_query_arg( 'type', $type, $upload_iframe_src );
   654 	}
   683 	}
   655 
   684 
   656 	if ( ! empty( $tab ) ) {
   685 	if ( ! empty( $tab ) ) {
   657 		$upload_iframe_src = add_query_arg( 'tab', $tab, $upload_iframe_src );
   686 		$upload_iframe_src = add_query_arg( 'tab', $tab, $upload_iframe_src );
   689 		$send_id = (int) reset( $keys );
   718 		$send_id = (int) reset( $keys );
   690 	}
   719 	}
   691 
   720 
   692 	if ( ! empty( $_POST['attachments'] ) ) {
   721 	if ( ! empty( $_POST['attachments'] ) ) {
   693 		foreach ( $_POST['attachments'] as $attachment_id => $attachment ) {
   722 		foreach ( $_POST['attachments'] as $attachment_id => $attachment ) {
   694 			$post = $_post = get_post( $attachment_id, ARRAY_A );
   723 			$post  = get_post( $attachment_id, ARRAY_A );
       
   724 			$_post = $post;
   695 
   725 
   696 			if ( ! current_user_can( 'edit_post', $attachment_id ) ) {
   726 			if ( ! current_user_can( 'edit_post', $attachment_id ) ) {
   697 				continue;
   727 				continue;
   698 			}
   728 			}
   699 
   729 
   700 			if ( isset( $attachment['post_content'] ) ) {
   730 			if ( isset( $attachment['post_content'] ) ) {
   701 				$post['post_content'] = $attachment['post_content'];
   731 				$post['post_content'] = $attachment['post_content'];
   702 			}
   732 			}
       
   733 
   703 			if ( isset( $attachment['post_title'] ) ) {
   734 			if ( isset( $attachment['post_title'] ) ) {
   704 				$post['post_title'] = $attachment['post_title'];
   735 				$post['post_title'] = $attachment['post_title'];
   705 			}
   736 			}
       
   737 
   706 			if ( isset( $attachment['post_excerpt'] ) ) {
   738 			if ( isset( $attachment['post_excerpt'] ) ) {
   707 				$post['post_excerpt'] = $attachment['post_excerpt'];
   739 				$post['post_excerpt'] = $attachment['post_excerpt'];
   708 			}
   740 			}
       
   741 
   709 			if ( isset( $attachment['menu_order'] ) ) {
   742 			if ( isset( $attachment['menu_order'] ) ) {
   710 				$post['menu_order'] = $attachment['menu_order'];
   743 				$post['menu_order'] = $attachment['menu_order'];
   711 			}
   744 			}
   712 
   745 
   713 			if ( isset( $send_id ) && $attachment_id == $send_id ) {
   746 			if ( isset( $send_id ) && $attachment_id == $send_id ) {
   728 			 */
   761 			 */
   729 			$post = apply_filters( 'attachment_fields_to_save', $post, $attachment );
   762 			$post = apply_filters( 'attachment_fields_to_save', $post, $attachment );
   730 
   763 
   731 			if ( isset( $attachment['image_alt'] ) ) {
   764 			if ( isset( $attachment['image_alt'] ) ) {
   732 				$image_alt = wp_unslash( $attachment['image_alt'] );
   765 				$image_alt = wp_unslash( $attachment['image_alt'] );
   733 				if ( $image_alt != get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) {
   766 
       
   767 				if ( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) !== $image_alt ) {
   734 					$image_alt = wp_strip_all_tags( $image_alt, true );
   768 					$image_alt = wp_strip_all_tags( $image_alt, true );
   735 
   769 
   736 					// Update_meta expects slashed.
   770 					// update_post_meta() expects slashed.
   737 					update_post_meta( $attachment_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
   771 					update_post_meta( $attachment_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
   738 				}
   772 				}
   739 			}
   773 			}
   740 
   774 
   741 			if ( isset( $post['errors'] ) ) {
   775 			if ( isset( $post['errors'] ) ) {
   760 		<script type="text/javascript">
   794 		<script type="text/javascript">
   761 		var win = window.dialogArguments || opener || parent || top;
   795 		var win = window.dialogArguments || opener || parent || top;
   762 		win.tb_remove();
   796 		win.tb_remove();
   763 		</script>
   797 		</script>
   764 		<?php
   798 		<?php
       
   799 
   765 		exit;
   800 		exit;
   766 	}
   801 	}
   767 
   802 
   768 	if ( isset( $send_id ) ) {
   803 	if ( isset( $send_id ) ) {
   769 		$attachment = wp_unslash( $_POST['attachments'][ $send_id ] );
   804 		$attachment = wp_unslash( $_POST['attachments'][ $send_id ] );
   770 
   805 		$html       = isset( $attachment['post_title'] ) ? $attachment['post_title'] : '';
   771 		$html = isset( $attachment['post_title'] ) ? $attachment['post_title'] : '';
   806 
   772 		if ( ! empty( $attachment['url'] ) ) {
   807 		if ( ! empty( $attachment['url'] ) ) {
   773 			$rel = '';
   808 			$rel = '';
       
   809 
   774 			if ( strpos( $attachment['url'], 'attachment_id' ) || get_attachment_link( $send_id ) == $attachment['url'] ) {
   810 			if ( strpos( $attachment['url'], 'attachment_id' ) || get_attachment_link( $send_id ) == $attachment['url'] ) {
   775 				$rel = " rel='attachment wp-att-" . esc_attr( $send_id ) . "'";
   811 				$rel = " rel='attachment wp-att-" . esc_attr( $send_id ) . "'";
   776 			}
   812 			}
       
   813 
   777 			$html = "<a href='{$attachment['url']}'$rel>$html</a>";
   814 			$html = "<a href='{$attachment['url']}'$rel>$html</a>";
   778 		}
   815 		}
   779 
   816 
   780 		/**
   817 		/**
   781 		 * Filters the HTML markup for a media item sent to the editor.
   818 		 * Filters the HTML markup for a media item sent to the editor.
   787 		 * @param string $html       HTML markup for a media item sent to the editor.
   824 		 * @param string $html       HTML markup for a media item sent to the editor.
   788 		 * @param int    $send_id    The first key from the $_POST['send'] data.
   825 		 * @param int    $send_id    The first key from the $_POST['send'] data.
   789 		 * @param array  $attachment Array of attachment metadata.
   826 		 * @param array  $attachment Array of attachment metadata.
   790 		 */
   827 		 */
   791 		$html = apply_filters( 'media_send_to_editor', $html, $send_id, $attachment );
   828 		$html = apply_filters( 'media_send_to_editor', $html, $send_id, $attachment );
       
   829 
   792 		return media_send_to_editor( $html );
   830 		return media_send_to_editor( $html );
   793 	}
   831 	}
   794 
   832 
   795 	return $errors;
   833 	return $errors;
   796 }
   834 }
   806 	$errors = array();
   844 	$errors = array();
   807 	$id     = 0;
   845 	$id     = 0;
   808 
   846 
   809 	if ( isset( $_POST['html-upload'] ) && ! empty( $_FILES ) ) {
   847 	if ( isset( $_POST['html-upload'] ) && ! empty( $_FILES ) ) {
   810 		check_admin_referer( 'media-form' );
   848 		check_admin_referer( 'media-form' );
   811 		// Upload File button was clicked
   849 		// Upload File button was clicked.
   812 		$id = media_handle_upload( 'async-upload', $_REQUEST['post_id'] );
   850 		$id = media_handle_upload( 'async-upload', $_REQUEST['post_id'] );
   813 		unset( $_FILES );
   851 		unset( $_FILES );
       
   852 
   814 		if ( is_wp_error( $id ) ) {
   853 		if ( is_wp_error( $id ) ) {
   815 			$errors['upload_error'] = $id;
   854 			$errors['upload_error'] = $id;
   816 			$id                     = false;
   855 			$id                     = false;
   817 		}
   856 		}
   818 	}
   857 	}
   819 
   858 
   820 	if ( ! empty( $_POST['insertonlybutton'] ) ) {
   859 	if ( ! empty( $_POST['insertonlybutton'] ) ) {
   821 		$src = $_POST['src'];
   860 		$src = $_POST['src'];
       
   861 
   822 		if ( ! empty( $src ) && ! strpos( $src, '://' ) ) {
   862 		if ( ! empty( $src ) && ! strpos( $src, '://' ) ) {
   823 			$src = "http://$src";
   863 			$src = "http://$src";
   824 		}
   864 		}
   825 
   865 
   826 		if ( isset( $_POST['media_type'] ) && 'image' != $_POST['media_type'] ) {
   866 		if ( isset( $_POST['media_type'] ) && 'image' !== $_POST['media_type'] ) {
   827 			$title = esc_html( wp_unslash( $_POST['title'] ) );
   867 			$title = esc_html( wp_unslash( $_POST['title'] ) );
   828 			if ( empty( $title ) ) {
   868 			if ( empty( $title ) ) {
   829 				$title = esc_html( wp_basename( $src ) );
   869 				$title = esc_html( wp_basename( $src ) );
   830 			}
   870 			}
   831 
   871 
   832 			if ( $title && $src ) {
   872 			if ( $title && $src ) {
   833 				$html = "<a href='" . esc_url( $src ) . "'>$title</a>";
   873 				$html = "<a href='" . esc_url( $src ) . "'>$title</a>";
   834 			}
   874 			}
   835 
   875 
   836 			$type = 'file';
   876 			$type = 'file';
   837 			if ( ( $ext = preg_replace( '/^.+?\.([^.]+)$/', '$1', $src ) ) && ( $ext_type = wp_ext2type( $ext ) )
   877 			$ext  = preg_replace( '/^.+?\.([^.]+)$/', '$1', $src );
   838 				&& ( 'audio' == $ext_type || 'video' == $ext_type ) ) {
   878 
       
   879 			if ( $ext ) {
       
   880 				$ext_type = wp_ext2type( $ext );
       
   881 				if ( 'audio' === $ext_type || 'video' === $ext_type ) {
   839 					$type = $ext_type;
   882 					$type = $ext_type;
       
   883 				}
   840 			}
   884 			}
   841 
   885 
   842 			/**
   886 			/**
   843 			 * Filters the URL sent to the editor for a specific media type.
   887 			 * Filters the URL sent to the editor for a specific media type.
   844 			 *
   888 			 *
   853 			 */
   897 			 */
   854 			$html = apply_filters( "{$type}_send_to_editor_url", $html, esc_url_raw( $src ), $title );
   898 			$html = apply_filters( "{$type}_send_to_editor_url", $html, esc_url_raw( $src ), $title );
   855 		} else {
   899 		} else {
   856 			$align = '';
   900 			$align = '';
   857 			$alt   = esc_attr( wp_unslash( $_POST['alt'] ) );
   901 			$alt   = esc_attr( wp_unslash( $_POST['alt'] ) );
       
   902 
   858 			if ( isset( $_POST['align'] ) ) {
   903 			if ( isset( $_POST['align'] ) ) {
   859 				$align = esc_attr( wp_unslash( $_POST['align'] ) );
   904 				$align = esc_attr( wp_unslash( $_POST['align'] ) );
   860 				$class = " class='align$align'";
   905 				$class = " class='align$align'";
   861 			}
   906 			}
       
   907 
   862 			if ( ! empty( $src ) ) {
   908 			if ( ! empty( $src ) ) {
   863 				$html = "<img src='" . esc_url( $src ) . "' alt='$alt'$class />";
   909 				$html = "<img src='" . esc_url( $src ) . "' alt='$alt'$class />";
   864 			}
   910 			}
   865 
   911 
   866 			/**
   912 			/**
   881 	}
   927 	}
   882 
   928 
   883 	if ( isset( $_POST['save'] ) ) {
   929 	if ( isset( $_POST['save'] ) ) {
   884 		$errors['upload_notice'] = __( 'Saved.' );
   930 		$errors['upload_notice'] = __( 'Saved.' );
   885 		wp_enqueue_script( 'admin-gallery' );
   931 		wp_enqueue_script( 'admin-gallery' );
       
   932 
   886 		return wp_iframe( 'media_upload_gallery_form', $errors );
   933 		return wp_iframe( 'media_upload_gallery_form', $errors );
   887 
   934 
   888 	} elseif ( ! empty( $_POST ) ) {
   935 	} elseif ( ! empty( $_POST ) ) {
   889 		$return = media_upload_form_handler();
   936 		$return = media_upload_form_handler();
   890 
   937 
   891 		if ( is_string( $return ) ) {
   938 		if ( is_string( $return ) ) {
   892 			return $return;
   939 			return $return;
   893 		}
   940 		}
       
   941 
   894 		if ( is_array( $return ) ) {
   942 		if ( is_array( $return ) ) {
   895 			$errors = $return;
   943 			$errors = $return;
   896 		}
   944 		}
   897 	}
   945 	}
   898 
   946 
   899 	if ( isset( $_GET['tab'] ) && $_GET['tab'] == 'type_url' ) {
   947 	if ( isset( $_GET['tab'] ) && 'type_url' === $_GET['tab'] ) {
   900 		$type = 'image';
   948 		$type = 'image';
   901 		if ( isset( $_GET['type'] ) && in_array( $_GET['type'], array( 'video', 'audio', 'file' ) ) ) {
   949 
       
   950 		if ( isset( $_GET['type'] ) && in_array( $_GET['type'], array( 'video', 'audio', 'file' ), true ) ) {
   902 			$type = $_GET['type'];
   951 			$type = $_GET['type'];
   903 		}
   952 		}
       
   953 
   904 		return wp_iframe( 'media_upload_type_url_form', $type, $errors, $id );
   954 		return wp_iframe( 'media_upload_type_url_form', $type, $errors, $id );
   905 	}
   955 	}
   906 
   956 
   907 	return wp_iframe( 'media_upload_type_form', 'image', $errors, $id );
   957 	return wp_iframe( 'media_upload_type_form', 'image', $errors, $id );
   908 }
   958 }
   911  * Downloads an image from the specified URL and attaches it to a post.
   961  * Downloads an image from the specified URL and attaches it to a post.
   912  *
   962  *
   913  * @since 2.6.0
   963  * @since 2.6.0
   914  * @since 4.2.0 Introduced the `$return` parameter.
   964  * @since 4.2.0 Introduced the `$return` parameter.
   915  * @since 4.8.0 Introduced the 'id' option within the `$return` parameter.
   965  * @since 4.8.0 Introduced the 'id' option within the `$return` parameter.
       
   966  * @since 5.3.0 The `$post_id` parameter was made optional.
       
   967  * @since 5.4.0 The original URL of the attachment is stored in the `_source_url`
       
   968  *              post meta value.
   916  *
   969  *
   917  * @param string $file    The URL of the image to download.
   970  * @param string $file    The URL of the image to download.
   918  * @param int    $post_id The post ID the media is to be associated with.
   971  * @param int    $post_id Optional. The post ID the media is to be associated with.
   919  * @param string $desc    Optional. Description of the image.
   972  * @param string $desc    Optional. Description of the image.
   920  * @param string $return  Optional. Accepts 'html' (image tag html) or 'src' (URL), or 'id' (attachment ID). Default 'html'.
   973  * @param string $return  Optional. Accepts 'html' (image tag html) or 'src' (URL),
       
   974  *                        or 'id' (attachment ID). Default 'html'.
   921  * @return string|WP_Error Populated HTML img tag on success, WP_Error object otherwise.
   975  * @return string|WP_Error Populated HTML img tag on success, WP_Error object otherwise.
   922  */
   976  */
   923 function media_sideload_image( $file, $post_id, $desc = null, $return = 'html' ) {
   977 function media_sideload_image( $file, $post_id = 0, $desc = null, $return = 'html' ) {
   924 	if ( ! empty( $file ) ) {
   978 	if ( ! empty( $file ) ) {
   925 
   979 
   926 		// Set variables for storage, fix file filename for query strings.
   980 		// Set variables for storage, fix file filename for query strings.
   927 		preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
   981 		preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
       
   982 
   928 		if ( ! $matches ) {
   983 		if ( ! $matches ) {
   929 			return new WP_Error( 'image_sideload_failed', __( 'Invalid image URL' ) );
   984 			return new WP_Error( 'image_sideload_failed', __( 'Invalid image URL.' ) );
   930 		}
   985 		}
   931 
   986 
   932 		$file_array         = array();
   987 		$file_array         = array();
   933 		$file_array['name'] = wp_basename( $matches[0] );
   988 		$file_array['name'] = wp_basename( $matches[0] );
   934 
   989 
   945 
  1000 
   946 		// If error storing permanently, unlink.
  1001 		// If error storing permanently, unlink.
   947 		if ( is_wp_error( $id ) ) {
  1002 		if ( is_wp_error( $id ) ) {
   948 			@unlink( $file_array['tmp_name'] );
  1003 			@unlink( $file_array['tmp_name'] );
   949 			return $id;
  1004 			return $id;
   950 			// If attachment id was requested, return it early.
  1005 		}
   951 		} elseif ( $return === 'id' ) {
  1006 
       
  1007 		// Store the original attachment source in meta.
       
  1008 		add_post_meta( $id, '_source_url', $file );
       
  1009 
       
  1010 		// If attachment ID was requested, return it.
       
  1011 		if ( 'id' === $return ) {
   952 			return $id;
  1012 			return $id;
   953 		}
  1013 		}
   954 
  1014 
   955 		$src = wp_get_attachment_url( $id );
  1015 		$src = wp_get_attachment_url( $id );
   956 	}
  1016 	}
   957 
  1017 
   958 	// Finally, check to make sure the file has been saved, then return the HTML.
  1018 	// Finally, check to make sure the file has been saved, then return the HTML.
   959 	if ( ! empty( $src ) ) {
  1019 	if ( ! empty( $src ) ) {
   960 		if ( $return === 'src' ) {
  1020 		if ( 'src' === $return ) {
   961 			return $src;
  1021 			return $src;
   962 		}
  1022 		}
   963 
  1023 
   964 		$alt  = isset( $desc ) ? esc_attr( $desc ) : '';
  1024 		$alt  = isset( $desc ) ? esc_attr( $desc ) : '';
   965 		$html = "<img src='$src' alt='$alt' />";
  1025 		$html = "<img src='$src' alt='$alt' />";
       
  1026 
   966 		return $html;
  1027 		return $html;
   967 	} else {
  1028 	} else {
   968 		return new WP_Error( 'image_sideload_failed' );
  1029 		return new WP_Error( 'image_sideload_failed' );
   969 	}
  1030 	}
   970 }
  1031 }
   983 		$return = media_upload_form_handler();
  1044 		$return = media_upload_form_handler();
   984 
  1045 
   985 		if ( is_string( $return ) ) {
  1046 		if ( is_string( $return ) ) {
   986 			return $return;
  1047 			return $return;
   987 		}
  1048 		}
       
  1049 
   988 		if ( is_array( $return ) ) {
  1050 		if ( is_array( $return ) ) {
   989 			$errors = $return;
  1051 			$errors = $return;
   990 		}
  1052 		}
   991 	}
  1053 	}
   992 
  1054 
  1001  *
  1063  *
  1002  * @return string|null
  1064  * @return string|null
  1003  */
  1065  */
  1004 function media_upload_library() {
  1066 function media_upload_library() {
  1005 	$errors = array();
  1067 	$errors = array();
       
  1068 
  1006 	if ( ! empty( $_POST ) ) {
  1069 	if ( ! empty( $_POST ) ) {
  1007 		$return = media_upload_form_handler();
  1070 		$return = media_upload_form_handler();
  1008 
  1071 
  1009 		if ( is_string( $return ) ) {
  1072 		if ( is_string( $return ) ) {
  1010 			return $return;
  1073 			return $return;
  1021  * Retrieve HTML for the image alignment radio buttons with the specified one checked.
  1084  * Retrieve HTML for the image alignment radio buttons with the specified one checked.
  1022  *
  1085  *
  1023  * @since 2.7.0
  1086  * @since 2.7.0
  1024  *
  1087  *
  1025  * @param WP_Post $post
  1088  * @param WP_Post $post
  1026  * @param string $checked
  1089  * @param string  $checked
  1027  * @return string
  1090  * @return string
  1028  */
  1091  */
  1029 function image_align_input_fields( $post, $checked = '' ) {
  1092 function image_align_input_fields( $post, $checked = '' ) {
  1030 
  1093 
  1031 	if ( empty( $checked ) ) {
  1094 	if ( empty( $checked ) ) {
  1036 		'none'   => __( 'None' ),
  1099 		'none'   => __( 'None' ),
  1037 		'left'   => __( 'Left' ),
  1100 		'left'   => __( 'Left' ),
  1038 		'center' => __( 'Center' ),
  1101 		'center' => __( 'Center' ),
  1039 		'right'  => __( 'Right' ),
  1102 		'right'  => __( 'Right' ),
  1040 	);
  1103 	);
       
  1104 
  1041 	if ( ! array_key_exists( (string) $checked, $alignments ) ) {
  1105 	if ( ! array_key_exists( (string) $checked, $alignments ) ) {
  1042 		$checked = 'none';
  1106 		$checked = 'none';
  1043 	}
  1107 	}
  1044 
  1108 
  1045 	$out = array();
  1109 	$out = array();
       
  1110 
  1046 	foreach ( $alignments as $name => $label ) {
  1111 	foreach ( $alignments as $name => $label ) {
  1047 		$name  = esc_attr( $name );
  1112 		$name  = esc_attr( $name );
  1048 		$out[] = "<input type='radio' name='attachments[{$post->ID}][align]' id='image-align-{$name}-{$post->ID}' value='$name'" .
  1113 		$out[] = "<input type='radio' name='attachments[{$post->ID}][align]' id='image-align-{$name}-{$post->ID}' value='$name'" .
  1049 			( $checked == $name ? " checked='checked'" : '' ) .
  1114 			( $checked == $name ? " checked='checked'" : '' ) .
  1050 			" /><label for='image-align-{$name}-{$post->ID}' class='align image-align-{$name}-label'>$label</label>";
  1115 			" /><label for='image-align-{$name}-{$post->ID}' class='align image-align-{$name}-label'>$label</label>";
  1051 	}
  1116 	}
       
  1117 
  1052 	return join( "\n", $out );
  1118 	return join( "\n", $out );
  1053 }
  1119 }
  1054 
  1120 
  1055 /**
  1121 /**
  1056  * Retrieve HTML for the size radio buttons with the specified one checked.
  1122  * Retrieve HTML for the size radio buttons with the specified one checked.
  1057  *
  1123  *
  1058  * @since 2.7.0
  1124  * @since 2.7.0
  1059  *
  1125  *
  1060  * @param WP_Post $post
  1126  * @param WP_Post     $post
  1061  * @param bool|string $check
  1127  * @param bool|string $check
  1062  * @return array
  1128  * @return array
  1063  */
  1129  */
  1064 function image_size_input_fields( $post, $check = '' ) {
  1130 function image_size_input_fields( $post, $check = '' ) {
  1065 	/**
  1131 	/**
  1066 	 * Filters the names and labels of the default image sizes.
  1132 	 * Filters the names and labels of the default image sizes.
  1067 	 *
  1133 	 *
  1068 	 * @since 3.3.0
  1134 	 * @since 3.3.0
  1069 	 *
  1135 	 *
  1070 	 * @param array $size_names Array of image sizes and their names. Default values
  1136 	 * @param string[] $size_names Array of image size labels keyed by their name. Default values
  1071 	 *                          include 'Thumbnail', 'Medium', 'Large', 'Full Size'.
  1137 	 *                             include 'Thumbnail', 'Medium', 'Large', and 'Full Size'.
  1072 	 */
  1138 	 */
  1073 	$size_names = apply_filters(
  1139 	$size_names = apply_filters(
  1074 		'image_size_names_choose',
  1140 		'image_size_names_choose',
  1075 		array(
  1141 		array(
  1076 			'thumbnail' => __( 'Thumbnail' ),
  1142 			'thumbnail' => __( 'Thumbnail' ),
  1081 	);
  1147 	);
  1082 
  1148 
  1083 	if ( empty( $check ) ) {
  1149 	if ( empty( $check ) ) {
  1084 		$check = get_user_setting( 'imgsize', 'medium' );
  1150 		$check = get_user_setting( 'imgsize', 'medium' );
  1085 	}
  1151 	}
       
  1152 
  1086 	$out = array();
  1153 	$out = array();
  1087 
  1154 
  1088 	foreach ( $size_names as $size => $label ) {
  1155 	foreach ( $size_names as $size => $label ) {
  1089 		$downsize = image_downsize( $post->ID, $size );
  1156 		$downsize = image_downsize( $post->ID, $size );
  1090 		$checked  = '';
  1157 		$checked  = '';
  1091 
  1158 
  1092 		// Is this size selectable?
  1159 		// Is this size selectable?
  1093 		$enabled = ( $downsize[3] || 'full' == $size );
  1160 		$enabled = ( $downsize[3] || 'full' === $size );
  1094 		$css_id  = "image-size-{$size}-{$post->ID}";
  1161 		$css_id  = "image-size-{$size}-{$post->ID}";
  1095 
  1162 
  1096 		// If this size is the default but that's not available, don't select it.
  1163 		// If this size is the default but that's not available, don't select it.
  1097 		if ( $size == $check ) {
  1164 		if ( $size == $check ) {
  1098 			if ( $enabled ) {
  1165 			if ( $enabled ) {
  1099 				$checked = " checked='checked'";
  1166 				$checked = " checked='checked'";
  1100 			} else {
  1167 			} else {
  1101 				$check = '';
  1168 				$check = '';
  1102 			}
  1169 			}
  1103 		} elseif ( ! $check && $enabled && 'thumbnail' != $size ) {
  1170 		} elseif ( ! $check && $enabled && 'thumbnail' !== $size ) {
  1104 			/*
  1171 			/*
  1105 			 * If $check is not enabled, default to the first available size
  1172 			 * If $check is not enabled, default to the first available size
  1106 			 * that's bigger than a thumbnail.
  1173 			 * that's bigger than a thumbnail.
  1107 			 */
  1174 			 */
  1108 			$check   = $size;
  1175 			$check   = $size;
  1133  * Retrieve HTML for the Link URL buttons with the default link type as specified.
  1200  * Retrieve HTML for the Link URL buttons with the default link type as specified.
  1134  *
  1201  *
  1135  * @since 2.7.0
  1202  * @since 2.7.0
  1136  *
  1203  *
  1137  * @param WP_Post $post
  1204  * @param WP_Post $post
  1138  * @param string $url_type
  1205  * @param string  $url_type
  1139  * @return string
  1206  * @return string
  1140  */
  1207  */
  1141 function image_link_input_fields( $post, $url_type = '' ) {
  1208 function image_link_input_fields( $post, $url_type = '' ) {
  1142 
  1209 
  1143 	$file = wp_get_attachment_url( $post->ID );
  1210 	$file = wp_get_attachment_url( $post->ID );
  1146 	if ( empty( $url_type ) ) {
  1213 	if ( empty( $url_type ) ) {
  1147 		$url_type = get_user_setting( 'urlbutton', 'post' );
  1214 		$url_type = get_user_setting( 'urlbutton', 'post' );
  1148 	}
  1215 	}
  1149 
  1216 
  1150 	$url = '';
  1217 	$url = '';
  1151 	if ( $url_type == 'file' ) {
  1218 
       
  1219 	if ( 'file' === $url_type ) {
  1152 		$url = $file;
  1220 		$url = $file;
  1153 	} elseif ( $url_type == 'post' ) {
  1221 	} elseif ( 'post' === $url_type ) {
  1154 		$url = $link;
  1222 		$url = $link;
  1155 	}
  1223 	}
  1156 
  1224 
  1157 	return "
  1225 	return "
  1158 	<input type='text' class='text urlfield' name='attachments[$post->ID][url]' value='" . esc_attr( $url ) . "' /><br />
  1226 	<input type='text' class='text urlfield' name='attachments[$post->ID][url]' value='" . esc_attr( $url ) . "' /><br />
  1180 /**
  1248 /**
  1181  * Retrieves the image attachment fields to edit form fields.
  1249  * Retrieves the image attachment fields to edit form fields.
  1182  *
  1250  *
  1183  * @since 2.5.0
  1251  * @since 2.5.0
  1184  *
  1252  *
  1185  * @param array $form_fields
  1253  * @param array  $form_fields
  1186  * @param object $post
  1254  * @param object $post
  1187  * @return array
  1255  * @return array
  1188  */
  1256  */
  1189 function image_attachment_fields_to_edit( $form_fields, $post ) {
  1257 function image_attachment_fields_to_edit( $form_fields, $post ) {
  1190 	return $form_fields;
  1258 	return $form_fields;
  1203 	unset( $form_fields['url'], $form_fields['align'], $form_fields['image-size'] );
  1271 	unset( $form_fields['url'], $form_fields['align'], $form_fields['image-size'] );
  1204 	return $form_fields;
  1272 	return $form_fields;
  1205 }
  1273 }
  1206 
  1274 
  1207 /**
  1275 /**
  1208  * Retrieves the post non-image attachment fields to edito form fields.
  1276  * Retrieves the post non-image attachment fields to edit form fields.
  1209  *
  1277  *
  1210  * @since 2.8.0
  1278  * @since 2.8.0
  1211  *
  1279  *
  1212  * @param array   $form_fields An array of attachment form fields.
  1280  * @param array   $form_fields An array of attachment form fields.
  1213  * @param WP_Post $post        The WP_Post attachment object.
  1281  * @param WP_Post $post        The WP_Post attachment object.
  1230  * @param array $post       The WP_Post attachment object converted to an array.
  1298  * @param array $post       The WP_Post attachment object converted to an array.
  1231  * @param array $attachment An array of attachment metadata.
  1299  * @param array $attachment An array of attachment metadata.
  1232  * @return array Filtered attachment post object.
  1300  * @return array Filtered attachment post object.
  1233  */
  1301  */
  1234 function image_attachment_fields_to_save( $post, $attachment ) {
  1302 function image_attachment_fields_to_save( $post, $attachment ) {
  1235 	if ( substr( $post['post_mime_type'], 0, 5 ) == 'image' ) {
  1303 	if ( 'image' === substr( $post['post_mime_type'], 0, 5 ) ) {
  1236 		if ( strlen( trim( $post['post_title'] ) ) == 0 ) {
  1304 		if ( strlen( trim( $post['post_title'] ) ) == 0 ) {
  1237 			$attachment_url                           = ( isset( $post['attachment_url'] ) ) ? $post['attachment_url'] : $post['guid'];
  1305 			$attachment_url                           = ( isset( $post['attachment_url'] ) ) ? $post['attachment_url'] : $post['guid'];
  1238 			$post['post_title']                       = preg_replace( '/\.\w+$/', '', wp_basename( $attachment_url ) );
  1306 			$post['post_title']                       = preg_replace( '/\.\w+$/', '', wp_basename( $attachment_url ) );
  1239 			$post['errors']['post_title']['errors'][] = __( 'Empty Title filled from filename.' );
  1307 			$post['errors']['post_title']['errors'][] = __( 'Empty Title filled from filename.' );
  1240 		}
  1308 		}
  1246 /**
  1314 /**
  1247  * Retrieves the media element HTML to send to the editor.
  1315  * Retrieves the media element HTML to send to the editor.
  1248  *
  1316  *
  1249  * @since 2.5.0
  1317  * @since 2.5.0
  1250  *
  1318  *
  1251  * @param string $html
  1319  * @param string  $html
  1252  * @param integer $attachment_id
  1320  * @param integer $attachment_id
  1253  * @param array $attachment
  1321  * @param array   $attachment
  1254  * @return string
  1322  * @return string
  1255  */
  1323  */
  1256 function image_media_send_to_editor( $html, $attachment_id, $attachment ) {
  1324 function image_media_send_to_editor( $html, $attachment_id, $attachment ) {
  1257 	$post = get_post( $attachment_id );
  1325 	$post = get_post( $attachment_id );
  1258 	if ( substr( $post->post_mime_type, 0, 5 ) == 'image' ) {
  1326 
       
  1327 	if ( 'image' === substr( $post->post_mime_type, 0, 5 ) ) {
  1259 		$url   = $attachment['url'];
  1328 		$url   = $attachment['url'];
  1260 		$align = ! empty( $attachment['align'] ) ? $attachment['align'] : 'none';
  1329 		$align = ! empty( $attachment['align'] ) ? $attachment['align'] : 'none';
  1261 		$size  = ! empty( $attachment['image-size'] ) ? $attachment['image-size'] : 'medium';
  1330 		$size  = ! empty( $attachment['image-size'] ) ? $attachment['image-size'] : 'medium';
  1262 		$alt   = ! empty( $attachment['image_alt'] ) ? $attachment['image_alt'] : '';
  1331 		$alt   = ! empty( $attachment['image_alt'] ) ? $attachment['image_alt'] : '';
  1263 		$rel   = ( strpos( $url, 'attachment_id' ) || $url === get_attachment_link( $attachment_id ) );
  1332 		$rel   = ( strpos( $url, 'attachment_id' ) || get_attachment_link( $attachment_id ) === $url );
  1264 
  1333 
  1265 		return get_image_send_to_editor( $attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel, $size, $alt );
  1334 		return get_image_send_to_editor( $attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel, $size, $alt );
  1266 	}
  1335 	}
  1267 
  1336 
  1268 	return $html;
  1337 	return $html;
  1272  * Retrieves the attachment fields to edit form fields.
  1341  * Retrieves the attachment fields to edit form fields.
  1273  *
  1342  *
  1274  * @since 2.5.0
  1343  * @since 2.5.0
  1275  *
  1344  *
  1276  * @param WP_Post $post
  1345  * @param WP_Post $post
  1277  * @param array $errors
  1346  * @param array   $errors
  1278  * @return array
  1347  * @return array
  1279  */
  1348  */
  1280 function get_attachment_fields_to_edit( $post, $errors = null ) {
  1349 function get_attachment_fields_to_edit( $post, $errors = null ) {
  1281 	if ( is_int( $post ) ) {
  1350 	if ( is_int( $post ) ) {
  1282 		$post = get_post( $post );
  1351 		$post = get_post( $post );
  1283 	}
  1352 	}
       
  1353 
  1284 	if ( is_array( $post ) ) {
  1354 	if ( is_array( $post ) ) {
  1285 		$post = new WP_Post( (object) $post );
  1355 		$post = new WP_Post( (object) $post );
  1286 	}
  1356 	}
  1287 
  1357 
  1288 	$image_url = wp_get_attachment_url( $post->ID );
  1358 	$image_url = wp_get_attachment_url( $post->ID );
  1324 		),
  1394 		),
  1325 	);
  1395 	);
  1326 
  1396 
  1327 	foreach ( get_attachment_taxonomies( $post ) as $taxonomy ) {
  1397 	foreach ( get_attachment_taxonomies( $post ) as $taxonomy ) {
  1328 		$t = (array) get_taxonomy( $taxonomy );
  1398 		$t = (array) get_taxonomy( $taxonomy );
       
  1399 
  1329 		if ( ! $t['public'] || ! $t['show_ui'] ) {
  1400 		if ( ! $t['public'] || ! $t['show_ui'] ) {
  1330 			continue;
  1401 			continue;
  1331 		}
  1402 		}
       
  1403 
  1332 		if ( empty( $t['label'] ) ) {
  1404 		if ( empty( $t['label'] ) ) {
  1333 			$t['label'] = $taxonomy;
  1405 			$t['label'] = $taxonomy;
  1334 		}
  1406 		}
       
  1407 
  1335 		if ( empty( $t['args'] ) ) {
  1408 		if ( empty( $t['args'] ) ) {
  1336 			$t['args'] = array();
  1409 			$t['args'] = array();
  1337 		}
  1410 		}
  1338 
  1411 
  1339 		$terms = get_object_term_cache( $post->ID, $taxonomy );
  1412 		$terms = get_object_term_cache( $post->ID, $taxonomy );
       
  1413 
  1340 		if ( false === $terms ) {
  1414 		if ( false === $terms ) {
  1341 			$terms = wp_get_object_terms( $post->ID, $taxonomy, $t['args'] );
  1415 			$terms = wp_get_object_terms( $post->ID, $taxonomy, $t['args'] );
  1342 		}
  1416 		}
  1343 
  1417 
  1344 		$values = array();
  1418 		$values = array();
  1345 
  1419 
  1346 		foreach ( $terms as $term ) {
  1420 		foreach ( $terms as $term ) {
  1347 			$values[] = $term->slug;
  1421 			$values[] = $term->slug;
  1348 		}
  1422 		}
       
  1423 
  1349 		$t['value'] = join( ', ', $values );
  1424 		$t['value'] = join( ', ', $values );
  1350 
  1425 
  1351 		$form_fields[ $taxonomy ] = $t;
  1426 		$form_fields[ $taxonomy ] = $t;
  1352 	}
  1427 	}
  1353 
  1428 
  1354 	// Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default
  1429 	/*
  1355 	// The recursive merge is easily traversed with array casting: foreach ( (array) $things as $thing )
  1430 	 * Merge default fields with their errors, so any key passed with the error
       
  1431 	 * (e.g. 'error', 'helps', 'value') will replace the default.
       
  1432 	 * The recursive merge is easily traversed with array casting:
       
  1433 	 * foreach ( (array) $things as $thing )
       
  1434 	 */
  1356 	$form_fields = array_merge_recursive( $form_fields, (array) $errors );
  1435 	$form_fields = array_merge_recursive( $form_fields, (array) $errors );
  1357 
  1436 
  1358 	// This was formerly in image_attachment_fields_to_edit().
  1437 	// This was formerly in image_attachment_fields_to_edit().
  1359 	if ( substr( $post->post_mime_type, 0, 5 ) == 'image' ) {
  1438 	if ( 'image' === substr( $post->post_mime_type, 0, 5 ) ) {
  1360 		$alt = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
  1439 		$alt = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
       
  1440 
  1361 		if ( empty( $alt ) ) {
  1441 		if ( empty( $alt ) ) {
  1362 			$alt = '';
  1442 			$alt = '';
  1363 		}
  1443 		}
  1364 
  1444 
  1365 		$form_fields['post_title']['required'] = true;
  1445 		$form_fields['post_title']['required'] = true;
  1402  * component. Will also create link for showing and hiding the form to modify
  1482  * component. Will also create link for showing and hiding the form to modify
  1403  * the image attachment.
  1483  * the image attachment.
  1404  *
  1484  *
  1405  * @since 2.5.0
  1485  * @since 2.5.0
  1406  *
  1486  *
  1407  * @global WP_Query $wp_the_query
  1487  * @global WP_Query $wp_the_query WordPress Query object.
  1408  *
  1488  *
  1409  * @param int $post_id Optional. Post ID.
  1489  * @param int   $post_id Optional. Post ID.
  1410  * @param array $errors Errors for attachment, if any.
  1490  * @param array $errors  Errors for attachment, if any.
  1411  * @return string
  1491  * @return string
  1412  */
  1492  */
  1413 function get_media_items( $post_id, $errors ) {
  1493 function get_media_items( $post_id, $errors ) {
  1414 	$attachments = array();
  1494 	$attachments = array();
       
  1495 
  1415 	if ( $post_id ) {
  1496 	if ( $post_id ) {
  1416 		$post = get_post( $post_id );
  1497 		$post = get_post( $post_id );
  1417 		if ( $post && $post->post_type == 'attachment' ) {
  1498 
       
  1499 		if ( $post && 'attachment' === $post->post_type ) {
  1418 			$attachments = array( $post->ID => $post );
  1500 			$attachments = array( $post->ID => $post );
  1419 		} else {
  1501 		} else {
  1420 			$attachments = get_children(
  1502 			$attachments = get_children(
  1421 				array(
  1503 				array(
  1422 					'post_parent' => $post_id,
  1504 					'post_parent' => $post_id,
  1434 		}
  1516 		}
  1435 	}
  1517 	}
  1436 
  1518 
  1437 	$output = '';
  1519 	$output = '';
  1438 	foreach ( (array) $attachments as $id => $attachment ) {
  1520 	foreach ( (array) $attachments as $id => $attachment ) {
  1439 		if ( $attachment->post_status == 'trash' ) {
  1521 		if ( 'trash' === $attachment->post_status ) {
  1440 			continue;
  1522 			continue;
  1441 		}
  1523 		}
  1442 		if ( $item = get_media_item( $id, array( 'errors' => isset( $errors[ $id ] ) ? $errors[ $id ] : null ) ) ) {
  1524 
       
  1525 		$item = get_media_item( $id, array( 'errors' => isset( $errors[ $id ] ) ? $errors[ $id ] : null ) );
       
  1526 
       
  1527 		if ( $item ) {
  1443 			$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>";
  1528 			$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>";
  1444 		}
  1529 		}
  1445 	}
  1530 	}
  1446 
  1531 
  1447 	return $output;
  1532 	return $output;
  1452  *
  1537  *
  1453  * @since 2.5.0
  1538  * @since 2.5.0
  1454  *
  1539  *
  1455  * @global string $redir_tab
  1540  * @global string $redir_tab
  1456  *
  1541  *
  1457  * @param int $attachment_id Attachment ID for modification.
  1542  * @param int          $attachment_id Attachment ID for modification.
  1458  * @param string|array $args Optional. Override defaults.
  1543  * @param string|array $args          Optional. Override defaults.
  1459  * @return string HTML form for attachment.
  1544  * @return string HTML form for attachment.
  1460  */
  1545  */
  1461 function get_media_item( $attachment_id, $args = null ) {
  1546 function get_media_item( $attachment_id, $args = null ) {
  1462 	global $redir_tab;
  1547 	global $redir_tab;
  1463 
  1548 
  1464 	if ( ( $attachment_id = intval( $attachment_id ) ) && $thumb_url = wp_get_attachment_image_src( $attachment_id, 'thumbnail', true ) ) {
  1549 	$thumb_url     = false;
  1465 		$thumb_url = $thumb_url[0];
  1550 	$attachment_id = intval( $attachment_id );
  1466 	} else {
  1551 
  1467 		$thumb_url = false;
  1552 	if ( $attachment_id ) {
       
  1553 		$thumb_url = wp_get_attachment_image_src( $attachment_id, 'thumbnail', true );
       
  1554 
       
  1555 		if ( $thumb_url ) {
       
  1556 			$thumb_url = $thumb_url[0];
       
  1557 		}
  1468 	}
  1558 	}
  1469 
  1559 
  1470 	$post            = get_post( $attachment_id );
  1560 	$post            = get_post( $attachment_id );
  1471 	$current_post_id = ! empty( $_GET['post_id'] ) ? (int) $_GET['post_id'] : 0;
  1561 	$current_post_id = ! empty( $_GET['post_id'] ) ? (int) $_GET['post_id'] : 0;
  1472 
  1562 
  1475 		'send'       => $current_post_id ? post_type_supports( get_post_type( $current_post_id ), 'editor' ) : true,
  1565 		'send'       => $current_post_id ? post_type_supports( get_post_type( $current_post_id ), 'editor' ) : true,
  1476 		'delete'     => true,
  1566 		'delete'     => true,
  1477 		'toggle'     => true,
  1567 		'toggle'     => true,
  1478 		'show_title' => true,
  1568 		'show_title' => true,
  1479 	);
  1569 	);
  1480 	$args         = wp_parse_args( $args, $default_args );
  1570 
       
  1571 	$parsed_args = wp_parse_args( $args, $default_args );
  1481 
  1572 
  1482 	/**
  1573 	/**
  1483 	 * Filters the arguments used to retrieve an image for the edit image form.
  1574 	 * Filters the arguments used to retrieve an image for the edit image form.
  1484 	 *
  1575 	 *
  1485 	 * @since 3.1.0
  1576 	 * @since 3.1.0
  1486 	 *
  1577 	 *
  1487 	 * @see get_media_item
  1578 	 * @see get_media_item
  1488 	 *
  1579 	 *
  1489 	 * @param array $args An array of arguments.
  1580 	 * @param array $parsed_args An array of arguments.
  1490 	 */
  1581 	 */
  1491 	$r = apply_filters( 'get_media_item_args', $args );
  1582 	$parsed_args = apply_filters( 'get_media_item_args', $parsed_args );
  1492 
  1583 
  1493 	$toggle_on  = __( 'Show' );
  1584 	$toggle_on  = __( 'Show' );
  1494 	$toggle_off = __( 'Hide' );
  1585 	$toggle_off = __( 'Hide' );
  1495 
  1586 
  1496 	$file     = get_attached_file( $post->ID );
  1587 	$file     = get_attached_file( $post->ID );
  1500 	$post_mime_types = get_post_mime_types();
  1591 	$post_mime_types = get_post_mime_types();
  1501 	$keys            = array_keys( wp_match_mime_types( array_keys( $post_mime_types ), $post->post_mime_type ) );
  1592 	$keys            = array_keys( wp_match_mime_types( array_keys( $post_mime_types ), $post->post_mime_type ) );
  1502 	$type            = reset( $keys );
  1593 	$type            = reset( $keys );
  1503 	$type_html       = "<input type='hidden' id='type-of-$attachment_id' value='" . esc_attr( $type ) . "' />";
  1594 	$type_html       = "<input type='hidden' id='type-of-$attachment_id' value='" . esc_attr( $type ) . "' />";
  1504 
  1595 
  1505 	$form_fields = get_attachment_fields_to_edit( $post, $r['errors'] );
  1596 	$form_fields = get_attachment_fields_to_edit( $post, $parsed_args['errors'] );
  1506 
  1597 
  1507 	if ( $r['toggle'] ) {
  1598 	if ( $parsed_args['toggle'] ) {
  1508 		$class        = empty( $r['errors'] ) ? 'startclosed' : 'startopen';
  1599 		$class        = empty( $parsed_args['errors'] ) ? 'startclosed' : 'startopen';
  1509 		$toggle_links = "
  1600 		$toggle_links = "
  1510 	<a class='toggle describe-toggle-on' href='#'>$toggle_on</a>
  1601 		<a class='toggle describe-toggle-on' href='#'>$toggle_on</a>
  1511 	<a class='toggle describe-toggle-off' href='#'>$toggle_off</a>";
  1602 		<a class='toggle describe-toggle-off' href='#'>$toggle_off</a>";
  1512 	} else {
  1603 	} else {
  1513 		$class        = '';
  1604 		$class        = '';
  1514 		$toggle_links = '';
  1605 		$toggle_links = '';
  1515 	}
  1606 	}
  1516 
  1607 
  1517 	$display_title = ( ! empty( $title ) ) ? $title : $filename; // $title shouldn't ever be empty, but just in case
  1608 	$display_title = ( ! empty( $title ) ) ? $title : $filename; // $title shouldn't ever be empty, but just in case.
  1518 	$display_title = $r['show_title'] ? "<div class='filename new'><span class='title'>" . wp_html_excerpt( $display_title, 60, '&hellip;' ) . '</span></div>' : '';
  1609 	$display_title = $parsed_args['show_title'] ? "<div class='filename new'><span class='title'>" . wp_html_excerpt( $display_title, 60, '&hellip;' ) . '</span></div>' : '';
  1519 
  1610 
  1520 	$gallery = ( ( isset( $_REQUEST['tab'] ) && 'gallery' == $_REQUEST['tab'] ) || ( isset( $redir_tab ) && 'gallery' == $redir_tab ) );
  1611 	$gallery = ( ( isset( $_REQUEST['tab'] ) && 'gallery' === $_REQUEST['tab'] ) || ( isset( $redir_tab ) && 'gallery' === $redir_tab ) );
  1521 	$order   = '';
  1612 	$order   = '';
  1522 
  1613 
  1523 	foreach ( $form_fields as $key => $val ) {
  1614 	foreach ( $form_fields as $key => $val ) {
  1524 		if ( 'menu_order' == $key ) {
  1615 		if ( 'menu_order' === $key ) {
  1525 			if ( $gallery ) {
  1616 			if ( $gallery ) {
  1526 				$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>";
  1617 				$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>";
  1527 			} else {
  1618 			} else {
  1528 				$order = "<input type='hidden' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ) . "' />";
  1619 				$order = "<input type='hidden' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ) . "' />";
  1529 			}
  1620 			}
  1533 		}
  1624 		}
  1534 	}
  1625 	}
  1535 
  1626 
  1536 	$media_dims = '';
  1627 	$media_dims = '';
  1537 	$meta       = wp_get_attachment_metadata( $post->ID );
  1628 	$meta       = wp_get_attachment_metadata( $post->ID );
       
  1629 
  1538 	if ( isset( $meta['width'], $meta['height'] ) ) {
  1630 	if ( isset( $meta['width'], $meta['height'] ) ) {
  1539 		$media_dims .= "<span id='media-dims-$post->ID'>{$meta['width']}&nbsp;&times;&nbsp;{$meta['height']}</span> ";
  1631 		$media_dims .= "<span id='media-dims-$post->ID'>{$meta['width']}&nbsp;&times;&nbsp;{$meta['height']}</span> ";
  1540 	}
  1632 	}
  1541 
  1633 
  1542 	/**
  1634 	/**
  1548 	 * @param WP_Post $post       The WP_Post attachment object.
  1640 	 * @param WP_Post $post       The WP_Post attachment object.
  1549 	 */
  1641 	 */
  1550 	$media_dims = apply_filters( 'media_meta', $media_dims, $post );
  1642 	$media_dims = apply_filters( 'media_meta', $media_dims, $post );
  1551 
  1643 
  1552 	$image_edit_button = '';
  1644 	$image_edit_button = '';
       
  1645 
  1553 	if ( wp_attachment_is_image( $post->ID ) && wp_image_editor_supports( array( 'mime_type' => $post->post_mime_type ) ) ) {
  1646 	if ( wp_attachment_is_image( $post->ID ) && wp_image_editor_supports( array( 'mime_type' => $post->post_mime_type ) ) ) {
  1554 		$nonce             = wp_create_nonce( "image_editor-$post->ID" );
  1647 		$nonce             = wp_create_nonce( "image_editor-$post->ID" );
  1555 		$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>";
  1648 		$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>";
  1556 	}
  1649 	}
  1557 
  1650 
  1558 	$attachment_url = get_permalink( $attachment_id );
  1651 	$attachment_url = get_permalink( $attachment_id );
  1559 
  1652 
  1560 	$item = "
  1653 	$item = "
  1561 	$type_html
  1654 		$type_html
  1562 	$toggle_links
  1655 		$toggle_links
  1563 	$order
  1656 		$order
  1564 	$display_title
  1657 		$display_title
  1565 	<table class='slidetoggle describe $class'>
  1658 		<table class='slidetoggle describe $class'>
  1566 		<thead class='media-item-info' id='media-head-$post->ID'>
  1659 			<thead class='media-item-info' id='media-head-$post->ID'>
  1567 		<tr>
  1660 			<tr>
  1568 			<td class='A1B1' id='thumbnail-head-$post->ID'>
  1661 			<td class='A1B1' id='thumbnail-head-$post->ID'>
  1569 			<p><a href='$attachment_url' target='_blank'><img class='thumbnail' src='$thumb_url' alt='' /></a></p>
  1662 			<p><a href='$attachment_url' target='_blank'><img class='thumbnail' src='$thumb_url' alt='' /></a></p>
  1570 			<p>$image_edit_button</p>
  1663 			<p>$image_edit_button</p>
  1571 			</td>
  1664 			</td>
  1572 			<td>
  1665 			<td>
  1573 			<p><strong>" . __( 'File name:' ) . "</strong> $filename</p>
  1666 			<p><strong>" . __( 'File name:' ) . "</strong> $filename</p>
  1574 			<p><strong>" . __( 'File type:' ) . "</strong> $post->post_mime_type</p>
  1667 			<p><strong>" . __( 'File type:' ) . "</strong> $post->post_mime_type</p>
  1575 			<p><strong>" . __( 'Upload date:' ) . '</strong> ' . mysql2date( __( 'F j, Y' ), $post->post_date ) . '</p>';
  1668 			<p><strong>" . __( 'Upload date:' ) . '</strong> ' . mysql2date( __( 'F j, Y' ), $post->post_date ) . '</p>';
       
  1669 
  1576 	if ( ! empty( $media_dims ) ) {
  1670 	if ( ! empty( $media_dims ) ) {
  1577 		$item .= '<p><strong>' . __( 'Dimensions:' ) . "</strong> $media_dims</p>\n";
  1671 		$item .= '<p><strong>' . __( 'Dimensions:' ) . "</strong> $media_dims</p>\n";
  1578 	}
  1672 	}
  1579 
  1673 
  1580 			$item .= "</td></tr>\n";
  1674 	$item .= "</td></tr>\n";
  1581 
  1675 
  1582 	$item .= "
  1676 	$item .= "
  1583 		</thead>
  1677 		</thead>
  1584 		<tbody>
  1678 		<tbody>
  1585 		<tr><td colspan='2' class='imgedit-response' id='imgedit-response-$post->ID'></td></tr>\n
  1679 		<tr><td colspan='2' class='imgedit-response' id='imgedit-response-$post->ID'></td></tr>\n
  1586 		<tr><td style='display:none' colspan='2' class='image-editor' id='image-editor-$post->ID'></td></tr>\n
  1680 		<tr><td style='display:none' colspan='2' class='image-editor' id='image-editor-$post->ID'></td></tr>\n
  1587 		<tr><td colspan='2'><p class='media-types media-types-required-info'>" . sprintf( __( 'Required fields are marked %s' ), '<span class="required">*</span>' ) . "</p></td></tr>\n";
  1681 		<tr><td colspan='2'><p class='media-types media-types-required-info'>" .
       
  1682 			/* translators: %s: Asterisk symbol (*). */
       
  1683 			sprintf( __( 'Required fields are marked %s' ), '<span class="required">*</span>' ) .
       
  1684 		"</p></td></tr>\n";
  1588 
  1685 
  1589 	$defaults = array(
  1686 	$defaults = array(
  1590 		'input'      => 'text',
  1687 		'input'      => 'text',
  1591 		'required'   => false,
  1688 		'required'   => false,
  1592 		'value'      => '',
  1689 		'value'      => '',
  1593 		'extra_rows' => array(),
  1690 		'extra_rows' => array(),
  1594 	);
  1691 	);
  1595 
  1692 
  1596 	if ( $r['send'] ) {
  1693 	if ( $parsed_args['send'] ) {
  1597 		$r['send'] = get_submit_button( __( 'Insert into Post' ), '', "send[$attachment_id]", false );
  1694 		$parsed_args['send'] = get_submit_button( __( 'Insert into Post' ), '', "send[$attachment_id]", false );
  1598 	}
  1695 	}
  1599 
  1696 
  1600 	$delete = empty( $r['delete'] ) ? '' : $r['delete'];
  1697 	$delete = empty( $parsed_args['delete'] ) ? '' : $parsed_args['delete'];
  1601 	if ( $delete && current_user_can( 'delete_post', $attachment_id ) ) {
  1698 	if ( $delete && current_user_can( 'delete_post', $attachment_id ) ) {
  1602 		if ( ! EMPTY_TRASH_DAYS ) {
  1699 		if ( ! EMPTY_TRASH_DAYS ) {
  1603 			$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>';
  1700 			$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>';
  1604 		} elseif ( ! MEDIA_TRASH ) {
  1701 		} elseif ( ! MEDIA_TRASH ) {
  1605 			$delete = "<a href='#' class='del-link' onclick=\"document.getElementById('del_attachment_$attachment_id').style.display='block';return false;\">" . __( 'Delete' ) . "</a>
  1702 			$delete = "<a href='#' class='del-link' onclick=\"document.getElementById('del_attachment_$attachment_id').style.display='block';return false;\">" . __( 'Delete' ) . "</a>
  1606 				<div id='del_attachment_$attachment_id' class='del-attachment' style='display:none;'>" .
  1703 				<div id='del_attachment_$attachment_id' class='del-attachment' style='display:none;'>" .
  1607 				/* translators: %s: file name */
  1704 				/* translators: %s: File name. */
  1608 				'<p>' . sprintf( __( 'You are about to delete %s.' ), '<strong>' . $filename . '</strong>' ) . "</p>
  1705 				'<p>' . sprintf( __( 'You are about to delete %s.' ), '<strong>' . $filename . '</strong>' ) . "</p>
  1609 				<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>
  1706 				<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>
  1610 				<a href='#' class='button' onclick=\"this.parentNode.style.display='none';return false;\">" . __( 'Cancel' ) . '</a>
  1707 				<a href='#' class='button' onclick=\"this.parentNode.style.display='none';return false;\">" . __( 'Cancel' ) . '</a>
  1611 				</div>';
  1708 				</div>';
  1612 		} else {
  1709 		} else {
  1617 		$delete = '';
  1714 		$delete = '';
  1618 	}
  1715 	}
  1619 
  1716 
  1620 	$thumbnail       = '';
  1717 	$thumbnail       = '';
  1621 	$calling_post_id = 0;
  1718 	$calling_post_id = 0;
       
  1719 
  1622 	if ( isset( $_GET['post_id'] ) ) {
  1720 	if ( isset( $_GET['post_id'] ) ) {
  1623 		$calling_post_id = absint( $_GET['post_id'] );
  1721 		$calling_post_id = absint( $_GET['post_id'] );
  1624 	} elseif ( isset( $_POST ) && count( $_POST ) ) {// Like for async-upload where $_GET['post_id'] isn't set
  1722 	} elseif ( isset( $_POST ) && count( $_POST ) ) {// Like for async-upload where $_GET['post_id'] isn't set.
  1625 		$calling_post_id = $post->post_parent;
  1723 		$calling_post_id = $post->post_parent;
  1626 	}
  1724 	}
  1627 	if ( 'image' == $type && $calling_post_id && current_theme_supports( 'post-thumbnails', get_post_type( $calling_post_id ) )
  1725 
  1628 		&& post_type_supports( get_post_type( $calling_post_id ), 'thumbnail' ) && get_post_thumbnail_id( $calling_post_id ) != $attachment_id ) {
  1726 	if ( 'image' === $type && $calling_post_id
       
  1727 		&& current_theme_supports( 'post-thumbnails', get_post_type( $calling_post_id ) )
       
  1728 		&& post_type_supports( get_post_type( $calling_post_id ), 'thumbnail' )
       
  1729 		&& get_post_thumbnail_id( $calling_post_id ) != $attachment_id
       
  1730 	) {
  1629 
  1731 
  1630 		$calling_post             = get_post( $calling_post_id );
  1732 		$calling_post             = get_post( $calling_post_id );
  1631 		$calling_post_type_object = get_post_type_object( $calling_post->post_type );
  1733 		$calling_post_type_object = get_post_type_object( $calling_post->post_type );
  1632 
  1734 
  1633 		$ajax_nonce = wp_create_nonce( "set_post_thumbnail-$calling_post_id" );
  1735 		$ajax_nonce = wp_create_nonce( "set_post_thumbnail-$calling_post_id" );
  1634 		$thumbnail  = "<a class='wp-post-thumbnail' id='wp-post-thumbnail-" . $attachment_id . "' href='#' onclick='WPSetAsThumbnail(\"$attachment_id\", \"$ajax_nonce\");return false;'>" . esc_html( $calling_post_type_object->labels->use_featured_image ) . '</a>';
  1736 		$thumbnail  = "<a class='wp-post-thumbnail' id='wp-post-thumbnail-" . $attachment_id . "' href='#' onclick='WPSetAsThumbnail(\"$attachment_id\", \"$ajax_nonce\");return false;'>" . esc_html( $calling_post_type_object->labels->use_featured_image ) . '</a>';
  1635 	}
  1737 	}
  1636 
  1738 
  1637 	if ( ( $r['send'] || $thumbnail || $delete ) && ! isset( $form_fields['buttons'] ) ) {
  1739 	if ( ( $parsed_args['send'] || $thumbnail || $delete ) && ! isset( $form_fields['buttons'] ) ) {
  1638 		$form_fields['buttons'] = array( 'tr' => "\t\t<tr class='submit'><td></td><td class='savesend'>" . $r['send'] . " $thumbnail $delete</td></tr>\n" );
  1740 		$form_fields['buttons'] = array( 'tr' => "\t\t<tr class='submit'><td></td><td class='savesend'>" . $parsed_args['send'] . " $thumbnail $delete</td></tr>\n" );
  1639 	}
  1741 	}
       
  1742 
  1640 	$hidden_fields = array();
  1743 	$hidden_fields = array();
  1641 
  1744 
  1642 	foreach ( $form_fields as $id => $field ) {
  1745 	foreach ( $form_fields as $id => $field ) {
  1643 		if ( $id[0] == '_' ) {
  1746 		if ( '_' === $id[0] ) {
  1644 			continue;
  1747 			continue;
  1645 		}
  1748 		}
  1646 
  1749 
  1647 		if ( ! empty( $field['tr'] ) ) {
  1750 		if ( ! empty( $field['tr'] ) ) {
  1648 			$item .= $field['tr'];
  1751 			$item .= $field['tr'];
  1650 		}
  1753 		}
  1651 
  1754 
  1652 		$field = array_merge( $defaults, $field );
  1755 		$field = array_merge( $defaults, $field );
  1653 		$name  = "attachments[$attachment_id][$id]";
  1756 		$name  = "attachments[$attachment_id][$id]";
  1654 
  1757 
  1655 		if ( $field['input'] == 'hidden' ) {
  1758 		if ( 'hidden' === $field['input'] ) {
  1656 			$hidden_fields[ $name ] = $field['value'];
  1759 			$hidden_fields[ $name ] = $field['value'];
  1657 			continue;
  1760 			continue;
  1658 		}
  1761 		}
  1659 
  1762 
  1660 		$required      = $field['required'] ? '<span class="required">*</span>' : '';
  1763 		$required      = $field['required'] ? '<span class="required">*</span>' : '';
  1661 		$required_attr = $field['required'] ? ' required' : '';
  1764 		$required_attr = $field['required'] ? ' required' : '';
  1662 		$class         = $id;
  1765 		$class         = $id;
  1663 		$class        .= $field['required'] ? ' form-required' : '';
  1766 		$class        .= $field['required'] ? ' form-required' : '';
  1664 
  1767 
  1665 		$item .= "\t\t<tr class='$class'>\n\t\t\t<th scope='row' class='label'><label for='$name'><span class='alignleft'>{$field['label']}{$required}</span><br class='clear' /></label></th>\n\t\t\t<td class='field'>";
  1768 		$item .= "\t\t<tr class='$class'>\n\t\t\t<th scope='row' class='label'><label for='$name'><span class='alignleft'>{$field['label']}{$required}</span><br class='clear' /></label></th>\n\t\t\t<td class='field'>";
       
  1769 
  1666 		if ( ! empty( $field[ $field['input'] ] ) ) {
  1770 		if ( ! empty( $field[ $field['input'] ] ) ) {
  1667 			$item .= $field[ $field['input'] ];
  1771 			$item .= $field[ $field['input'] ];
  1668 		} elseif ( $field['input'] == 'textarea' ) {
  1772 		} elseif ( 'textarea' === $field['input'] ) {
  1669 			if ( 'post_content' == $id && user_can_richedit() ) {
  1773 			if ( 'post_content' === $id && user_can_richedit() ) {
  1670 				// Sanitize_post() skips the post_content when user_can_richedit.
  1774 				// Sanitize_post() skips the post_content when user_can_richedit.
  1671 				$field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES );
  1775 				$field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES );
  1672 			}
  1776 			}
  1673 			// Post_excerpt is already escaped by sanitize_post() in get_attachment_fields_to_edit().
  1777 			// Post_excerpt is already escaped by sanitize_post() in get_attachment_fields_to_edit().
  1674 			$item .= "<textarea id='$name' name='$name'{$required_attr}>" . $field['value'] . '</textarea>';
  1778 			$item .= "<textarea id='$name' name='$name'{$required_attr}>" . $field['value'] . '</textarea>';
  1675 		} else {
  1779 		} else {
  1676 			$item .= "<input type='text' class='text' id='$name' name='$name' value='" . esc_attr( $field['value'] ) . "'{$required_attr} />";
  1780 			$item .= "<input type='text' class='text' id='$name' name='$name' value='" . esc_attr( $field['value'] ) . "'{$required_attr} />";
  1677 		}
  1781 		}
       
  1782 
  1678 		if ( ! empty( $field['helps'] ) ) {
  1783 		if ( ! empty( $field['helps'] ) ) {
  1679 			$item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>';
  1784 			$item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>';
  1680 		}
  1785 		}
  1681 		$item .= "</td>\n\t\t</tr>\n";
  1786 		$item .= "</td>\n\t\t</tr>\n";
  1682 
  1787 
  1704 	}
  1809 	}
  1705 
  1810 
  1706 	if ( ! empty( $form_fields['_final'] ) ) {
  1811 	if ( ! empty( $form_fields['_final'] ) ) {
  1707 		$item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
  1812 		$item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
  1708 	}
  1813 	}
       
  1814 
  1709 	$item .= "\t</tbody>\n";
  1815 	$item .= "\t</tbody>\n";
  1710 	$item .= "\t</table>\n";
  1816 	$item .= "\t</table>\n";
  1711 
  1817 
  1712 	foreach ( $hidden_fields as $name => $value ) {
  1818 	foreach ( $hidden_fields as $name => $value ) {
  1713 		$item .= "\t<input type='hidden' name='$name' id='$name' value='" . esc_attr( $value ) . "' />\n";
  1819 		$item .= "\t<input type='hidden' name='$name' id='$name' value='" . esc_attr( $value ) . "' />\n";
  1747 	$form_fields = array();
  1853 	$form_fields = array();
  1748 
  1854 
  1749 	if ( $args['in_modal'] ) {
  1855 	if ( $args['in_modal'] ) {
  1750 		foreach ( get_attachment_taxonomies( $post ) as $taxonomy ) {
  1856 		foreach ( get_attachment_taxonomies( $post ) as $taxonomy ) {
  1751 			$t = (array) get_taxonomy( $taxonomy );
  1857 			$t = (array) get_taxonomy( $taxonomy );
       
  1858 
  1752 			if ( ! $t['public'] || ! $t['show_ui'] ) {
  1859 			if ( ! $t['public'] || ! $t['show_ui'] ) {
  1753 				continue;
  1860 				continue;
  1754 			}
  1861 			}
       
  1862 
  1755 			if ( empty( $t['label'] ) ) {
  1863 			if ( empty( $t['label'] ) ) {
  1756 				$t['label'] = $taxonomy;
  1864 				$t['label'] = $taxonomy;
  1757 			}
  1865 			}
       
  1866 
  1758 			if ( empty( $t['args'] ) ) {
  1867 			if ( empty( $t['args'] ) ) {
  1759 				$t['args'] = array();
  1868 				$t['args'] = array();
  1760 			}
  1869 			}
  1761 
  1870 
  1762 			$terms = get_object_term_cache( $post->ID, $taxonomy );
  1871 			$terms = get_object_term_cache( $post->ID, $taxonomy );
       
  1872 
  1763 			if ( false === $terms ) {
  1873 			if ( false === $terms ) {
  1764 				$terms = wp_get_object_terms( $post->ID, $taxonomy, $t['args'] );
  1874 				$terms = wp_get_object_terms( $post->ID, $taxonomy, $t['args'] );
  1765 			}
  1875 			}
  1766 
  1876 
  1767 			$values = array();
  1877 			$values = array();
  1768 
  1878 
  1769 			foreach ( $terms as $term ) {
  1879 			foreach ( $terms as $term ) {
  1770 				$values[] = $term->slug;
  1880 				$values[] = $term->slug;
  1771 			}
  1881 			}
       
  1882 
  1772 			$t['value']    = join( ', ', $values );
  1883 			$t['value']    = join( ', ', $values );
  1773 			$t['taxonomy'] = true;
  1884 			$t['taxonomy'] = true;
  1774 
  1885 
  1775 			$form_fields[ $taxonomy ] = $t;
  1886 			$form_fields[ $taxonomy ] = $t;
  1776 		}
  1887 		}
  1777 	}
  1888 	}
  1778 
  1889 
  1779 	// Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default
  1890 	/*
  1780 	// The recursive merge is easily traversed with array casting: foreach ( (array) $things as $thing )
  1891 	 * Merge default fields with their errors, so any key passed with the error
       
  1892 	 * (e.g. 'error', 'helps', 'value') will replace the default.
       
  1893 	 * The recursive merge is easily traversed with array casting:
       
  1894 	 * foreach ( (array) $things as $thing )
       
  1895 	 */
  1781 	$form_fields = array_merge_recursive( $form_fields, (array) $args['errors'] );
  1896 	$form_fields = array_merge_recursive( $form_fields, (array) $args['errors'] );
  1782 
  1897 
  1783 	/** This filter is documented in wp-admin/includes/media.php */
  1898 	/** This filter is documented in wp-admin/includes/media.php */
  1784 	$form_fields = apply_filters( 'attachment_fields_to_edit', $form_fields, $post );
  1899 	$form_fields = apply_filters( 'attachment_fields_to_edit', $form_fields, $post );
  1785 
  1900 
  1808 	);
  1923 	);
  1809 
  1924 
  1810 	$hidden_fields = array();
  1925 	$hidden_fields = array();
  1811 
  1926 
  1812 	$item = '';
  1927 	$item = '';
       
  1928 
  1813 	foreach ( $form_fields as $id => $field ) {
  1929 	foreach ( $form_fields as $id => $field ) {
  1814 		if ( $id[0] == '_' ) {
  1930 		if ( '_' === $id[0] ) {
  1815 			continue;
  1931 			continue;
  1816 		}
  1932 		}
  1817 
  1933 
  1818 		$name    = "attachments[$attachment_id][$id]";
  1934 		$name    = "attachments[$attachment_id][$id]";
  1819 		$id_attr = "attachments-$attachment_id-$id";
  1935 		$id_attr = "attachments-$attachment_id-$id";
  1827 
  1943 
  1828 		if ( ( ! $field['show_in_edit'] && ! $args['in_modal'] ) || ( ! $field['show_in_modal'] && $args['in_modal'] ) ) {
  1944 		if ( ( ! $field['show_in_edit'] && ! $args['in_modal'] ) || ( ! $field['show_in_modal'] && $args['in_modal'] ) ) {
  1829 			continue;
  1945 			continue;
  1830 		}
  1946 		}
  1831 
  1947 
  1832 		if ( $field['input'] == 'hidden' ) {
  1948 		if ( 'hidden' === $field['input'] ) {
  1833 			$hidden_fields[ $name ] = $field['value'];
  1949 			$hidden_fields[ $name ] = $field['value'];
  1834 			continue;
  1950 			continue;
  1835 		}
  1951 		}
  1836 
  1952 
  1837 		$readonly      = ! $user_can_edit && ! empty( $field['taxonomy'] ) ? " readonly='readonly' " : '';
  1953 		$readonly      = ! $user_can_edit && ! empty( $field['taxonomy'] ) ? " readonly='readonly' " : '';
  1844 		$item .= "\t\t\t<th scope='row' class='label'><label for='$id_attr'><span class='alignleft'>{$field['label']}</span>$required<br class='clear' /></label>";
  1960 		$item .= "\t\t\t<th scope='row' class='label'><label for='$id_attr'><span class='alignleft'>{$field['label']}</span>$required<br class='clear' /></label>";
  1845 		$item .= "</th>\n\t\t\t<td class='field'>";
  1961 		$item .= "</th>\n\t\t\t<td class='field'>";
  1846 
  1962 
  1847 		if ( ! empty( $field[ $field['input'] ] ) ) {
  1963 		if ( ! empty( $field[ $field['input'] ] ) ) {
  1848 			$item .= $field[ $field['input'] ];
  1964 			$item .= $field[ $field['input'] ];
  1849 		} elseif ( $field['input'] == 'textarea' ) {
  1965 		} elseif ( 'textarea' === $field['input'] ) {
  1850 			if ( 'post_content' == $id && user_can_richedit() ) {
  1966 			if ( 'post_content' === $id && user_can_richedit() ) {
  1851 				// sanitize_post() skips the post_content when user_can_richedit.
  1967 				// sanitize_post() skips the post_content when user_can_richedit.
  1852 				$field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES );
  1968 				$field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES );
  1853 			}
  1969 			}
  1854 			$item .= "<textarea id='$id_attr' name='$name'{$required_attr}>" . $field['value'] . '</textarea>';
  1970 			$item .= "<textarea id='$id_attr' name='$name'{$required_attr}>" . $field['value'] . '</textarea>';
  1855 		} else {
  1971 		} else {
  1856 			$item .= "<input type='text' class='text' id='$id_attr' name='$name' value='" . esc_attr( $field['value'] ) . "' $readonly{$required_attr} />";
  1972 			$item .= "<input type='text' class='text' id='$id_attr' name='$name' value='" . esc_attr( $field['value'] ) . "' $readonly{$required_attr} />";
  1857 		}
  1973 		}
       
  1974 
  1858 		if ( ! empty( $field['helps'] ) ) {
  1975 		if ( ! empty( $field['helps'] ) ) {
  1859 			$item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>';
  1976 			$item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>';
  1860 		}
  1977 		}
       
  1978 
  1861 		$item .= "</td>\n\t\t</tr>\n";
  1979 		$item .= "</td>\n\t\t</tr>\n";
  1862 
  1980 
  1863 		$extra_rows = array();
  1981 		$extra_rows = array();
  1864 
  1982 
  1865 		if ( ! empty( $field['errors'] ) ) {
  1983 		if ( ! empty( $field['errors'] ) ) {
  1887 		$item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
  2005 		$item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
  1888 	}
  2006 	}
  1889 
  2007 
  1890 	if ( $item ) {
  2008 	if ( $item ) {
  1891 		$item = '<p class="media-types media-types-required-info">' .
  2009 		$item = '<p class="media-types media-types-required-info">' .
  1892 			sprintf( __( 'Required fields are marked %s' ), '<span class="required">*</span>' ) . '</p>
  2010 			/* translators: %s: Asterisk symbol (*). */
  1893 			<table class="compat-attachment-fields">' . $item . '</table>';
  2011 			sprintf( __( 'Required fields are marked %s' ), '<span class="required">*</span>' ) .
       
  2012 			'</p>' .
       
  2013 			'<table class="compat-attachment-fields">' . $item . '</table>';
  1894 	}
  2014 	}
  1895 
  2015 
  1896 	foreach ( $hidden_fields as $hidden_field => $value ) {
  2016 	foreach ( $hidden_fields as $hidden_field => $value ) {
  1897 		$item .= '<input type="hidden" name="' . esc_attr( $hidden_field ) . '" value="' . esc_attr( $value ) . '" />' . "\n";
  2017 		$item .= '<input type="hidden" name="' . esc_attr( $hidden_field ) . '" value="' . esc_attr( $value ) . '" />' . "\n";
  1898 	}
  2018 	}
  1914  */
  2034  */
  1915 function media_upload_header() {
  2035 function media_upload_header() {
  1916 	$post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
  2036 	$post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
  1917 
  2037 
  1918 	echo '<script type="text/javascript">post_id = ' . $post_id . ';</script>';
  2038 	echo '<script type="text/javascript">post_id = ' . $post_id . ';</script>';
       
  2039 
  1919 	if ( empty( $_GET['chromeless'] ) ) {
  2040 	if ( empty( $_GET['chromeless'] ) ) {
  1920 		echo '<div id="media-upload-header">';
  2041 		echo '<div id="media-upload-header">';
  1921 		the_media_upload_tabs();
  2042 		the_media_upload_tabs();
  1922 		echo '</div>';
  2043 		echo '</div>';
  1923 	}
  2044 	}
  1937  */
  2058  */
  1938 function media_upload_form( $errors = null ) {
  2059 function media_upload_form( $errors = null ) {
  1939 	global $type, $tab, $is_IE, $is_opera;
  2060 	global $type, $tab, $is_IE, $is_opera;
  1940 
  2061 
  1941 	if ( ! _device_can_upload() ) {
  2062 	if ( ! _device_can_upload() ) {
  1942 		echo '<p>' . sprintf( __( 'The web browser on your device cannot be used to upload files. You may be able to use the <a href="%s">native app for your device</a> instead.' ), 'https://apps.wordpress.org/' ) . '</p>';
  2063 		echo '<p>' . sprintf(
       
  2064 			/* translators: %s: https://apps.wordpress.org/ */
       
  2065 			__( '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.' ),
       
  2066 			'https://apps.wordpress.org/'
       
  2067 		) . '</p>';
  1943 		return;
  2068 		return;
  1944 	}
  2069 	}
  1945 
  2070 
  1946 	$upload_action_url = admin_url( 'async-upload.php' );
  2071 	$upload_action_url = admin_url( 'async-upload.php' );
  1947 	$post_id           = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
  2072 	$post_id           = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
  1950 
  2075 
  1951 	$max_upload_size = wp_max_upload_size();
  2076 	$max_upload_size = wp_max_upload_size();
  1952 	if ( ! $max_upload_size ) {
  2077 	if ( ! $max_upload_size ) {
  1953 		$max_upload_size = 0;
  2078 		$max_upload_size = 0;
  1954 	}
  2079 	}
       
  2080 
  1955 	?>
  2081 	?>
  1956 
  2082 	<div id="media-upload-notice">
  1957 <div id="media-upload-notice">
       
  1958 	<?php
  2083 	<?php
  1959 
  2084 
  1960 	if ( isset( $errors['upload_notice'] ) ) {
  2085 	if ( isset( $errors['upload_notice'] ) ) {
  1961 		echo $errors['upload_notice'];
  2086 		echo $errors['upload_notice'];
  1962 	}
  2087 	}
  1963 
  2088 
  1964 	?>
  2089 	?>
  1965 </div>
  2090 	</div>
  1966 <div id="media-upload-error">
  2091 	<div id="media-upload-error">
  1967 	<?php
  2092 	<?php
  1968 
  2093 
  1969 	if ( isset( $errors['upload_error'] ) && is_wp_error( $errors['upload_error'] ) ) {
  2094 	if ( isset( $errors['upload_error'] ) && is_wp_error( $errors['upload_error'] ) ) {
  1970 		echo $errors['upload_error']->get_error_message();
  2095 		echo $errors['upload_error']->get_error_message();
  1971 	}
  2096 	}
  1972 
  2097 
  1973 	?>
  2098 	?>
  1974 </div>
  2099 	</div>
  1975 	<?php
  2100 	<?php
       
  2101 
  1976 	if ( is_multisite() && ! is_upload_space_available() ) {
  2102 	if ( is_multisite() && ! is_upload_space_available() ) {
  1977 		/**
  2103 		/**
  1978 		 * Fires when an upload will exceed the defined upload space quota for a network site.
  2104 		 * Fires when an upload will exceed the defined upload space quota for a network site.
  1979 		 *
  2105 		 *
  1980 		 * @since 3.5.0
  2106 		 * @since 3.5.0
  1986 	/**
  2112 	/**
  1987 	 * Fires just before the legacy (pre-3.5.0) upload interface is loaded.
  2113 	 * Fires just before the legacy (pre-3.5.0) upload interface is loaded.
  1988 	 *
  2114 	 *
  1989 	 * @since 2.6.0
  2115 	 * @since 2.6.0
  1990 	 */
  2116 	 */
  1991 	do_action( 'pre-upload-ui' );
  2117 	do_action( 'pre-upload-ui' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  1992 
  2118 
  1993 	$post_params = array(
  2119 	$post_params = array(
  1994 		'post_id'  => $post_id,
  2120 		'post_id'  => $post_id,
  1995 		'_wpnonce' => wp_create_nonce( 'media-form' ),
  2121 		'_wpnonce' => wp_create_nonce( 'media-form' ),
  1996 		'type'     => $_type,
  2122 		'type'     => $_type,
  2016 		'browse_button'    => 'plupload-browse-button',
  2142 		'browse_button'    => 'plupload-browse-button',
  2017 		'container'        => 'plupload-upload-ui',
  2143 		'container'        => 'plupload-upload-ui',
  2018 		'drop_element'     => 'drag-drop-area',
  2144 		'drop_element'     => 'drag-drop-area',
  2019 		'file_data_name'   => 'async-upload',
  2145 		'file_data_name'   => 'async-upload',
  2020 		'url'              => $upload_action_url,
  2146 		'url'              => $upload_action_url,
  2021 		'filters'          => array(
  2147 		'filters'          => array( 'max_file_size' => $max_upload_size . 'b' ),
  2022 			'max_file_size' => $max_upload_size . 'b',
       
  2023 		),
       
  2024 		'multipart_params' => $post_params,
  2148 		'multipart_params' => $post_params,
  2025 	);
  2149 	);
  2026 
  2150 
  2027 	// Currently only iOS Safari supports multiple files uploading but iOS 7.x has a bug that prevents uploading of videos
  2151 	/*
  2028 	// when enabled. See #29602.
  2152 	 * Currently only iOS Safari supports multiple files uploading,
  2029 	if ( wp_is_mobile() && strpos( $_SERVER['HTTP_USER_AGENT'], 'OS 7_' ) !== false &&
  2153 	 * but iOS 7.x has a bug that prevents uploading of videos when enabled.
  2030 	strpos( $_SERVER['HTTP_USER_AGENT'], 'like Mac OS X' ) !== false ) {
  2154 	 * See #29602.
  2031 
  2155 	 */
       
  2156 	if (
       
  2157 		wp_is_mobile() &&
       
  2158 		strpos( $_SERVER['HTTP_USER_AGENT'], 'OS 7_' ) !== false &&
       
  2159 		strpos( $_SERVER['HTTP_USER_AGENT'], 'like Mac OS X' ) !== false
       
  2160 	) {
  2032 		$plupload_init['multi_selection'] = false;
  2161 		$plupload_init['multi_selection'] = false;
  2033 	}
  2162 	}
  2034 
  2163 
  2035 	/**
  2164 	/**
  2036 	 * Filters the default Plupload settings.
  2165 	 * Filters the default Plupload settings.
  2040 	 * @param array $plupload_init An array of default settings used by Plupload.
  2169 	 * @param array $plupload_init An array of default settings used by Plupload.
  2041 	 */
  2170 	 */
  2042 	$plupload_init = apply_filters( 'plupload_init', $plupload_init );
  2171 	$plupload_init = apply_filters( 'plupload_init', $plupload_init );
  2043 
  2172 
  2044 	?>
  2173 	?>
  2045 
  2174 	<script type="text/javascript">
  2046 <script type="text/javascript">
       
  2047 	<?php
  2175 	<?php
  2048 	// Verify size is an int. If not return default value.
  2176 	// Verify size is an int. If not return default value.
  2049 	$large_size_h = absint( get_option( 'large_size_h' ) );
  2177 	$large_size_h = absint( get_option( 'large_size_h' ) );
       
  2178 
  2050 	if ( ! $large_size_h ) {
  2179 	if ( ! $large_size_h ) {
  2051 		$large_size_h = 1024;
  2180 		$large_size_h = 1024;
  2052 	}
  2181 	}
       
  2182 
  2053 	$large_size_w = absint( get_option( 'large_size_w' ) );
  2183 	$large_size_w = absint( get_option( 'large_size_w' ) );
       
  2184 
  2054 	if ( ! $large_size_w ) {
  2185 	if ( ! $large_size_w ) {
  2055 		$large_size_w = 1024;
  2186 		$large_size_w = 1024;
  2056 	}
  2187 	}
       
  2188 
  2057 	?>
  2189 	?>
  2058 var resize_height = <?php echo $large_size_h; ?>, resize_width = <?php echo $large_size_w; ?>,
  2190 	var resize_height = <?php echo $large_size_h; ?>, resize_width = <?php echo $large_size_w; ?>,
  2059 wpUploaderInit = <?php echo wp_json_encode( $plupload_init ); ?>;
  2191 	wpUploaderInit = <?php echo wp_json_encode( $plupload_init ); ?>;
  2060 </script>
  2192 	</script>
  2061 
  2193 
  2062 <div id="plupload-upload-ui" class="hide-if-no-js">
  2194 	<div id="plupload-upload-ui" class="hide-if-no-js">
  2063 	<?php
  2195 	<?php
  2064 	/**
  2196 	/**
  2065 	 * Fires before the upload interface loads.
  2197 	 * Fires before the upload interface loads.
  2066 	 *
  2198 	 *
  2067 	 * @since 2.6.0 As 'pre-flash-upload-ui'
  2199 	 * @since 2.6.0 As 'pre-flash-upload-ui'
  2068 	 * @since 3.3.0
  2200 	 * @since 3.3.0
  2069 	 */
  2201 	 */
  2070 	do_action( 'pre-plupload-upload-ui' );
  2202 	do_action( 'pre-plupload-upload-ui' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
       
  2203 
  2071 	?>
  2204 	?>
  2072 <div id="drag-drop-area">
  2205 	<div id="drag-drop-area">
  2073 	<div class="drag-drop-inside">
  2206 		<div class="drag-drop-inside">
  2074 	<p class="drag-drop-info"><?php _e( 'Drop files here' ); ?></p>
  2207 		<p class="drag-drop-info"><?php _e( 'Drop files to upload' ); ?></p>
  2075 	<p><?php _ex( 'or', 'Uploader: Drop files here - or - Select Files' ); ?></p>
  2208 		<p><?php _ex( 'or', 'Uploader: Drop files here - or - Select Files' ); ?></p>
  2076 	<p class="drag-drop-buttons"><input id="plupload-browse-button" type="button" value="<?php esc_attr_e( 'Select Files' ); ?>" class="button" /></p>
  2209 		<p class="drag-drop-buttons"><input id="plupload-browse-button" type="button" value="<?php esc_attr_e( 'Select Files' ); ?>" class="button" /></p>
       
  2210 		</div>
  2077 	</div>
  2211 	</div>
  2078 </div>
       
  2079 	<?php
  2212 	<?php
  2080 	/**
  2213 	/**
  2081 	 * Fires after the upload interface loads.
  2214 	 * Fires after the upload interface loads.
  2082 	 *
  2215 	 *
  2083 	 * @since 2.6.0 As 'post-flash-upload-ui'
  2216 	 * @since 2.6.0 As 'post-flash-upload-ui'
  2084 	 * @since 3.3.0
  2217 	 * @since 3.3.0
  2085 	 */
  2218 	 */
  2086 	do_action( 'post-plupload-upload-ui' );
  2219 	do_action( 'post-plupload-upload-ui' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  2087 	?>
  2220 	?>
  2088 </div>
  2221 	</div>
  2089 
  2222 
  2090 <div id="html-upload-ui" class="hide-if-js">
  2223 	<div id="html-upload-ui" class="hide-if-js">
  2091 	<?php
  2224 	<?php
  2092 	/**
  2225 	/**
  2093 	 * Fires before the upload button in the media upload interface.
  2226 	 * Fires before the upload button in the media upload interface.
  2094 	 *
  2227 	 *
  2095 	 * @since 2.6.0
  2228 	 * @since 2.6.0
  2096 	 */
  2229 	 */
  2097 	do_action( 'pre-html-upload-ui' );
  2230 	do_action( 'pre-html-upload-ui' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
       
  2231 
  2098 	?>
  2232 	?>
  2099 	<p id="async-upload-wrap">
  2233 	<p id="async-upload-wrap">
  2100 		<label class="screen-reader-text" for="async-upload"><?php _e( 'Upload' ); ?></label>
  2234 		<label class="screen-reader-text" for="async-upload"><?php _e( 'Upload' ); ?></label>
  2101 		<input type="file" name="async-upload" id="async-upload" />
  2235 		<input type="file" name="async-upload" id="async-upload" />
  2102 		<?php submit_button( __( 'Upload' ), 'primary', 'html-upload', false ); ?>
  2236 		<?php submit_button( __( 'Upload' ), 'primary', 'html-upload', false ); ?>
  2107 	/**
  2241 	/**
  2108 	 * Fires after the upload button in the media upload interface.
  2242 	 * Fires after the upload button in the media upload interface.
  2109 	 *
  2243 	 *
  2110 	 * @since 2.6.0
  2244 	 * @since 2.6.0
  2111 	 */
  2245 	 */
  2112 	do_action( 'post-html-upload-ui' );
  2246 	do_action( 'post-html-upload-ui' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
       
  2247 
  2113 	?>
  2248 	?>
  2114 </div>
  2249 	</div>
  2115 
  2250 
  2116 <p class="max-upload-size"><?php printf( __( 'Maximum upload file size: %s.' ), esc_html( size_format( $max_upload_size ) ) ); ?></p>
  2251 <p class="max-upload-size">
       
  2252 	<?php
       
  2253 	/* translators: %s: Maximum allowed file size. */
       
  2254 	printf( __( 'Maximum upload file size: %s.' ), esc_html( size_format( $max_upload_size ) ) );
       
  2255 	?>
       
  2256 </p>
  2117 	<?php
  2257 	<?php
  2118 
  2258 
  2119 	/**
  2259 	/**
  2120 	 * Fires on the post upload UI screen.
  2260 	 * Fires on the post upload UI screen.
  2121 	 *
  2261 	 *
  2122 	 * Legacy (pre-3.5.0) media workflow hook.
  2262 	 * Legacy (pre-3.5.0) media workflow hook.
  2123 	 *
  2263 	 *
  2124 	 * @since 2.6.0
  2264 	 * @since 2.6.0
  2125 	 */
  2265 	 */
  2126 	do_action( 'post-upload-ui' );
  2266 	do_action( 'post-upload-ui' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  2127 }
  2267 }
  2128 
  2268 
  2129 /**
  2269 /**
  2130  * Outputs the legacy media upload form for a given media type.
  2270  * Outputs the legacy media upload form for a given media type.
  2131  *
  2271  *
  2132  * @since 2.5.0
  2272  * @since 2.5.0
  2133  *
  2273  *
  2134  * @param string $type
  2274  * @param string  $type
  2135  * @param object $errors
  2275  * @param object  $errors
  2136  * @param integer $id
  2276  * @param integer $id
  2137  */
  2277  */
  2138 function media_upload_type_form( $type = 'file', $errors = null, $id = null ) {
  2278 function media_upload_type_form( $type = 'file', $errors = null, $id = null ) {
  2139 
  2279 
  2140 	media_upload_header();
  2280 	media_upload_header();
  2155 	$form_class      = 'media-upload-form type-form validate';
  2295 	$form_class      = 'media-upload-form type-form validate';
  2156 
  2296 
  2157 	if ( get_user_setting( 'uploader' ) ) {
  2297 	if ( get_user_setting( 'uploader' ) ) {
  2158 		$form_class .= ' html-uploader';
  2298 		$form_class .= ' html-uploader';
  2159 	}
  2299 	}
       
  2300 
  2160 	?>
  2301 	?>
  2161 
  2302 	<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">
  2162 <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">
  2303 		<?php submit_button( '', 'hidden', 'save', false ); ?>
  2163 	<?php submit_button( '', 'hidden', 'save', false ); ?>
  2304 	<input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  2164 <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  2305 		<?php wp_nonce_field( 'media-form' ); ?>
  2165 	<?php wp_nonce_field( 'media-form' ); ?>
  2306 
  2166 
  2307 	<h3 class="media-title"><?php _e( 'Add media files from your computer' ); ?></h3>
  2167 <h3 class="media-title"><?php _e( 'Add media files from your computer' ); ?></h3>
       
  2168 
  2308 
  2169 	<?php media_upload_form( $errors ); ?>
  2309 	<?php media_upload_form( $errors ); ?>
  2170 
  2310 
  2171 <script type="text/javascript">
  2311 	<script type="text/javascript">
  2172 jQuery(function($){
  2312 	jQuery(function($){
  2173 	var preloaded = $(".media-item.preloaded");
  2313 		var preloaded = $(".media-item.preloaded");
  2174 	if ( preloaded.length > 0 ) {
  2314 		if ( preloaded.length > 0 ) {
  2175 		preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
  2315 			preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
  2176 	}
  2316 		}
  2177 	updateMediaForm();
  2317 		updateMediaForm();
  2178 });
  2318 	});
  2179 </script>
  2319 	</script>
  2180 <div id="media-items">
  2320 	<div id="media-items">
  2181 	<?php
  2321 	<?php
  2182 
  2322 
  2183 	if ( $id ) {
  2323 	if ( $id ) {
  2184 		if ( ! is_wp_error( $id ) ) {
  2324 		if ( ! is_wp_error( $id ) ) {
  2185 			add_filter( 'attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2 );
  2325 			add_filter( 'attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2 );
  2187 		} else {
  2327 		} else {
  2188 			echo '<div id="media-upload-error">' . esc_html( $id->get_error_message() ) . '</div></div>';
  2328 			echo '<div id="media-upload-error">' . esc_html( $id->get_error_message() ) . '</div></div>';
  2189 			exit;
  2329 			exit;
  2190 		}
  2330 		}
  2191 	}
  2331 	}
       
  2332 
  2192 	?>
  2333 	?>
  2193 </div>
  2334 	</div>
  2194 
  2335 
  2195 <p class="savebutton ml-submit">
  2336 	<p class="savebutton ml-submit">
  2196 	<?php submit_button( __( 'Save all changes' ), '', 'save', false ); ?>
  2337 		<?php submit_button( __( 'Save all changes' ), '', 'save', false ); ?>
  2197 </p>
  2338 	</p>
  2198 </form>
  2339 	</form>
  2199 	<?php
  2340 	<?php
  2200 }
  2341 }
  2201 
  2342 
  2202 /**
  2343 /**
  2203  * Outputs the legacy media upload form for external media.
  2344  * Outputs the legacy media upload form for external media.
  2204  *
  2345  *
  2205  * @since 2.7.0
  2346  * @since 2.7.0
  2206  *
  2347  *
  2207  * @param string $type
  2348  * @param string  $type
  2208  * @param object $errors
  2349  * @param object  $errors
  2209  * @param integer $id
  2350  * @param integer $id
  2210  */
  2351  */
  2211 function media_upload_type_url_form( $type = null, $errors = null, $id = null ) {
  2352 function media_upload_type_url_form( $type = null, $errors = null, $id = null ) {
  2212 	if ( null === $type ) {
  2353 	if ( null === $type ) {
  2213 		$type = 'image';
  2354 		$type = 'image';
  2223 	$form_class      = 'media-upload-form type-form validate';
  2364 	$form_class      = 'media-upload-form type-form validate';
  2224 
  2365 
  2225 	if ( get_user_setting( 'uploader' ) ) {
  2366 	if ( get_user_setting( 'uploader' ) ) {
  2226 		$form_class .= ' html-uploader';
  2367 		$form_class .= ' html-uploader';
  2227 	}
  2368 	}
       
  2369 
  2228 	?>
  2370 	?>
  2229 
  2371 	<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">
  2230 <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">
  2372 	<input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  2231 <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  2373 		<?php wp_nonce_field( 'media-form' ); ?>
  2232 	<?php wp_nonce_field( 'media-form' ); ?>
  2374 
  2233 
  2375 	<h3 class="media-title"><?php _e( 'Insert media from another website' ); ?></h3>
  2234 <h3 class="media-title"><?php _e( 'Insert media from another website' ); ?></h3>
  2376 
  2235 
  2377 	<script type="text/javascript">
  2236 <script type="text/javascript">
  2378 	var addExtImage = {
  2237 var addExtImage = {
       
  2238 
  2379 
  2239 	width : '',
  2380 	width : '',
  2240 	height : '',
  2381 	height : '',
  2241 	align : 'alignnone',
  2382 	align : 'alignnone',
  2242 
  2383 
  2243 	insert : function() {
  2384 	insert : function() {
  2244 		var t = this, html, f = document.forms[0], cls, title = '', alt = '', caption = '';
  2385 		var t = this, html, f = document.forms[0], cls, title = '', alt = '', caption = '';
  2245 
  2386 
  2246 		if ( '' == f.src.value || '' == t.width )
  2387 		if ( '' === f.src.value || '' === t.width )
  2247 			return false;
  2388 			return false;
  2248 
  2389 
  2249 		if ( f.alt.value )
  2390 		if ( f.alt.value )
  2250 			alt = f.alt.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  2391 			alt = f.alt.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  2251 
  2392 
  2252 	<?php
  2393 		<?php
  2253 	/** This filter is documented in wp-admin/includes/media.php */
  2394 		/** This filter is documented in wp-admin/includes/media.php */
  2254 	if ( ! apply_filters( 'disable_captions', '' ) ) {
  2395 		if ( ! apply_filters( 'disable_captions', '' ) ) {
       
  2396 			?>
       
  2397 			if ( f.caption.value ) {
       
  2398 				caption = f.caption.value.replace(/\r\n|\r/g, '\n');
       
  2399 				caption = caption.replace(/<[a-zA-Z0-9]+( [^<>]+)?>/g, function(a){
       
  2400 					return a.replace(/[\r\n\t]+/, ' ');
       
  2401 				});
       
  2402 
       
  2403 				caption = caption.replace(/\s*\n\s*/g, '<br />');
       
  2404 			}
       
  2405 			<?php
       
  2406 		}
       
  2407 
  2255 		?>
  2408 		?>
  2256 	if ( f.caption.value ) {
       
  2257 		caption = f.caption.value.replace(/\r\n|\r/g, '\n');
       
  2258 		caption = caption.replace(/<[a-zA-Z0-9]+( [^<>]+)?>/g, function(a){
       
  2259 			return a.replace(/[\r\n\t]+/, ' ');
       
  2260 		});
       
  2261 
       
  2262 		caption = caption.replace(/\s*\n\s*/g, '<br />');
       
  2263 	}
       
  2264 	<?php } ?>
       
  2265 
       
  2266 		cls = caption ? '' : ' class="'+t.align+'"';
  2409 		cls = caption ? '' : ' class="'+t.align+'"';
  2267 
  2410 
  2268 		html = '<img alt="'+alt+'" src="'+f.src.value+'"'+cls+' width="'+t.width+'" height="'+t.height+'" />';
  2411 		html = '<img alt="'+alt+'" src="'+f.src.value+'"'+cls+' width="'+t.width+'" height="'+t.height+'" />';
  2269 
  2412 
  2270 		if ( f.url.value ) {
  2413 		if ( f.url.value ) {
  2314 		t.preloadImg = new Image();
  2457 		t.preloadImg = new Image();
  2315 		t.preloadImg.onload = t.updateImageData;
  2458 		t.preloadImg.onload = t.updateImageData;
  2316 		t.preloadImg.onerror = t.resetImageData;
  2459 		t.preloadImg.onerror = t.resetImageData;
  2317 		t.preloadImg.src = src;
  2460 		t.preloadImg.src = src;
  2318 	}
  2461 	}
  2319 };
  2462 	};
  2320 
  2463 
  2321 jQuery(document).ready( function($) {
  2464 	jQuery(document).ready( function($) {
  2322 	$('.media-types input').click( function() {
  2465 		$('.media-types input').click( function() {
  2323 		$('table.describe').toggleClass('not-image', $('#not-image').prop('checked') );
  2466 			$('table.describe').toggleClass('not-image', $('#not-image').prop('checked') );
       
  2467 		});
  2324 	});
  2468 	});
  2325 });
  2469 	</script>
  2326 </script>
  2470 
  2327 
  2471 	<div id="media-items">
  2328 <div id="media-items">
  2472 	<div class="media-item media-blank">
  2329 <div class="media-item media-blank">
       
  2330 	<?php
  2473 	<?php
  2331 	/**
  2474 	/**
  2332 	 * Filters the insert media from URL form HTML.
  2475 	 * Filters the insert media from URL form HTML.
  2333 	 *
  2476 	 *
  2334 	 * @since 3.3.0
  2477 	 * @since 3.3.0
  2335 	 *
  2478 	 *
  2336 	 * @param string $form_html The insert from URL form HTML.
  2479 	 * @param string $form_html The insert from URL form HTML.
  2337 	 */
  2480 	 */
  2338 	echo apply_filters( 'type_url_form_media', wp_media_insert_url_form( $type ) );
  2481 	echo apply_filters( 'type_url_form_media', wp_media_insert_url_form( $type ) );
       
  2482 
  2339 	?>
  2483 	?>
  2340 </div>
  2484 	</div>
  2341 </div>
  2485 	</div>
  2342 </form>
  2486 	</form>
  2343 	<?php
  2487 	<?php
  2344 }
  2488 }
  2345 
  2489 
  2346 /**
  2490 /**
  2347  * Adds gallery form to upload iframe
  2491  * Adds gallery form to upload iframe
  2367 	$form_class      = 'media-upload-form validate';
  2511 	$form_class      = 'media-upload-form validate';
  2368 
  2512 
  2369 	if ( get_user_setting( 'uploader' ) ) {
  2513 	if ( get_user_setting( 'uploader' ) ) {
  2370 		$form_class .= ' html-uploader';
  2514 		$form_class .= ' html-uploader';
  2371 	}
  2515 	}
       
  2516 
  2372 	?>
  2517 	?>
  2373 
  2518 	<script type="text/javascript">
  2374 <script type="text/javascript">
  2519 	jQuery(function($){
  2375 jQuery(function($){
  2520 		var preloaded = $(".media-item.preloaded");
  2376 	var preloaded = $(".media-item.preloaded");
  2521 		if ( preloaded.length > 0 ) {
  2377 	if ( preloaded.length > 0 ) {
  2522 			preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
  2378 		preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
  2523 			updateMediaForm();
  2379 		updateMediaForm();
  2524 		}
  2380 	}
  2525 	});
  2381 });
  2526 	</script>
  2382 </script>
  2527 	<div id="sort-buttons" class="hide-if-no-js">
  2383 <div id="sort-buttons" class="hide-if-no-js">
  2528 	<span>
  2384 <span>
  2529 		<?php _e( 'All Tabs:' ); ?>
  2385 	<?php _e( 'All Tabs:' ); ?>
  2530 	<a href="#" id="showall"><?php _e( 'Show' ); ?></a>
  2386 <a href="#" id="showall"><?php _e( 'Show' ); ?></a>
  2531 	<a href="#" id="hideall" style="display:none;"><?php _e( 'Hide' ); ?></a>
  2387 <a href="#" id="hideall" style="display:none;"><?php _e( 'Hide' ); ?></a>
  2532 	</span>
  2388 </span>
  2533 		<?php _e( 'Sort Order:' ); ?>
  2389 	<?php _e( 'Sort Order:' ); ?>
  2534 	<a href="#" id="asc"><?php _e( 'Ascending' ); ?></a> |
  2390 <a href="#" id="asc"><?php _e( 'Ascending' ); ?></a> |
  2535 	<a href="#" id="desc"><?php _e( 'Descending' ); ?></a> |
  2391 <a href="#" id="desc"><?php _e( 'Descending' ); ?></a> |
  2536 	<a href="#" id="clear"><?php _ex( 'Clear', 'verb' ); ?></a>
  2392 <a href="#" id="clear"><?php _ex( 'Clear', 'verb' ); ?></a>
  2537 	</div>
  2393 </div>
  2538 	<form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="gallery-form">
  2394 <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="gallery-form">
  2539 		<?php wp_nonce_field( 'media-form' ); ?>
  2395 	<?php wp_nonce_field( 'media-form' ); ?>
  2540 		<?php // media_upload_form( $errors ); ?>
  2396 	<?php //media_upload_form( $errors ); ?>
  2541 	<table class="widefat">
  2397 <table class="widefat">
  2542 	<thead><tr>
  2398 <thead><tr>
  2543 	<th><?php _e( 'Media' ); ?></th>
  2399 <th><?php _e( 'Media' ); ?></th>
  2544 	<th class="order-head"><?php _e( 'Order' ); ?></th>
  2400 <th class="order-head"><?php _e( 'Order' ); ?></th>
  2545 	<th class="actions-head"><?php _e( 'Actions' ); ?></th>
  2401 <th class="actions-head"><?php _e( 'Actions' ); ?></th>
  2546 	</tr></thead>
  2402 </tr></thead>
  2547 	</table>
  2403 </table>
  2548 	<div id="media-items">
  2404 <div id="media-items">
  2549 		<?php add_filter( 'attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2 ); ?>
  2405 	<?php add_filter( 'attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2 ); ?>
  2550 		<?php echo get_media_items( $post_id, $errors ); ?>
  2406 	<?php echo get_media_items( $post_id, $errors ); ?>
  2551 	</div>
  2407 </div>
  2552 
  2408 
  2553 	<p class="ml-submit">
  2409 <p class="ml-submit">
  2554 		<?php
       
  2555 		submit_button(
       
  2556 			__( 'Save all changes' ),
       
  2557 			'savebutton',
       
  2558 			'save',
       
  2559 			false,
       
  2560 			array(
       
  2561 				'id'    => 'save-all',
       
  2562 				'style' => 'display: none;',
       
  2563 			)
       
  2564 		);
       
  2565 		?>
       
  2566 	<input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
       
  2567 	<input type="hidden" name="type" value="<?php echo esc_attr( $GLOBALS['type'] ); ?>" />
       
  2568 	<input type="hidden" name="tab" value="<?php echo esc_attr( $GLOBALS['tab'] ); ?>" />
       
  2569 	</p>
       
  2570 
       
  2571 	<div id="gallery-settings" style="display:none;">
       
  2572 	<div class="title"><?php _e( 'Gallery Settings' ); ?></div>
       
  2573 	<table id="basic" class="describe"><tbody>
       
  2574 		<tr>
       
  2575 		<th scope="row" class="label">
       
  2576 			<label>
       
  2577 			<span class="alignleft"><?php _e( 'Link thumbnails to:' ); ?></span>
       
  2578 			</label>
       
  2579 		</th>
       
  2580 		<td class="field">
       
  2581 			<input type="radio" name="linkto" id="linkto-file" value="file" />
       
  2582 			<label for="linkto-file" class="radio"><?php _e( 'Image File' ); ?></label>
       
  2583 
       
  2584 			<input type="radio" checked="checked" name="linkto" id="linkto-post" value="post" />
       
  2585 			<label for="linkto-post" class="radio"><?php _e( 'Attachment Page' ); ?></label>
       
  2586 		</td>
       
  2587 		</tr>
       
  2588 
       
  2589 		<tr>
       
  2590 		<th scope="row" class="label">
       
  2591 			<label>
       
  2592 			<span class="alignleft"><?php _e( 'Order images by:' ); ?></span>
       
  2593 			</label>
       
  2594 		</th>
       
  2595 		<td class="field">
       
  2596 			<select id="orderby" name="orderby">
       
  2597 				<option value="menu_order" selected="selected"><?php _e( 'Menu order' ); ?></option>
       
  2598 				<option value="title"><?php _e( 'Title' ); ?></option>
       
  2599 				<option value="post_date"><?php _e( 'Date/Time' ); ?></option>
       
  2600 				<option value="rand"><?php _e( 'Random' ); ?></option>
       
  2601 			</select>
       
  2602 		</td>
       
  2603 		</tr>
       
  2604 
       
  2605 		<tr>
       
  2606 		<th scope="row" class="label">
       
  2607 			<label>
       
  2608 			<span class="alignleft"><?php _e( 'Order:' ); ?></span>
       
  2609 			</label>
       
  2610 		</th>
       
  2611 		<td class="field">
       
  2612 			<input type="radio" checked="checked" name="order" id="order-asc" value="asc" />
       
  2613 			<label for="order-asc" class="radio"><?php _e( 'Ascending' ); ?></label>
       
  2614 
       
  2615 			<input type="radio" name="order" id="order-desc" value="desc" />
       
  2616 			<label for="order-desc" class="radio"><?php _e( 'Descending' ); ?></label>
       
  2617 		</td>
       
  2618 		</tr>
       
  2619 
       
  2620 		<tr>
       
  2621 		<th scope="row" class="label">
       
  2622 			<label>
       
  2623 			<span class="alignleft"><?php _e( 'Gallery columns:' ); ?></span>
       
  2624 			</label>
       
  2625 		</th>
       
  2626 		<td class="field">
       
  2627 			<select id="columns" name="columns">
       
  2628 				<option value="1">1</option>
       
  2629 				<option value="2">2</option>
       
  2630 				<option value="3" selected="selected">3</option>
       
  2631 				<option value="4">4</option>
       
  2632 				<option value="5">5</option>
       
  2633 				<option value="6">6</option>
       
  2634 				<option value="7">7</option>
       
  2635 				<option value="8">8</option>
       
  2636 				<option value="9">9</option>
       
  2637 			</select>
       
  2638 		</td>
       
  2639 		</tr>
       
  2640 	</tbody></table>
       
  2641 
       
  2642 	<p class="ml-submit">
       
  2643 	<input type="button" class="button" style="display:none;" onMouseDown="wpgallery.update();" name="insert-gallery" id="insert-gallery" value="<?php esc_attr_e( 'Insert gallery' ); ?>" />
       
  2644 	<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' ); ?>" />
       
  2645 	</p>
       
  2646 	</div>
       
  2647 	</form>
  2410 	<?php
  2648 	<?php
  2411 	submit_button(
       
  2412 		__( 'Save all changes' ),
       
  2413 		'savebutton',
       
  2414 		'save',
       
  2415 		false,
       
  2416 		array(
       
  2417 			'id'    => 'save-all',
       
  2418 			'style' => 'display: none;',
       
  2419 		)
       
  2420 	);
       
  2421 	?>
       
  2422 <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
       
  2423 <input type="hidden" name="type" value="<?php echo esc_attr( $GLOBALS['type'] ); ?>" />
       
  2424 <input type="hidden" name="tab" value="<?php echo esc_attr( $GLOBALS['tab'] ); ?>" />
       
  2425 </p>
       
  2426 
       
  2427 <div id="gallery-settings" style="display:none;">
       
  2428 <div class="title"><?php _e( 'Gallery Settings' ); ?></div>
       
  2429 <table id="basic" class="describe"><tbody>
       
  2430 	<tr>
       
  2431 	<th scope="row" class="label">
       
  2432 		<label>
       
  2433 		<span class="alignleft"><?php _e( 'Link thumbnails to:' ); ?></span>
       
  2434 		</label>
       
  2435 	</th>
       
  2436 	<td class="field">
       
  2437 		<input type="radio" name="linkto" id="linkto-file" value="file" />
       
  2438 		<label for="linkto-file" class="radio"><?php _e( 'Image File' ); ?></label>
       
  2439 
       
  2440 		<input type="radio" checked="checked" name="linkto" id="linkto-post" value="post" />
       
  2441 		<label for="linkto-post" class="radio"><?php _e( 'Attachment Page' ); ?></label>
       
  2442 	</td>
       
  2443 	</tr>
       
  2444 
       
  2445 	<tr>
       
  2446 	<th scope="row" class="label">
       
  2447 		<label>
       
  2448 		<span class="alignleft"><?php _e( 'Order images by:' ); ?></span>
       
  2449 		</label>
       
  2450 	</th>
       
  2451 	<td class="field">
       
  2452 		<select id="orderby" name="orderby">
       
  2453 			<option value="menu_order" selected="selected"><?php _e( 'Menu order' ); ?></option>
       
  2454 			<option value="title"><?php _e( 'Title' ); ?></option>
       
  2455 			<option value="post_date"><?php _e( 'Date/Time' ); ?></option>
       
  2456 			<option value="rand"><?php _e( 'Random' ); ?></option>
       
  2457 		</select>
       
  2458 	</td>
       
  2459 	</tr>
       
  2460 
       
  2461 	<tr>
       
  2462 	<th scope="row" class="label">
       
  2463 		<label>
       
  2464 		<span class="alignleft"><?php _e( 'Order:' ); ?></span>
       
  2465 		</label>
       
  2466 	</th>
       
  2467 	<td class="field">
       
  2468 		<input type="radio" checked="checked" name="order" id="order-asc" value="asc" />
       
  2469 		<label for="order-asc" class="radio"><?php _e( 'Ascending' ); ?></label>
       
  2470 
       
  2471 		<input type="radio" name="order" id="order-desc" value="desc" />
       
  2472 		<label for="order-desc" class="radio"><?php _e( 'Descending' ); ?></label>
       
  2473 	</td>
       
  2474 	</tr>
       
  2475 
       
  2476 	<tr>
       
  2477 	<th scope="row" class="label">
       
  2478 		<label>
       
  2479 		<span class="alignleft"><?php _e( 'Gallery columns:' ); ?></span>
       
  2480 		</label>
       
  2481 	</th>
       
  2482 	<td class="field">
       
  2483 		<select id="columns" name="columns">
       
  2484 			<option value="1">1</option>
       
  2485 			<option value="2">2</option>
       
  2486 			<option value="3" selected="selected">3</option>
       
  2487 			<option value="4">4</option>
       
  2488 			<option value="5">5</option>
       
  2489 			<option value="6">6</option>
       
  2490 			<option value="7">7</option>
       
  2491 			<option value="8">8</option>
       
  2492 			<option value="9">9</option>
       
  2493 		</select>
       
  2494 	</td>
       
  2495 	</tr>
       
  2496 </tbody></table>
       
  2497 
       
  2498 <p class="ml-submit">
       
  2499 <input type="button" class="button" style="display:none;" onMouseDown="wpgallery.update();" name="insert-gallery" id="insert-gallery" value="<?php esc_attr_e( 'Insert gallery' ); ?>" />
       
  2500 <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' ); ?>" />
       
  2501 </p>
       
  2502 </div>
       
  2503 </form>
       
  2504 	<?php
       
  2505 }
  2649 }
  2506 
  2650 
  2507 /**
  2651 /**
  2508  * Outputs the legacy media upload form for the media library.
  2652  * Outputs the legacy media upload form for the media library.
  2509  *
  2653  *
  2510  * @since 2.5.0
  2654  * @since 2.5.0
  2511  *
  2655  *
  2512  * @global wpdb      $wpdb
  2656  * @global wpdb      $wpdb            WordPress database abstraction object.
  2513  * @global WP_Query  $wp_query
  2657  * @global WP_Query  $wp_query        WordPress Query object.
  2514  * @global WP_Locale $wp_locale
  2658  * @global WP_Locale $wp_locale       WordPress date and time locale object.
  2515  * @global string    $type
  2659  * @global string    $type
  2516  * @global string    $tab
  2660  * @global string    $tab
  2517  * @global array     $post_mime_types
  2661  * @global array     $post_mime_types
  2518  *
  2662  *
  2519  * @param array $errors
  2663  * @param array $errors
  2546 	}
  2690 	}
  2547 
  2691 
  2548 	list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query( $q );
  2692 	list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query( $q );
  2549 
  2693 
  2550 	?>
  2694 	?>
  2551 
  2695 	<form id="filter" method="get">
  2552 <form id="filter" method="get">
  2696 	<input type="hidden" name="type" value="<?php echo esc_attr( $type ); ?>" />
  2553 <input type="hidden" name="type" value="<?php echo esc_attr( $type ); ?>" />
  2697 	<input type="hidden" name="tab" value="<?php echo esc_attr( $tab ); ?>" />
  2554 <input type="hidden" name="tab" value="<?php echo esc_attr( $tab ); ?>" />
  2698 	<input type="hidden" name="post_id" value="<?php echo (int) $post_id; ?>" />
  2555 <input type="hidden" name="post_id" value="<?php echo (int) $post_id; ?>" />
  2699 	<input type="hidden" name="post_mime_type" value="<?php echo isset( $_GET['post_mime_type'] ) ? esc_attr( $_GET['post_mime_type'] ) : ''; ?>" />
  2556 <input type="hidden" name="post_mime_type" value="<?php echo isset( $_GET['post_mime_type'] ) ? esc_attr( $_GET['post_mime_type'] ) : ''; ?>" />
  2700 	<input type="hidden" name="context" value="<?php echo isset( $_GET['context'] ) ? esc_attr( $_GET['context'] ) : ''; ?>" />
  2557 <input type="hidden" name="context" value="<?php echo isset( $_GET['context'] ) ? esc_attr( $_GET['context'] ) : ''; ?>" />
  2701 
  2558 
  2702 	<p id="media-search" class="search-box">
  2559 <p id="media-search" class="search-box">
  2703 		<label class="screen-reader-text" for="media-search-input"><?php _e( 'Search Media' ); ?>:</label>
  2560 	<label class="screen-reader-text" for="media-search-input"><?php _e( 'Search Media' ); ?>:</label>
  2704 		<input type="search" id="media-search-input" name="s" value="<?php the_search_query(); ?>" />
  2561 	<input type="search" id="media-search-input" name="s" value="<?php the_search_query(); ?>" />
  2705 		<?php submit_button( __( 'Search Media' ), '', '', false ); ?>
  2562 	<?php submit_button( __( 'Search Media' ), '', '', false ); ?>
  2706 	</p>
  2563 </p>
  2707 
  2564 
  2708 	<ul class="subsubsub">
  2565 <ul class="subsubsub">
  2709 		<?php
  2566 	<?php
  2710 		$type_links = array();
  2567 	$type_links = array();
  2711 		$_num_posts = (array) wp_count_attachments();
  2568 	$_num_posts = (array) wp_count_attachments();
  2712 		$matches    = wp_match_mime_types( array_keys( $post_mime_types ), array_keys( $_num_posts ) );
  2569 	$matches    = wp_match_mime_types( array_keys( $post_mime_types ), array_keys( $_num_posts ) );
  2713 		foreach ( $matches as $_type => $reals ) {
  2570 	foreach ( $matches as $_type => $reals ) {
  2714 			foreach ( $reals as $real ) {
  2571 		foreach ( $reals as $real ) {
  2715 				if ( isset( $num_posts[ $_type ] ) ) {
  2572 			if ( isset( $num_posts[ $_type ] ) ) {
  2716 					$num_posts[ $_type ] += $_num_posts[ $real ];
  2573 				$num_posts[ $_type ] += $_num_posts[ $real ];
  2717 				} else {
  2574 			} else {
  2718 					$num_posts[ $_type ] = $_num_posts[ $real ];
  2575 				$num_posts[ $_type ] = $_num_posts[ $real ];
  2719 				}
  2576 			}
  2720 			}
  2577 		}
  2721 		}
  2578 	}
  2722 		// If available type specified by media button clicked, filter by that type.
  2579 	// If available type specified by media button clicked, filter by that type
  2723 		if ( empty( $_GET['post_mime_type'] ) && ! empty( $num_posts[ $type ] ) ) {
  2580 	if ( empty( $_GET['post_mime_type'] ) && ! empty( $num_posts[ $type ] ) ) {
  2724 			$_GET['post_mime_type']                        = $type;
  2581 		$_GET['post_mime_type']                        = $type;
  2725 			list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
  2582 		list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
  2726 		}
  2583 	}
  2727 		if ( empty( $_GET['post_mime_type'] ) || 'all' === $_GET['post_mime_type'] ) {
  2584 	if ( empty( $_GET['post_mime_type'] ) || $_GET['post_mime_type'] == 'all' ) {
       
  2585 		$class = ' class="current"';
       
  2586 	} else {
       
  2587 		$class = '';
       
  2588 	}
       
  2589 	$type_links[] = '<li><a href="' . esc_url(
       
  2590 		add_query_arg(
       
  2591 			array(
       
  2592 				'post_mime_type' => 'all',
       
  2593 				'paged'          => false,
       
  2594 				'm'              => false,
       
  2595 			)
       
  2596 		)
       
  2597 	) . '"' . $class . '>' . __( 'All Types' ) . '</a>';
       
  2598 	foreach ( $post_mime_types as $mime_type => $label ) {
       
  2599 		$class = '';
       
  2600 
       
  2601 		if ( ! wp_match_mime_types( $mime_type, $avail_post_mime_types ) ) {
       
  2602 			continue;
       
  2603 		}
       
  2604 
       
  2605 		if ( isset( $_GET['post_mime_type'] ) && wp_match_mime_types( $mime_type, $_GET['post_mime_type'] ) ) {
       
  2606 			$class = ' class="current"';
  2728 			$class = ' class="current"';
  2607 		}
  2729 		} else {
  2608 
  2730 			$class = '';
       
  2731 		}
  2609 		$type_links[] = '<li><a href="' . esc_url(
  2732 		$type_links[] = '<li><a href="' . esc_url(
  2610 			add_query_arg(
  2733 			add_query_arg(
  2611 				array(
  2734 				array(
  2612 					'post_mime_type' => $mime_type,
  2735 					'post_mime_type' => 'all',
  2613 					'paged'          => false,
  2736 					'paged'          => false,
       
  2737 					'm'              => false,
  2614 				)
  2738 				)
  2615 			)
  2739 			)
  2616 		) . '"' . $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>';
  2740 		) . '"' . $class . '>' . __( 'All Types' ) . '</a>';
  2617 	}
  2741 		foreach ( $post_mime_types as $mime_type => $label ) {
  2618 	/**
  2742 			$class = '';
  2619 	 * Filters the media upload mime type list items.
  2743 
  2620 	 *
  2744 			if ( ! wp_match_mime_types( $mime_type, $avail_post_mime_types ) ) {
  2621 	 * Returned values should begin with an `<li>` tag.
  2745 				continue;
  2622 	 *
  2746 			}
  2623 	 * @since 3.1.0
  2747 
  2624 	 *
  2748 			if ( isset( $_GET['post_mime_type'] ) && wp_match_mime_types( $mime_type, $_GET['post_mime_type'] ) ) {
  2625 	 * @param string[] $type_links An array of list items containing mime type link HTML.
  2749 				$class = ' class="current"';
  2626 	 */
  2750 			}
  2627 	echo implode( ' | </li>', apply_filters( 'media_upload_mime_type_links', $type_links ) ) . '</li>';
  2751 
  2628 	unset( $type_links );
  2752 			$type_links[] = '<li><a href="' . esc_url(
  2629 	?>
  2753 				add_query_arg(
  2630 </ul>
  2754 					array(
  2631 
  2755 						'post_mime_type' => $mime_type,
  2632 <div class="tablenav">
  2756 						'paged'          => false,
  2633 
  2757 					)
  2634 	<?php
  2758 				)
  2635 	$page_links = paginate_links(
  2759 			) . '"' . $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>';
  2636 		array(
  2760 		}
  2637 			'base'      => add_query_arg( 'paged', '%#%' ),
  2761 		/**
  2638 			'format'    => '',
  2762 		 * Filters the media upload mime type list items.
  2639 			'prev_text' => __( '&laquo;' ),
  2763 		 *
  2640 			'next_text' => __( '&raquo;' ),
  2764 		 * Returned values should begin with an `<li>` tag.
  2641 			'total'     => ceil( $wp_query->found_posts / 10 ),
  2765 		 *
  2642 			'current'   => $q['paged'],
  2766 		 * @since 3.1.0
  2643 		)
  2767 		 *
  2644 	);
  2768 		 * @param string[] $type_links An array of list items containing mime type link HTML.
  2645 
  2769 		 */
  2646 	if ( $page_links ) {
  2770 		echo implode( ' | </li>', apply_filters( 'media_upload_mime_type_links', $type_links ) ) . '</li>';
  2647 		echo "<div class='tablenav-pages'>$page_links</div>";
  2771 		unset( $type_links );
  2648 	}
       
  2649 	?>
       
  2650 
       
  2651 <div class="alignleft actions">
       
  2652 	<?php
       
  2653 
       
  2654 	$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";
       
  2655 
       
  2656 	$arc_result = $wpdb->get_results( $arc_query );
       
  2657 
       
  2658 	$month_count    = count( $arc_result );
       
  2659 	$selected_month = isset( $_GET['m'] ) ? $_GET['m'] : 0;
       
  2660 
       
  2661 	if ( $month_count && ! ( 1 == $month_count && 0 == $arc_result[0]->mmonth ) ) {
       
  2662 		?>
  2772 		?>
  2663 <select name='m'>
  2773 	</ul>
  2664 <option<?php selected( $selected_month, 0 ); ?> value='0'><?php _e( 'All dates' ); ?></option>
  2774 
       
  2775 	<div class="tablenav">
       
  2776 
  2665 		<?php
  2777 		<?php
  2666 		foreach ( $arc_result as $arc_row ) {
  2778 		$page_links = paginate_links(
  2667 			if ( $arc_row->yyear == 0 ) {
  2779 			array(
  2668 				continue;
  2780 				'base'      => add_query_arg( 'paged', '%#%' ),
  2669 			}
  2781 				'format'    => '',
  2670 			$arc_row->mmonth = zeroise( $arc_row->mmonth, 2 );
  2782 				'prev_text' => __( '&laquo;' ),
  2671 
  2783 				'next_text' => __( '&raquo;' ),
  2672 			if ( $arc_row->yyear . $arc_row->mmonth == $selected_month ) {
  2784 				'total'     => ceil( $wp_query->found_posts / 10 ),
  2673 				$default = ' selected="selected"';
  2785 				'current'   => $q['paged'],
  2674 			} else {
  2786 			)
  2675 				$default = '';
  2787 		);
  2676 			}
  2788 
  2677 
  2789 		if ( $page_links ) {
  2678 			echo "<option$default value='" . esc_attr( $arc_row->yyear . $arc_row->mmonth ) . "'>";
  2790 			echo "<div class='tablenav-pages'>$page_links</div>";
  2679 			echo esc_html( $wp_locale->get_month( $arc_row->mmonth ) . " $arc_row->yyear" );
       
  2680 			echo "</option>\n";
       
  2681 		}
  2791 		}
  2682 		?>
  2792 		?>
  2683 </select>
  2793 
  2684 	<?php } ?>
  2794 	<div class="alignleft actions">
  2685 
  2795 		<?php
  2686 	<?php submit_button( __( 'Filter &#187;' ), '', 'post-query-submit', false ); ?>
  2796 
  2687 
  2797 		$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";
  2688 </div>
  2798 
  2689 
  2799 		$arc_result = $wpdb->get_results( $arc_query );
  2690 <br class="clear" />
  2800 
  2691 </div>
  2801 		$month_count    = count( $arc_result );
  2692 </form>
  2802 		$selected_month = isset( $_GET['m'] ) ? $_GET['m'] : 0;
  2693 
  2803 
  2694 <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="library-form">
  2804 		if ( $month_count && ! ( 1 == $month_count && 0 == $arc_result[0]->mmonth ) ) {
  2695 
  2805 			?>
       
  2806 			<select name='m'>
       
  2807 			<option<?php selected( $selected_month, 0 ); ?> value='0'><?php _e( 'All dates' ); ?></option>
       
  2808 			<?php
       
  2809 
       
  2810 			foreach ( $arc_result as $arc_row ) {
       
  2811 				if ( 0 == $arc_row->yyear ) {
       
  2812 					continue;
       
  2813 				}
       
  2814 
       
  2815 				$arc_row->mmonth = zeroise( $arc_row->mmonth, 2 );
       
  2816 
       
  2817 				if ( $arc_row->yyear . $arc_row->mmonth == $selected_month ) {
       
  2818 					$default = ' selected="selected"';
       
  2819 				} else {
       
  2820 					$default = '';
       
  2821 				}
       
  2822 
       
  2823 				echo "<option$default value='" . esc_attr( $arc_row->yyear . $arc_row->mmonth ) . "'>";
       
  2824 				echo esc_html( $wp_locale->get_month( $arc_row->mmonth ) . " $arc_row->yyear" );
       
  2825 				echo "</option>\n";
       
  2826 			}
       
  2827 
       
  2828 			?>
       
  2829 			</select>
       
  2830 		<?php } ?>
       
  2831 
       
  2832 		<?php submit_button( __( 'Filter &#187;' ), '', 'post-query-submit', false ); ?>
       
  2833 
       
  2834 	</div>
       
  2835 
       
  2836 	<br class="clear" />
       
  2837 	</div>
       
  2838 	</form>
       
  2839 
       
  2840 	<form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="library-form">
  2696 	<?php wp_nonce_field( 'media-form' ); ?>
  2841 	<?php wp_nonce_field( 'media-form' ); ?>
  2697 	<?php //media_upload_form( $errors ); ?>
  2842 	<?php // media_upload_form( $errors ); ?>
  2698 
  2843 
  2699 <script type="text/javascript">
  2844 	<script type="text/javascript">
  2700 <!--
  2845 	jQuery(function($){
  2701 jQuery(function($){
  2846 		var preloaded = $(".media-item.preloaded");
  2702 	var preloaded = $(".media-item.preloaded");
  2847 		if ( preloaded.length > 0 ) {
  2703 	if ( preloaded.length > 0 ) {
  2848 			preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
  2704 		preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
  2849 			updateMediaForm();
  2705 		updateMediaForm();
  2850 		}
  2706 	}
  2851 	});
  2707 });
  2852 	</script>
  2708 -->
  2853 
  2709 </script>
  2854 	<div id="media-items">
  2710 
  2855 		<?php add_filter( 'attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2 ); ?>
  2711 <div id="media-items">
  2856 		<?php echo get_media_items( null, $errors ); ?>
  2712 	<?php add_filter( 'attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2 ); ?>
  2857 	</div>
  2713 	<?php echo get_media_items( null, $errors ); ?>
  2858 	<p class="ml-submit">
  2714 </div>
  2859 		<?php submit_button( __( 'Save all changes' ), 'savebutton', 'save', false ); ?>
  2715 <p class="ml-submit">
  2860 	<input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  2716 	<?php submit_button( __( 'Save all changes' ), 'savebutton', 'save', false ); ?>
  2861 	</p>
  2717 <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  2862 	</form>
  2718 </p>
       
  2719 </form>
       
  2720 	<?php
  2863 	<?php
  2721 }
  2864 }
  2722 
  2865 
  2723 /**
  2866 /**
  2724  * Creates the form for external url
  2867  * Creates the form for external url
  2735 		<tr class="image-only">
  2878 		<tr class="image-only">
  2736 			<th scope="row" class="label">
  2879 			<th scope="row" class="label">
  2737 				<label for="caption"><span class="alignleft">' . __( 'Image Caption' ) . '</span></label>
  2880 				<label for="caption"><span class="alignleft">' . __( 'Image Caption' ) . '</span></label>
  2738 			</th>
  2881 			</th>
  2739 			<td class="field"><textarea id="caption" name="caption"></textarea></td>
  2882 			<td class="field"><textarea id="caption" name="caption"></textarea></td>
  2740 		</tr>
  2883 		</tr>';
  2741 ';
       
  2742 	} else {
  2884 	} else {
  2743 		$caption = '';
  2885 		$caption = '';
  2744 	}
  2886 	}
  2745 
  2887 
  2746 	$default_align = get_option( 'image_default_align' );
  2888 	$default_align = get_option( 'image_default_align' );
       
  2889 
  2747 	if ( empty( $default_align ) ) {
  2890 	if ( empty( $default_align ) ) {
  2748 		$default_align = 'none';
  2891 		$default_align = 'none';
  2749 	}
  2892 	}
  2750 
  2893 
  2751 	if ( 'image' == $default_view ) {
  2894 	if ( 'image' === $default_view ) {
  2752 		$view        = 'image-only';
  2895 		$view        = 'image-only';
  2753 		$table_class = '';
  2896 		$table_class = '';
  2754 	} else {
  2897 	} else {
  2755 		$view = $table_class = 'not-image';
  2898 		$view        = 'not-image';
       
  2899 		$table_class = $view;
  2756 	}
  2900 	}
  2757 
  2901 
  2758 	return '
  2902 	return '
  2759 	<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>
  2903 	<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>
  2760 	<p class="media-types media-types-required-info">' . sprintf( __( 'Required fields are marked %s' ), '<span class="required">*</span>' ) . '</p>
  2904 	<p class="media-types media-types-required-info">' .
       
  2905 		/* translators: %s: Asterisk symbol (*). */
       
  2906 		sprintf( __( 'Required fields are marked %s' ), '<span class="required">*</span>' ) .
       
  2907 	'</p>
  2761 	<table class="describe ' . $table_class . '"><tbody>
  2908 	<table class="describe ' . $table_class . '"><tbody>
  2762 		<tr>
  2909 		<tr>
  2763 			<th scope="row" class="label" style="width:130px;">
  2910 			<th scope="row" class="label" style="width:130px;">
  2764 				<label for="src"><span class="alignleft">' . __( 'URL' ) . '</span> <span class="required">*</span></label>
  2911 				<label for="src"><span class="alignleft">' . __( 'URL' ) . '</span> <span class="required">*</span></label>
  2765 				<span class="alignright" id="status_img"></span>
  2912 				<span class="alignright" id="status_img"></span>
  2785 		</tr>
  2932 		</tr>
  2786 		' . $caption . '
  2933 		' . $caption . '
  2787 		<tr class="align image-only">
  2934 		<tr class="align image-only">
  2788 			<th scope="row" class="label"><p><label for="align">' . __( 'Alignment' ) . '</label></p></th>
  2935 			<th scope="row" class="label"><p><label for="align">' . __( 'Alignment' ) . '</label></p></th>
  2789 			<td class="field">
  2936 			<td class="field">
  2790 				<input name="align" id="align-none" value="none" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ( $default_align == 'none' ? ' checked="checked"' : '' ) . ' />
  2937 				<input name="align" id="align-none" value="none" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ( 'none' === $default_align ? ' checked="checked"' : '' ) . ' />
  2791 				<label for="align-none" class="align image-align-none-label">' . __( 'None' ) . '</label>
  2938 				<label for="align-none" class="align image-align-none-label">' . __( 'None' ) . '</label>
  2792 				<input name="align" id="align-left" value="left" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ( $default_align == 'left' ? ' checked="checked"' : '' ) . ' />
  2939 				<input name="align" id="align-left" value="left" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ( 'left' === $default_align ? ' checked="checked"' : '' ) . ' />
  2793 				<label for="align-left" class="align image-align-left-label">' . __( 'Left' ) . '</label>
  2940 				<label for="align-left" class="align image-align-left-label">' . __( 'Left' ) . '</label>
  2794 				<input name="align" id="align-center" value="center" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ( $default_align == 'center' ? ' checked="checked"' : '' ) . ' />
  2941 				<input name="align" id="align-center" value="center" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ( 'center' === $default_align ? ' checked="checked"' : '' ) . ' />
  2795 				<label for="align-center" class="align image-align-center-label">' . __( 'Center' ) . '</label>
  2942 				<label for="align-center" class="align image-align-center-label">' . __( 'Center' ) . '</label>
  2796 				<input name="align" id="align-right" value="right" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ( $default_align == 'right' ? ' checked="checked"' : '' ) . ' />
  2943 				<input name="align" id="align-right" value="right" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ( 'right' === $default_align ? ' checked="checked"' : '' ) . ' />
  2797 				<label for="align-right" class="align image-align-right-label">' . __( 'Right' ) . '</label>
  2944 				<label for="align-right" class="align image-align-right-label">' . __( 'Right' ) . '</label>
  2798 			</td>
  2945 			</td>
  2799 		</tr>
  2946 		</tr>
  2800 
  2947 
  2801 		<tr class="image-only">
  2948 		<tr class="image-only">
  2818 			<td></td>
  2965 			<td></td>
  2819 			<td>
  2966 			<td>
  2820 				' . get_submit_button( __( 'Insert into Post' ), '', 'insertonlybutton', false ) . '
  2967 				' . get_submit_button( __( 'Insert into Post' ), '', 'insertonlybutton', false ) . '
  2821 			</td>
  2968 			</td>
  2822 		</tr>
  2969 		</tr>
  2823 	</tbody></table>
  2970 	</tbody></table>';
  2824 ';
       
  2825 
       
  2826 }
  2971 }
  2827 
  2972 
  2828 /**
  2973 /**
  2829  * Displays the multi-file uploader message.
  2974  * Displays the multi-file uploader message.
  2830  *
  2975  *
  2833  * @global int $post_ID
  2978  * @global int $post_ID
  2834  */
  2979  */
  2835 function media_upload_flash_bypass() {
  2980 function media_upload_flash_bypass() {
  2836 	$browser_uploader = admin_url( 'media-new.php?browser-uploader' );
  2981 	$browser_uploader = admin_url( 'media-new.php?browser-uploader' );
  2837 
  2982 
  2838 	if ( $post = get_post() ) {
  2983 	$post = get_post();
       
  2984 	if ( $post ) {
  2839 		$browser_uploader .= '&amp;post_id=' . intval( $post->ID );
  2985 		$browser_uploader .= '&amp;post_id=' . intval( $post->ID );
  2840 	} elseif ( ! empty( $GLOBALS['post_ID'] ) ) {
  2986 	} elseif ( ! empty( $GLOBALS['post_ID'] ) ) {
  2841 		$browser_uploader .= '&amp;post_id=' . intval( $GLOBALS['post_ID'] );
  2987 		$browser_uploader .= '&amp;post_id=' . intval( $GLOBALS['post_ID'] );
  2842 	}
  2988 	}
  2843 
  2989 
  2844 	?>
  2990 	?>
  2845 	<p class="upload-flash-bypass">
  2991 	<p class="upload-flash-bypass">
  2846 	<?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' ); ?>
  2992 	<?php
       
  2993 		printf(
       
  2994 			/* translators: 1: URL to browser uploader, 2: Additional link attributes. */
       
  2995 			__( 'You are using the multi-file uploader. Problems? Try the <a href="%1$s" %2$s>browser uploader</a> instead.' ),
       
  2996 			$browser_uploader,
       
  2997 			'target="_blank"'
       
  2998 		);
       
  2999 	?>
  2847 	</p>
  3000 	</p>
  2848 	<?php
  3001 	<?php
  2849 }
  3002 }
  2850 
  3003 
  2851 /**
  3004 /**
  2873  *
  3026  *
  2874  * @since 3.3.0
  3027  * @since 3.3.0
  2875  */
  3028  */
  2876 function media_upload_max_image_resize() {
  3029 function media_upload_max_image_resize() {
  2877 	$checked = get_user_setting( 'upload_resize' ) ? ' checked="true"' : '';
  3030 	$checked = get_user_setting( 'upload_resize' ) ? ' checked="true"' : '';
  2878 	$a       = $end = '';
  3031 	$a       = '';
       
  3032 	$end     = '';
  2879 
  3033 
  2880 	if ( current_user_can( 'manage_options' ) ) {
  3034 	if ( current_user_can( 'manage_options' ) ) {
  2881 		$a   = '<a href="' . esc_url( admin_url( 'options-media.php' ) ) . '" target="_blank">';
  3035 		$a   = '<a href="' . esc_url( admin_url( 'options-media.php' ) ) . '" target="_blank">';
  2882 		$end = '</a>';
  3036 		$end = '</a>';
  2883 	}
  3037 	}
       
  3038 
  2884 	?>
  3039 	?>
  2885 <p class="hide-if-no-js"><label>
  3040 	<p class="hide-if-no-js"><label>
  2886 <input name="image_resize" type="checkbox" id="image_resize" value="true"<?php echo $checked; ?> />
  3041 	<input name="image_resize" type="checkbox" id="image_resize" value="true"<?php echo $checked; ?> />
  2887 	<?php
  3042 	<?php
  2888 	/* translators: 1: link start tag, 2: link end tag, 3: width, 4: height */
  3043 	/* translators: 1: Link start tag, 2: Link end tag, 3: Width, 4: Height. */
  2889 	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' ) );
  3044 	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' ) );
       
  3045 
  2890 	?>
  3046 	?>
  2891 </label></p>
  3047 	</label></p>
  2892 	<?php
  3048 	<?php
  2893 }
  3049 }
  2894 
  3050 
  2895 /**
  3051 /**
  2896  * Displays the out of storage quota message in Multisite.
  3052  * Displays the out of storage quota message in Multisite.
  2897  *
  3053  *
  2898  * @since 3.5.0
  3054  * @since 3.5.0
  2899  */
  3055  */
  2900 function multisite_over_quota_message() {
  3056 function multisite_over_quota_message() {
  2901 	echo '<p>' . sprintf(
  3057 	echo '<p>' . sprintf(
  2902 		/* translators: %s: allowed space allocation */
  3058 		/* translators: %s: Allowed space allocation. */
  2903 		__( 'Sorry, you have used your space allocation of %s. Please delete some files to upload more files.' ),
  3059 		__( 'Sorry, you have used your space allocation of %s. Please delete some files to upload more files.' ),
  2904 		size_format( get_space_allowed() * MB_IN_BYTES )
  3060 		size_format( get_space_allowed() * MB_IN_BYTES )
  2905 	) . '</p>';
  3061 	) . '</p>';
  2906 }
  3062 }
  2907 
  3063 
  2912  *
  3068  *
  2913  * @param WP_Post $post A post object.
  3069  * @param WP_Post $post A post object.
  2914  */
  3070  */
  2915 function edit_form_image_editor( $post ) {
  3071 function edit_form_image_editor( $post ) {
  2916 	$open = isset( $_GET['image-editor'] );
  3072 	$open = isset( $_GET['image-editor'] );
       
  3073 
  2917 	if ( $open ) {
  3074 	if ( $open ) {
  2918 		require_once ABSPATH . 'wp-admin/includes/image-edit.php';
  3075 		require_once ABSPATH . 'wp-admin/includes/image-edit.php';
  2919 	}
  3076 	}
  2920 
  3077 
  2921 	$thumb_url = false;
  3078 	$thumb_url     = false;
  2922 	if ( $attachment_id = intval( $post->ID ) ) {
  3079 	$attachment_id = intval( $post->ID );
       
  3080 
       
  3081 	if ( $attachment_id ) {
  2923 		$thumb_url = wp_get_attachment_image_src( $attachment_id, array( 900, 450 ), true );
  3082 		$thumb_url = wp_get_attachment_image_src( $attachment_id, array( 900, 450 ), true );
  2924 	}
  3083 	}
  2925 
  3084 
  2926 	$alt_text = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
  3085 	$alt_text = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
  2927 
  3086 
  2928 	$att_url = wp_get_attachment_url( $post->ID );
  3087 	$att_url = wp_get_attachment_url( $post->ID );
  2929 	?>
  3088 	?>
  2930 	<div class="wp_attachment_holder wp-clearfix">
  3089 	<div class="wp_attachment_holder wp-clearfix">
  2931 	<?php
  3090 	<?php
       
  3091 
  2932 	if ( wp_attachment_is_image( $post->ID ) ) :
  3092 	if ( wp_attachment_is_image( $post->ID ) ) :
  2933 		$image_edit_button = '';
  3093 		$image_edit_button = '';
  2934 		if ( wp_image_editor_supports( array( 'mime_type' => $post->post_mime_type ) ) ) {
  3094 		if ( wp_image_editor_supports( array( 'mime_type' => $post->post_mime_type ) ) ) {
  2935 			$nonce             = wp_create_nonce( "image_editor-$post->ID" );
  3095 			$nonce             = wp_create_nonce( "image_editor-$post->ID" );
  2936 			$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>";
  3096 			$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>";
  2937 		}
  3097 		}
  2938 
  3098 
  2939 		$open_style = $not_open_style = '';
  3099 		$open_style     = '';
       
  3100 		$not_open_style = '';
       
  3101 
  2940 		if ( $open ) {
  3102 		if ( $open ) {
  2941 			$open_style = ' style="display:none"';
  3103 			$open_style = ' style="display:none"';
  2942 		} else {
  3104 		} else {
  2943 			$not_open_style = ' style="display:none"';
  3105 			$not_open_style = ' style="display:none"';
  2944 		}
  3106 		}
       
  3107 
  2945 		?>
  3108 		?>
  2946 
       
  2947 		<div class="imgedit-response" id="imgedit-response-<?php echo $attachment_id; ?>"></div>
  3109 		<div class="imgedit-response" id="imgedit-response-<?php echo $attachment_id; ?>"></div>
  2948 
  3110 
  2949 		<div<?php echo $open_style; ?> class="wp_attachment_image wp-clearfix" id="media-head-<?php echo $attachment_id; ?>">
  3111 		<div<?php echo $open_style; ?> class="wp_attachment_image wp-clearfix" id="media-head-<?php echo $attachment_id; ?>">
  2950 			<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>
  3112 			<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>
  2951 			<p><?php echo $image_edit_button; ?></p>
  3113 			<p><?php echo $image_edit_button; ?></p>
  2952 		</div>
  3114 		</div>
  2953 		<div<?php echo $not_open_style; ?> class="image-editor" id="image-editor-<?php echo $attachment_id; ?>">
  3115 		<div<?php echo $not_open_style; ?> class="image-editor" id="image-editor-<?php echo $attachment_id; ?>">
  2954 			<?php
  3116 		<?php
  2955 			if ( $open ) {
  3117 
  2956 				wp_image_editor( $attachment_id );}
  3118 		if ( $open ) {
  2957 			?>
  3119 			wp_image_editor( $attachment_id );
       
  3120 		}
       
  3121 
       
  3122 		?>
  2958 		</div>
  3123 		</div>
  2959 		<?php
  3124 		<?php
  2960 	elseif ( $attachment_id && wp_attachment_is( 'audio', $post ) ) :
  3125 	elseif ( $attachment_id && wp_attachment_is( 'audio', $post ) ) :
  2961 
  3126 
  2962 		wp_maybe_generate_attachment_metadata( $post );
  3127 		wp_maybe_generate_attachment_metadata( $post );
  2968 		wp_maybe_generate_attachment_metadata( $post );
  3133 		wp_maybe_generate_attachment_metadata( $post );
  2969 
  3134 
  2970 		$meta = wp_get_attachment_metadata( $attachment_id );
  3135 		$meta = wp_get_attachment_metadata( $attachment_id );
  2971 		$w    = ! empty( $meta['width'] ) ? min( $meta['width'], 640 ) : 0;
  3136 		$w    = ! empty( $meta['width'] ) ? min( $meta['width'], 640 ) : 0;
  2972 		$h    = ! empty( $meta['height'] ) ? $meta['height'] : 0;
  3137 		$h    = ! empty( $meta['height'] ) ? $meta['height'] : 0;
       
  3138 
  2973 		if ( $h && $w < $meta['width'] ) {
  3139 		if ( $h && $w < $meta['width'] ) {
  2974 			$h = round( ( $meta['height'] * $w ) / $meta['width'] );
  3140 			$h = round( ( $meta['height'] * $w ) / $meta['width'] );
  2975 		}
  3141 		}
  2976 
  3142 
  2977 		$attr = array( 'src' => $att_url );
  3143 		$attr = array( 'src' => $att_url );
       
  3144 
  2978 		if ( ! empty( $w ) && ! empty( $h ) ) {
  3145 		if ( ! empty( $w ) && ! empty( $h ) ) {
  2979 			$attr['width']  = $w;
  3146 			$attr['width']  = $w;
  2980 			$attr['height'] = $h;
  3147 			$attr['height'] = $h;
  2981 		}
  3148 		}
  2982 
  3149 
  2983 		$thumb_id = get_post_thumbnail_id( $attachment_id );
  3150 		$thumb_id = get_post_thumbnail_id( $attachment_id );
       
  3151 
  2984 		if ( ! empty( $thumb_id ) ) {
  3152 		if ( ! empty( $thumb_id ) ) {
  2985 			$attr['poster'] = wp_get_attachment_url( $thumb_id );
  3153 			$attr['poster'] = wp_get_attachment_url( $thumb_id );
  2986 		}
  3154 		}
  2987 
  3155 
  2988 		echo wp_video_shortcode( $attr );
  3156 		echo wp_video_shortcode( $attr );
  2989 
  3157 
  2990 	elseif ( isset( $thumb_url[0] ) ) :
  3158 	elseif ( isset( $thumb_url[0] ) ) :
  2991 
       
  2992 		?>
  3159 		?>
  2993 		<div class="wp_attachment_image wp-clearfix" id="media-head-<?php echo $attachment_id; ?>">
  3160 		<div class="wp_attachment_image wp-clearfix" id="media-head-<?php echo $attachment_id; ?>">
  2994 			<p id="thumbnail-head-<?php echo $attachment_id; ?>">
  3161 			<p id="thumbnail-head-<?php echo $attachment_id; ?>">
  2995 				<img class="thumbnail" src="<?php echo set_url_scheme( $thumb_url[0] ); ?>" style="max-width:100%" alt="" />
  3162 				<img class="thumbnail" src="<?php echo set_url_scheme( $thumb_url[0] ); ?>" style="max-width:100%" alt="" />
  2996 			</p>
  3163 			</p>
  3007 		 * @param WP_Post $post A post object.
  3174 		 * @param WP_Post $post A post object.
  3008 		 */
  3175 		 */
  3009 		do_action( 'wp_edit_form_attachment_display', $post );
  3176 		do_action( 'wp_edit_form_attachment_display', $post );
  3010 
  3177 
  3011 	endif;
  3178 	endif;
       
  3179 
  3012 	?>
  3180 	?>
  3013 	</div>
  3181 	</div>
  3014 	<div class="wp_attachment_details edit-form-section">
  3182 	<div class="wp_attachment_details edit-form-section">
  3015 	<?php if ( 'image' === substr( $post->post_mime_type, 0, 5 ) ) : ?>
  3183 	<?php if ( 'image' === substr( $post->post_mime_type, 0, 5 ) ) : ?>
  3016 		<p class="attachment-alt-text">
  3184 		<p class="attachment-alt-text">
  3017 			<label for="attachment_alt"><strong><?php _e( 'Alternative Text' ); ?></strong></label><br />
  3185 			<label for="attachment_alt"><strong><?php _e( 'Alternative Text' ); ?></strong></label><br />
  3018 			<input type="text" class="widefat" name="_wp_attachment_image_alt" id="attachment_alt" aria-describedby="alt-text-description" value="<?php echo esc_attr( $alt_text ); ?>" />
  3186 			<input type="text" class="widefat" name="_wp_attachment_image_alt" id="attachment_alt" aria-describedby="alt-text-description" value="<?php echo esc_attr( $alt_text ); ?>" />
  3019 		</p>
  3187 		</p>
  3020 		<p class="attachment-alt-text-description" id="alt-text-description">
  3188 		<p class="attachment-alt-text-description" id="alt-text-description">
  3021 			<?php
  3189 		<?php
  3022 			printf(
  3190 
  3023 				/* translators: 1: link to tutorial, 2: additional link attributes, 3: accessibility text */
  3191 		printf(
  3024 				__( '<a href="%1$s" %2$s>Describe the purpose of the image%3$s</a>. Leave empty if the image is purely decorative.' ),
  3192 			/* translators: 1: Link to tutorial, 2: Additional link attributes, 3: Accessibility text. */
  3025 				esc_url( 'https://www.w3.org/WAI/tutorials/images/decision-tree' ),
  3193 			__( '<a href="%1$s" %2$s>Describe the purpose of the image%3$s</a>. Leave empty if the image is purely decorative.' ),
  3026 				'target="_blank" rel="noopener noreferrer"',
  3194 			esc_url( 'https://www.w3.org/WAI/tutorials/images/decision-tree' ),
  3027 				sprintf(
  3195 			'target="_blank" rel="noopener noreferrer"',
  3028 					'<span class="screen-reader-text"> %s</span>',
  3196 			sprintf(
  3029 					/* translators: accessibility text */
  3197 				'<span class="screen-reader-text"> %s</span>',
  3030 					__( '(opens in a new tab)' )
  3198 				/* translators: Accessibility text. */
  3031 				)
  3199 				__( '(opens in a new tab)' )
  3032 			);
  3200 			)
  3033 			?>
  3201 		);
       
  3202 
       
  3203 		?>
  3034 		</p>
  3204 		</p>
  3035 	<?php endif; ?>
  3205 	<?php endif; ?>
  3036 
  3206 
  3037 		<p>
  3207 		<p>
  3038 			<label for="attachment_caption"><strong><?php _e( 'Caption' ); ?></strong></label><br />
  3208 			<label for="attachment_caption"><strong><?php _e( 'Caption' ); ?></strong></label><br />
  3039 			<textarea class="widefat" name="excerpt" id="attachment_caption"><?php echo $post->post_excerpt; ?></textarea>
  3209 			<textarea class="widefat" name="excerpt" id="attachment_caption"><?php echo $post->post_excerpt; ?></textarea>
  3040 		</p>
  3210 		</p>
  3041 
  3211 
  3042 	<?php
  3212 	<?php
  3043 		$quicktags_settings = array( 'buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,close' );
  3213 
  3044 		$editor_args        = array(
  3214 	$quicktags_settings = array( 'buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,close' );
  3045 			'textarea_name' => 'content',
  3215 	$editor_args        = array(
  3046 			'textarea_rows' => 5,
  3216 		'textarea_name' => 'content',
  3047 			'media_buttons' => false,
  3217 		'textarea_rows' => 5,
  3048 			'tinymce'       => false,
  3218 		'media_buttons' => false,
  3049 			'quicktags'     => $quicktags_settings,
  3219 		'tinymce'       => false,
  3050 		);
  3220 		'quicktags'     => $quicktags_settings,
       
  3221 	);
       
  3222 
  3051 	?>
  3223 	?>
  3052 
  3224 
  3053 	<label for="attachment_content"><strong><?php _e( 'Description' ); ?></strong>
  3225 	<label for="attachment_content" class="attachment-content-description"><strong><?php _e( 'Description' ); ?></strong>
  3054 		<?php
  3226 	<?php
  3055 		if ( preg_match( '#^(audio|video)/#', $post->post_mime_type ) ) {
  3227 
  3056 			echo ': ' . __( 'Displayed on attachment pages.' );
  3228 	if ( preg_match( '#^(audio|video)/#', $post->post_mime_type ) ) {
  3057 		}
  3229 		echo ': ' . __( 'Displayed on attachment pages.' );
  3058 		?>
  3230 	}
       
  3231 
       
  3232 	?>
  3059 	</label>
  3233 	</label>
  3060 	<?php wp_editor( $post->post_content, 'attachment_content', $editor_args ); ?>
  3234 	<?php wp_editor( format_to_edit( $post->post_content ), 'attachment_content', $editor_args ); ?>
  3061 
  3235 
  3062 	</div>
  3236 	</div>
  3063 	<?php
  3237 	<?php
       
  3238 
  3064 	$extras = get_compat_media_markup( $post->ID );
  3239 	$extras = get_compat_media_markup( $post->ID );
  3065 	echo $extras['item'];
  3240 	echo $extras['item'];
  3066 	echo '<input type="hidden" id="image-edit-context" value="edit-attachment" />' . "\n";
  3241 	echo '<input type="hidden" id="image-edit-context" value="edit-attachment" />' . "\n";
  3067 }
  3242 }
  3068 
  3243 
  3070  * Displays non-editable attachment metadata in the publish meta box.
  3245  * Displays non-editable attachment metadata in the publish meta box.
  3071  *
  3246  *
  3072  * @since 3.5.0
  3247  * @since 3.5.0
  3073  */
  3248  */
  3074 function attachment_submitbox_metadata() {
  3249 function attachment_submitbox_metadata() {
  3075 	$post = get_post();
  3250 	$post          = get_post();
  3076 
  3251 	$attachment_id = $post->ID;
  3077 	$file     = get_attached_file( $post->ID );
  3252 
       
  3253 	$file     = get_attached_file( $attachment_id );
  3078 	$filename = esc_html( wp_basename( $file ) );
  3254 	$filename = esc_html( wp_basename( $file ) );
  3079 
  3255 
  3080 	$media_dims = '';
  3256 	$media_dims = '';
  3081 	$meta       = wp_get_attachment_metadata( $post->ID );
  3257 	$meta       = wp_get_attachment_metadata( $attachment_id );
       
  3258 
  3082 	if ( isset( $meta['width'], $meta['height'] ) ) {
  3259 	if ( isset( $meta['width'], $meta['height'] ) ) {
  3083 		$media_dims .= "<span id='media-dims-$post->ID'>{$meta['width']}&nbsp;&times;&nbsp;{$meta['height']}</span> ";
  3260 		$media_dims .= "<span id='media-dims-$attachment_id'>{$meta['width']}&nbsp;&times;&nbsp;{$meta['height']}</span> ";
  3084 	}
  3261 	}
  3085 	/** This filter is documented in wp-admin/includes/media.php */
  3262 	/** This filter is documented in wp-admin/includes/media.php */
  3086 	$media_dims = apply_filters( 'media_meta', $media_dims, $post );
  3263 	$media_dims = apply_filters( 'media_meta', $media_dims, $post );
  3087 
  3264 
  3088 	$att_url = wp_get_attachment_url( $post->ID );
  3265 	$att_url = wp_get_attachment_url( $attachment_id );
       
  3266 
  3089 	?>
  3267 	?>
  3090 	<div class="misc-pub-section misc-pub-attachment">
  3268 	<div class="misc-pub-section misc-pub-attachment">
  3091 		<label for="attachment_url"><?php _e( 'File URL:' ); ?></label>
  3269 		<label for="attachment_url"><?php _e( 'File URL:' ); ?></label>
  3092 		<input type="text" class="widefat urlfield" readonly="readonly" name="attachment_url" id="attachment_url" value="<?php echo esc_attr( $att_url ); ?>" />
  3270 		<input type="text" class="widefat urlfield" readonly="readonly" name="attachment_url" id="attachment_url" value="<?php echo esc_attr( $att_url ); ?>" />
       
  3271 		<span class="copy-to-clipboard-container">
       
  3272 			<button type="button" class="button copy-attachment-url edit-media" data-clipboard-target="#attachment_url"><?php _e( 'Copy URL' ); ?></button>
       
  3273 			<span class="success hidden" aria-hidden="true"><?php _e( 'Copied!' ); ?></span>
       
  3274 		</span>
  3093 	</div>
  3275 	</div>
  3094 	<div class="misc-pub-section misc-pub-filename">
  3276 	<div class="misc-pub-section misc-pub-filename">
  3095 		<?php _e( 'File name:' ); ?> <strong><?php echo $filename; ?></strong>
  3277 		<?php _e( 'File name:' ); ?> <strong><?php echo $filename; ?></strong>
  3096 	</div>
  3278 	</div>
  3097 	<div class="misc-pub-section misc-pub-filetype">
  3279 	<div class="misc-pub-section misc-pub-filetype">
  3098 		<?php _e( 'File type:' ); ?> <strong>
  3280 		<?php _e( 'File type:' ); ?>
  3099 					<?php
  3281 		<strong>
  3100 					if ( preg_match( '/^.*?\.(\w+)$/', get_attached_file( $post->ID ), $matches ) ) {
  3282 		<?php
  3101 						echo esc_html( strtoupper( $matches[1] ) );
  3283 
  3102 						list( $mime_type ) = explode( '/', $post->post_mime_type );
  3284 		if ( preg_match( '/^.*?\.(\w+)$/', get_attached_file( $post->ID ), $matches ) ) {
  3103 						if ( $mime_type !== 'image' && ! empty( $meta['mime_type'] ) ) {
  3285 			echo esc_html( strtoupper( $matches[1] ) );
  3104 							if ( $meta['mime_type'] !== "$mime_type/" . strtolower( $matches[1] ) ) {
  3286 			list( $mime_type ) = explode( '/', $post->post_mime_type );
  3105 								echo ' (' . $meta['mime_type'] . ')';
  3287 			if ( 'image' !== $mime_type && ! empty( $meta['mime_type'] ) ) {
  3106 							}
  3288 				if ( "$mime_type/" . strtolower( $matches[1] ) !== $meta['mime_type'] ) {
  3107 						}
  3289 					echo ' (' . $meta['mime_type'] . ')';
  3108 					} else {
  3290 				}
  3109 						echo strtoupper( str_replace( 'image/', '', $post->post_mime_type ) );
  3291 			}
  3110 					}
  3292 		} else {
  3111 					?>
  3293 			echo strtoupper( str_replace( 'image/', '', $post->post_mime_type ) );
       
  3294 		}
       
  3295 
       
  3296 		?>
  3112 		</strong>
  3297 		</strong>
  3113 	</div>
  3298 	</div>
  3114 
  3299 
  3115 	<?php
  3300 	<?php
  3116 		$file_size = false;
  3301 
       
  3302 	$file_size = false;
  3117 
  3303 
  3118 	if ( isset( $meta['filesize'] ) ) {
  3304 	if ( isset( $meta['filesize'] ) ) {
  3119 		$file_size = $meta['filesize'];
  3305 		$file_size = $meta['filesize'];
  3120 	} elseif ( file_exists( $file ) ) {
  3306 	} elseif ( file_exists( $file ) ) {
  3121 		$file_size = filesize( $file );
  3307 		$file_size = filesize( $file );
  3122 	}
  3308 	}
  3123 
  3309 
  3124 	if ( ! empty( $file_size ) ) :
  3310 	if ( ! empty( $file_size ) ) {
  3125 		?>
  3311 		?>
  3126 			<div class="misc-pub-section misc-pub-filesize">
  3312 		<div class="misc-pub-section misc-pub-filesize">
  3127 				<?php _e( 'File size:' ); ?> <strong><?php echo size_format( $file_size ); ?></strong>
  3313 			<?php _e( 'File size:' ); ?> <strong><?php echo size_format( $file_size ); ?></strong>
  3128 			</div>
  3314 		</div>
  3129 			<?php
  3315 		<?php
  3130 		endif;
  3316 	}
  3131 
  3317 
  3132 	if ( preg_match( '#^(audio|video)/#', $post->post_mime_type ) ) {
  3318 	if ( preg_match( '#^(audio|video)/#', $post->post_mime_type ) ) {
  3133 		$fields = array(
  3319 		$fields = array(
  3134 			'length_formatted' => __( 'Length:' ),
  3320 			'length_formatted' => __( 'Length:' ),
  3135 			'bitrate'          => __( 'Bitrate:' ),
  3321 			'bitrate'          => __( 'Bitrate:' ),
  3151 
  3337 
  3152 		foreach ( $fields as $key => $label ) {
  3338 		foreach ( $fields as $key => $label ) {
  3153 			if ( empty( $meta[ $key ] ) ) {
  3339 			if ( empty( $meta[ $key ] ) ) {
  3154 				continue;
  3340 				continue;
  3155 			}
  3341 			}
       
  3342 
  3156 			?>
  3343 			?>
  3157 		<div class="misc-pub-section misc-pub-mime-meta misc-pub-<?php echo sanitize_html_class( $key ); ?>">
  3344 			<div class="misc-pub-section misc-pub-mime-meta misc-pub-<?php echo sanitize_html_class( $key ); ?>">
  3158 			<?php echo $label; ?> <strong>
  3345 				<?php echo $label; ?>
  3159 						<?php
  3346 				<strong>
  3160 						switch ( $key ) {
  3347 				<?php
  3161 							case 'bitrate':
  3348 
  3162 								echo round( $meta['bitrate'] / 1000 ) . 'kb/s';
  3349 				switch ( $key ) {
  3163 								if ( ! empty( $meta['bitrate_mode'] ) ) {
  3350 					case 'bitrate':
  3164 									echo ' ' . strtoupper( esc_html( $meta['bitrate_mode'] ) );
  3351 						echo round( $meta['bitrate'] / 1000 ) . 'kb/s';
  3165 								}
  3352 						if ( ! empty( $meta['bitrate_mode'] ) ) {
  3166 								break;
  3353 							echo ' ' . strtoupper( esc_html( $meta['bitrate_mode'] ) );
  3167 							default:
       
  3168 								echo esc_html( $meta[ $key ] );
       
  3169 								break;
       
  3170 						}
  3354 						}
  3171 						?>
  3355 						break;
  3172 			</strong>
  3356 					default:
  3173 		</div>
  3357 						echo esc_html( $meta[ $key ] );
       
  3358 						break;
       
  3359 				}
       
  3360 
       
  3361 				?>
       
  3362 				</strong>
       
  3363 			</div>
  3174 			<?php
  3364 			<?php
  3175 		}
  3365 		}
  3176 
  3366 
  3177 		$fields = array(
  3367 		$fields = array(
  3178 			'dataformat' => __( 'Audio Format:' ),
  3368 			'dataformat' => __( 'Audio Format:' ),
  3195 
  3385 
  3196 		foreach ( $audio_fields as $key => $label ) {
  3386 		foreach ( $audio_fields as $key => $label ) {
  3197 			if ( empty( $meta['audio'][ $key ] ) ) {
  3387 			if ( empty( $meta['audio'][ $key ] ) ) {
  3198 				continue;
  3388 				continue;
  3199 			}
  3389 			}
       
  3390 
  3200 			?>
  3391 			?>
  3201 		<div class="misc-pub-section misc-pub-audio misc-pub-<?php echo sanitize_html_class( $key ); ?>">
  3392 			<div class="misc-pub-section misc-pub-audio misc-pub-<?php echo sanitize_html_class( $key ); ?>">
  3202 			<?php echo $label; ?> <strong><?php echo esc_html( $meta['audio'][ $key ] ); ?></strong>
  3393 				<?php echo $label; ?> <strong><?php echo esc_html( $meta['audio'][ $key ] ); ?></strong>
       
  3394 			</div>
       
  3395 			<?php
       
  3396 		}
       
  3397 	}
       
  3398 
       
  3399 	if ( $media_dims ) {
       
  3400 		?>
       
  3401 		<div class="misc-pub-section misc-pub-dimensions">
       
  3402 			<?php _e( 'Dimensions:' ); ?> <strong><?php echo $media_dims; ?></strong>
  3203 		</div>
  3403 		</div>
  3204 			<?php
  3404 		<?php
  3205 		}
  3405 	}
  3206 	}
  3406 
  3207 
  3407 	if ( ! empty( $meta['original_image'] ) ) {
  3208 	if ( $media_dims ) :
       
  3209 		?>
  3408 		?>
  3210 	<div class="misc-pub-section misc-pub-dimensions">
  3409 		<div class="misc-pub-section misc-pub-original-image">
  3211 		<?php _e( 'Dimensions:' ); ?> <strong><?php echo $media_dims; ?></strong>
  3410 			<?php _e( 'Original image:' ); ?>
  3212 	</div>
  3411 			<a href="<?php echo esc_url( wp_get_original_image_url( $attachment_id ) ); ?>">
       
  3412 				<?php echo esc_html( wp_basename( wp_get_original_image_path( $attachment_id ) ) ); ?>
       
  3413 			</a>
       
  3414 		</div>
  3213 		<?php
  3415 		<?php
  3214 	endif;
  3416 	}
  3215 }
  3417 }
  3216 
  3418 
  3217 /**
  3419 /**
  3218  * Parse ID3v2, ID3v1, and getID3 comments to extract usable data
  3420  * Parse ID3v2, ID3v1, and getID3 comments to extract usable data
  3219  *
  3421  *
  3277 	if ( ! defined( 'GETID3_TEMP_DIR' ) ) {
  3479 	if ( ! defined( 'GETID3_TEMP_DIR' ) ) {
  3278 		define( 'GETID3_TEMP_DIR', get_temp_dir() );
  3480 		define( 'GETID3_TEMP_DIR', get_temp_dir() );
  3279 	}
  3481 	}
  3280 
  3482 
  3281 	if ( ! class_exists( 'getID3', false ) ) {
  3483 	if ( ! class_exists( 'getID3', false ) ) {
  3282 		require( ABSPATH . WPINC . '/ID3/getid3.php' );
  3484 		require ABSPATH . WPINC . '/ID3/getid3.php';
  3283 	}
  3485 	}
       
  3486 
  3284 	$id3  = new getID3();
  3487 	$id3  = new getID3();
  3285 	$data = $id3->analyze( $file );
  3488 	$data = $id3->analyze( $file );
  3286 
  3489 
  3287 	if ( isset( $data['video']['lossless'] ) ) {
  3490 	if ( isset( $data['video']['lossless'] ) ) {
  3288 		$metadata['lossless'] = $data['video']['lossless'];
  3491 		$metadata['lossless'] = $data['video']['lossless'];
  3289 	}
  3492 	}
       
  3493 
  3290 	if ( ! empty( $data['video']['bitrate'] ) ) {
  3494 	if ( ! empty( $data['video']['bitrate'] ) ) {
  3291 		$metadata['bitrate'] = (int) $data['video']['bitrate'];
  3495 		$metadata['bitrate'] = (int) $data['video']['bitrate'];
  3292 	}
  3496 	}
       
  3497 
  3293 	if ( ! empty( $data['video']['bitrate_mode'] ) ) {
  3498 	if ( ! empty( $data['video']['bitrate_mode'] ) ) {
  3294 		$metadata['bitrate_mode'] = $data['video']['bitrate_mode'];
  3499 		$metadata['bitrate_mode'] = $data['video']['bitrate_mode'];
  3295 	}
  3500 	}
       
  3501 
  3296 	if ( ! empty( $data['filesize'] ) ) {
  3502 	if ( ! empty( $data['filesize'] ) ) {
  3297 		$metadata['filesize'] = (int) $data['filesize'];
  3503 		$metadata['filesize'] = (int) $data['filesize'];
  3298 	}
  3504 	}
       
  3505 
  3299 	if ( ! empty( $data['mime_type'] ) ) {
  3506 	if ( ! empty( $data['mime_type'] ) ) {
  3300 		$metadata['mime_type'] = $data['mime_type'];
  3507 		$metadata['mime_type'] = $data['mime_type'];
  3301 	}
  3508 	}
       
  3509 
  3302 	if ( ! empty( $data['playtime_seconds'] ) ) {
  3510 	if ( ! empty( $data['playtime_seconds'] ) ) {
  3303 		$metadata['length'] = (int) round( $data['playtime_seconds'] );
  3511 		$metadata['length'] = (int) round( $data['playtime_seconds'] );
  3304 	}
  3512 	}
       
  3513 
  3305 	if ( ! empty( $data['playtime_string'] ) ) {
  3514 	if ( ! empty( $data['playtime_string'] ) ) {
  3306 		$metadata['length_formatted'] = $data['playtime_string'];
  3515 		$metadata['length_formatted'] = $data['playtime_string'];
  3307 	}
  3516 	}
       
  3517 
  3308 	if ( ! empty( $data['video']['resolution_x'] ) ) {
  3518 	if ( ! empty( $data['video']['resolution_x'] ) ) {
  3309 		$metadata['width'] = (int) $data['video']['resolution_x'];
  3519 		$metadata['width'] = (int) $data['video']['resolution_x'];
  3310 	}
  3520 	}
       
  3521 
  3311 	if ( ! empty( $data['video']['resolution_y'] ) ) {
  3522 	if ( ! empty( $data['video']['resolution_y'] ) ) {
  3312 		$metadata['height'] = (int) $data['video']['resolution_y'];
  3523 		$metadata['height'] = (int) $data['video']['resolution_y'];
  3313 	}
  3524 	}
       
  3525 
  3314 	if ( ! empty( $data['fileformat'] ) ) {
  3526 	if ( ! empty( $data['fileformat'] ) ) {
  3315 		$metadata['fileformat'] = $data['fileformat'];
  3527 		$metadata['fileformat'] = $data['fileformat'];
  3316 	}
  3528 	}
       
  3529 
  3317 	if ( ! empty( $data['video']['dataformat'] ) ) {
  3530 	if ( ! empty( $data['video']['dataformat'] ) ) {
  3318 		$metadata['dataformat'] = $data['video']['dataformat'];
  3531 		$metadata['dataformat'] = $data['video']['dataformat'];
  3319 	}
  3532 	}
       
  3533 
  3320 	if ( ! empty( $data['video']['encoder'] ) ) {
  3534 	if ( ! empty( $data['video']['encoder'] ) ) {
  3321 		$metadata['encoder'] = $data['video']['encoder'];
  3535 		$metadata['encoder'] = $data['video']['encoder'];
  3322 	}
  3536 	}
       
  3537 
  3323 	if ( ! empty( $data['video']['codec'] ) ) {
  3538 	if ( ! empty( $data['video']['codec'] ) ) {
  3324 		$metadata['codec'] = $data['video']['codec'];
  3539 		$metadata['codec'] = $data['video']['codec'];
  3325 	}
  3540 	}
  3326 
  3541 
  3327 	if ( ! empty( $data['audio'] ) ) {
  3542 	if ( ! empty( $data['audio'] ) ) {
  3330 	}
  3545 	}
  3331 
  3546 
  3332 	if ( empty( $metadata['created_timestamp'] ) ) {
  3547 	if ( empty( $metadata['created_timestamp'] ) ) {
  3333 		$created_timestamp = wp_get_media_creation_timestamp( $data );
  3548 		$created_timestamp = wp_get_media_creation_timestamp( $data );
  3334 
  3549 
  3335 		if ( $created_timestamp !== false ) {
  3550 		if ( false !== $created_timestamp ) {
  3336 			$metadata['created_timestamp'] = $created_timestamp;
  3551 			$metadata['created_timestamp'] = $created_timestamp;
  3337 		}
  3552 		}
  3338 	}
  3553 	}
  3339 
  3554 
  3340 	wp_add_id3_tag_data( $metadata, $data );
  3555 	wp_add_id3_tag_data( $metadata, $data );
  3367  */
  3582  */
  3368 function wp_read_audio_metadata( $file ) {
  3583 function wp_read_audio_metadata( $file ) {
  3369 	if ( ! file_exists( $file ) ) {
  3584 	if ( ! file_exists( $file ) ) {
  3370 		return false;
  3585 		return false;
  3371 	}
  3586 	}
       
  3587 
  3372 	$metadata = array();
  3588 	$metadata = array();
  3373 
  3589 
  3374 	if ( ! defined( 'GETID3_TEMP_DIR' ) ) {
  3590 	if ( ! defined( 'GETID3_TEMP_DIR' ) ) {
  3375 		define( 'GETID3_TEMP_DIR', get_temp_dir() );
  3591 		define( 'GETID3_TEMP_DIR', get_temp_dir() );
  3376 	}
  3592 	}
  3377 
  3593 
  3378 	if ( ! class_exists( 'getID3', false ) ) {
  3594 	if ( ! class_exists( 'getID3', false ) ) {
  3379 		require( ABSPATH . WPINC . '/ID3/getid3.php' );
  3595 		require ABSPATH . WPINC . '/ID3/getid3.php';
  3380 	}
  3596 	}
       
  3597 
  3381 	$id3  = new getID3();
  3598 	$id3  = new getID3();
  3382 	$data = $id3->analyze( $file );
  3599 	$data = $id3->analyze( $file );
  3383 
  3600 
  3384 	if ( ! empty( $data['audio'] ) ) {
  3601 	if ( ! empty( $data['audio'] ) ) {
  3385 		unset( $data['audio']['streams'] );
  3602 		unset( $data['audio']['streams'] );
  3387 	}
  3604 	}
  3388 
  3605 
  3389 	if ( ! empty( $data['fileformat'] ) ) {
  3606 	if ( ! empty( $data['fileformat'] ) ) {
  3390 		$metadata['fileformat'] = $data['fileformat'];
  3607 		$metadata['fileformat'] = $data['fileformat'];
  3391 	}
  3608 	}
       
  3609 
  3392 	if ( ! empty( $data['filesize'] ) ) {
  3610 	if ( ! empty( $data['filesize'] ) ) {
  3393 		$metadata['filesize'] = (int) $data['filesize'];
  3611 		$metadata['filesize'] = (int) $data['filesize'];
  3394 	}
  3612 	}
       
  3613 
  3395 	if ( ! empty( $data['mime_type'] ) ) {
  3614 	if ( ! empty( $data['mime_type'] ) ) {
  3396 		$metadata['mime_type'] = $data['mime_type'];
  3615 		$metadata['mime_type'] = $data['mime_type'];
  3397 	}
  3616 	}
       
  3617 
  3398 	if ( ! empty( $data['playtime_seconds'] ) ) {
  3618 	if ( ! empty( $data['playtime_seconds'] ) ) {
  3399 		$metadata['length'] = (int) round( $data['playtime_seconds'] );
  3619 		$metadata['length'] = (int) round( $data['playtime_seconds'] );
  3400 	}
  3620 	}
       
  3621 
  3401 	if ( ! empty( $data['playtime_string'] ) ) {
  3622 	if ( ! empty( $data['playtime_string'] ) ) {
  3402 		$metadata['length_formatted'] = $data['playtime_string'];
  3623 		$metadata['length_formatted'] = $data['playtime_string'];
  3403 	}
  3624 	}
  3404 
  3625 
  3405 	if ( empty( $metadata['created_timestamp'] ) ) {
  3626 	if ( empty( $metadata['created_timestamp'] ) ) {
  3462 
  3683 
  3463 	return $creation_date;
  3684 	return $creation_date;
  3464 }
  3685 }
  3465 
  3686 
  3466 /**
  3687 /**
  3467  * Encapsulate logic for Attach/Detach actions
  3688  * Encapsulates the logic for Attach/Detach actions.
  3468  *
  3689  *
  3469  * @since 4.2.0
  3690  * @since 4.2.0
  3470  *
  3691  *
  3471  * @global wpdb $wpdb WordPress database abstraction object.
  3692  * @global wpdb $wpdb WordPress database abstraction object.
  3472  *
  3693  *
  3482 	}
  3703 	}
  3483 
  3704 
  3484 	if ( ! current_user_can( 'edit_post', $parent_id ) ) {
  3705 	if ( ! current_user_can( 'edit_post', $parent_id ) ) {
  3485 		wp_die( __( 'Sorry, you are not allowed to edit this post.' ) );
  3706 		wp_die( __( 'Sorry, you are not allowed to edit this post.' ) );
  3486 	}
  3707 	}
       
  3708 
  3487 	$ids = array();
  3709 	$ids = array();
  3488 	foreach ( (array) $_REQUEST['media'] as $att_id ) {
  3710 
  3489 		$att_id = (int) $att_id;
  3711 	foreach ( (array) $_REQUEST['media'] as $attachment_id ) {
  3490 
  3712 		$attachment_id = (int) $attachment_id;
  3491 		if ( ! current_user_can( 'edit_post', $att_id ) ) {
  3713 
       
  3714 		if ( ! current_user_can( 'edit_post', $attachment_id ) ) {
  3492 			continue;
  3715 			continue;
  3493 		}
  3716 		}
  3494 
  3717 
  3495 		$ids[] = $att_id;
  3718 		$ids[] = $attachment_id;
  3496 	}
  3719 	}
  3497 
  3720 
  3498 	if ( ! empty( $ids ) ) {
  3721 	if ( ! empty( $ids ) ) {
  3499 		$ids_string = implode( ',', $ids );
  3722 		$ids_string = implode( ',', $ids );
       
  3723 
  3500 		if ( 'attach' === $action ) {
  3724 		if ( 'attach' === $action ) {
  3501 			$result = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_parent = %d WHERE post_type = 'attachment' AND ID IN ( $ids_string )", $parent_id ) );
  3725 			$result = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_parent = %d WHERE post_type = 'attachment' AND ID IN ( $ids_string )", $parent_id ) );
  3502 		} else {
  3726 		} else {
  3503 			$result = $wpdb->query( "UPDATE $wpdb->posts SET post_parent = 0 WHERE post_type = 'attachment' AND ID IN ( $ids_string )" );
  3727 			$result = $wpdb->query( "UPDATE $wpdb->posts SET post_parent = 0 WHERE post_type = 'attachment' AND ID IN ( $ids_string )" );
  3504 		}
  3728 		}
  3505 
       
  3506 		foreach ( $ids as $att_id ) {
       
  3507 			clean_attachment_cache( $att_id );
       
  3508 		}
       
  3509 	}
  3729 	}
  3510 
  3730 
  3511 	if ( isset( $result ) ) {
  3731 	if ( isset( $result ) ) {
       
  3732 		foreach ( $ids as $attachment_id ) {
       
  3733 			/**
       
  3734 			 * Fires when media is attached or detached from a post.
       
  3735 			 *
       
  3736 			 * @since 5.5.0
       
  3737 			 *
       
  3738 			 * @param string $action        Attach/detach action. Accepts 'attach' or 'detach'.
       
  3739 			 * @param int    $attachment_id The attachment ID.
       
  3740 			 * @param int    $parent_id     Attachment parent ID.
       
  3741 			 */
       
  3742 			do_action( 'wp_media_attach_action', $action, $attachment_id, $parent_id );
       
  3743 
       
  3744 			clean_attachment_cache( $attachment_id );
       
  3745 		}
       
  3746 
  3512 		$location = 'upload.php';
  3747 		$location = 'upload.php';
  3513 		if ( $referer = wp_get_referer() ) {
  3748 		$referer  = wp_get_referer();
       
  3749 
       
  3750 		if ( $referer ) {
  3514 			if ( false !== strpos( $referer, 'upload.php' ) ) {
  3751 			if ( false !== strpos( $referer, 'upload.php' ) ) {
  3515 				$location = remove_query_arg( array( 'attached', 'detach' ), $referer );
  3752 				$location = remove_query_arg( array( 'attached', 'detach' ), $referer );
  3516 			}
  3753 			}
  3517 		}
  3754 		}
  3518 
  3755 
  3519 		$key      = 'attach' === $action ? 'attached' : 'detach';
  3756 		$key      = 'attach' === $action ? 'attached' : 'detach';
  3520 		$location = add_query_arg( array( $key => $result ), $location );
  3757 		$location = add_query_arg( array( $key => $result ), $location );
       
  3758 
  3521 		wp_redirect( $location );
  3759 		wp_redirect( $location );
  3522 		exit;
  3760 		exit;
  3523 	}
  3761 	}
  3524 }
  3762 }