+/**
+ * Parse 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
+ */
+function wp_add_id3_tag_data( &$metadata, $data ) {
+ foreach ( array( 'id3v2', 'id3v1' ) as $version ) {
+ if ( ! empty( $data[$version]['comments'] ) ) {
+ foreach ( $data[$version]['comments'] as $key => $list ) {
+ if ( 'length' !== $key && ! empty( $list ) ) {
+ $metadata[$key] = reset( $list );
+ // Fix bug in byte stream analysis.
+ if ( 'terms_of_use' === $key && 0 === strpos( $metadata[$key], 'yright notice.' ) )
+ $metadata[$key] = 'Cop' . $metadata[$key];
+ }
+ }
+ break;
+ }
+ }
+
+ if ( ! empty( $data['id3v2']['APIC'] ) ) {
+ $image = reset( $data['id3v2']['APIC']);
+ if ( ! empty( $image['data'] ) ) {
+ $metadata['image'] = array(
+ 'data' => $image['data'],
+ 'mime' => $image['image_mime'],
+ 'width' => $image['image_width'],
+ 'height' => $image['image_height']
+ );
+ }
+ } elseif ( ! empty( $data['comments']['picture'] ) ) {
+ $image = reset( $data['comments']['picture'] );
+ if ( ! empty( $image['data'] ) ) {
+ $metadata['image'] = array(
+ 'data' => $image['data'],
+ 'mime' => $image['image_mime']
+ );
+ }
+ }
+}
+
+/**
+ * Retrieve metadata from a video file's ID3 tags
+ *
+ * @since 3.6.0
+ *
+ * @param string $file Path to file.
+ * @return array|bool Returns array of metadata, if found.
+ */
+function wp_read_video_metadata( $file ) {
+ if ( ! file_exists( $file ) ) {
+ return false;
+ }
+
+ $metadata = array();
+
+ if ( ! defined( 'GETID3_TEMP_DIR' ) ) {
+ define( 'GETID3_TEMP_DIR', get_temp_dir() );
+ }
+
+ if ( ! class_exists( 'getID3', false ) ) {
+ require( ABSPATH . WPINC . '/ID3/getid3.php' );
+ }
+ $id3 = new getID3();
+ $data = $id3->analyze( $file );
+
+ if ( isset( $data['video']['lossless'] ) )
+ $metadata['lossless'] = $data['video']['lossless'];
+ if ( ! empty( $data['video']['bitrate'] ) )
+ $metadata['bitrate'] = (int) $data['video']['bitrate'];
+ if ( ! empty( $data['video']['bitrate_mode'] ) )
+ $metadata['bitrate_mode'] = $data['video']['bitrate_mode'];
+ if ( ! empty( $data['filesize'] ) )
+ $metadata['filesize'] = (int) $data['filesize'];
+ if ( ! empty( $data['mime_type'] ) )
+ $metadata['mime_type'] = $data['mime_type'];
+ if ( ! empty( $data['playtime_seconds'] ) )
+ $metadata['length'] = (int) round( $data['playtime_seconds'] );
+ if ( ! empty( $data['playtime_string'] ) )
+ $metadata['length_formatted'] = $data['playtime_string'];
+ if ( ! empty( $data['video']['resolution_x'] ) )
+ $metadata['width'] = (int) $data['video']['resolution_x'];
+ if ( ! empty( $data['video']['resolution_y'] ) )
+ $metadata['height'] = (int) $data['video']['resolution_y'];
+ if ( ! empty( $data['fileformat'] ) )
+ $metadata['fileformat'] = $data['fileformat'];
+ if ( ! empty( $data['video']['dataformat'] ) )
+ $metadata['dataformat'] = $data['video']['dataformat'];
+ if ( ! empty( $data['video']['encoder'] ) )
+ $metadata['encoder'] = $data['video']['encoder'];
+ if ( ! empty( $data['video']['codec'] ) )
+ $metadata['codec'] = $data['video']['codec'];
+
+ if ( ! empty( $data['audio'] ) ) {
+ unset( $data['audio']['streams'] );
+ $metadata['audio'] = $data['audio'];
+ }
+
+ wp_add_id3_tag_data( $metadata, $data );
+
+ return $metadata;
+}
+
+/**
+ * Retrieve metadata from a audio file's ID3 tags
+ *
+ * @since 3.6.0
+ *
+ * @param string $file Path to file.
+ * @return array|bool Returns array of metadata, if found.
+ */
+function wp_read_audio_metadata( $file ) {
+ if ( ! file_exists( $file ) ) {
+ return false;
+ }
+ $metadata = array();
+
+ if ( ! defined( 'GETID3_TEMP_DIR' ) ) {
+ define( 'GETID3_TEMP_DIR', get_temp_dir() );
+ }
+
+ if ( ! class_exists( 'getID3', false ) ) {
+ require( ABSPATH . WPINC . '/ID3/getid3.php' );
+ }
+ $id3 = new getID3();
+ $data = $id3->analyze( $file );
+
+ if ( ! empty( $data['audio'] ) ) {
+ unset( $data['audio']['streams'] );
+ $metadata = $data['audio'];
+ }
+
+ if ( ! empty( $data['fileformat'] ) )
+ $metadata['fileformat'] = $data['fileformat'];
+ if ( ! empty( $data['filesize'] ) )
+ $metadata['filesize'] = (int) $data['filesize'];
+ if ( ! empty( $data['mime_type'] ) )
+ $metadata['mime_type'] = $data['mime_type'];
+ if ( ! empty( $data['playtime_seconds'] ) )
+ $metadata['length'] = (int) round( $data['playtime_seconds'] );
+ if ( ! empty( $data['playtime_string'] ) )
+ $metadata['length_formatted'] = $data['playtime_string'];
+
+ wp_add_id3_tag_data( $metadata, $data );
+
+ return $metadata;
+}
+
+/**
+ * Encapsulate logic for Attach/Detach actions
+ *
+ * @since 4.2.0
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
+ * @param int $parent_id Attachment parent ID.
+ * @param string $action Optional. Attach/detach action. Accepts 'attach' or 'detach'.
+ * Default 'attach'.
+ */
+function wp_media_attach_action( $parent_id, $action = 'attach' ) {
+ global $wpdb;