wp/wp-admin/includes/media.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
--- a/wp/wp-admin/includes/media.php	Wed Sep 21 18:19:35 2022 +0200
+++ b/wp/wp-admin/includes/media.php	Tue Sep 27 16:37:53 2022 +0200
@@ -7,7 +7,7 @@
  */
 
 /**
- * Defines the default media upload tabs
+ * Defines the default media upload tabs.
  *
  * @since 2.5.0
  *
@@ -32,7 +32,7 @@
 }
 
 /**
- * Adds the gallery tab back to the tabs array if post has image attachments
+ * Adds the gallery tab back to the tabs array if post has image attachments.
  *
  * @since 2.5.0
  *
@@ -282,7 +282,7 @@
  *
  * @since 2.5.0
  *
- * @param string $file_id   Index of the `$_FILES` array that the file was sent. Required.
+ * @param string $file_id   Index of the `$_FILES` array that the file was sent.
  * @param int    $post_id   The post ID of a post to attach the media item to. Required, but can
  *                          be set to 0, creating a media item that has no relationship to a post.
  * @param array  $post_data Optional. Overwrite some of the attachment.
@@ -361,12 +361,21 @@
 		if ( ! empty( $meta['track_number'] ) ) {
 			$track_number = explode( '/', $meta['track_number'] );
 
-			if ( isset( $track_number[1] ) ) {
-				/* translators: Audio file track information. 1: Audio track number, 2: Total audio tracks. */
-				$content .= ' ' . sprintf( __( 'Track %1$s of %2$s.' ), number_format_i18n( $track_number[0] ), number_format_i18n( $track_number[1] ) );
-			} else {
-				/* translators: Audio file track information. %s: Audio track number. */
-				$content .= ' ' . sprintf( __( 'Track %s.' ), number_format_i18n( $track_number[0] ) );
+			if ( is_numeric( $track_number[0] ) ) {
+				if ( isset( $track_number[1] ) && is_numeric( $track_number[1] ) ) {
+					$content .= ' ' . sprintf(
+						/* translators: Audio file track information. 1: Audio track number, 2: Total audio tracks. */
+						__( 'Track %1$s of %2$s.' ),
+						number_format_i18n( $track_number[0] ),
+						number_format_i18n( $track_number[1] )
+					);
+				} else {
+					$content .= ' ' . sprintf(
+						/* translators: Audio file track information. %s: Audio track number. */
+						__( 'Track %s.' ),
+						number_format_i18n( $track_number[0] )
+					);
+				}
 			}
 		}
 
@@ -533,7 +542,7 @@
 
 	?>
 	<script type="text/javascript">
-	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();}}};
+	addLoadEvent = function(func){if(typeof jQuery!=='undefined')jQuery(function(){func();});else if(typeof wpOnload!=='function'){wpOnload=func;}else{var oldonload=wpOnload;wpOnload=function(){oldonload();func();}}};
 	var ajaxurl = '<?php echo esc_js( admin_url( 'admin-ajax.php', 'relative' ) ); ?>', pagenow = 'media-upload-popup', adminpage = 'media-upload-popup',
 	isRtl = <?php echo (int) is_rtl(); ?>;
 	</script>
@@ -577,10 +586,8 @@
 		 * Fires in the admin header for each specific form tab in the legacy
 		 * (pre-3.5.0) media upload popup.
 		 *
-		 * The dynamic portion of the hook, `$content_func`, refers to the form
-		 * callback for the media upload type. Possible values include
-		 * 'media_upload_type_form', 'media_upload_type_url_form', and
-		 * 'media_upload_library_form'.
+		 * The dynamic portion of the hook name, `$content_func`, refers to the form
+		 * callback for the media upload type.
 		 *
 		 * @since 2.5.0
 		 */
@@ -614,7 +621,7 @@
 }
 
 /**
- * Adds the media button to the editor
+ * Adds the media button to the editor.
  *
  * @since 2.5.0
  *
@@ -715,7 +722,7 @@
  *
  * @since 2.5.0
  *
- * @return mixed void|object WP_Error on failure
+ * @return null|array|void Array of error messages keyed by attachment ID, null or void on success.
  */
 function media_upload_form_handler() {
 	check_admin_referer( 'media-form' );
@@ -976,21 +983,21 @@
  * Downloads an image from the specified URL, saves it as an attachment, and optionally attaches it to a post.
  *
  * @since 2.6.0
- * @since 4.2.0 Introduced the `$return` parameter.
- * @since 4.8.0 Introduced the 'id' option for the `$return` parameter.
+ * @since 4.2.0 Introduced the `$return_type` parameter.
+ * @since 4.8.0 Introduced the 'id' option for the `$return_type` parameter.
  * @since 5.3.0 The `$post_id` parameter was made optional.
  * @since 5.4.0 The original URL of the attachment is stored in the `_source_url`
  *              post meta value.
  *
- * @param string $file    The URL of the image to download.
- * @param int    $post_id Optional. The post ID the media is to be associated with.
- * @param string $desc    Optional. Description of the image.
- * @param string $return  Optional. Accepts 'html' (image tag html) or 'src' (URL),
- *                        or 'id' (attachment ID). Default 'html'.
+ * @param string $file        The URL of the image to download.
+ * @param int    $post_id     Optional. The post ID the media is to be associated with.
+ * @param string $desc        Optional. Description of the image.
+ * @param string $return_type Optional. Accepts 'html' (image tag html) or 'src' (URL),
+ *                            or 'id' (attachment ID). Default 'html'.
  * @return string|int|WP_Error Populated HTML img tag, attachment ID, or attachment source
  *                             on success, WP_Error object otherwise.
  */
-function media_sideload_image( $file, $post_id = 0, $desc = null, $return = 'html' ) {
+function media_sideload_image( $file, $post_id = 0, $desc = null, $return_type = 'html' ) {
 	if ( ! empty( $file ) ) {
 
 		$allowed_extensions = array( 'jpg', 'jpeg', 'jpe', 'png', 'gif', 'webp' );
@@ -1045,7 +1052,7 @@
 		add_post_meta( $id, '_source_url', $file );
 
 		// If attachment ID was requested, return it.
-		if ( 'id' === $return ) {
+		if ( 'id' === $return_type ) {
 			return $id;
 		}
 
@@ -1054,7 +1061,7 @@
 
 	// Finally, check to make sure the file has been saved, then return the HTML.
 	if ( ! empty( $src ) ) {
-		if ( 'src' === $return ) {
+		if ( 'src' === $return_type ) {
 			return $src;
 		}
 
@@ -1118,7 +1125,7 @@
 }
 
 /**
- * Retrieve HTML for the image alignment radio buttons with the specified one checked.
+ * Retrieves HTML for the image alignment radio buttons with the specified one checked.
  *
  * @since 2.7.0
  *
@@ -1156,7 +1163,7 @@
 }
 
 /**
- * Retrieve HTML for the size radio buttons with the specified one checked.
+ * Retrieves HTML for the size radio buttons with the specified one checked.
  *
  * @since 2.7.0
  *
@@ -1234,7 +1241,7 @@
 }
 
 /**
- * Retrieve HTML for the Link URL buttons with the default link type as specified.
+ * Retrieves HTML for the Link URL buttons with the default link type as specified.
  *
  * @since 2.7.0
  *
@@ -1268,7 +1275,7 @@
 }
 
 /**
- * Output a textarea element for inputting an attachment caption.
+ * Outputs a textarea element for inputting an attachment caption.
  *
  * @since 3.4.0
  *
@@ -1324,31 +1331,6 @@
 }
 
 /**
- * Filters input from media_upload_form_handler() and assigns a default
- * post_title from the file name if none supplied.
- *
- * Illustrates the use of the {@see 'attachment_fields_to_save'} filter
- * which can be used to add default values to any field before saving to DB.
- *
- * @since 2.5.0
- *
- * @param array $post       The WP_Post attachment object converted to an array.
- * @param array $attachment An array of attachment metadata.
- * @return array Filtered attachment post object.
- */
-function image_attachment_fields_to_save( $post, $attachment ) {
-	if ( 'image' === substr( $post['post_mime_type'], 0, 5 ) ) {
-		if ( strlen( trim( $post['post_title'] ) ) == 0 ) {
-			$attachment_url                           = ( isset( $post['attachment_url'] ) ) ? $post['attachment_url'] : $post['guid'];
-			$post['post_title']                       = preg_replace( '/\.\w+$/', '', wp_basename( $attachment_url ) );
-			$post['errors']['post_title']['errors'][] = __( 'Empty Title filled from filename.' );
-		}
-	}
-
-	return $post;
-}
-
-/**
  * Retrieves the media element HTML to send to the editor.
  *
  * @since 2.5.0
@@ -1513,7 +1495,7 @@
 }
 
 /**
- * Retrieve HTML for media items of post gallery.
+ * Retrieves HTML for media items of post gallery.
  *
  * The HTML markup retrieved will be created for the progress of SWF Upload
  * component. Will also create link for showing and hiding the form to modify
@@ -1523,9 +1505,9 @@
  *
  * @global WP_Query $wp_the_query WordPress Query object.
  *
- * @param int   $post_id Optional. Post ID.
+ * @param int   $post_id Post ID.
  * @param array $errors  Errors for attachment, if any.
- * @return string
+ * @return string HTML content for media items of post gallery.
  */
 function get_media_items( $post_id, $errors ) {
 	$attachments = array();
@@ -1570,7 +1552,7 @@
 }
 
 /**
- * Retrieve HTML form for modifying the image attachment.
+ * Retrieves HTML form for modifying the image attachment.
  *
  * @since 2.5.0
  *
@@ -2503,11 +2485,11 @@
 	}
 	};
 
-	jQuery(document).ready( function($) {
+	jQuery( function($) {
 		$('.media-types input').click( function() {
 			$('table.describe').toggleClass('not-image', $('#not-image').prop('checked') );
 		});
-	});
+	} );
 	</script>
 
 	<div id="media-items">
@@ -2530,7 +2512,7 @@
 }
 
 /**
- * Adds gallery form to upload iframe
+ * Adds gallery form to upload iframe.
  *
  * @since 2.5.0
  *
@@ -2579,7 +2561,6 @@
 	</div>
 	<form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="gallery-form">
 		<?php wp_nonce_field( 'media-form' ); ?>
-		<?php // media_upload_form( $errors ); ?>
 	<table class="widefat">
 	<thead><tr>
 	<th><?php _e( 'Media' ); ?></th>
@@ -2881,7 +2862,6 @@
 
 	<form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="library-form">
 	<?php wp_nonce_field( 'media-form' ); ?>
-	<?php // media_upload_form( $errors ); ?>
 
 	<script type="text/javascript">
 	jQuery(function($){
@@ -2906,12 +2886,12 @@
 }
 
 /**
- * Creates the form for external url
+ * Creates the form for external url.
  *
  * @since 2.7.0
  *
  * @param string $default_view
- * @return string the form html
+ * @return string HTML content of the form.
  */
 function wp_media_insert_url_form( $default_view = 'image' ) {
 	/** This filter is documented in wp-admin/includes/media.php */
@@ -3232,7 +3212,7 @@
 
 		printf(
 			/* translators: 1: Link to tutorial, 2: Additional link attributes, 3: Accessibility text. */
-			__( '<a href="%1$s" %2$s>Describe the purpose of the image%3$s</a>. Leave empty if the image is purely decorative.' ),
+			__( '<a href="%1$s" %2$s>Learn how to describe the purpose of the image%3$s</a>. Leave empty if the image is purely decorative.' ),
 			esc_url( 'https://www.w3.org/WAI/tutorials/images/decision-tree' ),
 			'target="_blank" rel="noopener"',
 			sprintf(
@@ -3382,7 +3362,7 @@
 	if ( isset( $meta['filesize'] ) ) {
 		$file_size = $meta['filesize'];
 	} elseif ( file_exists( $file ) ) {
-		$file_size = filesize( $file );
+		$file_size = wp_filesize( $file );
 	}
 
 	if ( ! empty( $file_size ) ) {
@@ -3495,12 +3475,12 @@
 }
 
 /**
- * Parse ID3v2, ID3v1, and getID3 comments to extract usable data
+ * Parses ID3v2, ID3v1, and getID3 comments to extract usable data.
  *
  * @since 3.6.0
  *
- * @param array $metadata An existing array with data
- * @param array $data Data supplied by ID3 tags
+ * @param array $metadata An existing array with data.
+ * @param array $data Data supplied by ID3 tags.
  */
 function wp_add_id3_tag_data( &$metadata, $data ) {
 	foreach ( array( 'id3v2', 'id3v1' ) as $version ) {
@@ -3540,7 +3520,7 @@
 }
 
 /**
- * Retrieve metadata from a video file's ID3 tags
+ * Retrieves metadata from a video file's ID3 tags.
  *
  * @since 3.6.0
  *
@@ -3562,7 +3542,10 @@
 		require ABSPATH . WPINC . '/ID3/getid3.php';
 	}
 
-	$id3  = new getID3();
+	$id3 = new getID3();
+	// Required to get the `created_timestamp` value.
+	$id3->options_audiovideo_quicktime_ReturnAtomData = true; // phpcs:ignore WordPress.NamingConventions.ValidVariableName
+
 	$data = $id3->analyze( $file );
 
 	if ( isset( $data['video']['lossless'] ) ) {
@@ -3645,13 +3628,13 @@
 	 * @param array  $metadata       Filtered Video metadata.
 	 * @param string $file           Path to video file.
 	 * @param string $file_format    File format of video, as analyzed by getID3.
-	 * @param string $data           Raw metadata from getID3.
+	 * @param array  $data           Raw metadata from getID3.
 	 */
 	return apply_filters( 'wp_read_video_metadata', $metadata, $file, $file_format, $data );
 }
 
 /**
- * Retrieve metadata from an audio file's ID3 tags.
+ * Retrieves metadata from an audio file's ID3 tags.
  *
  * @since 3.6.0
  *
@@ -3673,7 +3656,10 @@
 		require ABSPATH . WPINC . '/ID3/getid3.php';
 	}
 
-	$id3  = new getID3();
+	$id3 = new getID3();
+	// Required to get the `created_timestamp` value.
+	$id3->options_audiovideo_quicktime_ReturnAtomData = true; // phpcs:ignore WordPress.NamingConventions.ValidVariableName
+
 	$data = $id3->analyze( $file );
 
 	if ( ! empty( $data['audio'] ) ) {
@@ -3715,7 +3701,7 @@
 }
 
 /**
- * Parse creation date from media metadata.
+ * Parses creation date from media metadata.
  *
  * The getID3 library doesn't have a standard method for getting creation dates,
  * so the location of this data can vary based on the MIME type.
@@ -3744,17 +3730,17 @@
 
 		case 'matroska':
 		case 'webm':
-			if ( isset( $metadata['matroska']['comments']['creation_time']['0'] ) ) {
-				$creation_date = strtotime( $metadata['matroska']['comments']['creation_time']['0'] );
-			} elseif ( isset( $metadata['matroska']['info']['0']['DateUTC_unix'] ) ) {
-				$creation_date = (int) $metadata['matroska']['info']['0']['DateUTC_unix'];
+			if ( isset( $metadata['matroska']['comments']['creation_time'][0] ) ) {
+				$creation_date = strtotime( $metadata['matroska']['comments']['creation_time'][0] );
+			} elseif ( isset( $metadata['matroska']['info'][0]['DateUTC_unix'] ) ) {
+				$creation_date = (int) $metadata['matroska']['info'][0]['DateUTC_unix'];
 			}
 			break;
 
 		case 'quicktime':
 		case 'mp4':
-			if ( isset( $metadata['quicktime']['moov']['subatoms']['0']['creation_time_unix'] ) ) {
-				$creation_date = (int) $metadata['quicktime']['moov']['subatoms']['0']['creation_time_unix'];
+			if ( isset( $metadata['quicktime']['moov']['subatoms'][0]['creation_time_unix'] ) ) {
+				$creation_date = (int) $metadata['quicktime']['moov']['subatoms'][0]['creation_time_unix'];
 			}
 			break;
 	}