Index: wp-includes/post.php
===================================================================
--- wp-includes/post.php	(revision 20426)
+++ wp-includes/post.php	(working copy)
@@ -2034,20 +2034,12 @@
 	$wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'attachment' ) );
 
 	$comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $postid ));
-	if ( ! empty($comment_ids) ) {
-		do_action( 'delete_comment', $comment_ids );
-		foreach ( $comment_ids as $comment_id )
-			wp_delete_comment( $comment_id, true );
-		do_action( 'deleted_comment', $comment_ids );
-	}
+	foreach ( $comment_ids as $comment_id )
+		wp_delete_comment( $comment_id, true );
 
 	$post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $postid ));
-	if ( !empty($post_meta_ids) ) {
-		do_action( 'delete_postmeta', $post_meta_ids );
-		$in_post_meta_ids = "'" . implode("', '", $post_meta_ids) . "'";
-		$wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_id IN($in_post_meta_ids)" );
-		do_action( 'deleted_postmeta', $post_meta_ids );
-	}
+	foreach ( $post_meta_ids as $mid )
+		delete_metadata_by_id( 'post', $mid );
 
 	do_action( 'delete_post', $postid );
 	$wpdb->delete( $wpdb->posts, array( 'ID' => $postid ) );
@@ -3031,22 +3023,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 ) {
+		$value = 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 );
 }
 
 /**
@@ -3800,23 +3790,15 @@
 	wp_delete_object_term_relationships($post_id, array('category', 'post_tag'));
 	wp_delete_object_term_relationships($post_id, get_object_taxonomies($post->post_type));
 
-	$wpdb->delete( $wpdb->postmeta, array( 'meta_key' => '_thumbnail_id' , 'meta_value' => $post_id ) );
+	delete_metadata( 'post', null, '_thumbnail_id', $post_id, true ); // delete all for any posts.
 
 	$comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id ));
-	if ( ! empty( $comment_ids ) ) {
-		do_action( 'delete_comment', $comment_ids );
-		foreach ( $comment_ids as $comment_id )
-			wp_delete_comment( $comment_id, true );
-		do_action( 'deleted_comment', $comment_ids );
-	}
+	foreach ( $comment_ids as $comment_id )
+		wp_delete_comment( $comment_id, true );
 
 	$post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $post_id ));
-	if ( !empty($post_meta_ids) ) {
-		do_action( 'delete_postmeta', $post_meta_ids );
-		$in_post_meta_ids = "'" . implode("', '", $post_meta_ids) . "'";
-		$wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_id IN($in_post_meta_ids)" );
-		do_action( 'deleted_postmeta', $post_meta_ids );
-	}
+	foreach ( $post_meta_ids as $mid )
+		delete_metadata_by_id( 'post', $mid );
 
 	do_action( 'delete_post', $post_id );
 	$wpdb->delete( $wpdb->posts, array( 'ID' => $post_id ) );
@@ -4564,12 +4546,9 @@
 		return;
 
 	$data = array( 'post_id' => $post_id, 'meta_value' => '1' );
-	if ( get_option('default_pingback_flag') ) {
-		$wpdb->insert( $wpdb->postmeta, $data + array( 'meta_key' => '_pingme' ) );
-		do_action( 'added_postmeta', $wpdb->insert_id, $post_id, '_pingme', 1 );
-	}
-	$wpdb->insert( $wpdb->postmeta, $data + array( 'meta_key' => '_encloseme' ) );
-	do_action( 'added_postmeta', $wpdb->insert_id, $post_id, '_encloseme', 1 );
+	if ( get_option('default_pingback_flag') )
+		add_post_meta( $post_id, '_pingme', '1' );
+	add_post_meta( $post_id, '_encloseme', '1' );
 
 	wp_schedule_single_event(time(), 'do_pings');
 }
Index: wp-includes/comment.php
===================================================================
--- wp-includes/comment.php	(revision 20426)
+++ wp-includes/comment.php	(working copy)
@@ -980,13 +980,9 @@
 	}
 
 	// Delete metadata
-	$meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->commentmeta WHERE comment_id = %d ", $comment_id ) );
-	if ( !empty($meta_ids) ) {
-		do_action( 'delete_commentmeta', $meta_ids );
-		$in_meta_ids = "'" . implode("', '", $meta_ids) . "'";
-		$wpdb->query( "DELETE FROM $wpdb->commentmeta WHERE meta_id IN ($in_meta_ids)" );
-		do_action( 'deleted_commentmeta', $meta_ids );
-	}
+	$meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->commentmeta WHERE comment_id = %d", $comment_id ) );
+	foreach ( $meta_ids as $mid )
+		delete_metadata_by_id( 'comment', $mid );
 
 	if ( ! $wpdb->delete( $wpdb->comments, array( 'comment_ID' => $comment_id ) ) )
 		return false;
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,58 @@
 	$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] );
+	if ( $removed_links ) {
+		$enclosures_with_mids = $wpdb->get_results( $wpdb->prepare( "SELECT meta_id, meta_value FROM $wpdb->postmeta WHERE post_id = %d and meta_key = %s", $post_ID, 'enclosure' ) );
+		foreach ( $enclosures_with_mids as $enclosure ) {
+			list( $link ) = explode( "\n", $enclosure->meta_value ); // Get first line.
+			// The trim is a necessary standardization because get_enclosed() has trimmed URLs forever.
+			if ( in_array( trim( $link ), $removed_links ) )
+				delete_metadata_by_mid( 'post', $enclosure->meta_id );
 		}
 	}
 
-	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" );
 	}
 }
 
Index: wp-includes/meta.php
===================================================================
--- wp-includes/meta.php	(revision 20426)
+++ wp-includes/meta.php	(working copy)
@@ -223,6 +223,7 @@
 
 	do_action( "delete_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
 
+	// Old-style action.
 	if ( 'post' == $meta_type )
 		do_action( 'delete_postmeta', $meta_ids );
 
@@ -243,6 +244,7 @@
 
 	do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
 
+	// Old-style action.
 	if ( 'post' == $meta_type )
 		do_action( 'deleted_postmeta', $meta_ids );
 
@@ -485,8 +487,9 @@
 
 		do_action( "delete_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );
 
-		if ( 'post' == $meta_type )
-			do_action( 'delete_postmeta', $meta_id );
+		// Old-style action.
+		if ( 'post' == $meta_type || 'comment' == $meta_type )
+			do_action( "delete_{$meta_type}meta", $meta_id );
 
 		// Run the query, will return true if deleted, false otherwise
 		$result = (bool) $wpdb->delete( $table, array( $id_column => $meta_id ) );
@@ -496,8 +499,9 @@
 
 		do_action( "deleted_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );
 
-		if ( 'post' == $meta_type )
-			do_action( 'deleted_postmeta', $meta_id );
+		// Old-style action.
+		if ( 'post' == $meta_type || 'comment' == $meta_type )
+			do_action( "deleted_{$meta_type}meta", $meta_id );
 
 		return $result;
 
