Index: wp-admin/includes/misc.php
===================================================================
--- wp-admin/includes/misc.php	(revision 29750)
+++ wp-admin/includes/misc.php	(working copy)
@@ -100,46 +100,89 @@
  * @return bool True on write success, false on failure.
  */
 function insert_with_markers( $filename, $marker, $insertion ) {
-	if (!file_exists( $filename ) || is_writeable( $filename ) ) {
-		if (!file_exists( $filename ) ) {
+	// if we can write to the speified file
+	if ( ! file_exists( $filename ) || is_writeable( $filename ) ) {
+		// go ahead and add the markers to the insertion
+		$insertion = array_merge( array( '# BEGIN ' . $marker ), $insertion, array( '# END ' . $marker ) );
+
+		// load any existing file into an array for parsing below
+		if ( ! file_exists( $filename ) ) {
 			$markerdata = '';
 		} else {
-			$markerdata = explode( "\n", implode( '', file( $filename ) ) );
+			$markerdata = implode( '', file( $filename ) );
+			// handle empty file
+			$markerdata = $markerdata ? explode( "\n", $markerdata ) : '';
 		}
 
-		if ( !$f = @fopen( $filename, 'w' ) )
-			return false;
+		$output = '';
 
-		$foundit = false;
+		// if we actually have an existing file with lines in it, search the file for our insertion
+		// otherwise, just add the insertion as the ony thing to write
 		if ( $markerdata ) {
+			// cleanup the output for better match testing; array_values() used here for array comparison below
+			$sane_insertion = array_values( array_filter( array_map( 'trim', $insertion ) ) );
+
+			// track all lines in a possibly matching block
+			$possible = array();
+
+			// track whether we are currently adding to our output array or not
 			$state = true;
-			foreach ( $markerdata as $n => $markerline ) {
-				if (strpos($markerline, '# BEGIN ' . $marker) !== false)
+			$added_already = false;
+
+			// start searching the lines for a possible matching insertion
+			while ( ( $line = array_shift( $markerdata ) ) !== null ) {
+				// if we have a marker that matches our new marker, then we found what was previously written,
+				// so stop adding to the output array, and start tracking lines for later comparison
+				if ( strpos( $line, '# BEGIN ' . $marker ) !== false )
 					$state = false;
+
 				if ( $state ) {
-					if ( $n + 1 < count( $markerdata ) )
-						fwrite( $f, "{$markerline}\n" );
-					else
-						fwrite( $f, "{$markerline}" );
+					$output = $output . $line . "\n";
+				} else {
+					$possible[] = $line;
 				}
-				if (strpos($markerline, '# END ' . $marker) !== false) {
-					fwrite( $f, "# BEGIN {$marker}\n" );
-					if ( is_array( $insertion ))
-						foreach ( $insertion as $insertline )
-							fwrite( $f, "{$insertline}\n" );
-					fwrite( $f, "# END {$marker}\n" );
+
+				// if we are at the ending marker line, then we need to compare
+				if ( strpos( $line, '# END ' . $marker ) !== false ) {
+					// clean up the possible list for comparison to the insertion
+					$possible = array_values( array_filter( array_map( 'trim', $possible ) ) );
+
+					// if we found out block, then just quit with a success, becuase nothing needs to be done
+					// otherwise, ignore this block, and keep searching in case there are multiple blocks
+					// techinically this otherwise should not matter, but good to be safe
+					if ( ! $added_already && $possible === $sane_insertion ) {
+						return true;
+					} else {
+						$possible = array();
+
+						// prevent writing two of these blocks
+						if ( ! $added_already ) {
+							$output = $output . implode( "\n", $insertion ) . "\n";
+							$added_already = true;
+						}
+					}
+
 					$state = true;
-					$foundit = true;
 				}
 			}
+		} else {
+			$output = implode( "\n", $insertion );
 		}
-		if (!$foundit) {
-			fwrite( $f, "\n# BEGIN {$marker}\n" );
-			foreach ( $insertion as $insertline )
-				fwrite( $f, "{$insertline}\n" );
-			fwrite( $f, "# END {$marker}\n" );
-		}
+
+		$output = trim( $output );
+		$out_len = strlen( $output );
+
+		// if we made it this far, then it is time for writing, so open the output file for writing
+		if ( !$f = @fopen( $filename, 'w' ) )
+			return false;
+
+		// write the file
+		if ( $out_len )
+			fwrite( $f, $output, $out_len );
+
+		// close the file
 		fclose( $f );
+
 		return true;
 	} else {
 		return false;
