Index: wp-includes/post.php
===================================================================
--- wp-includes/post.php	(revision 20426)
+++ wp-includes/post.php	(working copy)
@@ -3031,22 +3031,20 @@
  * @param int $post_id Post ID.
  * @return array List of enclosures
  */
-function get_enclosed($post_id) {
-	$custom_fields = get_post_custom( $post_id );
+function get_enclosed( $post_id ) {
+	$enclosures = get_post_meta( $post_id, 'enclosure' );
+
+	if ( empty( $enclosures ) )
+		return array();
+
 	$pung = array();
-	if ( !is_array( $custom_fields ) )
-		return $pung;
 
-	foreach ( $custom_fields as $key => $val ) {
-		if ( 'enclosure' != $key || !is_array( $val ) )
-			continue;
-		foreach( $val as $enc ) {
-			$enclosure = explode( "\n", $enc );
-			$pung[] = trim( $enclosure[ 0 ] );
-		}
+	foreach ( $enclosures as $value ) {
+		$enclosure = explode( "\n", $value );
+		$pung[] = trim( $value[0] );
 	}
-	$pung = apply_filters('get_enclosed', $pung, $post_id);
-	return $pung;
+
+	return apply_filters( 'get_enclosed', $pung, $post_id );
 }
 
 /**
Index: wp-includes/functions.php
===================================================================
--- wp-includes/functions.php	(revision 20426)
+++ wp-includes/functions.php	(working copy)
@@ -410,9 +410,6 @@
 function do_enclose( $content, $post_ID ) {
 	global $wpdb;
 
-	//TODO: Tidy this ghetto code up and make the debug code optional
-	include_once( ABSPATH . WPINC . '/class-IXR.php' );
-
 	$post_links = array();
 
 	$pung = get_enclosed( $post_ID );
@@ -422,59 +419,54 @@
 	$punc = '.:?\-';
 	$any = $ltrs . $gunk . $punc;
 
-	preg_match_all( "{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp );
+	preg_match_all( "{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links );
 
-	foreach ( $pung as $link_test ) {
-		if ( !in_array( $link_test, $post_links_temp[0] ) ) { // link no longer in post
-			$mid = $wpdb->get_col( $wpdb->prepare("SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)", $post_ID, like_escape( $link_test ) . '%') );
-			do_action( 'delete_postmeta', $mid );
-			$wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_id IN(%s)", implode( ',', $mid ) ) );
-			do_action( 'deleted_postmeta', $mid );
-		}
+	// Remove enclosures that are no longer linked in the post.
+	$removed_links = array_diff( $pung, $post_links[0] );
+	foreach ( $removed_links as $link ) {
+			$mids = $wpdb->get_col( $wpdb->prepare("SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)", $post_ID, like_escape( $link ) . '%') );
+			foreach ( $mids as $mid )
+				delete_metadata_by_mid( 'post', $mid );
 	}
 
-	foreach ( (array) $post_links_temp[0] as $link_test ) {
-		if ( !in_array( $link_test, $pung ) ) { // If we haven't pung it already
-			$test = @parse_url( $link_test );
-			if ( false === $test )
-				continue;
-			if ( isset( $test['query'] ) )
-				$post_links[] = $link_test;
-			elseif ( isset($test['path']) && ( $test['path'] != '/' ) &&  ($test['path'] != '' ) )
-				$post_links[] = $link_test;
-		}
+	$new_links = array();
+
+	// Find enclosures that are new to the post.
+	$added_links = array_diff( $post_links[0], $pung );
+	foreach ( $added_links as $link ) {
+		$test = @parse_url( $link );
+		if ( false === $test )
+			continue;
+		if ( ! empty( $test['query'] ) )
+			$new_links[] = $link;
+		elseif ( ! empty( $test['path'] ) && '/' != $test['path'] )
+			$new_links[] = $link;
 	}
 
-	foreach ( (array) $post_links as $url ) {
-		if ( $url != '' && !$wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)", $post_ID, like_escape( $url ) . '%' ) ) ) {
+	$new_links = array_unique( $new_links );
 
-			if ( $headers = wp_get_http_headers( $url) ) {
-				$len = isset( $headers['content-length'] ) ? (int) $headers['content-length'] : 0;
-				$type = isset( $headers['content-type'] ) ? $headers['content-type'] : '';
-				$allowed_types = array( 'video', 'audio' );
+	foreach ( $new_links as $url ) {
+		if ( ! $headers = wp_get_http_headers( $url ) )
+			continue;
 
-				// Check to see if we can figure out the mime type from
-				// the extension
-				$url_parts = @parse_url( $url );
-				if ( false !== $url_parts ) {
-					$extension = pathinfo( $url_parts['path'], PATHINFO_EXTENSION );
-					if ( !empty( $extension ) ) {
-						foreach ( get_allowed_mime_types( ) as $exts => $mime ) {
-							if ( preg_match( '!^(' . $exts . ')$!i', $extension ) ) {
-								$type = $mime;
-								break;
-							}
-						}
-					}
-				}
+		$len = isset( $headers['content-length'] ) ? (int) $headers['content-length'] : 0;
+		$type = isset( $headers['content-type'] ) ? $headers['content-type'] : '';
 
-				if ( in_array( substr( $type, 0, strpos( $type, "/" ) ), $allowed_types ) ) {
-					$meta_value = "$url\n$len\n$type\n";
-					$wpdb->insert($wpdb->postmeta, array('post_id' => $post_ID, 'meta_key' => 'enclosure', 'meta_value' => $meta_value) );
-					do_action( 'added_postmeta', $wpdb->insert_id, $post_ID, 'enclosure', $meta_value );
+		// Check to see if we can figure out the mime type from the extension
+		$url_parts = @parse_url( $url );
+		if ( false !== $url_parts ) {
+			$extension = pathinfo( $url_parts['path'], PATHINFO_EXTENSION );
+			if ( !empty( $extension ) ) {
+				foreach ( get_allowed_mime_types() as $exts => $mime ) {
+					if ( preg_match( '!^(' . $exts . ')$!i', $extension ) )
+						break;
 				}
 			}
 		}
+
+		list( $type ) = explode( '/', $mime ); // Get first segment.
+		if ( 'video' == $type || 'audio' == $type )
+			add_post_meta( $post_ID, 'enclosure', "$url\n$len\n$mime\n" );
 	}
 }
 
